blob: 3c2ae0e4f25a8de78040f6ca0ca94d070f158dd2 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Masone02119d2008-09-05 16:13:11 -04002/*
3 * Copyright (C) 2008 Oracle. All rights reserved.
Chris Masone02119d2008-09-05 16:13:11 -04004 */
5
6#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Miao Xiec6adc9c2013-05-28 10:05:39 +00008#include <linux/blkdev.h>
Josef Bacik5dc562c2012-08-17 13:14:17 -04009#include <linux/list_sort.h>
Jeff Laytonc7f88c42017-12-11 06:35:12 -050010#include <linux/iversion.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020011#include "ctree.h"
Miao Xie995946d2014-04-02 19:51:06 +080012#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040013#include "disk-io.h"
14#include "locking.h"
15#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070016#include "backref.h"
Anand Jainebb87652016-03-10 17:26:59 +080017#include "compression.h"
Qu Wenruodf2c95f2016-08-15 10:36:52 +080018#include "qgroup.h"
Liu Bo900c9982018-01-25 11:02:56 -070019#include "inode-map.h"
Chris Masone02119d2008-09-05 16:13:11 -040020
21/* magic values for the inode_only field in btrfs_log_inode:
22 *
23 * LOG_INODE_ALL means to log everything
24 * LOG_INODE_EXISTS means to log just enough to recreate the inode
25 * during log replay
26 */
27#define LOG_INODE_ALL 0
28#define LOG_INODE_EXISTS 1
Liu Bo781feef2016-11-30 16:20:25 -080029#define LOG_OTHER_INODE 2
Chris Masone02119d2008-09-05 16:13:11 -040030
31/*
Chris Mason12fcfd22009-03-24 10:24:20 -040032 * directory trouble cases
33 *
34 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
35 * log, we must force a full commit before doing an fsync of the directory
36 * where the unlink was done.
37 * ---> record transid of last unlink/rename per directory
38 *
39 * mkdir foo/some_dir
40 * normal commit
41 * rename foo/some_dir foo2/some_dir
42 * mkdir foo/some_dir
43 * fsync foo/some_dir/some_file
44 *
45 * The fsync above will unlink the original some_dir without recording
46 * it in its new location (foo2). After a crash, some_dir will be gone
47 * unless the fsync of some_file forces a full commit
48 *
49 * 2) we must log any new names for any file or dir that is in the fsync
50 * log. ---> check inode while renaming/linking.
51 *
52 * 2a) we must log any new names for any file or dir during rename
53 * when the directory they are being removed from was logged.
54 * ---> check inode and old parent dir during rename
55 *
56 * 2a is actually the more important variant. With the extra logging
57 * a crash might unlink the old name without recreating the new one
58 *
59 * 3) after a crash, we must go through any directories with a link count
60 * of zero and redo the rm -rf
61 *
62 * mkdir f1/foo
63 * normal commit
64 * rm -rf f1/foo
65 * fsync(f1)
66 *
67 * The directory f1 was fully removed from the FS, but fsync was never
68 * called on f1, only its parent dir. After a crash the rm -rf must
69 * be replayed. This must be able to recurse down the entire
70 * directory tree. The inode link count fixup code takes care of the
71 * ugly details.
72 */
73
74/*
Chris Masone02119d2008-09-05 16:13:11 -040075 * stages for the tree walking. The first
76 * stage (0) is to only pin down the blocks we find
77 * the second stage (1) is to make sure that all the inodes
78 * we find in the log are created in the subvolume.
79 *
80 * The last stage is to deal with directories and links and extents
81 * and all the other fun semantics
82 */
83#define LOG_WALK_PIN_ONLY 0
84#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040085#define LOG_WALK_REPLAY_DIR_INDEX 2
86#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040087
Chris Mason12fcfd22009-03-24 10:24:20 -040088static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +020089 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +010090 int inode_only,
91 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +010092 const loff_t end,
93 struct btrfs_log_ctx *ctx);
Yan Zhengec051c02009-01-05 15:43:42 -050094static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
95 struct btrfs_root *root,
96 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -040097static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root,
99 struct btrfs_root *log,
100 struct btrfs_path *path,
101 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400102
103/*
104 * tree logging is a special write ahead log used to make sure that
105 * fsyncs and O_SYNCs can happen without doing full tree commits.
106 *
107 * Full tree commits are expensive because they require commonly
108 * modified blocks to be recowed, creating many dirty pages in the
109 * extent tree an 4x-6x higher write load than ext3.
110 *
111 * Instead of doing a tree commit on every fsync, we use the
112 * key ranges and transaction ids to find items for a given file or directory
113 * that have changed in this transaction. Those items are copied into
114 * a special tree (one per subvolume root), that tree is written to disk
115 * and then the fsync is considered complete.
116 *
117 * After a crash, items are copied out of the log-tree back into the
118 * subvolume tree. Any file data extents found are recorded in the extent
119 * allocation tree, and the log-tree freed.
120 *
121 * The log tree is read three times, once to pin down all the extents it is
122 * using in ram and once, once to create all the inodes logged in the tree
123 * and once to do all the other items.
124 */
125
126/*
Chris Masone02119d2008-09-05 16:13:11 -0400127 * start a sub transaction and setup the log tree
128 * this increments the log tree writer count to make the people
129 * syncing the tree wait for us to finish
130 */
131static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800132 struct btrfs_root *root,
133 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400134{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400135 struct btrfs_fs_info *fs_info = root->fs_info;
Zhaolei34eb2a52015-08-17 18:44:45 +0800136 int ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500137
138 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800139
Yan Zheng7237f182009-01-21 12:54:03 -0500140 if (root->log_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400141 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800142 ret = -EAGAIN;
143 goto out;
144 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800145
Josef Bacikff782e02009-10-08 15:30:04 -0400146 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800147 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800148 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400149 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800150 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400151 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800152 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400153 mutex_lock(&fs_info->tree_log_mutex);
154 if (!fs_info->log_root_tree)
155 ret = btrfs_init_log_root_tree(trans, fs_info);
156 mutex_unlock(&fs_info->tree_log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800157 if (ret)
158 goto out;
Josef Bacikff782e02009-10-08 15:30:04 -0400159
Chris Masone02119d2008-09-05 16:13:11 -0400160 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400161 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800162 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800163
164 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
165 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400166 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800167
Miao Xie2ecb7922012-09-06 04:04:27 -0600168 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500169 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800170 if (ctx) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800171 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800172 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800173 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800174 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800175
Miao Xiee87ac132014-02-20 18:08:53 +0800176out:
Yan Zheng7237f182009-01-21 12:54:03 -0500177 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800178 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400179}
180
181/*
182 * returns 0 if there was a log transaction running and we were able
183 * to join, or returns -ENOENT if there were not transactions
184 * in progress
185 */
186static int join_running_log_trans(struct btrfs_root *root)
187{
188 int ret = -ENOENT;
189
190 smp_mb();
191 if (!root->log_root)
192 return -ENOENT;
193
Yan Zheng7237f182009-01-21 12:54:03 -0500194 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400195 if (root->log_root) {
196 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500197 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400198 }
Yan Zheng7237f182009-01-21 12:54:03 -0500199 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400200 return ret;
201}
202
203/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400204 * This either makes the current running log transaction wait
205 * until you call btrfs_end_log_trans() or it makes any future
206 * log transactions wait until you call btrfs_end_log_trans()
207 */
208int btrfs_pin_log_trans(struct btrfs_root *root)
209{
210 int ret = -ENOENT;
211
212 mutex_lock(&root->log_mutex);
213 atomic_inc(&root->log_writers);
214 mutex_unlock(&root->log_mutex);
215 return ret;
216}
217
218/*
Chris Masone02119d2008-09-05 16:13:11 -0400219 * indicate we're done making changes to the log tree
220 * and wake up anyone waiting to do a sync
221 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100222void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400223{
Yan Zheng7237f182009-01-21 12:54:03 -0500224 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba093258e2018-02-26 16:15:17 +0100225 /* atomic_dec_and_test implies a barrier */
226 cond_wake_up_nomb(&root->log_writer_wait);
Yan Zheng7237f182009-01-21 12:54:03 -0500227 }
Chris Masone02119d2008-09-05 16:13:11 -0400228}
229
230
231/*
232 * the walk control struct is used to pass state down the chain when
233 * processing the log tree. The stage field tells us which part
234 * of the log tree processing we are currently doing. The others
235 * are state fields used for that specific part
236 */
237struct walk_control {
238 /* should we free the extent on disk when done? This is used
239 * at transaction commit time while freeing a log tree
240 */
241 int free;
242
243 /* should we write out the extent buffer? This is used
244 * while flushing the log tree to disk during a sync
245 */
246 int write;
247
248 /* should we wait for the extent buffer io to finish? Also used
249 * while flushing the log tree to disk for a sync
250 */
251 int wait;
252
253 /* pin only walk, we record which extents on disk belong to the
254 * log trees
255 */
256 int pin;
257
258 /* what stage of the replay code we're currently in */
259 int stage;
260
261 /* the root we are currently replaying */
262 struct btrfs_root *replay_dest;
263
264 /* the trans handle for the current replay */
265 struct btrfs_trans_handle *trans;
266
267 /* the function that gets used to process blocks we find in the
268 * tree. Note the extent_buffer might not be up to date when it is
269 * passed in, and it must be checked or read if you need the data
270 * inside it
271 */
272 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800273 struct walk_control *wc, u64 gen, int level);
Chris Masone02119d2008-09-05 16:13:11 -0400274};
275
276/*
277 * process_func used to pin down extents, write them or wait on them
278 */
279static int process_one_buffer(struct btrfs_root *log,
280 struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800281 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -0400282{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400283 struct btrfs_fs_info *fs_info = log->fs_info;
Josef Bacikb50c6e22013-04-25 15:55:30 -0400284 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400285
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400286 /*
287 * If this fs is mixed then we need to be able to process the leaves to
288 * pin down any logged extents, so we have to read the block.
289 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Qu Wenruo581c1762018-03-29 09:08:11 +0800291 ret = btrfs_read_buffer(eb, gen, level, NULL);
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400292 if (ret)
293 return ret;
294 }
295
Josef Bacikb50c6e22013-04-25 15:55:30 -0400296 if (wc->pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400297 ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
298 eb->len);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400299
300 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400301 if (wc->pin && btrfs_header_level(eb) == 0)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400302 ret = btrfs_exclude_logged_extents(fs_info, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400303 if (wc->write)
304 btrfs_write_tree_block(eb);
305 if (wc->wait)
306 btrfs_wait_tree_block_writeback(eb);
307 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400308 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400309}
310
311/*
312 * Item overwrite used by replay and tree logging. eb, slot and key all refer
313 * to the src data we are copying out.
314 *
315 * root is the tree we are copying into, and path is a scratch
316 * path for use in this function (it should be released on entry and
317 * will be released on exit).
318 *
319 * If the key is already in the destination tree the existing item is
320 * overwritten. If the existing item isn't big enough, it is extended.
321 * If it is too large, it is truncated.
322 *
323 * If the key isn't in the destination yet, a new item is inserted.
324 */
325static noinline int overwrite_item(struct btrfs_trans_handle *trans,
326 struct btrfs_root *root,
327 struct btrfs_path *path,
328 struct extent_buffer *eb, int slot,
329 struct btrfs_key *key)
330{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400331 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400332 int ret;
333 u32 item_size;
334 u64 saved_i_size = 0;
335 int save_old_i_size = 0;
336 unsigned long src_ptr;
337 unsigned long dst_ptr;
338 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000339 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400340
341 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
342 overwrite_root = 1;
343
344 item_size = btrfs_item_size_nr(eb, slot);
345 src_ptr = btrfs_item_ptr_offset(eb, slot);
346
347 /* look for the key in the destination tree */
348 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000349 if (ret < 0)
350 return ret;
351
Chris Masone02119d2008-09-05 16:13:11 -0400352 if (ret == 0) {
353 char *src_copy;
354 char *dst_copy;
355 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
356 path->slots[0]);
357 if (dst_size != item_size)
358 goto insert;
359
360 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200361 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400362 return 0;
363 }
364 dst_copy = kmalloc(item_size, GFP_NOFS);
365 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000366 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200367 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000368 kfree(dst_copy);
369 kfree(src_copy);
370 return -ENOMEM;
371 }
Chris Masone02119d2008-09-05 16:13:11 -0400372
373 read_extent_buffer(eb, src_copy, src_ptr, item_size);
374
375 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
376 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
377 item_size);
378 ret = memcmp(dst_copy, src_copy, item_size);
379
380 kfree(dst_copy);
381 kfree(src_copy);
382 /*
383 * they have the same contents, just return, this saves
384 * us from cowing blocks in the destination tree and doing
385 * extra writes that may not have been done by a previous
386 * sync
387 */
388 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200389 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400390 return 0;
391 }
392
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000393 /*
394 * We need to load the old nbytes into the inode so when we
395 * replay the extents we've logged we get the right nbytes.
396 */
397 if (inode_item) {
398 struct btrfs_inode_item *item;
399 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400400 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000401
402 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
403 struct btrfs_inode_item);
404 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
405 item = btrfs_item_ptr(eb, slot,
406 struct btrfs_inode_item);
407 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400408
409 /*
410 * If this is a directory we need to reset the i_size to
411 * 0 so that we can set it up properly when replaying
412 * the rest of the items in this log.
413 */
414 mode = btrfs_inode_mode(eb, item);
415 if (S_ISDIR(mode))
416 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000417 }
418 } else if (inode_item) {
419 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400420 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000421
422 /*
423 * New inode, set nbytes to 0 so that the nbytes comes out
424 * properly when we replay the extents.
425 */
426 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
427 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400428
429 /*
430 * If this is a directory we need to reset the i_size to 0 so
431 * that we can set it up properly when replaying the rest of
432 * the items in this log.
433 */
434 mode = btrfs_inode_mode(eb, item);
435 if (S_ISDIR(mode))
436 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400437 }
438insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200439 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400440 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000441 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400442 ret = btrfs_insert_empty_item(trans, root, path,
443 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000444 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400445
446 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000447 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400448 u32 found_size;
449 found_size = btrfs_item_size_nr(path->nodes[0],
450 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100451 if (found_size > item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400452 btrfs_truncate_item(fs_info, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100453 else if (found_size < item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400454 btrfs_extend_item(fs_info, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100455 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400456 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400457 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400458 }
459 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
460 path->slots[0]);
461
462 /* don't overwrite an existing inode if the generation number
463 * was logged as zero. This is done when the tree logging code
464 * is just logging an inode to make sure it exists after recovery.
465 *
466 * Also, don't overwrite i_size on directories during replay.
467 * log replay inserts and removes directory items based on the
468 * state of the tree found in the subvolume, and i_size is modified
469 * as it goes
470 */
471 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
472 struct btrfs_inode_item *src_item;
473 struct btrfs_inode_item *dst_item;
474
475 src_item = (struct btrfs_inode_item *)src_ptr;
476 dst_item = (struct btrfs_inode_item *)dst_ptr;
477
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000478 if (btrfs_inode_generation(eb, src_item) == 0) {
479 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000480 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000481
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000482 /*
483 * For regular files an ino_size == 0 is used only when
484 * logging that an inode exists, as part of a directory
485 * fsync, and the inode wasn't fsynced before. In this
486 * case don't set the size of the inode in the fs/subvol
487 * tree, otherwise we would be throwing valid data away.
488 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000489 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000490 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
491 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000492 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000493
494 btrfs_init_map_token(&token);
495 btrfs_set_token_inode_size(dst_eb, dst_item,
496 ino_size, &token);
497 }
Chris Masone02119d2008-09-05 16:13:11 -0400498 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000499 }
Chris Masone02119d2008-09-05 16:13:11 -0400500
501 if (overwrite_root &&
502 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
503 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
504 save_old_i_size = 1;
505 saved_i_size = btrfs_inode_size(path->nodes[0],
506 dst_item);
507 }
508 }
509
510 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
511 src_ptr, item_size);
512
513 if (save_old_i_size) {
514 struct btrfs_inode_item *dst_item;
515 dst_item = (struct btrfs_inode_item *)dst_ptr;
516 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
517 }
518
519 /* make sure the generation is filled in */
520 if (key->type == BTRFS_INODE_ITEM_KEY) {
521 struct btrfs_inode_item *dst_item;
522 dst_item = (struct btrfs_inode_item *)dst_ptr;
523 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
524 btrfs_set_inode_generation(path->nodes[0], dst_item,
525 trans->transid);
526 }
527 }
528no_copy:
529 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200530 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400531 return 0;
532}
533
534/*
535 * simple helper to read an inode off the disk from a given root
536 * This can only be called for subvolume roots and not for the log
537 */
538static noinline struct inode *read_one_inode(struct btrfs_root *root,
539 u64 objectid)
540{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400541 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400542 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400543
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400544 key.objectid = objectid;
545 key.type = BTRFS_INODE_ITEM_KEY;
546 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000547 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Al Viro2e19f1f2018-07-29 23:04:45 +0100548 if (IS_ERR(inode))
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400549 inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -0400550 return inode;
551}
552
553/* replays a single extent in 'eb' at 'slot' with 'key' into the
554 * subvolume 'root'. path is released on entry and should be released
555 * on exit.
556 *
557 * extents in the log tree have not been allocated out of the extent
558 * tree yet. So, this completes the allocation, taking a reference
559 * as required if the extent already exists or creating a new extent
560 * if it isn't in the extent allocation tree yet.
561 *
562 * The extent is inserted into the file, dropping any existing extents
563 * from the file that overlap the new one.
564 */
565static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
566 struct btrfs_root *root,
567 struct btrfs_path *path,
568 struct extent_buffer *eb, int slot,
569 struct btrfs_key *key)
570{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400571 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400572 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400573 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400574 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000575 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400576 struct btrfs_file_extent_item *item;
577 struct inode *inode = NULL;
578 unsigned long size;
579 int ret = 0;
580
581 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
582 found_type = btrfs_file_extent_type(eb, item);
583
Yan Zhengd899e052008-10-30 14:25:28 -0400584 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000585 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
586 nbytes = btrfs_file_extent_num_bytes(eb, item);
587 extent_end = start + nbytes;
588
589 /*
590 * We don't add to the inodes nbytes if we are prealloc or a
591 * hole.
592 */
593 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
594 nbytes = 0;
595 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +0800596 size = btrfs_file_extent_ram_bytes(eb, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000597 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400598 extent_end = ALIGN(start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400599 fs_info->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400600 } else {
601 ret = 0;
602 goto out;
603 }
604
605 inode = read_one_inode(root, key->objectid);
606 if (!inode) {
607 ret = -EIO;
608 goto out;
609 }
610
611 /*
612 * first check to see if we already have this extent in the
613 * file. This must be done before the btrfs_drop_extents run
614 * so we don't try to drop this extent.
615 */
David Sterbaf85b7372017-01-20 14:54:07 +0100616 ret = btrfs_lookup_file_extent(trans, root, path,
617 btrfs_ino(BTRFS_I(inode)), start, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400618
Yan Zhengd899e052008-10-30 14:25:28 -0400619 if (ret == 0 &&
620 (found_type == BTRFS_FILE_EXTENT_REG ||
621 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400622 struct btrfs_file_extent_item cmp1;
623 struct btrfs_file_extent_item cmp2;
624 struct btrfs_file_extent_item *existing;
625 struct extent_buffer *leaf;
626
627 leaf = path->nodes[0];
628 existing = btrfs_item_ptr(leaf, path->slots[0],
629 struct btrfs_file_extent_item);
630
631 read_extent_buffer(eb, &cmp1, (unsigned long)item,
632 sizeof(cmp1));
633 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
634 sizeof(cmp2));
635
636 /*
637 * we already have a pointer to this exact extent,
638 * we don't have to do anything
639 */
640 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200641 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400642 goto out;
643 }
644 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200645 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400646
647 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400648 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400649 if (ret)
650 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400651
Yan Zheng07d400a2009-01-06 11:42:00 -0500652 if (found_type == BTRFS_FILE_EXTENT_REG ||
653 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400654 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500655 unsigned long dest_offset;
656 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400657
Filipe Manana3168021c2017-02-01 14:58:02 +0000658 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
659 btrfs_fs_incompat(fs_info, NO_HOLES))
660 goto update_inode;
661
Yan Zheng07d400a2009-01-06 11:42:00 -0500662 ret = btrfs_insert_empty_item(trans, root, path, key,
663 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400664 if (ret)
665 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500666 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
667 path->slots[0]);
668 copy_extent_buffer(path->nodes[0], eb, dest_offset,
669 (unsigned long)item, sizeof(*item));
670
671 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
672 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
673 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400674 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500675
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800676 /*
677 * Manually record dirty extent, as here we did a shallow
678 * file extent item copy and skip normal backref update,
679 * but modifying extent tree all by ourselves.
680 * So need to manually record dirty extent for qgroup,
681 * as the owner of the file extent changed from log tree
682 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
683 */
Lu Fengqia95f3aa2018-07-18 16:28:03 +0800684 ret = btrfs_qgroup_trace_extent(trans,
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800685 btrfs_file_extent_disk_bytenr(eb, item),
686 btrfs_file_extent_disk_num_bytes(eb, item),
687 GFP_NOFS);
688 if (ret < 0)
689 goto out;
690
Yan Zheng07d400a2009-01-06 11:42:00 -0500691 if (ins.objectid > 0) {
692 u64 csum_start;
693 u64 csum_end;
694 LIST_HEAD(ordered_sums);
695 /*
696 * is this extent already allocated in the extent
697 * allocation tree? If so, just add a reference
698 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400699 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500700 ins.offset);
701 if (ret == 0) {
Josef Bacik84f7d8e2017-09-29 15:43:49 -0400702 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng07d400a2009-01-06 11:42:00 -0500703 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400704 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100705 key->objectid, offset);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400706 if (ret)
707 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500708 } else {
709 /*
710 * insert the extent pointer in the extent
711 * allocation tree
712 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400713 ret = btrfs_alloc_logged_file_extent(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400714 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400715 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400716 if (ret)
717 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500718 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200719 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500720
721 if (btrfs_file_extent_compression(eb, item)) {
722 csum_start = ins.objectid;
723 csum_end = csum_start + ins.offset;
724 } else {
725 csum_start = ins.objectid +
726 btrfs_file_extent_offset(eb, item);
727 csum_end = csum_start +
728 btrfs_file_extent_num_bytes(eb, item);
729 }
730
731 ret = btrfs_lookup_csums_range(root->log_root,
732 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100733 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400734 if (ret)
735 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100736 /*
737 * Now delete all existing cums in the csum root that
738 * cover our range. We do this because we can have an
739 * extent that is completely referenced by one file
740 * extent item and partially referenced by another
741 * file extent item (like after using the clone or
742 * extent_same ioctls). In this case if we end up doing
743 * the replay of the one that partially references the
744 * extent first, and we do not do the csum deletion
745 * below, we can get 2 csum items in the csum tree that
746 * overlap each other. For example, imagine our log has
747 * the two following file extent items:
748 *
749 * key (257 EXTENT_DATA 409600)
750 * extent data disk byte 12845056 nr 102400
751 * extent data offset 20480 nr 20480 ram 102400
752 *
753 * key (257 EXTENT_DATA 819200)
754 * extent data disk byte 12845056 nr 102400
755 * extent data offset 0 nr 102400 ram 102400
756 *
757 * Where the second one fully references the 100K extent
758 * that starts at disk byte 12845056, and the log tree
759 * has a single csum item that covers the entire range
760 * of the extent:
761 *
762 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
763 *
764 * After the first file extent item is replayed, the
765 * csum tree gets the following csum item:
766 *
767 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
768 *
769 * Which covers the 20K sub-range starting at offset 20K
770 * of our extent. Now when we replay the second file
771 * extent item, if we do not delete existing csum items
772 * that cover any of its blocks, we end up getting two
773 * csum items in our csum tree that overlap each other:
774 *
775 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
776 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
777 *
778 * Which is a problem, because after this anyone trying
779 * to lookup up for the checksum of any block of our
780 * extent starting at an offset of 40K or higher, will
781 * end up looking at the second csum item only, which
782 * does not contain the checksum for any block starting
783 * at offset 40K or higher of our extent.
784 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500785 while (!list_empty(&ordered_sums)) {
786 struct btrfs_ordered_sum *sums;
787 sums = list_entry(ordered_sums.next,
788 struct btrfs_ordered_sum,
789 list);
Josef Bacik36508602013-04-25 16:23:32 -0400790 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400791 ret = btrfs_del_csums(trans, fs_info,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400792 sums->bytenr,
793 sums->len);
Filipe Mananab84b8392015-08-19 11:09:40 +0100794 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400795 ret = btrfs_csum_file_blocks(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400796 fs_info->csum_root, sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500797 list_del(&sums->list);
798 kfree(sums);
799 }
Josef Bacik36508602013-04-25 16:23:32 -0400800 if (ret)
801 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500802 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200803 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500804 }
805 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
806 /* inline extents are easy, we just overwrite them */
807 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400808 if (ret)
809 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500810 }
811
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000812 inode_add_bytes(inode, nbytes);
Filipe Manana3168021c2017-02-01 14:58:02 +0000813update_inode:
Tsutomu Itohb9959292012-06-25 21:25:22 -0600814 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400815out:
816 if (inode)
817 iput(inode);
818 return ret;
819}
820
821/*
822 * when cleaning up conflicts between the directory names in the
823 * subvolume, directory names in the log and directory names in the
824 * inode back references, we may have to unlink inodes from directories.
825 *
826 * This is a helper function to do the unlink of a specific directory
827 * item
828 */
829static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
830 struct btrfs_root *root,
831 struct btrfs_path *path,
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200832 struct btrfs_inode *dir,
Chris Masone02119d2008-09-05 16:13:11 -0400833 struct btrfs_dir_item *di)
834{
835 struct inode *inode;
836 char *name;
837 int name_len;
838 struct extent_buffer *leaf;
839 struct btrfs_key location;
840 int ret;
841
842 leaf = path->nodes[0];
843
844 btrfs_dir_item_key_to_cpu(leaf, di, &location);
845 name_len = btrfs_dir_name_len(leaf, di);
846 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000847 if (!name)
848 return -ENOMEM;
849
Chris Masone02119d2008-09-05 16:13:11 -0400850 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200851 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400852
853 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000854 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400855 ret = -EIO;
856 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000857 }
Chris Masone02119d2008-09-05 16:13:11 -0400858
Yan Zhengec051c02009-01-05 15:43:42 -0500859 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400860 if (ret)
861 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400862
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200863 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
864 name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400865 if (ret)
866 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100867 else
Nikolay Borisove5c304e62018-02-07 17:55:43 +0200868 ret = btrfs_run_delayed_items(trans);
Josef Bacik36508602013-04-25 16:23:32 -0400869out:
870 kfree(name);
871 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400872 return ret;
873}
874
875/*
876 * helper function to see if a given name and sequence number found
877 * in an inode back reference are already in a directory and correctly
878 * point to this inode
879 */
880static noinline int inode_in_dir(struct btrfs_root *root,
881 struct btrfs_path *path,
882 u64 dirid, u64 objectid, u64 index,
883 const char *name, int name_len)
884{
885 struct btrfs_dir_item *di;
886 struct btrfs_key location;
887 int match = 0;
888
889 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
890 index, name, name_len, 0);
891 if (di && !IS_ERR(di)) {
892 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
893 if (location.objectid != objectid)
894 goto out;
895 } else
896 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200897 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400898
899 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
900 if (di && !IS_ERR(di)) {
901 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
902 if (location.objectid != objectid)
903 goto out;
904 } else
905 goto out;
906 match = 1;
907out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200908 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400909 return match;
910}
911
912/*
913 * helper function to check a log tree for a named back reference in
914 * an inode. This is used to decide if a back reference that is
915 * found in the subvolume conflicts with what we find in the log.
916 *
917 * inode backreferences may have multiple refs in a single item,
918 * during replay we process one reference at a time, and we don't
919 * want to delete valid links to a file from the subvolume if that
920 * link is also in the log.
921 */
922static noinline int backref_in_log(struct btrfs_root *log,
923 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700924 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000925 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400926{
927 struct btrfs_path *path;
928 struct btrfs_inode_ref *ref;
929 unsigned long ptr;
930 unsigned long ptr_end;
931 unsigned long name_ptr;
932 int found_name_len;
933 int item_size;
934 int ret;
935 int match = 0;
936
937 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000938 if (!path)
939 return -ENOMEM;
940
Chris Masone02119d2008-09-05 16:13:11 -0400941 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
942 if (ret != 0)
943 goto out;
944
Chris Masone02119d2008-09-05 16:13:11 -0400945 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700946
947 if (key->type == BTRFS_INODE_EXTREF_KEY) {
Filipe Manana1f250e92018-02-28 15:56:10 +0000948 if (btrfs_find_name_in_ext_backref(path->nodes[0],
949 path->slots[0],
950 ref_objectid,
Mark Fashehf1863732012-08-08 11:32:27 -0700951 name, namelen, NULL))
952 match = 1;
953
954 goto out;
955 }
956
957 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400958 ptr_end = ptr + item_size;
959 while (ptr < ptr_end) {
960 ref = (struct btrfs_inode_ref *)ptr;
961 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
962 if (found_name_len == namelen) {
963 name_ptr = (unsigned long)(ref + 1);
964 ret = memcmp_extent_buffer(path->nodes[0], name,
965 name_ptr, namelen);
966 if (ret == 0) {
967 match = 1;
968 goto out;
969 }
970 }
971 ptr = (unsigned long)(ref + 1) + found_name_len;
972 }
973out:
974 btrfs_free_path(path);
975 return match;
976}
977
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700978static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
979 struct btrfs_root *root,
980 struct btrfs_path *path,
981 struct btrfs_root *log_root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +0200982 struct btrfs_inode *dir,
983 struct btrfs_inode *inode,
Mark Fashehf1863732012-08-08 11:32:27 -0700984 u64 inode_objectid, u64 parent_objectid,
985 u64 ref_index, char *name, int namelen,
986 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700987{
988 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700989 char *victim_name;
990 int victim_name_len;
991 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700992 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700993 struct btrfs_key search_key;
994 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700995
Mark Fashehf1863732012-08-08 11:32:27 -0700996again:
997 /* Search old style refs */
998 search_key.objectid = inode_objectid;
999 search_key.type = BTRFS_INODE_REF_KEY;
1000 search_key.offset = parent_objectid;
1001 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001002 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001003 struct btrfs_inode_ref *victim_ref;
1004 unsigned long ptr;
1005 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001006
1007 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001008
1009 /* are we trying to overwrite a back ref for the root directory
1010 * if so, just jump out, we're done
1011 */
Mark Fashehf1863732012-08-08 11:32:27 -07001012 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001013 return 1;
1014
1015 /* check all the names in this back reference to see
1016 * if they are in the log. if so, we allow them to stay
1017 * otherwise they must be unlinked as a conflict
1018 */
1019 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1020 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1021 while (ptr < ptr_end) {
1022 victim_ref = (struct btrfs_inode_ref *)ptr;
1023 victim_name_len = btrfs_inode_ref_name_len(leaf,
1024 victim_ref);
1025 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001026 if (!victim_name)
1027 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001028
1029 read_extent_buffer(leaf, victim_name,
1030 (unsigned long)(victim_ref + 1),
1031 victim_name_len);
1032
Mark Fashehf1863732012-08-08 11:32:27 -07001033 if (!backref_in_log(log_root, &search_key,
1034 parent_objectid,
1035 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001036 victim_name_len)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001037 inc_nlink(&inode->vfs_inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001038 btrfs_release_path(path);
1039
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001040 ret = btrfs_unlink_inode(trans, root, dir, inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001041 victim_name, victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001042 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001043 if (ret)
1044 return ret;
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001045 ret = btrfs_run_delayed_items(trans);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001046 if (ret)
1047 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001048 *search_done = 1;
1049 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001050 }
1051 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001052
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001053 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1054 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001055
1056 /*
1057 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001058 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001059 */
1060 *search_done = 1;
1061 }
1062 btrfs_release_path(path);
1063
Mark Fashehf1863732012-08-08 11:32:27 -07001064 /* Same search but for extended refs */
1065 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1066 inode_objectid, parent_objectid, 0,
1067 0);
1068 if (!IS_ERR_OR_NULL(extref)) {
1069 u32 item_size;
1070 u32 cur_offset = 0;
1071 unsigned long base;
1072 struct inode *victim_parent;
1073
1074 leaf = path->nodes[0];
1075
1076 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1077 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1078
1079 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001080 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001081
1082 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1083
1084 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1085 goto next;
1086
1087 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001088 if (!victim_name)
1089 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001090 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1091 victim_name_len);
1092
1093 search_key.objectid = inode_objectid;
1094 search_key.type = BTRFS_INODE_EXTREF_KEY;
1095 search_key.offset = btrfs_extref_hash(parent_objectid,
1096 victim_name,
1097 victim_name_len);
1098 ret = 0;
1099 if (!backref_in_log(log_root, &search_key,
1100 parent_objectid, victim_name,
1101 victim_name_len)) {
1102 ret = -ENOENT;
1103 victim_parent = read_one_inode(root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001104 parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001105 if (victim_parent) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001106 inc_nlink(&inode->vfs_inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001107 btrfs_release_path(path);
1108
1109 ret = btrfs_unlink_inode(trans, root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001110 BTRFS_I(victim_parent),
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001111 inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001112 victim_name,
1113 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001114 if (!ret)
1115 ret = btrfs_run_delayed_items(
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001116 trans);
Mark Fashehf1863732012-08-08 11:32:27 -07001117 }
Mark Fashehf1863732012-08-08 11:32:27 -07001118 iput(victim_parent);
1119 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001120 if (ret)
1121 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001122 *search_done = 1;
1123 goto again;
1124 }
1125 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001126next:
1127 cur_offset += victim_name_len + sizeof(*extref);
1128 }
1129 *search_done = 1;
1130 }
1131 btrfs_release_path(path);
1132
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001133 /* look for a conflicting sequence number */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001134 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001135 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001136 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001137 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001138 if (ret)
1139 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001140 }
1141 btrfs_release_path(path);
1142
1143 /* look for a conflicing name */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001144 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001145 name, namelen, 0);
1146 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001147 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001148 if (ret)
1149 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001150 }
1151 btrfs_release_path(path);
1152
1153 return 0;
1154}
Chris Masone02119d2008-09-05 16:13:11 -04001155
Qu Wenruobae15d92017-11-08 08:54:26 +08001156static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1157 u32 *namelen, char **name, u64 *index,
1158 u64 *parent_objectid)
Mark Fashehf1863732012-08-08 11:32:27 -07001159{
1160 struct btrfs_inode_extref *extref;
1161
1162 extref = (struct btrfs_inode_extref *)ref_ptr;
1163
1164 *namelen = btrfs_inode_extref_name_len(eb, extref);
1165 *name = kmalloc(*namelen, GFP_NOFS);
1166 if (*name == NULL)
1167 return -ENOMEM;
1168
1169 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1170 *namelen);
1171
Filipe Manana1f250e92018-02-28 15:56:10 +00001172 if (index)
1173 *index = btrfs_inode_extref_index(eb, extref);
Mark Fashehf1863732012-08-08 11:32:27 -07001174 if (parent_objectid)
1175 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1176
1177 return 0;
1178}
1179
Qu Wenruobae15d92017-11-08 08:54:26 +08001180static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1181 u32 *namelen, char **name, u64 *index)
Mark Fashehf1863732012-08-08 11:32:27 -07001182{
1183 struct btrfs_inode_ref *ref;
1184
1185 ref = (struct btrfs_inode_ref *)ref_ptr;
1186
1187 *namelen = btrfs_inode_ref_name_len(eb, ref);
1188 *name = kmalloc(*namelen, GFP_NOFS);
1189 if (*name == NULL)
1190 return -ENOMEM;
1191
1192 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1193
Filipe Manana1f250e92018-02-28 15:56:10 +00001194 if (index)
1195 *index = btrfs_inode_ref_index(eb, ref);
Mark Fashehf1863732012-08-08 11:32:27 -07001196
1197 return 0;
1198}
1199
Chris Masone02119d2008-09-05 16:13:11 -04001200/*
Filipe Manana1f250e92018-02-28 15:56:10 +00001201 * Take an inode reference item from the log tree and iterate all names from the
1202 * inode reference item in the subvolume tree with the same key (if it exists).
1203 * For any name that is not in the inode reference item from the log tree, do a
1204 * proper unlink of that name (that is, remove its entry from the inode
1205 * reference item and both dir index keys).
1206 */
1207static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1208 struct btrfs_root *root,
1209 struct btrfs_path *path,
1210 struct btrfs_inode *inode,
1211 struct extent_buffer *log_eb,
1212 int log_slot,
1213 struct btrfs_key *key)
1214{
1215 int ret;
1216 unsigned long ref_ptr;
1217 unsigned long ref_end;
1218 struct extent_buffer *eb;
1219
1220again:
1221 btrfs_release_path(path);
1222 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1223 if (ret > 0) {
1224 ret = 0;
1225 goto out;
1226 }
1227 if (ret < 0)
1228 goto out;
1229
1230 eb = path->nodes[0];
1231 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1232 ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1233 while (ref_ptr < ref_end) {
1234 char *name = NULL;
1235 int namelen;
1236 u64 parent_id;
1237
1238 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1239 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1240 NULL, &parent_id);
1241 } else {
1242 parent_id = key->offset;
1243 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1244 NULL);
1245 }
1246 if (ret)
1247 goto out;
1248
1249 if (key->type == BTRFS_INODE_EXTREF_KEY)
1250 ret = btrfs_find_name_in_ext_backref(log_eb, log_slot,
1251 parent_id, name,
1252 namelen, NULL);
1253 else
1254 ret = btrfs_find_name_in_backref(log_eb, log_slot, name,
1255 namelen, NULL);
1256
1257 if (!ret) {
1258 struct inode *dir;
1259
1260 btrfs_release_path(path);
1261 dir = read_one_inode(root, parent_id);
1262 if (!dir) {
1263 ret = -ENOENT;
1264 kfree(name);
1265 goto out;
1266 }
1267 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
1268 inode, name, namelen);
1269 kfree(name);
1270 iput(dir);
1271 if (ret)
1272 goto out;
1273 goto again;
1274 }
1275
1276 kfree(name);
1277 ref_ptr += namelen;
1278 if (key->type == BTRFS_INODE_EXTREF_KEY)
1279 ref_ptr += sizeof(struct btrfs_inode_extref);
1280 else
1281 ref_ptr += sizeof(struct btrfs_inode_ref);
1282 }
1283 ret = 0;
1284 out:
1285 btrfs_release_path(path);
1286 return ret;
1287}
1288
Filipe Manana0d836392018-07-20 10:59:06 +01001289static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
1290 const u8 ref_type, const char *name,
1291 const int namelen)
1292{
1293 struct btrfs_key key;
1294 struct btrfs_path *path;
1295 const u64 parent_id = btrfs_ino(BTRFS_I(dir));
1296 int ret;
1297
1298 path = btrfs_alloc_path();
1299 if (!path)
1300 return -ENOMEM;
1301
1302 key.objectid = btrfs_ino(BTRFS_I(inode));
1303 key.type = ref_type;
1304 if (key.type == BTRFS_INODE_REF_KEY)
1305 key.offset = parent_id;
1306 else
1307 key.offset = btrfs_extref_hash(parent_id, name, namelen);
1308
1309 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
1310 if (ret < 0)
1311 goto out;
1312 if (ret > 0) {
1313 ret = 0;
1314 goto out;
1315 }
1316 if (key.type == BTRFS_INODE_EXTREF_KEY)
1317 ret = btrfs_find_name_in_ext_backref(path->nodes[0],
1318 path->slots[0], parent_id,
1319 name, namelen, NULL);
1320 else
1321 ret = btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
1322 name, namelen, NULL);
1323
1324out:
1325 btrfs_free_path(path);
1326 return ret;
1327}
1328
Filipe Manana1f250e92018-02-28 15:56:10 +00001329/*
Chris Masone02119d2008-09-05 16:13:11 -04001330 * replay one inode back reference item found in the log tree.
1331 * eb, slot and key refer to the buffer and key found in the log tree.
1332 * root is the destination we are replaying into, and path is for temp
1333 * use by this function. (it should be released on return).
1334 */
1335static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1336 struct btrfs_root *root,
1337 struct btrfs_root *log,
1338 struct btrfs_path *path,
1339 struct extent_buffer *eb, int slot,
1340 struct btrfs_key *key)
1341{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001342 struct inode *dir = NULL;
1343 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001344 unsigned long ref_ptr;
1345 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001346 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001347 int namelen;
1348 int ret;
liuboc622ae62011-03-26 08:01:12 -04001349 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001350 int log_ref_ver = 0;
1351 u64 parent_objectid;
1352 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001353 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001354 int ref_struct_size;
1355
1356 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1357 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1358
1359 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1360 struct btrfs_inode_extref *r;
1361
1362 ref_struct_size = sizeof(struct btrfs_inode_extref);
1363 log_ref_ver = 1;
1364 r = (struct btrfs_inode_extref *)ref_ptr;
1365 parent_objectid = btrfs_inode_extref_parent(eb, r);
1366 } else {
1367 ref_struct_size = sizeof(struct btrfs_inode_ref);
1368 parent_objectid = key->offset;
1369 }
1370 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001371
Chris Masone02119d2008-09-05 16:13:11 -04001372 /*
1373 * it is possible that we didn't log all the parent directories
1374 * for a given inode. If we don't find the dir, just don't
1375 * copy the back ref in. The link count fixup code will take
1376 * care of the rest
1377 */
Mark Fashehf1863732012-08-08 11:32:27 -07001378 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001379 if (!dir) {
1380 ret = -ENOENT;
1381 goto out;
1382 }
Chris Masone02119d2008-09-05 16:13:11 -04001383
Mark Fashehf1863732012-08-08 11:32:27 -07001384 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001385 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001386 ret = -EIO;
1387 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001388 }
Chris Masone02119d2008-09-05 16:13:11 -04001389
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001390 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001391 if (log_ref_ver) {
Qu Wenruobae15d92017-11-08 08:54:26 +08001392 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1393 &ref_index, &parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001394 /*
1395 * parent object can change from one array
1396 * item to another.
1397 */
1398 if (!dir)
1399 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001400 if (!dir) {
1401 ret = -ENOENT;
1402 goto out;
1403 }
Mark Fashehf1863732012-08-08 11:32:27 -07001404 } else {
Qu Wenruobae15d92017-11-08 08:54:26 +08001405 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1406 &ref_index);
Mark Fashehf1863732012-08-08 11:32:27 -07001407 }
1408 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001409 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001410
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001411 /* if we already have a perfect match, we're done */
David Sterbaf85b7372017-01-20 14:54:07 +01001412 if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1413 btrfs_ino(BTRFS_I(inode)), ref_index,
1414 name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001415 /*
1416 * look for a conflicting back reference in the
1417 * metadata. if we find one we have to unlink that name
1418 * of the file before we add our new link. Later on, we
1419 * overwrite any existing back reference, and we don't
1420 * want to create dangling pointers in the directory.
1421 */
Chris Masone02119d2008-09-05 16:13:11 -04001422
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001423 if (!search_done) {
1424 ret = __add_inode_ref(trans, root, path, log,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001425 BTRFS_I(dir),
David Sterbad75eefd2017-02-10 20:20:19 +01001426 BTRFS_I(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001427 inode_objectid,
1428 parent_objectid,
1429 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001430 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001431 if (ret) {
1432 if (ret == 1)
1433 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001434 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001435 }
Chris Masone02119d2008-09-05 16:13:11 -04001436 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001437
Filipe Manana0d836392018-07-20 10:59:06 +01001438 /*
1439 * If a reference item already exists for this inode
1440 * with the same parent and name, but different index,
1441 * drop it and the corresponding directory index entries
1442 * from the parent before adding the new reference item
1443 * and dir index entries, otherwise we would fail with
1444 * -EEXIST returned from btrfs_add_link() below.
1445 */
1446 ret = btrfs_inode_ref_exists(inode, dir, key->type,
1447 name, namelen);
1448 if (ret > 0) {
1449 ret = btrfs_unlink_inode(trans, root,
1450 BTRFS_I(dir),
1451 BTRFS_I(inode),
1452 name, namelen);
1453 /*
1454 * If we dropped the link count to 0, bump it so
1455 * that later the iput() on the inode will not
1456 * free it. We will fixup the link count later.
1457 */
1458 if (!ret && inode->i_nlink == 0)
1459 inc_nlink(inode);
1460 }
1461 if (ret < 0)
1462 goto out;
1463
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001464 /* insert our name */
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001465 ret = btrfs_add_link(trans, BTRFS_I(dir),
1466 BTRFS_I(inode),
1467 name, namelen, 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001468 if (ret)
1469 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001470
1471 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001472 }
liuboc622ae62011-03-26 08:01:12 -04001473
Mark Fashehf1863732012-08-08 11:32:27 -07001474 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001475 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001476 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001477 if (log_ref_ver) {
1478 iput(dir);
1479 dir = NULL;
1480 }
Chris Masone02119d2008-09-05 16:13:11 -04001481 }
Chris Masone02119d2008-09-05 16:13:11 -04001482
Filipe Manana1f250e92018-02-28 15:56:10 +00001483 /*
1484 * Before we overwrite the inode reference item in the subvolume tree
1485 * with the item from the log tree, we must unlink all names from the
1486 * parent directory that are in the subvolume's tree inode reference
1487 * item, otherwise we end up with an inconsistent subvolume tree where
1488 * dir index entries exist for a name but there is no inode reference
1489 * item with the same name.
1490 */
1491 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1492 key);
1493 if (ret)
1494 goto out;
1495
Chris Masone02119d2008-09-05 16:13:11 -04001496 /* finally write the back reference in the inode */
1497 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001498out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001499 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001500 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001501 iput(dir);
1502 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001503 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001504}
1505
Yan, Zhengc71bf092009-11-12 09:34:40 +00001506static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001507 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001508{
1509 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001510
David Sterba9c4f61f2015-01-02 19:12:57 +01001511 ret = btrfs_insert_orphan_item(trans, root, ino);
1512 if (ret == -EEXIST)
1513 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001514
Yan, Zhengc71bf092009-11-12 09:34:40 +00001515 return ret;
1516}
1517
Mark Fashehf1863732012-08-08 11:32:27 -07001518static int count_inode_extrefs(struct btrfs_root *root,
Nikolay Borisov36283652017-01-18 00:31:49 +02001519 struct btrfs_inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001520{
Mark Fashehf1863732012-08-08 11:32:27 -07001521 int ret = 0;
1522 int name_len;
1523 unsigned int nlink = 0;
1524 u32 item_size;
1525 u32 cur_offset = 0;
Nikolay Borisov36283652017-01-18 00:31:49 +02001526 u64 inode_objectid = btrfs_ino(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001527 u64 offset = 0;
1528 unsigned long ptr;
1529 struct btrfs_inode_extref *extref;
1530 struct extent_buffer *leaf;
1531
1532 while (1) {
1533 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1534 &extref, &offset);
1535 if (ret)
1536 break;
1537
1538 leaf = path->nodes[0];
1539 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1540 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001541 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001542
1543 while (cur_offset < item_size) {
1544 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1545 name_len = btrfs_inode_extref_name_len(leaf, extref);
1546
1547 nlink++;
1548
1549 cur_offset += name_len + sizeof(*extref);
1550 }
1551
1552 offset++;
1553 btrfs_release_path(path);
1554 }
1555 btrfs_release_path(path);
1556
Filipe Manana2c2c4522015-01-13 16:40:04 +00001557 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001558 return ret;
1559 return nlink;
1560}
1561
1562static int count_inode_refs(struct btrfs_root *root,
Nikolay Borisovf329e312017-01-18 00:31:50 +02001563 struct btrfs_inode *inode, struct btrfs_path *path)
Mark Fashehf1863732012-08-08 11:32:27 -07001564{
Chris Masone02119d2008-09-05 16:13:11 -04001565 int ret;
1566 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001567 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001568 unsigned long ptr;
1569 unsigned long ptr_end;
1570 int name_len;
Nikolay Borisovf329e312017-01-18 00:31:50 +02001571 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001572
Li Zefan33345d012011-04-20 10:31:50 +08001573 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001574 key.type = BTRFS_INODE_REF_KEY;
1575 key.offset = (u64)-1;
1576
Chris Masond3977122009-01-05 21:25:51 -05001577 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001578 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1579 if (ret < 0)
1580 break;
1581 if (ret > 0) {
1582 if (path->slots[0] == 0)
1583 break;
1584 path->slots[0]--;
1585 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001586process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001587 btrfs_item_key_to_cpu(path->nodes[0], &key,
1588 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001589 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001590 key.type != BTRFS_INODE_REF_KEY)
1591 break;
1592 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1593 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1594 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001595 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001596 struct btrfs_inode_ref *ref;
1597
1598 ref = (struct btrfs_inode_ref *)ptr;
1599 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1600 ref);
1601 ptr = (unsigned long)(ref + 1) + name_len;
1602 nlink++;
1603 }
1604
1605 if (key.offset == 0)
1606 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001607 if (path->slots[0] > 0) {
1608 path->slots[0]--;
1609 goto process_slot;
1610 }
Chris Masone02119d2008-09-05 16:13:11 -04001611 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001612 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001613 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001614 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001615
1616 return nlink;
1617}
1618
1619/*
1620 * There are a few corners where the link count of the file can't
1621 * be properly maintained during replay. So, instead of adding
1622 * lots of complexity to the log code, we just scan the backrefs
1623 * for any file that has been through replay.
1624 *
1625 * The scan will update the link count on the inode to reflect the
1626 * number of back refs found. If it goes down to zero, the iput
1627 * will free the inode.
1628 */
1629static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1630 struct btrfs_root *root,
1631 struct inode *inode)
1632{
1633 struct btrfs_path *path;
1634 int ret;
1635 u64 nlink = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001636 u64 ino = btrfs_ino(BTRFS_I(inode));
Mark Fashehf1863732012-08-08 11:32:27 -07001637
1638 path = btrfs_alloc_path();
1639 if (!path)
1640 return -ENOMEM;
1641
Nikolay Borisovf329e312017-01-18 00:31:50 +02001642 ret = count_inode_refs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001643 if (ret < 0)
1644 goto out;
1645
1646 nlink = ret;
1647
Nikolay Borisov36283652017-01-18 00:31:49 +02001648 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001649 if (ret < 0)
1650 goto out;
1651
1652 nlink += ret;
1653
1654 ret = 0;
1655
Chris Masone02119d2008-09-05 16:13:11 -04001656 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001657 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001658 btrfs_update_inode(trans, root, inode);
1659 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001660 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001661
Yan, Zhengc71bf092009-11-12 09:34:40 +00001662 if (inode->i_nlink == 0) {
1663 if (S_ISDIR(inode->i_mode)) {
1664 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001665 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001666 if (ret)
1667 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001668 }
Li Zefan33345d012011-04-20 10:31:50 +08001669 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001670 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001671
Mark Fashehf1863732012-08-08 11:32:27 -07001672out:
1673 btrfs_free_path(path);
1674 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001675}
1676
1677static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1678 struct btrfs_root *root,
1679 struct btrfs_path *path)
1680{
1681 int ret;
1682 struct btrfs_key key;
1683 struct inode *inode;
1684
1685 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1686 key.type = BTRFS_ORPHAN_ITEM_KEY;
1687 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001688 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001689 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1690 if (ret < 0)
1691 break;
1692
1693 if (ret == 1) {
1694 if (path->slots[0] == 0)
1695 break;
1696 path->slots[0]--;
1697 }
1698
1699 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1700 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1701 key.type != BTRFS_ORPHAN_ITEM_KEY)
1702 break;
1703
1704 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001705 if (ret)
1706 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001707
David Sterbab3b4aa72011-04-21 01:20:15 +02001708 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001709 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001710 if (!inode)
1711 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001712
1713 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001714 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001715 if (ret)
1716 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001717
Chris Mason12fcfd22009-03-24 10:24:20 -04001718 /*
1719 * fixup on a directory may create new entries,
1720 * make sure we always look for the highset possible
1721 * offset
1722 */
1723 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001724 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001725 ret = 0;
1726out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001727 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001728 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001729}
1730
1731
1732/*
1733 * record a given inode in the fixup dir so we can check its link
1734 * count when replay is done. The link count is incremented here
1735 * so the inode won't go away until we check it
1736 */
1737static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1738 struct btrfs_root *root,
1739 struct btrfs_path *path,
1740 u64 objectid)
1741{
1742 struct btrfs_key key;
1743 int ret = 0;
1744 struct inode *inode;
1745
1746 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001747 if (!inode)
1748 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001749
1750 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001751 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001752 key.offset = objectid;
1753
1754 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1755
David Sterbab3b4aa72011-04-21 01:20:15 +02001756 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001757 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001758 if (!inode->i_nlink)
1759 set_nlink(inode, 1);
1760 else
Zach Brown8b558c52013-10-16 12:10:34 -07001761 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001762 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001763 } else if (ret == -EEXIST) {
1764 ret = 0;
1765 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001766 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001767 }
1768 iput(inode);
1769
1770 return ret;
1771}
1772
1773/*
1774 * when replaying the log for a directory, we only insert names
1775 * for inodes that actually exist. This means an fsync on a directory
1776 * does not implicitly fsync all the new files in it
1777 */
1778static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1779 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001780 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001781 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001782 struct btrfs_key *location)
1783{
1784 struct inode *inode;
1785 struct inode *dir;
1786 int ret;
1787
1788 inode = read_one_inode(root, location->objectid);
1789 if (!inode)
1790 return -ENOENT;
1791
1792 dir = read_one_inode(root, dirid);
1793 if (!dir) {
1794 iput(inode);
1795 return -EIO;
1796 }
Josef Bacikd5554382013-09-11 14:17:00 -04001797
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001798 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1799 name_len, 1, index);
Chris Masone02119d2008-09-05 16:13:11 -04001800
1801 /* FIXME, put inode into FIXUP list */
1802
1803 iput(inode);
1804 iput(dir);
1805 return ret;
1806}
1807
1808/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001809 * Return true if an inode reference exists in the log for the given name,
1810 * inode and parent inode.
1811 */
1812static bool name_in_log_ref(struct btrfs_root *log_root,
1813 const char *name, const int name_len,
1814 const u64 dirid, const u64 ino)
1815{
1816 struct btrfs_key search_key;
1817
1818 search_key.objectid = ino;
1819 search_key.type = BTRFS_INODE_REF_KEY;
1820 search_key.offset = dirid;
1821 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1822 return true;
1823
1824 search_key.type = BTRFS_INODE_EXTREF_KEY;
1825 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1826 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1827 return true;
1828
1829 return false;
1830}
1831
1832/*
Chris Masone02119d2008-09-05 16:13:11 -04001833 * take a single entry in a log directory item and replay it into
1834 * the subvolume.
1835 *
1836 * if a conflicting item exists in the subdirectory already,
1837 * the inode it points to is unlinked and put into the link count
1838 * fix up tree.
1839 *
1840 * If a name from the log points to a file or directory that does
1841 * not exist in the FS, it is skipped. fsyncs on directories
1842 * do not force down inodes inside that directory, just changes to the
1843 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001844 *
1845 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1846 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001847 */
1848static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1849 struct btrfs_root *root,
1850 struct btrfs_path *path,
1851 struct extent_buffer *eb,
1852 struct btrfs_dir_item *di,
1853 struct btrfs_key *key)
1854{
1855 char *name;
1856 int name_len;
1857 struct btrfs_dir_item *dst_di;
1858 struct btrfs_key found_key;
1859 struct btrfs_key log_key;
1860 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001861 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001862 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001863 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001864 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001865 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001866
1867 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001868 if (!dir)
1869 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001870
1871 name_len = btrfs_dir_name_len(eb, di);
1872 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001873 if (!name) {
1874 ret = -ENOMEM;
1875 goto out;
1876 }
liubo2a29edc2011-01-26 06:22:08 +00001877
Chris Masone02119d2008-09-05 16:13:11 -04001878 log_type = btrfs_dir_type(eb, di);
1879 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1880 name_len);
1881
1882 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001883 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1884 if (exists == 0)
1885 exists = 1;
1886 else
1887 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001888 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001889
Chris Masone02119d2008-09-05 16:13:11 -04001890 if (key->type == BTRFS_DIR_ITEM_KEY) {
1891 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1892 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001893 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001894 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1895 key->objectid,
1896 key->offset, name,
1897 name_len, 1);
1898 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001899 /* Corruption */
1900 ret = -EINVAL;
1901 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001902 }
David Sterbac7040052011-04-19 18:00:01 +02001903 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001904 /* we need a sequence number to insert, so we only
1905 * do inserts for the BTRFS_DIR_INDEX_KEY types
1906 */
1907 if (key->type != BTRFS_DIR_INDEX_KEY)
1908 goto out;
1909 goto insert;
1910 }
1911
1912 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1913 /* the existing item matches the logged item */
1914 if (found_key.objectid == log_key.objectid &&
1915 found_key.type == log_key.type &&
1916 found_key.offset == log_key.offset &&
1917 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001918 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001919 goto out;
1920 }
1921
1922 /*
1923 * don't drop the conflicting directory entry if the inode
1924 * for the new entry doesn't exist
1925 */
Chris Mason4bef0842008-09-08 11:18:08 -04001926 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001927 goto out;
1928
Nikolay Borisov207e7d92017-01-18 00:31:45 +02001929 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001930 if (ret)
1931 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001932
1933 if (key->type == BTRFS_DIR_INDEX_KEY)
1934 goto insert;
1935out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001936 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001937 if (!ret && update_size) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02001938 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
Josef Bacikd5554382013-09-11 14:17:00 -04001939 ret = btrfs_update_inode(trans, root, dir);
1940 }
Chris Masone02119d2008-09-05 16:13:11 -04001941 kfree(name);
1942 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001943 if (!ret && name_added)
1944 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001945 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001946
1947insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001948 if (name_in_log_ref(root->log_root, name, name_len,
1949 key->objectid, log_key.objectid)) {
1950 /* The dentry will be added later. */
1951 ret = 0;
1952 update_size = false;
1953 goto out;
1954 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001955 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08001956 ret = insert_one_name(trans, root, key->objectid, key->offset,
1957 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001958 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001959 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001960 if (!ret)
1961 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001962 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001963 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001964 goto out;
1965}
1966
1967/*
1968 * find all the names in a directory item and reconcile them into
1969 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1970 * one name in a directory item, but the same code gets used for
1971 * both directory index types
1972 */
1973static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1974 struct btrfs_root *root,
1975 struct btrfs_path *path,
1976 struct extent_buffer *eb, int slot,
1977 struct btrfs_key *key)
1978{
Filipe Mananabb53eda2015-07-15 23:26:43 +01001979 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001980 u32 item_size = btrfs_item_size_nr(eb, slot);
1981 struct btrfs_dir_item *di;
1982 int name_len;
1983 unsigned long ptr;
1984 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001985 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001986
1987 ptr = btrfs_item_ptr_offset(eb, slot);
1988 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001989 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001990 di = (struct btrfs_dir_item *)ptr;
1991 name_len = btrfs_dir_name_len(eb, di);
1992 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001993 if (ret < 0)
1994 break;
Chris Masone02119d2008-09-05 16:13:11 -04001995 ptr = (unsigned long)(di + 1);
1996 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001997
1998 /*
1999 * If this entry refers to a non-directory (directories can not
2000 * have a link count > 1) and it was added in the transaction
2001 * that was not committed, make sure we fixup the link count of
2002 * the inode it the entry points to. Otherwise something like
2003 * the following would result in a directory pointing to an
2004 * inode with a wrong link that does not account for this dir
2005 * entry:
2006 *
2007 * mkdir testdir
2008 * touch testdir/foo
2009 * touch testdir/bar
2010 * sync
2011 *
2012 * ln testdir/bar testdir/bar_link
2013 * ln testdir/foo testdir/foo_link
2014 * xfs_io -c "fsync" testdir/bar
2015 *
2016 * <power failure>
2017 *
2018 * mount fs, log replay happens
2019 *
2020 * File foo would remain with a link count of 1 when it has two
2021 * entries pointing to it in the directory testdir. This would
2022 * make it impossible to ever delete the parent directory has
2023 * it would result in stale dentries that can never be deleted.
2024 */
2025 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
2026 struct btrfs_key di_key;
2027
2028 if (!fixup_path) {
2029 fixup_path = btrfs_alloc_path();
2030 if (!fixup_path) {
2031 ret = -ENOMEM;
2032 break;
2033 }
2034 }
2035
2036 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2037 ret = link_to_fixup_dir(trans, root, fixup_path,
2038 di_key.objectid);
2039 if (ret)
2040 break;
2041 }
2042 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002043 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01002044 btrfs_free_path(fixup_path);
2045 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002046}
2047
2048/*
2049 * directory replay has two parts. There are the standard directory
2050 * items in the log copied from the subvolume, and range items
2051 * created in the log while the subvolume was logged.
2052 *
2053 * The range items tell us which parts of the key space the log
2054 * is authoritative for. During replay, if a key in the subvolume
2055 * directory is in a logged range item, but not actually in the log
2056 * that means it was deleted from the directory before the fsync
2057 * and should be removed.
2058 */
2059static noinline int find_dir_range(struct btrfs_root *root,
2060 struct btrfs_path *path,
2061 u64 dirid, int key_type,
2062 u64 *start_ret, u64 *end_ret)
2063{
2064 struct btrfs_key key;
2065 u64 found_end;
2066 struct btrfs_dir_log_item *item;
2067 int ret;
2068 int nritems;
2069
2070 if (*start_ret == (u64)-1)
2071 return 1;
2072
2073 key.objectid = dirid;
2074 key.type = key_type;
2075 key.offset = *start_ret;
2076
2077 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2078 if (ret < 0)
2079 goto out;
2080 if (ret > 0) {
2081 if (path->slots[0] == 0)
2082 goto out;
2083 path->slots[0]--;
2084 }
2085 if (ret != 0)
2086 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2087
2088 if (key.type != key_type || key.objectid != dirid) {
2089 ret = 1;
2090 goto next;
2091 }
2092 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2093 struct btrfs_dir_log_item);
2094 found_end = btrfs_dir_log_end(path->nodes[0], item);
2095
2096 if (*start_ret >= key.offset && *start_ret <= found_end) {
2097 ret = 0;
2098 *start_ret = key.offset;
2099 *end_ret = found_end;
2100 goto out;
2101 }
2102 ret = 1;
2103next:
2104 /* check the next slot in the tree to see if it is a valid item */
2105 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko2a7bf532016-10-07 17:30:47 +08002106 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04002107 if (path->slots[0] >= nritems) {
2108 ret = btrfs_next_leaf(root, path);
2109 if (ret)
2110 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002111 }
2112
2113 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2114
2115 if (key.type != key_type || key.objectid != dirid) {
2116 ret = 1;
2117 goto out;
2118 }
2119 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2120 struct btrfs_dir_log_item);
2121 found_end = btrfs_dir_log_end(path->nodes[0], item);
2122 *start_ret = key.offset;
2123 *end_ret = found_end;
2124 ret = 0;
2125out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002126 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002127 return ret;
2128}
2129
2130/*
2131 * this looks for a given directory item in the log. If the directory
2132 * item is not in the log, the item is removed and the inode it points
2133 * to is unlinked
2134 */
2135static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2136 struct btrfs_root *root,
2137 struct btrfs_root *log,
2138 struct btrfs_path *path,
2139 struct btrfs_path *log_path,
2140 struct inode *dir,
2141 struct btrfs_key *dir_key)
2142{
2143 int ret;
2144 struct extent_buffer *eb;
2145 int slot;
2146 u32 item_size;
2147 struct btrfs_dir_item *di;
2148 struct btrfs_dir_item *log_di;
2149 int name_len;
2150 unsigned long ptr;
2151 unsigned long ptr_end;
2152 char *name;
2153 struct inode *inode;
2154 struct btrfs_key location;
2155
2156again:
2157 eb = path->nodes[0];
2158 slot = path->slots[0];
2159 item_size = btrfs_item_size_nr(eb, slot);
2160 ptr = btrfs_item_ptr_offset(eb, slot);
2161 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002162 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002163 di = (struct btrfs_dir_item *)ptr;
2164 name_len = btrfs_dir_name_len(eb, di);
2165 name = kmalloc(name_len, GFP_NOFS);
2166 if (!name) {
2167 ret = -ENOMEM;
2168 goto out;
2169 }
2170 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2171 name_len);
2172 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002173 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002174 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2175 dir_key->objectid,
2176 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002177 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002178 log_di = btrfs_lookup_dir_index_item(trans, log,
2179 log_path,
2180 dir_key->objectid,
2181 dir_key->offset,
2182 name, name_len, 0);
2183 }
Al Viro8d9e2202018-07-29 23:04:46 +01002184 if (!log_di || log_di == ERR_PTR(-ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002185 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002186 btrfs_release_path(path);
2187 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002188 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002189 if (!inode) {
2190 kfree(name);
2191 return -EIO;
2192 }
Chris Masone02119d2008-09-05 16:13:11 -04002193
2194 ret = link_to_fixup_dir(trans, root,
2195 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002196 if (ret) {
2197 kfree(name);
2198 iput(inode);
2199 goto out;
2200 }
2201
Zach Brown8b558c52013-10-16 12:10:34 -07002202 inc_nlink(inode);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02002203 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2204 BTRFS_I(inode), name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002205 if (!ret)
Nikolay Borisove5c304e62018-02-07 17:55:43 +02002206 ret = btrfs_run_delayed_items(trans);
Chris Masone02119d2008-09-05 16:13:11 -04002207 kfree(name);
2208 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002209 if (ret)
2210 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002211
2212 /* there might still be more names under this key
2213 * check and repeat if required
2214 */
2215 ret = btrfs_search_slot(NULL, root, dir_key, path,
2216 0, 0);
2217 if (ret == 0)
2218 goto again;
2219 ret = 0;
2220 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002221 } else if (IS_ERR(log_di)) {
2222 kfree(name);
2223 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002224 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002225 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002226 kfree(name);
2227
2228 ptr = (unsigned long)(di + 1);
2229 ptr += name_len;
2230 }
2231 ret = 0;
2232out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002233 btrfs_release_path(path);
2234 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002235 return ret;
2236}
2237
Filipe Manana4f764e52015-02-23 19:53:35 +00002238static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2239 struct btrfs_root *root,
2240 struct btrfs_root *log,
2241 struct btrfs_path *path,
2242 const u64 ino)
2243{
2244 struct btrfs_key search_key;
2245 struct btrfs_path *log_path;
2246 int i;
2247 int nritems;
2248 int ret;
2249
2250 log_path = btrfs_alloc_path();
2251 if (!log_path)
2252 return -ENOMEM;
2253
2254 search_key.objectid = ino;
2255 search_key.type = BTRFS_XATTR_ITEM_KEY;
2256 search_key.offset = 0;
2257again:
2258 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2259 if (ret < 0)
2260 goto out;
2261process_leaf:
2262 nritems = btrfs_header_nritems(path->nodes[0]);
2263 for (i = path->slots[0]; i < nritems; i++) {
2264 struct btrfs_key key;
2265 struct btrfs_dir_item *di;
2266 struct btrfs_dir_item *log_di;
2267 u32 total_size;
2268 u32 cur;
2269
2270 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2271 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2272 ret = 0;
2273 goto out;
2274 }
2275
2276 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2277 total_size = btrfs_item_size_nr(path->nodes[0], i);
2278 cur = 0;
2279 while (cur < total_size) {
2280 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2281 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2282 u32 this_len = sizeof(*di) + name_len + data_len;
2283 char *name;
2284
2285 name = kmalloc(name_len, GFP_NOFS);
2286 if (!name) {
2287 ret = -ENOMEM;
2288 goto out;
2289 }
2290 read_extent_buffer(path->nodes[0], name,
2291 (unsigned long)(di + 1), name_len);
2292
2293 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2294 name, name_len, 0);
2295 btrfs_release_path(log_path);
2296 if (!log_di) {
2297 /* Doesn't exist in log tree, so delete it. */
2298 btrfs_release_path(path);
2299 di = btrfs_lookup_xattr(trans, root, path, ino,
2300 name, name_len, -1);
2301 kfree(name);
2302 if (IS_ERR(di)) {
2303 ret = PTR_ERR(di);
2304 goto out;
2305 }
2306 ASSERT(di);
2307 ret = btrfs_delete_one_dir_name(trans, root,
2308 path, di);
2309 if (ret)
2310 goto out;
2311 btrfs_release_path(path);
2312 search_key = key;
2313 goto again;
2314 }
2315 kfree(name);
2316 if (IS_ERR(log_di)) {
2317 ret = PTR_ERR(log_di);
2318 goto out;
2319 }
2320 cur += this_len;
2321 di = (struct btrfs_dir_item *)((char *)di + this_len);
2322 }
2323 }
2324 ret = btrfs_next_leaf(root, path);
2325 if (ret > 0)
2326 ret = 0;
2327 else if (ret == 0)
2328 goto process_leaf;
2329out:
2330 btrfs_free_path(log_path);
2331 btrfs_release_path(path);
2332 return ret;
2333}
2334
2335
Chris Masone02119d2008-09-05 16:13:11 -04002336/*
2337 * deletion replay happens before we copy any new directory items
2338 * out of the log or out of backreferences from inodes. It
2339 * scans the log to find ranges of keys that log is authoritative for,
2340 * and then scans the directory to find items in those ranges that are
2341 * not present in the log.
2342 *
2343 * Anything we don't find in the log is unlinked and removed from the
2344 * directory.
2345 */
2346static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2347 struct btrfs_root *root,
2348 struct btrfs_root *log,
2349 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002350 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002351{
2352 u64 range_start;
2353 u64 range_end;
2354 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2355 int ret = 0;
2356 struct btrfs_key dir_key;
2357 struct btrfs_key found_key;
2358 struct btrfs_path *log_path;
2359 struct inode *dir;
2360
2361 dir_key.objectid = dirid;
2362 dir_key.type = BTRFS_DIR_ITEM_KEY;
2363 log_path = btrfs_alloc_path();
2364 if (!log_path)
2365 return -ENOMEM;
2366
2367 dir = read_one_inode(root, dirid);
2368 /* it isn't an error if the inode isn't there, that can happen
2369 * because we replay the deletes before we copy in the inode item
2370 * from the log
2371 */
2372 if (!dir) {
2373 btrfs_free_path(log_path);
2374 return 0;
2375 }
2376again:
2377 range_start = 0;
2378 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002379 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002380 if (del_all)
2381 range_end = (u64)-1;
2382 else {
2383 ret = find_dir_range(log, path, dirid, key_type,
2384 &range_start, &range_end);
2385 if (ret != 0)
2386 break;
2387 }
Chris Masone02119d2008-09-05 16:13:11 -04002388
2389 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002390 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002391 int nritems;
2392 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2393 0, 0);
2394 if (ret < 0)
2395 goto out;
2396
2397 nritems = btrfs_header_nritems(path->nodes[0]);
2398 if (path->slots[0] >= nritems) {
2399 ret = btrfs_next_leaf(root, path);
Liu Bob98def72018-04-03 01:59:48 +08002400 if (ret == 1)
Chris Masone02119d2008-09-05 16:13:11 -04002401 break;
Liu Bob98def72018-04-03 01:59:48 +08002402 else if (ret < 0)
2403 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002404 }
2405 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2406 path->slots[0]);
2407 if (found_key.objectid != dirid ||
2408 found_key.type != dir_key.type)
2409 goto next_type;
2410
2411 if (found_key.offset > range_end)
2412 break;
2413
2414 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002415 log_path, dir,
2416 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002417 if (ret)
2418 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002419 if (found_key.offset == (u64)-1)
2420 break;
2421 dir_key.offset = found_key.offset + 1;
2422 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002423 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002424 if (range_end == (u64)-1)
2425 break;
2426 range_start = range_end + 1;
2427 }
2428
2429next_type:
2430 ret = 0;
2431 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2432 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2433 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002434 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002435 goto again;
2436 }
2437out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002438 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002439 btrfs_free_path(log_path);
2440 iput(dir);
2441 return ret;
2442}
2443
2444/*
2445 * the process_func used to replay items from the log tree. This
2446 * gets called in two different stages. The first stage just looks
2447 * for inodes and makes sure they are all copied into the subvolume.
2448 *
2449 * The second stage copies all the other item types from the log into
2450 * the subvolume. The two stage approach is slower, but gets rid of
2451 * lots of complexity around inodes referencing other inodes that exist
2452 * only in the log (references come from either directory items or inode
2453 * back refs).
2454 */
2455static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +08002456 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -04002457{
2458 int nritems;
2459 struct btrfs_path *path;
2460 struct btrfs_root *root = wc->replay_dest;
2461 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002462 int i;
2463 int ret;
2464
Qu Wenruo581c1762018-03-29 09:08:11 +08002465 ret = btrfs_read_buffer(eb, gen, level, NULL);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002466 if (ret)
2467 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002468
2469 level = btrfs_header_level(eb);
2470
2471 if (level != 0)
2472 return 0;
2473
2474 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002475 if (!path)
2476 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002477
2478 nritems = btrfs_header_nritems(eb);
2479 for (i = 0; i < nritems; i++) {
2480 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002481
2482 /* inode keys are done during the first stage */
2483 if (key.type == BTRFS_INODE_ITEM_KEY &&
2484 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002485 struct btrfs_inode_item *inode_item;
2486 u32 mode;
2487
2488 inode_item = btrfs_item_ptr(eb, i,
2489 struct btrfs_inode_item);
Filipe Manana4f764e52015-02-23 19:53:35 +00002490 ret = replay_xattr_deletes(wc->trans, root, log,
2491 path, key.objectid);
2492 if (ret)
2493 break;
Chris Masone02119d2008-09-05 16:13:11 -04002494 mode = btrfs_inode_mode(eb, inode_item);
2495 if (S_ISDIR(mode)) {
2496 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002497 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002498 if (ret)
2499 break;
Chris Masone02119d2008-09-05 16:13:11 -04002500 }
2501 ret = overwrite_item(wc->trans, root, path,
2502 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002503 if (ret)
2504 break;
Chris Masone02119d2008-09-05 16:13:11 -04002505
Filipe Manana471d5572018-04-05 22:55:12 +01002506 /*
2507 * Before replaying extents, truncate the inode to its
2508 * size. We need to do it now and not after log replay
2509 * because before an fsync we can have prealloc extents
2510 * added beyond the inode's i_size. If we did it after,
2511 * through orphan cleanup for example, we would drop
2512 * those prealloc extents just after replaying them.
Chris Masone02119d2008-09-05 16:13:11 -04002513 */
2514 if (S_ISREG(mode)) {
Filipe Manana471d5572018-04-05 22:55:12 +01002515 struct inode *inode;
2516 u64 from;
2517
2518 inode = read_one_inode(root, key.objectid);
2519 if (!inode) {
2520 ret = -EIO;
2521 break;
2522 }
2523 from = ALIGN(i_size_read(inode),
2524 root->fs_info->sectorsize);
2525 ret = btrfs_drop_extents(wc->trans, root, inode,
2526 from, (u64)-1, 1);
2527 /*
2528 * If the nlink count is zero here, the iput
2529 * will free the inode. We bump it to make
2530 * sure it doesn't get freed until the link
2531 * count fixup is done.
2532 */
2533 if (!ret) {
2534 if (inode->i_nlink == 0)
2535 inc_nlink(inode);
2536 /* Update link count and nbytes. */
2537 ret = btrfs_update_inode(wc->trans,
2538 root, inode);
2539 }
2540 iput(inode);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002541 if (ret)
2542 break;
Chris Masone02119d2008-09-05 16:13:11 -04002543 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002544
Chris Masone02119d2008-09-05 16:13:11 -04002545 ret = link_to_fixup_dir(wc->trans, root,
2546 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002547 if (ret)
2548 break;
Chris Masone02119d2008-09-05 16:13:11 -04002549 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002550
2551 if (key.type == BTRFS_DIR_INDEX_KEY &&
2552 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2553 ret = replay_one_dir_item(wc->trans, root, path,
2554 eb, i, &key);
2555 if (ret)
2556 break;
2557 }
2558
Chris Masone02119d2008-09-05 16:13:11 -04002559 if (wc->stage < LOG_WALK_REPLAY_ALL)
2560 continue;
2561
2562 /* these keys are simply copied */
2563 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2564 ret = overwrite_item(wc->trans, root, path,
2565 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002566 if (ret)
2567 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002568 } else if (key.type == BTRFS_INODE_REF_KEY ||
2569 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002570 ret = add_inode_ref(wc->trans, root, log, path,
2571 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002572 if (ret && ret != -ENOENT)
2573 break;
2574 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002575 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2576 ret = replay_one_extent(wc->trans, root, path,
2577 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002578 if (ret)
2579 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002580 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002581 ret = replay_one_dir_item(wc->trans, root, path,
2582 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002583 if (ret)
2584 break;
Chris Masone02119d2008-09-05 16:13:11 -04002585 }
2586 }
2587 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002588 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002589}
2590
Chris Masond3977122009-01-05 21:25:51 -05002591static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002592 struct btrfs_root *root,
2593 struct btrfs_path *path, int *level,
2594 struct walk_control *wc)
2595{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002596 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002597 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002598 u64 bytenr;
2599 u64 ptr_gen;
2600 struct extent_buffer *next;
2601 struct extent_buffer *cur;
2602 struct extent_buffer *parent;
2603 u32 blocksize;
2604 int ret = 0;
2605
2606 WARN_ON(*level < 0);
2607 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2608
Chris Masond3977122009-01-05 21:25:51 -05002609 while (*level > 0) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002610 struct btrfs_key first_key;
2611
Chris Masone02119d2008-09-05 16:13:11 -04002612 WARN_ON(*level < 0);
2613 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2614 cur = path->nodes[*level];
2615
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302616 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002617
2618 if (path->slots[*level] >=
2619 btrfs_header_nritems(cur))
2620 break;
2621
2622 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2623 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08002624 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002625 blocksize = fs_info->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002626
2627 parent = path->nodes[*level];
2628 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002629
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002630 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002631 if (IS_ERR(next))
2632 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002633
Chris Masone02119d2008-09-05 16:13:11 -04002634 if (*level == 1) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002635 ret = wc->process_func(root, next, wc, ptr_gen,
2636 *level - 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002637 if (ret) {
2638 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002639 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002640 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002641
Chris Masone02119d2008-09-05 16:13:11 -04002642 path->slots[*level]++;
2643 if (wc->free) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002644 ret = btrfs_read_buffer(next, ptr_gen,
2645 *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002646 if (ret) {
2647 free_extent_buffer(next);
2648 return ret;
2649 }
Chris Masone02119d2008-09-05 16:13:11 -04002650
Josef Bacik681ae502013-10-07 15:11:00 -04002651 if (trans) {
2652 btrfs_tree_lock(next);
2653 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002654 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002655 btrfs_wait_tree_block_writeback(next);
2656 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002657 } else {
2658 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2659 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002660 }
Chris Masone02119d2008-09-05 16:13:11 -04002661
Chris Masone02119d2008-09-05 16:13:11 -04002662 WARN_ON(root_owner !=
2663 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002664 ret = btrfs_free_and_pin_reserved_extent(
2665 fs_info, bytenr,
2666 blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002667 if (ret) {
2668 free_extent_buffer(next);
2669 return ret;
2670 }
Chris Masone02119d2008-09-05 16:13:11 -04002671 }
2672 free_extent_buffer(next);
2673 continue;
2674 }
Qu Wenruo581c1762018-03-29 09:08:11 +08002675 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002676 if (ret) {
2677 free_extent_buffer(next);
2678 return ret;
2679 }
Chris Masone02119d2008-09-05 16:13:11 -04002680
2681 WARN_ON(*level <= 0);
2682 if (path->nodes[*level-1])
2683 free_extent_buffer(path->nodes[*level-1]);
2684 path->nodes[*level-1] = next;
2685 *level = btrfs_header_level(next);
2686 path->slots[*level] = 0;
2687 cond_resched();
2688 }
2689 WARN_ON(*level < 0);
2690 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2691
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002692 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002693
2694 cond_resched();
2695 return 0;
2696}
2697
Chris Masond3977122009-01-05 21:25:51 -05002698static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002699 struct btrfs_root *root,
2700 struct btrfs_path *path, int *level,
2701 struct walk_control *wc)
2702{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002703 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002704 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002705 int i;
2706 int slot;
2707 int ret;
2708
Chris Masond3977122009-01-05 21:25:51 -05002709 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002710 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002711 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002712 path->slots[i]++;
2713 *level = i;
2714 WARN_ON(*level == 0);
2715 return 0;
2716 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002717 struct extent_buffer *parent;
2718 if (path->nodes[*level] == root->node)
2719 parent = path->nodes[*level];
2720 else
2721 parent = path->nodes[*level + 1];
2722
2723 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002724 ret = wc->process_func(root, path->nodes[*level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002725 btrfs_header_generation(path->nodes[*level]),
2726 *level);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002727 if (ret)
2728 return ret;
2729
Chris Masone02119d2008-09-05 16:13:11 -04002730 if (wc->free) {
2731 struct extent_buffer *next;
2732
2733 next = path->nodes[*level];
2734
Josef Bacik681ae502013-10-07 15:11:00 -04002735 if (trans) {
2736 btrfs_tree_lock(next);
2737 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002738 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002739 btrfs_wait_tree_block_writeback(next);
2740 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002741 } else {
2742 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2743 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002744 }
Chris Masone02119d2008-09-05 16:13:11 -04002745
Chris Masone02119d2008-09-05 16:13:11 -04002746 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002747 ret = btrfs_free_and_pin_reserved_extent(
2748 fs_info,
Chris Masone02119d2008-09-05 16:13:11 -04002749 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002750 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002751 if (ret)
2752 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002753 }
2754 free_extent_buffer(path->nodes[*level]);
2755 path->nodes[*level] = NULL;
2756 *level = i + 1;
2757 }
2758 }
2759 return 1;
2760}
2761
2762/*
2763 * drop the reference count on the tree rooted at 'snap'. This traverses
2764 * the tree freeing any blocks that have a ref count of zero after being
2765 * decremented.
2766 */
2767static int walk_log_tree(struct btrfs_trans_handle *trans,
2768 struct btrfs_root *log, struct walk_control *wc)
2769{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002770 struct btrfs_fs_info *fs_info = log->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002771 int ret = 0;
2772 int wret;
2773 int level;
2774 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002775 int orig_level;
2776
2777 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002778 if (!path)
2779 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002780
2781 level = btrfs_header_level(log->node);
2782 orig_level = level;
2783 path->nodes[level] = log->node;
2784 extent_buffer_get(log->node);
2785 path->slots[level] = 0;
2786
Chris Masond3977122009-01-05 21:25:51 -05002787 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002788 wret = walk_down_log_tree(trans, log, path, &level, wc);
2789 if (wret > 0)
2790 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002791 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002792 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002793 goto out;
2794 }
Chris Masone02119d2008-09-05 16:13:11 -04002795
2796 wret = walk_up_log_tree(trans, log, path, &level, wc);
2797 if (wret > 0)
2798 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002799 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002800 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002801 goto out;
2802 }
Chris Masone02119d2008-09-05 16:13:11 -04002803 }
2804
2805 /* was the root node processed? if not, catch it here */
2806 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002807 ret = wc->process_func(log, path->nodes[orig_level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002808 btrfs_header_generation(path->nodes[orig_level]),
2809 orig_level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002810 if (ret)
2811 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002812 if (wc->free) {
2813 struct extent_buffer *next;
2814
2815 next = path->nodes[orig_level];
2816
Josef Bacik681ae502013-10-07 15:11:00 -04002817 if (trans) {
2818 btrfs_tree_lock(next);
2819 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002820 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002821 btrfs_wait_tree_block_writeback(next);
2822 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002823 } else {
2824 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2825 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002826 }
Chris Masone02119d2008-09-05 16:13:11 -04002827
Chris Masone02119d2008-09-05 16:13:11 -04002828 WARN_ON(log->root_key.objectid !=
2829 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002830 ret = btrfs_free_and_pin_reserved_extent(fs_info,
2831 next->start, next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002832 if (ret)
2833 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002834 }
2835 }
2836
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002837out:
Chris Masone02119d2008-09-05 16:13:11 -04002838 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002839 return ret;
2840}
2841
Yan Zheng7237f182009-01-21 12:54:03 -05002842/*
2843 * helper function to update the item for a given subvolumes log root
2844 * in the tree of log roots
2845 */
2846static int update_log_root(struct btrfs_trans_handle *trans,
2847 struct btrfs_root *log)
2848{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002849 struct btrfs_fs_info *fs_info = log->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -05002850 int ret;
2851
2852 if (log->log_transid == 1) {
2853 /* insert root item on the first sync */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002854 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002855 &log->root_key, &log->root_item);
2856 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002857 ret = btrfs_update_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002858 &log->root_key, &log->root_item);
2859 }
2860 return ret;
2861}
2862
Zhaolei60d53eb2015-08-17 18:44:46 +08002863static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002864{
2865 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002866 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002867
Yan Zheng7237f182009-01-21 12:54:03 -05002868 /*
2869 * we only allow two pending log transactions at a time,
2870 * so we know that if ours is more than 2 older than the
2871 * current transaction, we're done
2872 */
Liu Bo49e83f52017-09-01 16:14:30 -06002873 for (;;) {
Yan Zheng7237f182009-01-21 12:54:03 -05002874 prepare_to_wait(&root->log_commit_wait[index],
2875 &wait, TASK_UNINTERRUPTIBLE);
Liu Bo49e83f52017-09-01 16:14:30 -06002876
2877 if (!(root->log_transid_committed < transid &&
2878 atomic_read(&root->log_commit[index])))
2879 break;
2880
Yan Zheng7237f182009-01-21 12:54:03 -05002881 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002882 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002883 mutex_lock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002884 }
2885 finish_wait(&root->log_commit_wait[index], &wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002886}
2887
Zhaolei60d53eb2015-08-17 18:44:46 +08002888static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002889{
2890 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002891
Liu Bo49e83f52017-09-01 16:14:30 -06002892 for (;;) {
2893 prepare_to_wait(&root->log_writer_wait, &wait,
2894 TASK_UNINTERRUPTIBLE);
2895 if (!atomic_read(&root->log_writers))
2896 break;
2897
Yan Zheng7237f182009-01-21 12:54:03 -05002898 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002899 schedule();
Filipe Manana575849e2015-02-11 11:12:39 +00002900 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002901 }
Liu Bo49e83f52017-09-01 16:14:30 -06002902 finish_wait(&root->log_writer_wait, &wait);
Chris Masone02119d2008-09-05 16:13:11 -04002903}
2904
Miao Xie8b050d32014-02-20 18:08:58 +08002905static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2906 struct btrfs_log_ctx *ctx)
2907{
2908 if (!ctx)
2909 return;
2910
2911 mutex_lock(&root->log_mutex);
2912 list_del_init(&ctx->list);
2913 mutex_unlock(&root->log_mutex);
2914}
2915
2916/*
2917 * Invoked in log mutex context, or be sure there is no other task which
2918 * can access the list.
2919 */
2920static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2921 int index, int error)
2922{
2923 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002924 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002925
Chris Mason570dd452016-10-27 10:42:20 -07002926 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2927 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002928 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002929 }
Miao Xie8b050d32014-02-20 18:08:58 +08002930
2931 INIT_LIST_HEAD(&root->log_ctxs[index]);
2932}
2933
Chris Masone02119d2008-09-05 16:13:11 -04002934/*
2935 * btrfs_sync_log does sends a given tree log down to the disk and
2936 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002937 * you know that any inodes previously logged are safely on disk only
2938 * if it returns 0.
2939 *
2940 * Any other return value means you need to call btrfs_commit_transaction.
2941 * Some of the edge cases for fsyncing directories that have had unlinks
2942 * or renames done in the past mean that sometimes the only safe
2943 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2944 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002945 */
2946int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002947 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002948{
Yan Zheng7237f182009-01-21 12:54:03 -05002949 int index1;
2950 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002951 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002952 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002953 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002954 struct btrfs_root *log = root->log_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002955 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002956 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002957 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002958 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002959
Yan Zheng7237f182009-01-21 12:54:03 -05002960 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002961 log_transid = ctx->log_transid;
2962 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002963 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002964 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002965 }
Miao Xied1433de2014-02-20 18:08:59 +08002966
2967 index1 = log_transid % 2;
2968 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002969 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002970 mutex_unlock(&root->log_mutex);
2971 return ctx->log_ret;
2972 }
2973 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002974 atomic_set(&root->log_commit[index1], 1);
2975
2976 /* wait for previous tree log sync to complete */
2977 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002978 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002979
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002980 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002981 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002982 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002983 if (!btrfs_test_opt(fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08002984 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002985 mutex_unlock(&root->log_mutex);
2986 schedule_timeout_uninterruptible(1);
2987 mutex_lock(&root->log_mutex);
2988 }
Zhaolei60d53eb2015-08-17 18:44:46 +08002989 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002990 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002991 break;
2992 }
Chris Masond0c803c2008-09-11 16:17:57 -04002993
Chris Mason12fcfd22009-03-24 10:24:20 -04002994 /* bail out if we need to do a full commit */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002995 if (btrfs_need_log_full_commit(fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002996 ret = -EAGAIN;
2997 mutex_unlock(&root->log_mutex);
2998 goto out;
2999 }
3000
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003001 if (log_transid % 2 == 0)
3002 mark = EXTENT_DIRTY;
3003 else
3004 mark = EXTENT_NEW;
3005
Chris Mason690587d2009-10-13 13:29:19 -04003006 /* we start IO on all the marked extents here, but we don't actually
3007 * wait for them until later.
3008 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00003009 blk_start_plug(&plug);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003010 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003011 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003012 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04003013 btrfs_abort_transaction(trans, ret);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003014 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003015 mutex_unlock(&root->log_mutex);
3016 goto out;
3017 }
Yan Zheng7237f182009-01-21 12:54:03 -05003018
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003019 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05003020
Yan Zheng7237f182009-01-21 12:54:03 -05003021 root->log_transid++;
3022 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04003023 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05003024 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003025 * IO has been started, blocks of the log tree have WRITTEN flag set
3026 * in their headers. new modifications of the log will be written to
3027 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05003028 */
3029 mutex_unlock(&root->log_mutex);
3030
Filipe Manana28a23592016-08-23 21:13:51 +01003031 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08003032
Yan Zheng7237f182009-01-21 12:54:03 -05003033 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06003034 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05003035 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08003036
3037 index2 = log_root_tree->log_transid % 2;
3038 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
3039 root_log_ctx.log_transid = log_root_tree->log_transid;
3040
Yan Zheng7237f182009-01-21 12:54:03 -05003041 mutex_unlock(&log_root_tree->log_mutex);
3042
3043 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05003044
3045 mutex_lock(&log_root_tree->log_mutex);
3046 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba093258e2018-02-26 16:15:17 +01003047 /* atomic_dec_and_test implies a barrier */
3048 cond_wake_up_nomb(&log_root_tree->log_writer_wait);
Yan Zheng7237f182009-01-21 12:54:03 -05003049 }
3050
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003051 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08003052 if (!list_empty(&root_log_ctx.list))
3053 list_del_init(&root_log_ctx.list);
3054
Miao Xiec6adc9c2013-05-28 10:05:39 +00003055 blk_finish_plug(&plug);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003056 btrfs_set_log_full_commit(fs_info, trans);
Miao Xie995946d2014-04-02 19:51:06 +08003057
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003058 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003059 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003060 mutex_unlock(&log_root_tree->log_mutex);
3061 goto out;
3062 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003063 btrfs_wait_tree_log_extents(log, mark);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003064 mutex_unlock(&log_root_tree->log_mutex);
3065 ret = -EAGAIN;
3066 goto out;
3067 }
3068
Miao Xied1433de2014-02-20 18:08:59 +08003069 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08003070 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07003071 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08003072 mutex_unlock(&log_root_tree->log_mutex);
3073 ret = root_log_ctx.log_ret;
3074 goto out;
3075 }
Miao Xie8b050d32014-02-20 18:08:58 +08003076
Miao Xied1433de2014-02-20 18:08:59 +08003077 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05003078 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003079 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003080 ret = btrfs_wait_tree_log_extents(log, mark);
Zhaolei60d53eb2015-08-17 18:44:46 +08003081 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003082 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003083 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003084 if (!ret)
3085 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05003086 goto out;
3087 }
Miao Xied1433de2014-02-20 18:08:59 +08003088 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003089 atomic_set(&log_root_tree->log_commit[index2], 1);
3090
Chris Mason12fcfd22009-03-24 10:24:20 -04003091 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003092 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003093 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04003094 }
Yan Zheng7237f182009-01-21 12:54:03 -05003095
Zhaolei60d53eb2015-08-17 18:44:46 +08003096 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04003097
3098 /*
3099 * now that we've moved on to the tree of log tree roots,
3100 * check the full commit flag again
3101 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003102 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003103 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003104 btrfs_wait_tree_log_extents(log, mark);
Chris Mason12fcfd22009-03-24 10:24:20 -04003105 mutex_unlock(&log_root_tree->log_mutex);
3106 ret = -EAGAIN;
3107 goto out_wake_log_root;
3108 }
Yan Zheng7237f182009-01-21 12:54:03 -05003109
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003110 ret = btrfs_write_marked_extents(fs_info,
Miao Xiec6adc9c2013-05-28 10:05:39 +00003111 &log_root_tree->dirty_log_pages,
3112 EXTENT_DIRTY | EXTENT_NEW);
3113 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003114 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003115 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003116 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003117 mutex_unlock(&log_root_tree->log_mutex);
3118 goto out_wake_log_root;
3119 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003120 ret = btrfs_wait_tree_log_extents(log, mark);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003121 if (!ret)
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003122 ret = btrfs_wait_tree_log_extents(log_root_tree,
3123 EXTENT_NEW | EXTENT_DIRTY);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003124 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003125 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003126 mutex_unlock(&log_root_tree->log_mutex);
3127 goto out_wake_log_root;
3128 }
Chris Masone02119d2008-09-05 16:13:11 -04003129
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003130 btrfs_set_super_log_root(fs_info->super_for_commit,
3131 log_root_tree->node->start);
3132 btrfs_set_super_log_root_level(fs_info->super_for_commit,
3133 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04003134
Yan Zheng7237f182009-01-21 12:54:03 -05003135 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05003136 mutex_unlock(&log_root_tree->log_mutex);
3137
3138 /*
3139 * nobody else is going to jump in and write the the ctree
3140 * super here because the log_commit atomic below is protecting
3141 * us. We must be called with a transaction handle pinning
3142 * the running transaction open, so a full commit can't hop
3143 * in and cause problems either.
3144 */
David Sterbaeece6a92017-02-10 19:04:32 +01003145 ret = write_all_supers(fs_info, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003146 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003147 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003148 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003149 goto out_wake_log_root;
3150 }
Yan Zheng7237f182009-01-21 12:54:03 -05003151
Chris Mason257c62e2009-10-13 13:21:08 -04003152 mutex_lock(&root->log_mutex);
3153 if (root->last_log_commit < log_transid)
3154 root->last_log_commit = log_transid;
3155 mutex_unlock(&root->log_mutex);
3156
Chris Mason12fcfd22009-03-24 10:24:20 -04003157out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07003158 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003159 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3160
Miao Xied1433de2014-02-20 18:08:59 +08003161 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003162 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003163 mutex_unlock(&log_root_tree->log_mutex);
3164
David Sterba33a9eca2015-10-10 18:35:10 +02003165 /*
David Sterba093258e2018-02-26 16:15:17 +01003166 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3167 * all the updates above are seen by the woken threads. It might not be
3168 * necessary, but proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003169 */
David Sterba093258e2018-02-26 16:15:17 +01003170 cond_wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04003171out:
Miao Xied1433de2014-02-20 18:08:59 +08003172 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07003173 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08003174 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003175 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003176 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003177
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(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003184 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003185}
3186
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003187static void free_log_tree(struct btrfs_trans_handle *trans,
3188 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003189{
3190 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04003191 u64 start;
3192 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04003193 struct walk_control wc = {
3194 .free = 1,
3195 .process_func = process_one_buffer
3196 };
3197
Josef Bacik681ae502013-10-07 15:11:00 -04003198 ret = walk_log_tree(trans, log, &wc);
3199 /* I don't think this can happen but just in case */
3200 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003201 btrfs_abort_transaction(trans, ret);
Chris Masone02119d2008-09-05 16:13:11 -04003202
Chris Masond3977122009-01-05 21:25:51 -05003203 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003204 ret = find_first_extent_bit(&log->dirty_log_pages,
Liu Bo55237a52018-01-25 11:02:52 -07003205 0, &start, &end,
3206 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
Josef Bacike6138872012-09-27 17:07:30 -04003207 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003208 if (ret)
3209 break;
3210
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003211 clear_extent_bits(&log->dirty_log_pages, start, end,
Liu Bo55237a52018-01-25 11:02:52 -07003212 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Chris Masond0c803c2008-09-11 16:17:57 -04003213 }
3214
Yan Zheng7237f182009-01-21 12:54:03 -05003215 free_extent_buffer(log->node);
3216 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003217}
3218
3219/*
3220 * free all the extents used by the tree log. This should be called
3221 * at commit time of the full transaction
3222 */
3223int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3224{
3225 if (root->log_root) {
3226 free_log_tree(trans, root->log_root);
3227 root->log_root = NULL;
3228 }
3229 return 0;
3230}
3231
3232int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3233 struct btrfs_fs_info *fs_info)
3234{
3235 if (fs_info->log_root_tree) {
3236 free_log_tree(trans, fs_info->log_root_tree);
3237 fs_info->log_root_tree = NULL;
3238 }
Chris Masone02119d2008-09-05 16:13:11 -04003239 return 0;
3240}
3241
3242/*
Chris Masone02119d2008-09-05 16:13:11 -04003243 * If both a file and directory are logged, and unlinks or renames are
3244 * mixed in, we have a few interesting corners:
3245 *
3246 * create file X in dir Y
3247 * link file X to X.link in dir Y
3248 * fsync file X
3249 * unlink file X but leave X.link
3250 * fsync dir Y
3251 *
3252 * After a crash we would expect only X.link to exist. But file X
3253 * didn't get fsync'd again so the log has back refs for X and X.link.
3254 *
3255 * We solve this by removing directory entries and inode backrefs from the
3256 * log when a file that was logged in the current transaction is
3257 * unlinked. Any later fsync will include the updated log entries, and
3258 * we'll be able to reconstruct the proper directory items from backrefs.
3259 *
3260 * This optimizations allows us to avoid relogging the entire inode
3261 * or the entire directory.
3262 */
3263int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3264 struct btrfs_root *root,
3265 const char *name, int name_len,
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003266 struct btrfs_inode *dir, u64 index)
Chris Masone02119d2008-09-05 16:13:11 -04003267{
3268 struct btrfs_root *log;
3269 struct btrfs_dir_item *di;
3270 struct btrfs_path *path;
3271 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003272 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003273 int bytes_del = 0;
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003274 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003275
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003276 if (dir->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003277 return 0;
3278
Chris Masone02119d2008-09-05 16:13:11 -04003279 ret = join_running_log_trans(root);
3280 if (ret)
3281 return 0;
3282
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003283 mutex_lock(&dir->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003284
3285 log = root->log_root;
3286 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003287 if (!path) {
3288 err = -ENOMEM;
3289 goto out_unlock;
3290 }
liubo2a29edc2011-01-26 06:22:08 +00003291
Li Zefan33345d012011-04-20 10:31:50 +08003292 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003293 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003294 if (IS_ERR(di)) {
3295 err = PTR_ERR(di);
3296 goto fail;
3297 }
3298 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003299 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3300 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003301 if (ret) {
3302 err = ret;
3303 goto fail;
3304 }
Chris Masone02119d2008-09-05 16:13:11 -04003305 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003306 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003307 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003308 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003309 if (IS_ERR(di)) {
3310 err = PTR_ERR(di);
3311 goto fail;
3312 }
3313 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003314 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3315 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003316 if (ret) {
3317 err = ret;
3318 goto fail;
3319 }
Chris Masone02119d2008-09-05 16:13:11 -04003320 }
3321
3322 /* update the directory size in the log to reflect the names
3323 * we have removed
3324 */
3325 if (bytes_del) {
3326 struct btrfs_key key;
3327
Li Zefan33345d012011-04-20 10:31:50 +08003328 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003329 key.offset = 0;
3330 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003331 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003332
3333 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003334 if (ret < 0) {
3335 err = ret;
3336 goto fail;
3337 }
Chris Masone02119d2008-09-05 16:13:11 -04003338 if (ret == 0) {
3339 struct btrfs_inode_item *item;
3340 u64 i_size;
3341
3342 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3343 struct btrfs_inode_item);
3344 i_size = btrfs_inode_size(path->nodes[0], item);
3345 if (i_size > bytes_del)
3346 i_size -= bytes_del;
3347 else
3348 i_size = 0;
3349 btrfs_set_inode_size(path->nodes[0], item, i_size);
3350 btrfs_mark_buffer_dirty(path->nodes[0]);
3351 } else
3352 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003353 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003354 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003355fail:
Chris Masone02119d2008-09-05 16:13:11 -04003356 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003357out_unlock:
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003358 mutex_unlock(&dir->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003359 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003360 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003361 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003362 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003363 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003364
Chris Mason12fcfd22009-03-24 10:24:20 -04003365 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003366
Andi Kleen411fc6b2010-10-29 15:14:31 -04003367 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003368}
3369
3370/* see comments for btrfs_del_dir_entries_in_log */
3371int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3372 struct btrfs_root *root,
3373 const char *name, int name_len,
Nikolay Borisova491abb2017-01-18 00:31:33 +02003374 struct btrfs_inode *inode, u64 dirid)
Chris Masone02119d2008-09-05 16:13:11 -04003375{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003376 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003377 struct btrfs_root *log;
3378 u64 index;
3379 int ret;
3380
Nikolay Borisova491abb2017-01-18 00:31:33 +02003381 if (inode->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003382 return 0;
3383
Chris Masone02119d2008-09-05 16:13:11 -04003384 ret = join_running_log_trans(root);
3385 if (ret)
3386 return 0;
3387 log = root->log_root;
Nikolay Borisova491abb2017-01-18 00:31:33 +02003388 mutex_lock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003389
Nikolay Borisova491abb2017-01-18 00:31:33 +02003390 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003391 dirid, &index);
Nikolay Borisova491abb2017-01-18 00:31:33 +02003392 mutex_unlock(&inode->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003393 if (ret == -ENOSPC) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003394 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003395 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003396 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003397 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003398 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003399
Chris Masone02119d2008-09-05 16:13:11 -04003400 return ret;
3401}
3402
3403/*
3404 * creates a range item in the log for 'dirid'. first_offset and
3405 * last_offset tell us which parts of the key space the log should
3406 * be considered authoritative for.
3407 */
3408static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3409 struct btrfs_root *log,
3410 struct btrfs_path *path,
3411 int key_type, u64 dirid,
3412 u64 first_offset, u64 last_offset)
3413{
3414 int ret;
3415 struct btrfs_key key;
3416 struct btrfs_dir_log_item *item;
3417
3418 key.objectid = dirid;
3419 key.offset = first_offset;
3420 if (key_type == BTRFS_DIR_ITEM_KEY)
3421 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3422 else
3423 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3424 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003425 if (ret)
3426 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003427
3428 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3429 struct btrfs_dir_log_item);
3430 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3431 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003432 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003433 return 0;
3434}
3435
3436/*
3437 * log all the items included in the current transaction for a given
3438 * directory. This also creates the range items in the log tree required
3439 * to replay anything deleted before the fsync
3440 */
3441static noinline int log_dir_items(struct btrfs_trans_handle *trans,
Nikolay Borisov684a5772017-01-18 00:31:41 +02003442 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003443 struct btrfs_path *path,
3444 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003445 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003446 u64 min_offset, u64 *last_offset_ret)
3447{
3448 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003449 struct btrfs_root *log = root->log_root;
3450 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003451 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003452 int ret;
3453 int i;
3454 int nritems;
3455 u64 first_offset = min_offset;
3456 u64 last_offset = (u64)-1;
Nikolay Borisov684a5772017-01-18 00:31:41 +02003457 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003458
3459 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003460
Li Zefan33345d012011-04-20 10:31:50 +08003461 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003462 min_key.type = key_type;
3463 min_key.offset = min_offset;
3464
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003465 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003466
3467 /*
3468 * we didn't find anything from this transaction, see if there
3469 * is anything at all
3470 */
Li Zefan33345d012011-04-20 10:31:50 +08003471 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3472 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003473 min_key.type = key_type;
3474 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003475 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003476 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3477 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003478 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003479 return ret;
3480 }
Li Zefan33345d012011-04-20 10:31:50 +08003481 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003482
3483 /* if ret == 0 there are items for this type,
3484 * create a range to tell us the last key of this type.
3485 * otherwise, there are no items in this directory after
3486 * *min_offset, and we create a range to indicate that.
3487 */
3488 if (ret == 0) {
3489 struct btrfs_key tmp;
3490 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3491 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003492 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003493 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003494 }
3495 goto done;
3496 }
3497
3498 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003499 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003500 if (ret == 0) {
3501 struct btrfs_key tmp;
3502 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3503 if (key_type == tmp.type) {
3504 first_offset = tmp.offset;
3505 ret = overwrite_item(trans, log, dst_path,
3506 path->nodes[0], path->slots[0],
3507 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003508 if (ret) {
3509 err = ret;
3510 goto done;
3511 }
Chris Masone02119d2008-09-05 16:13:11 -04003512 }
3513 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003514 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003515
3516 /* find the first key from this transaction again */
3517 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303518 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003519 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003520
3521 /*
3522 * we have a block from this transaction, log every item in it
3523 * from our directory
3524 */
Chris Masond3977122009-01-05 21:25:51 -05003525 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003526 struct btrfs_key tmp;
3527 src = path->nodes[0];
3528 nritems = btrfs_header_nritems(src);
3529 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003530 struct btrfs_dir_item *di;
3531
Chris Masone02119d2008-09-05 16:13:11 -04003532 btrfs_item_key_to_cpu(src, &min_key, i);
3533
Li Zefan33345d012011-04-20 10:31:50 +08003534 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003535 goto done;
3536 ret = overwrite_item(trans, log, dst_path, src, i,
3537 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003538 if (ret) {
3539 err = ret;
3540 goto done;
3541 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003542
3543 /*
3544 * We must make sure that when we log a directory entry,
3545 * the corresponding inode, after log replay, has a
3546 * matching link count. For example:
3547 *
3548 * touch foo
3549 * mkdir mydir
3550 * sync
3551 * ln foo mydir/bar
3552 * xfs_io -c "fsync" mydir
3553 * <crash>
3554 * <mount fs and log replay>
3555 *
3556 * Would result in a fsync log that when replayed, our
3557 * file inode would have a link count of 1, but we get
3558 * two directory entries pointing to the same inode.
3559 * After removing one of the names, it would not be
3560 * possible to remove the other name, which resulted
3561 * always in stale file handle errors, and would not
3562 * be possible to rmdir the parent directory, since
3563 * its i_size could never decrement to the value
3564 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3565 */
3566 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3567 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3568 if (ctx &&
3569 (btrfs_dir_transid(src, di) == trans->transid ||
3570 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3571 tmp.type != BTRFS_ROOT_ITEM_KEY)
3572 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003573 }
3574 path->slots[0] = nritems;
3575
3576 /*
3577 * look ahead to the next item and see if it is also
3578 * from this directory and from this transaction
3579 */
3580 ret = btrfs_next_leaf(root, path);
Liu Bo80c0b422018-04-03 01:59:47 +08003581 if (ret) {
3582 if (ret == 1)
3583 last_offset = (u64)-1;
3584 else
3585 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003586 goto done;
3587 }
3588 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003589 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003590 last_offset = (u64)-1;
3591 goto done;
3592 }
3593 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3594 ret = overwrite_item(trans, log, dst_path,
3595 path->nodes[0], path->slots[0],
3596 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003597 if (ret)
3598 err = ret;
3599 else
3600 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003601 goto done;
3602 }
3603 }
3604done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003605 btrfs_release_path(path);
3606 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003607
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003608 if (err == 0) {
3609 *last_offset_ret = last_offset;
3610 /*
3611 * insert the log range keys to indicate where the log
3612 * is valid
3613 */
3614 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003615 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003616 if (ret)
3617 err = ret;
3618 }
3619 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003620}
3621
3622/*
3623 * logging directories is very similar to logging inodes, We find all the items
3624 * from the current transaction and write them to the log.
3625 *
3626 * The recovery code scans the directory in the subvolume, and if it finds a
3627 * key in the range logged that is not present in the log tree, then it means
3628 * that dir entry was unlinked during the transaction.
3629 *
3630 * In order for that scan to work, we must include one key smaller than
3631 * the smallest logged by this transaction and one key larger than the largest
3632 * key logged by this transaction.
3633 */
3634static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003635 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003636 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003637 struct btrfs_path *dst_path,
3638 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003639{
3640 u64 min_key;
3641 u64 max_key;
3642 int ret;
3643 int key_type = BTRFS_DIR_ITEM_KEY;
3644
3645again:
3646 min_key = 0;
3647 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003648 while (1) {
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003649 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3650 ctx, min_key, &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003651 if (ret)
3652 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003653 if (max_key == (u64)-1)
3654 break;
3655 min_key = max_key + 1;
3656 }
3657
3658 if (key_type == BTRFS_DIR_ITEM_KEY) {
3659 key_type = BTRFS_DIR_INDEX_KEY;
3660 goto again;
3661 }
3662 return 0;
3663}
3664
3665/*
3666 * a helper function to drop items from the log before we relog an
3667 * inode. max_key_type indicates the highest item type to remove.
3668 * This cannot be run for file data extents because it does not
3669 * free the extents they point to.
3670 */
3671static int drop_objectid_items(struct btrfs_trans_handle *trans,
3672 struct btrfs_root *log,
3673 struct btrfs_path *path,
3674 u64 objectid, int max_key_type)
3675{
3676 int ret;
3677 struct btrfs_key key;
3678 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003679 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003680
3681 key.objectid = objectid;
3682 key.type = max_key_type;
3683 key.offset = (u64)-1;
3684
Chris Masond3977122009-01-05 21:25:51 -05003685 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003686 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003687 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003688 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003689 break;
3690
3691 if (path->slots[0] == 0)
3692 break;
3693
3694 path->slots[0]--;
3695 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3696 path->slots[0]);
3697
3698 if (found_key.objectid != objectid)
3699 break;
3700
Josef Bacik18ec90d2012-09-28 11:56:28 -04003701 found_key.offset = 0;
3702 found_key.type = 0;
3703 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3704 &start_slot);
3705
3706 ret = btrfs_del_items(trans, log, path, start_slot,
3707 path->slots[0] - start_slot + 1);
3708 /*
3709 * If start slot isn't 0 then we don't need to re-search, we've
3710 * found the last guy with the objectid in this tree.
3711 */
3712 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003713 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003714 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003715 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003716 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003717 if (ret > 0)
3718 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003719 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003720}
3721
Josef Bacik94edf4a2012-09-25 14:56:25 -04003722static void fill_inode_item(struct btrfs_trans_handle *trans,
3723 struct extent_buffer *leaf,
3724 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003725 struct inode *inode, int log_inode_only,
3726 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003727{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003728 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003729
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003730 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003731
3732 if (log_inode_only) {
3733 /* set the generation to zero so the recover code
3734 * can tell the difference between an logging
3735 * just to say 'this inode exists' and a logging
3736 * to say 'update this inode with these values'
3737 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003738 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003739 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003740 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003741 btrfs_set_token_inode_generation(leaf, item,
3742 BTRFS_I(inode)->generation,
3743 &token);
3744 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003745 }
3746
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003747 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3748 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3749 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3750 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3751
David Sterbaa937b972014-12-12 17:39:12 +01003752 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003753 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003754 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003755 inode->i_atime.tv_nsec, &token);
3756
David Sterbaa937b972014-12-12 17:39:12 +01003757 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003758 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003759 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003760 inode->i_mtime.tv_nsec, &token);
3761
David Sterbaa937b972014-12-12 17:39:12 +01003762 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003763 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003764 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003765 inode->i_ctime.tv_nsec, &token);
3766
3767 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3768 &token);
3769
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003770 btrfs_set_token_inode_sequence(leaf, item,
3771 inode_peek_iversion(inode), &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003772 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3773 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3774 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3775 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003776}
3777
Josef Bacika95249b2012-10-11 16:17:34 -04003778static int log_inode_item(struct btrfs_trans_handle *trans,
3779 struct btrfs_root *log, struct btrfs_path *path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003780 struct btrfs_inode *inode)
Josef Bacika95249b2012-10-11 16:17:34 -04003781{
3782 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003783 int ret;
3784
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003785 ret = btrfs_insert_empty_item(trans, log, path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003786 &inode->location, sizeof(*inode_item));
Josef Bacika95249b2012-10-11 16:17:34 -04003787 if (ret && ret != -EEXIST)
3788 return ret;
3789 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3790 struct btrfs_inode_item);
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003791 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3792 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003793 btrfs_release_path(path);
3794 return 0;
3795}
3796
Chris Mason31ff1cd2008-09-11 16:17:57 -04003797static noinline int copy_items(struct btrfs_trans_handle *trans,
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003798 struct btrfs_inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003799 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003800 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003801 int start_slot, int nr, int inode_only,
3802 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003803{
David Sterba3ffbd682018-06-29 10:56:42 +02003804 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003805 unsigned long src_offset;
3806 unsigned long dst_offset;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003807 struct btrfs_root *log = inode->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003808 struct btrfs_file_extent_item *extent;
3809 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003810 struct extent_buffer *src = src_path->nodes[0];
3811 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003812 int ret;
3813 struct btrfs_key *ins_keys;
3814 u32 *ins_sizes;
3815 char *ins_data;
3816 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003817 struct list_head ordered_sums;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003818 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003819 bool has_extents = false;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003820 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003821 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003822
3823 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003824
3825 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3826 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003827 if (!ins_data)
3828 return -ENOMEM;
3829
Josef Bacik16e75492013-10-22 12:18:51 -04003830 first_key.objectid = (u64)-1;
3831
Chris Mason31ff1cd2008-09-11 16:17:57 -04003832 ins_sizes = (u32 *)ins_data;
3833 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3834
3835 for (i = 0; i < nr; i++) {
3836 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3837 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3838 }
3839 ret = btrfs_insert_empty_items(trans, log, dst_path,
3840 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003841 if (ret) {
3842 kfree(ins_data);
3843 return ret;
3844 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003845
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003846 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003847 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3848 dst_path->slots[0]);
3849
3850 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3851
Matthias Kaehlcke0dde10b2017-07-27 14:30:23 -07003852 if (i == nr - 1)
Josef Bacik16e75492013-10-22 12:18:51 -04003853 last_key = ins_keys[i];
3854
Josef Bacik94edf4a2012-09-25 14:56:25 -04003855 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003856 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3857 dst_path->slots[0],
3858 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003859 fill_inode_item(trans, dst_path->nodes[0], inode_item,
David Sterbaf85b7372017-01-20 14:54:07 +01003860 &inode->vfs_inode,
3861 inode_only == LOG_INODE_EXISTS,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003862 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003863 } else {
3864 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3865 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003866 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003867
Josef Bacik16e75492013-10-22 12:18:51 -04003868 /*
3869 * We set need_find_last_extent here in case we know we were
3870 * processing other items and then walk into the first extent in
3871 * the inode. If we don't hit an extent then nothing changes,
3872 * we'll do the last search the next time around.
3873 */
3874 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3875 has_extents = true;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003876 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003877 first_key = ins_keys[i];
3878 } else {
3879 need_find_last_extent = false;
3880 }
3881
Chris Mason31ff1cd2008-09-11 16:17:57 -04003882 /* take a reference on file data extents so that truncates
3883 * or deletes of this inode don't have to relog the inode
3884 * again
3885 */
David Sterba962a2982014-06-04 18:41:45 +02003886 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003887 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003888 int found_type;
3889 extent = btrfs_item_ptr(src, start_slot + i,
3890 struct btrfs_file_extent_item);
3891
liubo8e531cd2011-05-06 10:36:09 +08003892 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3893 continue;
3894
Chris Mason31ff1cd2008-09-11 16:17:57 -04003895 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003896 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003897 u64 ds, dl, cs, cl;
3898 ds = btrfs_file_extent_disk_bytenr(src,
3899 extent);
3900 /* ds == 0 is a hole */
3901 if (ds == 0)
3902 continue;
3903
3904 dl = btrfs_file_extent_disk_num_bytes(src,
3905 extent);
3906 cs = btrfs_file_extent_offset(src, extent);
3907 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003908 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003909 if (btrfs_file_extent_compression(src,
3910 extent)) {
3911 cs = 0;
3912 cl = dl;
3913 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003914
3915 ret = btrfs_lookup_csums_range(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003916 fs_info->csum_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003917 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003918 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003919 if (ret) {
3920 btrfs_release_path(dst_path);
3921 kfree(ins_data);
3922 return ret;
3923 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003924 }
3925 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003926 }
3927
3928 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003929 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003930 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003931
3932 /*
3933 * we have to do this after the loop above to avoid changing the
3934 * log tree while trying to change the log tree.
3935 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003936 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003937 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003938 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3939 struct btrfs_ordered_sum,
3940 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003941 if (!ret)
3942 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003943 list_del(&sums->list);
3944 kfree(sums);
3945 }
Josef Bacik16e75492013-10-22 12:18:51 -04003946
3947 if (!has_extents)
3948 return ret;
3949
Filipe Manana74121f7c2014-08-07 12:00:44 +01003950 if (need_find_last_extent && *last_extent == first_key.offset) {
3951 /*
3952 * We don't have any leafs between our current one and the one
3953 * we processed before that can have file extent items for our
3954 * inode (and have a generation number smaller than our current
3955 * transaction id).
3956 */
3957 need_find_last_extent = false;
3958 }
3959
Josef Bacik16e75492013-10-22 12:18:51 -04003960 /*
3961 * Because we use btrfs_search_forward we could skip leaves that were
3962 * not modified and then assume *last_extent is valid when it really
3963 * isn't. So back up to the previous leaf and read the end of the last
3964 * extent before we go and fill in holes.
3965 */
3966 if (need_find_last_extent) {
3967 u64 len;
3968
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003969 ret = btrfs_prev_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04003970 if (ret < 0)
3971 return ret;
3972 if (ret)
3973 goto fill_holes;
3974 if (src_path->slots[0])
3975 src_path->slots[0]--;
3976 src = src_path->nodes[0];
3977 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003978 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04003979 key.type != BTRFS_EXTENT_DATA_KEY)
3980 goto fill_holes;
3981 extent = btrfs_item_ptr(src, src_path->slots[0],
3982 struct btrfs_file_extent_item);
3983 if (btrfs_file_extent_type(src, extent) ==
3984 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08003985 len = btrfs_file_extent_ram_bytes(src, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003986 *last_extent = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003987 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04003988 } else {
3989 len = btrfs_file_extent_num_bytes(src, extent);
3990 *last_extent = key.offset + len;
3991 }
3992 }
3993fill_holes:
3994 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3995 * things could have happened
3996 *
3997 * 1) A merge could have happened, so we could currently be on a leaf
3998 * that holds what we were copying in the first place.
3999 * 2) A split could have happened, and now not all of the items we want
4000 * are on the same leaf.
4001 *
4002 * So we need to adjust how we search for holes, we need to drop the
4003 * path and re-search for the first extent key we found, and then walk
4004 * forward until we hit the last one we copied.
4005 */
4006 if (need_find_last_extent) {
4007 /* btrfs_prev_leaf could return 1 without releasing the path */
4008 btrfs_release_path(src_path);
David Sterbaf85b7372017-01-20 14:54:07 +01004009 ret = btrfs_search_slot(NULL, inode->root, &first_key,
4010 src_path, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004011 if (ret < 0)
4012 return ret;
4013 ASSERT(ret == 0);
4014 src = src_path->nodes[0];
4015 i = src_path->slots[0];
4016 } else {
4017 i = start_slot;
4018 }
4019
4020 /*
4021 * Ok so here we need to go through and fill in any holes we may have
4022 * to make sure that holes are punched for those areas in case they had
4023 * extents previously.
4024 */
4025 while (!done) {
4026 u64 offset, len;
4027 u64 extent_end;
4028
4029 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004030 ret = btrfs_next_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04004031 if (ret < 0)
4032 return ret;
4033 ASSERT(ret == 0);
4034 src = src_path->nodes[0];
4035 i = 0;
Filipe Manana8434ec42018-03-26 23:59:12 +01004036 need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04004037 }
4038
4039 btrfs_item_key_to_cpu(src, &key, i);
4040 if (!btrfs_comp_cpu_keys(&key, &last_key))
4041 done = true;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004042 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04004043 key.type != BTRFS_EXTENT_DATA_KEY) {
4044 i++;
4045 continue;
4046 }
4047 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
4048 if (btrfs_file_extent_type(src, extent) ==
4049 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004050 len = btrfs_file_extent_ram_bytes(src, extent);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004051 extent_end = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004052 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04004053 } else {
4054 len = btrfs_file_extent_num_bytes(src, extent);
4055 extent_end = key.offset + len;
4056 }
4057 i++;
4058
4059 if (*last_extent == key.offset) {
4060 *last_extent = extent_end;
4061 continue;
4062 }
4063 offset = *last_extent;
4064 len = key.offset - *last_extent;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004065 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01004066 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004067 if (ret)
4068 break;
Filipe Manana74121f7c2014-08-07 12:00:44 +01004069 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04004070 }
Filipe Manana4ee3fad2018-03-26 23:59:00 +01004071
4072 /*
4073 * Check if there is a hole between the last extent found in our leaf
4074 * and the first extent in the next leaf. If there is one, we need to
4075 * log an explicit hole so that at replay time we can punch the hole.
4076 */
4077 if (ret == 0 &&
4078 key.objectid == btrfs_ino(inode) &&
4079 key.type == BTRFS_EXTENT_DATA_KEY &&
4080 i == btrfs_header_nritems(src_path->nodes[0])) {
4081 ret = btrfs_next_leaf(inode->root, src_path);
4082 need_find_last_extent = true;
4083 if (ret > 0) {
4084 ret = 0;
4085 } else if (ret == 0) {
4086 btrfs_item_key_to_cpu(src_path->nodes[0], &key,
4087 src_path->slots[0]);
4088 if (key.objectid == btrfs_ino(inode) &&
4089 key.type == BTRFS_EXTENT_DATA_KEY &&
4090 *last_extent < key.offset) {
4091 const u64 len = key.offset - *last_extent;
4092
4093 ret = btrfs_insert_file_extent(trans, log,
4094 btrfs_ino(inode),
4095 *last_extent, 0,
4096 0, len, 0, len,
4097 0, 0, 0);
4098 }
4099 }
4100 }
Josef Bacik16e75492013-10-22 12:18:51 -04004101 /*
4102 * Need to let the callers know we dropped the path so they should
4103 * re-search.
4104 */
4105 if (!ret && need_find_last_extent)
4106 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004107 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004108}
4109
Josef Bacik5dc562c2012-08-17 13:14:17 -04004110static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
4111{
4112 struct extent_map *em1, *em2;
4113
4114 em1 = list_entry(a, struct extent_map, list);
4115 em2 = list_entry(b, struct extent_map, list);
4116
4117 if (em1->start < em2->start)
4118 return -1;
4119 else if (em1->start > em2->start)
4120 return 1;
4121 return 0;
4122}
4123
Josef Bacike7175a62018-05-23 11:58:34 -04004124static int log_extent_csums(struct btrfs_trans_handle *trans,
4125 struct btrfs_inode *inode,
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004126 struct btrfs_root *log_root,
Josef Bacike7175a62018-05-23 11:58:34 -04004127 const struct extent_map *em)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004128{
Josef Bacik2ab28f32012-10-12 15:27:49 -04004129 u64 csum_offset;
4130 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01004131 LIST_HEAD(ordered_sums);
4132 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004133
Josef Bacike7175a62018-05-23 11:58:34 -04004134 if (inode->flags & BTRFS_INODE_NODATASUM ||
4135 test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
Filipe Manana8407f552014-09-05 15:14:39 +01004136 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04004137 return 0;
4138
Josef Bacike7175a62018-05-23 11:58:34 -04004139 /* If we're compressed we have to save the entire range of csums. */
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004140 if (em->compress_type) {
4141 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004142 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004143 } else {
Josef Bacike7175a62018-05-23 11:58:34 -04004144 csum_offset = em->mod_start - em->start;
4145 csum_len = em->mod_len;
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004146 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004147
Josef Bacik70c8a912012-10-11 16:54:30 -04004148 /* block start is already adjusted for the file extent offset. */
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004149 ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
Josef Bacik70c8a912012-10-11 16:54:30 -04004150 em->block_start + csum_offset,
4151 em->block_start + csum_offset +
4152 csum_len - 1, &ordered_sums, 0);
4153 if (ret)
4154 return ret;
4155
4156 while (!list_empty(&ordered_sums)) {
4157 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4158 struct btrfs_ordered_sum,
4159 list);
4160 if (!ret)
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004161 ret = btrfs_csum_file_blocks(trans, log_root, sums);
Josef Bacik70c8a912012-10-11 16:54:30 -04004162 list_del(&sums->list);
4163 kfree(sums);
4164 }
4165
4166 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004167}
4168
Filipe Manana8407f552014-09-05 15:14:39 +01004169static int log_one_extent(struct btrfs_trans_handle *trans,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004170 struct btrfs_inode *inode, struct btrfs_root *root,
Filipe Manana8407f552014-09-05 15:14:39 +01004171 const struct extent_map *em,
4172 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004173 struct btrfs_log_ctx *ctx)
4174{
4175 struct btrfs_root *log = root->log_root;
4176 struct btrfs_file_extent_item *fi;
4177 struct extent_buffer *leaf;
4178 struct btrfs_map_token token;
4179 struct btrfs_key key;
4180 u64 extent_offset = em->start - em->orig_start;
4181 u64 block_len;
4182 int ret;
4183 int extent_inserted = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004184
Nikolay Borisova9ecb652018-06-20 17:26:42 +03004185 ret = log_extent_csums(trans, inode, log, em);
Filipe Manana8407f552014-09-05 15:14:39 +01004186 if (ret)
4187 return ret;
4188
Filipe Manana8407f552014-09-05 15:14:39 +01004189 btrfs_init_map_token(&token);
4190
Nikolay Borisov9d122622017-01-18 00:31:40 +02004191 ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
Filipe Manana8407f552014-09-05 15:14:39 +01004192 em->start + em->len, NULL, 0, 1,
4193 sizeof(*fi), &extent_inserted);
4194 if (ret)
4195 return ret;
4196
4197 if (!extent_inserted) {
Nikolay Borisov9d122622017-01-18 00:31:40 +02004198 key.objectid = btrfs_ino(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01004199 key.type = BTRFS_EXTENT_DATA_KEY;
4200 key.offset = em->start;
4201
4202 ret = btrfs_insert_empty_item(trans, log, path, &key,
4203 sizeof(*fi));
4204 if (ret)
4205 return ret;
4206 }
4207 leaf = path->nodes[0];
4208 fi = btrfs_item_ptr(leaf, path->slots[0],
4209 struct btrfs_file_extent_item);
4210
Josef Bacik50d9aa92014-11-21 14:52:38 -05004211 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004212 &token);
4213 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4214 btrfs_set_token_file_extent_type(leaf, fi,
4215 BTRFS_FILE_EXTENT_PREALLOC,
4216 &token);
4217 else
4218 btrfs_set_token_file_extent_type(leaf, fi,
4219 BTRFS_FILE_EXTENT_REG,
4220 &token);
4221
4222 block_len = max(em->block_len, em->orig_block_len);
4223 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4224 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4225 em->block_start,
4226 &token);
4227 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4228 &token);
4229 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4230 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4231 em->block_start -
4232 extent_offset, &token);
4233 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4234 &token);
4235 } else {
4236 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4237 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4238 &token);
4239 }
4240
4241 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4242 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4243 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4244 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4245 &token);
4246 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4247 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4248 btrfs_mark_buffer_dirty(leaf);
4249
4250 btrfs_release_path(path);
4251
4252 return ret;
4253}
4254
Filipe Manana31d11b82018-05-09 16:01:46 +01004255/*
4256 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4257 * lose them after doing a fast fsync and replaying the log. We scan the
4258 * subvolume's root instead of iterating the inode's extent map tree because
4259 * otherwise we can log incorrect extent items based on extent map conversion.
4260 * That can happen due to the fact that extent maps are merged when they
4261 * are not in the extent map tree's list of modified extents.
4262 */
4263static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4264 struct btrfs_inode *inode,
4265 struct btrfs_path *path)
4266{
4267 struct btrfs_root *root = inode->root;
4268 struct btrfs_key key;
4269 const u64 i_size = i_size_read(&inode->vfs_inode);
4270 const u64 ino = btrfs_ino(inode);
4271 struct btrfs_path *dst_path = NULL;
4272 u64 last_extent = (u64)-1;
4273 int ins_nr = 0;
4274 int start_slot;
4275 int ret;
4276
4277 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4278 return 0;
4279
4280 key.objectid = ino;
4281 key.type = BTRFS_EXTENT_DATA_KEY;
4282 key.offset = i_size;
4283 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4284 if (ret < 0)
4285 goto out;
4286
4287 while (true) {
4288 struct extent_buffer *leaf = path->nodes[0];
4289 int slot = path->slots[0];
4290
4291 if (slot >= btrfs_header_nritems(leaf)) {
4292 if (ins_nr > 0) {
4293 ret = copy_items(trans, inode, dst_path, path,
4294 &last_extent, start_slot,
4295 ins_nr, 1, 0);
4296 if (ret < 0)
4297 goto out;
4298 ins_nr = 0;
4299 }
4300 ret = btrfs_next_leaf(root, path);
4301 if (ret < 0)
4302 goto out;
4303 if (ret > 0) {
4304 ret = 0;
4305 break;
4306 }
4307 continue;
4308 }
4309
4310 btrfs_item_key_to_cpu(leaf, &key, slot);
4311 if (key.objectid > ino)
4312 break;
4313 if (WARN_ON_ONCE(key.objectid < ino) ||
4314 key.type < BTRFS_EXTENT_DATA_KEY ||
4315 key.offset < i_size) {
4316 path->slots[0]++;
4317 continue;
4318 }
4319 if (last_extent == (u64)-1) {
4320 last_extent = key.offset;
4321 /*
4322 * Avoid logging extent items logged in past fsync calls
4323 * and leading to duplicate keys in the log tree.
4324 */
4325 do {
4326 ret = btrfs_truncate_inode_items(trans,
4327 root->log_root,
4328 &inode->vfs_inode,
4329 i_size,
4330 BTRFS_EXTENT_DATA_KEY);
4331 } while (ret == -EAGAIN);
4332 if (ret)
4333 goto out;
4334 }
4335 if (ins_nr == 0)
4336 start_slot = slot;
4337 ins_nr++;
4338 path->slots[0]++;
4339 if (!dst_path) {
4340 dst_path = btrfs_alloc_path();
4341 if (!dst_path) {
4342 ret = -ENOMEM;
4343 goto out;
4344 }
4345 }
4346 }
4347 if (ins_nr > 0) {
4348 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4349 start_slot, ins_nr, 1, 0);
4350 if (ret > 0)
4351 ret = 0;
4352 }
4353out:
4354 btrfs_release_path(path);
4355 btrfs_free_path(dst_path);
4356 return ret;
4357}
4358
Josef Bacik5dc562c2012-08-17 13:14:17 -04004359static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4360 struct btrfs_root *root,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004361 struct btrfs_inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004362 struct btrfs_path *path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004363 struct btrfs_log_ctx *ctx,
4364 const u64 start,
4365 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004366{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004367 struct extent_map *em, *n;
4368 struct list_head extents;
Nikolay Borisov9d122622017-01-18 00:31:40 +02004369 struct extent_map_tree *tree = &inode->extent_tree;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004370 u64 logged_start, logged_end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004371 u64 test_gen;
4372 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004373 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004374
4375 INIT_LIST_HEAD(&extents);
4376
Nikolay Borisov9d122622017-01-18 00:31:40 +02004377 down_write(&inode->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004378 write_lock(&tree->lock);
4379 test_gen = root->fs_info->last_trans_committed;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004380 logged_start = start;
4381 logged_end = end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004382
4383 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4384 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004385 /*
4386 * Just an arbitrary number, this can be really CPU intensive
4387 * once we start getting a lot of extents, and really once we
4388 * have a bunch of extents we just want to commit since it will
4389 * be faster.
4390 */
4391 if (++num > 32768) {
4392 list_del_init(&tree->modified_extents);
4393 ret = -EFBIG;
4394 goto process;
4395 }
4396
Josef Bacik5dc562c2012-08-17 13:14:17 -04004397 if (em->generation <= test_gen)
4398 continue;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004399
Filipe Manana31d11b82018-05-09 16:01:46 +01004400 /* We log prealloc extents beyond eof later. */
4401 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4402 em->start >= i_size_read(&inode->vfs_inode))
4403 continue;
4404
Josef Bacik8c6c5922017-08-29 10:11:39 -04004405 if (em->start < logged_start)
4406 logged_start = em->start;
4407 if ((em->start + em->len - 1) > logged_end)
4408 logged_end = em->start + em->len - 1;
4409
Josef Bacikff44c6e2012-09-14 12:59:20 -04004410 /* Need a ref to keep it from getting evicted from cache */
Elena Reshetova490b54d2017-03-03 10:55:12 +02004411 refcount_inc(&em->refs);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004412 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004413 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004414 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004415 }
4416
4417 list_sort(NULL, &extents, extent_cmp);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004418process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004419 while (!list_empty(&extents)) {
4420 em = list_entry(extents.next, struct extent_map, list);
4421
4422 list_del_init(&em->list);
4423
4424 /*
4425 * If we had an error we just need to delete everybody from our
4426 * private list.
4427 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004428 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004429 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004430 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004431 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004432 }
4433
4434 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004435
Josef Bacika2120a42018-05-23 11:58:35 -04004436 ret = log_one_extent(trans, inode, root, em, path, ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004437 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004438 clear_em_logging(tree, em);
4439 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004440 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004441 WARN_ON(!list_empty(&extents));
4442 write_unlock(&tree->lock);
Nikolay Borisov9d122622017-01-18 00:31:40 +02004443 up_write(&inode->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004444
Josef Bacik5dc562c2012-08-17 13:14:17 -04004445 btrfs_release_path(path);
Filipe Manana31d11b82018-05-09 16:01:46 +01004446 if (!ret)
4447 ret = btrfs_log_prealloc_extents(trans, inode, path);
4448
Josef Bacik5dc562c2012-08-17 13:14:17 -04004449 return ret;
4450}
4451
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004452static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004453 struct btrfs_path *path, u64 *size_ret)
4454{
4455 struct btrfs_key key;
4456 int ret;
4457
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004458 key.objectid = btrfs_ino(inode);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004459 key.type = BTRFS_INODE_ITEM_KEY;
4460 key.offset = 0;
4461
4462 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4463 if (ret < 0) {
4464 return ret;
4465 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004466 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004467 } else {
4468 struct btrfs_inode_item *item;
4469
4470 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4471 struct btrfs_inode_item);
4472 *size_ret = btrfs_inode_size(path->nodes[0], item);
4473 }
4474
4475 btrfs_release_path(path);
4476 return 0;
4477}
4478
Filipe Manana36283bf2015-06-20 00:44:51 +01004479/*
4480 * At the moment we always log all xattrs. This is to figure out at log replay
4481 * time which xattrs must have their deletion replayed. If a xattr is missing
4482 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4483 * because if a xattr is deleted, the inode is fsynced and a power failure
4484 * happens, causing the log to be replayed the next time the fs is mounted,
4485 * we want the xattr to not exist anymore (same behaviour as other filesystems
4486 * with a journal, ext3/4, xfs, f2fs, etc).
4487 */
4488static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4489 struct btrfs_root *root,
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004490 struct btrfs_inode *inode,
Filipe Manana36283bf2015-06-20 00:44:51 +01004491 struct btrfs_path *path,
4492 struct btrfs_path *dst_path)
4493{
4494 int ret;
4495 struct btrfs_key key;
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004496 const u64 ino = btrfs_ino(inode);
Filipe Manana36283bf2015-06-20 00:44:51 +01004497 int ins_nr = 0;
4498 int start_slot = 0;
4499
4500 key.objectid = ino;
4501 key.type = BTRFS_XATTR_ITEM_KEY;
4502 key.offset = 0;
4503
4504 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4505 if (ret < 0)
4506 return ret;
4507
4508 while (true) {
4509 int slot = path->slots[0];
4510 struct extent_buffer *leaf = path->nodes[0];
4511 int nritems = btrfs_header_nritems(leaf);
4512
4513 if (slot >= nritems) {
4514 if (ins_nr > 0) {
4515 u64 last_extent = 0;
4516
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004517 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004518 &last_extent, start_slot,
4519 ins_nr, 1, 0);
4520 /* can't be 1, extent items aren't processed */
4521 ASSERT(ret <= 0);
4522 if (ret < 0)
4523 return ret;
4524 ins_nr = 0;
4525 }
4526 ret = btrfs_next_leaf(root, path);
4527 if (ret < 0)
4528 return ret;
4529 else if (ret > 0)
4530 break;
4531 continue;
4532 }
4533
4534 btrfs_item_key_to_cpu(leaf, &key, slot);
4535 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4536 break;
4537
4538 if (ins_nr == 0)
4539 start_slot = slot;
4540 ins_nr++;
4541 path->slots[0]++;
4542 cond_resched();
4543 }
4544 if (ins_nr > 0) {
4545 u64 last_extent = 0;
4546
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004547 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004548 &last_extent, start_slot,
4549 ins_nr, 1, 0);
4550 /* can't be 1, extent items aren't processed */
4551 ASSERT(ret <= 0);
4552 if (ret < 0)
4553 return ret;
4554 }
4555
4556 return 0;
4557}
4558
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004559/*
4560 * If the no holes feature is enabled we need to make sure any hole between the
4561 * last extent and the i_size of our inode is explicitly marked in the log. This
4562 * is to make sure that doing something like:
4563 *
4564 * 1) create file with 128Kb of data
4565 * 2) truncate file to 64Kb
4566 * 3) truncate file to 256Kb
4567 * 4) fsync file
4568 * 5) <crash/power failure>
4569 * 6) mount fs and trigger log replay
4570 *
4571 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4572 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4573 * file correspond to a hole. The presence of explicit holes in a log tree is
4574 * what guarantees that log replay will remove/adjust file extent items in the
4575 * fs/subvol tree.
4576 *
4577 * Here we do not need to care about holes between extents, that is already done
4578 * by copy_items(). We also only need to do this in the full sync path, where we
4579 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4580 * lookup the list of modified extent maps and if any represents a hole, we
4581 * insert a corresponding extent representing a hole in the log tree.
4582 */
4583static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4584 struct btrfs_root *root,
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004585 struct btrfs_inode *inode,
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004586 struct btrfs_path *path)
4587{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004588 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004589 int ret;
4590 struct btrfs_key key;
4591 u64 hole_start;
4592 u64 hole_size;
4593 struct extent_buffer *leaf;
4594 struct btrfs_root *log = root->log_root;
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004595 const u64 ino = btrfs_ino(inode);
4596 const u64 i_size = i_size_read(&inode->vfs_inode);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004597
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004598 if (!btrfs_fs_incompat(fs_info, NO_HOLES))
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004599 return 0;
4600
4601 key.objectid = ino;
4602 key.type = BTRFS_EXTENT_DATA_KEY;
4603 key.offset = (u64)-1;
4604
4605 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4606 ASSERT(ret != 0);
4607 if (ret < 0)
4608 return ret;
4609
4610 ASSERT(path->slots[0] > 0);
4611 path->slots[0]--;
4612 leaf = path->nodes[0];
4613 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4614
4615 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4616 /* inode does not have any extents */
4617 hole_start = 0;
4618 hole_size = i_size;
4619 } else {
4620 struct btrfs_file_extent_item *extent;
4621 u64 len;
4622
4623 /*
4624 * If there's an extent beyond i_size, an explicit hole was
4625 * already inserted by copy_items().
4626 */
4627 if (key.offset >= i_size)
4628 return 0;
4629
4630 extent = btrfs_item_ptr(leaf, path->slots[0],
4631 struct btrfs_file_extent_item);
4632
4633 if (btrfs_file_extent_type(leaf, extent) ==
4634 BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004635 len = btrfs_file_extent_ram_bytes(leaf, extent);
Filipe Manana6399fb52017-07-28 15:22:36 +01004636 ASSERT(len == i_size ||
4637 (len == fs_info->sectorsize &&
4638 btrfs_file_extent_compression(leaf, extent) !=
4639 BTRFS_COMPRESS_NONE));
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004640 return 0;
4641 }
4642
4643 len = btrfs_file_extent_num_bytes(leaf, extent);
4644 /* Last extent goes beyond i_size, no need to log a hole. */
4645 if (key.offset + len > i_size)
4646 return 0;
4647 hole_start = key.offset + len;
4648 hole_size = i_size - hole_start;
4649 }
4650 btrfs_release_path(path);
4651
4652 /* Last extent ends at i_size. */
4653 if (hole_size == 0)
4654 return 0;
4655
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004656 hole_size = ALIGN(hole_size, fs_info->sectorsize);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004657 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4658 hole_size, 0, hole_size, 0, 0, 0);
4659 return ret;
4660}
4661
Filipe Manana56f23fd2016-03-30 23:37:21 +01004662/*
4663 * When we are logging a new inode X, check if it doesn't have a reference that
4664 * matches the reference from some other inode Y created in a past transaction
4665 * and that was renamed in the current transaction. If we don't do this, then at
4666 * log replay time we can lose inode Y (and all its files if it's a directory):
4667 *
4668 * mkdir /mnt/x
4669 * echo "hello world" > /mnt/x/foobar
4670 * sync
4671 * mv /mnt/x /mnt/y
4672 * mkdir /mnt/x # or touch /mnt/x
4673 * xfs_io -c fsync /mnt/x
4674 * <power fail>
4675 * mount fs, trigger log replay
4676 *
4677 * After the log replay procedure, we would lose the first directory and all its
4678 * files (file foobar).
4679 * For the case where inode Y is not a directory we simply end up losing it:
4680 *
4681 * echo "123" > /mnt/foo
4682 * sync
4683 * mv /mnt/foo /mnt/bar
4684 * echo "abc" > /mnt/foo
4685 * xfs_io -c fsync /mnt/foo
4686 * <power fail>
4687 *
4688 * We also need this for cases where a snapshot entry is replaced by some other
4689 * entry (file or directory) otherwise we end up with an unreplayable log due to
4690 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4691 * if it were a regular entry:
4692 *
4693 * mkdir /mnt/x
4694 * btrfs subvolume snapshot /mnt /mnt/x/snap
4695 * btrfs subvolume delete /mnt/x/snap
4696 * rmdir /mnt/x
4697 * mkdir /mnt/x
4698 * fsync /mnt/x or fsync some new file inside it
4699 * <power fail>
4700 *
4701 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4702 * the same transaction.
4703 */
4704static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4705 const int slot,
4706 const struct btrfs_key *key,
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004707 struct btrfs_inode *inode,
Filipe Manana44f714d2016-06-06 16:11:13 +01004708 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004709{
4710 int ret;
4711 struct btrfs_path *search_path;
4712 char *name = NULL;
4713 u32 name_len = 0;
4714 u32 item_size = btrfs_item_size_nr(eb, slot);
4715 u32 cur_offset = 0;
4716 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4717
4718 search_path = btrfs_alloc_path();
4719 if (!search_path)
4720 return -ENOMEM;
4721 search_path->search_commit_root = 1;
4722 search_path->skip_locking = 1;
4723
4724 while (cur_offset < item_size) {
4725 u64 parent;
4726 u32 this_name_len;
4727 u32 this_len;
4728 unsigned long name_ptr;
4729 struct btrfs_dir_item *di;
4730
4731 if (key->type == BTRFS_INODE_REF_KEY) {
4732 struct btrfs_inode_ref *iref;
4733
4734 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4735 parent = key->offset;
4736 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4737 name_ptr = (unsigned long)(iref + 1);
4738 this_len = sizeof(*iref) + this_name_len;
4739 } else {
4740 struct btrfs_inode_extref *extref;
4741
4742 extref = (struct btrfs_inode_extref *)(ptr +
4743 cur_offset);
4744 parent = btrfs_inode_extref_parent(eb, extref);
4745 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4746 name_ptr = (unsigned long)&extref->name;
4747 this_len = sizeof(*extref) + this_name_len;
4748 }
4749
4750 if (this_name_len > name_len) {
4751 char *new_name;
4752
4753 new_name = krealloc(name, this_name_len, GFP_NOFS);
4754 if (!new_name) {
4755 ret = -ENOMEM;
4756 goto out;
4757 }
4758 name_len = this_name_len;
4759 name = new_name;
4760 }
4761
4762 read_extent_buffer(eb, name, name_ptr, this_name_len);
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004763 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4764 parent, name, this_name_len, 0);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004765 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004766 struct btrfs_key di_key;
4767
4768 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4769 di, &di_key);
4770 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4771 ret = 1;
4772 *other_ino = di_key.objectid;
4773 } else {
4774 ret = -EAGAIN;
4775 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004776 goto out;
4777 } else if (IS_ERR(di)) {
4778 ret = PTR_ERR(di);
4779 goto out;
4780 }
4781 btrfs_release_path(search_path);
4782
4783 cur_offset += this_len;
4784 }
4785 ret = 0;
4786out:
4787 btrfs_free_path(search_path);
4788 kfree(name);
4789 return ret;
4790}
4791
Chris Masone02119d2008-09-05 16:13:11 -04004792/* log a single inode in the tree log.
4793 * At least one parent directory for this inode must exist in the tree
4794 * or be logged already.
4795 *
4796 * Any items from this inode changed by the current transaction are copied
4797 * to the log tree. An extra reference is taken on any extents in this
4798 * file, allowing us to avoid a whole pile of corner cases around logging
4799 * blocks that have been removed from the tree.
4800 *
4801 * See LOG_INODE_ALL and related defines for a description of what inode_only
4802 * does.
4803 *
4804 * This handles both files and directories.
4805 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004806static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004807 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004808 int inode_only,
4809 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004810 const loff_t end,
4811 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004812{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004813 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004814 struct btrfs_path *path;
4815 struct btrfs_path *dst_path;
4816 struct btrfs_key min_key;
4817 struct btrfs_key max_key;
4818 struct btrfs_root *log = root->log_root;
Josef Bacik16e75492013-10-22 12:18:51 -04004819 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004820 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004821 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004822 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004823 int ins_start_slot = 0;
4824 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004825 bool fast_search = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004826 u64 ino = btrfs_ino(inode);
4827 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004828 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004829 bool need_log_inode_item = true;
Filipe Manana9a8fca62018-05-11 16:42:42 +01004830 bool xattrs_logged = false;
Chris Masone02119d2008-09-05 16:13:11 -04004831
Chris Masone02119d2008-09-05 16:13:11 -04004832 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004833 if (!path)
4834 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004835 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004836 if (!dst_path) {
4837 btrfs_free_path(path);
4838 return -ENOMEM;
4839 }
Chris Masone02119d2008-09-05 16:13:11 -04004840
Li Zefan33345d012011-04-20 10:31:50 +08004841 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004842 min_key.type = BTRFS_INODE_ITEM_KEY;
4843 min_key.offset = 0;
4844
Li Zefan33345d012011-04-20 10:31:50 +08004845 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004846
Chris Mason12fcfd22009-03-24 10:24:20 -04004847
Josef Bacik5dc562c2012-08-17 13:14:17 -04004848 /* today the code can only do partial logging of directories */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004849 if (S_ISDIR(inode->vfs_inode.i_mode) ||
Miao Xie5269b672012-11-01 07:35:23 +00004850 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004851 &inode->runtime_flags) &&
Liu Bo781feef2016-11-30 16:20:25 -08004852 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004853 max_key.type = BTRFS_XATTR_ITEM_KEY;
4854 else
4855 max_key.type = (u8)-1;
4856 max_key.offset = (u64)-1;
4857
Filipe Manana2c2c4522015-01-13 16:40:04 +00004858 /*
4859 * Only run delayed items if we are a dir or a new file.
4860 * Otherwise commit the delayed inode only, which is needed in
4861 * order for the log replay code to mark inodes for link count
4862 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4863 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004864 if (S_ISDIR(inode->vfs_inode.i_mode) ||
4865 inode->generation > fs_info->last_trans_committed)
4866 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004867 else
Nikolay Borisova59108a2017-01-18 00:31:48 +02004868 ret = btrfs_commit_inode_delayed_inode(inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004869
4870 if (ret) {
4871 btrfs_free_path(path);
4872 btrfs_free_path(dst_path);
4873 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004874 }
4875
Liu Bo781feef2016-11-30 16:20:25 -08004876 if (inode_only == LOG_OTHER_INODE) {
4877 inode_only = LOG_INODE_EXISTS;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004878 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
Liu Bo781feef2016-11-30 16:20:25 -08004879 } else {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004880 mutex_lock(&inode->log_mutex);
Liu Bo781feef2016-11-30 16:20:25 -08004881 }
Chris Masone02119d2008-09-05 16:13:11 -04004882
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004883 /*
Chris Masone02119d2008-09-05 16:13:11 -04004884 * a brute force approach to making sure we get the most uptodate
4885 * copies of everything.
4886 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004887 if (S_ISDIR(inode->vfs_inode.i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04004888 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4889
Filipe Manana4f764e52015-02-23 19:53:35 +00004890 if (inode_only == LOG_INODE_EXISTS)
4891 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004892 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004893 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004894 if (inode_only == LOG_INODE_EXISTS) {
4895 /*
4896 * Make sure the new inode item we write to the log has
4897 * the same isize as the current one (if it exists).
4898 * This is necessary to prevent data loss after log
4899 * replay, and also to prevent doing a wrong expanding
4900 * truncate - for e.g. create file, write 4K into offset
4901 * 0, fsync, write 4K into offset 4096, add hard link,
4902 * fsync some other file (to sync log), power fail - if
4903 * we use the inode's current i_size, after log replay
4904 * we get a 8Kb file, with the last 4Kb extent as a hole
4905 * (zeroes), as if an expanding truncate happened,
4906 * instead of getting a file of 4Kb only.
4907 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004908 err = logged_inode_size(log, inode, path, &logged_isize);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004909 if (err)
4910 goto out_unlock;
4911 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004912 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004913 &inode->runtime_flags)) {
Filipe Mananaa7429942015-02-13 16:56:14 +00004914 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004915 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004916 ret = drop_objectid_items(trans, log, path, ino,
4917 max_key.type);
4918 } else {
4919 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004920 &inode->runtime_flags);
Filipe Mananaa7429942015-02-13 16:56:14 +00004921 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004922 &inode->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004923 while(1) {
4924 ret = btrfs_truncate_inode_items(trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004925 log, &inode->vfs_inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08004926 if (ret != -EAGAIN)
4927 break;
4928 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004929 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004930 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004931 &inode->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004932 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004933 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004934 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004935 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004936 ret = drop_objectid_items(trans, log, path, ino,
4937 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004938 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004939 if (inode_only == LOG_INODE_ALL)
4940 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004941 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004942 }
Josef Bacika95249b2012-10-11 16:17:34 -04004943
Chris Masone02119d2008-09-05 16:13:11 -04004944 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004945 if (ret) {
4946 err = ret;
4947 goto out_unlock;
4948 }
Chris Masone02119d2008-09-05 16:13:11 -04004949
Chris Masond3977122009-01-05 21:25:51 -05004950 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004951 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004952 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004953 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004954 if (ret < 0) {
4955 err = ret;
4956 goto out_unlock;
4957 }
Chris Masone02119d2008-09-05 16:13:11 -04004958 if (ret != 0)
4959 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004960again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004961 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004962 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004963 break;
4964 if (min_key.type > max_key.type)
4965 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004966
Filipe Mananae4545de2015-06-17 12:49:23 +01004967 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4968 need_log_inode_item = false;
4969
Filipe Manana56f23fd2016-03-30 23:37:21 +01004970 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4971 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
Nikolay Borisova59108a2017-01-18 00:31:48 +02004972 inode->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004973 u64 other_ino = 0;
4974
Filipe Manana56f23fd2016-03-30 23:37:21 +01004975 ret = btrfs_check_ref_name_override(path->nodes[0],
Nikolay Borisova59108a2017-01-18 00:31:48 +02004976 path->slots[0], &min_key, inode,
4977 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004978 if (ret < 0) {
4979 err = ret;
4980 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004981 } else if (ret > 0 && ctx &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004982 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004983 struct btrfs_key inode_key;
4984 struct inode *other_inode;
4985
4986 if (ins_nr > 0) {
4987 ins_nr++;
4988 } else {
4989 ins_nr = 1;
4990 ins_start_slot = path->slots[0];
4991 }
Nikolay Borisova59108a2017-01-18 00:31:48 +02004992 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana44f714d2016-06-06 16:11:13 +01004993 &last_extent, ins_start_slot,
4994 ins_nr, inode_only,
4995 logged_isize);
4996 if (ret < 0) {
4997 err = ret;
4998 goto out_unlock;
4999 }
5000 ins_nr = 0;
5001 btrfs_release_path(path);
5002 inode_key.objectid = other_ino;
5003 inode_key.type = BTRFS_INODE_ITEM_KEY;
5004 inode_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005005 other_inode = btrfs_iget(fs_info->sb,
Filipe Manana44f714d2016-06-06 16:11:13 +01005006 &inode_key, root,
5007 NULL);
5008 /*
5009 * If the other inode that had a conflicting dir
5010 * entry was deleted in the current transaction,
5011 * we don't need to do more work nor fallback to
5012 * a transaction commit.
5013 */
Al Viro8d9e2202018-07-29 23:04:46 +01005014 if (other_inode == ERR_PTR(-ENOENT)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01005015 goto next_key;
5016 } else if (IS_ERR(other_inode)) {
5017 err = PTR_ERR(other_inode);
5018 goto out_unlock;
5019 }
5020 /*
5021 * We are safe logging the other inode without
5022 * acquiring its i_mutex as long as we log with
5023 * the LOG_INODE_EXISTS mode. We're safe against
5024 * concurrent renames of the other inode as well
5025 * because during a rename we pin the log and
5026 * update the log with the new name before we
5027 * unpin it.
5028 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005029 err = btrfs_log_inode(trans, root,
5030 BTRFS_I(other_inode),
5031 LOG_OTHER_INODE, 0, LLONG_MAX,
5032 ctx);
Filipe Manana44f714d2016-06-06 16:11:13 +01005033 iput(other_inode);
5034 if (err)
5035 goto out_unlock;
5036 else
5037 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01005038 }
5039 }
5040
Filipe Manana36283bf2015-06-20 00:44:51 +01005041 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5042 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
5043 if (ins_nr == 0)
5044 goto next_slot;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005045 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01005046 &last_extent, ins_start_slot,
5047 ins_nr, inode_only, logged_isize);
5048 if (ret < 0) {
5049 err = ret;
5050 goto out_unlock;
5051 }
5052 ins_nr = 0;
5053 if (ret) {
5054 btrfs_release_path(path);
5055 continue;
5056 }
5057 goto next_slot;
5058 }
5059
Chris Mason31ff1cd2008-09-11 16:17:57 -04005060 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5061 ins_nr++;
5062 goto next_slot;
5063 } else if (!ins_nr) {
5064 ins_start_slot = path->slots[0];
5065 ins_nr = 1;
5066 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04005067 }
5068
Nikolay Borisova59108a2017-01-18 00:31:48 +02005069 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005070 ins_start_slot, ins_nr, inode_only,
5071 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005072 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005073 err = ret;
5074 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02005075 }
5076 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04005077 ins_nr = 0;
5078 btrfs_release_path(path);
5079 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005080 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005081 ins_nr = 1;
5082 ins_start_slot = path->slots[0];
5083next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04005084
Chris Mason3a5f1d42008-09-11 15:53:37 -04005085 nritems = btrfs_header_nritems(path->nodes[0]);
5086 path->slots[0]++;
5087 if (path->slots[0] < nritems) {
5088 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
5089 path->slots[0]);
5090 goto again;
5091 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005092 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005093 ret = copy_items(trans, inode, dst_path, path,
Josef Bacik16e75492013-10-22 12:18:51 -04005094 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005095 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005096 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005097 err = ret;
5098 goto out_unlock;
5099 }
Josef Bacik16e75492013-10-22 12:18:51 -04005100 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005101 ins_nr = 0;
5102 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005103 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01005104next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005105 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04005106 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005107 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04005108 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005109 min_key.offset = 0;
5110 } else {
Chris Masone02119d2008-09-05 16:13:11 -04005111 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005112 }
Chris Masone02119d2008-09-05 16:13:11 -04005113 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005114 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005115 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005116 ins_start_slot, ins_nr, inode_only,
5117 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005118 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005119 err = ret;
5120 goto out_unlock;
5121 }
Josef Bacik16e75492013-10-22 12:18:51 -04005122 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005123 ins_nr = 0;
5124 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005125
Filipe Manana36283bf2015-06-20 00:44:51 +01005126 btrfs_release_path(path);
5127 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005128 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
Filipe Manana36283bf2015-06-20 00:44:51 +01005129 if (err)
5130 goto out_unlock;
Filipe Manana9a8fca62018-05-11 16:42:42 +01005131 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005132 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5133 btrfs_release_path(path);
5134 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005135 err = btrfs_log_trailing_hole(trans, root, inode, path);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005136 if (err)
5137 goto out_unlock;
5138 }
Josef Bacika95249b2012-10-11 16:17:34 -04005139log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04005140 btrfs_release_path(path);
5141 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01005142 if (need_log_inode_item) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005143 err = log_inode_item(trans, log, dst_path, inode);
Filipe Manana9a8fca62018-05-11 16:42:42 +01005144 if (!err && !xattrs_logged) {
5145 err = btrfs_log_all_xattrs(trans, root, inode, path,
5146 dst_path);
5147 btrfs_release_path(path);
5148 }
Filipe Mananae4545de2015-06-17 12:49:23 +01005149 if (err)
5150 goto out_unlock;
5151 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005152 if (fast_search) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005153 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Josef Bacika2120a42018-05-23 11:58:35 -04005154 ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005155 if (ret) {
5156 err = ret;
5157 goto out_unlock;
5158 }
Josef Bacikd006a042013-11-12 20:54:09 -05005159 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06005160 struct extent_map *em, *n;
5161
Filipe Manana49dae1b2014-09-06 22:34:39 +01005162 write_lock(&em_tree->lock);
5163 /*
5164 * We can't just remove every em if we're called for a ranged
5165 * fsync - that is, one that doesn't cover the whole possible
5166 * file range (0 to LLONG_MAX). This is because we can have
5167 * em's that fall outside the range we're logging and therefore
5168 * their ordered operations haven't completed yet
5169 * (btrfs_finish_ordered_io() not invoked yet). This means we
5170 * didn't get their respective file extent item in the fs/subvol
5171 * tree yet, and need to let the next fast fsync (one which
5172 * consults the list of modified extent maps) find the em so
5173 * that it logs a matching file extent item and waits for the
5174 * respective ordered operation to complete (if it's still
5175 * running).
5176 *
5177 * Removing every em outside the range we're logging would make
5178 * the next fast fsync not log their matching file extent items,
5179 * therefore making us lose data after a log replay.
5180 */
5181 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
5182 list) {
5183 const u64 mod_end = em->mod_start + em->mod_len - 1;
5184
5185 if (em->mod_start >= start && mod_end <= end)
5186 list_del_init(&em->list);
5187 }
5188 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005189 }
5190
Nikolay Borisova59108a2017-01-18 00:31:48 +02005191 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5192 ret = log_directory_changes(trans, root, inode, path, dst_path,
5193 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005194 if (ret) {
5195 err = ret;
5196 goto out_unlock;
5197 }
Chris Masone02119d2008-09-05 16:13:11 -04005198 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005199
Nikolay Borisova59108a2017-01-18 00:31:48 +02005200 spin_lock(&inode->lock);
5201 inode->logged_trans = trans->transid;
5202 inode->last_log_commit = inode->last_sub_trans;
5203 spin_unlock(&inode->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005204out_unlock:
Nikolay Borisova59108a2017-01-18 00:31:48 +02005205 mutex_unlock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04005206
5207 btrfs_free_path(path);
5208 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005209 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005210}
5211
Chris Mason12fcfd22009-03-24 10:24:20 -04005212/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005213 * Check if we must fallback to a transaction commit when logging an inode.
5214 * This must be called after logging the inode and is used only in the context
5215 * when fsyncing an inode requires the need to log some other inode - in which
5216 * case we can't lock the i_mutex of each other inode we need to log as that
5217 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5218 * log inodes up or down in the hierarchy) or rename operations for example. So
5219 * we take the log_mutex of the inode after we have logged it and then check for
5220 * its last_unlink_trans value - this is safe because any task setting
5221 * last_unlink_trans must take the log_mutex and it must do this before it does
5222 * the actual unlink operation, so if we do this check before a concurrent task
5223 * sets last_unlink_trans it means we've logged a consistent version/state of
5224 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005225 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005226 * we logged the inode or it might have also done the unlink).
5227 */
5228static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005229 struct btrfs_inode *inode)
Filipe Manana2be63d52016-02-12 11:34:23 +00005230{
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005231 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana2be63d52016-02-12 11:34:23 +00005232 bool ret = false;
5233
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005234 mutex_lock(&inode->log_mutex);
5235 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
Filipe Manana2be63d52016-02-12 11:34:23 +00005236 /*
5237 * Make sure any commits to the log are forced to be full
5238 * commits.
5239 */
5240 btrfs_set_log_full_commit(fs_info, trans);
5241 ret = true;
5242 }
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005243 mutex_unlock(&inode->log_mutex);
Filipe Manana2be63d52016-02-12 11:34:23 +00005244
5245 return ret;
5246}
5247
5248/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005249 * follow the dentry parent pointers up the chain and see if any
5250 * of the directories in it require a full commit before they can
5251 * be logged. Returns zero if nothing special needs to be done or 1 if
5252 * a full commit is required.
5253 */
5254static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005255 struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005256 struct dentry *parent,
5257 struct super_block *sb,
5258 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005259{
Chris Mason12fcfd22009-03-24 10:24:20 -04005260 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005261 struct dentry *old_parent = NULL;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005262 struct btrfs_inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005263
Chris Masonaf4176b2009-03-24 10:24:31 -04005264 /*
5265 * for regular files, if its inode is already on disk, we don't
5266 * have to worry about the parents at all. This is because
5267 * we can use the last_unlink_trans field to record renames
5268 * and other fun in this file.
5269 */
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005270 if (S_ISREG(inode->vfs_inode.i_mode) &&
5271 inode->generation <= last_committed &&
5272 inode->last_unlink_trans <= last_committed)
5273 goto out;
Chris Masonaf4176b2009-03-24 10:24:31 -04005274
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005275 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005276 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005277 goto out;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005278 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005279 }
5280
5281 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005282 /*
5283 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005284 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005285 * logged_trans so that further down in the log code we don't
5286 * think this inode has already been logged.
5287 */
5288 if (inode != orig_inode)
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005289 inode->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005290 smp_mb();
5291
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005292 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005293 ret = 1;
5294 break;
5295 }
5296
Al Virofc640052016-04-10 01:33:30 -04005297 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005298 break;
5299
Filipe Manana44f714d2016-06-06 16:11:13 +01005300 if (IS_ROOT(parent)) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005301 inode = BTRFS_I(d_inode(parent));
5302 if (btrfs_must_commit_transaction(trans, inode))
Filipe Manana44f714d2016-06-06 16:11:13 +01005303 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005304 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005305 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005306
Josef Bacik6a912212010-11-20 09:48:00 +00005307 parent = dget_parent(parent);
5308 dput(old_parent);
5309 old_parent = parent;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005310 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005311
5312 }
Josef Bacik6a912212010-11-20 09:48:00 +00005313 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005314out:
Chris Masone02119d2008-09-05 16:13:11 -04005315 return ret;
5316}
5317
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005318struct btrfs_dir_list {
5319 u64 ino;
5320 struct list_head list;
5321};
5322
5323/*
5324 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5325 * details about the why it is needed.
5326 * This is a recursive operation - if an existing dentry corresponds to a
5327 * directory, that directory's new entries are logged too (same behaviour as
5328 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5329 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5330 * complains about the following circular lock dependency / possible deadlock:
5331 *
5332 * CPU0 CPU1
5333 * ---- ----
5334 * lock(&type->i_mutex_dir_key#3/2);
5335 * lock(sb_internal#2);
5336 * lock(&type->i_mutex_dir_key#3/2);
5337 * lock(&sb->s_type->i_mutex_key#14);
5338 *
5339 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5340 * sb_start_intwrite() in btrfs_start_transaction().
5341 * Not locking i_mutex of the inodes is still safe because:
5342 *
5343 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5344 * that while logging the inode new references (names) are added or removed
5345 * from the inode, leaving the logged inode item with a link count that does
5346 * not match the number of logged inode reference items. This is fine because
5347 * at log replay time we compute the real number of links and correct the
5348 * link count in the inode item (see replay_one_buffer() and
5349 * link_to_fixup_dir());
5350 *
5351 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5352 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5353 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5354 * has a size that doesn't match the sum of the lengths of all the logged
5355 * names. This does not result in a problem because if a dir_item key is
5356 * logged but its matching dir_index key is not logged, at log replay time we
5357 * don't use it to replay the respective name (see replay_one_name()). On the
5358 * other hand if only the dir_index key ends up being logged, the respective
5359 * name is added to the fs/subvol tree with both the dir_item and dir_index
5360 * keys created (see replay_one_name()).
5361 * The directory's inode item with a wrong i_size is not a problem as well,
5362 * since we don't use it at log replay time to set the i_size in the inode
5363 * item of the fs/subvol tree (see overwrite_item()).
5364 */
5365static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5366 struct btrfs_root *root,
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005367 struct btrfs_inode *start_inode,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005368 struct btrfs_log_ctx *ctx)
5369{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005370 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005371 struct btrfs_root *log = root->log_root;
5372 struct btrfs_path *path;
5373 LIST_HEAD(dir_list);
5374 struct btrfs_dir_list *dir_elem;
5375 int ret = 0;
5376
5377 path = btrfs_alloc_path();
5378 if (!path)
5379 return -ENOMEM;
5380
5381 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5382 if (!dir_elem) {
5383 btrfs_free_path(path);
5384 return -ENOMEM;
5385 }
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005386 dir_elem->ino = btrfs_ino(start_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005387 list_add_tail(&dir_elem->list, &dir_list);
5388
5389 while (!list_empty(&dir_list)) {
5390 struct extent_buffer *leaf;
5391 struct btrfs_key min_key;
5392 int nritems;
5393 int i;
5394
5395 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5396 list);
5397 if (ret)
5398 goto next_dir_inode;
5399
5400 min_key.objectid = dir_elem->ino;
5401 min_key.type = BTRFS_DIR_ITEM_KEY;
5402 min_key.offset = 0;
5403again:
5404 btrfs_release_path(path);
5405 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5406 if (ret < 0) {
5407 goto next_dir_inode;
5408 } else if (ret > 0) {
5409 ret = 0;
5410 goto next_dir_inode;
5411 }
5412
5413process_leaf:
5414 leaf = path->nodes[0];
5415 nritems = btrfs_header_nritems(leaf);
5416 for (i = path->slots[0]; i < nritems; i++) {
5417 struct btrfs_dir_item *di;
5418 struct btrfs_key di_key;
5419 struct inode *di_inode;
5420 struct btrfs_dir_list *new_dir_elem;
5421 int log_mode = LOG_INODE_EXISTS;
5422 int type;
5423
5424 btrfs_item_key_to_cpu(leaf, &min_key, i);
5425 if (min_key.objectid != dir_elem->ino ||
5426 min_key.type != BTRFS_DIR_ITEM_KEY)
5427 goto next_dir_inode;
5428
5429 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5430 type = btrfs_dir_type(leaf, di);
5431 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5432 type != BTRFS_FT_DIR)
5433 continue;
5434 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5435 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5436 continue;
5437
Robbie Koec125cf2016-10-28 10:48:26 +08005438 btrfs_release_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005439 di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005440 if (IS_ERR(di_inode)) {
5441 ret = PTR_ERR(di_inode);
5442 goto next_dir_inode;
5443 }
5444
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02005445 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005446 iput(di_inode);
Robbie Koec125cf2016-10-28 10:48:26 +08005447 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005448 }
5449
5450 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005451 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005452 log_mode = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005453 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005454 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005455 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005456 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005457 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005458 iput(di_inode);
5459 if (ret)
5460 goto next_dir_inode;
5461 if (ctx->log_new_dentries) {
5462 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5463 GFP_NOFS);
5464 if (!new_dir_elem) {
5465 ret = -ENOMEM;
5466 goto next_dir_inode;
5467 }
5468 new_dir_elem->ino = di_key.objectid;
5469 list_add_tail(&new_dir_elem->list, &dir_list);
5470 }
5471 break;
5472 }
5473 if (i == nritems) {
5474 ret = btrfs_next_leaf(log, path);
5475 if (ret < 0) {
5476 goto next_dir_inode;
5477 } else if (ret > 0) {
5478 ret = 0;
5479 goto next_dir_inode;
5480 }
5481 goto process_leaf;
5482 }
5483 if (min_key.offset < (u64)-1) {
5484 min_key.offset++;
5485 goto again;
5486 }
5487next_dir_inode:
5488 list_del(&dir_elem->list);
5489 kfree(dir_elem);
5490 }
5491
5492 btrfs_free_path(path);
5493 return ret;
5494}
5495
Filipe Manana18aa0922015-08-05 16:49:08 +01005496static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005497 struct btrfs_inode *inode,
Filipe Manana18aa0922015-08-05 16:49:08 +01005498 struct btrfs_log_ctx *ctx)
5499{
David Sterba3ffbd682018-06-29 10:56:42 +02005500 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Manana18aa0922015-08-05 16:49:08 +01005501 int ret;
5502 struct btrfs_path *path;
5503 struct btrfs_key key;
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005504 struct btrfs_root *root = inode->root;
5505 const u64 ino = btrfs_ino(inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005506
5507 path = btrfs_alloc_path();
5508 if (!path)
5509 return -ENOMEM;
5510 path->skip_locking = 1;
5511 path->search_commit_root = 1;
5512
5513 key.objectid = ino;
5514 key.type = BTRFS_INODE_REF_KEY;
5515 key.offset = 0;
5516 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5517 if (ret < 0)
5518 goto out;
5519
5520 while (true) {
5521 struct extent_buffer *leaf = path->nodes[0];
5522 int slot = path->slots[0];
5523 u32 cur_offset = 0;
5524 u32 item_size;
5525 unsigned long ptr;
5526
5527 if (slot >= btrfs_header_nritems(leaf)) {
5528 ret = btrfs_next_leaf(root, path);
5529 if (ret < 0)
5530 goto out;
5531 else if (ret > 0)
5532 break;
5533 continue;
5534 }
5535
5536 btrfs_item_key_to_cpu(leaf, &key, slot);
5537 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5538 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5539 break;
5540
5541 item_size = btrfs_item_size_nr(leaf, slot);
5542 ptr = btrfs_item_ptr_offset(leaf, slot);
5543 while (cur_offset < item_size) {
5544 struct btrfs_key inode_key;
5545 struct inode *dir_inode;
5546
5547 inode_key.type = BTRFS_INODE_ITEM_KEY;
5548 inode_key.offset = 0;
5549
5550 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5551 struct btrfs_inode_extref *extref;
5552
5553 extref = (struct btrfs_inode_extref *)
5554 (ptr + cur_offset);
5555 inode_key.objectid = btrfs_inode_extref_parent(
5556 leaf, extref);
5557 cur_offset += sizeof(*extref);
5558 cur_offset += btrfs_inode_extref_name_len(leaf,
5559 extref);
5560 } else {
5561 inode_key.objectid = key.offset;
5562 cur_offset = item_size;
5563 }
5564
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005565 dir_inode = btrfs_iget(fs_info->sb, &inode_key,
Filipe Manana18aa0922015-08-05 16:49:08 +01005566 root, NULL);
5567 /* If parent inode was deleted, skip it. */
5568 if (IS_ERR(dir_inode))
5569 continue;
5570
Filipe Manana657ed1a2016-04-06 17:11:56 +01005571 if (ctx)
5572 ctx->log_new_dentries = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005573 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
Filipe Manana18aa0922015-08-05 16:49:08 +01005574 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005575 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005576 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005577 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005578 if (!ret && ctx && ctx->log_new_dentries)
5579 ret = log_new_dir_dentries(trans, root,
David Sterbaf85b7372017-01-20 14:54:07 +01005580 BTRFS_I(dir_inode), ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005581 iput(dir_inode);
5582 if (ret)
5583 goto out;
5584 }
5585 path->slots[0]++;
5586 }
5587 ret = 0;
5588out:
5589 btrfs_free_path(path);
5590 return ret;
5591}
5592
Chris Masone02119d2008-09-05 16:13:11 -04005593/*
5594 * helper function around btrfs_log_inode to make sure newly created
5595 * parent directories also end up in the log. A minimal inode and backref
5596 * only logging is done of any parent directories that are older than
5597 * the last committed transaction
5598 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005599static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005600 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005601 struct dentry *parent,
5602 const loff_t start,
5603 const loff_t end,
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07005604 int inode_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005605 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005606{
Nikolay Borisovf8822742018-02-27 17:37:17 +02005607 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005608 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04005609 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005610 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005611 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005612 u64 last_committed = fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005613 bool log_dentries = false;
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005614 struct btrfs_inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005615
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005616 sb = inode->vfs_inode.i_sb;
Chris Mason12fcfd22009-03-24 10:24:20 -04005617
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005618 if (btrfs_test_opt(fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005619 ret = 1;
5620 goto end_no_trans;
5621 }
5622
Miao Xie995946d2014-04-02 19:51:06 +08005623 /*
5624 * The prev transaction commit doesn't complete, we need do
5625 * full commit by ourselves.
5626 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005627 if (fs_info->last_trans_log_full_commit >
5628 fs_info->last_trans_committed) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005629 ret = 1;
5630 goto end_no_trans;
5631 }
5632
Nikolay Borisovf8822742018-02-27 17:37:17 +02005633 if (btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005634 ret = 1;
5635 goto end_no_trans;
5636 }
5637
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005638 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
5639 last_committed);
Chris Mason12fcfd22009-03-24 10:24:20 -04005640 if (ret)
5641 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005642
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005643 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005644 ret = BTRFS_NO_LOG_SYNC;
5645 goto end_no_trans;
5646 }
5647
Miao Xie8b050d32014-02-20 18:08:58 +08005648 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005649 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005650 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005651
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005652 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005653 if (ret)
5654 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005655
Chris Masonaf4176b2009-03-24 10:24:31 -04005656 /*
5657 * for regular files, if its inode is already on disk, we don't
5658 * have to worry about the parents at all. This is because
5659 * we can use the last_unlink_trans field to record renames
5660 * and other fun in this file.
5661 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005662 if (S_ISREG(inode->vfs_inode.i_mode) &&
5663 inode->generation <= last_committed &&
5664 inode->last_unlink_trans <= last_committed) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005665 ret = 0;
5666 goto end_trans;
5667 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005668
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005669 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005670 log_dentries = true;
5671
Filipe Manana18aa0922015-08-05 16:49:08 +01005672 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005673 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005674 * inodes are fully logged. This is to prevent leaving dangling
5675 * directory index entries in directories that were our parents but are
5676 * not anymore. Not doing this results in old parent directory being
5677 * impossible to delete after log replay (rmdir will always fail with
5678 * error -ENOTEMPTY).
5679 *
5680 * Example 1:
5681 *
5682 * mkdir testdir
5683 * touch testdir/foo
5684 * ln testdir/foo testdir/bar
5685 * sync
5686 * unlink testdir/bar
5687 * xfs_io -c fsync testdir/foo
5688 * <power failure>
5689 * mount fs, triggers log replay
5690 *
5691 * If we don't log the parent directory (testdir), after log replay the
5692 * directory still has an entry pointing to the file inode using the bar
5693 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5694 * the file inode has a link count of 1.
5695 *
5696 * Example 2:
5697 *
5698 * mkdir testdir
5699 * touch foo
5700 * ln foo testdir/foo2
5701 * ln foo testdir/foo3
5702 * sync
5703 * unlink testdir/foo3
5704 * xfs_io -c fsync foo
5705 * <power failure>
5706 * mount fs, triggers log replay
5707 *
5708 * Similar as the first example, after log replay the parent directory
5709 * testdir still has an entry pointing to the inode file with name foo3
5710 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5711 * and has a link count of 2.
5712 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005713 if (inode->last_unlink_trans > last_committed) {
Filipe Manana18aa0922015-08-05 16:49:08 +01005714 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5715 if (ret)
5716 goto end_trans;
5717 }
5718
Chris Masond3977122009-01-05 21:25:51 -05005719 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005720 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005721 break;
5722
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005723 inode = BTRFS_I(d_inode(parent));
5724 if (root != inode->root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005725 break;
5726
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005727 if (inode->generation > last_committed) {
5728 ret = btrfs_log_inode(trans, root, inode,
5729 LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005730 if (ret)
5731 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005732 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005733 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005734 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005735
Josef Bacik6a912212010-11-20 09:48:00 +00005736 parent = dget_parent(parent);
5737 dput(old_parent);
5738 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005739 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005740 if (log_dentries)
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005741 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005742 else
5743 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005744end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005745 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005746 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005747 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005748 ret = 1;
5749 }
Miao Xie8b050d32014-02-20 18:08:58 +08005750
5751 if (ret)
5752 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005753 btrfs_end_log_trans(root);
5754end_no_trans:
5755 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005756}
5757
5758/*
5759 * it is not safe to log dentry if the chunk root has added new
5760 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5761 * If this returns 1, you must commit the transaction to safely get your
5762 * data on disk.
5763 */
5764int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Nikolay Borisove5b84f7a2018-02-27 17:37:18 +02005765 struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005766 const loff_t start,
5767 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005768 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005769{
Josef Bacik6a912212010-11-20 09:48:00 +00005770 struct dentry *parent = dget_parent(dentry);
5771 int ret;
5772
Nikolay Borisovf8822742018-02-27 17:37:17 +02005773 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
5774 start, end, LOG_INODE_ALL, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005775 dput(parent);
5776
5777 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005778}
5779
5780/*
5781 * should be called during mount to recover any replay any log trees
5782 * from the FS
5783 */
5784int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5785{
5786 int ret;
5787 struct btrfs_path *path;
5788 struct btrfs_trans_handle *trans;
5789 struct btrfs_key key;
5790 struct btrfs_key found_key;
5791 struct btrfs_key tmp_key;
5792 struct btrfs_root *log;
5793 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5794 struct walk_control wc = {
5795 .process_func = process_one_buffer,
5796 .stage = 0,
5797 };
5798
Chris Masone02119d2008-09-05 16:13:11 -04005799 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005800 if (!path)
5801 return -ENOMEM;
5802
Josef Bacikafcdd122016-09-02 15:40:02 -04005803 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005804
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005805 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005806 if (IS_ERR(trans)) {
5807 ret = PTR_ERR(trans);
5808 goto error;
5809 }
Chris Masone02119d2008-09-05 16:13:11 -04005810
5811 wc.trans = trans;
5812 wc.pin = 1;
5813
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005814 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005815 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005816 btrfs_handle_fs_error(fs_info, ret,
5817 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005818 goto error;
5819 }
Chris Masone02119d2008-09-05 16:13:11 -04005820
5821again:
5822 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5823 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005824 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005825
Chris Masond3977122009-01-05 21:25:51 -05005826 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005827 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005828
5829 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005830 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005831 "Couldn't find tree log root.");
5832 goto error;
5833 }
Chris Masone02119d2008-09-05 16:13:11 -04005834 if (ret > 0) {
5835 if (path->slots[0] == 0)
5836 break;
5837 path->slots[0]--;
5838 }
5839 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5840 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005841 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005842 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5843 break;
5844
Miao Xiecb517ea2013-05-15 07:48:19 +00005845 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005846 if (IS_ERR(log)) {
5847 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005848 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005849 "Couldn't read tree log root.");
5850 goto error;
5851 }
Chris Masone02119d2008-09-05 16:13:11 -04005852
5853 tmp_key.objectid = found_key.offset;
5854 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5855 tmp_key.offset = (u64)-1;
5856
5857 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005858 if (IS_ERR(wc.replay_dest)) {
5859 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005860 free_extent_buffer(log->node);
5861 free_extent_buffer(log->commit_root);
5862 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005863 btrfs_handle_fs_error(fs_info, ret,
5864 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005865 goto error;
5866 }
Chris Masone02119d2008-09-05 16:13:11 -04005867
Yan Zheng07d400a2009-01-06 11:42:00 -05005868 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005869 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005870 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005871
Josef Bacikb50c6e22013-04-25 15:55:30 -04005872 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005873 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5874 path);
Chris Masone02119d2008-09-05 16:13:11 -04005875 }
Chris Masone02119d2008-09-05 16:13:11 -04005876
Liu Bo900c9982018-01-25 11:02:56 -07005877 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5878 struct btrfs_root *root = wc.replay_dest;
5879
5880 btrfs_release_path(path);
5881
5882 /*
5883 * We have just replayed everything, and the highest
5884 * objectid of fs roots probably has changed in case
5885 * some inode_item's got replayed.
5886 *
5887 * root->objectid_mutex is not acquired as log replay
5888 * could only happen during mount.
5889 */
5890 ret = btrfs_find_highest_objectid(root,
5891 &root->highest_objectid);
5892 }
5893
Chris Masone02119d2008-09-05 16:13:11 -04005894 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005895 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005896 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005897 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005898 kfree(log);
5899
Josef Bacikb50c6e22013-04-25 15:55:30 -04005900 if (ret)
5901 goto error;
5902
Chris Masone02119d2008-09-05 16:13:11 -04005903 if (found_key.offset == 0)
5904 break;
5905 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005906 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005907
5908 /* step one is to pin it all, step two is to replay just inodes */
5909 if (wc.pin) {
5910 wc.pin = 0;
5911 wc.process_func = replay_one_buffer;
5912 wc.stage = LOG_WALK_REPLAY_INODES;
5913 goto again;
5914 }
5915 /* step three is to replay everything */
5916 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5917 wc.stage++;
5918 goto again;
5919 }
5920
5921 btrfs_free_path(path);
5922
Josef Bacikabefa552013-04-24 16:40:05 -04005923 /* step 4: commit the transaction, which also unpins the blocks */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005924 ret = btrfs_commit_transaction(trans);
Josef Bacikabefa552013-04-24 16:40:05 -04005925 if (ret)
5926 return ret;
5927
Chris Masone02119d2008-09-05 16:13:11 -04005928 free_extent_buffer(log_root_tree->node);
5929 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005930 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005931 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005932
Josef Bacikabefa552013-04-24 16:40:05 -04005933 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005934error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005935 if (wc.trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005936 btrfs_end_transaction(wc.trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005937 btrfs_free_path(path);
5938 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005939}
Chris Mason12fcfd22009-03-24 10:24:20 -04005940
5941/*
5942 * there are some corner cases where we want to force a full
5943 * commit instead of allowing a directory to be logged.
5944 *
5945 * They revolve around files there were unlinked from the directory, and
5946 * this function updates the parent directory so that a full commit is
5947 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005948 *
5949 * Must be called before the unlink operations (updates to the subvolume tree,
5950 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005951 */
5952void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005953 struct btrfs_inode *dir, struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005954 int for_rename)
5955{
5956 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005957 * when we're logging a file, if it hasn't been renamed
5958 * or unlinked, and its inode is fully committed on disk,
5959 * we don't have to worry about walking up the directory chain
5960 * to log its parents.
5961 *
5962 * So, we use the last_unlink_trans field to put this transid
5963 * into the file. When the file is logged we check it and
5964 * don't log the parents if the file is fully on disk.
5965 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005966 mutex_lock(&inode->log_mutex);
5967 inode->last_unlink_trans = trans->transid;
5968 mutex_unlock(&inode->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04005969
5970 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005971 * if this directory was already logged any new
5972 * names for this file/dir will get recorded
5973 */
5974 smp_mb();
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005975 if (dir->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04005976 return;
5977
5978 /*
5979 * if the inode we're about to unlink was logged,
5980 * the log will be properly updated for any new names
5981 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005982 if (inode->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04005983 return;
5984
5985 /*
5986 * when renaming files across directories, if the directory
5987 * there we're unlinking from gets fsync'd later on, there's
5988 * no way to find the destination directory later and fsync it
5989 * properly. So, we have to be conservative and force commits
5990 * so the new name gets discovered.
5991 */
5992 if (for_rename)
5993 goto record;
5994
5995 /* we can safely do the unlink without any special recording */
5996 return;
5997
5998record:
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005999 mutex_lock(&dir->log_mutex);
6000 dir->last_unlink_trans = trans->transid;
6001 mutex_unlock(&dir->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04006002}
6003
6004/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006005 * Make sure that if someone attempts to fsync the parent directory of a deleted
6006 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6007 * that after replaying the log tree of the parent directory's root we will not
6008 * see the snapshot anymore and at log replay time we will not see any log tree
6009 * corresponding to the deleted snapshot's root, which could lead to replaying
6010 * it after replaying the log tree of the parent directory (which would replay
6011 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00006012 *
6013 * Must be called before the actual snapshot destroy operation (updates to the
6014 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006015 */
6016void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
Nikolay Borisov43663552017-01-18 00:31:29 +02006017 struct btrfs_inode *dir)
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006018{
Nikolay Borisov43663552017-01-18 00:31:29 +02006019 mutex_lock(&dir->log_mutex);
6020 dir->last_unlink_trans = trans->transid;
6021 mutex_unlock(&dir->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006022}
6023
6024/*
Chris Mason12fcfd22009-03-24 10:24:20 -04006025 * Call this after adding a new name for a file and it will properly
6026 * update the log to reflect the new name.
6027 *
Filipe Mananad4682ba2018-06-11 19:24:28 +01006028 * @ctx can not be NULL when @sync_log is false, and should be NULL when it's
6029 * true (because it's not used).
6030 *
6031 * Return value depends on whether @sync_log is true or false.
6032 * When true: returns BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6033 * committed by the caller, and BTRFS_DONT_NEED_TRANS_COMMIT
6034 * otherwise.
6035 * When false: returns BTRFS_DONT_NEED_LOG_SYNC if the caller does not need to
6036 * to sync the log, BTRFS_NEED_LOG_SYNC if it needs to sync the log,
6037 * or BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6038 * committed (without attempting to sync the log).
Chris Mason12fcfd22009-03-24 10:24:20 -04006039 */
6040int btrfs_log_new_name(struct btrfs_trans_handle *trans,
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006041 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Filipe Mananad4682ba2018-06-11 19:24:28 +01006042 struct dentry *parent,
6043 bool sync_log, struct btrfs_log_ctx *ctx)
Chris Mason12fcfd22009-03-24 10:24:20 -04006044{
David Sterba3ffbd682018-06-29 10:56:42 +02006045 struct btrfs_fs_info *fs_info = trans->fs_info;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006046 int ret;
Chris Mason12fcfd22009-03-24 10:24:20 -04006047
6048 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04006049 * this will force the logging code to walk the dentry chain
6050 * up for the file
6051 */
Filipe Manana9a6509c2018-02-28 15:55:40 +00006052 if (!S_ISDIR(inode->vfs_inode.i_mode))
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006053 inode->last_unlink_trans = trans->transid;
Chris Masonaf4176b2009-03-24 10:24:31 -04006054
6055 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04006056 * if this inode hasn't been logged and directory we're renaming it
6057 * from hasn't been logged, we don't need to log it
6058 */
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006059 if (inode->logged_trans <= fs_info->last_trans_committed &&
6060 (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
Filipe Mananad4682ba2018-06-11 19:24:28 +01006061 return sync_log ? BTRFS_DONT_NEED_TRANS_COMMIT :
6062 BTRFS_DONT_NEED_LOG_SYNC;
Chris Mason12fcfd22009-03-24 10:24:20 -04006063
Filipe Mananad4682ba2018-06-11 19:24:28 +01006064 if (sync_log) {
6065 struct btrfs_log_ctx ctx2;
6066
6067 btrfs_init_log_ctx(&ctx2, &inode->vfs_inode);
6068 ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6069 LOG_INODE_EXISTS, &ctx2);
6070 if (ret == BTRFS_NO_LOG_SYNC)
6071 return BTRFS_DONT_NEED_TRANS_COMMIT;
6072 else if (ret)
6073 return BTRFS_NEED_TRANS_COMMIT;
6074
6075 ret = btrfs_sync_log(trans, inode->root, &ctx2);
6076 if (ret)
6077 return BTRFS_NEED_TRANS_COMMIT;
6078 return BTRFS_DONT_NEED_TRANS_COMMIT;
6079 }
6080
6081 ASSERT(ctx);
6082 ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6083 LOG_INODE_EXISTS, ctx);
6084 if (ret == BTRFS_NO_LOG_SYNC)
6085 return BTRFS_DONT_NEED_LOG_SYNC;
6086 else if (ret)
6087 return BTRFS_NEED_TRANS_COMMIT;
6088
6089 return BTRFS_NEED_LOG_SYNC;
Chris Mason12fcfd22009-03-24 10:24:20 -04006090}
6091