blob: c03891f9d96e0e21bc10ccdbd059f767e0521a6b [file] [log] [blame]
Chris Masone02119d2008-09-05 16:13:11 -04001/*
2 * Copyright (C) 2008 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Miao Xiec6adc9c2013-05-28 10:05:39 +000021#include <linux/blkdev.h>
Josef Bacik5dc562c2012-08-17 13:14:17 -040022#include <linux/list_sort.h>
Miao Xie995946d2014-04-02 19:51:06 +080023#include "tree-log.h"
Chris Masone02119d2008-09-05 16:13:11 -040024#include "disk-io.h"
25#include "locking.h"
26#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070027#include "backref.h"
Mark Fashehf1863732012-08-08 11:32:27 -070028#include "hash.h"
Anand Jainebb87652016-03-10 17:26:59 +080029#include "compression.h"
Qu Wenruodf2c95f2016-08-15 10:36:52 +080030#include "qgroup.h"
Liu Boefb1cbc2018-01-25 11:02:56 -070031#include "inode-map.h"
Chris Masone02119d2008-09-05 16:13:11 -040032
33/* magic values for the inode_only field in btrfs_log_inode:
34 *
35 * LOG_INODE_ALL means to log everything
36 * LOG_INODE_EXISTS means to log just enough to recreate the inode
37 * during log replay
38 */
39#define LOG_INODE_ALL 0
40#define LOG_INODE_EXISTS 1
Liu Bo67312122016-11-30 16:20:25 -080041#define LOG_OTHER_INODE 2
Chris Masone02119d2008-09-05 16:13:11 -040042
43/*
Chris Mason12fcfd22009-03-24 10:24:20 -040044 * directory trouble cases
45 *
46 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
47 * log, we must force a full commit before doing an fsync of the directory
48 * where the unlink was done.
49 * ---> record transid of last unlink/rename per directory
50 *
51 * mkdir foo/some_dir
52 * normal commit
53 * rename foo/some_dir foo2/some_dir
54 * mkdir foo/some_dir
55 * fsync foo/some_dir/some_file
56 *
57 * The fsync above will unlink the original some_dir without recording
58 * it in its new location (foo2). After a crash, some_dir will be gone
59 * unless the fsync of some_file forces a full commit
60 *
61 * 2) we must log any new names for any file or dir that is in the fsync
62 * log. ---> check inode while renaming/linking.
63 *
64 * 2a) we must log any new names for any file or dir during rename
65 * when the directory they are being removed from was logged.
66 * ---> check inode and old parent dir during rename
67 *
68 * 2a is actually the more important variant. With the extra logging
69 * a crash might unlink the old name without recreating the new one
70 *
71 * 3) after a crash, we must go through any directories with a link count
72 * of zero and redo the rm -rf
73 *
74 * mkdir f1/foo
75 * normal commit
76 * rm -rf f1/foo
77 * fsync(f1)
78 *
79 * The directory f1 was fully removed from the FS, but fsync was never
80 * called on f1, only its parent dir. After a crash the rm -rf must
81 * be replayed. This must be able to recurse down the entire
82 * directory tree. The inode link count fixup code takes care of the
83 * ugly details.
84 */
85
86/*
Chris Masone02119d2008-09-05 16:13:11 -040087 * stages for the tree walking. The first
88 * stage (0) is to only pin down the blocks we find
89 * the second stage (1) is to make sure that all the inodes
90 * we find in the log are created in the subvolume.
91 *
92 * The last stage is to deal with directories and links and extents
93 * and all the other fun semantics
94 */
95#define LOG_WALK_PIN_ONLY 0
96#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040097#define LOG_WALK_REPLAY_DIR_INDEX 2
98#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040099
Chris Mason12fcfd22009-03-24 10:24:20 -0400100static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +0100101 struct btrfs_root *root, struct inode *inode,
102 int inode_only,
103 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +0100104 const loff_t end,
105 struct btrfs_log_ctx *ctx);
Yan Zhengec051c02009-01-05 15:43:42 -0500106static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
107 struct btrfs_root *root,
108 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400109static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
110 struct btrfs_root *root,
111 struct btrfs_root *log,
112 struct btrfs_path *path,
113 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400114
115/*
116 * tree logging is a special write ahead log used to make sure that
117 * fsyncs and O_SYNCs can happen without doing full tree commits.
118 *
119 * Full tree commits are expensive because they require commonly
120 * modified blocks to be recowed, creating many dirty pages in the
121 * extent tree an 4x-6x higher write load than ext3.
122 *
123 * Instead of doing a tree commit on every fsync, we use the
124 * key ranges and transaction ids to find items for a given file or directory
125 * that have changed in this transaction. Those items are copied into
126 * a special tree (one per subvolume root), that tree is written to disk
127 * and then the fsync is considered complete.
128 *
129 * After a crash, items are copied out of the log-tree back into the
130 * subvolume tree. Any file data extents found are recorded in the extent
131 * allocation tree, and the log-tree freed.
132 *
133 * The log tree is read three times, once to pin down all the extents it is
134 * using in ram and once, once to create all the inodes logged in the tree
135 * and once to do all the other items.
136 */
137
138/*
Chris Masone02119d2008-09-05 16:13:11 -0400139 * start a sub transaction and setup the log tree
140 * this increments the log tree writer count to make the people
141 * syncing the tree wait for us to finish
142 */
143static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800144 struct btrfs_root *root,
145 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400146{
Zhaolei34eb2a52015-08-17 18:44:45 +0800147 int ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500148
149 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800150
Yan Zheng7237f182009-01-21 12:54:03 -0500151 if (root->log_root) {
Miao Xie995946d2014-04-02 19:51:06 +0800152 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800153 ret = -EAGAIN;
154 goto out;
155 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800156
Josef Bacikff782e02009-10-08 15:30:04 -0400157 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800158 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800159 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400160 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800161 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400162 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800163 } else {
164 mutex_lock(&root->fs_info->tree_log_mutex);
165 if (!root->fs_info->log_root_tree)
166 ret = btrfs_init_log_root_tree(trans, root->fs_info);
167 mutex_unlock(&root->fs_info->tree_log_mutex);
168 if (ret)
169 goto out;
Josef Bacikff782e02009-10-08 15:30:04 -0400170
Chris Masone02119d2008-09-05 16:13:11 -0400171 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400172 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800173 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800174
175 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
176 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400177 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800178
Miao Xie2ecb7922012-09-06 04:04:27 -0600179 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500180 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800181 if (ctx) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800182 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800183 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800184 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800185 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800186
Miao Xiee87ac132014-02-20 18:08:53 +0800187out:
Yan Zheng7237f182009-01-21 12:54:03 -0500188 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800189 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400190}
191
192/*
193 * returns 0 if there was a log transaction running and we were able
194 * to join, or returns -ENOENT if there were not transactions
195 * in progress
196 */
197static int join_running_log_trans(struct btrfs_root *root)
198{
199 int ret = -ENOENT;
200
201 smp_mb();
202 if (!root->log_root)
203 return -ENOENT;
204
Yan Zheng7237f182009-01-21 12:54:03 -0500205 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400206 if (root->log_root) {
207 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500208 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400209 }
Yan Zheng7237f182009-01-21 12:54:03 -0500210 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400211 return ret;
212}
213
214/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400215 * This either makes the current running log transaction wait
216 * until you call btrfs_end_log_trans() or it makes any future
217 * log transactions wait until you call btrfs_end_log_trans()
218 */
219int btrfs_pin_log_trans(struct btrfs_root *root)
220{
221 int ret = -ENOENT;
222
223 mutex_lock(&root->log_mutex);
224 atomic_inc(&root->log_writers);
225 mutex_unlock(&root->log_mutex);
226 return ret;
227}
228
229/*
Chris Masone02119d2008-09-05 16:13:11 -0400230 * indicate we're done making changes to the log tree
231 * and wake up anyone waiting to do a sync
232 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100233void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400234{
Yan Zheng7237f182009-01-21 12:54:03 -0500235 if (atomic_dec_and_test(&root->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +0100236 /*
237 * Implicit memory barrier after atomic_dec_and_test
238 */
Yan Zheng7237f182009-01-21 12:54:03 -0500239 if (waitqueue_active(&root->log_writer_wait))
240 wake_up(&root->log_writer_wait);
241 }
Chris Masone02119d2008-09-05 16:13:11 -0400242}
243
244
245/*
246 * the walk control struct is used to pass state down the chain when
247 * processing the log tree. The stage field tells us which part
248 * of the log tree processing we are currently doing. The others
249 * are state fields used for that specific part
250 */
251struct walk_control {
252 /* should we free the extent on disk when done? This is used
253 * at transaction commit time while freeing a log tree
254 */
255 int free;
256
257 /* should we write out the extent buffer? This is used
258 * while flushing the log tree to disk during a sync
259 */
260 int write;
261
262 /* should we wait for the extent buffer io to finish? Also used
263 * while flushing the log tree to disk for a sync
264 */
265 int wait;
266
267 /* pin only walk, we record which extents on disk belong to the
268 * log trees
269 */
270 int pin;
271
272 /* what stage of the replay code we're currently in */
273 int stage;
274
275 /* the root we are currently replaying */
276 struct btrfs_root *replay_dest;
277
278 /* the trans handle for the current replay */
279 struct btrfs_trans_handle *trans;
280
281 /* the function that gets used to process blocks we find in the
282 * tree. Note the extent_buffer might not be up to date when it is
283 * passed in, and it must be checked or read if you need the data
284 * inside it
285 */
286 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
287 struct walk_control *wc, u64 gen);
288};
289
290/*
291 * process_func used to pin down extents, write them or wait on them
292 */
293static int process_one_buffer(struct btrfs_root *log,
294 struct extent_buffer *eb,
295 struct walk_control *wc, u64 gen)
296{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400297 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400298
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400299 /*
300 * If this fs is mixed then we need to be able to process the leaves to
301 * pin down any logged extents, so we have to read the block.
302 */
303 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
304 ret = btrfs_read_buffer(eb, gen);
305 if (ret)
306 return ret;
307 }
308
Josef Bacikb50c6e22013-04-25 15:55:30 -0400309 if (wc->pin)
310 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
311 eb->start, eb->len);
312
313 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400314 if (wc->pin && btrfs_header_level(eb) == 0)
315 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400316 if (wc->write)
317 btrfs_write_tree_block(eb);
318 if (wc->wait)
319 btrfs_wait_tree_block_writeback(eb);
320 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400321 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400322}
323
324/*
325 * Item overwrite used by replay and tree logging. eb, slot and key all refer
326 * to the src data we are copying out.
327 *
328 * root is the tree we are copying into, and path is a scratch
329 * path for use in this function (it should be released on entry and
330 * will be released on exit).
331 *
332 * If the key is already in the destination tree the existing item is
333 * overwritten. If the existing item isn't big enough, it is extended.
334 * If it is too large, it is truncated.
335 *
336 * If the key isn't in the destination yet, a new item is inserted.
337 */
338static noinline int overwrite_item(struct btrfs_trans_handle *trans,
339 struct btrfs_root *root,
340 struct btrfs_path *path,
341 struct extent_buffer *eb, int slot,
342 struct btrfs_key *key)
343{
344 int ret;
345 u32 item_size;
346 u64 saved_i_size = 0;
347 int save_old_i_size = 0;
348 unsigned long src_ptr;
349 unsigned long dst_ptr;
350 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000351 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400352
353 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
354 overwrite_root = 1;
355
356 item_size = btrfs_item_size_nr(eb, slot);
357 src_ptr = btrfs_item_ptr_offset(eb, slot);
358
359 /* look for the key in the destination tree */
360 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000361 if (ret < 0)
362 return ret;
363
Chris Masone02119d2008-09-05 16:13:11 -0400364 if (ret == 0) {
365 char *src_copy;
366 char *dst_copy;
367 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
368 path->slots[0]);
369 if (dst_size != item_size)
370 goto insert;
371
372 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200373 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400374 return 0;
375 }
376 dst_copy = kmalloc(item_size, GFP_NOFS);
377 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000378 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200379 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000380 kfree(dst_copy);
381 kfree(src_copy);
382 return -ENOMEM;
383 }
Chris Masone02119d2008-09-05 16:13:11 -0400384
385 read_extent_buffer(eb, src_copy, src_ptr, item_size);
386
387 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
388 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
389 item_size);
390 ret = memcmp(dst_copy, src_copy, item_size);
391
392 kfree(dst_copy);
393 kfree(src_copy);
394 /*
395 * they have the same contents, just return, this saves
396 * us from cowing blocks in the destination tree and doing
397 * extra writes that may not have been done by a previous
398 * sync
399 */
400 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200401 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400402 return 0;
403 }
404
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000405 /*
406 * We need to load the old nbytes into the inode so when we
407 * replay the extents we've logged we get the right nbytes.
408 */
409 if (inode_item) {
410 struct btrfs_inode_item *item;
411 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400412 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000413
414 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
415 struct btrfs_inode_item);
416 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
417 item = btrfs_item_ptr(eb, slot,
418 struct btrfs_inode_item);
419 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400420
421 /*
422 * If this is a directory we need to reset the i_size to
423 * 0 so that we can set it up properly when replaying
424 * the rest of the items in this log.
425 */
426 mode = btrfs_inode_mode(eb, item);
427 if (S_ISDIR(mode))
428 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000429 }
430 } else if (inode_item) {
431 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400432 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000433
434 /*
435 * New inode, set nbytes to 0 so that the nbytes comes out
436 * properly when we replay the extents.
437 */
438 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
439 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400440
441 /*
442 * If this is a directory we need to reset the i_size to 0 so
443 * that we can set it up properly when replaying the rest of
444 * the items in this log.
445 */
446 mode = btrfs_inode_mode(eb, item);
447 if (S_ISDIR(mode))
448 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400449 }
450insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200451 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400452 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000453 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400454 ret = btrfs_insert_empty_item(trans, root, path,
455 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000456 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400457
458 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000459 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400460 u32 found_size;
461 found_size = btrfs_item_size_nr(path->nodes[0],
462 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100463 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000464 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100465 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000466 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100467 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400468 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400469 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400470 }
471 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
472 path->slots[0]);
473
474 /* don't overwrite an existing inode if the generation number
475 * was logged as zero. This is done when the tree logging code
476 * is just logging an inode to make sure it exists after recovery.
477 *
478 * Also, don't overwrite i_size on directories during replay.
479 * log replay inserts and removes directory items based on the
480 * state of the tree found in the subvolume, and i_size is modified
481 * as it goes
482 */
483 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
484 struct btrfs_inode_item *src_item;
485 struct btrfs_inode_item *dst_item;
486
487 src_item = (struct btrfs_inode_item *)src_ptr;
488 dst_item = (struct btrfs_inode_item *)dst_ptr;
489
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000490 if (btrfs_inode_generation(eb, src_item) == 0) {
491 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000492 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000493
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000494 /*
495 * For regular files an ino_size == 0 is used only when
496 * logging that an inode exists, as part of a directory
497 * fsync, and the inode wasn't fsynced before. In this
498 * case don't set the size of the inode in the fs/subvol
499 * tree, otherwise we would be throwing valid data away.
500 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000501 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000502 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
503 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000504 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000505
506 btrfs_init_map_token(&token);
507 btrfs_set_token_inode_size(dst_eb, dst_item,
508 ino_size, &token);
509 }
Chris Masone02119d2008-09-05 16:13:11 -0400510 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000511 }
Chris Masone02119d2008-09-05 16:13:11 -0400512
513 if (overwrite_root &&
514 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
515 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
516 save_old_i_size = 1;
517 saved_i_size = btrfs_inode_size(path->nodes[0],
518 dst_item);
519 }
520 }
521
522 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
523 src_ptr, item_size);
524
525 if (save_old_i_size) {
526 struct btrfs_inode_item *dst_item;
527 dst_item = (struct btrfs_inode_item *)dst_ptr;
528 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
529 }
530
531 /* make sure the generation is filled in */
532 if (key->type == BTRFS_INODE_ITEM_KEY) {
533 struct btrfs_inode_item *dst_item;
534 dst_item = (struct btrfs_inode_item *)dst_ptr;
535 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
536 btrfs_set_inode_generation(path->nodes[0], dst_item,
537 trans->transid);
538 }
539 }
540no_copy:
541 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200542 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400543 return 0;
544}
545
546/*
547 * simple helper to read an inode off the disk from a given root
548 * This can only be called for subvolume roots and not for the log
549 */
550static noinline struct inode *read_one_inode(struct btrfs_root *root,
551 u64 objectid)
552{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400553 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400554 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400555
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400556 key.objectid = objectid;
557 key.type = BTRFS_INODE_ITEM_KEY;
558 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000559 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400560 if (IS_ERR(inode)) {
561 inode = NULL;
562 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400563 iput(inode);
564 inode = NULL;
565 }
566 return inode;
567}
568
569/* replays a single extent in 'eb' at 'slot' with 'key' into the
570 * subvolume 'root'. path is released on entry and should be released
571 * on exit.
572 *
573 * extents in the log tree have not been allocated out of the extent
574 * tree yet. So, this completes the allocation, taking a reference
575 * as required if the extent already exists or creating a new extent
576 * if it isn't in the extent allocation tree yet.
577 *
578 * The extent is inserted into the file, dropping any existing extents
579 * from the file that overlap the new one.
580 */
581static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
582 struct btrfs_root *root,
583 struct btrfs_path *path,
584 struct extent_buffer *eb, int slot,
585 struct btrfs_key *key)
586{
587 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400588 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400589 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000590 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400591 struct btrfs_file_extent_item *item;
592 struct inode *inode = NULL;
593 unsigned long size;
594 int ret = 0;
595
596 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
597 found_type = btrfs_file_extent_type(eb, item);
598
Yan Zhengd899e052008-10-30 14:25:28 -0400599 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000600 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
601 nbytes = btrfs_file_extent_num_bytes(eb, item);
602 extent_end = start + nbytes;
603
604 /*
605 * We don't add to the inodes nbytes if we are prealloc or a
606 * hole.
607 */
608 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
609 nbytes = 0;
610 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800611 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000612 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000613 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400614 } else {
615 ret = 0;
616 goto out;
617 }
618
619 inode = read_one_inode(root, key->objectid);
620 if (!inode) {
621 ret = -EIO;
622 goto out;
623 }
624
625 /*
626 * first check to see if we already have this extent in the
627 * file. This must be done before the btrfs_drop_extents run
628 * so we don't try to drop this extent.
629 */
Li Zefan33345d012011-04-20 10:31:50 +0800630 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400631 start, 0);
632
Yan Zhengd899e052008-10-30 14:25:28 -0400633 if (ret == 0 &&
634 (found_type == BTRFS_FILE_EXTENT_REG ||
635 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400636 struct btrfs_file_extent_item cmp1;
637 struct btrfs_file_extent_item cmp2;
638 struct btrfs_file_extent_item *existing;
639 struct extent_buffer *leaf;
640
641 leaf = path->nodes[0];
642 existing = btrfs_item_ptr(leaf, path->slots[0],
643 struct btrfs_file_extent_item);
644
645 read_extent_buffer(eb, &cmp1, (unsigned long)item,
646 sizeof(cmp1));
647 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
648 sizeof(cmp2));
649
650 /*
651 * we already have a pointer to this exact extent,
652 * we don't have to do anything
653 */
654 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200655 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400656 goto out;
657 }
658 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200659 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400660
661 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400662 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400663 if (ret)
664 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400665
Yan Zheng07d400a2009-01-06 11:42:00 -0500666 if (found_type == BTRFS_FILE_EXTENT_REG ||
667 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400668 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500669 unsigned long dest_offset;
670 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400671
Yan Zheng07d400a2009-01-06 11:42:00 -0500672 ret = btrfs_insert_empty_item(trans, root, path, key,
673 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400674 if (ret)
675 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500676 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
677 path->slots[0]);
678 copy_extent_buffer(path->nodes[0], eb, dest_offset,
679 (unsigned long)item, sizeof(*item));
680
681 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
682 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
683 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400684 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500685
Qu Wenruodf2c95f2016-08-15 10:36:52 +0800686 /*
687 * Manually record dirty extent, as here we did a shallow
688 * file extent item copy and skip normal backref update,
689 * but modifying extent tree all by ourselves.
690 * So need to manually record dirty extent for qgroup,
691 * as the owner of the file extent changed from log tree
692 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
693 */
694 ret = btrfs_qgroup_insert_dirty_extent(trans, root->fs_info,
695 btrfs_file_extent_disk_bytenr(eb, item),
696 btrfs_file_extent_disk_num_bytes(eb, item),
697 GFP_NOFS);
698 if (ret < 0)
699 goto out;
700
Yan Zheng07d400a2009-01-06 11:42:00 -0500701 if (ins.objectid > 0) {
702 u64 csum_start;
703 u64 csum_end;
704 LIST_HEAD(ordered_sums);
705 /*
706 * is this extent already allocated in the extent
707 * allocation tree? If so, just add a reference
708 */
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000709 ret = btrfs_lookup_data_extent(root, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500710 ins.offset);
711 if (ret == 0) {
712 ret = btrfs_inc_extent_ref(trans, root,
713 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400714 0, root->root_key.objectid,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100715 key->objectid, offset);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400716 if (ret)
717 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500718 } else {
719 /*
720 * insert the extent pointer in the extent
721 * allocation tree
722 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400723 ret = btrfs_alloc_logged_file_extent(trans,
724 root, root->root_key.objectid,
725 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400726 if (ret)
727 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500728 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200729 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500730
731 if (btrfs_file_extent_compression(eb, item)) {
732 csum_start = ins.objectid;
733 csum_end = csum_start + ins.offset;
734 } else {
735 csum_start = ins.objectid +
736 btrfs_file_extent_offset(eb, item);
737 csum_end = csum_start +
738 btrfs_file_extent_num_bytes(eb, item);
739 }
740
741 ret = btrfs_lookup_csums_range(root->log_root,
742 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100743 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400744 if (ret)
745 goto out;
Filipe Mananab84b8392015-08-19 11:09:40 +0100746 /*
747 * Now delete all existing cums in the csum root that
748 * cover our range. We do this because we can have an
749 * extent that is completely referenced by one file
750 * extent item and partially referenced by another
751 * file extent item (like after using the clone or
752 * extent_same ioctls). In this case if we end up doing
753 * the replay of the one that partially references the
754 * extent first, and we do not do the csum deletion
755 * below, we can get 2 csum items in the csum tree that
756 * overlap each other. For example, imagine our log has
757 * the two following file extent items:
758 *
759 * key (257 EXTENT_DATA 409600)
760 * extent data disk byte 12845056 nr 102400
761 * extent data offset 20480 nr 20480 ram 102400
762 *
763 * key (257 EXTENT_DATA 819200)
764 * extent data disk byte 12845056 nr 102400
765 * extent data offset 0 nr 102400 ram 102400
766 *
767 * Where the second one fully references the 100K extent
768 * that starts at disk byte 12845056, and the log tree
769 * has a single csum item that covers the entire range
770 * of the extent:
771 *
772 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
773 *
774 * After the first file extent item is replayed, the
775 * csum tree gets the following csum item:
776 *
777 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
778 *
779 * Which covers the 20K sub-range starting at offset 20K
780 * of our extent. Now when we replay the second file
781 * extent item, if we do not delete existing csum items
782 * that cover any of its blocks, we end up getting two
783 * csum items in our csum tree that overlap each other:
784 *
785 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
786 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
787 *
788 * Which is a problem, because after this anyone trying
789 * to lookup up for the checksum of any block of our
790 * extent starting at an offset of 40K or higher, will
791 * end up looking at the second csum item only, which
792 * does not contain the checksum for any block starting
793 * at offset 40K or higher of our extent.
794 */
Yan Zheng07d400a2009-01-06 11:42:00 -0500795 while (!list_empty(&ordered_sums)) {
796 struct btrfs_ordered_sum *sums;
797 sums = list_entry(ordered_sums.next,
798 struct btrfs_ordered_sum,
799 list);
Josef Bacik36508602013-04-25 16:23:32 -0400800 if (!ret)
Filipe Mananab84b8392015-08-19 11:09:40 +0100801 ret = btrfs_del_csums(trans,
802 root->fs_info->csum_root,
803 sums->bytenr,
804 sums->len);
805 if (!ret)
Josef Bacik36508602013-04-25 16:23:32 -0400806 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500807 root->fs_info->csum_root,
808 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500809 list_del(&sums->list);
810 kfree(sums);
811 }
Josef Bacik36508602013-04-25 16:23:32 -0400812 if (ret)
813 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500814 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200815 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500816 }
817 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
818 /* inline extents are easy, we just overwrite them */
819 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400820 if (ret)
821 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500822 }
823
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000824 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600825 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400826out:
827 if (inode)
828 iput(inode);
829 return ret;
830}
831
832/*
833 * when cleaning up conflicts between the directory names in the
834 * subvolume, directory names in the log and directory names in the
835 * inode back references, we may have to unlink inodes from directories.
836 *
837 * This is a helper function to do the unlink of a specific directory
838 * item
839 */
840static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
841 struct btrfs_root *root,
842 struct btrfs_path *path,
843 struct inode *dir,
844 struct btrfs_dir_item *di)
845{
846 struct inode *inode;
847 char *name;
848 int name_len;
849 struct extent_buffer *leaf;
850 struct btrfs_key location;
851 int ret;
852
853 leaf = path->nodes[0];
854
855 btrfs_dir_item_key_to_cpu(leaf, di, &location);
856 name_len = btrfs_dir_name_len(leaf, di);
857 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000858 if (!name)
859 return -ENOMEM;
860
Chris Masone02119d2008-09-05 16:13:11 -0400861 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200862 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400863
864 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000865 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400866 ret = -EIO;
867 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000868 }
Chris Masone02119d2008-09-05 16:13:11 -0400869
Yan Zhengec051c02009-01-05 15:43:42 -0500870 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400871 if (ret)
872 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400873
Chris Masone02119d2008-09-05 16:13:11 -0400874 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400875 if (ret)
876 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100877 else
878 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400879out:
880 kfree(name);
881 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400882 return ret;
883}
884
885/*
886 * helper function to see if a given name and sequence number found
887 * in an inode back reference are already in a directory and correctly
888 * point to this inode
889 */
890static noinline int inode_in_dir(struct btrfs_root *root,
891 struct btrfs_path *path,
892 u64 dirid, u64 objectid, u64 index,
893 const char *name, int name_len)
894{
895 struct btrfs_dir_item *di;
896 struct btrfs_key location;
897 int match = 0;
898
899 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
900 index, name, name_len, 0);
901 if (di && !IS_ERR(di)) {
902 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
903 if (location.objectid != objectid)
904 goto out;
905 } else
906 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200907 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400908
909 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
910 if (di && !IS_ERR(di)) {
911 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
912 if (location.objectid != objectid)
913 goto out;
914 } else
915 goto out;
916 match = 1;
917out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200918 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400919 return match;
920}
921
922/*
923 * helper function to check a log tree for a named back reference in
924 * an inode. This is used to decide if a back reference that is
925 * found in the subvolume conflicts with what we find in the log.
926 *
927 * inode backreferences may have multiple refs in a single item,
928 * during replay we process one reference at a time, and we don't
929 * want to delete valid links to a file from the subvolume if that
930 * link is also in the log.
931 */
932static noinline int backref_in_log(struct btrfs_root *log,
933 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700934 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000935 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400936{
937 struct btrfs_path *path;
938 struct btrfs_inode_ref *ref;
939 unsigned long ptr;
940 unsigned long ptr_end;
941 unsigned long name_ptr;
942 int found_name_len;
943 int item_size;
944 int ret;
945 int match = 0;
946
947 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000948 if (!path)
949 return -ENOMEM;
950
Chris Masone02119d2008-09-05 16:13:11 -0400951 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
952 if (ret != 0)
953 goto out;
954
Chris Masone02119d2008-09-05 16:13:11 -0400955 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700956
957 if (key->type == BTRFS_INODE_EXTREF_KEY) {
958 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
959 name, namelen, NULL))
960 match = 1;
961
962 goto out;
963 }
964
965 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400966 ptr_end = ptr + item_size;
967 while (ptr < ptr_end) {
968 ref = (struct btrfs_inode_ref *)ptr;
969 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
970 if (found_name_len == namelen) {
971 name_ptr = (unsigned long)(ref + 1);
972 ret = memcmp_extent_buffer(path->nodes[0], name,
973 name_ptr, namelen);
974 if (ret == 0) {
975 match = 1;
976 goto out;
977 }
978 }
979 ptr = (unsigned long)(ref + 1) + found_name_len;
980 }
981out:
982 btrfs_free_path(path);
983 return match;
984}
985
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700986static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
987 struct btrfs_root *root,
988 struct btrfs_path *path,
989 struct btrfs_root *log_root,
990 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700991 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700992 u64 inode_objectid, u64 parent_objectid,
993 u64 ref_index, char *name, int namelen,
994 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700995{
996 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700997 char *victim_name;
998 int victim_name_len;
999 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001000 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -07001001 struct btrfs_key search_key;
1002 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001003
Mark Fashehf1863732012-08-08 11:32:27 -07001004again:
1005 /* Search old style refs */
1006 search_key.objectid = inode_objectid;
1007 search_key.type = BTRFS_INODE_REF_KEY;
1008 search_key.offset = parent_objectid;
1009 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001010 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001011 struct btrfs_inode_ref *victim_ref;
1012 unsigned long ptr;
1013 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -07001014
1015 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001016
1017 /* are we trying to overwrite a back ref for the root directory
1018 * if so, just jump out, we're done
1019 */
Mark Fashehf1863732012-08-08 11:32:27 -07001020 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001021 return 1;
1022
1023 /* check all the names in this back reference to see
1024 * if they are in the log. if so, we allow them to stay
1025 * otherwise they must be unlinked as a conflict
1026 */
1027 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1028 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1029 while (ptr < ptr_end) {
1030 victim_ref = (struct btrfs_inode_ref *)ptr;
1031 victim_name_len = btrfs_inode_ref_name_len(leaf,
1032 victim_ref);
1033 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001034 if (!victim_name)
1035 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001036
1037 read_extent_buffer(leaf, victim_name,
1038 (unsigned long)(victim_ref + 1),
1039 victim_name_len);
1040
Mark Fashehf1863732012-08-08 11:32:27 -07001041 if (!backref_in_log(log_root, &search_key,
1042 parent_objectid,
1043 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001044 victim_name_len)) {
Zach Brown8b558c52013-10-16 12:10:34 -07001045 inc_nlink(inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001046 btrfs_release_path(path);
1047
1048 ret = btrfs_unlink_inode(trans, root, dir,
1049 inode, victim_name,
1050 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -07001051 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001052 if (ret)
1053 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001054 ret = btrfs_run_delayed_items(trans, root);
1055 if (ret)
1056 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001057 *search_done = 1;
1058 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001059 }
1060 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -07001061
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001062 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1063 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001064
1065 /*
1066 * NOTE: we have searched root tree and checked the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08001067 * corresponding ref, it does not need to check again.
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001068 */
1069 *search_done = 1;
1070 }
1071 btrfs_release_path(path);
1072
Mark Fashehf1863732012-08-08 11:32:27 -07001073 /* Same search but for extended refs */
1074 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1075 inode_objectid, parent_objectid, 0,
1076 0);
1077 if (!IS_ERR_OR_NULL(extref)) {
1078 u32 item_size;
1079 u32 cur_offset = 0;
1080 unsigned long base;
1081 struct inode *victim_parent;
1082
1083 leaf = path->nodes[0];
1084
1085 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1086 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1087
1088 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001089 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001090
1091 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1092
1093 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1094 goto next;
1095
1096 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001097 if (!victim_name)
1098 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001099 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1100 victim_name_len);
1101
1102 search_key.objectid = inode_objectid;
1103 search_key.type = BTRFS_INODE_EXTREF_KEY;
1104 search_key.offset = btrfs_extref_hash(parent_objectid,
1105 victim_name,
1106 victim_name_len);
1107 ret = 0;
1108 if (!backref_in_log(log_root, &search_key,
1109 parent_objectid, victim_name,
1110 victim_name_len)) {
1111 ret = -ENOENT;
1112 victim_parent = read_one_inode(root,
1113 parent_objectid);
1114 if (victim_parent) {
Zach Brown8b558c52013-10-16 12:10:34 -07001115 inc_nlink(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001116 btrfs_release_path(path);
1117
1118 ret = btrfs_unlink_inode(trans, root,
1119 victim_parent,
1120 inode,
1121 victim_name,
1122 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001123 if (!ret)
1124 ret = btrfs_run_delayed_items(
1125 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001126 }
Mark Fashehf1863732012-08-08 11:32:27 -07001127 iput(victim_parent);
1128 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001129 if (ret)
1130 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001131 *search_done = 1;
1132 goto again;
1133 }
1134 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001135 if (ret)
1136 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001137next:
1138 cur_offset += victim_name_len + sizeof(*extref);
1139 }
1140 *search_done = 1;
1141 }
1142 btrfs_release_path(path);
1143
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001144 /* look for a conflicting sequence number */
1145 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001146 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001147 if (di && !IS_ERR(di)) {
1148 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001149 if (ret)
1150 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001151 }
1152 btrfs_release_path(path);
1153
1154 /* look for a conflicing name */
1155 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1156 name, namelen, 0);
1157 if (di && !IS_ERR(di)) {
1158 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001159 if (ret)
1160 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001161 }
1162 btrfs_release_path(path);
1163
1164 return 0;
1165}
Chris Masone02119d2008-09-05 16:13:11 -04001166
Mark Fashehf1863732012-08-08 11:32:27 -07001167static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1168 u32 *namelen, char **name, u64 *index,
1169 u64 *parent_objectid)
1170{
1171 struct btrfs_inode_extref *extref;
1172
1173 extref = (struct btrfs_inode_extref *)ref_ptr;
1174
1175 *namelen = btrfs_inode_extref_name_len(eb, extref);
1176 *name = kmalloc(*namelen, GFP_NOFS);
1177 if (*name == NULL)
1178 return -ENOMEM;
1179
1180 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1181 *namelen);
1182
1183 *index = btrfs_inode_extref_index(eb, extref);
1184 if (parent_objectid)
1185 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1186
1187 return 0;
1188}
1189
1190static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1191 u32 *namelen, char **name, u64 *index)
1192{
1193 struct btrfs_inode_ref *ref;
1194
1195 ref = (struct btrfs_inode_ref *)ref_ptr;
1196
1197 *namelen = btrfs_inode_ref_name_len(eb, ref);
1198 *name = kmalloc(*namelen, GFP_NOFS);
1199 if (*name == NULL)
1200 return -ENOMEM;
1201
1202 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1203
1204 *index = btrfs_inode_ref_index(eb, ref);
1205
1206 return 0;
1207}
1208
Chris Masone02119d2008-09-05 16:13:11 -04001209/*
1210 * replay one inode back reference item found in the log tree.
1211 * eb, slot and key refer to the buffer and key found in the log tree.
1212 * root is the destination we are replaying into, and path is for temp
1213 * use by this function. (it should be released on return).
1214 */
1215static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1216 struct btrfs_root *root,
1217 struct btrfs_root *log,
1218 struct btrfs_path *path,
1219 struct extent_buffer *eb, int slot,
1220 struct btrfs_key *key)
1221{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001222 struct inode *dir = NULL;
1223 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001224 unsigned long ref_ptr;
1225 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001226 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001227 int namelen;
1228 int ret;
liuboc622ae62011-03-26 08:01:12 -04001229 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001230 int log_ref_ver = 0;
1231 u64 parent_objectid;
1232 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001233 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001234 int ref_struct_size;
1235
1236 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1237 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1238
1239 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1240 struct btrfs_inode_extref *r;
1241
1242 ref_struct_size = sizeof(struct btrfs_inode_extref);
1243 log_ref_ver = 1;
1244 r = (struct btrfs_inode_extref *)ref_ptr;
1245 parent_objectid = btrfs_inode_extref_parent(eb, r);
1246 } else {
1247 ref_struct_size = sizeof(struct btrfs_inode_ref);
1248 parent_objectid = key->offset;
1249 }
1250 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001251
Chris Masone02119d2008-09-05 16:13:11 -04001252 /*
1253 * it is possible that we didn't log all the parent directories
1254 * for a given inode. If we don't find the dir, just don't
1255 * copy the back ref in. The link count fixup code will take
1256 * care of the rest
1257 */
Mark Fashehf1863732012-08-08 11:32:27 -07001258 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001259 if (!dir) {
1260 ret = -ENOENT;
1261 goto out;
1262 }
Chris Masone02119d2008-09-05 16:13:11 -04001263
Mark Fashehf1863732012-08-08 11:32:27 -07001264 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001265 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001266 ret = -EIO;
1267 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001268 }
Chris Masone02119d2008-09-05 16:13:11 -04001269
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001270 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001271 if (log_ref_ver) {
1272 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1273 &ref_index, &parent_objectid);
1274 /*
1275 * parent object can change from one array
1276 * item to another.
1277 */
1278 if (!dir)
1279 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001280 if (!dir) {
1281 ret = -ENOENT;
1282 goto out;
1283 }
Mark Fashehf1863732012-08-08 11:32:27 -07001284 } else {
1285 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1286 &ref_index);
1287 }
1288 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001289 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001290
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001291 /* if we already have a perfect match, we're done */
1292 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001293 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001294 /*
1295 * look for a conflicting back reference in the
1296 * metadata. if we find one we have to unlink that name
1297 * of the file before we add our new link. Later on, we
1298 * overwrite any existing back reference, and we don't
1299 * want to create dangling pointers in the directory.
1300 */
Chris Masone02119d2008-09-05 16:13:11 -04001301
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001302 if (!search_done) {
1303 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001304 dir, inode, eb,
1305 inode_objectid,
1306 parent_objectid,
1307 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001308 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001309 if (ret) {
1310 if (ret == 1)
1311 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001312 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001313 }
Chris Masone02119d2008-09-05 16:13:11 -04001314 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001315
1316 /* insert our name */
1317 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001318 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001319 if (ret)
1320 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001321
1322 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001323 }
liuboc622ae62011-03-26 08:01:12 -04001324
Mark Fashehf1863732012-08-08 11:32:27 -07001325 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001326 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001327 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001328 if (log_ref_ver) {
1329 iput(dir);
1330 dir = NULL;
1331 }
Chris Masone02119d2008-09-05 16:13:11 -04001332 }
Chris Masone02119d2008-09-05 16:13:11 -04001333
1334 /* finally write the back reference in the inode */
1335 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001336out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001337 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001338 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001339 iput(dir);
1340 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001341 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001342}
1343
Yan, Zhengc71bf092009-11-12 09:34:40 +00001344static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001345 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001346{
1347 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001348
David Sterba9c4f61f2015-01-02 19:12:57 +01001349 ret = btrfs_insert_orphan_item(trans, root, ino);
1350 if (ret == -EEXIST)
1351 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001352
Yan, Zhengc71bf092009-11-12 09:34:40 +00001353 return ret;
1354}
1355
Mark Fashehf1863732012-08-08 11:32:27 -07001356static int count_inode_extrefs(struct btrfs_root *root,
1357 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001358{
Mark Fashehf1863732012-08-08 11:32:27 -07001359 int ret = 0;
1360 int name_len;
1361 unsigned int nlink = 0;
1362 u32 item_size;
1363 u32 cur_offset = 0;
1364 u64 inode_objectid = btrfs_ino(inode);
1365 u64 offset = 0;
1366 unsigned long ptr;
1367 struct btrfs_inode_extref *extref;
1368 struct extent_buffer *leaf;
1369
1370 while (1) {
1371 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1372 &extref, &offset);
1373 if (ret)
1374 break;
1375
1376 leaf = path->nodes[0];
1377 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1378 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001379 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001380
1381 while (cur_offset < item_size) {
1382 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1383 name_len = btrfs_inode_extref_name_len(leaf, extref);
1384
1385 nlink++;
1386
1387 cur_offset += name_len + sizeof(*extref);
1388 }
1389
1390 offset++;
1391 btrfs_release_path(path);
1392 }
1393 btrfs_release_path(path);
1394
Filipe Manana2c2c4522015-01-13 16:40:04 +00001395 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001396 return ret;
1397 return nlink;
1398}
1399
1400static int count_inode_refs(struct btrfs_root *root,
1401 struct inode *inode, struct btrfs_path *path)
1402{
Chris Masone02119d2008-09-05 16:13:11 -04001403 int ret;
1404 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001405 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001406 unsigned long ptr;
1407 unsigned long ptr_end;
1408 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001409 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001410
Li Zefan33345d012011-04-20 10:31:50 +08001411 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001412 key.type = BTRFS_INODE_REF_KEY;
1413 key.offset = (u64)-1;
1414
Chris Masond3977122009-01-05 21:25:51 -05001415 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001416 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1417 if (ret < 0)
1418 break;
1419 if (ret > 0) {
1420 if (path->slots[0] == 0)
1421 break;
1422 path->slots[0]--;
1423 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001424process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001425 btrfs_item_key_to_cpu(path->nodes[0], &key,
1426 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001427 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001428 key.type != BTRFS_INODE_REF_KEY)
1429 break;
1430 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1431 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1432 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001433 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001434 struct btrfs_inode_ref *ref;
1435
1436 ref = (struct btrfs_inode_ref *)ptr;
1437 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1438 ref);
1439 ptr = (unsigned long)(ref + 1) + name_len;
1440 nlink++;
1441 }
1442
1443 if (key.offset == 0)
1444 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001445 if (path->slots[0] > 0) {
1446 path->slots[0]--;
1447 goto process_slot;
1448 }
Chris Masone02119d2008-09-05 16:13:11 -04001449 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001450 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001451 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001452 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001453
1454 return nlink;
1455}
1456
1457/*
1458 * There are a few corners where the link count of the file can't
1459 * be properly maintained during replay. So, instead of adding
1460 * lots of complexity to the log code, we just scan the backrefs
1461 * for any file that has been through replay.
1462 *
1463 * The scan will update the link count on the inode to reflect the
1464 * number of back refs found. If it goes down to zero, the iput
1465 * will free the inode.
1466 */
1467static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1468 struct btrfs_root *root,
1469 struct inode *inode)
1470{
1471 struct btrfs_path *path;
1472 int ret;
1473 u64 nlink = 0;
1474 u64 ino = btrfs_ino(inode);
1475
1476 path = btrfs_alloc_path();
1477 if (!path)
1478 return -ENOMEM;
1479
1480 ret = count_inode_refs(root, inode, path);
1481 if (ret < 0)
1482 goto out;
1483
1484 nlink = ret;
1485
1486 ret = count_inode_extrefs(root, inode, path);
Mark Fashehf1863732012-08-08 11:32:27 -07001487 if (ret < 0)
1488 goto out;
1489
1490 nlink += ret;
1491
1492 ret = 0;
1493
Chris Masone02119d2008-09-05 16:13:11 -04001494 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001495 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001496 btrfs_update_inode(trans, root, inode);
1497 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001498 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001499
Yan, Zhengc71bf092009-11-12 09:34:40 +00001500 if (inode->i_nlink == 0) {
1501 if (S_ISDIR(inode->i_mode)) {
1502 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001503 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001504 if (ret)
1505 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001506 }
Li Zefan33345d012011-04-20 10:31:50 +08001507 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001508 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001509
Mark Fashehf1863732012-08-08 11:32:27 -07001510out:
1511 btrfs_free_path(path);
1512 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001513}
1514
1515static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1516 struct btrfs_root *root,
1517 struct btrfs_path *path)
1518{
1519 int ret;
1520 struct btrfs_key key;
1521 struct inode *inode;
1522
1523 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1524 key.type = BTRFS_ORPHAN_ITEM_KEY;
1525 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001526 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001527 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1528 if (ret < 0)
1529 break;
1530
1531 if (ret == 1) {
1532 if (path->slots[0] == 0)
1533 break;
1534 path->slots[0]--;
1535 }
1536
1537 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1538 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1539 key.type != BTRFS_ORPHAN_ITEM_KEY)
1540 break;
1541
1542 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001543 if (ret)
1544 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001545
David Sterbab3b4aa72011-04-21 01:20:15 +02001546 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001547 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001548 if (!inode)
1549 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001550
1551 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001552 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001553 if (ret)
1554 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001555
Chris Mason12fcfd22009-03-24 10:24:20 -04001556 /*
1557 * fixup on a directory may create new entries,
1558 * make sure we always look for the highset possible
1559 * offset
1560 */
1561 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001562 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001563 ret = 0;
1564out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001565 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001566 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001567}
1568
1569
1570/*
1571 * record a given inode in the fixup dir so we can check its link
1572 * count when replay is done. The link count is incremented here
1573 * so the inode won't go away until we check it
1574 */
1575static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1576 struct btrfs_root *root,
1577 struct btrfs_path *path,
1578 u64 objectid)
1579{
1580 struct btrfs_key key;
1581 int ret = 0;
1582 struct inode *inode;
1583
1584 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001585 if (!inode)
1586 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001587
1588 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001589 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001590 key.offset = objectid;
1591
1592 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1593
David Sterbab3b4aa72011-04-21 01:20:15 +02001594 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001595 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001596 if (!inode->i_nlink)
1597 set_nlink(inode, 1);
1598 else
Zach Brown8b558c52013-10-16 12:10:34 -07001599 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001600 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001601 } else if (ret == -EEXIST) {
1602 ret = 0;
1603 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001604 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001605 }
1606 iput(inode);
1607
1608 return ret;
1609}
1610
1611/*
1612 * when replaying the log for a directory, we only insert names
1613 * for inodes that actually exist. This means an fsync on a directory
1614 * does not implicitly fsync all the new files in it
1615 */
1616static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1617 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001618 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001619 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001620 struct btrfs_key *location)
1621{
1622 struct inode *inode;
1623 struct inode *dir;
1624 int ret;
1625
1626 inode = read_one_inode(root, location->objectid);
1627 if (!inode)
1628 return -ENOENT;
1629
1630 dir = read_one_inode(root, dirid);
1631 if (!dir) {
1632 iput(inode);
1633 return -EIO;
1634 }
Josef Bacikd5554382013-09-11 14:17:00 -04001635
Chris Masone02119d2008-09-05 16:13:11 -04001636 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1637
1638 /* FIXME, put inode into FIXUP list */
1639
1640 iput(inode);
1641 iput(dir);
1642 return ret;
1643}
1644
1645/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001646 * Return true if an inode reference exists in the log for the given name,
1647 * inode and parent inode.
1648 */
1649static bool name_in_log_ref(struct btrfs_root *log_root,
1650 const char *name, const int name_len,
1651 const u64 dirid, const u64 ino)
1652{
1653 struct btrfs_key search_key;
1654
1655 search_key.objectid = ino;
1656 search_key.type = BTRFS_INODE_REF_KEY;
1657 search_key.offset = dirid;
1658 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1659 return true;
1660
1661 search_key.type = BTRFS_INODE_EXTREF_KEY;
1662 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1663 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1664 return true;
1665
1666 return false;
1667}
1668
1669/*
Chris Masone02119d2008-09-05 16:13:11 -04001670 * take a single entry in a log directory item and replay it into
1671 * the subvolume.
1672 *
1673 * if a conflicting item exists in the subdirectory already,
1674 * the inode it points to is unlinked and put into the link count
1675 * fix up tree.
1676 *
1677 * If a name from the log points to a file or directory that does
1678 * not exist in the FS, it is skipped. fsyncs on directories
1679 * do not force down inodes inside that directory, just changes to the
1680 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001681 *
1682 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1683 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001684 */
1685static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1686 struct btrfs_root *root,
1687 struct btrfs_path *path,
1688 struct extent_buffer *eb,
1689 struct btrfs_dir_item *di,
1690 struct btrfs_key *key)
1691{
1692 char *name;
1693 int name_len;
1694 struct btrfs_dir_item *dst_di;
1695 struct btrfs_key found_key;
1696 struct btrfs_key log_key;
1697 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001698 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001699 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001700 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001701 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001702 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001703
1704 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001705 if (!dir)
1706 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001707
1708 name_len = btrfs_dir_name_len(eb, di);
1709 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001710 if (!name) {
1711 ret = -ENOMEM;
1712 goto out;
1713 }
liubo2a29edc2011-01-26 06:22:08 +00001714
Chris Masone02119d2008-09-05 16:13:11 -04001715 log_type = btrfs_dir_type(eb, di);
1716 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1717 name_len);
1718
1719 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001720 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1721 if (exists == 0)
1722 exists = 1;
1723 else
1724 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001725 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001726
Chris Masone02119d2008-09-05 16:13:11 -04001727 if (key->type == BTRFS_DIR_ITEM_KEY) {
1728 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1729 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001730 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001731 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1732 key->objectid,
1733 key->offset, name,
1734 name_len, 1);
1735 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001736 /* Corruption */
1737 ret = -EINVAL;
1738 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001739 }
David Sterbac7040052011-04-19 18:00:01 +02001740 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001741 /* we need a sequence number to insert, so we only
1742 * do inserts for the BTRFS_DIR_INDEX_KEY types
1743 */
1744 if (key->type != BTRFS_DIR_INDEX_KEY)
1745 goto out;
1746 goto insert;
1747 }
1748
1749 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1750 /* the existing item matches the logged item */
1751 if (found_key.objectid == log_key.objectid &&
1752 found_key.type == log_key.type &&
1753 found_key.offset == log_key.offset &&
1754 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001755 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001756 goto out;
1757 }
1758
1759 /*
1760 * don't drop the conflicting directory entry if the inode
1761 * for the new entry doesn't exist
1762 */
Chris Mason4bef0842008-09-08 11:18:08 -04001763 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001764 goto out;
1765
Chris Masone02119d2008-09-05 16:13:11 -04001766 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001767 if (ret)
1768 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001769
1770 if (key->type == BTRFS_DIR_INDEX_KEY)
1771 goto insert;
1772out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001773 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001774 if (!ret && update_size) {
1775 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1776 ret = btrfs_update_inode(trans, root, dir);
1777 }
Chris Masone02119d2008-09-05 16:13:11 -04001778 kfree(name);
1779 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001780 if (!ret && name_added)
1781 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001782 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001783
1784insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001785 if (name_in_log_ref(root->log_root, name, name_len,
1786 key->objectid, log_key.objectid)) {
1787 /* The dentry will be added later. */
1788 ret = 0;
1789 update_size = false;
1790 goto out;
1791 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001792 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08001793 ret = insert_one_name(trans, root, key->objectid, key->offset,
1794 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001795 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001796 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001797 if (!ret)
1798 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001799 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001800 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001801 goto out;
1802}
1803
1804/*
1805 * find all the names in a directory item and reconcile them into
1806 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1807 * one name in a directory item, but the same code gets used for
1808 * both directory index types
1809 */
1810static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1811 struct btrfs_root *root,
1812 struct btrfs_path *path,
1813 struct extent_buffer *eb, int slot,
1814 struct btrfs_key *key)
1815{
Filipe Mananabb53eda2015-07-15 23:26:43 +01001816 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001817 u32 item_size = btrfs_item_size_nr(eb, slot);
1818 struct btrfs_dir_item *di;
1819 int name_len;
1820 unsigned long ptr;
1821 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001822 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001823
1824 ptr = btrfs_item_ptr_offset(eb, slot);
1825 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001826 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001827 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001828 if (verify_dir_item(root, eb, di))
1829 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001830 name_len = btrfs_dir_name_len(eb, di);
1831 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001832 if (ret < 0)
1833 break;
Chris Masone02119d2008-09-05 16:13:11 -04001834 ptr = (unsigned long)(di + 1);
1835 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001836
1837 /*
1838 * If this entry refers to a non-directory (directories can not
1839 * have a link count > 1) and it was added in the transaction
1840 * that was not committed, make sure we fixup the link count of
1841 * the inode it the entry points to. Otherwise something like
1842 * the following would result in a directory pointing to an
1843 * inode with a wrong link that does not account for this dir
1844 * entry:
1845 *
1846 * mkdir testdir
1847 * touch testdir/foo
1848 * touch testdir/bar
1849 * sync
1850 *
1851 * ln testdir/bar testdir/bar_link
1852 * ln testdir/foo testdir/foo_link
1853 * xfs_io -c "fsync" testdir/bar
1854 *
1855 * <power failure>
1856 *
1857 * mount fs, log replay happens
1858 *
1859 * File foo would remain with a link count of 1 when it has two
1860 * entries pointing to it in the directory testdir. This would
1861 * make it impossible to ever delete the parent directory has
1862 * it would result in stale dentries that can never be deleted.
1863 */
1864 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1865 struct btrfs_key di_key;
1866
1867 if (!fixup_path) {
1868 fixup_path = btrfs_alloc_path();
1869 if (!fixup_path) {
1870 ret = -ENOMEM;
1871 break;
1872 }
1873 }
1874
1875 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1876 ret = link_to_fixup_dir(trans, root, fixup_path,
1877 di_key.objectid);
1878 if (ret)
1879 break;
1880 }
1881 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001882 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01001883 btrfs_free_path(fixup_path);
1884 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001885}
1886
1887/*
1888 * directory replay has two parts. There are the standard directory
1889 * items in the log copied from the subvolume, and range items
1890 * created in the log while the subvolume was logged.
1891 *
1892 * The range items tell us which parts of the key space the log
1893 * is authoritative for. During replay, if a key in the subvolume
1894 * directory is in a logged range item, but not actually in the log
1895 * that means it was deleted from the directory before the fsync
1896 * and should be removed.
1897 */
1898static noinline int find_dir_range(struct btrfs_root *root,
1899 struct btrfs_path *path,
1900 u64 dirid, int key_type,
1901 u64 *start_ret, u64 *end_ret)
1902{
1903 struct btrfs_key key;
1904 u64 found_end;
1905 struct btrfs_dir_log_item *item;
1906 int ret;
1907 int nritems;
1908
1909 if (*start_ret == (u64)-1)
1910 return 1;
1911
1912 key.objectid = dirid;
1913 key.type = key_type;
1914 key.offset = *start_ret;
1915
1916 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1917 if (ret < 0)
1918 goto out;
1919 if (ret > 0) {
1920 if (path->slots[0] == 0)
1921 goto out;
1922 path->slots[0]--;
1923 }
1924 if (ret != 0)
1925 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1926
1927 if (key.type != key_type || key.objectid != dirid) {
1928 ret = 1;
1929 goto next;
1930 }
1931 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1932 struct btrfs_dir_log_item);
1933 found_end = btrfs_dir_log_end(path->nodes[0], item);
1934
1935 if (*start_ret >= key.offset && *start_ret <= found_end) {
1936 ret = 0;
1937 *start_ret = key.offset;
1938 *end_ret = found_end;
1939 goto out;
1940 }
1941 ret = 1;
1942next:
1943 /* check the next slot in the tree to see if it is a valid item */
1944 nritems = btrfs_header_nritems(path->nodes[0]);
Robbie Ko26dc5242016-10-07 17:30:47 +08001945 path->slots[0]++;
Chris Masone02119d2008-09-05 16:13:11 -04001946 if (path->slots[0] >= nritems) {
1947 ret = btrfs_next_leaf(root, path);
1948 if (ret)
1949 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001950 }
1951
1952 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1953
1954 if (key.type != key_type || key.objectid != dirid) {
1955 ret = 1;
1956 goto out;
1957 }
1958 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1959 struct btrfs_dir_log_item);
1960 found_end = btrfs_dir_log_end(path->nodes[0], item);
1961 *start_ret = key.offset;
1962 *end_ret = found_end;
1963 ret = 0;
1964out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001965 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001966 return ret;
1967}
1968
1969/*
1970 * this looks for a given directory item in the log. If the directory
1971 * item is not in the log, the item is removed and the inode it points
1972 * to is unlinked
1973 */
1974static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1975 struct btrfs_root *root,
1976 struct btrfs_root *log,
1977 struct btrfs_path *path,
1978 struct btrfs_path *log_path,
1979 struct inode *dir,
1980 struct btrfs_key *dir_key)
1981{
1982 int ret;
1983 struct extent_buffer *eb;
1984 int slot;
1985 u32 item_size;
1986 struct btrfs_dir_item *di;
1987 struct btrfs_dir_item *log_di;
1988 int name_len;
1989 unsigned long ptr;
1990 unsigned long ptr_end;
1991 char *name;
1992 struct inode *inode;
1993 struct btrfs_key location;
1994
1995again:
1996 eb = path->nodes[0];
1997 slot = path->slots[0];
1998 item_size = btrfs_item_size_nr(eb, slot);
1999 ptr = btrfs_item_ptr_offset(eb, slot);
2000 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05002001 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04002002 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04002003 if (verify_dir_item(root, eb, di)) {
2004 ret = -EIO;
2005 goto out;
2006 }
2007
Chris Masone02119d2008-09-05 16:13:11 -04002008 name_len = btrfs_dir_name_len(eb, di);
2009 name = kmalloc(name_len, GFP_NOFS);
2010 if (!name) {
2011 ret = -ENOMEM;
2012 goto out;
2013 }
2014 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2015 name_len);
2016 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04002017 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002018 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2019 dir_key->objectid,
2020 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04002021 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002022 log_di = btrfs_lookup_dir_index_item(trans, log,
2023 log_path,
2024 dir_key->objectid,
2025 dir_key->offset,
2026 name, name_len, 0);
2027 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002028 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04002029 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02002030 btrfs_release_path(path);
2031 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002032 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00002033 if (!inode) {
2034 kfree(name);
2035 return -EIO;
2036 }
Chris Masone02119d2008-09-05 16:13:11 -04002037
2038 ret = link_to_fixup_dir(trans, root,
2039 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04002040 if (ret) {
2041 kfree(name);
2042 iput(inode);
2043 goto out;
2044 }
2045
Zach Brown8b558c52013-10-16 12:10:34 -07002046 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002047 ret = btrfs_unlink_inode(trans, root, dir, inode,
2048 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04002049 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01002050 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04002051 kfree(name);
2052 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04002053 if (ret)
2054 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002055
2056 /* there might still be more names under this key
2057 * check and repeat if required
2058 */
2059 ret = btrfs_search_slot(NULL, root, dir_key, path,
2060 0, 0);
2061 if (ret == 0)
2062 goto again;
2063 ret = 0;
2064 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00002065 } else if (IS_ERR(log_di)) {
2066 kfree(name);
2067 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04002068 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002069 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002070 kfree(name);
2071
2072 ptr = (unsigned long)(di + 1);
2073 ptr += name_len;
2074 }
2075 ret = 0;
2076out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002077 btrfs_release_path(path);
2078 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002079 return ret;
2080}
2081
Filipe Manana4f764e52015-02-23 19:53:35 +00002082static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2083 struct btrfs_root *root,
2084 struct btrfs_root *log,
2085 struct btrfs_path *path,
2086 const u64 ino)
2087{
2088 struct btrfs_key search_key;
2089 struct btrfs_path *log_path;
2090 int i;
2091 int nritems;
2092 int ret;
2093
2094 log_path = btrfs_alloc_path();
2095 if (!log_path)
2096 return -ENOMEM;
2097
2098 search_key.objectid = ino;
2099 search_key.type = BTRFS_XATTR_ITEM_KEY;
2100 search_key.offset = 0;
2101again:
2102 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2103 if (ret < 0)
2104 goto out;
2105process_leaf:
2106 nritems = btrfs_header_nritems(path->nodes[0]);
2107 for (i = path->slots[0]; i < nritems; i++) {
2108 struct btrfs_key key;
2109 struct btrfs_dir_item *di;
2110 struct btrfs_dir_item *log_di;
2111 u32 total_size;
2112 u32 cur;
2113
2114 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2115 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2116 ret = 0;
2117 goto out;
2118 }
2119
2120 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2121 total_size = btrfs_item_size_nr(path->nodes[0], i);
2122 cur = 0;
2123 while (cur < total_size) {
2124 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2125 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2126 u32 this_len = sizeof(*di) + name_len + data_len;
2127 char *name;
2128
2129 name = kmalloc(name_len, GFP_NOFS);
2130 if (!name) {
2131 ret = -ENOMEM;
2132 goto out;
2133 }
2134 read_extent_buffer(path->nodes[0], name,
2135 (unsigned long)(di + 1), name_len);
2136
2137 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2138 name, name_len, 0);
2139 btrfs_release_path(log_path);
2140 if (!log_di) {
2141 /* Doesn't exist in log tree, so delete it. */
2142 btrfs_release_path(path);
2143 di = btrfs_lookup_xattr(trans, root, path, ino,
2144 name, name_len, -1);
2145 kfree(name);
2146 if (IS_ERR(di)) {
2147 ret = PTR_ERR(di);
2148 goto out;
2149 }
2150 ASSERT(di);
2151 ret = btrfs_delete_one_dir_name(trans, root,
2152 path, di);
2153 if (ret)
2154 goto out;
2155 btrfs_release_path(path);
2156 search_key = key;
2157 goto again;
2158 }
2159 kfree(name);
2160 if (IS_ERR(log_di)) {
2161 ret = PTR_ERR(log_di);
2162 goto out;
2163 }
2164 cur += this_len;
2165 di = (struct btrfs_dir_item *)((char *)di + this_len);
2166 }
2167 }
2168 ret = btrfs_next_leaf(root, path);
2169 if (ret > 0)
2170 ret = 0;
2171 else if (ret == 0)
2172 goto process_leaf;
2173out:
2174 btrfs_free_path(log_path);
2175 btrfs_release_path(path);
2176 return ret;
2177}
2178
2179
Chris Masone02119d2008-09-05 16:13:11 -04002180/*
2181 * deletion replay happens before we copy any new directory items
2182 * out of the log or out of backreferences from inodes. It
2183 * scans the log to find ranges of keys that log is authoritative for,
2184 * and then scans the directory to find items in those ranges that are
2185 * not present in the log.
2186 *
2187 * Anything we don't find in the log is unlinked and removed from the
2188 * directory.
2189 */
2190static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2191 struct btrfs_root *root,
2192 struct btrfs_root *log,
2193 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002194 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002195{
2196 u64 range_start;
2197 u64 range_end;
2198 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2199 int ret = 0;
2200 struct btrfs_key dir_key;
2201 struct btrfs_key found_key;
2202 struct btrfs_path *log_path;
2203 struct inode *dir;
2204
2205 dir_key.objectid = dirid;
2206 dir_key.type = BTRFS_DIR_ITEM_KEY;
2207 log_path = btrfs_alloc_path();
2208 if (!log_path)
2209 return -ENOMEM;
2210
2211 dir = read_one_inode(root, dirid);
2212 /* it isn't an error if the inode isn't there, that can happen
2213 * because we replay the deletes before we copy in the inode item
2214 * from the log
2215 */
2216 if (!dir) {
2217 btrfs_free_path(log_path);
2218 return 0;
2219 }
2220again:
2221 range_start = 0;
2222 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002223 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002224 if (del_all)
2225 range_end = (u64)-1;
2226 else {
2227 ret = find_dir_range(log, path, dirid, key_type,
2228 &range_start, &range_end);
2229 if (ret != 0)
2230 break;
2231 }
Chris Masone02119d2008-09-05 16:13:11 -04002232
2233 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002234 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002235 int nritems;
2236 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2237 0, 0);
2238 if (ret < 0)
2239 goto out;
2240
2241 nritems = btrfs_header_nritems(path->nodes[0]);
2242 if (path->slots[0] >= nritems) {
2243 ret = btrfs_next_leaf(root, path);
Liu Bob3835752018-04-03 01:59:48 +08002244 if (ret == 1)
Chris Masone02119d2008-09-05 16:13:11 -04002245 break;
Liu Bob3835752018-04-03 01:59:48 +08002246 else if (ret < 0)
2247 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002248 }
2249 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2250 path->slots[0]);
2251 if (found_key.objectid != dirid ||
2252 found_key.type != dir_key.type)
2253 goto next_type;
2254
2255 if (found_key.offset > range_end)
2256 break;
2257
2258 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002259 log_path, dir,
2260 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002261 if (ret)
2262 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002263 if (found_key.offset == (u64)-1)
2264 break;
2265 dir_key.offset = found_key.offset + 1;
2266 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002267 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002268 if (range_end == (u64)-1)
2269 break;
2270 range_start = range_end + 1;
2271 }
2272
2273next_type:
2274 ret = 0;
2275 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2276 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2277 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002278 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002279 goto again;
2280 }
2281out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002282 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002283 btrfs_free_path(log_path);
2284 iput(dir);
2285 return ret;
2286}
2287
2288/*
2289 * the process_func used to replay items from the log tree. This
2290 * gets called in two different stages. The first stage just looks
2291 * for inodes and makes sure they are all copied into the subvolume.
2292 *
2293 * The second stage copies all the other item types from the log into
2294 * the subvolume. The two stage approach is slower, but gets rid of
2295 * lots of complexity around inodes referencing other inodes that exist
2296 * only in the log (references come from either directory items or inode
2297 * back refs).
2298 */
2299static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2300 struct walk_control *wc, u64 gen)
2301{
2302 int nritems;
2303 struct btrfs_path *path;
2304 struct btrfs_root *root = wc->replay_dest;
2305 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002306 int level;
2307 int i;
2308 int ret;
2309
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002310 ret = btrfs_read_buffer(eb, gen);
2311 if (ret)
2312 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002313
2314 level = btrfs_header_level(eb);
2315
2316 if (level != 0)
2317 return 0;
2318
2319 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002320 if (!path)
2321 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002322
2323 nritems = btrfs_header_nritems(eb);
2324 for (i = 0; i < nritems; i++) {
2325 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002326
2327 /* inode keys are done during the first stage */
2328 if (key.type == BTRFS_INODE_ITEM_KEY &&
2329 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002330 struct btrfs_inode_item *inode_item;
2331 u32 mode;
2332
2333 inode_item = btrfs_item_ptr(eb, i,
2334 struct btrfs_inode_item);
Filipe Manana4f764e52015-02-23 19:53:35 +00002335 ret = replay_xattr_deletes(wc->trans, root, log,
2336 path, key.objectid);
2337 if (ret)
2338 break;
Chris Masone02119d2008-09-05 16:13:11 -04002339 mode = btrfs_inode_mode(eb, inode_item);
2340 if (S_ISDIR(mode)) {
2341 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002342 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002343 if (ret)
2344 break;
Chris Masone02119d2008-09-05 16:13:11 -04002345 }
2346 ret = overwrite_item(wc->trans, root, path,
2347 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002348 if (ret)
2349 break;
Chris Masone02119d2008-09-05 16:13:11 -04002350
Yan, Zhengc71bf092009-11-12 09:34:40 +00002351 /* for regular files, make sure corresponding
Nicholas D Steeves01327612016-05-19 21:18:45 -04002352 * orphan item exist. extents past the new EOF
Yan, Zhengc71bf092009-11-12 09:34:40 +00002353 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002354 */
2355 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002356 ret = insert_orphan_item(wc->trans, root,
2357 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002358 if (ret)
2359 break;
Chris Masone02119d2008-09-05 16:13:11 -04002360 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002361
Chris Masone02119d2008-09-05 16:13:11 -04002362 ret = link_to_fixup_dir(wc->trans, root,
2363 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002364 if (ret)
2365 break;
Chris Masone02119d2008-09-05 16:13:11 -04002366 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002367
2368 if (key.type == BTRFS_DIR_INDEX_KEY &&
2369 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2370 ret = replay_one_dir_item(wc->trans, root, path,
2371 eb, i, &key);
2372 if (ret)
2373 break;
2374 }
2375
Chris Masone02119d2008-09-05 16:13:11 -04002376 if (wc->stage < LOG_WALK_REPLAY_ALL)
2377 continue;
2378
2379 /* these keys are simply copied */
2380 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2381 ret = overwrite_item(wc->trans, root, path,
2382 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002383 if (ret)
2384 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002385 } else if (key.type == BTRFS_INODE_REF_KEY ||
2386 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002387 ret = add_inode_ref(wc->trans, root, log, path,
2388 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002389 if (ret && ret != -ENOENT)
2390 break;
2391 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002392 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2393 ret = replay_one_extent(wc->trans, root, path,
2394 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002395 if (ret)
2396 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002397 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002398 ret = replay_one_dir_item(wc->trans, root, path,
2399 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002400 if (ret)
2401 break;
Chris Masone02119d2008-09-05 16:13:11 -04002402 }
2403 }
2404 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002405 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002406}
2407
Chris Masond3977122009-01-05 21:25:51 -05002408static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002409 struct btrfs_root *root,
2410 struct btrfs_path *path, int *level,
2411 struct walk_control *wc)
2412{
2413 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002414 u64 bytenr;
2415 u64 ptr_gen;
2416 struct extent_buffer *next;
2417 struct extent_buffer *cur;
2418 struct extent_buffer *parent;
2419 u32 blocksize;
2420 int ret = 0;
2421
2422 WARN_ON(*level < 0);
2423 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2424
Chris Masond3977122009-01-05 21:25:51 -05002425 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002426 WARN_ON(*level < 0);
2427 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2428 cur = path->nodes[*level];
2429
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302430 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002431
2432 if (path->slots[*level] >=
2433 btrfs_header_nritems(cur))
2434 break;
2435
2436 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2437 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
David Sterba707e8a02014-06-04 19:22:26 +02002438 blocksize = root->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002439
2440 parent = path->nodes[*level];
2441 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002442
David Sterbaa83fffb2014-06-15 02:39:54 +02002443 next = btrfs_find_create_tree_block(root, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002444 if (IS_ERR(next))
2445 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002446
Chris Masone02119d2008-09-05 16:13:11 -04002447 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002448 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002449 if (ret) {
2450 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002451 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002452 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002453
Chris Masone02119d2008-09-05 16:13:11 -04002454 path->slots[*level]++;
2455 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002456 ret = btrfs_read_buffer(next, ptr_gen);
2457 if (ret) {
2458 free_extent_buffer(next);
2459 return ret;
2460 }
Chris Masone02119d2008-09-05 16:13:11 -04002461
Josef Bacik681ae502013-10-07 15:11:00 -04002462 if (trans) {
2463 btrfs_tree_lock(next);
2464 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002465 clean_tree_block(trans, root->fs_info,
2466 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002467 btrfs_wait_tree_block_writeback(next);
2468 btrfs_tree_unlock(next);
Liu Bo0f4adc12018-01-25 11:02:51 -07002469 } else {
2470 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2471 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002472 }
Chris Masone02119d2008-09-05 16:13:11 -04002473
Chris Masone02119d2008-09-05 16:13:11 -04002474 WARN_ON(root_owner !=
2475 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002476 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002477 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002478 if (ret) {
2479 free_extent_buffer(next);
2480 return ret;
2481 }
Chris Masone02119d2008-09-05 16:13:11 -04002482 }
2483 free_extent_buffer(next);
2484 continue;
2485 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002486 ret = btrfs_read_buffer(next, ptr_gen);
2487 if (ret) {
2488 free_extent_buffer(next);
2489 return ret;
2490 }
Chris Masone02119d2008-09-05 16:13:11 -04002491
2492 WARN_ON(*level <= 0);
2493 if (path->nodes[*level-1])
2494 free_extent_buffer(path->nodes[*level-1]);
2495 path->nodes[*level-1] = next;
2496 *level = btrfs_header_level(next);
2497 path->slots[*level] = 0;
2498 cond_resched();
2499 }
2500 WARN_ON(*level < 0);
2501 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2502
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002503 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002504
2505 cond_resched();
2506 return 0;
2507}
2508
Chris Masond3977122009-01-05 21:25:51 -05002509static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002510 struct btrfs_root *root,
2511 struct btrfs_path *path, int *level,
2512 struct walk_control *wc)
2513{
2514 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002515 int i;
2516 int slot;
2517 int ret;
2518
Chris Masond3977122009-01-05 21:25:51 -05002519 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002520 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002521 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002522 path->slots[i]++;
2523 *level = i;
2524 WARN_ON(*level == 0);
2525 return 0;
2526 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002527 struct extent_buffer *parent;
2528 if (path->nodes[*level] == root->node)
2529 parent = path->nodes[*level];
2530 else
2531 parent = path->nodes[*level + 1];
2532
2533 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002534 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002535 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002536 if (ret)
2537 return ret;
2538
Chris Masone02119d2008-09-05 16:13:11 -04002539 if (wc->free) {
2540 struct extent_buffer *next;
2541
2542 next = path->nodes[*level];
2543
Josef Bacik681ae502013-10-07 15:11:00 -04002544 if (trans) {
2545 btrfs_tree_lock(next);
2546 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002547 clean_tree_block(trans, root->fs_info,
2548 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002549 btrfs_wait_tree_block_writeback(next);
2550 btrfs_tree_unlock(next);
Liu Bo0f4adc12018-01-25 11:02:51 -07002551 } else {
2552 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2553 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002554 }
Chris Masone02119d2008-09-05 16:13:11 -04002555
Chris Masone02119d2008-09-05 16:13:11 -04002556 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002557 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002558 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002559 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002560 if (ret)
2561 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002562 }
2563 free_extent_buffer(path->nodes[*level]);
2564 path->nodes[*level] = NULL;
2565 *level = i + 1;
2566 }
2567 }
2568 return 1;
2569}
2570
2571/*
2572 * drop the reference count on the tree rooted at 'snap'. This traverses
2573 * the tree freeing any blocks that have a ref count of zero after being
2574 * decremented.
2575 */
2576static int walk_log_tree(struct btrfs_trans_handle *trans,
2577 struct btrfs_root *log, struct walk_control *wc)
2578{
2579 int ret = 0;
2580 int wret;
2581 int level;
2582 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002583 int orig_level;
2584
2585 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002586 if (!path)
2587 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002588
2589 level = btrfs_header_level(log->node);
2590 orig_level = level;
2591 path->nodes[level] = log->node;
2592 extent_buffer_get(log->node);
2593 path->slots[level] = 0;
2594
Chris Masond3977122009-01-05 21:25:51 -05002595 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002596 wret = walk_down_log_tree(trans, log, path, &level, wc);
2597 if (wret > 0)
2598 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002599 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002600 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002601 goto out;
2602 }
Chris Masone02119d2008-09-05 16:13:11 -04002603
2604 wret = walk_up_log_tree(trans, log, path, &level, wc);
2605 if (wret > 0)
2606 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002607 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002608 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002609 goto out;
2610 }
Chris Masone02119d2008-09-05 16:13:11 -04002611 }
2612
2613 /* was the root node processed? if not, catch it here */
2614 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002615 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002616 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002617 if (ret)
2618 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002619 if (wc->free) {
2620 struct extent_buffer *next;
2621
2622 next = path->nodes[orig_level];
2623
Josef Bacik681ae502013-10-07 15:11:00 -04002624 if (trans) {
2625 btrfs_tree_lock(next);
2626 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002627 clean_tree_block(trans, log->fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002628 btrfs_wait_tree_block_writeback(next);
2629 btrfs_tree_unlock(next);
Liu Bo0f4adc12018-01-25 11:02:51 -07002630 } else {
2631 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2632 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002633 }
Chris Masone02119d2008-09-05 16:13:11 -04002634
Chris Masone02119d2008-09-05 16:13:11 -04002635 WARN_ON(log->root_key.objectid !=
2636 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002637 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002638 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002639 if (ret)
2640 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002641 }
2642 }
2643
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002644out:
Chris Masone02119d2008-09-05 16:13:11 -04002645 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002646 return ret;
2647}
2648
Yan Zheng7237f182009-01-21 12:54:03 -05002649/*
2650 * helper function to update the item for a given subvolumes log root
2651 * in the tree of log roots
2652 */
2653static int update_log_root(struct btrfs_trans_handle *trans,
2654 struct btrfs_root *log)
2655{
2656 int ret;
2657
2658 if (log->log_transid == 1) {
2659 /* insert root item on the first sync */
2660 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2661 &log->root_key, &log->root_item);
2662 } else {
2663 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2664 &log->root_key, &log->root_item);
2665 }
2666 return ret;
2667}
2668
Zhaolei60d53eb2015-08-17 18:44:46 +08002669static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002670{
2671 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002672 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002673
Yan Zheng7237f182009-01-21 12:54:03 -05002674 /*
2675 * we only allow two pending log transactions at a time,
2676 * so we know that if ours is more than 2 older than the
2677 * current transaction, we're done
2678 */
Chris Masone02119d2008-09-05 16:13:11 -04002679 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002680 prepare_to_wait(&root->log_commit_wait[index],
2681 &wait, TASK_UNINTERRUPTIBLE);
2682 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002683
Miao Xied1433de2014-02-20 18:08:59 +08002684 if (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002685 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002686 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002687
Yan Zheng7237f182009-01-21 12:54:03 -05002688 finish_wait(&root->log_commit_wait[index], &wait);
2689 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002690 } while (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002691 atomic_read(&root->log_commit[index]));
Yan Zheng7237f182009-01-21 12:54:03 -05002692}
2693
Zhaolei60d53eb2015-08-17 18:44:46 +08002694static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002695{
2696 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002697
2698 while (atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002699 prepare_to_wait(&root->log_writer_wait,
2700 &wait, TASK_UNINTERRUPTIBLE);
2701 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002702 if (atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002703 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002704 finish_wait(&root->log_writer_wait, &wait);
Filipe Manana575849e2015-02-11 11:12:39 +00002705 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002706 }
Chris Masone02119d2008-09-05 16:13:11 -04002707}
2708
Miao Xie8b050d32014-02-20 18:08:58 +08002709static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2710 struct btrfs_log_ctx *ctx)
2711{
2712 if (!ctx)
2713 return;
2714
2715 mutex_lock(&root->log_mutex);
2716 list_del_init(&ctx->list);
2717 mutex_unlock(&root->log_mutex);
2718}
2719
2720/*
2721 * Invoked in log mutex context, or be sure there is no other task which
2722 * can access the list.
2723 */
2724static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2725 int index, int error)
2726{
2727 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002728 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002729
Chris Mason570dd452016-10-27 10:42:20 -07002730 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2731 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002732 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002733 }
Miao Xie8b050d32014-02-20 18:08:58 +08002734
2735 INIT_LIST_HEAD(&root->log_ctxs[index]);
2736}
2737
Chris Masone02119d2008-09-05 16:13:11 -04002738/*
2739 * btrfs_sync_log does sends a given tree log down to the disk and
2740 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002741 * you know that any inodes previously logged are safely on disk only
2742 * if it returns 0.
2743 *
2744 * Any other return value means you need to call btrfs_commit_transaction.
2745 * Some of the edge cases for fsyncing directories that have had unlinks
2746 * or renames done in the past mean that sometimes the only safe
2747 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2748 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002749 */
2750int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002751 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002752{
Yan Zheng7237f182009-01-21 12:54:03 -05002753 int index1;
2754 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002755 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002756 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002757 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002758 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002759 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002760 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002761 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002762
Yan Zheng7237f182009-01-21 12:54:03 -05002763 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002764 log_transid = ctx->log_transid;
2765 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002766 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002767 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002768 }
Miao Xied1433de2014-02-20 18:08:59 +08002769
2770 index1 = log_transid % 2;
2771 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002772 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002773 mutex_unlock(&root->log_mutex);
2774 return ctx->log_ret;
2775 }
2776 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002777 atomic_set(&root->log_commit[index1], 1);
2778
2779 /* wait for previous tree log sync to complete */
2780 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002781 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002782
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002783 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002784 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002785 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney3cdde222016-06-09 21:38:35 -04002786 if (!btrfs_test_opt(root->fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08002787 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002788 mutex_unlock(&root->log_mutex);
2789 schedule_timeout_uninterruptible(1);
2790 mutex_lock(&root->log_mutex);
2791 }
Zhaolei60d53eb2015-08-17 18:44:46 +08002792 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002793 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002794 break;
2795 }
Chris Masond0c803c2008-09-11 16:17:57 -04002796
Chris Mason12fcfd22009-03-24 10:24:20 -04002797 /* bail out if we need to do a full commit */
Miao Xie995946d2014-04-02 19:51:06 +08002798 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002799 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002800 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002801 mutex_unlock(&root->log_mutex);
2802 goto out;
2803 }
2804
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002805 if (log_transid % 2 == 0)
2806 mark = EXTENT_DIRTY;
2807 else
2808 mark = EXTENT_NEW;
2809
Chris Mason690587d2009-10-13 13:29:19 -04002810 /* we start IO on all the marked extents here, but we don't actually
2811 * wait for them until later.
2812 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002813 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002814 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002815 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002816 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04002817 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002818 btrfs_free_logged_extents(log, log_transid);
Miao Xie995946d2014-04-02 19:51:06 +08002819 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002820 mutex_unlock(&root->log_mutex);
2821 goto out;
2822 }
Yan Zheng7237f182009-01-21 12:54:03 -05002823
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002824 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002825
Yan Zheng7237f182009-01-21 12:54:03 -05002826 root->log_transid++;
2827 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002828 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002829 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002830 * IO has been started, blocks of the log tree have WRITTEN flag set
2831 * in their headers. new modifications of the log will be written to
2832 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002833 */
2834 mutex_unlock(&root->log_mutex);
2835
Filipe Manana28a23592016-08-23 21:13:51 +01002836 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08002837
Yan Zheng7237f182009-01-21 12:54:03 -05002838 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002839 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002840 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002841
2842 index2 = log_root_tree->log_transid % 2;
2843 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2844 root_log_ctx.log_transid = log_root_tree->log_transid;
2845
Yan Zheng7237f182009-01-21 12:54:03 -05002846 mutex_unlock(&log_root_tree->log_mutex);
2847
2848 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002849
2850 mutex_lock(&log_root_tree->log_mutex);
2851 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +01002852 /*
2853 * Implicit memory barrier after atomic_dec_and_test
2854 */
Yan Zheng7237f182009-01-21 12:54:03 -05002855 if (waitqueue_active(&log_root_tree->log_writer_wait))
2856 wake_up(&log_root_tree->log_writer_wait);
2857 }
2858
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002859 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002860 if (!list_empty(&root_log_ctx.list))
2861 list_del_init(&root_log_ctx.list);
2862
Miao Xiec6adc9c2013-05-28 10:05:39 +00002863 blk_finish_plug(&plug);
Miao Xie995946d2014-04-02 19:51:06 +08002864 btrfs_set_log_full_commit(root->fs_info, trans);
2865
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002866 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002867 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002868 mutex_unlock(&log_root_tree->log_mutex);
2869 goto out;
2870 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002871 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002872 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002873 mutex_unlock(&log_root_tree->log_mutex);
2874 ret = -EAGAIN;
2875 goto out;
2876 }
2877
Miao Xied1433de2014-02-20 18:08:59 +08002878 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08002879 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07002880 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08002881 mutex_unlock(&log_root_tree->log_mutex);
2882 ret = root_log_ctx.log_ret;
2883 goto out;
2884 }
Miao Xie8b050d32014-02-20 18:08:58 +08002885
Miao Xied1433de2014-02-20 18:08:59 +08002886 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05002887 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002888 blk_finish_plug(&plug);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002889 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages,
2890 mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05002891 btrfs_wait_logged_extents(trans, log, log_transid);
Zhaolei60d53eb2015-08-17 18:44:46 +08002892 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002893 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002894 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002895 if (!ret)
2896 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002897 goto out;
2898 }
Miao Xied1433de2014-02-20 18:08:59 +08002899 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002900 atomic_set(&log_root_tree->log_commit[index2], 1);
2901
Chris Mason12fcfd22009-03-24 10:24:20 -04002902 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002903 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002904 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002905 }
Yan Zheng7237f182009-01-21 12:54:03 -05002906
Zhaolei60d53eb2015-08-17 18:44:46 +08002907 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04002908
2909 /*
2910 * now that we've moved on to the tree of log tree roots,
2911 * check the full commit flag again
2912 */
Miao Xie995946d2014-04-02 19:51:06 +08002913 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002914 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002915 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002916 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002917 mutex_unlock(&log_root_tree->log_mutex);
2918 ret = -EAGAIN;
2919 goto out_wake_log_root;
2920 }
Yan Zheng7237f182009-01-21 12:54:03 -05002921
Miao Xiec6adc9c2013-05-28 10:05:39 +00002922 ret = btrfs_write_marked_extents(log_root_tree,
2923 &log_root_tree->dirty_log_pages,
2924 EXTENT_DIRTY | EXTENT_NEW);
2925 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002926 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002927 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002928 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002929 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002930 mutex_unlock(&log_root_tree->log_mutex);
2931 goto out_wake_log_root;
2932 }
Filipe Manana5ab5e442014-11-13 16:59:53 +00002933 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2934 if (!ret)
2935 ret = btrfs_wait_marked_extents(log_root_tree,
2936 &log_root_tree->dirty_log_pages,
2937 EXTENT_NEW | EXTENT_DIRTY);
2938 if (ret) {
2939 btrfs_set_log_full_commit(root->fs_info, trans);
2940 btrfs_free_logged_extents(log, log_transid);
2941 mutex_unlock(&log_root_tree->log_mutex);
2942 goto out_wake_log_root;
2943 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05002944 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002945
David Sterba6c417612011-04-13 15:41:04 +02002946 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002947 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002948 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002949 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002950
Yan Zheng7237f182009-01-21 12:54:03 -05002951 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002952 mutex_unlock(&log_root_tree->log_mutex);
2953
2954 /*
2955 * nobody else is going to jump in and write the the ctree
2956 * super here because the log_commit atomic below is protecting
2957 * us. We must be called with a transaction handle pinning
2958 * the running transaction open, so a full commit can't hop
2959 * in and cause problems either.
2960 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002961 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002962 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002963 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002964 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002965 goto out_wake_log_root;
2966 }
Yan Zheng7237f182009-01-21 12:54:03 -05002967
Chris Mason257c62e2009-10-13 13:21:08 -04002968 mutex_lock(&root->log_mutex);
2969 if (root->last_log_commit < log_transid)
2970 root->last_log_commit = log_transid;
2971 mutex_unlock(&root->log_mutex);
2972
Chris Mason12fcfd22009-03-24 10:24:20 -04002973out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07002974 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002975 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2976
Miao Xied1433de2014-02-20 18:08:59 +08002977 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002978 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002979 mutex_unlock(&log_root_tree->log_mutex);
2980
David Sterba33a9eca2015-10-10 18:35:10 +02002981 /*
David Sterba65cb4692018-04-24 14:53:56 +02002982 * The barrier before waitqueue_active is needed so all the updates
2983 * above are seen by the woken threads. It might not be necessary, but
2984 * proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02002985 */
David Sterba65cb4692018-04-24 14:53:56 +02002986 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05002987 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2988 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002989out:
Miao Xied1433de2014-02-20 18:08:59 +08002990 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07002991 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08002992 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002993 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002994 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002995
David Sterba33a9eca2015-10-10 18:35:10 +02002996 /*
David Sterba65cb4692018-04-24 14:53:56 +02002997 * The barrier before waitqueue_active is needed so all the updates
2998 * above are seen by the woken threads. It might not be necessary, but
2999 * proving that seems to be hard.
David Sterba33a9eca2015-10-10 18:35:10 +02003000 */
David Sterba65cb4692018-04-24 14:53:56 +02003001 smp_mb();
Yan Zheng7237f182009-01-21 12:54:03 -05003002 if (waitqueue_active(&root->log_commit_wait[index1]))
3003 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05003004 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003005}
3006
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003007static void free_log_tree(struct btrfs_trans_handle *trans,
3008 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003009{
3010 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04003011 u64 start;
3012 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04003013 struct walk_control wc = {
3014 .free = 1,
3015 .process_func = process_one_buffer
3016 };
3017
Josef Bacik681ae502013-10-07 15:11:00 -04003018 ret = walk_log_tree(trans, log, &wc);
Jeff Mahoney1bd71122018-09-06 16:59:33 -04003019 if (ret) {
3020 if (trans)
3021 btrfs_abort_transaction(trans, ret);
3022 else
3023 btrfs_handle_fs_error(log->fs_info, ret, NULL);
3024 }
Chris Masone02119d2008-09-05 16:13:11 -04003025
Chris Masond3977122009-01-05 21:25:51 -05003026 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003027 ret = find_first_extent_bit(&log->dirty_log_pages,
Liu Bobc0d4312018-01-25 11:02:52 -07003028 0, &start, &end,
3029 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
Josef Bacike6138872012-09-27 17:07:30 -04003030 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003031 if (ret)
3032 break;
3033
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003034 clear_extent_bits(&log->dirty_log_pages, start, end,
Liu Bobc0d4312018-01-25 11:02:52 -07003035 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Chris Masond0c803c2008-09-11 16:17:57 -04003036 }
3037
Josef Bacik2ab28f32012-10-12 15:27:49 -04003038 /*
3039 * We may have short-circuited the log tree with the full commit logic
3040 * and left ordered extents on our list, so clear these out to keep us
3041 * from leaking inodes and memory.
3042 */
3043 btrfs_free_logged_extents(log, 0);
3044 btrfs_free_logged_extents(log, 1);
3045
Yan Zheng7237f182009-01-21 12:54:03 -05003046 free_extent_buffer(log->node);
3047 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003048}
3049
3050/*
3051 * free all the extents used by the tree log. This should be called
3052 * at commit time of the full transaction
3053 */
3054int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3055{
3056 if (root->log_root) {
3057 free_log_tree(trans, root->log_root);
3058 root->log_root = NULL;
3059 }
3060 return 0;
3061}
3062
3063int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3064 struct btrfs_fs_info *fs_info)
3065{
3066 if (fs_info->log_root_tree) {
3067 free_log_tree(trans, fs_info->log_root_tree);
3068 fs_info->log_root_tree = NULL;
3069 }
Chris Masone02119d2008-09-05 16:13:11 -04003070 return 0;
3071}
3072
3073/*
Chris Masone02119d2008-09-05 16:13:11 -04003074 * If both a file and directory are logged, and unlinks or renames are
3075 * mixed in, we have a few interesting corners:
3076 *
3077 * create file X in dir Y
3078 * link file X to X.link in dir Y
3079 * fsync file X
3080 * unlink file X but leave X.link
3081 * fsync dir Y
3082 *
3083 * After a crash we would expect only X.link to exist. But file X
3084 * didn't get fsync'd again so the log has back refs for X and X.link.
3085 *
3086 * We solve this by removing directory entries and inode backrefs from the
3087 * log when a file that was logged in the current transaction is
3088 * unlinked. Any later fsync will include the updated log entries, and
3089 * we'll be able to reconstruct the proper directory items from backrefs.
3090 *
3091 * This optimizations allows us to avoid relogging the entire inode
3092 * or the entire directory.
3093 */
3094int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3095 struct btrfs_root *root,
3096 const char *name, int name_len,
3097 struct inode *dir, u64 index)
3098{
3099 struct btrfs_root *log;
3100 struct btrfs_dir_item *di;
3101 struct btrfs_path *path;
3102 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003103 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003104 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003105 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003106
Chris Mason3a5f1d42008-09-11 15:53:37 -04003107 if (BTRFS_I(dir)->logged_trans < trans->transid)
3108 return 0;
3109
Chris Masone02119d2008-09-05 16:13:11 -04003110 ret = join_running_log_trans(root);
3111 if (ret)
3112 return 0;
3113
3114 mutex_lock(&BTRFS_I(dir)->log_mutex);
3115
3116 log = root->log_root;
3117 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003118 if (!path) {
3119 err = -ENOMEM;
3120 goto out_unlock;
3121 }
liubo2a29edc2011-01-26 06:22:08 +00003122
Li Zefan33345d012011-04-20 10:31:50 +08003123 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003124 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003125 if (IS_ERR(di)) {
3126 err = PTR_ERR(di);
3127 goto fail;
3128 }
3129 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003130 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3131 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003132 if (ret) {
3133 err = ret;
3134 goto fail;
3135 }
Chris Masone02119d2008-09-05 16:13:11 -04003136 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003137 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003138 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003139 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003140 if (IS_ERR(di)) {
3141 err = PTR_ERR(di);
3142 goto fail;
3143 }
3144 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003145 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3146 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003147 if (ret) {
3148 err = ret;
3149 goto fail;
3150 }
Chris Masone02119d2008-09-05 16:13:11 -04003151 }
3152
3153 /* update the directory size in the log to reflect the names
3154 * we have removed
3155 */
3156 if (bytes_del) {
3157 struct btrfs_key key;
3158
Li Zefan33345d012011-04-20 10:31:50 +08003159 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003160 key.offset = 0;
3161 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003162 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003163
3164 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003165 if (ret < 0) {
3166 err = ret;
3167 goto fail;
3168 }
Chris Masone02119d2008-09-05 16:13:11 -04003169 if (ret == 0) {
3170 struct btrfs_inode_item *item;
3171 u64 i_size;
3172
3173 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3174 struct btrfs_inode_item);
3175 i_size = btrfs_inode_size(path->nodes[0], item);
3176 if (i_size > bytes_del)
3177 i_size -= bytes_del;
3178 else
3179 i_size = 0;
3180 btrfs_set_inode_size(path->nodes[0], item, i_size);
3181 btrfs_mark_buffer_dirty(path->nodes[0]);
3182 } else
3183 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003184 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003185 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003186fail:
Chris Masone02119d2008-09-05 16:13:11 -04003187 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003188out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04003189 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003190 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003191 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003192 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003193 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003194 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003195
Chris Mason12fcfd22009-03-24 10:24:20 -04003196 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003197
Andi Kleen411fc6b2010-10-29 15:14:31 -04003198 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003199}
3200
3201/* see comments for btrfs_del_dir_entries_in_log */
3202int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3203 struct btrfs_root *root,
3204 const char *name, int name_len,
3205 struct inode *inode, u64 dirid)
3206{
3207 struct btrfs_root *log;
3208 u64 index;
3209 int ret;
3210
Chris Mason3a5f1d42008-09-11 15:53:37 -04003211 if (BTRFS_I(inode)->logged_trans < trans->transid)
3212 return 0;
3213
Chris Masone02119d2008-09-05 16:13:11 -04003214 ret = join_running_log_trans(root);
3215 if (ret)
3216 return 0;
3217 log = root->log_root;
3218 mutex_lock(&BTRFS_I(inode)->log_mutex);
3219
Li Zefan33345d012011-04-20 10:31:50 +08003220 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003221 dirid, &index);
3222 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003223 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003224 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003225 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003226 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003227 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003228 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003229
Chris Masone02119d2008-09-05 16:13:11 -04003230 return ret;
3231}
3232
3233/*
3234 * creates a range item in the log for 'dirid'. first_offset and
3235 * last_offset tell us which parts of the key space the log should
3236 * be considered authoritative for.
3237 */
3238static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3239 struct btrfs_root *log,
3240 struct btrfs_path *path,
3241 int key_type, u64 dirid,
3242 u64 first_offset, u64 last_offset)
3243{
3244 int ret;
3245 struct btrfs_key key;
3246 struct btrfs_dir_log_item *item;
3247
3248 key.objectid = dirid;
3249 key.offset = first_offset;
3250 if (key_type == BTRFS_DIR_ITEM_KEY)
3251 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3252 else
3253 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3254 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003255 if (ret)
3256 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003257
3258 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3259 struct btrfs_dir_log_item);
3260 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3261 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003262 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003263 return 0;
3264}
3265
3266/*
3267 * log all the items included in the current transaction for a given
3268 * directory. This also creates the range items in the log tree required
3269 * to replay anything deleted before the fsync
3270 */
3271static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3272 struct btrfs_root *root, struct inode *inode,
3273 struct btrfs_path *path,
3274 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003275 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003276 u64 min_offset, u64 *last_offset_ret)
3277{
3278 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003279 struct btrfs_root *log = root->log_root;
3280 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003281 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003282 int ret;
3283 int i;
3284 int nritems;
3285 u64 first_offset = min_offset;
3286 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08003287 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003288
3289 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003290
Li Zefan33345d012011-04-20 10:31:50 +08003291 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003292 min_key.type = key_type;
3293 min_key.offset = min_offset;
3294
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003295 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003296
3297 /*
3298 * we didn't find anything from this transaction, see if there
3299 * is anything at all
3300 */
Li Zefan33345d012011-04-20 10:31:50 +08003301 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3302 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003303 min_key.type = key_type;
3304 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003305 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003306 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3307 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003308 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003309 return ret;
3310 }
Li Zefan33345d012011-04-20 10:31:50 +08003311 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003312
3313 /* if ret == 0 there are items for this type,
3314 * create a range to tell us the last key of this type.
3315 * otherwise, there are no items in this directory after
3316 * *min_offset, and we create a range to indicate that.
3317 */
3318 if (ret == 0) {
3319 struct btrfs_key tmp;
3320 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3321 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003322 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003323 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003324 }
3325 goto done;
3326 }
3327
3328 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003329 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003330 if (ret == 0) {
3331 struct btrfs_key tmp;
3332 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3333 if (key_type == tmp.type) {
3334 first_offset = tmp.offset;
3335 ret = overwrite_item(trans, log, dst_path,
3336 path->nodes[0], path->slots[0],
3337 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003338 if (ret) {
3339 err = ret;
3340 goto done;
3341 }
Chris Masone02119d2008-09-05 16:13:11 -04003342 }
3343 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003344 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003345
3346 /* find the first key from this transaction again */
3347 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303348 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003349 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003350
3351 /*
3352 * we have a block from this transaction, log every item in it
3353 * from our directory
3354 */
Chris Masond3977122009-01-05 21:25:51 -05003355 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003356 struct btrfs_key tmp;
3357 src = path->nodes[0];
3358 nritems = btrfs_header_nritems(src);
3359 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003360 struct btrfs_dir_item *di;
3361
Chris Masone02119d2008-09-05 16:13:11 -04003362 btrfs_item_key_to_cpu(src, &min_key, i);
3363
Li Zefan33345d012011-04-20 10:31:50 +08003364 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003365 goto done;
3366 ret = overwrite_item(trans, log, dst_path, src, i,
3367 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003368 if (ret) {
3369 err = ret;
3370 goto done;
3371 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003372
3373 /*
3374 * We must make sure that when we log a directory entry,
3375 * the corresponding inode, after log replay, has a
3376 * matching link count. For example:
3377 *
3378 * touch foo
3379 * mkdir mydir
3380 * sync
3381 * ln foo mydir/bar
3382 * xfs_io -c "fsync" mydir
3383 * <crash>
3384 * <mount fs and log replay>
3385 *
3386 * Would result in a fsync log that when replayed, our
3387 * file inode would have a link count of 1, but we get
3388 * two directory entries pointing to the same inode.
3389 * After removing one of the names, it would not be
3390 * possible to remove the other name, which resulted
3391 * always in stale file handle errors, and would not
3392 * be possible to rmdir the parent directory, since
3393 * its i_size could never decrement to the value
3394 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3395 */
3396 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3397 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3398 if (ctx &&
3399 (btrfs_dir_transid(src, di) == trans->transid ||
3400 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3401 tmp.type != BTRFS_ROOT_ITEM_KEY)
3402 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003403 }
3404 path->slots[0] = nritems;
3405
3406 /*
3407 * look ahead to the next item and see if it is also
3408 * from this directory and from this transaction
3409 */
3410 ret = btrfs_next_leaf(root, path);
Liu Bo14c4d5f2018-04-03 01:59:47 +08003411 if (ret) {
3412 if (ret == 1)
3413 last_offset = (u64)-1;
3414 else
3415 err = ret;
Chris Masone02119d2008-09-05 16:13:11 -04003416 goto done;
3417 }
3418 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003419 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003420 last_offset = (u64)-1;
3421 goto done;
3422 }
3423 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3424 ret = overwrite_item(trans, log, dst_path,
3425 path->nodes[0], path->slots[0],
3426 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003427 if (ret)
3428 err = ret;
3429 else
3430 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003431 goto done;
3432 }
3433 }
3434done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003435 btrfs_release_path(path);
3436 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003437
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003438 if (err == 0) {
3439 *last_offset_ret = last_offset;
3440 /*
3441 * insert the log range keys to indicate where the log
3442 * is valid
3443 */
3444 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003445 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003446 if (ret)
3447 err = ret;
3448 }
3449 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003450}
3451
3452/*
3453 * logging directories is very similar to logging inodes, We find all the items
3454 * from the current transaction and write them to the log.
3455 *
3456 * The recovery code scans the directory in the subvolume, and if it finds a
3457 * key in the range logged that is not present in the log tree, then it means
3458 * that dir entry was unlinked during the transaction.
3459 *
3460 * In order for that scan to work, we must include one key smaller than
3461 * the smallest logged by this transaction and one key larger than the largest
3462 * key logged by this transaction.
3463 */
3464static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3465 struct btrfs_root *root, struct inode *inode,
3466 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003467 struct btrfs_path *dst_path,
3468 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003469{
3470 u64 min_key;
3471 u64 max_key;
3472 int ret;
3473 int key_type = BTRFS_DIR_ITEM_KEY;
3474
3475again:
3476 min_key = 0;
3477 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003478 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003479 ret = log_dir_items(trans, root, inode, path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003480 dst_path, key_type, ctx, min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003481 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003482 if (ret)
3483 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003484 if (max_key == (u64)-1)
3485 break;
3486 min_key = max_key + 1;
3487 }
3488
3489 if (key_type == BTRFS_DIR_ITEM_KEY) {
3490 key_type = BTRFS_DIR_INDEX_KEY;
3491 goto again;
3492 }
3493 return 0;
3494}
3495
3496/*
3497 * a helper function to drop items from the log before we relog an
3498 * inode. max_key_type indicates the highest item type to remove.
3499 * This cannot be run for file data extents because it does not
3500 * free the extents they point to.
3501 */
3502static int drop_objectid_items(struct btrfs_trans_handle *trans,
3503 struct btrfs_root *log,
3504 struct btrfs_path *path,
3505 u64 objectid, int max_key_type)
3506{
3507 int ret;
3508 struct btrfs_key key;
3509 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003510 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003511
3512 key.objectid = objectid;
3513 key.type = max_key_type;
3514 key.offset = (u64)-1;
3515
Chris Masond3977122009-01-05 21:25:51 -05003516 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003517 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003518 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003519 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003520 break;
3521
3522 if (path->slots[0] == 0)
3523 break;
3524
3525 path->slots[0]--;
3526 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3527 path->slots[0]);
3528
3529 if (found_key.objectid != objectid)
3530 break;
3531
Josef Bacik18ec90d2012-09-28 11:56:28 -04003532 found_key.offset = 0;
3533 found_key.type = 0;
3534 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3535 &start_slot);
3536
3537 ret = btrfs_del_items(trans, log, path, start_slot,
3538 path->slots[0] - start_slot + 1);
3539 /*
3540 * If start slot isn't 0 then we don't need to re-search, we've
3541 * found the last guy with the objectid in this tree.
3542 */
3543 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003544 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003545 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003546 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003547 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003548 if (ret > 0)
3549 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003550 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003551}
3552
Josef Bacik94edf4a2012-09-25 14:56:25 -04003553static void fill_inode_item(struct btrfs_trans_handle *trans,
3554 struct extent_buffer *leaf,
3555 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003556 struct inode *inode, int log_inode_only,
3557 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003558{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003559 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003560
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003561 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003562
3563 if (log_inode_only) {
3564 /* set the generation to zero so the recover code
3565 * can tell the difference between an logging
3566 * just to say 'this inode exists' and a logging
3567 * to say 'update this inode with these values'
3568 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003569 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003570 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003571 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003572 btrfs_set_token_inode_generation(leaf, item,
3573 BTRFS_I(inode)->generation,
3574 &token);
3575 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003576 }
3577
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003578 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3579 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3580 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3581 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3582
David Sterbaa937b972014-12-12 17:39:12 +01003583 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003584 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003585 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003586 inode->i_atime.tv_nsec, &token);
3587
David Sterbaa937b972014-12-12 17:39:12 +01003588 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003589 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003590 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003591 inode->i_mtime.tv_nsec, &token);
3592
David Sterbaa937b972014-12-12 17:39:12 +01003593 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003594 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003595 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003596 inode->i_ctime.tv_nsec, &token);
3597
3598 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3599 &token);
3600
3601 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3602 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3603 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3604 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3605 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003606}
3607
Josef Bacika95249b2012-10-11 16:17:34 -04003608static int log_inode_item(struct btrfs_trans_handle *trans,
3609 struct btrfs_root *log, struct btrfs_path *path,
3610 struct inode *inode)
3611{
3612 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003613 int ret;
3614
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003615 ret = btrfs_insert_empty_item(trans, log, path,
3616 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003617 sizeof(*inode_item));
3618 if (ret && ret != -EEXIST)
3619 return ret;
3620 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3621 struct btrfs_inode_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003622 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003623 btrfs_release_path(path);
3624 return 0;
3625}
3626
Chris Mason31ff1cd2008-09-11 16:17:57 -04003627static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003628 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003629 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003630 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003631 int start_slot, int nr, int inode_only,
3632 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003633{
3634 unsigned long src_offset;
3635 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003636 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003637 struct btrfs_file_extent_item *extent;
3638 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003639 struct extent_buffer *src = src_path->nodes[0];
3640 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003641 int ret;
3642 struct btrfs_key *ins_keys;
3643 u32 *ins_sizes;
3644 char *ins_data;
3645 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003646 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003647 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003648 bool has_extents = false;
Filipe Manana74121f72014-08-07 12:00:44 +01003649 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003650 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003651
3652 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003653
3654 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3655 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003656 if (!ins_data)
3657 return -ENOMEM;
3658
Josef Bacik16e75492013-10-22 12:18:51 -04003659 first_key.objectid = (u64)-1;
3660
Chris Mason31ff1cd2008-09-11 16:17:57 -04003661 ins_sizes = (u32 *)ins_data;
3662 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3663
3664 for (i = 0; i < nr; i++) {
3665 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3666 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3667 }
3668 ret = btrfs_insert_empty_items(trans, log, dst_path,
3669 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003670 if (ret) {
3671 kfree(ins_data);
3672 return ret;
3673 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003674
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003675 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003676 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3677 dst_path->slots[0]);
3678
3679 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3680
Matthias Kaehlckee7f42b02017-07-27 14:30:23 -07003681 if (i == nr - 1)
Josef Bacik16e75492013-10-22 12:18:51 -04003682 last_key = ins_keys[i];
3683
Josef Bacik94edf4a2012-09-25 14:56:25 -04003684 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003685 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3686 dst_path->slots[0],
3687 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003688 fill_inode_item(trans, dst_path->nodes[0], inode_item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003689 inode, inode_only == LOG_INODE_EXISTS,
3690 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003691 } else {
3692 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3693 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003694 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003695
Josef Bacik16e75492013-10-22 12:18:51 -04003696 /*
3697 * We set need_find_last_extent here in case we know we were
3698 * processing other items and then walk into the first extent in
3699 * the inode. If we don't hit an extent then nothing changes,
3700 * we'll do the last search the next time around.
3701 */
3702 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3703 has_extents = true;
Filipe Manana74121f72014-08-07 12:00:44 +01003704 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003705 first_key = ins_keys[i];
3706 } else {
3707 need_find_last_extent = false;
3708 }
3709
Chris Mason31ff1cd2008-09-11 16:17:57 -04003710 /* take a reference on file data extents so that truncates
3711 * or deletes of this inode don't have to relog the inode
3712 * again
3713 */
David Sterba962a2982014-06-04 18:41:45 +02003714 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003715 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003716 int found_type;
3717 extent = btrfs_item_ptr(src, start_slot + i,
3718 struct btrfs_file_extent_item);
3719
liubo8e531cd2011-05-06 10:36:09 +08003720 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3721 continue;
3722
Chris Mason31ff1cd2008-09-11 16:17:57 -04003723 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003724 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003725 u64 ds, dl, cs, cl;
3726 ds = btrfs_file_extent_disk_bytenr(src,
3727 extent);
3728 /* ds == 0 is a hole */
3729 if (ds == 0)
3730 continue;
3731
3732 dl = btrfs_file_extent_disk_num_bytes(src,
3733 extent);
3734 cs = btrfs_file_extent_offset(src, extent);
3735 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003736 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003737 if (btrfs_file_extent_compression(src,
3738 extent)) {
3739 cs = 0;
3740 cl = dl;
3741 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003742
3743 ret = btrfs_lookup_csums_range(
3744 log->fs_info->csum_root,
3745 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003746 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003747 if (ret) {
3748 btrfs_release_path(dst_path);
3749 kfree(ins_data);
3750 return ret;
3751 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003752 }
3753 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003754 }
3755
3756 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003757 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003758 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003759
3760 /*
3761 * we have to do this after the loop above to avoid changing the
3762 * log tree while trying to change the log tree.
3763 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003764 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003765 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003766 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3767 struct btrfs_ordered_sum,
3768 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003769 if (!ret)
3770 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003771 list_del(&sums->list);
3772 kfree(sums);
3773 }
Josef Bacik16e75492013-10-22 12:18:51 -04003774
3775 if (!has_extents)
3776 return ret;
3777
Filipe Manana74121f72014-08-07 12:00:44 +01003778 if (need_find_last_extent && *last_extent == first_key.offset) {
3779 /*
3780 * We don't have any leafs between our current one and the one
3781 * we processed before that can have file extent items for our
3782 * inode (and have a generation number smaller than our current
3783 * transaction id).
3784 */
3785 need_find_last_extent = false;
3786 }
3787
Josef Bacik16e75492013-10-22 12:18:51 -04003788 /*
3789 * Because we use btrfs_search_forward we could skip leaves that were
3790 * not modified and then assume *last_extent is valid when it really
3791 * isn't. So back up to the previous leaf and read the end of the last
3792 * extent before we go and fill in holes.
3793 */
3794 if (need_find_last_extent) {
3795 u64 len;
3796
3797 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3798 if (ret < 0)
3799 return ret;
3800 if (ret)
3801 goto fill_holes;
3802 if (src_path->slots[0])
3803 src_path->slots[0]--;
3804 src = src_path->nodes[0];
3805 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3806 if (key.objectid != btrfs_ino(inode) ||
3807 key.type != BTRFS_EXTENT_DATA_KEY)
3808 goto fill_holes;
3809 extent = btrfs_item_ptr(src, src_path->slots[0],
3810 struct btrfs_file_extent_item);
3811 if (btrfs_file_extent_type(src, extent) ==
3812 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003813 len = btrfs_file_extent_inline_len(src,
3814 src_path->slots[0],
3815 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003816 *last_extent = ALIGN(key.offset + len,
3817 log->sectorsize);
3818 } else {
3819 len = btrfs_file_extent_num_bytes(src, extent);
3820 *last_extent = key.offset + len;
3821 }
3822 }
3823fill_holes:
3824 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3825 * things could have happened
3826 *
3827 * 1) A merge could have happened, so we could currently be on a leaf
3828 * that holds what we were copying in the first place.
3829 * 2) A split could have happened, and now not all of the items we want
3830 * are on the same leaf.
3831 *
3832 * So we need to adjust how we search for holes, we need to drop the
3833 * path and re-search for the first extent key we found, and then walk
3834 * forward until we hit the last one we copied.
3835 */
3836 if (need_find_last_extent) {
3837 /* btrfs_prev_leaf could return 1 without releasing the path */
3838 btrfs_release_path(src_path);
3839 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3840 src_path, 0, 0);
3841 if (ret < 0)
3842 return ret;
3843 ASSERT(ret == 0);
3844 src = src_path->nodes[0];
3845 i = src_path->slots[0];
3846 } else {
3847 i = start_slot;
3848 }
3849
3850 /*
3851 * Ok so here we need to go through and fill in any holes we may have
3852 * to make sure that holes are punched for those areas in case they had
3853 * extents previously.
3854 */
3855 while (!done) {
3856 u64 offset, len;
3857 u64 extent_end;
3858
3859 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3860 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3861 if (ret < 0)
3862 return ret;
3863 ASSERT(ret == 0);
3864 src = src_path->nodes[0];
3865 i = 0;
Filipe Mananabee3c022018-03-26 23:59:12 +01003866 need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003867 }
3868
3869 btrfs_item_key_to_cpu(src, &key, i);
3870 if (!btrfs_comp_cpu_keys(&key, &last_key))
3871 done = true;
3872 if (key.objectid != btrfs_ino(inode) ||
3873 key.type != BTRFS_EXTENT_DATA_KEY) {
3874 i++;
3875 continue;
3876 }
3877 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3878 if (btrfs_file_extent_type(src, extent) ==
3879 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003880 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003881 extent_end = ALIGN(key.offset + len, log->sectorsize);
3882 } else {
3883 len = btrfs_file_extent_num_bytes(src, extent);
3884 extent_end = key.offset + len;
3885 }
3886 i++;
3887
3888 if (*last_extent == key.offset) {
3889 *last_extent = extent_end;
3890 continue;
3891 }
3892 offset = *last_extent;
3893 len = key.offset - *last_extent;
3894 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3895 offset, 0, 0, len, 0, len, 0,
3896 0, 0);
3897 if (ret)
3898 break;
Filipe Manana74121f72014-08-07 12:00:44 +01003899 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003900 }
3901 /*
3902 * Need to let the callers know we dropped the path so they should
3903 * re-search.
3904 */
3905 if (!ret && need_find_last_extent)
3906 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003907 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003908}
3909
Josef Bacik5dc562c2012-08-17 13:14:17 -04003910static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3911{
3912 struct extent_map *em1, *em2;
3913
3914 em1 = list_entry(a, struct extent_map, list);
3915 em2 = list_entry(b, struct extent_map, list);
3916
3917 if (em1->start < em2->start)
3918 return -1;
3919 else if (em1->start > em2->start)
3920 return 1;
3921 return 0;
3922}
3923
Filipe Manana8407f552014-09-05 15:14:39 +01003924static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3925 struct inode *inode,
3926 struct btrfs_root *root,
3927 const struct extent_map *em,
3928 const struct list_head *logged_list,
3929 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003930{
Josef Bacik2ab28f32012-10-12 15:27:49 -04003931 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01003932 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003933 u64 mod_start = em->mod_start;
3934 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003935 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003936 u64 csum_offset;
3937 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003938 LIST_HEAD(ordered_sums);
3939 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003940
Filipe Manana8407f552014-09-05 15:14:39 +01003941 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04003942
Filipe Manana8407f552014-09-05 15:14:39 +01003943 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3944 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003945 return 0;
3946
Josef Bacik2ab28f32012-10-12 15:27:49 -04003947 /*
Filipe Manana8407f552014-09-05 15:14:39 +01003948 * Wait far any ordered extent that covers our extent map. If it
3949 * finishes without an error, first check and see if our csums are on
3950 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04003951 */
Miao Xie827463c2014-01-14 20:31:51 +08003952 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003953 struct btrfs_ordered_sum *sum;
3954
3955 if (!mod_len)
3956 break;
3957
Josef Bacik2ab28f32012-10-12 15:27:49 -04003958 if (ordered->file_offset + ordered->len <= mod_start ||
3959 mod_start + mod_len <= ordered->file_offset)
3960 continue;
3961
Filipe Manana8407f552014-09-05 15:14:39 +01003962 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3963 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3964 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3965 const u64 start = ordered->file_offset;
3966 const u64 end = ordered->file_offset + ordered->len - 1;
3967
3968 WARN_ON(ordered->inode != inode);
3969 filemap_fdatawrite_range(inode->i_mapping, start, end);
3970 }
3971
3972 wait_event(ordered->wait,
3973 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3974 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3975
3976 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00003977 /*
3978 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3979 * i_mapping flags, so that the next fsync won't get
3980 * an outdated io error too.
3981 */
Miklos Szeredif0312212016-09-16 12:44:21 +02003982 filemap_check_errors(inode->i_mapping);
Filipe Manana8407f552014-09-05 15:14:39 +01003983 *ordered_io_error = true;
3984 break;
3985 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003986 /*
3987 * We are going to copy all the csums on this ordered extent, so
3988 * go ahead and adjust mod_start and mod_len in case this
3989 * ordered extent has already been logged.
3990 */
3991 if (ordered->file_offset > mod_start) {
3992 if (ordered->file_offset + ordered->len >=
3993 mod_start + mod_len)
3994 mod_len = ordered->file_offset - mod_start;
3995 /*
3996 * If we have this case
3997 *
3998 * |--------- logged extent ---------|
3999 * |----- ordered extent ----|
4000 *
4001 * Just don't mess with mod_start and mod_len, we'll
4002 * just end up logging more csums than we need and it
4003 * will be ok.
4004 */
4005 } else {
4006 if (ordered->file_offset + ordered->len <
4007 mod_start + mod_len) {
4008 mod_len = (mod_start + mod_len) -
4009 (ordered->file_offset + ordered->len);
4010 mod_start = ordered->file_offset +
4011 ordered->len;
4012 } else {
4013 mod_len = 0;
4014 }
4015 }
4016
Filipe Manana8407f552014-09-05 15:14:39 +01004017 if (skip_csum)
4018 continue;
4019
Josef Bacik2ab28f32012-10-12 15:27:49 -04004020 /*
4021 * To keep us from looping for the above case of an ordered
4022 * extent that falls inside of the logged extent.
4023 */
4024 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4025 &ordered->flags))
4026 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004027
Josef Bacik2ab28f32012-10-12 15:27:49 -04004028 list_for_each_entry(sum, &ordered->list, list) {
4029 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08004030 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01004031 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004032 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004033 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004034
Filipe Manana8407f552014-09-05 15:14:39 +01004035 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04004036 return ret;
4037
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004038 if (em->compress_type) {
4039 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004040 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004041 } else {
4042 csum_offset = mod_start - em->start;
4043 csum_len = mod_len;
4044 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004045
Josef Bacik70c8a912012-10-11 16:54:30 -04004046 /* block start is already adjusted for the file extent offset. */
4047 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
4048 em->block_start + csum_offset,
4049 em->block_start + csum_offset +
4050 csum_len - 1, &ordered_sums, 0);
4051 if (ret)
4052 return ret;
4053
4054 while (!list_empty(&ordered_sums)) {
4055 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4056 struct btrfs_ordered_sum,
4057 list);
4058 if (!ret)
4059 ret = btrfs_csum_file_blocks(trans, log, sums);
4060 list_del(&sums->list);
4061 kfree(sums);
4062 }
4063
4064 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004065}
4066
Filipe Manana8407f552014-09-05 15:14:39 +01004067static int log_one_extent(struct btrfs_trans_handle *trans,
4068 struct inode *inode, struct btrfs_root *root,
4069 const struct extent_map *em,
4070 struct btrfs_path *path,
4071 const struct list_head *logged_list,
4072 struct btrfs_log_ctx *ctx)
4073{
4074 struct btrfs_root *log = root->log_root;
4075 struct btrfs_file_extent_item *fi;
4076 struct extent_buffer *leaf;
4077 struct btrfs_map_token token;
4078 struct btrfs_key key;
4079 u64 extent_offset = em->start - em->orig_start;
4080 u64 block_len;
4081 int ret;
4082 int extent_inserted = 0;
4083 bool ordered_io_err = false;
4084
4085 ret = wait_ordered_extents(trans, inode, root, em, logged_list,
4086 &ordered_io_err);
4087 if (ret)
4088 return ret;
4089
4090 if (ordered_io_err) {
4091 ctx->io_err = -EIO;
4092 return 0;
4093 }
4094
4095 btrfs_init_map_token(&token);
4096
4097 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
4098 em->start + em->len, NULL, 0, 1,
4099 sizeof(*fi), &extent_inserted);
4100 if (ret)
4101 return ret;
4102
4103 if (!extent_inserted) {
4104 key.objectid = btrfs_ino(inode);
4105 key.type = BTRFS_EXTENT_DATA_KEY;
4106 key.offset = em->start;
4107
4108 ret = btrfs_insert_empty_item(trans, log, path, &key,
4109 sizeof(*fi));
4110 if (ret)
4111 return ret;
4112 }
4113 leaf = path->nodes[0];
4114 fi = btrfs_item_ptr(leaf, path->slots[0],
4115 struct btrfs_file_extent_item);
4116
Josef Bacik50d9aa92014-11-21 14:52:38 -05004117 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004118 &token);
4119 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4120 btrfs_set_token_file_extent_type(leaf, fi,
4121 BTRFS_FILE_EXTENT_PREALLOC,
4122 &token);
4123 else
4124 btrfs_set_token_file_extent_type(leaf, fi,
4125 BTRFS_FILE_EXTENT_REG,
4126 &token);
4127
4128 block_len = max(em->block_len, em->orig_block_len);
4129 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4130 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4131 em->block_start,
4132 &token);
4133 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4134 &token);
4135 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4136 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4137 em->block_start -
4138 extent_offset, &token);
4139 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4140 &token);
4141 } else {
4142 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4143 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4144 &token);
4145 }
4146
4147 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4148 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4149 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4150 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4151 &token);
4152 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4153 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4154 btrfs_mark_buffer_dirty(leaf);
4155
4156 btrfs_release_path(path);
4157
4158 return ret;
4159}
4160
Josef Bacik5dc562c2012-08-17 13:14:17 -04004161static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4162 struct btrfs_root *root,
4163 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004164 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004165 struct list_head *logged_list,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004166 struct btrfs_log_ctx *ctx,
4167 const u64 start,
4168 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004169{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004170 struct extent_map *em, *n;
4171 struct list_head extents;
4172 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4173 u64 test_gen;
4174 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004175 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004176
4177 INIT_LIST_HEAD(&extents);
4178
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004179 down_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004180 write_lock(&tree->lock);
4181 test_gen = root->fs_info->last_trans_committed;
4182
4183 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4184 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004185
4186 /*
4187 * Just an arbitrary number, this can be really CPU intensive
4188 * once we start getting a lot of extents, and really once we
4189 * have a bunch of extents we just want to commit since it will
4190 * be faster.
4191 */
4192 if (++num > 32768) {
4193 list_del_init(&tree->modified_extents);
4194 ret = -EFBIG;
4195 goto process;
4196 }
4197
Josef Bacik5dc562c2012-08-17 13:14:17 -04004198 if (em->generation <= test_gen)
4199 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004200 /* Need a ref to keep it from getting evicted from cache */
4201 atomic_inc(&em->refs);
4202 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004203 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004204 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004205 }
4206
4207 list_sort(NULL, &extents, extent_cmp);
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004208 btrfs_get_logged_extents(inode, logged_list, start, end);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004209 /*
4210 * Some ordered extents started by fsync might have completed
4211 * before we could collect them into the list logged_list, which
4212 * means they're gone, not in our logged_list nor in the inode's
4213 * ordered tree. We want the application/user space to know an
4214 * error happened while attempting to persist file data so that
4215 * it can take proper action. If such error happened, we leave
4216 * without writing to the log tree and the fsync must report the
4217 * file data write error and not commit the current transaction.
4218 */
Miklos Szeredif0312212016-09-16 12:44:21 +02004219 ret = filemap_check_errors(inode->i_mapping);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004220 if (ret)
4221 ctx->io_err = ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004222process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004223 while (!list_empty(&extents)) {
4224 em = list_entry(extents.next, struct extent_map, list);
4225
4226 list_del_init(&em->list);
4227
4228 /*
4229 * If we had an error we just need to delete everybody from our
4230 * private list.
4231 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004232 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004233 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004234 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004235 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004236 }
4237
4238 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004239
Filipe Manana8407f552014-09-05 15:14:39 +01004240 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4241 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004242 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004243 clear_em_logging(tree, em);
4244 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004245 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004246 WARN_ON(!list_empty(&extents));
4247 write_unlock(&tree->lock);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004248 up_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004249
Josef Bacik5dc562c2012-08-17 13:14:17 -04004250 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004251 return ret;
4252}
4253
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004254static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
4255 struct btrfs_path *path, u64 *size_ret)
4256{
4257 struct btrfs_key key;
4258 int ret;
4259
4260 key.objectid = btrfs_ino(inode);
4261 key.type = BTRFS_INODE_ITEM_KEY;
4262 key.offset = 0;
4263
4264 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4265 if (ret < 0) {
4266 return ret;
4267 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004268 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004269 } else {
4270 struct btrfs_inode_item *item;
4271
4272 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4273 struct btrfs_inode_item);
4274 *size_ret = btrfs_inode_size(path->nodes[0], item);
4275 }
4276
4277 btrfs_release_path(path);
4278 return 0;
4279}
4280
Filipe Manana36283bf2015-06-20 00:44:51 +01004281/*
4282 * At the moment we always log all xattrs. This is to figure out at log replay
4283 * time which xattrs must have their deletion replayed. If a xattr is missing
4284 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4285 * because if a xattr is deleted, the inode is fsynced and a power failure
4286 * happens, causing the log to be replayed the next time the fs is mounted,
4287 * we want the xattr to not exist anymore (same behaviour as other filesystems
4288 * with a journal, ext3/4, xfs, f2fs, etc).
4289 */
4290static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4291 struct btrfs_root *root,
4292 struct inode *inode,
4293 struct btrfs_path *path,
4294 struct btrfs_path *dst_path)
4295{
4296 int ret;
4297 struct btrfs_key key;
4298 const u64 ino = btrfs_ino(inode);
4299 int ins_nr = 0;
4300 int start_slot = 0;
4301
4302 key.objectid = ino;
4303 key.type = BTRFS_XATTR_ITEM_KEY;
4304 key.offset = 0;
4305
4306 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4307 if (ret < 0)
4308 return ret;
4309
4310 while (true) {
4311 int slot = path->slots[0];
4312 struct extent_buffer *leaf = path->nodes[0];
4313 int nritems = btrfs_header_nritems(leaf);
4314
4315 if (slot >= nritems) {
4316 if (ins_nr > 0) {
4317 u64 last_extent = 0;
4318
4319 ret = copy_items(trans, inode, dst_path, path,
4320 &last_extent, start_slot,
4321 ins_nr, 1, 0);
4322 /* can't be 1, extent items aren't processed */
4323 ASSERT(ret <= 0);
4324 if (ret < 0)
4325 return ret;
4326 ins_nr = 0;
4327 }
4328 ret = btrfs_next_leaf(root, path);
4329 if (ret < 0)
4330 return ret;
4331 else if (ret > 0)
4332 break;
4333 continue;
4334 }
4335
4336 btrfs_item_key_to_cpu(leaf, &key, slot);
4337 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4338 break;
4339
4340 if (ins_nr == 0)
4341 start_slot = slot;
4342 ins_nr++;
4343 path->slots[0]++;
4344 cond_resched();
4345 }
4346 if (ins_nr > 0) {
4347 u64 last_extent = 0;
4348
4349 ret = copy_items(trans, inode, dst_path, path,
4350 &last_extent, start_slot,
4351 ins_nr, 1, 0);
4352 /* can't be 1, extent items aren't processed */
4353 ASSERT(ret <= 0);
4354 if (ret < 0)
4355 return ret;
4356 }
4357
4358 return 0;
4359}
4360
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004361/*
4362 * If the no holes feature is enabled we need to make sure any hole between the
4363 * last extent and the i_size of our inode is explicitly marked in the log. This
4364 * is to make sure that doing something like:
4365 *
4366 * 1) create file with 128Kb of data
4367 * 2) truncate file to 64Kb
4368 * 3) truncate file to 256Kb
4369 * 4) fsync file
4370 * 5) <crash/power failure>
4371 * 6) mount fs and trigger log replay
4372 *
4373 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4374 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4375 * file correspond to a hole. The presence of explicit holes in a log tree is
4376 * what guarantees that log replay will remove/adjust file extent items in the
4377 * fs/subvol tree.
4378 *
4379 * Here we do not need to care about holes between extents, that is already done
4380 * by copy_items(). We also only need to do this in the full sync path, where we
4381 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4382 * lookup the list of modified extent maps and if any represents a hole, we
4383 * insert a corresponding extent representing a hole in the log tree.
4384 */
4385static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4386 struct btrfs_root *root,
4387 struct inode *inode,
4388 struct btrfs_path *path)
4389{
4390 int ret;
4391 struct btrfs_key key;
4392 u64 hole_start;
4393 u64 hole_size;
4394 struct extent_buffer *leaf;
4395 struct btrfs_root *log = root->log_root;
4396 const u64 ino = btrfs_ino(inode);
4397 const u64 i_size = i_size_read(inode);
4398
4399 if (!btrfs_fs_incompat(root->fs_info, NO_HOLES))
4400 return 0;
4401
4402 key.objectid = ino;
4403 key.type = BTRFS_EXTENT_DATA_KEY;
4404 key.offset = (u64)-1;
4405
4406 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4407 ASSERT(ret != 0);
4408 if (ret < 0)
4409 return ret;
4410
4411 ASSERT(path->slots[0] > 0);
4412 path->slots[0]--;
4413 leaf = path->nodes[0];
4414 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4415
4416 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4417 /* inode does not have any extents */
4418 hole_start = 0;
4419 hole_size = i_size;
4420 } else {
4421 struct btrfs_file_extent_item *extent;
4422 u64 len;
4423
4424 /*
4425 * If there's an extent beyond i_size, an explicit hole was
4426 * already inserted by copy_items().
4427 */
4428 if (key.offset >= i_size)
4429 return 0;
4430
4431 extent = btrfs_item_ptr(leaf, path->slots[0],
4432 struct btrfs_file_extent_item);
4433
4434 if (btrfs_file_extent_type(leaf, extent) ==
4435 BTRFS_FILE_EXTENT_INLINE) {
4436 len = btrfs_file_extent_inline_len(leaf,
4437 path->slots[0],
4438 extent);
4439 ASSERT(len == i_size);
4440 return 0;
4441 }
4442
4443 len = btrfs_file_extent_num_bytes(leaf, extent);
4444 /* Last extent goes beyond i_size, no need to log a hole. */
4445 if (key.offset + len > i_size)
4446 return 0;
4447 hole_start = key.offset + len;
4448 hole_size = i_size - hole_start;
4449 }
4450 btrfs_release_path(path);
4451
4452 /* Last extent ends at i_size. */
4453 if (hole_size == 0)
4454 return 0;
4455
4456 hole_size = ALIGN(hole_size, root->sectorsize);
4457 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4458 hole_size, 0, hole_size, 0, 0, 0);
4459 return ret;
4460}
4461
Filipe Manana56f23fd2016-03-30 23:37:21 +01004462/*
4463 * When we are logging a new inode X, check if it doesn't have a reference that
4464 * matches the reference from some other inode Y created in a past transaction
4465 * and that was renamed in the current transaction. If we don't do this, then at
4466 * log replay time we can lose inode Y (and all its files if it's a directory):
4467 *
4468 * mkdir /mnt/x
4469 * echo "hello world" > /mnt/x/foobar
4470 * sync
4471 * mv /mnt/x /mnt/y
4472 * mkdir /mnt/x # or touch /mnt/x
4473 * xfs_io -c fsync /mnt/x
4474 * <power fail>
4475 * mount fs, trigger log replay
4476 *
4477 * After the log replay procedure, we would lose the first directory and all its
4478 * files (file foobar).
4479 * For the case where inode Y is not a directory we simply end up losing it:
4480 *
4481 * echo "123" > /mnt/foo
4482 * sync
4483 * mv /mnt/foo /mnt/bar
4484 * echo "abc" > /mnt/foo
4485 * xfs_io -c fsync /mnt/foo
4486 * <power fail>
4487 *
4488 * We also need this for cases where a snapshot entry is replaced by some other
4489 * entry (file or directory) otherwise we end up with an unreplayable log due to
4490 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4491 * if it were a regular entry:
4492 *
4493 * mkdir /mnt/x
4494 * btrfs subvolume snapshot /mnt /mnt/x/snap
4495 * btrfs subvolume delete /mnt/x/snap
4496 * rmdir /mnt/x
4497 * mkdir /mnt/x
4498 * fsync /mnt/x or fsync some new file inside it
4499 * <power fail>
4500 *
4501 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4502 * the same transaction.
4503 */
4504static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4505 const int slot,
4506 const struct btrfs_key *key,
Filipe Manana44f714d2016-06-06 16:11:13 +01004507 struct inode *inode,
4508 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004509{
4510 int ret;
4511 struct btrfs_path *search_path;
4512 char *name = NULL;
4513 u32 name_len = 0;
4514 u32 item_size = btrfs_item_size_nr(eb, slot);
4515 u32 cur_offset = 0;
4516 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4517
4518 search_path = btrfs_alloc_path();
4519 if (!search_path)
4520 return -ENOMEM;
4521 search_path->search_commit_root = 1;
4522 search_path->skip_locking = 1;
4523
4524 while (cur_offset < item_size) {
4525 u64 parent;
4526 u32 this_name_len;
4527 u32 this_len;
4528 unsigned long name_ptr;
4529 struct btrfs_dir_item *di;
4530
4531 if (key->type == BTRFS_INODE_REF_KEY) {
4532 struct btrfs_inode_ref *iref;
4533
4534 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4535 parent = key->offset;
4536 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4537 name_ptr = (unsigned long)(iref + 1);
4538 this_len = sizeof(*iref) + this_name_len;
4539 } else {
4540 struct btrfs_inode_extref *extref;
4541
4542 extref = (struct btrfs_inode_extref *)(ptr +
4543 cur_offset);
4544 parent = btrfs_inode_extref_parent(eb, extref);
4545 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4546 name_ptr = (unsigned long)&extref->name;
4547 this_len = sizeof(*extref) + this_name_len;
4548 }
4549
4550 if (this_name_len > name_len) {
4551 char *new_name;
4552
4553 new_name = krealloc(name, this_name_len, GFP_NOFS);
4554 if (!new_name) {
4555 ret = -ENOMEM;
4556 goto out;
4557 }
4558 name_len = this_name_len;
4559 name = new_name;
4560 }
4561
4562 read_extent_buffer(eb, name, name_ptr, this_name_len);
4563 di = btrfs_lookup_dir_item(NULL, BTRFS_I(inode)->root,
4564 search_path, parent,
4565 name, this_name_len, 0);
4566 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004567 struct btrfs_key di_key;
4568
4569 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4570 di, &di_key);
4571 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4572 ret = 1;
4573 *other_ino = di_key.objectid;
4574 } else {
4575 ret = -EAGAIN;
4576 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004577 goto out;
4578 } else if (IS_ERR(di)) {
4579 ret = PTR_ERR(di);
4580 goto out;
4581 }
4582 btrfs_release_path(search_path);
4583
4584 cur_offset += this_len;
4585 }
4586 ret = 0;
4587out:
4588 btrfs_free_path(search_path);
4589 kfree(name);
4590 return ret;
4591}
4592
Chris Masone02119d2008-09-05 16:13:11 -04004593/* log a single inode in the tree log.
4594 * At least one parent directory for this inode must exist in the tree
4595 * or be logged already.
4596 *
4597 * Any items from this inode changed by the current transaction are copied
4598 * to the log tree. An extra reference is taken on any extents in this
4599 * file, allowing us to avoid a whole pile of corner cases around logging
4600 * blocks that have been removed from the tree.
4601 *
4602 * See LOG_INODE_ALL and related defines for a description of what inode_only
4603 * does.
4604 *
4605 * This handles both files and directories.
4606 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004607static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004608 struct btrfs_root *root, struct inode *inode,
4609 int inode_only,
4610 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004611 const loff_t end,
4612 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004613{
4614 struct btrfs_path *path;
4615 struct btrfs_path *dst_path;
4616 struct btrfs_key min_key;
4617 struct btrfs_key max_key;
4618 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004619 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08004620 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004621 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004622 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004623 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004624 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004625 int ins_start_slot = 0;
4626 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004627 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08004628 u64 ino = btrfs_ino(inode);
Filipe Manana49dae1b2014-09-06 22:34:39 +01004629 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004630 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004631 bool need_log_inode_item = true;
Filipe Manana92291242018-05-11 16:42:42 +01004632 bool xattrs_logged = false;
Chris Masone02119d2008-09-05 16:13:11 -04004633
Chris Masone02119d2008-09-05 16:13:11 -04004634 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004635 if (!path)
4636 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004637 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004638 if (!dst_path) {
4639 btrfs_free_path(path);
4640 return -ENOMEM;
4641 }
Chris Masone02119d2008-09-05 16:13:11 -04004642
Li Zefan33345d012011-04-20 10:31:50 +08004643 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004644 min_key.type = BTRFS_INODE_ITEM_KEY;
4645 min_key.offset = 0;
4646
Li Zefan33345d012011-04-20 10:31:50 +08004647 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004648
Chris Mason12fcfd22009-03-24 10:24:20 -04004649
Josef Bacik5dc562c2012-08-17 13:14:17 -04004650 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00004651 if (S_ISDIR(inode->i_mode) ||
4652 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4653 &BTRFS_I(inode)->runtime_flags) &&
Liu Bo67312122016-11-30 16:20:25 -08004654 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004655 max_key.type = BTRFS_XATTR_ITEM_KEY;
4656 else
4657 max_key.type = (u8)-1;
4658 max_key.offset = (u64)-1;
4659
Filipe Manana2c2c4522015-01-13 16:40:04 +00004660 /*
4661 * Only run delayed items if we are a dir or a new file.
4662 * Otherwise commit the delayed inode only, which is needed in
4663 * order for the log replay code to mark inodes for link count
4664 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4665 */
Josef Bacik94edf4a2012-09-25 14:56:25 -04004666 if (S_ISDIR(inode->i_mode) ||
Filipe Manana2c2c4522015-01-13 16:40:04 +00004667 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
Josef Bacik94edf4a2012-09-25 14:56:25 -04004668 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004669 else
4670 ret = btrfs_commit_inode_delayed_inode(inode);
4671
4672 if (ret) {
4673 btrfs_free_path(path);
4674 btrfs_free_path(dst_path);
4675 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004676 }
4677
Liu Bo67312122016-11-30 16:20:25 -08004678 if (inode_only == LOG_OTHER_INODE) {
4679 inode_only = LOG_INODE_EXISTS;
4680 mutex_lock_nested(&BTRFS_I(inode)->log_mutex,
4681 SINGLE_DEPTH_NESTING);
4682 } else {
4683 mutex_lock(&BTRFS_I(inode)->log_mutex);
4684 }
Chris Masone02119d2008-09-05 16:13:11 -04004685
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004686 /*
Chris Masone02119d2008-09-05 16:13:11 -04004687 * a brute force approach to making sure we get the most uptodate
4688 * copies of everything.
4689 */
4690 if (S_ISDIR(inode->i_mode)) {
4691 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4692
Filipe Manana4f764e52015-02-23 19:53:35 +00004693 if (inode_only == LOG_INODE_EXISTS)
4694 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004695 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004696 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004697 if (inode_only == LOG_INODE_EXISTS) {
4698 /*
4699 * Make sure the new inode item we write to the log has
4700 * the same isize as the current one (if it exists).
4701 * This is necessary to prevent data loss after log
4702 * replay, and also to prevent doing a wrong expanding
4703 * truncate - for e.g. create file, write 4K into offset
4704 * 0, fsync, write 4K into offset 4096, add hard link,
4705 * fsync some other file (to sync log), power fail - if
4706 * we use the inode's current i_size, after log replay
4707 * we get a 8Kb file, with the last 4Kb extent as a hole
4708 * (zeroes), as if an expanding truncate happened,
4709 * instead of getting a file of 4Kb only.
4710 */
4711 err = logged_inode_size(log, inode, path,
4712 &logged_isize);
4713 if (err)
4714 goto out_unlock;
4715 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004716 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4717 &BTRFS_I(inode)->runtime_flags)) {
4718 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004719 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004720 ret = drop_objectid_items(trans, log, path, ino,
4721 max_key.type);
4722 } else {
4723 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4724 &BTRFS_I(inode)->runtime_flags);
4725 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4726 &BTRFS_I(inode)->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004727 while(1) {
4728 ret = btrfs_truncate_inode_items(trans,
4729 log, inode, 0, 0);
4730 if (ret != -EAGAIN)
4731 break;
4732 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004733 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004734 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4735 &BTRFS_I(inode)->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004736 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004737 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004738 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004739 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004740 ret = drop_objectid_items(trans, log, path, ino,
4741 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004742 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004743 if (inode_only == LOG_INODE_ALL)
4744 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004745 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004746 }
Josef Bacika95249b2012-10-11 16:17:34 -04004747
Chris Masone02119d2008-09-05 16:13:11 -04004748 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004749 if (ret) {
4750 err = ret;
4751 goto out_unlock;
4752 }
Chris Masone02119d2008-09-05 16:13:11 -04004753
Chris Masond3977122009-01-05 21:25:51 -05004754 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004755 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004756 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004757 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004758 if (ret < 0) {
4759 err = ret;
4760 goto out_unlock;
4761 }
Chris Masone02119d2008-09-05 16:13:11 -04004762 if (ret != 0)
4763 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004764again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004765 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004766 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004767 break;
4768 if (min_key.type > max_key.type)
4769 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004770
Filipe Mananae4545de2015-06-17 12:49:23 +01004771 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4772 need_log_inode_item = false;
4773
Filipe Manana56f23fd2016-03-30 23:37:21 +01004774 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4775 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
4776 BTRFS_I(inode)->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004777 u64 other_ino = 0;
4778
Filipe Manana56f23fd2016-03-30 23:37:21 +01004779 ret = btrfs_check_ref_name_override(path->nodes[0],
4780 path->slots[0],
Filipe Manana44f714d2016-06-06 16:11:13 +01004781 &min_key, inode,
4782 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004783 if (ret < 0) {
4784 err = ret;
4785 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004786 } else if (ret > 0 && ctx &&
4787 other_ino != btrfs_ino(ctx->inode)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004788 struct btrfs_key inode_key;
4789 struct inode *other_inode;
4790
4791 if (ins_nr > 0) {
4792 ins_nr++;
4793 } else {
4794 ins_nr = 1;
4795 ins_start_slot = path->slots[0];
4796 }
4797 ret = copy_items(trans, inode, dst_path, path,
4798 &last_extent, ins_start_slot,
4799 ins_nr, inode_only,
4800 logged_isize);
4801 if (ret < 0) {
4802 err = ret;
4803 goto out_unlock;
4804 }
4805 ins_nr = 0;
4806 btrfs_release_path(path);
4807 inode_key.objectid = other_ino;
4808 inode_key.type = BTRFS_INODE_ITEM_KEY;
4809 inode_key.offset = 0;
4810 other_inode = btrfs_iget(root->fs_info->sb,
4811 &inode_key, root,
4812 NULL);
4813 /*
4814 * If the other inode that had a conflicting dir
4815 * entry was deleted in the current transaction,
4816 * we don't need to do more work nor fallback to
4817 * a transaction commit.
4818 */
4819 if (IS_ERR(other_inode) &&
4820 PTR_ERR(other_inode) == -ENOENT) {
4821 goto next_key;
4822 } else if (IS_ERR(other_inode)) {
4823 err = PTR_ERR(other_inode);
4824 goto out_unlock;
4825 }
4826 /*
4827 * We are safe logging the other inode without
4828 * acquiring its i_mutex as long as we log with
4829 * the LOG_INODE_EXISTS mode. We're safe against
4830 * concurrent renames of the other inode as well
4831 * because during a rename we pin the log and
4832 * update the log with the new name before we
4833 * unpin it.
4834 */
4835 err = btrfs_log_inode(trans, root, other_inode,
Liu Bo67312122016-11-30 16:20:25 -08004836 LOG_OTHER_INODE,
Filipe Manana44f714d2016-06-06 16:11:13 +01004837 0, LLONG_MAX, ctx);
4838 iput(other_inode);
4839 if (err)
4840 goto out_unlock;
4841 else
4842 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01004843 }
4844 }
4845
Filipe Manana36283bf2015-06-20 00:44:51 +01004846 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
4847 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
4848 if (ins_nr == 0)
4849 goto next_slot;
4850 ret = copy_items(trans, inode, dst_path, path,
4851 &last_extent, ins_start_slot,
4852 ins_nr, inode_only, logged_isize);
4853 if (ret < 0) {
4854 err = ret;
4855 goto out_unlock;
4856 }
4857 ins_nr = 0;
4858 if (ret) {
4859 btrfs_release_path(path);
4860 continue;
4861 }
4862 goto next_slot;
4863 }
4864
Chris Masone02119d2008-09-05 16:13:11 -04004865 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004866 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4867 ins_nr++;
4868 goto next_slot;
4869 } else if (!ins_nr) {
4870 ins_start_slot = path->slots[0];
4871 ins_nr = 1;
4872 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004873 }
4874
Josef Bacik16e75492013-10-22 12:18:51 -04004875 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004876 ins_start_slot, ins_nr, inode_only,
4877 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004878 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004879 err = ret;
4880 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02004881 }
4882 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04004883 ins_nr = 0;
4884 btrfs_release_path(path);
4885 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004886 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004887 ins_nr = 1;
4888 ins_start_slot = path->slots[0];
4889next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004890
Chris Mason3a5f1d42008-09-11 15:53:37 -04004891 nritems = btrfs_header_nritems(path->nodes[0]);
4892 path->slots[0]++;
4893 if (path->slots[0] < nritems) {
4894 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4895 path->slots[0]);
4896 goto again;
4897 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004898 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004899 ret = copy_items(trans, inode, dst_path, path,
4900 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004901 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004902 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004903 err = ret;
4904 goto out_unlock;
4905 }
Josef Bacik16e75492013-10-22 12:18:51 -04004906 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004907 ins_nr = 0;
4908 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004909 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01004910next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004911 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004912 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004913 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004914 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004915 min_key.offset = 0;
4916 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004917 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004918 }
Chris Masone02119d2008-09-05 16:13:11 -04004919 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004920 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004921 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004922 ins_start_slot, ins_nr, inode_only,
4923 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004924 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004925 err = ret;
4926 goto out_unlock;
4927 }
Josef Bacik16e75492013-10-22 12:18:51 -04004928 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004929 ins_nr = 0;
4930 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004931
Filipe Manana36283bf2015-06-20 00:44:51 +01004932 btrfs_release_path(path);
4933 btrfs_release_path(dst_path);
4934 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
4935 if (err)
4936 goto out_unlock;
Filipe Manana92291242018-05-11 16:42:42 +01004937 xattrs_logged = true;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004938 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
4939 btrfs_release_path(path);
4940 btrfs_release_path(dst_path);
4941 err = btrfs_log_trailing_hole(trans, root, inode, path);
4942 if (err)
4943 goto out_unlock;
4944 }
Josef Bacika95249b2012-10-11 16:17:34 -04004945log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004946 btrfs_release_path(path);
4947 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01004948 if (need_log_inode_item) {
4949 err = log_inode_item(trans, log, dst_path, inode);
Filipe Manana92291242018-05-11 16:42:42 +01004950 if (!err && !xattrs_logged) {
4951 err = btrfs_log_all_xattrs(trans, root, inode, path,
4952 dst_path);
4953 btrfs_release_path(path);
4954 }
Filipe Mananae4545de2015-06-17 12:49:23 +01004955 if (err)
4956 goto out_unlock;
4957 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004958 if (fast_search) {
Miao Xie827463c2014-01-14 20:31:51 +08004959 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004960 &logged_list, ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004961 if (ret) {
4962 err = ret;
4963 goto out_unlock;
4964 }
Josef Bacikd006a042013-11-12 20:54:09 -05004965 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004966 struct extent_map *em, *n;
4967
Filipe Manana49dae1b2014-09-06 22:34:39 +01004968 write_lock(&em_tree->lock);
4969 /*
4970 * We can't just remove every em if we're called for a ranged
4971 * fsync - that is, one that doesn't cover the whole possible
4972 * file range (0 to LLONG_MAX). This is because we can have
4973 * em's that fall outside the range we're logging and therefore
4974 * their ordered operations haven't completed yet
4975 * (btrfs_finish_ordered_io() not invoked yet). This means we
4976 * didn't get their respective file extent item in the fs/subvol
4977 * tree yet, and need to let the next fast fsync (one which
4978 * consults the list of modified extent maps) find the em so
4979 * that it logs a matching file extent item and waits for the
4980 * respective ordered operation to complete (if it's still
4981 * running).
4982 *
4983 * Removing every em outside the range we're logging would make
4984 * the next fast fsync not log their matching file extent items,
4985 * therefore making us lose data after a log replay.
4986 */
4987 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4988 list) {
4989 const u64 mod_end = em->mod_start + em->mod_len - 1;
4990
4991 if (em->mod_start >= start && mod_end <= end)
4992 list_del_init(&em->list);
4993 }
4994 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004995 }
4996
Chris Mason9623f9a2008-09-11 17:42:42 -04004997 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004998 ret = log_directory_changes(trans, root, inode, path, dst_path,
4999 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005000 if (ret) {
5001 err = ret;
5002 goto out_unlock;
5003 }
Chris Masone02119d2008-09-05 16:13:11 -04005004 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01005005
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005006 spin_lock(&BTRFS_I(inode)->lock);
Filipe Manana125c4cf92014-09-11 21:22:14 +01005007 BTRFS_I(inode)->logged_trans = trans->transid;
5008 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005009 spin_unlock(&BTRFS_I(inode)->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005010out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08005011 if (unlikely(err))
5012 btrfs_put_logged_extents(&logged_list);
5013 else
5014 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04005015 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5016
5017 btrfs_free_path(path);
5018 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005019 return err;
Chris Masone02119d2008-09-05 16:13:11 -04005020}
5021
Chris Mason12fcfd22009-03-24 10:24:20 -04005022/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005023 * Check if we must fallback to a transaction commit when logging an inode.
5024 * This must be called after logging the inode and is used only in the context
5025 * when fsyncing an inode requires the need to log some other inode - in which
5026 * case we can't lock the i_mutex of each other inode we need to log as that
5027 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5028 * log inodes up or down in the hierarchy) or rename operations for example. So
5029 * we take the log_mutex of the inode after we have logged it and then check for
5030 * its last_unlink_trans value - this is safe because any task setting
5031 * last_unlink_trans must take the log_mutex and it must do this before it does
5032 * the actual unlink operation, so if we do this check before a concurrent task
5033 * sets last_unlink_trans it means we've logged a consistent version/state of
5034 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005035 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005036 * we logged the inode or it might have also done the unlink).
5037 */
5038static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5039 struct inode *inode)
5040{
5041 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
5042 bool ret = false;
5043
5044 mutex_lock(&BTRFS_I(inode)->log_mutex);
5045 if (BTRFS_I(inode)->last_unlink_trans > fs_info->last_trans_committed) {
5046 /*
5047 * Make sure any commits to the log are forced to be full
5048 * commits.
5049 */
5050 btrfs_set_log_full_commit(fs_info, trans);
5051 ret = true;
5052 }
5053 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5054
5055 return ret;
5056}
5057
5058/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005059 * follow the dentry parent pointers up the chain and see if any
5060 * of the directories in it require a full commit before they can
5061 * be logged. Returns zero if nothing special needs to be done or 1 if
5062 * a full commit is required.
5063 */
5064static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5065 struct inode *inode,
5066 struct dentry *parent,
5067 struct super_block *sb,
5068 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005069{
Chris Mason12fcfd22009-03-24 10:24:20 -04005070 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005071 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04005072 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005073
Chris Masonaf4176b2009-03-24 10:24:31 -04005074 /*
5075 * for regular files, if its inode is already on disk, we don't
5076 * have to worry about the parents at all. This is because
5077 * we can use the last_unlink_trans field to record renames
5078 * and other fun in this file.
5079 */
5080 if (S_ISREG(inode->i_mode) &&
5081 BTRFS_I(inode)->generation <= last_committed &&
5082 BTRFS_I(inode)->last_unlink_trans <= last_committed)
5083 goto out;
5084
Chris Mason12fcfd22009-03-24 10:24:20 -04005085 if (!S_ISDIR(inode->i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005086 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005087 goto out;
David Howells2b0143b2015-03-17 22:25:59 +00005088 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005089 }
5090
5091 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005092 /*
5093 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005094 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005095 * logged_trans so that further down in the log code we don't
5096 * think this inode has already been logged.
5097 */
5098 if (inode != orig_inode)
5099 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005100 smp_mb();
5101
Filipe Manana2be63d52016-02-12 11:34:23 +00005102 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005103 ret = 1;
5104 break;
5105 }
5106
Al Virofc640052016-04-10 01:33:30 -04005107 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005108 break;
5109
Filipe Manana44f714d2016-06-06 16:11:13 +01005110 if (IS_ROOT(parent)) {
5111 inode = d_inode(parent);
5112 if (btrfs_must_commit_transaction(trans, inode))
5113 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005114 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005115 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005116
Josef Bacik6a912212010-11-20 09:48:00 +00005117 parent = dget_parent(parent);
5118 dput(old_parent);
5119 old_parent = parent;
David Howells2b0143b2015-03-17 22:25:59 +00005120 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005121
5122 }
Josef Bacik6a912212010-11-20 09:48:00 +00005123 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005124out:
Chris Masone02119d2008-09-05 16:13:11 -04005125 return ret;
5126}
5127
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005128struct btrfs_dir_list {
5129 u64 ino;
5130 struct list_head list;
5131};
5132
5133/*
5134 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5135 * details about the why it is needed.
5136 * This is a recursive operation - if an existing dentry corresponds to a
5137 * directory, that directory's new entries are logged too (same behaviour as
5138 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5139 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5140 * complains about the following circular lock dependency / possible deadlock:
5141 *
5142 * CPU0 CPU1
5143 * ---- ----
5144 * lock(&type->i_mutex_dir_key#3/2);
5145 * lock(sb_internal#2);
5146 * lock(&type->i_mutex_dir_key#3/2);
5147 * lock(&sb->s_type->i_mutex_key#14);
5148 *
5149 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5150 * sb_start_intwrite() in btrfs_start_transaction().
5151 * Not locking i_mutex of the inodes is still safe because:
5152 *
5153 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5154 * that while logging the inode new references (names) are added or removed
5155 * from the inode, leaving the logged inode item with a link count that does
5156 * not match the number of logged inode reference items. This is fine because
5157 * at log replay time we compute the real number of links and correct the
5158 * link count in the inode item (see replay_one_buffer() and
5159 * link_to_fixup_dir());
5160 *
5161 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5162 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5163 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5164 * has a size that doesn't match the sum of the lengths of all the logged
5165 * names. This does not result in a problem because if a dir_item key is
5166 * logged but its matching dir_index key is not logged, at log replay time we
5167 * don't use it to replay the respective name (see replay_one_name()). On the
5168 * other hand if only the dir_index key ends up being logged, the respective
5169 * name is added to the fs/subvol tree with both the dir_item and dir_index
5170 * keys created (see replay_one_name()).
5171 * The directory's inode item with a wrong i_size is not a problem as well,
5172 * since we don't use it at log replay time to set the i_size in the inode
5173 * item of the fs/subvol tree (see overwrite_item()).
5174 */
5175static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5176 struct btrfs_root *root,
5177 struct inode *start_inode,
5178 struct btrfs_log_ctx *ctx)
5179{
5180 struct btrfs_root *log = root->log_root;
5181 struct btrfs_path *path;
5182 LIST_HEAD(dir_list);
5183 struct btrfs_dir_list *dir_elem;
5184 int ret = 0;
5185
5186 path = btrfs_alloc_path();
5187 if (!path)
5188 return -ENOMEM;
5189
5190 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5191 if (!dir_elem) {
5192 btrfs_free_path(path);
5193 return -ENOMEM;
5194 }
5195 dir_elem->ino = btrfs_ino(start_inode);
5196 list_add_tail(&dir_elem->list, &dir_list);
5197
5198 while (!list_empty(&dir_list)) {
5199 struct extent_buffer *leaf;
5200 struct btrfs_key min_key;
5201 int nritems;
5202 int i;
5203
5204 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5205 list);
5206 if (ret)
5207 goto next_dir_inode;
5208
5209 min_key.objectid = dir_elem->ino;
5210 min_key.type = BTRFS_DIR_ITEM_KEY;
5211 min_key.offset = 0;
5212again:
5213 btrfs_release_path(path);
5214 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5215 if (ret < 0) {
5216 goto next_dir_inode;
5217 } else if (ret > 0) {
5218 ret = 0;
5219 goto next_dir_inode;
5220 }
5221
5222process_leaf:
5223 leaf = path->nodes[0];
5224 nritems = btrfs_header_nritems(leaf);
5225 for (i = path->slots[0]; i < nritems; i++) {
5226 struct btrfs_dir_item *di;
5227 struct btrfs_key di_key;
5228 struct inode *di_inode;
5229 struct btrfs_dir_list *new_dir_elem;
5230 int log_mode = LOG_INODE_EXISTS;
5231 int type;
5232
5233 btrfs_item_key_to_cpu(leaf, &min_key, i);
5234 if (min_key.objectid != dir_elem->ino ||
5235 min_key.type != BTRFS_DIR_ITEM_KEY)
5236 goto next_dir_inode;
5237
5238 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5239 type = btrfs_dir_type(leaf, di);
5240 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5241 type != BTRFS_FT_DIR)
5242 continue;
5243 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5244 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5245 continue;
5246
Robbie Ko664b0532016-10-28 10:48:26 +08005247 btrfs_release_path(path);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005248 di_inode = btrfs_iget(root->fs_info->sb, &di_key,
5249 root, NULL);
5250 if (IS_ERR(di_inode)) {
5251 ret = PTR_ERR(di_inode);
5252 goto next_dir_inode;
5253 }
5254
5255 if (btrfs_inode_in_log(di_inode, trans->transid)) {
5256 iput(di_inode);
Robbie Ko664b0532016-10-28 10:48:26 +08005257 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005258 }
5259
5260 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005261 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005262 log_mode = LOG_INODE_ALL;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005263 ret = btrfs_log_inode(trans, root, di_inode,
5264 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005265 if (!ret &&
5266 btrfs_must_commit_transaction(trans, di_inode))
5267 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005268 iput(di_inode);
5269 if (ret)
5270 goto next_dir_inode;
5271 if (ctx->log_new_dentries) {
5272 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5273 GFP_NOFS);
5274 if (!new_dir_elem) {
5275 ret = -ENOMEM;
5276 goto next_dir_inode;
5277 }
5278 new_dir_elem->ino = di_key.objectid;
5279 list_add_tail(&new_dir_elem->list, &dir_list);
5280 }
5281 break;
5282 }
5283 if (i == nritems) {
5284 ret = btrfs_next_leaf(log, path);
5285 if (ret < 0) {
5286 goto next_dir_inode;
5287 } else if (ret > 0) {
5288 ret = 0;
5289 goto next_dir_inode;
5290 }
5291 goto process_leaf;
5292 }
5293 if (min_key.offset < (u64)-1) {
5294 min_key.offset++;
5295 goto again;
5296 }
5297next_dir_inode:
5298 list_del(&dir_elem->list);
5299 kfree(dir_elem);
5300 }
5301
5302 btrfs_free_path(path);
5303 return ret;
5304}
5305
Filipe Manana18aa0922015-08-05 16:49:08 +01005306static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5307 struct inode *inode,
5308 struct btrfs_log_ctx *ctx)
5309{
5310 int ret;
5311 struct btrfs_path *path;
5312 struct btrfs_key key;
5313 struct btrfs_root *root = BTRFS_I(inode)->root;
5314 const u64 ino = btrfs_ino(inode);
5315
5316 path = btrfs_alloc_path();
5317 if (!path)
5318 return -ENOMEM;
5319 path->skip_locking = 1;
5320 path->search_commit_root = 1;
5321
5322 key.objectid = ino;
5323 key.type = BTRFS_INODE_REF_KEY;
5324 key.offset = 0;
5325 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5326 if (ret < 0)
5327 goto out;
5328
5329 while (true) {
5330 struct extent_buffer *leaf = path->nodes[0];
5331 int slot = path->slots[0];
5332 u32 cur_offset = 0;
5333 u32 item_size;
5334 unsigned long ptr;
5335
5336 if (slot >= btrfs_header_nritems(leaf)) {
5337 ret = btrfs_next_leaf(root, path);
5338 if (ret < 0)
5339 goto out;
5340 else if (ret > 0)
5341 break;
5342 continue;
5343 }
5344
5345 btrfs_item_key_to_cpu(leaf, &key, slot);
5346 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5347 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5348 break;
5349
5350 item_size = btrfs_item_size_nr(leaf, slot);
5351 ptr = btrfs_item_ptr_offset(leaf, slot);
5352 while (cur_offset < item_size) {
5353 struct btrfs_key inode_key;
5354 struct inode *dir_inode;
5355
5356 inode_key.type = BTRFS_INODE_ITEM_KEY;
5357 inode_key.offset = 0;
5358
5359 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5360 struct btrfs_inode_extref *extref;
5361
5362 extref = (struct btrfs_inode_extref *)
5363 (ptr + cur_offset);
5364 inode_key.objectid = btrfs_inode_extref_parent(
5365 leaf, extref);
5366 cur_offset += sizeof(*extref);
5367 cur_offset += btrfs_inode_extref_name_len(leaf,
5368 extref);
5369 } else {
5370 inode_key.objectid = key.offset;
5371 cur_offset = item_size;
5372 }
5373
5374 dir_inode = btrfs_iget(root->fs_info->sb, &inode_key,
5375 root, NULL);
5376 /* If parent inode was deleted, skip it. */
5377 if (IS_ERR(dir_inode))
5378 continue;
5379
Filipe Manana657ed1a2016-04-06 17:11:56 +01005380 if (ctx)
5381 ctx->log_new_dentries = false;
Filipe Manana18aa0922015-08-05 16:49:08 +01005382 ret = btrfs_log_inode(trans, root, dir_inode,
5383 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005384 if (!ret &&
5385 btrfs_must_commit_transaction(trans, dir_inode))
5386 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005387 if (!ret && ctx && ctx->log_new_dentries)
5388 ret = log_new_dir_dentries(trans, root,
5389 dir_inode, ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005390 iput(dir_inode);
5391 if (ret)
5392 goto out;
5393 }
5394 path->slots[0]++;
5395 }
5396 ret = 0;
5397out:
5398 btrfs_free_path(path);
5399 return ret;
5400}
5401
Chris Masone02119d2008-09-05 16:13:11 -04005402/*
5403 * helper function around btrfs_log_inode to make sure newly created
5404 * parent directories also end up in the log. A minimal inode and backref
5405 * only logging is done of any parent directories that are older than
5406 * the last committed transaction
5407 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005408static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5409 struct btrfs_root *root, struct inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005410 struct dentry *parent,
5411 const loff_t start,
5412 const loff_t end,
5413 int exists_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005414 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005415{
Chris Mason12fcfd22009-03-24 10:24:20 -04005416 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04005417 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005418 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005419 int ret = 0;
5420 u64 last_committed = root->fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005421 bool log_dentries = false;
5422 struct inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005423
5424 sb = inode->i_sb;
5425
Jeff Mahoney3cdde222016-06-09 21:38:35 -04005426 if (btrfs_test_opt(root->fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005427 ret = 1;
5428 goto end_no_trans;
5429 }
5430
Miao Xie995946d2014-04-02 19:51:06 +08005431 /*
5432 * The prev transaction commit doesn't complete, we need do
5433 * full commit by ourselves.
5434 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005435 if (root->fs_info->last_trans_log_full_commit >
5436 root->fs_info->last_trans_committed) {
5437 ret = 1;
5438 goto end_no_trans;
5439 }
5440
Yan, Zheng76dda932009-09-21 16:00:26 -04005441 if (root != BTRFS_I(inode)->root ||
5442 btrfs_root_refs(&root->root_item) == 0) {
5443 ret = 1;
5444 goto end_no_trans;
5445 }
5446
Chris Mason12fcfd22009-03-24 10:24:20 -04005447 ret = check_parent_dirs_for_sync(trans, inode, parent,
5448 sb, last_committed);
5449 if (ret)
5450 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005451
Josef Bacik22ee6982012-05-29 16:57:49 -04005452 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005453 ret = BTRFS_NO_LOG_SYNC;
5454 goto end_no_trans;
5455 }
5456
Miao Xie8b050d32014-02-20 18:08:58 +08005457 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005458 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005459 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005460
Filipe Manana8407f552014-09-05 15:14:39 +01005461 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005462 if (ret)
5463 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005464
Chris Masonaf4176b2009-03-24 10:24:31 -04005465 /*
5466 * for regular files, if its inode is already on disk, we don't
5467 * have to worry about the parents at all. This is because
5468 * we can use the last_unlink_trans field to record renames
5469 * and other fun in this file.
5470 */
5471 if (S_ISREG(inode->i_mode) &&
5472 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005473 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
5474 ret = 0;
5475 goto end_trans;
5476 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005477
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005478 if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
5479 log_dentries = true;
5480
Filipe Manana18aa0922015-08-05 16:49:08 +01005481 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005482 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005483 * inodes are fully logged. This is to prevent leaving dangling
5484 * directory index entries in directories that were our parents but are
5485 * not anymore. Not doing this results in old parent directory being
5486 * impossible to delete after log replay (rmdir will always fail with
5487 * error -ENOTEMPTY).
5488 *
5489 * Example 1:
5490 *
5491 * mkdir testdir
5492 * touch testdir/foo
5493 * ln testdir/foo testdir/bar
5494 * sync
5495 * unlink testdir/bar
5496 * xfs_io -c fsync testdir/foo
5497 * <power failure>
5498 * mount fs, triggers log replay
5499 *
5500 * If we don't log the parent directory (testdir), after log replay the
5501 * directory still has an entry pointing to the file inode using the bar
5502 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5503 * the file inode has a link count of 1.
5504 *
5505 * Example 2:
5506 *
5507 * mkdir testdir
5508 * touch foo
5509 * ln foo testdir/foo2
5510 * ln foo testdir/foo3
5511 * sync
5512 * unlink testdir/foo3
5513 * xfs_io -c fsync foo
5514 * <power failure>
5515 * mount fs, triggers log replay
5516 *
5517 * Similar as the first example, after log replay the parent directory
5518 * testdir still has an entry pointing to the inode file with name foo3
5519 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5520 * and has a link count of 2.
5521 */
5522 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
5523 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5524 if (ret)
5525 goto end_trans;
5526 }
5527
Chris Masond3977122009-01-05 21:25:51 -05005528 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005529 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005530 break;
5531
David Howells2b0143b2015-03-17 22:25:59 +00005532 inode = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005533 if (root != BTRFS_I(inode)->root)
5534 break;
5535
Filipe Manana18aa0922015-08-05 16:49:08 +01005536 if (BTRFS_I(inode)->generation > last_committed) {
5537 ret = btrfs_log_inode(trans, root, inode,
5538 LOG_INODE_EXISTS,
Filipe Manana8407f552014-09-05 15:14:39 +01005539 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005540 if (ret)
5541 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005542 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005543 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005544 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005545
Josef Bacik6a912212010-11-20 09:48:00 +00005546 parent = dget_parent(parent);
5547 dput(old_parent);
5548 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005549 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005550 if (log_dentries)
5551 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5552 else
5553 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005554end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005555 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005556 if (ret < 0) {
Miao Xie995946d2014-04-02 19:51:06 +08005557 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005558 ret = 1;
5559 }
Miao Xie8b050d32014-02-20 18:08:58 +08005560
5561 if (ret)
5562 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005563 btrfs_end_log_trans(root);
5564end_no_trans:
5565 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005566}
5567
5568/*
5569 * it is not safe to log dentry if the chunk root has added new
5570 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5571 * If this returns 1, you must commit the transaction to safely get your
5572 * data on disk.
5573 */
5574int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08005575 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005576 const loff_t start,
5577 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005578 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005579{
Josef Bacik6a912212010-11-20 09:48:00 +00005580 struct dentry *parent = dget_parent(dentry);
5581 int ret;
5582
David Howells2b0143b2015-03-17 22:25:59 +00005583 ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005584 start, end, 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005585 dput(parent);
5586
5587 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005588}
5589
5590/*
5591 * should be called during mount to recover any replay any log trees
5592 * from the FS
5593 */
5594int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5595{
5596 int ret;
5597 struct btrfs_path *path;
5598 struct btrfs_trans_handle *trans;
5599 struct btrfs_key key;
5600 struct btrfs_key found_key;
5601 struct btrfs_key tmp_key;
5602 struct btrfs_root *log;
5603 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5604 struct walk_control wc = {
5605 .process_func = process_one_buffer,
5606 .stage = 0,
5607 };
5608
Chris Masone02119d2008-09-05 16:13:11 -04005609 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005610 if (!path)
5611 return -ENOMEM;
5612
Josef Bacikafcdd122016-09-02 15:40:02 -04005613 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005614
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005615 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005616 if (IS_ERR(trans)) {
5617 ret = PTR_ERR(trans);
5618 goto error;
5619 }
Chris Masone02119d2008-09-05 16:13:11 -04005620
5621 wc.trans = trans;
5622 wc.pin = 1;
5623
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005624 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005625 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005626 btrfs_handle_fs_error(fs_info, ret,
5627 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005628 goto error;
5629 }
Chris Masone02119d2008-09-05 16:13:11 -04005630
5631again:
5632 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5633 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005634 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005635
Chris Masond3977122009-01-05 21:25:51 -05005636 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005637 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005638
5639 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005640 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005641 "Couldn't find tree log root.");
5642 goto error;
5643 }
Chris Masone02119d2008-09-05 16:13:11 -04005644 if (ret > 0) {
5645 if (path->slots[0] == 0)
5646 break;
5647 path->slots[0]--;
5648 }
5649 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5650 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005651 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005652 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5653 break;
5654
Miao Xiecb517ea2013-05-15 07:48:19 +00005655 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005656 if (IS_ERR(log)) {
5657 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005658 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005659 "Couldn't read tree log root.");
5660 goto error;
5661 }
Chris Masone02119d2008-09-05 16:13:11 -04005662
5663 tmp_key.objectid = found_key.offset;
5664 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5665 tmp_key.offset = (u64)-1;
5666
5667 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005668 if (IS_ERR(wc.replay_dest)) {
5669 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005670 free_extent_buffer(log->node);
5671 free_extent_buffer(log->commit_root);
5672 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005673 btrfs_handle_fs_error(fs_info, ret,
5674 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005675 goto error;
5676 }
Chris Masone02119d2008-09-05 16:13:11 -04005677
Yan Zheng07d400a2009-01-06 11:42:00 -05005678 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005679 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005680 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005681
Josef Bacikb50c6e22013-04-25 15:55:30 -04005682 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005683 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5684 path);
Chris Masone02119d2008-09-05 16:13:11 -04005685 }
Chris Masone02119d2008-09-05 16:13:11 -04005686
Liu Boefb1cbc2018-01-25 11:02:56 -07005687 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5688 struct btrfs_root *root = wc.replay_dest;
5689
5690 btrfs_release_path(path);
5691
5692 /*
5693 * We have just replayed everything, and the highest
5694 * objectid of fs roots probably has changed in case
5695 * some inode_item's got replayed.
5696 *
5697 * root->objectid_mutex is not acquired as log replay
5698 * could only happen during mount.
5699 */
5700 ret = btrfs_find_highest_objectid(root,
5701 &root->highest_objectid);
5702 }
5703
Chris Masone02119d2008-09-05 16:13:11 -04005704 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005705 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005706 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005707 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005708 kfree(log);
5709
Josef Bacikb50c6e22013-04-25 15:55:30 -04005710 if (ret)
5711 goto error;
5712
Chris Masone02119d2008-09-05 16:13:11 -04005713 if (found_key.offset == 0)
5714 break;
5715 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005716 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005717
5718 /* step one is to pin it all, step two is to replay just inodes */
5719 if (wc.pin) {
5720 wc.pin = 0;
5721 wc.process_func = replay_one_buffer;
5722 wc.stage = LOG_WALK_REPLAY_INODES;
5723 goto again;
5724 }
5725 /* step three is to replay everything */
5726 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5727 wc.stage++;
5728 goto again;
5729 }
5730
5731 btrfs_free_path(path);
5732
Josef Bacikabefa552013-04-24 16:40:05 -04005733 /* step 4: commit the transaction, which also unpins the blocks */
5734 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
5735 if (ret)
5736 return ret;
5737
Chris Masone02119d2008-09-05 16:13:11 -04005738 free_extent_buffer(log_root_tree->node);
5739 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005740 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005741 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005742
Josef Bacikabefa552013-04-24 16:40:05 -04005743 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005744error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005745 if (wc.trans)
5746 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005747 btrfs_free_path(path);
5748 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005749}
Chris Mason12fcfd22009-03-24 10:24:20 -04005750
5751/*
5752 * there are some corner cases where we want to force a full
5753 * commit instead of allowing a directory to be logged.
5754 *
5755 * They revolve around files there were unlinked from the directory, and
5756 * this function updates the parent directory so that a full commit is
5757 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005758 *
5759 * Must be called before the unlink operations (updates to the subvolume tree,
5760 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005761 */
5762void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
5763 struct inode *dir, struct inode *inode,
5764 int for_rename)
5765{
5766 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005767 * when we're logging a file, if it hasn't been renamed
5768 * or unlinked, and its inode is fully committed on disk,
5769 * we don't have to worry about walking up the directory chain
5770 * to log its parents.
5771 *
5772 * So, we use the last_unlink_trans field to put this transid
5773 * into the file. When the file is logged we check it and
5774 * don't log the parents if the file is fully on disk.
5775 */
Filipe Manana657ed1a2016-04-06 17:11:56 +01005776 mutex_lock(&BTRFS_I(inode)->log_mutex);
5777 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5778 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04005779
5780 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005781 * if this directory was already logged any new
5782 * names for this file/dir will get recorded
5783 */
5784 smp_mb();
5785 if (BTRFS_I(dir)->logged_trans == trans->transid)
5786 return;
5787
5788 /*
5789 * if the inode we're about to unlink was logged,
5790 * the log will be properly updated for any new names
5791 */
5792 if (BTRFS_I(inode)->logged_trans == trans->transid)
5793 return;
5794
5795 /*
5796 * when renaming files across directories, if the directory
5797 * there we're unlinking from gets fsync'd later on, there's
5798 * no way to find the destination directory later and fsync it
5799 * properly. So, we have to be conservative and force commits
5800 * so the new name gets discovered.
5801 */
5802 if (for_rename)
5803 goto record;
5804
5805 /* we can safely do the unlink without any special recording */
5806 return;
5807
5808record:
Filipe Manana2be63d52016-02-12 11:34:23 +00005809 mutex_lock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005810 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005811 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005812}
5813
5814/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005815 * Make sure that if someone attempts to fsync the parent directory of a deleted
5816 * snapshot, it ends up triggering a transaction commit. This is to guarantee
5817 * that after replaying the log tree of the parent directory's root we will not
5818 * see the snapshot anymore and at log replay time we will not see any log tree
5819 * corresponding to the deleted snapshot's root, which could lead to replaying
5820 * it after replaying the log tree of the parent directory (which would replay
5821 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00005822 *
5823 * Must be called before the actual snapshot destroy operation (updates to the
5824 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005825 */
5826void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
5827 struct inode *dir)
5828{
Filipe Manana2be63d52016-02-12 11:34:23 +00005829 mutex_lock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005830 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005831 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005832}
5833
5834/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005835 * Call this after adding a new name for a file and it will properly
5836 * update the log to reflect the new name.
5837 *
5838 * It will return zero if all goes well, and it will return 1 if a
5839 * full transaction commit is required.
5840 */
5841int btrfs_log_new_name(struct btrfs_trans_handle *trans,
5842 struct inode *inode, struct inode *old_dir,
5843 struct dentry *parent)
5844{
5845 struct btrfs_root * root = BTRFS_I(inode)->root;
5846
5847 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005848 * this will force the logging code to walk the dentry chain
5849 * up for the file
5850 */
5851 if (S_ISREG(inode->i_mode))
5852 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5853
5854 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005855 * if this inode hasn't been logged and directory we're renaming it
5856 * from hasn't been logged, we don't need to log it
5857 */
5858 if (BTRFS_I(inode)->logged_trans <=
5859 root->fs_info->last_trans_committed &&
5860 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
5861 root->fs_info->last_trans_committed))
5862 return 0;
5863
Filipe Manana49dae1b2014-09-06 22:34:39 +01005864 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
5865 LLONG_MAX, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04005866}
5867