blob: f79682937faf469a90b8526467a903ab62005bbe [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>
Miao Xie995946d2014-04-02 19:51:06 +080023#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040024#include "disk-io.h"
25#include "locking.h"
26#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070027#include "backref.h"
Mark Fashehf1863732012-08-08 11:32:27 -070028#include "hash.h"
Anand Jainebb87652016-03-10 17:26:59 +080029#include "compression.h"
Qu Wenruodf2c95f2016-08-15 10:36:52 +080030#include "qgroup.h"
Liu Boefb1cbc2018-01-25 11:02:56 -070031#include "inode-map.h"
Chris Masone02119d2008-09-05 16:13:11 -040032
33/* magic values for the inode_only field in btrfs_log_inode:
34 *
35 * LOG_INODE_ALL means to log everything
36 * LOG_INODE_EXISTS means to log just enough to recreate the inode
37 * during log replay
38 */
39#define LOG_INODE_ALL 0
40#define LOG_INODE_EXISTS 1
Liu Bo67312122016-11-30 16:20:25 -080041#define LOG_OTHER_INODE 2
Chris Masone02119d2008-09-05 16:13:11 -040042
43/*
Chris Mason12fcfd22009-03-24 10:24:20 -040044 * directory trouble cases
45 *
46 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
47 * log, we must force a full commit before doing an fsync of the directory
48 * where the unlink was done.
49 * ---> record transid of last unlink/rename per directory
50 *
51 * mkdir foo/some_dir
52 * normal commit
53 * rename foo/some_dir foo2/some_dir
54 * mkdir foo/some_dir
55 * fsync foo/some_dir/some_file
56 *
57 * The fsync above will unlink the original some_dir without recording
58 * it in its new location (foo2). After a crash, some_dir will be gone
59 * unless the fsync of some_file forces a full commit
60 *
61 * 2) we must log any new names for any file or dir that is in the fsync
62 * log. ---> check inode while renaming/linking.
63 *
64 * 2a) we must log any new names for any file or dir during rename
65 * when the directory they are being removed from was logged.
66 * ---> check inode and old parent dir during rename
67 *
68 * 2a is actually the more important variant. With the extra logging
69 * a crash might unlink the old name without recreating the new one
70 *
71 * 3) after a crash, we must go through any directories with a link count
72 * of zero and redo the rm -rf
73 *
74 * mkdir f1/foo
75 * normal commit
76 * rm -rf f1/foo
77 * fsync(f1)
78 *
79 * The directory f1 was fully removed from the FS, but fsync was never
80 * called on f1, only its parent dir. After a crash the rm -rf must
81 * be replayed. This must be able to recurse down the entire
82 * directory tree. The inode link count fixup code takes care of the
83 * ugly details.
84 */
85
86/*
Chris Masone02119d2008-09-05 16:13:11 -040087 * stages for the tree walking. The first
88 * stage (0) is to only pin down the blocks we find
89 * the second stage (1) is to make sure that all the inodes
90 * we find in the log are created in the subvolume.
91 *
92 * The last stage is to deal with directories and links and extents
93 * and all the other fun semantics
94 */
95#define LOG_WALK_PIN_ONLY 0
96#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040097#define LOG_WALK_REPLAY_DIR_INDEX 2
98#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040099
Chris Mason12fcfd22009-03-24 10:24:20 -0400100static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +0100101 struct btrfs_root *root, struct inode *inode,
102 int inode_only,
103 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +0100104 const loff_t end,
105 struct btrfs_log_ctx *ctx);
Yan Zhengec051c02009-01-05 15:43:42 -0500106static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
107 struct btrfs_root *root,
108 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400109static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
110 struct btrfs_root *root,
111 struct btrfs_root *log,
112 struct btrfs_path *path,
113 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400114
115/*
116 * tree logging is a special write ahead log used to make sure that
117 * fsyncs and O_SYNCs can happen without doing full tree commits.
118 *
119 * Full tree commits are expensive because they require commonly
120 * modified blocks to be recowed, creating many dirty pages in the
121 * extent tree an 4x-6x higher write load than ext3.
122 *
123 * Instead of doing a tree commit on every fsync, we use the
124 * key ranges and transaction ids to find items for a given file or directory
125 * that have changed in this transaction. Those items are copied into
126 * a special tree (one per subvolume root), that tree is written to disk
127 * and then the fsync is considered complete.
128 *
129 * After a crash, items are copied out of the log-tree back into the
130 * subvolume tree. Any file data extents found are recorded in the extent
131 * allocation tree, and the log-tree freed.
132 *
133 * The log tree is read three times, once to pin down all the extents it is
134 * using in ram and once, once to create all the inodes logged in the tree
135 * and once to do all the other items.
136 */
137
138/*
Chris Masone02119d2008-09-05 16:13:11 -0400139 * start a sub transaction and setup the log tree
140 * this increments the log tree writer count to make the people
141 * syncing the tree wait for us to finish
142 */
143static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800144 struct btrfs_root *root,
145 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400146{
Zhaolei34eb2a52015-08-17 18:44:45 +0800147 int ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500148
149 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800150
Yan Zheng7237f182009-01-21 12:54:03 -0500151 if (root->log_root) {
Miao Xie995946d2014-04-02 19:51:06 +0800152 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800153 ret = -EAGAIN;
154 goto out;
155 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800156
Josef Bacikff782e02009-10-08 15:30:04 -0400157 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800158 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800159 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400160 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800161 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400162 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800163 } else {
164 mutex_lock(&root->fs_info->tree_log_mutex);
165 if (!root->fs_info->log_root_tree)
166 ret = btrfs_init_log_root_tree(trans, root->fs_info);
167 mutex_unlock(&root->fs_info->tree_log_mutex);
168 if (ret)
169 goto out;
Josef Bacikff782e02009-10-08 15:30:04 -0400170
Chris Masone02119d2008-09-05 16:13:11 -0400171 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400172 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800173 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800174
175 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
176 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400177 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800178
Miao Xie2ecb7922012-09-06 04:04:27 -0600179 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500180 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800181 if (ctx) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800182 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800183 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800184 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800185 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800186
Miao Xiee87ac132014-02-20 18:08:53 +0800187out:
Yan Zheng7237f182009-01-21 12:54:03 -0500188 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800189 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400190}
191
192/*
193 * returns 0 if there was a log transaction running and we were able
194 * to join, or returns -ENOENT if there were not transactions
195 * in progress
196 */
197static int join_running_log_trans(struct btrfs_root *root)
198{
199 int ret = -ENOENT;
200
201 smp_mb();
202 if (!root->log_root)
203 return -ENOENT;
204
Yan Zheng7237f182009-01-21 12:54:03 -0500205 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400206 if (root->log_root) {
207 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500208 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400209 }
Yan Zheng7237f182009-01-21 12:54:03 -0500210 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400211 return ret;
212}
213
214/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400215 * This either makes the current running log transaction wait
216 * until you call btrfs_end_log_trans() or it makes any future
217 * log transactions wait until you call btrfs_end_log_trans()
218 */
219int btrfs_pin_log_trans(struct btrfs_root *root)
220{
221 int ret = -ENOENT;
222
223 mutex_lock(&root->log_mutex);
224 atomic_inc(&root->log_writers);
225 mutex_unlock(&root->log_mutex);
226 return ret;
227}
228
229/*
Chris Masone02119d2008-09-05 16:13:11 -0400230 * indicate we're done making changes to the log tree
231 * and wake up anyone waiting to do a sync
232 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100233void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400234{
Yan Zheng7237f182009-01-21 12:54:03 -0500235 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +0100236 /*
237 * Implicit memory barrier after atomic_dec_and_test
238 */
Yan Zheng7237f182009-01-21 12:54:03 -0500239 if (waitqueue_active(&root->log_writer_wait))
240 wake_up(&root->log_writer_wait);
241 }
Chris Masone02119d2008-09-05 16:13:11 -0400242}
243
244
245/*
246 * the walk control struct is used to pass state down the chain when
247 * processing the log tree. The stage field tells us which part
248 * of the log tree processing we are currently doing. The others
249 * are state fields used for that specific part
250 */
251struct walk_control {
252 /* should we free the extent on disk when done? This is used
253 * at transaction commit time while freeing a log tree
254 */
255 int free;
256
257 /* should we write out the extent buffer? This is used
258 * while flushing the log tree to disk during a sync
259 */
260 int write;
261
262 /* should we wait for the extent buffer io to finish? Also used
263 * while flushing the log tree to disk for a sync
264 */
265 int wait;
266
267 /* pin only walk, we record which extents on disk belong to the
268 * log trees
269 */
270 int pin;
271
272 /* what stage of the replay code we're currently in */
273 int stage;
274
275 /* the root we are currently replaying */
276 struct btrfs_root *replay_dest;
277
278 /* the trans handle for the current replay */
279 struct btrfs_trans_handle *trans;
280
281 /* the function that gets used to process blocks we find in the
282 * tree. Note the extent_buffer might not be up to date when it is
283 * passed in, and it must be checked or read if you need the data
284 * inside it
285 */
286 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
287 struct walk_control *wc, u64 gen);
288};
289
290/*
291 * process_func used to pin down extents, write them or wait on them
292 */
293static int process_one_buffer(struct btrfs_root *log,
294 struct extent_buffer *eb,
295 struct walk_control *wc, u64 gen)
296{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400297 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400298
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400299 /*
300 * If this fs is mixed then we need to be able to process the leaves to
301 * pin down any logged extents, so we have to read the block.
302 */
303 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
304 ret = btrfs_read_buffer(eb, gen);
305 if (ret)
306 return ret;
307 }
308
Josef Bacikb50c6e22013-04-25 15:55:30 -0400309 if (wc->pin)
310 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
311 eb->start, eb->len);
312
313 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400314 if (wc->pin && btrfs_header_level(eb) == 0)
315 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400316 if (wc->write)
317 btrfs_write_tree_block(eb);
318 if (wc->wait)
319 btrfs_wait_tree_block_writeback(eb);
320 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400321 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400322}
323
324/*
325 * Item overwrite used by replay and tree logging. eb, slot and key all refer
326 * to the src data we are copying out.
327 *
328 * root is the tree we are copying into, and path is a scratch
329 * path for use in this function (it should be released on entry and
330 * will be released on exit).
331 *
332 * If the key is already in the destination tree the existing item is
333 * overwritten. If the existing item isn't big enough, it is extended.
334 * If it is too large, it is truncated.
335 *
336 * If the key isn't in the destination yet, a new item is inserted.
337 */
338static noinline int overwrite_item(struct btrfs_trans_handle *trans,
339 struct btrfs_root *root,
340 struct btrfs_path *path,
341 struct extent_buffer *eb, int slot,
342 struct btrfs_key *key)
343{
344 int ret;
345 u32 item_size;
346 u64 saved_i_size = 0;
347 int save_old_i_size = 0;
348 unsigned long src_ptr;
349 unsigned long dst_ptr;
350 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000351 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400352
353 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
354 overwrite_root = 1;
355
356 item_size = btrfs_item_size_nr(eb, slot);
357 src_ptr = btrfs_item_ptr_offset(eb, slot);
358
359 /* look for the key in the destination tree */
360 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000361 if (ret < 0)
362 return ret;
363
Chris Masone02119d2008-09-05 16:13:11 -0400364 if (ret == 0) {
365 char *src_copy;
366 char *dst_copy;
367 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
368 path->slots[0]);
369 if (dst_size != item_size)
370 goto insert;
371
372 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200373 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400374 return 0;
375 }
376 dst_copy = kmalloc(item_size, GFP_NOFS);
377 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000378 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200379 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000380 kfree(dst_copy);
381 kfree(src_copy);
382 return -ENOMEM;
383 }
Chris Masone02119d2008-09-05 16:13:11 -0400384
385 read_extent_buffer(eb, src_copy, src_ptr, item_size);
386
387 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
388 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
389 item_size);
390 ret = memcmp(dst_copy, src_copy, item_size);
391
392 kfree(dst_copy);
393 kfree(src_copy);
394 /*
395 * they have the same contents, just return, this saves
396 * us from cowing blocks in the destination tree and doing
397 * extra writes that may not have been done by a previous
398 * sync
399 */
400 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200401 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400402 return 0;
403 }
404
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000405 /*
406 * We need to load the old nbytes into the inode so when we
407 * replay the extents we've logged we get the right nbytes.
408 */
409 if (inode_item) {
410 struct btrfs_inode_item *item;
411 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400412 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000413
414 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
415 struct btrfs_inode_item);
416 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
417 item = btrfs_item_ptr(eb, slot,
418 struct btrfs_inode_item);
419 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400420
421 /*
422 * If this is a directory we need to reset the i_size to
423 * 0 so that we can set it up properly when replaying
424 * the rest of the items in this log.
425 */
426 mode = btrfs_inode_mode(eb, item);
427 if (S_ISDIR(mode))
428 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000429 }
430 } else if (inode_item) {
431 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400432 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000433
434 /*
435 * New inode, set nbytes to 0 so that the nbytes comes out
436 * properly when we replay the extents.
437 */
438 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
439 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400440
441 /*
442 * If this is a directory we need to reset the i_size to 0 so
443 * that we can set it up properly when replaying the rest of
444 * the items in this log.
445 */
446 mode = btrfs_inode_mode(eb, item);
447 if (S_ISDIR(mode))
448 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400449 }
450insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200451 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400452 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000453 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400454 ret = btrfs_insert_empty_item(trans, root, path,
455 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000456 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400457
458 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000459 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400460 u32 found_size;
461 found_size = btrfs_item_size_nr(path->nodes[0],
462 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100463 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000464 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100465 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000466 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100467 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400468 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400469 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400470 }
471 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
472 path->slots[0]);
473
474 /* don't overwrite an existing inode if the generation number
475 * was logged as zero. This is done when the tree logging code
476 * is just logging an inode to make sure it exists after recovery.
477 *
478 * Also, don't overwrite i_size on directories during replay.
479 * log replay inserts and removes directory items based on the
480 * state of the tree found in the subvolume, and i_size is modified
481 * as it goes
482 */
483 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
484 struct btrfs_inode_item *src_item;
485 struct btrfs_inode_item *dst_item;
486
487 src_item = (struct btrfs_inode_item *)src_ptr;
488 dst_item = (struct btrfs_inode_item *)dst_ptr;
489
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000490 if (btrfs_inode_generation(eb, src_item) == 0) {
491 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000492 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000493
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000494 /*
495 * For regular files an ino_size == 0 is used only when
496 * logging that an inode exists, as part of a directory
497 * fsync, and the inode wasn't fsynced before. In this
498 * case don't set the size of the inode in the fs/subvol
499 * tree, otherwise we would be throwing valid data away.
500 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000501 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000502 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
503 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000504 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000505
506 btrfs_init_map_token(&token);
507 btrfs_set_token_inode_size(dst_eb, dst_item,
508 ino_size, &token);
509 }
Chris Masone02119d2008-09-05 16:13:11 -0400510 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000511 }
Chris Masone02119d2008-09-05 16:13:11 -0400512
513 if (overwrite_root &&
514 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
515 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
516 save_old_i_size = 1;
517 saved_i_size = btrfs_inode_size(path->nodes[0],
518 dst_item);
519 }
520 }
521
522 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
523 src_ptr, item_size);
524
525 if (save_old_i_size) {
526 struct btrfs_inode_item *dst_item;
527 dst_item = (struct btrfs_inode_item *)dst_ptr;
528 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
529 }
530
531 /* make sure the generation is filled in */
532 if (key->type == BTRFS_INODE_ITEM_KEY) {
533 struct btrfs_inode_item *dst_item;
534 dst_item = (struct btrfs_inode_item *)dst_ptr;
535 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
536 btrfs_set_inode_generation(path->nodes[0], dst_item,
537 trans->transid);
538 }
539 }
540no_copy:
541 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200542 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400543 return 0;
544}
545
546/*
547 * simple helper to read an inode off the disk from a given root
548 * This can only be called for subvolume roots and not for the log
549 */
550static noinline struct inode *read_one_inode(struct btrfs_root *root,
551 u64 objectid)
552{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400553 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400554 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400555
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400556 key.objectid = objectid;
557 key.type = BTRFS_INODE_ITEM_KEY;
558 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000559 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400560 if (IS_ERR(inode)) {
561 inode = NULL;
562 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400563 iput(inode);
564 inode = NULL;
565 }
566 return inode;
567}
568
569/* replays a single extent in 'eb' at 'slot' with 'key' into the
570 * subvolume 'root'. path is released on entry and should be released
571 * on exit.
572 *
573 * extents in the log tree have not been allocated out of the extent
574 * tree yet. So, this completes the allocation, taking a reference
575 * as required if the extent already exists or creating a new extent
576 * if it isn't in the extent allocation tree yet.
577 *
578 * The extent is inserted into the file, dropping any existing extents
579 * from the file that overlap the new one.
580 */
581static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
582 struct btrfs_root *root,
583 struct btrfs_path *path,
584 struct extent_buffer *eb, int slot,
585 struct btrfs_key *key)
586{
587 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400588 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400589 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000590 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400591 struct btrfs_file_extent_item *item;
592 struct inode *inode = NULL;
593 unsigned long size;
594 int ret = 0;
595
596 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
597 found_type = btrfs_file_extent_type(eb, item);
598
Yan Zhengd899e052008-10-30 14:25:28 -0400599 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000600 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
601 nbytes = btrfs_file_extent_num_bytes(eb, item);
602 extent_end = start + nbytes;
603
604 /*
605 * We don't add to the inodes nbytes if we are prealloc or a
606 * hole.
607 */
608 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
609 nbytes = 0;
610 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800611 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000612 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000613 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400614 } else {
615 ret = 0;
616 goto out;
617 }
618
619 inode = read_one_inode(root, key->objectid);
620 if (!inode) {
621 ret = -EIO;
622 goto out;
623 }
624
625 /*
626 * first check to see if we already have this extent in the
627 * file. This must be done before the btrfs_drop_extents run
628 * so we don't try to drop this extent.
629 */
Li Zefan33345d012011-04-20 10:31:50 +0800630 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400631 start, 0);
632
Yan Zhengd899e052008-10-30 14:25:28 -0400633 if (ret == 0 &&
634 (found_type == BTRFS_FILE_EXTENT_REG ||
635 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400636 struct btrfs_file_extent_item cmp1;
637 struct btrfs_file_extent_item cmp2;
638 struct btrfs_file_extent_item *existing;
639 struct extent_buffer *leaf;
640
641 leaf = path->nodes[0];
642 existing = btrfs_item_ptr(leaf, path->slots[0],
643 struct btrfs_file_extent_item);
644
645 read_extent_buffer(eb, &cmp1, (unsigned long)item,
646 sizeof(cmp1));
647 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
648 sizeof(cmp2));
649
650 /*
651 * we already have a pointer to this exact extent,
652 * we don't have to do anything
653 */
654 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200655 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400656 goto out;
657 }
658 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200659 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400660
661 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400662 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400663 if (ret)
664 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400665
Yan Zheng07d400a2009-01-06 11:42:00 -0500666 if (found_type == BTRFS_FILE_EXTENT_REG ||
667 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400668 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500669 unsigned long dest_offset;
670 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400671
Yan Zheng07d400a2009-01-06 11:42:00 -0500672 ret = btrfs_insert_empty_item(trans, root, path, key,
673 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400674 if (ret)
675 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500676 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
677 path->slots[0]);
678 copy_extent_buffer(path->nodes[0], eb, dest_offset,
679 (unsigned long)item, sizeof(*item));
680
681 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
682 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
683 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400684 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500685
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800686 /*
687 * Manually record dirty extent, as here we did a shallow
688 * file extent item copy and skip normal backref update,
689 * but modifying extent tree all by ourselves.
690 * So need to manually record dirty extent for qgroup,
691 * as the owner of the file extent changed from log tree
692 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
693 */
694 ret = btrfs_qgroup_insert_dirty_extent(trans, root->fs_info,
695 btrfs_file_extent_disk_bytenr(eb, item),
696 btrfs_file_extent_disk_num_bytes(eb, item),
697 GFP_NOFS);
698 if (ret < 0)
699 goto out;
700
Yan Zheng07d400a2009-01-06 11:42:00 -0500701 if (ins.objectid > 0) {
702 u64 csum_start;
703 u64 csum_end;
704 LIST_HEAD(ordered_sums);
705 /*
706 * is this extent already allocated in the extent
707 * allocation tree? If so, just add a reference
708 */
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000709 ret = btrfs_lookup_data_extent(root, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500710 ins.offset);
711 if (ret == 0) {
712 ret = btrfs_inc_extent_ref(trans, root,
713 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400714 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100715 key->objectid, offset);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400716 if (ret)
717 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500718 } else {
719 /*
720 * insert the extent pointer in the extent
721 * allocation tree
722 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400723 ret = btrfs_alloc_logged_file_extent(trans,
724 root, root->root_key.objectid,
725 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400726 if (ret)
727 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500728 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200729 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500730
731 if (btrfs_file_extent_compression(eb, item)) {
732 csum_start = ins.objectid;
733 csum_end = csum_start + ins.offset;
734 } else {
735 csum_start = ins.objectid +
736 btrfs_file_extent_offset(eb, item);
737 csum_end = csum_start +
738 btrfs_file_extent_num_bytes(eb, item);
739 }
740
741 ret = btrfs_lookup_csums_range(root->log_root,
742 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100743 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400744 if (ret)
745 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100746 /*
747 * Now delete all existing cums in the csum root that
748 * cover our range. We do this because we can have an
749 * extent that is completely referenced by one file
750 * extent item and partially referenced by another
751 * file extent item (like after using the clone or
752 * extent_same ioctls). In this case if we end up doing
753 * the replay of the one that partially references the
754 * extent first, and we do not do the csum deletion
755 * below, we can get 2 csum items in the csum tree that
756 * overlap each other. For example, imagine our log has
757 * the two following file extent items:
758 *
759 * key (257 EXTENT_DATA 409600)
760 * extent data disk byte 12845056 nr 102400
761 * extent data offset 20480 nr 20480 ram 102400
762 *
763 * key (257 EXTENT_DATA 819200)
764 * extent data disk byte 12845056 nr 102400
765 * extent data offset 0 nr 102400 ram 102400
766 *
767 * Where the second one fully references the 100K extent
768 * that starts at disk byte 12845056, and the log tree
769 * has a single csum item that covers the entire range
770 * of the extent:
771 *
772 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
773 *
774 * After the first file extent item is replayed, the
775 * csum tree gets the following csum item:
776 *
777 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
778 *
779 * Which covers the 20K sub-range starting at offset 20K
780 * of our extent. Now when we replay the second file
781 * extent item, if we do not delete existing csum items
782 * that cover any of its blocks, we end up getting two
783 * csum items in our csum tree that overlap each other:
784 *
785 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
786 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
787 *
788 * Which is a problem, because after this anyone trying
789 * to lookup up for the checksum of any block of our
790 * extent starting at an offset of 40K or higher, will
791 * end up looking at the second csum item only, which
792 * does not contain the checksum for any block starting
793 * at offset 40K or higher of our extent.
794 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500795 while (!list_empty(&ordered_sums)) {
796 struct btrfs_ordered_sum *sums;
797 sums = list_entry(ordered_sums.next,
798 struct btrfs_ordered_sum,
799 list);
Josef Bacik36508602013-04-25 16:23:32 -0400800 if (!ret)
Filipe Mananab84b8392015-08-19 11:09:40 +0100801 ret = btrfs_del_csums(trans,
802 root->fs_info->csum_root,
803 sums->bytenr,
804 sums->len);
805 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400806 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500807 root->fs_info->csum_root,
808 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500809 list_del(&sums->list);
810 kfree(sums);
811 }
Josef Bacik36508602013-04-25 16:23:32 -0400812 if (ret)
813 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500814 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200815 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500816 }
817 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
818 /* inline extents are easy, we just overwrite them */
819 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400820 if (ret)
821 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500822 }
823
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000824 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600825 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400826out:
827 if (inode)
828 iput(inode);
829 return ret;
830}
831
832/*
833 * when cleaning up conflicts between the directory names in the
834 * subvolume, directory names in the log and directory names in the
835 * inode back references, we may have to unlink inodes from directories.
836 *
837 * This is a helper function to do the unlink of a specific directory
838 * item
839 */
840static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
841 struct btrfs_root *root,
842 struct btrfs_path *path,
843 struct inode *dir,
844 struct btrfs_dir_item *di)
845{
846 struct inode *inode;
847 char *name;
848 int name_len;
849 struct extent_buffer *leaf;
850 struct btrfs_key location;
851 int ret;
852
853 leaf = path->nodes[0];
854
855 btrfs_dir_item_key_to_cpu(leaf, di, &location);
856 name_len = btrfs_dir_name_len(leaf, di);
857 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000858 if (!name)
859 return -ENOMEM;
860
Chris Masone02119d2008-09-05 16:13:11 -0400861 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200862 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400863
864 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000865 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400866 ret = -EIO;
867 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000868 }
Chris Masone02119d2008-09-05 16:13:11 -0400869
Yan Zhengec051c02009-01-05 15:43:42 -0500870 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400871 if (ret)
872 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400873
Chris Masone02119d2008-09-05 16:13:11 -0400874 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400875 if (ret)
876 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100877 else
878 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400879out:
880 kfree(name);
881 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400882 return ret;
883}
884
885/*
886 * helper function to see if a given name and sequence number found
887 * in an inode back reference are already in a directory and correctly
888 * point to this inode
889 */
890static noinline int inode_in_dir(struct btrfs_root *root,
891 struct btrfs_path *path,
892 u64 dirid, u64 objectid, u64 index,
893 const char *name, int name_len)
894{
895 struct btrfs_dir_item *di;
896 struct btrfs_key location;
897 int match = 0;
898
899 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
900 index, name, name_len, 0);
901 if (di && !IS_ERR(di)) {
902 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
903 if (location.objectid != objectid)
904 goto out;
905 } else
906 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200907 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400908
909 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
910 if (di && !IS_ERR(di)) {
911 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
912 if (location.objectid != objectid)
913 goto out;
914 } else
915 goto out;
916 match = 1;
917out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200918 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400919 return match;
920}
921
922/*
923 * helper function to check a log tree for a named back reference in
924 * an inode. This is used to decide if a back reference that is
925 * found in the subvolume conflicts with what we find in the log.
926 *
927 * inode backreferences may have multiple refs in a single item,
928 * during replay we process one reference at a time, and we don't
929 * want to delete valid links to a file from the subvolume if that
930 * link is also in the log.
931 */
932static noinline int backref_in_log(struct btrfs_root *log,
933 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700934 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000935 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400936{
937 struct btrfs_path *path;
938 struct btrfs_inode_ref *ref;
939 unsigned long ptr;
940 unsigned long ptr_end;
941 unsigned long name_ptr;
942 int found_name_len;
943 int item_size;
944 int ret;
945 int match = 0;
946
947 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000948 if (!path)
949 return -ENOMEM;
950
Chris Masone02119d2008-09-05 16:13:11 -0400951 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
952 if (ret != 0)
953 goto out;
954
Chris Masone02119d2008-09-05 16:13:11 -0400955 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700956
957 if (key->type == BTRFS_INODE_EXTREF_KEY) {
958 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
959 name, namelen, NULL))
960 match = 1;
961
962 goto out;
963 }
964
965 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400966 ptr_end = ptr + item_size;
967 while (ptr < ptr_end) {
968 ref = (struct btrfs_inode_ref *)ptr;
969 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
970 if (found_name_len == namelen) {
971 name_ptr = (unsigned long)(ref + 1);
972 ret = memcmp_extent_buffer(path->nodes[0], name,
973 name_ptr, namelen);
974 if (ret == 0) {
975 match = 1;
976 goto out;
977 }
978 }
979 ptr = (unsigned long)(ref + 1) + found_name_len;
980 }
981out:
982 btrfs_free_path(path);
983 return match;
984}
985
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700986static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
987 struct btrfs_root *root,
988 struct btrfs_path *path,
989 struct btrfs_root *log_root,
990 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700991 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700992 u64 inode_objectid, u64 parent_objectid,
993 u64 ref_index, char *name, int namelen,
994 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700995{
996 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700997 char *victim_name;
998 int victim_name_len;
999 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001000 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -07001001 struct btrfs_key search_key;
1002 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001003
Mark Fashehf1863732012-08-08 11:32:27 -07001004again:
1005 /* Search old style refs */
1006 search_key.objectid = inode_objectid;
1007 search_key.type = BTRFS_INODE_REF_KEY;
1008 search_key.offset = parent_objectid;
1009 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001010 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001011 struct btrfs_inode_ref *victim_ref;
1012 unsigned long ptr;
1013 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001014
1015 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001016
1017 /* are we trying to overwrite a back ref for the root directory
1018 * if so, just jump out, we're done
1019 */
Mark Fashehf1863732012-08-08 11:32:27 -07001020 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001021 return 1;
1022
1023 /* check all the names in this back reference to see
1024 * if they are in the log. if so, we allow them to stay
1025 * otherwise they must be unlinked as a conflict
1026 */
1027 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1028 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1029 while (ptr < ptr_end) {
1030 victim_ref = (struct btrfs_inode_ref *)ptr;
1031 victim_name_len = btrfs_inode_ref_name_len(leaf,
1032 victim_ref);
1033 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001034 if (!victim_name)
1035 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001036
1037 read_extent_buffer(leaf, victim_name,
1038 (unsigned long)(victim_ref + 1),
1039 victim_name_len);
1040
Mark Fashehf1863732012-08-08 11:32:27 -07001041 if (!backref_in_log(log_root, &search_key,
1042 parent_objectid,
1043 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001044 victim_name_len)) {
Zach Brown8b558c52013-10-16 12:10:34 -07001045 inc_nlink(inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001046 btrfs_release_path(path);
1047
1048 ret = btrfs_unlink_inode(trans, root, dir,
1049 inode, victim_name,
1050 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001051 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001052 if (ret)
1053 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001054 ret = btrfs_run_delayed_items(trans, root);
1055 if (ret)
1056 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001057 *search_done = 1;
1058 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001059 }
1060 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001061
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001062 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1063 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001064
1065 /*
1066 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001067 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001068 */
1069 *search_done = 1;
1070 }
1071 btrfs_release_path(path);
1072
Mark Fashehf1863732012-08-08 11:32:27 -07001073 /* Same search but for extended refs */
1074 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1075 inode_objectid, parent_objectid, 0,
1076 0);
1077 if (!IS_ERR_OR_NULL(extref)) {
1078 u32 item_size;
1079 u32 cur_offset = 0;
1080 unsigned long base;
1081 struct inode *victim_parent;
1082
1083 leaf = path->nodes[0];
1084
1085 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1086 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1087
1088 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001089 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001090
1091 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1092
1093 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1094 goto next;
1095
1096 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001097 if (!victim_name)
1098 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001099 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1100 victim_name_len);
1101
1102 search_key.objectid = inode_objectid;
1103 search_key.type = BTRFS_INODE_EXTREF_KEY;
1104 search_key.offset = btrfs_extref_hash(parent_objectid,
1105 victim_name,
1106 victim_name_len);
1107 ret = 0;
1108 if (!backref_in_log(log_root, &search_key,
1109 parent_objectid, victim_name,
1110 victim_name_len)) {
1111 ret = -ENOENT;
1112 victim_parent = read_one_inode(root,
1113 parent_objectid);
1114 if (victim_parent) {
Zach Brown8b558c52013-10-16 12:10:34 -07001115 inc_nlink(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001116 btrfs_release_path(path);
1117
1118 ret = btrfs_unlink_inode(trans, root,
1119 victim_parent,
1120 inode,
1121 victim_name,
1122 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001123 if (!ret)
1124 ret = btrfs_run_delayed_items(
1125 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001126 }
Mark Fashehf1863732012-08-08 11:32:27 -07001127 iput(victim_parent);
1128 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001129 if (ret)
1130 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001131 *search_done = 1;
1132 goto again;
1133 }
1134 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001135 if (ret)
1136 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001137next:
1138 cur_offset += victim_name_len + sizeof(*extref);
1139 }
1140 *search_done = 1;
1141 }
1142 btrfs_release_path(path);
1143
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001144 /* look for a conflicting sequence number */
1145 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001146 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001147 if (di && !IS_ERR(di)) {
1148 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001149 if (ret)
1150 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001151 }
1152 btrfs_release_path(path);
1153
1154 /* look for a conflicing name */
1155 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1156 name, namelen, 0);
1157 if (di && !IS_ERR(di)) {
1158 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 return 0;
1165}
Chris Masone02119d2008-09-05 16:13:11 -04001166
Mark Fashehf1863732012-08-08 11:32:27 -07001167static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1168 u32 *namelen, char **name, u64 *index,
1169 u64 *parent_objectid)
1170{
1171 struct btrfs_inode_extref *extref;
1172
1173 extref = (struct btrfs_inode_extref *)ref_ptr;
1174
1175 *namelen = btrfs_inode_extref_name_len(eb, extref);
1176 *name = kmalloc(*namelen, GFP_NOFS);
1177 if (*name == NULL)
1178 return -ENOMEM;
1179
1180 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1181 *namelen);
1182
1183 *index = btrfs_inode_extref_index(eb, extref);
1184 if (parent_objectid)
1185 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1186
1187 return 0;
1188}
1189
1190static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1191 u32 *namelen, char **name, u64 *index)
1192{
1193 struct btrfs_inode_ref *ref;
1194
1195 ref = (struct btrfs_inode_ref *)ref_ptr;
1196
1197 *namelen = btrfs_inode_ref_name_len(eb, ref);
1198 *name = kmalloc(*namelen, GFP_NOFS);
1199 if (*name == NULL)
1200 return -ENOMEM;
1201
1202 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1203
1204 *index = btrfs_inode_ref_index(eb, ref);
1205
1206 return 0;
1207}
1208
Chris Masone02119d2008-09-05 16:13:11 -04001209/*
1210 * replay one inode back reference item found in the log tree.
1211 * eb, slot and key refer to the buffer and key found in the log tree.
1212 * root is the destination we are replaying into, and path is for temp
1213 * use by this function. (it should be released on return).
1214 */
1215static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1216 struct btrfs_root *root,
1217 struct btrfs_root *log,
1218 struct btrfs_path *path,
1219 struct extent_buffer *eb, int slot,
1220 struct btrfs_key *key)
1221{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001222 struct inode *dir = NULL;
1223 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001224 unsigned long ref_ptr;
1225 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001226 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001227 int namelen;
1228 int ret;
liuboc622ae62011-03-26 08:01:12 -04001229 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001230 int log_ref_ver = 0;
1231 u64 parent_objectid;
1232 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001233 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001234 int ref_struct_size;
1235
1236 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1237 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1238
1239 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1240 struct btrfs_inode_extref *r;
1241
1242 ref_struct_size = sizeof(struct btrfs_inode_extref);
1243 log_ref_ver = 1;
1244 r = (struct btrfs_inode_extref *)ref_ptr;
1245 parent_objectid = btrfs_inode_extref_parent(eb, r);
1246 } else {
1247 ref_struct_size = sizeof(struct btrfs_inode_ref);
1248 parent_objectid = key->offset;
1249 }
1250 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001251
Chris Masone02119d2008-09-05 16:13:11 -04001252 /*
1253 * it is possible that we didn't log all the parent directories
1254 * for a given inode. If we don't find the dir, just don't
1255 * copy the back ref in. The link count fixup code will take
1256 * care of the rest
1257 */
Mark Fashehf1863732012-08-08 11:32:27 -07001258 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001259 if (!dir) {
1260 ret = -ENOENT;
1261 goto out;
1262 }
Chris Masone02119d2008-09-05 16:13:11 -04001263
Mark Fashehf1863732012-08-08 11:32:27 -07001264 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001265 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001266 ret = -EIO;
1267 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001268 }
Chris Masone02119d2008-09-05 16:13:11 -04001269
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001270 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001271 if (log_ref_ver) {
1272 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1273 &ref_index, &parent_objectid);
1274 /*
1275 * parent object can change from one array
1276 * item to another.
1277 */
1278 if (!dir)
1279 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001280 if (!dir) {
1281 ret = -ENOENT;
1282 goto out;
1283 }
Mark Fashehf1863732012-08-08 11:32:27 -07001284 } else {
1285 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1286 &ref_index);
1287 }
1288 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001289 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001290
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001291 /* if we already have a perfect match, we're done */
1292 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001293 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001294 /*
1295 * look for a conflicting back reference in the
1296 * metadata. if we find one we have to unlink that name
1297 * of the file before we add our new link. Later on, we
1298 * overwrite any existing back reference, and we don't
1299 * want to create dangling pointers in the directory.
1300 */
Chris Masone02119d2008-09-05 16:13:11 -04001301
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001302 if (!search_done) {
1303 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001304 dir, inode, eb,
1305 inode_objectid,
1306 parent_objectid,
1307 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001308 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001309 if (ret) {
1310 if (ret == 1)
1311 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001312 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001313 }
Chris Masone02119d2008-09-05 16:13:11 -04001314 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001315
1316 /* insert our name */
1317 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001318 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001319 if (ret)
1320 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001321
1322 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001323 }
liuboc622ae62011-03-26 08:01:12 -04001324
Mark Fashehf1863732012-08-08 11:32:27 -07001325 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001326 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001327 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001328 if (log_ref_ver) {
1329 iput(dir);
1330 dir = NULL;
1331 }
Chris Masone02119d2008-09-05 16:13:11 -04001332 }
Chris Masone02119d2008-09-05 16:13:11 -04001333
1334 /* finally write the back reference in the inode */
1335 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001336out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001337 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001338 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001339 iput(dir);
1340 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001341 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001342}
1343
Yan, Zhengc71bf092009-11-12 09:34:40 +00001344static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001345 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001346{
1347 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001348
David Sterba9c4f61f2015-01-02 19:12:57 +01001349 ret = btrfs_insert_orphan_item(trans, root, ino);
1350 if (ret == -EEXIST)
1351 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001352
Yan, Zhengc71bf092009-11-12 09:34:40 +00001353 return ret;
1354}
1355
Mark Fashehf1863732012-08-08 11:32:27 -07001356static int count_inode_extrefs(struct btrfs_root *root,
1357 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001358{
Mark Fashehf1863732012-08-08 11:32:27 -07001359 int ret = 0;
1360 int name_len;
1361 unsigned int nlink = 0;
1362 u32 item_size;
1363 u32 cur_offset = 0;
1364 u64 inode_objectid = btrfs_ino(inode);
1365 u64 offset = 0;
1366 unsigned long ptr;
1367 struct btrfs_inode_extref *extref;
1368 struct extent_buffer *leaf;
1369
1370 while (1) {
1371 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1372 &extref, &offset);
1373 if (ret)
1374 break;
1375
1376 leaf = path->nodes[0];
1377 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1378 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001379 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001380
1381 while (cur_offset < item_size) {
1382 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1383 name_len = btrfs_inode_extref_name_len(leaf, extref);
1384
1385 nlink++;
1386
1387 cur_offset += name_len + sizeof(*extref);
1388 }
1389
1390 offset++;
1391 btrfs_release_path(path);
1392 }
1393 btrfs_release_path(path);
1394
Filipe Manana2c2c4522015-01-13 16:40:04 +00001395 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001396 return ret;
1397 return nlink;
1398}
1399
1400static int count_inode_refs(struct btrfs_root *root,
1401 struct inode *inode, struct btrfs_path *path)
1402{
Chris Masone02119d2008-09-05 16:13:11 -04001403 int ret;
1404 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001405 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001406 unsigned long ptr;
1407 unsigned long ptr_end;
1408 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001409 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001410
Li Zefan33345d012011-04-20 10:31:50 +08001411 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001412 key.type = BTRFS_INODE_REF_KEY;
1413 key.offset = (u64)-1;
1414
Chris Masond3977122009-01-05 21:25:51 -05001415 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001416 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1417 if (ret < 0)
1418 break;
1419 if (ret > 0) {
1420 if (path->slots[0] == 0)
1421 break;
1422 path->slots[0]--;
1423 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001424process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001425 btrfs_item_key_to_cpu(path->nodes[0], &key,
1426 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001427 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001428 key.type != BTRFS_INODE_REF_KEY)
1429 break;
1430 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1431 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1432 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001433 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001434 struct btrfs_inode_ref *ref;
1435
1436 ref = (struct btrfs_inode_ref *)ptr;
1437 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1438 ref);
1439 ptr = (unsigned long)(ref + 1) + name_len;
1440 nlink++;
1441 }
1442
1443 if (key.offset == 0)
1444 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001445 if (path->slots[0] > 0) {
1446 path->slots[0]--;
1447 goto process_slot;
1448 }
Chris Masone02119d2008-09-05 16:13:11 -04001449 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001450 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001451 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001452 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001453
1454 return nlink;
1455}
1456
1457/*
1458 * There are a few corners where the link count of the file can't
1459 * be properly maintained during replay. So, instead of adding
1460 * lots of complexity to the log code, we just scan the backrefs
1461 * for any file that has been through replay.
1462 *
1463 * The scan will update the link count on the inode to reflect the
1464 * number of back refs found. If it goes down to zero, the iput
1465 * will free the inode.
1466 */
1467static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1468 struct btrfs_root *root,
1469 struct inode *inode)
1470{
1471 struct btrfs_path *path;
1472 int ret;
1473 u64 nlink = 0;
1474 u64 ino = btrfs_ino(inode);
1475
1476 path = btrfs_alloc_path();
1477 if (!path)
1478 return -ENOMEM;
1479
1480 ret = count_inode_refs(root, inode, path);
1481 if (ret < 0)
1482 goto out;
1483
1484 nlink = ret;
1485
1486 ret = count_inode_extrefs(root, inode, path);
Mark Fashehf1863732012-08-08 11:32:27 -07001487 if (ret < 0)
1488 goto out;
1489
1490 nlink += ret;
1491
1492 ret = 0;
1493
Chris Masone02119d2008-09-05 16:13:11 -04001494 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001495 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001496 btrfs_update_inode(trans, root, inode);
1497 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001498 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001499
Yan, Zhengc71bf092009-11-12 09:34:40 +00001500 if (inode->i_nlink == 0) {
1501 if (S_ISDIR(inode->i_mode)) {
1502 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001503 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001504 if (ret)
1505 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001506 }
Li Zefan33345d012011-04-20 10:31:50 +08001507 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001508 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001509
Mark Fashehf1863732012-08-08 11:32:27 -07001510out:
1511 btrfs_free_path(path);
1512 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001513}
1514
1515static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1516 struct btrfs_root *root,
1517 struct btrfs_path *path)
1518{
1519 int ret;
1520 struct btrfs_key key;
1521 struct inode *inode;
1522
1523 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1524 key.type = BTRFS_ORPHAN_ITEM_KEY;
1525 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001526 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001527 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1528 if (ret < 0)
1529 break;
1530
1531 if (ret == 1) {
1532 if (path->slots[0] == 0)
1533 break;
1534 path->slots[0]--;
1535 }
1536
1537 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1538 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1539 key.type != BTRFS_ORPHAN_ITEM_KEY)
1540 break;
1541
1542 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001543 if (ret)
1544 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001545
David Sterbab3b4aa72011-04-21 01:20:15 +02001546 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001547 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001548 if (!inode)
1549 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001550
1551 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001552 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001553 if (ret)
1554 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001555
Chris Mason12fcfd22009-03-24 10:24:20 -04001556 /*
1557 * fixup on a directory may create new entries,
1558 * make sure we always look for the highset possible
1559 * offset
1560 */
1561 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001562 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001563 ret = 0;
1564out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001565 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001566 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001567}
1568
1569
1570/*
1571 * record a given inode in the fixup dir so we can check its link
1572 * count when replay is done. The link count is incremented here
1573 * so the inode won't go away until we check it
1574 */
1575static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1576 struct btrfs_root *root,
1577 struct btrfs_path *path,
1578 u64 objectid)
1579{
1580 struct btrfs_key key;
1581 int ret = 0;
1582 struct inode *inode;
1583
1584 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001585 if (!inode)
1586 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001587
1588 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001589 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001590 key.offset = objectid;
1591
1592 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1593
David Sterbab3b4aa72011-04-21 01:20:15 +02001594 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001595 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001596 if (!inode->i_nlink)
1597 set_nlink(inode, 1);
1598 else
Zach Brown8b558c52013-10-16 12:10:34 -07001599 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001600 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001601 } else if (ret == -EEXIST) {
1602 ret = 0;
1603 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001604 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001605 }
1606 iput(inode);
1607
1608 return ret;
1609}
1610
1611/*
1612 * when replaying the log for a directory, we only insert names
1613 * for inodes that actually exist. This means an fsync on a directory
1614 * does not implicitly fsync all the new files in it
1615 */
1616static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1617 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001618 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001619 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001620 struct btrfs_key *location)
1621{
1622 struct inode *inode;
1623 struct inode *dir;
1624 int ret;
1625
1626 inode = read_one_inode(root, location->objectid);
1627 if (!inode)
1628 return -ENOENT;
1629
1630 dir = read_one_inode(root, dirid);
1631 if (!dir) {
1632 iput(inode);
1633 return -EIO;
1634 }
Josef Bacikd5554382013-09-11 14:17:00 -04001635
Chris Masone02119d2008-09-05 16:13:11 -04001636 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1637
1638 /* FIXME, put inode into FIXUP list */
1639
1640 iput(inode);
1641 iput(dir);
1642 return ret;
1643}
1644
1645/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001646 * Return true if an inode reference exists in the log for the given name,
1647 * inode and parent inode.
1648 */
1649static bool name_in_log_ref(struct btrfs_root *log_root,
1650 const char *name, const int name_len,
1651 const u64 dirid, const u64 ino)
1652{
1653 struct btrfs_key search_key;
1654
1655 search_key.objectid = ino;
1656 search_key.type = BTRFS_INODE_REF_KEY;
1657 search_key.offset = dirid;
1658 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1659 return true;
1660
1661 search_key.type = BTRFS_INODE_EXTREF_KEY;
1662 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1663 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1664 return true;
1665
1666 return false;
1667}
1668
1669/*
Chris Masone02119d2008-09-05 16:13:11 -04001670 * take a single entry in a log directory item and replay it into
1671 * the subvolume.
1672 *
1673 * if a conflicting item exists in the subdirectory already,
1674 * the inode it points to is unlinked and put into the link count
1675 * fix up tree.
1676 *
1677 * If a name from the log points to a file or directory that does
1678 * not exist in the FS, it is skipped. fsyncs on directories
1679 * do not force down inodes inside that directory, just changes to the
1680 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001681 *
1682 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1683 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001684 */
1685static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1686 struct btrfs_root *root,
1687 struct btrfs_path *path,
1688 struct extent_buffer *eb,
1689 struct btrfs_dir_item *di,
1690 struct btrfs_key *key)
1691{
1692 char *name;
1693 int name_len;
1694 struct btrfs_dir_item *dst_di;
1695 struct btrfs_key found_key;
1696 struct btrfs_key log_key;
1697 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001698 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001699 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001700 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001701 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001702 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001703
1704 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001705 if (!dir)
1706 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001707
1708 name_len = btrfs_dir_name_len(eb, di);
1709 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001710 if (!name) {
1711 ret = -ENOMEM;
1712 goto out;
1713 }
liubo2a29edc2011-01-26 06:22:08 +00001714
Chris Masone02119d2008-09-05 16:13:11 -04001715 log_type = btrfs_dir_type(eb, di);
1716 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1717 name_len);
1718
1719 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001720 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1721 if (exists == 0)
1722 exists = 1;
1723 else
1724 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001725 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001726
Chris Masone02119d2008-09-05 16:13:11 -04001727 if (key->type == BTRFS_DIR_ITEM_KEY) {
1728 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1729 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001730 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001731 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1732 key->objectid,
1733 key->offset, name,
1734 name_len, 1);
1735 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001736 /* Corruption */
1737 ret = -EINVAL;
1738 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001739 }
David Sterbac7040052011-04-19 18:00:01 +02001740 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001741 /* we need a sequence number to insert, so we only
1742 * do inserts for the BTRFS_DIR_INDEX_KEY types
1743 */
1744 if (key->type != BTRFS_DIR_INDEX_KEY)
1745 goto out;
1746 goto insert;
1747 }
1748
1749 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1750 /* the existing item matches the logged item */
1751 if (found_key.objectid == log_key.objectid &&
1752 found_key.type == log_key.type &&
1753 found_key.offset == log_key.offset &&
1754 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001755 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001756 goto out;
1757 }
1758
1759 /*
1760 * don't drop the conflicting directory entry if the inode
1761 * for the new entry doesn't exist
1762 */
Chris Mason4bef0842008-09-08 11:18:08 -04001763 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001764 goto out;
1765
Chris Masone02119d2008-09-05 16:13:11 -04001766 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001767 if (ret)
1768 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001769
1770 if (key->type == BTRFS_DIR_INDEX_KEY)
1771 goto insert;
1772out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001773 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001774 if (!ret && update_size) {
1775 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1776 ret = btrfs_update_inode(trans, root, dir);
1777 }
Chris Masone02119d2008-09-05 16:13:11 -04001778 kfree(name);
1779 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001780 if (!ret && name_added)
1781 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001782 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001783
1784insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001785 if (name_in_log_ref(root->log_root, name, name_len,
1786 key->objectid, log_key.objectid)) {
1787 /* The dentry will be added later. */
1788 ret = 0;
1789 update_size = false;
1790 goto out;
1791 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001792 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08001793 ret = insert_one_name(trans, root, key->objectid, key->offset,
1794 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001795 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001796 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001797 if (!ret)
1798 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001799 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001800 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001801 goto out;
1802}
1803
1804/*
1805 * find all the names in a directory item and reconcile them into
1806 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1807 * one name in a directory item, but the same code gets used for
1808 * both directory index types
1809 */
1810static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1811 struct btrfs_root *root,
1812 struct btrfs_path *path,
1813 struct extent_buffer *eb, int slot,
1814 struct btrfs_key *key)
1815{
Filipe Mananabb53eda2015-07-15 23:26:43 +01001816 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001817 u32 item_size = btrfs_item_size_nr(eb, slot);
1818 struct btrfs_dir_item *di;
1819 int name_len;
1820 unsigned long ptr;
1821 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001822 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001823
1824 ptr = btrfs_item_ptr_offset(eb, slot);
1825 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001826 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001827 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001828 if (verify_dir_item(root, eb, di))
1829 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001830 name_len = btrfs_dir_name_len(eb, di);
1831 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001832 if (ret < 0)
1833 break;
Chris Masone02119d2008-09-05 16:13:11 -04001834 ptr = (unsigned long)(di + 1);
1835 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001836
1837 /*
1838 * If this entry refers to a non-directory (directories can not
1839 * have a link count > 1) and it was added in the transaction
1840 * that was not committed, make sure we fixup the link count of
1841 * the inode it the entry points to. Otherwise something like
1842 * the following would result in a directory pointing to an
1843 * inode with a wrong link that does not account for this dir
1844 * entry:
1845 *
1846 * mkdir testdir
1847 * touch testdir/foo
1848 * touch testdir/bar
1849 * sync
1850 *
1851 * ln testdir/bar testdir/bar_link
1852 * ln testdir/foo testdir/foo_link
1853 * xfs_io -c "fsync" testdir/bar
1854 *
1855 * <power failure>
1856 *
1857 * mount fs, log replay happens
1858 *
1859 * File foo would remain with a link count of 1 when it has two
1860 * entries pointing to it in the directory testdir. This would
1861 * make it impossible to ever delete the parent directory has
1862 * it would result in stale dentries that can never be deleted.
1863 */
1864 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1865 struct btrfs_key di_key;
1866
1867 if (!fixup_path) {
1868 fixup_path = btrfs_alloc_path();
1869 if (!fixup_path) {
1870 ret = -ENOMEM;
1871 break;
1872 }
1873 }
1874
1875 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1876 ret = link_to_fixup_dir(trans, root, fixup_path,
1877 di_key.objectid);
1878 if (ret)
1879 break;
1880 }
1881 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001882 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01001883 btrfs_free_path(fixup_path);
1884 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001885}
1886
1887/*
1888 * directory replay has two parts. There are the standard directory
1889 * items in the log copied from the subvolume, and range items
1890 * created in the log while the subvolume was logged.
1891 *
1892 * The range items tell us which parts of the key space the log
1893 * is authoritative for. During replay, if a key in the subvolume
1894 * directory is in a logged range item, but not actually in the log
1895 * that means it was deleted from the directory before the fsync
1896 * and should be removed.
1897 */
1898static noinline int find_dir_range(struct btrfs_root *root,
1899 struct btrfs_path *path,
1900 u64 dirid, int key_type,
1901 u64 *start_ret, u64 *end_ret)
1902{
1903 struct btrfs_key key;
1904 u64 found_end;
1905 struct btrfs_dir_log_item *item;
1906 int ret;
1907 int nritems;
1908
1909 if (*start_ret == (u64)-1)
1910 return 1;
1911
1912 key.objectid = dirid;
1913 key.type = key_type;
1914 key.offset = *start_ret;
1915
1916 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1917 if (ret < 0)
1918 goto out;
1919 if (ret > 0) {
1920 if (path->slots[0] == 0)
1921 goto out;
1922 path->slots[0]--;
1923 }
1924 if (ret != 0)
1925 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1926
1927 if (key.type != key_type || key.objectid != dirid) {
1928 ret = 1;
1929 goto next;
1930 }
1931 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1932 struct btrfs_dir_log_item);
1933 found_end = btrfs_dir_log_end(path->nodes[0], item);
1934
1935 if (*start_ret >= key.offset && *start_ret <= found_end) {
1936 ret = 0;
1937 *start_ret = key.offset;
1938 *end_ret = found_end;
1939 goto out;
1940 }
1941 ret = 1;
1942next:
1943 /* check the next slot in the tree to see if it is a valid item */
1944 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko26dc5242016-10-07 17:30:47 +08001945 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04001946 if (path->slots[0] >= nritems) {
1947 ret = btrfs_next_leaf(root, path);
1948 if (ret)
1949 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001950 }
1951
1952 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1953
1954 if (key.type != key_type || key.objectid != dirid) {
1955 ret = 1;
1956 goto out;
1957 }
1958 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1959 struct btrfs_dir_log_item);
1960 found_end = btrfs_dir_log_end(path->nodes[0], item);
1961 *start_ret = key.offset;
1962 *end_ret = found_end;
1963 ret = 0;
1964out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001965 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001966 return ret;
1967}
1968
1969/*
1970 * this looks for a given directory item in the log. If the directory
1971 * item is not in the log, the item is removed and the inode it points
1972 * to is unlinked
1973 */
1974static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1975 struct btrfs_root *root,
1976 struct btrfs_root *log,
1977 struct btrfs_path *path,
1978 struct btrfs_path *log_path,
1979 struct inode *dir,
1980 struct btrfs_key *dir_key)
1981{
1982 int ret;
1983 struct extent_buffer *eb;
1984 int slot;
1985 u32 item_size;
1986 struct btrfs_dir_item *di;
1987 struct btrfs_dir_item *log_di;
1988 int name_len;
1989 unsigned long ptr;
1990 unsigned long ptr_end;
1991 char *name;
1992 struct inode *inode;
1993 struct btrfs_key location;
1994
1995again:
1996 eb = path->nodes[0];
1997 slot = path->slots[0];
1998 item_size = btrfs_item_size_nr(eb, slot);
1999 ptr = btrfs_item_ptr_offset(eb, slot);
2000 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002001 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002002 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04002003 if (verify_dir_item(root, eb, di)) {
2004 ret = -EIO;
2005 goto out;
2006 }
2007
Chris Masone02119d2008-09-05 16:13:11 -04002008 name_len = btrfs_dir_name_len(eb, di);
2009 name = kmalloc(name_len, GFP_NOFS);
2010 if (!name) {
2011 ret = -ENOMEM;
2012 goto out;
2013 }
2014 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2015 name_len);
2016 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002017 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002018 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2019 dir_key->objectid,
2020 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002021 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002022 log_di = btrfs_lookup_dir_index_item(trans, log,
2023 log_path,
2024 dir_key->objectid,
2025 dir_key->offset,
2026 name, name_len, 0);
2027 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002028 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002029 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002030 btrfs_release_path(path);
2031 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002032 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002033 if (!inode) {
2034 kfree(name);
2035 return -EIO;
2036 }
Chris Masone02119d2008-09-05 16:13:11 -04002037
2038 ret = link_to_fixup_dir(trans, root,
2039 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002040 if (ret) {
2041 kfree(name);
2042 iput(inode);
2043 goto out;
2044 }
2045
Zach Brown8b558c52013-10-16 12:10:34 -07002046 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002047 ret = btrfs_unlink_inode(trans, root, dir, inode,
2048 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002049 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01002050 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04002051 kfree(name);
2052 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002053 if (ret)
2054 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002055
2056 /* there might still be more names under this key
2057 * check and repeat if required
2058 */
2059 ret = btrfs_search_slot(NULL, root, dir_key, path,
2060 0, 0);
2061 if (ret == 0)
2062 goto again;
2063 ret = 0;
2064 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002065 } else if (IS_ERR(log_di)) {
2066 kfree(name);
2067 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002068 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002069 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002070 kfree(name);
2071
2072 ptr = (unsigned long)(di + 1);
2073 ptr += name_len;
2074 }
2075 ret = 0;
2076out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002077 btrfs_release_path(path);
2078 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002079 return ret;
2080}
2081
Filipe Manana4f764e52015-02-23 19:53:35 +00002082static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2083 struct btrfs_root *root,
2084 struct btrfs_root *log,
2085 struct btrfs_path *path,
2086 const u64 ino)
2087{
2088 struct btrfs_key search_key;
2089 struct btrfs_path *log_path;
2090 int i;
2091 int nritems;
2092 int ret;
2093
2094 log_path = btrfs_alloc_path();
2095 if (!log_path)
2096 return -ENOMEM;
2097
2098 search_key.objectid = ino;
2099 search_key.type = BTRFS_XATTR_ITEM_KEY;
2100 search_key.offset = 0;
2101again:
2102 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2103 if (ret < 0)
2104 goto out;
2105process_leaf:
2106 nritems = btrfs_header_nritems(path->nodes[0]);
2107 for (i = path->slots[0]; i < nritems; i++) {
2108 struct btrfs_key key;
2109 struct btrfs_dir_item *di;
2110 struct btrfs_dir_item *log_di;
2111 u32 total_size;
2112 u32 cur;
2113
2114 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2115 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2116 ret = 0;
2117 goto out;
2118 }
2119
2120 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2121 total_size = btrfs_item_size_nr(path->nodes[0], i);
2122 cur = 0;
2123 while (cur < total_size) {
2124 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2125 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2126 u32 this_len = sizeof(*di) + name_len + data_len;
2127 char *name;
2128
2129 name = kmalloc(name_len, GFP_NOFS);
2130 if (!name) {
2131 ret = -ENOMEM;
2132 goto out;
2133 }
2134 read_extent_buffer(path->nodes[0], name,
2135 (unsigned long)(di + 1), name_len);
2136
2137 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2138 name, name_len, 0);
2139 btrfs_release_path(log_path);
2140 if (!log_di) {
2141 /* Doesn't exist in log tree, so delete it. */
2142 btrfs_release_path(path);
2143 di = btrfs_lookup_xattr(trans, root, path, ino,
2144 name, name_len, -1);
2145 kfree(name);
2146 if (IS_ERR(di)) {
2147 ret = PTR_ERR(di);
2148 goto out;
2149 }
2150 ASSERT(di);
2151 ret = btrfs_delete_one_dir_name(trans, root,
2152 path, di);
2153 if (ret)
2154 goto out;
2155 btrfs_release_path(path);
2156 search_key = key;
2157 goto again;
2158 }
2159 kfree(name);
2160 if (IS_ERR(log_di)) {
2161 ret = PTR_ERR(log_di);
2162 goto out;
2163 }
2164 cur += this_len;
2165 di = (struct btrfs_dir_item *)((char *)di + this_len);
2166 }
2167 }
2168 ret = btrfs_next_leaf(root, path);
2169 if (ret > 0)
2170 ret = 0;
2171 else if (ret == 0)
2172 goto process_leaf;
2173out:
2174 btrfs_free_path(log_path);
2175 btrfs_release_path(path);
2176 return ret;
2177}
2178
2179
Chris Masone02119d2008-09-05 16:13:11 -04002180/*
2181 * deletion replay happens before we copy any new directory items
2182 * out of the log or out of backreferences from inodes. It
2183 * scans the log to find ranges of keys that log is authoritative for,
2184 * and then scans the directory to find items in those ranges that are
2185 * not present in the log.
2186 *
2187 * Anything we don't find in the log is unlinked and removed from the
2188 * directory.
2189 */
2190static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2191 struct btrfs_root *root,
2192 struct btrfs_root *log,
2193 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002194 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002195{
2196 u64 range_start;
2197 u64 range_end;
2198 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2199 int ret = 0;
2200 struct btrfs_key dir_key;
2201 struct btrfs_key found_key;
2202 struct btrfs_path *log_path;
2203 struct inode *dir;
2204
2205 dir_key.objectid = dirid;
2206 dir_key.type = BTRFS_DIR_ITEM_KEY;
2207 log_path = btrfs_alloc_path();
2208 if (!log_path)
2209 return -ENOMEM;
2210
2211 dir = read_one_inode(root, dirid);
2212 /* it isn't an error if the inode isn't there, that can happen
2213 * because we replay the deletes before we copy in the inode item
2214 * from the log
2215 */
2216 if (!dir) {
2217 btrfs_free_path(log_path);
2218 return 0;
2219 }
2220again:
2221 range_start = 0;
2222 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002223 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002224 if (del_all)
2225 range_end = (u64)-1;
2226 else {
2227 ret = find_dir_range(log, path, dirid, key_type,
2228 &range_start, &range_end);
2229 if (ret != 0)
2230 break;
2231 }
Chris Masone02119d2008-09-05 16:13:11 -04002232
2233 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002234 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002235 int nritems;
2236 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2237 0, 0);
2238 if (ret < 0)
2239 goto out;
2240
2241 nritems = btrfs_header_nritems(path->nodes[0]);
2242 if (path->slots[0] >= nritems) {
2243 ret = btrfs_next_leaf(root, path);
Liu Bob3835752018-04-03 01:59:48 +08002244 if (ret == 1)
Chris Masone02119d2008-09-05 16:13:11 -04002245 break;
Liu Bob3835752018-04-03 01:59:48 +08002246 else if (ret < 0)
2247 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002248 }
2249 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2250 path->slots[0]);
2251 if (found_key.objectid != dirid ||
2252 found_key.type != dir_key.type)
2253 goto next_type;
2254
2255 if (found_key.offset > range_end)
2256 break;
2257
2258 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002259 log_path, dir,
2260 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002261 if (ret)
2262 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002263 if (found_key.offset == (u64)-1)
2264 break;
2265 dir_key.offset = found_key.offset + 1;
2266 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002267 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002268 if (range_end == (u64)-1)
2269 break;
2270 range_start = range_end + 1;
2271 }
2272
2273next_type:
2274 ret = 0;
2275 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2276 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2277 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002278 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002279 goto again;
2280 }
2281out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002282 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002283 btrfs_free_path(log_path);
2284 iput(dir);
2285 return ret;
2286}
2287
2288/*
2289 * the process_func used to replay items from the log tree. This
2290 * gets called in two different stages. The first stage just looks
2291 * for inodes and makes sure they are all copied into the subvolume.
2292 *
2293 * The second stage copies all the other item types from the log into
2294 * the subvolume. The two stage approach is slower, but gets rid of
2295 * lots of complexity around inodes referencing other inodes that exist
2296 * only in the log (references come from either directory items or inode
2297 * back refs).
2298 */
2299static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2300 struct walk_control *wc, u64 gen)
2301{
2302 int nritems;
2303 struct btrfs_path *path;
2304 struct btrfs_root *root = wc->replay_dest;
2305 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002306 int level;
2307 int i;
2308 int ret;
2309
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002310 ret = btrfs_read_buffer(eb, gen);
2311 if (ret)
2312 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002313
2314 level = btrfs_header_level(eb);
2315
2316 if (level != 0)
2317 return 0;
2318
2319 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002320 if (!path)
2321 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002322
2323 nritems = btrfs_header_nritems(eb);
2324 for (i = 0; i < nritems; i++) {
2325 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002326
2327 /* inode keys are done during the first stage */
2328 if (key.type == BTRFS_INODE_ITEM_KEY &&
2329 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002330 struct btrfs_inode_item *inode_item;
2331 u32 mode;
2332
2333 inode_item = btrfs_item_ptr(eb, i,
2334 struct btrfs_inode_item);
Filipe Manana4f764e52015-02-23 19:53:35 +00002335 ret = replay_xattr_deletes(wc->trans, root, log,
2336 path, key.objectid);
2337 if (ret)
2338 break;
Chris Masone02119d2008-09-05 16:13:11 -04002339 mode = btrfs_inode_mode(eb, inode_item);
2340 if (S_ISDIR(mode)) {
2341 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002342 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002343 if (ret)
2344 break;
Chris Masone02119d2008-09-05 16:13:11 -04002345 }
2346 ret = overwrite_item(wc->trans, root, path,
2347 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002348 if (ret)
2349 break;
Chris Masone02119d2008-09-05 16:13:11 -04002350
Yan, Zhengc71bf092009-11-12 09:34:40 +00002351 /* for regular files, make sure corresponding
Nicholas D Steeves01327612016-05-19 21:18:45 -04002352 * orphan item exist. extents past the new EOF
Yan, Zhengc71bf092009-11-12 09:34:40 +00002353 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002354 */
2355 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002356 ret = insert_orphan_item(wc->trans, root,
2357 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002358 if (ret)
2359 break;
Chris Masone02119d2008-09-05 16:13:11 -04002360 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002361
Chris Masone02119d2008-09-05 16:13:11 -04002362 ret = link_to_fixup_dir(wc->trans, root,
2363 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002364 if (ret)
2365 break;
Chris Masone02119d2008-09-05 16:13:11 -04002366 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002367
2368 if (key.type == BTRFS_DIR_INDEX_KEY &&
2369 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2370 ret = replay_one_dir_item(wc->trans, root, path,
2371 eb, i, &key);
2372 if (ret)
2373 break;
2374 }
2375
Chris Masone02119d2008-09-05 16:13:11 -04002376 if (wc->stage < LOG_WALK_REPLAY_ALL)
2377 continue;
2378
2379 /* these keys are simply copied */
2380 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2381 ret = overwrite_item(wc->trans, root, path,
2382 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002383 if (ret)
2384 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002385 } else if (key.type == BTRFS_INODE_REF_KEY ||
2386 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002387 ret = add_inode_ref(wc->trans, root, log, path,
2388 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002389 if (ret && ret != -ENOENT)
2390 break;
2391 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002392 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2393 ret = replay_one_extent(wc->trans, root, path,
2394 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002395 if (ret)
2396 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002397 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002398 ret = replay_one_dir_item(wc->trans, root, path,
2399 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002400 if (ret)
2401 break;
Chris Masone02119d2008-09-05 16:13:11 -04002402 }
2403 }
2404 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002405 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002406}
2407
Chris Masond3977122009-01-05 21:25:51 -05002408static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002409 struct btrfs_root *root,
2410 struct btrfs_path *path, int *level,
2411 struct walk_control *wc)
2412{
2413 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002414 u64 bytenr;
2415 u64 ptr_gen;
2416 struct extent_buffer *next;
2417 struct extent_buffer *cur;
2418 struct extent_buffer *parent;
2419 u32 blocksize;
2420 int ret = 0;
2421
2422 WARN_ON(*level < 0);
2423 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2424
Chris Masond3977122009-01-05 21:25:51 -05002425 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002426 WARN_ON(*level < 0);
2427 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2428 cur = path->nodes[*level];
2429
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302430 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002431
2432 if (path->slots[*level] >=
2433 btrfs_header_nritems(cur))
2434 break;
2435
2436 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2437 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
David Sterba707e8a02014-06-04 19:22:26 +02002438 blocksize = root->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002439
2440 parent = path->nodes[*level];
2441 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002442
David Sterbaa83fffb2014-06-15 02:39:54 +02002443 next = btrfs_find_create_tree_block(root, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002444 if (IS_ERR(next))
2445 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002446
Chris Masone02119d2008-09-05 16:13:11 -04002447 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002448 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002449 if (ret) {
2450 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002451 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002452 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002453
Chris Masone02119d2008-09-05 16:13:11 -04002454 path->slots[*level]++;
2455 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002456 ret = btrfs_read_buffer(next, ptr_gen);
2457 if (ret) {
2458 free_extent_buffer(next);
2459 return ret;
2460 }
Chris Masone02119d2008-09-05 16:13:11 -04002461
Josef Bacik681ae502013-10-07 15:11:00 -04002462 if (trans) {
2463 btrfs_tree_lock(next);
2464 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002465 clean_tree_block(trans, root->fs_info,
2466 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002467 btrfs_wait_tree_block_writeback(next);
2468 btrfs_tree_unlock(next);
Liu Bo0f4adc12018-01-25 11:02:51 -07002469 } else {
2470 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2471 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002472 }
Chris Masone02119d2008-09-05 16:13:11 -04002473
Chris Masone02119d2008-09-05 16:13:11 -04002474 WARN_ON(root_owner !=
2475 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002476 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002477 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002478 if (ret) {
2479 free_extent_buffer(next);
2480 return ret;
2481 }
Chris Masone02119d2008-09-05 16:13:11 -04002482 }
2483 free_extent_buffer(next);
2484 continue;
2485 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002486 ret = btrfs_read_buffer(next, ptr_gen);
2487 if (ret) {
2488 free_extent_buffer(next);
2489 return ret;
2490 }
Chris Masone02119d2008-09-05 16:13:11 -04002491
2492 WARN_ON(*level <= 0);
2493 if (path->nodes[*level-1])
2494 free_extent_buffer(path->nodes[*level-1]);
2495 path->nodes[*level-1] = next;
2496 *level = btrfs_header_level(next);
2497 path->slots[*level] = 0;
2498 cond_resched();
2499 }
2500 WARN_ON(*level < 0);
2501 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2502
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002503 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002504
2505 cond_resched();
2506 return 0;
2507}
2508
Chris Masond3977122009-01-05 21:25:51 -05002509static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002510 struct btrfs_root *root,
2511 struct btrfs_path *path, int *level,
2512 struct walk_control *wc)
2513{
2514 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002515 int i;
2516 int slot;
2517 int ret;
2518
Chris Masond3977122009-01-05 21:25:51 -05002519 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002520 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002521 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002522 path->slots[i]++;
2523 *level = i;
2524 WARN_ON(*level == 0);
2525 return 0;
2526 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002527 struct extent_buffer *parent;
2528 if (path->nodes[*level] == root->node)
2529 parent = path->nodes[*level];
2530 else
2531 parent = path->nodes[*level + 1];
2532
2533 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002534 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002535 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002536 if (ret)
2537 return ret;
2538
Chris Masone02119d2008-09-05 16:13:11 -04002539 if (wc->free) {
2540 struct extent_buffer *next;
2541
2542 next = path->nodes[*level];
2543
Josef Bacik681ae502013-10-07 15:11:00 -04002544 if (trans) {
2545 btrfs_tree_lock(next);
2546 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002547 clean_tree_block(trans, root->fs_info,
2548 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002549 btrfs_wait_tree_block_writeback(next);
2550 btrfs_tree_unlock(next);
Liu Bo0f4adc12018-01-25 11:02:51 -07002551 } else {
2552 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2553 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002554 }
Chris Masone02119d2008-09-05 16:13:11 -04002555
Chris Masone02119d2008-09-05 16:13:11 -04002556 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002557 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002558 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002559 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002560 if (ret)
2561 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002562 }
2563 free_extent_buffer(path->nodes[*level]);
2564 path->nodes[*level] = NULL;
2565 *level = i + 1;
2566 }
2567 }
2568 return 1;
2569}
2570
2571/*
2572 * drop the reference count on the tree rooted at 'snap'. This traverses
2573 * the tree freeing any blocks that have a ref count of zero after being
2574 * decremented.
2575 */
2576static int walk_log_tree(struct btrfs_trans_handle *trans,
2577 struct btrfs_root *log, struct walk_control *wc)
2578{
2579 int ret = 0;
2580 int wret;
2581 int level;
2582 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002583 int orig_level;
2584
2585 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002586 if (!path)
2587 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002588
2589 level = btrfs_header_level(log->node);
2590 orig_level = level;
2591 path->nodes[level] = log->node;
2592 extent_buffer_get(log->node);
2593 path->slots[level] = 0;
2594
Chris Masond3977122009-01-05 21:25:51 -05002595 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002596 wret = walk_down_log_tree(trans, log, path, &level, wc);
2597 if (wret > 0)
2598 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002599 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002600 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002601 goto out;
2602 }
Chris Masone02119d2008-09-05 16:13:11 -04002603
2604 wret = walk_up_log_tree(trans, log, path, &level, wc);
2605 if (wret > 0)
2606 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002607 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002608 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002609 goto out;
2610 }
Chris Masone02119d2008-09-05 16:13:11 -04002611 }
2612
2613 /* was the root node processed? if not, catch it here */
2614 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002615 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002616 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002617 if (ret)
2618 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002619 if (wc->free) {
2620 struct extent_buffer *next;
2621
2622 next = path->nodes[orig_level];
2623
Josef Bacik681ae502013-10-07 15:11:00 -04002624 if (trans) {
2625 btrfs_tree_lock(next);
2626 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002627 clean_tree_block(trans, log->fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002628 btrfs_wait_tree_block_writeback(next);
2629 btrfs_tree_unlock(next);
Liu Bo0f4adc12018-01-25 11:02:51 -07002630 } else {
2631 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2632 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002633 }
Chris Masone02119d2008-09-05 16:13:11 -04002634
Chris Masone02119d2008-09-05 16:13:11 -04002635 WARN_ON(log->root_key.objectid !=
2636 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002637 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002638 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002639 if (ret)
2640 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002641 }
2642 }
2643
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002644out:
Chris Masone02119d2008-09-05 16:13:11 -04002645 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002646 return ret;
2647}
2648
Yan Zheng7237f182009-01-21 12:54:03 -05002649/*
2650 * helper function to update the item for a given subvolumes log root
2651 * in the tree of log roots
2652 */
2653static int update_log_root(struct btrfs_trans_handle *trans,
2654 struct btrfs_root *log)
2655{
2656 int ret;
2657
2658 if (log->log_transid == 1) {
2659 /* insert root item on the first sync */
2660 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2661 &log->root_key, &log->root_item);
2662 } else {
2663 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2664 &log->root_key, &log->root_item);
2665 }
2666 return ret;
2667}
2668
Zhaolei60d53eb2015-08-17 18:44:46 +08002669static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002670{
2671 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002672 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002673
Yan Zheng7237f182009-01-21 12:54:03 -05002674 /*
2675 * we only allow two pending log transactions at a time,
2676 * so we know that if ours is more than 2 older than the
2677 * current transaction, we're done
2678 */
Chris Masone02119d2008-09-05 16:13:11 -04002679 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002680 prepare_to_wait(&root->log_commit_wait[index],
2681 &wait, TASK_UNINTERRUPTIBLE);
2682 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002683
Miao Xied1433de2014-02-20 18:08:59 +08002684 if (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002685 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002686 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002687
Yan Zheng7237f182009-01-21 12:54:03 -05002688 finish_wait(&root->log_commit_wait[index], &wait);
2689 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002690 } while (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002691 atomic_read(&root->log_commit[index]));
Yan Zheng7237f182009-01-21 12:54:03 -05002692}
2693
Zhaolei60d53eb2015-08-17 18:44:46 +08002694static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002695{
2696 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002697
2698 while (atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002699 prepare_to_wait(&root->log_writer_wait,
2700 &wait, TASK_UNINTERRUPTIBLE);
2701 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002702 if (atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002703 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002704 finish_wait(&root->log_writer_wait, &wait);
Filipe Manana575849e2015-02-11 11:12:39 +00002705 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002706 }
Chris Masone02119d2008-09-05 16:13:11 -04002707}
2708
Miao Xie8b050d32014-02-20 18:08:58 +08002709static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2710 struct btrfs_log_ctx *ctx)
2711{
2712 if (!ctx)
2713 return;
2714
2715 mutex_lock(&root->log_mutex);
2716 list_del_init(&ctx->list);
2717 mutex_unlock(&root->log_mutex);
2718}
2719
2720/*
2721 * Invoked in log mutex context, or be sure there is no other task which
2722 * can access the list.
2723 */
2724static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2725 int index, int error)
2726{
2727 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002728 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002729
Chris Mason570dd452016-10-27 10:42:20 -07002730 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2731 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002732 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002733 }
Miao Xie8b050d32014-02-20 18:08:58 +08002734
2735 INIT_LIST_HEAD(&root->log_ctxs[index]);
2736}
2737
Chris Masone02119d2008-09-05 16:13:11 -04002738/*
2739 * btrfs_sync_log does sends a given tree log down to the disk and
2740 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002741 * you know that any inodes previously logged are safely on disk only
2742 * if it returns 0.
2743 *
2744 * Any other return value means you need to call btrfs_commit_transaction.
2745 * Some of the edge cases for fsyncing directories that have had unlinks
2746 * or renames done in the past mean that sometimes the only safe
2747 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2748 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002749 */
2750int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002751 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002752{
Yan Zheng7237f182009-01-21 12:54:03 -05002753 int index1;
2754 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002755 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002756 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002757 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002758 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002759 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002760 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002761 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002762
Yan Zheng7237f182009-01-21 12:54:03 -05002763 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002764 log_transid = ctx->log_transid;
2765 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002766 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002767 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002768 }
Miao Xied1433de2014-02-20 18:08:59 +08002769
2770 index1 = log_transid % 2;
2771 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002772 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002773 mutex_unlock(&root->log_mutex);
2774 return ctx->log_ret;
2775 }
2776 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002777 atomic_set(&root->log_commit[index1], 1);
2778
2779 /* wait for previous tree log sync to complete */
2780 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002781 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002782
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002783 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002784 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002785 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney3cdde222016-06-09 21:38:35 -04002786 if (!btrfs_test_opt(root->fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08002787 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002788 mutex_unlock(&root->log_mutex);
2789 schedule_timeout_uninterruptible(1);
2790 mutex_lock(&root->log_mutex);
2791 }
Zhaolei60d53eb2015-08-17 18:44:46 +08002792 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002793 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002794 break;
2795 }
Chris Masond0c803c2008-09-11 16:17:57 -04002796
Chris Mason12fcfd22009-03-24 10:24:20 -04002797 /* bail out if we need to do a full commit */
Miao Xie995946d2014-04-02 19:51:06 +08002798 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002799 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002800 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002801 mutex_unlock(&root->log_mutex);
2802 goto out;
2803 }
2804
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002805 if (log_transid % 2 == 0)
2806 mark = EXTENT_DIRTY;
2807 else
2808 mark = EXTENT_NEW;
2809
Chris Mason690587d2009-10-13 13:29:19 -04002810 /* we start IO on all the marked extents here, but we don't actually
2811 * wait for them until later.
2812 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002813 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002814 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002815 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002816 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04002817 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002818 btrfs_free_logged_extents(log, log_transid);
Miao Xie995946d2014-04-02 19:51:06 +08002819 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002820 mutex_unlock(&root->log_mutex);
2821 goto out;
2822 }
Yan Zheng7237f182009-01-21 12:54:03 -05002823
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002824 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002825
Yan Zheng7237f182009-01-21 12:54:03 -05002826 root->log_transid++;
2827 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002828 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002829 /*
Filipe Mananabb780442019-05-15 16:03:17 +01002830 * Update or create log root item under the root's log_mutex to prevent
2831 * races with concurrent log syncs that can lead to failure to update
2832 * log root item because it was not created yet.
2833 */
2834 ret = update_log_root(trans, log);
2835 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002836 * IO has been started, blocks of the log tree have WRITTEN flag set
2837 * in their headers. new modifications of the log will be written to
2838 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002839 */
2840 mutex_unlock(&root->log_mutex);
2841
Filipe Manana28a23592016-08-23 21:13:51 +01002842 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08002843
Yan Zheng7237f182009-01-21 12:54:03 -05002844 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002845 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002846 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002847
2848 index2 = log_root_tree->log_transid % 2;
2849 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2850 root_log_ctx.log_transid = log_root_tree->log_transid;
2851
Yan Zheng7237f182009-01-21 12:54:03 -05002852 mutex_unlock(&log_root_tree->log_mutex);
2853
Yan Zheng7237f182009-01-21 12:54:03 -05002854 mutex_lock(&log_root_tree->log_mutex);
2855 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +01002856 /*
2857 * Implicit memory barrier after atomic_dec_and_test
2858 */
Yan Zheng7237f182009-01-21 12:54:03 -05002859 if (waitqueue_active(&log_root_tree->log_writer_wait))
2860 wake_up(&log_root_tree->log_writer_wait);
2861 }
2862
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002863 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002864 if (!list_empty(&root_log_ctx.list))
2865 list_del_init(&root_log_ctx.list);
2866
Miao Xiec6adc9c2013-05-28 10:05:39 +00002867 blk_finish_plug(&plug);
Miao Xie995946d2014-04-02 19:51:06 +08002868 btrfs_set_log_full_commit(root->fs_info, trans);
2869
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002870 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002871 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002872 mutex_unlock(&log_root_tree->log_mutex);
2873 goto out;
2874 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002875 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002876 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002877 mutex_unlock(&log_root_tree->log_mutex);
2878 ret = -EAGAIN;
2879 goto out;
2880 }
2881
Miao Xied1433de2014-02-20 18:08:59 +08002882 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08002883 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07002884 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08002885 mutex_unlock(&log_root_tree->log_mutex);
2886 ret = root_log_ctx.log_ret;
2887 goto out;
2888 }
Miao Xie8b050d32014-02-20 18:08:58 +08002889
Miao Xied1433de2014-02-20 18:08:59 +08002890 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05002891 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002892 blk_finish_plug(&plug);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002893 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages,
2894 mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05002895 btrfs_wait_logged_extents(trans, log, log_transid);
Zhaolei60d53eb2015-08-17 18:44:46 +08002896 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002897 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002898 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002899 if (!ret)
2900 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002901 goto out;
2902 }
Miao Xied1433de2014-02-20 18:08:59 +08002903 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002904 atomic_set(&log_root_tree->log_commit[index2], 1);
2905
Chris Mason12fcfd22009-03-24 10:24:20 -04002906 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002907 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002908 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002909 }
Yan Zheng7237f182009-01-21 12:54:03 -05002910
Zhaolei60d53eb2015-08-17 18:44:46 +08002911 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04002912
2913 /*
2914 * now that we've moved on to the tree of log tree roots,
2915 * check the full commit flag again
2916 */
Miao Xie995946d2014-04-02 19:51:06 +08002917 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002918 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002919 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002920 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002921 mutex_unlock(&log_root_tree->log_mutex);
2922 ret = -EAGAIN;
2923 goto out_wake_log_root;
2924 }
Yan Zheng7237f182009-01-21 12:54:03 -05002925
Miao Xiec6adc9c2013-05-28 10:05:39 +00002926 ret = btrfs_write_marked_extents(log_root_tree,
2927 &log_root_tree->dirty_log_pages,
2928 EXTENT_DIRTY | EXTENT_NEW);
2929 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002930 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002931 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002932 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002933 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002934 mutex_unlock(&log_root_tree->log_mutex);
2935 goto out_wake_log_root;
2936 }
Filipe Manana5ab5e442014-11-13 16:59:53 +00002937 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2938 if (!ret)
2939 ret = btrfs_wait_marked_extents(log_root_tree,
2940 &log_root_tree->dirty_log_pages,
2941 EXTENT_NEW | EXTENT_DIRTY);
2942 if (ret) {
2943 btrfs_set_log_full_commit(root->fs_info, trans);
2944 btrfs_free_logged_extents(log, log_transid);
2945 mutex_unlock(&log_root_tree->log_mutex);
2946 goto out_wake_log_root;
2947 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05002948 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002949
David Sterba6c417612011-04-13 15:41:04 +02002950 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002951 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002952 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002953 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002954
Yan Zheng7237f182009-01-21 12:54:03 -05002955 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002956 mutex_unlock(&log_root_tree->log_mutex);
2957
2958 /*
2959 * nobody else is going to jump in and write the the ctree
2960 * super here because the log_commit atomic below is protecting
2961 * us. We must be called with a transaction handle pinning
2962 * the running transaction open, so a full commit can't hop
2963 * in and cause problems either.
2964 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002965 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002966 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002967 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002968 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002969 goto out_wake_log_root;
2970 }
Yan Zheng7237f182009-01-21 12:54:03 -05002971
Chris Mason257c62e2009-10-13 13:21:08 -04002972 mutex_lock(&root->log_mutex);
2973 if (root->last_log_commit < log_transid)
2974 root->last_log_commit = log_transid;
2975 mutex_unlock(&root->log_mutex);
2976
Chris Mason12fcfd22009-03-24 10:24:20 -04002977out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07002978 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002979 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2980
Miao Xied1433de2014-02-20 18:08:59 +08002981 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002982 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002983 mutex_unlock(&log_root_tree->log_mutex);
2984
David Sterba33a9eca2015-10-10 18:35:10 +02002985 /*
David Sterba65cb4692018-04-24 14:53:56 +02002986 * The barrier before waitqueue_active is needed so all the updates
2987 * above are seen by the woken threads. It might not be necessary, but
2988 * proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02002989 */
David Sterba65cb4692018-04-24 14:53:56 +02002990 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002991 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2992 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002993out:
Miao Xied1433de2014-02-20 18:08:59 +08002994 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07002995 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08002996 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002997 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002998 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002999
David Sterba33a9eca2015-10-10 18:35:10 +02003000 /*
David Sterba65cb4692018-04-24 14:53:56 +02003001 * The barrier before waitqueue_active is needed so all the updates
3002 * above are seen by the woken threads. It might not be necessary, but
3003 * proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003004 */
David Sterba65cb4692018-04-24 14:53:56 +02003005 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05003006 if (waitqueue_active(&root->log_commit_wait[index1]))
3007 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003008 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003009}
3010
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003011static void free_log_tree(struct btrfs_trans_handle *trans,
3012 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003013{
3014 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04003015 u64 start;
3016 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04003017 struct walk_control wc = {
3018 .free = 1,
3019 .process_func = process_one_buffer
3020 };
3021
Josef Bacik681ae502013-10-07 15:11:00 -04003022 ret = walk_log_tree(trans, log, &wc);
Jeff Mahoney1bd71122018-09-06 16:59:33 -04003023 if (ret) {
3024 if (trans)
3025 btrfs_abort_transaction(trans, ret);
3026 else
3027 btrfs_handle_fs_error(log->fs_info, ret, NULL);
3028 }
Chris Masone02119d2008-09-05 16:13:11 -04003029
Chris Masond3977122009-01-05 21:25:51 -05003030 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003031 ret = find_first_extent_bit(&log->dirty_log_pages,
Liu Bobc0d4312018-01-25 11:02:52 -07003032 0, &start, &end,
3033 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
Josef Bacike6138872012-09-27 17:07:30 -04003034 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003035 if (ret)
3036 break;
3037
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003038 clear_extent_bits(&log->dirty_log_pages, start, end,
Liu Bobc0d4312018-01-25 11:02:52 -07003039 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Chris Masond0c803c2008-09-11 16:17:57 -04003040 }
3041
Josef Bacik2ab28f32012-10-12 15:27:49 -04003042 /*
3043 * We may have short-circuited the log tree with the full commit logic
3044 * and left ordered extents on our list, so clear these out to keep us
3045 * from leaking inodes and memory.
3046 */
3047 btrfs_free_logged_extents(log, 0);
3048 btrfs_free_logged_extents(log, 1);
3049
Yan Zheng7237f182009-01-21 12:54:03 -05003050 free_extent_buffer(log->node);
3051 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003052}
3053
3054/*
3055 * free all the extents used by the tree log. This should be called
3056 * at commit time of the full transaction
3057 */
3058int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3059{
3060 if (root->log_root) {
3061 free_log_tree(trans, root->log_root);
3062 root->log_root = NULL;
3063 }
3064 return 0;
3065}
3066
3067int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3068 struct btrfs_fs_info *fs_info)
3069{
3070 if (fs_info->log_root_tree) {
3071 free_log_tree(trans, fs_info->log_root_tree);
3072 fs_info->log_root_tree = NULL;
3073 }
Chris Masone02119d2008-09-05 16:13:11 -04003074 return 0;
3075}
3076
3077/*
Chris Masone02119d2008-09-05 16:13:11 -04003078 * If both a file and directory are logged, and unlinks or renames are
3079 * mixed in, we have a few interesting corners:
3080 *
3081 * create file X in dir Y
3082 * link file X to X.link in dir Y
3083 * fsync file X
3084 * unlink file X but leave X.link
3085 * fsync dir Y
3086 *
3087 * After a crash we would expect only X.link to exist. But file X
3088 * didn't get fsync'd again so the log has back refs for X and X.link.
3089 *
3090 * We solve this by removing directory entries and inode backrefs from the
3091 * log when a file that was logged in the current transaction is
3092 * unlinked. Any later fsync will include the updated log entries, and
3093 * we'll be able to reconstruct the proper directory items from backrefs.
3094 *
3095 * This optimizations allows us to avoid relogging the entire inode
3096 * or the entire directory.
3097 */
3098int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3099 struct btrfs_root *root,
3100 const char *name, int name_len,
3101 struct inode *dir, u64 index)
3102{
3103 struct btrfs_root *log;
3104 struct btrfs_dir_item *di;
3105 struct btrfs_path *path;
3106 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003107 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003108 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003109 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003110
Chris Mason3a5f1d42008-09-11 15:53:37 -04003111 if (BTRFS_I(dir)->logged_trans < trans->transid)
3112 return 0;
3113
Chris Masone02119d2008-09-05 16:13:11 -04003114 ret = join_running_log_trans(root);
3115 if (ret)
3116 return 0;
3117
3118 mutex_lock(&BTRFS_I(dir)->log_mutex);
3119
3120 log = root->log_root;
3121 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003122 if (!path) {
3123 err = -ENOMEM;
3124 goto out_unlock;
3125 }
liubo2a29edc2011-01-26 06:22:08 +00003126
Li Zefan33345d012011-04-20 10:31:50 +08003127 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003128 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003129 if (IS_ERR(di)) {
3130 err = PTR_ERR(di);
3131 goto fail;
3132 }
3133 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003134 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3135 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003136 if (ret) {
3137 err = ret;
3138 goto fail;
3139 }
Chris Masone02119d2008-09-05 16:13:11 -04003140 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003141 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003142 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003143 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003144 if (IS_ERR(di)) {
3145 err = PTR_ERR(di);
3146 goto fail;
3147 }
3148 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003149 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3150 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003151 if (ret) {
3152 err = ret;
3153 goto fail;
3154 }
Chris Masone02119d2008-09-05 16:13:11 -04003155 }
3156
3157 /* update the directory size in the log to reflect the names
3158 * we have removed
3159 */
3160 if (bytes_del) {
3161 struct btrfs_key key;
3162
Li Zefan33345d012011-04-20 10:31:50 +08003163 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003164 key.offset = 0;
3165 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003166 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003167
3168 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003169 if (ret < 0) {
3170 err = ret;
3171 goto fail;
3172 }
Chris Masone02119d2008-09-05 16:13:11 -04003173 if (ret == 0) {
3174 struct btrfs_inode_item *item;
3175 u64 i_size;
3176
3177 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3178 struct btrfs_inode_item);
3179 i_size = btrfs_inode_size(path->nodes[0], item);
3180 if (i_size > bytes_del)
3181 i_size -= bytes_del;
3182 else
3183 i_size = 0;
3184 btrfs_set_inode_size(path->nodes[0], item, i_size);
3185 btrfs_mark_buffer_dirty(path->nodes[0]);
3186 } else
3187 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003188 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003189 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003190fail:
Chris Masone02119d2008-09-05 16:13:11 -04003191 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003192out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04003193 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003194 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003195 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003196 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003197 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003198 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003199
Chris Mason12fcfd22009-03-24 10:24:20 -04003200 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003201
Andi Kleen411fc6b2010-10-29 15:14:31 -04003202 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003203}
3204
3205/* see comments for btrfs_del_dir_entries_in_log */
3206int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3207 struct btrfs_root *root,
3208 const char *name, int name_len,
3209 struct inode *inode, u64 dirid)
3210{
3211 struct btrfs_root *log;
3212 u64 index;
3213 int ret;
3214
Chris Mason3a5f1d42008-09-11 15:53:37 -04003215 if (BTRFS_I(inode)->logged_trans < trans->transid)
3216 return 0;
3217
Chris Masone02119d2008-09-05 16:13:11 -04003218 ret = join_running_log_trans(root);
3219 if (ret)
3220 return 0;
3221 log = root->log_root;
3222 mutex_lock(&BTRFS_I(inode)->log_mutex);
3223
Li Zefan33345d012011-04-20 10:31:50 +08003224 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003225 dirid, &index);
3226 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003227 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003228 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003229 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003230 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003231 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003232 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003233
Chris Masone02119d2008-09-05 16:13:11 -04003234 return ret;
3235}
3236
3237/*
3238 * creates a range item in the log for 'dirid'. first_offset and
3239 * last_offset tell us which parts of the key space the log should
3240 * be considered authoritative for.
3241 */
3242static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3243 struct btrfs_root *log,
3244 struct btrfs_path *path,
3245 int key_type, u64 dirid,
3246 u64 first_offset, u64 last_offset)
3247{
3248 int ret;
3249 struct btrfs_key key;
3250 struct btrfs_dir_log_item *item;
3251
3252 key.objectid = dirid;
3253 key.offset = first_offset;
3254 if (key_type == BTRFS_DIR_ITEM_KEY)
3255 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3256 else
3257 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3258 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003259 if (ret)
3260 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003261
3262 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3263 struct btrfs_dir_log_item);
3264 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3265 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003266 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003267 return 0;
3268}
3269
3270/*
3271 * log all the items included in the current transaction for a given
3272 * directory. This also creates the range items in the log tree required
3273 * to replay anything deleted before the fsync
3274 */
3275static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3276 struct btrfs_root *root, struct inode *inode,
3277 struct btrfs_path *path,
3278 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003279 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003280 u64 min_offset, u64 *last_offset_ret)
3281{
3282 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003283 struct btrfs_root *log = root->log_root;
3284 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003285 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003286 int ret;
3287 int i;
3288 int nritems;
3289 u64 first_offset = min_offset;
3290 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08003291 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003292
3293 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003294
Li Zefan33345d012011-04-20 10:31:50 +08003295 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003296 min_key.type = key_type;
3297 min_key.offset = min_offset;
3298
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003299 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003300
3301 /*
3302 * we didn't find anything from this transaction, see if there
3303 * is anything at all
3304 */
Li Zefan33345d012011-04-20 10:31:50 +08003305 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3306 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003307 min_key.type = key_type;
3308 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003309 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003310 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3311 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003312 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003313 return ret;
3314 }
Li Zefan33345d012011-04-20 10:31:50 +08003315 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003316
3317 /* if ret == 0 there are items for this type,
3318 * create a range to tell us the last key of this type.
3319 * otherwise, there are no items in this directory after
3320 * *min_offset, and we create a range to indicate that.
3321 */
3322 if (ret == 0) {
3323 struct btrfs_key tmp;
3324 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3325 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003326 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003327 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003328 }
3329 goto done;
3330 }
3331
3332 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003333 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003334 if (ret == 0) {
3335 struct btrfs_key tmp;
3336 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3337 if (key_type == tmp.type) {
3338 first_offset = tmp.offset;
3339 ret = overwrite_item(trans, log, dst_path,
3340 path->nodes[0], path->slots[0],
3341 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003342 if (ret) {
3343 err = ret;
3344 goto done;
3345 }
Chris Masone02119d2008-09-05 16:13:11 -04003346 }
3347 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003348 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003349
Josef Bacik44f705e2019-03-06 17:13:04 -05003350 /*
3351 * Find the first key from this transaction again. See the note for
3352 * log_new_dir_dentries, if we're logging a directory recursively we
3353 * won't be holding its i_mutex, which means we can modify the directory
3354 * while we're logging it. If we remove an entry between our first
3355 * search and this search we'll not find the key again and can just
3356 * bail.
3357 */
Chris Masone02119d2008-09-05 16:13:11 -04003358 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Josef Bacik44f705e2019-03-06 17:13:04 -05003359 if (ret != 0)
Chris Masone02119d2008-09-05 16:13:11 -04003360 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003361
3362 /*
3363 * we have a block from this transaction, log every item in it
3364 * from our directory
3365 */
Chris Masond3977122009-01-05 21:25:51 -05003366 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003367 struct btrfs_key tmp;
3368 src = path->nodes[0];
3369 nritems = btrfs_header_nritems(src);
3370 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003371 struct btrfs_dir_item *di;
3372
Chris Masone02119d2008-09-05 16:13:11 -04003373 btrfs_item_key_to_cpu(src, &min_key, i);
3374
Li Zefan33345d012011-04-20 10:31:50 +08003375 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003376 goto done;
3377 ret = overwrite_item(trans, log, dst_path, src, i,
3378 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003379 if (ret) {
3380 err = ret;
3381 goto done;
3382 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003383
3384 /*
3385 * We must make sure that when we log a directory entry,
3386 * the corresponding inode, after log replay, has a
3387 * matching link count. For example:
3388 *
3389 * touch foo
3390 * mkdir mydir
3391 * sync
3392 * ln foo mydir/bar
3393 * xfs_io -c "fsync" mydir
3394 * <crash>
3395 * <mount fs and log replay>
3396 *
3397 * Would result in a fsync log that when replayed, our
3398 * file inode would have a link count of 1, but we get
3399 * two directory entries pointing to the same inode.
3400 * After removing one of the names, it would not be
3401 * possible to remove the other name, which resulted
3402 * always in stale file handle errors, and would not
3403 * be possible to rmdir the parent directory, since
3404 * its i_size could never decrement to the value
3405 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3406 */
3407 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3408 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3409 if (ctx &&
3410 (btrfs_dir_transid(src, di) == trans->transid ||
3411 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3412 tmp.type != BTRFS_ROOT_ITEM_KEY)
3413 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003414 }
3415 path->slots[0] = nritems;
3416
3417 /*
3418 * look ahead to the next item and see if it is also
3419 * from this directory and from this transaction
3420 */
3421 ret = btrfs_next_leaf(root, path);
Liu Bo14c4d5f2018-04-03 01:59:47 +08003422 if (ret) {
3423 if (ret == 1)
3424 last_offset = (u64)-1;
3425 else
3426 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003427 goto done;
3428 }
3429 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003430 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003431 last_offset = (u64)-1;
3432 goto done;
3433 }
3434 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3435 ret = overwrite_item(trans, log, dst_path,
3436 path->nodes[0], path->slots[0],
3437 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003438 if (ret)
3439 err = ret;
3440 else
3441 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003442 goto done;
3443 }
3444 }
3445done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003446 btrfs_release_path(path);
3447 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003448
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003449 if (err == 0) {
3450 *last_offset_ret = last_offset;
3451 /*
3452 * insert the log range keys to indicate where the log
3453 * is valid
3454 */
3455 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003456 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003457 if (ret)
3458 err = ret;
3459 }
3460 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003461}
3462
3463/*
3464 * logging directories is very similar to logging inodes, We find all the items
3465 * from the current transaction and write them to the log.
3466 *
3467 * The recovery code scans the directory in the subvolume, and if it finds a
3468 * key in the range logged that is not present in the log tree, then it means
3469 * that dir entry was unlinked during the transaction.
3470 *
3471 * In order for that scan to work, we must include one key smaller than
3472 * the smallest logged by this transaction and one key larger than the largest
3473 * key logged by this transaction.
3474 */
3475static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3476 struct btrfs_root *root, struct inode *inode,
3477 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003478 struct btrfs_path *dst_path,
3479 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003480{
3481 u64 min_key;
3482 u64 max_key;
3483 int ret;
3484 int key_type = BTRFS_DIR_ITEM_KEY;
3485
3486again:
3487 min_key = 0;
3488 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003489 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003490 ret = log_dir_items(trans, root, inode, path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003491 dst_path, key_type, ctx, min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003492 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003493 if (ret)
3494 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003495 if (max_key == (u64)-1)
3496 break;
3497 min_key = max_key + 1;
3498 }
3499
3500 if (key_type == BTRFS_DIR_ITEM_KEY) {
3501 key_type = BTRFS_DIR_INDEX_KEY;
3502 goto again;
3503 }
3504 return 0;
3505}
3506
3507/*
3508 * a helper function to drop items from the log before we relog an
3509 * inode. max_key_type indicates the highest item type to remove.
3510 * This cannot be run for file data extents because it does not
3511 * free the extents they point to.
3512 */
3513static int drop_objectid_items(struct btrfs_trans_handle *trans,
3514 struct btrfs_root *log,
3515 struct btrfs_path *path,
3516 u64 objectid, int max_key_type)
3517{
3518 int ret;
3519 struct btrfs_key key;
3520 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003521 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003522
3523 key.objectid = objectid;
3524 key.type = max_key_type;
3525 key.offset = (u64)-1;
3526
Chris Masond3977122009-01-05 21:25:51 -05003527 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003528 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003529 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003530 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003531 break;
3532
3533 if (path->slots[0] == 0)
3534 break;
3535
3536 path->slots[0]--;
3537 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3538 path->slots[0]);
3539
3540 if (found_key.objectid != objectid)
3541 break;
3542
Josef Bacik18ec90d2012-09-28 11:56:28 -04003543 found_key.offset = 0;
3544 found_key.type = 0;
3545 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3546 &start_slot);
3547
3548 ret = btrfs_del_items(trans, log, path, start_slot,
3549 path->slots[0] - start_slot + 1);
3550 /*
3551 * If start slot isn't 0 then we don't need to re-search, we've
3552 * found the last guy with the objectid in this tree.
3553 */
3554 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003555 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003556 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003557 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003558 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003559 if (ret > 0)
3560 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003561 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003562}
3563
Josef Bacik94edf4a2012-09-25 14:56:25 -04003564static void fill_inode_item(struct btrfs_trans_handle *trans,
3565 struct extent_buffer *leaf,
3566 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003567 struct inode *inode, int log_inode_only,
3568 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003569{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003570 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003571
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003572 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003573
3574 if (log_inode_only) {
3575 /* set the generation to zero so the recover code
3576 * can tell the difference between an logging
3577 * just to say 'this inode exists' and a logging
3578 * to say 'update this inode with these values'
3579 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003580 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003581 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003582 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003583 btrfs_set_token_inode_generation(leaf, item,
3584 BTRFS_I(inode)->generation,
3585 &token);
3586 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003587 }
3588
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003589 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3590 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3591 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3592 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3593
David Sterbaa937b972014-12-12 17:39:12 +01003594 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003595 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003596 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003597 inode->i_atime.tv_nsec, &token);
3598
David Sterbaa937b972014-12-12 17:39:12 +01003599 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003600 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003601 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003602 inode->i_mtime.tv_nsec, &token);
3603
David Sterbaa937b972014-12-12 17:39:12 +01003604 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003605 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003606 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003607 inode->i_ctime.tv_nsec, &token);
3608
3609 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3610 &token);
3611
3612 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3613 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3614 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3615 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3616 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003617}
3618
Josef Bacika95249b2012-10-11 16:17:34 -04003619static int log_inode_item(struct btrfs_trans_handle *trans,
3620 struct btrfs_root *log, struct btrfs_path *path,
3621 struct inode *inode)
3622{
3623 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003624 int ret;
3625
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003626 ret = btrfs_insert_empty_item(trans, log, path,
3627 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003628 sizeof(*inode_item));
3629 if (ret && ret != -EEXIST)
3630 return ret;
3631 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3632 struct btrfs_inode_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003633 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003634 btrfs_release_path(path);
3635 return 0;
3636}
3637
Chris Mason31ff1cd2008-09-11 16:17:57 -04003638static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003639 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003640 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003641 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003642 int start_slot, int nr, int inode_only,
3643 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003644{
3645 unsigned long src_offset;
3646 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003647 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003648 struct btrfs_file_extent_item *extent;
3649 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003650 struct extent_buffer *src = src_path->nodes[0];
3651 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003652 int ret;
3653 struct btrfs_key *ins_keys;
3654 u32 *ins_sizes;
3655 char *ins_data;
3656 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003657 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003658 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003659 bool has_extents = false;
Filipe Manana74121f72014-08-07 12:00:44 +01003660 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003661 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003662
3663 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003664
3665 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3666 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003667 if (!ins_data)
3668 return -ENOMEM;
3669
Josef Bacik16e75492013-10-22 12:18:51 -04003670 first_key.objectid = (u64)-1;
3671
Chris Mason31ff1cd2008-09-11 16:17:57 -04003672 ins_sizes = (u32 *)ins_data;
3673 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3674
3675 for (i = 0; i < nr; i++) {
3676 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3677 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3678 }
3679 ret = btrfs_insert_empty_items(trans, log, dst_path,
3680 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003681 if (ret) {
3682 kfree(ins_data);
3683 return ret;
3684 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003685
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003686 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003687 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3688 dst_path->slots[0]);
3689
3690 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3691
Matthias Kaehlckee7f42b02017-07-27 14:30:23 -07003692 if (i == nr - 1)
Josef Bacik16e75492013-10-22 12:18:51 -04003693 last_key = ins_keys[i];
3694
Josef Bacik94edf4a2012-09-25 14:56:25 -04003695 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003696 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3697 dst_path->slots[0],
3698 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003699 fill_inode_item(trans, dst_path->nodes[0], inode_item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003700 inode, inode_only == LOG_INODE_EXISTS,
3701 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003702 } else {
3703 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3704 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003705 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003706
Josef Bacik16e75492013-10-22 12:18:51 -04003707 /*
3708 * We set need_find_last_extent here in case we know we were
3709 * processing other items and then walk into the first extent in
3710 * the inode. If we don't hit an extent then nothing changes,
3711 * we'll do the last search the next time around.
3712 */
3713 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3714 has_extents = true;
Filipe Manana74121f72014-08-07 12:00:44 +01003715 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003716 first_key = ins_keys[i];
3717 } else {
3718 need_find_last_extent = false;
3719 }
3720
Chris Mason31ff1cd2008-09-11 16:17:57 -04003721 /* take a reference on file data extents so that truncates
3722 * or deletes of this inode don't have to relog the inode
3723 * again
3724 */
David Sterba962a2982014-06-04 18:41:45 +02003725 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003726 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003727 int found_type;
3728 extent = btrfs_item_ptr(src, start_slot + i,
3729 struct btrfs_file_extent_item);
3730
liubo8e531cd2011-05-06 10:36:09 +08003731 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3732 continue;
3733
Chris Mason31ff1cd2008-09-11 16:17:57 -04003734 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003735 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003736 u64 ds, dl, cs, cl;
3737 ds = btrfs_file_extent_disk_bytenr(src,
3738 extent);
3739 /* ds == 0 is a hole */
3740 if (ds == 0)
3741 continue;
3742
3743 dl = btrfs_file_extent_disk_num_bytes(src,
3744 extent);
3745 cs = btrfs_file_extent_offset(src, extent);
3746 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003747 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003748 if (btrfs_file_extent_compression(src,
3749 extent)) {
3750 cs = 0;
3751 cl = dl;
3752 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003753
3754 ret = btrfs_lookup_csums_range(
3755 log->fs_info->csum_root,
3756 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003757 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003758 if (ret) {
3759 btrfs_release_path(dst_path);
3760 kfree(ins_data);
3761 return ret;
3762 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003763 }
3764 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003765 }
3766
3767 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003768 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003769 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003770
3771 /*
3772 * we have to do this after the loop above to avoid changing the
3773 * log tree while trying to change the log tree.
3774 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003775 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003776 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003777 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3778 struct btrfs_ordered_sum,
3779 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003780 if (!ret)
3781 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003782 list_del(&sums->list);
3783 kfree(sums);
3784 }
Josef Bacik16e75492013-10-22 12:18:51 -04003785
3786 if (!has_extents)
3787 return ret;
3788
Filipe Manana74121f72014-08-07 12:00:44 +01003789 if (need_find_last_extent && *last_extent == first_key.offset) {
3790 /*
3791 * We don't have any leafs between our current one and the one
3792 * we processed before that can have file extent items for our
3793 * inode (and have a generation number smaller than our current
3794 * transaction id).
3795 */
3796 need_find_last_extent = false;
3797 }
3798
Josef Bacik16e75492013-10-22 12:18:51 -04003799 /*
3800 * Because we use btrfs_search_forward we could skip leaves that were
3801 * not modified and then assume *last_extent is valid when it really
3802 * isn't. So back up to the previous leaf and read the end of the last
3803 * extent before we go and fill in holes.
3804 */
3805 if (need_find_last_extent) {
3806 u64 len;
3807
3808 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3809 if (ret < 0)
3810 return ret;
3811 if (ret)
3812 goto fill_holes;
3813 if (src_path->slots[0])
3814 src_path->slots[0]--;
3815 src = src_path->nodes[0];
3816 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3817 if (key.objectid != btrfs_ino(inode) ||
3818 key.type != BTRFS_EXTENT_DATA_KEY)
3819 goto fill_holes;
3820 extent = btrfs_item_ptr(src, src_path->slots[0],
3821 struct btrfs_file_extent_item);
3822 if (btrfs_file_extent_type(src, extent) ==
3823 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003824 len = btrfs_file_extent_inline_len(src,
3825 src_path->slots[0],
3826 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003827 *last_extent = ALIGN(key.offset + len,
3828 log->sectorsize);
3829 } else {
3830 len = btrfs_file_extent_num_bytes(src, extent);
3831 *last_extent = key.offset + len;
3832 }
3833 }
3834fill_holes:
3835 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3836 * things could have happened
3837 *
3838 * 1) A merge could have happened, so we could currently be on a leaf
3839 * that holds what we were copying in the first place.
3840 * 2) A split could have happened, and now not all of the items we want
3841 * are on the same leaf.
3842 *
3843 * So we need to adjust how we search for holes, we need to drop the
3844 * path and re-search for the first extent key we found, and then walk
3845 * forward until we hit the last one we copied.
3846 */
3847 if (need_find_last_extent) {
3848 /* btrfs_prev_leaf could return 1 without releasing the path */
3849 btrfs_release_path(src_path);
3850 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3851 src_path, 0, 0);
3852 if (ret < 0)
3853 return ret;
3854 ASSERT(ret == 0);
3855 src = src_path->nodes[0];
3856 i = src_path->slots[0];
3857 } else {
3858 i = start_slot;
3859 }
3860
3861 /*
3862 * Ok so here we need to go through and fill in any holes we may have
3863 * to make sure that holes are punched for those areas in case they had
3864 * extents previously.
3865 */
3866 while (!done) {
3867 u64 offset, len;
3868 u64 extent_end;
3869
3870 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3871 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3872 if (ret < 0)
3873 return ret;
3874 ASSERT(ret == 0);
3875 src = src_path->nodes[0];
3876 i = 0;
Filipe Mananabee3c022018-03-26 23:59:12 +01003877 need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003878 }
3879
3880 btrfs_item_key_to_cpu(src, &key, i);
3881 if (!btrfs_comp_cpu_keys(&key, &last_key))
3882 done = true;
3883 if (key.objectid != btrfs_ino(inode) ||
3884 key.type != BTRFS_EXTENT_DATA_KEY) {
3885 i++;
3886 continue;
3887 }
3888 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3889 if (btrfs_file_extent_type(src, extent) ==
3890 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003891 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003892 extent_end = ALIGN(key.offset + len, log->sectorsize);
3893 } else {
3894 len = btrfs_file_extent_num_bytes(src, extent);
3895 extent_end = key.offset + len;
3896 }
3897 i++;
3898
3899 if (*last_extent == key.offset) {
3900 *last_extent = extent_end;
3901 continue;
3902 }
3903 offset = *last_extent;
3904 len = key.offset - *last_extent;
3905 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3906 offset, 0, 0, len, 0, len, 0,
3907 0, 0);
3908 if (ret)
3909 break;
Filipe Manana74121f72014-08-07 12:00:44 +01003910 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003911 }
3912 /*
3913 * Need to let the callers know we dropped the path so they should
3914 * re-search.
3915 */
3916 if (!ret && need_find_last_extent)
3917 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003918 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003919}
3920
Josef Bacik5dc562c2012-08-17 13:14:17 -04003921static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3922{
3923 struct extent_map *em1, *em2;
3924
3925 em1 = list_entry(a, struct extent_map, list);
3926 em2 = list_entry(b, struct extent_map, list);
3927
3928 if (em1->start < em2->start)
3929 return -1;
3930 else if (em1->start > em2->start)
3931 return 1;
3932 return 0;
3933}
3934
Filipe Manana8407f552014-09-05 15:14:39 +01003935static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3936 struct inode *inode,
3937 struct btrfs_root *root,
3938 const struct extent_map *em,
3939 const struct list_head *logged_list,
3940 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003941{
Josef Bacik2ab28f32012-10-12 15:27:49 -04003942 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01003943 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003944 u64 mod_start = em->mod_start;
3945 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003946 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003947 u64 csum_offset;
3948 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003949 LIST_HEAD(ordered_sums);
3950 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003951
Filipe Manana8407f552014-09-05 15:14:39 +01003952 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04003953
Filipe Manana8407f552014-09-05 15:14:39 +01003954 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3955 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003956 return 0;
3957
Josef Bacik2ab28f32012-10-12 15:27:49 -04003958 /*
Filipe Manana8407f552014-09-05 15:14:39 +01003959 * Wait far any ordered extent that covers our extent map. If it
3960 * finishes without an error, first check and see if our csums are on
3961 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04003962 */
Miao Xie827463c2014-01-14 20:31:51 +08003963 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003964 struct btrfs_ordered_sum *sum;
3965
3966 if (!mod_len)
3967 break;
3968
Josef Bacik2ab28f32012-10-12 15:27:49 -04003969 if (ordered->file_offset + ordered->len <= mod_start ||
3970 mod_start + mod_len <= ordered->file_offset)
3971 continue;
3972
Filipe Manana8407f552014-09-05 15:14:39 +01003973 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3974 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3975 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3976 const u64 start = ordered->file_offset;
3977 const u64 end = ordered->file_offset + ordered->len - 1;
3978
3979 WARN_ON(ordered->inode != inode);
3980 filemap_fdatawrite_range(inode->i_mapping, start, end);
3981 }
3982
3983 wait_event(ordered->wait,
3984 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3985 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3986
3987 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00003988 /*
3989 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3990 * i_mapping flags, so that the next fsync won't get
3991 * an outdated io error too.
3992 */
Miklos Szeredif0312212016-09-16 12:44:21 +02003993 filemap_check_errors(inode->i_mapping);
Filipe Manana8407f552014-09-05 15:14:39 +01003994 *ordered_io_error = true;
3995 break;
3996 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003997 /*
3998 * We are going to copy all the csums on this ordered extent, so
3999 * go ahead and adjust mod_start and mod_len in case this
4000 * ordered extent has already been logged.
4001 */
4002 if (ordered->file_offset > mod_start) {
4003 if (ordered->file_offset + ordered->len >=
4004 mod_start + mod_len)
4005 mod_len = ordered->file_offset - mod_start;
4006 /*
4007 * If we have this case
4008 *
4009 * |--------- logged extent ---------|
4010 * |----- ordered extent ----|
4011 *
4012 * Just don't mess with mod_start and mod_len, we'll
4013 * just end up logging more csums than we need and it
4014 * will be ok.
4015 */
4016 } else {
4017 if (ordered->file_offset + ordered->len <
4018 mod_start + mod_len) {
4019 mod_len = (mod_start + mod_len) -
4020 (ordered->file_offset + ordered->len);
4021 mod_start = ordered->file_offset +
4022 ordered->len;
4023 } else {
4024 mod_len = 0;
4025 }
4026 }
4027
Filipe Manana8407f552014-09-05 15:14:39 +01004028 if (skip_csum)
4029 continue;
4030
Josef Bacik2ab28f32012-10-12 15:27:49 -04004031 /*
4032 * To keep us from looping for the above case of an ordered
4033 * extent that falls inside of the logged extent.
4034 */
4035 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4036 &ordered->flags))
4037 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004038
Josef Bacik2ab28f32012-10-12 15:27:49 -04004039 list_for_each_entry(sum, &ordered->list, list) {
4040 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08004041 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01004042 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004043 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004044 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004045
Filipe Manana8407f552014-09-05 15:14:39 +01004046 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04004047 return ret;
4048
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004049 if (em->compress_type) {
4050 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004051 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004052 } else {
4053 csum_offset = mod_start - em->start;
4054 csum_len = mod_len;
4055 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004056
Josef Bacik70c8a912012-10-11 16:54:30 -04004057 /* block start is already adjusted for the file extent offset. */
4058 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
4059 em->block_start + csum_offset,
4060 em->block_start + csum_offset +
4061 csum_len - 1, &ordered_sums, 0);
4062 if (ret)
4063 return ret;
4064
4065 while (!list_empty(&ordered_sums)) {
4066 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4067 struct btrfs_ordered_sum,
4068 list);
4069 if (!ret)
4070 ret = btrfs_csum_file_blocks(trans, log, sums);
4071 list_del(&sums->list);
4072 kfree(sums);
4073 }
4074
4075 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004076}
4077
Filipe Manana8407f552014-09-05 15:14:39 +01004078static int log_one_extent(struct btrfs_trans_handle *trans,
4079 struct inode *inode, struct btrfs_root *root,
4080 const struct extent_map *em,
4081 struct btrfs_path *path,
4082 const struct list_head *logged_list,
4083 struct btrfs_log_ctx *ctx)
4084{
4085 struct btrfs_root *log = root->log_root;
4086 struct btrfs_file_extent_item *fi;
4087 struct extent_buffer *leaf;
4088 struct btrfs_map_token token;
4089 struct btrfs_key key;
4090 u64 extent_offset = em->start - em->orig_start;
4091 u64 block_len;
4092 int ret;
4093 int extent_inserted = 0;
4094 bool ordered_io_err = false;
4095
4096 ret = wait_ordered_extents(trans, inode, root, em, logged_list,
4097 &ordered_io_err);
4098 if (ret)
4099 return ret;
4100
4101 if (ordered_io_err) {
4102 ctx->io_err = -EIO;
4103 return 0;
4104 }
4105
4106 btrfs_init_map_token(&token);
4107
4108 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
4109 em->start + em->len, NULL, 0, 1,
4110 sizeof(*fi), &extent_inserted);
4111 if (ret)
4112 return ret;
4113
4114 if (!extent_inserted) {
4115 key.objectid = btrfs_ino(inode);
4116 key.type = BTRFS_EXTENT_DATA_KEY;
4117 key.offset = em->start;
4118
4119 ret = btrfs_insert_empty_item(trans, log, path, &key,
4120 sizeof(*fi));
4121 if (ret)
4122 return ret;
4123 }
4124 leaf = path->nodes[0];
4125 fi = btrfs_item_ptr(leaf, path->slots[0],
4126 struct btrfs_file_extent_item);
4127
Josef Bacik50d9aa92014-11-21 14:52:38 -05004128 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004129 &token);
4130 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4131 btrfs_set_token_file_extent_type(leaf, fi,
4132 BTRFS_FILE_EXTENT_PREALLOC,
4133 &token);
4134 else
4135 btrfs_set_token_file_extent_type(leaf, fi,
4136 BTRFS_FILE_EXTENT_REG,
4137 &token);
4138
4139 block_len = max(em->block_len, em->orig_block_len);
4140 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4141 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4142 em->block_start,
4143 &token);
4144 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4145 &token);
4146 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4147 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4148 em->block_start -
4149 extent_offset, &token);
4150 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4151 &token);
4152 } else {
4153 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4154 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4155 &token);
4156 }
4157
4158 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4159 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4160 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4161 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4162 &token);
4163 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4164 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4165 btrfs_mark_buffer_dirty(leaf);
4166
4167 btrfs_release_path(path);
4168
4169 return ret;
4170}
4171
Josef Bacik5dc562c2012-08-17 13:14:17 -04004172static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4173 struct btrfs_root *root,
4174 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004175 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004176 struct list_head *logged_list,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004177 struct btrfs_log_ctx *ctx,
4178 const u64 start,
4179 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004180{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004181 struct extent_map *em, *n;
4182 struct list_head extents;
4183 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4184 u64 test_gen;
4185 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004186 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004187
4188 INIT_LIST_HEAD(&extents);
4189
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004190 down_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004191 write_lock(&tree->lock);
4192 test_gen = root->fs_info->last_trans_committed;
4193
4194 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4195 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004196
4197 /*
4198 * Just an arbitrary number, this can be really CPU intensive
4199 * once we start getting a lot of extents, and really once we
4200 * have a bunch of extents we just want to commit since it will
4201 * be faster.
4202 */
4203 if (++num > 32768) {
4204 list_del_init(&tree->modified_extents);
4205 ret = -EFBIG;
4206 goto process;
4207 }
4208
Josef Bacik5dc562c2012-08-17 13:14:17 -04004209 if (em->generation <= test_gen)
4210 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004211 /* Need a ref to keep it from getting evicted from cache */
4212 atomic_inc(&em->refs);
4213 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004214 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004215 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004216 }
4217
4218 list_sort(NULL, &extents, extent_cmp);
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004219 btrfs_get_logged_extents(inode, logged_list, start, end);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004220 /*
4221 * Some ordered extents started by fsync might have completed
4222 * before we could collect them into the list logged_list, which
4223 * means they're gone, not in our logged_list nor in the inode's
4224 * ordered tree. We want the application/user space to know an
4225 * error happened while attempting to persist file data so that
4226 * it can take proper action. If such error happened, we leave
4227 * without writing to the log tree and the fsync must report the
4228 * file data write error and not commit the current transaction.
4229 */
Miklos Szeredif0312212016-09-16 12:44:21 +02004230 ret = filemap_check_errors(inode->i_mapping);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004231 if (ret)
4232 ctx->io_err = ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004233process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004234 while (!list_empty(&extents)) {
4235 em = list_entry(extents.next, struct extent_map, list);
4236
4237 list_del_init(&em->list);
4238
4239 /*
4240 * If we had an error we just need to delete everybody from our
4241 * private list.
4242 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004243 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004244 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004245 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004246 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004247 }
4248
4249 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004250
Filipe Manana8407f552014-09-05 15:14:39 +01004251 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4252 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004253 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004254 clear_em_logging(tree, em);
4255 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004256 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004257 WARN_ON(!list_empty(&extents));
4258 write_unlock(&tree->lock);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004259 up_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004260
Josef Bacik5dc562c2012-08-17 13:14:17 -04004261 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004262 return ret;
4263}
4264
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004265static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
4266 struct btrfs_path *path, u64 *size_ret)
4267{
4268 struct btrfs_key key;
4269 int ret;
4270
4271 key.objectid = btrfs_ino(inode);
4272 key.type = BTRFS_INODE_ITEM_KEY;
4273 key.offset = 0;
4274
4275 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4276 if (ret < 0) {
4277 return ret;
4278 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004279 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004280 } else {
4281 struct btrfs_inode_item *item;
4282
4283 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4284 struct btrfs_inode_item);
4285 *size_ret = btrfs_inode_size(path->nodes[0], item);
4286 }
4287
4288 btrfs_release_path(path);
4289 return 0;
4290}
4291
Filipe Manana36283bf2015-06-20 00:44:51 +01004292/*
4293 * At the moment we always log all xattrs. This is to figure out at log replay
4294 * time which xattrs must have their deletion replayed. If a xattr is missing
4295 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4296 * because if a xattr is deleted, the inode is fsynced and a power failure
4297 * happens, causing the log to be replayed the next time the fs is mounted,
4298 * we want the xattr to not exist anymore (same behaviour as other filesystems
4299 * with a journal, ext3/4, xfs, f2fs, etc).
4300 */
4301static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4302 struct btrfs_root *root,
4303 struct inode *inode,
4304 struct btrfs_path *path,
4305 struct btrfs_path *dst_path)
4306{
4307 int ret;
4308 struct btrfs_key key;
4309 const u64 ino = btrfs_ino(inode);
4310 int ins_nr = 0;
4311 int start_slot = 0;
4312
4313 key.objectid = ino;
4314 key.type = BTRFS_XATTR_ITEM_KEY;
4315 key.offset = 0;
4316
4317 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4318 if (ret < 0)
4319 return ret;
4320
4321 while (true) {
4322 int slot = path->slots[0];
4323 struct extent_buffer *leaf = path->nodes[0];
4324 int nritems = btrfs_header_nritems(leaf);
4325
4326 if (slot >= nritems) {
4327 if (ins_nr > 0) {
4328 u64 last_extent = 0;
4329
4330 ret = copy_items(trans, inode, dst_path, path,
4331 &last_extent, start_slot,
4332 ins_nr, 1, 0);
4333 /* can't be 1, extent items aren't processed */
4334 ASSERT(ret <= 0);
4335 if (ret < 0)
4336 return ret;
4337 ins_nr = 0;
4338 }
4339 ret = btrfs_next_leaf(root, path);
4340 if (ret < 0)
4341 return ret;
4342 else if (ret > 0)
4343 break;
4344 continue;
4345 }
4346
4347 btrfs_item_key_to_cpu(leaf, &key, slot);
4348 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4349 break;
4350
4351 if (ins_nr == 0)
4352 start_slot = slot;
4353 ins_nr++;
4354 path->slots[0]++;
4355 cond_resched();
4356 }
4357 if (ins_nr > 0) {
4358 u64 last_extent = 0;
4359
4360 ret = copy_items(trans, inode, dst_path, path,
4361 &last_extent, start_slot,
4362 ins_nr, 1, 0);
4363 /* can't be 1, extent items aren't processed */
4364 ASSERT(ret <= 0);
4365 if (ret < 0)
4366 return ret;
4367 }
4368
4369 return 0;
4370}
4371
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004372/*
4373 * If the no holes feature is enabled we need to make sure any hole between the
4374 * last extent and the i_size of our inode is explicitly marked in the log. This
4375 * is to make sure that doing something like:
4376 *
4377 * 1) create file with 128Kb of data
4378 * 2) truncate file to 64Kb
4379 * 3) truncate file to 256Kb
4380 * 4) fsync file
4381 * 5) <crash/power failure>
4382 * 6) mount fs and trigger log replay
4383 *
4384 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4385 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4386 * file correspond to a hole. The presence of explicit holes in a log tree is
4387 * what guarantees that log replay will remove/adjust file extent items in the
4388 * fs/subvol tree.
4389 *
4390 * Here we do not need to care about holes between extents, that is already done
4391 * by copy_items(). We also only need to do this in the full sync path, where we
4392 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4393 * lookup the list of modified extent maps and if any represents a hole, we
4394 * insert a corresponding extent representing a hole in the log tree.
4395 */
4396static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4397 struct btrfs_root *root,
4398 struct inode *inode,
4399 struct btrfs_path *path)
4400{
4401 int ret;
4402 struct btrfs_key key;
4403 u64 hole_start;
4404 u64 hole_size;
4405 struct extent_buffer *leaf;
4406 struct btrfs_root *log = root->log_root;
4407 const u64 ino = btrfs_ino(inode);
4408 const u64 i_size = i_size_read(inode);
4409
4410 if (!btrfs_fs_incompat(root->fs_info, NO_HOLES))
4411 return 0;
4412
4413 key.objectid = ino;
4414 key.type = BTRFS_EXTENT_DATA_KEY;
4415 key.offset = (u64)-1;
4416
4417 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4418 ASSERT(ret != 0);
4419 if (ret < 0)
4420 return ret;
4421
4422 ASSERT(path->slots[0] > 0);
4423 path->slots[0]--;
4424 leaf = path->nodes[0];
4425 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4426
4427 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4428 /* inode does not have any extents */
4429 hole_start = 0;
4430 hole_size = i_size;
4431 } else {
4432 struct btrfs_file_extent_item *extent;
4433 u64 len;
4434
4435 /*
4436 * If there's an extent beyond i_size, an explicit hole was
4437 * already inserted by copy_items().
4438 */
4439 if (key.offset >= i_size)
4440 return 0;
4441
4442 extent = btrfs_item_ptr(leaf, path->slots[0],
4443 struct btrfs_file_extent_item);
4444
4445 if (btrfs_file_extent_type(leaf, extent) ==
Filipe Manana131d3ff2019-03-19 17:18:13 +00004446 BTRFS_FILE_EXTENT_INLINE)
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004447 return 0;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004448
4449 len = btrfs_file_extent_num_bytes(leaf, extent);
4450 /* Last extent goes beyond i_size, no need to log a hole. */
4451 if (key.offset + len > i_size)
4452 return 0;
4453 hole_start = key.offset + len;
4454 hole_size = i_size - hole_start;
4455 }
4456 btrfs_release_path(path);
4457
4458 /* Last extent ends at i_size. */
4459 if (hole_size == 0)
4460 return 0;
4461
4462 hole_size = ALIGN(hole_size, root->sectorsize);
4463 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4464 hole_size, 0, hole_size, 0, 0, 0);
4465 return ret;
4466}
4467
Filipe Manana56f23fd2016-03-30 23:37:21 +01004468/*
4469 * When we are logging a new inode X, check if it doesn't have a reference that
4470 * matches the reference from some other inode Y created in a past transaction
4471 * and that was renamed in the current transaction. If we don't do this, then at
4472 * log replay time we can lose inode Y (and all its files if it's a directory):
4473 *
4474 * mkdir /mnt/x
4475 * echo "hello world" > /mnt/x/foobar
4476 * sync
4477 * mv /mnt/x /mnt/y
4478 * mkdir /mnt/x # or touch /mnt/x
4479 * xfs_io -c fsync /mnt/x
4480 * <power fail>
4481 * mount fs, trigger log replay
4482 *
4483 * After the log replay procedure, we would lose the first directory and all its
4484 * files (file foobar).
4485 * For the case where inode Y is not a directory we simply end up losing it:
4486 *
4487 * echo "123" > /mnt/foo
4488 * sync
4489 * mv /mnt/foo /mnt/bar
4490 * echo "abc" > /mnt/foo
4491 * xfs_io -c fsync /mnt/foo
4492 * <power fail>
4493 *
4494 * We also need this for cases where a snapshot entry is replaced by some other
4495 * entry (file or directory) otherwise we end up with an unreplayable log due to
4496 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4497 * if it were a regular entry:
4498 *
4499 * mkdir /mnt/x
4500 * btrfs subvolume snapshot /mnt /mnt/x/snap
4501 * btrfs subvolume delete /mnt/x/snap
4502 * rmdir /mnt/x
4503 * mkdir /mnt/x
4504 * fsync /mnt/x or fsync some new file inside it
4505 * <power fail>
4506 *
4507 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4508 * the same transaction.
4509 */
4510static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4511 const int slot,
4512 const struct btrfs_key *key,
Filipe Manana44f714d2016-06-06 16:11:13 +01004513 struct inode *inode,
4514 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004515{
4516 int ret;
4517 struct btrfs_path *search_path;
4518 char *name = NULL;
4519 u32 name_len = 0;
4520 u32 item_size = btrfs_item_size_nr(eb, slot);
4521 u32 cur_offset = 0;
4522 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4523
4524 search_path = btrfs_alloc_path();
4525 if (!search_path)
4526 return -ENOMEM;
4527 search_path->search_commit_root = 1;
4528 search_path->skip_locking = 1;
4529
4530 while (cur_offset < item_size) {
4531 u64 parent;
4532 u32 this_name_len;
4533 u32 this_len;
4534 unsigned long name_ptr;
4535 struct btrfs_dir_item *di;
4536
4537 if (key->type == BTRFS_INODE_REF_KEY) {
4538 struct btrfs_inode_ref *iref;
4539
4540 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4541 parent = key->offset;
4542 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4543 name_ptr = (unsigned long)(iref + 1);
4544 this_len = sizeof(*iref) + this_name_len;
4545 } else {
4546 struct btrfs_inode_extref *extref;
4547
4548 extref = (struct btrfs_inode_extref *)(ptr +
4549 cur_offset);
4550 parent = btrfs_inode_extref_parent(eb, extref);
4551 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4552 name_ptr = (unsigned long)&extref->name;
4553 this_len = sizeof(*extref) + this_name_len;
4554 }
4555
4556 if (this_name_len > name_len) {
4557 char *new_name;
4558
4559 new_name = krealloc(name, this_name_len, GFP_NOFS);
4560 if (!new_name) {
4561 ret = -ENOMEM;
4562 goto out;
4563 }
4564 name_len = this_name_len;
4565 name = new_name;
4566 }
4567
4568 read_extent_buffer(eb, name, name_ptr, this_name_len);
4569 di = btrfs_lookup_dir_item(NULL, BTRFS_I(inode)->root,
4570 search_path, parent,
4571 name, this_name_len, 0);
4572 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004573 struct btrfs_key di_key;
4574
4575 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4576 di, &di_key);
4577 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4578 ret = 1;
4579 *other_ino = di_key.objectid;
4580 } else {
4581 ret = -EAGAIN;
4582 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004583 goto out;
4584 } else if (IS_ERR(di)) {
4585 ret = PTR_ERR(di);
4586 goto out;
4587 }
4588 btrfs_release_path(search_path);
4589
4590 cur_offset += this_len;
4591 }
4592 ret = 0;
4593out:
4594 btrfs_free_path(search_path);
4595 kfree(name);
4596 return ret;
4597}
4598
Chris Masone02119d2008-09-05 16:13:11 -04004599/* log a single inode in the tree log.
4600 * At least one parent directory for this inode must exist in the tree
4601 * or be logged already.
4602 *
4603 * Any items from this inode changed by the current transaction are copied
4604 * to the log tree. An extra reference is taken on any extents in this
4605 * file, allowing us to avoid a whole pile of corner cases around logging
4606 * blocks that have been removed from the tree.
4607 *
4608 * See LOG_INODE_ALL and related defines for a description of what inode_only
4609 * does.
4610 *
4611 * This handles both files and directories.
4612 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004613static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004614 struct btrfs_root *root, struct inode *inode,
4615 int inode_only,
4616 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004617 const loff_t end,
4618 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004619{
4620 struct btrfs_path *path;
4621 struct btrfs_path *dst_path;
4622 struct btrfs_key min_key;
4623 struct btrfs_key max_key;
4624 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004625 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08004626 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004627 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004628 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004629 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004630 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004631 int ins_start_slot = 0;
4632 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004633 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08004634 u64 ino = btrfs_ino(inode);
Filipe Manana49dae1b2014-09-06 22:34:39 +01004635 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004636 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004637 bool need_log_inode_item = true;
Filipe Manana92291242018-05-11 16:42:42 +01004638 bool xattrs_logged = false;
Chris Masone02119d2008-09-05 16:13:11 -04004639
Chris Masone02119d2008-09-05 16:13:11 -04004640 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004641 if (!path)
4642 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004643 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004644 if (!dst_path) {
4645 btrfs_free_path(path);
4646 return -ENOMEM;
4647 }
Chris Masone02119d2008-09-05 16:13:11 -04004648
Li Zefan33345d012011-04-20 10:31:50 +08004649 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004650 min_key.type = BTRFS_INODE_ITEM_KEY;
4651 min_key.offset = 0;
4652
Li Zefan33345d012011-04-20 10:31:50 +08004653 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004654
Chris Mason12fcfd22009-03-24 10:24:20 -04004655
Josef Bacik5dc562c2012-08-17 13:14:17 -04004656 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00004657 if (S_ISDIR(inode->i_mode) ||
4658 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4659 &BTRFS_I(inode)->runtime_flags) &&
Liu Bo67312122016-11-30 16:20:25 -08004660 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004661 max_key.type = BTRFS_XATTR_ITEM_KEY;
4662 else
4663 max_key.type = (u8)-1;
4664 max_key.offset = (u64)-1;
4665
Filipe Manana2c2c4522015-01-13 16:40:04 +00004666 /*
4667 * Only run delayed items if we are a dir or a new file.
4668 * Otherwise commit the delayed inode only, which is needed in
4669 * order for the log replay code to mark inodes for link count
4670 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4671 */
Josef Bacik94edf4a2012-09-25 14:56:25 -04004672 if (S_ISDIR(inode->i_mode) ||
Filipe Manana2c2c4522015-01-13 16:40:04 +00004673 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
Josef Bacik94edf4a2012-09-25 14:56:25 -04004674 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004675 else
4676 ret = btrfs_commit_inode_delayed_inode(inode);
4677
4678 if (ret) {
4679 btrfs_free_path(path);
4680 btrfs_free_path(dst_path);
4681 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004682 }
4683
Liu Bo67312122016-11-30 16:20:25 -08004684 if (inode_only == LOG_OTHER_INODE) {
4685 inode_only = LOG_INODE_EXISTS;
4686 mutex_lock_nested(&BTRFS_I(inode)->log_mutex,
4687 SINGLE_DEPTH_NESTING);
4688 } else {
4689 mutex_lock(&BTRFS_I(inode)->log_mutex);
4690 }
Chris Masone02119d2008-09-05 16:13:11 -04004691
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004692 /*
Chris Masone02119d2008-09-05 16:13:11 -04004693 * a brute force approach to making sure we get the most uptodate
4694 * copies of everything.
4695 */
4696 if (S_ISDIR(inode->i_mode)) {
4697 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4698
Filipe Manana4f764e52015-02-23 19:53:35 +00004699 if (inode_only == LOG_INODE_EXISTS)
4700 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004701 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004702 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004703 if (inode_only == LOG_INODE_EXISTS) {
4704 /*
4705 * Make sure the new inode item we write to the log has
4706 * the same isize as the current one (if it exists).
4707 * This is necessary to prevent data loss after log
4708 * replay, and also to prevent doing a wrong expanding
4709 * truncate - for e.g. create file, write 4K into offset
4710 * 0, fsync, write 4K into offset 4096, add hard link,
4711 * fsync some other file (to sync log), power fail - if
4712 * we use the inode's current i_size, after log replay
4713 * we get a 8Kb file, with the last 4Kb extent as a hole
4714 * (zeroes), as if an expanding truncate happened,
4715 * instead of getting a file of 4Kb only.
4716 */
4717 err = logged_inode_size(log, inode, path,
4718 &logged_isize);
4719 if (err)
4720 goto out_unlock;
4721 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004722 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4723 &BTRFS_I(inode)->runtime_flags)) {
4724 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004725 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004726 ret = drop_objectid_items(trans, log, path, ino,
4727 max_key.type);
4728 } else {
4729 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4730 &BTRFS_I(inode)->runtime_flags);
4731 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4732 &BTRFS_I(inode)->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004733 while(1) {
4734 ret = btrfs_truncate_inode_items(trans,
4735 log, inode, 0, 0);
4736 if (ret != -EAGAIN)
4737 break;
4738 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004739 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004740 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4741 &BTRFS_I(inode)->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004742 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004743 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004744 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004745 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004746 ret = drop_objectid_items(trans, log, path, ino,
4747 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004748 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004749 if (inode_only == LOG_INODE_ALL)
4750 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004751 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004752 }
Josef Bacika95249b2012-10-11 16:17:34 -04004753
Chris Masone02119d2008-09-05 16:13:11 -04004754 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004755 if (ret) {
4756 err = ret;
4757 goto out_unlock;
4758 }
Chris Masone02119d2008-09-05 16:13:11 -04004759
Chris Masond3977122009-01-05 21:25:51 -05004760 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004761 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004762 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004763 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004764 if (ret < 0) {
4765 err = ret;
4766 goto out_unlock;
4767 }
Chris Masone02119d2008-09-05 16:13:11 -04004768 if (ret != 0)
4769 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004770again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004771 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004772 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004773 break;
4774 if (min_key.type > max_key.type)
4775 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004776
Filipe Mananae4545de2015-06-17 12:49:23 +01004777 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4778 need_log_inode_item = false;
4779
Filipe Manana56f23fd2016-03-30 23:37:21 +01004780 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4781 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
4782 BTRFS_I(inode)->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004783 u64 other_ino = 0;
4784
Filipe Manana56f23fd2016-03-30 23:37:21 +01004785 ret = btrfs_check_ref_name_override(path->nodes[0],
4786 path->slots[0],
Filipe Manana44f714d2016-06-06 16:11:13 +01004787 &min_key, inode,
4788 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004789 if (ret < 0) {
4790 err = ret;
4791 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004792 } else if (ret > 0 && ctx &&
4793 other_ino != btrfs_ino(ctx->inode)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004794 struct btrfs_key inode_key;
4795 struct inode *other_inode;
4796
4797 if (ins_nr > 0) {
4798 ins_nr++;
4799 } else {
4800 ins_nr = 1;
4801 ins_start_slot = path->slots[0];
4802 }
4803 ret = copy_items(trans, inode, dst_path, path,
4804 &last_extent, ins_start_slot,
4805 ins_nr, inode_only,
4806 logged_isize);
4807 if (ret < 0) {
4808 err = ret;
4809 goto out_unlock;
4810 }
4811 ins_nr = 0;
4812 btrfs_release_path(path);
4813 inode_key.objectid = other_ino;
4814 inode_key.type = BTRFS_INODE_ITEM_KEY;
4815 inode_key.offset = 0;
4816 other_inode = btrfs_iget(root->fs_info->sb,
4817 &inode_key, root,
4818 NULL);
4819 /*
4820 * If the other inode that had a conflicting dir
4821 * entry was deleted in the current transaction,
4822 * we don't need to do more work nor fallback to
4823 * a transaction commit.
4824 */
4825 if (IS_ERR(other_inode) &&
4826 PTR_ERR(other_inode) == -ENOENT) {
4827 goto next_key;
4828 } else if (IS_ERR(other_inode)) {
4829 err = PTR_ERR(other_inode);
4830 goto out_unlock;
4831 }
4832 /*
4833 * We are safe logging the other inode without
4834 * acquiring its i_mutex as long as we log with
4835 * the LOG_INODE_EXISTS mode. We're safe against
4836 * concurrent renames of the other inode as well
4837 * because during a rename we pin the log and
4838 * update the log with the new name before we
4839 * unpin it.
4840 */
4841 err = btrfs_log_inode(trans, root, other_inode,
Liu Bo67312122016-11-30 16:20:25 -08004842 LOG_OTHER_INODE,
Filipe Manana44f714d2016-06-06 16:11:13 +01004843 0, LLONG_MAX, ctx);
Filipe Manana4bc698d2019-09-10 15:26:49 +01004844 btrfs_add_delayed_iput(other_inode);
Filipe Manana44f714d2016-06-06 16:11:13 +01004845 if (err)
4846 goto out_unlock;
4847 else
4848 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01004849 }
4850 }
4851
Filipe Manana36283bf2015-06-20 00:44:51 +01004852 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
4853 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
4854 if (ins_nr == 0)
4855 goto next_slot;
4856 ret = copy_items(trans, inode, dst_path, path,
4857 &last_extent, ins_start_slot,
4858 ins_nr, inode_only, logged_isize);
4859 if (ret < 0) {
4860 err = ret;
4861 goto out_unlock;
4862 }
4863 ins_nr = 0;
4864 if (ret) {
4865 btrfs_release_path(path);
4866 continue;
4867 }
4868 goto next_slot;
4869 }
4870
Chris Masone02119d2008-09-05 16:13:11 -04004871 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004872 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4873 ins_nr++;
4874 goto next_slot;
4875 } else if (!ins_nr) {
4876 ins_start_slot = path->slots[0];
4877 ins_nr = 1;
4878 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004879 }
4880
Josef Bacik16e75492013-10-22 12:18:51 -04004881 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004882 ins_start_slot, ins_nr, inode_only,
4883 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004884 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004885 err = ret;
4886 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02004887 }
4888 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04004889 ins_nr = 0;
4890 btrfs_release_path(path);
4891 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004892 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004893 ins_nr = 1;
4894 ins_start_slot = path->slots[0];
4895next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004896
Chris Mason3a5f1d42008-09-11 15:53:37 -04004897 nritems = btrfs_header_nritems(path->nodes[0]);
4898 path->slots[0]++;
4899 if (path->slots[0] < nritems) {
4900 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4901 path->slots[0]);
4902 goto again;
4903 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004904 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004905 ret = copy_items(trans, inode, dst_path, path,
4906 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004907 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004908 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004909 err = ret;
4910 goto out_unlock;
4911 }
Josef Bacik16e75492013-10-22 12:18:51 -04004912 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004913 ins_nr = 0;
4914 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004915 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01004916next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004917 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004918 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004919 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004920 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004921 min_key.offset = 0;
4922 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004923 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004924 }
Chris Masone02119d2008-09-05 16:13:11 -04004925 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004926 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004927 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004928 ins_start_slot, ins_nr, inode_only,
4929 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004930 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004931 err = ret;
4932 goto out_unlock;
4933 }
Josef Bacik16e75492013-10-22 12:18:51 -04004934 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004935 ins_nr = 0;
4936 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004937
Filipe Manana36283bf2015-06-20 00:44:51 +01004938 btrfs_release_path(path);
4939 btrfs_release_path(dst_path);
4940 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
4941 if (err)
4942 goto out_unlock;
Filipe Manana92291242018-05-11 16:42:42 +01004943 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004944 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
4945 btrfs_release_path(path);
4946 btrfs_release_path(dst_path);
4947 err = btrfs_log_trailing_hole(trans, root, inode, path);
4948 if (err)
4949 goto out_unlock;
4950 }
Josef Bacika95249b2012-10-11 16:17:34 -04004951log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004952 btrfs_release_path(path);
4953 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01004954 if (need_log_inode_item) {
4955 err = log_inode_item(trans, log, dst_path, inode);
Filipe Manana92291242018-05-11 16:42:42 +01004956 if (!err && !xattrs_logged) {
4957 err = btrfs_log_all_xattrs(trans, root, inode, path,
4958 dst_path);
4959 btrfs_release_path(path);
4960 }
Filipe Mananae4545de2015-06-17 12:49:23 +01004961 if (err)
4962 goto out_unlock;
4963 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004964 if (fast_search) {
Miao Xie827463c2014-01-14 20:31:51 +08004965 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004966 &logged_list, ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004967 if (ret) {
4968 err = ret;
4969 goto out_unlock;
4970 }
Josef Bacikd006a042013-11-12 20:54:09 -05004971 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004972 struct extent_map *em, *n;
4973
Filipe Manana49dae1b2014-09-06 22:34:39 +01004974 write_lock(&em_tree->lock);
4975 /*
4976 * We can't just remove every em if we're called for a ranged
4977 * fsync - that is, one that doesn't cover the whole possible
4978 * file range (0 to LLONG_MAX). This is because we can have
4979 * em's that fall outside the range we're logging and therefore
4980 * their ordered operations haven't completed yet
4981 * (btrfs_finish_ordered_io() not invoked yet). This means we
4982 * didn't get their respective file extent item in the fs/subvol
4983 * tree yet, and need to let the next fast fsync (one which
4984 * consults the list of modified extent maps) find the em so
4985 * that it logs a matching file extent item and waits for the
4986 * respective ordered operation to complete (if it's still
4987 * running).
4988 *
4989 * Removing every em outside the range we're logging would make
4990 * the next fast fsync not log their matching file extent items,
4991 * therefore making us lose data after a log replay.
4992 */
4993 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4994 list) {
4995 const u64 mod_end = em->mod_start + em->mod_len - 1;
4996
4997 if (em->mod_start >= start && mod_end <= end)
4998 list_del_init(&em->list);
4999 }
5000 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005001 }
5002
Chris Mason9623f9a2008-09-11 17:42:42 -04005003 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005004 ret = log_directory_changes(trans, root, inode, path, dst_path,
5005 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005006 if (ret) {
5007 err = ret;
5008 goto out_unlock;
5009 }
Chris Masone02119d2008-09-05 16:13:11 -04005010 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005011
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005012 spin_lock(&BTRFS_I(inode)->lock);
Filipe Manana125c4cf92014-09-11 21:22:14 +01005013 BTRFS_I(inode)->logged_trans = trans->transid;
5014 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005015 spin_unlock(&BTRFS_I(inode)->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005016out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08005017 if (unlikely(err))
5018 btrfs_put_logged_extents(&logged_list);
5019 else
5020 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04005021 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5022
5023 btrfs_free_path(path);
5024 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005025 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005026}
5027
Chris Mason12fcfd22009-03-24 10:24:20 -04005028/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005029 * Check if we must fallback to a transaction commit when logging an inode.
5030 * This must be called after logging the inode and is used only in the context
5031 * when fsyncing an inode requires the need to log some other inode - in which
5032 * case we can't lock the i_mutex of each other inode we need to log as that
5033 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5034 * log inodes up or down in the hierarchy) or rename operations for example. So
5035 * we take the log_mutex of the inode after we have logged it and then check for
5036 * its last_unlink_trans value - this is safe because any task setting
5037 * last_unlink_trans must take the log_mutex and it must do this before it does
5038 * the actual unlink operation, so if we do this check before a concurrent task
5039 * sets last_unlink_trans it means we've logged a consistent version/state of
5040 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005041 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005042 * we logged the inode or it might have also done the unlink).
5043 */
5044static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5045 struct inode *inode)
5046{
5047 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
5048 bool ret = false;
5049
5050 mutex_lock(&BTRFS_I(inode)->log_mutex);
5051 if (BTRFS_I(inode)->last_unlink_trans > fs_info->last_trans_committed) {
5052 /*
5053 * Make sure any commits to the log are forced to be full
5054 * commits.
5055 */
5056 btrfs_set_log_full_commit(fs_info, trans);
5057 ret = true;
5058 }
5059 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5060
5061 return ret;
5062}
5063
5064/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005065 * follow the dentry parent pointers up the chain and see if any
5066 * of the directories in it require a full commit before they can
5067 * be logged. Returns zero if nothing special needs to be done or 1 if
5068 * a full commit is required.
5069 */
5070static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5071 struct inode *inode,
5072 struct dentry *parent,
5073 struct super_block *sb,
5074 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005075{
Chris Mason12fcfd22009-03-24 10:24:20 -04005076 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005077 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04005078 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005079
Chris Masonaf4176b2009-03-24 10:24:31 -04005080 /*
5081 * for regular files, if its inode is already on disk, we don't
5082 * have to worry about the parents at all. This is because
5083 * we can use the last_unlink_trans field to record renames
5084 * and other fun in this file.
5085 */
5086 if (S_ISREG(inode->i_mode) &&
5087 BTRFS_I(inode)->generation <= last_committed &&
5088 BTRFS_I(inode)->last_unlink_trans <= last_committed)
5089 goto out;
5090
Chris Mason12fcfd22009-03-24 10:24:20 -04005091 if (!S_ISDIR(inode->i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005092 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005093 goto out;
David Howells2b0143b2015-03-17 22:25:59 +00005094 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005095 }
5096
5097 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005098 /*
5099 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005100 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005101 * logged_trans so that further down in the log code we don't
5102 * think this inode has already been logged.
5103 */
5104 if (inode != orig_inode)
5105 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005106 smp_mb();
5107
Filipe Manana2be63d52016-02-12 11:34:23 +00005108 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005109 ret = 1;
5110 break;
5111 }
5112
Al Virofc640052016-04-10 01:33:30 -04005113 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005114 break;
5115
Filipe Manana44f714d2016-06-06 16:11:13 +01005116 if (IS_ROOT(parent)) {
5117 inode = d_inode(parent);
5118 if (btrfs_must_commit_transaction(trans, inode))
5119 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005120 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005121 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005122
Josef Bacik6a912212010-11-20 09:48:00 +00005123 parent = dget_parent(parent);
5124 dput(old_parent);
5125 old_parent = parent;
David Howells2b0143b2015-03-17 22:25:59 +00005126 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005127
5128 }
Josef Bacik6a912212010-11-20 09:48:00 +00005129 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005130out:
Chris Masone02119d2008-09-05 16:13:11 -04005131 return ret;
5132}
5133
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005134struct btrfs_dir_list {
5135 u64 ino;
5136 struct list_head list;
5137};
5138
5139/*
5140 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5141 * details about the why it is needed.
5142 * This is a recursive operation - if an existing dentry corresponds to a
5143 * directory, that directory's new entries are logged too (same behaviour as
5144 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5145 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5146 * complains about the following circular lock dependency / possible deadlock:
5147 *
5148 * CPU0 CPU1
5149 * ---- ----
5150 * lock(&type->i_mutex_dir_key#3/2);
5151 * lock(sb_internal#2);
5152 * lock(&type->i_mutex_dir_key#3/2);
5153 * lock(&sb->s_type->i_mutex_key#14);
5154 *
5155 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5156 * sb_start_intwrite() in btrfs_start_transaction().
5157 * Not locking i_mutex of the inodes is still safe because:
5158 *
5159 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5160 * that while logging the inode new references (names) are added or removed
5161 * from the inode, leaving the logged inode item with a link count that does
5162 * not match the number of logged inode reference items. This is fine because
5163 * at log replay time we compute the real number of links and correct the
5164 * link count in the inode item (see replay_one_buffer() and
5165 * link_to_fixup_dir());
5166 *
5167 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5168 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5169 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5170 * has a size that doesn't match the sum of the lengths of all the logged
5171 * names. This does not result in a problem because if a dir_item key is
5172 * logged but its matching dir_index key is not logged, at log replay time we
5173 * don't use it to replay the respective name (see replay_one_name()). On the
5174 * other hand if only the dir_index key ends up being logged, the respective
5175 * name is added to the fs/subvol tree with both the dir_item and dir_index
5176 * keys created (see replay_one_name()).
5177 * The directory's inode item with a wrong i_size is not a problem as well,
5178 * since we don't use it at log replay time to set the i_size in the inode
5179 * item of the fs/subvol tree (see overwrite_item()).
5180 */
5181static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5182 struct btrfs_root *root,
5183 struct inode *start_inode,
5184 struct btrfs_log_ctx *ctx)
5185{
5186 struct btrfs_root *log = root->log_root;
5187 struct btrfs_path *path;
5188 LIST_HEAD(dir_list);
5189 struct btrfs_dir_list *dir_elem;
5190 int ret = 0;
5191
5192 path = btrfs_alloc_path();
5193 if (!path)
5194 return -ENOMEM;
5195
5196 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5197 if (!dir_elem) {
5198 btrfs_free_path(path);
5199 return -ENOMEM;
5200 }
5201 dir_elem->ino = btrfs_ino(start_inode);
5202 list_add_tail(&dir_elem->list, &dir_list);
5203
5204 while (!list_empty(&dir_list)) {
5205 struct extent_buffer *leaf;
5206 struct btrfs_key min_key;
5207 int nritems;
5208 int i;
5209
5210 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5211 list);
5212 if (ret)
5213 goto next_dir_inode;
5214
5215 min_key.objectid = dir_elem->ino;
5216 min_key.type = BTRFS_DIR_ITEM_KEY;
5217 min_key.offset = 0;
5218again:
5219 btrfs_release_path(path);
5220 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5221 if (ret < 0) {
5222 goto next_dir_inode;
5223 } else if (ret > 0) {
5224 ret = 0;
5225 goto next_dir_inode;
5226 }
5227
5228process_leaf:
5229 leaf = path->nodes[0];
5230 nritems = btrfs_header_nritems(leaf);
5231 for (i = path->slots[0]; i < nritems; i++) {
5232 struct btrfs_dir_item *di;
5233 struct btrfs_key di_key;
5234 struct inode *di_inode;
5235 struct btrfs_dir_list *new_dir_elem;
5236 int log_mode = LOG_INODE_EXISTS;
5237 int type;
5238
5239 btrfs_item_key_to_cpu(leaf, &min_key, i);
5240 if (min_key.objectid != dir_elem->ino ||
5241 min_key.type != BTRFS_DIR_ITEM_KEY)
5242 goto next_dir_inode;
5243
5244 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5245 type = btrfs_dir_type(leaf, di);
5246 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5247 type != BTRFS_FT_DIR)
5248 continue;
5249 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5250 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5251 continue;
5252
Robbie Ko664b0532016-10-28 10:48:26 +08005253 btrfs_release_path(path);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005254 di_inode = btrfs_iget(root->fs_info->sb, &di_key,
5255 root, NULL);
5256 if (IS_ERR(di_inode)) {
5257 ret = PTR_ERR(di_inode);
5258 goto next_dir_inode;
5259 }
5260
5261 if (btrfs_inode_in_log(di_inode, trans->transid)) {
Filipe Manana4bc698d2019-09-10 15:26:49 +01005262 btrfs_add_delayed_iput(di_inode);
Robbie Ko664b0532016-10-28 10:48:26 +08005263 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005264 }
5265
5266 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005267 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005268 log_mode = LOG_INODE_ALL;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005269 ret = btrfs_log_inode(trans, root, di_inode,
5270 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005271 if (!ret &&
5272 btrfs_must_commit_transaction(trans, di_inode))
5273 ret = 1;
Filipe Manana4bc698d2019-09-10 15:26:49 +01005274 btrfs_add_delayed_iput(di_inode);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005275 if (ret)
5276 goto next_dir_inode;
5277 if (ctx->log_new_dentries) {
5278 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5279 GFP_NOFS);
5280 if (!new_dir_elem) {
5281 ret = -ENOMEM;
5282 goto next_dir_inode;
5283 }
5284 new_dir_elem->ino = di_key.objectid;
5285 list_add_tail(&new_dir_elem->list, &dir_list);
5286 }
5287 break;
5288 }
5289 if (i == nritems) {
5290 ret = btrfs_next_leaf(log, path);
5291 if (ret < 0) {
5292 goto next_dir_inode;
5293 } else if (ret > 0) {
5294 ret = 0;
5295 goto next_dir_inode;
5296 }
5297 goto process_leaf;
5298 }
5299 if (min_key.offset < (u64)-1) {
5300 min_key.offset++;
5301 goto again;
5302 }
5303next_dir_inode:
5304 list_del(&dir_elem->list);
5305 kfree(dir_elem);
5306 }
5307
5308 btrfs_free_path(path);
5309 return ret;
5310}
5311
Filipe Manana18aa0922015-08-05 16:49:08 +01005312static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5313 struct inode *inode,
5314 struct btrfs_log_ctx *ctx)
5315{
5316 int ret;
5317 struct btrfs_path *path;
5318 struct btrfs_key key;
5319 struct btrfs_root *root = BTRFS_I(inode)->root;
5320 const u64 ino = btrfs_ino(inode);
5321
5322 path = btrfs_alloc_path();
5323 if (!path)
5324 return -ENOMEM;
5325 path->skip_locking = 1;
5326 path->search_commit_root = 1;
5327
5328 key.objectid = ino;
5329 key.type = BTRFS_INODE_REF_KEY;
5330 key.offset = 0;
5331 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5332 if (ret < 0)
5333 goto out;
5334
5335 while (true) {
5336 struct extent_buffer *leaf = path->nodes[0];
5337 int slot = path->slots[0];
5338 u32 cur_offset = 0;
5339 u32 item_size;
5340 unsigned long ptr;
5341
5342 if (slot >= btrfs_header_nritems(leaf)) {
5343 ret = btrfs_next_leaf(root, path);
5344 if (ret < 0)
5345 goto out;
5346 else if (ret > 0)
5347 break;
5348 continue;
5349 }
5350
5351 btrfs_item_key_to_cpu(leaf, &key, slot);
5352 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5353 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5354 break;
5355
5356 item_size = btrfs_item_size_nr(leaf, slot);
5357 ptr = btrfs_item_ptr_offset(leaf, slot);
5358 while (cur_offset < item_size) {
5359 struct btrfs_key inode_key;
5360 struct inode *dir_inode;
5361
5362 inode_key.type = BTRFS_INODE_ITEM_KEY;
5363 inode_key.offset = 0;
5364
5365 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5366 struct btrfs_inode_extref *extref;
5367
5368 extref = (struct btrfs_inode_extref *)
5369 (ptr + cur_offset);
5370 inode_key.objectid = btrfs_inode_extref_parent(
5371 leaf, extref);
5372 cur_offset += sizeof(*extref);
5373 cur_offset += btrfs_inode_extref_name_len(leaf,
5374 extref);
5375 } else {
5376 inode_key.objectid = key.offset;
5377 cur_offset = item_size;
5378 }
5379
5380 dir_inode = btrfs_iget(root->fs_info->sb, &inode_key,
5381 root, NULL);
Filipe Manana6a0403a2018-10-09 15:05:29 +01005382 /*
5383 * If the parent inode was deleted, return an error to
5384 * fallback to a transaction commit. This is to prevent
5385 * getting an inode that was moved from one parent A to
5386 * a parent B, got its former parent A deleted and then
5387 * it got fsync'ed, from existing at both parents after
5388 * a log replay (and the old parent still existing).
5389 * Example:
5390 *
5391 * mkdir /mnt/A
5392 * mkdir /mnt/B
5393 * touch /mnt/B/bar
5394 * sync
5395 * mv /mnt/B/bar /mnt/A/bar
5396 * mv -T /mnt/A /mnt/B
5397 * fsync /mnt/B/bar
5398 * <power fail>
5399 *
5400 * If we ignore the old parent B which got deleted,
5401 * after a log replay we would have file bar linked
5402 * at both parents and the old parent B would still
5403 * exist.
5404 */
5405 if (IS_ERR(dir_inode)) {
5406 ret = PTR_ERR(dir_inode);
5407 goto out;
5408 }
Filipe Manana18aa0922015-08-05 16:49:08 +01005409
Filipe Manana657ed1a2016-04-06 17:11:56 +01005410 if (ctx)
5411 ctx->log_new_dentries = false;
Filipe Manana18aa0922015-08-05 16:49:08 +01005412 ret = btrfs_log_inode(trans, root, dir_inode,
5413 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005414 if (!ret &&
5415 btrfs_must_commit_transaction(trans, dir_inode))
5416 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005417 if (!ret && ctx && ctx->log_new_dentries)
5418 ret = log_new_dir_dentries(trans, root,
5419 dir_inode, ctx);
Filipe Manana4bc698d2019-09-10 15:26:49 +01005420 btrfs_add_delayed_iput(dir_inode);
Filipe Manana18aa0922015-08-05 16:49:08 +01005421 if (ret)
5422 goto out;
5423 }
5424 path->slots[0]++;
5425 }
5426 ret = 0;
5427out:
5428 btrfs_free_path(path);
5429 return ret;
5430}
5431
Chris Masone02119d2008-09-05 16:13:11 -04005432/*
5433 * helper function around btrfs_log_inode to make sure newly created
5434 * parent directories also end up in the log. A minimal inode and backref
5435 * only logging is done of any parent directories that are older than
5436 * the last committed transaction
5437 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005438static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5439 struct btrfs_root *root, struct inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005440 struct dentry *parent,
5441 const loff_t start,
5442 const loff_t end,
5443 int exists_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005444 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005445{
Chris Mason12fcfd22009-03-24 10:24:20 -04005446 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04005447 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005448 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005449 int ret = 0;
5450 u64 last_committed = root->fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005451 bool log_dentries = false;
5452 struct inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005453
5454 sb = inode->i_sb;
5455
Jeff Mahoney3cdde222016-06-09 21:38:35 -04005456 if (btrfs_test_opt(root->fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005457 ret = 1;
5458 goto end_no_trans;
5459 }
5460
Miao Xie995946d2014-04-02 19:51:06 +08005461 /*
5462 * The prev transaction commit doesn't complete, we need do
5463 * full commit by ourselves.
5464 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005465 if (root->fs_info->last_trans_log_full_commit >
5466 root->fs_info->last_trans_committed) {
5467 ret = 1;
5468 goto end_no_trans;
5469 }
5470
Yan, Zheng76dda932009-09-21 16:00:26 -04005471 if (root != BTRFS_I(inode)->root ||
5472 btrfs_root_refs(&root->root_item) == 0) {
5473 ret = 1;
5474 goto end_no_trans;
5475 }
5476
Chris Mason12fcfd22009-03-24 10:24:20 -04005477 ret = check_parent_dirs_for_sync(trans, inode, parent,
5478 sb, last_committed);
5479 if (ret)
5480 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005481
Josef Bacik22ee6982012-05-29 16:57:49 -04005482 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005483 ret = BTRFS_NO_LOG_SYNC;
5484 goto end_no_trans;
5485 }
5486
Miao Xie8b050d32014-02-20 18:08:58 +08005487 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005488 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005489 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005490
Filipe Manana8407f552014-09-05 15:14:39 +01005491 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005492 if (ret)
5493 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005494
Chris Masonaf4176b2009-03-24 10:24:31 -04005495 /*
5496 * for regular files, if its inode is already on disk, we don't
5497 * have to worry about the parents at all. This is because
5498 * we can use the last_unlink_trans field to record renames
5499 * and other fun in this file.
5500 */
5501 if (S_ISREG(inode->i_mode) &&
5502 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005503 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
5504 ret = 0;
5505 goto end_trans;
5506 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005507
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005508 if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
5509 log_dentries = true;
5510
Filipe Manana18aa0922015-08-05 16:49:08 +01005511 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005512 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005513 * inodes are fully logged. This is to prevent leaving dangling
5514 * directory index entries in directories that were our parents but are
5515 * not anymore. Not doing this results in old parent directory being
5516 * impossible to delete after log replay (rmdir will always fail with
5517 * error -ENOTEMPTY).
5518 *
5519 * Example 1:
5520 *
5521 * mkdir testdir
5522 * touch testdir/foo
5523 * ln testdir/foo testdir/bar
5524 * sync
5525 * unlink testdir/bar
5526 * xfs_io -c fsync testdir/foo
5527 * <power failure>
5528 * mount fs, triggers log replay
5529 *
5530 * If we don't log the parent directory (testdir), after log replay the
5531 * directory still has an entry pointing to the file inode using the bar
5532 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5533 * the file inode has a link count of 1.
5534 *
5535 * Example 2:
5536 *
5537 * mkdir testdir
5538 * touch foo
5539 * ln foo testdir/foo2
5540 * ln foo testdir/foo3
5541 * sync
5542 * unlink testdir/foo3
5543 * xfs_io -c fsync foo
5544 * <power failure>
5545 * mount fs, triggers log replay
5546 *
5547 * Similar as the first example, after log replay the parent directory
5548 * testdir still has an entry pointing to the inode file with name foo3
5549 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5550 * and has a link count of 2.
5551 */
5552 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
5553 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5554 if (ret)
5555 goto end_trans;
5556 }
5557
Chris Masond3977122009-01-05 21:25:51 -05005558 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005559 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005560 break;
5561
David Howells2b0143b2015-03-17 22:25:59 +00005562 inode = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005563 if (root != BTRFS_I(inode)->root)
5564 break;
5565
Filipe Manana18aa0922015-08-05 16:49:08 +01005566 if (BTRFS_I(inode)->generation > last_committed) {
5567 ret = btrfs_log_inode(trans, root, inode,
5568 LOG_INODE_EXISTS,
Filipe Manana8407f552014-09-05 15:14:39 +01005569 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005570 if (ret)
5571 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005572 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005573 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005574 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005575
Josef Bacik6a912212010-11-20 09:48:00 +00005576 parent = dget_parent(parent);
5577 dput(old_parent);
5578 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005579 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005580 if (log_dentries)
5581 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5582 else
5583 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005584end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005585 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005586 if (ret < 0) {
Miao Xie995946d2014-04-02 19:51:06 +08005587 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005588 ret = 1;
5589 }
Miao Xie8b050d32014-02-20 18:08:58 +08005590
5591 if (ret)
5592 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005593 btrfs_end_log_trans(root);
5594end_no_trans:
5595 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005596}
5597
5598/*
5599 * it is not safe to log dentry if the chunk root has added new
5600 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5601 * If this returns 1, you must commit the transaction to safely get your
5602 * data on disk.
5603 */
5604int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08005605 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005606 const loff_t start,
5607 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005608 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005609{
Josef Bacik6a912212010-11-20 09:48:00 +00005610 struct dentry *parent = dget_parent(dentry);
5611 int ret;
5612
David Howells2b0143b2015-03-17 22:25:59 +00005613 ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005614 start, end, 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005615 dput(parent);
5616
5617 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005618}
5619
5620/*
5621 * should be called during mount to recover any replay any log trees
5622 * from the FS
5623 */
5624int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5625{
5626 int ret;
5627 struct btrfs_path *path;
5628 struct btrfs_trans_handle *trans;
5629 struct btrfs_key key;
5630 struct btrfs_key found_key;
5631 struct btrfs_key tmp_key;
5632 struct btrfs_root *log;
5633 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5634 struct walk_control wc = {
5635 .process_func = process_one_buffer,
5636 .stage = 0,
5637 };
5638
Chris Masone02119d2008-09-05 16:13:11 -04005639 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005640 if (!path)
5641 return -ENOMEM;
5642
Josef Bacikafcdd122016-09-02 15:40:02 -04005643 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005644
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005645 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005646 if (IS_ERR(trans)) {
5647 ret = PTR_ERR(trans);
5648 goto error;
5649 }
Chris Masone02119d2008-09-05 16:13:11 -04005650
5651 wc.trans = trans;
5652 wc.pin = 1;
5653
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005654 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005655 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005656 btrfs_handle_fs_error(fs_info, ret,
5657 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005658 goto error;
5659 }
Chris Masone02119d2008-09-05 16:13:11 -04005660
5661again:
5662 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5663 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005664 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005665
Chris Masond3977122009-01-05 21:25:51 -05005666 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005667 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005668
5669 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005670 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005671 "Couldn't find tree log root.");
5672 goto error;
5673 }
Chris Masone02119d2008-09-05 16:13:11 -04005674 if (ret > 0) {
5675 if (path->slots[0] == 0)
5676 break;
5677 path->slots[0]--;
5678 }
5679 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5680 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005681 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005682 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5683 break;
5684
Miao Xiecb517ea2013-05-15 07:48:19 +00005685 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005686 if (IS_ERR(log)) {
5687 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005688 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005689 "Couldn't read tree log root.");
5690 goto error;
5691 }
Chris Masone02119d2008-09-05 16:13:11 -04005692
5693 tmp_key.objectid = found_key.offset;
5694 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5695 tmp_key.offset = (u64)-1;
5696
5697 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005698 if (IS_ERR(wc.replay_dest)) {
5699 ret = PTR_ERR(wc.replay_dest);
Josef Bacik7875fd32019-12-06 09:37:17 -05005700
5701 /*
5702 * We didn't find the subvol, likely because it was
5703 * deleted. This is ok, simply skip this log and go to
5704 * the next one.
5705 *
5706 * We need to exclude the root because we can't have
5707 * other log replays overwriting this log as we'll read
5708 * it back in a few more times. This will keep our
5709 * block from being modified, and we'll just bail for
5710 * each subsequent pass.
5711 */
5712 if (ret == -ENOENT)
5713 ret = btrfs_pin_extent_for_log_replay(fs_info->extent_root,
5714 log->node->start,
5715 log->node->len);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005716 free_extent_buffer(log->node);
5717 free_extent_buffer(log->commit_root);
5718 kfree(log);
Josef Bacik7875fd32019-12-06 09:37:17 -05005719
5720 if (!ret)
5721 goto next;
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005722 btrfs_handle_fs_error(fs_info, ret,
5723 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005724 goto error;
5725 }
Chris Masone02119d2008-09-05 16:13:11 -04005726
Yan Zheng07d400a2009-01-06 11:42:00 -05005727 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005728 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005729 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005730
Josef Bacikb50c6e22013-04-25 15:55:30 -04005731 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005732 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5733 path);
Chris Masone02119d2008-09-05 16:13:11 -04005734 }
Chris Masone02119d2008-09-05 16:13:11 -04005735
Liu Boefb1cbc2018-01-25 11:02:56 -07005736 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5737 struct btrfs_root *root = wc.replay_dest;
5738
5739 btrfs_release_path(path);
5740
5741 /*
5742 * We have just replayed everything, and the highest
5743 * objectid of fs roots probably has changed in case
5744 * some inode_item's got replayed.
5745 *
5746 * root->objectid_mutex is not acquired as log replay
5747 * could only happen during mount.
5748 */
5749 ret = btrfs_find_highest_objectid(root,
5750 &root->highest_objectid);
5751 }
5752
Yan Zheng07d400a2009-01-06 11:42:00 -05005753 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005754 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005755 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005756 kfree(log);
5757
Josef Bacikb50c6e22013-04-25 15:55:30 -04005758 if (ret)
5759 goto error;
Josef Bacik7875fd32019-12-06 09:37:17 -05005760next:
Chris Masone02119d2008-09-05 16:13:11 -04005761 if (found_key.offset == 0)
5762 break;
Josef Bacik7875fd32019-12-06 09:37:17 -05005763 key.offset = found_key.offset - 1;
Chris Masone02119d2008-09-05 16:13:11 -04005764 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005765 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005766
5767 /* step one is to pin it all, step two is to replay just inodes */
5768 if (wc.pin) {
5769 wc.pin = 0;
5770 wc.process_func = replay_one_buffer;
5771 wc.stage = LOG_WALK_REPLAY_INODES;
5772 goto again;
5773 }
5774 /* step three is to replay everything */
5775 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5776 wc.stage++;
5777 goto again;
5778 }
5779
5780 btrfs_free_path(path);
5781
Josef Bacikabefa552013-04-24 16:40:05 -04005782 /* step 4: commit the transaction, which also unpins the blocks */
5783 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
5784 if (ret)
5785 return ret;
5786
Chris Masone02119d2008-09-05 16:13:11 -04005787 free_extent_buffer(log_root_tree->node);
5788 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005789 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005790 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005791
Josef Bacikabefa552013-04-24 16:40:05 -04005792 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005793error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005794 if (wc.trans)
5795 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005796 btrfs_free_path(path);
5797 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005798}
Chris Mason12fcfd22009-03-24 10:24:20 -04005799
5800/*
5801 * there are some corner cases where we want to force a full
5802 * commit instead of allowing a directory to be logged.
5803 *
5804 * They revolve around files there were unlinked from the directory, and
5805 * this function updates the parent directory so that a full commit is
5806 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005807 *
5808 * Must be called before the unlink operations (updates to the subvolume tree,
5809 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005810 */
5811void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
5812 struct inode *dir, struct inode *inode,
5813 int for_rename)
5814{
5815 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005816 * when we're logging a file, if it hasn't been renamed
5817 * or unlinked, and its inode is fully committed on disk,
5818 * we don't have to worry about walking up the directory chain
5819 * to log its parents.
5820 *
5821 * So, we use the last_unlink_trans field to put this transid
5822 * into the file. When the file is logged we check it and
5823 * don't log the parents if the file is fully on disk.
5824 */
Filipe Manana657ed1a2016-04-06 17:11:56 +01005825 mutex_lock(&BTRFS_I(inode)->log_mutex);
5826 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5827 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04005828
5829 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005830 * if this directory was already logged any new
5831 * names for this file/dir will get recorded
5832 */
5833 smp_mb();
5834 if (BTRFS_I(dir)->logged_trans == trans->transid)
5835 return;
5836
5837 /*
5838 * if the inode we're about to unlink was logged,
5839 * the log will be properly updated for any new names
5840 */
5841 if (BTRFS_I(inode)->logged_trans == trans->transid)
5842 return;
5843
5844 /*
5845 * when renaming files across directories, if the directory
5846 * there we're unlinking from gets fsync'd later on, there's
5847 * no way to find the destination directory later and fsync it
5848 * properly. So, we have to be conservative and force commits
5849 * so the new name gets discovered.
5850 */
5851 if (for_rename)
5852 goto record;
5853
5854 /* we can safely do the unlink without any special recording */
5855 return;
5856
5857record:
Filipe Manana2be63d52016-02-12 11:34:23 +00005858 mutex_lock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005859 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005860 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005861}
5862
5863/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005864 * Make sure that if someone attempts to fsync the parent directory of a deleted
5865 * snapshot, it ends up triggering a transaction commit. This is to guarantee
5866 * that after replaying the log tree of the parent directory's root we will not
5867 * see the snapshot anymore and at log replay time we will not see any log tree
5868 * corresponding to the deleted snapshot's root, which could lead to replaying
5869 * it after replaying the log tree of the parent directory (which would replay
5870 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00005871 *
5872 * Must be called before the actual snapshot destroy operation (updates to the
5873 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005874 */
5875void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
5876 struct inode *dir)
5877{
Filipe Manana2be63d52016-02-12 11:34:23 +00005878 mutex_lock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005879 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005880 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005881}
5882
5883/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005884 * Call this after adding a new name for a file and it will properly
5885 * update the log to reflect the new name.
5886 *
5887 * It will return zero if all goes well, and it will return 1 if a
5888 * full transaction commit is required.
5889 */
5890int btrfs_log_new_name(struct btrfs_trans_handle *trans,
5891 struct inode *inode, struct inode *old_dir,
5892 struct dentry *parent)
5893{
5894 struct btrfs_root * root = BTRFS_I(inode)->root;
5895
5896 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005897 * this will force the logging code to walk the dentry chain
5898 * up for the file
5899 */
5900 if (S_ISREG(inode->i_mode))
5901 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5902
5903 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005904 * if this inode hasn't been logged and directory we're renaming it
5905 * from hasn't been logged, we don't need to log it
5906 */
5907 if (BTRFS_I(inode)->logged_trans <=
5908 root->fs_info->last_trans_committed &&
5909 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
5910 root->fs_info->last_trans_committed))
5911 return 0;
5912
Filipe Manana49dae1b2014-09-06 22:34:39 +01005913 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
5914 LLONG_MAX, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04005915}
5916