blob: 44d34923de9c775cd6ba4516e3e433b7e1ccc1ad [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 /*
2982 * The barrier before waitqueue_active is implied by mutex_unlock
2983 */
Yan Zheng7237f182009-01-21 12:54:03 -05002984 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2985 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002986out:
Miao Xied1433de2014-02-20 18:08:59 +08002987 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07002988 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08002989 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002990 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002991 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002992
David Sterba33a9eca2015-10-10 18:35:10 +02002993 /*
2994 * The barrier before waitqueue_active is implied by mutex_unlock
2995 */
Yan Zheng7237f182009-01-21 12:54:03 -05002996 if (waitqueue_active(&root->log_commit_wait[index1]))
2997 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002998 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002999}
3000
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003001static void free_log_tree(struct btrfs_trans_handle *trans,
3002 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003003{
3004 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04003005 u64 start;
3006 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04003007 struct walk_control wc = {
3008 .free = 1,
3009 .process_func = process_one_buffer
3010 };
3011
Josef Bacik681ae502013-10-07 15:11:00 -04003012 ret = walk_log_tree(trans, log, &wc);
3013 /* I don't think this can happen but just in case */
3014 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003015 btrfs_abort_transaction(trans, ret);
Chris Masone02119d2008-09-05 16:13:11 -04003016
Chris Masond3977122009-01-05 21:25:51 -05003017 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003018 ret = find_first_extent_bit(&log->dirty_log_pages,
Liu Bobc0d4312018-01-25 11:02:52 -07003019 0, &start, &end,
3020 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
Josef Bacike6138872012-09-27 17:07:30 -04003021 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003022 if (ret)
3023 break;
3024
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003025 clear_extent_bits(&log->dirty_log_pages, start, end,
Liu Bobc0d4312018-01-25 11:02:52 -07003026 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Chris Masond0c803c2008-09-11 16:17:57 -04003027 }
3028
Josef Bacik2ab28f32012-10-12 15:27:49 -04003029 /*
3030 * We may have short-circuited the log tree with the full commit logic
3031 * and left ordered extents on our list, so clear these out to keep us
3032 * from leaking inodes and memory.
3033 */
3034 btrfs_free_logged_extents(log, 0);
3035 btrfs_free_logged_extents(log, 1);
3036
Yan Zheng7237f182009-01-21 12:54:03 -05003037 free_extent_buffer(log->node);
3038 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003039}
3040
3041/*
3042 * free all the extents used by the tree log. This should be called
3043 * at commit time of the full transaction
3044 */
3045int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3046{
3047 if (root->log_root) {
3048 free_log_tree(trans, root->log_root);
3049 root->log_root = NULL;
3050 }
3051 return 0;
3052}
3053
3054int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3055 struct btrfs_fs_info *fs_info)
3056{
3057 if (fs_info->log_root_tree) {
3058 free_log_tree(trans, fs_info->log_root_tree);
3059 fs_info->log_root_tree = NULL;
3060 }
Chris Masone02119d2008-09-05 16:13:11 -04003061 return 0;
3062}
3063
3064/*
Chris Masone02119d2008-09-05 16:13:11 -04003065 * If both a file and directory are logged, and unlinks or renames are
3066 * mixed in, we have a few interesting corners:
3067 *
3068 * create file X in dir Y
3069 * link file X to X.link in dir Y
3070 * fsync file X
3071 * unlink file X but leave X.link
3072 * fsync dir Y
3073 *
3074 * After a crash we would expect only X.link to exist. But file X
3075 * didn't get fsync'd again so the log has back refs for X and X.link.
3076 *
3077 * We solve this by removing directory entries and inode backrefs from the
3078 * log when a file that was logged in the current transaction is
3079 * unlinked. Any later fsync will include the updated log entries, and
3080 * we'll be able to reconstruct the proper directory items from backrefs.
3081 *
3082 * This optimizations allows us to avoid relogging the entire inode
3083 * or the entire directory.
3084 */
3085int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3086 struct btrfs_root *root,
3087 const char *name, int name_len,
3088 struct inode *dir, u64 index)
3089{
3090 struct btrfs_root *log;
3091 struct btrfs_dir_item *di;
3092 struct btrfs_path *path;
3093 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003094 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003095 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003096 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003097
Chris Mason3a5f1d42008-09-11 15:53:37 -04003098 if (BTRFS_I(dir)->logged_trans < trans->transid)
3099 return 0;
3100
Chris Masone02119d2008-09-05 16:13:11 -04003101 ret = join_running_log_trans(root);
3102 if (ret)
3103 return 0;
3104
3105 mutex_lock(&BTRFS_I(dir)->log_mutex);
3106
3107 log = root->log_root;
3108 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003109 if (!path) {
3110 err = -ENOMEM;
3111 goto out_unlock;
3112 }
liubo2a29edc2011-01-26 06:22:08 +00003113
Li Zefan33345d012011-04-20 10:31:50 +08003114 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003115 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003116 if (IS_ERR(di)) {
3117 err = PTR_ERR(di);
3118 goto fail;
3119 }
3120 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003121 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3122 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003123 if (ret) {
3124 err = ret;
3125 goto fail;
3126 }
Chris Masone02119d2008-09-05 16:13:11 -04003127 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003128 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003129 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003130 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003131 if (IS_ERR(di)) {
3132 err = PTR_ERR(di);
3133 goto fail;
3134 }
3135 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003136 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3137 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003138 if (ret) {
3139 err = ret;
3140 goto fail;
3141 }
Chris Masone02119d2008-09-05 16:13:11 -04003142 }
3143
3144 /* update the directory size in the log to reflect the names
3145 * we have removed
3146 */
3147 if (bytes_del) {
3148 struct btrfs_key key;
3149
Li Zefan33345d012011-04-20 10:31:50 +08003150 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003151 key.offset = 0;
3152 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003153 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003154
3155 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003156 if (ret < 0) {
3157 err = ret;
3158 goto fail;
3159 }
Chris Masone02119d2008-09-05 16:13:11 -04003160 if (ret == 0) {
3161 struct btrfs_inode_item *item;
3162 u64 i_size;
3163
3164 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3165 struct btrfs_inode_item);
3166 i_size = btrfs_inode_size(path->nodes[0], item);
3167 if (i_size > bytes_del)
3168 i_size -= bytes_del;
3169 else
3170 i_size = 0;
3171 btrfs_set_inode_size(path->nodes[0], item, i_size);
3172 btrfs_mark_buffer_dirty(path->nodes[0]);
3173 } else
3174 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003175 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003176 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003177fail:
Chris Masone02119d2008-09-05 16:13:11 -04003178 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003179out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04003180 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003181 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003182 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003183 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003184 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003185 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003186
Chris Mason12fcfd22009-03-24 10:24:20 -04003187 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003188
Andi Kleen411fc6b2010-10-29 15:14:31 -04003189 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003190}
3191
3192/* see comments for btrfs_del_dir_entries_in_log */
3193int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3194 struct btrfs_root *root,
3195 const char *name, int name_len,
3196 struct inode *inode, u64 dirid)
3197{
3198 struct btrfs_root *log;
3199 u64 index;
3200 int ret;
3201
Chris Mason3a5f1d42008-09-11 15:53:37 -04003202 if (BTRFS_I(inode)->logged_trans < trans->transid)
3203 return 0;
3204
Chris Masone02119d2008-09-05 16:13:11 -04003205 ret = join_running_log_trans(root);
3206 if (ret)
3207 return 0;
3208 log = root->log_root;
3209 mutex_lock(&BTRFS_I(inode)->log_mutex);
3210
Li Zefan33345d012011-04-20 10:31:50 +08003211 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003212 dirid, &index);
3213 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003214 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003215 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003216 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003217 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003218 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003219 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003220
Chris Masone02119d2008-09-05 16:13:11 -04003221 return ret;
3222}
3223
3224/*
3225 * creates a range item in the log for 'dirid'. first_offset and
3226 * last_offset tell us which parts of the key space the log should
3227 * be considered authoritative for.
3228 */
3229static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3230 struct btrfs_root *log,
3231 struct btrfs_path *path,
3232 int key_type, u64 dirid,
3233 u64 first_offset, u64 last_offset)
3234{
3235 int ret;
3236 struct btrfs_key key;
3237 struct btrfs_dir_log_item *item;
3238
3239 key.objectid = dirid;
3240 key.offset = first_offset;
3241 if (key_type == BTRFS_DIR_ITEM_KEY)
3242 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3243 else
3244 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3245 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003246 if (ret)
3247 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003248
3249 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3250 struct btrfs_dir_log_item);
3251 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3252 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003253 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003254 return 0;
3255}
3256
3257/*
3258 * log all the items included in the current transaction for a given
3259 * directory. This also creates the range items in the log tree required
3260 * to replay anything deleted before the fsync
3261 */
3262static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3263 struct btrfs_root *root, struct inode *inode,
3264 struct btrfs_path *path,
3265 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003266 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003267 u64 min_offset, u64 *last_offset_ret)
3268{
3269 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003270 struct btrfs_root *log = root->log_root;
3271 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003272 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003273 int ret;
3274 int i;
3275 int nritems;
3276 u64 first_offset = min_offset;
3277 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08003278 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003279
3280 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003281
Li Zefan33345d012011-04-20 10:31:50 +08003282 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003283 min_key.type = key_type;
3284 min_key.offset = min_offset;
3285
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003286 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003287
3288 /*
3289 * we didn't find anything from this transaction, see if there
3290 * is anything at all
3291 */
Li Zefan33345d012011-04-20 10:31:50 +08003292 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3293 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003294 min_key.type = key_type;
3295 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003296 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003297 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3298 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003299 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003300 return ret;
3301 }
Li Zefan33345d012011-04-20 10:31:50 +08003302 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003303
3304 /* if ret == 0 there are items for this type,
3305 * create a range to tell us the last key of this type.
3306 * otherwise, there are no items in this directory after
3307 * *min_offset, and we create a range to indicate that.
3308 */
3309 if (ret == 0) {
3310 struct btrfs_key tmp;
3311 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3312 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003313 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003314 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003315 }
3316 goto done;
3317 }
3318
3319 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003320 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003321 if (ret == 0) {
3322 struct btrfs_key tmp;
3323 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3324 if (key_type == tmp.type) {
3325 first_offset = tmp.offset;
3326 ret = overwrite_item(trans, log, dst_path,
3327 path->nodes[0], path->slots[0],
3328 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003329 if (ret) {
3330 err = ret;
3331 goto done;
3332 }
Chris Masone02119d2008-09-05 16:13:11 -04003333 }
3334 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003335 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003336
3337 /* find the first key from this transaction again */
3338 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303339 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003340 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003341
3342 /*
3343 * we have a block from this transaction, log every item in it
3344 * from our directory
3345 */
Chris Masond3977122009-01-05 21:25:51 -05003346 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003347 struct btrfs_key tmp;
3348 src = path->nodes[0];
3349 nritems = btrfs_header_nritems(src);
3350 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003351 struct btrfs_dir_item *di;
3352
Chris Masone02119d2008-09-05 16:13:11 -04003353 btrfs_item_key_to_cpu(src, &min_key, i);
3354
Li Zefan33345d012011-04-20 10:31:50 +08003355 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003356 goto done;
3357 ret = overwrite_item(trans, log, dst_path, src, i,
3358 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003359 if (ret) {
3360 err = ret;
3361 goto done;
3362 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003363
3364 /*
3365 * We must make sure that when we log a directory entry,
3366 * the corresponding inode, after log replay, has a
3367 * matching link count. For example:
3368 *
3369 * touch foo
3370 * mkdir mydir
3371 * sync
3372 * ln foo mydir/bar
3373 * xfs_io -c "fsync" mydir
3374 * <crash>
3375 * <mount fs and log replay>
3376 *
3377 * Would result in a fsync log that when replayed, our
3378 * file inode would have a link count of 1, but we get
3379 * two directory entries pointing to the same inode.
3380 * After removing one of the names, it would not be
3381 * possible to remove the other name, which resulted
3382 * always in stale file handle errors, and would not
3383 * be possible to rmdir the parent directory, since
3384 * its i_size could never decrement to the value
3385 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3386 */
3387 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3388 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3389 if (ctx &&
3390 (btrfs_dir_transid(src, di) == trans->transid ||
3391 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3392 tmp.type != BTRFS_ROOT_ITEM_KEY)
3393 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003394 }
3395 path->slots[0] = nritems;
3396
3397 /*
3398 * look ahead to the next item and see if it is also
3399 * from this directory and from this transaction
3400 */
3401 ret = btrfs_next_leaf(root, path);
Liu Bo14c4d5f2018-04-03 01:59:47 +08003402 if (ret) {
3403 if (ret == 1)
3404 last_offset = (u64)-1;
3405 else
3406 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003407 goto done;
3408 }
3409 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003410 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003411 last_offset = (u64)-1;
3412 goto done;
3413 }
3414 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3415 ret = overwrite_item(trans, log, dst_path,
3416 path->nodes[0], path->slots[0],
3417 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003418 if (ret)
3419 err = ret;
3420 else
3421 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003422 goto done;
3423 }
3424 }
3425done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003426 btrfs_release_path(path);
3427 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003428
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003429 if (err == 0) {
3430 *last_offset_ret = last_offset;
3431 /*
3432 * insert the log range keys to indicate where the log
3433 * is valid
3434 */
3435 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003436 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003437 if (ret)
3438 err = ret;
3439 }
3440 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003441}
3442
3443/*
3444 * logging directories is very similar to logging inodes, We find all the items
3445 * from the current transaction and write them to the log.
3446 *
3447 * The recovery code scans the directory in the subvolume, and if it finds a
3448 * key in the range logged that is not present in the log tree, then it means
3449 * that dir entry was unlinked during the transaction.
3450 *
3451 * In order for that scan to work, we must include one key smaller than
3452 * the smallest logged by this transaction and one key larger than the largest
3453 * key logged by this transaction.
3454 */
3455static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3456 struct btrfs_root *root, struct inode *inode,
3457 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003458 struct btrfs_path *dst_path,
3459 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003460{
3461 u64 min_key;
3462 u64 max_key;
3463 int ret;
3464 int key_type = BTRFS_DIR_ITEM_KEY;
3465
3466again:
3467 min_key = 0;
3468 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003469 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003470 ret = log_dir_items(trans, root, inode, path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003471 dst_path, key_type, ctx, min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003472 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003473 if (ret)
3474 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003475 if (max_key == (u64)-1)
3476 break;
3477 min_key = max_key + 1;
3478 }
3479
3480 if (key_type == BTRFS_DIR_ITEM_KEY) {
3481 key_type = BTRFS_DIR_INDEX_KEY;
3482 goto again;
3483 }
3484 return 0;
3485}
3486
3487/*
3488 * a helper function to drop items from the log before we relog an
3489 * inode. max_key_type indicates the highest item type to remove.
3490 * This cannot be run for file data extents because it does not
3491 * free the extents they point to.
3492 */
3493static int drop_objectid_items(struct btrfs_trans_handle *trans,
3494 struct btrfs_root *log,
3495 struct btrfs_path *path,
3496 u64 objectid, int max_key_type)
3497{
3498 int ret;
3499 struct btrfs_key key;
3500 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003501 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003502
3503 key.objectid = objectid;
3504 key.type = max_key_type;
3505 key.offset = (u64)-1;
3506
Chris Masond3977122009-01-05 21:25:51 -05003507 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003508 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003509 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003510 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003511 break;
3512
3513 if (path->slots[0] == 0)
3514 break;
3515
3516 path->slots[0]--;
3517 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3518 path->slots[0]);
3519
3520 if (found_key.objectid != objectid)
3521 break;
3522
Josef Bacik18ec90d2012-09-28 11:56:28 -04003523 found_key.offset = 0;
3524 found_key.type = 0;
3525 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3526 &start_slot);
3527
3528 ret = btrfs_del_items(trans, log, path, start_slot,
3529 path->slots[0] - start_slot + 1);
3530 /*
3531 * If start slot isn't 0 then we don't need to re-search, we've
3532 * found the last guy with the objectid in this tree.
3533 */
3534 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003535 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003536 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003537 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003538 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003539 if (ret > 0)
3540 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003541 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003542}
3543
Josef Bacik94edf4a2012-09-25 14:56:25 -04003544static void fill_inode_item(struct btrfs_trans_handle *trans,
3545 struct extent_buffer *leaf,
3546 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003547 struct inode *inode, int log_inode_only,
3548 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003549{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003550 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003551
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003552 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003553
3554 if (log_inode_only) {
3555 /* set the generation to zero so the recover code
3556 * can tell the difference between an logging
3557 * just to say 'this inode exists' and a logging
3558 * to say 'update this inode with these values'
3559 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003560 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003561 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003562 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003563 btrfs_set_token_inode_generation(leaf, item,
3564 BTRFS_I(inode)->generation,
3565 &token);
3566 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003567 }
3568
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003569 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3570 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3571 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3572 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3573
David Sterbaa937b972014-12-12 17:39:12 +01003574 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003575 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003576 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003577 inode->i_atime.tv_nsec, &token);
3578
David Sterbaa937b972014-12-12 17:39:12 +01003579 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003580 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003581 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003582 inode->i_mtime.tv_nsec, &token);
3583
David Sterbaa937b972014-12-12 17:39:12 +01003584 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003585 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003586 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003587 inode->i_ctime.tv_nsec, &token);
3588
3589 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3590 &token);
3591
3592 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3593 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3594 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3595 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3596 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003597}
3598
Josef Bacika95249b2012-10-11 16:17:34 -04003599static int log_inode_item(struct btrfs_trans_handle *trans,
3600 struct btrfs_root *log, struct btrfs_path *path,
3601 struct inode *inode)
3602{
3603 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003604 int ret;
3605
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003606 ret = btrfs_insert_empty_item(trans, log, path,
3607 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003608 sizeof(*inode_item));
3609 if (ret && ret != -EEXIST)
3610 return ret;
3611 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3612 struct btrfs_inode_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003613 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003614 btrfs_release_path(path);
3615 return 0;
3616}
3617
Chris Mason31ff1cd2008-09-11 16:17:57 -04003618static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003619 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003620 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003621 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003622 int start_slot, int nr, int inode_only,
3623 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003624{
3625 unsigned long src_offset;
3626 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003627 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003628 struct btrfs_file_extent_item *extent;
3629 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003630 struct extent_buffer *src = src_path->nodes[0];
3631 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003632 int ret;
3633 struct btrfs_key *ins_keys;
3634 u32 *ins_sizes;
3635 char *ins_data;
3636 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003637 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003638 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003639 bool has_extents = false;
Filipe Manana74121f72014-08-07 12:00:44 +01003640 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003641 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003642
3643 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003644
3645 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3646 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003647 if (!ins_data)
3648 return -ENOMEM;
3649
Josef Bacik16e75492013-10-22 12:18:51 -04003650 first_key.objectid = (u64)-1;
3651
Chris Mason31ff1cd2008-09-11 16:17:57 -04003652 ins_sizes = (u32 *)ins_data;
3653 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3654
3655 for (i = 0; i < nr; i++) {
3656 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3657 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3658 }
3659 ret = btrfs_insert_empty_items(trans, log, dst_path,
3660 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003661 if (ret) {
3662 kfree(ins_data);
3663 return ret;
3664 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003665
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003666 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003667 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3668 dst_path->slots[0]);
3669
3670 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3671
Matthias Kaehlckee7f42b02017-07-27 14:30:23 -07003672 if (i == nr - 1)
Josef Bacik16e75492013-10-22 12:18:51 -04003673 last_key = ins_keys[i];
3674
Josef Bacik94edf4a2012-09-25 14:56:25 -04003675 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003676 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3677 dst_path->slots[0],
3678 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003679 fill_inode_item(trans, dst_path->nodes[0], inode_item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003680 inode, inode_only == LOG_INODE_EXISTS,
3681 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003682 } else {
3683 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3684 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003685 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003686
Josef Bacik16e75492013-10-22 12:18:51 -04003687 /*
3688 * We set need_find_last_extent here in case we know we were
3689 * processing other items and then walk into the first extent in
3690 * the inode. If we don't hit an extent then nothing changes,
3691 * we'll do the last search the next time around.
3692 */
3693 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3694 has_extents = true;
Filipe Manana74121f72014-08-07 12:00:44 +01003695 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003696 first_key = ins_keys[i];
3697 } else {
3698 need_find_last_extent = false;
3699 }
3700
Chris Mason31ff1cd2008-09-11 16:17:57 -04003701 /* take a reference on file data extents so that truncates
3702 * or deletes of this inode don't have to relog the inode
3703 * again
3704 */
David Sterba962a2982014-06-04 18:41:45 +02003705 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003706 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003707 int found_type;
3708 extent = btrfs_item_ptr(src, start_slot + i,
3709 struct btrfs_file_extent_item);
3710
liubo8e531cd2011-05-06 10:36:09 +08003711 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3712 continue;
3713
Chris Mason31ff1cd2008-09-11 16:17:57 -04003714 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003715 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003716 u64 ds, dl, cs, cl;
3717 ds = btrfs_file_extent_disk_bytenr(src,
3718 extent);
3719 /* ds == 0 is a hole */
3720 if (ds == 0)
3721 continue;
3722
3723 dl = btrfs_file_extent_disk_num_bytes(src,
3724 extent);
3725 cs = btrfs_file_extent_offset(src, extent);
3726 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003727 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003728 if (btrfs_file_extent_compression(src,
3729 extent)) {
3730 cs = 0;
3731 cl = dl;
3732 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003733
3734 ret = btrfs_lookup_csums_range(
3735 log->fs_info->csum_root,
3736 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003737 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003738 if (ret) {
3739 btrfs_release_path(dst_path);
3740 kfree(ins_data);
3741 return ret;
3742 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003743 }
3744 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003745 }
3746
3747 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003748 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003749 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003750
3751 /*
3752 * we have to do this after the loop above to avoid changing the
3753 * log tree while trying to change the log tree.
3754 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003755 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003756 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003757 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3758 struct btrfs_ordered_sum,
3759 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003760 if (!ret)
3761 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003762 list_del(&sums->list);
3763 kfree(sums);
3764 }
Josef Bacik16e75492013-10-22 12:18:51 -04003765
3766 if (!has_extents)
3767 return ret;
3768
Filipe Manana74121f72014-08-07 12:00:44 +01003769 if (need_find_last_extent && *last_extent == first_key.offset) {
3770 /*
3771 * We don't have any leafs between our current one and the one
3772 * we processed before that can have file extent items for our
3773 * inode (and have a generation number smaller than our current
3774 * transaction id).
3775 */
3776 need_find_last_extent = false;
3777 }
3778
Josef Bacik16e75492013-10-22 12:18:51 -04003779 /*
3780 * Because we use btrfs_search_forward we could skip leaves that were
3781 * not modified and then assume *last_extent is valid when it really
3782 * isn't. So back up to the previous leaf and read the end of the last
3783 * extent before we go and fill in holes.
3784 */
3785 if (need_find_last_extent) {
3786 u64 len;
3787
3788 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3789 if (ret < 0)
3790 return ret;
3791 if (ret)
3792 goto fill_holes;
3793 if (src_path->slots[0])
3794 src_path->slots[0]--;
3795 src = src_path->nodes[0];
3796 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3797 if (key.objectid != btrfs_ino(inode) ||
3798 key.type != BTRFS_EXTENT_DATA_KEY)
3799 goto fill_holes;
3800 extent = btrfs_item_ptr(src, src_path->slots[0],
3801 struct btrfs_file_extent_item);
3802 if (btrfs_file_extent_type(src, extent) ==
3803 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003804 len = btrfs_file_extent_inline_len(src,
3805 src_path->slots[0],
3806 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003807 *last_extent = ALIGN(key.offset + len,
3808 log->sectorsize);
3809 } else {
3810 len = btrfs_file_extent_num_bytes(src, extent);
3811 *last_extent = key.offset + len;
3812 }
3813 }
3814fill_holes:
3815 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3816 * things could have happened
3817 *
3818 * 1) A merge could have happened, so we could currently be on a leaf
3819 * that holds what we were copying in the first place.
3820 * 2) A split could have happened, and now not all of the items we want
3821 * are on the same leaf.
3822 *
3823 * So we need to adjust how we search for holes, we need to drop the
3824 * path and re-search for the first extent key we found, and then walk
3825 * forward until we hit the last one we copied.
3826 */
3827 if (need_find_last_extent) {
3828 /* btrfs_prev_leaf could return 1 without releasing the path */
3829 btrfs_release_path(src_path);
3830 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3831 src_path, 0, 0);
3832 if (ret < 0)
3833 return ret;
3834 ASSERT(ret == 0);
3835 src = src_path->nodes[0];
3836 i = src_path->slots[0];
3837 } else {
3838 i = start_slot;
3839 }
3840
3841 /*
3842 * Ok so here we need to go through and fill in any holes we may have
3843 * to make sure that holes are punched for those areas in case they had
3844 * extents previously.
3845 */
3846 while (!done) {
3847 u64 offset, len;
3848 u64 extent_end;
3849
3850 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3851 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3852 if (ret < 0)
3853 return ret;
3854 ASSERT(ret == 0);
3855 src = src_path->nodes[0];
3856 i = 0;
Filipe Mananabee3c022018-03-26 23:59:12 +01003857 need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003858 }
3859
3860 btrfs_item_key_to_cpu(src, &key, i);
3861 if (!btrfs_comp_cpu_keys(&key, &last_key))
3862 done = true;
3863 if (key.objectid != btrfs_ino(inode) ||
3864 key.type != BTRFS_EXTENT_DATA_KEY) {
3865 i++;
3866 continue;
3867 }
3868 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3869 if (btrfs_file_extent_type(src, extent) ==
3870 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003871 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003872 extent_end = ALIGN(key.offset + len, log->sectorsize);
3873 } else {
3874 len = btrfs_file_extent_num_bytes(src, extent);
3875 extent_end = key.offset + len;
3876 }
3877 i++;
3878
3879 if (*last_extent == key.offset) {
3880 *last_extent = extent_end;
3881 continue;
3882 }
3883 offset = *last_extent;
3884 len = key.offset - *last_extent;
3885 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3886 offset, 0, 0, len, 0, len, 0,
3887 0, 0);
3888 if (ret)
3889 break;
Filipe Manana74121f72014-08-07 12:00:44 +01003890 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003891 }
3892 /*
3893 * Need to let the callers know we dropped the path so they should
3894 * re-search.
3895 */
3896 if (!ret && need_find_last_extent)
3897 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003898 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003899}
3900
Josef Bacik5dc562c2012-08-17 13:14:17 -04003901static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3902{
3903 struct extent_map *em1, *em2;
3904
3905 em1 = list_entry(a, struct extent_map, list);
3906 em2 = list_entry(b, struct extent_map, list);
3907
3908 if (em1->start < em2->start)
3909 return -1;
3910 else if (em1->start > em2->start)
3911 return 1;
3912 return 0;
3913}
3914
Filipe Manana8407f552014-09-05 15:14:39 +01003915static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3916 struct inode *inode,
3917 struct btrfs_root *root,
3918 const struct extent_map *em,
3919 const struct list_head *logged_list,
3920 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003921{
Josef Bacik2ab28f32012-10-12 15:27:49 -04003922 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01003923 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003924 u64 mod_start = em->mod_start;
3925 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003926 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003927 u64 csum_offset;
3928 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003929 LIST_HEAD(ordered_sums);
3930 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003931
Filipe Manana8407f552014-09-05 15:14:39 +01003932 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04003933
Filipe Manana8407f552014-09-05 15:14:39 +01003934 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3935 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003936 return 0;
3937
Josef Bacik2ab28f32012-10-12 15:27:49 -04003938 /*
Filipe Manana8407f552014-09-05 15:14:39 +01003939 * Wait far any ordered extent that covers our extent map. If it
3940 * finishes without an error, first check and see if our csums are on
3941 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04003942 */
Miao Xie827463c2014-01-14 20:31:51 +08003943 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003944 struct btrfs_ordered_sum *sum;
3945
3946 if (!mod_len)
3947 break;
3948
Josef Bacik2ab28f32012-10-12 15:27:49 -04003949 if (ordered->file_offset + ordered->len <= mod_start ||
3950 mod_start + mod_len <= ordered->file_offset)
3951 continue;
3952
Filipe Manana8407f552014-09-05 15:14:39 +01003953 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3954 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3955 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3956 const u64 start = ordered->file_offset;
3957 const u64 end = ordered->file_offset + ordered->len - 1;
3958
3959 WARN_ON(ordered->inode != inode);
3960 filemap_fdatawrite_range(inode->i_mapping, start, end);
3961 }
3962
3963 wait_event(ordered->wait,
3964 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3965 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3966
3967 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00003968 /*
3969 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3970 * i_mapping flags, so that the next fsync won't get
3971 * an outdated io error too.
3972 */
Miklos Szeredif0312212016-09-16 12:44:21 +02003973 filemap_check_errors(inode->i_mapping);
Filipe Manana8407f552014-09-05 15:14:39 +01003974 *ordered_io_error = true;
3975 break;
3976 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003977 /*
3978 * We are going to copy all the csums on this ordered extent, so
3979 * go ahead and adjust mod_start and mod_len in case this
3980 * ordered extent has already been logged.
3981 */
3982 if (ordered->file_offset > mod_start) {
3983 if (ordered->file_offset + ordered->len >=
3984 mod_start + mod_len)
3985 mod_len = ordered->file_offset - mod_start;
3986 /*
3987 * If we have this case
3988 *
3989 * |--------- logged extent ---------|
3990 * |----- ordered extent ----|
3991 *
3992 * Just don't mess with mod_start and mod_len, we'll
3993 * just end up logging more csums than we need and it
3994 * will be ok.
3995 */
3996 } else {
3997 if (ordered->file_offset + ordered->len <
3998 mod_start + mod_len) {
3999 mod_len = (mod_start + mod_len) -
4000 (ordered->file_offset + ordered->len);
4001 mod_start = ordered->file_offset +
4002 ordered->len;
4003 } else {
4004 mod_len = 0;
4005 }
4006 }
4007
Filipe Manana8407f552014-09-05 15:14:39 +01004008 if (skip_csum)
4009 continue;
4010
Josef Bacik2ab28f32012-10-12 15:27:49 -04004011 /*
4012 * To keep us from looping for the above case of an ordered
4013 * extent that falls inside of the logged extent.
4014 */
4015 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4016 &ordered->flags))
4017 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004018
Josef Bacik2ab28f32012-10-12 15:27:49 -04004019 list_for_each_entry(sum, &ordered->list, list) {
4020 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08004021 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01004022 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004023 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004024 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004025
Filipe Manana8407f552014-09-05 15:14:39 +01004026 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04004027 return ret;
4028
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004029 if (em->compress_type) {
4030 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004031 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004032 } else {
4033 csum_offset = mod_start - em->start;
4034 csum_len = mod_len;
4035 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004036
Josef Bacik70c8a912012-10-11 16:54:30 -04004037 /* block start is already adjusted for the file extent offset. */
4038 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
4039 em->block_start + csum_offset,
4040 em->block_start + csum_offset +
4041 csum_len - 1, &ordered_sums, 0);
4042 if (ret)
4043 return ret;
4044
4045 while (!list_empty(&ordered_sums)) {
4046 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4047 struct btrfs_ordered_sum,
4048 list);
4049 if (!ret)
4050 ret = btrfs_csum_file_blocks(trans, log, sums);
4051 list_del(&sums->list);
4052 kfree(sums);
4053 }
4054
4055 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004056}
4057
Filipe Manana8407f552014-09-05 15:14:39 +01004058static int log_one_extent(struct btrfs_trans_handle *trans,
4059 struct inode *inode, struct btrfs_root *root,
4060 const struct extent_map *em,
4061 struct btrfs_path *path,
4062 const struct list_head *logged_list,
4063 struct btrfs_log_ctx *ctx)
4064{
4065 struct btrfs_root *log = root->log_root;
4066 struct btrfs_file_extent_item *fi;
4067 struct extent_buffer *leaf;
4068 struct btrfs_map_token token;
4069 struct btrfs_key key;
4070 u64 extent_offset = em->start - em->orig_start;
4071 u64 block_len;
4072 int ret;
4073 int extent_inserted = 0;
4074 bool ordered_io_err = false;
4075
4076 ret = wait_ordered_extents(trans, inode, root, em, logged_list,
4077 &ordered_io_err);
4078 if (ret)
4079 return ret;
4080
4081 if (ordered_io_err) {
4082 ctx->io_err = -EIO;
4083 return 0;
4084 }
4085
4086 btrfs_init_map_token(&token);
4087
4088 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
4089 em->start + em->len, NULL, 0, 1,
4090 sizeof(*fi), &extent_inserted);
4091 if (ret)
4092 return ret;
4093
4094 if (!extent_inserted) {
4095 key.objectid = btrfs_ino(inode);
4096 key.type = BTRFS_EXTENT_DATA_KEY;
4097 key.offset = em->start;
4098
4099 ret = btrfs_insert_empty_item(trans, log, path, &key,
4100 sizeof(*fi));
4101 if (ret)
4102 return ret;
4103 }
4104 leaf = path->nodes[0];
4105 fi = btrfs_item_ptr(leaf, path->slots[0],
4106 struct btrfs_file_extent_item);
4107
Josef Bacik50d9aa92014-11-21 14:52:38 -05004108 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004109 &token);
4110 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4111 btrfs_set_token_file_extent_type(leaf, fi,
4112 BTRFS_FILE_EXTENT_PREALLOC,
4113 &token);
4114 else
4115 btrfs_set_token_file_extent_type(leaf, fi,
4116 BTRFS_FILE_EXTENT_REG,
4117 &token);
4118
4119 block_len = max(em->block_len, em->orig_block_len);
4120 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4121 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4122 em->block_start,
4123 &token);
4124 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4125 &token);
4126 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4127 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4128 em->block_start -
4129 extent_offset, &token);
4130 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4131 &token);
4132 } else {
4133 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4134 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4135 &token);
4136 }
4137
4138 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4139 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4140 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4141 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4142 &token);
4143 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4144 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4145 btrfs_mark_buffer_dirty(leaf);
4146
4147 btrfs_release_path(path);
4148
4149 return ret;
4150}
4151
Josef Bacik5dc562c2012-08-17 13:14:17 -04004152static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4153 struct btrfs_root *root,
4154 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004155 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004156 struct list_head *logged_list,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004157 struct btrfs_log_ctx *ctx,
4158 const u64 start,
4159 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004160{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004161 struct extent_map *em, *n;
4162 struct list_head extents;
4163 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4164 u64 test_gen;
4165 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004166 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004167
4168 INIT_LIST_HEAD(&extents);
4169
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004170 down_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004171 write_lock(&tree->lock);
4172 test_gen = root->fs_info->last_trans_committed;
4173
4174 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4175 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004176
4177 /*
4178 * Just an arbitrary number, this can be really CPU intensive
4179 * once we start getting a lot of extents, and really once we
4180 * have a bunch of extents we just want to commit since it will
4181 * be faster.
4182 */
4183 if (++num > 32768) {
4184 list_del_init(&tree->modified_extents);
4185 ret = -EFBIG;
4186 goto process;
4187 }
4188
Josef Bacik5dc562c2012-08-17 13:14:17 -04004189 if (em->generation <= test_gen)
4190 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004191 /* Need a ref to keep it from getting evicted from cache */
4192 atomic_inc(&em->refs);
4193 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004194 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004195 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004196 }
4197
4198 list_sort(NULL, &extents, extent_cmp);
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004199 btrfs_get_logged_extents(inode, logged_list, start, end);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004200 /*
4201 * Some ordered extents started by fsync might have completed
4202 * before we could collect them into the list logged_list, which
4203 * means they're gone, not in our logged_list nor in the inode's
4204 * ordered tree. We want the application/user space to know an
4205 * error happened while attempting to persist file data so that
4206 * it can take proper action. If such error happened, we leave
4207 * without writing to the log tree and the fsync must report the
4208 * file data write error and not commit the current transaction.
4209 */
Miklos Szeredif0312212016-09-16 12:44:21 +02004210 ret = filemap_check_errors(inode->i_mapping);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004211 if (ret)
4212 ctx->io_err = ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004213process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004214 while (!list_empty(&extents)) {
4215 em = list_entry(extents.next, struct extent_map, list);
4216
4217 list_del_init(&em->list);
4218
4219 /*
4220 * If we had an error we just need to delete everybody from our
4221 * private list.
4222 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004223 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004224 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004225 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004226 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004227 }
4228
4229 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004230
Filipe Manana8407f552014-09-05 15:14:39 +01004231 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4232 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004233 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004234 clear_em_logging(tree, em);
4235 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004236 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004237 WARN_ON(!list_empty(&extents));
4238 write_unlock(&tree->lock);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004239 up_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004240
Josef Bacik5dc562c2012-08-17 13:14:17 -04004241 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004242 return ret;
4243}
4244
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004245static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
4246 struct btrfs_path *path, u64 *size_ret)
4247{
4248 struct btrfs_key key;
4249 int ret;
4250
4251 key.objectid = btrfs_ino(inode);
4252 key.type = BTRFS_INODE_ITEM_KEY;
4253 key.offset = 0;
4254
4255 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4256 if (ret < 0) {
4257 return ret;
4258 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004259 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004260 } else {
4261 struct btrfs_inode_item *item;
4262
4263 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4264 struct btrfs_inode_item);
4265 *size_ret = btrfs_inode_size(path->nodes[0], item);
4266 }
4267
4268 btrfs_release_path(path);
4269 return 0;
4270}
4271
Filipe Manana36283bf2015-06-20 00:44:51 +01004272/*
4273 * At the moment we always log all xattrs. This is to figure out at log replay
4274 * time which xattrs must have their deletion replayed. If a xattr is missing
4275 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4276 * because if a xattr is deleted, the inode is fsynced and a power failure
4277 * happens, causing the log to be replayed the next time the fs is mounted,
4278 * we want the xattr to not exist anymore (same behaviour as other filesystems
4279 * with a journal, ext3/4, xfs, f2fs, etc).
4280 */
4281static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4282 struct btrfs_root *root,
4283 struct inode *inode,
4284 struct btrfs_path *path,
4285 struct btrfs_path *dst_path)
4286{
4287 int ret;
4288 struct btrfs_key key;
4289 const u64 ino = btrfs_ino(inode);
4290 int ins_nr = 0;
4291 int start_slot = 0;
4292
4293 key.objectid = ino;
4294 key.type = BTRFS_XATTR_ITEM_KEY;
4295 key.offset = 0;
4296
4297 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4298 if (ret < 0)
4299 return ret;
4300
4301 while (true) {
4302 int slot = path->slots[0];
4303 struct extent_buffer *leaf = path->nodes[0];
4304 int nritems = btrfs_header_nritems(leaf);
4305
4306 if (slot >= nritems) {
4307 if (ins_nr > 0) {
4308 u64 last_extent = 0;
4309
4310 ret = copy_items(trans, inode, dst_path, path,
4311 &last_extent, start_slot,
4312 ins_nr, 1, 0);
4313 /* can't be 1, extent items aren't processed */
4314 ASSERT(ret <= 0);
4315 if (ret < 0)
4316 return ret;
4317 ins_nr = 0;
4318 }
4319 ret = btrfs_next_leaf(root, path);
4320 if (ret < 0)
4321 return ret;
4322 else if (ret > 0)
4323 break;
4324 continue;
4325 }
4326
4327 btrfs_item_key_to_cpu(leaf, &key, slot);
4328 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4329 break;
4330
4331 if (ins_nr == 0)
4332 start_slot = slot;
4333 ins_nr++;
4334 path->slots[0]++;
4335 cond_resched();
4336 }
4337 if (ins_nr > 0) {
4338 u64 last_extent = 0;
4339
4340 ret = copy_items(trans, inode, dst_path, path,
4341 &last_extent, start_slot,
4342 ins_nr, 1, 0);
4343 /* can't be 1, extent items aren't processed */
4344 ASSERT(ret <= 0);
4345 if (ret < 0)
4346 return ret;
4347 }
4348
4349 return 0;
4350}
4351
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004352/*
4353 * If the no holes feature is enabled we need to make sure any hole between the
4354 * last extent and the i_size of our inode is explicitly marked in the log. This
4355 * is to make sure that doing something like:
4356 *
4357 * 1) create file with 128Kb of data
4358 * 2) truncate file to 64Kb
4359 * 3) truncate file to 256Kb
4360 * 4) fsync file
4361 * 5) <crash/power failure>
4362 * 6) mount fs and trigger log replay
4363 *
4364 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4365 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4366 * file correspond to a hole. The presence of explicit holes in a log tree is
4367 * what guarantees that log replay will remove/adjust file extent items in the
4368 * fs/subvol tree.
4369 *
4370 * Here we do not need to care about holes between extents, that is already done
4371 * by copy_items(). We also only need to do this in the full sync path, where we
4372 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4373 * lookup the list of modified extent maps and if any represents a hole, we
4374 * insert a corresponding extent representing a hole in the log tree.
4375 */
4376static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4377 struct btrfs_root *root,
4378 struct inode *inode,
4379 struct btrfs_path *path)
4380{
4381 int ret;
4382 struct btrfs_key key;
4383 u64 hole_start;
4384 u64 hole_size;
4385 struct extent_buffer *leaf;
4386 struct btrfs_root *log = root->log_root;
4387 const u64 ino = btrfs_ino(inode);
4388 const u64 i_size = i_size_read(inode);
4389
4390 if (!btrfs_fs_incompat(root->fs_info, NO_HOLES))
4391 return 0;
4392
4393 key.objectid = ino;
4394 key.type = BTRFS_EXTENT_DATA_KEY;
4395 key.offset = (u64)-1;
4396
4397 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4398 ASSERT(ret != 0);
4399 if (ret < 0)
4400 return ret;
4401
4402 ASSERT(path->slots[0] > 0);
4403 path->slots[0]--;
4404 leaf = path->nodes[0];
4405 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4406
4407 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4408 /* inode does not have any extents */
4409 hole_start = 0;
4410 hole_size = i_size;
4411 } else {
4412 struct btrfs_file_extent_item *extent;
4413 u64 len;
4414
4415 /*
4416 * If there's an extent beyond i_size, an explicit hole was
4417 * already inserted by copy_items().
4418 */
4419 if (key.offset >= i_size)
4420 return 0;
4421
4422 extent = btrfs_item_ptr(leaf, path->slots[0],
4423 struct btrfs_file_extent_item);
4424
4425 if (btrfs_file_extent_type(leaf, extent) ==
4426 BTRFS_FILE_EXTENT_INLINE) {
4427 len = btrfs_file_extent_inline_len(leaf,
4428 path->slots[0],
4429 extent);
4430 ASSERT(len == i_size);
4431 return 0;
4432 }
4433
4434 len = btrfs_file_extent_num_bytes(leaf, extent);
4435 /* Last extent goes beyond i_size, no need to log a hole. */
4436 if (key.offset + len > i_size)
4437 return 0;
4438 hole_start = key.offset + len;
4439 hole_size = i_size - hole_start;
4440 }
4441 btrfs_release_path(path);
4442
4443 /* Last extent ends at i_size. */
4444 if (hole_size == 0)
4445 return 0;
4446
4447 hole_size = ALIGN(hole_size, root->sectorsize);
4448 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4449 hole_size, 0, hole_size, 0, 0, 0);
4450 return ret;
4451}
4452
Filipe Manana56f23fd2016-03-30 23:37:21 +01004453/*
4454 * When we are logging a new inode X, check if it doesn't have a reference that
4455 * matches the reference from some other inode Y created in a past transaction
4456 * and that was renamed in the current transaction. If we don't do this, then at
4457 * log replay time we can lose inode Y (and all its files if it's a directory):
4458 *
4459 * mkdir /mnt/x
4460 * echo "hello world" > /mnt/x/foobar
4461 * sync
4462 * mv /mnt/x /mnt/y
4463 * mkdir /mnt/x # or touch /mnt/x
4464 * xfs_io -c fsync /mnt/x
4465 * <power fail>
4466 * mount fs, trigger log replay
4467 *
4468 * After the log replay procedure, we would lose the first directory and all its
4469 * files (file foobar).
4470 * For the case where inode Y is not a directory we simply end up losing it:
4471 *
4472 * echo "123" > /mnt/foo
4473 * sync
4474 * mv /mnt/foo /mnt/bar
4475 * echo "abc" > /mnt/foo
4476 * xfs_io -c fsync /mnt/foo
4477 * <power fail>
4478 *
4479 * We also need this for cases where a snapshot entry is replaced by some other
4480 * entry (file or directory) otherwise we end up with an unreplayable log due to
4481 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4482 * if it were a regular entry:
4483 *
4484 * mkdir /mnt/x
4485 * btrfs subvolume snapshot /mnt /mnt/x/snap
4486 * btrfs subvolume delete /mnt/x/snap
4487 * rmdir /mnt/x
4488 * mkdir /mnt/x
4489 * fsync /mnt/x or fsync some new file inside it
4490 * <power fail>
4491 *
4492 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4493 * the same transaction.
4494 */
4495static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4496 const int slot,
4497 const struct btrfs_key *key,
Filipe Manana44f714d2016-06-06 16:11:13 +01004498 struct inode *inode,
4499 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004500{
4501 int ret;
4502 struct btrfs_path *search_path;
4503 char *name = NULL;
4504 u32 name_len = 0;
4505 u32 item_size = btrfs_item_size_nr(eb, slot);
4506 u32 cur_offset = 0;
4507 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4508
4509 search_path = btrfs_alloc_path();
4510 if (!search_path)
4511 return -ENOMEM;
4512 search_path->search_commit_root = 1;
4513 search_path->skip_locking = 1;
4514
4515 while (cur_offset < item_size) {
4516 u64 parent;
4517 u32 this_name_len;
4518 u32 this_len;
4519 unsigned long name_ptr;
4520 struct btrfs_dir_item *di;
4521
4522 if (key->type == BTRFS_INODE_REF_KEY) {
4523 struct btrfs_inode_ref *iref;
4524
4525 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4526 parent = key->offset;
4527 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4528 name_ptr = (unsigned long)(iref + 1);
4529 this_len = sizeof(*iref) + this_name_len;
4530 } else {
4531 struct btrfs_inode_extref *extref;
4532
4533 extref = (struct btrfs_inode_extref *)(ptr +
4534 cur_offset);
4535 parent = btrfs_inode_extref_parent(eb, extref);
4536 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4537 name_ptr = (unsigned long)&extref->name;
4538 this_len = sizeof(*extref) + this_name_len;
4539 }
4540
4541 if (this_name_len > name_len) {
4542 char *new_name;
4543
4544 new_name = krealloc(name, this_name_len, GFP_NOFS);
4545 if (!new_name) {
4546 ret = -ENOMEM;
4547 goto out;
4548 }
4549 name_len = this_name_len;
4550 name = new_name;
4551 }
4552
4553 read_extent_buffer(eb, name, name_ptr, this_name_len);
4554 di = btrfs_lookup_dir_item(NULL, BTRFS_I(inode)->root,
4555 search_path, parent,
4556 name, this_name_len, 0);
4557 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004558 struct btrfs_key di_key;
4559
4560 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4561 di, &di_key);
4562 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4563 ret = 1;
4564 *other_ino = di_key.objectid;
4565 } else {
4566 ret = -EAGAIN;
4567 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004568 goto out;
4569 } else if (IS_ERR(di)) {
4570 ret = PTR_ERR(di);
4571 goto out;
4572 }
4573 btrfs_release_path(search_path);
4574
4575 cur_offset += this_len;
4576 }
4577 ret = 0;
4578out:
4579 btrfs_free_path(search_path);
4580 kfree(name);
4581 return ret;
4582}
4583
Chris Masone02119d2008-09-05 16:13:11 -04004584/* log a single inode in the tree log.
4585 * At least one parent directory for this inode must exist in the tree
4586 * or be logged already.
4587 *
4588 * Any items from this inode changed by the current transaction are copied
4589 * to the log tree. An extra reference is taken on any extents in this
4590 * file, allowing us to avoid a whole pile of corner cases around logging
4591 * blocks that have been removed from the tree.
4592 *
4593 * See LOG_INODE_ALL and related defines for a description of what inode_only
4594 * does.
4595 *
4596 * This handles both files and directories.
4597 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004598static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004599 struct btrfs_root *root, struct inode *inode,
4600 int inode_only,
4601 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004602 const loff_t end,
4603 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004604{
4605 struct btrfs_path *path;
4606 struct btrfs_path *dst_path;
4607 struct btrfs_key min_key;
4608 struct btrfs_key max_key;
4609 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004610 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08004611 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004612 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004613 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004614 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004615 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004616 int ins_start_slot = 0;
4617 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004618 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08004619 u64 ino = btrfs_ino(inode);
Filipe Manana49dae1b2014-09-06 22:34:39 +01004620 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004621 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004622 bool need_log_inode_item = true;
Filipe Manana92291242018-05-11 16:42:42 +01004623 bool xattrs_logged = false;
Chris Masone02119d2008-09-05 16:13:11 -04004624
Chris Masone02119d2008-09-05 16:13:11 -04004625 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004626 if (!path)
4627 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004628 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004629 if (!dst_path) {
4630 btrfs_free_path(path);
4631 return -ENOMEM;
4632 }
Chris Masone02119d2008-09-05 16:13:11 -04004633
Li Zefan33345d012011-04-20 10:31:50 +08004634 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004635 min_key.type = BTRFS_INODE_ITEM_KEY;
4636 min_key.offset = 0;
4637
Li Zefan33345d012011-04-20 10:31:50 +08004638 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004639
Chris Mason12fcfd22009-03-24 10:24:20 -04004640
Josef Bacik5dc562c2012-08-17 13:14:17 -04004641 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00004642 if (S_ISDIR(inode->i_mode) ||
4643 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4644 &BTRFS_I(inode)->runtime_flags) &&
Liu Bo67312122016-11-30 16:20:25 -08004645 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004646 max_key.type = BTRFS_XATTR_ITEM_KEY;
4647 else
4648 max_key.type = (u8)-1;
4649 max_key.offset = (u64)-1;
4650
Filipe Manana2c2c4522015-01-13 16:40:04 +00004651 /*
4652 * Only run delayed items if we are a dir or a new file.
4653 * Otherwise commit the delayed inode only, which is needed in
4654 * order for the log replay code to mark inodes for link count
4655 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4656 */
Josef Bacik94edf4a2012-09-25 14:56:25 -04004657 if (S_ISDIR(inode->i_mode) ||
Filipe Manana2c2c4522015-01-13 16:40:04 +00004658 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
Josef Bacik94edf4a2012-09-25 14:56:25 -04004659 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004660 else
4661 ret = btrfs_commit_inode_delayed_inode(inode);
4662
4663 if (ret) {
4664 btrfs_free_path(path);
4665 btrfs_free_path(dst_path);
4666 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004667 }
4668
Liu Bo67312122016-11-30 16:20:25 -08004669 if (inode_only == LOG_OTHER_INODE) {
4670 inode_only = LOG_INODE_EXISTS;
4671 mutex_lock_nested(&BTRFS_I(inode)->log_mutex,
4672 SINGLE_DEPTH_NESTING);
4673 } else {
4674 mutex_lock(&BTRFS_I(inode)->log_mutex);
4675 }
Chris Masone02119d2008-09-05 16:13:11 -04004676
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004677 /*
Chris Masone02119d2008-09-05 16:13:11 -04004678 * a brute force approach to making sure we get the most uptodate
4679 * copies of everything.
4680 */
4681 if (S_ISDIR(inode->i_mode)) {
4682 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4683
Filipe Manana4f764e52015-02-23 19:53:35 +00004684 if (inode_only == LOG_INODE_EXISTS)
4685 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004686 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004687 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004688 if (inode_only == LOG_INODE_EXISTS) {
4689 /*
4690 * Make sure the new inode item we write to the log has
4691 * the same isize as the current one (if it exists).
4692 * This is necessary to prevent data loss after log
4693 * replay, and also to prevent doing a wrong expanding
4694 * truncate - for e.g. create file, write 4K into offset
4695 * 0, fsync, write 4K into offset 4096, add hard link,
4696 * fsync some other file (to sync log), power fail - if
4697 * we use the inode's current i_size, after log replay
4698 * we get a 8Kb file, with the last 4Kb extent as a hole
4699 * (zeroes), as if an expanding truncate happened,
4700 * instead of getting a file of 4Kb only.
4701 */
4702 err = logged_inode_size(log, inode, path,
4703 &logged_isize);
4704 if (err)
4705 goto out_unlock;
4706 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004707 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4708 &BTRFS_I(inode)->runtime_flags)) {
4709 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004710 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004711 ret = drop_objectid_items(trans, log, path, ino,
4712 max_key.type);
4713 } else {
4714 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4715 &BTRFS_I(inode)->runtime_flags);
4716 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4717 &BTRFS_I(inode)->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004718 while(1) {
4719 ret = btrfs_truncate_inode_items(trans,
4720 log, inode, 0, 0);
4721 if (ret != -EAGAIN)
4722 break;
4723 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004724 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004725 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4726 &BTRFS_I(inode)->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004727 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004728 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004729 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004730 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004731 ret = drop_objectid_items(trans, log, path, ino,
4732 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004733 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004734 if (inode_only == LOG_INODE_ALL)
4735 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004736 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004737 }
Josef Bacika95249b2012-10-11 16:17:34 -04004738
Chris Masone02119d2008-09-05 16:13:11 -04004739 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004740 if (ret) {
4741 err = ret;
4742 goto out_unlock;
4743 }
Chris Masone02119d2008-09-05 16:13:11 -04004744
Chris Masond3977122009-01-05 21:25:51 -05004745 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004746 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004747 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004748 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004749 if (ret < 0) {
4750 err = ret;
4751 goto out_unlock;
4752 }
Chris Masone02119d2008-09-05 16:13:11 -04004753 if (ret != 0)
4754 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004755again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004756 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004757 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004758 break;
4759 if (min_key.type > max_key.type)
4760 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004761
Filipe Mananae4545de2015-06-17 12:49:23 +01004762 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4763 need_log_inode_item = false;
4764
Filipe Manana56f23fd2016-03-30 23:37:21 +01004765 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4766 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
4767 BTRFS_I(inode)->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004768 u64 other_ino = 0;
4769
Filipe Manana56f23fd2016-03-30 23:37:21 +01004770 ret = btrfs_check_ref_name_override(path->nodes[0],
4771 path->slots[0],
Filipe Manana44f714d2016-06-06 16:11:13 +01004772 &min_key, inode,
4773 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004774 if (ret < 0) {
4775 err = ret;
4776 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004777 } else if (ret > 0 && ctx &&
4778 other_ino != btrfs_ino(ctx->inode)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004779 struct btrfs_key inode_key;
4780 struct inode *other_inode;
4781
4782 if (ins_nr > 0) {
4783 ins_nr++;
4784 } else {
4785 ins_nr = 1;
4786 ins_start_slot = path->slots[0];
4787 }
4788 ret = copy_items(trans, inode, dst_path, path,
4789 &last_extent, ins_start_slot,
4790 ins_nr, inode_only,
4791 logged_isize);
4792 if (ret < 0) {
4793 err = ret;
4794 goto out_unlock;
4795 }
4796 ins_nr = 0;
4797 btrfs_release_path(path);
4798 inode_key.objectid = other_ino;
4799 inode_key.type = BTRFS_INODE_ITEM_KEY;
4800 inode_key.offset = 0;
4801 other_inode = btrfs_iget(root->fs_info->sb,
4802 &inode_key, root,
4803 NULL);
4804 /*
4805 * If the other inode that had a conflicting dir
4806 * entry was deleted in the current transaction,
4807 * we don't need to do more work nor fallback to
4808 * a transaction commit.
4809 */
4810 if (IS_ERR(other_inode) &&
4811 PTR_ERR(other_inode) == -ENOENT) {
4812 goto next_key;
4813 } else if (IS_ERR(other_inode)) {
4814 err = PTR_ERR(other_inode);
4815 goto out_unlock;
4816 }
4817 /*
4818 * We are safe logging the other inode without
4819 * acquiring its i_mutex as long as we log with
4820 * the LOG_INODE_EXISTS mode. We're safe against
4821 * concurrent renames of the other inode as well
4822 * because during a rename we pin the log and
4823 * update the log with the new name before we
4824 * unpin it.
4825 */
4826 err = btrfs_log_inode(trans, root, other_inode,
Liu Bo67312122016-11-30 16:20:25 -08004827 LOG_OTHER_INODE,
Filipe Manana44f714d2016-06-06 16:11:13 +01004828 0, LLONG_MAX, ctx);
4829 iput(other_inode);
4830 if (err)
4831 goto out_unlock;
4832 else
4833 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01004834 }
4835 }
4836
Filipe Manana36283bf2015-06-20 00:44:51 +01004837 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
4838 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
4839 if (ins_nr == 0)
4840 goto next_slot;
4841 ret = copy_items(trans, inode, dst_path, path,
4842 &last_extent, ins_start_slot,
4843 ins_nr, inode_only, logged_isize);
4844 if (ret < 0) {
4845 err = ret;
4846 goto out_unlock;
4847 }
4848 ins_nr = 0;
4849 if (ret) {
4850 btrfs_release_path(path);
4851 continue;
4852 }
4853 goto next_slot;
4854 }
4855
Chris Masone02119d2008-09-05 16:13:11 -04004856 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004857 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4858 ins_nr++;
4859 goto next_slot;
4860 } else if (!ins_nr) {
4861 ins_start_slot = path->slots[0];
4862 ins_nr = 1;
4863 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004864 }
4865
Josef Bacik16e75492013-10-22 12:18:51 -04004866 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004867 ins_start_slot, ins_nr, inode_only,
4868 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004869 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004870 err = ret;
4871 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02004872 }
4873 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04004874 ins_nr = 0;
4875 btrfs_release_path(path);
4876 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004877 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004878 ins_nr = 1;
4879 ins_start_slot = path->slots[0];
4880next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004881
Chris Mason3a5f1d42008-09-11 15:53:37 -04004882 nritems = btrfs_header_nritems(path->nodes[0]);
4883 path->slots[0]++;
4884 if (path->slots[0] < nritems) {
4885 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4886 path->slots[0]);
4887 goto again;
4888 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004889 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004890 ret = copy_items(trans, inode, dst_path, path,
4891 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004892 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004893 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004894 err = ret;
4895 goto out_unlock;
4896 }
Josef Bacik16e75492013-10-22 12:18:51 -04004897 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004898 ins_nr = 0;
4899 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004900 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01004901next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004902 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004903 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004904 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004905 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004906 min_key.offset = 0;
4907 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004908 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004909 }
Chris Masone02119d2008-09-05 16:13:11 -04004910 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004911 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004912 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004913 ins_start_slot, ins_nr, inode_only,
4914 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004915 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004916 err = ret;
4917 goto out_unlock;
4918 }
Josef Bacik16e75492013-10-22 12:18:51 -04004919 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004920 ins_nr = 0;
4921 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004922
Filipe Manana36283bf2015-06-20 00:44:51 +01004923 btrfs_release_path(path);
4924 btrfs_release_path(dst_path);
4925 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
4926 if (err)
4927 goto out_unlock;
Filipe Manana92291242018-05-11 16:42:42 +01004928 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004929 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
4930 btrfs_release_path(path);
4931 btrfs_release_path(dst_path);
4932 err = btrfs_log_trailing_hole(trans, root, inode, path);
4933 if (err)
4934 goto out_unlock;
4935 }
Josef Bacika95249b2012-10-11 16:17:34 -04004936log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004937 btrfs_release_path(path);
4938 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01004939 if (need_log_inode_item) {
4940 err = log_inode_item(trans, log, dst_path, inode);
Filipe Manana92291242018-05-11 16:42:42 +01004941 if (!err && !xattrs_logged) {
4942 err = btrfs_log_all_xattrs(trans, root, inode, path,
4943 dst_path);
4944 btrfs_release_path(path);
4945 }
Filipe Mananae4545de2015-06-17 12:49:23 +01004946 if (err)
4947 goto out_unlock;
4948 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004949 if (fast_search) {
Miao Xie827463c2014-01-14 20:31:51 +08004950 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004951 &logged_list, ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004952 if (ret) {
4953 err = ret;
4954 goto out_unlock;
4955 }
Josef Bacikd006a042013-11-12 20:54:09 -05004956 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004957 struct extent_map *em, *n;
4958
Filipe Manana49dae1b2014-09-06 22:34:39 +01004959 write_lock(&em_tree->lock);
4960 /*
4961 * We can't just remove every em if we're called for a ranged
4962 * fsync - that is, one that doesn't cover the whole possible
4963 * file range (0 to LLONG_MAX). This is because we can have
4964 * em's that fall outside the range we're logging and therefore
4965 * their ordered operations haven't completed yet
4966 * (btrfs_finish_ordered_io() not invoked yet). This means we
4967 * didn't get their respective file extent item in the fs/subvol
4968 * tree yet, and need to let the next fast fsync (one which
4969 * consults the list of modified extent maps) find the em so
4970 * that it logs a matching file extent item and waits for the
4971 * respective ordered operation to complete (if it's still
4972 * running).
4973 *
4974 * Removing every em outside the range we're logging would make
4975 * the next fast fsync not log their matching file extent items,
4976 * therefore making us lose data after a log replay.
4977 */
4978 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4979 list) {
4980 const u64 mod_end = em->mod_start + em->mod_len - 1;
4981
4982 if (em->mod_start >= start && mod_end <= end)
4983 list_del_init(&em->list);
4984 }
4985 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004986 }
4987
Chris Mason9623f9a2008-09-11 17:42:42 -04004988 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004989 ret = log_directory_changes(trans, root, inode, path, dst_path,
4990 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004991 if (ret) {
4992 err = ret;
4993 goto out_unlock;
4994 }
Chris Masone02119d2008-09-05 16:13:11 -04004995 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01004996
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004997 spin_lock(&BTRFS_I(inode)->lock);
Filipe Manana125c4cf92014-09-11 21:22:14 +01004998 BTRFS_I(inode)->logged_trans = trans->transid;
4999 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005000 spin_unlock(&BTRFS_I(inode)->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005001out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08005002 if (unlikely(err))
5003 btrfs_put_logged_extents(&logged_list);
5004 else
5005 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04005006 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5007
5008 btrfs_free_path(path);
5009 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005010 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005011}
5012
Chris Mason12fcfd22009-03-24 10:24:20 -04005013/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005014 * Check if we must fallback to a transaction commit when logging an inode.
5015 * This must be called after logging the inode and is used only in the context
5016 * when fsyncing an inode requires the need to log some other inode - in which
5017 * case we can't lock the i_mutex of each other inode we need to log as that
5018 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5019 * log inodes up or down in the hierarchy) or rename operations for example. So
5020 * we take the log_mutex of the inode after we have logged it and then check for
5021 * its last_unlink_trans value - this is safe because any task setting
5022 * last_unlink_trans must take the log_mutex and it must do this before it does
5023 * the actual unlink operation, so if we do this check before a concurrent task
5024 * sets last_unlink_trans it means we've logged a consistent version/state of
5025 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005026 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005027 * we logged the inode or it might have also done the unlink).
5028 */
5029static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5030 struct inode *inode)
5031{
5032 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
5033 bool ret = false;
5034
5035 mutex_lock(&BTRFS_I(inode)->log_mutex);
5036 if (BTRFS_I(inode)->last_unlink_trans > fs_info->last_trans_committed) {
5037 /*
5038 * Make sure any commits to the log are forced to be full
5039 * commits.
5040 */
5041 btrfs_set_log_full_commit(fs_info, trans);
5042 ret = true;
5043 }
5044 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5045
5046 return ret;
5047}
5048
5049/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005050 * follow the dentry parent pointers up the chain and see if any
5051 * of the directories in it require a full commit before they can
5052 * be logged. Returns zero if nothing special needs to be done or 1 if
5053 * a full commit is required.
5054 */
5055static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5056 struct inode *inode,
5057 struct dentry *parent,
5058 struct super_block *sb,
5059 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005060{
Chris Mason12fcfd22009-03-24 10:24:20 -04005061 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005062 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04005063 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005064
Chris Masonaf4176b2009-03-24 10:24:31 -04005065 /*
5066 * for regular files, if its inode is already on disk, we don't
5067 * have to worry about the parents at all. This is because
5068 * we can use the last_unlink_trans field to record renames
5069 * and other fun in this file.
5070 */
5071 if (S_ISREG(inode->i_mode) &&
5072 BTRFS_I(inode)->generation <= last_committed &&
5073 BTRFS_I(inode)->last_unlink_trans <= last_committed)
5074 goto out;
5075
Chris Mason12fcfd22009-03-24 10:24:20 -04005076 if (!S_ISDIR(inode->i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005077 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005078 goto out;
David Howells2b0143b2015-03-17 22:25:59 +00005079 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005080 }
5081
5082 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005083 /*
5084 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005085 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005086 * logged_trans so that further down in the log code we don't
5087 * think this inode has already been logged.
5088 */
5089 if (inode != orig_inode)
5090 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005091 smp_mb();
5092
Filipe Manana2be63d52016-02-12 11:34:23 +00005093 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005094 ret = 1;
5095 break;
5096 }
5097
Al Virofc640052016-04-10 01:33:30 -04005098 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005099 break;
5100
Filipe Manana44f714d2016-06-06 16:11:13 +01005101 if (IS_ROOT(parent)) {
5102 inode = d_inode(parent);
5103 if (btrfs_must_commit_transaction(trans, inode))
5104 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005105 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005106 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005107
Josef Bacik6a912212010-11-20 09:48:00 +00005108 parent = dget_parent(parent);
5109 dput(old_parent);
5110 old_parent = parent;
David Howells2b0143b2015-03-17 22:25:59 +00005111 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005112
5113 }
Josef Bacik6a912212010-11-20 09:48:00 +00005114 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005115out:
Chris Masone02119d2008-09-05 16:13:11 -04005116 return ret;
5117}
5118
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005119struct btrfs_dir_list {
5120 u64 ino;
5121 struct list_head list;
5122};
5123
5124/*
5125 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5126 * details about the why it is needed.
5127 * This is a recursive operation - if an existing dentry corresponds to a
5128 * directory, that directory's new entries are logged too (same behaviour as
5129 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5130 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5131 * complains about the following circular lock dependency / possible deadlock:
5132 *
5133 * CPU0 CPU1
5134 * ---- ----
5135 * lock(&type->i_mutex_dir_key#3/2);
5136 * lock(sb_internal#2);
5137 * lock(&type->i_mutex_dir_key#3/2);
5138 * lock(&sb->s_type->i_mutex_key#14);
5139 *
5140 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5141 * sb_start_intwrite() in btrfs_start_transaction().
5142 * Not locking i_mutex of the inodes is still safe because:
5143 *
5144 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5145 * that while logging the inode new references (names) are added or removed
5146 * from the inode, leaving the logged inode item with a link count that does
5147 * not match the number of logged inode reference items. This is fine because
5148 * at log replay time we compute the real number of links and correct the
5149 * link count in the inode item (see replay_one_buffer() and
5150 * link_to_fixup_dir());
5151 *
5152 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5153 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5154 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5155 * has a size that doesn't match the sum of the lengths of all the logged
5156 * names. This does not result in a problem because if a dir_item key is
5157 * logged but its matching dir_index key is not logged, at log replay time we
5158 * don't use it to replay the respective name (see replay_one_name()). On the
5159 * other hand if only the dir_index key ends up being logged, the respective
5160 * name is added to the fs/subvol tree with both the dir_item and dir_index
5161 * keys created (see replay_one_name()).
5162 * The directory's inode item with a wrong i_size is not a problem as well,
5163 * since we don't use it at log replay time to set the i_size in the inode
5164 * item of the fs/subvol tree (see overwrite_item()).
5165 */
5166static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5167 struct btrfs_root *root,
5168 struct inode *start_inode,
5169 struct btrfs_log_ctx *ctx)
5170{
5171 struct btrfs_root *log = root->log_root;
5172 struct btrfs_path *path;
5173 LIST_HEAD(dir_list);
5174 struct btrfs_dir_list *dir_elem;
5175 int ret = 0;
5176
5177 path = btrfs_alloc_path();
5178 if (!path)
5179 return -ENOMEM;
5180
5181 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5182 if (!dir_elem) {
5183 btrfs_free_path(path);
5184 return -ENOMEM;
5185 }
5186 dir_elem->ino = btrfs_ino(start_inode);
5187 list_add_tail(&dir_elem->list, &dir_list);
5188
5189 while (!list_empty(&dir_list)) {
5190 struct extent_buffer *leaf;
5191 struct btrfs_key min_key;
5192 int nritems;
5193 int i;
5194
5195 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5196 list);
5197 if (ret)
5198 goto next_dir_inode;
5199
5200 min_key.objectid = dir_elem->ino;
5201 min_key.type = BTRFS_DIR_ITEM_KEY;
5202 min_key.offset = 0;
5203again:
5204 btrfs_release_path(path);
5205 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5206 if (ret < 0) {
5207 goto next_dir_inode;
5208 } else if (ret > 0) {
5209 ret = 0;
5210 goto next_dir_inode;
5211 }
5212
5213process_leaf:
5214 leaf = path->nodes[0];
5215 nritems = btrfs_header_nritems(leaf);
5216 for (i = path->slots[0]; i < nritems; i++) {
5217 struct btrfs_dir_item *di;
5218 struct btrfs_key di_key;
5219 struct inode *di_inode;
5220 struct btrfs_dir_list *new_dir_elem;
5221 int log_mode = LOG_INODE_EXISTS;
5222 int type;
5223
5224 btrfs_item_key_to_cpu(leaf, &min_key, i);
5225 if (min_key.objectid != dir_elem->ino ||
5226 min_key.type != BTRFS_DIR_ITEM_KEY)
5227 goto next_dir_inode;
5228
5229 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5230 type = btrfs_dir_type(leaf, di);
5231 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5232 type != BTRFS_FT_DIR)
5233 continue;
5234 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5235 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5236 continue;
5237
Robbie Ko664b0532016-10-28 10:48:26 +08005238 btrfs_release_path(path);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005239 di_inode = btrfs_iget(root->fs_info->sb, &di_key,
5240 root, NULL);
5241 if (IS_ERR(di_inode)) {
5242 ret = PTR_ERR(di_inode);
5243 goto next_dir_inode;
5244 }
5245
5246 if (btrfs_inode_in_log(di_inode, trans->transid)) {
5247 iput(di_inode);
Robbie Ko664b0532016-10-28 10:48:26 +08005248 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005249 }
5250
5251 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005252 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005253 log_mode = LOG_INODE_ALL;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005254 ret = btrfs_log_inode(trans, root, di_inode,
5255 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005256 if (!ret &&
5257 btrfs_must_commit_transaction(trans, di_inode))
5258 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005259 iput(di_inode);
5260 if (ret)
5261 goto next_dir_inode;
5262 if (ctx->log_new_dentries) {
5263 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5264 GFP_NOFS);
5265 if (!new_dir_elem) {
5266 ret = -ENOMEM;
5267 goto next_dir_inode;
5268 }
5269 new_dir_elem->ino = di_key.objectid;
5270 list_add_tail(&new_dir_elem->list, &dir_list);
5271 }
5272 break;
5273 }
5274 if (i == nritems) {
5275 ret = btrfs_next_leaf(log, path);
5276 if (ret < 0) {
5277 goto next_dir_inode;
5278 } else if (ret > 0) {
5279 ret = 0;
5280 goto next_dir_inode;
5281 }
5282 goto process_leaf;
5283 }
5284 if (min_key.offset < (u64)-1) {
5285 min_key.offset++;
5286 goto again;
5287 }
5288next_dir_inode:
5289 list_del(&dir_elem->list);
5290 kfree(dir_elem);
5291 }
5292
5293 btrfs_free_path(path);
5294 return ret;
5295}
5296
Filipe Manana18aa0922015-08-05 16:49:08 +01005297static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5298 struct inode *inode,
5299 struct btrfs_log_ctx *ctx)
5300{
5301 int ret;
5302 struct btrfs_path *path;
5303 struct btrfs_key key;
5304 struct btrfs_root *root = BTRFS_I(inode)->root;
5305 const u64 ino = btrfs_ino(inode);
5306
5307 path = btrfs_alloc_path();
5308 if (!path)
5309 return -ENOMEM;
5310 path->skip_locking = 1;
5311 path->search_commit_root = 1;
5312
5313 key.objectid = ino;
5314 key.type = BTRFS_INODE_REF_KEY;
5315 key.offset = 0;
5316 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5317 if (ret < 0)
5318 goto out;
5319
5320 while (true) {
5321 struct extent_buffer *leaf = path->nodes[0];
5322 int slot = path->slots[0];
5323 u32 cur_offset = 0;
5324 u32 item_size;
5325 unsigned long ptr;
5326
5327 if (slot >= btrfs_header_nritems(leaf)) {
5328 ret = btrfs_next_leaf(root, path);
5329 if (ret < 0)
5330 goto out;
5331 else if (ret > 0)
5332 break;
5333 continue;
5334 }
5335
5336 btrfs_item_key_to_cpu(leaf, &key, slot);
5337 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5338 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5339 break;
5340
5341 item_size = btrfs_item_size_nr(leaf, slot);
5342 ptr = btrfs_item_ptr_offset(leaf, slot);
5343 while (cur_offset < item_size) {
5344 struct btrfs_key inode_key;
5345 struct inode *dir_inode;
5346
5347 inode_key.type = BTRFS_INODE_ITEM_KEY;
5348 inode_key.offset = 0;
5349
5350 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5351 struct btrfs_inode_extref *extref;
5352
5353 extref = (struct btrfs_inode_extref *)
5354 (ptr + cur_offset);
5355 inode_key.objectid = btrfs_inode_extref_parent(
5356 leaf, extref);
5357 cur_offset += sizeof(*extref);
5358 cur_offset += btrfs_inode_extref_name_len(leaf,
5359 extref);
5360 } else {
5361 inode_key.objectid = key.offset;
5362 cur_offset = item_size;
5363 }
5364
5365 dir_inode = btrfs_iget(root->fs_info->sb, &inode_key,
5366 root, NULL);
5367 /* If parent inode was deleted, skip it. */
5368 if (IS_ERR(dir_inode))
5369 continue;
5370
Filipe Manana657ed1a2016-04-06 17:11:56 +01005371 if (ctx)
5372 ctx->log_new_dentries = false;
Filipe Manana18aa0922015-08-05 16:49:08 +01005373 ret = btrfs_log_inode(trans, root, dir_inode,
5374 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005375 if (!ret &&
5376 btrfs_must_commit_transaction(trans, dir_inode))
5377 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005378 if (!ret && ctx && ctx->log_new_dentries)
5379 ret = log_new_dir_dentries(trans, root,
5380 dir_inode, ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005381 iput(dir_inode);
5382 if (ret)
5383 goto out;
5384 }
5385 path->slots[0]++;
5386 }
5387 ret = 0;
5388out:
5389 btrfs_free_path(path);
5390 return ret;
5391}
5392
Chris Masone02119d2008-09-05 16:13:11 -04005393/*
5394 * helper function around btrfs_log_inode to make sure newly created
5395 * parent directories also end up in the log. A minimal inode and backref
5396 * only logging is done of any parent directories that are older than
5397 * the last committed transaction
5398 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005399static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5400 struct btrfs_root *root, struct inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005401 struct dentry *parent,
5402 const loff_t start,
5403 const loff_t end,
5404 int exists_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005405 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005406{
Chris Mason12fcfd22009-03-24 10:24:20 -04005407 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04005408 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005409 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005410 int ret = 0;
5411 u64 last_committed = root->fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005412 bool log_dentries = false;
5413 struct inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005414
5415 sb = inode->i_sb;
5416
Jeff Mahoney3cdde222016-06-09 21:38:35 -04005417 if (btrfs_test_opt(root->fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005418 ret = 1;
5419 goto end_no_trans;
5420 }
5421
Miao Xie995946d2014-04-02 19:51:06 +08005422 /*
5423 * The prev transaction commit doesn't complete, we need do
5424 * full commit by ourselves.
5425 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005426 if (root->fs_info->last_trans_log_full_commit >
5427 root->fs_info->last_trans_committed) {
5428 ret = 1;
5429 goto end_no_trans;
5430 }
5431
Yan, Zheng76dda932009-09-21 16:00:26 -04005432 if (root != BTRFS_I(inode)->root ||
5433 btrfs_root_refs(&root->root_item) == 0) {
5434 ret = 1;
5435 goto end_no_trans;
5436 }
5437
Chris Mason12fcfd22009-03-24 10:24:20 -04005438 ret = check_parent_dirs_for_sync(trans, inode, parent,
5439 sb, last_committed);
5440 if (ret)
5441 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005442
Josef Bacik22ee6982012-05-29 16:57:49 -04005443 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005444 ret = BTRFS_NO_LOG_SYNC;
5445 goto end_no_trans;
5446 }
5447
Miao Xie8b050d32014-02-20 18:08:58 +08005448 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005449 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005450 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005451
Filipe Manana8407f552014-09-05 15:14:39 +01005452 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005453 if (ret)
5454 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005455
Chris Masonaf4176b2009-03-24 10:24:31 -04005456 /*
5457 * for regular files, if its inode is already on disk, we don't
5458 * have to worry about the parents at all. This is because
5459 * we can use the last_unlink_trans field to record renames
5460 * and other fun in this file.
5461 */
5462 if (S_ISREG(inode->i_mode) &&
5463 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005464 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
5465 ret = 0;
5466 goto end_trans;
5467 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005468
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005469 if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
5470 log_dentries = true;
5471
Filipe Manana18aa0922015-08-05 16:49:08 +01005472 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005473 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005474 * inodes are fully logged. This is to prevent leaving dangling
5475 * directory index entries in directories that were our parents but are
5476 * not anymore. Not doing this results in old parent directory being
5477 * impossible to delete after log replay (rmdir will always fail with
5478 * error -ENOTEMPTY).
5479 *
5480 * Example 1:
5481 *
5482 * mkdir testdir
5483 * touch testdir/foo
5484 * ln testdir/foo testdir/bar
5485 * sync
5486 * unlink testdir/bar
5487 * xfs_io -c fsync testdir/foo
5488 * <power failure>
5489 * mount fs, triggers log replay
5490 *
5491 * If we don't log the parent directory (testdir), after log replay the
5492 * directory still has an entry pointing to the file inode using the bar
5493 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5494 * the file inode has a link count of 1.
5495 *
5496 * Example 2:
5497 *
5498 * mkdir testdir
5499 * touch foo
5500 * ln foo testdir/foo2
5501 * ln foo testdir/foo3
5502 * sync
5503 * unlink testdir/foo3
5504 * xfs_io -c fsync foo
5505 * <power failure>
5506 * mount fs, triggers log replay
5507 *
5508 * Similar as the first example, after log replay the parent directory
5509 * testdir still has an entry pointing to the inode file with name foo3
5510 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5511 * and has a link count of 2.
5512 */
5513 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
5514 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5515 if (ret)
5516 goto end_trans;
5517 }
5518
Chris Masond3977122009-01-05 21:25:51 -05005519 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005520 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005521 break;
5522
David Howells2b0143b2015-03-17 22:25:59 +00005523 inode = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005524 if (root != BTRFS_I(inode)->root)
5525 break;
5526
Filipe Manana18aa0922015-08-05 16:49:08 +01005527 if (BTRFS_I(inode)->generation > last_committed) {
5528 ret = btrfs_log_inode(trans, root, inode,
5529 LOG_INODE_EXISTS,
Filipe Manana8407f552014-09-05 15:14:39 +01005530 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005531 if (ret)
5532 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005533 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005534 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005535 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005536
Josef Bacik6a912212010-11-20 09:48:00 +00005537 parent = dget_parent(parent);
5538 dput(old_parent);
5539 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005540 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005541 if (log_dentries)
5542 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5543 else
5544 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005545end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005546 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005547 if (ret < 0) {
Miao Xie995946d2014-04-02 19:51:06 +08005548 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005549 ret = 1;
5550 }
Miao Xie8b050d32014-02-20 18:08:58 +08005551
5552 if (ret)
5553 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005554 btrfs_end_log_trans(root);
5555end_no_trans:
5556 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005557}
5558
5559/*
5560 * it is not safe to log dentry if the chunk root has added new
5561 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5562 * If this returns 1, you must commit the transaction to safely get your
5563 * data on disk.
5564 */
5565int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08005566 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005567 const loff_t start,
5568 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005569 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005570{
Josef Bacik6a912212010-11-20 09:48:00 +00005571 struct dentry *parent = dget_parent(dentry);
5572 int ret;
5573
David Howells2b0143b2015-03-17 22:25:59 +00005574 ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005575 start, end, 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005576 dput(parent);
5577
5578 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005579}
5580
5581/*
5582 * should be called during mount to recover any replay any log trees
5583 * from the FS
5584 */
5585int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5586{
5587 int ret;
5588 struct btrfs_path *path;
5589 struct btrfs_trans_handle *trans;
5590 struct btrfs_key key;
5591 struct btrfs_key found_key;
5592 struct btrfs_key tmp_key;
5593 struct btrfs_root *log;
5594 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5595 struct walk_control wc = {
5596 .process_func = process_one_buffer,
5597 .stage = 0,
5598 };
5599
Chris Masone02119d2008-09-05 16:13:11 -04005600 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005601 if (!path)
5602 return -ENOMEM;
5603
Josef Bacikafcdd122016-09-02 15:40:02 -04005604 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005605
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005606 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005607 if (IS_ERR(trans)) {
5608 ret = PTR_ERR(trans);
5609 goto error;
5610 }
Chris Masone02119d2008-09-05 16:13:11 -04005611
5612 wc.trans = trans;
5613 wc.pin = 1;
5614
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005615 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005616 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005617 btrfs_handle_fs_error(fs_info, ret,
5618 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005619 goto error;
5620 }
Chris Masone02119d2008-09-05 16:13:11 -04005621
5622again:
5623 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5624 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005625 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005626
Chris Masond3977122009-01-05 21:25:51 -05005627 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005628 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005629
5630 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005631 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005632 "Couldn't find tree log root.");
5633 goto error;
5634 }
Chris Masone02119d2008-09-05 16:13:11 -04005635 if (ret > 0) {
5636 if (path->slots[0] == 0)
5637 break;
5638 path->slots[0]--;
5639 }
5640 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5641 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005642 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005643 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5644 break;
5645
Miao Xiecb517ea2013-05-15 07:48:19 +00005646 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005647 if (IS_ERR(log)) {
5648 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005649 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005650 "Couldn't read tree log root.");
5651 goto error;
5652 }
Chris Masone02119d2008-09-05 16:13:11 -04005653
5654 tmp_key.objectid = found_key.offset;
5655 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5656 tmp_key.offset = (u64)-1;
5657
5658 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005659 if (IS_ERR(wc.replay_dest)) {
5660 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005661 free_extent_buffer(log->node);
5662 free_extent_buffer(log->commit_root);
5663 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005664 btrfs_handle_fs_error(fs_info, ret,
5665 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005666 goto error;
5667 }
Chris Masone02119d2008-09-05 16:13:11 -04005668
Yan Zheng07d400a2009-01-06 11:42:00 -05005669 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005670 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005671 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005672
Josef Bacikb50c6e22013-04-25 15:55:30 -04005673 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005674 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5675 path);
Chris Masone02119d2008-09-05 16:13:11 -04005676 }
Chris Masone02119d2008-09-05 16:13:11 -04005677
Liu Boefb1cbc2018-01-25 11:02:56 -07005678 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5679 struct btrfs_root *root = wc.replay_dest;
5680
5681 btrfs_release_path(path);
5682
5683 /*
5684 * We have just replayed everything, and the highest
5685 * objectid of fs roots probably has changed in case
5686 * some inode_item's got replayed.
5687 *
5688 * root->objectid_mutex is not acquired as log replay
5689 * could only happen during mount.
5690 */
5691 ret = btrfs_find_highest_objectid(root,
5692 &root->highest_objectid);
5693 }
5694
Chris Masone02119d2008-09-05 16:13:11 -04005695 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005696 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005697 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005698 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005699 kfree(log);
5700
Josef Bacikb50c6e22013-04-25 15:55:30 -04005701 if (ret)
5702 goto error;
5703
Chris Masone02119d2008-09-05 16:13:11 -04005704 if (found_key.offset == 0)
5705 break;
5706 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005707 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005708
5709 /* step one is to pin it all, step two is to replay just inodes */
5710 if (wc.pin) {
5711 wc.pin = 0;
5712 wc.process_func = replay_one_buffer;
5713 wc.stage = LOG_WALK_REPLAY_INODES;
5714 goto again;
5715 }
5716 /* step three is to replay everything */
5717 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5718 wc.stage++;
5719 goto again;
5720 }
5721
5722 btrfs_free_path(path);
5723
Josef Bacikabefa552013-04-24 16:40:05 -04005724 /* step 4: commit the transaction, which also unpins the blocks */
5725 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
5726 if (ret)
5727 return ret;
5728
Chris Masone02119d2008-09-05 16:13:11 -04005729 free_extent_buffer(log_root_tree->node);
5730 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005731 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005732 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005733
Josef Bacikabefa552013-04-24 16:40:05 -04005734 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005735error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005736 if (wc.trans)
5737 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005738 btrfs_free_path(path);
5739 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005740}
Chris Mason12fcfd22009-03-24 10:24:20 -04005741
5742/*
5743 * there are some corner cases where we want to force a full
5744 * commit instead of allowing a directory to be logged.
5745 *
5746 * They revolve around files there were unlinked from the directory, and
5747 * this function updates the parent directory so that a full commit is
5748 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005749 *
5750 * Must be called before the unlink operations (updates to the subvolume tree,
5751 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005752 */
5753void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
5754 struct inode *dir, struct inode *inode,
5755 int for_rename)
5756{
5757 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005758 * when we're logging a file, if it hasn't been renamed
5759 * or unlinked, and its inode is fully committed on disk,
5760 * we don't have to worry about walking up the directory chain
5761 * to log its parents.
5762 *
5763 * So, we use the last_unlink_trans field to put this transid
5764 * into the file. When the file is logged we check it and
5765 * don't log the parents if the file is fully on disk.
5766 */
Filipe Manana657ed1a2016-04-06 17:11:56 +01005767 mutex_lock(&BTRFS_I(inode)->log_mutex);
5768 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5769 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04005770
5771 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005772 * if this directory was already logged any new
5773 * names for this file/dir will get recorded
5774 */
5775 smp_mb();
5776 if (BTRFS_I(dir)->logged_trans == trans->transid)
5777 return;
5778
5779 /*
5780 * if the inode we're about to unlink was logged,
5781 * the log will be properly updated for any new names
5782 */
5783 if (BTRFS_I(inode)->logged_trans == trans->transid)
5784 return;
5785
5786 /*
5787 * when renaming files across directories, if the directory
5788 * there we're unlinking from gets fsync'd later on, there's
5789 * no way to find the destination directory later and fsync it
5790 * properly. So, we have to be conservative and force commits
5791 * so the new name gets discovered.
5792 */
5793 if (for_rename)
5794 goto record;
5795
5796 /* we can safely do the unlink without any special recording */
5797 return;
5798
5799record:
Filipe Manana2be63d52016-02-12 11:34:23 +00005800 mutex_lock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005801 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005802 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005803}
5804
5805/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005806 * Make sure that if someone attempts to fsync the parent directory of a deleted
5807 * snapshot, it ends up triggering a transaction commit. This is to guarantee
5808 * that after replaying the log tree of the parent directory's root we will not
5809 * see the snapshot anymore and at log replay time we will not see any log tree
5810 * corresponding to the deleted snapshot's root, which could lead to replaying
5811 * it after replaying the log tree of the parent directory (which would replay
5812 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00005813 *
5814 * Must be called before the actual snapshot destroy operation (updates to the
5815 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005816 */
5817void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
5818 struct inode *dir)
5819{
Filipe Manana2be63d52016-02-12 11:34:23 +00005820 mutex_lock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005821 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005822 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005823}
5824
5825/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005826 * Call this after adding a new name for a file and it will properly
5827 * update the log to reflect the new name.
5828 *
5829 * It will return zero if all goes well, and it will return 1 if a
5830 * full transaction commit is required.
5831 */
5832int btrfs_log_new_name(struct btrfs_trans_handle *trans,
5833 struct inode *inode, struct inode *old_dir,
5834 struct dentry *parent)
5835{
5836 struct btrfs_root * root = BTRFS_I(inode)->root;
5837
5838 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005839 * this will force the logging code to walk the dentry chain
5840 * up for the file
5841 */
5842 if (S_ISREG(inode->i_mode))
5843 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5844
5845 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005846 * if this inode hasn't been logged and directory we're renaming it
5847 * from hasn't been logged, we don't need to log it
5848 */
5849 if (BTRFS_I(inode)->logged_trans <=
5850 root->fs_info->last_trans_committed &&
5851 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
5852 root->fs_info->last_trans_committed))
5853 return 0;
5854
Filipe Manana49dae1b2014-09-06 22:34:39 +01005855 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
5856 LLONG_MAX, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04005857}
5858