blob: 5539f0b95efa8fad0f95393d6ed33f9c10966119 [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);
2244 if (ret)
2245 break;
2246 }
2247 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2248 path->slots[0]);
2249 if (found_key.objectid != dirid ||
2250 found_key.type != dir_key.type)
2251 goto next_type;
2252
2253 if (found_key.offset > range_end)
2254 break;
2255
2256 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002257 log_path, dir,
2258 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002259 if (ret)
2260 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002261 if (found_key.offset == (u64)-1)
2262 break;
2263 dir_key.offset = found_key.offset + 1;
2264 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002265 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002266 if (range_end == (u64)-1)
2267 break;
2268 range_start = range_end + 1;
2269 }
2270
2271next_type:
2272 ret = 0;
2273 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2274 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2275 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002276 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002277 goto again;
2278 }
2279out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002280 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002281 btrfs_free_path(log_path);
2282 iput(dir);
2283 return ret;
2284}
2285
2286/*
2287 * the process_func used to replay items from the log tree. This
2288 * gets called in two different stages. The first stage just looks
2289 * for inodes and makes sure they are all copied into the subvolume.
2290 *
2291 * The second stage copies all the other item types from the log into
2292 * the subvolume. The two stage approach is slower, but gets rid of
2293 * lots of complexity around inodes referencing other inodes that exist
2294 * only in the log (references come from either directory items or inode
2295 * back refs).
2296 */
2297static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2298 struct walk_control *wc, u64 gen)
2299{
2300 int nritems;
2301 struct btrfs_path *path;
2302 struct btrfs_root *root = wc->replay_dest;
2303 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002304 int level;
2305 int i;
2306 int ret;
2307
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002308 ret = btrfs_read_buffer(eb, gen);
2309 if (ret)
2310 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002311
2312 level = btrfs_header_level(eb);
2313
2314 if (level != 0)
2315 return 0;
2316
2317 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002318 if (!path)
2319 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002320
2321 nritems = btrfs_header_nritems(eb);
2322 for (i = 0; i < nritems; i++) {
2323 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002324
2325 /* inode keys are done during the first stage */
2326 if (key.type == BTRFS_INODE_ITEM_KEY &&
2327 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002328 struct btrfs_inode_item *inode_item;
2329 u32 mode;
2330
2331 inode_item = btrfs_item_ptr(eb, i,
2332 struct btrfs_inode_item);
Filipe Manana4f764e52015-02-23 19:53:35 +00002333 ret = replay_xattr_deletes(wc->trans, root, log,
2334 path, key.objectid);
2335 if (ret)
2336 break;
Chris Masone02119d2008-09-05 16:13:11 -04002337 mode = btrfs_inode_mode(eb, inode_item);
2338 if (S_ISDIR(mode)) {
2339 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002340 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002341 if (ret)
2342 break;
Chris Masone02119d2008-09-05 16:13:11 -04002343 }
2344 ret = overwrite_item(wc->trans, root, path,
2345 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002346 if (ret)
2347 break;
Chris Masone02119d2008-09-05 16:13:11 -04002348
Yan, Zhengc71bf092009-11-12 09:34:40 +00002349 /* for regular files, make sure corresponding
Nicholas D Steeves01327612016-05-19 21:18:45 -04002350 * orphan item exist. extents past the new EOF
Yan, Zhengc71bf092009-11-12 09:34:40 +00002351 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002352 */
2353 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002354 ret = insert_orphan_item(wc->trans, root,
2355 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002356 if (ret)
2357 break;
Chris Masone02119d2008-09-05 16:13:11 -04002358 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002359
Chris Masone02119d2008-09-05 16:13:11 -04002360 ret = link_to_fixup_dir(wc->trans, root,
2361 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002362 if (ret)
2363 break;
Chris Masone02119d2008-09-05 16:13:11 -04002364 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002365
2366 if (key.type == BTRFS_DIR_INDEX_KEY &&
2367 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2368 ret = replay_one_dir_item(wc->trans, root, path,
2369 eb, i, &key);
2370 if (ret)
2371 break;
2372 }
2373
Chris Masone02119d2008-09-05 16:13:11 -04002374 if (wc->stage < LOG_WALK_REPLAY_ALL)
2375 continue;
2376
2377 /* these keys are simply copied */
2378 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2379 ret = overwrite_item(wc->trans, root, path,
2380 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002381 if (ret)
2382 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002383 } else if (key.type == BTRFS_INODE_REF_KEY ||
2384 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002385 ret = add_inode_ref(wc->trans, root, log, path,
2386 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002387 if (ret && ret != -ENOENT)
2388 break;
2389 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002390 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2391 ret = replay_one_extent(wc->trans, root, path,
2392 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002393 if (ret)
2394 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002395 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002396 ret = replay_one_dir_item(wc->trans, root, path,
2397 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002398 if (ret)
2399 break;
Chris Masone02119d2008-09-05 16:13:11 -04002400 }
2401 }
2402 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002403 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002404}
2405
Chris Masond3977122009-01-05 21:25:51 -05002406static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002407 struct btrfs_root *root,
2408 struct btrfs_path *path, int *level,
2409 struct walk_control *wc)
2410{
2411 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002412 u64 bytenr;
2413 u64 ptr_gen;
2414 struct extent_buffer *next;
2415 struct extent_buffer *cur;
2416 struct extent_buffer *parent;
2417 u32 blocksize;
2418 int ret = 0;
2419
2420 WARN_ON(*level < 0);
2421 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2422
Chris Masond3977122009-01-05 21:25:51 -05002423 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002424 WARN_ON(*level < 0);
2425 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2426 cur = path->nodes[*level];
2427
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302428 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002429
2430 if (path->slots[*level] >=
2431 btrfs_header_nritems(cur))
2432 break;
2433
2434 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2435 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
David Sterba707e8a02014-06-04 19:22:26 +02002436 blocksize = root->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002437
2438 parent = path->nodes[*level];
2439 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002440
David Sterbaa83fffb2014-06-15 02:39:54 +02002441 next = btrfs_find_create_tree_block(root, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07002442 if (IS_ERR(next))
2443 return PTR_ERR(next);
Chris Masone02119d2008-09-05 16:13:11 -04002444
Chris Masone02119d2008-09-05 16:13:11 -04002445 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002446 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002447 if (ret) {
2448 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002449 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002450 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002451
Chris Masone02119d2008-09-05 16:13:11 -04002452 path->slots[*level]++;
2453 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002454 ret = btrfs_read_buffer(next, ptr_gen);
2455 if (ret) {
2456 free_extent_buffer(next);
2457 return ret;
2458 }
Chris Masone02119d2008-09-05 16:13:11 -04002459
Josef Bacik681ae502013-10-07 15:11:00 -04002460 if (trans) {
2461 btrfs_tree_lock(next);
2462 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002463 clean_tree_block(trans, root->fs_info,
2464 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002465 btrfs_wait_tree_block_writeback(next);
2466 btrfs_tree_unlock(next);
Liu Bo0f4adc12018-01-25 11:02:51 -07002467 } else {
2468 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2469 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002470 }
Chris Masone02119d2008-09-05 16:13:11 -04002471
Chris Masone02119d2008-09-05 16:13:11 -04002472 WARN_ON(root_owner !=
2473 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002474 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002475 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002476 if (ret) {
2477 free_extent_buffer(next);
2478 return ret;
2479 }
Chris Masone02119d2008-09-05 16:13:11 -04002480 }
2481 free_extent_buffer(next);
2482 continue;
2483 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002484 ret = btrfs_read_buffer(next, ptr_gen);
2485 if (ret) {
2486 free_extent_buffer(next);
2487 return ret;
2488 }
Chris Masone02119d2008-09-05 16:13:11 -04002489
2490 WARN_ON(*level <= 0);
2491 if (path->nodes[*level-1])
2492 free_extent_buffer(path->nodes[*level-1]);
2493 path->nodes[*level-1] = next;
2494 *level = btrfs_header_level(next);
2495 path->slots[*level] = 0;
2496 cond_resched();
2497 }
2498 WARN_ON(*level < 0);
2499 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2500
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002501 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002502
2503 cond_resched();
2504 return 0;
2505}
2506
Chris Masond3977122009-01-05 21:25:51 -05002507static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002508 struct btrfs_root *root,
2509 struct btrfs_path *path, int *level,
2510 struct walk_control *wc)
2511{
2512 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002513 int i;
2514 int slot;
2515 int ret;
2516
Chris Masond3977122009-01-05 21:25:51 -05002517 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002518 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002519 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002520 path->slots[i]++;
2521 *level = i;
2522 WARN_ON(*level == 0);
2523 return 0;
2524 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002525 struct extent_buffer *parent;
2526 if (path->nodes[*level] == root->node)
2527 parent = path->nodes[*level];
2528 else
2529 parent = path->nodes[*level + 1];
2530
2531 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002532 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002533 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002534 if (ret)
2535 return ret;
2536
Chris Masone02119d2008-09-05 16:13:11 -04002537 if (wc->free) {
2538 struct extent_buffer *next;
2539
2540 next = path->nodes[*level];
2541
Josef Bacik681ae502013-10-07 15:11:00 -04002542 if (trans) {
2543 btrfs_tree_lock(next);
2544 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002545 clean_tree_block(trans, root->fs_info,
2546 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002547 btrfs_wait_tree_block_writeback(next);
2548 btrfs_tree_unlock(next);
Liu Bo0f4adc12018-01-25 11:02:51 -07002549 } else {
2550 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2551 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002552 }
Chris Masone02119d2008-09-05 16:13:11 -04002553
Chris Masone02119d2008-09-05 16:13:11 -04002554 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002555 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002556 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002557 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002558 if (ret)
2559 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002560 }
2561 free_extent_buffer(path->nodes[*level]);
2562 path->nodes[*level] = NULL;
2563 *level = i + 1;
2564 }
2565 }
2566 return 1;
2567}
2568
2569/*
2570 * drop the reference count on the tree rooted at 'snap'. This traverses
2571 * the tree freeing any blocks that have a ref count of zero after being
2572 * decremented.
2573 */
2574static int walk_log_tree(struct btrfs_trans_handle *trans,
2575 struct btrfs_root *log, struct walk_control *wc)
2576{
2577 int ret = 0;
2578 int wret;
2579 int level;
2580 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002581 int orig_level;
2582
2583 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002584 if (!path)
2585 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002586
2587 level = btrfs_header_level(log->node);
2588 orig_level = level;
2589 path->nodes[level] = log->node;
2590 extent_buffer_get(log->node);
2591 path->slots[level] = 0;
2592
Chris Masond3977122009-01-05 21:25:51 -05002593 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002594 wret = walk_down_log_tree(trans, log, path, &level, wc);
2595 if (wret > 0)
2596 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002597 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002598 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002599 goto out;
2600 }
Chris Masone02119d2008-09-05 16:13:11 -04002601
2602 wret = walk_up_log_tree(trans, log, path, &level, wc);
2603 if (wret > 0)
2604 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002605 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002606 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002607 goto out;
2608 }
Chris Masone02119d2008-09-05 16:13:11 -04002609 }
2610
2611 /* was the root node processed? if not, catch it here */
2612 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002613 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002614 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002615 if (ret)
2616 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002617 if (wc->free) {
2618 struct extent_buffer *next;
2619
2620 next = path->nodes[orig_level];
2621
Josef Bacik681ae502013-10-07 15:11:00 -04002622 if (trans) {
2623 btrfs_tree_lock(next);
2624 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002625 clean_tree_block(trans, log->fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002626 btrfs_wait_tree_block_writeback(next);
2627 btrfs_tree_unlock(next);
Liu Bo0f4adc12018-01-25 11:02:51 -07002628 } else {
2629 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2630 clear_extent_buffer_dirty(next);
Josef Bacik681ae502013-10-07 15:11:00 -04002631 }
Chris Masone02119d2008-09-05 16:13:11 -04002632
Chris Masone02119d2008-09-05 16:13:11 -04002633 WARN_ON(log->root_key.objectid !=
2634 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002635 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002636 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002637 if (ret)
2638 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002639 }
2640 }
2641
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002642out:
Chris Masone02119d2008-09-05 16:13:11 -04002643 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002644 return ret;
2645}
2646
Yan Zheng7237f182009-01-21 12:54:03 -05002647/*
2648 * helper function to update the item for a given subvolumes log root
2649 * in the tree of log roots
2650 */
2651static int update_log_root(struct btrfs_trans_handle *trans,
2652 struct btrfs_root *log)
2653{
2654 int ret;
2655
2656 if (log->log_transid == 1) {
2657 /* insert root item on the first sync */
2658 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2659 &log->root_key, &log->root_item);
2660 } else {
2661 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2662 &log->root_key, &log->root_item);
2663 }
2664 return ret;
2665}
2666
Zhaolei60d53eb2015-08-17 18:44:46 +08002667static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002668{
2669 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002670 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002671
Yan Zheng7237f182009-01-21 12:54:03 -05002672 /*
2673 * we only allow two pending log transactions at a time,
2674 * so we know that if ours is more than 2 older than the
2675 * current transaction, we're done
2676 */
Chris Masone02119d2008-09-05 16:13:11 -04002677 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002678 prepare_to_wait(&root->log_commit_wait[index],
2679 &wait, TASK_UNINTERRUPTIBLE);
2680 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002681
Miao Xied1433de2014-02-20 18:08:59 +08002682 if (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002683 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002684 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002685
Yan Zheng7237f182009-01-21 12:54:03 -05002686 finish_wait(&root->log_commit_wait[index], &wait);
2687 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002688 } while (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002689 atomic_read(&root->log_commit[index]));
Yan Zheng7237f182009-01-21 12:54:03 -05002690}
2691
Zhaolei60d53eb2015-08-17 18:44:46 +08002692static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002693{
2694 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002695
2696 while (atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002697 prepare_to_wait(&root->log_writer_wait,
2698 &wait, TASK_UNINTERRUPTIBLE);
2699 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002700 if (atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002701 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002702 finish_wait(&root->log_writer_wait, &wait);
Filipe Manana575849e2015-02-11 11:12:39 +00002703 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002704 }
Chris Masone02119d2008-09-05 16:13:11 -04002705}
2706
Miao Xie8b050d32014-02-20 18:08:58 +08002707static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2708 struct btrfs_log_ctx *ctx)
2709{
2710 if (!ctx)
2711 return;
2712
2713 mutex_lock(&root->log_mutex);
2714 list_del_init(&ctx->list);
2715 mutex_unlock(&root->log_mutex);
2716}
2717
2718/*
2719 * Invoked in log mutex context, or be sure there is no other task which
2720 * can access the list.
2721 */
2722static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2723 int index, int error)
2724{
2725 struct btrfs_log_ctx *ctx;
Chris Mason570dd452016-10-27 10:42:20 -07002726 struct btrfs_log_ctx *safe;
Miao Xie8b050d32014-02-20 18:08:58 +08002727
Chris Mason570dd452016-10-27 10:42:20 -07002728 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2729 list_del_init(&ctx->list);
Miao Xie8b050d32014-02-20 18:08:58 +08002730 ctx->log_ret = error;
Chris Mason570dd452016-10-27 10:42:20 -07002731 }
Miao Xie8b050d32014-02-20 18:08:58 +08002732
2733 INIT_LIST_HEAD(&root->log_ctxs[index]);
2734}
2735
Chris Masone02119d2008-09-05 16:13:11 -04002736/*
2737 * btrfs_sync_log does sends a given tree log down to the disk and
2738 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002739 * you know that any inodes previously logged are safely on disk only
2740 * if it returns 0.
2741 *
2742 * Any other return value means you need to call btrfs_commit_transaction.
2743 * Some of the edge cases for fsyncing directories that have had unlinks
2744 * or renames done in the past mean that sometimes the only safe
2745 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2746 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002747 */
2748int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002749 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002750{
Yan Zheng7237f182009-01-21 12:54:03 -05002751 int index1;
2752 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002753 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002754 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002755 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002756 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002757 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002758 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002759 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002760
Yan Zheng7237f182009-01-21 12:54:03 -05002761 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002762 log_transid = ctx->log_transid;
2763 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002764 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002765 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002766 }
Miao Xied1433de2014-02-20 18:08:59 +08002767
2768 index1 = log_transid % 2;
2769 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002770 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002771 mutex_unlock(&root->log_mutex);
2772 return ctx->log_ret;
2773 }
2774 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002775 atomic_set(&root->log_commit[index1], 1);
2776
2777 /* wait for previous tree log sync to complete */
2778 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002779 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002780
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002781 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002782 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002783 /* when we're on an ssd, just kick the log commit out */
Jeff Mahoney3cdde222016-06-09 21:38:35 -04002784 if (!btrfs_test_opt(root->fs_info, SSD) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08002785 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002786 mutex_unlock(&root->log_mutex);
2787 schedule_timeout_uninterruptible(1);
2788 mutex_lock(&root->log_mutex);
2789 }
Zhaolei60d53eb2015-08-17 18:44:46 +08002790 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002791 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002792 break;
2793 }
Chris Masond0c803c2008-09-11 16:17:57 -04002794
Chris Mason12fcfd22009-03-24 10:24:20 -04002795 /* bail out if we need to do a full commit */
Miao Xie995946d2014-04-02 19:51:06 +08002796 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002797 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002798 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002799 mutex_unlock(&root->log_mutex);
2800 goto out;
2801 }
2802
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002803 if (log_transid % 2 == 0)
2804 mark = EXTENT_DIRTY;
2805 else
2806 mark = EXTENT_NEW;
2807
Chris Mason690587d2009-10-13 13:29:19 -04002808 /* we start IO on all the marked extents here, but we don't actually
2809 * wait for them until later.
2810 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002811 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002812 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002813 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002814 blk_finish_plug(&plug);
Jeff Mahoney66642832016-06-10 18:19:25 -04002815 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002816 btrfs_free_logged_extents(log, log_transid);
Miao Xie995946d2014-04-02 19:51:06 +08002817 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002818 mutex_unlock(&root->log_mutex);
2819 goto out;
2820 }
Yan Zheng7237f182009-01-21 12:54:03 -05002821
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002822 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002823
Yan Zheng7237f182009-01-21 12:54:03 -05002824 root->log_transid++;
2825 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002826 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002827 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002828 * IO has been started, blocks of the log tree have WRITTEN flag set
2829 * in their headers. new modifications of the log will be written to
2830 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002831 */
2832 mutex_unlock(&root->log_mutex);
2833
Filipe Manana28a23592016-08-23 21:13:51 +01002834 btrfs_init_log_ctx(&root_log_ctx, NULL);
Miao Xied1433de2014-02-20 18:08:59 +08002835
Yan Zheng7237f182009-01-21 12:54:03 -05002836 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002837 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002838 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002839
2840 index2 = log_root_tree->log_transid % 2;
2841 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2842 root_log_ctx.log_transid = log_root_tree->log_transid;
2843
Yan Zheng7237f182009-01-21 12:54:03 -05002844 mutex_unlock(&log_root_tree->log_mutex);
2845
2846 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002847
2848 mutex_lock(&log_root_tree->log_mutex);
2849 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
David Sterba779adf02015-02-16 19:39:00 +01002850 /*
2851 * Implicit memory barrier after atomic_dec_and_test
2852 */
Yan Zheng7237f182009-01-21 12:54:03 -05002853 if (waitqueue_active(&log_root_tree->log_writer_wait))
2854 wake_up(&log_root_tree->log_writer_wait);
2855 }
2856
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002857 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002858 if (!list_empty(&root_log_ctx.list))
2859 list_del_init(&root_log_ctx.list);
2860
Miao Xiec6adc9c2013-05-28 10:05:39 +00002861 blk_finish_plug(&plug);
Miao Xie995946d2014-04-02 19:51:06 +08002862 btrfs_set_log_full_commit(root->fs_info, trans);
2863
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002864 if (ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002865 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002866 mutex_unlock(&log_root_tree->log_mutex);
2867 goto out;
2868 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002869 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002870 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002871 mutex_unlock(&log_root_tree->log_mutex);
2872 ret = -EAGAIN;
2873 goto out;
2874 }
2875
Miao Xied1433de2014-02-20 18:08:59 +08002876 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08002877 blk_finish_plug(&plug);
Chris Masoncbd60aa2016-09-06 05:37:40 -07002878 list_del_init(&root_log_ctx.list);
Miao Xied1433de2014-02-20 18:08:59 +08002879 mutex_unlock(&log_root_tree->log_mutex);
2880 ret = root_log_ctx.log_ret;
2881 goto out;
2882 }
Miao Xie8b050d32014-02-20 18:08:58 +08002883
Miao Xied1433de2014-02-20 18:08:59 +08002884 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05002885 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002886 blk_finish_plug(&plug);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002887 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages,
2888 mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05002889 btrfs_wait_logged_extents(trans, log, log_transid);
Zhaolei60d53eb2015-08-17 18:44:46 +08002890 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002891 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002892 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002893 if (!ret)
2894 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002895 goto out;
2896 }
Miao Xied1433de2014-02-20 18:08:59 +08002897 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002898 atomic_set(&log_root_tree->log_commit[index2], 1);
2899
Chris Mason12fcfd22009-03-24 10:24:20 -04002900 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002901 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002902 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002903 }
Yan Zheng7237f182009-01-21 12:54:03 -05002904
Zhaolei60d53eb2015-08-17 18:44:46 +08002905 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04002906
2907 /*
2908 * now that we've moved on to the tree of log tree roots,
2909 * check the full commit flag again
2910 */
Miao Xie995946d2014-04-02 19:51:06 +08002911 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002912 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002913 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002914 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002915 mutex_unlock(&log_root_tree->log_mutex);
2916 ret = -EAGAIN;
2917 goto out_wake_log_root;
2918 }
Yan Zheng7237f182009-01-21 12:54:03 -05002919
Miao Xiec6adc9c2013-05-28 10:05:39 +00002920 ret = btrfs_write_marked_extents(log_root_tree,
2921 &log_root_tree->dirty_log_pages,
2922 EXTENT_DIRTY | EXTENT_NEW);
2923 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002924 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002925 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002926 btrfs_abort_transaction(trans, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002927 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002928 mutex_unlock(&log_root_tree->log_mutex);
2929 goto out_wake_log_root;
2930 }
Filipe Manana5ab5e442014-11-13 16:59:53 +00002931 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2932 if (!ret)
2933 ret = btrfs_wait_marked_extents(log_root_tree,
2934 &log_root_tree->dirty_log_pages,
2935 EXTENT_NEW | EXTENT_DIRTY);
2936 if (ret) {
2937 btrfs_set_log_full_commit(root->fs_info, trans);
2938 btrfs_free_logged_extents(log, log_transid);
2939 mutex_unlock(&log_root_tree->log_mutex);
2940 goto out_wake_log_root;
2941 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05002942 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002943
David Sterba6c417612011-04-13 15:41:04 +02002944 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002945 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002946 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002947 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002948
Yan Zheng7237f182009-01-21 12:54:03 -05002949 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002950 mutex_unlock(&log_root_tree->log_mutex);
2951
2952 /*
2953 * nobody else is going to jump in and write the the ctree
2954 * super here because the log_commit atomic below is protecting
2955 * us. We must be called with a transaction handle pinning
2956 * the running transaction open, so a full commit can't hop
2957 * in and cause problems either.
2958 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002959 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002960 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002961 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney66642832016-06-10 18:19:25 -04002962 btrfs_abort_transaction(trans, ret);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002963 goto out_wake_log_root;
2964 }
Yan Zheng7237f182009-01-21 12:54:03 -05002965
Chris Mason257c62e2009-10-13 13:21:08 -04002966 mutex_lock(&root->log_mutex);
2967 if (root->last_log_commit < log_transid)
2968 root->last_log_commit = log_transid;
2969 mutex_unlock(&root->log_mutex);
2970
Chris Mason12fcfd22009-03-24 10:24:20 -04002971out_wake_log_root:
Chris Mason570dd452016-10-27 10:42:20 -07002972 mutex_lock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002973 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2974
Miao Xied1433de2014-02-20 18:08:59 +08002975 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002976 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002977 mutex_unlock(&log_root_tree->log_mutex);
2978
David Sterba33a9eca2015-10-10 18:35:10 +02002979 /*
2980 * The barrier before waitqueue_active is implied by mutex_unlock
2981 */
Yan Zheng7237f182009-01-21 12:54:03 -05002982 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2983 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002984out:
Miao Xied1433de2014-02-20 18:08:59 +08002985 mutex_lock(&root->log_mutex);
Chris Mason570dd452016-10-27 10:42:20 -07002986 btrfs_remove_all_log_ctxs(root, index1, ret);
Miao Xied1433de2014-02-20 18:08:59 +08002987 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002988 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002989 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002990
David Sterba33a9eca2015-10-10 18:35:10 +02002991 /*
2992 * The barrier before waitqueue_active is implied by mutex_unlock
2993 */
Yan Zheng7237f182009-01-21 12:54:03 -05002994 if (waitqueue_active(&root->log_commit_wait[index1]))
2995 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002996 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002997}
2998
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002999static void free_log_tree(struct btrfs_trans_handle *trans,
3000 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04003001{
3002 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04003003 u64 start;
3004 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04003005 struct walk_control wc = {
3006 .free = 1,
3007 .process_func = process_one_buffer
3008 };
3009
Josef Bacik681ae502013-10-07 15:11:00 -04003010 ret = walk_log_tree(trans, log, &wc);
3011 /* I don't think this can happen but just in case */
3012 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003013 btrfs_abort_transaction(trans, ret);
Chris Masone02119d2008-09-05 16:13:11 -04003014
Chris Masond3977122009-01-05 21:25:51 -05003015 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04003016 ret = find_first_extent_bit(&log->dirty_log_pages,
Liu Bobc0d4312018-01-25 11:02:52 -07003017 0, &start, &end,
3018 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
Josef Bacike6138872012-09-27 17:07:30 -04003019 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04003020 if (ret)
3021 break;
3022
Yan, Zheng8cef4e12009-11-12 09:33:26 +00003023 clear_extent_bits(&log->dirty_log_pages, start, end,
Liu Bobc0d4312018-01-25 11:02:52 -07003024 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
Chris Masond0c803c2008-09-11 16:17:57 -04003025 }
3026
Josef Bacik2ab28f32012-10-12 15:27:49 -04003027 /*
3028 * We may have short-circuited the log tree with the full commit logic
3029 * and left ordered extents on our list, so clear these out to keep us
3030 * from leaking inodes and memory.
3031 */
3032 btrfs_free_logged_extents(log, 0);
3033 btrfs_free_logged_extents(log, 1);
3034
Yan Zheng7237f182009-01-21 12:54:03 -05003035 free_extent_buffer(log->node);
3036 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003037}
3038
3039/*
3040 * free all the extents used by the tree log. This should be called
3041 * at commit time of the full transaction
3042 */
3043int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3044{
3045 if (root->log_root) {
3046 free_log_tree(trans, root->log_root);
3047 root->log_root = NULL;
3048 }
3049 return 0;
3050}
3051
3052int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3053 struct btrfs_fs_info *fs_info)
3054{
3055 if (fs_info->log_root_tree) {
3056 free_log_tree(trans, fs_info->log_root_tree);
3057 fs_info->log_root_tree = NULL;
3058 }
Chris Masone02119d2008-09-05 16:13:11 -04003059 return 0;
3060}
3061
3062/*
Chris Masone02119d2008-09-05 16:13:11 -04003063 * If both a file and directory are logged, and unlinks or renames are
3064 * mixed in, we have a few interesting corners:
3065 *
3066 * create file X in dir Y
3067 * link file X to X.link in dir Y
3068 * fsync file X
3069 * unlink file X but leave X.link
3070 * fsync dir Y
3071 *
3072 * After a crash we would expect only X.link to exist. But file X
3073 * didn't get fsync'd again so the log has back refs for X and X.link.
3074 *
3075 * We solve this by removing directory entries and inode backrefs from the
3076 * log when a file that was logged in the current transaction is
3077 * unlinked. Any later fsync will include the updated log entries, and
3078 * we'll be able to reconstruct the proper directory items from backrefs.
3079 *
3080 * This optimizations allows us to avoid relogging the entire inode
3081 * or the entire directory.
3082 */
3083int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3084 struct btrfs_root *root,
3085 const char *name, int name_len,
3086 struct inode *dir, u64 index)
3087{
3088 struct btrfs_root *log;
3089 struct btrfs_dir_item *di;
3090 struct btrfs_path *path;
3091 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003092 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003093 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003094 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003095
Chris Mason3a5f1d42008-09-11 15:53:37 -04003096 if (BTRFS_I(dir)->logged_trans < trans->transid)
3097 return 0;
3098
Chris Masone02119d2008-09-05 16:13:11 -04003099 ret = join_running_log_trans(root);
3100 if (ret)
3101 return 0;
3102
3103 mutex_lock(&BTRFS_I(dir)->log_mutex);
3104
3105 log = root->log_root;
3106 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003107 if (!path) {
3108 err = -ENOMEM;
3109 goto out_unlock;
3110 }
liubo2a29edc2011-01-26 06:22:08 +00003111
Li Zefan33345d012011-04-20 10:31:50 +08003112 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003113 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003114 if (IS_ERR(di)) {
3115 err = PTR_ERR(di);
3116 goto fail;
3117 }
3118 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003119 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3120 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003121 if (ret) {
3122 err = ret;
3123 goto fail;
3124 }
Chris Masone02119d2008-09-05 16:13:11 -04003125 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003126 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003127 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003128 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003129 if (IS_ERR(di)) {
3130 err = PTR_ERR(di);
3131 goto fail;
3132 }
3133 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003134 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3135 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003136 if (ret) {
3137 err = ret;
3138 goto fail;
3139 }
Chris Masone02119d2008-09-05 16:13:11 -04003140 }
3141
3142 /* update the directory size in the log to reflect the names
3143 * we have removed
3144 */
3145 if (bytes_del) {
3146 struct btrfs_key key;
3147
Li Zefan33345d012011-04-20 10:31:50 +08003148 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003149 key.offset = 0;
3150 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003151 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003152
3153 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003154 if (ret < 0) {
3155 err = ret;
3156 goto fail;
3157 }
Chris Masone02119d2008-09-05 16:13:11 -04003158 if (ret == 0) {
3159 struct btrfs_inode_item *item;
3160 u64 i_size;
3161
3162 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3163 struct btrfs_inode_item);
3164 i_size = btrfs_inode_size(path->nodes[0], item);
3165 if (i_size > bytes_del)
3166 i_size -= bytes_del;
3167 else
3168 i_size = 0;
3169 btrfs_set_inode_size(path->nodes[0], item, i_size);
3170 btrfs_mark_buffer_dirty(path->nodes[0]);
3171 } else
3172 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003173 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003174 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003175fail:
Chris Masone02119d2008-09-05 16:13:11 -04003176 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003177out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04003178 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003179 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003180 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003181 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003182 } else if (ret < 0)
Jeff Mahoney66642832016-06-10 18:19:25 -04003183 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003184
Chris Mason12fcfd22009-03-24 10:24:20 -04003185 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003186
Andi Kleen411fc6b2010-10-29 15:14:31 -04003187 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003188}
3189
3190/* see comments for btrfs_del_dir_entries_in_log */
3191int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3192 struct btrfs_root *root,
3193 const char *name, int name_len,
3194 struct inode *inode, u64 dirid)
3195{
3196 struct btrfs_root *log;
3197 u64 index;
3198 int ret;
3199
Chris Mason3a5f1d42008-09-11 15:53:37 -04003200 if (BTRFS_I(inode)->logged_trans < trans->transid)
3201 return 0;
3202
Chris Masone02119d2008-09-05 16:13:11 -04003203 ret = join_running_log_trans(root);
3204 if (ret)
3205 return 0;
3206 log = root->log_root;
3207 mutex_lock(&BTRFS_I(inode)->log_mutex);
3208
Li Zefan33345d012011-04-20 10:31:50 +08003209 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003210 dirid, &index);
3211 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003212 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003213 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003214 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003215 } else if (ret < 0 && ret != -ENOENT)
Jeff Mahoney66642832016-06-10 18:19:25 -04003216 btrfs_abort_transaction(trans, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003217 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003218
Chris Masone02119d2008-09-05 16:13:11 -04003219 return ret;
3220}
3221
3222/*
3223 * creates a range item in the log for 'dirid'. first_offset and
3224 * last_offset tell us which parts of the key space the log should
3225 * be considered authoritative for.
3226 */
3227static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3228 struct btrfs_root *log,
3229 struct btrfs_path *path,
3230 int key_type, u64 dirid,
3231 u64 first_offset, u64 last_offset)
3232{
3233 int ret;
3234 struct btrfs_key key;
3235 struct btrfs_dir_log_item *item;
3236
3237 key.objectid = dirid;
3238 key.offset = first_offset;
3239 if (key_type == BTRFS_DIR_ITEM_KEY)
3240 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3241 else
3242 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3243 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003244 if (ret)
3245 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003246
3247 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3248 struct btrfs_dir_log_item);
3249 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3250 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003251 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003252 return 0;
3253}
3254
3255/*
3256 * log all the items included in the current transaction for a given
3257 * directory. This also creates the range items in the log tree required
3258 * to replay anything deleted before the fsync
3259 */
3260static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3261 struct btrfs_root *root, struct inode *inode,
3262 struct btrfs_path *path,
3263 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003264 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003265 u64 min_offset, u64 *last_offset_ret)
3266{
3267 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003268 struct btrfs_root *log = root->log_root;
3269 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003270 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003271 int ret;
3272 int i;
3273 int nritems;
3274 u64 first_offset = min_offset;
3275 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08003276 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003277
3278 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003279
Li Zefan33345d012011-04-20 10:31:50 +08003280 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003281 min_key.type = key_type;
3282 min_key.offset = min_offset;
3283
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003284 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003285
3286 /*
3287 * we didn't find anything from this transaction, see if there
3288 * is anything at all
3289 */
Li Zefan33345d012011-04-20 10:31:50 +08003290 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3291 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003292 min_key.type = key_type;
3293 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003294 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003295 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3296 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003297 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003298 return ret;
3299 }
Li Zefan33345d012011-04-20 10:31:50 +08003300 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003301
3302 /* if ret == 0 there are items for this type,
3303 * create a range to tell us the last key of this type.
3304 * otherwise, there are no items in this directory after
3305 * *min_offset, and we create a range to indicate that.
3306 */
3307 if (ret == 0) {
3308 struct btrfs_key tmp;
3309 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3310 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003311 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003312 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003313 }
3314 goto done;
3315 }
3316
3317 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003318 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003319 if (ret == 0) {
3320 struct btrfs_key tmp;
3321 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3322 if (key_type == tmp.type) {
3323 first_offset = tmp.offset;
3324 ret = overwrite_item(trans, log, dst_path,
3325 path->nodes[0], path->slots[0],
3326 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003327 if (ret) {
3328 err = ret;
3329 goto done;
3330 }
Chris Masone02119d2008-09-05 16:13:11 -04003331 }
3332 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003333 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003334
3335 /* find the first key from this transaction again */
3336 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303337 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003338 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003339
3340 /*
3341 * we have a block from this transaction, log every item in it
3342 * from our directory
3343 */
Chris Masond3977122009-01-05 21:25:51 -05003344 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003345 struct btrfs_key tmp;
3346 src = path->nodes[0];
3347 nritems = btrfs_header_nritems(src);
3348 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003349 struct btrfs_dir_item *di;
3350
Chris Masone02119d2008-09-05 16:13:11 -04003351 btrfs_item_key_to_cpu(src, &min_key, i);
3352
Li Zefan33345d012011-04-20 10:31:50 +08003353 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003354 goto done;
3355 ret = overwrite_item(trans, log, dst_path, src, i,
3356 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003357 if (ret) {
3358 err = ret;
3359 goto done;
3360 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003361
3362 /*
3363 * We must make sure that when we log a directory entry,
3364 * the corresponding inode, after log replay, has a
3365 * matching link count. For example:
3366 *
3367 * touch foo
3368 * mkdir mydir
3369 * sync
3370 * ln foo mydir/bar
3371 * xfs_io -c "fsync" mydir
3372 * <crash>
3373 * <mount fs and log replay>
3374 *
3375 * Would result in a fsync log that when replayed, our
3376 * file inode would have a link count of 1, but we get
3377 * two directory entries pointing to the same inode.
3378 * After removing one of the names, it would not be
3379 * possible to remove the other name, which resulted
3380 * always in stale file handle errors, and would not
3381 * be possible to rmdir the parent directory, since
3382 * its i_size could never decrement to the value
3383 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3384 */
3385 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3386 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3387 if (ctx &&
3388 (btrfs_dir_transid(src, di) == trans->transid ||
3389 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3390 tmp.type != BTRFS_ROOT_ITEM_KEY)
3391 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003392 }
3393 path->slots[0] = nritems;
3394
3395 /*
3396 * look ahead to the next item and see if it is also
3397 * from this directory and from this transaction
3398 */
3399 ret = btrfs_next_leaf(root, path);
3400 if (ret == 1) {
3401 last_offset = (u64)-1;
3402 goto done;
3403 }
3404 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003405 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003406 last_offset = (u64)-1;
3407 goto done;
3408 }
3409 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3410 ret = overwrite_item(trans, log, dst_path,
3411 path->nodes[0], path->slots[0],
3412 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003413 if (ret)
3414 err = ret;
3415 else
3416 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003417 goto done;
3418 }
3419 }
3420done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003421 btrfs_release_path(path);
3422 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003423
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003424 if (err == 0) {
3425 *last_offset_ret = last_offset;
3426 /*
3427 * insert the log range keys to indicate where the log
3428 * is valid
3429 */
3430 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003431 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003432 if (ret)
3433 err = ret;
3434 }
3435 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003436}
3437
3438/*
3439 * logging directories is very similar to logging inodes, We find all the items
3440 * from the current transaction and write them to the log.
3441 *
3442 * The recovery code scans the directory in the subvolume, and if it finds a
3443 * key in the range logged that is not present in the log tree, then it means
3444 * that dir entry was unlinked during the transaction.
3445 *
3446 * In order for that scan to work, we must include one key smaller than
3447 * the smallest logged by this transaction and one key larger than the largest
3448 * key logged by this transaction.
3449 */
3450static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3451 struct btrfs_root *root, struct inode *inode,
3452 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003453 struct btrfs_path *dst_path,
3454 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003455{
3456 u64 min_key;
3457 u64 max_key;
3458 int ret;
3459 int key_type = BTRFS_DIR_ITEM_KEY;
3460
3461again:
3462 min_key = 0;
3463 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003464 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003465 ret = log_dir_items(trans, root, inode, path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003466 dst_path, key_type, ctx, min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003467 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003468 if (ret)
3469 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003470 if (max_key == (u64)-1)
3471 break;
3472 min_key = max_key + 1;
3473 }
3474
3475 if (key_type == BTRFS_DIR_ITEM_KEY) {
3476 key_type = BTRFS_DIR_INDEX_KEY;
3477 goto again;
3478 }
3479 return 0;
3480}
3481
3482/*
3483 * a helper function to drop items from the log before we relog an
3484 * inode. max_key_type indicates the highest item type to remove.
3485 * This cannot be run for file data extents because it does not
3486 * free the extents they point to.
3487 */
3488static int drop_objectid_items(struct btrfs_trans_handle *trans,
3489 struct btrfs_root *log,
3490 struct btrfs_path *path,
3491 u64 objectid, int max_key_type)
3492{
3493 int ret;
3494 struct btrfs_key key;
3495 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003496 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003497
3498 key.objectid = objectid;
3499 key.type = max_key_type;
3500 key.offset = (u64)-1;
3501
Chris Masond3977122009-01-05 21:25:51 -05003502 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003503 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003504 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003505 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003506 break;
3507
3508 if (path->slots[0] == 0)
3509 break;
3510
3511 path->slots[0]--;
3512 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3513 path->slots[0]);
3514
3515 if (found_key.objectid != objectid)
3516 break;
3517
Josef Bacik18ec90d2012-09-28 11:56:28 -04003518 found_key.offset = 0;
3519 found_key.type = 0;
3520 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3521 &start_slot);
3522
3523 ret = btrfs_del_items(trans, log, path, start_slot,
3524 path->slots[0] - start_slot + 1);
3525 /*
3526 * If start slot isn't 0 then we don't need to re-search, we've
3527 * found the last guy with the objectid in this tree.
3528 */
3529 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003530 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003531 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003532 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003533 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003534 if (ret > 0)
3535 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003536 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003537}
3538
Josef Bacik94edf4a2012-09-25 14:56:25 -04003539static void fill_inode_item(struct btrfs_trans_handle *trans,
3540 struct extent_buffer *leaf,
3541 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003542 struct inode *inode, int log_inode_only,
3543 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003544{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003545 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003546
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003547 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003548
3549 if (log_inode_only) {
3550 /* set the generation to zero so the recover code
3551 * can tell the difference between an logging
3552 * just to say 'this inode exists' and a logging
3553 * to say 'update this inode with these values'
3554 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003555 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003556 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003557 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003558 btrfs_set_token_inode_generation(leaf, item,
3559 BTRFS_I(inode)->generation,
3560 &token);
3561 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003562 }
3563
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003564 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3565 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3566 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3567 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3568
David Sterbaa937b972014-12-12 17:39:12 +01003569 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003570 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003571 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003572 inode->i_atime.tv_nsec, &token);
3573
David Sterbaa937b972014-12-12 17:39:12 +01003574 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003575 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003576 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003577 inode->i_mtime.tv_nsec, &token);
3578
David Sterbaa937b972014-12-12 17:39:12 +01003579 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003580 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003581 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003582 inode->i_ctime.tv_nsec, &token);
3583
3584 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3585 &token);
3586
3587 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3588 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3589 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3590 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3591 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003592}
3593
Josef Bacika95249b2012-10-11 16:17:34 -04003594static int log_inode_item(struct btrfs_trans_handle *trans,
3595 struct btrfs_root *log, struct btrfs_path *path,
3596 struct inode *inode)
3597{
3598 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003599 int ret;
3600
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003601 ret = btrfs_insert_empty_item(trans, log, path,
3602 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003603 sizeof(*inode_item));
3604 if (ret && ret != -EEXIST)
3605 return ret;
3606 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3607 struct btrfs_inode_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003608 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003609 btrfs_release_path(path);
3610 return 0;
3611}
3612
Chris Mason31ff1cd2008-09-11 16:17:57 -04003613static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003614 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003615 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003616 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003617 int start_slot, int nr, int inode_only,
3618 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003619{
3620 unsigned long src_offset;
3621 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003622 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003623 struct btrfs_file_extent_item *extent;
3624 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003625 struct extent_buffer *src = src_path->nodes[0];
3626 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003627 int ret;
3628 struct btrfs_key *ins_keys;
3629 u32 *ins_sizes;
3630 char *ins_data;
3631 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003632 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003633 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003634 bool has_extents = false;
Filipe Manana74121f72014-08-07 12:00:44 +01003635 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003636 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003637
3638 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003639
3640 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3641 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003642 if (!ins_data)
3643 return -ENOMEM;
3644
Josef Bacik16e75492013-10-22 12:18:51 -04003645 first_key.objectid = (u64)-1;
3646
Chris Mason31ff1cd2008-09-11 16:17:57 -04003647 ins_sizes = (u32 *)ins_data;
3648 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3649
3650 for (i = 0; i < nr; i++) {
3651 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3652 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3653 }
3654 ret = btrfs_insert_empty_items(trans, log, dst_path,
3655 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003656 if (ret) {
3657 kfree(ins_data);
3658 return ret;
3659 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003660
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003661 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003662 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3663 dst_path->slots[0]);
3664
3665 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3666
Josef Bacik16e75492013-10-22 12:18:51 -04003667 if ((i == (nr - 1)))
3668 last_key = ins_keys[i];
3669
Josef Bacik94edf4a2012-09-25 14:56:25 -04003670 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003671 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3672 dst_path->slots[0],
3673 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003674 fill_inode_item(trans, dst_path->nodes[0], inode_item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003675 inode, inode_only == LOG_INODE_EXISTS,
3676 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003677 } else {
3678 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3679 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003680 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003681
Josef Bacik16e75492013-10-22 12:18:51 -04003682 /*
3683 * We set need_find_last_extent here in case we know we were
3684 * processing other items and then walk into the first extent in
3685 * the inode. If we don't hit an extent then nothing changes,
3686 * we'll do the last search the next time around.
3687 */
3688 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3689 has_extents = true;
Filipe Manana74121f72014-08-07 12:00:44 +01003690 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003691 first_key = ins_keys[i];
3692 } else {
3693 need_find_last_extent = false;
3694 }
3695
Chris Mason31ff1cd2008-09-11 16:17:57 -04003696 /* take a reference on file data extents so that truncates
3697 * or deletes of this inode don't have to relog the inode
3698 * again
3699 */
David Sterba962a2982014-06-04 18:41:45 +02003700 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003701 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003702 int found_type;
3703 extent = btrfs_item_ptr(src, start_slot + i,
3704 struct btrfs_file_extent_item);
3705
liubo8e531cd2011-05-06 10:36:09 +08003706 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3707 continue;
3708
Chris Mason31ff1cd2008-09-11 16:17:57 -04003709 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003710 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003711 u64 ds, dl, cs, cl;
3712 ds = btrfs_file_extent_disk_bytenr(src,
3713 extent);
3714 /* ds == 0 is a hole */
3715 if (ds == 0)
3716 continue;
3717
3718 dl = btrfs_file_extent_disk_num_bytes(src,
3719 extent);
3720 cs = btrfs_file_extent_offset(src, extent);
3721 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003722 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003723 if (btrfs_file_extent_compression(src,
3724 extent)) {
3725 cs = 0;
3726 cl = dl;
3727 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003728
3729 ret = btrfs_lookup_csums_range(
3730 log->fs_info->csum_root,
3731 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003732 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003733 if (ret) {
3734 btrfs_release_path(dst_path);
3735 kfree(ins_data);
3736 return ret;
3737 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003738 }
3739 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003740 }
3741
3742 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003743 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003744 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003745
3746 /*
3747 * we have to do this after the loop above to avoid changing the
3748 * log tree while trying to change the log tree.
3749 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003750 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003751 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003752 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3753 struct btrfs_ordered_sum,
3754 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003755 if (!ret)
3756 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003757 list_del(&sums->list);
3758 kfree(sums);
3759 }
Josef Bacik16e75492013-10-22 12:18:51 -04003760
3761 if (!has_extents)
3762 return ret;
3763
Filipe Manana74121f72014-08-07 12:00:44 +01003764 if (need_find_last_extent && *last_extent == first_key.offset) {
3765 /*
3766 * We don't have any leafs between our current one and the one
3767 * we processed before that can have file extent items for our
3768 * inode (and have a generation number smaller than our current
3769 * transaction id).
3770 */
3771 need_find_last_extent = false;
3772 }
3773
Josef Bacik16e75492013-10-22 12:18:51 -04003774 /*
3775 * Because we use btrfs_search_forward we could skip leaves that were
3776 * not modified and then assume *last_extent is valid when it really
3777 * isn't. So back up to the previous leaf and read the end of the last
3778 * extent before we go and fill in holes.
3779 */
3780 if (need_find_last_extent) {
3781 u64 len;
3782
3783 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3784 if (ret < 0)
3785 return ret;
3786 if (ret)
3787 goto fill_holes;
3788 if (src_path->slots[0])
3789 src_path->slots[0]--;
3790 src = src_path->nodes[0];
3791 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3792 if (key.objectid != btrfs_ino(inode) ||
3793 key.type != BTRFS_EXTENT_DATA_KEY)
3794 goto fill_holes;
3795 extent = btrfs_item_ptr(src, src_path->slots[0],
3796 struct btrfs_file_extent_item);
3797 if (btrfs_file_extent_type(src, extent) ==
3798 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003799 len = btrfs_file_extent_inline_len(src,
3800 src_path->slots[0],
3801 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003802 *last_extent = ALIGN(key.offset + len,
3803 log->sectorsize);
3804 } else {
3805 len = btrfs_file_extent_num_bytes(src, extent);
3806 *last_extent = key.offset + len;
3807 }
3808 }
3809fill_holes:
3810 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3811 * things could have happened
3812 *
3813 * 1) A merge could have happened, so we could currently be on a leaf
3814 * that holds what we were copying in the first place.
3815 * 2) A split could have happened, and now not all of the items we want
3816 * are on the same leaf.
3817 *
3818 * So we need to adjust how we search for holes, we need to drop the
3819 * path and re-search for the first extent key we found, and then walk
3820 * forward until we hit the last one we copied.
3821 */
3822 if (need_find_last_extent) {
3823 /* btrfs_prev_leaf could return 1 without releasing the path */
3824 btrfs_release_path(src_path);
3825 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3826 src_path, 0, 0);
3827 if (ret < 0)
3828 return ret;
3829 ASSERT(ret == 0);
3830 src = src_path->nodes[0];
3831 i = src_path->slots[0];
3832 } else {
3833 i = start_slot;
3834 }
3835
3836 /*
3837 * Ok so here we need to go through and fill in any holes we may have
3838 * to make sure that holes are punched for those areas in case they had
3839 * extents previously.
3840 */
3841 while (!done) {
3842 u64 offset, len;
3843 u64 extent_end;
3844
3845 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3846 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3847 if (ret < 0)
3848 return ret;
3849 ASSERT(ret == 0);
3850 src = src_path->nodes[0];
3851 i = 0;
3852 }
3853
3854 btrfs_item_key_to_cpu(src, &key, i);
3855 if (!btrfs_comp_cpu_keys(&key, &last_key))
3856 done = true;
3857 if (key.objectid != btrfs_ino(inode) ||
3858 key.type != BTRFS_EXTENT_DATA_KEY) {
3859 i++;
3860 continue;
3861 }
3862 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3863 if (btrfs_file_extent_type(src, extent) ==
3864 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003865 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003866 extent_end = ALIGN(key.offset + len, log->sectorsize);
3867 } else {
3868 len = btrfs_file_extent_num_bytes(src, extent);
3869 extent_end = key.offset + len;
3870 }
3871 i++;
3872
3873 if (*last_extent == key.offset) {
3874 *last_extent = extent_end;
3875 continue;
3876 }
3877 offset = *last_extent;
3878 len = key.offset - *last_extent;
3879 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3880 offset, 0, 0, len, 0, len, 0,
3881 0, 0);
3882 if (ret)
3883 break;
Filipe Manana74121f72014-08-07 12:00:44 +01003884 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003885 }
3886 /*
3887 * Need to let the callers know we dropped the path so they should
3888 * re-search.
3889 */
3890 if (!ret && need_find_last_extent)
3891 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003892 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003893}
3894
Josef Bacik5dc562c2012-08-17 13:14:17 -04003895static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3896{
3897 struct extent_map *em1, *em2;
3898
3899 em1 = list_entry(a, struct extent_map, list);
3900 em2 = list_entry(b, struct extent_map, list);
3901
3902 if (em1->start < em2->start)
3903 return -1;
3904 else if (em1->start > em2->start)
3905 return 1;
3906 return 0;
3907}
3908
Filipe Manana8407f552014-09-05 15:14:39 +01003909static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3910 struct inode *inode,
3911 struct btrfs_root *root,
3912 const struct extent_map *em,
3913 const struct list_head *logged_list,
3914 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003915{
Josef Bacik2ab28f32012-10-12 15:27:49 -04003916 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01003917 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003918 u64 mod_start = em->mod_start;
3919 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003920 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003921 u64 csum_offset;
3922 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003923 LIST_HEAD(ordered_sums);
3924 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003925
Filipe Manana8407f552014-09-05 15:14:39 +01003926 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04003927
Filipe Manana8407f552014-09-05 15:14:39 +01003928 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3929 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003930 return 0;
3931
Josef Bacik2ab28f32012-10-12 15:27:49 -04003932 /*
Filipe Manana8407f552014-09-05 15:14:39 +01003933 * Wait far any ordered extent that covers our extent map. If it
3934 * finishes without an error, first check and see if our csums are on
3935 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04003936 */
Miao Xie827463c2014-01-14 20:31:51 +08003937 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003938 struct btrfs_ordered_sum *sum;
3939
3940 if (!mod_len)
3941 break;
3942
Josef Bacik2ab28f32012-10-12 15:27:49 -04003943 if (ordered->file_offset + ordered->len <= mod_start ||
3944 mod_start + mod_len <= ordered->file_offset)
3945 continue;
3946
Filipe Manana8407f552014-09-05 15:14:39 +01003947 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3948 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3949 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3950 const u64 start = ordered->file_offset;
3951 const u64 end = ordered->file_offset + ordered->len - 1;
3952
3953 WARN_ON(ordered->inode != inode);
3954 filemap_fdatawrite_range(inode->i_mapping, start, end);
3955 }
3956
3957 wait_event(ordered->wait,
3958 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3959 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3960
3961 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00003962 /*
3963 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3964 * i_mapping flags, so that the next fsync won't get
3965 * an outdated io error too.
3966 */
Miklos Szeredif0312212016-09-16 12:44:21 +02003967 filemap_check_errors(inode->i_mapping);
Filipe Manana8407f552014-09-05 15:14:39 +01003968 *ordered_io_error = true;
3969 break;
3970 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003971 /*
3972 * We are going to copy all the csums on this ordered extent, so
3973 * go ahead and adjust mod_start and mod_len in case this
3974 * ordered extent has already been logged.
3975 */
3976 if (ordered->file_offset > mod_start) {
3977 if (ordered->file_offset + ordered->len >=
3978 mod_start + mod_len)
3979 mod_len = ordered->file_offset - mod_start;
3980 /*
3981 * If we have this case
3982 *
3983 * |--------- logged extent ---------|
3984 * |----- ordered extent ----|
3985 *
3986 * Just don't mess with mod_start and mod_len, we'll
3987 * just end up logging more csums than we need and it
3988 * will be ok.
3989 */
3990 } else {
3991 if (ordered->file_offset + ordered->len <
3992 mod_start + mod_len) {
3993 mod_len = (mod_start + mod_len) -
3994 (ordered->file_offset + ordered->len);
3995 mod_start = ordered->file_offset +
3996 ordered->len;
3997 } else {
3998 mod_len = 0;
3999 }
4000 }
4001
Filipe Manana8407f552014-09-05 15:14:39 +01004002 if (skip_csum)
4003 continue;
4004
Josef Bacik2ab28f32012-10-12 15:27:49 -04004005 /*
4006 * To keep us from looping for the above case of an ordered
4007 * extent that falls inside of the logged extent.
4008 */
4009 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4010 &ordered->flags))
4011 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004012
Josef Bacik2ab28f32012-10-12 15:27:49 -04004013 list_for_each_entry(sum, &ordered->list, list) {
4014 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08004015 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01004016 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004017 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004018 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004019
Filipe Manana8407f552014-09-05 15:14:39 +01004020 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04004021 return ret;
4022
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004023 if (em->compress_type) {
4024 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01004025 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00004026 } else {
4027 csum_offset = mod_start - em->start;
4028 csum_len = mod_len;
4029 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04004030
Josef Bacik70c8a912012-10-11 16:54:30 -04004031 /* block start is already adjusted for the file extent offset. */
4032 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
4033 em->block_start + csum_offset,
4034 em->block_start + csum_offset +
4035 csum_len - 1, &ordered_sums, 0);
4036 if (ret)
4037 return ret;
4038
4039 while (!list_empty(&ordered_sums)) {
4040 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4041 struct btrfs_ordered_sum,
4042 list);
4043 if (!ret)
4044 ret = btrfs_csum_file_blocks(trans, log, sums);
4045 list_del(&sums->list);
4046 kfree(sums);
4047 }
4048
4049 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004050}
4051
Filipe Manana8407f552014-09-05 15:14:39 +01004052static int log_one_extent(struct btrfs_trans_handle *trans,
4053 struct inode *inode, struct btrfs_root *root,
4054 const struct extent_map *em,
4055 struct btrfs_path *path,
4056 const struct list_head *logged_list,
4057 struct btrfs_log_ctx *ctx)
4058{
4059 struct btrfs_root *log = root->log_root;
4060 struct btrfs_file_extent_item *fi;
4061 struct extent_buffer *leaf;
4062 struct btrfs_map_token token;
4063 struct btrfs_key key;
4064 u64 extent_offset = em->start - em->orig_start;
4065 u64 block_len;
4066 int ret;
4067 int extent_inserted = 0;
4068 bool ordered_io_err = false;
4069
4070 ret = wait_ordered_extents(trans, inode, root, em, logged_list,
4071 &ordered_io_err);
4072 if (ret)
4073 return ret;
4074
4075 if (ordered_io_err) {
4076 ctx->io_err = -EIO;
4077 return 0;
4078 }
4079
4080 btrfs_init_map_token(&token);
4081
4082 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
4083 em->start + em->len, NULL, 0, 1,
4084 sizeof(*fi), &extent_inserted);
4085 if (ret)
4086 return ret;
4087
4088 if (!extent_inserted) {
4089 key.objectid = btrfs_ino(inode);
4090 key.type = BTRFS_EXTENT_DATA_KEY;
4091 key.offset = em->start;
4092
4093 ret = btrfs_insert_empty_item(trans, log, path, &key,
4094 sizeof(*fi));
4095 if (ret)
4096 return ret;
4097 }
4098 leaf = path->nodes[0];
4099 fi = btrfs_item_ptr(leaf, path->slots[0],
4100 struct btrfs_file_extent_item);
4101
Josef Bacik50d9aa92014-11-21 14:52:38 -05004102 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004103 &token);
4104 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4105 btrfs_set_token_file_extent_type(leaf, fi,
4106 BTRFS_FILE_EXTENT_PREALLOC,
4107 &token);
4108 else
4109 btrfs_set_token_file_extent_type(leaf, fi,
4110 BTRFS_FILE_EXTENT_REG,
4111 &token);
4112
4113 block_len = max(em->block_len, em->orig_block_len);
4114 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4115 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4116 em->block_start,
4117 &token);
4118 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4119 &token);
4120 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4121 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4122 em->block_start -
4123 extent_offset, &token);
4124 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4125 &token);
4126 } else {
4127 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4128 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4129 &token);
4130 }
4131
4132 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4133 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4134 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4135 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4136 &token);
4137 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4138 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4139 btrfs_mark_buffer_dirty(leaf);
4140
4141 btrfs_release_path(path);
4142
4143 return ret;
4144}
4145
Josef Bacik5dc562c2012-08-17 13:14:17 -04004146static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4147 struct btrfs_root *root,
4148 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004149 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004150 struct list_head *logged_list,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004151 struct btrfs_log_ctx *ctx,
4152 const u64 start,
4153 const u64 end)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004154{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004155 struct extent_map *em, *n;
4156 struct list_head extents;
4157 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4158 u64 test_gen;
4159 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004160 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004161
4162 INIT_LIST_HEAD(&extents);
4163
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004164 down_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004165 write_lock(&tree->lock);
4166 test_gen = root->fs_info->last_trans_committed;
4167
4168 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4169 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004170
4171 /*
4172 * Just an arbitrary number, this can be really CPU intensive
4173 * once we start getting a lot of extents, and really once we
4174 * have a bunch of extents we just want to commit since it will
4175 * be faster.
4176 */
4177 if (++num > 32768) {
4178 list_del_init(&tree->modified_extents);
4179 ret = -EFBIG;
4180 goto process;
4181 }
4182
Josef Bacik5dc562c2012-08-17 13:14:17 -04004183 if (em->generation <= test_gen)
4184 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004185 /* Need a ref to keep it from getting evicted from cache */
4186 atomic_inc(&em->refs);
4187 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004188 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004189 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004190 }
4191
4192 list_sort(NULL, &extents, extent_cmp);
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004193 btrfs_get_logged_extents(inode, logged_list, start, end);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004194 /*
4195 * Some ordered extents started by fsync might have completed
4196 * before we could collect them into the list logged_list, which
4197 * means they're gone, not in our logged_list nor in the inode's
4198 * ordered tree. We want the application/user space to know an
4199 * error happened while attempting to persist file data so that
4200 * it can take proper action. If such error happened, we leave
4201 * without writing to the log tree and the fsync must report the
4202 * file data write error and not commit the current transaction.
4203 */
Miklos Szeredif0312212016-09-16 12:44:21 +02004204 ret = filemap_check_errors(inode->i_mapping);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004205 if (ret)
4206 ctx->io_err = ret;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004207process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004208 while (!list_empty(&extents)) {
4209 em = list_entry(extents.next, struct extent_map, list);
4210
4211 list_del_init(&em->list);
4212
4213 /*
4214 * If we had an error we just need to delete everybody from our
4215 * private list.
4216 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004217 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004218 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004219 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004220 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004221 }
4222
4223 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004224
Filipe Manana8407f552014-09-05 15:14:39 +01004225 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4226 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004227 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004228 clear_em_logging(tree, em);
4229 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004230 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004231 WARN_ON(!list_empty(&extents));
4232 write_unlock(&tree->lock);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01004233 up_write(&BTRFS_I(inode)->dio_sem);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004234
Josef Bacik5dc562c2012-08-17 13:14:17 -04004235 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004236 return ret;
4237}
4238
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004239static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
4240 struct btrfs_path *path, u64 *size_ret)
4241{
4242 struct btrfs_key key;
4243 int ret;
4244
4245 key.objectid = btrfs_ino(inode);
4246 key.type = BTRFS_INODE_ITEM_KEY;
4247 key.offset = 0;
4248
4249 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4250 if (ret < 0) {
4251 return ret;
4252 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004253 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004254 } else {
4255 struct btrfs_inode_item *item;
4256
4257 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4258 struct btrfs_inode_item);
4259 *size_ret = btrfs_inode_size(path->nodes[0], item);
4260 }
4261
4262 btrfs_release_path(path);
4263 return 0;
4264}
4265
Filipe Manana36283bf2015-06-20 00:44:51 +01004266/*
4267 * At the moment we always log all xattrs. This is to figure out at log replay
4268 * time which xattrs must have their deletion replayed. If a xattr is missing
4269 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4270 * because if a xattr is deleted, the inode is fsynced and a power failure
4271 * happens, causing the log to be replayed the next time the fs is mounted,
4272 * we want the xattr to not exist anymore (same behaviour as other filesystems
4273 * with a journal, ext3/4, xfs, f2fs, etc).
4274 */
4275static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4276 struct btrfs_root *root,
4277 struct inode *inode,
4278 struct btrfs_path *path,
4279 struct btrfs_path *dst_path)
4280{
4281 int ret;
4282 struct btrfs_key key;
4283 const u64 ino = btrfs_ino(inode);
4284 int ins_nr = 0;
4285 int start_slot = 0;
4286
4287 key.objectid = ino;
4288 key.type = BTRFS_XATTR_ITEM_KEY;
4289 key.offset = 0;
4290
4291 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4292 if (ret < 0)
4293 return ret;
4294
4295 while (true) {
4296 int slot = path->slots[0];
4297 struct extent_buffer *leaf = path->nodes[0];
4298 int nritems = btrfs_header_nritems(leaf);
4299
4300 if (slot >= nritems) {
4301 if (ins_nr > 0) {
4302 u64 last_extent = 0;
4303
4304 ret = copy_items(trans, inode, dst_path, path,
4305 &last_extent, start_slot,
4306 ins_nr, 1, 0);
4307 /* can't be 1, extent items aren't processed */
4308 ASSERT(ret <= 0);
4309 if (ret < 0)
4310 return ret;
4311 ins_nr = 0;
4312 }
4313 ret = btrfs_next_leaf(root, path);
4314 if (ret < 0)
4315 return ret;
4316 else if (ret > 0)
4317 break;
4318 continue;
4319 }
4320
4321 btrfs_item_key_to_cpu(leaf, &key, slot);
4322 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4323 break;
4324
4325 if (ins_nr == 0)
4326 start_slot = slot;
4327 ins_nr++;
4328 path->slots[0]++;
4329 cond_resched();
4330 }
4331 if (ins_nr > 0) {
4332 u64 last_extent = 0;
4333
4334 ret = copy_items(trans, inode, dst_path, path,
4335 &last_extent, start_slot,
4336 ins_nr, 1, 0);
4337 /* can't be 1, extent items aren't processed */
4338 ASSERT(ret <= 0);
4339 if (ret < 0)
4340 return ret;
4341 }
4342
4343 return 0;
4344}
4345
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004346/*
4347 * If the no holes feature is enabled we need to make sure any hole between the
4348 * last extent and the i_size of our inode is explicitly marked in the log. This
4349 * is to make sure that doing something like:
4350 *
4351 * 1) create file with 128Kb of data
4352 * 2) truncate file to 64Kb
4353 * 3) truncate file to 256Kb
4354 * 4) fsync file
4355 * 5) <crash/power failure>
4356 * 6) mount fs and trigger log replay
4357 *
4358 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4359 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4360 * file correspond to a hole. The presence of explicit holes in a log tree is
4361 * what guarantees that log replay will remove/adjust file extent items in the
4362 * fs/subvol tree.
4363 *
4364 * Here we do not need to care about holes between extents, that is already done
4365 * by copy_items(). We also only need to do this in the full sync path, where we
4366 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4367 * lookup the list of modified extent maps and if any represents a hole, we
4368 * insert a corresponding extent representing a hole in the log tree.
4369 */
4370static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4371 struct btrfs_root *root,
4372 struct inode *inode,
4373 struct btrfs_path *path)
4374{
4375 int ret;
4376 struct btrfs_key key;
4377 u64 hole_start;
4378 u64 hole_size;
4379 struct extent_buffer *leaf;
4380 struct btrfs_root *log = root->log_root;
4381 const u64 ino = btrfs_ino(inode);
4382 const u64 i_size = i_size_read(inode);
4383
4384 if (!btrfs_fs_incompat(root->fs_info, NO_HOLES))
4385 return 0;
4386
4387 key.objectid = ino;
4388 key.type = BTRFS_EXTENT_DATA_KEY;
4389 key.offset = (u64)-1;
4390
4391 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4392 ASSERT(ret != 0);
4393 if (ret < 0)
4394 return ret;
4395
4396 ASSERT(path->slots[0] > 0);
4397 path->slots[0]--;
4398 leaf = path->nodes[0];
4399 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4400
4401 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4402 /* inode does not have any extents */
4403 hole_start = 0;
4404 hole_size = i_size;
4405 } else {
4406 struct btrfs_file_extent_item *extent;
4407 u64 len;
4408
4409 /*
4410 * If there's an extent beyond i_size, an explicit hole was
4411 * already inserted by copy_items().
4412 */
4413 if (key.offset >= i_size)
4414 return 0;
4415
4416 extent = btrfs_item_ptr(leaf, path->slots[0],
4417 struct btrfs_file_extent_item);
4418
4419 if (btrfs_file_extent_type(leaf, extent) ==
4420 BTRFS_FILE_EXTENT_INLINE) {
4421 len = btrfs_file_extent_inline_len(leaf,
4422 path->slots[0],
4423 extent);
4424 ASSERT(len == i_size);
4425 return 0;
4426 }
4427
4428 len = btrfs_file_extent_num_bytes(leaf, extent);
4429 /* Last extent goes beyond i_size, no need to log a hole. */
4430 if (key.offset + len > i_size)
4431 return 0;
4432 hole_start = key.offset + len;
4433 hole_size = i_size - hole_start;
4434 }
4435 btrfs_release_path(path);
4436
4437 /* Last extent ends at i_size. */
4438 if (hole_size == 0)
4439 return 0;
4440
4441 hole_size = ALIGN(hole_size, root->sectorsize);
4442 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4443 hole_size, 0, hole_size, 0, 0, 0);
4444 return ret;
4445}
4446
Filipe Manana56f23fd2016-03-30 23:37:21 +01004447/*
4448 * When we are logging a new inode X, check if it doesn't have a reference that
4449 * matches the reference from some other inode Y created in a past transaction
4450 * and that was renamed in the current transaction. If we don't do this, then at
4451 * log replay time we can lose inode Y (and all its files if it's a directory):
4452 *
4453 * mkdir /mnt/x
4454 * echo "hello world" > /mnt/x/foobar
4455 * sync
4456 * mv /mnt/x /mnt/y
4457 * mkdir /mnt/x # or touch /mnt/x
4458 * xfs_io -c fsync /mnt/x
4459 * <power fail>
4460 * mount fs, trigger log replay
4461 *
4462 * After the log replay procedure, we would lose the first directory and all its
4463 * files (file foobar).
4464 * For the case where inode Y is not a directory we simply end up losing it:
4465 *
4466 * echo "123" > /mnt/foo
4467 * sync
4468 * mv /mnt/foo /mnt/bar
4469 * echo "abc" > /mnt/foo
4470 * xfs_io -c fsync /mnt/foo
4471 * <power fail>
4472 *
4473 * We also need this for cases where a snapshot entry is replaced by some other
4474 * entry (file or directory) otherwise we end up with an unreplayable log due to
4475 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4476 * if it were a regular entry:
4477 *
4478 * mkdir /mnt/x
4479 * btrfs subvolume snapshot /mnt /mnt/x/snap
4480 * btrfs subvolume delete /mnt/x/snap
4481 * rmdir /mnt/x
4482 * mkdir /mnt/x
4483 * fsync /mnt/x or fsync some new file inside it
4484 * <power fail>
4485 *
4486 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4487 * the same transaction.
4488 */
4489static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4490 const int slot,
4491 const struct btrfs_key *key,
Filipe Manana44f714d2016-06-06 16:11:13 +01004492 struct inode *inode,
4493 u64 *other_ino)
Filipe Manana56f23fd2016-03-30 23:37:21 +01004494{
4495 int ret;
4496 struct btrfs_path *search_path;
4497 char *name = NULL;
4498 u32 name_len = 0;
4499 u32 item_size = btrfs_item_size_nr(eb, slot);
4500 u32 cur_offset = 0;
4501 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4502
4503 search_path = btrfs_alloc_path();
4504 if (!search_path)
4505 return -ENOMEM;
4506 search_path->search_commit_root = 1;
4507 search_path->skip_locking = 1;
4508
4509 while (cur_offset < item_size) {
4510 u64 parent;
4511 u32 this_name_len;
4512 u32 this_len;
4513 unsigned long name_ptr;
4514 struct btrfs_dir_item *di;
4515
4516 if (key->type == BTRFS_INODE_REF_KEY) {
4517 struct btrfs_inode_ref *iref;
4518
4519 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4520 parent = key->offset;
4521 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4522 name_ptr = (unsigned long)(iref + 1);
4523 this_len = sizeof(*iref) + this_name_len;
4524 } else {
4525 struct btrfs_inode_extref *extref;
4526
4527 extref = (struct btrfs_inode_extref *)(ptr +
4528 cur_offset);
4529 parent = btrfs_inode_extref_parent(eb, extref);
4530 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4531 name_ptr = (unsigned long)&extref->name;
4532 this_len = sizeof(*extref) + this_name_len;
4533 }
4534
4535 if (this_name_len > name_len) {
4536 char *new_name;
4537
4538 new_name = krealloc(name, this_name_len, GFP_NOFS);
4539 if (!new_name) {
4540 ret = -ENOMEM;
4541 goto out;
4542 }
4543 name_len = this_name_len;
4544 name = new_name;
4545 }
4546
4547 read_extent_buffer(eb, name, name_ptr, this_name_len);
4548 di = btrfs_lookup_dir_item(NULL, BTRFS_I(inode)->root,
4549 search_path, parent,
4550 name, this_name_len, 0);
4551 if (di && !IS_ERR(di)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004552 struct btrfs_key di_key;
4553
4554 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4555 di, &di_key);
4556 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4557 ret = 1;
4558 *other_ino = di_key.objectid;
4559 } else {
4560 ret = -EAGAIN;
4561 }
Filipe Manana56f23fd2016-03-30 23:37:21 +01004562 goto out;
4563 } else if (IS_ERR(di)) {
4564 ret = PTR_ERR(di);
4565 goto out;
4566 }
4567 btrfs_release_path(search_path);
4568
4569 cur_offset += this_len;
4570 }
4571 ret = 0;
4572out:
4573 btrfs_free_path(search_path);
4574 kfree(name);
4575 return ret;
4576}
4577
Chris Masone02119d2008-09-05 16:13:11 -04004578/* log a single inode in the tree log.
4579 * At least one parent directory for this inode must exist in the tree
4580 * or be logged already.
4581 *
4582 * Any items from this inode changed by the current transaction are copied
4583 * to the log tree. An extra reference is taken on any extents in this
4584 * file, allowing us to avoid a whole pile of corner cases around logging
4585 * blocks that have been removed from the tree.
4586 *
4587 * See LOG_INODE_ALL and related defines for a description of what inode_only
4588 * does.
4589 *
4590 * This handles both files and directories.
4591 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004592static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004593 struct btrfs_root *root, struct inode *inode,
4594 int inode_only,
4595 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004596 const loff_t end,
4597 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004598{
4599 struct btrfs_path *path;
4600 struct btrfs_path *dst_path;
4601 struct btrfs_key min_key;
4602 struct btrfs_key max_key;
4603 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004604 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08004605 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004606 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004607 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004608 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004609 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004610 int ins_start_slot = 0;
4611 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004612 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08004613 u64 ino = btrfs_ino(inode);
Filipe Manana49dae1b2014-09-06 22:34:39 +01004614 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004615 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004616 bool need_log_inode_item = true;
Chris Masone02119d2008-09-05 16:13:11 -04004617
Chris Masone02119d2008-09-05 16:13:11 -04004618 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004619 if (!path)
4620 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004621 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004622 if (!dst_path) {
4623 btrfs_free_path(path);
4624 return -ENOMEM;
4625 }
Chris Masone02119d2008-09-05 16:13:11 -04004626
Li Zefan33345d012011-04-20 10:31:50 +08004627 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004628 min_key.type = BTRFS_INODE_ITEM_KEY;
4629 min_key.offset = 0;
4630
Li Zefan33345d012011-04-20 10:31:50 +08004631 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004632
Chris Mason12fcfd22009-03-24 10:24:20 -04004633
Josef Bacik5dc562c2012-08-17 13:14:17 -04004634 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00004635 if (S_ISDIR(inode->i_mode) ||
4636 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4637 &BTRFS_I(inode)->runtime_flags) &&
Liu Bo67312122016-11-30 16:20:25 -08004638 inode_only >= LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004639 max_key.type = BTRFS_XATTR_ITEM_KEY;
4640 else
4641 max_key.type = (u8)-1;
4642 max_key.offset = (u64)-1;
4643
Filipe Manana2c2c4522015-01-13 16:40:04 +00004644 /*
4645 * Only run delayed items if we are a dir or a new file.
4646 * Otherwise commit the delayed inode only, which is needed in
4647 * order for the log replay code to mark inodes for link count
4648 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4649 */
Josef Bacik94edf4a2012-09-25 14:56:25 -04004650 if (S_ISDIR(inode->i_mode) ||
Filipe Manana2c2c4522015-01-13 16:40:04 +00004651 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
Josef Bacik94edf4a2012-09-25 14:56:25 -04004652 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004653 else
4654 ret = btrfs_commit_inode_delayed_inode(inode);
4655
4656 if (ret) {
4657 btrfs_free_path(path);
4658 btrfs_free_path(dst_path);
4659 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004660 }
4661
Liu Bo67312122016-11-30 16:20:25 -08004662 if (inode_only == LOG_OTHER_INODE) {
4663 inode_only = LOG_INODE_EXISTS;
4664 mutex_lock_nested(&BTRFS_I(inode)->log_mutex,
4665 SINGLE_DEPTH_NESTING);
4666 } else {
4667 mutex_lock(&BTRFS_I(inode)->log_mutex);
4668 }
Chris Masone02119d2008-09-05 16:13:11 -04004669
Filipe Manana5e33a2b2016-02-25 23:19:38 +00004670 /*
Chris Masone02119d2008-09-05 16:13:11 -04004671 * a brute force approach to making sure we get the most uptodate
4672 * copies of everything.
4673 */
4674 if (S_ISDIR(inode->i_mode)) {
4675 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4676
Filipe Manana4f764e52015-02-23 19:53:35 +00004677 if (inode_only == LOG_INODE_EXISTS)
4678 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004679 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004680 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004681 if (inode_only == LOG_INODE_EXISTS) {
4682 /*
4683 * Make sure the new inode item we write to the log has
4684 * the same isize as the current one (if it exists).
4685 * This is necessary to prevent data loss after log
4686 * replay, and also to prevent doing a wrong expanding
4687 * truncate - for e.g. create file, write 4K into offset
4688 * 0, fsync, write 4K into offset 4096, add hard link,
4689 * fsync some other file (to sync log), power fail - if
4690 * we use the inode's current i_size, after log replay
4691 * we get a 8Kb file, with the last 4Kb extent as a hole
4692 * (zeroes), as if an expanding truncate happened,
4693 * instead of getting a file of 4Kb only.
4694 */
4695 err = logged_inode_size(log, inode, path,
4696 &logged_isize);
4697 if (err)
4698 goto out_unlock;
4699 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004700 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4701 &BTRFS_I(inode)->runtime_flags)) {
4702 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004703 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004704 ret = drop_objectid_items(trans, log, path, ino,
4705 max_key.type);
4706 } else {
4707 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4708 &BTRFS_I(inode)->runtime_flags);
4709 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4710 &BTRFS_I(inode)->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004711 while(1) {
4712 ret = btrfs_truncate_inode_items(trans,
4713 log, inode, 0, 0);
4714 if (ret != -EAGAIN)
4715 break;
4716 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004717 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004718 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4719 &BTRFS_I(inode)->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004720 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004721 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004722 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004723 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004724 ret = drop_objectid_items(trans, log, path, ino,
4725 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004726 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004727 if (inode_only == LOG_INODE_ALL)
4728 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004729 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004730 }
Josef Bacika95249b2012-10-11 16:17:34 -04004731
Chris Masone02119d2008-09-05 16:13:11 -04004732 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004733 if (ret) {
4734 err = ret;
4735 goto out_unlock;
4736 }
Chris Masone02119d2008-09-05 16:13:11 -04004737
Chris Masond3977122009-01-05 21:25:51 -05004738 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004739 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004740 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004741 path, trans->transid);
Liu Bofb770ae2016-07-05 12:10:14 -07004742 if (ret < 0) {
4743 err = ret;
4744 goto out_unlock;
4745 }
Chris Masone02119d2008-09-05 16:13:11 -04004746 if (ret != 0)
4747 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004748again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004749 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004750 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004751 break;
4752 if (min_key.type > max_key.type)
4753 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004754
Filipe Mananae4545de2015-06-17 12:49:23 +01004755 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4756 need_log_inode_item = false;
4757
Filipe Manana56f23fd2016-03-30 23:37:21 +01004758 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4759 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
4760 BTRFS_I(inode)->generation == trans->transid) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004761 u64 other_ino = 0;
4762
Filipe Manana56f23fd2016-03-30 23:37:21 +01004763 ret = btrfs_check_ref_name_override(path->nodes[0],
4764 path->slots[0],
Filipe Manana44f714d2016-06-06 16:11:13 +01004765 &min_key, inode,
4766 &other_ino);
Filipe Manana56f23fd2016-03-30 23:37:21 +01004767 if (ret < 0) {
4768 err = ret;
4769 goto out_unlock;
Filipe Manana28a23592016-08-23 21:13:51 +01004770 } else if (ret > 0 && ctx &&
4771 other_ino != btrfs_ino(ctx->inode)) {
Filipe Manana44f714d2016-06-06 16:11:13 +01004772 struct btrfs_key inode_key;
4773 struct inode *other_inode;
4774
4775 if (ins_nr > 0) {
4776 ins_nr++;
4777 } else {
4778 ins_nr = 1;
4779 ins_start_slot = path->slots[0];
4780 }
4781 ret = copy_items(trans, inode, dst_path, path,
4782 &last_extent, ins_start_slot,
4783 ins_nr, inode_only,
4784 logged_isize);
4785 if (ret < 0) {
4786 err = ret;
4787 goto out_unlock;
4788 }
4789 ins_nr = 0;
4790 btrfs_release_path(path);
4791 inode_key.objectid = other_ino;
4792 inode_key.type = BTRFS_INODE_ITEM_KEY;
4793 inode_key.offset = 0;
4794 other_inode = btrfs_iget(root->fs_info->sb,
4795 &inode_key, root,
4796 NULL);
4797 /*
4798 * If the other inode that had a conflicting dir
4799 * entry was deleted in the current transaction,
4800 * we don't need to do more work nor fallback to
4801 * a transaction commit.
4802 */
4803 if (IS_ERR(other_inode) &&
4804 PTR_ERR(other_inode) == -ENOENT) {
4805 goto next_key;
4806 } else if (IS_ERR(other_inode)) {
4807 err = PTR_ERR(other_inode);
4808 goto out_unlock;
4809 }
4810 /*
4811 * We are safe logging the other inode without
4812 * acquiring its i_mutex as long as we log with
4813 * the LOG_INODE_EXISTS mode. We're safe against
4814 * concurrent renames of the other inode as well
4815 * because during a rename we pin the log and
4816 * update the log with the new name before we
4817 * unpin it.
4818 */
4819 err = btrfs_log_inode(trans, root, other_inode,
Liu Bo67312122016-11-30 16:20:25 -08004820 LOG_OTHER_INODE,
Filipe Manana44f714d2016-06-06 16:11:13 +01004821 0, LLONG_MAX, ctx);
4822 iput(other_inode);
4823 if (err)
4824 goto out_unlock;
4825 else
4826 goto next_key;
Filipe Manana56f23fd2016-03-30 23:37:21 +01004827 }
4828 }
4829
Filipe Manana36283bf2015-06-20 00:44:51 +01004830 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
4831 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
4832 if (ins_nr == 0)
4833 goto next_slot;
4834 ret = copy_items(trans, inode, dst_path, path,
4835 &last_extent, ins_start_slot,
4836 ins_nr, inode_only, logged_isize);
4837 if (ret < 0) {
4838 err = ret;
4839 goto out_unlock;
4840 }
4841 ins_nr = 0;
4842 if (ret) {
4843 btrfs_release_path(path);
4844 continue;
4845 }
4846 goto next_slot;
4847 }
4848
Chris Masone02119d2008-09-05 16:13:11 -04004849 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004850 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4851 ins_nr++;
4852 goto next_slot;
4853 } else if (!ins_nr) {
4854 ins_start_slot = path->slots[0];
4855 ins_nr = 1;
4856 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004857 }
4858
Josef Bacik16e75492013-10-22 12:18:51 -04004859 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004860 ins_start_slot, ins_nr, inode_only,
4861 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004862 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004863 err = ret;
4864 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02004865 }
4866 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04004867 ins_nr = 0;
4868 btrfs_release_path(path);
4869 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004870 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004871 ins_nr = 1;
4872 ins_start_slot = path->slots[0];
4873next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004874
Chris Mason3a5f1d42008-09-11 15:53:37 -04004875 nritems = btrfs_header_nritems(path->nodes[0]);
4876 path->slots[0]++;
4877 if (path->slots[0] < nritems) {
4878 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4879 path->slots[0]);
4880 goto again;
4881 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004882 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004883 ret = copy_items(trans, inode, dst_path, path,
4884 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004885 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004886 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004887 err = ret;
4888 goto out_unlock;
4889 }
Josef Bacik16e75492013-10-22 12:18:51 -04004890 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004891 ins_nr = 0;
4892 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004893 btrfs_release_path(path);
Filipe Manana44f714d2016-06-06 16:11:13 +01004894next_key:
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004895 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004896 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004897 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004898 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004899 min_key.offset = 0;
4900 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004901 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004902 }
Chris Masone02119d2008-09-05 16:13:11 -04004903 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004904 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004905 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004906 ins_start_slot, ins_nr, inode_only,
4907 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004908 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004909 err = ret;
4910 goto out_unlock;
4911 }
Josef Bacik16e75492013-10-22 12:18:51 -04004912 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004913 ins_nr = 0;
4914 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004915
Filipe Manana36283bf2015-06-20 00:44:51 +01004916 btrfs_release_path(path);
4917 btrfs_release_path(dst_path);
4918 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
4919 if (err)
4920 goto out_unlock;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004921 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
4922 btrfs_release_path(path);
4923 btrfs_release_path(dst_path);
4924 err = btrfs_log_trailing_hole(trans, root, inode, path);
4925 if (err)
4926 goto out_unlock;
4927 }
Josef Bacika95249b2012-10-11 16:17:34 -04004928log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004929 btrfs_release_path(path);
4930 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01004931 if (need_log_inode_item) {
4932 err = log_inode_item(trans, log, dst_path, inode);
4933 if (err)
4934 goto out_unlock;
4935 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004936 if (fast_search) {
Miao Xie827463c2014-01-14 20:31:51 +08004937 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Mananade0ee0e2016-01-21 10:17:54 +00004938 &logged_list, ctx, start, end);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004939 if (ret) {
4940 err = ret;
4941 goto out_unlock;
4942 }
Josef Bacikd006a042013-11-12 20:54:09 -05004943 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004944 struct extent_map *em, *n;
4945
Filipe Manana49dae1b2014-09-06 22:34:39 +01004946 write_lock(&em_tree->lock);
4947 /*
4948 * We can't just remove every em if we're called for a ranged
4949 * fsync - that is, one that doesn't cover the whole possible
4950 * file range (0 to LLONG_MAX). This is because we can have
4951 * em's that fall outside the range we're logging and therefore
4952 * their ordered operations haven't completed yet
4953 * (btrfs_finish_ordered_io() not invoked yet). This means we
4954 * didn't get their respective file extent item in the fs/subvol
4955 * tree yet, and need to let the next fast fsync (one which
4956 * consults the list of modified extent maps) find the em so
4957 * that it logs a matching file extent item and waits for the
4958 * respective ordered operation to complete (if it's still
4959 * running).
4960 *
4961 * Removing every em outside the range we're logging would make
4962 * the next fast fsync not log their matching file extent items,
4963 * therefore making us lose data after a log replay.
4964 */
4965 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4966 list) {
4967 const u64 mod_end = em->mod_start + em->mod_len - 1;
4968
4969 if (em->mod_start >= start && mod_end <= end)
4970 list_del_init(&em->list);
4971 }
4972 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004973 }
4974
Chris Mason9623f9a2008-09-11 17:42:42 -04004975 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004976 ret = log_directory_changes(trans, root, inode, path, dst_path,
4977 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004978 if (ret) {
4979 err = ret;
4980 goto out_unlock;
4981 }
Chris Masone02119d2008-09-05 16:13:11 -04004982 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01004983
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004984 spin_lock(&BTRFS_I(inode)->lock);
Filipe Manana125c4cf92014-09-11 21:22:14 +01004985 BTRFS_I(inode)->logged_trans = trans->transid;
4986 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004987 spin_unlock(&BTRFS_I(inode)->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004988out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08004989 if (unlikely(err))
4990 btrfs_put_logged_extents(&logged_list);
4991 else
4992 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04004993 mutex_unlock(&BTRFS_I(inode)->log_mutex);
4994
4995 btrfs_free_path(path);
4996 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004997 return err;
Chris Masone02119d2008-09-05 16:13:11 -04004998}
4999
Chris Mason12fcfd22009-03-24 10:24:20 -04005000/*
Filipe Manana2be63d52016-02-12 11:34:23 +00005001 * Check if we must fallback to a transaction commit when logging an inode.
5002 * This must be called after logging the inode and is used only in the context
5003 * when fsyncing an inode requires the need to log some other inode - in which
5004 * case we can't lock the i_mutex of each other inode we need to log as that
5005 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5006 * log inodes up or down in the hierarchy) or rename operations for example. So
5007 * we take the log_mutex of the inode after we have logged it and then check for
5008 * its last_unlink_trans value - this is safe because any task setting
5009 * last_unlink_trans must take the log_mutex and it must do this before it does
5010 * the actual unlink operation, so if we do this check before a concurrent task
5011 * sets last_unlink_trans it means we've logged a consistent version/state of
5012 * all the inode items, otherwise we are not sure and must do a transaction
Nicholas D Steeves01327612016-05-19 21:18:45 -04005013 * commit (the concurrent task might have only updated last_unlink_trans before
Filipe Manana2be63d52016-02-12 11:34:23 +00005014 * we logged the inode or it might have also done the unlink).
5015 */
5016static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5017 struct inode *inode)
5018{
5019 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
5020 bool ret = false;
5021
5022 mutex_lock(&BTRFS_I(inode)->log_mutex);
5023 if (BTRFS_I(inode)->last_unlink_trans > fs_info->last_trans_committed) {
5024 /*
5025 * Make sure any commits to the log are forced to be full
5026 * commits.
5027 */
5028 btrfs_set_log_full_commit(fs_info, trans);
5029 ret = true;
5030 }
5031 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5032
5033 return ret;
5034}
5035
5036/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005037 * follow the dentry parent pointers up the chain and see if any
5038 * of the directories in it require a full commit before they can
5039 * be logged. Returns zero if nothing special needs to be done or 1 if
5040 * a full commit is required.
5041 */
5042static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5043 struct inode *inode,
5044 struct dentry *parent,
5045 struct super_block *sb,
5046 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04005047{
Chris Mason12fcfd22009-03-24 10:24:20 -04005048 int ret = 0;
Josef Bacik6a912212010-11-20 09:48:00 +00005049 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04005050 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04005051
Chris Masonaf4176b2009-03-24 10:24:31 -04005052 /*
5053 * for regular files, if its inode is already on disk, we don't
5054 * have to worry about the parents at all. This is because
5055 * we can use the last_unlink_trans field to record renames
5056 * and other fun in this file.
5057 */
5058 if (S_ISREG(inode->i_mode) &&
5059 BTRFS_I(inode)->generation <= last_committed &&
5060 BTRFS_I(inode)->last_unlink_trans <= last_committed)
5061 goto out;
5062
Chris Mason12fcfd22009-03-24 10:24:20 -04005063 if (!S_ISDIR(inode->i_mode)) {
Al Virofc640052016-04-10 01:33:30 -04005064 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005065 goto out;
David Howells2b0143b2015-03-17 22:25:59 +00005066 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005067 }
5068
5069 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04005070 /*
5071 * If we are logging a directory then we start with our inode,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005072 * not our parent's inode, so we need to skip setting the
Josef Bacikde2b5302013-09-11 09:36:30 -04005073 * logged_trans so that further down in the log code we don't
5074 * think this inode has already been logged.
5075 */
5076 if (inode != orig_inode)
5077 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04005078 smp_mb();
5079
Filipe Manana2be63d52016-02-12 11:34:23 +00005080 if (btrfs_must_commit_transaction(trans, inode)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04005081 ret = 1;
5082 break;
5083 }
5084
Al Virofc640052016-04-10 01:33:30 -04005085 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04005086 break;
5087
Filipe Manana44f714d2016-06-06 16:11:13 +01005088 if (IS_ROOT(parent)) {
5089 inode = d_inode(parent);
5090 if (btrfs_must_commit_transaction(trans, inode))
5091 ret = 1;
Chris Mason12fcfd22009-03-24 10:24:20 -04005092 break;
Filipe Manana44f714d2016-06-06 16:11:13 +01005093 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005094
Josef Bacik6a912212010-11-20 09:48:00 +00005095 parent = dget_parent(parent);
5096 dput(old_parent);
5097 old_parent = parent;
David Howells2b0143b2015-03-17 22:25:59 +00005098 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005099
5100 }
Josef Bacik6a912212010-11-20 09:48:00 +00005101 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04005102out:
Chris Masone02119d2008-09-05 16:13:11 -04005103 return ret;
5104}
5105
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005106struct btrfs_dir_list {
5107 u64 ino;
5108 struct list_head list;
5109};
5110
5111/*
5112 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5113 * details about the why it is needed.
5114 * This is a recursive operation - if an existing dentry corresponds to a
5115 * directory, that directory's new entries are logged too (same behaviour as
5116 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5117 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5118 * complains about the following circular lock dependency / possible deadlock:
5119 *
5120 * CPU0 CPU1
5121 * ---- ----
5122 * lock(&type->i_mutex_dir_key#3/2);
5123 * lock(sb_internal#2);
5124 * lock(&type->i_mutex_dir_key#3/2);
5125 * lock(&sb->s_type->i_mutex_key#14);
5126 *
5127 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5128 * sb_start_intwrite() in btrfs_start_transaction().
5129 * Not locking i_mutex of the inodes is still safe because:
5130 *
5131 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5132 * that while logging the inode new references (names) are added or removed
5133 * from the inode, leaving the logged inode item with a link count that does
5134 * not match the number of logged inode reference items. This is fine because
5135 * at log replay time we compute the real number of links and correct the
5136 * link count in the inode item (see replay_one_buffer() and
5137 * link_to_fixup_dir());
5138 *
5139 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5140 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5141 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5142 * has a size that doesn't match the sum of the lengths of all the logged
5143 * names. This does not result in a problem because if a dir_item key is
5144 * logged but its matching dir_index key is not logged, at log replay time we
5145 * don't use it to replay the respective name (see replay_one_name()). On the
5146 * other hand if only the dir_index key ends up being logged, the respective
5147 * name is added to the fs/subvol tree with both the dir_item and dir_index
5148 * keys created (see replay_one_name()).
5149 * The directory's inode item with a wrong i_size is not a problem as well,
5150 * since we don't use it at log replay time to set the i_size in the inode
5151 * item of the fs/subvol tree (see overwrite_item()).
5152 */
5153static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5154 struct btrfs_root *root,
5155 struct inode *start_inode,
5156 struct btrfs_log_ctx *ctx)
5157{
5158 struct btrfs_root *log = root->log_root;
5159 struct btrfs_path *path;
5160 LIST_HEAD(dir_list);
5161 struct btrfs_dir_list *dir_elem;
5162 int ret = 0;
5163
5164 path = btrfs_alloc_path();
5165 if (!path)
5166 return -ENOMEM;
5167
5168 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5169 if (!dir_elem) {
5170 btrfs_free_path(path);
5171 return -ENOMEM;
5172 }
5173 dir_elem->ino = btrfs_ino(start_inode);
5174 list_add_tail(&dir_elem->list, &dir_list);
5175
5176 while (!list_empty(&dir_list)) {
5177 struct extent_buffer *leaf;
5178 struct btrfs_key min_key;
5179 int nritems;
5180 int i;
5181
5182 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5183 list);
5184 if (ret)
5185 goto next_dir_inode;
5186
5187 min_key.objectid = dir_elem->ino;
5188 min_key.type = BTRFS_DIR_ITEM_KEY;
5189 min_key.offset = 0;
5190again:
5191 btrfs_release_path(path);
5192 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5193 if (ret < 0) {
5194 goto next_dir_inode;
5195 } else if (ret > 0) {
5196 ret = 0;
5197 goto next_dir_inode;
5198 }
5199
5200process_leaf:
5201 leaf = path->nodes[0];
5202 nritems = btrfs_header_nritems(leaf);
5203 for (i = path->slots[0]; i < nritems; i++) {
5204 struct btrfs_dir_item *di;
5205 struct btrfs_key di_key;
5206 struct inode *di_inode;
5207 struct btrfs_dir_list *new_dir_elem;
5208 int log_mode = LOG_INODE_EXISTS;
5209 int type;
5210
5211 btrfs_item_key_to_cpu(leaf, &min_key, i);
5212 if (min_key.objectid != dir_elem->ino ||
5213 min_key.type != BTRFS_DIR_ITEM_KEY)
5214 goto next_dir_inode;
5215
5216 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5217 type = btrfs_dir_type(leaf, di);
5218 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5219 type != BTRFS_FT_DIR)
5220 continue;
5221 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5222 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5223 continue;
5224
Robbie Ko664b0532016-10-28 10:48:26 +08005225 btrfs_release_path(path);
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005226 di_inode = btrfs_iget(root->fs_info->sb, &di_key,
5227 root, NULL);
5228 if (IS_ERR(di_inode)) {
5229 ret = PTR_ERR(di_inode);
5230 goto next_dir_inode;
5231 }
5232
5233 if (btrfs_inode_in_log(di_inode, trans->transid)) {
5234 iput(di_inode);
Robbie Ko664b0532016-10-28 10:48:26 +08005235 break;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005236 }
5237
5238 ctx->log_new_dentries = false;
Filipe Manana3f9749f2016-04-25 04:45:02 +01005239 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005240 log_mode = LOG_INODE_ALL;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005241 ret = btrfs_log_inode(trans, root, di_inode,
5242 log_mode, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005243 if (!ret &&
5244 btrfs_must_commit_transaction(trans, di_inode))
5245 ret = 1;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005246 iput(di_inode);
5247 if (ret)
5248 goto next_dir_inode;
5249 if (ctx->log_new_dentries) {
5250 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5251 GFP_NOFS);
5252 if (!new_dir_elem) {
5253 ret = -ENOMEM;
5254 goto next_dir_inode;
5255 }
5256 new_dir_elem->ino = di_key.objectid;
5257 list_add_tail(&new_dir_elem->list, &dir_list);
5258 }
5259 break;
5260 }
5261 if (i == nritems) {
5262 ret = btrfs_next_leaf(log, path);
5263 if (ret < 0) {
5264 goto next_dir_inode;
5265 } else if (ret > 0) {
5266 ret = 0;
5267 goto next_dir_inode;
5268 }
5269 goto process_leaf;
5270 }
5271 if (min_key.offset < (u64)-1) {
5272 min_key.offset++;
5273 goto again;
5274 }
5275next_dir_inode:
5276 list_del(&dir_elem->list);
5277 kfree(dir_elem);
5278 }
5279
5280 btrfs_free_path(path);
5281 return ret;
5282}
5283
Filipe Manana18aa0922015-08-05 16:49:08 +01005284static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5285 struct inode *inode,
5286 struct btrfs_log_ctx *ctx)
5287{
5288 int ret;
5289 struct btrfs_path *path;
5290 struct btrfs_key key;
5291 struct btrfs_root *root = BTRFS_I(inode)->root;
5292 const u64 ino = btrfs_ino(inode);
5293
5294 path = btrfs_alloc_path();
5295 if (!path)
5296 return -ENOMEM;
5297 path->skip_locking = 1;
5298 path->search_commit_root = 1;
5299
5300 key.objectid = ino;
5301 key.type = BTRFS_INODE_REF_KEY;
5302 key.offset = 0;
5303 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5304 if (ret < 0)
5305 goto out;
5306
5307 while (true) {
5308 struct extent_buffer *leaf = path->nodes[0];
5309 int slot = path->slots[0];
5310 u32 cur_offset = 0;
5311 u32 item_size;
5312 unsigned long ptr;
5313
5314 if (slot >= btrfs_header_nritems(leaf)) {
5315 ret = btrfs_next_leaf(root, path);
5316 if (ret < 0)
5317 goto out;
5318 else if (ret > 0)
5319 break;
5320 continue;
5321 }
5322
5323 btrfs_item_key_to_cpu(leaf, &key, slot);
5324 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5325 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5326 break;
5327
5328 item_size = btrfs_item_size_nr(leaf, slot);
5329 ptr = btrfs_item_ptr_offset(leaf, slot);
5330 while (cur_offset < item_size) {
5331 struct btrfs_key inode_key;
5332 struct inode *dir_inode;
5333
5334 inode_key.type = BTRFS_INODE_ITEM_KEY;
5335 inode_key.offset = 0;
5336
5337 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5338 struct btrfs_inode_extref *extref;
5339
5340 extref = (struct btrfs_inode_extref *)
5341 (ptr + cur_offset);
5342 inode_key.objectid = btrfs_inode_extref_parent(
5343 leaf, extref);
5344 cur_offset += sizeof(*extref);
5345 cur_offset += btrfs_inode_extref_name_len(leaf,
5346 extref);
5347 } else {
5348 inode_key.objectid = key.offset;
5349 cur_offset = item_size;
5350 }
5351
5352 dir_inode = btrfs_iget(root->fs_info->sb, &inode_key,
5353 root, NULL);
5354 /* If parent inode was deleted, skip it. */
5355 if (IS_ERR(dir_inode))
5356 continue;
5357
Filipe Manana657ed1a2016-04-06 17:11:56 +01005358 if (ctx)
5359 ctx->log_new_dentries = false;
Filipe Manana18aa0922015-08-05 16:49:08 +01005360 ret = btrfs_log_inode(trans, root, dir_inode,
5361 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
Filipe Manana2be63d52016-02-12 11:34:23 +00005362 if (!ret &&
5363 btrfs_must_commit_transaction(trans, dir_inode))
5364 ret = 1;
Filipe Manana657ed1a2016-04-06 17:11:56 +01005365 if (!ret && ctx && ctx->log_new_dentries)
5366 ret = log_new_dir_dentries(trans, root,
5367 dir_inode, ctx);
Filipe Manana18aa0922015-08-05 16:49:08 +01005368 iput(dir_inode);
5369 if (ret)
5370 goto out;
5371 }
5372 path->slots[0]++;
5373 }
5374 ret = 0;
5375out:
5376 btrfs_free_path(path);
5377 return ret;
5378}
5379
Chris Masone02119d2008-09-05 16:13:11 -04005380/*
5381 * helper function around btrfs_log_inode to make sure newly created
5382 * parent directories also end up in the log. A minimal inode and backref
5383 * only logging is done of any parent directories that are older than
5384 * the last committed transaction
5385 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005386static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5387 struct btrfs_root *root, struct inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005388 struct dentry *parent,
5389 const loff_t start,
5390 const loff_t end,
5391 int exists_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005392 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005393{
Chris Mason12fcfd22009-03-24 10:24:20 -04005394 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04005395 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005396 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005397 int ret = 0;
5398 u64 last_committed = root->fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005399 bool log_dentries = false;
5400 struct inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005401
5402 sb = inode->i_sb;
5403
Jeff Mahoney3cdde222016-06-09 21:38:35 -04005404 if (btrfs_test_opt(root->fs_info, NOTREELOG)) {
Sage Weil3a5e1402009-04-02 16:49:40 -04005405 ret = 1;
5406 goto end_no_trans;
5407 }
5408
Miao Xie995946d2014-04-02 19:51:06 +08005409 /*
5410 * The prev transaction commit doesn't complete, we need do
5411 * full commit by ourselves.
5412 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005413 if (root->fs_info->last_trans_log_full_commit >
5414 root->fs_info->last_trans_committed) {
5415 ret = 1;
5416 goto end_no_trans;
5417 }
5418
Yan, Zheng76dda932009-09-21 16:00:26 -04005419 if (root != BTRFS_I(inode)->root ||
5420 btrfs_root_refs(&root->root_item) == 0) {
5421 ret = 1;
5422 goto end_no_trans;
5423 }
5424
Chris Mason12fcfd22009-03-24 10:24:20 -04005425 ret = check_parent_dirs_for_sync(trans, inode, parent,
5426 sb, last_committed);
5427 if (ret)
5428 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005429
Josef Bacik22ee6982012-05-29 16:57:49 -04005430 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005431 ret = BTRFS_NO_LOG_SYNC;
5432 goto end_no_trans;
5433 }
5434
Miao Xie8b050d32014-02-20 18:08:58 +08005435 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005436 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005437 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005438
Filipe Manana8407f552014-09-05 15:14:39 +01005439 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005440 if (ret)
5441 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005442
Chris Masonaf4176b2009-03-24 10:24:31 -04005443 /*
5444 * for regular files, if its inode is already on disk, we don't
5445 * have to worry about the parents at all. This is because
5446 * we can use the last_unlink_trans field to record renames
5447 * and other fun in this file.
5448 */
5449 if (S_ISREG(inode->i_mode) &&
5450 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005451 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
5452 ret = 0;
5453 goto end_trans;
5454 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005455
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005456 if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
5457 log_dentries = true;
5458
Filipe Manana18aa0922015-08-05 16:49:08 +01005459 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005460 * On unlink we must make sure all our current and old parent directory
Filipe Manana18aa0922015-08-05 16:49:08 +01005461 * inodes are fully logged. This is to prevent leaving dangling
5462 * directory index entries in directories that were our parents but are
5463 * not anymore. Not doing this results in old parent directory being
5464 * impossible to delete after log replay (rmdir will always fail with
5465 * error -ENOTEMPTY).
5466 *
5467 * Example 1:
5468 *
5469 * mkdir testdir
5470 * touch testdir/foo
5471 * ln testdir/foo testdir/bar
5472 * sync
5473 * unlink testdir/bar
5474 * xfs_io -c fsync testdir/foo
5475 * <power failure>
5476 * mount fs, triggers log replay
5477 *
5478 * If we don't log the parent directory (testdir), after log replay the
5479 * directory still has an entry pointing to the file inode using the bar
5480 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5481 * the file inode has a link count of 1.
5482 *
5483 * Example 2:
5484 *
5485 * mkdir testdir
5486 * touch foo
5487 * ln foo testdir/foo2
5488 * ln foo testdir/foo3
5489 * sync
5490 * unlink testdir/foo3
5491 * xfs_io -c fsync foo
5492 * <power failure>
5493 * mount fs, triggers log replay
5494 *
5495 * Similar as the first example, after log replay the parent directory
5496 * testdir still has an entry pointing to the inode file with name foo3
5497 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5498 * and has a link count of 2.
5499 */
5500 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
5501 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5502 if (ret)
5503 goto end_trans;
5504 }
5505
Chris Masond3977122009-01-05 21:25:51 -05005506 while (1) {
Al Virofc640052016-04-10 01:33:30 -04005507 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005508 break;
5509
David Howells2b0143b2015-03-17 22:25:59 +00005510 inode = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005511 if (root != BTRFS_I(inode)->root)
5512 break;
5513
Filipe Manana18aa0922015-08-05 16:49:08 +01005514 if (BTRFS_I(inode)->generation > last_committed) {
5515 ret = btrfs_log_inode(trans, root, inode,
5516 LOG_INODE_EXISTS,
Filipe Manana8407f552014-09-05 15:14:39 +01005517 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005518 if (ret)
5519 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005520 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005521 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005522 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005523
Josef Bacik6a912212010-11-20 09:48:00 +00005524 parent = dget_parent(parent);
5525 dput(old_parent);
5526 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005527 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005528 if (log_dentries)
5529 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5530 else
5531 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005532end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005533 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005534 if (ret < 0) {
Miao Xie995946d2014-04-02 19:51:06 +08005535 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005536 ret = 1;
5537 }
Miao Xie8b050d32014-02-20 18:08:58 +08005538
5539 if (ret)
5540 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005541 btrfs_end_log_trans(root);
5542end_no_trans:
5543 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005544}
5545
5546/*
5547 * it is not safe to log dentry if the chunk root has added new
5548 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5549 * If this returns 1, you must commit the transaction to safely get your
5550 * data on disk.
5551 */
5552int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08005553 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005554 const loff_t start,
5555 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005556 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005557{
Josef Bacik6a912212010-11-20 09:48:00 +00005558 struct dentry *parent = dget_parent(dentry);
5559 int ret;
5560
David Howells2b0143b2015-03-17 22:25:59 +00005561 ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005562 start, end, 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005563 dput(parent);
5564
5565 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005566}
5567
5568/*
5569 * should be called during mount to recover any replay any log trees
5570 * from the FS
5571 */
5572int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5573{
5574 int ret;
5575 struct btrfs_path *path;
5576 struct btrfs_trans_handle *trans;
5577 struct btrfs_key key;
5578 struct btrfs_key found_key;
5579 struct btrfs_key tmp_key;
5580 struct btrfs_root *log;
5581 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5582 struct walk_control wc = {
5583 .process_func = process_one_buffer,
5584 .stage = 0,
5585 };
5586
Chris Masone02119d2008-09-05 16:13:11 -04005587 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005588 if (!path)
5589 return -ENOMEM;
5590
Josef Bacikafcdd122016-09-02 15:40:02 -04005591 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005592
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005593 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005594 if (IS_ERR(trans)) {
5595 ret = PTR_ERR(trans);
5596 goto error;
5597 }
Chris Masone02119d2008-09-05 16:13:11 -04005598
5599 wc.trans = trans;
5600 wc.pin = 1;
5601
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005602 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005603 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005604 btrfs_handle_fs_error(fs_info, ret,
5605 "Failed to pin buffers while recovering log root tree.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005606 goto error;
5607 }
Chris Masone02119d2008-09-05 16:13:11 -04005608
5609again:
5610 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5611 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005612 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005613
Chris Masond3977122009-01-05 21:25:51 -05005614 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005615 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005616
5617 if (ret < 0) {
Anand Jain34d97002016-03-16 16:43:06 +08005618 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005619 "Couldn't find tree log root.");
5620 goto error;
5621 }
Chris Masone02119d2008-09-05 16:13:11 -04005622 if (ret > 0) {
5623 if (path->slots[0] == 0)
5624 break;
5625 path->slots[0]--;
5626 }
5627 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5628 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005629 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005630 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5631 break;
5632
Miao Xiecb517ea2013-05-15 07:48:19 +00005633 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005634 if (IS_ERR(log)) {
5635 ret = PTR_ERR(log);
Anand Jain34d97002016-03-16 16:43:06 +08005636 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005637 "Couldn't read tree log root.");
5638 goto error;
5639 }
Chris Masone02119d2008-09-05 16:13:11 -04005640
5641 tmp_key.objectid = found_key.offset;
5642 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5643 tmp_key.offset = (u64)-1;
5644
5645 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005646 if (IS_ERR(wc.replay_dest)) {
5647 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005648 free_extent_buffer(log->node);
5649 free_extent_buffer(log->commit_root);
5650 kfree(log);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005651 btrfs_handle_fs_error(fs_info, ret,
5652 "Couldn't read target root for tree log recovery.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005653 goto error;
5654 }
Chris Masone02119d2008-09-05 16:13:11 -04005655
Yan Zheng07d400a2009-01-06 11:42:00 -05005656 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005657 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005658 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005659
Josef Bacikb50c6e22013-04-25 15:55:30 -04005660 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005661 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5662 path);
Chris Masone02119d2008-09-05 16:13:11 -04005663 }
Chris Masone02119d2008-09-05 16:13:11 -04005664
Liu Boefb1cbc2018-01-25 11:02:56 -07005665 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5666 struct btrfs_root *root = wc.replay_dest;
5667
5668 btrfs_release_path(path);
5669
5670 /*
5671 * We have just replayed everything, and the highest
5672 * objectid of fs roots probably has changed in case
5673 * some inode_item's got replayed.
5674 *
5675 * root->objectid_mutex is not acquired as log replay
5676 * could only happen during mount.
5677 */
5678 ret = btrfs_find_highest_objectid(root,
5679 &root->highest_objectid);
5680 }
5681
Chris Masone02119d2008-09-05 16:13:11 -04005682 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005683 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005684 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005685 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005686 kfree(log);
5687
Josef Bacikb50c6e22013-04-25 15:55:30 -04005688 if (ret)
5689 goto error;
5690
Chris Masone02119d2008-09-05 16:13:11 -04005691 if (found_key.offset == 0)
5692 break;
5693 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005694 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005695
5696 /* step one is to pin it all, step two is to replay just inodes */
5697 if (wc.pin) {
5698 wc.pin = 0;
5699 wc.process_func = replay_one_buffer;
5700 wc.stage = LOG_WALK_REPLAY_INODES;
5701 goto again;
5702 }
5703 /* step three is to replay everything */
5704 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5705 wc.stage++;
5706 goto again;
5707 }
5708
5709 btrfs_free_path(path);
5710
Josef Bacikabefa552013-04-24 16:40:05 -04005711 /* step 4: commit the transaction, which also unpins the blocks */
5712 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
5713 if (ret)
5714 return ret;
5715
Chris Masone02119d2008-09-05 16:13:11 -04005716 free_extent_buffer(log_root_tree->node);
5717 log_root_tree->log_root = NULL;
Josef Bacikafcdd122016-09-02 15:40:02 -04005718 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
Chris Masone02119d2008-09-05 16:13:11 -04005719 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005720
Josef Bacikabefa552013-04-24 16:40:05 -04005721 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005722error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005723 if (wc.trans)
5724 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005725 btrfs_free_path(path);
5726 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005727}
Chris Mason12fcfd22009-03-24 10:24:20 -04005728
5729/*
5730 * there are some corner cases where we want to force a full
5731 * commit instead of allowing a directory to be logged.
5732 *
5733 * They revolve around files there were unlinked from the directory, and
5734 * this function updates the parent directory so that a full commit is
5735 * properly done if it is fsync'd later after the unlinks are done.
Filipe Manana2be63d52016-02-12 11:34:23 +00005736 *
5737 * Must be called before the unlink operations (updates to the subvolume tree,
5738 * inodes, etc) are done.
Chris Mason12fcfd22009-03-24 10:24:20 -04005739 */
5740void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
5741 struct inode *dir, struct inode *inode,
5742 int for_rename)
5743{
5744 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005745 * when we're logging a file, if it hasn't been renamed
5746 * or unlinked, and its inode is fully committed on disk,
5747 * we don't have to worry about walking up the directory chain
5748 * to log its parents.
5749 *
5750 * So, we use the last_unlink_trans field to put this transid
5751 * into the file. When the file is logged we check it and
5752 * don't log the parents if the file is fully on disk.
5753 */
Filipe Manana657ed1a2016-04-06 17:11:56 +01005754 mutex_lock(&BTRFS_I(inode)->log_mutex);
5755 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5756 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Chris Masonaf4176b2009-03-24 10:24:31 -04005757
5758 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005759 * if this directory was already logged any new
5760 * names for this file/dir will get recorded
5761 */
5762 smp_mb();
5763 if (BTRFS_I(dir)->logged_trans == trans->transid)
5764 return;
5765
5766 /*
5767 * if the inode we're about to unlink was logged,
5768 * the log will be properly updated for any new names
5769 */
5770 if (BTRFS_I(inode)->logged_trans == trans->transid)
5771 return;
5772
5773 /*
5774 * when renaming files across directories, if the directory
5775 * there we're unlinking from gets fsync'd later on, there's
5776 * no way to find the destination directory later and fsync it
5777 * properly. So, we have to be conservative and force commits
5778 * so the new name gets discovered.
5779 */
5780 if (for_rename)
5781 goto record;
5782
5783 /* we can safely do the unlink without any special recording */
5784 return;
5785
5786record:
Filipe Manana2be63d52016-02-12 11:34:23 +00005787 mutex_lock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005788 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005789 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04005790}
5791
5792/*
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005793 * Make sure that if someone attempts to fsync the parent directory of a deleted
5794 * snapshot, it ends up triggering a transaction commit. This is to guarantee
5795 * that after replaying the log tree of the parent directory's root we will not
5796 * see the snapshot anymore and at log replay time we will not see any log tree
5797 * corresponding to the deleted snapshot's root, which could lead to replaying
5798 * it after replaying the log tree of the parent directory (which would replay
5799 * the snapshot delete operation).
Filipe Manana2be63d52016-02-12 11:34:23 +00005800 *
5801 * Must be called before the actual snapshot destroy operation (updates to the
5802 * parent root and tree of tree roots trees, etc) are done.
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005803 */
5804void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
5805 struct inode *dir)
5806{
Filipe Manana2be63d52016-02-12 11:34:23 +00005807 mutex_lock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005808 BTRFS_I(dir)->last_unlink_trans = trans->transid;
Filipe Manana2be63d52016-02-12 11:34:23 +00005809 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Filipe Manana1ec9a1a2016-02-10 10:42:25 +00005810}
5811
5812/*
Chris Mason12fcfd22009-03-24 10:24:20 -04005813 * Call this after adding a new name for a file and it will properly
5814 * update the log to reflect the new name.
5815 *
5816 * It will return zero if all goes well, and it will return 1 if a
5817 * full transaction commit is required.
5818 */
5819int btrfs_log_new_name(struct btrfs_trans_handle *trans,
5820 struct inode *inode, struct inode *old_dir,
5821 struct dentry *parent)
5822{
5823 struct btrfs_root * root = BTRFS_I(inode)->root;
5824
5825 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005826 * this will force the logging code to walk the dentry chain
5827 * up for the file
5828 */
5829 if (S_ISREG(inode->i_mode))
5830 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5831
5832 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005833 * if this inode hasn't been logged and directory we're renaming it
5834 * from hasn't been logged, we don't need to log it
5835 */
5836 if (BTRFS_I(inode)->logged_trans <=
5837 root->fs_info->last_trans_committed &&
5838 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
5839 root->fs_info->last_trans_committed))
5840 return 0;
5841
Filipe Manana49dae1b2014-09-06 22:34:39 +01005842 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
5843 LLONG_MAX, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04005844}
5845