blob: 0dba09334a163ab9222c22335fc22f46cfbcd5da [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Masone02119d2008-09-05 16:13:11 -04002/*
3 * Copyright (C) 2008 Oracle. All rights reserved.
Chris Masone02119d2008-09-05 16:13:11 -04004 */
5
6#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Miao Xiec6adc9c2013-05-28 10:05:39 +00008#include <linux/blkdev.h>
Josef Bacik5dc562c2012-08-17 13:14:17 -04009#include <linux/list_sort.h>
Jeff Laytonc7f88c42017-12-11 06:35:12 -050010#include <linux/iversion.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020011#include "ctree.h"
Miao Xie995946d2014-04-02 19:51:06 +080012#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040013#include "disk-io.h"
14#include "locking.h"
15#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070016#include "backref.h"
Anand Jainebb87652016-03-10 17:26:59 +080017#include "compression.h"
Qu Wenruodf2c95f2016-08-15 10:36:52 +080018#include "qgroup.h"
Liu Bo900c9982018-01-25 11:02:56 -070019#include "inode-map.h"
Chris Masone02119d2008-09-05 16:13:11 -040020
21/* magic values for the inode_only field in btrfs_log_inode:
22 *
23 * LOG_INODE_ALL means to log everything
24 * LOG_INODE_EXISTS means to log just enough to recreate the inode
25 * during log replay
26 */
27#define LOG_INODE_ALL 0
28#define LOG_INODE_EXISTS 1
Liu Bo781feef2016-11-30 16:20:25 -080029#define LOG_OTHER_INODE 2
Chris Masone02119d2008-09-05 16:13:11 -040030
31/*
Chris Mason12fcfd22009-03-24 10:24:20 -040032 * directory trouble cases
33 *
34 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
35 * log, we must force a full commit before doing an fsync of the directory
36 * where the unlink was done.
37 * ---> record transid of last unlink/rename per directory
38 *
39 * mkdir foo/some_dir
40 * normal commit
41 * rename foo/some_dir foo2/some_dir
42 * mkdir foo/some_dir
43 * fsync foo/some_dir/some_file
44 *
45 * The fsync above will unlink the original some_dir without recording
46 * it in its new location (foo2). After a crash, some_dir will be gone
47 * unless the fsync of some_file forces a full commit
48 *
49 * 2) we must log any new names for any file or dir that is in the fsync
50 * log. ---> check inode while renaming/linking.
51 *
52 * 2a) we must log any new names for any file or dir during rename
53 * when the directory they are being removed from was logged.
54 * ---> check inode and old parent dir during rename
55 *
56 * 2a is actually the more important variant. With the extra logging
57 * a crash might unlink the old name without recreating the new one
58 *
59 * 3) after a crash, we must go through any directories with a link count
60 * of zero and redo the rm -rf
61 *
62 * mkdir f1/foo
63 * normal commit
64 * rm -rf f1/foo
65 * fsync(f1)
66 *
67 * The directory f1 was fully removed from the FS, but fsync was never
68 * called on f1, only its parent dir. After a crash the rm -rf must
69 * be replayed. This must be able to recurse down the entire
70 * directory tree. The inode link count fixup code takes care of the
71 * ugly details.
72 */
73
74/*
Chris Masone02119d2008-09-05 16:13:11 -040075 * stages for the tree walking. The first
76 * stage (0) is to only pin down the blocks we find
77 * the second stage (1) is to make sure that all the inodes
78 * we find in the log are created in the subvolume.
79 *
80 * The last stage is to deal with directories and links and extents
81 * and all the other fun semantics
82 */
83#define LOG_WALK_PIN_ONLY 0
84#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040085#define LOG_WALK_REPLAY_DIR_INDEX 2
86#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040087
Chris Mason12fcfd22009-03-24 10:24:20 -040088static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +020089 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +010090 int inode_only,
91 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +010092 const loff_t end,
93 struct btrfs_log_ctx *ctx);
Yan Zhengec051c02009-01-05 15:43:42 -050094static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
95 struct btrfs_root *root,
96 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -040097static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root,
99 struct btrfs_root *log,
100 struct btrfs_path *path,
101 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400102
103/*
104 * tree logging is a special write ahead log used to make sure that
105 * fsyncs and O_SYNCs can happen without doing full tree commits.
106 *
107 * Full tree commits are expensive because they require commonly
108 * modified blocks to be recowed, creating many dirty pages in the
109 * extent tree an 4x-6x higher write load than ext3.
110 *
111 * Instead of doing a tree commit on every fsync, we use the
112 * key ranges and transaction ids to find items for a given file or directory
113 * that have changed in this transaction. Those items are copied into
114 * a special tree (one per subvolume root), that tree is written to disk
115 * and then the fsync is considered complete.
116 *
117 * After a crash, items are copied out of the log-tree back into the
118 * subvolume tree. Any file data extents found are recorded in the extent
119 * allocation tree, and the log-tree freed.
120 *
121 * The log tree is read three times, once to pin down all the extents it is
122 * using in ram and once, once to create all the inodes logged in the tree
123 * and once to do all the other items.
124 */
125
126/*
Chris Masone02119d2008-09-05 16:13:11 -0400127 * start a sub transaction and setup the log tree
128 * this increments the log tree writer count to make the people
129 * syncing the tree wait for us to finish
130 */
131static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800132 struct btrfs_root *root,
133 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400134{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400135 struct btrfs_fs_info *fs_info = root->fs_info;
Zhaolei34eb2a52015-08-17 18:44:45 +0800136 int ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500137
138 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800139
Yan Zheng7237f182009-01-21 12:54:03 -0500140 if (root->log_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400141 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800142 ret = -EAGAIN;
143 goto out;
144 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800145
Josef Bacikff782e02009-10-08 15:30:04 -0400146 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800147 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800148 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400149 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800150 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400151 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800152 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400153 mutex_lock(&fs_info->tree_log_mutex);
154 if (!fs_info->log_root_tree)
155 ret = btrfs_init_log_root_tree(trans, fs_info);
156 mutex_unlock(&fs_info->tree_log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800157 if (ret)
158 goto out;
Josef Bacikff782e02009-10-08 15:30:04 -0400159
Chris Masone02119d2008-09-05 16:13:11 -0400160 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400161 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800162 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800163
164 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
165 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400166 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800167
Miao Xie2ecb7922012-09-06 04:04:27 -0600168 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500169 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800170 if (ctx) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800171 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800172 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800173 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800174 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800175
Miao Xiee87ac132014-02-20 18:08:53 +0800176out:
Yan Zheng7237f182009-01-21 12:54:03 -0500177 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800178 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400179}
180
181/*
182 * returns 0 if there was a log transaction running and we were able
183 * to join, or returns -ENOENT if there were not transactions
184 * in progress
185 */
186static int join_running_log_trans(struct btrfs_root *root)
187{
188 int ret = -ENOENT;
189
190 smp_mb();
191 if (!root->log_root)
192 return -ENOENT;
193
Yan Zheng7237f182009-01-21 12:54:03 -0500194 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400195 if (root->log_root) {
196 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500197 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400198 }
Yan Zheng7237f182009-01-21 12:54:03 -0500199 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400200 return ret;
201}
202
203/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400204 * This either makes the current running log transaction wait
205 * until you call btrfs_end_log_trans() or it makes any future
206 * log transactions wait until you call btrfs_end_log_trans()
207 */
zhong jiang45128b02018-08-17 00:37:15 +0800208void btrfs_pin_log_trans(struct btrfs_root *root)
Chris Mason12fcfd22009-03-24 10:24:20 -0400209{
Chris Mason12fcfd22009-03-24 10:24:20 -0400210 mutex_lock(&root->log_mutex);
211 atomic_inc(&root->log_writers);
212 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -0400213}
214
215/*
Chris Masone02119d2008-09-05 16:13:11 -0400216 * indicate we're done making changes to the log tree
217 * and wake up anyone waiting to do a sync
218 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100219void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400220{
Yan Zheng7237f182009-01-21 12:54:03 -0500221 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba093258e2018-02-26 16:15:17 +0100222 /* atomic_dec_and_test implies a barrier */
223 cond_wake_up_nomb(&root->log_writer_wait);
Yan Zheng7237f182009-01-21 12:54:03 -0500224 }
Chris Masone02119d2008-09-05 16:13:11 -0400225}
226
227
228/*
229 * the walk control struct is used to pass state down the chain when
230 * processing the log tree. The stage field tells us which part
231 * of the log tree processing we are currently doing. The others
232 * are state fields used for that specific part
233 */
234struct walk_control {
235 /* should we free the extent on disk when done? This is used
236 * at transaction commit time while freeing a log tree
237 */
238 int free;
239
240 /* should we write out the extent buffer? This is used
241 * while flushing the log tree to disk during a sync
242 */
243 int write;
244
245 /* should we wait for the extent buffer io to finish? Also used
246 * while flushing the log tree to disk for a sync
247 */
248 int wait;
249
250 /* pin only walk, we record which extents on disk belong to the
251 * log trees
252 */
253 int pin;
254
255 /* what stage of the replay code we're currently in */
256 int stage;
257
Filipe Mananaf2d72f42018-10-08 11:12:55 +0100258 /*
259 * Ignore any items from the inode currently being processed. Needs
260 * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in
261 * the LOG_WALK_REPLAY_INODES stage.
262 */
263 bool ignore_cur_inode;
264
Chris Masone02119d2008-09-05 16:13:11 -0400265 /* the root we are currently replaying */
266 struct btrfs_root *replay_dest;
267
268 /* the trans handle for the current replay */
269 struct btrfs_trans_handle *trans;
270
271 /* the function that gets used to process blocks we find in the
272 * tree. Note the extent_buffer might not be up to date when it is
273 * passed in, and it must be checked or read if you need the data
274 * inside it
275 */
276 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800277 struct walk_control *wc, u64 gen, int level);
Chris Masone02119d2008-09-05 16:13:11 -0400278};
279
280/*
281 * process_func used to pin down extents, write them or wait on them
282 */
283static int process_one_buffer(struct btrfs_root *log,
284 struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800285 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -0400286{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400287 struct btrfs_fs_info *fs_info = log->fs_info;
Josef Bacikb50c6e22013-04-25 15:55:30 -0400288 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400289
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400290 /*
291 * If this fs is mixed then we need to be able to process the leaves to
292 * pin down any logged extents, so we have to read the block.
293 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400294 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Qu Wenruo581c1762018-03-29 09:08:11 +0800295 ret = btrfs_read_buffer(eb, gen, level, NULL);
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400296 if (ret)
297 return ret;
298 }
299
Josef Bacikb50c6e22013-04-25 15:55:30 -0400300 if (wc->pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400301 ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
302 eb->len);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400303
304 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400305 if (wc->pin && btrfs_header_level(eb) == 0)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400306 ret = btrfs_exclude_logged_extents(fs_info, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400307 if (wc->write)
308 btrfs_write_tree_block(eb);
309 if (wc->wait)
310 btrfs_wait_tree_block_writeback(eb);
311 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400312 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400313}
314
315/*
316 * Item overwrite used by replay and tree logging. eb, slot and key all refer
317 * to the src data we are copying out.
318 *
319 * root is the tree we are copying into, and path is a scratch
320 * path for use in this function (it should be released on entry and
321 * will be released on exit).
322 *
323 * If the key is already in the destination tree the existing item is
324 * overwritten. If the existing item isn't big enough, it is extended.
325 * If it is too large, it is truncated.
326 *
327 * If the key isn't in the destination yet, a new item is inserted.
328 */
329static noinline int overwrite_item(struct btrfs_trans_handle *trans,
330 struct btrfs_root *root,
331 struct btrfs_path *path,
332 struct extent_buffer *eb, int slot,
333 struct btrfs_key *key)
334{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400335 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400336 int ret;
337 u32 item_size;
338 u64 saved_i_size = 0;
339 int save_old_i_size = 0;
340 unsigned long src_ptr;
341 unsigned long dst_ptr;
342 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000343 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400344
345 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
346 overwrite_root = 1;
347
348 item_size = btrfs_item_size_nr(eb, slot);
349 src_ptr = btrfs_item_ptr_offset(eb, slot);
350
351 /* look for the key in the destination tree */
352 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000353 if (ret < 0)
354 return ret;
355
Chris Masone02119d2008-09-05 16:13:11 -0400356 if (ret == 0) {
357 char *src_copy;
358 char *dst_copy;
359 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
360 path->slots[0]);
361 if (dst_size != item_size)
362 goto insert;
363
364 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200365 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400366 return 0;
367 }
368 dst_copy = kmalloc(item_size, GFP_NOFS);
369 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000370 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200371 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000372 kfree(dst_copy);
373 kfree(src_copy);
374 return -ENOMEM;
375 }
Chris Masone02119d2008-09-05 16:13:11 -0400376
377 read_extent_buffer(eb, src_copy, src_ptr, item_size);
378
379 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
380 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
381 item_size);
382 ret = memcmp(dst_copy, src_copy, item_size);
383
384 kfree(dst_copy);
385 kfree(src_copy);
386 /*
387 * they have the same contents, just return, this saves
388 * us from cowing blocks in the destination tree and doing
389 * extra writes that may not have been done by a previous
390 * sync
391 */
392 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200393 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400394 return 0;
395 }
396
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000397 /*
398 * We need to load the old nbytes into the inode so when we
399 * replay the extents we've logged we get the right nbytes.
400 */
401 if (inode_item) {
402 struct btrfs_inode_item *item;
403 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400404 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000405
406 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
407 struct btrfs_inode_item);
408 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
409 item = btrfs_item_ptr(eb, slot,
410 struct btrfs_inode_item);
411 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400412
413 /*
414 * If this is a directory we need to reset the i_size to
415 * 0 so that we can set it up properly when replaying
416 * the rest of the items in this log.
417 */
418 mode = btrfs_inode_mode(eb, item);
419 if (S_ISDIR(mode))
420 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000421 }
422 } else if (inode_item) {
423 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400424 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000425
426 /*
427 * New inode, set nbytes to 0 so that the nbytes comes out
428 * properly when we replay the extents.
429 */
430 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
431 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400432
433 /*
434 * If this is a directory we need to reset the i_size to 0 so
435 * that we can set it up properly when replaying the rest of
436 * the items in this log.
437 */
438 mode = btrfs_inode_mode(eb, item);
439 if (S_ISDIR(mode))
440 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400441 }
442insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200443 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400444 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000445 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400446 ret = btrfs_insert_empty_item(trans, root, path,
447 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000448 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400449
450 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000451 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400452 u32 found_size;
453 found_size = btrfs_item_size_nr(path->nodes[0],
454 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100455 if (found_size > item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400456 btrfs_truncate_item(fs_info, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100457 else if (found_size < item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400458 btrfs_extend_item(fs_info, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100459 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400460 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400461 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400462 }
463 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
464 path->slots[0]);
465
466 /* don't overwrite an existing inode if the generation number
467 * was logged as zero. This is done when the tree logging code
468 * is just logging an inode to make sure it exists after recovery.
469 *
470 * Also, don't overwrite i_size on directories during replay.
471 * log replay inserts and removes directory items based on the
472 * state of the tree found in the subvolume, and i_size is modified
473 * as it goes
474 */
475 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
476 struct btrfs_inode_item *src_item;
477 struct btrfs_inode_item *dst_item;
478
479 src_item = (struct btrfs_inode_item *)src_ptr;
480 dst_item = (struct btrfs_inode_item *)dst_ptr;
481
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000482 if (btrfs_inode_generation(eb, src_item) == 0) {
483 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000484 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000485
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000486 /*
487 * For regular files an ino_size == 0 is used only when
488 * logging that an inode exists, as part of a directory
489 * fsync, and the inode wasn't fsynced before. In this
490 * case don't set the size of the inode in the fs/subvol
491 * tree, otherwise we would be throwing valid data away.
492 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000493 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000494 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
495 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000496 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000497
498 btrfs_init_map_token(&token);
499 btrfs_set_token_inode_size(dst_eb, dst_item,
500 ino_size, &token);
501 }
Chris Masone02119d2008-09-05 16:13:11 -0400502 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000503 }
Chris Masone02119d2008-09-05 16:13:11 -0400504
505 if (overwrite_root &&
506 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
507 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
508 save_old_i_size = 1;
509 saved_i_size = btrfs_inode_size(path->nodes[0],
510 dst_item);
511 }
512 }
513
514 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
515 src_ptr, item_size);
516
517 if (save_old_i_size) {
518 struct btrfs_inode_item *dst_item;
519 dst_item = (struct btrfs_inode_item *)dst_ptr;
520 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
521 }
522
523 /* make sure the generation is filled in */
524 if (key->type == BTRFS_INODE_ITEM_KEY) {
525 struct btrfs_inode_item *dst_item;
526 dst_item = (struct btrfs_inode_item *)dst_ptr;
527 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
528 btrfs_set_inode_generation(path->nodes[0], dst_item,
529 trans->transid);
530 }
531 }
532no_copy:
533 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200534 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400535 return 0;
536}
537
538/*
539 * simple helper to read an inode off the disk from a given root
540 * This can only be called for subvolume roots and not for the log
541 */
542static noinline struct inode *read_one_inode(struct btrfs_root *root,
543 u64 objectid)
544{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400545 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400546 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400547
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400548 key.objectid = objectid;
549 key.type = BTRFS_INODE_ITEM_KEY;
550 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000551 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Al Viro2e19f1f2018-07-29 23:04:45 +0100552 if (IS_ERR(inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400553 inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -0400554 return inode;
555}
556
557/* replays a single extent in 'eb' at 'slot' with 'key' into the
558 * subvolume 'root'. path is released on entry and should be released
559 * on exit.
560 *
561 * extents in the log tree have not been allocated out of the extent
562 * tree yet. So, this completes the allocation, taking a reference
563 * as required if the extent already exists or creating a new extent
564 * if it isn't in the extent allocation tree yet.
565 *
566 * The extent is inserted into the file, dropping any existing extents
567 * from the file that overlap the new one.
568 */
569static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
570 struct btrfs_root *root,
571 struct btrfs_path *path,
572 struct extent_buffer *eb, int slot,
573 struct btrfs_key *key)
574{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400575 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400576 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400577 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400578 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000579 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400580 struct btrfs_file_extent_item *item;
581 struct inode *inode = NULL;
582 unsigned long size;
583 int ret = 0;
584
585 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
586 found_type = btrfs_file_extent_type(eb, item);
587
Yan Zhengd899e052008-10-30 14:25:28 -0400588 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000589 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
590 nbytes = btrfs_file_extent_num_bytes(eb, item);
591 extent_end = start + nbytes;
592
593 /*
594 * We don't add to the inodes nbytes if we are prealloc or a
595 * hole.
596 */
597 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
598 nbytes = 0;
599 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +0800600 size = btrfs_file_extent_ram_bytes(eb, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000601 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400602 extent_end = ALIGN(start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400603 fs_info->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400604 } else {
605 ret = 0;
606 goto out;
607 }
608
609 inode = read_one_inode(root, key->objectid);
610 if (!inode) {
611 ret = -EIO;
612 goto out;
613 }
614
615 /*
616 * first check to see if we already have this extent in the
617 * file. This must be done before the btrfs_drop_extents run
618 * so we don't try to drop this extent.
619 */
David Sterbaf85b7372017-01-20 14:54:07 +0100620 ret = btrfs_lookup_file_extent(trans, root, path,
621 btrfs_ino(BTRFS_I(inode)), start, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400622
Yan Zhengd899e052008-10-30 14:25:28 -0400623 if (ret == 0 &&
624 (found_type == BTRFS_FILE_EXTENT_REG ||
625 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400626 struct btrfs_file_extent_item cmp1;
627 struct btrfs_file_extent_item cmp2;
628 struct btrfs_file_extent_item *existing;
629 struct extent_buffer *leaf;
630
631 leaf = path->nodes[0];
632 existing = btrfs_item_ptr(leaf, path->slots[0],
633 struct btrfs_file_extent_item);
634
635 read_extent_buffer(eb, &cmp1, (unsigned long)item,
636 sizeof(cmp1));
637 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
638 sizeof(cmp2));
639
640 /*
641 * we already have a pointer to this exact extent,
642 * we don't have to do anything
643 */
644 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200645 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400646 goto out;
647 }
648 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200649 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400650
651 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400652 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400653 if (ret)
654 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400655
Yan Zheng07d400a2009-01-06 11:42:00 -0500656 if (found_type == BTRFS_FILE_EXTENT_REG ||
657 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400658 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500659 unsigned long dest_offset;
660 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400661
Filipe Manana3168021c2017-02-01 14:58:02 +0000662 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
663 btrfs_fs_incompat(fs_info, NO_HOLES))
664 goto update_inode;
665
Yan Zheng07d400a2009-01-06 11:42:00 -0500666 ret = btrfs_insert_empty_item(trans, root, path, key,
667 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400668 if (ret)
669 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500670 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
671 path->slots[0]);
672 copy_extent_buffer(path->nodes[0], eb, dest_offset,
673 (unsigned long)item, sizeof(*item));
674
675 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
676 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
677 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400678 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500679
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800680 /*
681 * Manually record dirty extent, as here we did a shallow
682 * file extent item copy and skip normal backref update,
683 * but modifying extent tree all by ourselves.
684 * So need to manually record dirty extent for qgroup,
685 * as the owner of the file extent changed from log tree
686 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
687 */
Lu Fengqia95f3aa2018-07-18 16:28:03 +0800688 ret = btrfs_qgroup_trace_extent(trans,
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800689 btrfs_file_extent_disk_bytenr(eb, item),
690 btrfs_file_extent_disk_num_bytes(eb, item),
691 GFP_NOFS);
692 if (ret < 0)
693 goto out;
694
Yan Zheng07d400a2009-01-06 11:42:00 -0500695 if (ins.objectid > 0) {
696 u64 csum_start;
697 u64 csum_end;
698 LIST_HEAD(ordered_sums);
699 /*
700 * is this extent already allocated in the extent
701 * allocation tree? If so, just add a reference
702 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400703 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500704 ins.offset);
705 if (ret == 0) {
Josef Bacik84f7d8e2017-09-29 15:43:49 -0400706 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng07d400a2009-01-06 11:42:00 -0500707 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400708 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100709 key->objectid, offset);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400710 if (ret)
711 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500712 } else {
713 /*
714 * insert the extent pointer in the extent
715 * allocation tree
716 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400717 ret = btrfs_alloc_logged_file_extent(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400718 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400719 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400720 if (ret)
721 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500722 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200723 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500724
725 if (btrfs_file_extent_compression(eb, item)) {
726 csum_start = ins.objectid;
727 csum_end = csum_start + ins.offset;
728 } else {
729 csum_start = ins.objectid +
730 btrfs_file_extent_offset(eb, item);
731 csum_end = csum_start +
732 btrfs_file_extent_num_bytes(eb, item);
733 }
734
735 ret = btrfs_lookup_csums_range(root->log_root,
736 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100737 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400738 if (ret)
739 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100740 /*
741 * Now delete all existing cums in the csum root that
742 * cover our range. We do this because we can have an
743 * extent that is completely referenced by one file
744 * extent item and partially referenced by another
745 * file extent item (like after using the clone or
746 * extent_same ioctls). In this case if we end up doing
747 * the replay of the one that partially references the
748 * extent first, and we do not do the csum deletion
749 * below, we can get 2 csum items in the csum tree that
750 * overlap each other. For example, imagine our log has
751 * the two following file extent items:
752 *
753 * key (257 EXTENT_DATA 409600)
754 * extent data disk byte 12845056 nr 102400
755 * extent data offset 20480 nr 20480 ram 102400
756 *
757 * key (257 EXTENT_DATA 819200)
758 * extent data disk byte 12845056 nr 102400
759 * extent data offset 0 nr 102400 ram 102400
760 *
761 * Where the second one fully references the 100K extent
762 * that starts at disk byte 12845056, and the log tree
763 * has a single csum item that covers the entire range
764 * of the extent:
765 *
766 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
767 *
768 * After the first file extent item is replayed, the
769 * csum tree gets the following csum item:
770 *
771 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
772 *
773 * Which covers the 20K sub-range starting at offset 20K
774 * of our extent. Now when we replay the second file
775 * extent item, if we do not delete existing csum items
776 * that cover any of its blocks, we end up getting two
777 * csum items in our csum tree that overlap each other:
778 *
779 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
780 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
781 *
782 * Which is a problem, because after this anyone trying
783 * to lookup up for the checksum of any block of our
784 * extent starting at an offset of 40K or higher, will
785 * end up looking at the second csum item only, which
786 * does not contain the checksum for any block starting
787 * at offset 40K or higher of our extent.
788 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500789 while (!list_empty(&ordered_sums)) {
790 struct btrfs_ordered_sum *sums;
791 sums = list_entry(ordered_sums.next,
792 struct btrfs_ordered_sum,
793 list);
Josef Bacik36508602013-04-25 16:23:32 -0400794 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400795 ret = btrfs_del_csums(trans, fs_info,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400796 sums->bytenr,
797 sums->len);
Filipe Mananab84b8392015-08-19 11:09:40 +0100798 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400799 ret = btrfs_csum_file_blocks(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400800 fs_info->csum_root, sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500801 list_del(&sums->list);
802 kfree(sums);
803 }
Josef Bacik36508602013-04-25 16:23:32 -0400804 if (ret)
805 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500806 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200807 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500808 }
809 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
810 /* inline extents are easy, we just overwrite them */
811 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400812 if (ret)
813 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500814 }
815
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000816 inode_add_bytes(inode, nbytes);
Filipe Manana3168021c2017-02-01 14:58:02 +0000817update_inode:
Tsutomu Itohb9959292012-06-25 21:25:22 -0600818 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400819out:
820 if (inode)
821 iput(inode);
822 return ret;
823}
824
825/*
826 * when cleaning up conflicts between the directory names in the
827 * subvolume, directory names in the log and directory names in the
828 * inode back references, we may have to unlink inodes from directories.
829 *
830 * This is a helper function to do the unlink of a specific directory
831 * item
832 */
833static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
834 struct btrfs_root *root,
835 struct btrfs_path *path,
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200836 struct btrfs_inode *dir,
Chris Masone02119d2008-09-05 16:13:11 -0400837 struct btrfs_dir_item *di)
838{
839 struct inode *inode;
840 char *name;
841 int name_len;
842 struct extent_buffer *leaf;
843 struct btrfs_key location;
844 int ret;
845
846 leaf = path->nodes[0];
847
848 btrfs_dir_item_key_to_cpu(leaf, di, &location);
849 name_len = btrfs_dir_name_len(leaf, di);
850 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000851 if (!name)
852 return -ENOMEM;
853
Chris Masone02119d2008-09-05 16:13:11 -0400854 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200855 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400856
857 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000858 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400859 ret = -EIO;
860 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000861 }
Chris Masone02119d2008-09-05 16:13:11 -0400862
Yan Zhengec051c02009-01-05 15:43:42 -0500863 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400864 if (ret)
865 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400866
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200867 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
868 name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400869 if (ret)
870 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100871 else
Nikolay Borisove5c304e62018-02-07 17:55:43 +0200872 ret = btrfs_run_delayed_items(trans);
Josef Bacik36508602013-04-25 16:23:32 -0400873out:
874 kfree(name);
875 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400876 return ret;
877}
878
879/*
880 * helper function to see if a given name and sequence number found
881 * in an inode back reference are already in a directory and correctly
882 * point to this inode
883 */
884static noinline int inode_in_dir(struct btrfs_root *root,
885 struct btrfs_path *path,
886 u64 dirid, u64 objectid, u64 index,
887 const char *name, int name_len)
888{
889 struct btrfs_dir_item *di;
890 struct btrfs_key location;
891 int match = 0;
892
893 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
894 index, name, name_len, 0);
895 if (di && !IS_ERR(di)) {
896 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
897 if (location.objectid != objectid)
898 goto out;
899 } else
900 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200901 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400902
903 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
904 if (di && !IS_ERR(di)) {
905 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
906 if (location.objectid != objectid)
907 goto out;
908 } else
909 goto out;
910 match = 1;
911out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200912 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400913 return match;
914}
915
916/*
917 * helper function to check a log tree for a named back reference in
918 * an inode. This is used to decide if a back reference that is
919 * found in the subvolume conflicts with what we find in the log.
920 *
921 * inode backreferences may have multiple refs in a single item,
922 * during replay we process one reference at a time, and we don't
923 * want to delete valid links to a file from the subvolume if that
924 * link is also in the log.
925 */
926static noinline int backref_in_log(struct btrfs_root *log,
927 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700928 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000929 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400930{
931 struct btrfs_path *path;
932 struct btrfs_inode_ref *ref;
933 unsigned long ptr;
934 unsigned long ptr_end;
935 unsigned long name_ptr;
936 int found_name_len;
937 int item_size;
938 int ret;
939 int match = 0;
940
941 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000942 if (!path)
943 return -ENOMEM;
944
Chris Masone02119d2008-09-05 16:13:11 -0400945 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
946 if (ret != 0)
947 goto out;
948
Chris Masone02119d2008-09-05 16:13:11 -0400949 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700950
951 if (key->type == BTRFS_INODE_EXTREF_KEY) {
Filipe Manana1f250e92018-02-28 15:56:10 +0000952 if (btrfs_find_name_in_ext_backref(path->nodes[0],
953 path->slots[0],
954 ref_objectid,
Mark Fashehf1863732012-08-08 11:32:27 -0700955 name, namelen, NULL))
956 match = 1;
957
958 goto out;
959 }
960
961 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400962 ptr_end = ptr + item_size;
963 while (ptr < ptr_end) {
964 ref = (struct btrfs_inode_ref *)ptr;
965 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
966 if (found_name_len == namelen) {
967 name_ptr = (unsigned long)(ref + 1);
968 ret = memcmp_extent_buffer(path->nodes[0], name,
969 name_ptr, namelen);
970 if (ret == 0) {
971 match = 1;
972 goto out;
973 }
974 }
975 ptr = (unsigned long)(ref + 1) + found_name_len;
976 }
977out:
978 btrfs_free_path(path);
979 return match;
980}
981
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700982static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
983 struct btrfs_root *root,
984 struct btrfs_path *path,
985 struct btrfs_root *log_root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +0200986 struct btrfs_inode *dir,
987 struct btrfs_inode *inode,
Mark Fashehf1863732012-08-08 11:32:27 -0700988 u64 inode_objectid, u64 parent_objectid,
989 u64 ref_index, char *name, int namelen,
990 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700991{
992 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700993 char *victim_name;
994 int victim_name_len;
995 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700996 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700997 struct btrfs_key search_key;
998 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700999
Mark Fashehf1863732012-08-08 11:32:27 -07001000again:
1001 /* Search old style refs */
1002 search_key.objectid = inode_objectid;
1003 search_key.type = BTRFS_INODE_REF_KEY;
1004 search_key.offset = parent_objectid;
1005 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001006 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001007 struct btrfs_inode_ref *victim_ref;
1008 unsigned long ptr;
1009 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001010
1011 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001012
1013 /* are we trying to overwrite a back ref for the root directory
1014 * if so, just jump out, we're done
1015 */
Mark Fashehf1863732012-08-08 11:32:27 -07001016 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001017 return 1;
1018
1019 /* check all the names in this back reference to see
1020 * if they are in the log. if so, we allow them to stay
1021 * otherwise they must be unlinked as a conflict
1022 */
1023 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1024 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1025 while (ptr < ptr_end) {
1026 victim_ref = (struct btrfs_inode_ref *)ptr;
1027 victim_name_len = btrfs_inode_ref_name_len(leaf,
1028 victim_ref);
1029 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001030 if (!victim_name)
1031 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001032
1033 read_extent_buffer(leaf, victim_name,
1034 (unsigned long)(victim_ref + 1),
1035 victim_name_len);
1036
Mark Fashehf1863732012-08-08 11:32:27 -07001037 if (!backref_in_log(log_root, &search_key,
1038 parent_objectid,
1039 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001040 victim_name_len)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001041 inc_nlink(&inode->vfs_inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001042 btrfs_release_path(path);
1043
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001044 ret = btrfs_unlink_inode(trans, root, dir, inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001045 victim_name, victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001046 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001047 if (ret)
1048 return ret;
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001049 ret = btrfs_run_delayed_items(trans);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001050 if (ret)
1051 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001052 *search_done = 1;
1053 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001054 }
1055 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001056
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001057 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1058 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001059
1060 /*
1061 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001062 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001063 */
1064 *search_done = 1;
1065 }
1066 btrfs_release_path(path);
1067
Mark Fashehf1863732012-08-08 11:32:27 -07001068 /* Same search but for extended refs */
1069 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1070 inode_objectid, parent_objectid, 0,
1071 0);
1072 if (!IS_ERR_OR_NULL(extref)) {
1073 u32 item_size;
1074 u32 cur_offset = 0;
1075 unsigned long base;
1076 struct inode *victim_parent;
1077
1078 leaf = path->nodes[0];
1079
1080 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1081 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1082
1083 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001084 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001085
1086 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1087
1088 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1089 goto next;
1090
1091 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001092 if (!victim_name)
1093 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001094 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1095 victim_name_len);
1096
1097 search_key.objectid = inode_objectid;
1098 search_key.type = BTRFS_INODE_EXTREF_KEY;
1099 search_key.offset = btrfs_extref_hash(parent_objectid,
1100 victim_name,
1101 victim_name_len);
1102 ret = 0;
1103 if (!backref_in_log(log_root, &search_key,
1104 parent_objectid, victim_name,
1105 victim_name_len)) {
1106 ret = -ENOENT;
1107 victim_parent = read_one_inode(root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001108 parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001109 if (victim_parent) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001110 inc_nlink(&inode->vfs_inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001111 btrfs_release_path(path);
1112
1113 ret = btrfs_unlink_inode(trans, root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001114 BTRFS_I(victim_parent),
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001115 inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001116 victim_name,
1117 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001118 if (!ret)
1119 ret = btrfs_run_delayed_items(
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001120 trans);
Mark Fashehf1863732012-08-08 11:32:27 -07001121 }
Mark Fashehf1863732012-08-08 11:32:27 -07001122 iput(victim_parent);
1123 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001124 if (ret)
1125 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001126 *search_done = 1;
1127 goto again;
1128 }
1129 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001130next:
1131 cur_offset += victim_name_len + sizeof(*extref);
1132 }
1133 *search_done = 1;
1134 }
1135 btrfs_release_path(path);
1136
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001137 /* look for a conflicting sequence number */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001138 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001139 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001140 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001141 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001142 if (ret)
1143 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001144 }
1145 btrfs_release_path(path);
1146
1147 /* look for a conflicing name */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001148 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001149 name, namelen, 0);
1150 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001151 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001152 if (ret)
1153 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001154 }
1155 btrfs_release_path(path);
1156
1157 return 0;
1158}
Chris Masone02119d2008-09-05 16:13:11 -04001159
Qu Wenruobae15d92017-11-08 08:54:26 +08001160static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1161 u32 *namelen, char **name, u64 *index,
1162 u64 *parent_objectid)
Mark Fashehf1863732012-08-08 11:32:27 -07001163{
1164 struct btrfs_inode_extref *extref;
1165
1166 extref = (struct btrfs_inode_extref *)ref_ptr;
1167
1168 *namelen = btrfs_inode_extref_name_len(eb, extref);
1169 *name = kmalloc(*namelen, GFP_NOFS);
1170 if (*name == NULL)
1171 return -ENOMEM;
1172
1173 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1174 *namelen);
1175
Filipe Manana1f250e92018-02-28 15:56:10 +00001176 if (index)
1177 *index = btrfs_inode_extref_index(eb, extref);
Mark Fashehf1863732012-08-08 11:32:27 -07001178 if (parent_objectid)
1179 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1180
1181 return 0;
1182}
1183
Qu Wenruobae15d92017-11-08 08:54:26 +08001184static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1185 u32 *namelen, char **name, u64 *index)
Mark Fashehf1863732012-08-08 11:32:27 -07001186{
1187 struct btrfs_inode_ref *ref;
1188
1189 ref = (struct btrfs_inode_ref *)ref_ptr;
1190
1191 *namelen = btrfs_inode_ref_name_len(eb, ref);
1192 *name = kmalloc(*namelen, GFP_NOFS);
1193 if (*name == NULL)
1194 return -ENOMEM;
1195
1196 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1197
Filipe Manana1f250e92018-02-28 15:56:10 +00001198 if (index)
1199 *index = btrfs_inode_ref_index(eb, ref);
Mark Fashehf1863732012-08-08 11:32:27 -07001200
1201 return 0;
1202}
1203
Chris Masone02119d2008-09-05 16:13:11 -04001204/*
Filipe Manana1f250e92018-02-28 15:56:10 +00001205 * Take an inode reference item from the log tree and iterate all names from the
1206 * inode reference item in the subvolume tree with the same key (if it exists).
1207 * For any name that is not in the inode reference item from the log tree, do a
1208 * proper unlink of that name (that is, remove its entry from the inode
1209 * reference item and both dir index keys).
1210 */
1211static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1212 struct btrfs_root *root,
1213 struct btrfs_path *path,
1214 struct btrfs_inode *inode,
1215 struct extent_buffer *log_eb,
1216 int log_slot,
1217 struct btrfs_key *key)
1218{
1219 int ret;
1220 unsigned long ref_ptr;
1221 unsigned long ref_end;
1222 struct extent_buffer *eb;
1223
1224again:
1225 btrfs_release_path(path);
1226 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1227 if (ret > 0) {
1228 ret = 0;
1229 goto out;
1230 }
1231 if (ret < 0)
1232 goto out;
1233
1234 eb = path->nodes[0];
1235 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1236 ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1237 while (ref_ptr < ref_end) {
1238 char *name = NULL;
1239 int namelen;
1240 u64 parent_id;
1241
1242 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1243 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1244 NULL, &parent_id);
1245 } else {
1246 parent_id = key->offset;
1247 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1248 NULL);
1249 }
1250 if (ret)
1251 goto out;
1252
1253 if (key->type == BTRFS_INODE_EXTREF_KEY)
1254 ret = btrfs_find_name_in_ext_backref(log_eb, log_slot,
1255 parent_id, name,
1256 namelen, NULL);
1257 else
1258 ret = btrfs_find_name_in_backref(log_eb, log_slot, name,
1259 namelen, NULL);
1260
1261 if (!ret) {
1262 struct inode *dir;
1263
1264 btrfs_release_path(path);
1265 dir = read_one_inode(root, parent_id);
1266 if (!dir) {
1267 ret = -ENOENT;
1268 kfree(name);
1269 goto out;
1270 }
1271 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
1272 inode, name, namelen);
1273 kfree(name);
1274 iput(dir);
1275 if (ret)
1276 goto out;
1277 goto again;
1278 }
1279
1280 kfree(name);
1281 ref_ptr += namelen;
1282 if (key->type == BTRFS_INODE_EXTREF_KEY)
1283 ref_ptr += sizeof(struct btrfs_inode_extref);
1284 else
1285 ref_ptr += sizeof(struct btrfs_inode_ref);
1286 }
1287 ret = 0;
1288 out:
1289 btrfs_release_path(path);
1290 return ret;
1291}
1292
Filipe Manana0d836392018-07-20 10:59:06 +01001293static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
1294 const u8 ref_type, const char *name,
1295 const int namelen)
1296{
1297 struct btrfs_key key;
1298 struct btrfs_path *path;
1299 const u64 parent_id = btrfs_ino(BTRFS_I(dir));
1300 int ret;
1301
1302 path = btrfs_alloc_path();
1303 if (!path)
1304 return -ENOMEM;
1305
1306 key.objectid = btrfs_ino(BTRFS_I(inode));
1307 key.type = ref_type;
1308 if (key.type == BTRFS_INODE_REF_KEY)
1309 key.offset = parent_id;
1310 else
1311 key.offset = btrfs_extref_hash(parent_id, name, namelen);
1312
1313 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
1314 if (ret < 0)
1315 goto out;
1316 if (ret > 0) {
1317 ret = 0;
1318 goto out;
1319 }
1320 if (key.type == BTRFS_INODE_EXTREF_KEY)
1321 ret = btrfs_find_name_in_ext_backref(path->nodes[0],
1322 path->slots[0], parent_id,
1323 name, namelen, NULL);
1324 else
1325 ret = btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
1326 name, namelen, NULL);
1327
1328out:
1329 btrfs_free_path(path);
1330 return ret;
1331}
1332
Filipe Manana1f250e92018-02-28 15:56:10 +00001333/*
Chris Masone02119d2008-09-05 16:13:11 -04001334 * replay one inode back reference item found in the log tree.
1335 * eb, slot and key refer to the buffer and key found in the log tree.
1336 * root is the destination we are replaying into, and path is for temp
1337 * use by this function. (it should be released on return).
1338 */
1339static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1340 struct btrfs_root *root,
1341 struct btrfs_root *log,
1342 struct btrfs_path *path,
1343 struct extent_buffer *eb, int slot,
1344 struct btrfs_key *key)
1345{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001346 struct inode *dir = NULL;
1347 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001348 unsigned long ref_ptr;
1349 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001350 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001351 int namelen;
1352 int ret;
liuboc622ae62011-03-26 08:01:12 -04001353 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001354 int log_ref_ver = 0;
1355 u64 parent_objectid;
1356 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001357 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001358 int ref_struct_size;
1359
1360 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1361 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1362
1363 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1364 struct btrfs_inode_extref *r;
1365
1366 ref_struct_size = sizeof(struct btrfs_inode_extref);
1367 log_ref_ver = 1;
1368 r = (struct btrfs_inode_extref *)ref_ptr;
1369 parent_objectid = btrfs_inode_extref_parent(eb, r);
1370 } else {
1371 ref_struct_size = sizeof(struct btrfs_inode_ref);
1372 parent_objectid = key->offset;
1373 }
1374 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001375
Chris Masone02119d2008-09-05 16:13:11 -04001376 /*
1377 * it is possible that we didn't log all the parent directories
1378 * for a given inode. If we don't find the dir, just don't
1379 * copy the back ref in. The link count fixup code will take
1380 * care of the rest
1381 */
Mark Fashehf1863732012-08-08 11:32:27 -07001382 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001383 if (!dir) {
1384 ret = -ENOENT;
1385 goto out;
1386 }
Chris Masone02119d2008-09-05 16:13:11 -04001387
Mark Fashehf1863732012-08-08 11:32:27 -07001388 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001389 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001390 ret = -EIO;
1391 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001392 }
Chris Masone02119d2008-09-05 16:13:11 -04001393
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001394 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001395 if (log_ref_ver) {
Qu Wenruobae15d92017-11-08 08:54:26 +08001396 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1397 &ref_index, &parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001398 /*
1399 * parent object can change from one array
1400 * item to another.
1401 */
1402 if (!dir)
1403 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001404 if (!dir) {
1405 ret = -ENOENT;
1406 goto out;
1407 }
Mark Fashehf1863732012-08-08 11:32:27 -07001408 } else {
Qu Wenruobae15d92017-11-08 08:54:26 +08001409 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1410 &ref_index);
Mark Fashehf1863732012-08-08 11:32:27 -07001411 }
1412 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001413 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001414
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001415 /* if we already have a perfect match, we're done */
David Sterbaf85b7372017-01-20 14:54:07 +01001416 if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1417 btrfs_ino(BTRFS_I(inode)), ref_index,
1418 name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001419 /*
1420 * look for a conflicting back reference in the
1421 * metadata. if we find one we have to unlink that name
1422 * of the file before we add our new link. Later on, we
1423 * overwrite any existing back reference, and we don't
1424 * want to create dangling pointers in the directory.
1425 */
Chris Masone02119d2008-09-05 16:13:11 -04001426
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001427 if (!search_done) {
1428 ret = __add_inode_ref(trans, root, path, log,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001429 BTRFS_I(dir),
David Sterbad75eefd2017-02-10 20:20:19 +01001430 BTRFS_I(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001431 inode_objectid,
1432 parent_objectid,
1433 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001434 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001435 if (ret) {
1436 if (ret == 1)
1437 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001438 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001439 }
Chris Masone02119d2008-09-05 16:13:11 -04001440 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001441
Filipe Manana0d836392018-07-20 10:59:06 +01001442 /*
1443 * If a reference item already exists for this inode
1444 * with the same parent and name, but different index,
1445 * drop it and the corresponding directory index entries
1446 * from the parent before adding the new reference item
1447 * and dir index entries, otherwise we would fail with
1448 * -EEXIST returned from btrfs_add_link() below.
1449 */
1450 ret = btrfs_inode_ref_exists(inode, dir, key->type,
1451 name, namelen);
1452 if (ret > 0) {
1453 ret = btrfs_unlink_inode(trans, root,
1454 BTRFS_I(dir),
1455 BTRFS_I(inode),
1456 name, namelen);
1457 /*
1458 * If we dropped the link count to 0, bump it so
1459 * that later the iput() on the inode will not
1460 * free it. We will fixup the link count later.
1461 */
1462 if (!ret && inode->i_nlink == 0)
1463 inc_nlink(inode);
1464 }
1465 if (ret < 0)
1466 goto out;
1467
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001468 /* insert our name */
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001469 ret = btrfs_add_link(trans, BTRFS_I(dir),
1470 BTRFS_I(inode),
1471 name, namelen, 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001472 if (ret)
1473 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001474
1475 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001476 }
liuboc622ae62011-03-26 08:01:12 -04001477
Mark Fashehf1863732012-08-08 11:32:27 -07001478 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001479 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001480 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001481 if (log_ref_ver) {
1482 iput(dir);
1483 dir = NULL;
1484 }
Chris Masone02119d2008-09-05 16:13:11 -04001485 }
Chris Masone02119d2008-09-05 16:13:11 -04001486
Filipe Manana1f250e92018-02-28 15:56:10 +00001487 /*
1488 * Before we overwrite the inode reference item in the subvolume tree
1489 * with the item from the log tree, we must unlink all names from the
1490 * parent directory that are in the subvolume's tree inode reference
1491 * item, otherwise we end up with an inconsistent subvolume tree where
1492 * dir index entries exist for a name but there is no inode reference
1493 * item with the same name.
1494 */
1495 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1496 key);
1497 if (ret)
1498 goto out;
1499
Chris Masone02119d2008-09-05 16:13:11 -04001500 /* finally write the back reference in the inode */
1501 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001502out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001503 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001504 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001505 iput(dir);
1506 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001507 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001508}
1509
Yan, Zhengc71bf092009-11-12 09:34:40 +00001510static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001511 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001512{
1513 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001514
David Sterba9c4f61f2015-01-02 19:12:57 +01001515 ret = btrfs_insert_orphan_item(trans, root, ino);
1516 if (ret == -EEXIST)
1517 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001518
Yan, Zhengc71bf092009-11-12 09:34:40 +00001519 return ret;
1520}
1521
Mark Fashehf1863732012-08-08 11:32:27 -07001522static int count_inode_extrefs(struct btrfs_root *root,
Nikolay Borisov36283652017-01-18 00:31:49 +02001523 struct btrfs_inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001524{
Mark Fashehf1863732012-08-08 11:32:27 -07001525 int ret = 0;
1526 int name_len;
1527 unsigned int nlink = 0;
1528 u32 item_size;
1529 u32 cur_offset = 0;
Nikolay Borisov36283652017-01-18 00:31:49 +02001530 u64 inode_objectid = btrfs_ino(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001531 u64 offset = 0;
1532 unsigned long ptr;
1533 struct btrfs_inode_extref *extref;
1534 struct extent_buffer *leaf;
1535
1536 while (1) {
1537 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1538 &extref, &offset);
1539 if (ret)
1540 break;
1541
1542 leaf = path->nodes[0];
1543 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1544 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001545 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001546
1547 while (cur_offset < item_size) {
1548 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1549 name_len = btrfs_inode_extref_name_len(leaf, extref);
1550
1551 nlink++;
1552
1553 cur_offset += name_len + sizeof(*extref);
1554 }
1555
1556 offset++;
1557 btrfs_release_path(path);
1558 }
1559 btrfs_release_path(path);
1560
Filipe Manana2c2c4522015-01-13 16:40:04 +00001561 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001562 return ret;
1563 return nlink;
1564}
1565
1566static int count_inode_refs(struct btrfs_root *root,
Nikolay Borisovf329e312017-01-18 00:31:50 +02001567 struct btrfs_inode *inode, struct btrfs_path *path)
Mark Fashehf1863732012-08-08 11:32:27 -07001568{
Chris Masone02119d2008-09-05 16:13:11 -04001569 int ret;
1570 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001571 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001572 unsigned long ptr;
1573 unsigned long ptr_end;
1574 int name_len;
Nikolay Borisovf329e312017-01-18 00:31:50 +02001575 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001576
Li Zefan33345d012011-04-20 10:31:50 +08001577 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001578 key.type = BTRFS_INODE_REF_KEY;
1579 key.offset = (u64)-1;
1580
Chris Masond3977122009-01-05 21:25:51 -05001581 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001582 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1583 if (ret < 0)
1584 break;
1585 if (ret > 0) {
1586 if (path->slots[0] == 0)
1587 break;
1588 path->slots[0]--;
1589 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001590process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001591 btrfs_item_key_to_cpu(path->nodes[0], &key,
1592 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001593 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001594 key.type != BTRFS_INODE_REF_KEY)
1595 break;
1596 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1597 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1598 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001599 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001600 struct btrfs_inode_ref *ref;
1601
1602 ref = (struct btrfs_inode_ref *)ptr;
1603 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1604 ref);
1605 ptr = (unsigned long)(ref + 1) + name_len;
1606 nlink++;
1607 }
1608
1609 if (key.offset == 0)
1610 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001611 if (path->slots[0] > 0) {
1612 path->slots[0]--;
1613 goto process_slot;
1614 }
Chris Masone02119d2008-09-05 16:13:11 -04001615 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001616 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001617 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001618 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001619
1620 return nlink;
1621}
1622
1623/*
1624 * There are a few corners where the link count of the file can't
1625 * be properly maintained during replay. So, instead of adding
1626 * lots of complexity to the log code, we just scan the backrefs
1627 * for any file that has been through replay.
1628 *
1629 * The scan will update the link count on the inode to reflect the
1630 * number of back refs found. If it goes down to zero, the iput
1631 * will free the inode.
1632 */
1633static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1634 struct btrfs_root *root,
1635 struct inode *inode)
1636{
1637 struct btrfs_path *path;
1638 int ret;
1639 u64 nlink = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001640 u64 ino = btrfs_ino(BTRFS_I(inode));
Mark Fashehf1863732012-08-08 11:32:27 -07001641
1642 path = btrfs_alloc_path();
1643 if (!path)
1644 return -ENOMEM;
1645
Nikolay Borisovf329e312017-01-18 00:31:50 +02001646 ret = count_inode_refs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001647 if (ret < 0)
1648 goto out;
1649
1650 nlink = ret;
1651
Nikolay Borisov36283652017-01-18 00:31:49 +02001652 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001653 if (ret < 0)
1654 goto out;
1655
1656 nlink += ret;
1657
1658 ret = 0;
1659
Chris Masone02119d2008-09-05 16:13:11 -04001660 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001661 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001662 btrfs_update_inode(trans, root, inode);
1663 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001664 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001665
Yan, Zhengc71bf092009-11-12 09:34:40 +00001666 if (inode->i_nlink == 0) {
1667 if (S_ISDIR(inode->i_mode)) {
1668 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001669 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001670 if (ret)
1671 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001672 }
Li Zefan33345d012011-04-20 10:31:50 +08001673 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001674 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001675
Mark Fashehf1863732012-08-08 11:32:27 -07001676out:
1677 btrfs_free_path(path);
1678 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001679}
1680
1681static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1682 struct btrfs_root *root,
1683 struct btrfs_path *path)
1684{
1685 int ret;
1686 struct btrfs_key key;
1687 struct inode *inode;
1688
1689 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1690 key.type = BTRFS_ORPHAN_ITEM_KEY;
1691 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001692 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001693 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1694 if (ret < 0)
1695 break;
1696
1697 if (ret == 1) {
1698 if (path->slots[0] == 0)
1699 break;
1700 path->slots[0]--;
1701 }
1702
1703 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1704 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1705 key.type != BTRFS_ORPHAN_ITEM_KEY)
1706 break;
1707
1708 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001709 if (ret)
1710 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001711
David Sterbab3b4aa72011-04-21 01:20:15 +02001712 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001713 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001714 if (!inode)
1715 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001716
1717 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001718 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001719 if (ret)
1720 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001721
Chris Mason12fcfd22009-03-24 10:24:20 -04001722 /*
1723 * fixup on a directory may create new entries,
1724 * make sure we always look for the highset possible
1725 * offset
1726 */
1727 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001728 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001729 ret = 0;
1730out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001731 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001732 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001733}
1734
1735
1736/*
1737 * record a given inode in the fixup dir so we can check its link
1738 * count when replay is done. The link count is incremented here
1739 * so the inode won't go away until we check it
1740 */
1741static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1742 struct btrfs_root *root,
1743 struct btrfs_path *path,
1744 u64 objectid)
1745{
1746 struct btrfs_key key;
1747 int ret = 0;
1748 struct inode *inode;
1749
1750 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001751 if (!inode)
1752 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001753
1754 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001755 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001756 key.offset = objectid;
1757
1758 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1759
David Sterbab3b4aa72011-04-21 01:20:15 +02001760 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001761 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001762 if (!inode->i_nlink)
1763 set_nlink(inode, 1);
1764 else
Zach Brown8b558c52013-10-16 12:10:34 -07001765 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001766 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001767 } else if (ret == -EEXIST) {
1768 ret = 0;
1769 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001770 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001771 }
1772 iput(inode);
1773
1774 return ret;
1775}
1776
1777/*
1778 * when replaying the log for a directory, we only insert names
1779 * for inodes that actually exist. This means an fsync on a directory
1780 * does not implicitly fsync all the new files in it
1781 */
1782static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1783 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001784 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001785 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001786 struct btrfs_key *location)
1787{
1788 struct inode *inode;
1789 struct inode *dir;
1790 int ret;
1791
1792 inode = read_one_inode(root, location->objectid);
1793 if (!inode)
1794 return -ENOENT;
1795
1796 dir = read_one_inode(root, dirid);
1797 if (!dir) {
1798 iput(inode);
1799 return -EIO;
1800 }
Josef Bacikd5554382013-09-11 14:17:00 -04001801
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001802 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1803 name_len, 1, index);
Chris Masone02119d2008-09-05 16:13:11 -04001804
1805 /* FIXME, put inode into FIXUP list */
1806
1807 iput(inode);
1808 iput(dir);
1809 return ret;
1810}
1811
1812/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001813 * Return true if an inode reference exists in the log for the given name,
1814 * inode and parent inode.
1815 */
1816static bool name_in_log_ref(struct btrfs_root *log_root,
1817 const char *name, const int name_len,
1818 const u64 dirid, const u64 ino)
1819{
1820 struct btrfs_key search_key;
1821
1822 search_key.objectid = ino;
1823 search_key.type = BTRFS_INODE_REF_KEY;
1824 search_key.offset = dirid;
1825 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1826 return true;
1827
1828 search_key.type = BTRFS_INODE_EXTREF_KEY;
1829 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1830 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1831 return true;
1832
1833 return false;
1834}
1835
1836/*
Chris Masone02119d2008-09-05 16:13:11 -04001837 * take a single entry in a log directory item and replay it into
1838 * the subvolume.
1839 *
1840 * if a conflicting item exists in the subdirectory already,
1841 * the inode it points to is unlinked and put into the link count
1842 * fix up tree.
1843 *
1844 * If a name from the log points to a file or directory that does
1845 * not exist in the FS, it is skipped. fsyncs on directories
1846 * do not force down inodes inside that directory, just changes to the
1847 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001848 *
1849 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1850 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001851 */
1852static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1853 struct btrfs_root *root,
1854 struct btrfs_path *path,
1855 struct extent_buffer *eb,
1856 struct btrfs_dir_item *di,
1857 struct btrfs_key *key)
1858{
1859 char *name;
1860 int name_len;
1861 struct btrfs_dir_item *dst_di;
1862 struct btrfs_key found_key;
1863 struct btrfs_key log_key;
1864 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001865 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001866 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001867 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001868 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001869 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001870
1871 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001872 if (!dir)
1873 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001874
1875 name_len = btrfs_dir_name_len(eb, di);
1876 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001877 if (!name) {
1878 ret = -ENOMEM;
1879 goto out;
1880 }
liubo2a29edc2011-01-26 06:22:08 +00001881
Chris Masone02119d2008-09-05 16:13:11 -04001882 log_type = btrfs_dir_type(eb, di);
1883 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1884 name_len);
1885
1886 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001887 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1888 if (exists == 0)
1889 exists = 1;
1890 else
1891 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001892 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001893
Chris Masone02119d2008-09-05 16:13:11 -04001894 if (key->type == BTRFS_DIR_ITEM_KEY) {
1895 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1896 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001897 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001898 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1899 key->objectid,
1900 key->offset, name,
1901 name_len, 1);
1902 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001903 /* Corruption */
1904 ret = -EINVAL;
1905 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001906 }
David Sterbac7040052011-04-19 18:00:01 +02001907 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001908 /* we need a sequence number to insert, so we only
1909 * do inserts for the BTRFS_DIR_INDEX_KEY types
1910 */
1911 if (key->type != BTRFS_DIR_INDEX_KEY)
1912 goto out;
1913 goto insert;
1914 }
1915
1916 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1917 /* the existing item matches the logged item */
1918 if (found_key.objectid == log_key.objectid &&
1919 found_key.type == log_key.type &&
1920 found_key.offset == log_key.offset &&
1921 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001922 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001923 goto out;
1924 }
1925
1926 /*
1927 * don't drop the conflicting directory entry if the inode
1928 * for the new entry doesn't exist
1929 */
Chris Mason4bef0842008-09-08 11:18:08 -04001930 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001931 goto out;
1932
Nikolay Borisov207e7d92017-01-18 00:31:45 +02001933 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001934 if (ret)
1935 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001936
1937 if (key->type == BTRFS_DIR_INDEX_KEY)
1938 goto insert;
1939out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001940 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001941 if (!ret && update_size) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02001942 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
Josef Bacikd5554382013-09-11 14:17:00 -04001943 ret = btrfs_update_inode(trans, root, dir);
1944 }
Chris Masone02119d2008-09-05 16:13:11 -04001945 kfree(name);
1946 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001947 if (!ret && name_added)
1948 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001949 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001950
1951insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001952 if (name_in_log_ref(root->log_root, name, name_len,
1953 key->objectid, log_key.objectid)) {
1954 /* The dentry will be added later. */
1955 ret = 0;
1956 update_size = false;
1957 goto out;
1958 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001959 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08001960 ret = insert_one_name(trans, root, key->objectid, key->offset,
1961 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001962 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001963 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001964 if (!ret)
1965 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001966 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001967 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001968 goto out;
1969}
1970
1971/*
1972 * find all the names in a directory item and reconcile them into
1973 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1974 * one name in a directory item, but the same code gets used for
1975 * both directory index types
1976 */
1977static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1978 struct btrfs_root *root,
1979 struct btrfs_path *path,
1980 struct extent_buffer *eb, int slot,
1981 struct btrfs_key *key)
1982{
Filipe Mananabb53eda2015-07-15 23:26:43 +01001983 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001984 u32 item_size = btrfs_item_size_nr(eb, slot);
1985 struct btrfs_dir_item *di;
1986 int name_len;
1987 unsigned long ptr;
1988 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001989 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001990
1991 ptr = btrfs_item_ptr_offset(eb, slot);
1992 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001993 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001994 di = (struct btrfs_dir_item *)ptr;
1995 name_len = btrfs_dir_name_len(eb, di);
1996 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001997 if (ret < 0)
1998 break;
Chris Masone02119d2008-09-05 16:13:11 -04001999 ptr = (unsigned long)(di + 1);
2000 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01002001
2002 /*
2003 * If this entry refers to a non-directory (directories can not
2004 * have a link count > 1) and it was added in the transaction
2005 * that was not committed, make sure we fixup the link count of
2006 * the inode it the entry points to. Otherwise something like
2007 * the following would result in a directory pointing to an
2008 * inode with a wrong link that does not account for this dir
2009 * entry:
2010 *
2011 * mkdir testdir
2012 * touch testdir/foo
2013 * touch testdir/bar
2014 * sync
2015 *
2016 * ln testdir/bar testdir/bar_link
2017 * ln testdir/foo testdir/foo_link
2018 * xfs_io -c "fsync" testdir/bar
2019 *
2020 * <power failure>
2021 *
2022 * mount fs, log replay happens
2023 *
2024 * File foo would remain with a link count of 1 when it has two
2025 * entries pointing to it in the directory testdir. This would
2026 * make it impossible to ever delete the parent directory has
2027 * it would result in stale dentries that can never be deleted.
2028 */
2029 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
2030 struct btrfs_key di_key;
2031
2032 if (!fixup_path) {
2033 fixup_path = btrfs_alloc_path();
2034 if (!fixup_path) {
2035 ret = -ENOMEM;
2036 break;
2037 }
2038 }
2039
2040 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2041 ret = link_to_fixup_dir(trans, root, fixup_path,
2042 di_key.objectid);
2043 if (ret)
2044 break;
2045 }
2046 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002047 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01002048 btrfs_free_path(fixup_path);
2049 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002050}
2051
2052/*
2053 * directory replay has two parts. There are the standard directory
2054 * items in the log copied from the subvolume, and range items
2055 * created in the log while the subvolume was logged.
2056 *
2057 * The range items tell us which parts of the key space the log
2058 * is authoritative for. During replay, if a key in the subvolume
2059 * directory is in a logged range item, but not actually in the log
2060 * that means it was deleted from the directory before the fsync
2061 * and should be removed.
2062 */
2063static noinline int find_dir_range(struct btrfs_root *root,
2064 struct btrfs_path *path,
2065 u64 dirid, int key_type,
2066 u64 *start_ret, u64 *end_ret)
2067{
2068 struct btrfs_key key;
2069 u64 found_end;
2070 struct btrfs_dir_log_item *item;
2071 int ret;
2072 int nritems;
2073
2074 if (*start_ret == (u64)-1)
2075 return 1;
2076
2077 key.objectid = dirid;
2078 key.type = key_type;
2079 key.offset = *start_ret;
2080
2081 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2082 if (ret < 0)
2083 goto out;
2084 if (ret > 0) {
2085 if (path->slots[0] == 0)
2086 goto out;
2087 path->slots[0]--;
2088 }
2089 if (ret != 0)
2090 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2091
2092 if (key.type != key_type || key.objectid != dirid) {
2093 ret = 1;
2094 goto next;
2095 }
2096 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2097 struct btrfs_dir_log_item);
2098 found_end = btrfs_dir_log_end(path->nodes[0], item);
2099
2100 if (*start_ret >= key.offset && *start_ret <= found_end) {
2101 ret = 0;
2102 *start_ret = key.offset;
2103 *end_ret = found_end;
2104 goto out;
2105 }
2106 ret = 1;
2107next:
2108 /* check the next slot in the tree to see if it is a valid item */
2109 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko2a7bf532016-10-07 17:30:47 +08002110 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04002111 if (path->slots[0] >= nritems) {
2112 ret = btrfs_next_leaf(root, path);
2113 if (ret)
2114 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002115 }
2116
2117 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2118
2119 if (key.type != key_type || key.objectid != dirid) {
2120 ret = 1;
2121 goto out;
2122 }
2123 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2124 struct btrfs_dir_log_item);
2125 found_end = btrfs_dir_log_end(path->nodes[0], item);
2126 *start_ret = key.offset;
2127 *end_ret = found_end;
2128 ret = 0;
2129out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002130 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002131 return ret;
2132}
2133
2134/*
2135 * this looks for a given directory item in the log. If the directory
2136 * item is not in the log, the item is removed and the inode it points
2137 * to is unlinked
2138 */
2139static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2140 struct btrfs_root *root,
2141 struct btrfs_root *log,
2142 struct btrfs_path *path,
2143 struct btrfs_path *log_path,
2144 struct inode *dir,
2145 struct btrfs_key *dir_key)
2146{
2147 int ret;
2148 struct extent_buffer *eb;
2149 int slot;
2150 u32 item_size;
2151 struct btrfs_dir_item *di;
2152 struct btrfs_dir_item *log_di;
2153 int name_len;
2154 unsigned long ptr;
2155 unsigned long ptr_end;
2156 char *name;
2157 struct inode *inode;
2158 struct btrfs_key location;
2159
2160again:
2161 eb = path->nodes[0];
2162 slot = path->slots[0];
2163 item_size = btrfs_item_size_nr(eb, slot);
2164 ptr = btrfs_item_ptr_offset(eb, slot);
2165 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002166 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002167 di = (struct btrfs_dir_item *)ptr;
2168 name_len = btrfs_dir_name_len(eb, di);
2169 name = kmalloc(name_len, GFP_NOFS);
2170 if (!name) {
2171 ret = -ENOMEM;
2172 goto out;
2173 }
2174 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2175 name_len);
2176 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002177 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002178 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2179 dir_key->objectid,
2180 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002181 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002182 log_di = btrfs_lookup_dir_index_item(trans, log,
2183 log_path,
2184 dir_key->objectid,
2185 dir_key->offset,
2186 name, name_len, 0);
2187 }
Al Viro8d9e2202018-07-29 23:04:46 +01002188 if (!log_di || log_di == ERR_PTR(-ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002189 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002190 btrfs_release_path(path);
2191 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002192 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002193 if (!inode) {
2194 kfree(name);
2195 return -EIO;
2196 }
Chris Masone02119d2008-09-05 16:13:11 -04002197
2198 ret = link_to_fixup_dir(trans, root,
2199 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002200 if (ret) {
2201 kfree(name);
2202 iput(inode);
2203 goto out;
2204 }
2205
Zach Brown8b558c52013-10-16 12:10:34 -07002206 inc_nlink(inode);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02002207 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2208 BTRFS_I(inode), name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002209 if (!ret)
Nikolay Borisove5c304e62018-02-07 17:55:43 +02002210 ret = btrfs_run_delayed_items(trans);
Chris Masone02119d2008-09-05 16:13:11 -04002211 kfree(name);
2212 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002213 if (ret)
2214 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002215
2216 /* there might still be more names under this key
2217 * check and repeat if required
2218 */
2219 ret = btrfs_search_slot(NULL, root, dir_key, path,
2220 0, 0);
2221 if (ret == 0)
2222 goto again;
2223 ret = 0;
2224 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002225 } else if (IS_ERR(log_di)) {
2226 kfree(name);
2227 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002228 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002229 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002230 kfree(name);
2231
2232 ptr = (unsigned long)(di + 1);
2233 ptr += name_len;
2234 }
2235 ret = 0;
2236out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002237 btrfs_release_path(path);
2238 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002239 return ret;
2240}
2241
Filipe Manana4f764e52015-02-23 19:53:35 +00002242static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2243 struct btrfs_root *root,
2244 struct btrfs_root *log,
2245 struct btrfs_path *path,
2246 const u64 ino)
2247{
2248 struct btrfs_key search_key;
2249 struct btrfs_path *log_path;
2250 int i;
2251 int nritems;
2252 int ret;
2253
2254 log_path = btrfs_alloc_path();
2255 if (!log_path)
2256 return -ENOMEM;
2257
2258 search_key.objectid = ino;
2259 search_key.type = BTRFS_XATTR_ITEM_KEY;
2260 search_key.offset = 0;
2261again:
2262 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2263 if (ret < 0)
2264 goto out;
2265process_leaf:
2266 nritems = btrfs_header_nritems(path->nodes[0]);
2267 for (i = path->slots[0]; i < nritems; i++) {
2268 struct btrfs_key key;
2269 struct btrfs_dir_item *di;
2270 struct btrfs_dir_item *log_di;
2271 u32 total_size;
2272 u32 cur;
2273
2274 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2275 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2276 ret = 0;
2277 goto out;
2278 }
2279
2280 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2281 total_size = btrfs_item_size_nr(path->nodes[0], i);
2282 cur = 0;
2283 while (cur < total_size) {
2284 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2285 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2286 u32 this_len = sizeof(*di) + name_len + data_len;
2287 char *name;
2288
2289 name = kmalloc(name_len, GFP_NOFS);
2290 if (!name) {
2291 ret = -ENOMEM;
2292 goto out;
2293 }
2294 read_extent_buffer(path->nodes[0], name,
2295 (unsigned long)(di + 1), name_len);
2296
2297 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2298 name, name_len, 0);
2299 btrfs_release_path(log_path);
2300 if (!log_di) {
2301 /* Doesn't exist in log tree, so delete it. */
2302 btrfs_release_path(path);
2303 di = btrfs_lookup_xattr(trans, root, path, ino,
2304 name, name_len, -1);
2305 kfree(name);
2306 if (IS_ERR(di)) {
2307 ret = PTR_ERR(di);
2308 goto out;
2309 }
2310 ASSERT(di);
2311 ret = btrfs_delete_one_dir_name(trans, root,
2312 path, di);
2313 if (ret)
2314 goto out;
2315 btrfs_release_path(path);
2316 search_key = key;
2317 goto again;
2318 }
2319 kfree(name);
2320 if (IS_ERR(log_di)) {
2321 ret = PTR_ERR(log_di);
2322 goto out;
2323 }
2324 cur += this_len;
2325 di = (struct btrfs_dir_item *)((char *)di + this_len);
2326 }
2327 }
2328 ret = btrfs_next_leaf(root, path);
2329 if (ret > 0)
2330 ret = 0;
2331 else if (ret == 0)
2332 goto process_leaf;
2333out:
2334 btrfs_free_path(log_path);
2335 btrfs_release_path(path);
2336 return ret;
2337}
2338
2339
Chris Masone02119d2008-09-05 16:13:11 -04002340/*
2341 * deletion replay happens before we copy any new directory items
2342 * out of the log or out of backreferences from inodes. It
2343 * scans the log to find ranges of keys that log is authoritative for,
2344 * and then scans the directory to find items in those ranges that are
2345 * not present in the log.
2346 *
2347 * Anything we don't find in the log is unlinked and removed from the
2348 * directory.
2349 */
2350static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2351 struct btrfs_root *root,
2352 struct btrfs_root *log,
2353 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002354 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002355{
2356 u64 range_start;
2357 u64 range_end;
2358 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2359 int ret = 0;
2360 struct btrfs_key dir_key;
2361 struct btrfs_key found_key;
2362 struct btrfs_path *log_path;
2363 struct inode *dir;
2364
2365 dir_key.objectid = dirid;
2366 dir_key.type = BTRFS_DIR_ITEM_KEY;
2367 log_path = btrfs_alloc_path();
2368 if (!log_path)
2369 return -ENOMEM;
2370
2371 dir = read_one_inode(root, dirid);
2372 /* it isn't an error if the inode isn't there, that can happen
2373 * because we replay the deletes before we copy in the inode item
2374 * from the log
2375 */
2376 if (!dir) {
2377 btrfs_free_path(log_path);
2378 return 0;
2379 }
2380again:
2381 range_start = 0;
2382 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002383 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002384 if (del_all)
2385 range_end = (u64)-1;
2386 else {
2387 ret = find_dir_range(log, path, dirid, key_type,
2388 &range_start, &range_end);
2389 if (ret != 0)
2390 break;
2391 }
Chris Masone02119d2008-09-05 16:13:11 -04002392
2393 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002394 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002395 int nritems;
2396 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2397 0, 0);
2398 if (ret < 0)
2399 goto out;
2400
2401 nritems = btrfs_header_nritems(path->nodes[0]);
2402 if (path->slots[0] >= nritems) {
2403 ret = btrfs_next_leaf(root, path);
Liu Bob98def72018-04-03 01:59:48 +08002404 if (ret == 1)
Chris Masone02119d2008-09-05 16:13:11 -04002405 break;
Liu Bob98def72018-04-03 01:59:48 +08002406 else if (ret < 0)
2407 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002408 }
2409 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2410 path->slots[0]);
2411 if (found_key.objectid != dirid ||
2412 found_key.type != dir_key.type)
2413 goto next_type;
2414
2415 if (found_key.offset > range_end)
2416 break;
2417
2418 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002419 log_path, dir,
2420 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002421 if (ret)
2422 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002423 if (found_key.offset == (u64)-1)
2424 break;
2425 dir_key.offset = found_key.offset + 1;
2426 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002427 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002428 if (range_end == (u64)-1)
2429 break;
2430 range_start = range_end + 1;
2431 }
2432
2433next_type:
2434 ret = 0;
2435 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2436 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2437 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002438 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002439 goto again;
2440 }
2441out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002442 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002443 btrfs_free_path(log_path);
2444 iput(dir);
2445 return ret;
2446}
2447
2448/*
2449 * the process_func used to replay items from the log tree. This
2450 * gets called in two different stages. The first stage just looks
2451 * for inodes and makes sure they are all copied into the subvolume.
2452 *
2453 * The second stage copies all the other item types from the log into
2454 * the subvolume. The two stage approach is slower, but gets rid of
2455 * lots of complexity around inodes referencing other inodes that exist
2456 * only in the log (references come from either directory items or inode
2457 * back refs).
2458 */
2459static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +08002460 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -04002461{
2462 int nritems;
2463 struct btrfs_path *path;
2464 struct btrfs_root *root = wc->replay_dest;
2465 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002466 int i;
2467 int ret;
2468
Qu Wenruo581c1762018-03-29 09:08:11 +08002469 ret = btrfs_read_buffer(eb, gen, level, NULL);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002470 if (ret)
2471 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002472
2473 level = btrfs_header_level(eb);
2474
2475 if (level != 0)
2476 return 0;
2477
2478 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002479 if (!path)
2480 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002481
2482 nritems = btrfs_header_nritems(eb);
2483 for (i = 0; i < nritems; i++) {
2484 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002485
2486 /* inode keys are done during the first stage */
2487 if (key.type == BTRFS_INODE_ITEM_KEY &&
2488 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002489 struct btrfs_inode_item *inode_item;
2490 u32 mode;
2491
2492 inode_item = btrfs_item_ptr(eb, i,
2493 struct btrfs_inode_item);
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002494 /*
2495 * If we have a tmpfile (O_TMPFILE) that got fsync'ed
2496 * and never got linked before the fsync, skip it, as
2497 * replaying it is pointless since it would be deleted
2498 * later. We skip logging tmpfiles, but it's always
2499 * possible we are replaying a log created with a kernel
2500 * that used to log tmpfiles.
2501 */
2502 if (btrfs_inode_nlink(eb, inode_item) == 0) {
2503 wc->ignore_cur_inode = true;
2504 continue;
2505 } else {
2506 wc->ignore_cur_inode = false;
2507 }
Filipe Manana4f764e52015-02-23 19:53:35 +00002508 ret = replay_xattr_deletes(wc->trans, root, log,
2509 path, key.objectid);
2510 if (ret)
2511 break;
Chris Masone02119d2008-09-05 16:13:11 -04002512 mode = btrfs_inode_mode(eb, inode_item);
2513 if (S_ISDIR(mode)) {
2514 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002515 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002516 if (ret)
2517 break;
Chris Masone02119d2008-09-05 16:13:11 -04002518 }
2519 ret = overwrite_item(wc->trans, root, path,
2520 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002521 if (ret)
2522 break;
Chris Masone02119d2008-09-05 16:13:11 -04002523
Filipe Manana471d5572018-04-05 22:55:12 +01002524 /*
2525 * Before replaying extents, truncate the inode to its
2526 * size. We need to do it now and not after log replay
2527 * because before an fsync we can have prealloc extents
2528 * added beyond the inode's i_size. If we did it after,
2529 * through orphan cleanup for example, we would drop
2530 * those prealloc extents just after replaying them.
Chris Masone02119d2008-09-05 16:13:11 -04002531 */
2532 if (S_ISREG(mode)) {
Filipe Manana471d5572018-04-05 22:55:12 +01002533 struct inode *inode;
2534 u64 from;
2535
2536 inode = read_one_inode(root, key.objectid);
2537 if (!inode) {
2538 ret = -EIO;
2539 break;
2540 }
2541 from = ALIGN(i_size_read(inode),
2542 root->fs_info->sectorsize);
2543 ret = btrfs_drop_extents(wc->trans, root, inode,
2544 from, (u64)-1, 1);
Filipe Manana471d5572018-04-05 22:55:12 +01002545 if (!ret) {
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002546 /* Update the inode's nbytes. */
Filipe Manana471d5572018-04-05 22:55:12 +01002547 ret = btrfs_update_inode(wc->trans,
2548 root, inode);
2549 }
2550 iput(inode);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002551 if (ret)
2552 break;
Chris Masone02119d2008-09-05 16:13:11 -04002553 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002554
Chris Masone02119d2008-09-05 16:13:11 -04002555 ret = link_to_fixup_dir(wc->trans, root,
2556 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002557 if (ret)
2558 break;
Chris Masone02119d2008-09-05 16:13:11 -04002559 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002560
Filipe Mananaf2d72f42018-10-08 11:12:55 +01002561 if (wc->ignore_cur_inode)
2562 continue;
2563
Josef Bacikdd8e7212013-09-11 11:57:23 -04002564 if (key.type == BTRFS_DIR_INDEX_KEY &&
2565 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2566 ret = replay_one_dir_item(wc->trans, root, path,
2567 eb, i, &key);
2568 if (ret)
2569 break;
2570 }
2571
Chris Masone02119d2008-09-05 16:13:11 -04002572 if (wc->stage < LOG_WALK_REPLAY_ALL)
2573 continue;
2574
2575 /* these keys are simply copied */
2576 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2577 ret = overwrite_item(wc->trans, root, path,
2578 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002579 if (ret)
2580 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002581 } else if (key.type == BTRFS_INODE_REF_KEY ||
2582 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002583 ret = add_inode_ref(wc->trans, root, log, path,
2584 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002585 if (ret && ret != -ENOENT)
2586 break;
2587 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002588 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2589 ret = replay_one_extent(wc->trans, root, path,
2590 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002591 if (ret)
2592 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002593 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002594 ret = replay_one_dir_item(wc->trans, root, path,
2595 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002596 if (ret)
2597 break;
Chris Masone02119d2008-09-05 16:13:11 -04002598 }
2599 }
2600 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002601 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002602}
2603
Chris Masond3977122009-01-05 21:25:51 -05002604static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002605 struct btrfs_root *root,
2606 struct btrfs_path *path, int *level,
2607 struct walk_control *wc)
2608{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002609 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002610 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002611 u64 bytenr;
2612 u64 ptr_gen;
2613 struct extent_buffer *next;
2614 struct extent_buffer *cur;
2615 struct extent_buffer *parent;
2616 u32 blocksize;
2617 int ret = 0;
2618
2619 WARN_ON(*level < 0);
2620 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2621
Chris Masond3977122009-01-05 21:25:51 -05002622 while (*level > 0) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002623 struct btrfs_key first_key;
2624
Chris Masone02119d2008-09-05 16:13:11 -04002625 WARN_ON(*level < 0);
2626 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2627 cur = path->nodes[*level];
2628
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302629 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002630
2631 if (path->slots[*level] >=
2632 btrfs_header_nritems(cur))
2633 break;
2634
2635 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2636 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08002637 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002638 blocksize = fs_info->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002639
2640 parent = path->nodes[*level];
2641 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002642
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002643 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002644 if (IS_ERR(next))
2645 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002646
Chris Masone02119d2008-09-05 16:13:11 -04002647 if (*level == 1) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002648 ret = wc->process_func(root, next, wc, ptr_gen,
2649 *level - 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002650 if (ret) {
2651 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002652 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002653 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002654
Chris Masone02119d2008-09-05 16:13:11 -04002655 path->slots[*level]++;
2656 if (wc->free) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002657 ret = btrfs_read_buffer(next, ptr_gen,
2658 *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002659 if (ret) {
2660 free_extent_buffer(next);
2661 return ret;
2662 }
Chris Masone02119d2008-09-05 16:13:11 -04002663
Josef Bacik681ae502013-10-07 15:11:00 -04002664 if (trans) {
2665 btrfs_tree_lock(next);
2666 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002667 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002668 btrfs_wait_tree_block_writeback(next);
2669 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002670 } else {
2671 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2672 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002673 }
Chris Masone02119d2008-09-05 16:13:11 -04002674
Chris Masone02119d2008-09-05 16:13:11 -04002675 WARN_ON(root_owner !=
2676 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002677 ret = btrfs_free_and_pin_reserved_extent(
2678 fs_info, bytenr,
2679 blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002680 if (ret) {
2681 free_extent_buffer(next);
2682 return ret;
2683 }
Chris Masone02119d2008-09-05 16:13:11 -04002684 }
2685 free_extent_buffer(next);
2686 continue;
2687 }
Qu Wenruo581c1762018-03-29 09:08:11 +08002688 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002689 if (ret) {
2690 free_extent_buffer(next);
2691 return ret;
2692 }
Chris Masone02119d2008-09-05 16:13:11 -04002693
2694 WARN_ON(*level <= 0);
2695 if (path->nodes[*level-1])
2696 free_extent_buffer(path->nodes[*level-1]);
2697 path->nodes[*level-1] = next;
2698 *level = btrfs_header_level(next);
2699 path->slots[*level] = 0;
2700 cond_resched();
2701 }
2702 WARN_ON(*level < 0);
2703 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2704
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002705 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002706
2707 cond_resched();
2708 return 0;
2709}
2710
Chris Masond3977122009-01-05 21:25:51 -05002711static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002712 struct btrfs_root *root,
2713 struct btrfs_path *path, int *level,
2714 struct walk_control *wc)
2715{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002716 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002717 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002718 int i;
2719 int slot;
2720 int ret;
2721
Chris Masond3977122009-01-05 21:25:51 -05002722 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002723 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002724 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002725 path->slots[i]++;
2726 *level = i;
2727 WARN_ON(*level == 0);
2728 return 0;
2729 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002730 struct extent_buffer *parent;
2731 if (path->nodes[*level] == root->node)
2732 parent = path->nodes[*level];
2733 else
2734 parent = path->nodes[*level + 1];
2735
2736 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002737 ret = wc->process_func(root, path->nodes[*level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002738 btrfs_header_generation(path->nodes[*level]),
2739 *level);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002740 if (ret)
2741 return ret;
2742
Chris Masone02119d2008-09-05 16:13:11 -04002743 if (wc->free) {
2744 struct extent_buffer *next;
2745
2746 next = path->nodes[*level];
2747
Josef Bacik681ae502013-10-07 15:11:00 -04002748 if (trans) {
2749 btrfs_tree_lock(next);
2750 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002751 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002752 btrfs_wait_tree_block_writeback(next);
2753 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002754 } else {
2755 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2756 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002757 }
Chris Masone02119d2008-09-05 16:13:11 -04002758
Chris Masone02119d2008-09-05 16:13:11 -04002759 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002760 ret = btrfs_free_and_pin_reserved_extent(
2761 fs_info,
Chris Masone02119d2008-09-05 16:13:11 -04002762 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002763 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002764 if (ret)
2765 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002766 }
2767 free_extent_buffer(path->nodes[*level]);
2768 path->nodes[*level] = NULL;
2769 *level = i + 1;
2770 }
2771 }
2772 return 1;
2773}
2774
2775/*
2776 * drop the reference count on the tree rooted at 'snap'. This traverses
2777 * the tree freeing any blocks that have a ref count of zero after being
2778 * decremented.
2779 */
2780static int walk_log_tree(struct btrfs_trans_handle *trans,
2781 struct btrfs_root *log, struct walk_control *wc)
2782{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002783 struct btrfs_fs_info *fs_info = log->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002784 int ret = 0;
2785 int wret;
2786 int level;
2787 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002788 int orig_level;
2789
2790 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002791 if (!path)
2792 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002793
2794 level = btrfs_header_level(log->node);
2795 orig_level = level;
2796 path->nodes[level] = log->node;
2797 extent_buffer_get(log->node);
2798 path->slots[level] = 0;
2799
Chris Masond3977122009-01-05 21:25:51 -05002800 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002801 wret = walk_down_log_tree(trans, log, path, &level, wc);
2802 if (wret > 0)
2803 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002804 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002805 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002806 goto out;
2807 }
Chris Masone02119d2008-09-05 16:13:11 -04002808
2809 wret = walk_up_log_tree(trans, log, path, &level, wc);
2810 if (wret > 0)
2811 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002812 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002813 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002814 goto out;
2815 }
Chris Masone02119d2008-09-05 16:13:11 -04002816 }
2817
2818 /* was the root node processed? if not, catch it here */
2819 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002820 ret = wc->process_func(log, path->nodes[orig_level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002821 btrfs_header_generation(path->nodes[orig_level]),
2822 orig_level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002823 if (ret)
2824 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002825 if (wc->free) {
2826 struct extent_buffer *next;
2827
2828 next = path->nodes[orig_level];
2829
Josef Bacik681ae502013-10-07 15:11:00 -04002830 if (trans) {
2831 btrfs_tree_lock(next);
2832 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002833 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002834 btrfs_wait_tree_block_writeback(next);
2835 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002836 } else {
2837 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2838 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002839 }
Chris Masone02119d2008-09-05 16:13:11 -04002840
Chris Masone02119d2008-09-05 16:13:11 -04002841 WARN_ON(log->root_key.objectid !=
2842 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002843 ret = btrfs_free_and_pin_reserved_extent(fs_info,
2844 next->start, next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002845 if (ret)
2846 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002847 }
2848 }
2849
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002850out:
Chris Masone02119d2008-09-05 16:13:11 -04002851 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002852 return ret;
2853}
2854
Yan Zheng7237f182009-01-21 12:54:03 -05002855/*
2856 * helper function to update the item for a given subvolumes log root
2857 * in the tree of log roots
2858 */
2859static int update_log_root(struct btrfs_trans_handle *trans,
2860 struct btrfs_root *log)
2861{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002862 struct btrfs_fs_info *fs_info = log->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -05002863 int ret;
2864
2865 if (log->log_transid == 1) {
2866 /* insert root item on the first sync */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002867 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002868 &log->root_key, &log->root_item);
2869 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002870 ret = btrfs_update_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002871 &log->root_key, &log->root_item);
2872 }
2873 return ret;
2874}
2875
Zhaolei60d53eb2015-08-17 18:44:46 +08002876static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002877{
2878 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002879 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002880
Yan Zheng7237f182009-01-21 12:54:03 -05002881 /*
2882 * we only allow two pending log transactions at a time,
2883 * so we know that if ours is more than 2 older than the
2884 * current transaction, we're done
2885 */
Liu Bo49e83f52017-09-01 16:14:30 -06002886 for (;;) {
Yan Zheng7237f182009-01-21 12:54:03 -05002887 prepare_to_wait(&root->log_commit_wait[index],
2888 &wait, TASK_UNINTERRUPTIBLE);
Liu Bo49e83f52017-09-01 16:14:30 -06002889
2890 if (!(root->log_transid_committed < transid &&
2891 atomic_read(&root->log_commit[index])))
2892 break;
2893
Yan Zheng7237f182009-01-21 12:54:03 -05002894 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002895 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002896 mutex_lock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002897 }
2898 finish_wait(&root->log_commit_wait[index], &wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002899}
2900
Zhaolei60d53eb2015-08-17 18:44:46 +08002901static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002902{
2903 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002904
Liu Bo49e83f52017-09-01 16:14:30 -06002905 for (;;) {
2906 prepare_to_wait(&root->log_writer_wait, &wait,
2907 TASK_UNINTERRUPTIBLE);
2908 if (!atomic_read(&root->log_writers))
2909 break;
2910
Yan Zheng7237f182009-01-21 12:54:03 -05002911 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002912 schedule();
Filipe Manana575849e2015-02-11 11:12:39 +00002913 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002914 }
Liu Bo49e83f52017-09-01 16:14:30 -06002915 finish_wait(&root->log_writer_wait, &wait);
Chris Masone02119d2008-09-05 16:13:11 -04002916}
2917
Miao Xie8b050d32014-02-20 18:08:58 +08002918static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2919 struct btrfs_log_ctx *ctx)
2920{
2921 if (!ctx)
2922 return;
2923
2924 mutex_lock(&root->log_mutex);
2925 list_del_init(&ctx->list);
2926 mutex_unlock(&root->log_mutex);
2927}
2928
2929/*
2930 * Invoked in log mutex context, or be sure there is no other task which
2931 * can access the list.
2932 */
2933static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2934 int index, int error)
2935{
2936 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002937 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002938
Chris Mason570dd452016-10-27 10:42:20 -07002939 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2940 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002941 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002942 }
Miao Xie8b050d32014-02-20 18:08:58 +08002943
2944 INIT_LIST_HEAD(&root->log_ctxs[index]);
2945}
2946
Chris Masone02119d2008-09-05 16:13:11 -04002947/*
2948 * btrfs_sync_log does sends a given tree log down to the disk and
2949 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002950 * you know that any inodes previously logged are safely on disk only
2951 * if it returns 0.
2952 *
2953 * Any other return value means you need to call btrfs_commit_transaction.
2954 * Some of the edge cases for fsyncing directories that have had unlinks
2955 * or renames done in the past mean that sometimes the only safe
2956 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2957 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002958 */
2959int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002960 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002961{
Yan Zheng7237f182009-01-21 12:54:03 -05002962 int index1;
2963 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002964 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002965 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002966 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002967 struct btrfs_root *log = root->log_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002968 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002969 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002970 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002971 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002972
Yan Zheng7237f182009-01-21 12:54:03 -05002973 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002974 log_transid = ctx->log_transid;
2975 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002976 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002977 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002978 }
Miao Xied1433de2014-02-20 18:08:59 +08002979
2980 index1 = log_transid % 2;
2981 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002982 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002983 mutex_unlock(&root->log_mutex);
2984 return ctx->log_ret;
2985 }
2986 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002987 atomic_set(&root->log_commit[index1], 1);
2988
2989 /* wait for previous tree log sync to complete */
2990 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002991 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002992
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002993 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002994 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002995 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002996 if (!btrfs_test_opt(fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08002997 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002998 mutex_unlock(&root->log_mutex);
2999 schedule_timeout_uninterruptible(1);
3000 mutex_lock(&root->log_mutex);
3001 }
Zhaolei60d53eb2015-08-17 18:44:46 +08003002 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06003003 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04003004 break;
3005 }
Chris Masond0c803c2008-09-11 16:17:57 -04003006
Chris Mason12fcfd22009-03-24 10:24:20 -04003007 /* bail out if we need to do a full commit */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003008 if (btrfs_need_log_full_commit(fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04003009 ret = -EAGAIN;
3010 mutex_unlock(&root->log_mutex);
3011 goto out;
3012 }
3013
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003014 if (log_transid % 2 == 0)
3015 mark = EXTENT_DIRTY;
3016 else
3017 mark = EXTENT_NEW;
3018
Chris Mason690587d2009-10-13 13:29:19 -04003019 /* we start IO on all the marked extents here, but we don't actually
3020 * wait for them until later.
3021 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00003022 blk_start_plug(&plug);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003023 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003024 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003025 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04003026 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003027 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003028 mutex_unlock(&root->log_mutex);
3029 goto out;
3030 }
Yan Zheng7237f182009-01-21 12:54:03 -05003031
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003032 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05003033
Yan Zheng7237f182009-01-21 12:54:03 -05003034 root->log_transid++;
3035 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04003036 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05003037 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003038 * IO has been started, blocks of the log tree have WRITTEN flag set
3039 * in their headers. new modifications of the log will be written to
3040 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05003041 */
3042 mutex_unlock(&root->log_mutex);
3043
Filipe Manana28a23592016-08-23 21:13:51 +01003044 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08003045
Yan Zheng7237f182009-01-21 12:54:03 -05003046 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06003047 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05003048 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08003049
3050 index2 = log_root_tree->log_transid % 2;
3051 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
3052 root_log_ctx.log_transid = log_root_tree->log_transid;
3053
Yan Zheng7237f182009-01-21 12:54:03 -05003054 mutex_unlock(&log_root_tree->log_mutex);
3055
3056 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05003057
3058 mutex_lock(&log_root_tree->log_mutex);
3059 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba093258e2018-02-26 16:15:17 +01003060 /* atomic_dec_and_test implies a barrier */
3061 cond_wake_up_nomb(&log_root_tree->log_writer_wait);
Yan Zheng7237f182009-01-21 12:54:03 -05003062 }
3063
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003064 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08003065 if (!list_empty(&root_log_ctx.list))
3066 list_del_init(&root_log_ctx.list);
3067
Miao Xiec6adc9c2013-05-28 10:05:39 +00003068 blk_finish_plug(&plug);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003069 btrfs_set_log_full_commit(fs_info, trans);
Miao Xie995946d2014-04-02 19:51:06 +08003070
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003071 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003072 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003073 mutex_unlock(&log_root_tree->log_mutex);
3074 goto out;
3075 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003076 btrfs_wait_tree_log_extents(log, mark);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003077 mutex_unlock(&log_root_tree->log_mutex);
3078 ret = -EAGAIN;
3079 goto out;
3080 }
3081
Miao Xied1433de2014-02-20 18:08:59 +08003082 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08003083 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07003084 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08003085 mutex_unlock(&log_root_tree->log_mutex);
3086 ret = root_log_ctx.log_ret;
3087 goto out;
3088 }
Miao Xie8b050d32014-02-20 18:08:58 +08003089
Miao Xied1433de2014-02-20 18:08:59 +08003090 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05003091 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003092 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003093 ret = btrfs_wait_tree_log_extents(log, mark);
Zhaolei60d53eb2015-08-17 18:44:46 +08003094 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003095 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003096 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003097 if (!ret)
3098 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05003099 goto out;
3100 }
Miao Xied1433de2014-02-20 18:08:59 +08003101 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003102 atomic_set(&log_root_tree->log_commit[index2], 1);
3103
Chris Mason12fcfd22009-03-24 10:24:20 -04003104 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003105 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003106 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04003107 }
Yan Zheng7237f182009-01-21 12:54:03 -05003108
Zhaolei60d53eb2015-08-17 18:44:46 +08003109 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04003110
3111 /*
3112 * now that we've moved on to the tree of log tree roots,
3113 * check the full commit flag again
3114 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003115 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003116 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003117 btrfs_wait_tree_log_extents(log, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04003118 mutex_unlock(&log_root_tree->log_mutex);
3119 ret = -EAGAIN;
3120 goto out_wake_log_root;
3121 }
Yan Zheng7237f182009-01-21 12:54:03 -05003122
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003123 ret = btrfs_write_marked_extents(fs_info,
Miao Xiec6adc9c2013-05-28 10:05:39 +00003124 &log_root_tree->dirty_log_pages,
3125 EXTENT_DIRTY | EXTENT_NEW);
3126 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003127 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003128 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003129 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003130 mutex_unlock(&log_root_tree->log_mutex);
3131 goto out_wake_log_root;
3132 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003133 ret = btrfs_wait_tree_log_extents(log, mark);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003134 if (!ret)
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003135 ret = btrfs_wait_tree_log_extents(log_root_tree,
3136 EXTENT_NEW | EXTENT_DIRTY);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003137 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003138 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003139 mutex_unlock(&log_root_tree->log_mutex);
3140 goto out_wake_log_root;
3141 }
Chris Masone02119d2008-09-05 16:13:11 -04003142
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003143 btrfs_set_super_log_root(fs_info->super_for_commit,
3144 log_root_tree->node->start);
3145 btrfs_set_super_log_root_level(fs_info->super_for_commit,
3146 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04003147
Yan Zheng7237f182009-01-21 12:54:03 -05003148 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05003149 mutex_unlock(&log_root_tree->log_mutex);
3150
3151 /*
3152 * nobody else is going to jump in and write the the ctree
3153 * super here because the log_commit atomic below is protecting
3154 * us. We must be called with a transaction handle pinning
3155 * the running transaction open, so a full commit can't hop
3156 * in and cause problems either.
3157 */
David Sterbaeece6a92017-02-10 19:04:32 +01003158 ret = write_all_supers(fs_info, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003159 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003160 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003161 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003162 goto out_wake_log_root;
3163 }
Yan Zheng7237f182009-01-21 12:54:03 -05003164
Chris Mason257c62e2009-10-13 13:21:08 -04003165 mutex_lock(&root->log_mutex);
3166 if (root->last_log_commit < log_transid)
3167 root->last_log_commit = log_transid;
3168 mutex_unlock(&root->log_mutex);
3169
Chris Mason12fcfd22009-03-24 10:24:20 -04003170out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07003171 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003172 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3173
Miao Xied1433de2014-02-20 18:08:59 +08003174 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003175 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003176 mutex_unlock(&log_root_tree->log_mutex);
3177
David Sterba33a9eca2015-10-10 18:35:10 +02003178 /*
David Sterba093258e2018-02-26 16:15:17 +01003179 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3180 * all the updates above are seen by the woken threads. It might not be
3181 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003182 */
David Sterba093258e2018-02-26 16:15:17 +01003183 cond_wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04003184out:
Miao Xied1433de2014-02-20 18:08:59 +08003185 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07003186 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08003187 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003188 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003189 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003190
David Sterba33a9eca2015-10-10 18:35:10 +02003191 /*
David Sterba093258e2018-02-26 16:15:17 +01003192 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3193 * all the updates above are seen by the woken threads. It might not be
3194 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003195 */
David Sterba093258e2018-02-26 16:15:17 +01003196 cond_wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003197 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003198}
3199
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003200static void free_log_tree(struct btrfs_trans_handle *trans,
3201 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003202{
3203 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04003204 u64 start;
3205 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04003206 struct walk_control wc = {
3207 .free = 1,
3208 .process_func = process_one_buffer
3209 };
3210
Josef Bacik681ae502013-10-07 15:11:00 -04003211 ret = walk_log_tree(trans, log, &wc);
Jeff Mahoney374b0e22018-09-06 16:59:33 -04003212 if (ret) {
3213 if (trans)
3214 btrfs_abort_transaction(trans, ret);
3215 else
3216 btrfs_handle_fs_error(log->fs_info, ret, NULL);
3217 }
Chris Masone02119d2008-09-05 16:13:11 -04003218
Chris Masond3977122009-01-05 21:25:51 -05003219 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003220 ret = find_first_extent_bit(&log->dirty_log_pages,
Liu Bo55237a52018-01-25 11:02:52 -07003221 0, &start, &end,
3222 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
Josef Bacike6138872012-09-27 17:07:30 -04003223 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003224 if (ret)
3225 break;
3226
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003227 clear_extent_bits(&log->dirty_log_pages, start, end,
Liu Bo55237a52018-01-25 11:02:52 -07003228 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Chris Masond0c803c2008-09-11 16:17:57 -04003229 }
3230
Yan Zheng7237f182009-01-21 12:54:03 -05003231 free_extent_buffer(log->node);
3232 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003233}
3234
3235/*
3236 * free all the extents used by the tree log. This should be called
3237 * at commit time of the full transaction
3238 */
3239int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3240{
3241 if (root->log_root) {
3242 free_log_tree(trans, root->log_root);
3243 root->log_root = NULL;
3244 }
3245 return 0;
3246}
3247
3248int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3249 struct btrfs_fs_info *fs_info)
3250{
3251 if (fs_info->log_root_tree) {
3252 free_log_tree(trans, fs_info->log_root_tree);
3253 fs_info->log_root_tree = NULL;
3254 }
Chris Masone02119d2008-09-05 16:13:11 -04003255 return 0;
3256}
3257
3258/*
Chris Masone02119d2008-09-05 16:13:11 -04003259 * If both a file and directory are logged, and unlinks or renames are
3260 * mixed in, we have a few interesting corners:
3261 *
3262 * create file X in dir Y
3263 * link file X to X.link in dir Y
3264 * fsync file X
3265 * unlink file X but leave X.link
3266 * fsync dir Y
3267 *
3268 * After a crash we would expect only X.link to exist. But file X
3269 * didn't get fsync'd again so the log has back refs for X and X.link.
3270 *
3271 * We solve this by removing directory entries and inode backrefs from the
3272 * log when a file that was logged in the current transaction is
3273 * unlinked. Any later fsync will include the updated log entries, and
3274 * we'll be able to reconstruct the proper directory items from backrefs.
3275 *
3276 * This optimizations allows us to avoid relogging the entire inode
3277 * or the entire directory.
3278 */
3279int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3280 struct btrfs_root *root,
3281 const char *name, int name_len,
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003282 struct btrfs_inode *dir, u64 index)
Chris Masone02119d2008-09-05 16:13:11 -04003283{
3284 struct btrfs_root *log;
3285 struct btrfs_dir_item *di;
3286 struct btrfs_path *path;
3287 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003288 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003289 int bytes_del = 0;
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003290 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003291
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003292 if (dir->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003293 return 0;
3294
Chris Masone02119d2008-09-05 16:13:11 -04003295 ret = join_running_log_trans(root);
3296 if (ret)
3297 return 0;
3298
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003299 mutex_lock(&dir->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003300
3301 log = root->log_root;
3302 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003303 if (!path) {
3304 err = -ENOMEM;
3305 goto out_unlock;
3306 }
liubo2a29edc2011-01-26 06:22:08 +00003307
Li Zefan33345d012011-04-20 10:31:50 +08003308 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003309 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003310 if (IS_ERR(di)) {
3311 err = PTR_ERR(di);
3312 goto fail;
3313 }
3314 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003315 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3316 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003317 if (ret) {
3318 err = ret;
3319 goto fail;
3320 }
Chris Masone02119d2008-09-05 16:13:11 -04003321 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003322 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003323 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003324 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003325 if (IS_ERR(di)) {
3326 err = PTR_ERR(di);
3327 goto fail;
3328 }
3329 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003330 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3331 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003332 if (ret) {
3333 err = ret;
3334 goto fail;
3335 }
Chris Masone02119d2008-09-05 16:13:11 -04003336 }
3337
3338 /* update the directory size in the log to reflect the names
3339 * we have removed
3340 */
3341 if (bytes_del) {
3342 struct btrfs_key key;
3343
Li Zefan33345d012011-04-20 10:31:50 +08003344 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003345 key.offset = 0;
3346 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003347 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003348
3349 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003350 if (ret < 0) {
3351 err = ret;
3352 goto fail;
3353 }
Chris Masone02119d2008-09-05 16:13:11 -04003354 if (ret == 0) {
3355 struct btrfs_inode_item *item;
3356 u64 i_size;
3357
3358 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3359 struct btrfs_inode_item);
3360 i_size = btrfs_inode_size(path->nodes[0], item);
3361 if (i_size > bytes_del)
3362 i_size -= bytes_del;
3363 else
3364 i_size = 0;
3365 btrfs_set_inode_size(path->nodes[0], item, i_size);
3366 btrfs_mark_buffer_dirty(path->nodes[0]);
3367 } else
3368 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003369 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003370 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003371fail:
Chris Masone02119d2008-09-05 16:13:11 -04003372 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003373out_unlock:
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003374 mutex_unlock(&dir->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003375 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003376 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003377 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003378 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003379 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003380
Chris Mason12fcfd22009-03-24 10:24:20 -04003381 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003382
Andi Kleen411fc6b2010-10-29 15:14:31 -04003383 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003384}
3385
3386/* see comments for btrfs_del_dir_entries_in_log */
3387int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3388 struct btrfs_root *root,
3389 const char *name, int name_len,
Nikolay Borisova491abb2017-01-18 00:31:33 +02003390 struct btrfs_inode *inode, u64 dirid)
Chris Masone02119d2008-09-05 16:13:11 -04003391{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003392 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003393 struct btrfs_root *log;
3394 u64 index;
3395 int ret;
3396
Nikolay Borisova491abb2017-01-18 00:31:33 +02003397 if (inode->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003398 return 0;
3399
Chris Masone02119d2008-09-05 16:13:11 -04003400 ret = join_running_log_trans(root);
3401 if (ret)
3402 return 0;
3403 log = root->log_root;
Nikolay Borisova491abb2017-01-18 00:31:33 +02003404 mutex_lock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003405
Nikolay Borisova491abb2017-01-18 00:31:33 +02003406 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003407 dirid, &index);
Nikolay Borisova491abb2017-01-18 00:31:33 +02003408 mutex_unlock(&inode->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003409 if (ret == -ENOSPC) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003410 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003411 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003412 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003413 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003414 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003415
Chris Masone02119d2008-09-05 16:13:11 -04003416 return ret;
3417}
3418
3419/*
3420 * creates a range item in the log for 'dirid'. first_offset and
3421 * last_offset tell us which parts of the key space the log should
3422 * be considered authoritative for.
3423 */
3424static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3425 struct btrfs_root *log,
3426 struct btrfs_path *path,
3427 int key_type, u64 dirid,
3428 u64 first_offset, u64 last_offset)
3429{
3430 int ret;
3431 struct btrfs_key key;
3432 struct btrfs_dir_log_item *item;
3433
3434 key.objectid = dirid;
3435 key.offset = first_offset;
3436 if (key_type == BTRFS_DIR_ITEM_KEY)
3437 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3438 else
3439 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3440 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003441 if (ret)
3442 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003443
3444 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3445 struct btrfs_dir_log_item);
3446 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3447 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003448 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003449 return 0;
3450}
3451
3452/*
3453 * log all the items included in the current transaction for a given
3454 * directory. This also creates the range items in the log tree required
3455 * to replay anything deleted before the fsync
3456 */
3457static noinline int log_dir_items(struct btrfs_trans_handle *trans,
Nikolay Borisov684a5772017-01-18 00:31:41 +02003458 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003459 struct btrfs_path *path,
3460 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003461 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003462 u64 min_offset, u64 *last_offset_ret)
3463{
3464 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003465 struct btrfs_root *log = root->log_root;
3466 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003467 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003468 int ret;
3469 int i;
3470 int nritems;
3471 u64 first_offset = min_offset;
3472 u64 last_offset = (u64)-1;
Nikolay Borisov684a5772017-01-18 00:31:41 +02003473 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003474
3475 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003476
Li Zefan33345d012011-04-20 10:31:50 +08003477 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003478 min_key.type = key_type;
3479 min_key.offset = min_offset;
3480
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003481 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003482
3483 /*
3484 * we didn't find anything from this transaction, see if there
3485 * is anything at all
3486 */
Li Zefan33345d012011-04-20 10:31:50 +08003487 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3488 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003489 min_key.type = key_type;
3490 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003491 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003492 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3493 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003494 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003495 return ret;
3496 }
Li Zefan33345d012011-04-20 10:31:50 +08003497 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003498
3499 /* if ret == 0 there are items for this type,
3500 * create a range to tell us the last key of this type.
3501 * otherwise, there are no items in this directory after
3502 * *min_offset, and we create a range to indicate that.
3503 */
3504 if (ret == 0) {
3505 struct btrfs_key tmp;
3506 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3507 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003508 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003509 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003510 }
3511 goto done;
3512 }
3513
3514 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003515 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003516 if (ret == 0) {
3517 struct btrfs_key tmp;
3518 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3519 if (key_type == tmp.type) {
3520 first_offset = tmp.offset;
3521 ret = overwrite_item(trans, log, dst_path,
3522 path->nodes[0], path->slots[0],
3523 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003524 if (ret) {
3525 err = ret;
3526 goto done;
3527 }
Chris Masone02119d2008-09-05 16:13:11 -04003528 }
3529 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003530 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003531
3532 /* find the first key from this transaction again */
3533 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303534 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003535 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003536
3537 /*
3538 * we have a block from this transaction, log every item in it
3539 * from our directory
3540 */
Chris Masond3977122009-01-05 21:25:51 -05003541 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003542 struct btrfs_key tmp;
3543 src = path->nodes[0];
3544 nritems = btrfs_header_nritems(src);
3545 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003546 struct btrfs_dir_item *di;
3547
Chris Masone02119d2008-09-05 16:13:11 -04003548 btrfs_item_key_to_cpu(src, &min_key, i);
3549
Li Zefan33345d012011-04-20 10:31:50 +08003550 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003551 goto done;
3552 ret = overwrite_item(trans, log, dst_path, src, i,
3553 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003554 if (ret) {
3555 err = ret;
3556 goto done;
3557 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003558
3559 /*
3560 * We must make sure that when we log a directory entry,
3561 * the corresponding inode, after log replay, has a
3562 * matching link count. For example:
3563 *
3564 * touch foo
3565 * mkdir mydir
3566 * sync
3567 * ln foo mydir/bar
3568 * xfs_io -c "fsync" mydir
3569 * <crash>
3570 * <mount fs and log replay>
3571 *
3572 * Would result in a fsync log that when replayed, our
3573 * file inode would have a link count of 1, but we get
3574 * two directory entries pointing to the same inode.
3575 * After removing one of the names, it would not be
3576 * possible to remove the other name, which resulted
3577 * always in stale file handle errors, and would not
3578 * be possible to rmdir the parent directory, since
3579 * its i_size could never decrement to the value
3580 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3581 */
3582 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3583 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3584 if (ctx &&
3585 (btrfs_dir_transid(src, di) == trans->transid ||
3586 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3587 tmp.type != BTRFS_ROOT_ITEM_KEY)
3588 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003589 }
3590 path->slots[0] = nritems;
3591
3592 /*
3593 * look ahead to the next item and see if it is also
3594 * from this directory and from this transaction
3595 */
3596 ret = btrfs_next_leaf(root, path);
Liu Bo80c0b422018-04-03 01:59:47 +08003597 if (ret) {
3598 if (ret == 1)
3599 last_offset = (u64)-1;
3600 else
3601 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003602 goto done;
3603 }
3604 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003605 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003606 last_offset = (u64)-1;
3607 goto done;
3608 }
3609 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3610 ret = overwrite_item(trans, log, dst_path,
3611 path->nodes[0], path->slots[0],
3612 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003613 if (ret)
3614 err = ret;
3615 else
3616 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003617 goto done;
3618 }
3619 }
3620done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003621 btrfs_release_path(path);
3622 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003623
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003624 if (err == 0) {
3625 *last_offset_ret = last_offset;
3626 /*
3627 * insert the log range keys to indicate where the log
3628 * is valid
3629 */
3630 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003631 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003632 if (ret)
3633 err = ret;
3634 }
3635 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003636}
3637
3638/*
3639 * logging directories is very similar to logging inodes, We find all the items
3640 * from the current transaction and write them to the log.
3641 *
3642 * The recovery code scans the directory in the subvolume, and if it finds a
3643 * key in the range logged that is not present in the log tree, then it means
3644 * that dir entry was unlinked during the transaction.
3645 *
3646 * In order for that scan to work, we must include one key smaller than
3647 * the smallest logged by this transaction and one key larger than the largest
3648 * key logged by this transaction.
3649 */
3650static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003651 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003652 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003653 struct btrfs_path *dst_path,
3654 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003655{
3656 u64 min_key;
3657 u64 max_key;
3658 int ret;
3659 int key_type = BTRFS_DIR_ITEM_KEY;
3660
3661again:
3662 min_key = 0;
3663 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003664 while (1) {
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003665 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3666 ctx, min_key, &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003667 if (ret)
3668 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003669 if (max_key == (u64)-1)
3670 break;
3671 min_key = max_key + 1;
3672 }
3673
3674 if (key_type == BTRFS_DIR_ITEM_KEY) {
3675 key_type = BTRFS_DIR_INDEX_KEY;
3676 goto again;
3677 }
3678 return 0;
3679}
3680
3681/*
3682 * a helper function to drop items from the log before we relog an
3683 * inode. max_key_type indicates the highest item type to remove.
3684 * This cannot be run for file data extents because it does not
3685 * free the extents they point to.
3686 */
3687static int drop_objectid_items(struct btrfs_trans_handle *trans,
3688 struct btrfs_root *log,
3689 struct btrfs_path *path,
3690 u64 objectid, int max_key_type)
3691{
3692 int ret;
3693 struct btrfs_key key;
3694 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003695 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003696
3697 key.objectid = objectid;
3698 key.type = max_key_type;
3699 key.offset = (u64)-1;
3700
Chris Masond3977122009-01-05 21:25:51 -05003701 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003702 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003703 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003704 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003705 break;
3706
3707 if (path->slots[0] == 0)
3708 break;
3709
3710 path->slots[0]--;
3711 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3712 path->slots[0]);
3713
3714 if (found_key.objectid != objectid)
3715 break;
3716
Josef Bacik18ec90d2012-09-28 11:56:28 -04003717 found_key.offset = 0;
3718 found_key.type = 0;
3719 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3720 &start_slot);
3721
3722 ret = btrfs_del_items(trans, log, path, start_slot,
3723 path->slots[0] - start_slot + 1);
3724 /*
3725 * If start slot isn't 0 then we don't need to re-search, we've
3726 * found the last guy with the objectid in this tree.
3727 */
3728 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003729 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003730 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003731 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003732 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003733 if (ret > 0)
3734 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003735 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003736}
3737
Josef Bacik94edf4a2012-09-25 14:56:25 -04003738static void fill_inode_item(struct btrfs_trans_handle *trans,
3739 struct extent_buffer *leaf,
3740 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003741 struct inode *inode, int log_inode_only,
3742 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003743{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003744 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003745
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003746 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003747
3748 if (log_inode_only) {
3749 /* set the generation to zero so the recover code
3750 * can tell the difference between an logging
3751 * just to say 'this inode exists' and a logging
3752 * to say 'update this inode with these values'
3753 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003754 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003755 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003756 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003757 btrfs_set_token_inode_generation(leaf, item,
3758 BTRFS_I(inode)->generation,
3759 &token);
3760 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003761 }
3762
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003763 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3764 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3765 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3766 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3767
David Sterbaa937b972014-12-12 17:39:12 +01003768 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003769 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003770 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003771 inode->i_atime.tv_nsec, &token);
3772
David Sterbaa937b972014-12-12 17:39:12 +01003773 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003774 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003775 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003776 inode->i_mtime.tv_nsec, &token);
3777
David Sterbaa937b972014-12-12 17:39:12 +01003778 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003779 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003780 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003781 inode->i_ctime.tv_nsec, &token);
3782
3783 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3784 &token);
3785
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003786 btrfs_set_token_inode_sequence(leaf, item,
3787 inode_peek_iversion(inode), &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003788 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3789 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3790 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3791 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003792}
3793
Josef Bacika95249b2012-10-11 16:17:34 -04003794static int log_inode_item(struct btrfs_trans_handle *trans,
3795 struct btrfs_root *log, struct btrfs_path *path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003796 struct btrfs_inode *inode)
Josef Bacika95249b2012-10-11 16:17:34 -04003797{
3798 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003799 int ret;
3800
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003801 ret = btrfs_insert_empty_item(trans, log, path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003802 &inode->location, sizeof(*inode_item));
Josef Bacika95249b2012-10-11 16:17:34 -04003803 if (ret && ret != -EEXIST)
3804 return ret;
3805 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3806 struct btrfs_inode_item);
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003807 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3808 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003809 btrfs_release_path(path);
3810 return 0;
3811}
3812
Chris Mason31ff1cd2008-09-11 16:17:57 -04003813static noinline int copy_items(struct btrfs_trans_handle *trans,
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003814 struct btrfs_inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003815 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003816 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003817 int start_slot, int nr, int inode_only,
3818 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003819{
David Sterba3ffbd682018-06-29 10:56:42 +02003820 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003821 unsigned long src_offset;
3822 unsigned long dst_offset;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003823 struct btrfs_root *log = inode->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003824 struct btrfs_file_extent_item *extent;
3825 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003826 struct extent_buffer *src = src_path->nodes[0];
3827 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003828 int ret;
3829 struct btrfs_key *ins_keys;
3830 u32 *ins_sizes;
3831 char *ins_data;
3832 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003833 struct list_head ordered_sums;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003834 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003835 bool has_extents = false;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003836 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003837 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003838
3839 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003840
3841 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3842 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003843 if (!ins_data)
3844 return -ENOMEM;
3845
Josef Bacik16e75492013-10-22 12:18:51 -04003846 first_key.objectid = (u64)-1;
3847
Chris Mason31ff1cd2008-09-11 16:17:57 -04003848 ins_sizes = (u32 *)ins_data;
3849 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3850
3851 for (i = 0; i < nr; i++) {
3852 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3853 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3854 }
3855 ret = btrfs_insert_empty_items(trans, log, dst_path,
3856 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003857 if (ret) {
3858 kfree(ins_data);
3859 return ret;
3860 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003861
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003862 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003863 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3864 dst_path->slots[0]);
3865
3866 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3867
Matthias Kaehlcke0dde10b2017-07-27 14:30:23 -07003868 if (i == nr - 1)
Josef Bacik16e75492013-10-22 12:18:51 -04003869 last_key = ins_keys[i];
3870
Josef Bacik94edf4a2012-09-25 14:56:25 -04003871 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003872 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3873 dst_path->slots[0],
3874 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003875 fill_inode_item(trans, dst_path->nodes[0], inode_item,
David Sterbaf85b7372017-01-20 14:54:07 +01003876 &inode->vfs_inode,
3877 inode_only == LOG_INODE_EXISTS,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003878 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003879 } else {
3880 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3881 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003882 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003883
Josef Bacik16e75492013-10-22 12:18:51 -04003884 /*
3885 * We set need_find_last_extent here in case we know we were
3886 * processing other items and then walk into the first extent in
3887 * the inode. If we don't hit an extent then nothing changes,
3888 * we'll do the last search the next time around.
3889 */
3890 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3891 has_extents = true;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003892 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003893 first_key = ins_keys[i];
3894 } else {
3895 need_find_last_extent = false;
3896 }
3897
Chris Mason31ff1cd2008-09-11 16:17:57 -04003898 /* take a reference on file data extents so that truncates
3899 * or deletes of this inode don't have to relog the inode
3900 * again
3901 */
David Sterba962a2982014-06-04 18:41:45 +02003902 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003903 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003904 int found_type;
3905 extent = btrfs_item_ptr(src, start_slot + i,
3906 struct btrfs_file_extent_item);
3907
liubo8e531cd2011-05-06 10:36:09 +08003908 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3909 continue;
3910
Chris Mason31ff1cd2008-09-11 16:17:57 -04003911 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003912 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003913 u64 ds, dl, cs, cl;
3914 ds = btrfs_file_extent_disk_bytenr(src,
3915 extent);
3916 /* ds == 0 is a hole */
3917 if (ds == 0)
3918 continue;
3919
3920 dl = btrfs_file_extent_disk_num_bytes(src,
3921 extent);
3922 cs = btrfs_file_extent_offset(src, extent);
3923 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003924 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003925 if (btrfs_file_extent_compression(src,
3926 extent)) {
3927 cs = 0;
3928 cl = dl;
3929 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003930
3931 ret = btrfs_lookup_csums_range(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003932 fs_info->csum_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003933 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003934 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003935 if (ret) {
3936 btrfs_release_path(dst_path);
3937 kfree(ins_data);
3938 return ret;
3939 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003940 }
3941 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003942 }
3943
3944 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003945 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003946 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003947
3948 /*
3949 * we have to do this after the loop above to avoid changing the
3950 * log tree while trying to change the log tree.
3951 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003952 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003953 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003954 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3955 struct btrfs_ordered_sum,
3956 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003957 if (!ret)
3958 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003959 list_del(&sums->list);
3960 kfree(sums);
3961 }
Josef Bacik16e75492013-10-22 12:18:51 -04003962
3963 if (!has_extents)
3964 return ret;
3965
Filipe Manana74121f7c2014-08-07 12:00:44 +01003966 if (need_find_last_extent && *last_extent == first_key.offset) {
3967 /*
3968 * We don't have any leafs between our current one and the one
3969 * we processed before that can have file extent items for our
3970 * inode (and have a generation number smaller than our current
3971 * transaction id).
3972 */
3973 need_find_last_extent = false;
3974 }
3975
Josef Bacik16e75492013-10-22 12:18:51 -04003976 /*
3977 * Because we use btrfs_search_forward we could skip leaves that were
3978 * not modified and then assume *last_extent is valid when it really
3979 * isn't. So back up to the previous leaf and read the end of the last
3980 * extent before we go and fill in holes.
3981 */
3982 if (need_find_last_extent) {
3983 u64 len;
3984
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003985 ret = btrfs_prev_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04003986 if (ret < 0)
3987 return ret;
3988 if (ret)
3989 goto fill_holes;
3990 if (src_path->slots[0])
3991 src_path->slots[0]--;
3992 src = src_path->nodes[0];
3993 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003994 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04003995 key.type != BTRFS_EXTENT_DATA_KEY)
3996 goto fill_holes;
3997 extent = btrfs_item_ptr(src, src_path->slots[0],
3998 struct btrfs_file_extent_item);
3999 if (btrfs_file_extent_type(src, extent) ==
4000 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004001 len = btrfs_file_extent_ram_bytes(src, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04004002 *last_extent = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004003 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04004004 } else {
4005 len = btrfs_file_extent_num_bytes(src, extent);
4006 *last_extent = key.offset + len;
4007 }
4008 }
4009fill_holes:
4010 /* So we did prev_leaf, now we need to move to the next leaf, but a few
4011 * things could have happened
4012 *
4013 * 1) A merge could have happened, so we could currently be on a leaf
4014 * that holds what we were copying in the first place.
4015 * 2) A split could have happened, and now not all of the items we want
4016 * are on the same leaf.
4017 *
4018 * So we need to adjust how we search for holes, we need to drop the
4019 * path and re-search for the first extent key we found, and then walk
4020 * forward until we hit the last one we copied.
4021 */
4022 if (need_find_last_extent) {
4023 /* btrfs_prev_leaf could return 1 without releasing the path */
4024 btrfs_release_path(src_path);
David Sterbaf85b7372017-01-20 14:54:07 +01004025 ret = btrfs_search_slot(NULL, inode->root, &first_key,
4026 src_path, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004027 if (ret < 0)
4028 return ret;
4029 ASSERT(ret == 0);
4030 src = src_path->nodes[0];
4031 i = src_path->slots[0];
4032 } else {
4033 i = start_slot;
4034 }
4035
4036 /*
4037 * Ok so here we need to go through and fill in any holes we may have
4038 * to make sure that holes are punched for those areas in case they had
4039 * extents previously.
4040 */
4041 while (!done) {
4042 u64 offset, len;
4043 u64 extent_end;
4044
4045 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004046 ret = btrfs_next_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04004047 if (ret < 0)
4048 return ret;
4049 ASSERT(ret == 0);
4050 src = src_path->nodes[0];
4051 i = 0;
Filipe Manana8434ec42018-03-26 23:59:12 +01004052 need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04004053 }
4054
4055 btrfs_item_key_to_cpu(src, &key, i);
4056 if (!btrfs_comp_cpu_keys(&key, &last_key))
4057 done = true;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004058 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04004059 key.type != BTRFS_EXTENT_DATA_KEY) {
4060 i++;
4061 continue;
4062 }
4063 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
4064 if (btrfs_file_extent_type(src, extent) ==
4065 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004066 len = btrfs_file_extent_ram_bytes(src, extent);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004067 extent_end = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004068 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04004069 } else {
4070 len = btrfs_file_extent_num_bytes(src, extent);
4071 extent_end = key.offset + len;
4072 }
4073 i++;
4074
4075 if (*last_extent == key.offset) {
4076 *last_extent = extent_end;
4077 continue;
4078 }
4079 offset = *last_extent;
4080 len = key.offset - *last_extent;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004081 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01004082 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004083 if (ret)
4084 break;
Filipe Manana74121f7c2014-08-07 12:00:44 +01004085 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04004086 }
Filipe Manana4ee3fad2018-03-26 23:59:00 +01004087
4088 /*
4089 * Check if there is a hole between the last extent found in our leaf
4090 * and the first extent in the next leaf. If there is one, we need to
4091 * log an explicit hole so that at replay time we can punch the hole.
4092 */
4093 if (ret == 0 &&
4094 key.objectid == btrfs_ino(inode) &&
4095 key.type == BTRFS_EXTENT_DATA_KEY &&
4096 i == btrfs_header_nritems(src_path->nodes[0])) {
4097 ret = btrfs_next_leaf(inode->root, src_path);
4098 need_find_last_extent = true;
4099 if (ret > 0) {
4100 ret = 0;
4101 } else if (ret == 0) {
4102 btrfs_item_key_to_cpu(src_path->nodes[0], &key,
4103 src_path->slots[0]);
4104 if (key.objectid == btrfs_ino(inode) &&
4105 key.type == BTRFS_EXTENT_DATA_KEY &&
4106 *last_extent < key.offset) {
4107 const u64 len = key.offset - *last_extent;
4108
4109 ret = btrfs_insert_file_extent(trans, log,
4110 btrfs_ino(inode),
4111 *last_extent, 0,
4112 0, len, 0, len,
4113 0, 0, 0);
4114 }
4115 }
4116 }
Josef Bacik16e75492013-10-22 12:18:51 -04004117 /*
4118 * Need to let the callers know we dropped the path so they should
4119 * re-search.
4120 */
4121 if (!ret && need_find_last_extent)
4122 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004123 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004124}
4125
Josef Bacik5dc562c2012-08-17 13:14:17 -04004126static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
4127{
4128 struct extent_map *em1, *em2;
4129
4130 em1 = list_entry(a, struct extent_map, list);
4131 em2 = list_entry(b, struct extent_map, list);
4132
4133 if (em1->start < em2->start)
4134 return -1;
4135 else if (em1->start > em2->start)
4136 return 1;
4137 return 0;
4138}
4139
Josef Bacike7175a62018-05-23 11:58:34 -04004140static int log_extent_csums(struct btrfs_trans_handle *trans,
4141 struct btrfs_inode *inode,
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004142 struct btrfs_root *log_root,
Josef Bacike7175a62018-05-23 11:58:34 -04004143 const struct extent_map *em)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004144{
Josef Bacik2ab28f32012-10-12 15:27:49 -04004145 u64 csum_offset;
4146 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01004147 LIST_HEAD(ordered_sums);
4148 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004149
Josef Bacike7175a62018-05-23 11:58:34 -04004150 if (inode->flags & BTRFS_INODE_NODATASUM ||
4151 test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
Filipe Manana8407f552014-09-05 15:14:39 +01004152 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04004153 return 0;
4154
Josef Bacike7175a62018-05-23 11:58:34 -04004155 /* If we're compressed we have to save the entire range of csums. */
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004156 if (em->compress_type) {
4157 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004158 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004159 } else {
Josef Bacike7175a62018-05-23 11:58:34 -04004160 csum_offset = em->mod_start - em->start;
4161 csum_len = em->mod_len;
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004162 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004163
Josef Bacik70c8a912012-10-11 16:54:30 -04004164 /* block start is already adjusted for the file extent offset. */
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004165 ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
Josef Bacik70c8a912012-10-11 16:54:30 -04004166 em->block_start + csum_offset,
4167 em->block_start + csum_offset +
4168 csum_len - 1, &ordered_sums, 0);
4169 if (ret)
4170 return ret;
4171
4172 while (!list_empty(&ordered_sums)) {
4173 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4174 struct btrfs_ordered_sum,
4175 list);
4176 if (!ret)
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004177 ret = btrfs_csum_file_blocks(trans, log_root, sums);
Josef Bacik70c8a912012-10-11 16:54:30 -04004178 list_del(&sums->list);
4179 kfree(sums);
4180 }
4181
4182 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004183}
4184
Filipe Manana8407f552014-09-05 15:14:39 +01004185static int log_one_extent(struct btrfs_trans_handle *trans,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004186 struct btrfs_inode *inode, struct btrfs_root *root,
Filipe Manana8407f552014-09-05 15:14:39 +01004187 const struct extent_map *em,
4188 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004189 struct btrfs_log_ctx *ctx)
4190{
4191 struct btrfs_root *log = root->log_root;
4192 struct btrfs_file_extent_item *fi;
4193 struct extent_buffer *leaf;
4194 struct btrfs_map_token token;
4195 struct btrfs_key key;
4196 u64 extent_offset = em->start - em->orig_start;
4197 u64 block_len;
4198 int ret;
4199 int extent_inserted = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004200
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004201 ret = log_extent_csums(trans, inode, log, em);
Filipe Manana8407f552014-09-05 15:14:39 +01004202 if (ret)
4203 return ret;
4204
Filipe Manana8407f552014-09-05 15:14:39 +01004205 btrfs_init_map_token(&token);
4206
Nikolay Borisov9d122622017-01-18 00:31:40 +02004207 ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
Filipe Manana8407f552014-09-05 15:14:39 +01004208 em->start + em->len, NULL, 0, 1,
4209 sizeof(*fi), &extent_inserted);
4210 if (ret)
4211 return ret;
4212
4213 if (!extent_inserted) {
Nikolay Borisov9d122622017-01-18 00:31:40 +02004214 key.objectid = btrfs_ino(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01004215 key.type = BTRFS_EXTENT_DATA_KEY;
4216 key.offset = em->start;
4217
4218 ret = btrfs_insert_empty_item(trans, log, path, &key,
4219 sizeof(*fi));
4220 if (ret)
4221 return ret;
4222 }
4223 leaf = path->nodes[0];
4224 fi = btrfs_item_ptr(leaf, path->slots[0],
4225 struct btrfs_file_extent_item);
4226
Josef Bacik50d9aa92014-11-21 14:52:38 -05004227 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004228 &token);
4229 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4230 btrfs_set_token_file_extent_type(leaf, fi,
4231 BTRFS_FILE_EXTENT_PREALLOC,
4232 &token);
4233 else
4234 btrfs_set_token_file_extent_type(leaf, fi,
4235 BTRFS_FILE_EXTENT_REG,
4236 &token);
4237
4238 block_len = max(em->block_len, em->orig_block_len);
4239 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4240 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4241 em->block_start,
4242 &token);
4243 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4244 &token);
4245 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4246 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4247 em->block_start -
4248 extent_offset, &token);
4249 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4250 &token);
4251 } else {
4252 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4253 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4254 &token);
4255 }
4256
4257 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4258 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4259 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4260 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4261 &token);
4262 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4263 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4264 btrfs_mark_buffer_dirty(leaf);
4265
4266 btrfs_release_path(path);
4267
4268 return ret;
4269}
4270
Filipe Manana31d11b82018-05-09 16:01:46 +01004271/*
4272 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4273 * lose them after doing a fast fsync and replaying the log. We scan the
4274 * subvolume's root instead of iterating the inode's extent map tree because
4275 * otherwise we can log incorrect extent items based on extent map conversion.
4276 * That can happen due to the fact that extent maps are merged when they
4277 * are not in the extent map tree's list of modified extents.
4278 */
4279static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4280 struct btrfs_inode *inode,
4281 struct btrfs_path *path)
4282{
4283 struct btrfs_root *root = inode->root;
4284 struct btrfs_key key;
4285 const u64 i_size = i_size_read(&inode->vfs_inode);
4286 const u64 ino = btrfs_ino(inode);
4287 struct btrfs_path *dst_path = NULL;
4288 u64 last_extent = (u64)-1;
4289 int ins_nr = 0;
4290 int start_slot;
4291 int ret;
4292
4293 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4294 return 0;
4295
4296 key.objectid = ino;
4297 key.type = BTRFS_EXTENT_DATA_KEY;
4298 key.offset = i_size;
4299 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4300 if (ret < 0)
4301 goto out;
4302
4303 while (true) {
4304 struct extent_buffer *leaf = path->nodes[0];
4305 int slot = path->slots[0];
4306
4307 if (slot >= btrfs_header_nritems(leaf)) {
4308 if (ins_nr > 0) {
4309 ret = copy_items(trans, inode, dst_path, path,
4310 &last_extent, start_slot,
4311 ins_nr, 1, 0);
4312 if (ret < 0)
4313 goto out;
4314 ins_nr = 0;
4315 }
4316 ret = btrfs_next_leaf(root, path);
4317 if (ret < 0)
4318 goto out;
4319 if (ret > 0) {
4320 ret = 0;
4321 break;
4322 }
4323 continue;
4324 }
4325
4326 btrfs_item_key_to_cpu(leaf, &key, slot);
4327 if (key.objectid > ino)
4328 break;
4329 if (WARN_ON_ONCE(key.objectid < ino) ||
4330 key.type < BTRFS_EXTENT_DATA_KEY ||
4331 key.offset < i_size) {
4332 path->slots[0]++;
4333 continue;
4334 }
4335 if (last_extent == (u64)-1) {
4336 last_extent = key.offset;
4337 /*
4338 * Avoid logging extent items logged in past fsync calls
4339 * and leading to duplicate keys in the log tree.
4340 */
4341 do {
4342 ret = btrfs_truncate_inode_items(trans,
4343 root->log_root,
4344 &inode->vfs_inode,
4345 i_size,
4346 BTRFS_EXTENT_DATA_KEY);
4347 } while (ret == -EAGAIN);
4348 if (ret)
4349 goto out;
4350 }
4351 if (ins_nr == 0)
4352 start_slot = slot;
4353 ins_nr++;
4354 path->slots[0]++;
4355 if (!dst_path) {
4356 dst_path = btrfs_alloc_path();
4357 if (!dst_path) {
4358 ret = -ENOMEM;
4359 goto out;
4360 }
4361 }
4362 }
4363 if (ins_nr > 0) {
4364 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4365 start_slot, ins_nr, 1, 0);
4366 if (ret > 0)
4367 ret = 0;
4368 }
4369out:
4370 btrfs_release_path(path);
4371 btrfs_free_path(dst_path);
4372 return ret;
4373}
4374
Josef Bacik5dc562c2012-08-17 13:14:17 -04004375static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4376 struct btrfs_root *root,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004377 struct btrfs_inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004378 struct btrfs_path *path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004379 struct btrfs_log_ctx *ctx,
4380 const u64 start,
4381 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004382{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004383 struct extent_map *em, *n;
4384 struct list_head extents;
Nikolay Borisov9d122622017-01-18 00:31:40 +02004385 struct extent_map_tree *tree = &inode->extent_tree;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004386 u64 logged_start, logged_end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004387 u64 test_gen;
4388 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004389 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004390
4391 INIT_LIST_HEAD(&extents);
4392
Nikolay Borisov9d122622017-01-18 00:31:40 +02004393 down_write(&inode->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004394 write_lock(&tree->lock);
4395 test_gen = root->fs_info->last_trans_committed;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004396 logged_start = start;
4397 logged_end = end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004398
4399 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4400 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004401 /*
4402 * Just an arbitrary number, this can be really CPU intensive
4403 * once we start getting a lot of extents, and really once we
4404 * have a bunch of extents we just want to commit since it will
4405 * be faster.
4406 */
4407 if (++num > 32768) {
4408 list_del_init(&tree->modified_extents);
4409 ret = -EFBIG;
4410 goto process;
4411 }
4412
Josef Bacik5dc562c2012-08-17 13:14:17 -04004413 if (em->generation <= test_gen)
4414 continue;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004415
Filipe Manana31d11b82018-05-09 16:01:46 +01004416 /* We log prealloc extents beyond eof later. */
4417 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4418 em->start >= i_size_read(&inode->vfs_inode))
4419 continue;
4420
Josef Bacik8c6c5922017-08-29 10:11:39 -04004421 if (em->start < logged_start)
4422 logged_start = em->start;
4423 if ((em->start + em->len - 1) > logged_end)
4424 logged_end = em->start + em->len - 1;
4425
Josef Bacikff44c6e2012-09-14 12:59:20 -04004426 /* Need a ref to keep it from getting evicted from cache */
Elena Reshetova490b54d2017-03-03 10:55:12 +02004427 refcount_inc(&em->refs);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004428 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004429 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004430 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004431 }
4432
4433 list_sort(NULL, &extents, extent_cmp);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004434process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004435 while (!list_empty(&extents)) {
4436 em = list_entry(extents.next, struct extent_map, list);
4437
4438 list_del_init(&em->list);
4439
4440 /*
4441 * If we had an error we just need to delete everybody from our
4442 * private list.
4443 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004444 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004445 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004446 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004447 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004448 }
4449
4450 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004451
Josef Bacika2120a42018-05-23 11:58:35 -04004452 ret = log_one_extent(trans, inode, root, em, path, ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004453 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004454 clear_em_logging(tree, em);
4455 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004456 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004457 WARN_ON(!list_empty(&extents));
4458 write_unlock(&tree->lock);
Nikolay Borisov9d122622017-01-18 00:31:40 +02004459 up_write(&inode->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004460
Josef Bacik5dc562c2012-08-17 13:14:17 -04004461 btrfs_release_path(path);
Filipe Manana31d11b82018-05-09 16:01:46 +01004462 if (!ret)
4463 ret = btrfs_log_prealloc_extents(trans, inode, path);
4464
Josef Bacik5dc562c2012-08-17 13:14:17 -04004465 return ret;
4466}
4467
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004468static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004469 struct btrfs_path *path, u64 *size_ret)
4470{
4471 struct btrfs_key key;
4472 int ret;
4473
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004474 key.objectid = btrfs_ino(inode);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004475 key.type = BTRFS_INODE_ITEM_KEY;
4476 key.offset = 0;
4477
4478 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4479 if (ret < 0) {
4480 return ret;
4481 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004482 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004483 } else {
4484 struct btrfs_inode_item *item;
4485
4486 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4487 struct btrfs_inode_item);
4488 *size_ret = btrfs_inode_size(path->nodes[0], item);
4489 }
4490
4491 btrfs_release_path(path);
4492 return 0;
4493}
4494
Filipe Manana36283bf2015-06-20 00:44:51 +01004495/*
4496 * At the moment we always log all xattrs. This is to figure out at log replay
4497 * time which xattrs must have their deletion replayed. If a xattr is missing
4498 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4499 * because if a xattr is deleted, the inode is fsynced and a power failure
4500 * happens, causing the log to be replayed the next time the fs is mounted,
4501 * we want the xattr to not exist anymore (same behaviour as other filesystems
4502 * with a journal, ext3/4, xfs, f2fs, etc).
4503 */
4504static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4505 struct btrfs_root *root,
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004506 struct btrfs_inode *inode,
Filipe Manana36283bf2015-06-20 00:44:51 +01004507 struct btrfs_path *path,
4508 struct btrfs_path *dst_path)
4509{
4510 int ret;
4511 struct btrfs_key key;
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004512 const u64 ino = btrfs_ino(inode);
Filipe Manana36283bf2015-06-20 00:44:51 +01004513 int ins_nr = 0;
4514 int start_slot = 0;
4515
4516 key.objectid = ino;
4517 key.type = BTRFS_XATTR_ITEM_KEY;
4518 key.offset = 0;
4519
4520 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4521 if (ret < 0)
4522 return ret;
4523
4524 while (true) {
4525 int slot = path->slots[0];
4526 struct extent_buffer *leaf = path->nodes[0];
4527 int nritems = btrfs_header_nritems(leaf);
4528
4529 if (slot >= nritems) {
4530 if (ins_nr > 0) {
4531 u64 last_extent = 0;
4532
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004533 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004534 &last_extent, start_slot,
4535 ins_nr, 1, 0);
4536 /* can't be 1, extent items aren't processed */
4537 ASSERT(ret <= 0);
4538 if (ret < 0)
4539 return ret;
4540 ins_nr = 0;
4541 }
4542 ret = btrfs_next_leaf(root, path);
4543 if (ret < 0)
4544 return ret;
4545 else if (ret > 0)
4546 break;
4547 continue;
4548 }
4549
4550 btrfs_item_key_to_cpu(leaf, &key, slot);
4551 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4552 break;
4553
4554 if (ins_nr == 0)
4555 start_slot = slot;
4556 ins_nr++;
4557 path->slots[0]++;
4558 cond_resched();
4559 }
4560 if (ins_nr > 0) {
4561 u64 last_extent = 0;
4562
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004563 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004564 &last_extent, start_slot,
4565 ins_nr, 1, 0);
4566 /* can't be 1, extent items aren't processed */
4567 ASSERT(ret <= 0);
4568 if (ret < 0)
4569 return ret;
4570 }
4571
4572 return 0;
4573}
4574
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004575/*
4576 * If the no holes feature is enabled we need to make sure any hole between the
4577 * last extent and the i_size of our inode is explicitly marked in the log. This
4578 * is to make sure that doing something like:
4579 *
4580 * 1) create file with 128Kb of data
4581 * 2) truncate file to 64Kb
4582 * 3) truncate file to 256Kb
4583 * 4) fsync file
4584 * 5) <crash/power failure>
4585 * 6) mount fs and trigger log replay
4586 *
4587 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4588 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4589 * file correspond to a hole. The presence of explicit holes in a log tree is
4590 * what guarantees that log replay will remove/adjust file extent items in the
4591 * fs/subvol tree.
4592 *
4593 * Here we do not need to care about holes between extents, that is already done
4594 * by copy_items(). We also only need to do this in the full sync path, where we
4595 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4596 * lookup the list of modified extent maps and if any represents a hole, we
4597 * insert a corresponding extent representing a hole in the log tree.
4598 */
4599static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4600 struct btrfs_root *root,
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004601 struct btrfs_inode *inode,
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004602 struct btrfs_path *path)
4603{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004604 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004605 int ret;
4606 struct btrfs_key key;
4607 u64 hole_start;
4608 u64 hole_size;
4609 struct extent_buffer *leaf;
4610 struct btrfs_root *log = root->log_root;
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004611 const u64 ino = btrfs_ino(inode);
4612 const u64 i_size = i_size_read(&inode->vfs_inode);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004613
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004614 if (!btrfs_fs_incompat(fs_info, NO_HOLES))
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004615 return 0;
4616
4617 key.objectid = ino;
4618 key.type = BTRFS_EXTENT_DATA_KEY;
4619 key.offset = (u64)-1;
4620
4621 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4622 ASSERT(ret != 0);
4623 if (ret < 0)
4624 return ret;
4625
4626 ASSERT(path->slots[0] > 0);
4627 path->slots[0]--;
4628 leaf = path->nodes[0];
4629 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4630
4631 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4632 /* inode does not have any extents */
4633 hole_start = 0;
4634 hole_size = i_size;
4635 } else {
4636 struct btrfs_file_extent_item *extent;
4637 u64 len;
4638
4639 /*
4640 * If there's an extent beyond i_size, an explicit hole was
4641 * already inserted by copy_items().
4642 */
4643 if (key.offset >= i_size)
4644 return 0;
4645
4646 extent = btrfs_item_ptr(leaf, path->slots[0],
4647 struct btrfs_file_extent_item);
4648
4649 if (btrfs_file_extent_type(leaf, extent) ==
4650 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004651 len = btrfs_file_extent_ram_bytes(leaf, extent);
Filipe Manana6399fb52017-07-28 15:22:36 +01004652 ASSERT(len == i_size ||
4653 (len == fs_info->sectorsize &&
4654 btrfs_file_extent_compression(leaf, extent) !=
4655 BTRFS_COMPRESS_NONE));
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004656 return 0;
4657 }
4658
4659 len = btrfs_file_extent_num_bytes(leaf, extent);
4660 /* Last extent goes beyond i_size, no need to log a hole. */
4661 if (key.offset + len > i_size)
4662 return 0;
4663 hole_start = key.offset + len;
4664 hole_size = i_size - hole_start;
4665 }
4666 btrfs_release_path(path);
4667
4668 /* Last extent ends at i_size. */
4669 if (hole_size == 0)
4670 return 0;
4671
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004672 hole_size = ALIGN(hole_size, fs_info->sectorsize);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004673 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4674 hole_size, 0, hole_size, 0, 0, 0);
4675 return ret;
4676}
4677
Filipe Manana56f23fd2016-03-30 23:37:21 +01004678/*
4679 * When we are logging a new inode X, check if it doesn't have a reference that
4680 * matches the reference from some other inode Y created in a past transaction
4681 * and that was renamed in the current transaction. If we don't do this, then at
4682 * log replay time we can lose inode Y (and all its files if it's a directory):
4683 *
4684 * mkdir /mnt/x
4685 * echo "hello world" > /mnt/x/foobar
4686 * sync
4687 * mv /mnt/x /mnt/y
4688 * mkdir /mnt/x # or touch /mnt/x
4689 * xfs_io -c fsync /mnt/x
4690 * <power fail>
4691 * mount fs, trigger log replay
4692 *
4693 * After the log replay procedure, we would lose the first directory and all its
4694 * files (file foobar).
4695 * For the case where inode Y is not a directory we simply end up losing it:
4696 *
4697 * echo "123" > /mnt/foo
4698 * sync
4699 * mv /mnt/foo /mnt/bar
4700 * echo "abc" > /mnt/foo
4701 * xfs_io -c fsync /mnt/foo
4702 * <power fail>
4703 *
4704 * We also need this for cases where a snapshot entry is replaced by some other
4705 * entry (file or directory) otherwise we end up with an unreplayable log due to
4706 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4707 * if it were a regular entry:
4708 *
4709 * mkdir /mnt/x
4710 * btrfs subvolume snapshot /mnt /mnt/x/snap
4711 * btrfs subvolume delete /mnt/x/snap
4712 * rmdir /mnt/x
4713 * mkdir /mnt/x
4714 * fsync /mnt/x or fsync some new file inside it
4715 * <power fail>
4716 *
4717 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4718 * the same transaction.
4719 */
4720static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4721 const int slot,
4722 const struct btrfs_key *key,
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004723 struct btrfs_inode *inode,
Filipe Manana44f714d2016-06-06 16:11:13 +01004724 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004725{
4726 int ret;
4727 struct btrfs_path *search_path;
4728 char *name = NULL;
4729 u32 name_len = 0;
4730 u32 item_size = btrfs_item_size_nr(eb, slot);
4731 u32 cur_offset = 0;
4732 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4733
4734 search_path = btrfs_alloc_path();
4735 if (!search_path)
4736 return -ENOMEM;
4737 search_path->search_commit_root = 1;
4738 search_path->skip_locking = 1;
4739
4740 while (cur_offset < item_size) {
4741 u64 parent;
4742 u32 this_name_len;
4743 u32 this_len;
4744 unsigned long name_ptr;
4745 struct btrfs_dir_item *di;
4746
4747 if (key->type == BTRFS_INODE_REF_KEY) {
4748 struct btrfs_inode_ref *iref;
4749
4750 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4751 parent = key->offset;
4752 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4753 name_ptr = (unsigned long)(iref + 1);
4754 this_len = sizeof(*iref) + this_name_len;
4755 } else {
4756 struct btrfs_inode_extref *extref;
4757
4758 extref = (struct btrfs_inode_extref *)(ptr +
4759 cur_offset);
4760 parent = btrfs_inode_extref_parent(eb, extref);
4761 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4762 name_ptr = (unsigned long)&extref->name;
4763 this_len = sizeof(*extref) + this_name_len;
4764 }
4765
4766 if (this_name_len > name_len) {
4767 char *new_name;
4768
4769 new_name = krealloc(name, this_name_len, GFP_NOFS);
4770 if (!new_name) {
4771 ret = -ENOMEM;
4772 goto out;
4773 }
4774 name_len = this_name_len;
4775 name = new_name;
4776 }
4777
4778 read_extent_buffer(eb, name, name_ptr, this_name_len);
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004779 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4780 parent, name, this_name_len, 0);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004781 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004782 struct btrfs_key di_key;
4783
4784 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4785 di, &di_key);
4786 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4787 ret = 1;
4788 *other_ino = di_key.objectid;
4789 } else {
4790 ret = -EAGAIN;
4791 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004792 goto out;
4793 } else if (IS_ERR(di)) {
4794 ret = PTR_ERR(di);
4795 goto out;
4796 }
4797 btrfs_release_path(search_path);
4798
4799 cur_offset += this_len;
4800 }
4801 ret = 0;
4802out:
4803 btrfs_free_path(search_path);
4804 kfree(name);
4805 return ret;
4806}
4807
Chris Masone02119d2008-09-05 16:13:11 -04004808/* log a single inode in the tree log.
4809 * At least one parent directory for this inode must exist in the tree
4810 * or be logged already.
4811 *
4812 * Any items from this inode changed by the current transaction are copied
4813 * to the log tree. An extra reference is taken on any extents in this
4814 * file, allowing us to avoid a whole pile of corner cases around logging
4815 * blocks that have been removed from the tree.
4816 *
4817 * See LOG_INODE_ALL and related defines for a description of what inode_only
4818 * does.
4819 *
4820 * This handles both files and directories.
4821 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004822static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004823 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004824 int inode_only,
4825 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004826 const loff_t end,
4827 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004828{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004829 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004830 struct btrfs_path *path;
4831 struct btrfs_path *dst_path;
4832 struct btrfs_key min_key;
4833 struct btrfs_key max_key;
4834 struct btrfs_root *log = root->log_root;
Josef Bacik16e75492013-10-22 12:18:51 -04004835 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004836 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004837 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004838 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004839 int ins_start_slot = 0;
4840 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004841 bool fast_search = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004842 u64 ino = btrfs_ino(inode);
4843 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004844 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004845 bool need_log_inode_item = true;
Filipe Manana9a8fca62018-05-11 16:42:42 +01004846 bool xattrs_logged = false;
Chris Masone02119d2008-09-05 16:13:11 -04004847
Chris Masone02119d2008-09-05 16:13:11 -04004848 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004849 if (!path)
4850 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004851 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004852 if (!dst_path) {
4853 btrfs_free_path(path);
4854 return -ENOMEM;
4855 }
Chris Masone02119d2008-09-05 16:13:11 -04004856
Li Zefan33345d012011-04-20 10:31:50 +08004857 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004858 min_key.type = BTRFS_INODE_ITEM_KEY;
4859 min_key.offset = 0;
4860
Li Zefan33345d012011-04-20 10:31:50 +08004861 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004862
Chris Mason12fcfd22009-03-24 10:24:20 -04004863
Josef Bacik5dc562c2012-08-17 13:14:17 -04004864 /* today the code can only do partial logging of directories */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004865 if (S_ISDIR(inode->vfs_inode.i_mode) ||
Miao Xie5269b672012-11-01 07:35:23 +00004866 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004867 &inode->runtime_flags) &&
Liu Bo781feef2016-11-30 16:20:25 -08004868 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004869 max_key.type = BTRFS_XATTR_ITEM_KEY;
4870 else
4871 max_key.type = (u8)-1;
4872 max_key.offset = (u64)-1;
4873
Filipe Manana2c2c4522015-01-13 16:40:04 +00004874 /*
4875 * Only run delayed items if we are a dir or a new file.
4876 * Otherwise commit the delayed inode only, which is needed in
4877 * order for the log replay code to mark inodes for link count
4878 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4879 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004880 if (S_ISDIR(inode->vfs_inode.i_mode) ||
4881 inode->generation > fs_info->last_trans_committed)
4882 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004883 else
Nikolay Borisova59108a2017-01-18 00:31:48 +02004884 ret = btrfs_commit_inode_delayed_inode(inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004885
4886 if (ret) {
4887 btrfs_free_path(path);
4888 btrfs_free_path(dst_path);
4889 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004890 }
4891
Liu Bo781feef2016-11-30 16:20:25 -08004892 if (inode_only == LOG_OTHER_INODE) {
4893 inode_only = LOG_INODE_EXISTS;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004894 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
Liu Bo781feef2016-11-30 16:20:25 -08004895 } else {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004896 mutex_lock(&inode->log_mutex);
Liu Bo781feef2016-11-30 16:20:25 -08004897 }
Chris Masone02119d2008-09-05 16:13:11 -04004898
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004899 /*
Chris Masone02119d2008-09-05 16:13:11 -04004900 * a brute force approach to making sure we get the most uptodate
4901 * copies of everything.
4902 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004903 if (S_ISDIR(inode->vfs_inode.i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04004904 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4905
Filipe Manana4f764e52015-02-23 19:53:35 +00004906 if (inode_only == LOG_INODE_EXISTS)
4907 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004908 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004909 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004910 if (inode_only == LOG_INODE_EXISTS) {
4911 /*
4912 * Make sure the new inode item we write to the log has
4913 * the same isize as the current one (if it exists).
4914 * This is necessary to prevent data loss after log
4915 * replay, and also to prevent doing a wrong expanding
4916 * truncate - for e.g. create file, write 4K into offset
4917 * 0, fsync, write 4K into offset 4096, add hard link,
4918 * fsync some other file (to sync log), power fail - if
4919 * we use the inode's current i_size, after log replay
4920 * we get a 8Kb file, with the last 4Kb extent as a hole
4921 * (zeroes), as if an expanding truncate happened,
4922 * instead of getting a file of 4Kb only.
4923 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004924 err = logged_inode_size(log, inode, path, &logged_isize);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004925 if (err)
4926 goto out_unlock;
4927 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004928 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004929 &inode->runtime_flags)) {
Filipe Mananaa7429942015-02-13 16:56:14 +00004930 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004931 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004932 ret = drop_objectid_items(trans, log, path, ino,
4933 max_key.type);
4934 } else {
4935 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004936 &inode->runtime_flags);
Filipe Mananaa7429942015-02-13 16:56:14 +00004937 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004938 &inode->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004939 while(1) {
4940 ret = btrfs_truncate_inode_items(trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004941 log, &inode->vfs_inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08004942 if (ret != -EAGAIN)
4943 break;
4944 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004945 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004946 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004947 &inode->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004948 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004949 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004950 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004951 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004952 ret = drop_objectid_items(trans, log, path, ino,
4953 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004954 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004955 if (inode_only == LOG_INODE_ALL)
4956 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004957 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004958 }
Josef Bacika95249b2012-10-11 16:17:34 -04004959
Chris Masone02119d2008-09-05 16:13:11 -04004960 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004961 if (ret) {
4962 err = ret;
4963 goto out_unlock;
4964 }
Chris Masone02119d2008-09-05 16:13:11 -04004965
Chris Masond3977122009-01-05 21:25:51 -05004966 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004967 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004968 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004969 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004970 if (ret < 0) {
4971 err = ret;
4972 goto out_unlock;
4973 }
Chris Masone02119d2008-09-05 16:13:11 -04004974 if (ret != 0)
4975 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004976again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004977 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004978 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004979 break;
4980 if (min_key.type > max_key.type)
4981 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004982
Filipe Mananae4545de2015-06-17 12:49:23 +01004983 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4984 need_log_inode_item = false;
4985
Filipe Manana56f23fd2016-03-30 23:37:21 +01004986 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4987 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
Nikolay Borisova59108a2017-01-18 00:31:48 +02004988 inode->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004989 u64 other_ino = 0;
4990
Filipe Manana56f23fd2016-03-30 23:37:21 +01004991 ret = btrfs_check_ref_name_override(path->nodes[0],
Nikolay Borisova59108a2017-01-18 00:31:48 +02004992 path->slots[0], &min_key, inode,
4993 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004994 if (ret < 0) {
4995 err = ret;
4996 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004997 } else if (ret > 0 && ctx &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004998 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004999 struct btrfs_key inode_key;
5000 struct inode *other_inode;
5001
5002 if (ins_nr > 0) {
5003 ins_nr++;
5004 } else {
5005 ins_nr = 1;
5006 ins_start_slot = path->slots[0];
5007 }
Nikolay Borisova59108a2017-01-18 00:31:48 +02005008 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana44f714d2016-06-06 16:11:13 +01005009 &last_extent, ins_start_slot,
5010 ins_nr, inode_only,
5011 logged_isize);
5012 if (ret < 0) {
5013 err = ret;
5014 goto out_unlock;
5015 }
5016 ins_nr = 0;
5017 btrfs_release_path(path);
5018 inode_key.objectid = other_ino;
5019 inode_key.type = BTRFS_INODE_ITEM_KEY;
5020 inode_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005021 other_inode = btrfs_iget(fs_info->sb,
Filipe Manana44f714d2016-06-06 16:11:13 +01005022 &inode_key, root,
5023 NULL);
5024 /*
5025 * If the other inode that had a conflicting dir
5026 * entry was deleted in the current transaction,
5027 * we don't need to do more work nor fallback to
5028 * a transaction commit.
5029 */
Al Viro8d9e2202018-07-29 23:04:46 +01005030 if (other_inode == ERR_PTR(-ENOENT)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01005031 goto next_key;
5032 } else if (IS_ERR(other_inode)) {
5033 err = PTR_ERR(other_inode);
5034 goto out_unlock;
5035 }
5036 /*
5037 * We are safe logging the other inode without
5038 * acquiring its i_mutex as long as we log with
5039 * the LOG_INODE_EXISTS mode. We're safe against
5040 * concurrent renames of the other inode as well
5041 * because during a rename we pin the log and
5042 * update the log with the new name before we
5043 * unpin it.
5044 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005045 err = btrfs_log_inode(trans, root,
5046 BTRFS_I(other_inode),
5047 LOG_OTHER_INODE, 0, LLONG_MAX,
5048 ctx);
Filipe Manana44f714d2016-06-06 16:11:13 +01005049 iput(other_inode);
5050 if (err)
5051 goto out_unlock;
5052 else
5053 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01005054 }
5055 }
5056
Filipe Manana36283bf2015-06-20 00:44:51 +01005057 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5058 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
5059 if (ins_nr == 0)
5060 goto next_slot;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005061 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01005062 &last_extent, ins_start_slot,
5063 ins_nr, inode_only, logged_isize);
5064 if (ret < 0) {
5065 err = ret;
5066 goto out_unlock;
5067 }
5068 ins_nr = 0;
5069 if (ret) {
5070 btrfs_release_path(path);
5071 continue;
5072 }
5073 goto next_slot;
5074 }
5075
Chris Mason31ff1cd2008-09-11 16:17:57 -04005076 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5077 ins_nr++;
5078 goto next_slot;
5079 } else if (!ins_nr) {
5080 ins_start_slot = path->slots[0];
5081 ins_nr = 1;
5082 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04005083 }
5084
Nikolay Borisova59108a2017-01-18 00:31:48 +02005085 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005086 ins_start_slot, ins_nr, inode_only,
5087 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005088 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005089 err = ret;
5090 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02005091 }
5092 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04005093 ins_nr = 0;
5094 btrfs_release_path(path);
5095 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005096 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005097 ins_nr = 1;
5098 ins_start_slot = path->slots[0];
5099next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04005100
Chris Mason3a5f1d42008-09-11 15:53:37 -04005101 nritems = btrfs_header_nritems(path->nodes[0]);
5102 path->slots[0]++;
5103 if (path->slots[0] < nritems) {
5104 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
5105 path->slots[0]);
5106 goto again;
5107 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005108 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005109 ret = copy_items(trans, inode, dst_path, path,
Josef Bacik16e75492013-10-22 12:18:51 -04005110 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005111 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005112 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005113 err = ret;
5114 goto out_unlock;
5115 }
Josef Bacik16e75492013-10-22 12:18:51 -04005116 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005117 ins_nr = 0;
5118 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005119 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01005120next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005121 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04005122 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005123 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04005124 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005125 min_key.offset = 0;
5126 } else {
Chris Masone02119d2008-09-05 16:13:11 -04005127 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005128 }
Chris Masone02119d2008-09-05 16:13:11 -04005129 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005130 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005131 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005132 ins_start_slot, ins_nr, inode_only,
5133 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005134 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005135 err = ret;
5136 goto out_unlock;
5137 }
Josef Bacik16e75492013-10-22 12:18:51 -04005138 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005139 ins_nr = 0;
5140 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005141
Filipe Manana36283bf2015-06-20 00:44:51 +01005142 btrfs_release_path(path);
5143 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005144 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
Filipe Manana36283bf2015-06-20 00:44:51 +01005145 if (err)
5146 goto out_unlock;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005147 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005148 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5149 btrfs_release_path(path);
5150 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005151 err = btrfs_log_trailing_hole(trans, root, inode, path);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005152 if (err)
5153 goto out_unlock;
5154 }
Josef Bacika95249b2012-10-11 16:17:34 -04005155log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04005156 btrfs_release_path(path);
5157 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01005158 if (need_log_inode_item) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005159 err = log_inode_item(trans, log, dst_path, inode);
Filipe Manana9a8fca62018-05-11 16:42:42 +01005160 if (!err && !xattrs_logged) {
5161 err = btrfs_log_all_xattrs(trans, root, inode, path,
5162 dst_path);
5163 btrfs_release_path(path);
5164 }
Filipe Mananae4545de2015-06-17 12:49:23 +01005165 if (err)
5166 goto out_unlock;
5167 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005168 if (fast_search) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005169 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Josef Bacika2120a42018-05-23 11:58:35 -04005170 ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005171 if (ret) {
5172 err = ret;
5173 goto out_unlock;
5174 }
Josef Bacikd006a042013-11-12 20:54:09 -05005175 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06005176 struct extent_map *em, *n;
5177
Filipe Manana49dae1b2014-09-06 22:34:39 +01005178 write_lock(&em_tree->lock);
5179 /*
5180 * We can't just remove every em if we're called for a ranged
5181 * fsync - that is, one that doesn't cover the whole possible
5182 * file range (0 to LLONG_MAX). This is because we can have
5183 * em's that fall outside the range we're logging and therefore
5184 * their ordered operations haven't completed yet
5185 * (btrfs_finish_ordered_io() not invoked yet). This means we
5186 * didn't get their respective file extent item in the fs/subvol
5187 * tree yet, and need to let the next fast fsync (one which
5188 * consults the list of modified extent maps) find the em so
5189 * that it logs a matching file extent item and waits for the
5190 * respective ordered operation to complete (if it's still
5191 * running).
5192 *
5193 * Removing every em outside the range we're logging would make
5194 * the next fast fsync not log their matching file extent items,
5195 * therefore making us lose data after a log replay.
5196 */
5197 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
5198 list) {
5199 const u64 mod_end = em->mod_start + em->mod_len - 1;
5200
5201 if (em->mod_start >= start && mod_end <= end)
5202 list_del_init(&em->list);
5203 }
5204 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005205 }
5206
Nikolay Borisova59108a2017-01-18 00:31:48 +02005207 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5208 ret = log_directory_changes(trans, root, inode, path, dst_path,
5209 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005210 if (ret) {
5211 err = ret;
5212 goto out_unlock;
5213 }
Chris Masone02119d2008-09-05 16:13:11 -04005214 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005215
Nikolay Borisova59108a2017-01-18 00:31:48 +02005216 spin_lock(&inode->lock);
5217 inode->logged_trans = trans->transid;
5218 inode->last_log_commit = inode->last_sub_trans;
5219 spin_unlock(&inode->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005220out_unlock:
Nikolay Borisova59108a2017-01-18 00:31:48 +02005221 mutex_unlock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04005222
5223 btrfs_free_path(path);
5224 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005225 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005226}
5227
Chris Mason12fcfd22009-03-24 10:24:20 -04005228/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005229 * Check if we must fallback to a transaction commit when logging an inode.
5230 * This must be called after logging the inode and is used only in the context
5231 * when fsyncing an inode requires the need to log some other inode - in which
5232 * case we can't lock the i_mutex of each other inode we need to log as that
5233 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5234 * log inodes up or down in the hierarchy) or rename operations for example. So
5235 * we take the log_mutex of the inode after we have logged it and then check for
5236 * its last_unlink_trans value - this is safe because any task setting
5237 * last_unlink_trans must take the log_mutex and it must do this before it does
5238 * the actual unlink operation, so if we do this check before a concurrent task
5239 * sets last_unlink_trans it means we've logged a consistent version/state of
5240 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005241 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005242 * we logged the inode or it might have also done the unlink).
5243 */
5244static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005245 struct btrfs_inode *inode)
Filipe Manana2be63d52016-02-12 11:34:23 +00005246{
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005247 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana2be63d52016-02-12 11:34:23 +00005248 bool ret = false;
5249
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005250 mutex_lock(&inode->log_mutex);
5251 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
Filipe Manana2be63d52016-02-12 11:34:23 +00005252 /*
5253 * Make sure any commits to the log are forced to be full
5254 * commits.
5255 */
5256 btrfs_set_log_full_commit(fs_info, trans);
5257 ret = true;
5258 }
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005259 mutex_unlock(&inode->log_mutex);
Filipe Manana2be63d52016-02-12 11:34:23 +00005260
5261 return ret;
5262}
5263
5264/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005265 * follow the dentry parent pointers up the chain and see if any
5266 * of the directories in it require a full commit before they can
5267 * be logged. Returns zero if nothing special needs to be done or 1 if
5268 * a full commit is required.
5269 */
5270static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005271 struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005272 struct dentry *parent,
5273 struct super_block *sb,
5274 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005275{
Chris Mason12fcfd22009-03-24 10:24:20 -04005276 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005277 struct dentry *old_parent = NULL;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005278 struct btrfs_inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005279
Chris Masonaf4176b2009-03-24 10:24:31 -04005280 /*
5281 * for regular files, if its inode is already on disk, we don't
5282 * have to worry about the parents at all. This is because
5283 * we can use the last_unlink_trans field to record renames
5284 * and other fun in this file.
5285 */
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005286 if (S_ISREG(inode->vfs_inode.i_mode) &&
5287 inode->generation <= last_committed &&
5288 inode->last_unlink_trans <= last_committed)
5289 goto out;
Chris Masonaf4176b2009-03-24 10:24:31 -04005290
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005291 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005292 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005293 goto out;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005294 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005295 }
5296
5297 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005298 /*
5299 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005300 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005301 * logged_trans so that further down in the log code we don't
5302 * think this inode has already been logged.
5303 */
5304 if (inode != orig_inode)
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005305 inode->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005306 smp_mb();
5307
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005308 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005309 ret = 1;
5310 break;
5311 }
5312
Al Virofc640052016-04-10 01:33:30 -04005313 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005314 break;
5315
Filipe Manana44f714d2016-06-06 16:11:13 +01005316 if (IS_ROOT(parent)) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005317 inode = BTRFS_I(d_inode(parent));
5318 if (btrfs_must_commit_transaction(trans, inode))
Filipe Manana44f714d2016-06-06 16:11:13 +01005319 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005320 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005321 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005322
Josef Bacik6a912212010-11-20 09:48:00 +00005323 parent = dget_parent(parent);
5324 dput(old_parent);
5325 old_parent = parent;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005326 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005327
5328 }
Josef Bacik6a912212010-11-20 09:48:00 +00005329 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005330out:
Chris Masone02119d2008-09-05 16:13:11 -04005331 return ret;
5332}
5333
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005334struct btrfs_dir_list {
5335 u64 ino;
5336 struct list_head list;
5337};
5338
5339/*
5340 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5341 * details about the why it is needed.
5342 * This is a recursive operation - if an existing dentry corresponds to a
5343 * directory, that directory's new entries are logged too (same behaviour as
5344 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5345 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5346 * complains about the following circular lock dependency / possible deadlock:
5347 *
5348 * CPU0 CPU1
5349 * ---- ----
5350 * lock(&type->i_mutex_dir_key#3/2);
5351 * lock(sb_internal#2);
5352 * lock(&type->i_mutex_dir_key#3/2);
5353 * lock(&sb->s_type->i_mutex_key#14);
5354 *
5355 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5356 * sb_start_intwrite() in btrfs_start_transaction().
5357 * Not locking i_mutex of the inodes is still safe because:
5358 *
5359 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5360 * that while logging the inode new references (names) are added or removed
5361 * from the inode, leaving the logged inode item with a link count that does
5362 * not match the number of logged inode reference items. This is fine because
5363 * at log replay time we compute the real number of links and correct the
5364 * link count in the inode item (see replay_one_buffer() and
5365 * link_to_fixup_dir());
5366 *
5367 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5368 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5369 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5370 * has a size that doesn't match the sum of the lengths of all the logged
5371 * names. This does not result in a problem because if a dir_item key is
5372 * logged but its matching dir_index key is not logged, at log replay time we
5373 * don't use it to replay the respective name (see replay_one_name()). On the
5374 * other hand if only the dir_index key ends up being logged, the respective
5375 * name is added to the fs/subvol tree with both the dir_item and dir_index
5376 * keys created (see replay_one_name()).
5377 * The directory's inode item with a wrong i_size is not a problem as well,
5378 * since we don't use it at log replay time to set the i_size in the inode
5379 * item of the fs/subvol tree (see overwrite_item()).
5380 */
5381static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5382 struct btrfs_root *root,
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005383 struct btrfs_inode *start_inode,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005384 struct btrfs_log_ctx *ctx)
5385{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005386 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005387 struct btrfs_root *log = root->log_root;
5388 struct btrfs_path *path;
5389 LIST_HEAD(dir_list);
5390 struct btrfs_dir_list *dir_elem;
5391 int ret = 0;
5392
5393 path = btrfs_alloc_path();
5394 if (!path)
5395 return -ENOMEM;
5396
5397 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5398 if (!dir_elem) {
5399 btrfs_free_path(path);
5400 return -ENOMEM;
5401 }
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005402 dir_elem->ino = btrfs_ino(start_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005403 list_add_tail(&dir_elem->list, &dir_list);
5404
5405 while (!list_empty(&dir_list)) {
5406 struct extent_buffer *leaf;
5407 struct btrfs_key min_key;
5408 int nritems;
5409 int i;
5410
5411 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5412 list);
5413 if (ret)
5414 goto next_dir_inode;
5415
5416 min_key.objectid = dir_elem->ino;
5417 min_key.type = BTRFS_DIR_ITEM_KEY;
5418 min_key.offset = 0;
5419again:
5420 btrfs_release_path(path);
5421 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5422 if (ret < 0) {
5423 goto next_dir_inode;
5424 } else if (ret > 0) {
5425 ret = 0;
5426 goto next_dir_inode;
5427 }
5428
5429process_leaf:
5430 leaf = path->nodes[0];
5431 nritems = btrfs_header_nritems(leaf);
5432 for (i = path->slots[0]; i < nritems; i++) {
5433 struct btrfs_dir_item *di;
5434 struct btrfs_key di_key;
5435 struct inode *di_inode;
5436 struct btrfs_dir_list *new_dir_elem;
5437 int log_mode = LOG_INODE_EXISTS;
5438 int type;
5439
5440 btrfs_item_key_to_cpu(leaf, &min_key, i);
5441 if (min_key.objectid != dir_elem->ino ||
5442 min_key.type != BTRFS_DIR_ITEM_KEY)
5443 goto next_dir_inode;
5444
5445 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5446 type = btrfs_dir_type(leaf, di);
5447 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5448 type != BTRFS_FT_DIR)
5449 continue;
5450 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5451 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5452 continue;
5453
Robbie Koec125cf2016-10-28 10:48:26 +08005454 btrfs_release_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005455 di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005456 if (IS_ERR(di_inode)) {
5457 ret = PTR_ERR(di_inode);
5458 goto next_dir_inode;
5459 }
5460
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02005461 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005462 iput(di_inode);
Robbie Koec125cf2016-10-28 10:48:26 +08005463 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005464 }
5465
5466 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005467 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005468 log_mode = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005469 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005470 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005471 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005472 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005473 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005474 iput(di_inode);
5475 if (ret)
5476 goto next_dir_inode;
5477 if (ctx->log_new_dentries) {
5478 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5479 GFP_NOFS);
5480 if (!new_dir_elem) {
5481 ret = -ENOMEM;
5482 goto next_dir_inode;
5483 }
5484 new_dir_elem->ino = di_key.objectid;
5485 list_add_tail(&new_dir_elem->list, &dir_list);
5486 }
5487 break;
5488 }
5489 if (i == nritems) {
5490 ret = btrfs_next_leaf(log, path);
5491 if (ret < 0) {
5492 goto next_dir_inode;
5493 } else if (ret > 0) {
5494 ret = 0;
5495 goto next_dir_inode;
5496 }
5497 goto process_leaf;
5498 }
5499 if (min_key.offset < (u64)-1) {
5500 min_key.offset++;
5501 goto again;
5502 }
5503next_dir_inode:
5504 list_del(&dir_elem->list);
5505 kfree(dir_elem);
5506 }
5507
5508 btrfs_free_path(path);
5509 return ret;
5510}
5511
Filipe Manana18aa0922015-08-05 16:49:08 +01005512static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005513 struct btrfs_inode *inode,
Filipe Manana18aa0922015-08-05 16:49:08 +01005514 struct btrfs_log_ctx *ctx)
5515{
David Sterba3ffbd682018-06-29 10:56:42 +02005516 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana18aa0922015-08-05 16:49:08 +01005517 int ret;
5518 struct btrfs_path *path;
5519 struct btrfs_key key;
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005520 struct btrfs_root *root = inode->root;
5521 const u64 ino = btrfs_ino(inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005522
5523 path = btrfs_alloc_path();
5524 if (!path)
5525 return -ENOMEM;
5526 path->skip_locking = 1;
5527 path->search_commit_root = 1;
5528
5529 key.objectid = ino;
5530 key.type = BTRFS_INODE_REF_KEY;
5531 key.offset = 0;
5532 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5533 if (ret < 0)
5534 goto out;
5535
5536 while (true) {
5537 struct extent_buffer *leaf = path->nodes[0];
5538 int slot = path->slots[0];
5539 u32 cur_offset = 0;
5540 u32 item_size;
5541 unsigned long ptr;
5542
5543 if (slot >= btrfs_header_nritems(leaf)) {
5544 ret = btrfs_next_leaf(root, path);
5545 if (ret < 0)
5546 goto out;
5547 else if (ret > 0)
5548 break;
5549 continue;
5550 }
5551
5552 btrfs_item_key_to_cpu(leaf, &key, slot);
5553 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5554 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5555 break;
5556
5557 item_size = btrfs_item_size_nr(leaf, slot);
5558 ptr = btrfs_item_ptr_offset(leaf, slot);
5559 while (cur_offset < item_size) {
5560 struct btrfs_key inode_key;
5561 struct inode *dir_inode;
5562
5563 inode_key.type = BTRFS_INODE_ITEM_KEY;
5564 inode_key.offset = 0;
5565
5566 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5567 struct btrfs_inode_extref *extref;
5568
5569 extref = (struct btrfs_inode_extref *)
5570 (ptr + cur_offset);
5571 inode_key.objectid = btrfs_inode_extref_parent(
5572 leaf, extref);
5573 cur_offset += sizeof(*extref);
5574 cur_offset += btrfs_inode_extref_name_len(leaf,
5575 extref);
5576 } else {
5577 inode_key.objectid = key.offset;
5578 cur_offset = item_size;
5579 }
5580
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005581 dir_inode = btrfs_iget(fs_info->sb, &inode_key,
Filipe Manana18aa0922015-08-05 16:49:08 +01005582 root, NULL);
Filipe Manana0f375ee2018-10-09 15:05:29 +01005583 /*
5584 * If the parent inode was deleted, return an error to
5585 * fallback to a transaction commit. This is to prevent
5586 * getting an inode that was moved from one parent A to
5587 * a parent B, got its former parent A deleted and then
5588 * it got fsync'ed, from existing at both parents after
5589 * a log replay (and the old parent still existing).
5590 * Example:
5591 *
5592 * mkdir /mnt/A
5593 * mkdir /mnt/B
5594 * touch /mnt/B/bar
5595 * sync
5596 * mv /mnt/B/bar /mnt/A/bar
5597 * mv -T /mnt/A /mnt/B
5598 * fsync /mnt/B/bar
5599 * <power fail>
5600 *
5601 * If we ignore the old parent B which got deleted,
5602 * after a log replay we would have file bar linked
5603 * at both parents and the old parent B would still
5604 * exist.
5605 */
5606 if (IS_ERR(dir_inode)) {
5607 ret = PTR_ERR(dir_inode);
5608 goto out;
5609 }
Filipe Manana18aa0922015-08-05 16:49:08 +01005610
Filipe Manana657ed1a2016-04-06 17:11:56 +01005611 if (ctx)
5612 ctx->log_new_dentries = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005613 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
Filipe Manana18aa0922015-08-05 16:49:08 +01005614 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005615 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005616 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005617 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005618 if (!ret && ctx && ctx->log_new_dentries)
5619 ret = log_new_dir_dentries(trans, root,
David Sterbaf85b7372017-01-20 14:54:07 +01005620 BTRFS_I(dir_inode), ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005621 iput(dir_inode);
5622 if (ret)
5623 goto out;
5624 }
5625 path->slots[0]++;
5626 }
5627 ret = 0;
5628out:
5629 btrfs_free_path(path);
5630 return ret;
5631}
5632
Chris Masone02119d2008-09-05 16:13:11 -04005633/*
5634 * helper function around btrfs_log_inode to make sure newly created
5635 * parent directories also end up in the log. A minimal inode and backref
5636 * only logging is done of any parent directories that are older than
5637 * the last committed transaction
5638 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005639static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005640 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005641 struct dentry *parent,
5642 const loff_t start,
5643 const loff_t end,
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07005644 int inode_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005645 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005646{
Nikolay Borisovf8822742018-02-27 17:37:17 +02005647 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005648 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04005649 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005650 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005651 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005652 u64 last_committed = fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005653 bool log_dentries = false;
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005654 struct btrfs_inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005655
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005656 sb = inode->vfs_inode.i_sb;
Chris Mason12fcfd22009-03-24 10:24:20 -04005657
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005658 if (btrfs_test_opt(fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005659 ret = 1;
5660 goto end_no_trans;
5661 }
5662
Miao Xie995946d2014-04-02 19:51:06 +08005663 /*
5664 * The prev transaction commit doesn't complete, we need do
5665 * full commit by ourselves.
5666 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005667 if (fs_info->last_trans_log_full_commit >
5668 fs_info->last_trans_committed) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005669 ret = 1;
5670 goto end_no_trans;
5671 }
5672
Nikolay Borisovf8822742018-02-27 17:37:17 +02005673 if (btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005674 ret = 1;
5675 goto end_no_trans;
5676 }
5677
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005678 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
5679 last_committed);
Chris Mason12fcfd22009-03-24 10:24:20 -04005680 if (ret)
5681 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005682
Filipe Mananaf2d72f42018-10-08 11:12:55 +01005683 /*
5684 * Skip already logged inodes or inodes corresponding to tmpfiles
5685 * (since logging them is pointless, a link count of 0 means they
5686 * will never be accessible).
5687 */
5688 if (btrfs_inode_in_log(inode, trans->transid) ||
5689 inode->vfs_inode.i_nlink == 0) {
Chris Mason257c62e2009-10-13 13:21:08 -04005690 ret = BTRFS_NO_LOG_SYNC;
5691 goto end_no_trans;
5692 }
5693
Miao Xie8b050d32014-02-20 18:08:58 +08005694 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005695 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005696 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005697
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005698 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005699 if (ret)
5700 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005701
Chris Masonaf4176b2009-03-24 10:24:31 -04005702 /*
5703 * for regular files, if its inode is already on disk, we don't
5704 * have to worry about the parents at all. This is because
5705 * we can use the last_unlink_trans field to record renames
5706 * and other fun in this file.
5707 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005708 if (S_ISREG(inode->vfs_inode.i_mode) &&
5709 inode->generation <= last_committed &&
5710 inode->last_unlink_trans <= last_committed) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005711 ret = 0;
5712 goto end_trans;
5713 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005714
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005715 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005716 log_dentries = true;
5717
Filipe Manana18aa0922015-08-05 16:49:08 +01005718 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005719 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005720 * inodes are fully logged. This is to prevent leaving dangling
5721 * directory index entries in directories that were our parents but are
5722 * not anymore. Not doing this results in old parent directory being
5723 * impossible to delete after log replay (rmdir will always fail with
5724 * error -ENOTEMPTY).
5725 *
5726 * Example 1:
5727 *
5728 * mkdir testdir
5729 * touch testdir/foo
5730 * ln testdir/foo testdir/bar
5731 * sync
5732 * unlink testdir/bar
5733 * xfs_io -c fsync testdir/foo
5734 * <power failure>
5735 * mount fs, triggers log replay
5736 *
5737 * If we don't log the parent directory (testdir), after log replay the
5738 * directory still has an entry pointing to the file inode using the bar
5739 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5740 * the file inode has a link count of 1.
5741 *
5742 * Example 2:
5743 *
5744 * mkdir testdir
5745 * touch foo
5746 * ln foo testdir/foo2
5747 * ln foo testdir/foo3
5748 * sync
5749 * unlink testdir/foo3
5750 * xfs_io -c fsync foo
5751 * <power failure>
5752 * mount fs, triggers log replay
5753 *
5754 * Similar as the first example, after log replay the parent directory
5755 * testdir still has an entry pointing to the inode file with name foo3
5756 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5757 * and has a link count of 2.
5758 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005759 if (inode->last_unlink_trans > last_committed) {
Filipe Manana18aa0922015-08-05 16:49:08 +01005760 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5761 if (ret)
5762 goto end_trans;
5763 }
5764
Chris Masond3977122009-01-05 21:25:51 -05005765 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005766 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005767 break;
5768
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005769 inode = BTRFS_I(d_inode(parent));
5770 if (root != inode->root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005771 break;
5772
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005773 if (inode->generation > last_committed) {
5774 ret = btrfs_log_inode(trans, root, inode,
5775 LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005776 if (ret)
5777 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005778 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005779 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005780 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005781
Josef Bacik6a912212010-11-20 09:48:00 +00005782 parent = dget_parent(parent);
5783 dput(old_parent);
5784 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005785 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005786 if (log_dentries)
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005787 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005788 else
5789 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005790end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005791 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005792 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005793 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005794 ret = 1;
5795 }
Miao Xie8b050d32014-02-20 18:08:58 +08005796
5797 if (ret)
5798 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005799 btrfs_end_log_trans(root);
5800end_no_trans:
5801 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005802}
5803
5804/*
5805 * it is not safe to log dentry if the chunk root has added new
5806 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5807 * If this returns 1, you must commit the transaction to safely get your
5808 * data on disk.
5809 */
5810int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Nikolay Borisove5b84f7a2018-02-27 17:37:18 +02005811 struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005812 const loff_t start,
5813 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005814 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005815{
Josef Bacik6a912212010-11-20 09:48:00 +00005816 struct dentry *parent = dget_parent(dentry);
5817 int ret;
5818
Nikolay Borisovf8822742018-02-27 17:37:17 +02005819 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
5820 start, end, LOG_INODE_ALL, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005821 dput(parent);
5822
5823 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005824}
5825
5826/*
5827 * should be called during mount to recover any replay any log trees
5828 * from the FS
5829 */
5830int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5831{
5832 int ret;
5833 struct btrfs_path *path;
5834 struct btrfs_trans_handle *trans;
5835 struct btrfs_key key;
5836 struct btrfs_key found_key;
5837 struct btrfs_key tmp_key;
5838 struct btrfs_root *log;
5839 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5840 struct walk_control wc = {
5841 .process_func = process_one_buffer,
5842 .stage = 0,
5843 };
5844
Chris Masone02119d2008-09-05 16:13:11 -04005845 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005846 if (!path)
5847 return -ENOMEM;
5848
Josef Bacikafcdd122016-09-02 15:40:02 -04005849 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005850
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005851 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005852 if (IS_ERR(trans)) {
5853 ret = PTR_ERR(trans);
5854 goto error;
5855 }
Chris Masone02119d2008-09-05 16:13:11 -04005856
5857 wc.trans = trans;
5858 wc.pin = 1;
5859
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005860 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005861 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005862 btrfs_handle_fs_error(fs_info, ret,
5863 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005864 goto error;
5865 }
Chris Masone02119d2008-09-05 16:13:11 -04005866
5867again:
5868 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5869 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005870 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005871
Chris Masond3977122009-01-05 21:25:51 -05005872 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005873 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005874
5875 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005876 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005877 "Couldn't find tree log root.");
5878 goto error;
5879 }
Chris Masone02119d2008-09-05 16:13:11 -04005880 if (ret > 0) {
5881 if (path->slots[0] == 0)
5882 break;
5883 path->slots[0]--;
5884 }
5885 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5886 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005887 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005888 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5889 break;
5890
Miao Xiecb517ea2013-05-15 07:48:19 +00005891 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005892 if (IS_ERR(log)) {
5893 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005894 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005895 "Couldn't read tree log root.");
5896 goto error;
5897 }
Chris Masone02119d2008-09-05 16:13:11 -04005898
5899 tmp_key.objectid = found_key.offset;
5900 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5901 tmp_key.offset = (u64)-1;
5902
5903 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005904 if (IS_ERR(wc.replay_dest)) {
5905 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005906 free_extent_buffer(log->node);
5907 free_extent_buffer(log->commit_root);
5908 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005909 btrfs_handle_fs_error(fs_info, ret,
5910 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005911 goto error;
5912 }
Chris Masone02119d2008-09-05 16:13:11 -04005913
Yan Zheng07d400a2009-01-06 11:42:00 -05005914 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005915 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005916 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005917
Josef Bacikb50c6e22013-04-25 15:55:30 -04005918 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005919 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5920 path);
Chris Masone02119d2008-09-05 16:13:11 -04005921 }
Chris Masone02119d2008-09-05 16:13:11 -04005922
Liu Bo900c9982018-01-25 11:02:56 -07005923 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5924 struct btrfs_root *root = wc.replay_dest;
5925
5926 btrfs_release_path(path);
5927
5928 /*
5929 * We have just replayed everything, and the highest
5930 * objectid of fs roots probably has changed in case
5931 * some inode_item's got replayed.
5932 *
5933 * root->objectid_mutex is not acquired as log replay
5934 * could only happen during mount.
5935 */
5936 ret = btrfs_find_highest_objectid(root,
5937 &root->highest_objectid);
5938 }
5939
Chris Masone02119d2008-09-05 16:13:11 -04005940 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005941 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005942 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005943 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005944 kfree(log);
5945
Josef Bacikb50c6e22013-04-25 15:55:30 -04005946 if (ret)
5947 goto error;
5948
Chris Masone02119d2008-09-05 16:13:11 -04005949 if (found_key.offset == 0)
5950 break;
5951 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005952 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005953
5954 /* step one is to pin it all, step two is to replay just inodes */
5955 if (wc.pin) {
5956 wc.pin = 0;
5957 wc.process_func = replay_one_buffer;
5958 wc.stage = LOG_WALK_REPLAY_INODES;
5959 goto again;
5960 }
5961 /* step three is to replay everything */
5962 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5963 wc.stage++;
5964 goto again;
5965 }
5966
5967 btrfs_free_path(path);
5968
Josef Bacikabefa552013-04-24 16:40:05 -04005969 /* step 4: commit the transaction, which also unpins the blocks */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005970 ret = btrfs_commit_transaction(trans);
Josef Bacikabefa552013-04-24 16:40:05 -04005971 if (ret)
5972 return ret;
5973
Chris Masone02119d2008-09-05 16:13:11 -04005974 free_extent_buffer(log_root_tree->node);
5975 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005976 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005977 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005978
Josef Bacikabefa552013-04-24 16:40:05 -04005979 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005980error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005981 if (wc.trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005982 btrfs_end_transaction(wc.trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005983 btrfs_free_path(path);
5984 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005985}
Chris Mason12fcfd22009-03-24 10:24:20 -04005986
5987/*
5988 * there are some corner cases where we want to force a full
5989 * commit instead of allowing a directory to be logged.
5990 *
5991 * They revolve around files there were unlinked from the directory, and
5992 * this function updates the parent directory so that a full commit is
5993 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005994 *
5995 * Must be called before the unlink operations (updates to the subvolume tree,
5996 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005997 */
5998void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005999 struct btrfs_inode *dir, struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04006000 int for_rename)
6001{
6002 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006003 * when we're logging a file, if it hasn't been renamed
6004 * or unlinked, and its inode is fully committed on disk,
6005 * we don't have to worry about walking up the directory chain
6006 * to log its parents.
6007 *
6008 * So, we use the last_unlink_trans field to put this transid
6009 * into the file. When the file is logged we check it and
6010 * don't log the parents if the file is fully on disk.
6011 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006012 mutex_lock(&inode->log_mutex);
6013 inode->last_unlink_trans = trans->transid;
6014 mutex_unlock(&inode->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04006015
6016 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006017 * if this directory was already logged any new
6018 * names for this file/dir will get recorded
6019 */
6020 smp_mb();
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006021 if (dir->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006022 return;
6023
6024 /*
6025 * if the inode we're about to unlink was logged,
6026 * the log will be properly updated for any new names
6027 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006028 if (inode->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04006029 return;
6030
6031 /*
6032 * when renaming files across directories, if the directory
6033 * there we're unlinking from gets fsync'd later on, there's
6034 * no way to find the destination directory later and fsync it
6035 * properly. So, we have to be conservative and force commits
6036 * so the new name gets discovered.
6037 */
6038 if (for_rename)
6039 goto record;
6040
6041 /* we can safely do the unlink without any special recording */
6042 return;
6043
6044record:
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006045 mutex_lock(&dir->log_mutex);
6046 dir->last_unlink_trans = trans->transid;
6047 mutex_unlock(&dir->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04006048}
6049
6050/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006051 * Make sure that if someone attempts to fsync the parent directory of a deleted
6052 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6053 * that after replaying the log tree of the parent directory's root we will not
6054 * see the snapshot anymore and at log replay time we will not see any log tree
6055 * corresponding to the deleted snapshot's root, which could lead to replaying
6056 * it after replaying the log tree of the parent directory (which would replay
6057 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00006058 *
6059 * Must be called before the actual snapshot destroy operation (updates to the
6060 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006061 */
6062void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
Nikolay Borisov43663552017-01-18 00:31:29 +02006063 struct btrfs_inode *dir)
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006064{
Nikolay Borisov43663552017-01-18 00:31:29 +02006065 mutex_lock(&dir->log_mutex);
6066 dir->last_unlink_trans = trans->transid;
6067 mutex_unlock(&dir->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006068}
6069
6070/*
Chris Mason12fcfd22009-03-24 10:24:20 -04006071 * Call this after adding a new name for a file and it will properly
6072 * update the log to reflect the new name.
6073 *
Filipe Mananad4682ba2018-06-11 19:24:28 +01006074 * @ctx can not be NULL when @sync_log is false, and should be NULL when it's
6075 * true (because it's not used).
6076 *
6077 * Return value depends on whether @sync_log is true or false.
6078 * When true: returns BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6079 * committed by the caller, and BTRFS_DONT_NEED_TRANS_COMMIT
6080 * otherwise.
6081 * When false: returns BTRFS_DONT_NEED_LOG_SYNC if the caller does not need to
6082 * to sync the log, BTRFS_NEED_LOG_SYNC if it needs to sync the log,
6083 * or BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6084 * committed (without attempting to sync the log).
Chris Mason12fcfd22009-03-24 10:24:20 -04006085 */
6086int btrfs_log_new_name(struct btrfs_trans_handle *trans,
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006087 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Filipe Mananad4682ba2018-06-11 19:24:28 +01006088 struct dentry *parent,
6089 bool sync_log, struct btrfs_log_ctx *ctx)
Chris Mason12fcfd22009-03-24 10:24:20 -04006090{
David Sterba3ffbd682018-06-29 10:56:42 +02006091 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006092 int ret;
Chris Mason12fcfd22009-03-24 10:24:20 -04006093
6094 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006095 * this will force the logging code to walk the dentry chain
6096 * up for the file
6097 */
Filipe Manana9a6509c2018-02-28 15:55:40 +00006098 if (!S_ISDIR(inode->vfs_inode.i_mode))
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006099 inode->last_unlink_trans = trans->transid;
Chris Masonaf4176b2009-03-24 10:24:31 -04006100
6101 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006102 * if this inode hasn't been logged and directory we're renaming it
6103 * from hasn't been logged, we don't need to log it
6104 */
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006105 if (inode->logged_trans <= fs_info->last_trans_committed &&
6106 (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
Filipe Mananad4682ba2018-06-11 19:24:28 +01006107 return sync_log ? BTRFS_DONT_NEED_TRANS_COMMIT :
6108 BTRFS_DONT_NEED_LOG_SYNC;
Chris Mason12fcfd22009-03-24 10:24:20 -04006109
Filipe Mananad4682ba2018-06-11 19:24:28 +01006110 if (sync_log) {
6111 struct btrfs_log_ctx ctx2;
6112
6113 btrfs_init_log_ctx(&ctx2, &inode->vfs_inode);
6114 ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6115 LOG_INODE_EXISTS, &ctx2);
6116 if (ret == BTRFS_NO_LOG_SYNC)
6117 return BTRFS_DONT_NEED_TRANS_COMMIT;
6118 else if (ret)
6119 return BTRFS_NEED_TRANS_COMMIT;
6120
6121 ret = btrfs_sync_log(trans, inode->root, &ctx2);
6122 if (ret)
6123 return BTRFS_NEED_TRANS_COMMIT;
6124 return BTRFS_DONT_NEED_TRANS_COMMIT;
6125 }
6126
6127 ASSERT(ctx);
6128 ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6129 LOG_INODE_EXISTS, ctx);
6130 if (ret == BTRFS_NO_LOG_SYNC)
6131 return BTRFS_DONT_NEED_LOG_SYNC;
6132 else if (ret)
6133 return BTRFS_NEED_TRANS_COMMIT;
6134
6135 return BTRFS_NEED_LOG_SYNC;
Chris Mason12fcfd22009-03-24 10:24:20 -04006136}
6137