blob: 44966fd0079072321e9bef370bb77ed20ba87a18 [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 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002830 * IO has been started, blocks of the log tree have WRITTEN flag set
2831 * in their headers. new modifications of the log will be written to
2832 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002833 */
2834 mutex_unlock(&root->log_mutex);
2835
Filipe Manana28a23592016-08-23 21:13:51 +01002836 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08002837
Yan Zheng7237f182009-01-21 12:54:03 -05002838 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002839 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002840 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002841
2842 index2 = log_root_tree->log_transid % 2;
2843 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2844 root_log_ctx.log_transid = log_root_tree->log_transid;
2845
Yan Zheng7237f182009-01-21 12:54:03 -05002846 mutex_unlock(&log_root_tree->log_mutex);
2847
2848 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002849
2850 mutex_lock(&log_root_tree->log_mutex);
2851 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +01002852 /*
2853 * Implicit memory barrier after atomic_dec_and_test
2854 */
Yan Zheng7237f182009-01-21 12:54:03 -05002855 if (waitqueue_active(&log_root_tree->log_writer_wait))
2856 wake_up(&log_root_tree->log_writer_wait);
2857 }
2858
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002859 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002860 if (!list_empty(&root_log_ctx.list))
2861 list_del_init(&root_log_ctx.list);
2862
Miao Xiec6adc9c2013-05-28 10:05:39 +00002863 blk_finish_plug(&plug);
Miao Xie995946d2014-04-02 19:51:06 +08002864 btrfs_set_log_full_commit(root->fs_info, trans);
2865
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002866 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002867 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002868 mutex_unlock(&log_root_tree->log_mutex);
2869 goto out;
2870 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002871 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002872 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002873 mutex_unlock(&log_root_tree->log_mutex);
2874 ret = -EAGAIN;
2875 goto out;
2876 }
2877
Miao Xied1433de2014-02-20 18:08:59 +08002878 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08002879 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07002880 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08002881 mutex_unlock(&log_root_tree->log_mutex);
2882 ret = root_log_ctx.log_ret;
2883 goto out;
2884 }
Miao Xie8b050d32014-02-20 18:08:58 +08002885
Miao Xied1433de2014-02-20 18:08:59 +08002886 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05002887 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002888 blk_finish_plug(&plug);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002889 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages,
2890 mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05002891 btrfs_wait_logged_extents(trans, log, log_transid);
Zhaolei60d53eb2015-08-17 18:44:46 +08002892 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002893 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002894 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002895 if (!ret)
2896 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002897 goto out;
2898 }
Miao Xied1433de2014-02-20 18:08:59 +08002899 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002900 atomic_set(&log_root_tree->log_commit[index2], 1);
2901
Chris Mason12fcfd22009-03-24 10:24:20 -04002902 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002903 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002904 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002905 }
Yan Zheng7237f182009-01-21 12:54:03 -05002906
Zhaolei60d53eb2015-08-17 18:44:46 +08002907 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04002908
2909 /*
2910 * now that we've moved on to the tree of log tree roots,
2911 * check the full commit flag again
2912 */
Miao Xie995946d2014-04-02 19:51:06 +08002913 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002914 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002915 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002916 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002917 mutex_unlock(&log_root_tree->log_mutex);
2918 ret = -EAGAIN;
2919 goto out_wake_log_root;
2920 }
Yan Zheng7237f182009-01-21 12:54:03 -05002921
Miao Xiec6adc9c2013-05-28 10:05:39 +00002922 ret = btrfs_write_marked_extents(log_root_tree,
2923 &log_root_tree->dirty_log_pages,
2924 EXTENT_DIRTY | EXTENT_NEW);
2925 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002926 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002927 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002928 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002929 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002930 mutex_unlock(&log_root_tree->log_mutex);
2931 goto out_wake_log_root;
2932 }
Filipe Manana5ab5e442014-11-13 16:59:53 +00002933 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2934 if (!ret)
2935 ret = btrfs_wait_marked_extents(log_root_tree,
2936 &log_root_tree->dirty_log_pages,
2937 EXTENT_NEW | EXTENT_DIRTY);
2938 if (ret) {
2939 btrfs_set_log_full_commit(root->fs_info, trans);
2940 btrfs_free_logged_extents(log, log_transid);
2941 mutex_unlock(&log_root_tree->log_mutex);
2942 goto out_wake_log_root;
2943 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05002944 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002945
David Sterba6c417612011-04-13 15:41:04 +02002946 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002947 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002948 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002949 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002950
Yan Zheng7237f182009-01-21 12:54:03 -05002951 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002952 mutex_unlock(&log_root_tree->log_mutex);
2953
2954 /*
2955 * nobody else is going to jump in and write the the ctree
2956 * super here because the log_commit atomic below is protecting
2957 * us. We must be called with a transaction handle pinning
2958 * the running transaction open, so a full commit can't hop
2959 * in and cause problems either.
2960 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002961 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002962 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002963 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002964 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002965 goto out_wake_log_root;
2966 }
Yan Zheng7237f182009-01-21 12:54:03 -05002967
Chris Mason257c62e2009-10-13 13:21:08 -04002968 mutex_lock(&root->log_mutex);
2969 if (root->last_log_commit < log_transid)
2970 root->last_log_commit = log_transid;
2971 mutex_unlock(&root->log_mutex);
2972
Chris Mason12fcfd22009-03-24 10:24:20 -04002973out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07002974 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002975 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2976
Miao Xied1433de2014-02-20 18:08:59 +08002977 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002978 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002979 mutex_unlock(&log_root_tree->log_mutex);
2980
David Sterba33a9eca2015-10-10 18:35:10 +02002981 /*
David Sterba65cb4692018-04-24 14:53:56 +02002982 * The barrier before waitqueue_active is needed so all the updates
2983 * above are seen by the woken threads. It might not be necessary, but
2984 * proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02002985 */
David Sterba65cb4692018-04-24 14:53:56 +02002986 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002987 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2988 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002989out:
Miao Xied1433de2014-02-20 18:08:59 +08002990 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07002991 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08002992 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002993 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002994 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002995
David Sterba33a9eca2015-10-10 18:35:10 +02002996 /*
David Sterba65cb4692018-04-24 14:53:56 +02002997 * The barrier before waitqueue_active is needed so all the updates
2998 * above are seen by the woken threads. It might not be necessary, but
2999 * proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003000 */
David Sterba65cb4692018-04-24 14:53:56 +02003001 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05003002 if (waitqueue_active(&root->log_commit_wait[index1]))
3003 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003004 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003005}
3006
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003007static void free_log_tree(struct btrfs_trans_handle *trans,
3008 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003009{
3010 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04003011 u64 start;
3012 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04003013 struct walk_control wc = {
3014 .free = 1,
3015 .process_func = process_one_buffer
3016 };
3017
Josef Bacik681ae502013-10-07 15:11:00 -04003018 ret = walk_log_tree(trans, log, &wc);
3019 /* I don't think this can happen but just in case */
3020 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003021 btrfs_abort_transaction(trans, ret);
Chris Masone02119d2008-09-05 16:13:11 -04003022
Chris Masond3977122009-01-05 21:25:51 -05003023 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003024 ret = find_first_extent_bit(&log->dirty_log_pages,
Liu Bobc0d4312018-01-25 11:02:52 -07003025 0, &start, &end,
3026 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
Josef Bacike6138872012-09-27 17:07:30 -04003027 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003028 if (ret)
3029 break;
3030
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003031 clear_extent_bits(&log->dirty_log_pages, start, end,
Liu Bobc0d4312018-01-25 11:02:52 -07003032 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Chris Masond0c803c2008-09-11 16:17:57 -04003033 }
3034
Josef Bacik2ab28f32012-10-12 15:27:49 -04003035 /*
3036 * We may have short-circuited the log tree with the full commit logic
3037 * and left ordered extents on our list, so clear these out to keep us
3038 * from leaking inodes and memory.
3039 */
3040 btrfs_free_logged_extents(log, 0);
3041 btrfs_free_logged_extents(log, 1);
3042
Yan Zheng7237f182009-01-21 12:54:03 -05003043 free_extent_buffer(log->node);
3044 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003045}
3046
3047/*
3048 * free all the extents used by the tree log. This should be called
3049 * at commit time of the full transaction
3050 */
3051int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3052{
3053 if (root->log_root) {
3054 free_log_tree(trans, root->log_root);
3055 root->log_root = NULL;
3056 }
3057 return 0;
3058}
3059
3060int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3061 struct btrfs_fs_info *fs_info)
3062{
3063 if (fs_info->log_root_tree) {
3064 free_log_tree(trans, fs_info->log_root_tree);
3065 fs_info->log_root_tree = NULL;
3066 }
Chris Masone02119d2008-09-05 16:13:11 -04003067 return 0;
3068}
3069
3070/*
Chris Masone02119d2008-09-05 16:13:11 -04003071 * If both a file and directory are logged, and unlinks or renames are
3072 * mixed in, we have a few interesting corners:
3073 *
3074 * create file X in dir Y
3075 * link file X to X.link in dir Y
3076 * fsync file X
3077 * unlink file X but leave X.link
3078 * fsync dir Y
3079 *
3080 * After a crash we would expect only X.link to exist. But file X
3081 * didn't get fsync'd again so the log has back refs for X and X.link.
3082 *
3083 * We solve this by removing directory entries and inode backrefs from the
3084 * log when a file that was logged in the current transaction is
3085 * unlinked. Any later fsync will include the updated log entries, and
3086 * we'll be able to reconstruct the proper directory items from backrefs.
3087 *
3088 * This optimizations allows us to avoid relogging the entire inode
3089 * or the entire directory.
3090 */
3091int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3092 struct btrfs_root *root,
3093 const char *name, int name_len,
3094 struct inode *dir, u64 index)
3095{
3096 struct btrfs_root *log;
3097 struct btrfs_dir_item *di;
3098 struct btrfs_path *path;
3099 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003100 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003101 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003102 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003103
Chris Mason3a5f1d42008-09-11 15:53:37 -04003104 if (BTRFS_I(dir)->logged_trans < trans->transid)
3105 return 0;
3106
Chris Masone02119d2008-09-05 16:13:11 -04003107 ret = join_running_log_trans(root);
3108 if (ret)
3109 return 0;
3110
3111 mutex_lock(&BTRFS_I(dir)->log_mutex);
3112
3113 log = root->log_root;
3114 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003115 if (!path) {
3116 err = -ENOMEM;
3117 goto out_unlock;
3118 }
liubo2a29edc2011-01-26 06:22:08 +00003119
Li Zefan33345d012011-04-20 10:31:50 +08003120 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003121 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003122 if (IS_ERR(di)) {
3123 err = PTR_ERR(di);
3124 goto fail;
3125 }
3126 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003127 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3128 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003129 if (ret) {
3130 err = ret;
3131 goto fail;
3132 }
Chris Masone02119d2008-09-05 16:13:11 -04003133 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003134 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003135 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003136 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003137 if (IS_ERR(di)) {
3138 err = PTR_ERR(di);
3139 goto fail;
3140 }
3141 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003142 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3143 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003144 if (ret) {
3145 err = ret;
3146 goto fail;
3147 }
Chris Masone02119d2008-09-05 16:13:11 -04003148 }
3149
3150 /* update the directory size in the log to reflect the names
3151 * we have removed
3152 */
3153 if (bytes_del) {
3154 struct btrfs_key key;
3155
Li Zefan33345d012011-04-20 10:31:50 +08003156 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003157 key.offset = 0;
3158 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003159 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003160
3161 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003162 if (ret < 0) {
3163 err = ret;
3164 goto fail;
3165 }
Chris Masone02119d2008-09-05 16:13:11 -04003166 if (ret == 0) {
3167 struct btrfs_inode_item *item;
3168 u64 i_size;
3169
3170 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3171 struct btrfs_inode_item);
3172 i_size = btrfs_inode_size(path->nodes[0], item);
3173 if (i_size > bytes_del)
3174 i_size -= bytes_del;
3175 else
3176 i_size = 0;
3177 btrfs_set_inode_size(path->nodes[0], item, i_size);
3178 btrfs_mark_buffer_dirty(path->nodes[0]);
3179 } else
3180 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003181 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003182 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003183fail:
Chris Masone02119d2008-09-05 16:13:11 -04003184 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003185out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04003186 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003187 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003188 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003189 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003190 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003191 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003192
Chris Mason12fcfd22009-03-24 10:24:20 -04003193 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003194
Andi Kleen411fc6b2010-10-29 15:14:31 -04003195 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003196}
3197
3198/* see comments for btrfs_del_dir_entries_in_log */
3199int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3200 struct btrfs_root *root,
3201 const char *name, int name_len,
3202 struct inode *inode, u64 dirid)
3203{
3204 struct btrfs_root *log;
3205 u64 index;
3206 int ret;
3207
Chris Mason3a5f1d42008-09-11 15:53:37 -04003208 if (BTRFS_I(inode)->logged_trans < trans->transid)
3209 return 0;
3210
Chris Masone02119d2008-09-05 16:13:11 -04003211 ret = join_running_log_trans(root);
3212 if (ret)
3213 return 0;
3214 log = root->log_root;
3215 mutex_lock(&BTRFS_I(inode)->log_mutex);
3216
Li Zefan33345d012011-04-20 10:31:50 +08003217 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003218 dirid, &index);
3219 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003220 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003221 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003222 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003223 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003224 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003225 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003226
Chris Masone02119d2008-09-05 16:13:11 -04003227 return ret;
3228}
3229
3230/*
3231 * creates a range item in the log for 'dirid'. first_offset and
3232 * last_offset tell us which parts of the key space the log should
3233 * be considered authoritative for.
3234 */
3235static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3236 struct btrfs_root *log,
3237 struct btrfs_path *path,
3238 int key_type, u64 dirid,
3239 u64 first_offset, u64 last_offset)
3240{
3241 int ret;
3242 struct btrfs_key key;
3243 struct btrfs_dir_log_item *item;
3244
3245 key.objectid = dirid;
3246 key.offset = first_offset;
3247 if (key_type == BTRFS_DIR_ITEM_KEY)
3248 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3249 else
3250 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3251 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003252 if (ret)
3253 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003254
3255 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3256 struct btrfs_dir_log_item);
3257 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3258 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003259 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003260 return 0;
3261}
3262
3263/*
3264 * log all the items included in the current transaction for a given
3265 * directory. This also creates the range items in the log tree required
3266 * to replay anything deleted before the fsync
3267 */
3268static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3269 struct btrfs_root *root, struct inode *inode,
3270 struct btrfs_path *path,
3271 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003272 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003273 u64 min_offset, u64 *last_offset_ret)
3274{
3275 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003276 struct btrfs_root *log = root->log_root;
3277 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003278 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003279 int ret;
3280 int i;
3281 int nritems;
3282 u64 first_offset = min_offset;
3283 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08003284 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003285
3286 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003287
Li Zefan33345d012011-04-20 10:31:50 +08003288 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003289 min_key.type = key_type;
3290 min_key.offset = min_offset;
3291
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003292 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003293
3294 /*
3295 * we didn't find anything from this transaction, see if there
3296 * is anything at all
3297 */
Li Zefan33345d012011-04-20 10:31:50 +08003298 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3299 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003300 min_key.type = key_type;
3301 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003302 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003303 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3304 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003305 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003306 return ret;
3307 }
Li Zefan33345d012011-04-20 10:31:50 +08003308 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003309
3310 /* if ret == 0 there are items for this type,
3311 * create a range to tell us the last key of this type.
3312 * otherwise, there are no items in this directory after
3313 * *min_offset, and we create a range to indicate that.
3314 */
3315 if (ret == 0) {
3316 struct btrfs_key tmp;
3317 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3318 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003319 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003320 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003321 }
3322 goto done;
3323 }
3324
3325 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003326 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003327 if (ret == 0) {
3328 struct btrfs_key tmp;
3329 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3330 if (key_type == tmp.type) {
3331 first_offset = tmp.offset;
3332 ret = overwrite_item(trans, log, dst_path,
3333 path->nodes[0], path->slots[0],
3334 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003335 if (ret) {
3336 err = ret;
3337 goto done;
3338 }
Chris Masone02119d2008-09-05 16:13:11 -04003339 }
3340 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003341 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003342
3343 /* find the first key from this transaction again */
3344 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303345 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003346 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003347
3348 /*
3349 * we have a block from this transaction, log every item in it
3350 * from our directory
3351 */
Chris Masond3977122009-01-05 21:25:51 -05003352 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003353 struct btrfs_key tmp;
3354 src = path->nodes[0];
3355 nritems = btrfs_header_nritems(src);
3356 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003357 struct btrfs_dir_item *di;
3358
Chris Masone02119d2008-09-05 16:13:11 -04003359 btrfs_item_key_to_cpu(src, &min_key, i);
3360
Li Zefan33345d012011-04-20 10:31:50 +08003361 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003362 goto done;
3363 ret = overwrite_item(trans, log, dst_path, src, i,
3364 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003365 if (ret) {
3366 err = ret;
3367 goto done;
3368 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003369
3370 /*
3371 * We must make sure that when we log a directory entry,
3372 * the corresponding inode, after log replay, has a
3373 * matching link count. For example:
3374 *
3375 * touch foo
3376 * mkdir mydir
3377 * sync
3378 * ln foo mydir/bar
3379 * xfs_io -c "fsync" mydir
3380 * <crash>
3381 * <mount fs and log replay>
3382 *
3383 * Would result in a fsync log that when replayed, our
3384 * file inode would have a link count of 1, but we get
3385 * two directory entries pointing to the same inode.
3386 * After removing one of the names, it would not be
3387 * possible to remove the other name, which resulted
3388 * always in stale file handle errors, and would not
3389 * be possible to rmdir the parent directory, since
3390 * its i_size could never decrement to the value
3391 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3392 */
3393 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3394 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3395 if (ctx &&
3396 (btrfs_dir_transid(src, di) == trans->transid ||
3397 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3398 tmp.type != BTRFS_ROOT_ITEM_KEY)
3399 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003400 }
3401 path->slots[0] = nritems;
3402
3403 /*
3404 * look ahead to the next item and see if it is also
3405 * from this directory and from this transaction
3406 */
3407 ret = btrfs_next_leaf(root, path);
Liu Bo14c4d5f2018-04-03 01:59:47 +08003408 if (ret) {
3409 if (ret == 1)
3410 last_offset = (u64)-1;
3411 else
3412 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003413 goto done;
3414 }
3415 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003416 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003417 last_offset = (u64)-1;
3418 goto done;
3419 }
3420 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3421 ret = overwrite_item(trans, log, dst_path,
3422 path->nodes[0], path->slots[0],
3423 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003424 if (ret)
3425 err = ret;
3426 else
3427 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003428 goto done;
3429 }
3430 }
3431done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003432 btrfs_release_path(path);
3433 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003434
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003435 if (err == 0) {
3436 *last_offset_ret = last_offset;
3437 /*
3438 * insert the log range keys to indicate where the log
3439 * is valid
3440 */
3441 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003442 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003443 if (ret)
3444 err = ret;
3445 }
3446 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003447}
3448
3449/*
3450 * logging directories is very similar to logging inodes, We find all the items
3451 * from the current transaction and write them to the log.
3452 *
3453 * The recovery code scans the directory in the subvolume, and if it finds a
3454 * key in the range logged that is not present in the log tree, then it means
3455 * that dir entry was unlinked during the transaction.
3456 *
3457 * In order for that scan to work, we must include one key smaller than
3458 * the smallest logged by this transaction and one key larger than the largest
3459 * key logged by this transaction.
3460 */
3461static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3462 struct btrfs_root *root, struct inode *inode,
3463 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003464 struct btrfs_path *dst_path,
3465 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003466{
3467 u64 min_key;
3468 u64 max_key;
3469 int ret;
3470 int key_type = BTRFS_DIR_ITEM_KEY;
3471
3472again:
3473 min_key = 0;
3474 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003475 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003476 ret = log_dir_items(trans, root, inode, path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003477 dst_path, key_type, ctx, min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003478 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003479 if (ret)
3480 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003481 if (max_key == (u64)-1)
3482 break;
3483 min_key = max_key + 1;
3484 }
3485
3486 if (key_type == BTRFS_DIR_ITEM_KEY) {
3487 key_type = BTRFS_DIR_INDEX_KEY;
3488 goto again;
3489 }
3490 return 0;
3491}
3492
3493/*
3494 * a helper function to drop items from the log before we relog an
3495 * inode. max_key_type indicates the highest item type to remove.
3496 * This cannot be run for file data extents because it does not
3497 * free the extents they point to.
3498 */
3499static int drop_objectid_items(struct btrfs_trans_handle *trans,
3500 struct btrfs_root *log,
3501 struct btrfs_path *path,
3502 u64 objectid, int max_key_type)
3503{
3504 int ret;
3505 struct btrfs_key key;
3506 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003507 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003508
3509 key.objectid = objectid;
3510 key.type = max_key_type;
3511 key.offset = (u64)-1;
3512
Chris Masond3977122009-01-05 21:25:51 -05003513 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003514 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003515 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003516 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003517 break;
3518
3519 if (path->slots[0] == 0)
3520 break;
3521
3522 path->slots[0]--;
3523 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3524 path->slots[0]);
3525
3526 if (found_key.objectid != objectid)
3527 break;
3528
Josef Bacik18ec90d2012-09-28 11:56:28 -04003529 found_key.offset = 0;
3530 found_key.type = 0;
3531 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3532 &start_slot);
3533
3534 ret = btrfs_del_items(trans, log, path, start_slot,
3535 path->slots[0] - start_slot + 1);
3536 /*
3537 * If start slot isn't 0 then we don't need to re-search, we've
3538 * found the last guy with the objectid in this tree.
3539 */
3540 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003541 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003542 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003543 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003544 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003545 if (ret > 0)
3546 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003547 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003548}
3549
Josef Bacik94edf4a2012-09-25 14:56:25 -04003550static void fill_inode_item(struct btrfs_trans_handle *trans,
3551 struct extent_buffer *leaf,
3552 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003553 struct inode *inode, int log_inode_only,
3554 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003555{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003556 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003557
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003558 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003559
3560 if (log_inode_only) {
3561 /* set the generation to zero so the recover code
3562 * can tell the difference between an logging
3563 * just to say 'this inode exists' and a logging
3564 * to say 'update this inode with these values'
3565 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003566 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003567 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003568 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003569 btrfs_set_token_inode_generation(leaf, item,
3570 BTRFS_I(inode)->generation,
3571 &token);
3572 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003573 }
3574
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003575 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3576 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3577 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3578 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3579
David Sterbaa937b972014-12-12 17:39:12 +01003580 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003581 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003582 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003583 inode->i_atime.tv_nsec, &token);
3584
David Sterbaa937b972014-12-12 17:39:12 +01003585 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003586 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003587 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003588 inode->i_mtime.tv_nsec, &token);
3589
David Sterbaa937b972014-12-12 17:39:12 +01003590 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003591 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003592 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003593 inode->i_ctime.tv_nsec, &token);
3594
3595 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3596 &token);
3597
3598 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3599 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3600 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3601 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3602 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003603}
3604
Josef Bacika95249b2012-10-11 16:17:34 -04003605static int log_inode_item(struct btrfs_trans_handle *trans,
3606 struct btrfs_root *log, struct btrfs_path *path,
3607 struct inode *inode)
3608{
3609 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003610 int ret;
3611
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003612 ret = btrfs_insert_empty_item(trans, log, path,
3613 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003614 sizeof(*inode_item));
3615 if (ret && ret != -EEXIST)
3616 return ret;
3617 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3618 struct btrfs_inode_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003619 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003620 btrfs_release_path(path);
3621 return 0;
3622}
3623
Chris Mason31ff1cd2008-09-11 16:17:57 -04003624static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003625 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003626 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003627 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003628 int start_slot, int nr, int inode_only,
3629 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003630{
3631 unsigned long src_offset;
3632 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003633 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003634 struct btrfs_file_extent_item *extent;
3635 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003636 struct extent_buffer *src = src_path->nodes[0];
3637 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003638 int ret;
3639 struct btrfs_key *ins_keys;
3640 u32 *ins_sizes;
3641 char *ins_data;
3642 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003643 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003644 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003645 bool has_extents = false;
Filipe Manana74121f72014-08-07 12:00:44 +01003646 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003647 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003648
3649 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003650
3651 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3652 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003653 if (!ins_data)
3654 return -ENOMEM;
3655
Josef Bacik16e75492013-10-22 12:18:51 -04003656 first_key.objectid = (u64)-1;
3657
Chris Mason31ff1cd2008-09-11 16:17:57 -04003658 ins_sizes = (u32 *)ins_data;
3659 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3660
3661 for (i = 0; i < nr; i++) {
3662 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3663 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3664 }
3665 ret = btrfs_insert_empty_items(trans, log, dst_path,
3666 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003667 if (ret) {
3668 kfree(ins_data);
3669 return ret;
3670 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003671
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003672 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003673 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3674 dst_path->slots[0]);
3675
3676 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3677
Matthias Kaehlckee7f42b02017-07-27 14:30:23 -07003678 if (i == nr - 1)
Josef Bacik16e75492013-10-22 12:18:51 -04003679 last_key = ins_keys[i];
3680
Josef Bacik94edf4a2012-09-25 14:56:25 -04003681 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003682 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3683 dst_path->slots[0],
3684 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003685 fill_inode_item(trans, dst_path->nodes[0], inode_item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003686 inode, inode_only == LOG_INODE_EXISTS,
3687 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003688 } else {
3689 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3690 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003691 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003692
Josef Bacik16e75492013-10-22 12:18:51 -04003693 /*
3694 * We set need_find_last_extent here in case we know we were
3695 * processing other items and then walk into the first extent in
3696 * the inode. If we don't hit an extent then nothing changes,
3697 * we'll do the last search the next time around.
3698 */
3699 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3700 has_extents = true;
Filipe Manana74121f72014-08-07 12:00:44 +01003701 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003702 first_key = ins_keys[i];
3703 } else {
3704 need_find_last_extent = false;
3705 }
3706
Chris Mason31ff1cd2008-09-11 16:17:57 -04003707 /* take a reference on file data extents so that truncates
3708 * or deletes of this inode don't have to relog the inode
3709 * again
3710 */
David Sterba962a2982014-06-04 18:41:45 +02003711 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003712 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003713 int found_type;
3714 extent = btrfs_item_ptr(src, start_slot + i,
3715 struct btrfs_file_extent_item);
3716
liubo8e531cd2011-05-06 10:36:09 +08003717 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3718 continue;
3719
Chris Mason31ff1cd2008-09-11 16:17:57 -04003720 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003721 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003722 u64 ds, dl, cs, cl;
3723 ds = btrfs_file_extent_disk_bytenr(src,
3724 extent);
3725 /* ds == 0 is a hole */
3726 if (ds == 0)
3727 continue;
3728
3729 dl = btrfs_file_extent_disk_num_bytes(src,
3730 extent);
3731 cs = btrfs_file_extent_offset(src, extent);
3732 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003733 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003734 if (btrfs_file_extent_compression(src,
3735 extent)) {
3736 cs = 0;
3737 cl = dl;
3738 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003739
3740 ret = btrfs_lookup_csums_range(
3741 log->fs_info->csum_root,
3742 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003743 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003744 if (ret) {
3745 btrfs_release_path(dst_path);
3746 kfree(ins_data);
3747 return ret;
3748 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003749 }
3750 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003751 }
3752
3753 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003754 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003755 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003756
3757 /*
3758 * we have to do this after the loop above to avoid changing the
3759 * log tree while trying to change the log tree.
3760 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003761 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003762 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003763 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3764 struct btrfs_ordered_sum,
3765 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003766 if (!ret)
3767 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003768 list_del(&sums->list);
3769 kfree(sums);
3770 }
Josef Bacik16e75492013-10-22 12:18:51 -04003771
3772 if (!has_extents)
3773 return ret;
3774
Filipe Manana74121f72014-08-07 12:00:44 +01003775 if (need_find_last_extent && *last_extent == first_key.offset) {
3776 /*
3777 * We don't have any leafs between our current one and the one
3778 * we processed before that can have file extent items for our
3779 * inode (and have a generation number smaller than our current
3780 * transaction id).
3781 */
3782 need_find_last_extent = false;
3783 }
3784
Josef Bacik16e75492013-10-22 12:18:51 -04003785 /*
3786 * Because we use btrfs_search_forward we could skip leaves that were
3787 * not modified and then assume *last_extent is valid when it really
3788 * isn't. So back up to the previous leaf and read the end of the last
3789 * extent before we go and fill in holes.
3790 */
3791 if (need_find_last_extent) {
3792 u64 len;
3793
3794 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3795 if (ret < 0)
3796 return ret;
3797 if (ret)
3798 goto fill_holes;
3799 if (src_path->slots[0])
3800 src_path->slots[0]--;
3801 src = src_path->nodes[0];
3802 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3803 if (key.objectid != btrfs_ino(inode) ||
3804 key.type != BTRFS_EXTENT_DATA_KEY)
3805 goto fill_holes;
3806 extent = btrfs_item_ptr(src, src_path->slots[0],
3807 struct btrfs_file_extent_item);
3808 if (btrfs_file_extent_type(src, extent) ==
3809 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003810 len = btrfs_file_extent_inline_len(src,
3811 src_path->slots[0],
3812 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003813 *last_extent = ALIGN(key.offset + len,
3814 log->sectorsize);
3815 } else {
3816 len = btrfs_file_extent_num_bytes(src, extent);
3817 *last_extent = key.offset + len;
3818 }
3819 }
3820fill_holes:
3821 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3822 * things could have happened
3823 *
3824 * 1) A merge could have happened, so we could currently be on a leaf
3825 * that holds what we were copying in the first place.
3826 * 2) A split could have happened, and now not all of the items we want
3827 * are on the same leaf.
3828 *
3829 * So we need to adjust how we search for holes, we need to drop the
3830 * path and re-search for the first extent key we found, and then walk
3831 * forward until we hit the last one we copied.
3832 */
3833 if (need_find_last_extent) {
3834 /* btrfs_prev_leaf could return 1 without releasing the path */
3835 btrfs_release_path(src_path);
3836 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3837 src_path, 0, 0);
3838 if (ret < 0)
3839 return ret;
3840 ASSERT(ret == 0);
3841 src = src_path->nodes[0];
3842 i = src_path->slots[0];
3843 } else {
3844 i = start_slot;
3845 }
3846
3847 /*
3848 * Ok so here we need to go through and fill in any holes we may have
3849 * to make sure that holes are punched for those areas in case they had
3850 * extents previously.
3851 */
3852 while (!done) {
3853 u64 offset, len;
3854 u64 extent_end;
3855
3856 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3857 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3858 if (ret < 0)
3859 return ret;
3860 ASSERT(ret == 0);
3861 src = src_path->nodes[0];
3862 i = 0;
Filipe Mananabee3c022018-03-26 23:59:12 +01003863 need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003864 }
3865
3866 btrfs_item_key_to_cpu(src, &key, i);
3867 if (!btrfs_comp_cpu_keys(&key, &last_key))
3868 done = true;
3869 if (key.objectid != btrfs_ino(inode) ||
3870 key.type != BTRFS_EXTENT_DATA_KEY) {
3871 i++;
3872 continue;
3873 }
3874 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3875 if (btrfs_file_extent_type(src, extent) ==
3876 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003877 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003878 extent_end = ALIGN(key.offset + len, log->sectorsize);
3879 } else {
3880 len = btrfs_file_extent_num_bytes(src, extent);
3881 extent_end = key.offset + len;
3882 }
3883 i++;
3884
3885 if (*last_extent == key.offset) {
3886 *last_extent = extent_end;
3887 continue;
3888 }
3889 offset = *last_extent;
3890 len = key.offset - *last_extent;
3891 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3892 offset, 0, 0, len, 0, len, 0,
3893 0, 0);
3894 if (ret)
3895 break;
Filipe Manana74121f72014-08-07 12:00:44 +01003896 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003897 }
3898 /*
3899 * Need to let the callers know we dropped the path so they should
3900 * re-search.
3901 */
3902 if (!ret && need_find_last_extent)
3903 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003904 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003905}
3906
Josef Bacik5dc562c2012-08-17 13:14:17 -04003907static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3908{
3909 struct extent_map *em1, *em2;
3910
3911 em1 = list_entry(a, struct extent_map, list);
3912 em2 = list_entry(b, struct extent_map, list);
3913
3914 if (em1->start < em2->start)
3915 return -1;
3916 else if (em1->start > em2->start)
3917 return 1;
3918 return 0;
3919}
3920
Filipe Manana8407f552014-09-05 15:14:39 +01003921static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3922 struct inode *inode,
3923 struct btrfs_root *root,
3924 const struct extent_map *em,
3925 const struct list_head *logged_list,
3926 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003927{
Josef Bacik2ab28f32012-10-12 15:27:49 -04003928 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01003929 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003930 u64 mod_start = em->mod_start;
3931 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003932 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003933 u64 csum_offset;
3934 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003935 LIST_HEAD(ordered_sums);
3936 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003937
Filipe Manana8407f552014-09-05 15:14:39 +01003938 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04003939
Filipe Manana8407f552014-09-05 15:14:39 +01003940 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3941 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003942 return 0;
3943
Josef Bacik2ab28f32012-10-12 15:27:49 -04003944 /*
Filipe Manana8407f552014-09-05 15:14:39 +01003945 * Wait far any ordered extent that covers our extent map. If it
3946 * finishes without an error, first check and see if our csums are on
3947 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04003948 */
Miao Xie827463c2014-01-14 20:31:51 +08003949 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003950 struct btrfs_ordered_sum *sum;
3951
3952 if (!mod_len)
3953 break;
3954
Josef Bacik2ab28f32012-10-12 15:27:49 -04003955 if (ordered->file_offset + ordered->len <= mod_start ||
3956 mod_start + mod_len <= ordered->file_offset)
3957 continue;
3958
Filipe Manana8407f552014-09-05 15:14:39 +01003959 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3960 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3961 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3962 const u64 start = ordered->file_offset;
3963 const u64 end = ordered->file_offset + ordered->len - 1;
3964
3965 WARN_ON(ordered->inode != inode);
3966 filemap_fdatawrite_range(inode->i_mapping, start, end);
3967 }
3968
3969 wait_event(ordered->wait,
3970 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3971 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3972
3973 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00003974 /*
3975 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3976 * i_mapping flags, so that the next fsync won't get
3977 * an outdated io error too.
3978 */
Miklos Szeredif0312212016-09-16 12:44:21 +02003979 filemap_check_errors(inode->i_mapping);
Filipe Manana8407f552014-09-05 15:14:39 +01003980 *ordered_io_error = true;
3981 break;
3982 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003983 /*
3984 * We are going to copy all the csums on this ordered extent, so
3985 * go ahead and adjust mod_start and mod_len in case this
3986 * ordered extent has already been logged.
3987 */
3988 if (ordered->file_offset > mod_start) {
3989 if (ordered->file_offset + ordered->len >=
3990 mod_start + mod_len)
3991 mod_len = ordered->file_offset - mod_start;
3992 /*
3993 * If we have this case
3994 *
3995 * |--------- logged extent ---------|
3996 * |----- ordered extent ----|
3997 *
3998 * Just don't mess with mod_start and mod_len, we'll
3999 * just end up logging more csums than we need and it
4000 * will be ok.
4001 */
4002 } else {
4003 if (ordered->file_offset + ordered->len <
4004 mod_start + mod_len) {
4005 mod_len = (mod_start + mod_len) -
4006 (ordered->file_offset + ordered->len);
4007 mod_start = ordered->file_offset +
4008 ordered->len;
4009 } else {
4010 mod_len = 0;
4011 }
4012 }
4013
Filipe Manana8407f552014-09-05 15:14:39 +01004014 if (skip_csum)
4015 continue;
4016
Josef Bacik2ab28f32012-10-12 15:27:49 -04004017 /*
4018 * To keep us from looping for the above case of an ordered
4019 * extent that falls inside of the logged extent.
4020 */
4021 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4022 &ordered->flags))
4023 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004024
Josef Bacik2ab28f32012-10-12 15:27:49 -04004025 list_for_each_entry(sum, &ordered->list, list) {
4026 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08004027 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01004028 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004029 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004030 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004031
Filipe Manana8407f552014-09-05 15:14:39 +01004032 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04004033 return ret;
4034
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004035 if (em->compress_type) {
4036 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004037 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004038 } else {
4039 csum_offset = mod_start - em->start;
4040 csum_len = mod_len;
4041 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004042
Josef Bacik70c8a912012-10-11 16:54:30 -04004043 /* block start is already adjusted for the file extent offset. */
4044 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
4045 em->block_start + csum_offset,
4046 em->block_start + csum_offset +
4047 csum_len - 1, &ordered_sums, 0);
4048 if (ret)
4049 return ret;
4050
4051 while (!list_empty(&ordered_sums)) {
4052 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4053 struct btrfs_ordered_sum,
4054 list);
4055 if (!ret)
4056 ret = btrfs_csum_file_blocks(trans, log, sums);
4057 list_del(&sums->list);
4058 kfree(sums);
4059 }
4060
4061 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004062}
4063
Filipe Manana8407f552014-09-05 15:14:39 +01004064static int log_one_extent(struct btrfs_trans_handle *trans,
4065 struct inode *inode, struct btrfs_root *root,
4066 const struct extent_map *em,
4067 struct btrfs_path *path,
4068 const struct list_head *logged_list,
4069 struct btrfs_log_ctx *ctx)
4070{
4071 struct btrfs_root *log = root->log_root;
4072 struct btrfs_file_extent_item *fi;
4073 struct extent_buffer *leaf;
4074 struct btrfs_map_token token;
4075 struct btrfs_key key;
4076 u64 extent_offset = em->start - em->orig_start;
4077 u64 block_len;
4078 int ret;
4079 int extent_inserted = 0;
4080 bool ordered_io_err = false;
4081
4082 ret = wait_ordered_extents(trans, inode, root, em, logged_list,
4083 &ordered_io_err);
4084 if (ret)
4085 return ret;
4086
4087 if (ordered_io_err) {
4088 ctx->io_err = -EIO;
4089 return 0;
4090 }
4091
4092 btrfs_init_map_token(&token);
4093
4094 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
4095 em->start + em->len, NULL, 0, 1,
4096 sizeof(*fi), &extent_inserted);
4097 if (ret)
4098 return ret;
4099
4100 if (!extent_inserted) {
4101 key.objectid = btrfs_ino(inode);
4102 key.type = BTRFS_EXTENT_DATA_KEY;
4103 key.offset = em->start;
4104
4105 ret = btrfs_insert_empty_item(trans, log, path, &key,
4106 sizeof(*fi));
4107 if (ret)
4108 return ret;
4109 }
4110 leaf = path->nodes[0];
4111 fi = btrfs_item_ptr(leaf, path->slots[0],
4112 struct btrfs_file_extent_item);
4113
Josef Bacik50d9aa92014-11-21 14:52:38 -05004114 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004115 &token);
4116 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4117 btrfs_set_token_file_extent_type(leaf, fi,
4118 BTRFS_FILE_EXTENT_PREALLOC,
4119 &token);
4120 else
4121 btrfs_set_token_file_extent_type(leaf, fi,
4122 BTRFS_FILE_EXTENT_REG,
4123 &token);
4124
4125 block_len = max(em->block_len, em->orig_block_len);
4126 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4127 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4128 em->block_start,
4129 &token);
4130 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4131 &token);
4132 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4133 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4134 em->block_start -
4135 extent_offset, &token);
4136 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4137 &token);
4138 } else {
4139 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4140 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4141 &token);
4142 }
4143
4144 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4145 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4146 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4147 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4148 &token);
4149 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4150 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4151 btrfs_mark_buffer_dirty(leaf);
4152
4153 btrfs_release_path(path);
4154
4155 return ret;
4156}
4157
Josef Bacik5dc562c2012-08-17 13:14:17 -04004158static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4159 struct btrfs_root *root,
4160 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004161 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004162 struct list_head *logged_list,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004163 struct btrfs_log_ctx *ctx,
4164 const u64 start,
4165 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004166{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004167 struct extent_map *em, *n;
4168 struct list_head extents;
4169 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4170 u64 test_gen;
4171 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004172 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004173
4174 INIT_LIST_HEAD(&extents);
4175
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004176 down_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004177 write_lock(&tree->lock);
4178 test_gen = root->fs_info->last_trans_committed;
4179
4180 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4181 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004182
4183 /*
4184 * Just an arbitrary number, this can be really CPU intensive
4185 * once we start getting a lot of extents, and really once we
4186 * have a bunch of extents we just want to commit since it will
4187 * be faster.
4188 */
4189 if (++num > 32768) {
4190 list_del_init(&tree->modified_extents);
4191 ret = -EFBIG;
4192 goto process;
4193 }
4194
Josef Bacik5dc562c2012-08-17 13:14:17 -04004195 if (em->generation <= test_gen)
4196 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004197 /* Need a ref to keep it from getting evicted from cache */
4198 atomic_inc(&em->refs);
4199 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004200 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004201 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004202 }
4203
4204 list_sort(NULL, &extents, extent_cmp);
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004205 btrfs_get_logged_extents(inode, logged_list, start, end);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004206 /*
4207 * Some ordered extents started by fsync might have completed
4208 * before we could collect them into the list logged_list, which
4209 * means they're gone, not in our logged_list nor in the inode's
4210 * ordered tree. We want the application/user space to know an
4211 * error happened while attempting to persist file data so that
4212 * it can take proper action. If such error happened, we leave
4213 * without writing to the log tree and the fsync must report the
4214 * file data write error and not commit the current transaction.
4215 */
Miklos Szeredif0312212016-09-16 12:44:21 +02004216 ret = filemap_check_errors(inode->i_mapping);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004217 if (ret)
4218 ctx->io_err = ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004219process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004220 while (!list_empty(&extents)) {
4221 em = list_entry(extents.next, struct extent_map, list);
4222
4223 list_del_init(&em->list);
4224
4225 /*
4226 * If we had an error we just need to delete everybody from our
4227 * private list.
4228 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004229 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004230 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004231 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004232 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004233 }
4234
4235 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004236
Filipe Manana8407f552014-09-05 15:14:39 +01004237 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4238 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004239 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004240 clear_em_logging(tree, em);
4241 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004242 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004243 WARN_ON(!list_empty(&extents));
4244 write_unlock(&tree->lock);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004245 up_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004246
Josef Bacik5dc562c2012-08-17 13:14:17 -04004247 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004248 return ret;
4249}
4250
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004251static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
4252 struct btrfs_path *path, u64 *size_ret)
4253{
4254 struct btrfs_key key;
4255 int ret;
4256
4257 key.objectid = btrfs_ino(inode);
4258 key.type = BTRFS_INODE_ITEM_KEY;
4259 key.offset = 0;
4260
4261 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4262 if (ret < 0) {
4263 return ret;
4264 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004265 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004266 } else {
4267 struct btrfs_inode_item *item;
4268
4269 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4270 struct btrfs_inode_item);
4271 *size_ret = btrfs_inode_size(path->nodes[0], item);
4272 }
4273
4274 btrfs_release_path(path);
4275 return 0;
4276}
4277
Filipe Manana36283bf2015-06-20 00:44:51 +01004278/*
4279 * At the moment we always log all xattrs. This is to figure out at log replay
4280 * time which xattrs must have their deletion replayed. If a xattr is missing
4281 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4282 * because if a xattr is deleted, the inode is fsynced and a power failure
4283 * happens, causing the log to be replayed the next time the fs is mounted,
4284 * we want the xattr to not exist anymore (same behaviour as other filesystems
4285 * with a journal, ext3/4, xfs, f2fs, etc).
4286 */
4287static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4288 struct btrfs_root *root,
4289 struct inode *inode,
4290 struct btrfs_path *path,
4291 struct btrfs_path *dst_path)
4292{
4293 int ret;
4294 struct btrfs_key key;
4295 const u64 ino = btrfs_ino(inode);
4296 int ins_nr = 0;
4297 int start_slot = 0;
4298
4299 key.objectid = ino;
4300 key.type = BTRFS_XATTR_ITEM_KEY;
4301 key.offset = 0;
4302
4303 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4304 if (ret < 0)
4305 return ret;
4306
4307 while (true) {
4308 int slot = path->slots[0];
4309 struct extent_buffer *leaf = path->nodes[0];
4310 int nritems = btrfs_header_nritems(leaf);
4311
4312 if (slot >= nritems) {
4313 if (ins_nr > 0) {
4314 u64 last_extent = 0;
4315
4316 ret = copy_items(trans, inode, dst_path, path,
4317 &last_extent, start_slot,
4318 ins_nr, 1, 0);
4319 /* can't be 1, extent items aren't processed */
4320 ASSERT(ret <= 0);
4321 if (ret < 0)
4322 return ret;
4323 ins_nr = 0;
4324 }
4325 ret = btrfs_next_leaf(root, path);
4326 if (ret < 0)
4327 return ret;
4328 else if (ret > 0)
4329 break;
4330 continue;
4331 }
4332
4333 btrfs_item_key_to_cpu(leaf, &key, slot);
4334 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4335 break;
4336
4337 if (ins_nr == 0)
4338 start_slot = slot;
4339 ins_nr++;
4340 path->slots[0]++;
4341 cond_resched();
4342 }
4343 if (ins_nr > 0) {
4344 u64 last_extent = 0;
4345
4346 ret = copy_items(trans, inode, dst_path, path,
4347 &last_extent, start_slot,
4348 ins_nr, 1, 0);
4349 /* can't be 1, extent items aren't processed */
4350 ASSERT(ret <= 0);
4351 if (ret < 0)
4352 return ret;
4353 }
4354
4355 return 0;
4356}
4357
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004358/*
4359 * If the no holes feature is enabled we need to make sure any hole between the
4360 * last extent and the i_size of our inode is explicitly marked in the log. This
4361 * is to make sure that doing something like:
4362 *
4363 * 1) create file with 128Kb of data
4364 * 2) truncate file to 64Kb
4365 * 3) truncate file to 256Kb
4366 * 4) fsync file
4367 * 5) <crash/power failure>
4368 * 6) mount fs and trigger log replay
4369 *
4370 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4371 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4372 * file correspond to a hole. The presence of explicit holes in a log tree is
4373 * what guarantees that log replay will remove/adjust file extent items in the
4374 * fs/subvol tree.
4375 *
4376 * Here we do not need to care about holes between extents, that is already done
4377 * by copy_items(). We also only need to do this in the full sync path, where we
4378 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4379 * lookup the list of modified extent maps and if any represents a hole, we
4380 * insert a corresponding extent representing a hole in the log tree.
4381 */
4382static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4383 struct btrfs_root *root,
4384 struct inode *inode,
4385 struct btrfs_path *path)
4386{
4387 int ret;
4388 struct btrfs_key key;
4389 u64 hole_start;
4390 u64 hole_size;
4391 struct extent_buffer *leaf;
4392 struct btrfs_root *log = root->log_root;
4393 const u64 ino = btrfs_ino(inode);
4394 const u64 i_size = i_size_read(inode);
4395
4396 if (!btrfs_fs_incompat(root->fs_info, NO_HOLES))
4397 return 0;
4398
4399 key.objectid = ino;
4400 key.type = BTRFS_EXTENT_DATA_KEY;
4401 key.offset = (u64)-1;
4402
4403 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4404 ASSERT(ret != 0);
4405 if (ret < 0)
4406 return ret;
4407
4408 ASSERT(path->slots[0] > 0);
4409 path->slots[0]--;
4410 leaf = path->nodes[0];
4411 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4412
4413 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4414 /* inode does not have any extents */
4415 hole_start = 0;
4416 hole_size = i_size;
4417 } else {
4418 struct btrfs_file_extent_item *extent;
4419 u64 len;
4420
4421 /*
4422 * If there's an extent beyond i_size, an explicit hole was
4423 * already inserted by copy_items().
4424 */
4425 if (key.offset >= i_size)
4426 return 0;
4427
4428 extent = btrfs_item_ptr(leaf, path->slots[0],
4429 struct btrfs_file_extent_item);
4430
4431 if (btrfs_file_extent_type(leaf, extent) ==
4432 BTRFS_FILE_EXTENT_INLINE) {
4433 len = btrfs_file_extent_inline_len(leaf,
4434 path->slots[0],
4435 extent);
4436 ASSERT(len == i_size);
4437 return 0;
4438 }
4439
4440 len = btrfs_file_extent_num_bytes(leaf, extent);
4441 /* Last extent goes beyond i_size, no need to log a hole. */
4442 if (key.offset + len > i_size)
4443 return 0;
4444 hole_start = key.offset + len;
4445 hole_size = i_size - hole_start;
4446 }
4447 btrfs_release_path(path);
4448
4449 /* Last extent ends at i_size. */
4450 if (hole_size == 0)
4451 return 0;
4452
4453 hole_size = ALIGN(hole_size, root->sectorsize);
4454 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4455 hole_size, 0, hole_size, 0, 0, 0);
4456 return ret;
4457}
4458
Filipe Manana56f23fd2016-03-30 23:37:21 +01004459/*
4460 * When we are logging a new inode X, check if it doesn't have a reference that
4461 * matches the reference from some other inode Y created in a past transaction
4462 * and that was renamed in the current transaction. If we don't do this, then at
4463 * log replay time we can lose inode Y (and all its files if it's a directory):
4464 *
4465 * mkdir /mnt/x
4466 * echo "hello world" > /mnt/x/foobar
4467 * sync
4468 * mv /mnt/x /mnt/y
4469 * mkdir /mnt/x # or touch /mnt/x
4470 * xfs_io -c fsync /mnt/x
4471 * <power fail>
4472 * mount fs, trigger log replay
4473 *
4474 * After the log replay procedure, we would lose the first directory and all its
4475 * files (file foobar).
4476 * For the case where inode Y is not a directory we simply end up losing it:
4477 *
4478 * echo "123" > /mnt/foo
4479 * sync
4480 * mv /mnt/foo /mnt/bar
4481 * echo "abc" > /mnt/foo
4482 * xfs_io -c fsync /mnt/foo
4483 * <power fail>
4484 *
4485 * We also need this for cases where a snapshot entry is replaced by some other
4486 * entry (file or directory) otherwise we end up with an unreplayable log due to
4487 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4488 * if it were a regular entry:
4489 *
4490 * mkdir /mnt/x
4491 * btrfs subvolume snapshot /mnt /mnt/x/snap
4492 * btrfs subvolume delete /mnt/x/snap
4493 * rmdir /mnt/x
4494 * mkdir /mnt/x
4495 * fsync /mnt/x or fsync some new file inside it
4496 * <power fail>
4497 *
4498 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4499 * the same transaction.
4500 */
4501static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4502 const int slot,
4503 const struct btrfs_key *key,
Filipe Manana44f714d2016-06-06 16:11:13 +01004504 struct inode *inode,
4505 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004506{
4507 int ret;
4508 struct btrfs_path *search_path;
4509 char *name = NULL;
4510 u32 name_len = 0;
4511 u32 item_size = btrfs_item_size_nr(eb, slot);
4512 u32 cur_offset = 0;
4513 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4514
4515 search_path = btrfs_alloc_path();
4516 if (!search_path)
4517 return -ENOMEM;
4518 search_path->search_commit_root = 1;
4519 search_path->skip_locking = 1;
4520
4521 while (cur_offset < item_size) {
4522 u64 parent;
4523 u32 this_name_len;
4524 u32 this_len;
4525 unsigned long name_ptr;
4526 struct btrfs_dir_item *di;
4527
4528 if (key->type == BTRFS_INODE_REF_KEY) {
4529 struct btrfs_inode_ref *iref;
4530
4531 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4532 parent = key->offset;
4533 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4534 name_ptr = (unsigned long)(iref + 1);
4535 this_len = sizeof(*iref) + this_name_len;
4536 } else {
4537 struct btrfs_inode_extref *extref;
4538
4539 extref = (struct btrfs_inode_extref *)(ptr +
4540 cur_offset);
4541 parent = btrfs_inode_extref_parent(eb, extref);
4542 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4543 name_ptr = (unsigned long)&extref->name;
4544 this_len = sizeof(*extref) + this_name_len;
4545 }
4546
4547 if (this_name_len > name_len) {
4548 char *new_name;
4549
4550 new_name = krealloc(name, this_name_len, GFP_NOFS);
4551 if (!new_name) {
4552 ret = -ENOMEM;
4553 goto out;
4554 }
4555 name_len = this_name_len;
4556 name = new_name;
4557 }
4558
4559 read_extent_buffer(eb, name, name_ptr, this_name_len);
4560 di = btrfs_lookup_dir_item(NULL, BTRFS_I(inode)->root,
4561 search_path, parent,
4562 name, this_name_len, 0);
4563 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004564 struct btrfs_key di_key;
4565
4566 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4567 di, &di_key);
4568 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4569 ret = 1;
4570 *other_ino = di_key.objectid;
4571 } else {
4572 ret = -EAGAIN;
4573 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004574 goto out;
4575 } else if (IS_ERR(di)) {
4576 ret = PTR_ERR(di);
4577 goto out;
4578 }
4579 btrfs_release_path(search_path);
4580
4581 cur_offset += this_len;
4582 }
4583 ret = 0;
4584out:
4585 btrfs_free_path(search_path);
4586 kfree(name);
4587 return ret;
4588}
4589
Chris Masone02119d2008-09-05 16:13:11 -04004590/* log a single inode in the tree log.
4591 * At least one parent directory for this inode must exist in the tree
4592 * or be logged already.
4593 *
4594 * Any items from this inode changed by the current transaction are copied
4595 * to the log tree. An extra reference is taken on any extents in this
4596 * file, allowing us to avoid a whole pile of corner cases around logging
4597 * blocks that have been removed from the tree.
4598 *
4599 * See LOG_INODE_ALL and related defines for a description of what inode_only
4600 * does.
4601 *
4602 * This handles both files and directories.
4603 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004604static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004605 struct btrfs_root *root, struct inode *inode,
4606 int inode_only,
4607 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004608 const loff_t end,
4609 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004610{
4611 struct btrfs_path *path;
4612 struct btrfs_path *dst_path;
4613 struct btrfs_key min_key;
4614 struct btrfs_key max_key;
4615 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004616 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08004617 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004618 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004619 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004620 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004621 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004622 int ins_start_slot = 0;
4623 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004624 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08004625 u64 ino = btrfs_ino(inode);
Filipe Manana49dae1b2014-09-06 22:34:39 +01004626 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004627 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004628 bool need_log_inode_item = true;
Filipe Manana92291242018-05-11 16:42:42 +01004629 bool xattrs_logged = false;
Chris Masone02119d2008-09-05 16:13:11 -04004630
Chris Masone02119d2008-09-05 16:13:11 -04004631 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004632 if (!path)
4633 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004634 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004635 if (!dst_path) {
4636 btrfs_free_path(path);
4637 return -ENOMEM;
4638 }
Chris Masone02119d2008-09-05 16:13:11 -04004639
Li Zefan33345d012011-04-20 10:31:50 +08004640 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004641 min_key.type = BTRFS_INODE_ITEM_KEY;
4642 min_key.offset = 0;
4643
Li Zefan33345d012011-04-20 10:31:50 +08004644 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004645
Chris Mason12fcfd22009-03-24 10:24:20 -04004646
Josef Bacik5dc562c2012-08-17 13:14:17 -04004647 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00004648 if (S_ISDIR(inode->i_mode) ||
4649 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4650 &BTRFS_I(inode)->runtime_flags) &&
Liu Bo67312122016-11-30 16:20:25 -08004651 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004652 max_key.type = BTRFS_XATTR_ITEM_KEY;
4653 else
4654 max_key.type = (u8)-1;
4655 max_key.offset = (u64)-1;
4656
Filipe Manana2c2c4522015-01-13 16:40:04 +00004657 /*
4658 * Only run delayed items if we are a dir or a new file.
4659 * Otherwise commit the delayed inode only, which is needed in
4660 * order for the log replay code to mark inodes for link count
4661 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4662 */
Josef Bacik94edf4a2012-09-25 14:56:25 -04004663 if (S_ISDIR(inode->i_mode) ||
Filipe Manana2c2c4522015-01-13 16:40:04 +00004664 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
Josef Bacik94edf4a2012-09-25 14:56:25 -04004665 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004666 else
4667 ret = btrfs_commit_inode_delayed_inode(inode);
4668
4669 if (ret) {
4670 btrfs_free_path(path);
4671 btrfs_free_path(dst_path);
4672 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004673 }
4674
Liu Bo67312122016-11-30 16:20:25 -08004675 if (inode_only == LOG_OTHER_INODE) {
4676 inode_only = LOG_INODE_EXISTS;
4677 mutex_lock_nested(&BTRFS_I(inode)->log_mutex,
4678 SINGLE_DEPTH_NESTING);
4679 } else {
4680 mutex_lock(&BTRFS_I(inode)->log_mutex);
4681 }
Chris Masone02119d2008-09-05 16:13:11 -04004682
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004683 /*
Chris Masone02119d2008-09-05 16:13:11 -04004684 * a brute force approach to making sure we get the most uptodate
4685 * copies of everything.
4686 */
4687 if (S_ISDIR(inode->i_mode)) {
4688 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4689
Filipe Manana4f764e52015-02-23 19:53:35 +00004690 if (inode_only == LOG_INODE_EXISTS)
4691 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004692 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004693 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004694 if (inode_only == LOG_INODE_EXISTS) {
4695 /*
4696 * Make sure the new inode item we write to the log has
4697 * the same isize as the current one (if it exists).
4698 * This is necessary to prevent data loss after log
4699 * replay, and also to prevent doing a wrong expanding
4700 * truncate - for e.g. create file, write 4K into offset
4701 * 0, fsync, write 4K into offset 4096, add hard link,
4702 * fsync some other file (to sync log), power fail - if
4703 * we use the inode's current i_size, after log replay
4704 * we get a 8Kb file, with the last 4Kb extent as a hole
4705 * (zeroes), as if an expanding truncate happened,
4706 * instead of getting a file of 4Kb only.
4707 */
4708 err = logged_inode_size(log, inode, path,
4709 &logged_isize);
4710 if (err)
4711 goto out_unlock;
4712 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004713 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4714 &BTRFS_I(inode)->runtime_flags)) {
4715 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004716 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004717 ret = drop_objectid_items(trans, log, path, ino,
4718 max_key.type);
4719 } else {
4720 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4721 &BTRFS_I(inode)->runtime_flags);
4722 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4723 &BTRFS_I(inode)->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004724 while(1) {
4725 ret = btrfs_truncate_inode_items(trans,
4726 log, inode, 0, 0);
4727 if (ret != -EAGAIN)
4728 break;
4729 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004730 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004731 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4732 &BTRFS_I(inode)->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004733 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004734 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004735 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004736 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004737 ret = drop_objectid_items(trans, log, path, ino,
4738 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004739 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004740 if (inode_only == LOG_INODE_ALL)
4741 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004742 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004743 }
Josef Bacika95249b2012-10-11 16:17:34 -04004744
Chris Masone02119d2008-09-05 16:13:11 -04004745 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004746 if (ret) {
4747 err = ret;
4748 goto out_unlock;
4749 }
Chris Masone02119d2008-09-05 16:13:11 -04004750
Chris Masond3977122009-01-05 21:25:51 -05004751 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004752 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004753 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004754 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004755 if (ret < 0) {
4756 err = ret;
4757 goto out_unlock;
4758 }
Chris Masone02119d2008-09-05 16:13:11 -04004759 if (ret != 0)
4760 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004761again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004762 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004763 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004764 break;
4765 if (min_key.type > max_key.type)
4766 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004767
Filipe Mananae4545de2015-06-17 12:49:23 +01004768 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4769 need_log_inode_item = false;
4770
Filipe Manana56f23fd2016-03-30 23:37:21 +01004771 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4772 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
4773 BTRFS_I(inode)->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004774 u64 other_ino = 0;
4775
Filipe Manana56f23fd2016-03-30 23:37:21 +01004776 ret = btrfs_check_ref_name_override(path->nodes[0],
4777 path->slots[0],
Filipe Manana44f714d2016-06-06 16:11:13 +01004778 &min_key, inode,
4779 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004780 if (ret < 0) {
4781 err = ret;
4782 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004783 } else if (ret > 0 && ctx &&
4784 other_ino != btrfs_ino(ctx->inode)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004785 struct btrfs_key inode_key;
4786 struct inode *other_inode;
4787
4788 if (ins_nr > 0) {
4789 ins_nr++;
4790 } else {
4791 ins_nr = 1;
4792 ins_start_slot = path->slots[0];
4793 }
4794 ret = copy_items(trans, inode, dst_path, path,
4795 &last_extent, ins_start_slot,
4796 ins_nr, inode_only,
4797 logged_isize);
4798 if (ret < 0) {
4799 err = ret;
4800 goto out_unlock;
4801 }
4802 ins_nr = 0;
4803 btrfs_release_path(path);
4804 inode_key.objectid = other_ino;
4805 inode_key.type = BTRFS_INODE_ITEM_KEY;
4806 inode_key.offset = 0;
4807 other_inode = btrfs_iget(root->fs_info->sb,
4808 &inode_key, root,
4809 NULL);
4810 /*
4811 * If the other inode that had a conflicting dir
4812 * entry was deleted in the current transaction,
4813 * we don't need to do more work nor fallback to
4814 * a transaction commit.
4815 */
4816 if (IS_ERR(other_inode) &&
4817 PTR_ERR(other_inode) == -ENOENT) {
4818 goto next_key;
4819 } else if (IS_ERR(other_inode)) {
4820 err = PTR_ERR(other_inode);
4821 goto out_unlock;
4822 }
4823 /*
4824 * We are safe logging the other inode without
4825 * acquiring its i_mutex as long as we log with
4826 * the LOG_INODE_EXISTS mode. We're safe against
4827 * concurrent renames of the other inode as well
4828 * because during a rename we pin the log and
4829 * update the log with the new name before we
4830 * unpin it.
4831 */
4832 err = btrfs_log_inode(trans, root, other_inode,
Liu Bo67312122016-11-30 16:20:25 -08004833 LOG_OTHER_INODE,
Filipe Manana44f714d2016-06-06 16:11:13 +01004834 0, LLONG_MAX, ctx);
4835 iput(other_inode);
4836 if (err)
4837 goto out_unlock;
4838 else
4839 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01004840 }
4841 }
4842
Filipe Manana36283bf2015-06-20 00:44:51 +01004843 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
4844 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
4845 if (ins_nr == 0)
4846 goto next_slot;
4847 ret = copy_items(trans, inode, dst_path, path,
4848 &last_extent, ins_start_slot,
4849 ins_nr, inode_only, logged_isize);
4850 if (ret < 0) {
4851 err = ret;
4852 goto out_unlock;
4853 }
4854 ins_nr = 0;
4855 if (ret) {
4856 btrfs_release_path(path);
4857 continue;
4858 }
4859 goto next_slot;
4860 }
4861
Chris Masone02119d2008-09-05 16:13:11 -04004862 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004863 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4864 ins_nr++;
4865 goto next_slot;
4866 } else if (!ins_nr) {
4867 ins_start_slot = path->slots[0];
4868 ins_nr = 1;
4869 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004870 }
4871
Josef Bacik16e75492013-10-22 12:18:51 -04004872 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004873 ins_start_slot, ins_nr, inode_only,
4874 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004875 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004876 err = ret;
4877 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02004878 }
4879 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04004880 ins_nr = 0;
4881 btrfs_release_path(path);
4882 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004883 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004884 ins_nr = 1;
4885 ins_start_slot = path->slots[0];
4886next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004887
Chris Mason3a5f1d42008-09-11 15:53:37 -04004888 nritems = btrfs_header_nritems(path->nodes[0]);
4889 path->slots[0]++;
4890 if (path->slots[0] < nritems) {
4891 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4892 path->slots[0]);
4893 goto again;
4894 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004895 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004896 ret = copy_items(trans, inode, dst_path, path,
4897 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004898 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004899 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004900 err = ret;
4901 goto out_unlock;
4902 }
Josef Bacik16e75492013-10-22 12:18:51 -04004903 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004904 ins_nr = 0;
4905 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004906 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01004907next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004908 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004909 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004910 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004911 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004912 min_key.offset = 0;
4913 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004914 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004915 }
Chris Masone02119d2008-09-05 16:13:11 -04004916 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004917 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004918 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004919 ins_start_slot, ins_nr, inode_only,
4920 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004921 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004922 err = ret;
4923 goto out_unlock;
4924 }
Josef Bacik16e75492013-10-22 12:18:51 -04004925 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004926 ins_nr = 0;
4927 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004928
Filipe Manana36283bf2015-06-20 00:44:51 +01004929 btrfs_release_path(path);
4930 btrfs_release_path(dst_path);
4931 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
4932 if (err)
4933 goto out_unlock;
Filipe Manana92291242018-05-11 16:42:42 +01004934 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004935 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
4936 btrfs_release_path(path);
4937 btrfs_release_path(dst_path);
4938 err = btrfs_log_trailing_hole(trans, root, inode, path);
4939 if (err)
4940 goto out_unlock;
4941 }
Josef Bacika95249b2012-10-11 16:17:34 -04004942log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004943 btrfs_release_path(path);
4944 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01004945 if (need_log_inode_item) {
4946 err = log_inode_item(trans, log, dst_path, inode);
Filipe Manana92291242018-05-11 16:42:42 +01004947 if (!err && !xattrs_logged) {
4948 err = btrfs_log_all_xattrs(trans, root, inode, path,
4949 dst_path);
4950 btrfs_release_path(path);
4951 }
Filipe Mananae4545de2015-06-17 12:49:23 +01004952 if (err)
4953 goto out_unlock;
4954 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004955 if (fast_search) {
Miao Xie827463c2014-01-14 20:31:51 +08004956 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004957 &logged_list, ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004958 if (ret) {
4959 err = ret;
4960 goto out_unlock;
4961 }
Josef Bacikd006a042013-11-12 20:54:09 -05004962 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004963 struct extent_map *em, *n;
4964
Filipe Manana49dae1b2014-09-06 22:34:39 +01004965 write_lock(&em_tree->lock);
4966 /*
4967 * We can't just remove every em if we're called for a ranged
4968 * fsync - that is, one that doesn't cover the whole possible
4969 * file range (0 to LLONG_MAX). This is because we can have
4970 * em's that fall outside the range we're logging and therefore
4971 * their ordered operations haven't completed yet
4972 * (btrfs_finish_ordered_io() not invoked yet). This means we
4973 * didn't get their respective file extent item in the fs/subvol
4974 * tree yet, and need to let the next fast fsync (one which
4975 * consults the list of modified extent maps) find the em so
4976 * that it logs a matching file extent item and waits for the
4977 * respective ordered operation to complete (if it's still
4978 * running).
4979 *
4980 * Removing every em outside the range we're logging would make
4981 * the next fast fsync not log their matching file extent items,
4982 * therefore making us lose data after a log replay.
4983 */
4984 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4985 list) {
4986 const u64 mod_end = em->mod_start + em->mod_len - 1;
4987
4988 if (em->mod_start >= start && mod_end <= end)
4989 list_del_init(&em->list);
4990 }
4991 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004992 }
4993
Chris Mason9623f9a2008-09-11 17:42:42 -04004994 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004995 ret = log_directory_changes(trans, root, inode, path, dst_path,
4996 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004997 if (ret) {
4998 err = ret;
4999 goto out_unlock;
5000 }
Chris Masone02119d2008-09-05 16:13:11 -04005001 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005002
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005003 spin_lock(&BTRFS_I(inode)->lock);
Filipe Manana125c4cf92014-09-11 21:22:14 +01005004 BTRFS_I(inode)->logged_trans = trans->transid;
5005 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005006 spin_unlock(&BTRFS_I(inode)->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005007out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08005008 if (unlikely(err))
5009 btrfs_put_logged_extents(&logged_list);
5010 else
5011 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04005012 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5013
5014 btrfs_free_path(path);
5015 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005016 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005017}
5018
Chris Mason12fcfd22009-03-24 10:24:20 -04005019/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005020 * Check if we must fallback to a transaction commit when logging an inode.
5021 * This must be called after logging the inode and is used only in the context
5022 * when fsyncing an inode requires the need to log some other inode - in which
5023 * case we can't lock the i_mutex of each other inode we need to log as that
5024 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5025 * log inodes up or down in the hierarchy) or rename operations for example. So
5026 * we take the log_mutex of the inode after we have logged it and then check for
5027 * its last_unlink_trans value - this is safe because any task setting
5028 * last_unlink_trans must take the log_mutex and it must do this before it does
5029 * the actual unlink operation, so if we do this check before a concurrent task
5030 * sets last_unlink_trans it means we've logged a consistent version/state of
5031 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005032 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005033 * we logged the inode or it might have also done the unlink).
5034 */
5035static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5036 struct inode *inode)
5037{
5038 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
5039 bool ret = false;
5040
5041 mutex_lock(&BTRFS_I(inode)->log_mutex);
5042 if (BTRFS_I(inode)->last_unlink_trans > fs_info->last_trans_committed) {
5043 /*
5044 * Make sure any commits to the log are forced to be full
5045 * commits.
5046 */
5047 btrfs_set_log_full_commit(fs_info, trans);
5048 ret = true;
5049 }
5050 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5051
5052 return ret;
5053}
5054
5055/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005056 * follow the dentry parent pointers up the chain and see if any
5057 * of the directories in it require a full commit before they can
5058 * be logged. Returns zero if nothing special needs to be done or 1 if
5059 * a full commit is required.
5060 */
5061static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5062 struct inode *inode,
5063 struct dentry *parent,
5064 struct super_block *sb,
5065 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005066{
Chris Mason12fcfd22009-03-24 10:24:20 -04005067 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005068 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04005069 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005070
Chris Masonaf4176b2009-03-24 10:24:31 -04005071 /*
5072 * for regular files, if its inode is already on disk, we don't
5073 * have to worry about the parents at all. This is because
5074 * we can use the last_unlink_trans field to record renames
5075 * and other fun in this file.
5076 */
5077 if (S_ISREG(inode->i_mode) &&
5078 BTRFS_I(inode)->generation <= last_committed &&
5079 BTRFS_I(inode)->last_unlink_trans <= last_committed)
5080 goto out;
5081
Chris Mason12fcfd22009-03-24 10:24:20 -04005082 if (!S_ISDIR(inode->i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005083 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005084 goto out;
David Howells2b0143b2015-03-17 22:25:59 +00005085 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005086 }
5087
5088 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005089 /*
5090 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005091 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005092 * logged_trans so that further down in the log code we don't
5093 * think this inode has already been logged.
5094 */
5095 if (inode != orig_inode)
5096 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005097 smp_mb();
5098
Filipe Manana2be63d52016-02-12 11:34:23 +00005099 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005100 ret = 1;
5101 break;
5102 }
5103
Al Virofc640052016-04-10 01:33:30 -04005104 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005105 break;
5106
Filipe Manana44f714d2016-06-06 16:11:13 +01005107 if (IS_ROOT(parent)) {
5108 inode = d_inode(parent);
5109 if (btrfs_must_commit_transaction(trans, inode))
5110 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005111 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005112 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005113
Josef Bacik6a912212010-11-20 09:48:00 +00005114 parent = dget_parent(parent);
5115 dput(old_parent);
5116 old_parent = parent;
David Howells2b0143b2015-03-17 22:25:59 +00005117 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005118
5119 }
Josef Bacik6a912212010-11-20 09:48:00 +00005120 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005121out:
Chris Masone02119d2008-09-05 16:13:11 -04005122 return ret;
5123}
5124
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005125struct btrfs_dir_list {
5126 u64 ino;
5127 struct list_head list;
5128};
5129
5130/*
5131 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5132 * details about the why it is needed.
5133 * This is a recursive operation - if an existing dentry corresponds to a
5134 * directory, that directory's new entries are logged too (same behaviour as
5135 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5136 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5137 * complains about the following circular lock dependency / possible deadlock:
5138 *
5139 * CPU0 CPU1
5140 * ---- ----
5141 * lock(&type->i_mutex_dir_key#3/2);
5142 * lock(sb_internal#2);
5143 * lock(&type->i_mutex_dir_key#3/2);
5144 * lock(&sb->s_type->i_mutex_key#14);
5145 *
5146 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5147 * sb_start_intwrite() in btrfs_start_transaction().
5148 * Not locking i_mutex of the inodes is still safe because:
5149 *
5150 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5151 * that while logging the inode new references (names) are added or removed
5152 * from the inode, leaving the logged inode item with a link count that does
5153 * not match the number of logged inode reference items. This is fine because
5154 * at log replay time we compute the real number of links and correct the
5155 * link count in the inode item (see replay_one_buffer() and
5156 * link_to_fixup_dir());
5157 *
5158 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5159 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5160 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5161 * has a size that doesn't match the sum of the lengths of all the logged
5162 * names. This does not result in a problem because if a dir_item key is
5163 * logged but its matching dir_index key is not logged, at log replay time we
5164 * don't use it to replay the respective name (see replay_one_name()). On the
5165 * other hand if only the dir_index key ends up being logged, the respective
5166 * name is added to the fs/subvol tree with both the dir_item and dir_index
5167 * keys created (see replay_one_name()).
5168 * The directory's inode item with a wrong i_size is not a problem as well,
5169 * since we don't use it at log replay time to set the i_size in the inode
5170 * item of the fs/subvol tree (see overwrite_item()).
5171 */
5172static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5173 struct btrfs_root *root,
5174 struct inode *start_inode,
5175 struct btrfs_log_ctx *ctx)
5176{
5177 struct btrfs_root *log = root->log_root;
5178 struct btrfs_path *path;
5179 LIST_HEAD(dir_list);
5180 struct btrfs_dir_list *dir_elem;
5181 int ret = 0;
5182
5183 path = btrfs_alloc_path();
5184 if (!path)
5185 return -ENOMEM;
5186
5187 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5188 if (!dir_elem) {
5189 btrfs_free_path(path);
5190 return -ENOMEM;
5191 }
5192 dir_elem->ino = btrfs_ino(start_inode);
5193 list_add_tail(&dir_elem->list, &dir_list);
5194
5195 while (!list_empty(&dir_list)) {
5196 struct extent_buffer *leaf;
5197 struct btrfs_key min_key;
5198 int nritems;
5199 int i;
5200
5201 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5202 list);
5203 if (ret)
5204 goto next_dir_inode;
5205
5206 min_key.objectid = dir_elem->ino;
5207 min_key.type = BTRFS_DIR_ITEM_KEY;
5208 min_key.offset = 0;
5209again:
5210 btrfs_release_path(path);
5211 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5212 if (ret < 0) {
5213 goto next_dir_inode;
5214 } else if (ret > 0) {
5215 ret = 0;
5216 goto next_dir_inode;
5217 }
5218
5219process_leaf:
5220 leaf = path->nodes[0];
5221 nritems = btrfs_header_nritems(leaf);
5222 for (i = path->slots[0]; i < nritems; i++) {
5223 struct btrfs_dir_item *di;
5224 struct btrfs_key di_key;
5225 struct inode *di_inode;
5226 struct btrfs_dir_list *new_dir_elem;
5227 int log_mode = LOG_INODE_EXISTS;
5228 int type;
5229
5230 btrfs_item_key_to_cpu(leaf, &min_key, i);
5231 if (min_key.objectid != dir_elem->ino ||
5232 min_key.type != BTRFS_DIR_ITEM_KEY)
5233 goto next_dir_inode;
5234
5235 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5236 type = btrfs_dir_type(leaf, di);
5237 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5238 type != BTRFS_FT_DIR)
5239 continue;
5240 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5241 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5242 continue;
5243
Robbie Ko664b0532016-10-28 10:48:26 +08005244 btrfs_release_path(path);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005245 di_inode = btrfs_iget(root->fs_info->sb, &di_key,
5246 root, NULL);
5247 if (IS_ERR(di_inode)) {
5248 ret = PTR_ERR(di_inode);
5249 goto next_dir_inode;
5250 }
5251
5252 if (btrfs_inode_in_log(di_inode, trans->transid)) {
5253 iput(di_inode);
Robbie Ko664b0532016-10-28 10:48:26 +08005254 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005255 }
5256
5257 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005258 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005259 log_mode = LOG_INODE_ALL;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005260 ret = btrfs_log_inode(trans, root, di_inode,
5261 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005262 if (!ret &&
5263 btrfs_must_commit_transaction(trans, di_inode))
5264 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005265 iput(di_inode);
5266 if (ret)
5267 goto next_dir_inode;
5268 if (ctx->log_new_dentries) {
5269 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5270 GFP_NOFS);
5271 if (!new_dir_elem) {
5272 ret = -ENOMEM;
5273 goto next_dir_inode;
5274 }
5275 new_dir_elem->ino = di_key.objectid;
5276 list_add_tail(&new_dir_elem->list, &dir_list);
5277 }
5278 break;
5279 }
5280 if (i == nritems) {
5281 ret = btrfs_next_leaf(log, path);
5282 if (ret < 0) {
5283 goto next_dir_inode;
5284 } else if (ret > 0) {
5285 ret = 0;
5286 goto next_dir_inode;
5287 }
5288 goto process_leaf;
5289 }
5290 if (min_key.offset < (u64)-1) {
5291 min_key.offset++;
5292 goto again;
5293 }
5294next_dir_inode:
5295 list_del(&dir_elem->list);
5296 kfree(dir_elem);
5297 }
5298
5299 btrfs_free_path(path);
5300 return ret;
5301}
5302
Filipe Manana18aa0922015-08-05 16:49:08 +01005303static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5304 struct inode *inode,
5305 struct btrfs_log_ctx *ctx)
5306{
5307 int ret;
5308 struct btrfs_path *path;
5309 struct btrfs_key key;
5310 struct btrfs_root *root = BTRFS_I(inode)->root;
5311 const u64 ino = btrfs_ino(inode);
5312
5313 path = btrfs_alloc_path();
5314 if (!path)
5315 return -ENOMEM;
5316 path->skip_locking = 1;
5317 path->search_commit_root = 1;
5318
5319 key.objectid = ino;
5320 key.type = BTRFS_INODE_REF_KEY;
5321 key.offset = 0;
5322 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5323 if (ret < 0)
5324 goto out;
5325
5326 while (true) {
5327 struct extent_buffer *leaf = path->nodes[0];
5328 int slot = path->slots[0];
5329 u32 cur_offset = 0;
5330 u32 item_size;
5331 unsigned long ptr;
5332
5333 if (slot >= btrfs_header_nritems(leaf)) {
5334 ret = btrfs_next_leaf(root, path);
5335 if (ret < 0)
5336 goto out;
5337 else if (ret > 0)
5338 break;
5339 continue;
5340 }
5341
5342 btrfs_item_key_to_cpu(leaf, &key, slot);
5343 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5344 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5345 break;
5346
5347 item_size = btrfs_item_size_nr(leaf, slot);
5348 ptr = btrfs_item_ptr_offset(leaf, slot);
5349 while (cur_offset < item_size) {
5350 struct btrfs_key inode_key;
5351 struct inode *dir_inode;
5352
5353 inode_key.type = BTRFS_INODE_ITEM_KEY;
5354 inode_key.offset = 0;
5355
5356 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5357 struct btrfs_inode_extref *extref;
5358
5359 extref = (struct btrfs_inode_extref *)
5360 (ptr + cur_offset);
5361 inode_key.objectid = btrfs_inode_extref_parent(
5362 leaf, extref);
5363 cur_offset += sizeof(*extref);
5364 cur_offset += btrfs_inode_extref_name_len(leaf,
5365 extref);
5366 } else {
5367 inode_key.objectid = key.offset;
5368 cur_offset = item_size;
5369 }
5370
5371 dir_inode = btrfs_iget(root->fs_info->sb, &inode_key,
5372 root, NULL);
5373 /* If parent inode was deleted, skip it. */
5374 if (IS_ERR(dir_inode))
5375 continue;
5376
Filipe Manana657ed1a2016-04-06 17:11:56 +01005377 if (ctx)
5378 ctx->log_new_dentries = false;
Filipe Manana18aa0922015-08-05 16:49:08 +01005379 ret = btrfs_log_inode(trans, root, dir_inode,
5380 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005381 if (!ret &&
5382 btrfs_must_commit_transaction(trans, dir_inode))
5383 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005384 if (!ret && ctx && ctx->log_new_dentries)
5385 ret = log_new_dir_dentries(trans, root,
5386 dir_inode, ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005387 iput(dir_inode);
5388 if (ret)
5389 goto out;
5390 }
5391 path->slots[0]++;
5392 }
5393 ret = 0;
5394out:
5395 btrfs_free_path(path);
5396 return ret;
5397}
5398
Chris Masone02119d2008-09-05 16:13:11 -04005399/*
5400 * helper function around btrfs_log_inode to make sure newly created
5401 * parent directories also end up in the log. A minimal inode and backref
5402 * only logging is done of any parent directories that are older than
5403 * the last committed transaction
5404 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005405static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5406 struct btrfs_root *root, struct inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005407 struct dentry *parent,
5408 const loff_t start,
5409 const loff_t end,
5410 int exists_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005411 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005412{
Chris Mason12fcfd22009-03-24 10:24:20 -04005413 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04005414 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005415 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005416 int ret = 0;
5417 u64 last_committed = root->fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005418 bool log_dentries = false;
5419 struct inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005420
5421 sb = inode->i_sb;
5422
Jeff Mahoney3cdde222016-06-09 21:38:35 -04005423 if (btrfs_test_opt(root->fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005424 ret = 1;
5425 goto end_no_trans;
5426 }
5427
Miao Xie995946d2014-04-02 19:51:06 +08005428 /*
5429 * The prev transaction commit doesn't complete, we need do
5430 * full commit by ourselves.
5431 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005432 if (root->fs_info->last_trans_log_full_commit >
5433 root->fs_info->last_trans_committed) {
5434 ret = 1;
5435 goto end_no_trans;
5436 }
5437
Yan, Zheng76dda932009-09-21 16:00:26 -04005438 if (root != BTRFS_I(inode)->root ||
5439 btrfs_root_refs(&root->root_item) == 0) {
5440 ret = 1;
5441 goto end_no_trans;
5442 }
5443
Chris Mason12fcfd22009-03-24 10:24:20 -04005444 ret = check_parent_dirs_for_sync(trans, inode, parent,
5445 sb, last_committed);
5446 if (ret)
5447 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005448
Josef Bacik22ee6982012-05-29 16:57:49 -04005449 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005450 ret = BTRFS_NO_LOG_SYNC;
5451 goto end_no_trans;
5452 }
5453
Miao Xie8b050d32014-02-20 18:08:58 +08005454 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005455 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005456 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005457
Filipe Manana8407f552014-09-05 15:14:39 +01005458 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005459 if (ret)
5460 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005461
Chris Masonaf4176b2009-03-24 10:24:31 -04005462 /*
5463 * for regular files, if its inode is already on disk, we don't
5464 * have to worry about the parents at all. This is because
5465 * we can use the last_unlink_trans field to record renames
5466 * and other fun in this file.
5467 */
5468 if (S_ISREG(inode->i_mode) &&
5469 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005470 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
5471 ret = 0;
5472 goto end_trans;
5473 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005474
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005475 if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
5476 log_dentries = true;
5477
Filipe Manana18aa0922015-08-05 16:49:08 +01005478 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005479 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005480 * inodes are fully logged. This is to prevent leaving dangling
5481 * directory index entries in directories that were our parents but are
5482 * not anymore. Not doing this results in old parent directory being
5483 * impossible to delete after log replay (rmdir will always fail with
5484 * error -ENOTEMPTY).
5485 *
5486 * Example 1:
5487 *
5488 * mkdir testdir
5489 * touch testdir/foo
5490 * ln testdir/foo testdir/bar
5491 * sync
5492 * unlink testdir/bar
5493 * xfs_io -c fsync testdir/foo
5494 * <power failure>
5495 * mount fs, triggers log replay
5496 *
5497 * If we don't log the parent directory (testdir), after log replay the
5498 * directory still has an entry pointing to the file inode using the bar
5499 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5500 * the file inode has a link count of 1.
5501 *
5502 * Example 2:
5503 *
5504 * mkdir testdir
5505 * touch foo
5506 * ln foo testdir/foo2
5507 * ln foo testdir/foo3
5508 * sync
5509 * unlink testdir/foo3
5510 * xfs_io -c fsync foo
5511 * <power failure>
5512 * mount fs, triggers log replay
5513 *
5514 * Similar as the first example, after log replay the parent directory
5515 * testdir still has an entry pointing to the inode file with name foo3
5516 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5517 * and has a link count of 2.
5518 */
5519 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
5520 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5521 if (ret)
5522 goto end_trans;
5523 }
5524
Chris Masond3977122009-01-05 21:25:51 -05005525 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005526 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005527 break;
5528
David Howells2b0143b2015-03-17 22:25:59 +00005529 inode = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005530 if (root != BTRFS_I(inode)->root)
5531 break;
5532
Filipe Manana18aa0922015-08-05 16:49:08 +01005533 if (BTRFS_I(inode)->generation > last_committed) {
5534 ret = btrfs_log_inode(trans, root, inode,
5535 LOG_INODE_EXISTS,
Filipe Manana8407f552014-09-05 15:14:39 +01005536 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005537 if (ret)
5538 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005539 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005540 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005541 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005542
Josef Bacik6a912212010-11-20 09:48:00 +00005543 parent = dget_parent(parent);
5544 dput(old_parent);
5545 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005546 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005547 if (log_dentries)
5548 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5549 else
5550 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005551end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005552 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005553 if (ret < 0) {
Miao Xie995946d2014-04-02 19:51:06 +08005554 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005555 ret = 1;
5556 }
Miao Xie8b050d32014-02-20 18:08:58 +08005557
5558 if (ret)
5559 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005560 btrfs_end_log_trans(root);
5561end_no_trans:
5562 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005563}
5564
5565/*
5566 * it is not safe to log dentry if the chunk root has added new
5567 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5568 * If this returns 1, you must commit the transaction to safely get your
5569 * data on disk.
5570 */
5571int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08005572 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005573 const loff_t start,
5574 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005575 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005576{
Josef Bacik6a912212010-11-20 09:48:00 +00005577 struct dentry *parent = dget_parent(dentry);
5578 int ret;
5579
David Howells2b0143b2015-03-17 22:25:59 +00005580 ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005581 start, end, 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005582 dput(parent);
5583
5584 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005585}
5586
5587/*
5588 * should be called during mount to recover any replay any log trees
5589 * from the FS
5590 */
5591int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5592{
5593 int ret;
5594 struct btrfs_path *path;
5595 struct btrfs_trans_handle *trans;
5596 struct btrfs_key key;
5597 struct btrfs_key found_key;
5598 struct btrfs_key tmp_key;
5599 struct btrfs_root *log;
5600 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5601 struct walk_control wc = {
5602 .process_func = process_one_buffer,
5603 .stage = 0,
5604 };
5605
Chris Masone02119d2008-09-05 16:13:11 -04005606 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005607 if (!path)
5608 return -ENOMEM;
5609
Josef Bacikafcdd122016-09-02 15:40:02 -04005610 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005611
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005612 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005613 if (IS_ERR(trans)) {
5614 ret = PTR_ERR(trans);
5615 goto error;
5616 }
Chris Masone02119d2008-09-05 16:13:11 -04005617
5618 wc.trans = trans;
5619 wc.pin = 1;
5620
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005621 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005622 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005623 btrfs_handle_fs_error(fs_info, ret,
5624 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005625 goto error;
5626 }
Chris Masone02119d2008-09-05 16:13:11 -04005627
5628again:
5629 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5630 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005631 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005632
Chris Masond3977122009-01-05 21:25:51 -05005633 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005634 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005635
5636 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005637 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005638 "Couldn't find tree log root.");
5639 goto error;
5640 }
Chris Masone02119d2008-09-05 16:13:11 -04005641 if (ret > 0) {
5642 if (path->slots[0] == 0)
5643 break;
5644 path->slots[0]--;
5645 }
5646 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5647 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005648 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005649 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5650 break;
5651
Miao Xiecb517ea2013-05-15 07:48:19 +00005652 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005653 if (IS_ERR(log)) {
5654 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005655 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005656 "Couldn't read tree log root.");
5657 goto error;
5658 }
Chris Masone02119d2008-09-05 16:13:11 -04005659
5660 tmp_key.objectid = found_key.offset;
5661 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5662 tmp_key.offset = (u64)-1;
5663
5664 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005665 if (IS_ERR(wc.replay_dest)) {
5666 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005667 free_extent_buffer(log->node);
5668 free_extent_buffer(log->commit_root);
5669 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005670 btrfs_handle_fs_error(fs_info, ret,
5671 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005672 goto error;
5673 }
Chris Masone02119d2008-09-05 16:13:11 -04005674
Yan Zheng07d400a2009-01-06 11:42:00 -05005675 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005676 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005677 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005678
Josef Bacikb50c6e22013-04-25 15:55:30 -04005679 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005680 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5681 path);
Chris Masone02119d2008-09-05 16:13:11 -04005682 }
Chris Masone02119d2008-09-05 16:13:11 -04005683
Liu Boefb1cbc2018-01-25 11:02:56 -07005684 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5685 struct btrfs_root *root = wc.replay_dest;
5686
5687 btrfs_release_path(path);
5688
5689 /*
5690 * We have just replayed everything, and the highest
5691 * objectid of fs roots probably has changed in case
5692 * some inode_item's got replayed.
5693 *
5694 * root->objectid_mutex is not acquired as log replay
5695 * could only happen during mount.
5696 */
5697 ret = btrfs_find_highest_objectid(root,
5698 &root->highest_objectid);
5699 }
5700
Chris Masone02119d2008-09-05 16:13:11 -04005701 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005702 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005703 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005704 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005705 kfree(log);
5706
Josef Bacikb50c6e22013-04-25 15:55:30 -04005707 if (ret)
5708 goto error;
5709
Chris Masone02119d2008-09-05 16:13:11 -04005710 if (found_key.offset == 0)
5711 break;
5712 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005713 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005714
5715 /* step one is to pin it all, step two is to replay just inodes */
5716 if (wc.pin) {
5717 wc.pin = 0;
5718 wc.process_func = replay_one_buffer;
5719 wc.stage = LOG_WALK_REPLAY_INODES;
5720 goto again;
5721 }
5722 /* step three is to replay everything */
5723 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5724 wc.stage++;
5725 goto again;
5726 }
5727
5728 btrfs_free_path(path);
5729
Josef Bacikabefa552013-04-24 16:40:05 -04005730 /* step 4: commit the transaction, which also unpins the blocks */
5731 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
5732 if (ret)
5733 return ret;
5734
Chris Masone02119d2008-09-05 16:13:11 -04005735 free_extent_buffer(log_root_tree->node);
5736 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005737 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005738 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005739
Josef Bacikabefa552013-04-24 16:40:05 -04005740 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005741error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005742 if (wc.trans)
5743 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005744 btrfs_free_path(path);
5745 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005746}
Chris Mason12fcfd22009-03-24 10:24:20 -04005747
5748/*
5749 * there are some corner cases where we want to force a full
5750 * commit instead of allowing a directory to be logged.
5751 *
5752 * They revolve around files there were unlinked from the directory, and
5753 * this function updates the parent directory so that a full commit is
5754 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005755 *
5756 * Must be called before the unlink operations (updates to the subvolume tree,
5757 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005758 */
5759void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
5760 struct inode *dir, struct inode *inode,
5761 int for_rename)
5762{
5763 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005764 * when we're logging a file, if it hasn't been renamed
5765 * or unlinked, and its inode is fully committed on disk,
5766 * we don't have to worry about walking up the directory chain
5767 * to log its parents.
5768 *
5769 * So, we use the last_unlink_trans field to put this transid
5770 * into the file. When the file is logged we check it and
5771 * don't log the parents if the file is fully on disk.
5772 */
Filipe Manana657ed1a2016-04-06 17:11:56 +01005773 mutex_lock(&BTRFS_I(inode)->log_mutex);
5774 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5775 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04005776
5777 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005778 * if this directory was already logged any new
5779 * names for this file/dir will get recorded
5780 */
5781 smp_mb();
5782 if (BTRFS_I(dir)->logged_trans == trans->transid)
5783 return;
5784
5785 /*
5786 * if the inode we're about to unlink was logged,
5787 * the log will be properly updated for any new names
5788 */
5789 if (BTRFS_I(inode)->logged_trans == trans->transid)
5790 return;
5791
5792 /*
5793 * when renaming files across directories, if the directory
5794 * there we're unlinking from gets fsync'd later on, there's
5795 * no way to find the destination directory later and fsync it
5796 * properly. So, we have to be conservative and force commits
5797 * so the new name gets discovered.
5798 */
5799 if (for_rename)
5800 goto record;
5801
5802 /* we can safely do the unlink without any special recording */
5803 return;
5804
5805record:
Filipe Manana2be63d52016-02-12 11:34:23 +00005806 mutex_lock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005807 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005808 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005809}
5810
5811/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005812 * Make sure that if someone attempts to fsync the parent directory of a deleted
5813 * snapshot, it ends up triggering a transaction commit. This is to guarantee
5814 * that after replaying the log tree of the parent directory's root we will not
5815 * see the snapshot anymore and at log replay time we will not see any log tree
5816 * corresponding to the deleted snapshot's root, which could lead to replaying
5817 * it after replaying the log tree of the parent directory (which would replay
5818 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00005819 *
5820 * Must be called before the actual snapshot destroy operation (updates to the
5821 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005822 */
5823void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
5824 struct inode *dir)
5825{
Filipe Manana2be63d52016-02-12 11:34:23 +00005826 mutex_lock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005827 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005828 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005829}
5830
5831/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005832 * Call this after adding a new name for a file and it will properly
5833 * update the log to reflect the new name.
5834 *
5835 * It will return zero if all goes well, and it will return 1 if a
5836 * full transaction commit is required.
5837 */
5838int btrfs_log_new_name(struct btrfs_trans_handle *trans,
5839 struct inode *inode, struct inode *old_dir,
5840 struct dentry *parent)
5841{
5842 struct btrfs_root * root = BTRFS_I(inode)->root;
5843
5844 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005845 * this will force the logging code to walk the dentry chain
5846 * up for the file
5847 */
5848 if (S_ISREG(inode->i_mode))
5849 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5850
5851 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005852 * if this inode hasn't been logged and directory we're renaming it
5853 * from hasn't been logged, we don't need to log it
5854 */
5855 if (BTRFS_I(inode)->logged_trans <=
5856 root->fs_info->last_trans_committed &&
5857 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
5858 root->fs_info->last_trans_committed))
5859 return 0;
5860
Filipe Manana49dae1b2014-09-06 22:34:39 +01005861 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
5862 LLONG_MAX, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04005863}
5864