blob: 6d650468d21acd7e62f3746453abefde5166361e [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"
Chris Masone02119d2008-09-05 16:13:11 -040029
30/* magic values for the inode_only field in btrfs_log_inode:
31 *
32 * LOG_INODE_ALL means to log everything
33 * LOG_INODE_EXISTS means to log just enough to recreate the inode
34 * during log replay
35 */
36#define LOG_INODE_ALL 0
37#define LOG_INODE_EXISTS 1
38
39/*
Chris Mason12fcfd22009-03-24 10:24:20 -040040 * directory trouble cases
41 *
42 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
43 * log, we must force a full commit before doing an fsync of the directory
44 * where the unlink was done.
45 * ---> record transid of last unlink/rename per directory
46 *
47 * mkdir foo/some_dir
48 * normal commit
49 * rename foo/some_dir foo2/some_dir
50 * mkdir foo/some_dir
51 * fsync foo/some_dir/some_file
52 *
53 * The fsync above will unlink the original some_dir without recording
54 * it in its new location (foo2). After a crash, some_dir will be gone
55 * unless the fsync of some_file forces a full commit
56 *
57 * 2) we must log any new names for any file or dir that is in the fsync
58 * log. ---> check inode while renaming/linking.
59 *
60 * 2a) we must log any new names for any file or dir during rename
61 * when the directory they are being removed from was logged.
62 * ---> check inode and old parent dir during rename
63 *
64 * 2a is actually the more important variant. With the extra logging
65 * a crash might unlink the old name without recreating the new one
66 *
67 * 3) after a crash, we must go through any directories with a link count
68 * of zero and redo the rm -rf
69 *
70 * mkdir f1/foo
71 * normal commit
72 * rm -rf f1/foo
73 * fsync(f1)
74 *
75 * The directory f1 was fully removed from the FS, but fsync was never
76 * called on f1, only its parent dir. After a crash the rm -rf must
77 * be replayed. This must be able to recurse down the entire
78 * directory tree. The inode link count fixup code takes care of the
79 * ugly details.
80 */
81
82/*
Chris Masone02119d2008-09-05 16:13:11 -040083 * stages for the tree walking. The first
84 * stage (0) is to only pin down the blocks we find
85 * the second stage (1) is to make sure that all the inodes
86 * we find in the log are created in the subvolume.
87 *
88 * The last stage is to deal with directories and links and extents
89 * and all the other fun semantics
90 */
91#define LOG_WALK_PIN_ONLY 0
92#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040093#define LOG_WALK_REPLAY_DIR_INDEX 2
94#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040095
Chris Mason12fcfd22009-03-24 10:24:20 -040096static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +010097 struct btrfs_root *root, struct inode *inode,
98 int inode_only,
99 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +0100100 const loff_t end,
101 struct btrfs_log_ctx *ctx);
Yan Zhengec051c02009-01-05 15:43:42 -0500102static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
103 struct btrfs_root *root,
104 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400105static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
106 struct btrfs_root *root,
107 struct btrfs_root *log,
108 struct btrfs_path *path,
109 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400110
111/*
112 * tree logging is a special write ahead log used to make sure that
113 * fsyncs and O_SYNCs can happen without doing full tree commits.
114 *
115 * Full tree commits are expensive because they require commonly
116 * modified blocks to be recowed, creating many dirty pages in the
117 * extent tree an 4x-6x higher write load than ext3.
118 *
119 * Instead of doing a tree commit on every fsync, we use the
120 * key ranges and transaction ids to find items for a given file or directory
121 * that have changed in this transaction. Those items are copied into
122 * a special tree (one per subvolume root), that tree is written to disk
123 * and then the fsync is considered complete.
124 *
125 * After a crash, items are copied out of the log-tree back into the
126 * subvolume tree. Any file data extents found are recorded in the extent
127 * allocation tree, and the log-tree freed.
128 *
129 * The log tree is read three times, once to pin down all the extents it is
130 * using in ram and once, once to create all the inodes logged in the tree
131 * and once to do all the other items.
132 */
133
134/*
Chris Masone02119d2008-09-05 16:13:11 -0400135 * start a sub transaction and setup the log tree
136 * this increments the log tree writer count to make the people
137 * syncing the tree wait for us to finish
138 */
139static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800140 struct btrfs_root *root,
141 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400142{
Zhaolei34eb2a52015-08-17 18:44:45 +0800143 int ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500144
145 mutex_lock(&root->log_mutex);
Zhaolei34eb2a52015-08-17 18:44:45 +0800146
Yan Zheng7237f182009-01-21 12:54:03 -0500147 if (root->log_root) {
Miao Xie995946d2014-04-02 19:51:06 +0800148 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xie50471a32014-02-20 18:08:57 +0800149 ret = -EAGAIN;
150 goto out;
151 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800152
Josef Bacikff782e02009-10-08 15:30:04 -0400153 if (!root->log_start_pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800154 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Zhaolei34eb2a52015-08-17 18:44:45 +0800155 root->log_start_pid = current->pid;
Josef Bacikff782e02009-10-08 15:30:04 -0400156 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800157 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400158 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800159 } else {
160 mutex_lock(&root->fs_info->tree_log_mutex);
161 if (!root->fs_info->log_root_tree)
162 ret = btrfs_init_log_root_tree(trans, root->fs_info);
163 mutex_unlock(&root->fs_info->tree_log_mutex);
164 if (ret)
165 goto out;
Josef Bacikff782e02009-10-08 15:30:04 -0400166
Chris Masone02119d2008-09-05 16:13:11 -0400167 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400168 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800169 goto out;
Zhaolei34eb2a52015-08-17 18:44:45 +0800170
171 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
172 root->log_start_pid = current->pid;
Chris Masone02119d2008-09-05 16:13:11 -0400173 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800174
Miao Xie2ecb7922012-09-06 04:04:27 -0600175 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500176 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800177 if (ctx) {
Zhaolei34eb2a52015-08-17 18:44:45 +0800178 int index = root->log_transid % 2;
Miao Xie8b050d32014-02-20 18:08:58 +0800179 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800180 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800181 }
Zhaolei34eb2a52015-08-17 18:44:45 +0800182
Miao Xiee87ac132014-02-20 18:08:53 +0800183out:
Yan Zheng7237f182009-01-21 12:54:03 -0500184 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800185 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400186}
187
188/*
189 * returns 0 if there was a log transaction running and we were able
190 * to join, or returns -ENOENT if there were not transactions
191 * in progress
192 */
193static int join_running_log_trans(struct btrfs_root *root)
194{
195 int ret = -ENOENT;
196
197 smp_mb();
198 if (!root->log_root)
199 return -ENOENT;
200
Yan Zheng7237f182009-01-21 12:54:03 -0500201 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400202 if (root->log_root) {
203 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500204 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400205 }
Yan Zheng7237f182009-01-21 12:54:03 -0500206 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400207 return ret;
208}
209
210/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400211 * This either makes the current running log transaction wait
212 * until you call btrfs_end_log_trans() or it makes any future
213 * log transactions wait until you call btrfs_end_log_trans()
214 */
215int btrfs_pin_log_trans(struct btrfs_root *root)
216{
217 int ret = -ENOENT;
218
219 mutex_lock(&root->log_mutex);
220 atomic_inc(&root->log_writers);
221 mutex_unlock(&root->log_mutex);
222 return ret;
223}
224
225/*
Chris Masone02119d2008-09-05 16:13:11 -0400226 * indicate we're done making changes to the log tree
227 * and wake up anyone waiting to do a sync
228 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100229void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400230{
Yan Zheng7237f182009-01-21 12:54:03 -0500231 if (atomic_dec_and_test(&root->log_writers)) {
232 smp_mb();
233 if (waitqueue_active(&root->log_writer_wait))
234 wake_up(&root->log_writer_wait);
235 }
Chris Masone02119d2008-09-05 16:13:11 -0400236}
237
238
239/*
240 * the walk control struct is used to pass state down the chain when
241 * processing the log tree. The stage field tells us which part
242 * of the log tree processing we are currently doing. The others
243 * are state fields used for that specific part
244 */
245struct walk_control {
246 /* should we free the extent on disk when done? This is used
247 * at transaction commit time while freeing a log tree
248 */
249 int free;
250
251 /* should we write out the extent buffer? This is used
252 * while flushing the log tree to disk during a sync
253 */
254 int write;
255
256 /* should we wait for the extent buffer io to finish? Also used
257 * while flushing the log tree to disk for a sync
258 */
259 int wait;
260
261 /* pin only walk, we record which extents on disk belong to the
262 * log trees
263 */
264 int pin;
265
266 /* what stage of the replay code we're currently in */
267 int stage;
268
269 /* the root we are currently replaying */
270 struct btrfs_root *replay_dest;
271
272 /* the trans handle for the current replay */
273 struct btrfs_trans_handle *trans;
274
275 /* the function that gets used to process blocks we find in the
276 * tree. Note the extent_buffer might not be up to date when it is
277 * passed in, and it must be checked or read if you need the data
278 * inside it
279 */
280 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
281 struct walk_control *wc, u64 gen);
282};
283
284/*
285 * process_func used to pin down extents, write them or wait on them
286 */
287static int process_one_buffer(struct btrfs_root *log,
288 struct extent_buffer *eb,
289 struct walk_control *wc, u64 gen)
290{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400291 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400292
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400293 /*
294 * If this fs is mixed then we need to be able to process the leaves to
295 * pin down any logged extents, so we have to read the block.
296 */
297 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
298 ret = btrfs_read_buffer(eb, gen);
299 if (ret)
300 return ret;
301 }
302
Josef Bacikb50c6e22013-04-25 15:55:30 -0400303 if (wc->pin)
304 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
305 eb->start, eb->len);
306
307 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400308 if (wc->pin && btrfs_header_level(eb) == 0)
309 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400310 if (wc->write)
311 btrfs_write_tree_block(eb);
312 if (wc->wait)
313 btrfs_wait_tree_block_writeback(eb);
314 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400315 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400316}
317
318/*
319 * Item overwrite used by replay and tree logging. eb, slot and key all refer
320 * to the src data we are copying out.
321 *
322 * root is the tree we are copying into, and path is a scratch
323 * path for use in this function (it should be released on entry and
324 * will be released on exit).
325 *
326 * If the key is already in the destination tree the existing item is
327 * overwritten. If the existing item isn't big enough, it is extended.
328 * If it is too large, it is truncated.
329 *
330 * If the key isn't in the destination yet, a new item is inserted.
331 */
332static noinline int overwrite_item(struct btrfs_trans_handle *trans,
333 struct btrfs_root *root,
334 struct btrfs_path *path,
335 struct extent_buffer *eb, int slot,
336 struct btrfs_key *key)
337{
338 int ret;
339 u32 item_size;
340 u64 saved_i_size = 0;
341 int save_old_i_size = 0;
342 unsigned long src_ptr;
343 unsigned long dst_ptr;
344 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000345 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400346
347 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
348 overwrite_root = 1;
349
350 item_size = btrfs_item_size_nr(eb, slot);
351 src_ptr = btrfs_item_ptr_offset(eb, slot);
352
353 /* look for the key in the destination tree */
354 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000355 if (ret < 0)
356 return ret;
357
Chris Masone02119d2008-09-05 16:13:11 -0400358 if (ret == 0) {
359 char *src_copy;
360 char *dst_copy;
361 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
362 path->slots[0]);
363 if (dst_size != item_size)
364 goto insert;
365
366 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200367 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400368 return 0;
369 }
370 dst_copy = kmalloc(item_size, GFP_NOFS);
371 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000372 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200373 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000374 kfree(dst_copy);
375 kfree(src_copy);
376 return -ENOMEM;
377 }
Chris Masone02119d2008-09-05 16:13:11 -0400378
379 read_extent_buffer(eb, src_copy, src_ptr, item_size);
380
381 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
382 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
383 item_size);
384 ret = memcmp(dst_copy, src_copy, item_size);
385
386 kfree(dst_copy);
387 kfree(src_copy);
388 /*
389 * they have the same contents, just return, this saves
390 * us from cowing blocks in the destination tree and doing
391 * extra writes that may not have been done by a previous
392 * sync
393 */
394 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200395 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400396 return 0;
397 }
398
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000399 /*
400 * We need to load the old nbytes into the inode so when we
401 * replay the extents we've logged we get the right nbytes.
402 */
403 if (inode_item) {
404 struct btrfs_inode_item *item;
405 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400406 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000407
408 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
409 struct btrfs_inode_item);
410 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
411 item = btrfs_item_ptr(eb, slot,
412 struct btrfs_inode_item);
413 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400414
415 /*
416 * If this is a directory we need to reset the i_size to
417 * 0 so that we can set it up properly when replaying
418 * the rest of the items in this log.
419 */
420 mode = btrfs_inode_mode(eb, item);
421 if (S_ISDIR(mode))
422 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000423 }
424 } else if (inode_item) {
425 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400426 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000427
428 /*
429 * New inode, set nbytes to 0 so that the nbytes comes out
430 * properly when we replay the extents.
431 */
432 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
433 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400434
435 /*
436 * If this is a directory we need to reset the i_size to 0 so
437 * that we can set it up properly when replaying the rest of
438 * the items in this log.
439 */
440 mode = btrfs_inode_mode(eb, item);
441 if (S_ISDIR(mode))
442 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400443 }
444insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200445 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400446 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000447 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400448 ret = btrfs_insert_empty_item(trans, root, path,
449 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000450 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400451
452 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000453 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400454 u32 found_size;
455 found_size = btrfs_item_size_nr(path->nodes[0],
456 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100457 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000458 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100459 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000460 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100461 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400462 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400463 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400464 }
465 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
466 path->slots[0]);
467
468 /* don't overwrite an existing inode if the generation number
469 * was logged as zero. This is done when the tree logging code
470 * is just logging an inode to make sure it exists after recovery.
471 *
472 * Also, don't overwrite i_size on directories during replay.
473 * log replay inserts and removes directory items based on the
474 * state of the tree found in the subvolume, and i_size is modified
475 * as it goes
476 */
477 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
478 struct btrfs_inode_item *src_item;
479 struct btrfs_inode_item *dst_item;
480
481 src_item = (struct btrfs_inode_item *)src_ptr;
482 dst_item = (struct btrfs_inode_item *)dst_ptr;
483
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000484 if (btrfs_inode_generation(eb, src_item) == 0) {
485 struct extent_buffer *dst_eb = path->nodes[0];
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000486 const u64 ino_size = btrfs_inode_size(eb, src_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000487
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000488 /*
489 * For regular files an ino_size == 0 is used only when
490 * logging that an inode exists, as part of a directory
491 * fsync, and the inode wasn't fsynced before. In this
492 * case don't set the size of the inode in the fs/subvol
493 * tree, otherwise we would be throwing valid data away.
494 */
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000495 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
Filipe Manana2f2ff0e2015-03-20 17:19:46 +0000496 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
497 ino_size != 0) {
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000498 struct btrfs_map_token token;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000499
500 btrfs_init_map_token(&token);
501 btrfs_set_token_inode_size(dst_eb, dst_item,
502 ino_size, &token);
503 }
Chris Masone02119d2008-09-05 16:13:11 -0400504 goto no_copy;
Filipe Manana1a4bcf42015-02-13 12:30:56 +0000505 }
Chris Masone02119d2008-09-05 16:13:11 -0400506
507 if (overwrite_root &&
508 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
509 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
510 save_old_i_size = 1;
511 saved_i_size = btrfs_inode_size(path->nodes[0],
512 dst_item);
513 }
514 }
515
516 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
517 src_ptr, item_size);
518
519 if (save_old_i_size) {
520 struct btrfs_inode_item *dst_item;
521 dst_item = (struct btrfs_inode_item *)dst_ptr;
522 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
523 }
524
525 /* make sure the generation is filled in */
526 if (key->type == BTRFS_INODE_ITEM_KEY) {
527 struct btrfs_inode_item *dst_item;
528 dst_item = (struct btrfs_inode_item *)dst_ptr;
529 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
530 btrfs_set_inode_generation(path->nodes[0], dst_item,
531 trans->transid);
532 }
533 }
534no_copy:
535 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200536 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400537 return 0;
538}
539
540/*
541 * simple helper to read an inode off the disk from a given root
542 * This can only be called for subvolume roots and not for the log
543 */
544static noinline struct inode *read_one_inode(struct btrfs_root *root,
545 u64 objectid)
546{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400547 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400548 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400549
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400550 key.objectid = objectid;
551 key.type = BTRFS_INODE_ITEM_KEY;
552 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000553 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400554 if (IS_ERR(inode)) {
555 inode = NULL;
556 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400557 iput(inode);
558 inode = NULL;
559 }
560 return inode;
561}
562
563/* replays a single extent in 'eb' at 'slot' with 'key' into the
564 * subvolume 'root'. path is released on entry and should be released
565 * on exit.
566 *
567 * extents in the log tree have not been allocated out of the extent
568 * tree yet. So, this completes the allocation, taking a reference
569 * as required if the extent already exists or creating a new extent
570 * if it isn't in the extent allocation tree yet.
571 *
572 * The extent is inserted into the file, dropping any existing extents
573 * from the file that overlap the new one.
574 */
575static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
576 struct btrfs_root *root,
577 struct btrfs_path *path,
578 struct extent_buffer *eb, int slot,
579 struct btrfs_key *key)
580{
581 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400582 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400583 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000584 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400585 struct btrfs_file_extent_item *item;
586 struct inode *inode = NULL;
587 unsigned long size;
588 int ret = 0;
589
590 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
591 found_type = btrfs_file_extent_type(eb, item);
592
Yan Zhengd899e052008-10-30 14:25:28 -0400593 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000594 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
595 nbytes = btrfs_file_extent_num_bytes(eb, item);
596 extent_end = start + nbytes;
597
598 /*
599 * We don't add to the inodes nbytes if we are prealloc or a
600 * hole.
601 */
602 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
603 nbytes = 0;
604 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800605 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000606 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000607 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400608 } else {
609 ret = 0;
610 goto out;
611 }
612
613 inode = read_one_inode(root, key->objectid);
614 if (!inode) {
615 ret = -EIO;
616 goto out;
617 }
618
619 /*
620 * first check to see if we already have this extent in the
621 * file. This must be done before the btrfs_drop_extents run
622 * so we don't try to drop this extent.
623 */
Li Zefan33345d012011-04-20 10:31:50 +0800624 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400625 start, 0);
626
Yan Zhengd899e052008-10-30 14:25:28 -0400627 if (ret == 0 &&
628 (found_type == BTRFS_FILE_EXTENT_REG ||
629 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400630 struct btrfs_file_extent_item cmp1;
631 struct btrfs_file_extent_item cmp2;
632 struct btrfs_file_extent_item *existing;
633 struct extent_buffer *leaf;
634
635 leaf = path->nodes[0];
636 existing = btrfs_item_ptr(leaf, path->slots[0],
637 struct btrfs_file_extent_item);
638
639 read_extent_buffer(eb, &cmp1, (unsigned long)item,
640 sizeof(cmp1));
641 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
642 sizeof(cmp2));
643
644 /*
645 * we already have a pointer to this exact extent,
646 * we don't have to do anything
647 */
648 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200649 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400650 goto out;
651 }
652 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200653 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400654
655 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400656 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400657 if (ret)
658 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400659
Yan Zheng07d400a2009-01-06 11:42:00 -0500660 if (found_type == BTRFS_FILE_EXTENT_REG ||
661 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400662 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500663 unsigned long dest_offset;
664 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400665
Yan Zheng07d400a2009-01-06 11:42:00 -0500666 ret = btrfs_insert_empty_item(trans, root, path, key,
667 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400668 if (ret)
669 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500670 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
671 path->slots[0]);
672 copy_extent_buffer(path->nodes[0], eb, dest_offset,
673 (unsigned long)item, sizeof(*item));
674
675 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
676 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
677 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400678 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500679
680 if (ins.objectid > 0) {
681 u64 csum_start;
682 u64 csum_end;
683 LIST_HEAD(ordered_sums);
684 /*
685 * is this extent already allocated in the extent
686 * allocation tree? If so, just add a reference
687 */
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000688 ret = btrfs_lookup_data_extent(root, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500689 ins.offset);
690 if (ret == 0) {
691 ret = btrfs_inc_extent_ref(trans, root,
692 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400693 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200694 key->objectid, offset, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400695 if (ret)
696 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500697 } else {
698 /*
699 * insert the extent pointer in the extent
700 * allocation tree
701 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400702 ret = btrfs_alloc_logged_file_extent(trans,
703 root, root->root_key.objectid,
704 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400705 if (ret)
706 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500707 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200708 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500709
710 if (btrfs_file_extent_compression(eb, item)) {
711 csum_start = ins.objectid;
712 csum_end = csum_start + ins.offset;
713 } else {
714 csum_start = ins.objectid +
715 btrfs_file_extent_offset(eb, item);
716 csum_end = csum_start +
717 btrfs_file_extent_num_bytes(eb, item);
718 }
719
720 ret = btrfs_lookup_csums_range(root->log_root,
721 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100722 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400723 if (ret)
724 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500725 while (!list_empty(&ordered_sums)) {
726 struct btrfs_ordered_sum *sums;
727 sums = list_entry(ordered_sums.next,
728 struct btrfs_ordered_sum,
729 list);
Josef Bacik36508602013-04-25 16:23:32 -0400730 if (!ret)
731 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500732 root->fs_info->csum_root,
733 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500734 list_del(&sums->list);
735 kfree(sums);
736 }
Josef Bacik36508602013-04-25 16:23:32 -0400737 if (ret)
738 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500739 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200740 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500741 }
742 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
743 /* inline extents are easy, we just overwrite them */
744 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400745 if (ret)
746 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500747 }
748
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000749 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600750 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400751out:
752 if (inode)
753 iput(inode);
754 return ret;
755}
756
757/*
758 * when cleaning up conflicts between the directory names in the
759 * subvolume, directory names in the log and directory names in the
760 * inode back references, we may have to unlink inodes from directories.
761 *
762 * This is a helper function to do the unlink of a specific directory
763 * item
764 */
765static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
766 struct btrfs_root *root,
767 struct btrfs_path *path,
768 struct inode *dir,
769 struct btrfs_dir_item *di)
770{
771 struct inode *inode;
772 char *name;
773 int name_len;
774 struct extent_buffer *leaf;
775 struct btrfs_key location;
776 int ret;
777
778 leaf = path->nodes[0];
779
780 btrfs_dir_item_key_to_cpu(leaf, di, &location);
781 name_len = btrfs_dir_name_len(leaf, di);
782 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000783 if (!name)
784 return -ENOMEM;
785
Chris Masone02119d2008-09-05 16:13:11 -0400786 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200787 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400788
789 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000790 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400791 ret = -EIO;
792 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000793 }
Chris Masone02119d2008-09-05 16:13:11 -0400794
Yan Zhengec051c02009-01-05 15:43:42 -0500795 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400796 if (ret)
797 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400798
Chris Masone02119d2008-09-05 16:13:11 -0400799 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400800 if (ret)
801 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100802 else
803 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400804out:
805 kfree(name);
806 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400807 return ret;
808}
809
810/*
811 * helper function to see if a given name and sequence number found
812 * in an inode back reference are already in a directory and correctly
813 * point to this inode
814 */
815static noinline int inode_in_dir(struct btrfs_root *root,
816 struct btrfs_path *path,
817 u64 dirid, u64 objectid, u64 index,
818 const char *name, int name_len)
819{
820 struct btrfs_dir_item *di;
821 struct btrfs_key location;
822 int match = 0;
823
824 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
825 index, name, name_len, 0);
826 if (di && !IS_ERR(di)) {
827 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
828 if (location.objectid != objectid)
829 goto out;
830 } else
831 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200832 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400833
834 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
835 if (di && !IS_ERR(di)) {
836 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
837 if (location.objectid != objectid)
838 goto out;
839 } else
840 goto out;
841 match = 1;
842out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200843 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400844 return match;
845}
846
847/*
848 * helper function to check a log tree for a named back reference in
849 * an inode. This is used to decide if a back reference that is
850 * found in the subvolume conflicts with what we find in the log.
851 *
852 * inode backreferences may have multiple refs in a single item,
853 * during replay we process one reference at a time, and we don't
854 * want to delete valid links to a file from the subvolume if that
855 * link is also in the log.
856 */
857static noinline int backref_in_log(struct btrfs_root *log,
858 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700859 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000860 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400861{
862 struct btrfs_path *path;
863 struct btrfs_inode_ref *ref;
864 unsigned long ptr;
865 unsigned long ptr_end;
866 unsigned long name_ptr;
867 int found_name_len;
868 int item_size;
869 int ret;
870 int match = 0;
871
872 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000873 if (!path)
874 return -ENOMEM;
875
Chris Masone02119d2008-09-05 16:13:11 -0400876 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
877 if (ret != 0)
878 goto out;
879
Chris Masone02119d2008-09-05 16:13:11 -0400880 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700881
882 if (key->type == BTRFS_INODE_EXTREF_KEY) {
883 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
884 name, namelen, NULL))
885 match = 1;
886
887 goto out;
888 }
889
890 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400891 ptr_end = ptr + item_size;
892 while (ptr < ptr_end) {
893 ref = (struct btrfs_inode_ref *)ptr;
894 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
895 if (found_name_len == namelen) {
896 name_ptr = (unsigned long)(ref + 1);
897 ret = memcmp_extent_buffer(path->nodes[0], name,
898 name_ptr, namelen);
899 if (ret == 0) {
900 match = 1;
901 goto out;
902 }
903 }
904 ptr = (unsigned long)(ref + 1) + found_name_len;
905 }
906out:
907 btrfs_free_path(path);
908 return match;
909}
910
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700911static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
912 struct btrfs_root *root,
913 struct btrfs_path *path,
914 struct btrfs_root *log_root,
915 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700916 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700917 u64 inode_objectid, u64 parent_objectid,
918 u64 ref_index, char *name, int namelen,
919 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700920{
921 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700922 char *victim_name;
923 int victim_name_len;
924 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700925 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700926 struct btrfs_key search_key;
927 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700928
Mark Fashehf1863732012-08-08 11:32:27 -0700929again:
930 /* Search old style refs */
931 search_key.objectid = inode_objectid;
932 search_key.type = BTRFS_INODE_REF_KEY;
933 search_key.offset = parent_objectid;
934 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700935 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700936 struct btrfs_inode_ref *victim_ref;
937 unsigned long ptr;
938 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -0700939
940 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700941
942 /* are we trying to overwrite a back ref for the root directory
943 * if so, just jump out, we're done
944 */
Mark Fashehf1863732012-08-08 11:32:27 -0700945 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700946 return 1;
947
948 /* check all the names in this back reference to see
949 * if they are in the log. if so, we allow them to stay
950 * otherwise they must be unlinked as a conflict
951 */
952 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
953 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
954 while (ptr < ptr_end) {
955 victim_ref = (struct btrfs_inode_ref *)ptr;
956 victim_name_len = btrfs_inode_ref_name_len(leaf,
957 victim_ref);
958 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400959 if (!victim_name)
960 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700961
962 read_extent_buffer(leaf, victim_name,
963 (unsigned long)(victim_ref + 1),
964 victim_name_len);
965
Mark Fashehf1863732012-08-08 11:32:27 -0700966 if (!backref_in_log(log_root, &search_key,
967 parent_objectid,
968 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700969 victim_name_len)) {
Zach Brown8b558c52013-10-16 12:10:34 -0700970 inc_nlink(inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700971 btrfs_release_path(path);
972
973 ret = btrfs_unlink_inode(trans, root, dir,
974 inode, victim_name,
975 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -0700976 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -0400977 if (ret)
978 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100979 ret = btrfs_run_delayed_items(trans, root);
980 if (ret)
981 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700982 *search_done = 1;
983 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700984 }
985 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -0700986
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700987 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
988 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700989
990 /*
991 * NOTE: we have searched root tree and checked the
992 * coresponding ref, it does not need to check again.
993 */
994 *search_done = 1;
995 }
996 btrfs_release_path(path);
997
Mark Fashehf1863732012-08-08 11:32:27 -0700998 /* Same search but for extended refs */
999 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1000 inode_objectid, parent_objectid, 0,
1001 0);
1002 if (!IS_ERR_OR_NULL(extref)) {
1003 u32 item_size;
1004 u32 cur_offset = 0;
1005 unsigned long base;
1006 struct inode *victim_parent;
1007
1008 leaf = path->nodes[0];
1009
1010 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1011 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1012
1013 while (cur_offset < item_size) {
Quentin Casasnovasdd9ef132015-03-03 16:31:38 +01001014 extref = (struct btrfs_inode_extref *)(base + cur_offset);
Mark Fashehf1863732012-08-08 11:32:27 -07001015
1016 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1017
1018 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1019 goto next;
1020
1021 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001022 if (!victim_name)
1023 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001024 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1025 victim_name_len);
1026
1027 search_key.objectid = inode_objectid;
1028 search_key.type = BTRFS_INODE_EXTREF_KEY;
1029 search_key.offset = btrfs_extref_hash(parent_objectid,
1030 victim_name,
1031 victim_name_len);
1032 ret = 0;
1033 if (!backref_in_log(log_root, &search_key,
1034 parent_objectid, victim_name,
1035 victim_name_len)) {
1036 ret = -ENOENT;
1037 victim_parent = read_one_inode(root,
1038 parent_objectid);
1039 if (victim_parent) {
Zach Brown8b558c52013-10-16 12:10:34 -07001040 inc_nlink(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001041 btrfs_release_path(path);
1042
1043 ret = btrfs_unlink_inode(trans, root,
1044 victim_parent,
1045 inode,
1046 victim_name,
1047 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001048 if (!ret)
1049 ret = btrfs_run_delayed_items(
1050 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001051 }
Mark Fashehf1863732012-08-08 11:32:27 -07001052 iput(victim_parent);
1053 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001054 if (ret)
1055 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001056 *search_done = 1;
1057 goto again;
1058 }
1059 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001060 if (ret)
1061 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001062next:
1063 cur_offset += victim_name_len + sizeof(*extref);
1064 }
1065 *search_done = 1;
1066 }
1067 btrfs_release_path(path);
1068
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001069 /* look for a conflicting sequence number */
1070 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001071 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001072 if (di && !IS_ERR(di)) {
1073 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001074 if (ret)
1075 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001076 }
1077 btrfs_release_path(path);
1078
1079 /* look for a conflicing name */
1080 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1081 name, namelen, 0);
1082 if (di && !IS_ERR(di)) {
1083 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001084 if (ret)
1085 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001086 }
1087 btrfs_release_path(path);
1088
1089 return 0;
1090}
Chris Masone02119d2008-09-05 16:13:11 -04001091
Mark Fashehf1863732012-08-08 11:32:27 -07001092static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1093 u32 *namelen, char **name, u64 *index,
1094 u64 *parent_objectid)
1095{
1096 struct btrfs_inode_extref *extref;
1097
1098 extref = (struct btrfs_inode_extref *)ref_ptr;
1099
1100 *namelen = btrfs_inode_extref_name_len(eb, extref);
1101 *name = kmalloc(*namelen, GFP_NOFS);
1102 if (*name == NULL)
1103 return -ENOMEM;
1104
1105 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1106 *namelen);
1107
1108 *index = btrfs_inode_extref_index(eb, extref);
1109 if (parent_objectid)
1110 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1111
1112 return 0;
1113}
1114
1115static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1116 u32 *namelen, char **name, u64 *index)
1117{
1118 struct btrfs_inode_ref *ref;
1119
1120 ref = (struct btrfs_inode_ref *)ref_ptr;
1121
1122 *namelen = btrfs_inode_ref_name_len(eb, ref);
1123 *name = kmalloc(*namelen, GFP_NOFS);
1124 if (*name == NULL)
1125 return -ENOMEM;
1126
1127 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1128
1129 *index = btrfs_inode_ref_index(eb, ref);
1130
1131 return 0;
1132}
1133
Chris Masone02119d2008-09-05 16:13:11 -04001134/*
1135 * replay one inode back reference item found in the log tree.
1136 * eb, slot and key refer to the buffer and key found in the log tree.
1137 * root is the destination we are replaying into, and path is for temp
1138 * use by this function. (it should be released on return).
1139 */
1140static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1141 struct btrfs_root *root,
1142 struct btrfs_root *log,
1143 struct btrfs_path *path,
1144 struct extent_buffer *eb, int slot,
1145 struct btrfs_key *key)
1146{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001147 struct inode *dir = NULL;
1148 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001149 unsigned long ref_ptr;
1150 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001151 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001152 int namelen;
1153 int ret;
liuboc622ae62011-03-26 08:01:12 -04001154 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001155 int log_ref_ver = 0;
1156 u64 parent_objectid;
1157 u64 inode_objectid;
Chris Masonf46dbe32012-10-09 11:17:20 -04001158 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001159 int ref_struct_size;
1160
1161 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1162 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1163
1164 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1165 struct btrfs_inode_extref *r;
1166
1167 ref_struct_size = sizeof(struct btrfs_inode_extref);
1168 log_ref_ver = 1;
1169 r = (struct btrfs_inode_extref *)ref_ptr;
1170 parent_objectid = btrfs_inode_extref_parent(eb, r);
1171 } else {
1172 ref_struct_size = sizeof(struct btrfs_inode_ref);
1173 parent_objectid = key->offset;
1174 }
1175 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001176
Chris Masone02119d2008-09-05 16:13:11 -04001177 /*
1178 * it is possible that we didn't log all the parent directories
1179 * for a given inode. If we don't find the dir, just don't
1180 * copy the back ref in. The link count fixup code will take
1181 * care of the rest
1182 */
Mark Fashehf1863732012-08-08 11:32:27 -07001183 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001184 if (!dir) {
1185 ret = -ENOENT;
1186 goto out;
1187 }
Chris Masone02119d2008-09-05 16:13:11 -04001188
Mark Fashehf1863732012-08-08 11:32:27 -07001189 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001190 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001191 ret = -EIO;
1192 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001193 }
Chris Masone02119d2008-09-05 16:13:11 -04001194
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001195 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001196 if (log_ref_ver) {
1197 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1198 &ref_index, &parent_objectid);
1199 /*
1200 * parent object can change from one array
1201 * item to another.
1202 */
1203 if (!dir)
1204 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001205 if (!dir) {
1206 ret = -ENOENT;
1207 goto out;
1208 }
Mark Fashehf1863732012-08-08 11:32:27 -07001209 } else {
1210 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1211 &ref_index);
1212 }
1213 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001214 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001215
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001216 /* if we already have a perfect match, we're done */
1217 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001218 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001219 /*
1220 * look for a conflicting back reference in the
1221 * metadata. if we find one we have to unlink that name
1222 * of the file before we add our new link. Later on, we
1223 * overwrite any existing back reference, and we don't
1224 * want to create dangling pointers in the directory.
1225 */
Chris Masone02119d2008-09-05 16:13:11 -04001226
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001227 if (!search_done) {
1228 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001229 dir, inode, eb,
1230 inode_objectid,
1231 parent_objectid,
1232 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001233 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001234 if (ret) {
1235 if (ret == 1)
1236 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001237 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001238 }
Chris Masone02119d2008-09-05 16:13:11 -04001239 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001240
1241 /* insert our name */
1242 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001243 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001244 if (ret)
1245 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001246
1247 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001248 }
liuboc622ae62011-03-26 08:01:12 -04001249
Mark Fashehf1863732012-08-08 11:32:27 -07001250 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001251 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001252 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001253 if (log_ref_ver) {
1254 iput(dir);
1255 dir = NULL;
1256 }
Chris Masone02119d2008-09-05 16:13:11 -04001257 }
Chris Masone02119d2008-09-05 16:13:11 -04001258
1259 /* finally write the back reference in the inode */
1260 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001261out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001262 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001263 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001264 iput(dir);
1265 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001266 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001267}
1268
Yan, Zhengc71bf092009-11-12 09:34:40 +00001269static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001270 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001271{
1272 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001273
David Sterba9c4f61f2015-01-02 19:12:57 +01001274 ret = btrfs_insert_orphan_item(trans, root, ino);
1275 if (ret == -EEXIST)
1276 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001277
Yan, Zhengc71bf092009-11-12 09:34:40 +00001278 return ret;
1279}
1280
Mark Fashehf1863732012-08-08 11:32:27 -07001281static int count_inode_extrefs(struct btrfs_root *root,
1282 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001283{
Mark Fashehf1863732012-08-08 11:32:27 -07001284 int ret = 0;
1285 int name_len;
1286 unsigned int nlink = 0;
1287 u32 item_size;
1288 u32 cur_offset = 0;
1289 u64 inode_objectid = btrfs_ino(inode);
1290 u64 offset = 0;
1291 unsigned long ptr;
1292 struct btrfs_inode_extref *extref;
1293 struct extent_buffer *leaf;
1294
1295 while (1) {
1296 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1297 &extref, &offset);
1298 if (ret)
1299 break;
1300
1301 leaf = path->nodes[0];
1302 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1303 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001304 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001305
1306 while (cur_offset < item_size) {
1307 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1308 name_len = btrfs_inode_extref_name_len(leaf, extref);
1309
1310 nlink++;
1311
1312 cur_offset += name_len + sizeof(*extref);
1313 }
1314
1315 offset++;
1316 btrfs_release_path(path);
1317 }
1318 btrfs_release_path(path);
1319
Filipe Manana2c2c4522015-01-13 16:40:04 +00001320 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001321 return ret;
1322 return nlink;
1323}
1324
1325static int count_inode_refs(struct btrfs_root *root,
1326 struct inode *inode, struct btrfs_path *path)
1327{
Chris Masone02119d2008-09-05 16:13:11 -04001328 int ret;
1329 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001330 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001331 unsigned long ptr;
1332 unsigned long ptr_end;
1333 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001334 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001335
Li Zefan33345d012011-04-20 10:31:50 +08001336 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001337 key.type = BTRFS_INODE_REF_KEY;
1338 key.offset = (u64)-1;
1339
Chris Masond3977122009-01-05 21:25:51 -05001340 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001341 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1342 if (ret < 0)
1343 break;
1344 if (ret > 0) {
1345 if (path->slots[0] == 0)
1346 break;
1347 path->slots[0]--;
1348 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001349process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001350 btrfs_item_key_to_cpu(path->nodes[0], &key,
1351 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001352 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001353 key.type != BTRFS_INODE_REF_KEY)
1354 break;
1355 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1356 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1357 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001358 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001359 struct btrfs_inode_ref *ref;
1360
1361 ref = (struct btrfs_inode_ref *)ptr;
1362 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1363 ref);
1364 ptr = (unsigned long)(ref + 1) + name_len;
1365 nlink++;
1366 }
1367
1368 if (key.offset == 0)
1369 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001370 if (path->slots[0] > 0) {
1371 path->slots[0]--;
1372 goto process_slot;
1373 }
Chris Masone02119d2008-09-05 16:13:11 -04001374 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001375 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001376 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001377 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001378
1379 return nlink;
1380}
1381
1382/*
1383 * There are a few corners where the link count of the file can't
1384 * be properly maintained during replay. So, instead of adding
1385 * lots of complexity to the log code, we just scan the backrefs
1386 * for any file that has been through replay.
1387 *
1388 * The scan will update the link count on the inode to reflect the
1389 * number of back refs found. If it goes down to zero, the iput
1390 * will free the inode.
1391 */
1392static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1393 struct btrfs_root *root,
1394 struct inode *inode)
1395{
1396 struct btrfs_path *path;
1397 int ret;
1398 u64 nlink = 0;
1399 u64 ino = btrfs_ino(inode);
1400
1401 path = btrfs_alloc_path();
1402 if (!path)
1403 return -ENOMEM;
1404
1405 ret = count_inode_refs(root, inode, path);
1406 if (ret < 0)
1407 goto out;
1408
1409 nlink = ret;
1410
1411 ret = count_inode_extrefs(root, inode, path);
Mark Fashehf1863732012-08-08 11:32:27 -07001412 if (ret < 0)
1413 goto out;
1414
1415 nlink += ret;
1416
1417 ret = 0;
1418
Chris Masone02119d2008-09-05 16:13:11 -04001419 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001420 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001421 btrfs_update_inode(trans, root, inode);
1422 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001423 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001424
Yan, Zhengc71bf092009-11-12 09:34:40 +00001425 if (inode->i_nlink == 0) {
1426 if (S_ISDIR(inode->i_mode)) {
1427 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001428 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001429 if (ret)
1430 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001431 }
Li Zefan33345d012011-04-20 10:31:50 +08001432 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001433 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001434
Mark Fashehf1863732012-08-08 11:32:27 -07001435out:
1436 btrfs_free_path(path);
1437 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001438}
1439
1440static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1441 struct btrfs_root *root,
1442 struct btrfs_path *path)
1443{
1444 int ret;
1445 struct btrfs_key key;
1446 struct inode *inode;
1447
1448 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1449 key.type = BTRFS_ORPHAN_ITEM_KEY;
1450 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001451 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001452 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1453 if (ret < 0)
1454 break;
1455
1456 if (ret == 1) {
1457 if (path->slots[0] == 0)
1458 break;
1459 path->slots[0]--;
1460 }
1461
1462 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1463 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1464 key.type != BTRFS_ORPHAN_ITEM_KEY)
1465 break;
1466
1467 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001468 if (ret)
1469 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001470
David Sterbab3b4aa72011-04-21 01:20:15 +02001471 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001472 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001473 if (!inode)
1474 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001475
1476 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001477 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001478 if (ret)
1479 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001480
Chris Mason12fcfd22009-03-24 10:24:20 -04001481 /*
1482 * fixup on a directory may create new entries,
1483 * make sure we always look for the highset possible
1484 * offset
1485 */
1486 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001487 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001488 ret = 0;
1489out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001490 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001491 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001492}
1493
1494
1495/*
1496 * record a given inode in the fixup dir so we can check its link
1497 * count when replay is done. The link count is incremented here
1498 * so the inode won't go away until we check it
1499 */
1500static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1501 struct btrfs_root *root,
1502 struct btrfs_path *path,
1503 u64 objectid)
1504{
1505 struct btrfs_key key;
1506 int ret = 0;
1507 struct inode *inode;
1508
1509 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001510 if (!inode)
1511 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001512
1513 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001514 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001515 key.offset = objectid;
1516
1517 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1518
David Sterbab3b4aa72011-04-21 01:20:15 +02001519 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001520 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001521 if (!inode->i_nlink)
1522 set_nlink(inode, 1);
1523 else
Zach Brown8b558c52013-10-16 12:10:34 -07001524 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001525 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001526 } else if (ret == -EEXIST) {
1527 ret = 0;
1528 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001529 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001530 }
1531 iput(inode);
1532
1533 return ret;
1534}
1535
1536/*
1537 * when replaying the log for a directory, we only insert names
1538 * for inodes that actually exist. This means an fsync on a directory
1539 * does not implicitly fsync all the new files in it
1540 */
1541static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1542 struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04001543 u64 dirid, u64 index,
Zhaolei60d53eb2015-08-17 18:44:46 +08001544 char *name, int name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001545 struct btrfs_key *location)
1546{
1547 struct inode *inode;
1548 struct inode *dir;
1549 int ret;
1550
1551 inode = read_one_inode(root, location->objectid);
1552 if (!inode)
1553 return -ENOENT;
1554
1555 dir = read_one_inode(root, dirid);
1556 if (!dir) {
1557 iput(inode);
1558 return -EIO;
1559 }
Josef Bacikd5554382013-09-11 14:17:00 -04001560
Chris Masone02119d2008-09-05 16:13:11 -04001561 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1562
1563 /* FIXME, put inode into FIXUP list */
1564
1565 iput(inode);
1566 iput(dir);
1567 return ret;
1568}
1569
1570/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001571 * Return true if an inode reference exists in the log for the given name,
1572 * inode and parent inode.
1573 */
1574static bool name_in_log_ref(struct btrfs_root *log_root,
1575 const char *name, const int name_len,
1576 const u64 dirid, const u64 ino)
1577{
1578 struct btrfs_key search_key;
1579
1580 search_key.objectid = ino;
1581 search_key.type = BTRFS_INODE_REF_KEY;
1582 search_key.offset = dirid;
1583 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1584 return true;
1585
1586 search_key.type = BTRFS_INODE_EXTREF_KEY;
1587 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1588 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1589 return true;
1590
1591 return false;
1592}
1593
1594/*
Chris Masone02119d2008-09-05 16:13:11 -04001595 * take a single entry in a log directory item and replay it into
1596 * the subvolume.
1597 *
1598 * if a conflicting item exists in the subdirectory already,
1599 * the inode it points to is unlinked and put into the link count
1600 * fix up tree.
1601 *
1602 * If a name from the log points to a file or directory that does
1603 * not exist in the FS, it is skipped. fsyncs on directories
1604 * do not force down inodes inside that directory, just changes to the
1605 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001606 *
1607 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1608 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001609 */
1610static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1611 struct btrfs_root *root,
1612 struct btrfs_path *path,
1613 struct extent_buffer *eb,
1614 struct btrfs_dir_item *di,
1615 struct btrfs_key *key)
1616{
1617 char *name;
1618 int name_len;
1619 struct btrfs_dir_item *dst_di;
1620 struct btrfs_key found_key;
1621 struct btrfs_key log_key;
1622 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001623 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001624 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001625 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001626 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001627 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001628
1629 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001630 if (!dir)
1631 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001632
1633 name_len = btrfs_dir_name_len(eb, di);
1634 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001635 if (!name) {
1636 ret = -ENOMEM;
1637 goto out;
1638 }
liubo2a29edc2011-01-26 06:22:08 +00001639
Chris Masone02119d2008-09-05 16:13:11 -04001640 log_type = btrfs_dir_type(eb, di);
1641 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1642 name_len);
1643
1644 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001645 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1646 if (exists == 0)
1647 exists = 1;
1648 else
1649 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001650 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001651
Chris Masone02119d2008-09-05 16:13:11 -04001652 if (key->type == BTRFS_DIR_ITEM_KEY) {
1653 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1654 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001655 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001656 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1657 key->objectid,
1658 key->offset, name,
1659 name_len, 1);
1660 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001661 /* Corruption */
1662 ret = -EINVAL;
1663 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001664 }
David Sterbac7040052011-04-19 18:00:01 +02001665 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001666 /* we need a sequence number to insert, so we only
1667 * do inserts for the BTRFS_DIR_INDEX_KEY types
1668 */
1669 if (key->type != BTRFS_DIR_INDEX_KEY)
1670 goto out;
1671 goto insert;
1672 }
1673
1674 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1675 /* the existing item matches the logged item */
1676 if (found_key.objectid == log_key.objectid &&
1677 found_key.type == log_key.type &&
1678 found_key.offset == log_key.offset &&
1679 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001680 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001681 goto out;
1682 }
1683
1684 /*
1685 * don't drop the conflicting directory entry if the inode
1686 * for the new entry doesn't exist
1687 */
Chris Mason4bef0842008-09-08 11:18:08 -04001688 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001689 goto out;
1690
Chris Masone02119d2008-09-05 16:13:11 -04001691 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001692 if (ret)
1693 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001694
1695 if (key->type == BTRFS_DIR_INDEX_KEY)
1696 goto insert;
1697out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001698 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001699 if (!ret && update_size) {
1700 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1701 ret = btrfs_update_inode(trans, root, dir);
1702 }
Chris Masone02119d2008-09-05 16:13:11 -04001703 kfree(name);
1704 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001705 if (!ret && name_added)
1706 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001707 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001708
1709insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001710 if (name_in_log_ref(root->log_root, name, name_len,
1711 key->objectid, log_key.objectid)) {
1712 /* The dentry will be added later. */
1713 ret = 0;
1714 update_size = false;
1715 goto out;
1716 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001717 btrfs_release_path(path);
Zhaolei60d53eb2015-08-17 18:44:46 +08001718 ret = insert_one_name(trans, root, key->objectid, key->offset,
1719 name, name_len, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001720 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001721 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001722 if (!ret)
1723 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001724 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001725 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001726 goto out;
1727}
1728
1729/*
1730 * find all the names in a directory item and reconcile them into
1731 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1732 * one name in a directory item, but the same code gets used for
1733 * both directory index types
1734 */
1735static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1736 struct btrfs_root *root,
1737 struct btrfs_path *path,
1738 struct extent_buffer *eb, int slot,
1739 struct btrfs_key *key)
1740{
Filipe Mananabb53eda2015-07-15 23:26:43 +01001741 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001742 u32 item_size = btrfs_item_size_nr(eb, slot);
1743 struct btrfs_dir_item *di;
1744 int name_len;
1745 unsigned long ptr;
1746 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001747 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001748
1749 ptr = btrfs_item_ptr_offset(eb, slot);
1750 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001751 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001752 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001753 if (verify_dir_item(root, eb, di))
1754 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001755 name_len = btrfs_dir_name_len(eb, di);
1756 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001757 if (ret < 0)
1758 break;
Chris Masone02119d2008-09-05 16:13:11 -04001759 ptr = (unsigned long)(di + 1);
1760 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001761
1762 /*
1763 * If this entry refers to a non-directory (directories can not
1764 * have a link count > 1) and it was added in the transaction
1765 * that was not committed, make sure we fixup the link count of
1766 * the inode it the entry points to. Otherwise something like
1767 * the following would result in a directory pointing to an
1768 * inode with a wrong link that does not account for this dir
1769 * entry:
1770 *
1771 * mkdir testdir
1772 * touch testdir/foo
1773 * touch testdir/bar
1774 * sync
1775 *
1776 * ln testdir/bar testdir/bar_link
1777 * ln testdir/foo testdir/foo_link
1778 * xfs_io -c "fsync" testdir/bar
1779 *
1780 * <power failure>
1781 *
1782 * mount fs, log replay happens
1783 *
1784 * File foo would remain with a link count of 1 when it has two
1785 * entries pointing to it in the directory testdir. This would
1786 * make it impossible to ever delete the parent directory has
1787 * it would result in stale dentries that can never be deleted.
1788 */
1789 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1790 struct btrfs_key di_key;
1791
1792 if (!fixup_path) {
1793 fixup_path = btrfs_alloc_path();
1794 if (!fixup_path) {
1795 ret = -ENOMEM;
1796 break;
1797 }
1798 }
1799
1800 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1801 ret = link_to_fixup_dir(trans, root, fixup_path,
1802 di_key.objectid);
1803 if (ret)
1804 break;
1805 }
1806 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001807 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01001808 btrfs_free_path(fixup_path);
1809 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001810}
1811
1812/*
1813 * directory replay has two parts. There are the standard directory
1814 * items in the log copied from the subvolume, and range items
1815 * created in the log while the subvolume was logged.
1816 *
1817 * The range items tell us which parts of the key space the log
1818 * is authoritative for. During replay, if a key in the subvolume
1819 * directory is in a logged range item, but not actually in the log
1820 * that means it was deleted from the directory before the fsync
1821 * and should be removed.
1822 */
1823static noinline int find_dir_range(struct btrfs_root *root,
1824 struct btrfs_path *path,
1825 u64 dirid, int key_type,
1826 u64 *start_ret, u64 *end_ret)
1827{
1828 struct btrfs_key key;
1829 u64 found_end;
1830 struct btrfs_dir_log_item *item;
1831 int ret;
1832 int nritems;
1833
1834 if (*start_ret == (u64)-1)
1835 return 1;
1836
1837 key.objectid = dirid;
1838 key.type = key_type;
1839 key.offset = *start_ret;
1840
1841 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1842 if (ret < 0)
1843 goto out;
1844 if (ret > 0) {
1845 if (path->slots[0] == 0)
1846 goto out;
1847 path->slots[0]--;
1848 }
1849 if (ret != 0)
1850 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1851
1852 if (key.type != key_type || key.objectid != dirid) {
1853 ret = 1;
1854 goto next;
1855 }
1856 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1857 struct btrfs_dir_log_item);
1858 found_end = btrfs_dir_log_end(path->nodes[0], item);
1859
1860 if (*start_ret >= key.offset && *start_ret <= found_end) {
1861 ret = 0;
1862 *start_ret = key.offset;
1863 *end_ret = found_end;
1864 goto out;
1865 }
1866 ret = 1;
1867next:
1868 /* check the next slot in the tree to see if it is a valid item */
1869 nritems = btrfs_header_nritems(path->nodes[0]);
1870 if (path->slots[0] >= nritems) {
1871 ret = btrfs_next_leaf(root, path);
1872 if (ret)
1873 goto out;
1874 } else {
1875 path->slots[0]++;
1876 }
1877
1878 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1879
1880 if (key.type != key_type || key.objectid != dirid) {
1881 ret = 1;
1882 goto out;
1883 }
1884 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1885 struct btrfs_dir_log_item);
1886 found_end = btrfs_dir_log_end(path->nodes[0], item);
1887 *start_ret = key.offset;
1888 *end_ret = found_end;
1889 ret = 0;
1890out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001891 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001892 return ret;
1893}
1894
1895/*
1896 * this looks for a given directory item in the log. If the directory
1897 * item is not in the log, the item is removed and the inode it points
1898 * to is unlinked
1899 */
1900static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1901 struct btrfs_root *root,
1902 struct btrfs_root *log,
1903 struct btrfs_path *path,
1904 struct btrfs_path *log_path,
1905 struct inode *dir,
1906 struct btrfs_key *dir_key)
1907{
1908 int ret;
1909 struct extent_buffer *eb;
1910 int slot;
1911 u32 item_size;
1912 struct btrfs_dir_item *di;
1913 struct btrfs_dir_item *log_di;
1914 int name_len;
1915 unsigned long ptr;
1916 unsigned long ptr_end;
1917 char *name;
1918 struct inode *inode;
1919 struct btrfs_key location;
1920
1921again:
1922 eb = path->nodes[0];
1923 slot = path->slots[0];
1924 item_size = btrfs_item_size_nr(eb, slot);
1925 ptr = btrfs_item_ptr_offset(eb, slot);
1926 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001927 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001928 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001929 if (verify_dir_item(root, eb, di)) {
1930 ret = -EIO;
1931 goto out;
1932 }
1933
Chris Masone02119d2008-09-05 16:13:11 -04001934 name_len = btrfs_dir_name_len(eb, di);
1935 name = kmalloc(name_len, GFP_NOFS);
1936 if (!name) {
1937 ret = -ENOMEM;
1938 goto out;
1939 }
1940 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1941 name_len);
1942 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001943 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001944 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1945 dir_key->objectid,
1946 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001947 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001948 log_di = btrfs_lookup_dir_index_item(trans, log,
1949 log_path,
1950 dir_key->objectid,
1951 dir_key->offset,
1952 name, name_len, 0);
1953 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001954 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04001955 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001956 btrfs_release_path(path);
1957 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001958 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001959 if (!inode) {
1960 kfree(name);
1961 return -EIO;
1962 }
Chris Masone02119d2008-09-05 16:13:11 -04001963
1964 ret = link_to_fixup_dir(trans, root,
1965 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04001966 if (ret) {
1967 kfree(name);
1968 iput(inode);
1969 goto out;
1970 }
1971
Zach Brown8b558c52013-10-16 12:10:34 -07001972 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001973 ret = btrfs_unlink_inode(trans, root, dir, inode,
1974 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04001975 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001976 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001977 kfree(name);
1978 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001979 if (ret)
1980 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001981
1982 /* there might still be more names under this key
1983 * check and repeat if required
1984 */
1985 ret = btrfs_search_slot(NULL, root, dir_key, path,
1986 0, 0);
1987 if (ret == 0)
1988 goto again;
1989 ret = 0;
1990 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001991 } else if (IS_ERR(log_di)) {
1992 kfree(name);
1993 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04001994 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001995 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001996 kfree(name);
1997
1998 ptr = (unsigned long)(di + 1);
1999 ptr += name_len;
2000 }
2001 ret = 0;
2002out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002003 btrfs_release_path(path);
2004 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002005 return ret;
2006}
2007
Filipe Manana4f764e52015-02-23 19:53:35 +00002008static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2009 struct btrfs_root *root,
2010 struct btrfs_root *log,
2011 struct btrfs_path *path,
2012 const u64 ino)
2013{
2014 struct btrfs_key search_key;
2015 struct btrfs_path *log_path;
2016 int i;
2017 int nritems;
2018 int ret;
2019
2020 log_path = btrfs_alloc_path();
2021 if (!log_path)
2022 return -ENOMEM;
2023
2024 search_key.objectid = ino;
2025 search_key.type = BTRFS_XATTR_ITEM_KEY;
2026 search_key.offset = 0;
2027again:
2028 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2029 if (ret < 0)
2030 goto out;
2031process_leaf:
2032 nritems = btrfs_header_nritems(path->nodes[0]);
2033 for (i = path->slots[0]; i < nritems; i++) {
2034 struct btrfs_key key;
2035 struct btrfs_dir_item *di;
2036 struct btrfs_dir_item *log_di;
2037 u32 total_size;
2038 u32 cur;
2039
2040 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2041 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2042 ret = 0;
2043 goto out;
2044 }
2045
2046 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2047 total_size = btrfs_item_size_nr(path->nodes[0], i);
2048 cur = 0;
2049 while (cur < total_size) {
2050 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2051 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2052 u32 this_len = sizeof(*di) + name_len + data_len;
2053 char *name;
2054
2055 name = kmalloc(name_len, GFP_NOFS);
2056 if (!name) {
2057 ret = -ENOMEM;
2058 goto out;
2059 }
2060 read_extent_buffer(path->nodes[0], name,
2061 (unsigned long)(di + 1), name_len);
2062
2063 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2064 name, name_len, 0);
2065 btrfs_release_path(log_path);
2066 if (!log_di) {
2067 /* Doesn't exist in log tree, so delete it. */
2068 btrfs_release_path(path);
2069 di = btrfs_lookup_xattr(trans, root, path, ino,
2070 name, name_len, -1);
2071 kfree(name);
2072 if (IS_ERR(di)) {
2073 ret = PTR_ERR(di);
2074 goto out;
2075 }
2076 ASSERT(di);
2077 ret = btrfs_delete_one_dir_name(trans, root,
2078 path, di);
2079 if (ret)
2080 goto out;
2081 btrfs_release_path(path);
2082 search_key = key;
2083 goto again;
2084 }
2085 kfree(name);
2086 if (IS_ERR(log_di)) {
2087 ret = PTR_ERR(log_di);
2088 goto out;
2089 }
2090 cur += this_len;
2091 di = (struct btrfs_dir_item *)((char *)di + this_len);
2092 }
2093 }
2094 ret = btrfs_next_leaf(root, path);
2095 if (ret > 0)
2096 ret = 0;
2097 else if (ret == 0)
2098 goto process_leaf;
2099out:
2100 btrfs_free_path(log_path);
2101 btrfs_release_path(path);
2102 return ret;
2103}
2104
2105
Chris Masone02119d2008-09-05 16:13:11 -04002106/*
2107 * deletion replay happens before we copy any new directory items
2108 * out of the log or out of backreferences from inodes. It
2109 * scans the log to find ranges of keys that log is authoritative for,
2110 * and then scans the directory to find items in those ranges that are
2111 * not present in the log.
2112 *
2113 * Anything we don't find in the log is unlinked and removed from the
2114 * directory.
2115 */
2116static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2117 struct btrfs_root *root,
2118 struct btrfs_root *log,
2119 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002120 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002121{
2122 u64 range_start;
2123 u64 range_end;
2124 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2125 int ret = 0;
2126 struct btrfs_key dir_key;
2127 struct btrfs_key found_key;
2128 struct btrfs_path *log_path;
2129 struct inode *dir;
2130
2131 dir_key.objectid = dirid;
2132 dir_key.type = BTRFS_DIR_ITEM_KEY;
2133 log_path = btrfs_alloc_path();
2134 if (!log_path)
2135 return -ENOMEM;
2136
2137 dir = read_one_inode(root, dirid);
2138 /* it isn't an error if the inode isn't there, that can happen
2139 * because we replay the deletes before we copy in the inode item
2140 * from the log
2141 */
2142 if (!dir) {
2143 btrfs_free_path(log_path);
2144 return 0;
2145 }
2146again:
2147 range_start = 0;
2148 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002149 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002150 if (del_all)
2151 range_end = (u64)-1;
2152 else {
2153 ret = find_dir_range(log, path, dirid, key_type,
2154 &range_start, &range_end);
2155 if (ret != 0)
2156 break;
2157 }
Chris Masone02119d2008-09-05 16:13:11 -04002158
2159 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002160 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002161 int nritems;
2162 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2163 0, 0);
2164 if (ret < 0)
2165 goto out;
2166
2167 nritems = btrfs_header_nritems(path->nodes[0]);
2168 if (path->slots[0] >= nritems) {
2169 ret = btrfs_next_leaf(root, path);
2170 if (ret)
2171 break;
2172 }
2173 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2174 path->slots[0]);
2175 if (found_key.objectid != dirid ||
2176 found_key.type != dir_key.type)
2177 goto next_type;
2178
2179 if (found_key.offset > range_end)
2180 break;
2181
2182 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002183 log_path, dir,
2184 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002185 if (ret)
2186 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002187 if (found_key.offset == (u64)-1)
2188 break;
2189 dir_key.offset = found_key.offset + 1;
2190 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002191 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002192 if (range_end == (u64)-1)
2193 break;
2194 range_start = range_end + 1;
2195 }
2196
2197next_type:
2198 ret = 0;
2199 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2200 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2201 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002202 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002203 goto again;
2204 }
2205out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002206 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002207 btrfs_free_path(log_path);
2208 iput(dir);
2209 return ret;
2210}
2211
2212/*
2213 * the process_func used to replay items from the log tree. This
2214 * gets called in two different stages. The first stage just looks
2215 * for inodes and makes sure they are all copied into the subvolume.
2216 *
2217 * The second stage copies all the other item types from the log into
2218 * the subvolume. The two stage approach is slower, but gets rid of
2219 * lots of complexity around inodes referencing other inodes that exist
2220 * only in the log (references come from either directory items or inode
2221 * back refs).
2222 */
2223static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2224 struct walk_control *wc, u64 gen)
2225{
2226 int nritems;
2227 struct btrfs_path *path;
2228 struct btrfs_root *root = wc->replay_dest;
2229 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002230 int level;
2231 int i;
2232 int ret;
2233
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002234 ret = btrfs_read_buffer(eb, gen);
2235 if (ret)
2236 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002237
2238 level = btrfs_header_level(eb);
2239
2240 if (level != 0)
2241 return 0;
2242
2243 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002244 if (!path)
2245 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002246
2247 nritems = btrfs_header_nritems(eb);
2248 for (i = 0; i < nritems; i++) {
2249 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002250
2251 /* inode keys are done during the first stage */
2252 if (key.type == BTRFS_INODE_ITEM_KEY &&
2253 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002254 struct btrfs_inode_item *inode_item;
2255 u32 mode;
2256
2257 inode_item = btrfs_item_ptr(eb, i,
2258 struct btrfs_inode_item);
Filipe Manana4f764e52015-02-23 19:53:35 +00002259 ret = replay_xattr_deletes(wc->trans, root, log,
2260 path, key.objectid);
2261 if (ret)
2262 break;
Chris Masone02119d2008-09-05 16:13:11 -04002263 mode = btrfs_inode_mode(eb, inode_item);
2264 if (S_ISDIR(mode)) {
2265 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002266 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002267 if (ret)
2268 break;
Chris Masone02119d2008-09-05 16:13:11 -04002269 }
2270 ret = overwrite_item(wc->trans, root, path,
2271 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002272 if (ret)
2273 break;
Chris Masone02119d2008-09-05 16:13:11 -04002274
Yan, Zhengc71bf092009-11-12 09:34:40 +00002275 /* for regular files, make sure corresponding
2276 * orhpan item exist. extents past the new EOF
2277 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002278 */
2279 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002280 ret = insert_orphan_item(wc->trans, root,
2281 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002282 if (ret)
2283 break;
Chris Masone02119d2008-09-05 16:13:11 -04002284 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002285
Chris Masone02119d2008-09-05 16:13:11 -04002286 ret = link_to_fixup_dir(wc->trans, root,
2287 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002288 if (ret)
2289 break;
Chris Masone02119d2008-09-05 16:13:11 -04002290 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002291
2292 if (key.type == BTRFS_DIR_INDEX_KEY &&
2293 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2294 ret = replay_one_dir_item(wc->trans, root, path,
2295 eb, i, &key);
2296 if (ret)
2297 break;
2298 }
2299
Chris Masone02119d2008-09-05 16:13:11 -04002300 if (wc->stage < LOG_WALK_REPLAY_ALL)
2301 continue;
2302
2303 /* these keys are simply copied */
2304 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2305 ret = overwrite_item(wc->trans, root, path,
2306 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002307 if (ret)
2308 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002309 } else if (key.type == BTRFS_INODE_REF_KEY ||
2310 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002311 ret = add_inode_ref(wc->trans, root, log, path,
2312 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002313 if (ret && ret != -ENOENT)
2314 break;
2315 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002316 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2317 ret = replay_one_extent(wc->trans, root, path,
2318 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002319 if (ret)
2320 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002321 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002322 ret = replay_one_dir_item(wc->trans, root, path,
2323 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002324 if (ret)
2325 break;
Chris Masone02119d2008-09-05 16:13:11 -04002326 }
2327 }
2328 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002329 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002330}
2331
Chris Masond3977122009-01-05 21:25:51 -05002332static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002333 struct btrfs_root *root,
2334 struct btrfs_path *path, int *level,
2335 struct walk_control *wc)
2336{
2337 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002338 u64 bytenr;
2339 u64 ptr_gen;
2340 struct extent_buffer *next;
2341 struct extent_buffer *cur;
2342 struct extent_buffer *parent;
2343 u32 blocksize;
2344 int ret = 0;
2345
2346 WARN_ON(*level < 0);
2347 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2348
Chris Masond3977122009-01-05 21:25:51 -05002349 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002350 WARN_ON(*level < 0);
2351 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2352 cur = path->nodes[*level];
2353
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302354 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002355
2356 if (path->slots[*level] >=
2357 btrfs_header_nritems(cur))
2358 break;
2359
2360 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2361 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
David Sterba707e8a02014-06-04 19:22:26 +02002362 blocksize = root->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002363
2364 parent = path->nodes[*level];
2365 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002366
David Sterbaa83fffb2014-06-15 02:39:54 +02002367 next = btrfs_find_create_tree_block(root, bytenr);
liubo2a29edc2011-01-26 06:22:08 +00002368 if (!next)
2369 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002370
Chris Masone02119d2008-09-05 16:13:11 -04002371 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002372 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002373 if (ret) {
2374 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002375 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002376 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002377
Chris Masone02119d2008-09-05 16:13:11 -04002378 path->slots[*level]++;
2379 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002380 ret = btrfs_read_buffer(next, ptr_gen);
2381 if (ret) {
2382 free_extent_buffer(next);
2383 return ret;
2384 }
Chris Masone02119d2008-09-05 16:13:11 -04002385
Josef Bacik681ae502013-10-07 15:11:00 -04002386 if (trans) {
2387 btrfs_tree_lock(next);
2388 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002389 clean_tree_block(trans, root->fs_info,
2390 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002391 btrfs_wait_tree_block_writeback(next);
2392 btrfs_tree_unlock(next);
2393 }
Chris Masone02119d2008-09-05 16:13:11 -04002394
Chris Masone02119d2008-09-05 16:13:11 -04002395 WARN_ON(root_owner !=
2396 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002397 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002398 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002399 if (ret) {
2400 free_extent_buffer(next);
2401 return ret;
2402 }
Chris Masone02119d2008-09-05 16:13:11 -04002403 }
2404 free_extent_buffer(next);
2405 continue;
2406 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002407 ret = btrfs_read_buffer(next, ptr_gen);
2408 if (ret) {
2409 free_extent_buffer(next);
2410 return ret;
2411 }
Chris Masone02119d2008-09-05 16:13:11 -04002412
2413 WARN_ON(*level <= 0);
2414 if (path->nodes[*level-1])
2415 free_extent_buffer(path->nodes[*level-1]);
2416 path->nodes[*level-1] = next;
2417 *level = btrfs_header_level(next);
2418 path->slots[*level] = 0;
2419 cond_resched();
2420 }
2421 WARN_ON(*level < 0);
2422 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2423
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002424 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002425
2426 cond_resched();
2427 return 0;
2428}
2429
Chris Masond3977122009-01-05 21:25:51 -05002430static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002431 struct btrfs_root *root,
2432 struct btrfs_path *path, int *level,
2433 struct walk_control *wc)
2434{
2435 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002436 int i;
2437 int slot;
2438 int ret;
2439
Chris Masond3977122009-01-05 21:25:51 -05002440 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002441 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002442 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002443 path->slots[i]++;
2444 *level = i;
2445 WARN_ON(*level == 0);
2446 return 0;
2447 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002448 struct extent_buffer *parent;
2449 if (path->nodes[*level] == root->node)
2450 parent = path->nodes[*level];
2451 else
2452 parent = path->nodes[*level + 1];
2453
2454 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002455 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002456 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002457 if (ret)
2458 return ret;
2459
Chris Masone02119d2008-09-05 16:13:11 -04002460 if (wc->free) {
2461 struct extent_buffer *next;
2462
2463 next = path->nodes[*level];
2464
Josef Bacik681ae502013-10-07 15:11:00 -04002465 if (trans) {
2466 btrfs_tree_lock(next);
2467 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002468 clean_tree_block(trans, root->fs_info,
2469 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002470 btrfs_wait_tree_block_writeback(next);
2471 btrfs_tree_unlock(next);
2472 }
Chris Masone02119d2008-09-05 16:13:11 -04002473
Chris Masone02119d2008-09-05 16:13:11 -04002474 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002475 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002476 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002477 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002478 if (ret)
2479 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002480 }
2481 free_extent_buffer(path->nodes[*level]);
2482 path->nodes[*level] = NULL;
2483 *level = i + 1;
2484 }
2485 }
2486 return 1;
2487}
2488
2489/*
2490 * drop the reference count on the tree rooted at 'snap'. This traverses
2491 * the tree freeing any blocks that have a ref count of zero after being
2492 * decremented.
2493 */
2494static int walk_log_tree(struct btrfs_trans_handle *trans,
2495 struct btrfs_root *log, struct walk_control *wc)
2496{
2497 int ret = 0;
2498 int wret;
2499 int level;
2500 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002501 int orig_level;
2502
2503 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002504 if (!path)
2505 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002506
2507 level = btrfs_header_level(log->node);
2508 orig_level = level;
2509 path->nodes[level] = log->node;
2510 extent_buffer_get(log->node);
2511 path->slots[level] = 0;
2512
Chris Masond3977122009-01-05 21:25:51 -05002513 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002514 wret = walk_down_log_tree(trans, log, path, &level, wc);
2515 if (wret > 0)
2516 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002517 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002518 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002519 goto out;
2520 }
Chris Masone02119d2008-09-05 16:13:11 -04002521
2522 wret = walk_up_log_tree(trans, log, path, &level, wc);
2523 if (wret > 0)
2524 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002525 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002526 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002527 goto out;
2528 }
Chris Masone02119d2008-09-05 16:13:11 -04002529 }
2530
2531 /* was the root node processed? if not, catch it here */
2532 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002533 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002534 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002535 if (ret)
2536 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002537 if (wc->free) {
2538 struct extent_buffer *next;
2539
2540 next = path->nodes[orig_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, log->fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002546 btrfs_wait_tree_block_writeback(next);
2547 btrfs_tree_unlock(next);
2548 }
Chris Masone02119d2008-09-05 16:13:11 -04002549
Chris Masone02119d2008-09-05 16:13:11 -04002550 WARN_ON(log->root_key.objectid !=
2551 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002552 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002553 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002554 if (ret)
2555 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002556 }
2557 }
2558
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002559out:
Chris Masone02119d2008-09-05 16:13:11 -04002560 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002561 return ret;
2562}
2563
Yan Zheng7237f182009-01-21 12:54:03 -05002564/*
2565 * helper function to update the item for a given subvolumes log root
2566 * in the tree of log roots
2567 */
2568static int update_log_root(struct btrfs_trans_handle *trans,
2569 struct btrfs_root *log)
2570{
2571 int ret;
2572
2573 if (log->log_transid == 1) {
2574 /* insert root item on the first sync */
2575 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2576 &log->root_key, &log->root_item);
2577 } else {
2578 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2579 &log->root_key, &log->root_item);
2580 }
2581 return ret;
2582}
2583
Zhaolei60d53eb2015-08-17 18:44:46 +08002584static void wait_log_commit(struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002585{
2586 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002587 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002588
Yan Zheng7237f182009-01-21 12:54:03 -05002589 /*
2590 * we only allow two pending log transactions at a time,
2591 * so we know that if ours is more than 2 older than the
2592 * current transaction, we're done
2593 */
Chris Masone02119d2008-09-05 16:13:11 -04002594 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002595 prepare_to_wait(&root->log_commit_wait[index],
2596 &wait, TASK_UNINTERRUPTIBLE);
2597 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002598
Miao Xied1433de2014-02-20 18:08:59 +08002599 if (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002600 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002601 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002602
Yan Zheng7237f182009-01-21 12:54:03 -05002603 finish_wait(&root->log_commit_wait[index], &wait);
2604 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002605 } while (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002606 atomic_read(&root->log_commit[index]));
Yan Zheng7237f182009-01-21 12:54:03 -05002607}
2608
Zhaolei60d53eb2015-08-17 18:44:46 +08002609static void wait_for_writer(struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002610{
2611 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002612
2613 while (atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002614 prepare_to_wait(&root->log_writer_wait,
2615 &wait, TASK_UNINTERRUPTIBLE);
2616 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002617 if (atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002618 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002619 finish_wait(&root->log_writer_wait, &wait);
Filipe Manana575849e2015-02-11 11:12:39 +00002620 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002621 }
Chris Masone02119d2008-09-05 16:13:11 -04002622}
2623
Miao Xie8b050d32014-02-20 18:08:58 +08002624static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2625 struct btrfs_log_ctx *ctx)
2626{
2627 if (!ctx)
2628 return;
2629
2630 mutex_lock(&root->log_mutex);
2631 list_del_init(&ctx->list);
2632 mutex_unlock(&root->log_mutex);
2633}
2634
2635/*
2636 * Invoked in log mutex context, or be sure there is no other task which
2637 * can access the list.
2638 */
2639static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2640 int index, int error)
2641{
2642 struct btrfs_log_ctx *ctx;
2643
2644 if (!error) {
2645 INIT_LIST_HEAD(&root->log_ctxs[index]);
2646 return;
2647 }
2648
2649 list_for_each_entry(ctx, &root->log_ctxs[index], list)
2650 ctx->log_ret = error;
2651
2652 INIT_LIST_HEAD(&root->log_ctxs[index]);
2653}
2654
Chris Masone02119d2008-09-05 16:13:11 -04002655/*
2656 * btrfs_sync_log does sends a given tree log down to the disk and
2657 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002658 * you know that any inodes previously logged are safely on disk only
2659 * if it returns 0.
2660 *
2661 * Any other return value means you need to call btrfs_commit_transaction.
2662 * Some of the edge cases for fsyncing directories that have had unlinks
2663 * or renames done in the past mean that sometimes the only safe
2664 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2665 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002666 */
2667int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002668 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002669{
Yan Zheng7237f182009-01-21 12:54:03 -05002670 int index1;
2671 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002672 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002673 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002674 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002675 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002676 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002677 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002678 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002679
Yan Zheng7237f182009-01-21 12:54:03 -05002680 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002681 log_transid = ctx->log_transid;
2682 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002683 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002684 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002685 }
Miao Xied1433de2014-02-20 18:08:59 +08002686
2687 index1 = log_transid % 2;
2688 if (atomic_read(&root->log_commit[index1])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002689 wait_log_commit(root, log_transid);
Miao Xied1433de2014-02-20 18:08:59 +08002690 mutex_unlock(&root->log_mutex);
2691 return ctx->log_ret;
2692 }
2693 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002694 atomic_set(&root->log_commit[index1], 1);
2695
2696 /* wait for previous tree log sync to complete */
2697 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Zhaolei60d53eb2015-08-17 18:44:46 +08002698 wait_log_commit(root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002699
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002700 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002701 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002702 /* when we're on an ssd, just kick the log commit out */
Miao Xie27cdeb72014-04-02 19:51:05 +08002703 if (!btrfs_test_opt(root, SSD) &&
2704 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002705 mutex_unlock(&root->log_mutex);
2706 schedule_timeout_uninterruptible(1);
2707 mutex_lock(&root->log_mutex);
2708 }
Zhaolei60d53eb2015-08-17 18:44:46 +08002709 wait_for_writer(root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002710 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002711 break;
2712 }
Chris Masond0c803c2008-09-11 16:17:57 -04002713
Chris Mason12fcfd22009-03-24 10:24:20 -04002714 /* bail out if we need to do a full commit */
Miao Xie995946d2014-04-02 19:51:06 +08002715 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002716 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002717 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002718 mutex_unlock(&root->log_mutex);
2719 goto out;
2720 }
2721
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002722 if (log_transid % 2 == 0)
2723 mark = EXTENT_DIRTY;
2724 else
2725 mark = EXTENT_NEW;
2726
Chris Mason690587d2009-10-13 13:29:19 -04002727 /* we start IO on all the marked extents here, but we don't actually
2728 * wait for them until later.
2729 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002730 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002731 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002732 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002733 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002734 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002735 btrfs_free_logged_extents(log, log_transid);
Miao Xie995946d2014-04-02 19:51:06 +08002736 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002737 mutex_unlock(&root->log_mutex);
2738 goto out;
2739 }
Yan Zheng7237f182009-01-21 12:54:03 -05002740
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002741 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002742
Yan Zheng7237f182009-01-21 12:54:03 -05002743 root->log_transid++;
2744 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002745 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002746 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002747 * IO has been started, blocks of the log tree have WRITTEN flag set
2748 * in their headers. new modifications of the log will be written to
2749 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002750 */
2751 mutex_unlock(&root->log_mutex);
2752
Miao Xied1433de2014-02-20 18:08:59 +08002753 btrfs_init_log_ctx(&root_log_ctx);
2754
Yan Zheng7237f182009-01-21 12:54:03 -05002755 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002756 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002757 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002758
2759 index2 = log_root_tree->log_transid % 2;
2760 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2761 root_log_ctx.log_transid = log_root_tree->log_transid;
2762
Yan Zheng7237f182009-01-21 12:54:03 -05002763 mutex_unlock(&log_root_tree->log_mutex);
2764
2765 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002766
2767 mutex_lock(&log_root_tree->log_mutex);
2768 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2769 smp_mb();
2770 if (waitqueue_active(&log_root_tree->log_writer_wait))
2771 wake_up(&log_root_tree->log_writer_wait);
2772 }
2773
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002774 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002775 if (!list_empty(&root_log_ctx.list))
2776 list_del_init(&root_log_ctx.list);
2777
Miao Xiec6adc9c2013-05-28 10:05:39 +00002778 blk_finish_plug(&plug);
Miao Xie995946d2014-04-02 19:51:06 +08002779 btrfs_set_log_full_commit(root->fs_info, trans);
2780
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002781 if (ret != -ENOSPC) {
2782 btrfs_abort_transaction(trans, root, ret);
2783 mutex_unlock(&log_root_tree->log_mutex);
2784 goto out;
2785 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002786 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002787 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002788 mutex_unlock(&log_root_tree->log_mutex);
2789 ret = -EAGAIN;
2790 goto out;
2791 }
2792
Miao Xied1433de2014-02-20 18:08:59 +08002793 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08002794 blk_finish_plug(&plug);
Miao Xied1433de2014-02-20 18:08:59 +08002795 mutex_unlock(&log_root_tree->log_mutex);
2796 ret = root_log_ctx.log_ret;
2797 goto out;
2798 }
Miao Xie8b050d32014-02-20 18:08:58 +08002799
Miao Xied1433de2014-02-20 18:08:59 +08002800 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05002801 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002802 blk_finish_plug(&plug);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002803 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages,
2804 mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05002805 btrfs_wait_logged_extents(trans, log, log_transid);
Zhaolei60d53eb2015-08-17 18:44:46 +08002806 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002807 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002808 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002809 if (!ret)
2810 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002811 goto out;
2812 }
Miao Xied1433de2014-02-20 18:08:59 +08002813 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002814 atomic_set(&log_root_tree->log_commit[index2], 1);
2815
Chris Mason12fcfd22009-03-24 10:24:20 -04002816 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
Zhaolei60d53eb2015-08-17 18:44:46 +08002817 wait_log_commit(log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002818 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002819 }
Yan Zheng7237f182009-01-21 12:54:03 -05002820
Zhaolei60d53eb2015-08-17 18:44:46 +08002821 wait_for_writer(log_root_tree);
Chris Mason12fcfd22009-03-24 10:24:20 -04002822
2823 /*
2824 * now that we've moved on to the tree of log tree roots,
2825 * check the full commit flag again
2826 */
Miao Xie995946d2014-04-02 19:51:06 +08002827 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002828 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002829 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002830 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002831 mutex_unlock(&log_root_tree->log_mutex);
2832 ret = -EAGAIN;
2833 goto out_wake_log_root;
2834 }
Yan Zheng7237f182009-01-21 12:54:03 -05002835
Miao Xiec6adc9c2013-05-28 10:05:39 +00002836 ret = btrfs_write_marked_extents(log_root_tree,
2837 &log_root_tree->dirty_log_pages,
2838 EXTENT_DIRTY | EXTENT_NEW);
2839 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002840 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002841 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002842 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002843 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002844 mutex_unlock(&log_root_tree->log_mutex);
2845 goto out_wake_log_root;
2846 }
Filipe Manana5ab5e442014-11-13 16:59:53 +00002847 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2848 if (!ret)
2849 ret = btrfs_wait_marked_extents(log_root_tree,
2850 &log_root_tree->dirty_log_pages,
2851 EXTENT_NEW | EXTENT_DIRTY);
2852 if (ret) {
2853 btrfs_set_log_full_commit(root->fs_info, trans);
2854 btrfs_free_logged_extents(log, log_transid);
2855 mutex_unlock(&log_root_tree->log_mutex);
2856 goto out_wake_log_root;
2857 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05002858 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002859
David Sterba6c417612011-04-13 15:41:04 +02002860 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002861 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002862 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002863 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002864
Yan Zheng7237f182009-01-21 12:54:03 -05002865 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002866 mutex_unlock(&log_root_tree->log_mutex);
2867
2868 /*
2869 * nobody else is going to jump in and write the the ctree
2870 * super here because the log_commit atomic below is protecting
2871 * us. We must be called with a transaction handle pinning
2872 * the running transaction open, so a full commit can't hop
2873 * in and cause problems either.
2874 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002875 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002876 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002877 btrfs_set_log_full_commit(root->fs_info, trans);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002878 btrfs_abort_transaction(trans, root, ret);
2879 goto out_wake_log_root;
2880 }
Yan Zheng7237f182009-01-21 12:54:03 -05002881
Chris Mason257c62e2009-10-13 13:21:08 -04002882 mutex_lock(&root->log_mutex);
2883 if (root->last_log_commit < log_transid)
2884 root->last_log_commit = log_transid;
2885 mutex_unlock(&root->log_mutex);
2886
Chris Mason12fcfd22009-03-24 10:24:20 -04002887out_wake_log_root:
Miao Xie8b050d32014-02-20 18:08:58 +08002888 /*
2889 * We needn't get log_mutex here because we are sure all
2890 * the other tasks are blocked.
2891 */
2892 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2893
Miao Xied1433de2014-02-20 18:08:59 +08002894 mutex_lock(&log_root_tree->log_mutex);
2895 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002896 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002897 mutex_unlock(&log_root_tree->log_mutex);
2898
Yan Zheng7237f182009-01-21 12:54:03 -05002899 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2900 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002901out:
Miao Xie8b050d32014-02-20 18:08:58 +08002902 /* See above. */
2903 btrfs_remove_all_log_ctxs(root, index1, ret);
2904
Miao Xied1433de2014-02-20 18:08:59 +08002905 mutex_lock(&root->log_mutex);
2906 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002907 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002908 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002909
Yan Zheng7237f182009-01-21 12:54:03 -05002910 if (waitqueue_active(&root->log_commit_wait[index1]))
2911 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002912 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002913}
2914
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002915static void free_log_tree(struct btrfs_trans_handle *trans,
2916 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002917{
2918 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002919 u64 start;
2920 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002921 struct walk_control wc = {
2922 .free = 1,
2923 .process_func = process_one_buffer
2924 };
2925
Josef Bacik681ae502013-10-07 15:11:00 -04002926 ret = walk_log_tree(trans, log, &wc);
2927 /* I don't think this can happen but just in case */
2928 if (ret)
2929 btrfs_abort_transaction(trans, log, ret);
Chris Masone02119d2008-09-05 16:13:11 -04002930
Chris Masond3977122009-01-05 21:25:51 -05002931 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002932 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002933 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2934 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002935 if (ret)
2936 break;
2937
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002938 clear_extent_bits(&log->dirty_log_pages, start, end,
2939 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002940 }
2941
Josef Bacik2ab28f32012-10-12 15:27:49 -04002942 /*
2943 * We may have short-circuited the log tree with the full commit logic
2944 * and left ordered extents on our list, so clear these out to keep us
2945 * from leaking inodes and memory.
2946 */
2947 btrfs_free_logged_extents(log, 0);
2948 btrfs_free_logged_extents(log, 1);
2949
Yan Zheng7237f182009-01-21 12:54:03 -05002950 free_extent_buffer(log->node);
2951 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002952}
2953
2954/*
2955 * free all the extents used by the tree log. This should be called
2956 * at commit time of the full transaction
2957 */
2958int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2959{
2960 if (root->log_root) {
2961 free_log_tree(trans, root->log_root);
2962 root->log_root = NULL;
2963 }
2964 return 0;
2965}
2966
2967int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2968 struct btrfs_fs_info *fs_info)
2969{
2970 if (fs_info->log_root_tree) {
2971 free_log_tree(trans, fs_info->log_root_tree);
2972 fs_info->log_root_tree = NULL;
2973 }
Chris Masone02119d2008-09-05 16:13:11 -04002974 return 0;
2975}
2976
2977/*
Chris Masone02119d2008-09-05 16:13:11 -04002978 * If both a file and directory are logged, and unlinks or renames are
2979 * mixed in, we have a few interesting corners:
2980 *
2981 * create file X in dir Y
2982 * link file X to X.link in dir Y
2983 * fsync file X
2984 * unlink file X but leave X.link
2985 * fsync dir Y
2986 *
2987 * After a crash we would expect only X.link to exist. But file X
2988 * didn't get fsync'd again so the log has back refs for X and X.link.
2989 *
2990 * We solve this by removing directory entries and inode backrefs from the
2991 * log when a file that was logged in the current transaction is
2992 * unlinked. Any later fsync will include the updated log entries, and
2993 * we'll be able to reconstruct the proper directory items from backrefs.
2994 *
2995 * This optimizations allows us to avoid relogging the entire inode
2996 * or the entire directory.
2997 */
2998int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2999 struct btrfs_root *root,
3000 const char *name, int name_len,
3001 struct inode *dir, u64 index)
3002{
3003 struct btrfs_root *log;
3004 struct btrfs_dir_item *di;
3005 struct btrfs_path *path;
3006 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003007 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003008 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003009 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003010
Chris Mason3a5f1d42008-09-11 15:53:37 -04003011 if (BTRFS_I(dir)->logged_trans < trans->transid)
3012 return 0;
3013
Chris Masone02119d2008-09-05 16:13:11 -04003014 ret = join_running_log_trans(root);
3015 if (ret)
3016 return 0;
3017
3018 mutex_lock(&BTRFS_I(dir)->log_mutex);
3019
3020 log = root->log_root;
3021 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003022 if (!path) {
3023 err = -ENOMEM;
3024 goto out_unlock;
3025 }
liubo2a29edc2011-01-26 06:22:08 +00003026
Li Zefan33345d012011-04-20 10:31:50 +08003027 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003028 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003029 if (IS_ERR(di)) {
3030 err = PTR_ERR(di);
3031 goto fail;
3032 }
3033 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003034 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3035 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003036 if (ret) {
3037 err = ret;
3038 goto fail;
3039 }
Chris Masone02119d2008-09-05 16:13:11 -04003040 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003041 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003042 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003043 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003044 if (IS_ERR(di)) {
3045 err = PTR_ERR(di);
3046 goto fail;
3047 }
3048 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003049 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3050 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003051 if (ret) {
3052 err = ret;
3053 goto fail;
3054 }
Chris Masone02119d2008-09-05 16:13:11 -04003055 }
3056
3057 /* update the directory size in the log to reflect the names
3058 * we have removed
3059 */
3060 if (bytes_del) {
3061 struct btrfs_key key;
3062
Li Zefan33345d012011-04-20 10:31:50 +08003063 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003064 key.offset = 0;
3065 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003066 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003067
3068 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003069 if (ret < 0) {
3070 err = ret;
3071 goto fail;
3072 }
Chris Masone02119d2008-09-05 16:13:11 -04003073 if (ret == 0) {
3074 struct btrfs_inode_item *item;
3075 u64 i_size;
3076
3077 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3078 struct btrfs_inode_item);
3079 i_size = btrfs_inode_size(path->nodes[0], item);
3080 if (i_size > bytes_del)
3081 i_size -= bytes_del;
3082 else
3083 i_size = 0;
3084 btrfs_set_inode_size(path->nodes[0], item, i_size);
3085 btrfs_mark_buffer_dirty(path->nodes[0]);
3086 } else
3087 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003088 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003089 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003090fail:
Chris Masone02119d2008-09-05 16:13:11 -04003091 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003092out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04003093 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003094 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003095 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003096 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003097 } else if (ret < 0)
3098 btrfs_abort_transaction(trans, root, ret);
3099
Chris Mason12fcfd22009-03-24 10:24:20 -04003100 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003101
Andi Kleen411fc6b2010-10-29 15:14:31 -04003102 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003103}
3104
3105/* see comments for btrfs_del_dir_entries_in_log */
3106int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3107 struct btrfs_root *root,
3108 const char *name, int name_len,
3109 struct inode *inode, u64 dirid)
3110{
3111 struct btrfs_root *log;
3112 u64 index;
3113 int ret;
3114
Chris Mason3a5f1d42008-09-11 15:53:37 -04003115 if (BTRFS_I(inode)->logged_trans < trans->transid)
3116 return 0;
3117
Chris Masone02119d2008-09-05 16:13:11 -04003118 ret = join_running_log_trans(root);
3119 if (ret)
3120 return 0;
3121 log = root->log_root;
3122 mutex_lock(&BTRFS_I(inode)->log_mutex);
3123
Li Zefan33345d012011-04-20 10:31:50 +08003124 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003125 dirid, &index);
3126 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003127 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003128 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003129 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003130 } else if (ret < 0 && ret != -ENOENT)
3131 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003132 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003133
Chris Masone02119d2008-09-05 16:13:11 -04003134 return ret;
3135}
3136
3137/*
3138 * creates a range item in the log for 'dirid'. first_offset and
3139 * last_offset tell us which parts of the key space the log should
3140 * be considered authoritative for.
3141 */
3142static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3143 struct btrfs_root *log,
3144 struct btrfs_path *path,
3145 int key_type, u64 dirid,
3146 u64 first_offset, u64 last_offset)
3147{
3148 int ret;
3149 struct btrfs_key key;
3150 struct btrfs_dir_log_item *item;
3151
3152 key.objectid = dirid;
3153 key.offset = first_offset;
3154 if (key_type == BTRFS_DIR_ITEM_KEY)
3155 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3156 else
3157 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3158 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003159 if (ret)
3160 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003161
3162 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3163 struct btrfs_dir_log_item);
3164 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3165 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003166 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003167 return 0;
3168}
3169
3170/*
3171 * log all the items included in the current transaction for a given
3172 * directory. This also creates the range items in the log tree required
3173 * to replay anything deleted before the fsync
3174 */
3175static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3176 struct btrfs_root *root, struct inode *inode,
3177 struct btrfs_path *path,
3178 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003179 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003180 u64 min_offset, u64 *last_offset_ret)
3181{
3182 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003183 struct btrfs_root *log = root->log_root;
3184 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003185 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003186 int ret;
3187 int i;
3188 int nritems;
3189 u64 first_offset = min_offset;
3190 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08003191 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003192
3193 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003194
Li Zefan33345d012011-04-20 10:31:50 +08003195 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003196 min_key.type = key_type;
3197 min_key.offset = min_offset;
3198
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003199 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003200
3201 /*
3202 * we didn't find anything from this transaction, see if there
3203 * is anything at all
3204 */
Li Zefan33345d012011-04-20 10:31:50 +08003205 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3206 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003207 min_key.type = key_type;
3208 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003209 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003210 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3211 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003212 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003213 return ret;
3214 }
Li Zefan33345d012011-04-20 10:31:50 +08003215 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003216
3217 /* if ret == 0 there are items for this type,
3218 * create a range to tell us the last key of this type.
3219 * otherwise, there are no items in this directory after
3220 * *min_offset, and we create a range to indicate that.
3221 */
3222 if (ret == 0) {
3223 struct btrfs_key tmp;
3224 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3225 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003226 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003227 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003228 }
3229 goto done;
3230 }
3231
3232 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003233 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003234 if (ret == 0) {
3235 struct btrfs_key tmp;
3236 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3237 if (key_type == tmp.type) {
3238 first_offset = tmp.offset;
3239 ret = overwrite_item(trans, log, dst_path,
3240 path->nodes[0], path->slots[0],
3241 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003242 if (ret) {
3243 err = ret;
3244 goto done;
3245 }
Chris Masone02119d2008-09-05 16:13:11 -04003246 }
3247 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003248 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003249
3250 /* find the first key from this transaction again */
3251 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303252 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003253 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003254
3255 /*
3256 * we have a block from this transaction, log every item in it
3257 * from our directory
3258 */
Chris Masond3977122009-01-05 21:25:51 -05003259 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003260 struct btrfs_key tmp;
3261 src = path->nodes[0];
3262 nritems = btrfs_header_nritems(src);
3263 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003264 struct btrfs_dir_item *di;
3265
Chris Masone02119d2008-09-05 16:13:11 -04003266 btrfs_item_key_to_cpu(src, &min_key, i);
3267
Li Zefan33345d012011-04-20 10:31:50 +08003268 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003269 goto done;
3270 ret = overwrite_item(trans, log, dst_path, src, i,
3271 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003272 if (ret) {
3273 err = ret;
3274 goto done;
3275 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003276
3277 /*
3278 * We must make sure that when we log a directory entry,
3279 * the corresponding inode, after log replay, has a
3280 * matching link count. For example:
3281 *
3282 * touch foo
3283 * mkdir mydir
3284 * sync
3285 * ln foo mydir/bar
3286 * xfs_io -c "fsync" mydir
3287 * <crash>
3288 * <mount fs and log replay>
3289 *
3290 * Would result in a fsync log that when replayed, our
3291 * file inode would have a link count of 1, but we get
3292 * two directory entries pointing to the same inode.
3293 * After removing one of the names, it would not be
3294 * possible to remove the other name, which resulted
3295 * always in stale file handle errors, and would not
3296 * be possible to rmdir the parent directory, since
3297 * its i_size could never decrement to the value
3298 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3299 */
3300 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3301 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3302 if (ctx &&
3303 (btrfs_dir_transid(src, di) == trans->transid ||
3304 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3305 tmp.type != BTRFS_ROOT_ITEM_KEY)
3306 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003307 }
3308 path->slots[0] = nritems;
3309
3310 /*
3311 * look ahead to the next item and see if it is also
3312 * from this directory and from this transaction
3313 */
3314 ret = btrfs_next_leaf(root, path);
3315 if (ret == 1) {
3316 last_offset = (u64)-1;
3317 goto done;
3318 }
3319 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003320 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003321 last_offset = (u64)-1;
3322 goto done;
3323 }
3324 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3325 ret = overwrite_item(trans, log, dst_path,
3326 path->nodes[0], path->slots[0],
3327 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003328 if (ret)
3329 err = ret;
3330 else
3331 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003332 goto done;
3333 }
3334 }
3335done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003336 btrfs_release_path(path);
3337 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003338
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003339 if (err == 0) {
3340 *last_offset_ret = last_offset;
3341 /*
3342 * insert the log range keys to indicate where the log
3343 * is valid
3344 */
3345 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003346 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003347 if (ret)
3348 err = ret;
3349 }
3350 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003351}
3352
3353/*
3354 * logging directories is very similar to logging inodes, We find all the items
3355 * from the current transaction and write them to the log.
3356 *
3357 * The recovery code scans the directory in the subvolume, and if it finds a
3358 * key in the range logged that is not present in the log tree, then it means
3359 * that dir entry was unlinked during the transaction.
3360 *
3361 * In order for that scan to work, we must include one key smaller than
3362 * the smallest logged by this transaction and one key larger than the largest
3363 * key logged by this transaction.
3364 */
3365static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3366 struct btrfs_root *root, struct inode *inode,
3367 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003368 struct btrfs_path *dst_path,
3369 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003370{
3371 u64 min_key;
3372 u64 max_key;
3373 int ret;
3374 int key_type = BTRFS_DIR_ITEM_KEY;
3375
3376again:
3377 min_key = 0;
3378 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003379 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003380 ret = log_dir_items(trans, root, inode, path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003381 dst_path, key_type, ctx, min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003382 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003383 if (ret)
3384 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003385 if (max_key == (u64)-1)
3386 break;
3387 min_key = max_key + 1;
3388 }
3389
3390 if (key_type == BTRFS_DIR_ITEM_KEY) {
3391 key_type = BTRFS_DIR_INDEX_KEY;
3392 goto again;
3393 }
3394 return 0;
3395}
3396
3397/*
3398 * a helper function to drop items from the log before we relog an
3399 * inode. max_key_type indicates the highest item type to remove.
3400 * This cannot be run for file data extents because it does not
3401 * free the extents they point to.
3402 */
3403static int drop_objectid_items(struct btrfs_trans_handle *trans,
3404 struct btrfs_root *log,
3405 struct btrfs_path *path,
3406 u64 objectid, int max_key_type)
3407{
3408 int ret;
3409 struct btrfs_key key;
3410 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003411 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003412
3413 key.objectid = objectid;
3414 key.type = max_key_type;
3415 key.offset = (u64)-1;
3416
Chris Masond3977122009-01-05 21:25:51 -05003417 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003418 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003419 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003420 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003421 break;
3422
3423 if (path->slots[0] == 0)
3424 break;
3425
3426 path->slots[0]--;
3427 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3428 path->slots[0]);
3429
3430 if (found_key.objectid != objectid)
3431 break;
3432
Josef Bacik18ec90d2012-09-28 11:56:28 -04003433 found_key.offset = 0;
3434 found_key.type = 0;
3435 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3436 &start_slot);
3437
3438 ret = btrfs_del_items(trans, log, path, start_slot,
3439 path->slots[0] - start_slot + 1);
3440 /*
3441 * If start slot isn't 0 then we don't need to re-search, we've
3442 * found the last guy with the objectid in this tree.
3443 */
3444 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003445 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003446 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003447 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003448 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003449 if (ret > 0)
3450 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003451 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003452}
3453
Josef Bacik94edf4a2012-09-25 14:56:25 -04003454static void fill_inode_item(struct btrfs_trans_handle *trans,
3455 struct extent_buffer *leaf,
3456 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003457 struct inode *inode, int log_inode_only,
3458 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003459{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003460 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003461
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003462 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003463
3464 if (log_inode_only) {
3465 /* set the generation to zero so the recover code
3466 * can tell the difference between an logging
3467 * just to say 'this inode exists' and a logging
3468 * to say 'update this inode with these values'
3469 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003470 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003471 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003472 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003473 btrfs_set_token_inode_generation(leaf, item,
3474 BTRFS_I(inode)->generation,
3475 &token);
3476 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003477 }
3478
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003479 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3480 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3481 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3482 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3483
David Sterbaa937b972014-12-12 17:39:12 +01003484 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003485 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003486 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003487 inode->i_atime.tv_nsec, &token);
3488
David Sterbaa937b972014-12-12 17:39:12 +01003489 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003490 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003491 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003492 inode->i_mtime.tv_nsec, &token);
3493
David Sterbaa937b972014-12-12 17:39:12 +01003494 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003495 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003496 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003497 inode->i_ctime.tv_nsec, &token);
3498
3499 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3500 &token);
3501
3502 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3503 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3504 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3505 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3506 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003507}
3508
Josef Bacika95249b2012-10-11 16:17:34 -04003509static int log_inode_item(struct btrfs_trans_handle *trans,
3510 struct btrfs_root *log, struct btrfs_path *path,
3511 struct inode *inode)
3512{
3513 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003514 int ret;
3515
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003516 ret = btrfs_insert_empty_item(trans, log, path,
3517 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003518 sizeof(*inode_item));
3519 if (ret && ret != -EEXIST)
3520 return ret;
3521 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3522 struct btrfs_inode_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003523 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003524 btrfs_release_path(path);
3525 return 0;
3526}
3527
Chris Mason31ff1cd2008-09-11 16:17:57 -04003528static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003529 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003530 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003531 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003532 int start_slot, int nr, int inode_only,
3533 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003534{
3535 unsigned long src_offset;
3536 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003537 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003538 struct btrfs_file_extent_item *extent;
3539 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003540 struct extent_buffer *src = src_path->nodes[0];
3541 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003542 int ret;
3543 struct btrfs_key *ins_keys;
3544 u32 *ins_sizes;
3545 char *ins_data;
3546 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003547 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003548 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003549 bool has_extents = false;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003550 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003551 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003552
3553 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003554
3555 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3556 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003557 if (!ins_data)
3558 return -ENOMEM;
3559
Josef Bacik16e75492013-10-22 12:18:51 -04003560 first_key.objectid = (u64)-1;
3561
Chris Mason31ff1cd2008-09-11 16:17:57 -04003562 ins_sizes = (u32 *)ins_data;
3563 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3564
3565 for (i = 0; i < nr; i++) {
3566 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3567 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3568 }
3569 ret = btrfs_insert_empty_items(trans, log, dst_path,
3570 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003571 if (ret) {
3572 kfree(ins_data);
3573 return ret;
3574 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003575
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003576 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003577 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3578 dst_path->slots[0]);
3579
3580 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3581
Josef Bacik16e75492013-10-22 12:18:51 -04003582 if ((i == (nr - 1)))
3583 last_key = ins_keys[i];
3584
Josef Bacik94edf4a2012-09-25 14:56:25 -04003585 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003586 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3587 dst_path->slots[0],
3588 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003589 fill_inode_item(trans, dst_path->nodes[0], inode_item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003590 inode, inode_only == LOG_INODE_EXISTS,
3591 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003592 } else {
3593 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3594 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003595 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003596
Josef Bacik16e75492013-10-22 12:18:51 -04003597 /*
3598 * We set need_find_last_extent here in case we know we were
3599 * processing other items and then walk into the first extent in
3600 * the inode. If we don't hit an extent then nothing changes,
3601 * we'll do the last search the next time around.
3602 */
3603 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3604 has_extents = true;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003605 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003606 first_key = ins_keys[i];
3607 } else {
3608 need_find_last_extent = false;
3609 }
3610
Chris Mason31ff1cd2008-09-11 16:17:57 -04003611 /* take a reference on file data extents so that truncates
3612 * or deletes of this inode don't have to relog the inode
3613 * again
3614 */
David Sterba962a2982014-06-04 18:41:45 +02003615 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003616 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003617 int found_type;
3618 extent = btrfs_item_ptr(src, start_slot + i,
3619 struct btrfs_file_extent_item);
3620
liubo8e531cd2011-05-06 10:36:09 +08003621 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3622 continue;
3623
Chris Mason31ff1cd2008-09-11 16:17:57 -04003624 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003625 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003626 u64 ds, dl, cs, cl;
3627 ds = btrfs_file_extent_disk_bytenr(src,
3628 extent);
3629 /* ds == 0 is a hole */
3630 if (ds == 0)
3631 continue;
3632
3633 dl = btrfs_file_extent_disk_num_bytes(src,
3634 extent);
3635 cs = btrfs_file_extent_offset(src, extent);
3636 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003637 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003638 if (btrfs_file_extent_compression(src,
3639 extent)) {
3640 cs = 0;
3641 cl = dl;
3642 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003643
3644 ret = btrfs_lookup_csums_range(
3645 log->fs_info->csum_root,
3646 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003647 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003648 if (ret) {
3649 btrfs_release_path(dst_path);
3650 kfree(ins_data);
3651 return ret;
3652 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003653 }
3654 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003655 }
3656
3657 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003658 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003659 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003660
3661 /*
3662 * we have to do this after the loop above to avoid changing the
3663 * log tree while trying to change the log tree.
3664 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003665 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003666 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003667 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3668 struct btrfs_ordered_sum,
3669 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003670 if (!ret)
3671 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003672 list_del(&sums->list);
3673 kfree(sums);
3674 }
Josef Bacik16e75492013-10-22 12:18:51 -04003675
3676 if (!has_extents)
3677 return ret;
3678
Filipe Manana74121f7c2014-08-07 12:00:44 +01003679 if (need_find_last_extent && *last_extent == first_key.offset) {
3680 /*
3681 * We don't have any leafs between our current one and the one
3682 * we processed before that can have file extent items for our
3683 * inode (and have a generation number smaller than our current
3684 * transaction id).
3685 */
3686 need_find_last_extent = false;
3687 }
3688
Josef Bacik16e75492013-10-22 12:18:51 -04003689 /*
3690 * Because we use btrfs_search_forward we could skip leaves that were
3691 * not modified and then assume *last_extent is valid when it really
3692 * isn't. So back up to the previous leaf and read the end of the last
3693 * extent before we go and fill in holes.
3694 */
3695 if (need_find_last_extent) {
3696 u64 len;
3697
3698 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3699 if (ret < 0)
3700 return ret;
3701 if (ret)
3702 goto fill_holes;
3703 if (src_path->slots[0])
3704 src_path->slots[0]--;
3705 src = src_path->nodes[0];
3706 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3707 if (key.objectid != btrfs_ino(inode) ||
3708 key.type != BTRFS_EXTENT_DATA_KEY)
3709 goto fill_holes;
3710 extent = btrfs_item_ptr(src, src_path->slots[0],
3711 struct btrfs_file_extent_item);
3712 if (btrfs_file_extent_type(src, extent) ==
3713 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003714 len = btrfs_file_extent_inline_len(src,
3715 src_path->slots[0],
3716 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003717 *last_extent = ALIGN(key.offset + len,
3718 log->sectorsize);
3719 } else {
3720 len = btrfs_file_extent_num_bytes(src, extent);
3721 *last_extent = key.offset + len;
3722 }
3723 }
3724fill_holes:
3725 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3726 * things could have happened
3727 *
3728 * 1) A merge could have happened, so we could currently be on a leaf
3729 * that holds what we were copying in the first place.
3730 * 2) A split could have happened, and now not all of the items we want
3731 * are on the same leaf.
3732 *
3733 * So we need to adjust how we search for holes, we need to drop the
3734 * path and re-search for the first extent key we found, and then walk
3735 * forward until we hit the last one we copied.
3736 */
3737 if (need_find_last_extent) {
3738 /* btrfs_prev_leaf could return 1 without releasing the path */
3739 btrfs_release_path(src_path);
3740 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3741 src_path, 0, 0);
3742 if (ret < 0)
3743 return ret;
3744 ASSERT(ret == 0);
3745 src = src_path->nodes[0];
3746 i = src_path->slots[0];
3747 } else {
3748 i = start_slot;
3749 }
3750
3751 /*
3752 * Ok so here we need to go through and fill in any holes we may have
3753 * to make sure that holes are punched for those areas in case they had
3754 * extents previously.
3755 */
3756 while (!done) {
3757 u64 offset, len;
3758 u64 extent_end;
3759
3760 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3761 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3762 if (ret < 0)
3763 return ret;
3764 ASSERT(ret == 0);
3765 src = src_path->nodes[0];
3766 i = 0;
3767 }
3768
3769 btrfs_item_key_to_cpu(src, &key, i);
3770 if (!btrfs_comp_cpu_keys(&key, &last_key))
3771 done = true;
3772 if (key.objectid != btrfs_ino(inode) ||
3773 key.type != BTRFS_EXTENT_DATA_KEY) {
3774 i++;
3775 continue;
3776 }
3777 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3778 if (btrfs_file_extent_type(src, extent) ==
3779 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003780 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003781 extent_end = ALIGN(key.offset + len, log->sectorsize);
3782 } else {
3783 len = btrfs_file_extent_num_bytes(src, extent);
3784 extent_end = key.offset + len;
3785 }
3786 i++;
3787
3788 if (*last_extent == key.offset) {
3789 *last_extent = extent_end;
3790 continue;
3791 }
3792 offset = *last_extent;
3793 len = key.offset - *last_extent;
3794 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3795 offset, 0, 0, len, 0, len, 0,
3796 0, 0);
3797 if (ret)
3798 break;
Filipe Manana74121f7c2014-08-07 12:00:44 +01003799 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003800 }
3801 /*
3802 * Need to let the callers know we dropped the path so they should
3803 * re-search.
3804 */
3805 if (!ret && need_find_last_extent)
3806 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003807 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003808}
3809
Josef Bacik5dc562c2012-08-17 13:14:17 -04003810static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3811{
3812 struct extent_map *em1, *em2;
3813
3814 em1 = list_entry(a, struct extent_map, list);
3815 em2 = list_entry(b, struct extent_map, list);
3816
3817 if (em1->start < em2->start)
3818 return -1;
3819 else if (em1->start > em2->start)
3820 return 1;
3821 return 0;
3822}
3823
Filipe Manana8407f552014-09-05 15:14:39 +01003824static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3825 struct inode *inode,
3826 struct btrfs_root *root,
3827 const struct extent_map *em,
3828 const struct list_head *logged_list,
3829 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003830{
Josef Bacik2ab28f32012-10-12 15:27:49 -04003831 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01003832 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003833 u64 mod_start = em->mod_start;
3834 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003835 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003836 u64 csum_offset;
3837 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003838 LIST_HEAD(ordered_sums);
3839 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003840
Filipe Manana8407f552014-09-05 15:14:39 +01003841 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04003842
Filipe Manana8407f552014-09-05 15:14:39 +01003843 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3844 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003845 return 0;
3846
Josef Bacik2ab28f32012-10-12 15:27:49 -04003847 /*
Filipe Manana8407f552014-09-05 15:14:39 +01003848 * Wait far any ordered extent that covers our extent map. If it
3849 * finishes without an error, first check and see if our csums are on
3850 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04003851 */
Miao Xie827463c2014-01-14 20:31:51 +08003852 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003853 struct btrfs_ordered_sum *sum;
3854
3855 if (!mod_len)
3856 break;
3857
Josef Bacik2ab28f32012-10-12 15:27:49 -04003858 if (ordered->file_offset + ordered->len <= mod_start ||
3859 mod_start + mod_len <= ordered->file_offset)
3860 continue;
3861
Filipe Manana8407f552014-09-05 15:14:39 +01003862 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3863 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3864 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3865 const u64 start = ordered->file_offset;
3866 const u64 end = ordered->file_offset + ordered->len - 1;
3867
3868 WARN_ON(ordered->inode != inode);
3869 filemap_fdatawrite_range(inode->i_mapping, start, end);
3870 }
3871
3872 wait_event(ordered->wait,
3873 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3874 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3875
3876 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00003877 /*
3878 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3879 * i_mapping flags, so that the next fsync won't get
3880 * an outdated io error too.
3881 */
3882 btrfs_inode_check_errors(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01003883 *ordered_io_error = true;
3884 break;
3885 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003886 /*
3887 * We are going to copy all the csums on this ordered extent, so
3888 * go ahead and adjust mod_start and mod_len in case this
3889 * ordered extent has already been logged.
3890 */
3891 if (ordered->file_offset > mod_start) {
3892 if (ordered->file_offset + ordered->len >=
3893 mod_start + mod_len)
3894 mod_len = ordered->file_offset - mod_start;
3895 /*
3896 * If we have this case
3897 *
3898 * |--------- logged extent ---------|
3899 * |----- ordered extent ----|
3900 *
3901 * Just don't mess with mod_start and mod_len, we'll
3902 * just end up logging more csums than we need and it
3903 * will be ok.
3904 */
3905 } else {
3906 if (ordered->file_offset + ordered->len <
3907 mod_start + mod_len) {
3908 mod_len = (mod_start + mod_len) -
3909 (ordered->file_offset + ordered->len);
3910 mod_start = ordered->file_offset +
3911 ordered->len;
3912 } else {
3913 mod_len = 0;
3914 }
3915 }
3916
Filipe Manana8407f552014-09-05 15:14:39 +01003917 if (skip_csum)
3918 continue;
3919
Josef Bacik2ab28f32012-10-12 15:27:49 -04003920 /*
3921 * To keep us from looping for the above case of an ordered
3922 * extent that falls inside of the logged extent.
3923 */
3924 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3925 &ordered->flags))
3926 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003927
Josef Bacik2ab28f32012-10-12 15:27:49 -04003928 list_for_each_entry(sum, &ordered->list, list) {
3929 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08003930 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01003931 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003932 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003933 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003934
Filipe Manana8407f552014-09-05 15:14:39 +01003935 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04003936 return ret;
3937
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00003938 if (em->compress_type) {
3939 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01003940 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00003941 } else {
3942 csum_offset = mod_start - em->start;
3943 csum_len = mod_len;
3944 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003945
Josef Bacik70c8a912012-10-11 16:54:30 -04003946 /* block start is already adjusted for the file extent offset. */
3947 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3948 em->block_start + csum_offset,
3949 em->block_start + csum_offset +
3950 csum_len - 1, &ordered_sums, 0);
3951 if (ret)
3952 return ret;
3953
3954 while (!list_empty(&ordered_sums)) {
3955 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3956 struct btrfs_ordered_sum,
3957 list);
3958 if (!ret)
3959 ret = btrfs_csum_file_blocks(trans, log, sums);
3960 list_del(&sums->list);
3961 kfree(sums);
3962 }
3963
3964 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003965}
3966
Filipe Manana8407f552014-09-05 15:14:39 +01003967static int log_one_extent(struct btrfs_trans_handle *trans,
3968 struct inode *inode, struct btrfs_root *root,
3969 const struct extent_map *em,
3970 struct btrfs_path *path,
3971 const struct list_head *logged_list,
3972 struct btrfs_log_ctx *ctx)
3973{
3974 struct btrfs_root *log = root->log_root;
3975 struct btrfs_file_extent_item *fi;
3976 struct extent_buffer *leaf;
3977 struct btrfs_map_token token;
3978 struct btrfs_key key;
3979 u64 extent_offset = em->start - em->orig_start;
3980 u64 block_len;
3981 int ret;
3982 int extent_inserted = 0;
3983 bool ordered_io_err = false;
3984
3985 ret = wait_ordered_extents(trans, inode, root, em, logged_list,
3986 &ordered_io_err);
3987 if (ret)
3988 return ret;
3989
3990 if (ordered_io_err) {
3991 ctx->io_err = -EIO;
3992 return 0;
3993 }
3994
3995 btrfs_init_map_token(&token);
3996
3997 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3998 em->start + em->len, NULL, 0, 1,
3999 sizeof(*fi), &extent_inserted);
4000 if (ret)
4001 return ret;
4002
4003 if (!extent_inserted) {
4004 key.objectid = btrfs_ino(inode);
4005 key.type = BTRFS_EXTENT_DATA_KEY;
4006 key.offset = em->start;
4007
4008 ret = btrfs_insert_empty_item(trans, log, path, &key,
4009 sizeof(*fi));
4010 if (ret)
4011 return ret;
4012 }
4013 leaf = path->nodes[0];
4014 fi = btrfs_item_ptr(leaf, path->slots[0],
4015 struct btrfs_file_extent_item);
4016
Josef Bacik50d9aa92014-11-21 14:52:38 -05004017 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004018 &token);
4019 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4020 btrfs_set_token_file_extent_type(leaf, fi,
4021 BTRFS_FILE_EXTENT_PREALLOC,
4022 &token);
4023 else
4024 btrfs_set_token_file_extent_type(leaf, fi,
4025 BTRFS_FILE_EXTENT_REG,
4026 &token);
4027
4028 block_len = max(em->block_len, em->orig_block_len);
4029 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4030 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4031 em->block_start,
4032 &token);
4033 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4034 &token);
4035 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4036 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4037 em->block_start -
4038 extent_offset, &token);
4039 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4040 &token);
4041 } else {
4042 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4043 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4044 &token);
4045 }
4046
4047 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4048 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4049 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4050 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4051 &token);
4052 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4053 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4054 btrfs_mark_buffer_dirty(leaf);
4055
4056 btrfs_release_path(path);
4057
4058 return ret;
4059}
4060
Josef Bacik5dc562c2012-08-17 13:14:17 -04004061static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4062 struct btrfs_root *root,
4063 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004064 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004065 struct list_head *logged_list,
4066 struct btrfs_log_ctx *ctx)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004067{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004068 struct extent_map *em, *n;
4069 struct list_head extents;
4070 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4071 u64 test_gen;
4072 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004073 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004074
4075 INIT_LIST_HEAD(&extents);
4076
Josef Bacik5dc562c2012-08-17 13:14:17 -04004077 write_lock(&tree->lock);
4078 test_gen = root->fs_info->last_trans_committed;
4079
4080 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4081 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004082
4083 /*
4084 * Just an arbitrary number, this can be really CPU intensive
4085 * once we start getting a lot of extents, and really once we
4086 * have a bunch of extents we just want to commit since it will
4087 * be faster.
4088 */
4089 if (++num > 32768) {
4090 list_del_init(&tree->modified_extents);
4091 ret = -EFBIG;
4092 goto process;
4093 }
4094
Josef Bacik5dc562c2012-08-17 13:14:17 -04004095 if (em->generation <= test_gen)
4096 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004097 /* Need a ref to keep it from getting evicted from cache */
4098 atomic_inc(&em->refs);
4099 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004100 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004101 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004102 }
4103
4104 list_sort(NULL, &extents, extent_cmp);
4105
Josef Bacik2ab28f32012-10-12 15:27:49 -04004106process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004107 while (!list_empty(&extents)) {
4108 em = list_entry(extents.next, struct extent_map, list);
4109
4110 list_del_init(&em->list);
4111
4112 /*
4113 * If we had an error we just need to delete everybody from our
4114 * private list.
4115 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004116 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004117 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004118 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004119 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004120 }
4121
4122 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004123
Filipe Manana8407f552014-09-05 15:14:39 +01004124 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4125 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004126 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004127 clear_em_logging(tree, em);
4128 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004129 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004130 WARN_ON(!list_empty(&extents));
4131 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004132
Josef Bacik5dc562c2012-08-17 13:14:17 -04004133 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004134 return ret;
4135}
4136
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004137static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
4138 struct btrfs_path *path, u64 *size_ret)
4139{
4140 struct btrfs_key key;
4141 int ret;
4142
4143 key.objectid = btrfs_ino(inode);
4144 key.type = BTRFS_INODE_ITEM_KEY;
4145 key.offset = 0;
4146
4147 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4148 if (ret < 0) {
4149 return ret;
4150 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004151 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004152 } else {
4153 struct btrfs_inode_item *item;
4154
4155 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4156 struct btrfs_inode_item);
4157 *size_ret = btrfs_inode_size(path->nodes[0], item);
4158 }
4159
4160 btrfs_release_path(path);
4161 return 0;
4162}
4163
Filipe Manana36283bf2015-06-20 00:44:51 +01004164/*
4165 * At the moment we always log all xattrs. This is to figure out at log replay
4166 * time which xattrs must have their deletion replayed. If a xattr is missing
4167 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4168 * because if a xattr is deleted, the inode is fsynced and a power failure
4169 * happens, causing the log to be replayed the next time the fs is mounted,
4170 * we want the xattr to not exist anymore (same behaviour as other filesystems
4171 * with a journal, ext3/4, xfs, f2fs, etc).
4172 */
4173static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4174 struct btrfs_root *root,
4175 struct inode *inode,
4176 struct btrfs_path *path,
4177 struct btrfs_path *dst_path)
4178{
4179 int ret;
4180 struct btrfs_key key;
4181 const u64 ino = btrfs_ino(inode);
4182 int ins_nr = 0;
4183 int start_slot = 0;
4184
4185 key.objectid = ino;
4186 key.type = BTRFS_XATTR_ITEM_KEY;
4187 key.offset = 0;
4188
4189 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4190 if (ret < 0)
4191 return ret;
4192
4193 while (true) {
4194 int slot = path->slots[0];
4195 struct extent_buffer *leaf = path->nodes[0];
4196 int nritems = btrfs_header_nritems(leaf);
4197
4198 if (slot >= nritems) {
4199 if (ins_nr > 0) {
4200 u64 last_extent = 0;
4201
4202 ret = copy_items(trans, inode, dst_path, path,
4203 &last_extent, start_slot,
4204 ins_nr, 1, 0);
4205 /* can't be 1, extent items aren't processed */
4206 ASSERT(ret <= 0);
4207 if (ret < 0)
4208 return ret;
4209 ins_nr = 0;
4210 }
4211 ret = btrfs_next_leaf(root, path);
4212 if (ret < 0)
4213 return ret;
4214 else if (ret > 0)
4215 break;
4216 continue;
4217 }
4218
4219 btrfs_item_key_to_cpu(leaf, &key, slot);
4220 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4221 break;
4222
4223 if (ins_nr == 0)
4224 start_slot = slot;
4225 ins_nr++;
4226 path->slots[0]++;
4227 cond_resched();
4228 }
4229 if (ins_nr > 0) {
4230 u64 last_extent = 0;
4231
4232 ret = copy_items(trans, inode, dst_path, path,
4233 &last_extent, start_slot,
4234 ins_nr, 1, 0);
4235 /* can't be 1, extent items aren't processed */
4236 ASSERT(ret <= 0);
4237 if (ret < 0)
4238 return ret;
4239 }
4240
4241 return 0;
4242}
4243
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004244/*
4245 * If the no holes feature is enabled we need to make sure any hole between the
4246 * last extent and the i_size of our inode is explicitly marked in the log. This
4247 * is to make sure that doing something like:
4248 *
4249 * 1) create file with 128Kb of data
4250 * 2) truncate file to 64Kb
4251 * 3) truncate file to 256Kb
4252 * 4) fsync file
4253 * 5) <crash/power failure>
4254 * 6) mount fs and trigger log replay
4255 *
4256 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4257 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4258 * file correspond to a hole. The presence of explicit holes in a log tree is
4259 * what guarantees that log replay will remove/adjust file extent items in the
4260 * fs/subvol tree.
4261 *
4262 * Here we do not need to care about holes between extents, that is already done
4263 * by copy_items(). We also only need to do this in the full sync path, where we
4264 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4265 * lookup the list of modified extent maps and if any represents a hole, we
4266 * insert a corresponding extent representing a hole in the log tree.
4267 */
4268static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4269 struct btrfs_root *root,
4270 struct inode *inode,
4271 struct btrfs_path *path)
4272{
4273 int ret;
4274 struct btrfs_key key;
4275 u64 hole_start;
4276 u64 hole_size;
4277 struct extent_buffer *leaf;
4278 struct btrfs_root *log = root->log_root;
4279 const u64 ino = btrfs_ino(inode);
4280 const u64 i_size = i_size_read(inode);
4281
4282 if (!btrfs_fs_incompat(root->fs_info, NO_HOLES))
4283 return 0;
4284
4285 key.objectid = ino;
4286 key.type = BTRFS_EXTENT_DATA_KEY;
4287 key.offset = (u64)-1;
4288
4289 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4290 ASSERT(ret != 0);
4291 if (ret < 0)
4292 return ret;
4293
4294 ASSERT(path->slots[0] > 0);
4295 path->slots[0]--;
4296 leaf = path->nodes[0];
4297 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4298
4299 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4300 /* inode does not have any extents */
4301 hole_start = 0;
4302 hole_size = i_size;
4303 } else {
4304 struct btrfs_file_extent_item *extent;
4305 u64 len;
4306
4307 /*
4308 * If there's an extent beyond i_size, an explicit hole was
4309 * already inserted by copy_items().
4310 */
4311 if (key.offset >= i_size)
4312 return 0;
4313
4314 extent = btrfs_item_ptr(leaf, path->slots[0],
4315 struct btrfs_file_extent_item);
4316
4317 if (btrfs_file_extent_type(leaf, extent) ==
4318 BTRFS_FILE_EXTENT_INLINE) {
4319 len = btrfs_file_extent_inline_len(leaf,
4320 path->slots[0],
4321 extent);
4322 ASSERT(len == i_size);
4323 return 0;
4324 }
4325
4326 len = btrfs_file_extent_num_bytes(leaf, extent);
4327 /* Last extent goes beyond i_size, no need to log a hole. */
4328 if (key.offset + len > i_size)
4329 return 0;
4330 hole_start = key.offset + len;
4331 hole_size = i_size - hole_start;
4332 }
4333 btrfs_release_path(path);
4334
4335 /* Last extent ends at i_size. */
4336 if (hole_size == 0)
4337 return 0;
4338
4339 hole_size = ALIGN(hole_size, root->sectorsize);
4340 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4341 hole_size, 0, hole_size, 0, 0, 0);
4342 return ret;
4343}
4344
Chris Masone02119d2008-09-05 16:13:11 -04004345/* log a single inode in the tree log.
4346 * At least one parent directory for this inode must exist in the tree
4347 * or be logged already.
4348 *
4349 * Any items from this inode changed by the current transaction are copied
4350 * to the log tree. An extra reference is taken on any extents in this
4351 * file, allowing us to avoid a whole pile of corner cases around logging
4352 * blocks that have been removed from the tree.
4353 *
4354 * See LOG_INODE_ALL and related defines for a description of what inode_only
4355 * does.
4356 *
4357 * This handles both files and directories.
4358 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004359static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004360 struct btrfs_root *root, struct inode *inode,
4361 int inode_only,
4362 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004363 const loff_t end,
4364 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004365{
4366 struct btrfs_path *path;
4367 struct btrfs_path *dst_path;
4368 struct btrfs_key min_key;
4369 struct btrfs_key max_key;
4370 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004371 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08004372 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004373 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004374 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004375 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004376 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004377 int ins_start_slot = 0;
4378 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004379 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08004380 u64 ino = btrfs_ino(inode);
Filipe Manana49dae1b2014-09-06 22:34:39 +01004381 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004382 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004383 bool need_log_inode_item = true;
Chris Masone02119d2008-09-05 16:13:11 -04004384
Chris Masone02119d2008-09-05 16:13:11 -04004385 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004386 if (!path)
4387 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004388 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004389 if (!dst_path) {
4390 btrfs_free_path(path);
4391 return -ENOMEM;
4392 }
Chris Masone02119d2008-09-05 16:13:11 -04004393
Li Zefan33345d012011-04-20 10:31:50 +08004394 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004395 min_key.type = BTRFS_INODE_ITEM_KEY;
4396 min_key.offset = 0;
4397
Li Zefan33345d012011-04-20 10:31:50 +08004398 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004399
Chris Mason12fcfd22009-03-24 10:24:20 -04004400
Josef Bacik5dc562c2012-08-17 13:14:17 -04004401 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00004402 if (S_ISDIR(inode->i_mode) ||
4403 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4404 &BTRFS_I(inode)->runtime_flags) &&
4405 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004406 max_key.type = BTRFS_XATTR_ITEM_KEY;
4407 else
4408 max_key.type = (u8)-1;
4409 max_key.offset = (u64)-1;
4410
Filipe Manana2c2c4522015-01-13 16:40:04 +00004411 /*
4412 * Only run delayed items if we are a dir or a new file.
4413 * Otherwise commit the delayed inode only, which is needed in
4414 * order for the log replay code to mark inodes for link count
4415 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4416 */
Josef Bacik94edf4a2012-09-25 14:56:25 -04004417 if (S_ISDIR(inode->i_mode) ||
Filipe Manana2c2c4522015-01-13 16:40:04 +00004418 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
Josef Bacik94edf4a2012-09-25 14:56:25 -04004419 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004420 else
4421 ret = btrfs_commit_inode_delayed_inode(inode);
4422
4423 if (ret) {
4424 btrfs_free_path(path);
4425 btrfs_free_path(dst_path);
4426 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004427 }
4428
Chris Masone02119d2008-09-05 16:13:11 -04004429 mutex_lock(&BTRFS_I(inode)->log_mutex);
4430
Filipe Manana08702952014-11-13 17:00:35 +00004431 btrfs_get_logged_extents(inode, &logged_list, start, end);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004432
Chris Masone02119d2008-09-05 16:13:11 -04004433 /*
4434 * a brute force approach to making sure we get the most uptodate
4435 * copies of everything.
4436 */
4437 if (S_ISDIR(inode->i_mode)) {
4438 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4439
Filipe Manana4f764e52015-02-23 19:53:35 +00004440 if (inode_only == LOG_INODE_EXISTS)
4441 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004442 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004443 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004444 if (inode_only == LOG_INODE_EXISTS) {
4445 /*
4446 * Make sure the new inode item we write to the log has
4447 * the same isize as the current one (if it exists).
4448 * This is necessary to prevent data loss after log
4449 * replay, and also to prevent doing a wrong expanding
4450 * truncate - for e.g. create file, write 4K into offset
4451 * 0, fsync, write 4K into offset 4096, add hard link,
4452 * fsync some other file (to sync log), power fail - if
4453 * we use the inode's current i_size, after log replay
4454 * we get a 8Kb file, with the last 4Kb extent as a hole
4455 * (zeroes), as if an expanding truncate happened,
4456 * instead of getting a file of 4Kb only.
4457 */
4458 err = logged_inode_size(log, inode, path,
4459 &logged_isize);
4460 if (err)
4461 goto out_unlock;
4462 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004463 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4464 &BTRFS_I(inode)->runtime_flags)) {
4465 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004466 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004467 ret = drop_objectid_items(trans, log, path, ino,
4468 max_key.type);
4469 } else {
4470 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4471 &BTRFS_I(inode)->runtime_flags);
4472 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4473 &BTRFS_I(inode)->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004474 while(1) {
4475 ret = btrfs_truncate_inode_items(trans,
4476 log, inode, 0, 0);
4477 if (ret != -EAGAIN)
4478 break;
4479 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004480 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004481 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4482 &BTRFS_I(inode)->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004483 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004484 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004485 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004486 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004487 ret = drop_objectid_items(trans, log, path, ino,
4488 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004489 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004490 if (inode_only == LOG_INODE_ALL)
4491 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004492 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004493 }
Josef Bacika95249b2012-10-11 16:17:34 -04004494
Chris Masone02119d2008-09-05 16:13:11 -04004495 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004496 if (ret) {
4497 err = ret;
4498 goto out_unlock;
4499 }
Chris Masone02119d2008-09-05 16:13:11 -04004500
Chris Masond3977122009-01-05 21:25:51 -05004501 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004502 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004503 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004504 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04004505 if (ret != 0)
4506 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004507again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004508 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004509 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004510 break;
4511 if (min_key.type > max_key.type)
4512 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004513
Filipe Mananae4545de2015-06-17 12:49:23 +01004514 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4515 need_log_inode_item = false;
4516
Filipe Manana36283bf2015-06-20 00:44:51 +01004517 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
4518 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
4519 if (ins_nr == 0)
4520 goto next_slot;
4521 ret = copy_items(trans, inode, dst_path, path,
4522 &last_extent, ins_start_slot,
4523 ins_nr, inode_only, logged_isize);
4524 if (ret < 0) {
4525 err = ret;
4526 goto out_unlock;
4527 }
4528 ins_nr = 0;
4529 if (ret) {
4530 btrfs_release_path(path);
4531 continue;
4532 }
4533 goto next_slot;
4534 }
4535
Chris Masone02119d2008-09-05 16:13:11 -04004536 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004537 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4538 ins_nr++;
4539 goto next_slot;
4540 } else if (!ins_nr) {
4541 ins_start_slot = path->slots[0];
4542 ins_nr = 1;
4543 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004544 }
4545
Josef Bacik16e75492013-10-22 12:18:51 -04004546 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004547 ins_start_slot, ins_nr, inode_only,
4548 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004549 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004550 err = ret;
4551 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02004552 }
4553 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04004554 ins_nr = 0;
4555 btrfs_release_path(path);
4556 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004557 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004558 ins_nr = 1;
4559 ins_start_slot = path->slots[0];
4560next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004561
Chris Mason3a5f1d42008-09-11 15:53:37 -04004562 nritems = btrfs_header_nritems(path->nodes[0]);
4563 path->slots[0]++;
4564 if (path->slots[0] < nritems) {
4565 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4566 path->slots[0]);
4567 goto again;
4568 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004569 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004570 ret = copy_items(trans, inode, dst_path, path,
4571 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004572 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004573 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004574 err = ret;
4575 goto out_unlock;
4576 }
Josef Bacik16e75492013-10-22 12:18:51 -04004577 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004578 ins_nr = 0;
4579 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004580 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04004581
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004582 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004583 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004584 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004585 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004586 min_key.offset = 0;
4587 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004588 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004589 }
Chris Masone02119d2008-09-05 16:13:11 -04004590 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004591 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004592 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004593 ins_start_slot, ins_nr, inode_only,
4594 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004595 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004596 err = ret;
4597 goto out_unlock;
4598 }
Josef Bacik16e75492013-10-22 12:18:51 -04004599 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004600 ins_nr = 0;
4601 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004602
Filipe Manana36283bf2015-06-20 00:44:51 +01004603 btrfs_release_path(path);
4604 btrfs_release_path(dst_path);
4605 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
4606 if (err)
4607 goto out_unlock;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004608 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
4609 btrfs_release_path(path);
4610 btrfs_release_path(dst_path);
4611 err = btrfs_log_trailing_hole(trans, root, inode, path);
4612 if (err)
4613 goto out_unlock;
4614 }
Josef Bacika95249b2012-10-11 16:17:34 -04004615log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004616 btrfs_release_path(path);
4617 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01004618 if (need_log_inode_item) {
4619 err = log_inode_item(trans, log, dst_path, inode);
4620 if (err)
4621 goto out_unlock;
4622 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004623 if (fast_search) {
Filipe Mananab38ef712014-11-13 17:01:45 +00004624 /*
4625 * Some ordered extents started by fsync might have completed
4626 * before we collected the ordered extents in logged_list, which
4627 * means they're gone, not in our logged_list nor in the inode's
4628 * ordered tree. We want the application/user space to know an
4629 * error happened while attempting to persist file data so that
4630 * it can take proper action. If such error happened, we leave
4631 * without writing to the log tree and the fsync must report the
4632 * file data write error and not commit the current transaction.
4633 */
4634 err = btrfs_inode_check_errors(inode);
4635 if (err) {
4636 ctx->io_err = err;
4637 goto out_unlock;
4638 }
Miao Xie827463c2014-01-14 20:31:51 +08004639 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Manana8407f552014-09-05 15:14:39 +01004640 &logged_list, ctx);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004641 if (ret) {
4642 err = ret;
4643 goto out_unlock;
4644 }
Josef Bacikd006a042013-11-12 20:54:09 -05004645 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004646 struct extent_map *em, *n;
4647
Filipe Manana49dae1b2014-09-06 22:34:39 +01004648 write_lock(&em_tree->lock);
4649 /*
4650 * We can't just remove every em if we're called for a ranged
4651 * fsync - that is, one that doesn't cover the whole possible
4652 * file range (0 to LLONG_MAX). This is because we can have
4653 * em's that fall outside the range we're logging and therefore
4654 * their ordered operations haven't completed yet
4655 * (btrfs_finish_ordered_io() not invoked yet). This means we
4656 * didn't get their respective file extent item in the fs/subvol
4657 * tree yet, and need to let the next fast fsync (one which
4658 * consults the list of modified extent maps) find the em so
4659 * that it logs a matching file extent item and waits for the
4660 * respective ordered operation to complete (if it's still
4661 * running).
4662 *
4663 * Removing every em outside the range we're logging would make
4664 * the next fast fsync not log their matching file extent items,
4665 * therefore making us lose data after a log replay.
4666 */
4667 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4668 list) {
4669 const u64 mod_end = em->mod_start + em->mod_len - 1;
4670
4671 if (em->mod_start >= start && mod_end <= end)
4672 list_del_init(&em->list);
4673 }
4674 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004675 }
4676
Chris Mason9623f9a2008-09-11 17:42:42 -04004677 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004678 ret = log_directory_changes(trans, root, inode, path, dst_path,
4679 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004680 if (ret) {
4681 err = ret;
4682 goto out_unlock;
4683 }
Chris Masone02119d2008-09-05 16:13:11 -04004684 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01004685
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004686 spin_lock(&BTRFS_I(inode)->lock);
Filipe Manana125c4cf92014-09-11 21:22:14 +01004687 BTRFS_I(inode)->logged_trans = trans->transid;
4688 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004689 spin_unlock(&BTRFS_I(inode)->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004690out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08004691 if (unlikely(err))
4692 btrfs_put_logged_extents(&logged_list);
4693 else
4694 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04004695 mutex_unlock(&BTRFS_I(inode)->log_mutex);
4696
4697 btrfs_free_path(path);
4698 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004699 return err;
Chris Masone02119d2008-09-05 16:13:11 -04004700}
4701
Chris Mason12fcfd22009-03-24 10:24:20 -04004702/*
4703 * follow the dentry parent pointers up the chain and see if any
4704 * of the directories in it require a full commit before they can
4705 * be logged. Returns zero if nothing special needs to be done or 1 if
4706 * a full commit is required.
4707 */
4708static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
4709 struct inode *inode,
4710 struct dentry *parent,
4711 struct super_block *sb,
4712 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04004713{
Chris Mason12fcfd22009-03-24 10:24:20 -04004714 int ret = 0;
4715 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00004716 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04004717 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04004718
Chris Masonaf4176b2009-03-24 10:24:31 -04004719 /*
4720 * for regular files, if its inode is already on disk, we don't
4721 * have to worry about the parents at all. This is because
4722 * we can use the last_unlink_trans field to record renames
4723 * and other fun in this file.
4724 */
4725 if (S_ISREG(inode->i_mode) &&
4726 BTRFS_I(inode)->generation <= last_committed &&
4727 BTRFS_I(inode)->last_unlink_trans <= last_committed)
4728 goto out;
4729
Chris Mason12fcfd22009-03-24 10:24:20 -04004730 if (!S_ISDIR(inode->i_mode)) {
David Howells2b0143b2015-03-17 22:25:59 +00004731 if (!parent || d_really_is_negative(parent) || sb != d_inode(parent)->i_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04004732 goto out;
David Howells2b0143b2015-03-17 22:25:59 +00004733 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04004734 }
4735
4736 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04004737 /*
4738 * If we are logging a directory then we start with our inode,
4739 * not our parents inode, so we need to skipp setting the
4740 * logged_trans so that further down in the log code we don't
4741 * think this inode has already been logged.
4742 */
4743 if (inode != orig_inode)
4744 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04004745 smp_mb();
4746
4747 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
4748 root = BTRFS_I(inode)->root;
4749
4750 /*
4751 * make sure any commits to the log are forced
4752 * to be full commits
4753 */
Miao Xie995946d2014-04-02 19:51:06 +08004754 btrfs_set_log_full_commit(root->fs_info, trans);
Chris Mason12fcfd22009-03-24 10:24:20 -04004755 ret = 1;
4756 break;
4757 }
4758
David Howells2b0143b2015-03-17 22:25:59 +00004759 if (!parent || d_really_is_negative(parent) || sb != d_inode(parent)->i_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04004760 break;
4761
Yan, Zheng76dda932009-09-21 16:00:26 -04004762 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04004763 break;
4764
Josef Bacik6a912212010-11-20 09:48:00 +00004765 parent = dget_parent(parent);
4766 dput(old_parent);
4767 old_parent = parent;
David Howells2b0143b2015-03-17 22:25:59 +00004768 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04004769
4770 }
Josef Bacik6a912212010-11-20 09:48:00 +00004771 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04004772out:
Chris Masone02119d2008-09-05 16:13:11 -04004773 return ret;
4774}
4775
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004776struct btrfs_dir_list {
4777 u64 ino;
4778 struct list_head list;
4779};
4780
4781/*
4782 * Log the inodes of the new dentries of a directory. See log_dir_items() for
4783 * details about the why it is needed.
4784 * This is a recursive operation - if an existing dentry corresponds to a
4785 * directory, that directory's new entries are logged too (same behaviour as
4786 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
4787 * the dentries point to we do not lock their i_mutex, otherwise lockdep
4788 * complains about the following circular lock dependency / possible deadlock:
4789 *
4790 * CPU0 CPU1
4791 * ---- ----
4792 * lock(&type->i_mutex_dir_key#3/2);
4793 * lock(sb_internal#2);
4794 * lock(&type->i_mutex_dir_key#3/2);
4795 * lock(&sb->s_type->i_mutex_key#14);
4796 *
4797 * Where sb_internal is the lock (a counter that works as a lock) acquired by
4798 * sb_start_intwrite() in btrfs_start_transaction().
4799 * Not locking i_mutex of the inodes is still safe because:
4800 *
4801 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
4802 * that while logging the inode new references (names) are added or removed
4803 * from the inode, leaving the logged inode item with a link count that does
4804 * not match the number of logged inode reference items. This is fine because
4805 * at log replay time we compute the real number of links and correct the
4806 * link count in the inode item (see replay_one_buffer() and
4807 * link_to_fixup_dir());
4808 *
4809 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
4810 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
4811 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
4812 * has a size that doesn't match the sum of the lengths of all the logged
4813 * names. This does not result in a problem because if a dir_item key is
4814 * logged but its matching dir_index key is not logged, at log replay time we
4815 * don't use it to replay the respective name (see replay_one_name()). On the
4816 * other hand if only the dir_index key ends up being logged, the respective
4817 * name is added to the fs/subvol tree with both the dir_item and dir_index
4818 * keys created (see replay_one_name()).
4819 * The directory's inode item with a wrong i_size is not a problem as well,
4820 * since we don't use it at log replay time to set the i_size in the inode
4821 * item of the fs/subvol tree (see overwrite_item()).
4822 */
4823static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
4824 struct btrfs_root *root,
4825 struct inode *start_inode,
4826 struct btrfs_log_ctx *ctx)
4827{
4828 struct btrfs_root *log = root->log_root;
4829 struct btrfs_path *path;
4830 LIST_HEAD(dir_list);
4831 struct btrfs_dir_list *dir_elem;
4832 int ret = 0;
4833
4834 path = btrfs_alloc_path();
4835 if (!path)
4836 return -ENOMEM;
4837
4838 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
4839 if (!dir_elem) {
4840 btrfs_free_path(path);
4841 return -ENOMEM;
4842 }
4843 dir_elem->ino = btrfs_ino(start_inode);
4844 list_add_tail(&dir_elem->list, &dir_list);
4845
4846 while (!list_empty(&dir_list)) {
4847 struct extent_buffer *leaf;
4848 struct btrfs_key min_key;
4849 int nritems;
4850 int i;
4851
4852 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
4853 list);
4854 if (ret)
4855 goto next_dir_inode;
4856
4857 min_key.objectid = dir_elem->ino;
4858 min_key.type = BTRFS_DIR_ITEM_KEY;
4859 min_key.offset = 0;
4860again:
4861 btrfs_release_path(path);
4862 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
4863 if (ret < 0) {
4864 goto next_dir_inode;
4865 } else if (ret > 0) {
4866 ret = 0;
4867 goto next_dir_inode;
4868 }
4869
4870process_leaf:
4871 leaf = path->nodes[0];
4872 nritems = btrfs_header_nritems(leaf);
4873 for (i = path->slots[0]; i < nritems; i++) {
4874 struct btrfs_dir_item *di;
4875 struct btrfs_key di_key;
4876 struct inode *di_inode;
4877 struct btrfs_dir_list *new_dir_elem;
4878 int log_mode = LOG_INODE_EXISTS;
4879 int type;
4880
4881 btrfs_item_key_to_cpu(leaf, &min_key, i);
4882 if (min_key.objectid != dir_elem->ino ||
4883 min_key.type != BTRFS_DIR_ITEM_KEY)
4884 goto next_dir_inode;
4885
4886 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
4887 type = btrfs_dir_type(leaf, di);
4888 if (btrfs_dir_transid(leaf, di) < trans->transid &&
4889 type != BTRFS_FT_DIR)
4890 continue;
4891 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
4892 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
4893 continue;
4894
4895 di_inode = btrfs_iget(root->fs_info->sb, &di_key,
4896 root, NULL);
4897 if (IS_ERR(di_inode)) {
4898 ret = PTR_ERR(di_inode);
4899 goto next_dir_inode;
4900 }
4901
4902 if (btrfs_inode_in_log(di_inode, trans->transid)) {
4903 iput(di_inode);
4904 continue;
4905 }
4906
4907 ctx->log_new_dentries = false;
4908 if (type == BTRFS_FT_DIR)
4909 log_mode = LOG_INODE_ALL;
4910 btrfs_release_path(path);
4911 ret = btrfs_log_inode(trans, root, di_inode,
4912 log_mode, 0, LLONG_MAX, ctx);
4913 iput(di_inode);
4914 if (ret)
4915 goto next_dir_inode;
4916 if (ctx->log_new_dentries) {
4917 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
4918 GFP_NOFS);
4919 if (!new_dir_elem) {
4920 ret = -ENOMEM;
4921 goto next_dir_inode;
4922 }
4923 new_dir_elem->ino = di_key.objectid;
4924 list_add_tail(&new_dir_elem->list, &dir_list);
4925 }
4926 break;
4927 }
4928 if (i == nritems) {
4929 ret = btrfs_next_leaf(log, path);
4930 if (ret < 0) {
4931 goto next_dir_inode;
4932 } else if (ret > 0) {
4933 ret = 0;
4934 goto next_dir_inode;
4935 }
4936 goto process_leaf;
4937 }
4938 if (min_key.offset < (u64)-1) {
4939 min_key.offset++;
4940 goto again;
4941 }
4942next_dir_inode:
4943 list_del(&dir_elem->list);
4944 kfree(dir_elem);
4945 }
4946
4947 btrfs_free_path(path);
4948 return ret;
4949}
4950
Filipe Manana18aa0922015-08-05 16:49:08 +01004951static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
4952 struct inode *inode,
4953 struct btrfs_log_ctx *ctx)
4954{
4955 int ret;
4956 struct btrfs_path *path;
4957 struct btrfs_key key;
4958 struct btrfs_root *root = BTRFS_I(inode)->root;
4959 const u64 ino = btrfs_ino(inode);
4960
4961 path = btrfs_alloc_path();
4962 if (!path)
4963 return -ENOMEM;
4964 path->skip_locking = 1;
4965 path->search_commit_root = 1;
4966
4967 key.objectid = ino;
4968 key.type = BTRFS_INODE_REF_KEY;
4969 key.offset = 0;
4970 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4971 if (ret < 0)
4972 goto out;
4973
4974 while (true) {
4975 struct extent_buffer *leaf = path->nodes[0];
4976 int slot = path->slots[0];
4977 u32 cur_offset = 0;
4978 u32 item_size;
4979 unsigned long ptr;
4980
4981 if (slot >= btrfs_header_nritems(leaf)) {
4982 ret = btrfs_next_leaf(root, path);
4983 if (ret < 0)
4984 goto out;
4985 else if (ret > 0)
4986 break;
4987 continue;
4988 }
4989
4990 btrfs_item_key_to_cpu(leaf, &key, slot);
4991 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
4992 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
4993 break;
4994
4995 item_size = btrfs_item_size_nr(leaf, slot);
4996 ptr = btrfs_item_ptr_offset(leaf, slot);
4997 while (cur_offset < item_size) {
4998 struct btrfs_key inode_key;
4999 struct inode *dir_inode;
5000
5001 inode_key.type = BTRFS_INODE_ITEM_KEY;
5002 inode_key.offset = 0;
5003
5004 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5005 struct btrfs_inode_extref *extref;
5006
5007 extref = (struct btrfs_inode_extref *)
5008 (ptr + cur_offset);
5009 inode_key.objectid = btrfs_inode_extref_parent(
5010 leaf, extref);
5011 cur_offset += sizeof(*extref);
5012 cur_offset += btrfs_inode_extref_name_len(leaf,
5013 extref);
5014 } else {
5015 inode_key.objectid = key.offset;
5016 cur_offset = item_size;
5017 }
5018
5019 dir_inode = btrfs_iget(root->fs_info->sb, &inode_key,
5020 root, NULL);
5021 /* If parent inode was deleted, skip it. */
5022 if (IS_ERR(dir_inode))
5023 continue;
5024
5025 ret = btrfs_log_inode(trans, root, dir_inode,
5026 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
5027 iput(dir_inode);
5028 if (ret)
5029 goto out;
5030 }
5031 path->slots[0]++;
5032 }
5033 ret = 0;
5034out:
5035 btrfs_free_path(path);
5036 return ret;
5037}
5038
Chris Masone02119d2008-09-05 16:13:11 -04005039/*
5040 * helper function around btrfs_log_inode to make sure newly created
5041 * parent directories also end up in the log. A minimal inode and backref
5042 * only logging is done of any parent directories that are older than
5043 * the last committed transaction
5044 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005045static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5046 struct btrfs_root *root, struct inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005047 struct dentry *parent,
5048 const loff_t start,
5049 const loff_t end,
5050 int exists_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005051 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005052{
Chris Mason12fcfd22009-03-24 10:24:20 -04005053 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04005054 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005055 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005056 int ret = 0;
5057 u64 last_committed = root->fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005058 bool log_dentries = false;
5059 struct inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005060
5061 sb = inode->i_sb;
5062
Sage Weil3a5e1402009-04-02 16:49:40 -04005063 if (btrfs_test_opt(root, NOTREELOG)) {
5064 ret = 1;
5065 goto end_no_trans;
5066 }
5067
Miao Xie995946d2014-04-02 19:51:06 +08005068 /*
5069 * The prev transaction commit doesn't complete, we need do
5070 * full commit by ourselves.
5071 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005072 if (root->fs_info->last_trans_log_full_commit >
5073 root->fs_info->last_trans_committed) {
5074 ret = 1;
5075 goto end_no_trans;
5076 }
5077
Yan, Zheng76dda932009-09-21 16:00:26 -04005078 if (root != BTRFS_I(inode)->root ||
5079 btrfs_root_refs(&root->root_item) == 0) {
5080 ret = 1;
5081 goto end_no_trans;
5082 }
5083
Chris Mason12fcfd22009-03-24 10:24:20 -04005084 ret = check_parent_dirs_for_sync(trans, inode, parent,
5085 sb, last_committed);
5086 if (ret)
5087 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005088
Josef Bacik22ee6985d2012-05-29 16:57:49 -04005089 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005090 ret = BTRFS_NO_LOG_SYNC;
5091 goto end_no_trans;
5092 }
5093
Miao Xie8b050d32014-02-20 18:08:58 +08005094 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005095 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005096 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005097
Filipe Manana8407f552014-09-05 15:14:39 +01005098 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005099 if (ret)
5100 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005101
Chris Masonaf4176b2009-03-24 10:24:31 -04005102 /*
5103 * for regular files, if its inode is already on disk, we don't
5104 * have to worry about the parents at all. This is because
5105 * we can use the last_unlink_trans field to record renames
5106 * and other fun in this file.
5107 */
5108 if (S_ISREG(inode->i_mode) &&
5109 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005110 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
5111 ret = 0;
5112 goto end_trans;
5113 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005114
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005115 if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
5116 log_dentries = true;
5117
Filipe Manana18aa0922015-08-05 16:49:08 +01005118 /*
5119 * On unlink we must make sure all our current and old parent directores
5120 * inodes are fully logged. This is to prevent leaving dangling
5121 * directory index entries in directories that were our parents but are
5122 * not anymore. Not doing this results in old parent directory being
5123 * impossible to delete after log replay (rmdir will always fail with
5124 * error -ENOTEMPTY).
5125 *
5126 * Example 1:
5127 *
5128 * mkdir testdir
5129 * touch testdir/foo
5130 * ln testdir/foo testdir/bar
5131 * sync
5132 * unlink testdir/bar
5133 * xfs_io -c fsync testdir/foo
5134 * <power failure>
5135 * mount fs, triggers log replay
5136 *
5137 * If we don't log the parent directory (testdir), after log replay the
5138 * directory still has an entry pointing to the file inode using the bar
5139 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5140 * the file inode has a link count of 1.
5141 *
5142 * Example 2:
5143 *
5144 * mkdir testdir
5145 * touch foo
5146 * ln foo testdir/foo2
5147 * ln foo testdir/foo3
5148 * sync
5149 * unlink testdir/foo3
5150 * xfs_io -c fsync foo
5151 * <power failure>
5152 * mount fs, triggers log replay
5153 *
5154 * Similar as the first example, after log replay the parent directory
5155 * testdir still has an entry pointing to the inode file with name foo3
5156 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5157 * and has a link count of 2.
5158 */
5159 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
5160 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5161 if (ret)
5162 goto end_trans;
5163 }
5164
Chris Masond3977122009-01-05 21:25:51 -05005165 while (1) {
David Howells2b0143b2015-03-17 22:25:59 +00005166 if (!parent || d_really_is_negative(parent) || sb != d_inode(parent)->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005167 break;
5168
David Howells2b0143b2015-03-17 22:25:59 +00005169 inode = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005170 if (root != BTRFS_I(inode)->root)
5171 break;
5172
Filipe Manana18aa0922015-08-05 16:49:08 +01005173 if (BTRFS_I(inode)->generation > last_committed) {
5174 ret = btrfs_log_inode(trans, root, inode,
5175 LOG_INODE_EXISTS,
Filipe Manana8407f552014-09-05 15:14:39 +01005176 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005177 if (ret)
5178 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005179 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005180 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005181 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005182
Josef Bacik6a912212010-11-20 09:48:00 +00005183 parent = dget_parent(parent);
5184 dput(old_parent);
5185 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005186 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005187 if (log_dentries)
5188 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5189 else
5190 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005191end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005192 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005193 if (ret < 0) {
Miao Xie995946d2014-04-02 19:51:06 +08005194 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005195 ret = 1;
5196 }
Miao Xie8b050d32014-02-20 18:08:58 +08005197
5198 if (ret)
5199 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005200 btrfs_end_log_trans(root);
5201end_no_trans:
5202 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005203}
5204
5205/*
5206 * it is not safe to log dentry if the chunk root has added new
5207 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5208 * If this returns 1, you must commit the transaction to safely get your
5209 * data on disk.
5210 */
5211int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08005212 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005213 const loff_t start,
5214 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005215 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005216{
Josef Bacik6a912212010-11-20 09:48:00 +00005217 struct dentry *parent = dget_parent(dentry);
5218 int ret;
5219
David Howells2b0143b2015-03-17 22:25:59 +00005220 ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005221 start, end, 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005222 dput(parent);
5223
5224 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005225}
5226
5227/*
5228 * should be called during mount to recover any replay any log trees
5229 * from the FS
5230 */
5231int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5232{
5233 int ret;
5234 struct btrfs_path *path;
5235 struct btrfs_trans_handle *trans;
5236 struct btrfs_key key;
5237 struct btrfs_key found_key;
5238 struct btrfs_key tmp_key;
5239 struct btrfs_root *log;
5240 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5241 struct walk_control wc = {
5242 .process_func = process_one_buffer,
5243 .stage = 0,
5244 };
5245
Chris Masone02119d2008-09-05 16:13:11 -04005246 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005247 if (!path)
5248 return -ENOMEM;
5249
5250 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04005251
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005252 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005253 if (IS_ERR(trans)) {
5254 ret = PTR_ERR(trans);
5255 goto error;
5256 }
Chris Masone02119d2008-09-05 16:13:11 -04005257
5258 wc.trans = trans;
5259 wc.pin = 1;
5260
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005261 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005262 if (ret) {
5263 btrfs_error(fs_info, ret, "Failed to pin buffers while "
5264 "recovering log root tree.");
5265 goto error;
5266 }
Chris Masone02119d2008-09-05 16:13:11 -04005267
5268again:
5269 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5270 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005271 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005272
Chris Masond3977122009-01-05 21:25:51 -05005273 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005274 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005275
5276 if (ret < 0) {
5277 btrfs_error(fs_info, ret,
5278 "Couldn't find tree log root.");
5279 goto error;
5280 }
Chris Masone02119d2008-09-05 16:13:11 -04005281 if (ret > 0) {
5282 if (path->slots[0] == 0)
5283 break;
5284 path->slots[0]--;
5285 }
5286 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5287 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005288 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005289 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5290 break;
5291
Miao Xiecb517ea2013-05-15 07:48:19 +00005292 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005293 if (IS_ERR(log)) {
5294 ret = PTR_ERR(log);
5295 btrfs_error(fs_info, ret,
5296 "Couldn't read tree log root.");
5297 goto error;
5298 }
Chris Masone02119d2008-09-05 16:13:11 -04005299
5300 tmp_key.objectid = found_key.offset;
5301 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5302 tmp_key.offset = (u64)-1;
5303
5304 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005305 if (IS_ERR(wc.replay_dest)) {
5306 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005307 free_extent_buffer(log->node);
5308 free_extent_buffer(log->commit_root);
5309 kfree(log);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005310 btrfs_error(fs_info, ret, "Couldn't read target root "
5311 "for tree log recovery.");
5312 goto error;
5313 }
Chris Masone02119d2008-09-05 16:13:11 -04005314
Yan Zheng07d400a2009-01-06 11:42:00 -05005315 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005316 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005317 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005318
Josef Bacikb50c6e22013-04-25 15:55:30 -04005319 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005320 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5321 path);
Chris Masone02119d2008-09-05 16:13:11 -04005322 }
Chris Masone02119d2008-09-05 16:13:11 -04005323
5324 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005325 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005326 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005327 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005328 kfree(log);
5329
Josef Bacikb50c6e22013-04-25 15:55:30 -04005330 if (ret)
5331 goto error;
5332
Chris Masone02119d2008-09-05 16:13:11 -04005333 if (found_key.offset == 0)
5334 break;
5335 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005336 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005337
5338 /* step one is to pin it all, step two is to replay just inodes */
5339 if (wc.pin) {
5340 wc.pin = 0;
5341 wc.process_func = replay_one_buffer;
5342 wc.stage = LOG_WALK_REPLAY_INODES;
5343 goto again;
5344 }
5345 /* step three is to replay everything */
5346 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5347 wc.stage++;
5348 goto again;
5349 }
5350
5351 btrfs_free_path(path);
5352
Josef Bacikabefa552013-04-24 16:40:05 -04005353 /* step 4: commit the transaction, which also unpins the blocks */
5354 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
5355 if (ret)
5356 return ret;
5357
Chris Masone02119d2008-09-05 16:13:11 -04005358 free_extent_buffer(log_root_tree->node);
5359 log_root_tree->log_root = NULL;
5360 fs_info->log_root_recovering = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005361 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005362
Josef Bacikabefa552013-04-24 16:40:05 -04005363 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005364error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005365 if (wc.trans)
5366 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005367 btrfs_free_path(path);
5368 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005369}
Chris Mason12fcfd22009-03-24 10:24:20 -04005370
5371/*
5372 * there are some corner cases where we want to force a full
5373 * commit instead of allowing a directory to be logged.
5374 *
5375 * They revolve around files there were unlinked from the directory, and
5376 * this function updates the parent directory so that a full commit is
5377 * properly done if it is fsync'd later after the unlinks are done.
5378 */
5379void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
5380 struct inode *dir, struct inode *inode,
5381 int for_rename)
5382{
5383 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005384 * when we're logging a file, if it hasn't been renamed
5385 * or unlinked, and its inode is fully committed on disk,
5386 * we don't have to worry about walking up the directory chain
5387 * to log its parents.
5388 *
5389 * So, we use the last_unlink_trans field to put this transid
5390 * into the file. When the file is logged we check it and
5391 * don't log the parents if the file is fully on disk.
5392 */
5393 if (S_ISREG(inode->i_mode))
5394 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5395
5396 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005397 * if this directory was already logged any new
5398 * names for this file/dir will get recorded
5399 */
5400 smp_mb();
5401 if (BTRFS_I(dir)->logged_trans == trans->transid)
5402 return;
5403
5404 /*
5405 * if the inode we're about to unlink was logged,
5406 * the log will be properly updated for any new names
5407 */
5408 if (BTRFS_I(inode)->logged_trans == trans->transid)
5409 return;
5410
5411 /*
5412 * when renaming files across directories, if the directory
5413 * there we're unlinking from gets fsync'd later on, there's
5414 * no way to find the destination directory later and fsync it
5415 * properly. So, we have to be conservative and force commits
5416 * so the new name gets discovered.
5417 */
5418 if (for_rename)
5419 goto record;
5420
5421 /* we can safely do the unlink without any special recording */
5422 return;
5423
5424record:
5425 BTRFS_I(dir)->last_unlink_trans = trans->transid;
5426}
5427
5428/*
5429 * Call this after adding a new name for a file and it will properly
5430 * update the log to reflect the new name.
5431 *
5432 * It will return zero if all goes well, and it will return 1 if a
5433 * full transaction commit is required.
5434 */
5435int btrfs_log_new_name(struct btrfs_trans_handle *trans,
5436 struct inode *inode, struct inode *old_dir,
5437 struct dentry *parent)
5438{
5439 struct btrfs_root * root = BTRFS_I(inode)->root;
5440
5441 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005442 * this will force the logging code to walk the dentry chain
5443 * up for the file
5444 */
5445 if (S_ISREG(inode->i_mode))
5446 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5447
5448 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005449 * if this inode hasn't been logged and directory we're renaming it
5450 * from hasn't been logged, we don't need to log it
5451 */
5452 if (BTRFS_I(inode)->logged_trans <=
5453 root->fs_info->last_trans_committed &&
5454 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
5455 root->fs_info->last_trans_committed))
5456 return 0;
5457
Filipe Manana49dae1b2014-09-06 22:34:39 +01005458 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
5459 LLONG_MAX, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04005460}
5461