blob: d20dbbe214e5490b7140434b323d1eec2e6138b6 [file] [log] [blame]
Chris Masone02119d2008-09-05 16:13:11 -04001/*
2 * Copyright (C) 2008 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Miao Xiec6adc9c2013-05-28 10:05:39 +000021#include <linux/blkdev.h>
Josef Bacik5dc562c2012-08-17 13:14:17 -040022#include <linux/list_sort.h>
Jeff Laytonc7f88c42017-12-11 06:35:12 -050023#include <linux/iversion.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020024#include "ctree.h"
Miao Xie995946d2014-04-02 19:51:06 +080025#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040026#include "disk-io.h"
27#include "locking.h"
28#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070029#include "backref.h"
Anand Jainebb87652016-03-10 17:26:59 +080030#include "compression.h"
Qu Wenruodf2c95f2016-08-15 10:36:52 +080031#include "qgroup.h"
Liu Bo900c9982018-01-25 11:02:56 -070032#include "inode-map.h"
Chris Masone02119d2008-09-05 16:13:11 -040033
34/* magic values for the inode_only field in btrfs_log_inode:
35 *
36 * LOG_INODE_ALL means to log everything
37 * LOG_INODE_EXISTS means to log just enough to recreate the inode
38 * during log replay
39 */
40#define LOG_INODE_ALL 0
41#define LOG_INODE_EXISTS 1
Liu Bo781feef2016-11-30 16:20:25 -080042#define LOG_OTHER_INODE 2
Chris Masone02119d2008-09-05 16:13:11 -040043
44/*
Chris Mason12fcfd22009-03-24 10:24:20 -040045 * directory trouble cases
46 *
47 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
48 * log, we must force a full commit before doing an fsync of the directory
49 * where the unlink was done.
50 * ---> record transid of last unlink/rename per directory
51 *
52 * mkdir foo/some_dir
53 * normal commit
54 * rename foo/some_dir foo2/some_dir
55 * mkdir foo/some_dir
56 * fsync foo/some_dir/some_file
57 *
58 * The fsync above will unlink the original some_dir without recording
59 * it in its new location (foo2). After a crash, some_dir will be gone
60 * unless the fsync of some_file forces a full commit
61 *
62 * 2) we must log any new names for any file or dir that is in the fsync
63 * log. ---> check inode while renaming/linking.
64 *
65 * 2a) we must log any new names for any file or dir during rename
66 * when the directory they are being removed from was logged.
67 * ---> check inode and old parent dir during rename
68 *
69 * 2a is actually the more important variant. With the extra logging
70 * a crash might unlink the old name without recreating the new one
71 *
72 * 3) after a crash, we must go through any directories with a link count
73 * of zero and redo the rm -rf
74 *
75 * mkdir f1/foo
76 * normal commit
77 * rm -rf f1/foo
78 * fsync(f1)
79 *
80 * The directory f1 was fully removed from the FS, but fsync was never
81 * called on f1, only its parent dir. After a crash the rm -rf must
82 * be replayed. This must be able to recurse down the entire
83 * directory tree. The inode link count fixup code takes care of the
84 * ugly details.
85 */
86
87/*
Chris Masone02119d2008-09-05 16:13:11 -040088 * stages for the tree walking. The first
89 * stage (0) is to only pin down the blocks we find
90 * the second stage (1) is to make sure that all the inodes
91 * we find in the log are created in the subvolume.
92 *
93 * The last stage is to deal with directories and links and extents
94 * and all the other fun semantics
95 */
96#define LOG_WALK_PIN_ONLY 0
97#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040098#define LOG_WALK_REPLAY_DIR_INDEX 2
99#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -0400100
Chris Mason12fcfd22009-03-24 10:24:20 -0400101static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +0200102 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +0100103 int inode_only,
104 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +0100105 const loff_t end,
106 struct btrfs_log_ctx *ctx);
Yan Zhengec051c02009-01-05 15:43:42 -0500107static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
108 struct btrfs_root *root,
109 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400110static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
111 struct btrfs_root *root,
112 struct btrfs_root *log,
113 struct btrfs_path *path,
114 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400115
116/*
117 * tree logging is a special write ahead log used to make sure that
118 * fsyncs and O_SYNCs can happen without doing full tree commits.
119 *
120 * Full tree commits are expensive because they require commonly
121 * modified blocks to be recowed, creating many dirty pages in the
122 * extent tree an 4x-6x higher write load than ext3.
123 *
124 * Instead of doing a tree commit on every fsync, we use the
125 * key ranges and transaction ids to find items for a given file or directory
126 * that have changed in this transaction. Those items are copied into
127 * a special tree (one per subvolume root), that tree is written to disk
128 * and then the fsync is considered complete.
129 *
130 * After a crash, items are copied out of the log-tree back into the
131 * subvolume tree. Any file data extents found are recorded in the extent
132 * allocation tree, and the log-tree freed.
133 *
134 * The log tree is read three times, once to pin down all the extents it is
135 * using in ram and once, once to create all the inodes logged in the tree
136 * and once to do all the other items.
137 */
138
139/*
Chris Masone02119d2008-09-05 16:13:11 -0400140 * start a sub transaction and setup the log tree
141 * this increments the log tree writer count to make the people
142 * syncing the tree wait for us to finish
143 */
144static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800145 struct btrfs_root *root,
146 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400147{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400148 struct btrfs_fs_info *fs_info = root->fs_info;
Zhaolei34eb2a52015-08-17 18:44:45 +0800149 int ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500150
151 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800152
Yan Zheng7237f182009-01-21 12:54:03 -0500153 if (root->log_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400154 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800155 ret = -EAGAIN;
156 goto out;
157 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800158
Josef Bacikff782e02009-10-08 15:30:04 -0400159 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800160 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800161 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400162 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800163 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400164 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800165 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400166 mutex_lock(&fs_info->tree_log_mutex);
167 if (!fs_info->log_root_tree)
168 ret = btrfs_init_log_root_tree(trans, fs_info);
169 mutex_unlock(&fs_info->tree_log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800170 if (ret)
171 goto out;
Josef Bacikff782e02009-10-08 15:30:04 -0400172
Chris Masone02119d2008-09-05 16:13:11 -0400173 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400174 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800175 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800176
177 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
178 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400179 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800180
Miao Xie2ecb7922012-09-06 04:04:27 -0600181 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500182 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800183 if (ctx) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800184 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800185 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800186 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800187 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800188
Miao Xiee87ac132014-02-20 18:08:53 +0800189out:
Yan Zheng7237f182009-01-21 12:54:03 -0500190 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800191 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400192}
193
194/*
195 * returns 0 if there was a log transaction running and we were able
196 * to join, or returns -ENOENT if there were not transactions
197 * in progress
198 */
199static int join_running_log_trans(struct btrfs_root *root)
200{
201 int ret = -ENOENT;
202
203 smp_mb();
204 if (!root->log_root)
205 return -ENOENT;
206
Yan Zheng7237f182009-01-21 12:54:03 -0500207 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400208 if (root->log_root) {
209 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500210 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400211 }
Yan Zheng7237f182009-01-21 12:54:03 -0500212 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400213 return ret;
214}
215
216/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400217 * This either makes the current running log transaction wait
218 * until you call btrfs_end_log_trans() or it makes any future
219 * log transactions wait until you call btrfs_end_log_trans()
220 */
221int btrfs_pin_log_trans(struct btrfs_root *root)
222{
223 int ret = -ENOENT;
224
225 mutex_lock(&root->log_mutex);
226 atomic_inc(&root->log_writers);
227 mutex_unlock(&root->log_mutex);
228 return ret;
229}
230
231/*
Chris Masone02119d2008-09-05 16:13:11 -0400232 * indicate we're done making changes to the log tree
233 * and wake up anyone waiting to do a sync
234 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100235void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400236{
Yan Zheng7237f182009-01-21 12:54:03 -0500237 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +0100238 /*
239 * Implicit memory barrier after atomic_dec_and_test
240 */
Yan Zheng7237f182009-01-21 12:54:03 -0500241 if (waitqueue_active(&root->log_writer_wait))
242 wake_up(&root->log_writer_wait);
243 }
Chris Masone02119d2008-09-05 16:13:11 -0400244}
245
246
247/*
248 * the walk control struct is used to pass state down the chain when
249 * processing the log tree. The stage field tells us which part
250 * of the log tree processing we are currently doing. The others
251 * are state fields used for that specific part
252 */
253struct walk_control {
254 /* should we free the extent on disk when done? This is used
255 * at transaction commit time while freeing a log tree
256 */
257 int free;
258
259 /* should we write out the extent buffer? This is used
260 * while flushing the log tree to disk during a sync
261 */
262 int write;
263
264 /* should we wait for the extent buffer io to finish? Also used
265 * while flushing the log tree to disk for a sync
266 */
267 int wait;
268
269 /* pin only walk, we record which extents on disk belong to the
270 * log trees
271 */
272 int pin;
273
274 /* what stage of the replay code we're currently in */
275 int stage;
276
277 /* the root we are currently replaying */
278 struct btrfs_root *replay_dest;
279
280 /* the trans handle for the current replay */
281 struct btrfs_trans_handle *trans;
282
283 /* the function that gets used to process blocks we find in the
284 * tree. Note the extent_buffer might not be up to date when it is
285 * passed in, and it must be checked or read if you need the data
286 * inside it
287 */
288 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800289 struct walk_control *wc, u64 gen, int level);
Chris Masone02119d2008-09-05 16:13:11 -0400290};
291
292/*
293 * process_func used to pin down extents, write them or wait on them
294 */
295static int process_one_buffer(struct btrfs_root *log,
296 struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800297 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -0400298{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400299 struct btrfs_fs_info *fs_info = log->fs_info;
Josef Bacikb50c6e22013-04-25 15:55:30 -0400300 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400301
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400302 /*
303 * If this fs is mixed then we need to be able to process the leaves to
304 * pin down any logged extents, so we have to read the block.
305 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400306 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
Qu Wenruo581c1762018-03-29 09:08:11 +0800307 ret = btrfs_read_buffer(eb, gen, level, NULL);
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400308 if (ret)
309 return ret;
310 }
311
Josef Bacikb50c6e22013-04-25 15:55:30 -0400312 if (wc->pin)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400313 ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
314 eb->len);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400315
316 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400317 if (wc->pin && btrfs_header_level(eb) == 0)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400318 ret = btrfs_exclude_logged_extents(fs_info, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400319 if (wc->write)
320 btrfs_write_tree_block(eb);
321 if (wc->wait)
322 btrfs_wait_tree_block_writeback(eb);
323 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400324 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400325}
326
327/*
328 * Item overwrite used by replay and tree logging. eb, slot and key all refer
329 * to the src data we are copying out.
330 *
331 * root is the tree we are copying into, and path is a scratch
332 * path for use in this function (it should be released on entry and
333 * will be released on exit).
334 *
335 * If the key is already in the destination tree the existing item is
336 * overwritten. If the existing item isn't big enough, it is extended.
337 * If it is too large, it is truncated.
338 *
339 * If the key isn't in the destination yet, a new item is inserted.
340 */
341static noinline int overwrite_item(struct btrfs_trans_handle *trans,
342 struct btrfs_root *root,
343 struct btrfs_path *path,
344 struct extent_buffer *eb, int slot,
345 struct btrfs_key *key)
346{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400347 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400348 int ret;
349 u32 item_size;
350 u64 saved_i_size = 0;
351 int save_old_i_size = 0;
352 unsigned long src_ptr;
353 unsigned long dst_ptr;
354 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000355 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400356
357 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
358 overwrite_root = 1;
359
360 item_size = btrfs_item_size_nr(eb, slot);
361 src_ptr = btrfs_item_ptr_offset(eb, slot);
362
363 /* look for the key in the destination tree */
364 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000365 if (ret < 0)
366 return ret;
367
Chris Masone02119d2008-09-05 16:13:11 -0400368 if (ret == 0) {
369 char *src_copy;
370 char *dst_copy;
371 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
372 path->slots[0]);
373 if (dst_size != item_size)
374 goto insert;
375
376 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200377 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400378 return 0;
379 }
380 dst_copy = kmalloc(item_size, GFP_NOFS);
381 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000382 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200383 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000384 kfree(dst_copy);
385 kfree(src_copy);
386 return -ENOMEM;
387 }
Chris Masone02119d2008-09-05 16:13:11 -0400388
389 read_extent_buffer(eb, src_copy, src_ptr, item_size);
390
391 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
392 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
393 item_size);
394 ret = memcmp(dst_copy, src_copy, item_size);
395
396 kfree(dst_copy);
397 kfree(src_copy);
398 /*
399 * they have the same contents, just return, this saves
400 * us from cowing blocks in the destination tree and doing
401 * extra writes that may not have been done by a previous
402 * sync
403 */
404 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200405 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400406 return 0;
407 }
408
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000409 /*
410 * We need to load the old nbytes into the inode so when we
411 * replay the extents we've logged we get the right nbytes.
412 */
413 if (inode_item) {
414 struct btrfs_inode_item *item;
415 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400416 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000417
418 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
419 struct btrfs_inode_item);
420 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
421 item = btrfs_item_ptr(eb, slot,
422 struct btrfs_inode_item);
423 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400424
425 /*
426 * If this is a directory we need to reset the i_size to
427 * 0 so that we can set it up properly when replaying
428 * the rest of the items in this log.
429 */
430 mode = btrfs_inode_mode(eb, item);
431 if (S_ISDIR(mode))
432 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000433 }
434 } else if (inode_item) {
435 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400436 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000437
438 /*
439 * New inode, set nbytes to 0 so that the nbytes comes out
440 * properly when we replay the extents.
441 */
442 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
443 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400444
445 /*
446 * If this is a directory we need to reset the i_size to 0 so
447 * that we can set it up properly when replaying the rest of
448 * the items in this log.
449 */
450 mode = btrfs_inode_mode(eb, item);
451 if (S_ISDIR(mode))
452 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400453 }
454insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200455 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400456 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000457 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400458 ret = btrfs_insert_empty_item(trans, root, path,
459 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000460 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400461
462 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000463 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400464 u32 found_size;
465 found_size = btrfs_item_size_nr(path->nodes[0],
466 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100467 if (found_size > item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400468 btrfs_truncate_item(fs_info, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100469 else if (found_size < item_size)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400470 btrfs_extend_item(fs_info, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100471 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400472 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400473 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400474 }
475 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
476 path->slots[0]);
477
478 /* don't overwrite an existing inode if the generation number
479 * was logged as zero. This is done when the tree logging code
480 * is just logging an inode to make sure it exists after recovery.
481 *
482 * Also, don't overwrite i_size on directories during replay.
483 * log replay inserts and removes directory items based on the
484 * state of the tree found in the subvolume, and i_size is modified
485 * as it goes
486 */
487 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
488 struct btrfs_inode_item *src_item;
489 struct btrfs_inode_item *dst_item;
490
491 src_item = (struct btrfs_inode_item *)src_ptr;
492 dst_item = (struct btrfs_inode_item *)dst_ptr;
493
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000494 if (btrfs_inode_generation(eb, src_item) == 0) {
495 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000496 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000497
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000498 /*
499 * For regular files an ino_size == 0 is used only when
500 * logging that an inode exists, as part of a directory
501 * fsync, and the inode wasn't fsynced before. In this
502 * case don't set the size of the inode in the fs/subvol
503 * tree, otherwise we would be throwing valid data away.
504 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000505 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000506 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
507 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000508 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000509
510 btrfs_init_map_token(&token);
511 btrfs_set_token_inode_size(dst_eb, dst_item,
512 ino_size, &token);
513 }
Chris Masone02119d2008-09-05 16:13:11 -0400514 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000515 }
Chris Masone02119d2008-09-05 16:13:11 -0400516
517 if (overwrite_root &&
518 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
519 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
520 save_old_i_size = 1;
521 saved_i_size = btrfs_inode_size(path->nodes[0],
522 dst_item);
523 }
524 }
525
526 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
527 src_ptr, item_size);
528
529 if (save_old_i_size) {
530 struct btrfs_inode_item *dst_item;
531 dst_item = (struct btrfs_inode_item *)dst_ptr;
532 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
533 }
534
535 /* make sure the generation is filled in */
536 if (key->type == BTRFS_INODE_ITEM_KEY) {
537 struct btrfs_inode_item *dst_item;
538 dst_item = (struct btrfs_inode_item *)dst_ptr;
539 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
540 btrfs_set_inode_generation(path->nodes[0], dst_item,
541 trans->transid);
542 }
543 }
544no_copy:
545 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200546 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400547 return 0;
548}
549
550/*
551 * simple helper to read an inode off the disk from a given root
552 * This can only be called for subvolume roots and not for the log
553 */
554static noinline struct inode *read_one_inode(struct btrfs_root *root,
555 u64 objectid)
556{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400557 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400558 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400559
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400560 key.objectid = objectid;
561 key.type = BTRFS_INODE_ITEM_KEY;
562 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000563 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400564 if (IS_ERR(inode)) {
565 inode = NULL;
566 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400567 iput(inode);
568 inode = NULL;
569 }
570 return inode;
571}
572
573/* replays a single extent in 'eb' at 'slot' with 'key' into the
574 * subvolume 'root'. path is released on entry and should be released
575 * on exit.
576 *
577 * extents in the log tree have not been allocated out of the extent
578 * tree yet. So, this completes the allocation, taking a reference
579 * as required if the extent already exists or creating a new extent
580 * if it isn't in the extent allocation tree yet.
581 *
582 * The extent is inserted into the file, dropping any existing extents
583 * from the file that overlap the new one.
584 */
585static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
586 struct btrfs_root *root,
587 struct btrfs_path *path,
588 struct extent_buffer *eb, int slot,
589 struct btrfs_key *key)
590{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400591 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -0400592 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400593 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400594 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000595 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400596 struct btrfs_file_extent_item *item;
597 struct inode *inode = NULL;
598 unsigned long size;
599 int ret = 0;
600
601 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
602 found_type = btrfs_file_extent_type(eb, item);
603
Yan Zhengd899e052008-10-30 14:25:28 -0400604 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000605 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
606 nbytes = btrfs_file_extent_num_bytes(eb, item);
607 extent_end = start + nbytes;
608
609 /*
610 * We don't add to the inodes nbytes if we are prealloc or a
611 * hole.
612 */
613 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
614 nbytes = 0;
615 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800616 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000617 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400618 extent_end = ALIGN(start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400619 fs_info->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400620 } else {
621 ret = 0;
622 goto out;
623 }
624
625 inode = read_one_inode(root, key->objectid);
626 if (!inode) {
627 ret = -EIO;
628 goto out;
629 }
630
631 /*
632 * first check to see if we already have this extent in the
633 * file. This must be done before the btrfs_drop_extents run
634 * so we don't try to drop this extent.
635 */
David Sterbaf85b7372017-01-20 14:54:07 +0100636 ret = btrfs_lookup_file_extent(trans, root, path,
637 btrfs_ino(BTRFS_I(inode)), start, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400638
Yan Zhengd899e052008-10-30 14:25:28 -0400639 if (ret == 0 &&
640 (found_type == BTRFS_FILE_EXTENT_REG ||
641 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400642 struct btrfs_file_extent_item cmp1;
643 struct btrfs_file_extent_item cmp2;
644 struct btrfs_file_extent_item *existing;
645 struct extent_buffer *leaf;
646
647 leaf = path->nodes[0];
648 existing = btrfs_item_ptr(leaf, path->slots[0],
649 struct btrfs_file_extent_item);
650
651 read_extent_buffer(eb, &cmp1, (unsigned long)item,
652 sizeof(cmp1));
653 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
654 sizeof(cmp2));
655
656 /*
657 * we already have a pointer to this exact extent,
658 * we don't have to do anything
659 */
660 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200661 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400662 goto out;
663 }
664 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200665 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400666
667 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400668 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400669 if (ret)
670 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400671
Yan Zheng07d400a2009-01-06 11:42:00 -0500672 if (found_type == BTRFS_FILE_EXTENT_REG ||
673 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400674 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500675 unsigned long dest_offset;
676 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400677
Filipe Manana3168021c2017-02-01 14:58:02 +0000678 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
679 btrfs_fs_incompat(fs_info, NO_HOLES))
680 goto update_inode;
681
Yan Zheng07d400a2009-01-06 11:42:00 -0500682 ret = btrfs_insert_empty_item(trans, root, path, key,
683 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400684 if (ret)
685 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500686 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
687 path->slots[0]);
688 copy_extent_buffer(path->nodes[0], eb, dest_offset,
689 (unsigned long)item, sizeof(*item));
690
691 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
692 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
693 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400694 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500695
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800696 /*
697 * Manually record dirty extent, as here we did a shallow
698 * file extent item copy and skip normal backref update,
699 * but modifying extent tree all by ourselves.
700 * So need to manually record dirty extent for qgroup,
701 * as the owner of the file extent changed from log tree
702 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
703 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400704 ret = btrfs_qgroup_trace_extent(trans, fs_info,
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800705 btrfs_file_extent_disk_bytenr(eb, item),
706 btrfs_file_extent_disk_num_bytes(eb, item),
707 GFP_NOFS);
708 if (ret < 0)
709 goto out;
710
Yan Zheng07d400a2009-01-06 11:42:00 -0500711 if (ins.objectid > 0) {
712 u64 csum_start;
713 u64 csum_end;
714 LIST_HEAD(ordered_sums);
715 /*
716 * is this extent already allocated in the extent
717 * allocation tree? If so, just add a reference
718 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400719 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500720 ins.offset);
721 if (ret == 0) {
Josef Bacik84f7d8e2017-09-29 15:43:49 -0400722 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng07d400a2009-01-06 11:42:00 -0500723 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400724 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100725 key->objectid, offset);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400726 if (ret)
727 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500728 } else {
729 /*
730 * insert the extent pointer in the extent
731 * allocation tree
732 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400733 ret = btrfs_alloc_logged_file_extent(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400734 fs_info,
735 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400736 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400737 if (ret)
738 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500739 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200740 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500741
742 if (btrfs_file_extent_compression(eb, item)) {
743 csum_start = ins.objectid;
744 csum_end = csum_start + ins.offset;
745 } else {
746 csum_start = ins.objectid +
747 btrfs_file_extent_offset(eb, item);
748 csum_end = csum_start +
749 btrfs_file_extent_num_bytes(eb, item);
750 }
751
752 ret = btrfs_lookup_csums_range(root->log_root,
753 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100754 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400755 if (ret)
756 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100757 /*
758 * Now delete all existing cums in the csum root that
759 * cover our range. We do this because we can have an
760 * extent that is completely referenced by one file
761 * extent item and partially referenced by another
762 * file extent item (like after using the clone or
763 * extent_same ioctls). In this case if we end up doing
764 * the replay of the one that partially references the
765 * extent first, and we do not do the csum deletion
766 * below, we can get 2 csum items in the csum tree that
767 * overlap each other. For example, imagine our log has
768 * the two following file extent items:
769 *
770 * key (257 EXTENT_DATA 409600)
771 * extent data disk byte 12845056 nr 102400
772 * extent data offset 20480 nr 20480 ram 102400
773 *
774 * key (257 EXTENT_DATA 819200)
775 * extent data disk byte 12845056 nr 102400
776 * extent data offset 0 nr 102400 ram 102400
777 *
778 * Where the second one fully references the 100K extent
779 * that starts at disk byte 12845056, and the log tree
780 * has a single csum item that covers the entire range
781 * of the extent:
782 *
783 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
784 *
785 * After the first file extent item is replayed, the
786 * csum tree gets the following csum item:
787 *
788 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
789 *
790 * Which covers the 20K sub-range starting at offset 20K
791 * of our extent. Now when we replay the second file
792 * extent item, if we do not delete existing csum items
793 * that cover any of its blocks, we end up getting two
794 * csum items in our csum tree that overlap each other:
795 *
796 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
797 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
798 *
799 * Which is a problem, because after this anyone trying
800 * to lookup up for the checksum of any block of our
801 * extent starting at an offset of 40K or higher, will
802 * end up looking at the second csum item only, which
803 * does not contain the checksum for any block starting
804 * at offset 40K or higher of our extent.
805 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500806 while (!list_empty(&ordered_sums)) {
807 struct btrfs_ordered_sum *sums;
808 sums = list_entry(ordered_sums.next,
809 struct btrfs_ordered_sum,
810 list);
Josef Bacik36508602013-04-25 16:23:32 -0400811 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400812 ret = btrfs_del_csums(trans, fs_info,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400813 sums->bytenr,
814 sums->len);
Filipe Mananab84b8392015-08-19 11:09:40 +0100815 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400816 ret = btrfs_csum_file_blocks(trans,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400817 fs_info->csum_root, sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500818 list_del(&sums->list);
819 kfree(sums);
820 }
Josef Bacik36508602013-04-25 16:23:32 -0400821 if (ret)
822 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500823 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200824 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500825 }
826 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
827 /* inline extents are easy, we just overwrite them */
828 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400829 if (ret)
830 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500831 }
832
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000833 inode_add_bytes(inode, nbytes);
Filipe Manana3168021c2017-02-01 14:58:02 +0000834update_inode:
Tsutomu Itohb9959292012-06-25 21:25:22 -0600835 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400836out:
837 if (inode)
838 iput(inode);
839 return ret;
840}
841
842/*
843 * when cleaning up conflicts between the directory names in the
844 * subvolume, directory names in the log and directory names in the
845 * inode back references, we may have to unlink inodes from directories.
846 *
847 * This is a helper function to do the unlink of a specific directory
848 * item
849 */
850static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
851 struct btrfs_root *root,
852 struct btrfs_path *path,
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200853 struct btrfs_inode *dir,
Chris Masone02119d2008-09-05 16:13:11 -0400854 struct btrfs_dir_item *di)
855{
856 struct inode *inode;
857 char *name;
858 int name_len;
859 struct extent_buffer *leaf;
860 struct btrfs_key location;
861 int ret;
862
863 leaf = path->nodes[0];
864
865 btrfs_dir_item_key_to_cpu(leaf, di, &location);
866 name_len = btrfs_dir_name_len(leaf, di);
867 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000868 if (!name)
869 return -ENOMEM;
870
Chris Masone02119d2008-09-05 16:13:11 -0400871 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200872 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400873
874 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000875 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400876 ret = -EIO;
877 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000878 }
Chris Masone02119d2008-09-05 16:13:11 -0400879
Yan Zhengec051c02009-01-05 15:43:42 -0500880 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400881 if (ret)
882 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400883
Nikolay Borisov207e7d92017-01-18 00:31:45 +0200884 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
885 name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400886 if (ret)
887 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100888 else
Nikolay Borisove5c304e62018-02-07 17:55:43 +0200889 ret = btrfs_run_delayed_items(trans);
Josef Bacik36508602013-04-25 16:23:32 -0400890out:
891 kfree(name);
892 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400893 return ret;
894}
895
896/*
897 * helper function to see if a given name and sequence number found
898 * in an inode back reference are already in a directory and correctly
899 * point to this inode
900 */
901static noinline int inode_in_dir(struct btrfs_root *root,
902 struct btrfs_path *path,
903 u64 dirid, u64 objectid, u64 index,
904 const char *name, int name_len)
905{
906 struct btrfs_dir_item *di;
907 struct btrfs_key location;
908 int match = 0;
909
910 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
911 index, name, name_len, 0);
912 if (di && !IS_ERR(di)) {
913 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
914 if (location.objectid != objectid)
915 goto out;
916 } else
917 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200918 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400919
920 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
921 if (di && !IS_ERR(di)) {
922 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
923 if (location.objectid != objectid)
924 goto out;
925 } else
926 goto out;
927 match = 1;
928out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200929 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400930 return match;
931}
932
933/*
934 * helper function to check a log tree for a named back reference in
935 * an inode. This is used to decide if a back reference that is
936 * found in the subvolume conflicts with what we find in the log.
937 *
938 * inode backreferences may have multiple refs in a single item,
939 * during replay we process one reference at a time, and we don't
940 * want to delete valid links to a file from the subvolume if that
941 * link is also in the log.
942 */
943static noinline int backref_in_log(struct btrfs_root *log,
944 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700945 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000946 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400947{
948 struct btrfs_path *path;
949 struct btrfs_inode_ref *ref;
950 unsigned long ptr;
951 unsigned long ptr_end;
952 unsigned long name_ptr;
953 int found_name_len;
954 int item_size;
955 int ret;
956 int match = 0;
957
958 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000959 if (!path)
960 return -ENOMEM;
961
Chris Masone02119d2008-09-05 16:13:11 -0400962 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
963 if (ret != 0)
964 goto out;
965
Chris Masone02119d2008-09-05 16:13:11 -0400966 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700967
968 if (key->type == BTRFS_INODE_EXTREF_KEY) {
Filipe Manana1f250e92018-02-28 15:56:10 +0000969 if (btrfs_find_name_in_ext_backref(path->nodes[0],
970 path->slots[0],
971 ref_objectid,
Mark Fashehf1863732012-08-08 11:32:27 -0700972 name, namelen, NULL))
973 match = 1;
974
975 goto out;
976 }
977
978 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400979 ptr_end = ptr + item_size;
980 while (ptr < ptr_end) {
981 ref = (struct btrfs_inode_ref *)ptr;
982 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
983 if (found_name_len == namelen) {
984 name_ptr = (unsigned long)(ref + 1);
985 ret = memcmp_extent_buffer(path->nodes[0], name,
986 name_ptr, namelen);
987 if (ret == 0) {
988 match = 1;
989 goto out;
990 }
991 }
992 ptr = (unsigned long)(ref + 1) + found_name_len;
993 }
994out:
995 btrfs_free_path(path);
996 return match;
997}
998
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700999static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
1000 struct btrfs_root *root,
1001 struct btrfs_path *path,
1002 struct btrfs_root *log_root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001003 struct btrfs_inode *dir,
1004 struct btrfs_inode *inode,
Mark Fashehf1863732012-08-08 11:32:27 -07001005 u64 inode_objectid, u64 parent_objectid,
1006 u64 ref_index, char *name, int namelen,
1007 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001008{
1009 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001010 char *victim_name;
1011 int victim_name_len;
1012 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001013 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -07001014 struct btrfs_key search_key;
1015 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001016
Mark Fashehf1863732012-08-08 11:32:27 -07001017again:
1018 /* Search old style refs */
1019 search_key.objectid = inode_objectid;
1020 search_key.type = BTRFS_INODE_REF_KEY;
1021 search_key.offset = parent_objectid;
1022 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001023 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001024 struct btrfs_inode_ref *victim_ref;
1025 unsigned long ptr;
1026 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001027
1028 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001029
1030 /* are we trying to overwrite a back ref for the root directory
1031 * if so, just jump out, we're done
1032 */
Mark Fashehf1863732012-08-08 11:32:27 -07001033 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001034 return 1;
1035
1036 /* check all the names in this back reference to see
1037 * if they are in the log. if so, we allow them to stay
1038 * otherwise they must be unlinked as a conflict
1039 */
1040 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1041 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1042 while (ptr < ptr_end) {
1043 victim_ref = (struct btrfs_inode_ref *)ptr;
1044 victim_name_len = btrfs_inode_ref_name_len(leaf,
1045 victim_ref);
1046 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001047 if (!victim_name)
1048 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001049
1050 read_extent_buffer(leaf, victim_name,
1051 (unsigned long)(victim_ref + 1),
1052 victim_name_len);
1053
Mark Fashehf1863732012-08-08 11:32:27 -07001054 if (!backref_in_log(log_root, &search_key,
1055 parent_objectid,
1056 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001057 victim_name_len)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001058 inc_nlink(&inode->vfs_inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001059 btrfs_release_path(path);
1060
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001061 ret = btrfs_unlink_inode(trans, root, dir, inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001062 victim_name, victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001063 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001064 if (ret)
1065 return ret;
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001066 ret = btrfs_run_delayed_items(trans);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001067 if (ret)
1068 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001069 *search_done = 1;
1070 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001071 }
1072 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001073
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001074 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1075 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001076
1077 /*
1078 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001079 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001080 */
1081 *search_done = 1;
1082 }
1083 btrfs_release_path(path);
1084
Mark Fashehf1863732012-08-08 11:32:27 -07001085 /* Same search but for extended refs */
1086 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1087 inode_objectid, parent_objectid, 0,
1088 0);
1089 if (!IS_ERR_OR_NULL(extref)) {
1090 u32 item_size;
1091 u32 cur_offset = 0;
1092 unsigned long base;
1093 struct inode *victim_parent;
1094
1095 leaf = path->nodes[0];
1096
1097 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1098 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1099
1100 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001101 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001102
1103 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1104
1105 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1106 goto next;
1107
1108 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001109 if (!victim_name)
1110 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001111 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1112 victim_name_len);
1113
1114 search_key.objectid = inode_objectid;
1115 search_key.type = BTRFS_INODE_EXTREF_KEY;
1116 search_key.offset = btrfs_extref_hash(parent_objectid,
1117 victim_name,
1118 victim_name_len);
1119 ret = 0;
1120 if (!backref_in_log(log_root, &search_key,
1121 parent_objectid, victim_name,
1122 victim_name_len)) {
1123 ret = -ENOENT;
1124 victim_parent = read_one_inode(root,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001125 parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001126 if (victim_parent) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001127 inc_nlink(&inode->vfs_inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001128 btrfs_release_path(path);
1129
1130 ret = btrfs_unlink_inode(trans, root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001131 BTRFS_I(victim_parent),
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001132 inode,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02001133 victim_name,
1134 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001135 if (!ret)
1136 ret = btrfs_run_delayed_items(
Nikolay Borisove5c304e62018-02-07 17:55:43 +02001137 trans);
Mark Fashehf1863732012-08-08 11:32:27 -07001138 }
Mark Fashehf1863732012-08-08 11:32:27 -07001139 iput(victim_parent);
1140 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001141 if (ret)
1142 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001143 *search_done = 1;
1144 goto again;
1145 }
1146 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001147next:
1148 cur_offset += victim_name_len + sizeof(*extref);
1149 }
1150 *search_done = 1;
1151 }
1152 btrfs_release_path(path);
1153
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001154 /* look for a conflicting sequence number */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001155 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001156 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001157 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001158 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001159 if (ret)
1160 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001161 }
1162 btrfs_release_path(path);
1163
1164 /* look for a conflicing name */
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001165 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001166 name, namelen, 0);
1167 if (di && !IS_ERR(di)) {
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001168 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001169 if (ret)
1170 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001171 }
1172 btrfs_release_path(path);
1173
1174 return 0;
1175}
Chris Masone02119d2008-09-05 16:13:11 -04001176
Qu Wenruobae15d92017-11-08 08:54:26 +08001177static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1178 u32 *namelen, char **name, u64 *index,
1179 u64 *parent_objectid)
Mark Fashehf1863732012-08-08 11:32:27 -07001180{
1181 struct btrfs_inode_extref *extref;
1182
1183 extref = (struct btrfs_inode_extref *)ref_ptr;
1184
1185 *namelen = btrfs_inode_extref_name_len(eb, extref);
1186 *name = kmalloc(*namelen, GFP_NOFS);
1187 if (*name == NULL)
1188 return -ENOMEM;
1189
1190 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1191 *namelen);
1192
Filipe Manana1f250e92018-02-28 15:56:10 +00001193 if (index)
1194 *index = btrfs_inode_extref_index(eb, extref);
Mark Fashehf1863732012-08-08 11:32:27 -07001195 if (parent_objectid)
1196 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1197
1198 return 0;
1199}
1200
Qu Wenruobae15d92017-11-08 08:54:26 +08001201static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1202 u32 *namelen, char **name, u64 *index)
Mark Fashehf1863732012-08-08 11:32:27 -07001203{
1204 struct btrfs_inode_ref *ref;
1205
1206 ref = (struct btrfs_inode_ref *)ref_ptr;
1207
1208 *namelen = btrfs_inode_ref_name_len(eb, ref);
1209 *name = kmalloc(*namelen, GFP_NOFS);
1210 if (*name == NULL)
1211 return -ENOMEM;
1212
1213 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1214
Filipe Manana1f250e92018-02-28 15:56:10 +00001215 if (index)
1216 *index = btrfs_inode_ref_index(eb, ref);
Mark Fashehf1863732012-08-08 11:32:27 -07001217
1218 return 0;
1219}
1220
Chris Masone02119d2008-09-05 16:13:11 -04001221/*
Filipe Manana1f250e92018-02-28 15:56:10 +00001222 * Take an inode reference item from the log tree and iterate all names from the
1223 * inode reference item in the subvolume tree with the same key (if it exists).
1224 * For any name that is not in the inode reference item from the log tree, do a
1225 * proper unlink of that name (that is, remove its entry from the inode
1226 * reference item and both dir index keys).
1227 */
1228static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1229 struct btrfs_root *root,
1230 struct btrfs_path *path,
1231 struct btrfs_inode *inode,
1232 struct extent_buffer *log_eb,
1233 int log_slot,
1234 struct btrfs_key *key)
1235{
1236 int ret;
1237 unsigned long ref_ptr;
1238 unsigned long ref_end;
1239 struct extent_buffer *eb;
1240
1241again:
1242 btrfs_release_path(path);
1243 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1244 if (ret > 0) {
1245 ret = 0;
1246 goto out;
1247 }
1248 if (ret < 0)
1249 goto out;
1250
1251 eb = path->nodes[0];
1252 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1253 ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1254 while (ref_ptr < ref_end) {
1255 char *name = NULL;
1256 int namelen;
1257 u64 parent_id;
1258
1259 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1260 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1261 NULL, &parent_id);
1262 } else {
1263 parent_id = key->offset;
1264 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1265 NULL);
1266 }
1267 if (ret)
1268 goto out;
1269
1270 if (key->type == BTRFS_INODE_EXTREF_KEY)
1271 ret = btrfs_find_name_in_ext_backref(log_eb, log_slot,
1272 parent_id, name,
1273 namelen, NULL);
1274 else
1275 ret = btrfs_find_name_in_backref(log_eb, log_slot, name,
1276 namelen, NULL);
1277
1278 if (!ret) {
1279 struct inode *dir;
1280
1281 btrfs_release_path(path);
1282 dir = read_one_inode(root, parent_id);
1283 if (!dir) {
1284 ret = -ENOENT;
1285 kfree(name);
1286 goto out;
1287 }
1288 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
1289 inode, name, namelen);
1290 kfree(name);
1291 iput(dir);
1292 if (ret)
1293 goto out;
1294 goto again;
1295 }
1296
1297 kfree(name);
1298 ref_ptr += namelen;
1299 if (key->type == BTRFS_INODE_EXTREF_KEY)
1300 ref_ptr += sizeof(struct btrfs_inode_extref);
1301 else
1302 ref_ptr += sizeof(struct btrfs_inode_ref);
1303 }
1304 ret = 0;
1305 out:
1306 btrfs_release_path(path);
1307 return ret;
1308}
1309
1310/*
Chris Masone02119d2008-09-05 16:13:11 -04001311 * replay one inode back reference item found in the log tree.
1312 * eb, slot and key refer to the buffer and key found in the log tree.
1313 * root is the destination we are replaying into, and path is for temp
1314 * use by this function. (it should be released on return).
1315 */
1316static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1317 struct btrfs_root *root,
1318 struct btrfs_root *log,
1319 struct btrfs_path *path,
1320 struct extent_buffer *eb, int slot,
1321 struct btrfs_key *key)
1322{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001323 struct inode *dir = NULL;
1324 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001325 unsigned long ref_ptr;
1326 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001327 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001328 int namelen;
1329 int ret;
liuboc622ae62011-03-26 08:01:12 -04001330 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001331 int log_ref_ver = 0;
1332 u64 parent_objectid;
1333 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001334 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001335 int ref_struct_size;
1336
1337 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1338 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1339
1340 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1341 struct btrfs_inode_extref *r;
1342
1343 ref_struct_size = sizeof(struct btrfs_inode_extref);
1344 log_ref_ver = 1;
1345 r = (struct btrfs_inode_extref *)ref_ptr;
1346 parent_objectid = btrfs_inode_extref_parent(eb, r);
1347 } else {
1348 ref_struct_size = sizeof(struct btrfs_inode_ref);
1349 parent_objectid = key->offset;
1350 }
1351 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001352
Chris Masone02119d2008-09-05 16:13:11 -04001353 /*
1354 * it is possible that we didn't log all the parent directories
1355 * for a given inode. If we don't find the dir, just don't
1356 * copy the back ref in. The link count fixup code will take
1357 * care of the rest
1358 */
Mark Fashehf1863732012-08-08 11:32:27 -07001359 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001360 if (!dir) {
1361 ret = -ENOENT;
1362 goto out;
1363 }
Chris Masone02119d2008-09-05 16:13:11 -04001364
Mark Fashehf1863732012-08-08 11:32:27 -07001365 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001366 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001367 ret = -EIO;
1368 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001369 }
Chris Masone02119d2008-09-05 16:13:11 -04001370
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001371 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001372 if (log_ref_ver) {
Qu Wenruobae15d92017-11-08 08:54:26 +08001373 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1374 &ref_index, &parent_objectid);
Mark Fashehf1863732012-08-08 11:32:27 -07001375 /*
1376 * parent object can change from one array
1377 * item to another.
1378 */
1379 if (!dir)
1380 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001381 if (!dir) {
1382 ret = -ENOENT;
1383 goto out;
1384 }
Mark Fashehf1863732012-08-08 11:32:27 -07001385 } else {
Qu Wenruobae15d92017-11-08 08:54:26 +08001386 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1387 &ref_index);
Mark Fashehf1863732012-08-08 11:32:27 -07001388 }
1389 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001390 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001391
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001392 /* if we already have a perfect match, we're done */
David Sterbaf85b7372017-01-20 14:54:07 +01001393 if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1394 btrfs_ino(BTRFS_I(inode)), ref_index,
1395 name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001396 /*
1397 * look for a conflicting back reference in the
1398 * metadata. if we find one we have to unlink that name
1399 * of the file before we add our new link. Later on, we
1400 * overwrite any existing back reference, and we don't
1401 * want to create dangling pointers in the directory.
1402 */
Chris Masone02119d2008-09-05 16:13:11 -04001403
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001404 if (!search_done) {
1405 ret = __add_inode_ref(trans, root, path, log,
Nikolay Borisov94c91a12017-01-18 00:31:46 +02001406 BTRFS_I(dir),
David Sterbad75eefd2017-02-10 20:20:19 +01001407 BTRFS_I(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001408 inode_objectid,
1409 parent_objectid,
1410 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001411 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001412 if (ret) {
1413 if (ret == 1)
1414 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001415 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001416 }
Chris Masone02119d2008-09-05 16:13:11 -04001417 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001418
1419 /* insert our name */
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001420 ret = btrfs_add_link(trans, BTRFS_I(dir),
1421 BTRFS_I(inode),
1422 name, namelen, 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001423 if (ret)
1424 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001425
1426 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001427 }
liuboc622ae62011-03-26 08:01:12 -04001428
Mark Fashehf1863732012-08-08 11:32:27 -07001429 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001430 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001431 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001432 if (log_ref_ver) {
1433 iput(dir);
1434 dir = NULL;
1435 }
Chris Masone02119d2008-09-05 16:13:11 -04001436 }
Chris Masone02119d2008-09-05 16:13:11 -04001437
Filipe Manana1f250e92018-02-28 15:56:10 +00001438 /*
1439 * Before we overwrite the inode reference item in the subvolume tree
1440 * with the item from the log tree, we must unlink all names from the
1441 * parent directory that are in the subvolume's tree inode reference
1442 * item, otherwise we end up with an inconsistent subvolume tree where
1443 * dir index entries exist for a name but there is no inode reference
1444 * item with the same name.
1445 */
1446 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1447 key);
1448 if (ret)
1449 goto out;
1450
Chris Masone02119d2008-09-05 16:13:11 -04001451 /* finally write the back reference in the inode */
1452 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001453out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001454 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001455 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001456 iput(dir);
1457 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001458 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001459}
1460
Yan, Zhengc71bf092009-11-12 09:34:40 +00001461static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001462 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001463{
1464 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001465
David Sterba9c4f61f2015-01-02 19:12:57 +01001466 ret = btrfs_insert_orphan_item(trans, root, ino);
1467 if (ret == -EEXIST)
1468 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001469
Yan, Zhengc71bf092009-11-12 09:34:40 +00001470 return ret;
1471}
1472
Mark Fashehf1863732012-08-08 11:32:27 -07001473static int count_inode_extrefs(struct btrfs_root *root,
Nikolay Borisov36283652017-01-18 00:31:49 +02001474 struct btrfs_inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001475{
Mark Fashehf1863732012-08-08 11:32:27 -07001476 int ret = 0;
1477 int name_len;
1478 unsigned int nlink = 0;
1479 u32 item_size;
1480 u32 cur_offset = 0;
Nikolay Borisov36283652017-01-18 00:31:49 +02001481 u64 inode_objectid = btrfs_ino(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001482 u64 offset = 0;
1483 unsigned long ptr;
1484 struct btrfs_inode_extref *extref;
1485 struct extent_buffer *leaf;
1486
1487 while (1) {
1488 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1489 &extref, &offset);
1490 if (ret)
1491 break;
1492
1493 leaf = path->nodes[0];
1494 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1495 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001496 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001497
1498 while (cur_offset < item_size) {
1499 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1500 name_len = btrfs_inode_extref_name_len(leaf, extref);
1501
1502 nlink++;
1503
1504 cur_offset += name_len + sizeof(*extref);
1505 }
1506
1507 offset++;
1508 btrfs_release_path(path);
1509 }
1510 btrfs_release_path(path);
1511
Filipe Manana2c2c4522015-01-13 16:40:04 +00001512 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001513 return ret;
1514 return nlink;
1515}
1516
1517static int count_inode_refs(struct btrfs_root *root,
Nikolay Borisovf329e312017-01-18 00:31:50 +02001518 struct btrfs_inode *inode, struct btrfs_path *path)
Mark Fashehf1863732012-08-08 11:32:27 -07001519{
Chris Masone02119d2008-09-05 16:13:11 -04001520 int ret;
1521 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001522 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001523 unsigned long ptr;
1524 unsigned long ptr_end;
1525 int name_len;
Nikolay Borisovf329e312017-01-18 00:31:50 +02001526 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001527
Li Zefan33345d012011-04-20 10:31:50 +08001528 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001529 key.type = BTRFS_INODE_REF_KEY;
1530 key.offset = (u64)-1;
1531
Chris Masond3977122009-01-05 21:25:51 -05001532 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001533 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1534 if (ret < 0)
1535 break;
1536 if (ret > 0) {
1537 if (path->slots[0] == 0)
1538 break;
1539 path->slots[0]--;
1540 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001541process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001542 btrfs_item_key_to_cpu(path->nodes[0], &key,
1543 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001544 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001545 key.type != BTRFS_INODE_REF_KEY)
1546 break;
1547 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1548 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1549 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001550 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001551 struct btrfs_inode_ref *ref;
1552
1553 ref = (struct btrfs_inode_ref *)ptr;
1554 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1555 ref);
1556 ptr = (unsigned long)(ref + 1) + name_len;
1557 nlink++;
1558 }
1559
1560 if (key.offset == 0)
1561 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001562 if (path->slots[0] > 0) {
1563 path->slots[0]--;
1564 goto process_slot;
1565 }
Chris Masone02119d2008-09-05 16:13:11 -04001566 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001567 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001568 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001569 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001570
1571 return nlink;
1572}
1573
1574/*
1575 * There are a few corners where the link count of the file can't
1576 * be properly maintained during replay. So, instead of adding
1577 * lots of complexity to the log code, we just scan the backrefs
1578 * for any file that has been through replay.
1579 *
1580 * The scan will update the link count on the inode to reflect the
1581 * number of back refs found. If it goes down to zero, the iput
1582 * will free the inode.
1583 */
1584static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1585 struct btrfs_root *root,
1586 struct inode *inode)
1587{
1588 struct btrfs_path *path;
1589 int ret;
1590 u64 nlink = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001591 u64 ino = btrfs_ino(BTRFS_I(inode));
Mark Fashehf1863732012-08-08 11:32:27 -07001592
1593 path = btrfs_alloc_path();
1594 if (!path)
1595 return -ENOMEM;
1596
Nikolay Borisovf329e312017-01-18 00:31:50 +02001597 ret = count_inode_refs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001598 if (ret < 0)
1599 goto out;
1600
1601 nlink = ret;
1602
Nikolay Borisov36283652017-01-18 00:31:49 +02001603 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
Mark Fashehf1863732012-08-08 11:32:27 -07001604 if (ret < 0)
1605 goto out;
1606
1607 nlink += ret;
1608
1609 ret = 0;
1610
Chris Masone02119d2008-09-05 16:13:11 -04001611 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001612 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001613 btrfs_update_inode(trans, root, inode);
1614 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001615 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001616
Yan, Zhengc71bf092009-11-12 09:34:40 +00001617 if (inode->i_nlink == 0) {
1618 if (S_ISDIR(inode->i_mode)) {
1619 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001620 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001621 if (ret)
1622 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001623 }
Li Zefan33345d012011-04-20 10:31:50 +08001624 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001625 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001626
Mark Fashehf1863732012-08-08 11:32:27 -07001627out:
1628 btrfs_free_path(path);
1629 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001630}
1631
1632static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1633 struct btrfs_root *root,
1634 struct btrfs_path *path)
1635{
1636 int ret;
1637 struct btrfs_key key;
1638 struct inode *inode;
1639
1640 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1641 key.type = BTRFS_ORPHAN_ITEM_KEY;
1642 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001643 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001644 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1645 if (ret < 0)
1646 break;
1647
1648 if (ret == 1) {
1649 if (path->slots[0] == 0)
1650 break;
1651 path->slots[0]--;
1652 }
1653
1654 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1655 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1656 key.type != BTRFS_ORPHAN_ITEM_KEY)
1657 break;
1658
1659 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001660 if (ret)
1661 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001662
David Sterbab3b4aa72011-04-21 01:20:15 +02001663 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001664 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001665 if (!inode)
1666 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001667
1668 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001669 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001670 if (ret)
1671 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001672
Chris Mason12fcfd22009-03-24 10:24:20 -04001673 /*
1674 * fixup on a directory may create new entries,
1675 * make sure we always look for the highset possible
1676 * offset
1677 */
1678 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001679 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001680 ret = 0;
1681out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001682 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001683 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001684}
1685
1686
1687/*
1688 * record a given inode in the fixup dir so we can check its link
1689 * count when replay is done. The link count is incremented here
1690 * so the inode won't go away until we check it
1691 */
1692static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1693 struct btrfs_root *root,
1694 struct btrfs_path *path,
1695 u64 objectid)
1696{
1697 struct btrfs_key key;
1698 int ret = 0;
1699 struct inode *inode;
1700
1701 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001702 if (!inode)
1703 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001704
1705 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001706 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001707 key.offset = objectid;
1708
1709 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1710
David Sterbab3b4aa72011-04-21 01:20:15 +02001711 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001712 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001713 if (!inode->i_nlink)
1714 set_nlink(inode, 1);
1715 else
Zach Brown8b558c52013-10-16 12:10:34 -07001716 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001717 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001718 } else if (ret == -EEXIST) {
1719 ret = 0;
1720 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001721 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001722 }
1723 iput(inode);
1724
1725 return ret;
1726}
1727
1728/*
1729 * when replaying the log for a directory, we only insert names
1730 * for inodes that actually exist. This means an fsync on a directory
1731 * does not implicitly fsync all the new files in it
1732 */
1733static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1734 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001735 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001736 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001737 struct btrfs_key *location)
1738{
1739 struct inode *inode;
1740 struct inode *dir;
1741 int ret;
1742
1743 inode = read_one_inode(root, location->objectid);
1744 if (!inode)
1745 return -ENOENT;
1746
1747 dir = read_one_inode(root, dirid);
1748 if (!dir) {
1749 iput(inode);
1750 return -EIO;
1751 }
Josef Bacikd5554382013-09-11 14:17:00 -04001752
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02001753 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1754 name_len, 1, index);
Chris Masone02119d2008-09-05 16:13:11 -04001755
1756 /* FIXME, put inode into FIXUP list */
1757
1758 iput(inode);
1759 iput(dir);
1760 return ret;
1761}
1762
1763/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001764 * Return true if an inode reference exists in the log for the given name,
1765 * inode and parent inode.
1766 */
1767static bool name_in_log_ref(struct btrfs_root *log_root,
1768 const char *name, const int name_len,
1769 const u64 dirid, const u64 ino)
1770{
1771 struct btrfs_key search_key;
1772
1773 search_key.objectid = ino;
1774 search_key.type = BTRFS_INODE_REF_KEY;
1775 search_key.offset = dirid;
1776 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1777 return true;
1778
1779 search_key.type = BTRFS_INODE_EXTREF_KEY;
1780 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1781 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1782 return true;
1783
1784 return false;
1785}
1786
1787/*
Chris Masone02119d2008-09-05 16:13:11 -04001788 * take a single entry in a log directory item and replay it into
1789 * the subvolume.
1790 *
1791 * if a conflicting item exists in the subdirectory already,
1792 * the inode it points to is unlinked and put into the link count
1793 * fix up tree.
1794 *
1795 * If a name from the log points to a file or directory that does
1796 * not exist in the FS, it is skipped. fsyncs on directories
1797 * do not force down inodes inside that directory, just changes to the
1798 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001799 *
1800 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1801 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001802 */
1803static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1804 struct btrfs_root *root,
1805 struct btrfs_path *path,
1806 struct extent_buffer *eb,
1807 struct btrfs_dir_item *di,
1808 struct btrfs_key *key)
1809{
1810 char *name;
1811 int name_len;
1812 struct btrfs_dir_item *dst_di;
1813 struct btrfs_key found_key;
1814 struct btrfs_key log_key;
1815 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001816 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001817 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001818 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001819 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001820 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001821
1822 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001823 if (!dir)
1824 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001825
1826 name_len = btrfs_dir_name_len(eb, di);
1827 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001828 if (!name) {
1829 ret = -ENOMEM;
1830 goto out;
1831 }
liubo2a29edc2011-01-26 06:22:08 +00001832
Chris Masone02119d2008-09-05 16:13:11 -04001833 log_type = btrfs_dir_type(eb, di);
1834 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1835 name_len);
1836
1837 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001838 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1839 if (exists == 0)
1840 exists = 1;
1841 else
1842 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001843 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001844
Chris Masone02119d2008-09-05 16:13:11 -04001845 if (key->type == BTRFS_DIR_ITEM_KEY) {
1846 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1847 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001848 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001849 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1850 key->objectid,
1851 key->offset, name,
1852 name_len, 1);
1853 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001854 /* Corruption */
1855 ret = -EINVAL;
1856 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001857 }
David Sterbac7040052011-04-19 18:00:01 +02001858 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001859 /* we need a sequence number to insert, so we only
1860 * do inserts for the BTRFS_DIR_INDEX_KEY types
1861 */
1862 if (key->type != BTRFS_DIR_INDEX_KEY)
1863 goto out;
1864 goto insert;
1865 }
1866
1867 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1868 /* the existing item matches the logged item */
1869 if (found_key.objectid == log_key.objectid &&
1870 found_key.type == log_key.type &&
1871 found_key.offset == log_key.offset &&
1872 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001873 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001874 goto out;
1875 }
1876
1877 /*
1878 * don't drop the conflicting directory entry if the inode
1879 * for the new entry doesn't exist
1880 */
Chris Mason4bef0842008-09-08 11:18:08 -04001881 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001882 goto out;
1883
Nikolay Borisov207e7d92017-01-18 00:31:45 +02001884 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001885 if (ret)
1886 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001887
1888 if (key->type == BTRFS_DIR_INDEX_KEY)
1889 goto insert;
1890out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001891 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001892 if (!ret && update_size) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02001893 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
Josef Bacikd5554382013-09-11 14:17:00 -04001894 ret = btrfs_update_inode(trans, root, dir);
1895 }
Chris Masone02119d2008-09-05 16:13:11 -04001896 kfree(name);
1897 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001898 if (!ret && name_added)
1899 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001900 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001901
1902insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001903 if (name_in_log_ref(root->log_root, name, name_len,
1904 key->objectid, log_key.objectid)) {
1905 /* The dentry will be added later. */
1906 ret = 0;
1907 update_size = false;
1908 goto out;
1909 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001910 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08001911 ret = insert_one_name(trans, root, key->objectid, key->offset,
1912 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001913 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001914 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001915 if (!ret)
1916 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001917 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001918 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001919 goto out;
1920}
1921
1922/*
1923 * find all the names in a directory item and reconcile them into
1924 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1925 * one name in a directory item, but the same code gets used for
1926 * both directory index types
1927 */
1928static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1929 struct btrfs_root *root,
1930 struct btrfs_path *path,
1931 struct extent_buffer *eb, int slot,
1932 struct btrfs_key *key)
1933{
Filipe Mananabb53eda2015-07-15 23:26:43 +01001934 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001935 u32 item_size = btrfs_item_size_nr(eb, slot);
1936 struct btrfs_dir_item *di;
1937 int name_len;
1938 unsigned long ptr;
1939 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001940 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001941
1942 ptr = btrfs_item_ptr_offset(eb, slot);
1943 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001944 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001945 di = (struct btrfs_dir_item *)ptr;
1946 name_len = btrfs_dir_name_len(eb, di);
1947 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001948 if (ret < 0)
1949 break;
Chris Masone02119d2008-09-05 16:13:11 -04001950 ptr = (unsigned long)(di + 1);
1951 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001952
1953 /*
1954 * If this entry refers to a non-directory (directories can not
1955 * have a link count > 1) and it was added in the transaction
1956 * that was not committed, make sure we fixup the link count of
1957 * the inode it the entry points to. Otherwise something like
1958 * the following would result in a directory pointing to an
1959 * inode with a wrong link that does not account for this dir
1960 * entry:
1961 *
1962 * mkdir testdir
1963 * touch testdir/foo
1964 * touch testdir/bar
1965 * sync
1966 *
1967 * ln testdir/bar testdir/bar_link
1968 * ln testdir/foo testdir/foo_link
1969 * xfs_io -c "fsync" testdir/bar
1970 *
1971 * <power failure>
1972 *
1973 * mount fs, log replay happens
1974 *
1975 * File foo would remain with a link count of 1 when it has two
1976 * entries pointing to it in the directory testdir. This would
1977 * make it impossible to ever delete the parent directory has
1978 * it would result in stale dentries that can never be deleted.
1979 */
1980 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1981 struct btrfs_key di_key;
1982
1983 if (!fixup_path) {
1984 fixup_path = btrfs_alloc_path();
1985 if (!fixup_path) {
1986 ret = -ENOMEM;
1987 break;
1988 }
1989 }
1990
1991 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1992 ret = link_to_fixup_dir(trans, root, fixup_path,
1993 di_key.objectid);
1994 if (ret)
1995 break;
1996 }
1997 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001998 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01001999 btrfs_free_path(fixup_path);
2000 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002001}
2002
2003/*
2004 * directory replay has two parts. There are the standard directory
2005 * items in the log copied from the subvolume, and range items
2006 * created in the log while the subvolume was logged.
2007 *
2008 * The range items tell us which parts of the key space the log
2009 * is authoritative for. During replay, if a key in the subvolume
2010 * directory is in a logged range item, but not actually in the log
2011 * that means it was deleted from the directory before the fsync
2012 * and should be removed.
2013 */
2014static noinline int find_dir_range(struct btrfs_root *root,
2015 struct btrfs_path *path,
2016 u64 dirid, int key_type,
2017 u64 *start_ret, u64 *end_ret)
2018{
2019 struct btrfs_key key;
2020 u64 found_end;
2021 struct btrfs_dir_log_item *item;
2022 int ret;
2023 int nritems;
2024
2025 if (*start_ret == (u64)-1)
2026 return 1;
2027
2028 key.objectid = dirid;
2029 key.type = key_type;
2030 key.offset = *start_ret;
2031
2032 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2033 if (ret < 0)
2034 goto out;
2035 if (ret > 0) {
2036 if (path->slots[0] == 0)
2037 goto out;
2038 path->slots[0]--;
2039 }
2040 if (ret != 0)
2041 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2042
2043 if (key.type != key_type || key.objectid != dirid) {
2044 ret = 1;
2045 goto next;
2046 }
2047 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2048 struct btrfs_dir_log_item);
2049 found_end = btrfs_dir_log_end(path->nodes[0], item);
2050
2051 if (*start_ret >= key.offset && *start_ret <= found_end) {
2052 ret = 0;
2053 *start_ret = key.offset;
2054 *end_ret = found_end;
2055 goto out;
2056 }
2057 ret = 1;
2058next:
2059 /* check the next slot in the tree to see if it is a valid item */
2060 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko2a7bf532016-10-07 17:30:47 +08002061 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04002062 if (path->slots[0] >= nritems) {
2063 ret = btrfs_next_leaf(root, path);
2064 if (ret)
2065 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002066 }
2067
2068 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2069
2070 if (key.type != key_type || key.objectid != dirid) {
2071 ret = 1;
2072 goto out;
2073 }
2074 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2075 struct btrfs_dir_log_item);
2076 found_end = btrfs_dir_log_end(path->nodes[0], item);
2077 *start_ret = key.offset;
2078 *end_ret = found_end;
2079 ret = 0;
2080out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002081 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002082 return ret;
2083}
2084
2085/*
2086 * this looks for a given directory item in the log. If the directory
2087 * item is not in the log, the item is removed and the inode it points
2088 * to is unlinked
2089 */
2090static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2091 struct btrfs_root *root,
2092 struct btrfs_root *log,
2093 struct btrfs_path *path,
2094 struct btrfs_path *log_path,
2095 struct inode *dir,
2096 struct btrfs_key *dir_key)
2097{
2098 int ret;
2099 struct extent_buffer *eb;
2100 int slot;
2101 u32 item_size;
2102 struct btrfs_dir_item *di;
2103 struct btrfs_dir_item *log_di;
2104 int name_len;
2105 unsigned long ptr;
2106 unsigned long ptr_end;
2107 char *name;
2108 struct inode *inode;
2109 struct btrfs_key location;
2110
2111again:
2112 eb = path->nodes[0];
2113 slot = path->slots[0];
2114 item_size = btrfs_item_size_nr(eb, slot);
2115 ptr = btrfs_item_ptr_offset(eb, slot);
2116 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002117 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002118 di = (struct btrfs_dir_item *)ptr;
2119 name_len = btrfs_dir_name_len(eb, di);
2120 name = kmalloc(name_len, GFP_NOFS);
2121 if (!name) {
2122 ret = -ENOMEM;
2123 goto out;
2124 }
2125 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2126 name_len);
2127 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002128 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002129 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2130 dir_key->objectid,
2131 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002132 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002133 log_di = btrfs_lookup_dir_index_item(trans, log,
2134 log_path,
2135 dir_key->objectid,
2136 dir_key->offset,
2137 name, name_len, 0);
2138 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002139 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002140 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002141 btrfs_release_path(path);
2142 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002143 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002144 if (!inode) {
2145 kfree(name);
2146 return -EIO;
2147 }
Chris Masone02119d2008-09-05 16:13:11 -04002148
2149 ret = link_to_fixup_dir(trans, root,
2150 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002151 if (ret) {
2152 kfree(name);
2153 iput(inode);
2154 goto out;
2155 }
2156
Zach Brown8b558c52013-10-16 12:10:34 -07002157 inc_nlink(inode);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02002158 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2159 BTRFS_I(inode), name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002160 if (!ret)
Nikolay Borisove5c304e62018-02-07 17:55:43 +02002161 ret = btrfs_run_delayed_items(trans);
Chris Masone02119d2008-09-05 16:13:11 -04002162 kfree(name);
2163 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002164 if (ret)
2165 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002166
2167 /* there might still be more names under this key
2168 * check and repeat if required
2169 */
2170 ret = btrfs_search_slot(NULL, root, dir_key, path,
2171 0, 0);
2172 if (ret == 0)
2173 goto again;
2174 ret = 0;
2175 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002176 } else if (IS_ERR(log_di)) {
2177 kfree(name);
2178 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002179 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002180 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002181 kfree(name);
2182
2183 ptr = (unsigned long)(di + 1);
2184 ptr += name_len;
2185 }
2186 ret = 0;
2187out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002188 btrfs_release_path(path);
2189 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002190 return ret;
2191}
2192
Filipe Manana4f764e52015-02-23 19:53:35 +00002193static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2194 struct btrfs_root *root,
2195 struct btrfs_root *log,
2196 struct btrfs_path *path,
2197 const u64 ino)
2198{
2199 struct btrfs_key search_key;
2200 struct btrfs_path *log_path;
2201 int i;
2202 int nritems;
2203 int ret;
2204
2205 log_path = btrfs_alloc_path();
2206 if (!log_path)
2207 return -ENOMEM;
2208
2209 search_key.objectid = ino;
2210 search_key.type = BTRFS_XATTR_ITEM_KEY;
2211 search_key.offset = 0;
2212again:
2213 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2214 if (ret < 0)
2215 goto out;
2216process_leaf:
2217 nritems = btrfs_header_nritems(path->nodes[0]);
2218 for (i = path->slots[0]; i < nritems; i++) {
2219 struct btrfs_key key;
2220 struct btrfs_dir_item *di;
2221 struct btrfs_dir_item *log_di;
2222 u32 total_size;
2223 u32 cur;
2224
2225 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2226 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2227 ret = 0;
2228 goto out;
2229 }
2230
2231 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2232 total_size = btrfs_item_size_nr(path->nodes[0], i);
2233 cur = 0;
2234 while (cur < total_size) {
2235 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2236 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2237 u32 this_len = sizeof(*di) + name_len + data_len;
2238 char *name;
2239
2240 name = kmalloc(name_len, GFP_NOFS);
2241 if (!name) {
2242 ret = -ENOMEM;
2243 goto out;
2244 }
2245 read_extent_buffer(path->nodes[0], name,
2246 (unsigned long)(di + 1), name_len);
2247
2248 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2249 name, name_len, 0);
2250 btrfs_release_path(log_path);
2251 if (!log_di) {
2252 /* Doesn't exist in log tree, so delete it. */
2253 btrfs_release_path(path);
2254 di = btrfs_lookup_xattr(trans, root, path, ino,
2255 name, name_len, -1);
2256 kfree(name);
2257 if (IS_ERR(di)) {
2258 ret = PTR_ERR(di);
2259 goto out;
2260 }
2261 ASSERT(di);
2262 ret = btrfs_delete_one_dir_name(trans, root,
2263 path, di);
2264 if (ret)
2265 goto out;
2266 btrfs_release_path(path);
2267 search_key = key;
2268 goto again;
2269 }
2270 kfree(name);
2271 if (IS_ERR(log_di)) {
2272 ret = PTR_ERR(log_di);
2273 goto out;
2274 }
2275 cur += this_len;
2276 di = (struct btrfs_dir_item *)((char *)di + this_len);
2277 }
2278 }
2279 ret = btrfs_next_leaf(root, path);
2280 if (ret > 0)
2281 ret = 0;
2282 else if (ret == 0)
2283 goto process_leaf;
2284out:
2285 btrfs_free_path(log_path);
2286 btrfs_release_path(path);
2287 return ret;
2288}
2289
2290
Chris Masone02119d2008-09-05 16:13:11 -04002291/*
2292 * deletion replay happens before we copy any new directory items
2293 * out of the log or out of backreferences from inodes. It
2294 * scans the log to find ranges of keys that log is authoritative for,
2295 * and then scans the directory to find items in those ranges that are
2296 * not present in the log.
2297 *
2298 * Anything we don't find in the log is unlinked and removed from the
2299 * directory.
2300 */
2301static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2302 struct btrfs_root *root,
2303 struct btrfs_root *log,
2304 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002305 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002306{
2307 u64 range_start;
2308 u64 range_end;
2309 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2310 int ret = 0;
2311 struct btrfs_key dir_key;
2312 struct btrfs_key found_key;
2313 struct btrfs_path *log_path;
2314 struct inode *dir;
2315
2316 dir_key.objectid = dirid;
2317 dir_key.type = BTRFS_DIR_ITEM_KEY;
2318 log_path = btrfs_alloc_path();
2319 if (!log_path)
2320 return -ENOMEM;
2321
2322 dir = read_one_inode(root, dirid);
2323 /* it isn't an error if the inode isn't there, that can happen
2324 * because we replay the deletes before we copy in the inode item
2325 * from the log
2326 */
2327 if (!dir) {
2328 btrfs_free_path(log_path);
2329 return 0;
2330 }
2331again:
2332 range_start = 0;
2333 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002334 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002335 if (del_all)
2336 range_end = (u64)-1;
2337 else {
2338 ret = find_dir_range(log, path, dirid, key_type,
2339 &range_start, &range_end);
2340 if (ret != 0)
2341 break;
2342 }
Chris Masone02119d2008-09-05 16:13:11 -04002343
2344 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002345 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002346 int nritems;
2347 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2348 0, 0);
2349 if (ret < 0)
2350 goto out;
2351
2352 nritems = btrfs_header_nritems(path->nodes[0]);
2353 if (path->slots[0] >= nritems) {
2354 ret = btrfs_next_leaf(root, path);
Liu Bob98def72018-04-03 01:59:48 +08002355 if (ret == 1)
Chris Masone02119d2008-09-05 16:13:11 -04002356 break;
Liu Bob98def72018-04-03 01:59:48 +08002357 else if (ret < 0)
2358 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002359 }
2360 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2361 path->slots[0]);
2362 if (found_key.objectid != dirid ||
2363 found_key.type != dir_key.type)
2364 goto next_type;
2365
2366 if (found_key.offset > range_end)
2367 break;
2368
2369 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002370 log_path, dir,
2371 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002372 if (ret)
2373 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002374 if (found_key.offset == (u64)-1)
2375 break;
2376 dir_key.offset = found_key.offset + 1;
2377 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002378 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002379 if (range_end == (u64)-1)
2380 break;
2381 range_start = range_end + 1;
2382 }
2383
2384next_type:
2385 ret = 0;
2386 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2387 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2388 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002389 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002390 goto again;
2391 }
2392out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002393 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002394 btrfs_free_path(log_path);
2395 iput(dir);
2396 return ret;
2397}
2398
2399/*
2400 * the process_func used to replay items from the log tree. This
2401 * gets called in two different stages. The first stage just looks
2402 * for inodes and makes sure they are all copied into the subvolume.
2403 *
2404 * The second stage copies all the other item types from the log into
2405 * the subvolume. The two stage approach is slower, but gets rid of
2406 * lots of complexity around inodes referencing other inodes that exist
2407 * only in the log (references come from either directory items or inode
2408 * back refs).
2409 */
2410static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +08002411 struct walk_control *wc, u64 gen, int level)
Chris Masone02119d2008-09-05 16:13:11 -04002412{
2413 int nritems;
2414 struct btrfs_path *path;
2415 struct btrfs_root *root = wc->replay_dest;
2416 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002417 int i;
2418 int ret;
2419
Qu Wenruo581c1762018-03-29 09:08:11 +08002420 ret = btrfs_read_buffer(eb, gen, level, NULL);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002421 if (ret)
2422 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002423
2424 level = btrfs_header_level(eb);
2425
2426 if (level != 0)
2427 return 0;
2428
2429 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002430 if (!path)
2431 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002432
2433 nritems = btrfs_header_nritems(eb);
2434 for (i = 0; i < nritems; i++) {
2435 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002436
2437 /* inode keys are done during the first stage */
2438 if (key.type == BTRFS_INODE_ITEM_KEY &&
2439 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002440 struct btrfs_inode_item *inode_item;
2441 u32 mode;
2442
2443 inode_item = btrfs_item_ptr(eb, i,
2444 struct btrfs_inode_item);
Filipe Manana4f764e52015-02-23 19:53:35 +00002445 ret = replay_xattr_deletes(wc->trans, root, log,
2446 path, key.objectid);
2447 if (ret)
2448 break;
Chris Masone02119d2008-09-05 16:13:11 -04002449 mode = btrfs_inode_mode(eb, inode_item);
2450 if (S_ISDIR(mode)) {
2451 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002452 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002453 if (ret)
2454 break;
Chris Masone02119d2008-09-05 16:13:11 -04002455 }
2456 ret = overwrite_item(wc->trans, root, path,
2457 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002458 if (ret)
2459 break;
Chris Masone02119d2008-09-05 16:13:11 -04002460
Filipe Manana471d5572018-04-05 22:55:12 +01002461 /*
2462 * Before replaying extents, truncate the inode to its
2463 * size. We need to do it now and not after log replay
2464 * because before an fsync we can have prealloc extents
2465 * added beyond the inode's i_size. If we did it after,
2466 * through orphan cleanup for example, we would drop
2467 * those prealloc extents just after replaying them.
Chris Masone02119d2008-09-05 16:13:11 -04002468 */
2469 if (S_ISREG(mode)) {
Filipe Manana471d5572018-04-05 22:55:12 +01002470 struct inode *inode;
2471 u64 from;
2472
2473 inode = read_one_inode(root, key.objectid);
2474 if (!inode) {
2475 ret = -EIO;
2476 break;
2477 }
2478 from = ALIGN(i_size_read(inode),
2479 root->fs_info->sectorsize);
2480 ret = btrfs_drop_extents(wc->trans, root, inode,
2481 from, (u64)-1, 1);
2482 /*
2483 * If the nlink count is zero here, the iput
2484 * will free the inode. We bump it to make
2485 * sure it doesn't get freed until the link
2486 * count fixup is done.
2487 */
2488 if (!ret) {
2489 if (inode->i_nlink == 0)
2490 inc_nlink(inode);
2491 /* Update link count and nbytes. */
2492 ret = btrfs_update_inode(wc->trans,
2493 root, inode);
2494 }
2495 iput(inode);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002496 if (ret)
2497 break;
Chris Masone02119d2008-09-05 16:13:11 -04002498 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002499
Chris Masone02119d2008-09-05 16:13:11 -04002500 ret = link_to_fixup_dir(wc->trans, root,
2501 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002502 if (ret)
2503 break;
Chris Masone02119d2008-09-05 16:13:11 -04002504 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002505
2506 if (key.type == BTRFS_DIR_INDEX_KEY &&
2507 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2508 ret = replay_one_dir_item(wc->trans, root, path,
2509 eb, i, &key);
2510 if (ret)
2511 break;
2512 }
2513
Chris Masone02119d2008-09-05 16:13:11 -04002514 if (wc->stage < LOG_WALK_REPLAY_ALL)
2515 continue;
2516
2517 /* these keys are simply copied */
2518 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2519 ret = overwrite_item(wc->trans, root, path,
2520 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002521 if (ret)
2522 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002523 } else if (key.type == BTRFS_INODE_REF_KEY ||
2524 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002525 ret = add_inode_ref(wc->trans, root, log, path,
2526 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002527 if (ret && ret != -ENOENT)
2528 break;
2529 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002530 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2531 ret = replay_one_extent(wc->trans, root, path,
2532 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002533 if (ret)
2534 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002535 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002536 ret = replay_one_dir_item(wc->trans, root, path,
2537 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002538 if (ret)
2539 break;
Chris Masone02119d2008-09-05 16:13:11 -04002540 }
2541 }
2542 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002543 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002544}
2545
Chris Masond3977122009-01-05 21:25:51 -05002546static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002547 struct btrfs_root *root,
2548 struct btrfs_path *path, int *level,
2549 struct walk_control *wc)
2550{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002551 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002552 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002553 u64 bytenr;
2554 u64 ptr_gen;
2555 struct extent_buffer *next;
2556 struct extent_buffer *cur;
2557 struct extent_buffer *parent;
2558 u32 blocksize;
2559 int ret = 0;
2560
2561 WARN_ON(*level < 0);
2562 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2563
Chris Masond3977122009-01-05 21:25:51 -05002564 while (*level > 0) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002565 struct btrfs_key first_key;
2566
Chris Masone02119d2008-09-05 16:13:11 -04002567 WARN_ON(*level < 0);
2568 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2569 cur = path->nodes[*level];
2570
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302571 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002572
2573 if (path->slots[*level] >=
2574 btrfs_header_nritems(cur))
2575 break;
2576
2577 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2578 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Qu Wenruo581c1762018-03-29 09:08:11 +08002579 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002580 blocksize = fs_info->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002581
2582 parent = path->nodes[*level];
2583 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002584
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002585 next = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002586 if (IS_ERR(next))
2587 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002588
Chris Masone02119d2008-09-05 16:13:11 -04002589 if (*level == 1) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002590 ret = wc->process_func(root, next, wc, ptr_gen,
2591 *level - 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002592 if (ret) {
2593 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002594 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002595 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002596
Chris Masone02119d2008-09-05 16:13:11 -04002597 path->slots[*level]++;
2598 if (wc->free) {
Qu Wenruo581c1762018-03-29 09:08:11 +08002599 ret = btrfs_read_buffer(next, ptr_gen,
2600 *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002601 if (ret) {
2602 free_extent_buffer(next);
2603 return ret;
2604 }
Chris Masone02119d2008-09-05 16:13:11 -04002605
Josef Bacik681ae502013-10-07 15:11:00 -04002606 if (trans) {
2607 btrfs_tree_lock(next);
2608 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002609 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002610 btrfs_wait_tree_block_writeback(next);
2611 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002612 } else {
2613 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2614 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002615 }
Chris Masone02119d2008-09-05 16:13:11 -04002616
Chris Masone02119d2008-09-05 16:13:11 -04002617 WARN_ON(root_owner !=
2618 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002619 ret = btrfs_free_and_pin_reserved_extent(
2620 fs_info, bytenr,
2621 blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002622 if (ret) {
2623 free_extent_buffer(next);
2624 return ret;
2625 }
Chris Masone02119d2008-09-05 16:13:11 -04002626 }
2627 free_extent_buffer(next);
2628 continue;
2629 }
Qu Wenruo581c1762018-03-29 09:08:11 +08002630 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002631 if (ret) {
2632 free_extent_buffer(next);
2633 return ret;
2634 }
Chris Masone02119d2008-09-05 16:13:11 -04002635
2636 WARN_ON(*level <= 0);
2637 if (path->nodes[*level-1])
2638 free_extent_buffer(path->nodes[*level-1]);
2639 path->nodes[*level-1] = next;
2640 *level = btrfs_header_level(next);
2641 path->slots[*level] = 0;
2642 cond_resched();
2643 }
2644 WARN_ON(*level < 0);
2645 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2646
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002647 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002648
2649 cond_resched();
2650 return 0;
2651}
2652
Chris Masond3977122009-01-05 21:25:51 -05002653static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002654 struct btrfs_root *root,
2655 struct btrfs_path *path, int *level,
2656 struct walk_control *wc)
2657{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002658 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002659 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002660 int i;
2661 int slot;
2662 int ret;
2663
Chris Masond3977122009-01-05 21:25:51 -05002664 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002665 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002666 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002667 path->slots[i]++;
2668 *level = i;
2669 WARN_ON(*level == 0);
2670 return 0;
2671 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002672 struct extent_buffer *parent;
2673 if (path->nodes[*level] == root->node)
2674 parent = path->nodes[*level];
2675 else
2676 parent = path->nodes[*level + 1];
2677
2678 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002679 ret = wc->process_func(root, path->nodes[*level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002680 btrfs_header_generation(path->nodes[*level]),
2681 *level);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002682 if (ret)
2683 return ret;
2684
Chris Masone02119d2008-09-05 16:13:11 -04002685 if (wc->free) {
2686 struct extent_buffer *next;
2687
2688 next = path->nodes[*level];
2689
Josef Bacik681ae502013-10-07 15:11:00 -04002690 if (trans) {
2691 btrfs_tree_lock(next);
2692 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002693 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002694 btrfs_wait_tree_block_writeback(next);
2695 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002696 } else {
2697 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2698 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002699 }
Chris Masone02119d2008-09-05 16:13:11 -04002700
Chris Masone02119d2008-09-05 16:13:11 -04002701 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002702 ret = btrfs_free_and_pin_reserved_extent(
2703 fs_info,
Chris Masone02119d2008-09-05 16:13:11 -04002704 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002705 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002706 if (ret)
2707 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002708 }
2709 free_extent_buffer(path->nodes[*level]);
2710 path->nodes[*level] = NULL;
2711 *level = i + 1;
2712 }
2713 }
2714 return 1;
2715}
2716
2717/*
2718 * drop the reference count on the tree rooted at 'snap'. This traverses
2719 * the tree freeing any blocks that have a ref count of zero after being
2720 * decremented.
2721 */
2722static int walk_log_tree(struct btrfs_trans_handle *trans,
2723 struct btrfs_root *log, struct walk_control *wc)
2724{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002725 struct btrfs_fs_info *fs_info = log->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002726 int ret = 0;
2727 int wret;
2728 int level;
2729 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002730 int orig_level;
2731
2732 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002733 if (!path)
2734 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002735
2736 level = btrfs_header_level(log->node);
2737 orig_level = level;
2738 path->nodes[level] = log->node;
2739 extent_buffer_get(log->node);
2740 path->slots[level] = 0;
2741
Chris Masond3977122009-01-05 21:25:51 -05002742 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002743 wret = walk_down_log_tree(trans, log, path, &level, wc);
2744 if (wret > 0)
2745 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002746 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002747 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002748 goto out;
2749 }
Chris Masone02119d2008-09-05 16:13:11 -04002750
2751 wret = walk_up_log_tree(trans, log, path, &level, wc);
2752 if (wret > 0)
2753 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002754 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002755 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002756 goto out;
2757 }
Chris Masone02119d2008-09-05 16:13:11 -04002758 }
2759
2760 /* was the root node processed? if not, catch it here */
2761 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002762 ret = wc->process_func(log, path->nodes[orig_level], wc,
Qu Wenruo581c1762018-03-29 09:08:11 +08002763 btrfs_header_generation(path->nodes[orig_level]),
2764 orig_level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002765 if (ret)
2766 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002767 if (wc->free) {
2768 struct extent_buffer *next;
2769
2770 next = path->nodes[orig_level];
2771
Josef Bacik681ae502013-10-07 15:11:00 -04002772 if (trans) {
2773 btrfs_tree_lock(next);
2774 btrfs_set_lock_blocking(next);
David Sterba7c302b42017-02-10 18:47:57 +01002775 clean_tree_block(fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002776 btrfs_wait_tree_block_writeback(next);
2777 btrfs_tree_unlock(next);
Liu Bo18464302018-01-25 11:02:51 -07002778 } else {
2779 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2780 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002781 }
Chris Masone02119d2008-09-05 16:13:11 -04002782
Chris Masone02119d2008-09-05 16:13:11 -04002783 WARN_ON(log->root_key.objectid !=
2784 BTRFS_TREE_LOG_OBJECTID);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002785 ret = btrfs_free_and_pin_reserved_extent(fs_info,
2786 next->start, next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002787 if (ret)
2788 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002789 }
2790 }
2791
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002792out:
Chris Masone02119d2008-09-05 16:13:11 -04002793 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002794 return ret;
2795}
2796
Yan Zheng7237f182009-01-21 12:54:03 -05002797/*
2798 * helper function to update the item for a given subvolumes log root
2799 * in the tree of log roots
2800 */
2801static int update_log_root(struct btrfs_trans_handle *trans,
2802 struct btrfs_root *log)
2803{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002804 struct btrfs_fs_info *fs_info = log->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -05002805 int ret;
2806
2807 if (log->log_transid == 1) {
2808 /* insert root item on the first sync */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002809 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002810 &log->root_key, &log->root_item);
2811 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002812 ret = btrfs_update_root(trans, fs_info->log_root_tree,
Yan Zheng7237f182009-01-21 12:54:03 -05002813 &log->root_key, &log->root_item);
2814 }
2815 return ret;
2816}
2817
Zhaolei60d53eb2015-08-17 18:44:46 +08002818static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002819{
2820 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002821 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002822
Yan Zheng7237f182009-01-21 12:54:03 -05002823 /*
2824 * we only allow two pending log transactions at a time,
2825 * so we know that if ours is more than 2 older than the
2826 * current transaction, we're done
2827 */
Liu Bo49e83f52017-09-01 16:14:30 -06002828 for (;;) {
Yan Zheng7237f182009-01-21 12:54:03 -05002829 prepare_to_wait(&root->log_commit_wait[index],
2830 &wait, TASK_UNINTERRUPTIBLE);
Liu Bo49e83f52017-09-01 16:14:30 -06002831
2832 if (!(root->log_transid_committed < transid &&
2833 atomic_read(&root->log_commit[index])))
2834 break;
2835
Yan Zheng7237f182009-01-21 12:54:03 -05002836 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002837 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002838 mutex_lock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002839 }
2840 finish_wait(&root->log_commit_wait[index], &wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002841}
2842
Zhaolei60d53eb2015-08-17 18:44:46 +08002843static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002844{
2845 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002846
Liu Bo49e83f52017-09-01 16:14:30 -06002847 for (;;) {
2848 prepare_to_wait(&root->log_writer_wait, &wait,
2849 TASK_UNINTERRUPTIBLE);
2850 if (!atomic_read(&root->log_writers))
2851 break;
2852
Yan Zheng7237f182009-01-21 12:54:03 -05002853 mutex_unlock(&root->log_mutex);
Liu Bo49e83f52017-09-01 16:14:30 -06002854 schedule();
Filipe Manana575849e2015-02-11 11:12:39 +00002855 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002856 }
Liu Bo49e83f52017-09-01 16:14:30 -06002857 finish_wait(&root->log_writer_wait, &wait);
Chris Masone02119d2008-09-05 16:13:11 -04002858}
2859
Miao Xie8b050d32014-02-20 18:08:58 +08002860static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2861 struct btrfs_log_ctx *ctx)
2862{
2863 if (!ctx)
2864 return;
2865
2866 mutex_lock(&root->log_mutex);
2867 list_del_init(&ctx->list);
2868 mutex_unlock(&root->log_mutex);
2869}
2870
2871/*
2872 * Invoked in log mutex context, or be sure there is no other task which
2873 * can access the list.
2874 */
2875static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2876 int index, int error)
2877{
2878 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002879 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002880
Chris Mason570dd452016-10-27 10:42:20 -07002881 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2882 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002883 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002884 }
Miao Xie8b050d32014-02-20 18:08:58 +08002885
2886 INIT_LIST_HEAD(&root->log_ctxs[index]);
2887}
2888
Chris Masone02119d2008-09-05 16:13:11 -04002889/*
2890 * btrfs_sync_log does sends a given tree log down to the disk and
2891 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002892 * you know that any inodes previously logged are safely on disk only
2893 * if it returns 0.
2894 *
2895 * Any other return value means you need to call btrfs_commit_transaction.
2896 * Some of the edge cases for fsyncing directories that have had unlinks
2897 * or renames done in the past mean that sometimes the only safe
2898 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2899 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002900 */
2901int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002902 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002903{
Yan Zheng7237f182009-01-21 12:54:03 -05002904 int index1;
2905 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002906 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002907 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002908 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04002909 struct btrfs_root *log = root->log_root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002910 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002911 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002912 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002913 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002914
Yan Zheng7237f182009-01-21 12:54:03 -05002915 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002916 log_transid = ctx->log_transid;
2917 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002918 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002919 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002920 }
Miao Xied1433de2014-02-20 18:08:59 +08002921
2922 index1 = log_transid % 2;
2923 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002924 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002925 mutex_unlock(&root->log_mutex);
2926 return ctx->log_ret;
2927 }
2928 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002929 atomic_set(&root->log_commit[index1], 1);
2930
2931 /* wait for previous tree log sync to complete */
2932 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002933 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002934
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002935 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002936 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002937 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002938 if (!btrfs_test_opt(fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08002939 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002940 mutex_unlock(&root->log_mutex);
2941 schedule_timeout_uninterruptible(1);
2942 mutex_lock(&root->log_mutex);
2943 }
Zhaolei60d53eb2015-08-17 18:44:46 +08002944 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002945 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002946 break;
2947 }
Chris Masond0c803c2008-09-11 16:17:57 -04002948
Chris Mason12fcfd22009-03-24 10:24:20 -04002949 /* bail out if we need to do a full commit */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002950 if (btrfs_need_log_full_commit(fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002951 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002952 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002953 mutex_unlock(&root->log_mutex);
2954 goto out;
2955 }
2956
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002957 if (log_transid % 2 == 0)
2958 mark = EXTENT_DIRTY;
2959 else
2960 mark = EXTENT_NEW;
2961
Chris Mason690587d2009-10-13 13:29:19 -04002962 /* we start IO on all the marked extents here, but we don't actually
2963 * wait for them until later.
2964 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002965 blk_start_plug(&plug);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002966 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002967 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002968 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04002969 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002970 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002971 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002972 mutex_unlock(&root->log_mutex);
2973 goto out;
2974 }
Yan Zheng7237f182009-01-21 12:54:03 -05002975
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002976 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002977
Yan Zheng7237f182009-01-21 12:54:03 -05002978 root->log_transid++;
2979 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002980 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002981 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002982 * IO has been started, blocks of the log tree have WRITTEN flag set
2983 * in their headers. new modifications of the log will be written to
2984 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002985 */
2986 mutex_unlock(&root->log_mutex);
2987
Filipe Manana28a23592016-08-23 21:13:51 +01002988 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08002989
Yan Zheng7237f182009-01-21 12:54:03 -05002990 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002991 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002992 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002993
2994 index2 = log_root_tree->log_transid % 2;
2995 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2996 root_log_ctx.log_transid = log_root_tree->log_transid;
2997
Yan Zheng7237f182009-01-21 12:54:03 -05002998 mutex_unlock(&log_root_tree->log_mutex);
2999
3000 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05003001
3002 mutex_lock(&log_root_tree->log_mutex);
3003 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +01003004 /*
3005 * Implicit memory barrier after atomic_dec_and_test
3006 */
Yan Zheng7237f182009-01-21 12:54:03 -05003007 if (waitqueue_active(&log_root_tree->log_writer_wait))
3008 wake_up(&log_root_tree->log_writer_wait);
3009 }
3010
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003011 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08003012 if (!list_empty(&root_log_ctx.list))
3013 list_del_init(&root_log_ctx.list);
3014
Miao Xiec6adc9c2013-05-28 10:05:39 +00003015 blk_finish_plug(&plug);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003016 btrfs_set_log_full_commit(fs_info, trans);
Miao Xie995946d2014-04-02 19:51:06 +08003017
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003018 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003019 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003020 mutex_unlock(&log_root_tree->log_mutex);
3021 goto out;
3022 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003023 btrfs_wait_tree_log_extents(log, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003024 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003025 mutex_unlock(&log_root_tree->log_mutex);
3026 ret = -EAGAIN;
3027 goto out;
3028 }
3029
Miao Xied1433de2014-02-20 18:08:59 +08003030 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08003031 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07003032 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08003033 mutex_unlock(&log_root_tree->log_mutex);
3034 ret = root_log_ctx.log_ret;
3035 goto out;
3036 }
Miao Xie8b050d32014-02-20 18:08:58 +08003037
Miao Xied1433de2014-02-20 18:08:59 +08003038 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05003039 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003040 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003041 ret = btrfs_wait_tree_log_extents(log, mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05003042 btrfs_wait_logged_extents(trans, log, log_transid);
Zhaolei60d53eb2015-08-17 18:44:46 +08003043 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003044 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003045 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003046 if (!ret)
3047 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05003048 goto out;
3049 }
Miao Xied1433de2014-02-20 18:08:59 +08003050 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05003051 atomic_set(&log_root_tree->log_commit[index2], 1);
3052
Chris Mason12fcfd22009-03-24 10:24:20 -04003053 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08003054 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08003055 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04003056 }
Yan Zheng7237f182009-01-21 12:54:03 -05003057
Zhaolei60d53eb2015-08-17 18:44:46 +08003058 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04003059
3060 /*
3061 * now that we've moved on to the tree of log tree roots,
3062 * check the full commit flag again
3063 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003064 if (btrfs_need_log_full_commit(fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00003065 blk_finish_plug(&plug);
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003066 btrfs_wait_tree_log_extents(log, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003067 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04003068 mutex_unlock(&log_root_tree->log_mutex);
3069 ret = -EAGAIN;
3070 goto out_wake_log_root;
3071 }
Yan Zheng7237f182009-01-21 12:54:03 -05003072
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003073 ret = btrfs_write_marked_extents(fs_info,
Miao Xiec6adc9c2013-05-28 10:05:39 +00003074 &log_root_tree->dirty_log_pages,
3075 EXTENT_DIRTY | EXTENT_NEW);
3076 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003077 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003078 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003079 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003080 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003081 mutex_unlock(&log_root_tree->log_mutex);
3082 goto out_wake_log_root;
3083 }
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003084 ret = btrfs_wait_tree_log_extents(log, mark);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003085 if (!ret)
Jeff Mahoneybf89d382016-09-09 20:42:44 -04003086 ret = btrfs_wait_tree_log_extents(log_root_tree,
3087 EXTENT_NEW | EXTENT_DIRTY);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003088 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003089 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana5ab5e442014-11-13 16:59:53 +00003090 btrfs_free_logged_extents(log, log_transid);
3091 mutex_unlock(&log_root_tree->log_mutex);
3092 goto out_wake_log_root;
3093 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05003094 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04003095
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003096 btrfs_set_super_log_root(fs_info->super_for_commit,
3097 log_root_tree->node->start);
3098 btrfs_set_super_log_root_level(fs_info->super_for_commit,
3099 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04003100
Yan Zheng7237f182009-01-21 12:54:03 -05003101 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05003102 mutex_unlock(&log_root_tree->log_mutex);
3103
3104 /*
3105 * nobody else is going to jump in and write the the ctree
3106 * super here because the log_commit atomic below is protecting
3107 * us. We must be called with a transaction handle pinning
3108 * the running transaction open, so a full commit can't hop
3109 * in and cause problems either.
3110 */
David Sterbaeece6a92017-02-10 19:04:32 +01003111 ret = write_all_supers(fs_info, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003112 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003113 btrfs_set_log_full_commit(fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04003114 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003115 goto out_wake_log_root;
3116 }
Yan Zheng7237f182009-01-21 12:54:03 -05003117
Chris Mason257c62e2009-10-13 13:21:08 -04003118 mutex_lock(&root->log_mutex);
3119 if (root->last_log_commit < log_transid)
3120 root->last_log_commit = log_transid;
3121 mutex_unlock(&root->log_mutex);
3122
Chris Mason12fcfd22009-03-24 10:24:20 -04003123out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07003124 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003125 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3126
Miao Xied1433de2014-02-20 18:08:59 +08003127 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003128 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003129 mutex_unlock(&log_root_tree->log_mutex);
3130
David Sterba33a9eca2015-10-10 18:35:10 +02003131 /*
3132 * The barrier before waitqueue_active is implied by mutex_unlock
3133 */
Yan Zheng7237f182009-01-21 12:54:03 -05003134 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
3135 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04003136out:
Miao Xied1433de2014-02-20 18:08:59 +08003137 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07003138 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08003139 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05003140 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08003141 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08003142
David Sterba33a9eca2015-10-10 18:35:10 +02003143 /*
3144 * The barrier before waitqueue_active is implied by mutex_unlock
3145 */
Yan Zheng7237f182009-01-21 12:54:03 -05003146 if (waitqueue_active(&root->log_commit_wait[index1]))
3147 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003148 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003149}
3150
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003151static void free_log_tree(struct btrfs_trans_handle *trans,
3152 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003153{
3154 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04003155 u64 start;
3156 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04003157 struct walk_control wc = {
3158 .free = 1,
3159 .process_func = process_one_buffer
3160 };
3161
Josef Bacik681ae502013-10-07 15:11:00 -04003162 ret = walk_log_tree(trans, log, &wc);
3163 /* I don't think this can happen but just in case */
3164 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003165 btrfs_abort_transaction(trans, ret);
Chris Masone02119d2008-09-05 16:13:11 -04003166
Chris Masond3977122009-01-05 21:25:51 -05003167 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003168 ret = find_first_extent_bit(&log->dirty_log_pages,
Liu Bo55237a52018-01-25 11:02:52 -07003169 0, &start, &end,
3170 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
Josef Bacike6138872012-09-27 17:07:30 -04003171 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003172 if (ret)
3173 break;
3174
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003175 clear_extent_bits(&log->dirty_log_pages, start, end,
Liu Bo55237a52018-01-25 11:02:52 -07003176 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Chris Masond0c803c2008-09-11 16:17:57 -04003177 }
3178
Josef Bacik2ab28f32012-10-12 15:27:49 -04003179 /*
3180 * We may have short-circuited the log tree with the full commit logic
3181 * and left ordered extents on our list, so clear these out to keep us
3182 * from leaking inodes and memory.
3183 */
3184 btrfs_free_logged_extents(log, 0);
3185 btrfs_free_logged_extents(log, 1);
3186
Yan Zheng7237f182009-01-21 12:54:03 -05003187 free_extent_buffer(log->node);
3188 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003189}
3190
3191/*
3192 * free all the extents used by the tree log. This should be called
3193 * at commit time of the full transaction
3194 */
3195int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3196{
3197 if (root->log_root) {
3198 free_log_tree(trans, root->log_root);
3199 root->log_root = NULL;
3200 }
3201 return 0;
3202}
3203
3204int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3205 struct btrfs_fs_info *fs_info)
3206{
3207 if (fs_info->log_root_tree) {
3208 free_log_tree(trans, fs_info->log_root_tree);
3209 fs_info->log_root_tree = NULL;
3210 }
Chris Masone02119d2008-09-05 16:13:11 -04003211 return 0;
3212}
3213
3214/*
Chris Masone02119d2008-09-05 16:13:11 -04003215 * If both a file and directory are logged, and unlinks or renames are
3216 * mixed in, we have a few interesting corners:
3217 *
3218 * create file X in dir Y
3219 * link file X to X.link in dir Y
3220 * fsync file X
3221 * unlink file X but leave X.link
3222 * fsync dir Y
3223 *
3224 * After a crash we would expect only X.link to exist. But file X
3225 * didn't get fsync'd again so the log has back refs for X and X.link.
3226 *
3227 * We solve this by removing directory entries and inode backrefs from the
3228 * log when a file that was logged in the current transaction is
3229 * unlinked. Any later fsync will include the updated log entries, and
3230 * we'll be able to reconstruct the proper directory items from backrefs.
3231 *
3232 * This optimizations allows us to avoid relogging the entire inode
3233 * or the entire directory.
3234 */
3235int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3236 struct btrfs_root *root,
3237 const char *name, int name_len,
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003238 struct btrfs_inode *dir, u64 index)
Chris Masone02119d2008-09-05 16:13:11 -04003239{
3240 struct btrfs_root *log;
3241 struct btrfs_dir_item *di;
3242 struct btrfs_path *path;
3243 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003244 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003245 int bytes_del = 0;
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003246 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003247
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003248 if (dir->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003249 return 0;
3250
Chris Masone02119d2008-09-05 16:13:11 -04003251 ret = join_running_log_trans(root);
3252 if (ret)
3253 return 0;
3254
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003255 mutex_lock(&dir->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003256
3257 log = root->log_root;
3258 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003259 if (!path) {
3260 err = -ENOMEM;
3261 goto out_unlock;
3262 }
liubo2a29edc2011-01-26 06:22:08 +00003263
Li Zefan33345d012011-04-20 10:31:50 +08003264 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003265 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003266 if (IS_ERR(di)) {
3267 err = PTR_ERR(di);
3268 goto fail;
3269 }
3270 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003271 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3272 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003273 if (ret) {
3274 err = ret;
3275 goto fail;
3276 }
Chris Masone02119d2008-09-05 16:13:11 -04003277 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003278 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003279 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003280 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003281 if (IS_ERR(di)) {
3282 err = PTR_ERR(di);
3283 goto fail;
3284 }
3285 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003286 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3287 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003288 if (ret) {
3289 err = ret;
3290 goto fail;
3291 }
Chris Masone02119d2008-09-05 16:13:11 -04003292 }
3293
3294 /* update the directory size in the log to reflect the names
3295 * we have removed
3296 */
3297 if (bytes_del) {
3298 struct btrfs_key key;
3299
Li Zefan33345d012011-04-20 10:31:50 +08003300 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003301 key.offset = 0;
3302 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003303 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003304
3305 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003306 if (ret < 0) {
3307 err = ret;
3308 goto fail;
3309 }
Chris Masone02119d2008-09-05 16:13:11 -04003310 if (ret == 0) {
3311 struct btrfs_inode_item *item;
3312 u64 i_size;
3313
3314 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3315 struct btrfs_inode_item);
3316 i_size = btrfs_inode_size(path->nodes[0], item);
3317 if (i_size > bytes_del)
3318 i_size -= bytes_del;
3319 else
3320 i_size = 0;
3321 btrfs_set_inode_size(path->nodes[0], item, i_size);
3322 btrfs_mark_buffer_dirty(path->nodes[0]);
3323 } else
3324 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003325 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003326 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003327fail:
Chris Masone02119d2008-09-05 16:13:11 -04003328 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003329out_unlock:
Nikolay Borisov49f34d12017-01-18 00:31:32 +02003330 mutex_unlock(&dir->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003331 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003332 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003333 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003334 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003335 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003336
Chris Mason12fcfd22009-03-24 10:24:20 -04003337 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003338
Andi Kleen411fc6b2010-10-29 15:14:31 -04003339 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003340}
3341
3342/* see comments for btrfs_del_dir_entries_in_log */
3343int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3344 struct btrfs_root *root,
3345 const char *name, int name_len,
Nikolay Borisova491abb2017-01-18 00:31:33 +02003346 struct btrfs_inode *inode, u64 dirid)
Chris Masone02119d2008-09-05 16:13:11 -04003347{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003348 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04003349 struct btrfs_root *log;
3350 u64 index;
3351 int ret;
3352
Nikolay Borisova491abb2017-01-18 00:31:33 +02003353 if (inode->logged_trans < trans->transid)
Chris Mason3a5f1d42008-09-11 15:53:37 -04003354 return 0;
3355
Chris Masone02119d2008-09-05 16:13:11 -04003356 ret = join_running_log_trans(root);
3357 if (ret)
3358 return 0;
3359 log = root->log_root;
Nikolay Borisova491abb2017-01-18 00:31:33 +02003360 mutex_lock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003361
Nikolay Borisova491abb2017-01-18 00:31:33 +02003362 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003363 dirid, &index);
Nikolay Borisova491abb2017-01-18 00:31:33 +02003364 mutex_unlock(&inode->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003365 if (ret == -ENOSPC) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003366 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003367 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003368 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003369 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003370 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003371
Chris Masone02119d2008-09-05 16:13:11 -04003372 return ret;
3373}
3374
3375/*
3376 * creates a range item in the log for 'dirid'. first_offset and
3377 * last_offset tell us which parts of the key space the log should
3378 * be considered authoritative for.
3379 */
3380static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3381 struct btrfs_root *log,
3382 struct btrfs_path *path,
3383 int key_type, u64 dirid,
3384 u64 first_offset, u64 last_offset)
3385{
3386 int ret;
3387 struct btrfs_key key;
3388 struct btrfs_dir_log_item *item;
3389
3390 key.objectid = dirid;
3391 key.offset = first_offset;
3392 if (key_type == BTRFS_DIR_ITEM_KEY)
3393 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3394 else
3395 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3396 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003397 if (ret)
3398 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003399
3400 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3401 struct btrfs_dir_log_item);
3402 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3403 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003404 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003405 return 0;
3406}
3407
3408/*
3409 * log all the items included in the current transaction for a given
3410 * directory. This also creates the range items in the log tree required
3411 * to replay anything deleted before the fsync
3412 */
3413static noinline int log_dir_items(struct btrfs_trans_handle *trans,
Nikolay Borisov684a5772017-01-18 00:31:41 +02003414 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003415 struct btrfs_path *path,
3416 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003417 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003418 u64 min_offset, u64 *last_offset_ret)
3419{
3420 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003421 struct btrfs_root *log = root->log_root;
3422 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003423 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003424 int ret;
3425 int i;
3426 int nritems;
3427 u64 first_offset = min_offset;
3428 u64 last_offset = (u64)-1;
Nikolay Borisov684a5772017-01-18 00:31:41 +02003429 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003430
3431 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003432
Li Zefan33345d012011-04-20 10:31:50 +08003433 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003434 min_key.type = key_type;
3435 min_key.offset = min_offset;
3436
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003437 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003438
3439 /*
3440 * we didn't find anything from this transaction, see if there
3441 * is anything at all
3442 */
Li Zefan33345d012011-04-20 10:31:50 +08003443 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3444 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003445 min_key.type = key_type;
3446 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003447 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003448 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3449 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003450 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003451 return ret;
3452 }
Li Zefan33345d012011-04-20 10:31:50 +08003453 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003454
3455 /* if ret == 0 there are items for this type,
3456 * create a range to tell us the last key of this type.
3457 * otherwise, there are no items in this directory after
3458 * *min_offset, and we create a range to indicate that.
3459 */
3460 if (ret == 0) {
3461 struct btrfs_key tmp;
3462 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3463 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003464 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003465 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003466 }
3467 goto done;
3468 }
3469
3470 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003471 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003472 if (ret == 0) {
3473 struct btrfs_key tmp;
3474 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3475 if (key_type == tmp.type) {
3476 first_offset = tmp.offset;
3477 ret = overwrite_item(trans, log, dst_path,
3478 path->nodes[0], path->slots[0],
3479 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003480 if (ret) {
3481 err = ret;
3482 goto done;
3483 }
Chris Masone02119d2008-09-05 16:13:11 -04003484 }
3485 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003486 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003487
3488 /* find the first key from this transaction again */
3489 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303490 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003491 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003492
3493 /*
3494 * we have a block from this transaction, log every item in it
3495 * from our directory
3496 */
Chris Masond3977122009-01-05 21:25:51 -05003497 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003498 struct btrfs_key tmp;
3499 src = path->nodes[0];
3500 nritems = btrfs_header_nritems(src);
3501 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003502 struct btrfs_dir_item *di;
3503
Chris Masone02119d2008-09-05 16:13:11 -04003504 btrfs_item_key_to_cpu(src, &min_key, i);
3505
Li Zefan33345d012011-04-20 10:31:50 +08003506 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003507 goto done;
3508 ret = overwrite_item(trans, log, dst_path, src, i,
3509 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003510 if (ret) {
3511 err = ret;
3512 goto done;
3513 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003514
3515 /*
3516 * We must make sure that when we log a directory entry,
3517 * the corresponding inode, after log replay, has a
3518 * matching link count. For example:
3519 *
3520 * touch foo
3521 * mkdir mydir
3522 * sync
3523 * ln foo mydir/bar
3524 * xfs_io -c "fsync" mydir
3525 * <crash>
3526 * <mount fs and log replay>
3527 *
3528 * Would result in a fsync log that when replayed, our
3529 * file inode would have a link count of 1, but we get
3530 * two directory entries pointing to the same inode.
3531 * After removing one of the names, it would not be
3532 * possible to remove the other name, which resulted
3533 * always in stale file handle errors, and would not
3534 * be possible to rmdir the parent directory, since
3535 * its i_size could never decrement to the value
3536 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3537 */
3538 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3539 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3540 if (ctx &&
3541 (btrfs_dir_transid(src, di) == trans->transid ||
3542 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3543 tmp.type != BTRFS_ROOT_ITEM_KEY)
3544 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003545 }
3546 path->slots[0] = nritems;
3547
3548 /*
3549 * look ahead to the next item and see if it is also
3550 * from this directory and from this transaction
3551 */
3552 ret = btrfs_next_leaf(root, path);
Liu Bo80c0b422018-04-03 01:59:47 +08003553 if (ret) {
3554 if (ret == 1)
3555 last_offset = (u64)-1;
3556 else
3557 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003558 goto done;
3559 }
3560 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003561 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003562 last_offset = (u64)-1;
3563 goto done;
3564 }
3565 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3566 ret = overwrite_item(trans, log, dst_path,
3567 path->nodes[0], path->slots[0],
3568 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003569 if (ret)
3570 err = ret;
3571 else
3572 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003573 goto done;
3574 }
3575 }
3576done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003577 btrfs_release_path(path);
3578 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003579
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003580 if (err == 0) {
3581 *last_offset_ret = last_offset;
3582 /*
3583 * insert the log range keys to indicate where the log
3584 * is valid
3585 */
3586 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003587 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003588 if (ret)
3589 err = ret;
3590 }
3591 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003592}
3593
3594/*
3595 * logging directories is very similar to logging inodes, We find all the items
3596 * from the current transaction and write them to the log.
3597 *
3598 * The recovery code scans the directory in the subvolume, and if it finds a
3599 * key in the range logged that is not present in the log tree, then it means
3600 * that dir entry was unlinked during the transaction.
3601 *
3602 * In order for that scan to work, we must include one key smaller than
3603 * the smallest logged by this transaction and one key larger than the largest
3604 * key logged by this transaction.
3605 */
3606static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003607 struct btrfs_root *root, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04003608 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003609 struct btrfs_path *dst_path,
3610 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003611{
3612 u64 min_key;
3613 u64 max_key;
3614 int ret;
3615 int key_type = BTRFS_DIR_ITEM_KEY;
3616
3617again:
3618 min_key = 0;
3619 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003620 while (1) {
Nikolay Borisovdbf39ea2017-01-18 00:31:42 +02003621 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3622 ctx, min_key, &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003623 if (ret)
3624 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003625 if (max_key == (u64)-1)
3626 break;
3627 min_key = max_key + 1;
3628 }
3629
3630 if (key_type == BTRFS_DIR_ITEM_KEY) {
3631 key_type = BTRFS_DIR_INDEX_KEY;
3632 goto again;
3633 }
3634 return 0;
3635}
3636
3637/*
3638 * a helper function to drop items from the log before we relog an
3639 * inode. max_key_type indicates the highest item type to remove.
3640 * This cannot be run for file data extents because it does not
3641 * free the extents they point to.
3642 */
3643static int drop_objectid_items(struct btrfs_trans_handle *trans,
3644 struct btrfs_root *log,
3645 struct btrfs_path *path,
3646 u64 objectid, int max_key_type)
3647{
3648 int ret;
3649 struct btrfs_key key;
3650 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003651 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003652
3653 key.objectid = objectid;
3654 key.type = max_key_type;
3655 key.offset = (u64)-1;
3656
Chris Masond3977122009-01-05 21:25:51 -05003657 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003658 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003659 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003660 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003661 break;
3662
3663 if (path->slots[0] == 0)
3664 break;
3665
3666 path->slots[0]--;
3667 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3668 path->slots[0]);
3669
3670 if (found_key.objectid != objectid)
3671 break;
3672
Josef Bacik18ec90d2012-09-28 11:56:28 -04003673 found_key.offset = 0;
3674 found_key.type = 0;
3675 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3676 &start_slot);
3677
3678 ret = btrfs_del_items(trans, log, path, start_slot,
3679 path->slots[0] - start_slot + 1);
3680 /*
3681 * If start slot isn't 0 then we don't need to re-search, we've
3682 * found the last guy with the objectid in this tree.
3683 */
3684 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003685 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003686 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003687 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003688 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003689 if (ret > 0)
3690 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003691 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003692}
3693
Josef Bacik94edf4a2012-09-25 14:56:25 -04003694static void fill_inode_item(struct btrfs_trans_handle *trans,
3695 struct extent_buffer *leaf,
3696 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003697 struct inode *inode, int log_inode_only,
3698 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003699{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003700 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003701
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003702 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003703
3704 if (log_inode_only) {
3705 /* set the generation to zero so the recover code
3706 * can tell the difference between an logging
3707 * just to say 'this inode exists' and a logging
3708 * to say 'update this inode with these values'
3709 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003710 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003711 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003712 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003713 btrfs_set_token_inode_generation(leaf, item,
3714 BTRFS_I(inode)->generation,
3715 &token);
3716 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003717 }
3718
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003719 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3720 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3721 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3722 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3723
David Sterbaa937b972014-12-12 17:39:12 +01003724 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003725 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003726 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003727 inode->i_atime.tv_nsec, &token);
3728
David Sterbaa937b972014-12-12 17:39:12 +01003729 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003730 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003731 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003732 inode->i_mtime.tv_nsec, &token);
3733
David Sterbaa937b972014-12-12 17:39:12 +01003734 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003735 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003736 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003737 inode->i_ctime.tv_nsec, &token);
3738
3739 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3740 &token);
3741
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003742 btrfs_set_token_inode_sequence(leaf, item,
3743 inode_peek_iversion(inode), &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003744 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3745 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3746 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3747 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003748}
3749
Josef Bacika95249b2012-10-11 16:17:34 -04003750static int log_inode_item(struct btrfs_trans_handle *trans,
3751 struct btrfs_root *log, struct btrfs_path *path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003752 struct btrfs_inode *inode)
Josef Bacika95249b2012-10-11 16:17:34 -04003753{
3754 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003755 int ret;
3756
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003757 ret = btrfs_insert_empty_item(trans, log, path,
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003758 &inode->location, sizeof(*inode_item));
Josef Bacika95249b2012-10-11 16:17:34 -04003759 if (ret && ret != -EEXIST)
3760 return ret;
3761 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3762 struct btrfs_inode_item);
Nikolay Borisov6d889a32017-01-18 00:31:47 +02003763 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3764 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003765 btrfs_release_path(path);
3766 return 0;
3767}
3768
Chris Mason31ff1cd2008-09-11 16:17:57 -04003769static noinline int copy_items(struct btrfs_trans_handle *trans,
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003770 struct btrfs_inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003771 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003772 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003773 int start_slot, int nr, int inode_only,
3774 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003775{
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003776 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003777 unsigned long src_offset;
3778 unsigned long dst_offset;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003779 struct btrfs_root *log = inode->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003780 struct btrfs_file_extent_item *extent;
3781 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003782 struct extent_buffer *src = src_path->nodes[0];
3783 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003784 int ret;
3785 struct btrfs_key *ins_keys;
3786 u32 *ins_sizes;
3787 char *ins_data;
3788 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003789 struct list_head ordered_sums;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003790 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003791 bool has_extents = false;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003792 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003793 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003794
3795 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003796
3797 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3798 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003799 if (!ins_data)
3800 return -ENOMEM;
3801
Josef Bacik16e75492013-10-22 12:18:51 -04003802 first_key.objectid = (u64)-1;
3803
Chris Mason31ff1cd2008-09-11 16:17:57 -04003804 ins_sizes = (u32 *)ins_data;
3805 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3806
3807 for (i = 0; i < nr; i++) {
3808 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3809 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3810 }
3811 ret = btrfs_insert_empty_items(trans, log, dst_path,
3812 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003813 if (ret) {
3814 kfree(ins_data);
3815 return ret;
3816 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003817
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003818 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003819 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3820 dst_path->slots[0]);
3821
3822 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3823
Matthias Kaehlcke0dde10b2017-07-27 14:30:23 -07003824 if (i == nr - 1)
Josef Bacik16e75492013-10-22 12:18:51 -04003825 last_key = ins_keys[i];
3826
Josef Bacik94edf4a2012-09-25 14:56:25 -04003827 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003828 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3829 dst_path->slots[0],
3830 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003831 fill_inode_item(trans, dst_path->nodes[0], inode_item,
David Sterbaf85b7372017-01-20 14:54:07 +01003832 &inode->vfs_inode,
3833 inode_only == LOG_INODE_EXISTS,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003834 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003835 } else {
3836 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3837 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003838 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003839
Josef Bacik16e75492013-10-22 12:18:51 -04003840 /*
3841 * We set need_find_last_extent here in case we know we were
3842 * processing other items and then walk into the first extent in
3843 * the inode. If we don't hit an extent then nothing changes,
3844 * we'll do the last search the next time around.
3845 */
3846 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3847 has_extents = true;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003848 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003849 first_key = ins_keys[i];
3850 } else {
3851 need_find_last_extent = false;
3852 }
3853
Chris Mason31ff1cd2008-09-11 16:17:57 -04003854 /* take a reference on file data extents so that truncates
3855 * or deletes of this inode don't have to relog the inode
3856 * again
3857 */
David Sterba962a2982014-06-04 18:41:45 +02003858 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003859 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003860 int found_type;
3861 extent = btrfs_item_ptr(src, start_slot + i,
3862 struct btrfs_file_extent_item);
3863
liubo8e531cd2011-05-06 10:36:09 +08003864 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3865 continue;
3866
Chris Mason31ff1cd2008-09-11 16:17:57 -04003867 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003868 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003869 u64 ds, dl, cs, cl;
3870 ds = btrfs_file_extent_disk_bytenr(src,
3871 extent);
3872 /* ds == 0 is a hole */
3873 if (ds == 0)
3874 continue;
3875
3876 dl = btrfs_file_extent_disk_num_bytes(src,
3877 extent);
3878 cs = btrfs_file_extent_offset(src, extent);
3879 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003880 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003881 if (btrfs_file_extent_compression(src,
3882 extent)) {
3883 cs = 0;
3884 cl = dl;
3885 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003886
3887 ret = btrfs_lookup_csums_range(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003888 fs_info->csum_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003889 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003890 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003891 if (ret) {
3892 btrfs_release_path(dst_path);
3893 kfree(ins_data);
3894 return ret;
3895 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003896 }
3897 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003898 }
3899
3900 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003901 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003902 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003903
3904 /*
3905 * we have to do this after the loop above to avoid changing the
3906 * log tree while trying to change the log tree.
3907 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003908 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003909 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003910 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3911 struct btrfs_ordered_sum,
3912 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003913 if (!ret)
3914 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003915 list_del(&sums->list);
3916 kfree(sums);
3917 }
Josef Bacik16e75492013-10-22 12:18:51 -04003918
3919 if (!has_extents)
3920 return ret;
3921
Filipe Manana74121f7c2014-08-07 12:00:44 +01003922 if (need_find_last_extent && *last_extent == first_key.offset) {
3923 /*
3924 * We don't have any leafs between our current one and the one
3925 * we processed before that can have file extent items for our
3926 * inode (and have a generation number smaller than our current
3927 * transaction id).
3928 */
3929 need_find_last_extent = false;
3930 }
3931
Josef Bacik16e75492013-10-22 12:18:51 -04003932 /*
3933 * Because we use btrfs_search_forward we could skip leaves that were
3934 * not modified and then assume *last_extent is valid when it really
3935 * isn't. So back up to the previous leaf and read the end of the last
3936 * extent before we go and fill in holes.
3937 */
3938 if (need_find_last_extent) {
3939 u64 len;
3940
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003941 ret = btrfs_prev_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04003942 if (ret < 0)
3943 return ret;
3944 if (ret)
3945 goto fill_holes;
3946 if (src_path->slots[0])
3947 src_path->slots[0]--;
3948 src = src_path->nodes[0];
3949 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
Nikolay Borisov44d70e12017-01-18 00:31:36 +02003950 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04003951 key.type != BTRFS_EXTENT_DATA_KEY)
3952 goto fill_holes;
3953 extent = btrfs_item_ptr(src, src_path->slots[0],
3954 struct btrfs_file_extent_item);
3955 if (btrfs_file_extent_type(src, extent) ==
3956 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003957 len = btrfs_file_extent_inline_len(src,
3958 src_path->slots[0],
3959 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003960 *last_extent = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003961 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04003962 } else {
3963 len = btrfs_file_extent_num_bytes(src, extent);
3964 *last_extent = key.offset + len;
3965 }
3966 }
3967fill_holes:
3968 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3969 * things could have happened
3970 *
3971 * 1) A merge could have happened, so we could currently be on a leaf
3972 * that holds what we were copying in the first place.
3973 * 2) A split could have happened, and now not all of the items we want
3974 * are on the same leaf.
3975 *
3976 * So we need to adjust how we search for holes, we need to drop the
3977 * path and re-search for the first extent key we found, and then walk
3978 * forward until we hit the last one we copied.
3979 */
3980 if (need_find_last_extent) {
3981 /* btrfs_prev_leaf could return 1 without releasing the path */
3982 btrfs_release_path(src_path);
David Sterbaf85b7372017-01-20 14:54:07 +01003983 ret = btrfs_search_slot(NULL, inode->root, &first_key,
3984 src_path, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04003985 if (ret < 0)
3986 return ret;
3987 ASSERT(ret == 0);
3988 src = src_path->nodes[0];
3989 i = src_path->slots[0];
3990 } else {
3991 i = start_slot;
3992 }
3993
3994 /*
3995 * Ok so here we need to go through and fill in any holes we may have
3996 * to make sure that holes are punched for those areas in case they had
3997 * extents previously.
3998 */
3999 while (!done) {
4000 u64 offset, len;
4001 u64 extent_end;
4002
4003 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004004 ret = btrfs_next_leaf(inode->root, src_path);
Josef Bacik16e75492013-10-22 12:18:51 -04004005 if (ret < 0)
4006 return ret;
4007 ASSERT(ret == 0);
4008 src = src_path->nodes[0];
4009 i = 0;
Filipe Manana8434ec42018-03-26 23:59:12 +01004010 need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04004011 }
4012
4013 btrfs_item_key_to_cpu(src, &key, i);
4014 if (!btrfs_comp_cpu_keys(&key, &last_key))
4015 done = true;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004016 if (key.objectid != btrfs_ino(inode) ||
Josef Bacik16e75492013-10-22 12:18:51 -04004017 key.type != BTRFS_EXTENT_DATA_KEY) {
4018 i++;
4019 continue;
4020 }
4021 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
4022 if (btrfs_file_extent_type(src, extent) ==
4023 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004024 len = btrfs_file_extent_inline_len(src, i, extent);
Jeff Mahoneyda170662016-06-15 09:22:56 -04004025 extent_end = ALIGN(key.offset + len,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004026 fs_info->sectorsize);
Josef Bacik16e75492013-10-22 12:18:51 -04004027 } else {
4028 len = btrfs_file_extent_num_bytes(src, extent);
4029 extent_end = key.offset + len;
4030 }
4031 i++;
4032
4033 if (*last_extent == key.offset) {
4034 *last_extent = extent_end;
4035 continue;
4036 }
4037 offset = *last_extent;
4038 len = key.offset - *last_extent;
Nikolay Borisov44d70e12017-01-18 00:31:36 +02004039 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01004040 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004041 if (ret)
4042 break;
Filipe Manana74121f7c2014-08-07 12:00:44 +01004043 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04004044 }
Filipe Manana4ee3fad2018-03-26 23:59:00 +01004045
4046 /*
4047 * Check if there is a hole between the last extent found in our leaf
4048 * and the first extent in the next leaf. If there is one, we need to
4049 * log an explicit hole so that at replay time we can punch the hole.
4050 */
4051 if (ret == 0 &&
4052 key.objectid == btrfs_ino(inode) &&
4053 key.type == BTRFS_EXTENT_DATA_KEY &&
4054 i == btrfs_header_nritems(src_path->nodes[0])) {
4055 ret = btrfs_next_leaf(inode->root, src_path);
4056 need_find_last_extent = true;
4057 if (ret > 0) {
4058 ret = 0;
4059 } else if (ret == 0) {
4060 btrfs_item_key_to_cpu(src_path->nodes[0], &key,
4061 src_path->slots[0]);
4062 if (key.objectid == btrfs_ino(inode) &&
4063 key.type == BTRFS_EXTENT_DATA_KEY &&
4064 *last_extent < key.offset) {
4065 const u64 len = key.offset - *last_extent;
4066
4067 ret = btrfs_insert_file_extent(trans, log,
4068 btrfs_ino(inode),
4069 *last_extent, 0,
4070 0, len, 0, len,
4071 0, 0, 0);
4072 }
4073 }
4074 }
Josef Bacik16e75492013-10-22 12:18:51 -04004075 /*
4076 * Need to let the callers know we dropped the path so they should
4077 * re-search.
4078 */
4079 if (!ret && need_find_last_extent)
4080 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004081 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004082}
4083
Josef Bacik5dc562c2012-08-17 13:14:17 -04004084static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
4085{
4086 struct extent_map *em1, *em2;
4087
4088 em1 = list_entry(a, struct extent_map, list);
4089 em2 = list_entry(b, struct extent_map, list);
4090
4091 if (em1->start < em2->start)
4092 return -1;
4093 else if (em1->start > em2->start)
4094 return 1;
4095 return 0;
4096}
4097
Filipe Manana8407f552014-09-05 15:14:39 +01004098static int wait_ordered_extents(struct btrfs_trans_handle *trans,
4099 struct inode *inode,
4100 struct btrfs_root *root,
4101 const struct extent_map *em,
4102 const struct list_head *logged_list,
4103 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004104{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004105 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004106 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01004107 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004108 u64 mod_start = em->mod_start;
4109 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01004110 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004111 u64 csum_offset;
4112 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01004113 LIST_HEAD(ordered_sums);
4114 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004115
Filipe Manana8407f552014-09-05 15:14:39 +01004116 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04004117
Filipe Manana8407f552014-09-05 15:14:39 +01004118 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
4119 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04004120 return 0;
4121
Josef Bacik2ab28f32012-10-12 15:27:49 -04004122 /*
Filipe Manana8407f552014-09-05 15:14:39 +01004123 * Wait far any ordered extent that covers our extent map. If it
4124 * finishes without an error, first check and see if our csums are on
4125 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04004126 */
Miao Xie827463c2014-01-14 20:31:51 +08004127 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04004128 struct btrfs_ordered_sum *sum;
4129
4130 if (!mod_len)
4131 break;
4132
Josef Bacik2ab28f32012-10-12 15:27:49 -04004133 if (ordered->file_offset + ordered->len <= mod_start ||
4134 mod_start + mod_len <= ordered->file_offset)
4135 continue;
4136
Filipe Manana8407f552014-09-05 15:14:39 +01004137 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
4138 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
4139 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
4140 const u64 start = ordered->file_offset;
4141 const u64 end = ordered->file_offset + ordered->len - 1;
4142
4143 WARN_ON(ordered->inode != inode);
4144 filemap_fdatawrite_range(inode->i_mapping, start, end);
4145 }
4146
4147 wait_event(ordered->wait,
4148 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
4149 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
4150
4151 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00004152 /*
4153 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
4154 * i_mapping flags, so that the next fsync won't get
4155 * an outdated io error too.
4156 */
Miklos Szeredif0312212016-09-16 12:44:21 +02004157 filemap_check_errors(inode->i_mapping);
Filipe Manana8407f552014-09-05 15:14:39 +01004158 *ordered_io_error = true;
4159 break;
4160 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004161 /*
4162 * We are going to copy all the csums on this ordered extent, so
4163 * go ahead and adjust mod_start and mod_len in case this
4164 * ordered extent has already been logged.
4165 */
4166 if (ordered->file_offset > mod_start) {
4167 if (ordered->file_offset + ordered->len >=
4168 mod_start + mod_len)
4169 mod_len = ordered->file_offset - mod_start;
4170 /*
4171 * If we have this case
4172 *
4173 * |--------- logged extent ---------|
4174 * |----- ordered extent ----|
4175 *
4176 * Just don't mess with mod_start and mod_len, we'll
4177 * just end up logging more csums than we need and it
4178 * will be ok.
4179 */
4180 } else {
4181 if (ordered->file_offset + ordered->len <
4182 mod_start + mod_len) {
4183 mod_len = (mod_start + mod_len) -
4184 (ordered->file_offset + ordered->len);
4185 mod_start = ordered->file_offset +
4186 ordered->len;
4187 } else {
4188 mod_len = 0;
4189 }
4190 }
4191
Filipe Manana8407f552014-09-05 15:14:39 +01004192 if (skip_csum)
4193 continue;
4194
Josef Bacik2ab28f32012-10-12 15:27:49 -04004195 /*
4196 * To keep us from looping for the above case of an ordered
4197 * extent that falls inside of the logged extent.
4198 */
4199 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4200 &ordered->flags))
4201 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004202
Josef Bacik2ab28f32012-10-12 15:27:49 -04004203 list_for_each_entry(sum, &ordered->list, list) {
4204 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08004205 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01004206 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004207 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004208 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004209
Filipe Manana8407f552014-09-05 15:14:39 +01004210 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04004211 return ret;
4212
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004213 if (em->compress_type) {
4214 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004215 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004216 } else {
4217 csum_offset = mod_start - em->start;
4218 csum_len = mod_len;
4219 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004220
Josef Bacik70c8a912012-10-11 16:54:30 -04004221 /* block start is already adjusted for the file extent offset. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004222 ret = btrfs_lookup_csums_range(fs_info->csum_root,
Josef Bacik70c8a912012-10-11 16:54:30 -04004223 em->block_start + csum_offset,
4224 em->block_start + csum_offset +
4225 csum_len - 1, &ordered_sums, 0);
4226 if (ret)
4227 return ret;
4228
4229 while (!list_empty(&ordered_sums)) {
4230 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4231 struct btrfs_ordered_sum,
4232 list);
4233 if (!ret)
4234 ret = btrfs_csum_file_blocks(trans, log, sums);
4235 list_del(&sums->list);
4236 kfree(sums);
4237 }
4238
4239 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004240}
4241
Filipe Manana8407f552014-09-05 15:14:39 +01004242static int log_one_extent(struct btrfs_trans_handle *trans,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004243 struct btrfs_inode *inode, struct btrfs_root *root,
Filipe Manana8407f552014-09-05 15:14:39 +01004244 const struct extent_map *em,
4245 struct btrfs_path *path,
4246 const struct list_head *logged_list,
4247 struct btrfs_log_ctx *ctx)
4248{
4249 struct btrfs_root *log = root->log_root;
4250 struct btrfs_file_extent_item *fi;
4251 struct extent_buffer *leaf;
4252 struct btrfs_map_token token;
4253 struct btrfs_key key;
4254 u64 extent_offset = em->start - em->orig_start;
4255 u64 block_len;
4256 int ret;
4257 int extent_inserted = 0;
4258 bool ordered_io_err = false;
4259
David Sterbaf85b7372017-01-20 14:54:07 +01004260 ret = wait_ordered_extents(trans, &inode->vfs_inode, root, em,
4261 logged_list, &ordered_io_err);
Filipe Manana8407f552014-09-05 15:14:39 +01004262 if (ret)
4263 return ret;
4264
4265 if (ordered_io_err) {
4266 ctx->io_err = -EIO;
Liu Boebb70442017-11-21 14:35:40 -07004267 return ctx->io_err;
Filipe Manana8407f552014-09-05 15:14:39 +01004268 }
4269
4270 btrfs_init_map_token(&token);
4271
Nikolay Borisov9d122622017-01-18 00:31:40 +02004272 ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
Filipe Manana8407f552014-09-05 15:14:39 +01004273 em->start + em->len, NULL, 0, 1,
4274 sizeof(*fi), &extent_inserted);
4275 if (ret)
4276 return ret;
4277
4278 if (!extent_inserted) {
Nikolay Borisov9d122622017-01-18 00:31:40 +02004279 key.objectid = btrfs_ino(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01004280 key.type = BTRFS_EXTENT_DATA_KEY;
4281 key.offset = em->start;
4282
4283 ret = btrfs_insert_empty_item(trans, log, path, &key,
4284 sizeof(*fi));
4285 if (ret)
4286 return ret;
4287 }
4288 leaf = path->nodes[0];
4289 fi = btrfs_item_ptr(leaf, path->slots[0],
4290 struct btrfs_file_extent_item);
4291
Josef Bacik50d9aa92014-11-21 14:52:38 -05004292 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004293 &token);
4294 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4295 btrfs_set_token_file_extent_type(leaf, fi,
4296 BTRFS_FILE_EXTENT_PREALLOC,
4297 &token);
4298 else
4299 btrfs_set_token_file_extent_type(leaf, fi,
4300 BTRFS_FILE_EXTENT_REG,
4301 &token);
4302
4303 block_len = max(em->block_len, em->orig_block_len);
4304 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4305 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4306 em->block_start,
4307 &token);
4308 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4309 &token);
4310 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4311 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4312 em->block_start -
4313 extent_offset, &token);
4314 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4315 &token);
4316 } else {
4317 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4318 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4319 &token);
4320 }
4321
4322 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4323 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4324 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4325 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4326 &token);
4327 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4328 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4329 btrfs_mark_buffer_dirty(leaf);
4330
4331 btrfs_release_path(path);
4332
4333 return ret;
4334}
4335
Josef Bacik5dc562c2012-08-17 13:14:17 -04004336static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4337 struct btrfs_root *root,
Nikolay Borisov9d122622017-01-18 00:31:40 +02004338 struct btrfs_inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004339 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004340 struct list_head *logged_list,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004341 struct btrfs_log_ctx *ctx,
4342 const u64 start,
4343 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004344{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004345 struct extent_map *em, *n;
4346 struct list_head extents;
Nikolay Borisov9d122622017-01-18 00:31:40 +02004347 struct extent_map_tree *tree = &inode->extent_tree;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004348 u64 logged_start, logged_end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004349 u64 test_gen;
4350 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004351 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004352
4353 INIT_LIST_HEAD(&extents);
4354
Nikolay Borisov9d122622017-01-18 00:31:40 +02004355 down_write(&inode->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004356 write_lock(&tree->lock);
4357 test_gen = root->fs_info->last_trans_committed;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004358 logged_start = start;
4359 logged_end = end;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004360
4361 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4362 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004363 /*
4364 * Just an arbitrary number, this can be really CPU intensive
4365 * once we start getting a lot of extents, and really once we
4366 * have a bunch of extents we just want to commit since it will
4367 * be faster.
4368 */
4369 if (++num > 32768) {
4370 list_del_init(&tree->modified_extents);
4371 ret = -EFBIG;
4372 goto process;
4373 }
4374
Josef Bacik5dc562c2012-08-17 13:14:17 -04004375 if (em->generation <= test_gen)
4376 continue;
Josef Bacik8c6c5922017-08-29 10:11:39 -04004377
4378 if (em->start < logged_start)
4379 logged_start = em->start;
4380 if ((em->start + em->len - 1) > logged_end)
4381 logged_end = em->start + em->len - 1;
4382
Josef Bacikff44c6e2012-09-14 12:59:20 -04004383 /* Need a ref to keep it from getting evicted from cache */
Elena Reshetova490b54d2017-03-03 10:55:12 +02004384 refcount_inc(&em->refs);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004385 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004386 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004387 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004388 }
4389
Filipe Manana471d5572018-04-05 22:55:12 +01004390 /*
4391 * Add all prealloc extents beyond the inode's i_size to make sure we
4392 * don't lose them after doing a fast fsync and replaying the log.
4393 */
4394 if (inode->flags & BTRFS_INODE_PREALLOC) {
4395 struct rb_node *node;
4396
4397 for (node = rb_last(&tree->map); node; node = rb_prev(node)) {
4398 em = rb_entry(node, struct extent_map, rb_node);
4399 if (em->start < i_size_read(&inode->vfs_inode))
4400 break;
4401 if (!list_empty(&em->list))
4402 continue;
4403 /* Same as above loop. */
4404 if (++num > 32768) {
4405 list_del_init(&tree->modified_extents);
4406 ret = -EFBIG;
4407 goto process;
4408 }
4409 refcount_inc(&em->refs);
4410 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
4411 list_add_tail(&em->list, &extents);
4412 }
4413 }
4414
Josef Bacik5dc562c2012-08-17 13:14:17 -04004415 list_sort(NULL, &extents, extent_cmp);
Josef Bacik8c6c5922017-08-29 10:11:39 -04004416 btrfs_get_logged_extents(inode, logged_list, logged_start, logged_end);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004417 /*
4418 * Some ordered extents started by fsync might have completed
4419 * before we could collect them into the list logged_list, which
4420 * means they're gone, not in our logged_list nor in the inode's
4421 * ordered tree. We want the application/user space to know an
4422 * error happened while attempting to persist file data so that
4423 * it can take proper action. If such error happened, we leave
4424 * without writing to the log tree and the fsync must report the
4425 * file data write error and not commit the current transaction.
4426 */
Nikolay Borisov9d122622017-01-18 00:31:40 +02004427 ret = filemap_check_errors(inode->vfs_inode.i_mapping);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004428 if (ret)
4429 ctx->io_err = ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004430process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004431 while (!list_empty(&extents)) {
4432 em = list_entry(extents.next, struct extent_map, list);
4433
4434 list_del_init(&em->list);
4435
4436 /*
4437 * If we had an error we just need to delete everybody from our
4438 * private list.
4439 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004440 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004441 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004442 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004443 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004444 }
4445
4446 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004447
Filipe Manana8407f552014-09-05 15:14:39 +01004448 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4449 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004450 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004451 clear_em_logging(tree, em);
4452 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004453 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004454 WARN_ON(!list_empty(&extents));
4455 write_unlock(&tree->lock);
Nikolay Borisov9d122622017-01-18 00:31:40 +02004456 up_write(&inode->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004457
Josef Bacik5dc562c2012-08-17 13:14:17 -04004458 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004459 return ret;
4460}
4461
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004462static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004463 struct btrfs_path *path, u64 *size_ret)
4464{
4465 struct btrfs_key key;
4466 int ret;
4467
Nikolay Borisov481b01c2017-01-18 00:31:34 +02004468 key.objectid = btrfs_ino(inode);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004469 key.type = BTRFS_INODE_ITEM_KEY;
4470 key.offset = 0;
4471
4472 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4473 if (ret < 0) {
4474 return ret;
4475 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004476 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004477 } else {
4478 struct btrfs_inode_item *item;
4479
4480 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4481 struct btrfs_inode_item);
4482 *size_ret = btrfs_inode_size(path->nodes[0], item);
4483 }
4484
4485 btrfs_release_path(path);
4486 return 0;
4487}
4488
Filipe Manana36283bf2015-06-20 00:44:51 +01004489/*
4490 * At the moment we always log all xattrs. This is to figure out at log replay
4491 * time which xattrs must have their deletion replayed. If a xattr is missing
4492 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4493 * because if a xattr is deleted, the inode is fsynced and a power failure
4494 * happens, causing the log to be replayed the next time the fs is mounted,
4495 * we want the xattr to not exist anymore (same behaviour as other filesystems
4496 * with a journal, ext3/4, xfs, f2fs, etc).
4497 */
4498static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4499 struct btrfs_root *root,
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004500 struct btrfs_inode *inode,
Filipe Manana36283bf2015-06-20 00:44:51 +01004501 struct btrfs_path *path,
4502 struct btrfs_path *dst_path)
4503{
4504 int ret;
4505 struct btrfs_key key;
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004506 const u64 ino = btrfs_ino(inode);
Filipe Manana36283bf2015-06-20 00:44:51 +01004507 int ins_nr = 0;
4508 int start_slot = 0;
4509
4510 key.objectid = ino;
4511 key.type = BTRFS_XATTR_ITEM_KEY;
4512 key.offset = 0;
4513
4514 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4515 if (ret < 0)
4516 return ret;
4517
4518 while (true) {
4519 int slot = path->slots[0];
4520 struct extent_buffer *leaf = path->nodes[0];
4521 int nritems = btrfs_header_nritems(leaf);
4522
4523 if (slot >= nritems) {
4524 if (ins_nr > 0) {
4525 u64 last_extent = 0;
4526
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004527 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004528 &last_extent, start_slot,
4529 ins_nr, 1, 0);
4530 /* can't be 1, extent items aren't processed */
4531 ASSERT(ret <= 0);
4532 if (ret < 0)
4533 return ret;
4534 ins_nr = 0;
4535 }
4536 ret = btrfs_next_leaf(root, path);
4537 if (ret < 0)
4538 return ret;
4539 else if (ret > 0)
4540 break;
4541 continue;
4542 }
4543
4544 btrfs_item_key_to_cpu(leaf, &key, slot);
4545 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4546 break;
4547
4548 if (ins_nr == 0)
4549 start_slot = slot;
4550 ins_nr++;
4551 path->slots[0]++;
4552 cond_resched();
4553 }
4554 if (ins_nr > 0) {
4555 u64 last_extent = 0;
4556
Nikolay Borisov1a93c362017-01-18 00:31:37 +02004557 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01004558 &last_extent, start_slot,
4559 ins_nr, 1, 0);
4560 /* can't be 1, extent items aren't processed */
4561 ASSERT(ret <= 0);
4562 if (ret < 0)
4563 return ret;
4564 }
4565
4566 return 0;
4567}
4568
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004569/*
4570 * If the no holes feature is enabled we need to make sure any hole between the
4571 * last extent and the i_size of our inode is explicitly marked in the log. This
4572 * is to make sure that doing something like:
4573 *
4574 * 1) create file with 128Kb of data
4575 * 2) truncate file to 64Kb
4576 * 3) truncate file to 256Kb
4577 * 4) fsync file
4578 * 5) <crash/power failure>
4579 * 6) mount fs and trigger log replay
4580 *
4581 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4582 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4583 * file correspond to a hole. The presence of explicit holes in a log tree is
4584 * what guarantees that log replay will remove/adjust file extent items in the
4585 * fs/subvol tree.
4586 *
4587 * Here we do not need to care about holes between extents, that is already done
4588 * by copy_items(). We also only need to do this in the full sync path, where we
4589 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4590 * lookup the list of modified extent maps and if any represents a hole, we
4591 * insert a corresponding extent representing a hole in the log tree.
4592 */
4593static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4594 struct btrfs_root *root,
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004595 struct btrfs_inode *inode,
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004596 struct btrfs_path *path)
4597{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004598 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004599 int ret;
4600 struct btrfs_key key;
4601 u64 hole_start;
4602 u64 hole_size;
4603 struct extent_buffer *leaf;
4604 struct btrfs_root *log = root->log_root;
Nikolay Borisova0308dd2017-01-18 00:31:38 +02004605 const u64 ino = btrfs_ino(inode);
4606 const u64 i_size = i_size_read(&inode->vfs_inode);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004607
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004608 if (!btrfs_fs_incompat(fs_info, NO_HOLES))
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004609 return 0;
4610
4611 key.objectid = ino;
4612 key.type = BTRFS_EXTENT_DATA_KEY;
4613 key.offset = (u64)-1;
4614
4615 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4616 ASSERT(ret != 0);
4617 if (ret < 0)
4618 return ret;
4619
4620 ASSERT(path->slots[0] > 0);
4621 path->slots[0]--;
4622 leaf = path->nodes[0];
4623 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4624
4625 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4626 /* inode does not have any extents */
4627 hole_start = 0;
4628 hole_size = i_size;
4629 } else {
4630 struct btrfs_file_extent_item *extent;
4631 u64 len;
4632
4633 /*
4634 * If there's an extent beyond i_size, an explicit hole was
4635 * already inserted by copy_items().
4636 */
4637 if (key.offset >= i_size)
4638 return 0;
4639
4640 extent = btrfs_item_ptr(leaf, path->slots[0],
4641 struct btrfs_file_extent_item);
4642
4643 if (btrfs_file_extent_type(leaf, extent) ==
4644 BTRFS_FILE_EXTENT_INLINE) {
4645 len = btrfs_file_extent_inline_len(leaf,
4646 path->slots[0],
4647 extent);
Filipe Manana6399fb52017-07-28 15:22:36 +01004648 ASSERT(len == i_size ||
4649 (len == fs_info->sectorsize &&
4650 btrfs_file_extent_compression(leaf, extent) !=
4651 BTRFS_COMPRESS_NONE));
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004652 return 0;
4653 }
4654
4655 len = btrfs_file_extent_num_bytes(leaf, extent);
4656 /* Last extent goes beyond i_size, no need to log a hole. */
4657 if (key.offset + len > i_size)
4658 return 0;
4659 hole_start = key.offset + len;
4660 hole_size = i_size - hole_start;
4661 }
4662 btrfs_release_path(path);
4663
4664 /* Last extent ends at i_size. */
4665 if (hole_size == 0)
4666 return 0;
4667
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004668 hole_size = ALIGN(hole_size, fs_info->sectorsize);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004669 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4670 hole_size, 0, hole_size, 0, 0, 0);
4671 return ret;
4672}
4673
Filipe Manana56f23fd2016-03-30 23:37:21 +01004674/*
4675 * When we are logging a new inode X, check if it doesn't have a reference that
4676 * matches the reference from some other inode Y created in a past transaction
4677 * and that was renamed in the current transaction. If we don't do this, then at
4678 * log replay time we can lose inode Y (and all its files if it's a directory):
4679 *
4680 * mkdir /mnt/x
4681 * echo "hello world" > /mnt/x/foobar
4682 * sync
4683 * mv /mnt/x /mnt/y
4684 * mkdir /mnt/x # or touch /mnt/x
4685 * xfs_io -c fsync /mnt/x
4686 * <power fail>
4687 * mount fs, trigger log replay
4688 *
4689 * After the log replay procedure, we would lose the first directory and all its
4690 * files (file foobar).
4691 * For the case where inode Y is not a directory we simply end up losing it:
4692 *
4693 * echo "123" > /mnt/foo
4694 * sync
4695 * mv /mnt/foo /mnt/bar
4696 * echo "abc" > /mnt/foo
4697 * xfs_io -c fsync /mnt/foo
4698 * <power fail>
4699 *
4700 * We also need this for cases where a snapshot entry is replaced by some other
4701 * entry (file or directory) otherwise we end up with an unreplayable log due to
4702 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4703 * if it were a regular entry:
4704 *
4705 * mkdir /mnt/x
4706 * btrfs subvolume snapshot /mnt /mnt/x/snap
4707 * btrfs subvolume delete /mnt/x/snap
4708 * rmdir /mnt/x
4709 * mkdir /mnt/x
4710 * fsync /mnt/x or fsync some new file inside it
4711 * <power fail>
4712 *
4713 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4714 * the same transaction.
4715 */
4716static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4717 const int slot,
4718 const struct btrfs_key *key,
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004719 struct btrfs_inode *inode,
Filipe Manana44f714d2016-06-06 16:11:13 +01004720 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004721{
4722 int ret;
4723 struct btrfs_path *search_path;
4724 char *name = NULL;
4725 u32 name_len = 0;
4726 u32 item_size = btrfs_item_size_nr(eb, slot);
4727 u32 cur_offset = 0;
4728 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4729
4730 search_path = btrfs_alloc_path();
4731 if (!search_path)
4732 return -ENOMEM;
4733 search_path->search_commit_root = 1;
4734 search_path->skip_locking = 1;
4735
4736 while (cur_offset < item_size) {
4737 u64 parent;
4738 u32 this_name_len;
4739 u32 this_len;
4740 unsigned long name_ptr;
4741 struct btrfs_dir_item *di;
4742
4743 if (key->type == BTRFS_INODE_REF_KEY) {
4744 struct btrfs_inode_ref *iref;
4745
4746 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4747 parent = key->offset;
4748 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4749 name_ptr = (unsigned long)(iref + 1);
4750 this_len = sizeof(*iref) + this_name_len;
4751 } else {
4752 struct btrfs_inode_extref *extref;
4753
4754 extref = (struct btrfs_inode_extref *)(ptr +
4755 cur_offset);
4756 parent = btrfs_inode_extref_parent(eb, extref);
4757 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4758 name_ptr = (unsigned long)&extref->name;
4759 this_len = sizeof(*extref) + this_name_len;
4760 }
4761
4762 if (this_name_len > name_len) {
4763 char *new_name;
4764
4765 new_name = krealloc(name, this_name_len, GFP_NOFS);
4766 if (!new_name) {
4767 ret = -ENOMEM;
4768 goto out;
4769 }
4770 name_len = this_name_len;
4771 name = new_name;
4772 }
4773
4774 read_extent_buffer(eb, name, name_ptr, this_name_len);
Nikolay Borisov4791c8f2017-01-18 00:31:35 +02004775 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4776 parent, name, this_name_len, 0);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004777 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004778 struct btrfs_key di_key;
4779
4780 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4781 di, &di_key);
4782 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4783 ret = 1;
4784 *other_ino = di_key.objectid;
4785 } else {
4786 ret = -EAGAIN;
4787 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004788 goto out;
4789 } else if (IS_ERR(di)) {
4790 ret = PTR_ERR(di);
4791 goto out;
4792 }
4793 btrfs_release_path(search_path);
4794
4795 cur_offset += this_len;
4796 }
4797 ret = 0;
4798out:
4799 btrfs_free_path(search_path);
4800 kfree(name);
4801 return ret;
4802}
4803
Chris Masone02119d2008-09-05 16:13:11 -04004804/* log a single inode in the tree log.
4805 * At least one parent directory for this inode must exist in the tree
4806 * or be logged already.
4807 *
4808 * Any items from this inode changed by the current transaction are copied
4809 * to the log tree. An extra reference is taken on any extents in this
4810 * file, allowing us to avoid a whole pile of corner cases around logging
4811 * blocks that have been removed from the tree.
4812 *
4813 * See LOG_INODE_ALL and related defines for a description of what inode_only
4814 * does.
4815 *
4816 * This handles both files and directories.
4817 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004818static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004819 struct btrfs_root *root, struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004820 int inode_only,
4821 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004822 const loff_t end,
4823 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004824{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004825 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04004826 struct btrfs_path *path;
4827 struct btrfs_path *dst_path;
4828 struct btrfs_key min_key;
4829 struct btrfs_key max_key;
4830 struct btrfs_root *log = root->log_root;
Miao Xie827463c2014-01-14 20:31:51 +08004831 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004832 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004833 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004834 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004835 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004836 int ins_start_slot = 0;
4837 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004838 bool fast_search = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004839 u64 ino = btrfs_ino(inode);
4840 struct extent_map_tree *em_tree = &inode->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004841 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004842 bool need_log_inode_item = true;
Chris Masone02119d2008-09-05 16:13:11 -04004843
Chris Masone02119d2008-09-05 16:13:11 -04004844 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004845 if (!path)
4846 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004847 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004848 if (!dst_path) {
4849 btrfs_free_path(path);
4850 return -ENOMEM;
4851 }
Chris Masone02119d2008-09-05 16:13:11 -04004852
Li Zefan33345d012011-04-20 10:31:50 +08004853 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004854 min_key.type = BTRFS_INODE_ITEM_KEY;
4855 min_key.offset = 0;
4856
Li Zefan33345d012011-04-20 10:31:50 +08004857 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004858
Chris Mason12fcfd22009-03-24 10:24:20 -04004859
Josef Bacik5dc562c2012-08-17 13:14:17 -04004860 /* today the code can only do partial logging of directories */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004861 if (S_ISDIR(inode->vfs_inode.i_mode) ||
Miao Xie5269b672012-11-01 07:35:23 +00004862 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004863 &inode->runtime_flags) &&
Liu Bo781feef2016-11-30 16:20:25 -08004864 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004865 max_key.type = BTRFS_XATTR_ITEM_KEY;
4866 else
4867 max_key.type = (u8)-1;
4868 max_key.offset = (u64)-1;
4869
Filipe Manana2c2c4522015-01-13 16:40:04 +00004870 /*
4871 * Only run delayed items if we are a dir or a new file.
4872 * Otherwise commit the delayed inode only, which is needed in
4873 * order for the log replay code to mark inodes for link count
4874 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4875 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004876 if (S_ISDIR(inode->vfs_inode.i_mode) ||
4877 inode->generation > fs_info->last_trans_committed)
4878 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004879 else
Nikolay Borisova59108a2017-01-18 00:31:48 +02004880 ret = btrfs_commit_inode_delayed_inode(inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004881
4882 if (ret) {
4883 btrfs_free_path(path);
4884 btrfs_free_path(dst_path);
4885 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004886 }
4887
Liu Bo781feef2016-11-30 16:20:25 -08004888 if (inode_only == LOG_OTHER_INODE) {
4889 inode_only = LOG_INODE_EXISTS;
Nikolay Borisova59108a2017-01-18 00:31:48 +02004890 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
Liu Bo781feef2016-11-30 16:20:25 -08004891 } else {
Nikolay Borisova59108a2017-01-18 00:31:48 +02004892 mutex_lock(&inode->log_mutex);
Liu Bo781feef2016-11-30 16:20:25 -08004893 }
Chris Masone02119d2008-09-05 16:13:11 -04004894
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004895 /*
Chris Masone02119d2008-09-05 16:13:11 -04004896 * a brute force approach to making sure we get the most uptodate
4897 * copies of everything.
4898 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004899 if (S_ISDIR(inode->vfs_inode.i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04004900 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4901
Filipe Manana4f764e52015-02-23 19:53:35 +00004902 if (inode_only == LOG_INODE_EXISTS)
4903 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004904 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004905 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004906 if (inode_only == LOG_INODE_EXISTS) {
4907 /*
4908 * Make sure the new inode item we write to the log has
4909 * the same isize as the current one (if it exists).
4910 * This is necessary to prevent data loss after log
4911 * replay, and also to prevent doing a wrong expanding
4912 * truncate - for e.g. create file, write 4K into offset
4913 * 0, fsync, write 4K into offset 4096, add hard link,
4914 * fsync some other file (to sync log), power fail - if
4915 * we use the inode's current i_size, after log replay
4916 * we get a 8Kb file, with the last 4Kb extent as a hole
4917 * (zeroes), as if an expanding truncate happened,
4918 * instead of getting a file of 4Kb only.
4919 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02004920 err = logged_inode_size(log, inode, path, &logged_isize);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004921 if (err)
4922 goto out_unlock;
4923 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004924 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004925 &inode->runtime_flags)) {
Filipe Mananaa7429942015-02-13 16:56:14 +00004926 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004927 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004928 ret = drop_objectid_items(trans, log, path, ino,
4929 max_key.type);
4930 } else {
4931 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004932 &inode->runtime_flags);
Filipe Mananaa7429942015-02-13 16:56:14 +00004933 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004934 &inode->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004935 while(1) {
4936 ret = btrfs_truncate_inode_items(trans,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004937 log, &inode->vfs_inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08004938 if (ret != -EAGAIN)
4939 break;
4940 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004941 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004942 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Nikolay Borisova59108a2017-01-18 00:31:48 +02004943 &inode->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004944 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004945 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004946 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004947 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004948 ret = drop_objectid_items(trans, log, path, ino,
4949 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004950 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004951 if (inode_only == LOG_INODE_ALL)
4952 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004953 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004954 }
Josef Bacika95249b2012-10-11 16:17:34 -04004955
Chris Masone02119d2008-09-05 16:13:11 -04004956 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004957 if (ret) {
4958 err = ret;
4959 goto out_unlock;
4960 }
Chris Masone02119d2008-09-05 16:13:11 -04004961
Chris Masond3977122009-01-05 21:25:51 -05004962 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004963 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004964 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004965 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004966 if (ret < 0) {
4967 err = ret;
4968 goto out_unlock;
4969 }
Chris Masone02119d2008-09-05 16:13:11 -04004970 if (ret != 0)
4971 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004972again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004973 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004974 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004975 break;
4976 if (min_key.type > max_key.type)
4977 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004978
Filipe Mananae4545de2015-06-17 12:49:23 +01004979 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4980 need_log_inode_item = false;
4981
Filipe Manana56f23fd2016-03-30 23:37:21 +01004982 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4983 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
Nikolay Borisova59108a2017-01-18 00:31:48 +02004984 inode->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004985 u64 other_ino = 0;
4986
Filipe Manana56f23fd2016-03-30 23:37:21 +01004987 ret = btrfs_check_ref_name_override(path->nodes[0],
Nikolay Borisova59108a2017-01-18 00:31:48 +02004988 path->slots[0], &min_key, inode,
4989 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004990 if (ret < 0) {
4991 err = ret;
4992 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004993 } else if (ret > 0 && ctx &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004994 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004995 struct btrfs_key inode_key;
4996 struct inode *other_inode;
4997
4998 if (ins_nr > 0) {
4999 ins_nr++;
5000 } else {
5001 ins_nr = 1;
5002 ins_start_slot = path->slots[0];
5003 }
Nikolay Borisova59108a2017-01-18 00:31:48 +02005004 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana44f714d2016-06-06 16:11:13 +01005005 &last_extent, ins_start_slot,
5006 ins_nr, inode_only,
5007 logged_isize);
5008 if (ret < 0) {
5009 err = ret;
5010 goto out_unlock;
5011 }
5012 ins_nr = 0;
5013 btrfs_release_path(path);
5014 inode_key.objectid = other_ino;
5015 inode_key.type = BTRFS_INODE_ITEM_KEY;
5016 inode_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005017 other_inode = btrfs_iget(fs_info->sb,
Filipe Manana44f714d2016-06-06 16:11:13 +01005018 &inode_key, root,
5019 NULL);
5020 /*
5021 * If the other inode that had a conflicting dir
5022 * entry was deleted in the current transaction,
5023 * we don't need to do more work nor fallback to
5024 * a transaction commit.
5025 */
5026 if (IS_ERR(other_inode) &&
5027 PTR_ERR(other_inode) == -ENOENT) {
5028 goto next_key;
5029 } else if (IS_ERR(other_inode)) {
5030 err = PTR_ERR(other_inode);
5031 goto out_unlock;
5032 }
5033 /*
5034 * We are safe logging the other inode without
5035 * acquiring its i_mutex as long as we log with
5036 * the LOG_INODE_EXISTS mode. We're safe against
5037 * concurrent renames of the other inode as well
5038 * because during a rename we pin the log and
5039 * update the log with the new name before we
5040 * unpin it.
5041 */
Nikolay Borisova59108a2017-01-18 00:31:48 +02005042 err = btrfs_log_inode(trans, root,
5043 BTRFS_I(other_inode),
5044 LOG_OTHER_INODE, 0, LLONG_MAX,
5045 ctx);
Filipe Manana44f714d2016-06-06 16:11:13 +01005046 iput(other_inode);
5047 if (err)
5048 goto out_unlock;
5049 else
5050 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01005051 }
5052 }
5053
Filipe Manana36283bf2015-06-20 00:44:51 +01005054 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5055 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
5056 if (ins_nr == 0)
5057 goto next_slot;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005058 ret = copy_items(trans, inode, dst_path, path,
Filipe Manana36283bf2015-06-20 00:44:51 +01005059 &last_extent, ins_start_slot,
5060 ins_nr, inode_only, logged_isize);
5061 if (ret < 0) {
5062 err = ret;
5063 goto out_unlock;
5064 }
5065 ins_nr = 0;
5066 if (ret) {
5067 btrfs_release_path(path);
5068 continue;
5069 }
5070 goto next_slot;
5071 }
5072
Chris Mason31ff1cd2008-09-11 16:17:57 -04005073 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5074 ins_nr++;
5075 goto next_slot;
5076 } else if (!ins_nr) {
5077 ins_start_slot = path->slots[0];
5078 ins_nr = 1;
5079 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04005080 }
5081
Nikolay Borisova59108a2017-01-18 00:31:48 +02005082 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005083 ins_start_slot, ins_nr, inode_only,
5084 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005085 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005086 err = ret;
5087 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02005088 }
5089 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04005090 ins_nr = 0;
5091 btrfs_release_path(path);
5092 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005093 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005094 ins_nr = 1;
5095 ins_start_slot = path->slots[0];
5096next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04005097
Chris Mason3a5f1d42008-09-11 15:53:37 -04005098 nritems = btrfs_header_nritems(path->nodes[0]);
5099 path->slots[0]++;
5100 if (path->slots[0] < nritems) {
5101 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
5102 path->slots[0]);
5103 goto again;
5104 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005105 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005106 ret = copy_items(trans, inode, dst_path, path,
Josef Bacik16e75492013-10-22 12:18:51 -04005107 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005108 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005109 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005110 err = ret;
5111 goto out_unlock;
5112 }
Josef Bacik16e75492013-10-22 12:18:51 -04005113 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005114 ins_nr = 0;
5115 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005116 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01005117next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005118 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04005119 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005120 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04005121 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005122 min_key.offset = 0;
5123 } else {
Chris Masone02119d2008-09-05 16:13:11 -04005124 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01005125 }
Chris Masone02119d2008-09-05 16:13:11 -04005126 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04005127 if (ins_nr) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005128 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00005129 ins_start_slot, ins_nr, inode_only,
5130 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04005131 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005132 err = ret;
5133 goto out_unlock;
5134 }
Josef Bacik16e75492013-10-22 12:18:51 -04005135 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04005136 ins_nr = 0;
5137 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005138
Filipe Manana36283bf2015-06-20 00:44:51 +01005139 btrfs_release_path(path);
5140 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005141 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
Filipe Manana36283bf2015-06-20 00:44:51 +01005142 if (err)
5143 goto out_unlock;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005144 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5145 btrfs_release_path(path);
5146 btrfs_release_path(dst_path);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005147 err = btrfs_log_trailing_hole(trans, root, inode, path);
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01005148 if (err)
5149 goto out_unlock;
5150 }
Josef Bacika95249b2012-10-11 16:17:34 -04005151log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04005152 btrfs_release_path(path);
5153 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01005154 if (need_log_inode_item) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005155 err = log_inode_item(trans, log, dst_path, inode);
Filipe Mananae4545de2015-06-17 12:49:23 +01005156 if (err)
5157 goto out_unlock;
5158 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04005159 if (fast_search) {
Nikolay Borisova59108a2017-01-18 00:31:48 +02005160 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00005161 &logged_list, ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005162 if (ret) {
5163 err = ret;
5164 goto out_unlock;
5165 }
Josef Bacikd006a042013-11-12 20:54:09 -05005166 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06005167 struct extent_map *em, *n;
5168
Filipe Manana49dae1b2014-09-06 22:34:39 +01005169 write_lock(&em_tree->lock);
5170 /*
5171 * We can't just remove every em if we're called for a ranged
5172 * fsync - that is, one that doesn't cover the whole possible
5173 * file range (0 to LLONG_MAX). This is because we can have
5174 * em's that fall outside the range we're logging and therefore
5175 * their ordered operations haven't completed yet
5176 * (btrfs_finish_ordered_io() not invoked yet). This means we
5177 * didn't get their respective file extent item in the fs/subvol
5178 * tree yet, and need to let the next fast fsync (one which
5179 * consults the list of modified extent maps) find the em so
5180 * that it logs a matching file extent item and waits for the
5181 * respective ordered operation to complete (if it's still
5182 * running).
5183 *
5184 * Removing every em outside the range we're logging would make
5185 * the next fast fsync not log their matching file extent items,
5186 * therefore making us lose data after a log replay.
5187 */
5188 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
5189 list) {
5190 const u64 mod_end = em->mod_start + em->mod_len - 1;
5191
5192 if (em->mod_start >= start && mod_end <= end)
5193 list_del_init(&em->list);
5194 }
5195 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005196 }
5197
Nikolay Borisova59108a2017-01-18 00:31:48 +02005198 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5199 ret = log_directory_changes(trans, root, inode, path, dst_path,
5200 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005201 if (ret) {
5202 err = ret;
5203 goto out_unlock;
5204 }
Chris Masone02119d2008-09-05 16:13:11 -04005205 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005206
Nikolay Borisova59108a2017-01-18 00:31:48 +02005207 spin_lock(&inode->lock);
5208 inode->logged_trans = trans->transid;
5209 inode->last_log_commit = inode->last_sub_trans;
5210 spin_unlock(&inode->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005211out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08005212 if (unlikely(err))
5213 btrfs_put_logged_extents(&logged_list);
5214 else
5215 btrfs_submit_logged_extents(&logged_list, log);
Nikolay Borisova59108a2017-01-18 00:31:48 +02005216 mutex_unlock(&inode->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04005217
5218 btrfs_free_path(path);
5219 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005220 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005221}
5222
Chris Mason12fcfd22009-03-24 10:24:20 -04005223/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005224 * Check if we must fallback to a transaction commit when logging an inode.
5225 * This must be called after logging the inode and is used only in the context
5226 * when fsyncing an inode requires the need to log some other inode - in which
5227 * case we can't lock the i_mutex of each other inode we need to log as that
5228 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5229 * log inodes up or down in the hierarchy) or rename operations for example. So
5230 * we take the log_mutex of the inode after we have logged it and then check for
5231 * its last_unlink_trans value - this is safe because any task setting
5232 * last_unlink_trans must take the log_mutex and it must do this before it does
5233 * the actual unlink operation, so if we do this check before a concurrent task
5234 * sets last_unlink_trans it means we've logged a consistent version/state of
5235 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005236 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005237 * we logged the inode or it might have also done the unlink).
5238 */
5239static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005240 struct btrfs_inode *inode)
Filipe Manana2be63d52016-02-12 11:34:23 +00005241{
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005242 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana2be63d52016-02-12 11:34:23 +00005243 bool ret = false;
5244
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005245 mutex_lock(&inode->log_mutex);
5246 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
Filipe Manana2be63d52016-02-12 11:34:23 +00005247 /*
5248 * Make sure any commits to the log are forced to be full
5249 * commits.
5250 */
5251 btrfs_set_log_full_commit(fs_info, trans);
5252 ret = true;
5253 }
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005254 mutex_unlock(&inode->log_mutex);
Filipe Manana2be63d52016-02-12 11:34:23 +00005255
5256 return ret;
5257}
5258
5259/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005260 * follow the dentry parent pointers up the chain and see if any
5261 * of the directories in it require a full commit before they can
5262 * be logged. Returns zero if nothing special needs to be done or 1 if
5263 * a full commit is required.
5264 */
5265static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005266 struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005267 struct dentry *parent,
5268 struct super_block *sb,
5269 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005270{
Chris Mason12fcfd22009-03-24 10:24:20 -04005271 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005272 struct dentry *old_parent = NULL;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005273 struct btrfs_inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005274
Chris Masonaf4176b2009-03-24 10:24:31 -04005275 /*
5276 * for regular files, if its inode is already on disk, we don't
5277 * have to worry about the parents at all. This is because
5278 * we can use the last_unlink_trans field to record renames
5279 * and other fun in this file.
5280 */
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005281 if (S_ISREG(inode->vfs_inode.i_mode) &&
5282 inode->generation <= last_committed &&
5283 inode->last_unlink_trans <= last_committed)
5284 goto out;
Chris Masonaf4176b2009-03-24 10:24:31 -04005285
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005286 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005287 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005288 goto out;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005289 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005290 }
5291
5292 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005293 /*
5294 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005295 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005296 * logged_trans so that further down in the log code we don't
5297 * think this inode has already been logged.
5298 */
5299 if (inode != orig_inode)
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005300 inode->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005301 smp_mb();
5302
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005303 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005304 ret = 1;
5305 break;
5306 }
5307
Al Virofc640052016-04-10 01:33:30 -04005308 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005309 break;
5310
Filipe Manana44f714d2016-06-06 16:11:13 +01005311 if (IS_ROOT(parent)) {
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005312 inode = BTRFS_I(d_inode(parent));
5313 if (btrfs_must_commit_transaction(trans, inode))
Filipe Manana44f714d2016-06-06 16:11:13 +01005314 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005315 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005316 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005317
Josef Bacik6a912212010-11-20 09:48:00 +00005318 parent = dget_parent(parent);
5319 dput(old_parent);
5320 old_parent = parent;
Nikolay Borisovaefa6112017-02-20 13:51:00 +02005321 inode = BTRFS_I(d_inode(parent));
Chris Mason12fcfd22009-03-24 10:24:20 -04005322
5323 }
Josef Bacik6a912212010-11-20 09:48:00 +00005324 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005325out:
Chris Masone02119d2008-09-05 16:13:11 -04005326 return ret;
5327}
5328
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005329struct btrfs_dir_list {
5330 u64 ino;
5331 struct list_head list;
5332};
5333
5334/*
5335 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5336 * details about the why it is needed.
5337 * This is a recursive operation - if an existing dentry corresponds to a
5338 * directory, that directory's new entries are logged too (same behaviour as
5339 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5340 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5341 * complains about the following circular lock dependency / possible deadlock:
5342 *
5343 * CPU0 CPU1
5344 * ---- ----
5345 * lock(&type->i_mutex_dir_key#3/2);
5346 * lock(sb_internal#2);
5347 * lock(&type->i_mutex_dir_key#3/2);
5348 * lock(&sb->s_type->i_mutex_key#14);
5349 *
5350 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5351 * sb_start_intwrite() in btrfs_start_transaction().
5352 * Not locking i_mutex of the inodes is still safe because:
5353 *
5354 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5355 * that while logging the inode new references (names) are added or removed
5356 * from the inode, leaving the logged inode item with a link count that does
5357 * not match the number of logged inode reference items. This is fine because
5358 * at log replay time we compute the real number of links and correct the
5359 * link count in the inode item (see replay_one_buffer() and
5360 * link_to_fixup_dir());
5361 *
5362 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5363 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5364 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5365 * has a size that doesn't match the sum of the lengths of all the logged
5366 * names. This does not result in a problem because if a dir_item key is
5367 * logged but its matching dir_index key is not logged, at log replay time we
5368 * don't use it to replay the respective name (see replay_one_name()). On the
5369 * other hand if only the dir_index key ends up being logged, the respective
5370 * name is added to the fs/subvol tree with both the dir_item and dir_index
5371 * keys created (see replay_one_name()).
5372 * The directory's inode item with a wrong i_size is not a problem as well,
5373 * since we don't use it at log replay time to set the i_size in the inode
5374 * item of the fs/subvol tree (see overwrite_item()).
5375 */
5376static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5377 struct btrfs_root *root,
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005378 struct btrfs_inode *start_inode,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005379 struct btrfs_log_ctx *ctx)
5380{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005381 struct btrfs_fs_info *fs_info = root->fs_info;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005382 struct btrfs_root *log = root->log_root;
5383 struct btrfs_path *path;
5384 LIST_HEAD(dir_list);
5385 struct btrfs_dir_list *dir_elem;
5386 int ret = 0;
5387
5388 path = btrfs_alloc_path();
5389 if (!path)
5390 return -ENOMEM;
5391
5392 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5393 if (!dir_elem) {
5394 btrfs_free_path(path);
5395 return -ENOMEM;
5396 }
Nikolay Borisov51cc0d32017-01-18 00:31:43 +02005397 dir_elem->ino = btrfs_ino(start_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005398 list_add_tail(&dir_elem->list, &dir_list);
5399
5400 while (!list_empty(&dir_list)) {
5401 struct extent_buffer *leaf;
5402 struct btrfs_key min_key;
5403 int nritems;
5404 int i;
5405
5406 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5407 list);
5408 if (ret)
5409 goto next_dir_inode;
5410
5411 min_key.objectid = dir_elem->ino;
5412 min_key.type = BTRFS_DIR_ITEM_KEY;
5413 min_key.offset = 0;
5414again:
5415 btrfs_release_path(path);
5416 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5417 if (ret < 0) {
5418 goto next_dir_inode;
5419 } else if (ret > 0) {
5420 ret = 0;
5421 goto next_dir_inode;
5422 }
5423
5424process_leaf:
5425 leaf = path->nodes[0];
5426 nritems = btrfs_header_nritems(leaf);
5427 for (i = path->slots[0]; i < nritems; i++) {
5428 struct btrfs_dir_item *di;
5429 struct btrfs_key di_key;
5430 struct inode *di_inode;
5431 struct btrfs_dir_list *new_dir_elem;
5432 int log_mode = LOG_INODE_EXISTS;
5433 int type;
5434
5435 btrfs_item_key_to_cpu(leaf, &min_key, i);
5436 if (min_key.objectid != dir_elem->ino ||
5437 min_key.type != BTRFS_DIR_ITEM_KEY)
5438 goto next_dir_inode;
5439
5440 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5441 type = btrfs_dir_type(leaf, di);
5442 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5443 type != BTRFS_FT_DIR)
5444 continue;
5445 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5446 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5447 continue;
5448
Robbie Koec125cf2016-10-28 10:48:26 +08005449 btrfs_release_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005450 di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005451 if (IS_ERR(di_inode)) {
5452 ret = PTR_ERR(di_inode);
5453 goto next_dir_inode;
5454 }
5455
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02005456 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005457 iput(di_inode);
Robbie Koec125cf2016-10-28 10:48:26 +08005458 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005459 }
5460
5461 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005462 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005463 log_mode = LOG_INODE_ALL;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005464 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005465 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005466 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005467 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005468 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005469 iput(di_inode);
5470 if (ret)
5471 goto next_dir_inode;
5472 if (ctx->log_new_dentries) {
5473 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5474 GFP_NOFS);
5475 if (!new_dir_elem) {
5476 ret = -ENOMEM;
5477 goto next_dir_inode;
5478 }
5479 new_dir_elem->ino = di_key.objectid;
5480 list_add_tail(&new_dir_elem->list, &dir_list);
5481 }
5482 break;
5483 }
5484 if (i == nritems) {
5485 ret = btrfs_next_leaf(log, path);
5486 if (ret < 0) {
5487 goto next_dir_inode;
5488 } else if (ret > 0) {
5489 ret = 0;
5490 goto next_dir_inode;
5491 }
5492 goto process_leaf;
5493 }
5494 if (min_key.offset < (u64)-1) {
5495 min_key.offset++;
5496 goto again;
5497 }
5498next_dir_inode:
5499 list_del(&dir_elem->list);
5500 kfree(dir_elem);
5501 }
5502
5503 btrfs_free_path(path);
5504 return ret;
5505}
5506
Filipe Manana18aa0922015-08-05 16:49:08 +01005507static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005508 struct btrfs_inode *inode,
Filipe Manana18aa0922015-08-05 16:49:08 +01005509 struct btrfs_log_ctx *ctx)
5510{
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005511 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Filipe Manana18aa0922015-08-05 16:49:08 +01005512 int ret;
5513 struct btrfs_path *path;
5514 struct btrfs_key key;
Nikolay Borisovd0a0b782017-02-20 13:50:30 +02005515 struct btrfs_root *root = inode->root;
5516 const u64 ino = btrfs_ino(inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005517
5518 path = btrfs_alloc_path();
5519 if (!path)
5520 return -ENOMEM;
5521 path->skip_locking = 1;
5522 path->search_commit_root = 1;
5523
5524 key.objectid = ino;
5525 key.type = BTRFS_INODE_REF_KEY;
5526 key.offset = 0;
5527 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5528 if (ret < 0)
5529 goto out;
5530
5531 while (true) {
5532 struct extent_buffer *leaf = path->nodes[0];
5533 int slot = path->slots[0];
5534 u32 cur_offset = 0;
5535 u32 item_size;
5536 unsigned long ptr;
5537
5538 if (slot >= btrfs_header_nritems(leaf)) {
5539 ret = btrfs_next_leaf(root, path);
5540 if (ret < 0)
5541 goto out;
5542 else if (ret > 0)
5543 break;
5544 continue;
5545 }
5546
5547 btrfs_item_key_to_cpu(leaf, &key, slot);
5548 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5549 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5550 break;
5551
5552 item_size = btrfs_item_size_nr(leaf, slot);
5553 ptr = btrfs_item_ptr_offset(leaf, slot);
5554 while (cur_offset < item_size) {
5555 struct btrfs_key inode_key;
5556 struct inode *dir_inode;
5557
5558 inode_key.type = BTRFS_INODE_ITEM_KEY;
5559 inode_key.offset = 0;
5560
5561 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5562 struct btrfs_inode_extref *extref;
5563
5564 extref = (struct btrfs_inode_extref *)
5565 (ptr + cur_offset);
5566 inode_key.objectid = btrfs_inode_extref_parent(
5567 leaf, extref);
5568 cur_offset += sizeof(*extref);
5569 cur_offset += btrfs_inode_extref_name_len(leaf,
5570 extref);
5571 } else {
5572 inode_key.objectid = key.offset;
5573 cur_offset = item_size;
5574 }
5575
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005576 dir_inode = btrfs_iget(fs_info->sb, &inode_key,
Filipe Manana18aa0922015-08-05 16:49:08 +01005577 root, NULL);
5578 /* If parent inode was deleted, skip it. */
5579 if (IS_ERR(dir_inode))
5580 continue;
5581
Filipe Manana657ed1a2016-04-06 17:11:56 +01005582 if (ctx)
5583 ctx->log_new_dentries = false;
Nikolay Borisova59108a2017-01-18 00:31:48 +02005584 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
Filipe Manana18aa0922015-08-05 16:49:08 +01005585 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005586 if (!ret &&
Nikolay Borisovab1717b2017-01-18 00:31:27 +02005587 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
Filipe Manana2be63d52016-02-12 11:34:23 +00005588 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005589 if (!ret && ctx && ctx->log_new_dentries)
5590 ret = log_new_dir_dentries(trans, root,
David Sterbaf85b7372017-01-20 14:54:07 +01005591 BTRFS_I(dir_inode), ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005592 iput(dir_inode);
5593 if (ret)
5594 goto out;
5595 }
5596 path->slots[0]++;
5597 }
5598 ret = 0;
5599out:
5600 btrfs_free_path(path);
5601 return ret;
5602}
5603
Chris Masone02119d2008-09-05 16:13:11 -04005604/*
5605 * helper function around btrfs_log_inode to make sure newly created
5606 * parent directories also end up in the log. A minimal inode and backref
5607 * only logging is done of any parent directories that are older than
5608 * the last committed transaction
5609 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005610static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005611 struct btrfs_inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005612 struct dentry *parent,
5613 const loff_t start,
5614 const loff_t end,
Edmund Nadolski41a1ead2017-11-20 13:24:47 -07005615 int inode_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005616 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005617{
Nikolay Borisovf8822742018-02-27 17:37:17 +02005618 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005619 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone02119d2008-09-05 16:13:11 -04005620 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005621 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005622 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005623 u64 last_committed = fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005624 bool log_dentries = false;
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005625 struct btrfs_inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005626
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005627 sb = inode->vfs_inode.i_sb;
Chris Mason12fcfd22009-03-24 10:24:20 -04005628
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005629 if (btrfs_test_opt(fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005630 ret = 1;
5631 goto end_no_trans;
5632 }
5633
Miao Xie995946d2014-04-02 19:51:06 +08005634 /*
5635 * The prev transaction commit doesn't complete, we need do
5636 * full commit by ourselves.
5637 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005638 if (fs_info->last_trans_log_full_commit >
5639 fs_info->last_trans_committed) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005640 ret = 1;
5641 goto end_no_trans;
5642 }
5643
Nikolay Borisovf8822742018-02-27 17:37:17 +02005644 if (btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005645 ret = 1;
5646 goto end_no_trans;
5647 }
5648
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005649 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
5650 last_committed);
Chris Mason12fcfd22009-03-24 10:24:20 -04005651 if (ret)
5652 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005653
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005654 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005655 ret = BTRFS_NO_LOG_SYNC;
5656 goto end_no_trans;
5657 }
5658
Miao Xie8b050d32014-02-20 18:08:58 +08005659 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005660 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005661 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005662
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005663 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005664 if (ret)
5665 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005666
Chris Masonaf4176b2009-03-24 10:24:31 -04005667 /*
5668 * for regular files, if its inode is already on disk, we don't
5669 * have to worry about the parents at all. This is because
5670 * we can use the last_unlink_trans field to record renames
5671 * and other fun in this file.
5672 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005673 if (S_ISREG(inode->vfs_inode.i_mode) &&
5674 inode->generation <= last_committed &&
5675 inode->last_unlink_trans <= last_committed) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005676 ret = 0;
5677 goto end_trans;
5678 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005679
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005680 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005681 log_dentries = true;
5682
Filipe Manana18aa0922015-08-05 16:49:08 +01005683 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005684 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005685 * inodes are fully logged. This is to prevent leaving dangling
5686 * directory index entries in directories that were our parents but are
5687 * not anymore. Not doing this results in old parent directory being
5688 * impossible to delete after log replay (rmdir will always fail with
5689 * error -ENOTEMPTY).
5690 *
5691 * Example 1:
5692 *
5693 * mkdir testdir
5694 * touch testdir/foo
5695 * ln testdir/foo testdir/bar
5696 * sync
5697 * unlink testdir/bar
5698 * xfs_io -c fsync testdir/foo
5699 * <power failure>
5700 * mount fs, triggers log replay
5701 *
5702 * If we don't log the parent directory (testdir), after log replay the
5703 * directory still has an entry pointing to the file inode using the bar
5704 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5705 * the file inode has a link count of 1.
5706 *
5707 * Example 2:
5708 *
5709 * mkdir testdir
5710 * touch foo
5711 * ln foo testdir/foo2
5712 * ln foo testdir/foo3
5713 * sync
5714 * unlink testdir/foo3
5715 * xfs_io -c fsync foo
5716 * <power failure>
5717 * mount fs, triggers log replay
5718 *
5719 * Similar as the first example, after log replay the parent directory
5720 * testdir still has an entry pointing to the inode file with name foo3
5721 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5722 * and has a link count of 2.
5723 */
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005724 if (inode->last_unlink_trans > last_committed) {
Filipe Manana18aa0922015-08-05 16:49:08 +01005725 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5726 if (ret)
5727 goto end_trans;
5728 }
5729
Chris Masond3977122009-01-05 21:25:51 -05005730 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005731 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005732 break;
5733
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005734 inode = BTRFS_I(d_inode(parent));
5735 if (root != inode->root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005736 break;
5737
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005738 if (inode->generation > last_committed) {
5739 ret = btrfs_log_inode(trans, root, inode,
5740 LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005741 if (ret)
5742 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005743 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005744 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005745 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005746
Josef Bacik6a912212010-11-20 09:48:00 +00005747 parent = dget_parent(parent);
5748 dput(old_parent);
5749 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005750 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005751 if (log_dentries)
Nikolay Borisov19df27a2017-02-20 13:51:01 +02005752 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005753 else
5754 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005755end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005756 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005757 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005758 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005759 ret = 1;
5760 }
Miao Xie8b050d32014-02-20 18:08:58 +08005761
5762 if (ret)
5763 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005764 btrfs_end_log_trans(root);
5765end_no_trans:
5766 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005767}
5768
5769/*
5770 * it is not safe to log dentry if the chunk root has added new
5771 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5772 * If this returns 1, you must commit the transaction to safely get your
5773 * data on disk.
5774 */
5775int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Nikolay Borisove5b84f7a2018-02-27 17:37:18 +02005776 struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005777 const loff_t start,
5778 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005779 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005780{
Josef Bacik6a912212010-11-20 09:48:00 +00005781 struct dentry *parent = dget_parent(dentry);
5782 int ret;
5783
Nikolay Borisovf8822742018-02-27 17:37:17 +02005784 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
5785 start, end, LOG_INODE_ALL, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005786 dput(parent);
5787
5788 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005789}
5790
5791/*
5792 * should be called during mount to recover any replay any log trees
5793 * from the FS
5794 */
5795int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5796{
5797 int ret;
5798 struct btrfs_path *path;
5799 struct btrfs_trans_handle *trans;
5800 struct btrfs_key key;
5801 struct btrfs_key found_key;
5802 struct btrfs_key tmp_key;
5803 struct btrfs_root *log;
5804 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5805 struct walk_control wc = {
5806 .process_func = process_one_buffer,
5807 .stage = 0,
5808 };
5809
Chris Masone02119d2008-09-05 16:13:11 -04005810 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005811 if (!path)
5812 return -ENOMEM;
5813
Josef Bacikafcdd122016-09-02 15:40:02 -04005814 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005815
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005816 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005817 if (IS_ERR(trans)) {
5818 ret = PTR_ERR(trans);
5819 goto error;
5820 }
Chris Masone02119d2008-09-05 16:13:11 -04005821
5822 wc.trans = trans;
5823 wc.pin = 1;
5824
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005825 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005826 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005827 btrfs_handle_fs_error(fs_info, ret,
5828 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005829 goto error;
5830 }
Chris Masone02119d2008-09-05 16:13:11 -04005831
5832again:
5833 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5834 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005835 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005836
Chris Masond3977122009-01-05 21:25:51 -05005837 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005838 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005839
5840 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005841 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005842 "Couldn't find tree log root.");
5843 goto error;
5844 }
Chris Masone02119d2008-09-05 16:13:11 -04005845 if (ret > 0) {
5846 if (path->slots[0] == 0)
5847 break;
5848 path->slots[0]--;
5849 }
5850 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5851 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005852 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005853 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5854 break;
5855
Miao Xiecb517ea2013-05-15 07:48:19 +00005856 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005857 if (IS_ERR(log)) {
5858 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005859 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005860 "Couldn't read tree log root.");
5861 goto error;
5862 }
Chris Masone02119d2008-09-05 16:13:11 -04005863
5864 tmp_key.objectid = found_key.offset;
5865 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5866 tmp_key.offset = (u64)-1;
5867
5868 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005869 if (IS_ERR(wc.replay_dest)) {
5870 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005871 free_extent_buffer(log->node);
5872 free_extent_buffer(log->commit_root);
5873 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005874 btrfs_handle_fs_error(fs_info, ret,
5875 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005876 goto error;
5877 }
Chris Masone02119d2008-09-05 16:13:11 -04005878
Yan Zheng07d400a2009-01-06 11:42:00 -05005879 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005880 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005881 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005882
Josef Bacikb50c6e22013-04-25 15:55:30 -04005883 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005884 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5885 path);
Chris Masone02119d2008-09-05 16:13:11 -04005886 }
Chris Masone02119d2008-09-05 16:13:11 -04005887
Liu Bo900c9982018-01-25 11:02:56 -07005888 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5889 struct btrfs_root *root = wc.replay_dest;
5890
5891 btrfs_release_path(path);
5892
5893 /*
5894 * We have just replayed everything, and the highest
5895 * objectid of fs roots probably has changed in case
5896 * some inode_item's got replayed.
5897 *
5898 * root->objectid_mutex is not acquired as log replay
5899 * could only happen during mount.
5900 */
5901 ret = btrfs_find_highest_objectid(root,
5902 &root->highest_objectid);
5903 }
5904
Chris Masone02119d2008-09-05 16:13:11 -04005905 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005906 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005907 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005908 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005909 kfree(log);
5910
Josef Bacikb50c6e22013-04-25 15:55:30 -04005911 if (ret)
5912 goto error;
5913
Chris Masone02119d2008-09-05 16:13:11 -04005914 if (found_key.offset == 0)
5915 break;
5916 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005917 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005918
5919 /* step one is to pin it all, step two is to replay just inodes */
5920 if (wc.pin) {
5921 wc.pin = 0;
5922 wc.process_func = replay_one_buffer;
5923 wc.stage = LOG_WALK_REPLAY_INODES;
5924 goto again;
5925 }
5926 /* step three is to replay everything */
5927 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5928 wc.stage++;
5929 goto again;
5930 }
5931
5932 btrfs_free_path(path);
5933
Josef Bacikabefa552013-04-24 16:40:05 -04005934 /* step 4: commit the transaction, which also unpins the blocks */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005935 ret = btrfs_commit_transaction(trans);
Josef Bacikabefa552013-04-24 16:40:05 -04005936 if (ret)
5937 return ret;
5938
Chris Masone02119d2008-09-05 16:13:11 -04005939 free_extent_buffer(log_root_tree->node);
5940 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005941 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005942 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005943
Josef Bacikabefa552013-04-24 16:40:05 -04005944 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005945error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005946 if (wc.trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005947 btrfs_end_transaction(wc.trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005948 btrfs_free_path(path);
5949 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005950}
Chris Mason12fcfd22009-03-24 10:24:20 -04005951
5952/*
5953 * there are some corner cases where we want to force a full
5954 * commit instead of allowing a directory to be logged.
5955 *
5956 * They revolve around files there were unlinked from the directory, and
5957 * this function updates the parent directory so that a full commit is
5958 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005959 *
5960 * Must be called before the unlink operations (updates to the subvolume tree,
5961 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005962 */
5963void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005964 struct btrfs_inode *dir, struct btrfs_inode *inode,
Chris Mason12fcfd22009-03-24 10:24:20 -04005965 int for_rename)
5966{
5967 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005968 * when we're logging a file, if it hasn't been renamed
5969 * or unlinked, and its inode is fully committed on disk,
5970 * we don't have to worry about walking up the directory chain
5971 * to log its parents.
5972 *
5973 * So, we use the last_unlink_trans field to put this transid
5974 * into the file. When the file is logged we check it and
5975 * don't log the parents if the file is fully on disk.
5976 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005977 mutex_lock(&inode->log_mutex);
5978 inode->last_unlink_trans = trans->transid;
5979 mutex_unlock(&inode->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04005980
5981 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005982 * if this directory was already logged any new
5983 * names for this file/dir will get recorded
5984 */
5985 smp_mb();
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005986 if (dir->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04005987 return;
5988
5989 /*
5990 * if the inode we're about to unlink was logged,
5991 * the log will be properly updated for any new names
5992 */
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02005993 if (inode->logged_trans == trans->transid)
Chris Mason12fcfd22009-03-24 10:24:20 -04005994 return;
5995
5996 /*
5997 * when renaming files across directories, if the directory
5998 * there we're unlinking from gets fsync'd later on, there's
5999 * no way to find the destination directory later and fsync it
6000 * properly. So, we have to be conservative and force commits
6001 * so the new name gets discovered.
6002 */
6003 if (for_rename)
6004 goto record;
6005
6006 /* we can safely do the unlink without any special recording */
6007 return;
6008
6009record:
Nikolay Borisov4176bdb2017-01-18 00:31:28 +02006010 mutex_lock(&dir->log_mutex);
6011 dir->last_unlink_trans = trans->transid;
6012 mutex_unlock(&dir->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04006013}
6014
6015/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006016 * Make sure that if someone attempts to fsync the parent directory of a deleted
6017 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6018 * that after replaying the log tree of the parent directory's root we will not
6019 * see the snapshot anymore and at log replay time we will not see any log tree
6020 * corresponding to the deleted snapshot's root, which could lead to replaying
6021 * it after replaying the log tree of the parent directory (which would replay
6022 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00006023 *
6024 * Must be called before the actual snapshot destroy operation (updates to the
6025 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006026 */
6027void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
Nikolay Borisov43663552017-01-18 00:31:29 +02006028 struct btrfs_inode *dir)
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006029{
Nikolay Borisov43663552017-01-18 00:31:29 +02006030 mutex_lock(&dir->log_mutex);
6031 dir->last_unlink_trans = trans->transid;
6032 mutex_unlock(&dir->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00006033}
6034
6035/*
Chris Mason12fcfd22009-03-24 10:24:20 -04006036 * Call this after adding a new name for a file and it will properly
6037 * update the log to reflect the new name.
6038 *
6039 * It will return zero if all goes well, and it will return 1 if a
6040 * full transaction commit is required.
6041 */
6042int btrfs_log_new_name(struct btrfs_trans_handle *trans,
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006043 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
Chris Mason12fcfd22009-03-24 10:24:20 -04006044 struct dentry *parent)
6045{
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006046 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
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))
Chris Mason12fcfd22009-03-24 10:24:20 -04006061 return 0;
6062
Nikolay Borisovf8822742018-02-27 17:37:17 +02006063 return btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6064 LOG_INODE_EXISTS, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04006065}
6066