blob: 2e65e8e73844b6ebd1ff853e1f367d6cf810dbcd [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 Masonf46dbe3de2012-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,
1543 struct btrfs_path *path,
1544 u64 dirid, u64 index,
1545 char *name, int name_len, u8 type,
1546 struct btrfs_key *location)
1547{
1548 struct inode *inode;
1549 struct inode *dir;
1550 int ret;
1551
1552 inode = read_one_inode(root, location->objectid);
1553 if (!inode)
1554 return -ENOENT;
1555
1556 dir = read_one_inode(root, dirid);
1557 if (!dir) {
1558 iput(inode);
1559 return -EIO;
1560 }
Josef Bacikd5554382013-09-11 14:17:00 -04001561
Chris Masone02119d2008-09-05 16:13:11 -04001562 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1563
1564 /* FIXME, put inode into FIXUP list */
1565
1566 iput(inode);
1567 iput(dir);
1568 return ret;
1569}
1570
1571/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001572 * Return true if an inode reference exists in the log for the given name,
1573 * inode and parent inode.
1574 */
1575static bool name_in_log_ref(struct btrfs_root *log_root,
1576 const char *name, const int name_len,
1577 const u64 dirid, const u64 ino)
1578{
1579 struct btrfs_key search_key;
1580
1581 search_key.objectid = ino;
1582 search_key.type = BTRFS_INODE_REF_KEY;
1583 search_key.offset = dirid;
1584 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1585 return true;
1586
1587 search_key.type = BTRFS_INODE_EXTREF_KEY;
1588 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1589 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1590 return true;
1591
1592 return false;
1593}
1594
1595/*
Chris Masone02119d2008-09-05 16:13:11 -04001596 * take a single entry in a log directory item and replay it into
1597 * the subvolume.
1598 *
1599 * if a conflicting item exists in the subdirectory already,
1600 * the inode it points to is unlinked and put into the link count
1601 * fix up tree.
1602 *
1603 * If a name from the log points to a file or directory that does
1604 * not exist in the FS, it is skipped. fsyncs on directories
1605 * do not force down inodes inside that directory, just changes to the
1606 * names or unlinks in a directory.
Filipe Mananabb53eda2015-07-15 23:26:43 +01001607 *
1608 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1609 * non-existing inode) and 1 if the name was replayed.
Chris Masone02119d2008-09-05 16:13:11 -04001610 */
1611static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1612 struct btrfs_root *root,
1613 struct btrfs_path *path,
1614 struct extent_buffer *eb,
1615 struct btrfs_dir_item *di,
1616 struct btrfs_key *key)
1617{
1618 char *name;
1619 int name_len;
1620 struct btrfs_dir_item *dst_di;
1621 struct btrfs_key found_key;
1622 struct btrfs_key log_key;
1623 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001624 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001625 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001626 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001627 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001628 bool name_added = false;
Chris Masone02119d2008-09-05 16:13:11 -04001629
1630 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001631 if (!dir)
1632 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001633
1634 name_len = btrfs_dir_name_len(eb, di);
1635 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001636 if (!name) {
1637 ret = -ENOMEM;
1638 goto out;
1639 }
liubo2a29edc2011-01-26 06:22:08 +00001640
Chris Masone02119d2008-09-05 16:13:11 -04001641 log_type = btrfs_dir_type(eb, di);
1642 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1643 name_len);
1644
1645 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001646 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1647 if (exists == 0)
1648 exists = 1;
1649 else
1650 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001651 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001652
Chris Masone02119d2008-09-05 16:13:11 -04001653 if (key->type == BTRFS_DIR_ITEM_KEY) {
1654 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1655 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001656 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001657 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1658 key->objectid,
1659 key->offset, name,
1660 name_len, 1);
1661 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001662 /* Corruption */
1663 ret = -EINVAL;
1664 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001665 }
David Sterbac7040052011-04-19 18:00:01 +02001666 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001667 /* we need a sequence number to insert, so we only
1668 * do inserts for the BTRFS_DIR_INDEX_KEY types
1669 */
1670 if (key->type != BTRFS_DIR_INDEX_KEY)
1671 goto out;
1672 goto insert;
1673 }
1674
1675 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1676 /* the existing item matches the logged item */
1677 if (found_key.objectid == log_key.objectid &&
1678 found_key.type == log_key.type &&
1679 found_key.offset == log_key.offset &&
1680 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001681 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001682 goto out;
1683 }
1684
1685 /*
1686 * don't drop the conflicting directory entry if the inode
1687 * for the new entry doesn't exist
1688 */
Chris Mason4bef0842008-09-08 11:18:08 -04001689 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001690 goto out;
1691
Chris Masone02119d2008-09-05 16:13:11 -04001692 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001693 if (ret)
1694 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001695
1696 if (key->type == BTRFS_DIR_INDEX_KEY)
1697 goto insert;
1698out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001699 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001700 if (!ret && update_size) {
1701 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1702 ret = btrfs_update_inode(trans, root, dir);
1703 }
Chris Masone02119d2008-09-05 16:13:11 -04001704 kfree(name);
1705 iput(dir);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001706 if (!ret && name_added)
1707 ret = 1;
Josef Bacik36508602013-04-25 16:23:32 -04001708 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001709
1710insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001711 if (name_in_log_ref(root->log_root, name, name_len,
1712 key->objectid, log_key.objectid)) {
1713 /* The dentry will be added later. */
1714 ret = 0;
1715 update_size = false;
1716 goto out;
1717 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001718 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001719 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1720 name, name_len, log_type, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001721 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001722 goto out;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001723 if (!ret)
1724 name_added = true;
Josef Bacikd5554382013-09-11 14:17:00 -04001725 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001726 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001727 goto out;
1728}
1729
1730/*
1731 * find all the names in a directory item and reconcile them into
1732 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1733 * one name in a directory item, but the same code gets used for
1734 * both directory index types
1735 */
1736static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1737 struct btrfs_root *root,
1738 struct btrfs_path *path,
1739 struct extent_buffer *eb, int slot,
1740 struct btrfs_key *key)
1741{
Filipe Mananabb53eda2015-07-15 23:26:43 +01001742 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001743 u32 item_size = btrfs_item_size_nr(eb, slot);
1744 struct btrfs_dir_item *di;
1745 int name_len;
1746 unsigned long ptr;
1747 unsigned long ptr_end;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001748 struct btrfs_path *fixup_path = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001749
1750 ptr = btrfs_item_ptr_offset(eb, slot);
1751 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001752 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001753 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001754 if (verify_dir_item(root, eb, di))
1755 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001756 name_len = btrfs_dir_name_len(eb, di);
1757 ret = replay_one_name(trans, root, path, eb, di, key);
Filipe Mananabb53eda2015-07-15 23:26:43 +01001758 if (ret < 0)
1759 break;
Chris Masone02119d2008-09-05 16:13:11 -04001760 ptr = (unsigned long)(di + 1);
1761 ptr += name_len;
Filipe Mananabb53eda2015-07-15 23:26:43 +01001762
1763 /*
1764 * If this entry refers to a non-directory (directories can not
1765 * have a link count > 1) and it was added in the transaction
1766 * that was not committed, make sure we fixup the link count of
1767 * the inode it the entry points to. Otherwise something like
1768 * the following would result in a directory pointing to an
1769 * inode with a wrong link that does not account for this dir
1770 * entry:
1771 *
1772 * mkdir testdir
1773 * touch testdir/foo
1774 * touch testdir/bar
1775 * sync
1776 *
1777 * ln testdir/bar testdir/bar_link
1778 * ln testdir/foo testdir/foo_link
1779 * xfs_io -c "fsync" testdir/bar
1780 *
1781 * <power failure>
1782 *
1783 * mount fs, log replay happens
1784 *
1785 * File foo would remain with a link count of 1 when it has two
1786 * entries pointing to it in the directory testdir. This would
1787 * make it impossible to ever delete the parent directory has
1788 * it would result in stale dentries that can never be deleted.
1789 */
1790 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1791 struct btrfs_key di_key;
1792
1793 if (!fixup_path) {
1794 fixup_path = btrfs_alloc_path();
1795 if (!fixup_path) {
1796 ret = -ENOMEM;
1797 break;
1798 }
1799 }
1800
1801 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1802 ret = link_to_fixup_dir(trans, root, fixup_path,
1803 di_key.objectid);
1804 if (ret)
1805 break;
1806 }
1807 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001808 }
Filipe Mananabb53eda2015-07-15 23:26:43 +01001809 btrfs_free_path(fixup_path);
1810 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001811}
1812
1813/*
1814 * directory replay has two parts. There are the standard directory
1815 * items in the log copied from the subvolume, and range items
1816 * created in the log while the subvolume was logged.
1817 *
1818 * The range items tell us which parts of the key space the log
1819 * is authoritative for. During replay, if a key in the subvolume
1820 * directory is in a logged range item, but not actually in the log
1821 * that means it was deleted from the directory before the fsync
1822 * and should be removed.
1823 */
1824static noinline int find_dir_range(struct btrfs_root *root,
1825 struct btrfs_path *path,
1826 u64 dirid, int key_type,
1827 u64 *start_ret, u64 *end_ret)
1828{
1829 struct btrfs_key key;
1830 u64 found_end;
1831 struct btrfs_dir_log_item *item;
1832 int ret;
1833 int nritems;
1834
1835 if (*start_ret == (u64)-1)
1836 return 1;
1837
1838 key.objectid = dirid;
1839 key.type = key_type;
1840 key.offset = *start_ret;
1841
1842 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1843 if (ret < 0)
1844 goto out;
1845 if (ret > 0) {
1846 if (path->slots[0] == 0)
1847 goto out;
1848 path->slots[0]--;
1849 }
1850 if (ret != 0)
1851 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1852
1853 if (key.type != key_type || key.objectid != dirid) {
1854 ret = 1;
1855 goto next;
1856 }
1857 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1858 struct btrfs_dir_log_item);
1859 found_end = btrfs_dir_log_end(path->nodes[0], item);
1860
1861 if (*start_ret >= key.offset && *start_ret <= found_end) {
1862 ret = 0;
1863 *start_ret = key.offset;
1864 *end_ret = found_end;
1865 goto out;
1866 }
1867 ret = 1;
1868next:
1869 /* check the next slot in the tree to see if it is a valid item */
1870 nritems = btrfs_header_nritems(path->nodes[0]);
1871 if (path->slots[0] >= nritems) {
1872 ret = btrfs_next_leaf(root, path);
1873 if (ret)
1874 goto out;
1875 } else {
1876 path->slots[0]++;
1877 }
1878
1879 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1880
1881 if (key.type != key_type || key.objectid != dirid) {
1882 ret = 1;
1883 goto out;
1884 }
1885 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1886 struct btrfs_dir_log_item);
1887 found_end = btrfs_dir_log_end(path->nodes[0], item);
1888 *start_ret = key.offset;
1889 *end_ret = found_end;
1890 ret = 0;
1891out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001892 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001893 return ret;
1894}
1895
1896/*
1897 * this looks for a given directory item in the log. If the directory
1898 * item is not in the log, the item is removed and the inode it points
1899 * to is unlinked
1900 */
1901static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1902 struct btrfs_root *root,
1903 struct btrfs_root *log,
1904 struct btrfs_path *path,
1905 struct btrfs_path *log_path,
1906 struct inode *dir,
1907 struct btrfs_key *dir_key)
1908{
1909 int ret;
1910 struct extent_buffer *eb;
1911 int slot;
1912 u32 item_size;
1913 struct btrfs_dir_item *di;
1914 struct btrfs_dir_item *log_di;
1915 int name_len;
1916 unsigned long ptr;
1917 unsigned long ptr_end;
1918 char *name;
1919 struct inode *inode;
1920 struct btrfs_key location;
1921
1922again:
1923 eb = path->nodes[0];
1924 slot = path->slots[0];
1925 item_size = btrfs_item_size_nr(eb, slot);
1926 ptr = btrfs_item_ptr_offset(eb, slot);
1927 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001928 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001929 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001930 if (verify_dir_item(root, eb, di)) {
1931 ret = -EIO;
1932 goto out;
1933 }
1934
Chris Masone02119d2008-09-05 16:13:11 -04001935 name_len = btrfs_dir_name_len(eb, di);
1936 name = kmalloc(name_len, GFP_NOFS);
1937 if (!name) {
1938 ret = -ENOMEM;
1939 goto out;
1940 }
1941 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1942 name_len);
1943 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001944 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001945 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1946 dir_key->objectid,
1947 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001948 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001949 log_di = btrfs_lookup_dir_index_item(trans, log,
1950 log_path,
1951 dir_key->objectid,
1952 dir_key->offset,
1953 name, name_len, 0);
1954 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001955 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04001956 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001957 btrfs_release_path(path);
1958 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001959 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001960 if (!inode) {
1961 kfree(name);
1962 return -EIO;
1963 }
Chris Masone02119d2008-09-05 16:13:11 -04001964
1965 ret = link_to_fixup_dir(trans, root,
1966 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04001967 if (ret) {
1968 kfree(name);
1969 iput(inode);
1970 goto out;
1971 }
1972
Zach Brown8b558c52013-10-16 12:10:34 -07001973 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001974 ret = btrfs_unlink_inode(trans, root, dir, inode,
1975 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04001976 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001977 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001978 kfree(name);
1979 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001980 if (ret)
1981 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001982
1983 /* there might still be more names under this key
1984 * check and repeat if required
1985 */
1986 ret = btrfs_search_slot(NULL, root, dir_key, path,
1987 0, 0);
1988 if (ret == 0)
1989 goto again;
1990 ret = 0;
1991 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001992 } else if (IS_ERR(log_di)) {
1993 kfree(name);
1994 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04001995 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001996 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001997 kfree(name);
1998
1999 ptr = (unsigned long)(di + 1);
2000 ptr += name_len;
2001 }
2002 ret = 0;
2003out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002004 btrfs_release_path(path);
2005 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04002006 return ret;
2007}
2008
Filipe Manana4f764e52015-02-23 19:53:35 +00002009static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2010 struct btrfs_root *root,
2011 struct btrfs_root *log,
2012 struct btrfs_path *path,
2013 const u64 ino)
2014{
2015 struct btrfs_key search_key;
2016 struct btrfs_path *log_path;
2017 int i;
2018 int nritems;
2019 int ret;
2020
2021 log_path = btrfs_alloc_path();
2022 if (!log_path)
2023 return -ENOMEM;
2024
2025 search_key.objectid = ino;
2026 search_key.type = BTRFS_XATTR_ITEM_KEY;
2027 search_key.offset = 0;
2028again:
2029 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2030 if (ret < 0)
2031 goto out;
2032process_leaf:
2033 nritems = btrfs_header_nritems(path->nodes[0]);
2034 for (i = path->slots[0]; i < nritems; i++) {
2035 struct btrfs_key key;
2036 struct btrfs_dir_item *di;
2037 struct btrfs_dir_item *log_di;
2038 u32 total_size;
2039 u32 cur;
2040
2041 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2042 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2043 ret = 0;
2044 goto out;
2045 }
2046
2047 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2048 total_size = btrfs_item_size_nr(path->nodes[0], i);
2049 cur = 0;
2050 while (cur < total_size) {
2051 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2052 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2053 u32 this_len = sizeof(*di) + name_len + data_len;
2054 char *name;
2055
2056 name = kmalloc(name_len, GFP_NOFS);
2057 if (!name) {
2058 ret = -ENOMEM;
2059 goto out;
2060 }
2061 read_extent_buffer(path->nodes[0], name,
2062 (unsigned long)(di + 1), name_len);
2063
2064 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2065 name, name_len, 0);
2066 btrfs_release_path(log_path);
2067 if (!log_di) {
2068 /* Doesn't exist in log tree, so delete it. */
2069 btrfs_release_path(path);
2070 di = btrfs_lookup_xattr(trans, root, path, ino,
2071 name, name_len, -1);
2072 kfree(name);
2073 if (IS_ERR(di)) {
2074 ret = PTR_ERR(di);
2075 goto out;
2076 }
2077 ASSERT(di);
2078 ret = btrfs_delete_one_dir_name(trans, root,
2079 path, di);
2080 if (ret)
2081 goto out;
2082 btrfs_release_path(path);
2083 search_key = key;
2084 goto again;
2085 }
2086 kfree(name);
2087 if (IS_ERR(log_di)) {
2088 ret = PTR_ERR(log_di);
2089 goto out;
2090 }
2091 cur += this_len;
2092 di = (struct btrfs_dir_item *)((char *)di + this_len);
2093 }
2094 }
2095 ret = btrfs_next_leaf(root, path);
2096 if (ret > 0)
2097 ret = 0;
2098 else if (ret == 0)
2099 goto process_leaf;
2100out:
2101 btrfs_free_path(log_path);
2102 btrfs_release_path(path);
2103 return ret;
2104}
2105
2106
Chris Masone02119d2008-09-05 16:13:11 -04002107/*
2108 * deletion replay happens before we copy any new directory items
2109 * out of the log or out of backreferences from inodes. It
2110 * scans the log to find ranges of keys that log is authoritative for,
2111 * and then scans the directory to find items in those ranges that are
2112 * not present in the log.
2113 *
2114 * Anything we don't find in the log is unlinked and removed from the
2115 * directory.
2116 */
2117static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2118 struct btrfs_root *root,
2119 struct btrfs_root *log,
2120 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002121 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04002122{
2123 u64 range_start;
2124 u64 range_end;
2125 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2126 int ret = 0;
2127 struct btrfs_key dir_key;
2128 struct btrfs_key found_key;
2129 struct btrfs_path *log_path;
2130 struct inode *dir;
2131
2132 dir_key.objectid = dirid;
2133 dir_key.type = BTRFS_DIR_ITEM_KEY;
2134 log_path = btrfs_alloc_path();
2135 if (!log_path)
2136 return -ENOMEM;
2137
2138 dir = read_one_inode(root, dirid);
2139 /* it isn't an error if the inode isn't there, that can happen
2140 * because we replay the deletes before we copy in the inode item
2141 * from the log
2142 */
2143 if (!dir) {
2144 btrfs_free_path(log_path);
2145 return 0;
2146 }
2147again:
2148 range_start = 0;
2149 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05002150 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002151 if (del_all)
2152 range_end = (u64)-1;
2153 else {
2154 ret = find_dir_range(log, path, dirid, key_type,
2155 &range_start, &range_end);
2156 if (ret != 0)
2157 break;
2158 }
Chris Masone02119d2008-09-05 16:13:11 -04002159
2160 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05002161 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002162 int nritems;
2163 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2164 0, 0);
2165 if (ret < 0)
2166 goto out;
2167
2168 nritems = btrfs_header_nritems(path->nodes[0]);
2169 if (path->slots[0] >= nritems) {
2170 ret = btrfs_next_leaf(root, path);
2171 if (ret)
2172 break;
2173 }
2174 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2175 path->slots[0]);
2176 if (found_key.objectid != dirid ||
2177 found_key.type != dir_key.type)
2178 goto next_type;
2179
2180 if (found_key.offset > range_end)
2181 break;
2182
2183 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002184 log_path, dir,
2185 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002186 if (ret)
2187 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002188 if (found_key.offset == (u64)-1)
2189 break;
2190 dir_key.offset = found_key.offset + 1;
2191 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002192 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002193 if (range_end == (u64)-1)
2194 break;
2195 range_start = range_end + 1;
2196 }
2197
2198next_type:
2199 ret = 0;
2200 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2201 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2202 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002203 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002204 goto again;
2205 }
2206out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002207 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002208 btrfs_free_path(log_path);
2209 iput(dir);
2210 return ret;
2211}
2212
2213/*
2214 * the process_func used to replay items from the log tree. This
2215 * gets called in two different stages. The first stage just looks
2216 * for inodes and makes sure they are all copied into the subvolume.
2217 *
2218 * The second stage copies all the other item types from the log into
2219 * the subvolume. The two stage approach is slower, but gets rid of
2220 * lots of complexity around inodes referencing other inodes that exist
2221 * only in the log (references come from either directory items or inode
2222 * back refs).
2223 */
2224static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2225 struct walk_control *wc, u64 gen)
2226{
2227 int nritems;
2228 struct btrfs_path *path;
2229 struct btrfs_root *root = wc->replay_dest;
2230 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002231 int level;
2232 int i;
2233 int ret;
2234
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002235 ret = btrfs_read_buffer(eb, gen);
2236 if (ret)
2237 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002238
2239 level = btrfs_header_level(eb);
2240
2241 if (level != 0)
2242 return 0;
2243
2244 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002245 if (!path)
2246 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002247
2248 nritems = btrfs_header_nritems(eb);
2249 for (i = 0; i < nritems; i++) {
2250 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002251
2252 /* inode keys are done during the first stage */
2253 if (key.type == BTRFS_INODE_ITEM_KEY &&
2254 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002255 struct btrfs_inode_item *inode_item;
2256 u32 mode;
2257
2258 inode_item = btrfs_item_ptr(eb, i,
2259 struct btrfs_inode_item);
Filipe Manana4f764e52015-02-23 19:53:35 +00002260 ret = replay_xattr_deletes(wc->trans, root, log,
2261 path, key.objectid);
2262 if (ret)
2263 break;
Chris Masone02119d2008-09-05 16:13:11 -04002264 mode = btrfs_inode_mode(eb, inode_item);
2265 if (S_ISDIR(mode)) {
2266 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002267 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002268 if (ret)
2269 break;
Chris Masone02119d2008-09-05 16:13:11 -04002270 }
2271 ret = overwrite_item(wc->trans, root, path,
2272 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002273 if (ret)
2274 break;
Chris Masone02119d2008-09-05 16:13:11 -04002275
Yan, Zhengc71bf092009-11-12 09:34:40 +00002276 /* for regular files, make sure corresponding
2277 * orhpan item exist. extents past the new EOF
2278 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002279 */
2280 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002281 ret = insert_orphan_item(wc->trans, root,
2282 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002283 if (ret)
2284 break;
Chris Masone02119d2008-09-05 16:13:11 -04002285 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002286
Chris Masone02119d2008-09-05 16:13:11 -04002287 ret = link_to_fixup_dir(wc->trans, root,
2288 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002289 if (ret)
2290 break;
Chris Masone02119d2008-09-05 16:13:11 -04002291 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002292
2293 if (key.type == BTRFS_DIR_INDEX_KEY &&
2294 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2295 ret = replay_one_dir_item(wc->trans, root, path,
2296 eb, i, &key);
2297 if (ret)
2298 break;
2299 }
2300
Chris Masone02119d2008-09-05 16:13:11 -04002301 if (wc->stage < LOG_WALK_REPLAY_ALL)
2302 continue;
2303
2304 /* these keys are simply copied */
2305 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2306 ret = overwrite_item(wc->trans, root, path,
2307 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002308 if (ret)
2309 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002310 } else if (key.type == BTRFS_INODE_REF_KEY ||
2311 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002312 ret = add_inode_ref(wc->trans, root, log, path,
2313 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002314 if (ret && ret != -ENOENT)
2315 break;
2316 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002317 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2318 ret = replay_one_extent(wc->trans, root, path,
2319 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002320 if (ret)
2321 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002322 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002323 ret = replay_one_dir_item(wc->trans, root, path,
2324 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002325 if (ret)
2326 break;
Chris Masone02119d2008-09-05 16:13:11 -04002327 }
2328 }
2329 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002330 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002331}
2332
Chris Masond3977122009-01-05 21:25:51 -05002333static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002334 struct btrfs_root *root,
2335 struct btrfs_path *path, int *level,
2336 struct walk_control *wc)
2337{
2338 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002339 u64 bytenr;
2340 u64 ptr_gen;
2341 struct extent_buffer *next;
2342 struct extent_buffer *cur;
2343 struct extent_buffer *parent;
2344 u32 blocksize;
2345 int ret = 0;
2346
2347 WARN_ON(*level < 0);
2348 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2349
Chris Masond3977122009-01-05 21:25:51 -05002350 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002351 WARN_ON(*level < 0);
2352 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2353 cur = path->nodes[*level];
2354
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302355 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002356
2357 if (path->slots[*level] >=
2358 btrfs_header_nritems(cur))
2359 break;
2360
2361 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2362 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
David Sterba707e8a02014-06-04 19:22:26 +02002363 blocksize = root->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002364
2365 parent = path->nodes[*level];
2366 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002367
David Sterbaa83fffb2014-06-15 02:39:54 +02002368 next = btrfs_find_create_tree_block(root, bytenr);
liubo2a29edc2011-01-26 06:22:08 +00002369 if (!next)
2370 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002371
Chris Masone02119d2008-09-05 16:13:11 -04002372 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002373 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002374 if (ret) {
2375 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002376 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002377 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002378
Chris Masone02119d2008-09-05 16:13:11 -04002379 path->slots[*level]++;
2380 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002381 ret = btrfs_read_buffer(next, ptr_gen);
2382 if (ret) {
2383 free_extent_buffer(next);
2384 return ret;
2385 }
Chris Masone02119d2008-09-05 16:13:11 -04002386
Josef Bacik681ae502013-10-07 15:11:00 -04002387 if (trans) {
2388 btrfs_tree_lock(next);
2389 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002390 clean_tree_block(trans, root->fs_info,
2391 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002392 btrfs_wait_tree_block_writeback(next);
2393 btrfs_tree_unlock(next);
2394 }
Chris Masone02119d2008-09-05 16:13:11 -04002395
Chris Masone02119d2008-09-05 16:13:11 -04002396 WARN_ON(root_owner !=
2397 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002398 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002399 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002400 if (ret) {
2401 free_extent_buffer(next);
2402 return ret;
2403 }
Chris Masone02119d2008-09-05 16:13:11 -04002404 }
2405 free_extent_buffer(next);
2406 continue;
2407 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002408 ret = btrfs_read_buffer(next, ptr_gen);
2409 if (ret) {
2410 free_extent_buffer(next);
2411 return ret;
2412 }
Chris Masone02119d2008-09-05 16:13:11 -04002413
2414 WARN_ON(*level <= 0);
2415 if (path->nodes[*level-1])
2416 free_extent_buffer(path->nodes[*level-1]);
2417 path->nodes[*level-1] = next;
2418 *level = btrfs_header_level(next);
2419 path->slots[*level] = 0;
2420 cond_resched();
2421 }
2422 WARN_ON(*level < 0);
2423 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2424
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002425 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002426
2427 cond_resched();
2428 return 0;
2429}
2430
Chris Masond3977122009-01-05 21:25:51 -05002431static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002432 struct btrfs_root *root,
2433 struct btrfs_path *path, int *level,
2434 struct walk_control *wc)
2435{
2436 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002437 int i;
2438 int slot;
2439 int ret;
2440
Chris Masond3977122009-01-05 21:25:51 -05002441 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002442 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002443 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002444 path->slots[i]++;
2445 *level = i;
2446 WARN_ON(*level == 0);
2447 return 0;
2448 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002449 struct extent_buffer *parent;
2450 if (path->nodes[*level] == root->node)
2451 parent = path->nodes[*level];
2452 else
2453 parent = path->nodes[*level + 1];
2454
2455 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002456 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002457 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002458 if (ret)
2459 return ret;
2460
Chris Masone02119d2008-09-05 16:13:11 -04002461 if (wc->free) {
2462 struct extent_buffer *next;
2463
2464 next = path->nodes[*level];
2465
Josef Bacik681ae502013-10-07 15:11:00 -04002466 if (trans) {
2467 btrfs_tree_lock(next);
2468 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002469 clean_tree_block(trans, root->fs_info,
2470 next);
Josef Bacik681ae502013-10-07 15:11:00 -04002471 btrfs_wait_tree_block_writeback(next);
2472 btrfs_tree_unlock(next);
2473 }
Chris Masone02119d2008-09-05 16:13:11 -04002474
Chris Masone02119d2008-09-05 16:13:11 -04002475 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002476 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002477 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002478 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002479 if (ret)
2480 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002481 }
2482 free_extent_buffer(path->nodes[*level]);
2483 path->nodes[*level] = NULL;
2484 *level = i + 1;
2485 }
2486 }
2487 return 1;
2488}
2489
2490/*
2491 * drop the reference count on the tree rooted at 'snap'. This traverses
2492 * the tree freeing any blocks that have a ref count of zero after being
2493 * decremented.
2494 */
2495static int walk_log_tree(struct btrfs_trans_handle *trans,
2496 struct btrfs_root *log, struct walk_control *wc)
2497{
2498 int ret = 0;
2499 int wret;
2500 int level;
2501 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002502 int orig_level;
2503
2504 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002505 if (!path)
2506 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002507
2508 level = btrfs_header_level(log->node);
2509 orig_level = level;
2510 path->nodes[level] = log->node;
2511 extent_buffer_get(log->node);
2512 path->slots[level] = 0;
2513
Chris Masond3977122009-01-05 21:25:51 -05002514 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002515 wret = walk_down_log_tree(trans, log, path, &level, wc);
2516 if (wret > 0)
2517 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002518 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002519 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002520 goto out;
2521 }
Chris Masone02119d2008-09-05 16:13:11 -04002522
2523 wret = walk_up_log_tree(trans, log, path, &level, wc);
2524 if (wret > 0)
2525 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002526 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002527 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002528 goto out;
2529 }
Chris Masone02119d2008-09-05 16:13:11 -04002530 }
2531
2532 /* was the root node processed? if not, catch it here */
2533 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002534 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002535 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002536 if (ret)
2537 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002538 if (wc->free) {
2539 struct extent_buffer *next;
2540
2541 next = path->nodes[orig_level];
2542
Josef Bacik681ae502013-10-07 15:11:00 -04002543 if (trans) {
2544 btrfs_tree_lock(next);
2545 btrfs_set_lock_blocking(next);
Daniel Dressler01d58472014-11-21 17:15:07 +09002546 clean_tree_block(trans, log->fs_info, next);
Josef Bacik681ae502013-10-07 15:11:00 -04002547 btrfs_wait_tree_block_writeback(next);
2548 btrfs_tree_unlock(next);
2549 }
Chris Masone02119d2008-09-05 16:13:11 -04002550
Chris Masone02119d2008-09-05 16:13:11 -04002551 WARN_ON(log->root_key.objectid !=
2552 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002553 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002554 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002555 if (ret)
2556 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002557 }
2558 }
2559
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002560out:
Chris Masone02119d2008-09-05 16:13:11 -04002561 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002562 return ret;
2563}
2564
Yan Zheng7237f182009-01-21 12:54:03 -05002565/*
2566 * helper function to update the item for a given subvolumes log root
2567 * in the tree of log roots
2568 */
2569static int update_log_root(struct btrfs_trans_handle *trans,
2570 struct btrfs_root *log)
2571{
2572 int ret;
2573
2574 if (log->log_transid == 1) {
2575 /* insert root item on the first sync */
2576 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2577 &log->root_key, &log->root_item);
2578 } else {
2579 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2580 &log->root_key, &log->root_item);
2581 }
2582 return ret;
2583}
2584
Miao Xie8b050d32014-02-20 18:08:58 +08002585static void wait_log_commit(struct btrfs_trans_handle *trans,
2586 struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002587{
2588 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002589 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002590
Yan Zheng7237f182009-01-21 12:54:03 -05002591 /*
2592 * we only allow two pending log transactions at a time,
2593 * so we know that if ours is more than 2 older than the
2594 * current transaction, we're done
2595 */
Chris Masone02119d2008-09-05 16:13:11 -04002596 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002597 prepare_to_wait(&root->log_commit_wait[index],
2598 &wait, TASK_UNINTERRUPTIBLE);
2599 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002600
Miao Xied1433de2014-02-20 18:08:59 +08002601 if (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002602 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002603 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002604
Yan Zheng7237f182009-01-21 12:54:03 -05002605 finish_wait(&root->log_commit_wait[index], &wait);
2606 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002607 } while (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002608 atomic_read(&root->log_commit[index]));
Yan Zheng7237f182009-01-21 12:54:03 -05002609}
2610
Jeff Mahoney143bede2012-03-01 14:56:26 +01002611static void wait_for_writer(struct btrfs_trans_handle *trans,
2612 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002613{
2614 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002615
2616 while (atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002617 prepare_to_wait(&root->log_writer_wait,
2618 &wait, TASK_UNINTERRUPTIBLE);
2619 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002620 if (atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002621 schedule();
Yan Zheng7237f182009-01-21 12:54:03 -05002622 finish_wait(&root->log_writer_wait, &wait);
Filipe Manana575849e2015-02-11 11:12:39 +00002623 mutex_lock(&root->log_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05002624 }
Chris Masone02119d2008-09-05 16:13:11 -04002625}
2626
Miao Xie8b050d32014-02-20 18:08:58 +08002627static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2628 struct btrfs_log_ctx *ctx)
2629{
2630 if (!ctx)
2631 return;
2632
2633 mutex_lock(&root->log_mutex);
2634 list_del_init(&ctx->list);
2635 mutex_unlock(&root->log_mutex);
2636}
2637
2638/*
2639 * Invoked in log mutex context, or be sure there is no other task which
2640 * can access the list.
2641 */
2642static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2643 int index, int error)
2644{
2645 struct btrfs_log_ctx *ctx;
2646
2647 if (!error) {
2648 INIT_LIST_HEAD(&root->log_ctxs[index]);
2649 return;
2650 }
2651
2652 list_for_each_entry(ctx, &root->log_ctxs[index], list)
2653 ctx->log_ret = error;
2654
2655 INIT_LIST_HEAD(&root->log_ctxs[index]);
2656}
2657
Chris Masone02119d2008-09-05 16:13:11 -04002658/*
2659 * btrfs_sync_log does sends a given tree log down to the disk and
2660 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002661 * you know that any inodes previously logged are safely on disk only
2662 * if it returns 0.
2663 *
2664 * Any other return value means you need to call btrfs_commit_transaction.
2665 * Some of the edge cases for fsyncing directories that have had unlinks
2666 * or renames done in the past mean that sometimes the only safe
2667 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2668 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002669 */
2670int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002671 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002672{
Yan Zheng7237f182009-01-21 12:54:03 -05002673 int index1;
2674 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002675 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002676 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002677 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002678 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002679 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002680 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002681 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002682
Yan Zheng7237f182009-01-21 12:54:03 -05002683 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002684 log_transid = ctx->log_transid;
2685 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002686 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002687 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002688 }
Miao Xied1433de2014-02-20 18:08:59 +08002689
2690 index1 = log_transid % 2;
2691 if (atomic_read(&root->log_commit[index1])) {
2692 wait_log_commit(trans, root, log_transid);
2693 mutex_unlock(&root->log_mutex);
2694 return ctx->log_ret;
2695 }
2696 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002697 atomic_set(&root->log_commit[index1], 1);
2698
2699 /* wait for previous tree log sync to complete */
2700 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Miao Xied1433de2014-02-20 18:08:59 +08002701 wait_log_commit(trans, root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002702
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002703 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002704 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002705 /* when we're on an ssd, just kick the log commit out */
Miao Xie27cdeb72014-04-02 19:51:05 +08002706 if (!btrfs_test_opt(root, SSD) &&
2707 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002708 mutex_unlock(&root->log_mutex);
2709 schedule_timeout_uninterruptible(1);
2710 mutex_lock(&root->log_mutex);
2711 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002712 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002713 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002714 break;
2715 }
Chris Masond0c803c2008-09-11 16:17:57 -04002716
Chris Mason12fcfd22009-03-24 10:24:20 -04002717 /* bail out if we need to do a full commit */
Miao Xie995946d2014-04-02 19:51:06 +08002718 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002719 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002720 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002721 mutex_unlock(&root->log_mutex);
2722 goto out;
2723 }
2724
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002725 if (log_transid % 2 == 0)
2726 mark = EXTENT_DIRTY;
2727 else
2728 mark = EXTENT_NEW;
2729
Chris Mason690587d2009-10-13 13:29:19 -04002730 /* we start IO on all the marked extents here, but we don't actually
2731 * wait for them until later.
2732 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002733 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002734 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002735 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002736 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002737 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002738 btrfs_free_logged_extents(log, log_transid);
Miao Xie995946d2014-04-02 19:51:06 +08002739 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002740 mutex_unlock(&root->log_mutex);
2741 goto out;
2742 }
Yan Zheng7237f182009-01-21 12:54:03 -05002743
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002744 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002745
Yan Zheng7237f182009-01-21 12:54:03 -05002746 root->log_transid++;
2747 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002748 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002749 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002750 * IO has been started, blocks of the log tree have WRITTEN flag set
2751 * in their headers. new modifications of the log will be written to
2752 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002753 */
2754 mutex_unlock(&root->log_mutex);
2755
Miao Xied1433de2014-02-20 18:08:59 +08002756 btrfs_init_log_ctx(&root_log_ctx);
2757
Yan Zheng7237f182009-01-21 12:54:03 -05002758 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002759 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002760 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002761
2762 index2 = log_root_tree->log_transid % 2;
2763 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2764 root_log_ctx.log_transid = log_root_tree->log_transid;
2765
Yan Zheng7237f182009-01-21 12:54:03 -05002766 mutex_unlock(&log_root_tree->log_mutex);
2767
2768 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002769
2770 mutex_lock(&log_root_tree->log_mutex);
2771 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2772 smp_mb();
2773 if (waitqueue_active(&log_root_tree->log_writer_wait))
2774 wake_up(&log_root_tree->log_writer_wait);
2775 }
2776
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002777 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002778 if (!list_empty(&root_log_ctx.list))
2779 list_del_init(&root_log_ctx.list);
2780
Miao Xiec6adc9c2013-05-28 10:05:39 +00002781 blk_finish_plug(&plug);
Miao Xie995946d2014-04-02 19:51:06 +08002782 btrfs_set_log_full_commit(root->fs_info, trans);
2783
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002784 if (ret != -ENOSPC) {
2785 btrfs_abort_transaction(trans, root, ret);
2786 mutex_unlock(&log_root_tree->log_mutex);
2787 goto out;
2788 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002789 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002790 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002791 mutex_unlock(&log_root_tree->log_mutex);
2792 ret = -EAGAIN;
2793 goto out;
2794 }
2795
Miao Xied1433de2014-02-20 18:08:59 +08002796 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
Forrest Liu3da5ab52015-01-30 19:42:12 +08002797 blk_finish_plug(&plug);
Miao Xied1433de2014-02-20 18:08:59 +08002798 mutex_unlock(&log_root_tree->log_mutex);
2799 ret = root_log_ctx.log_ret;
2800 goto out;
2801 }
Miao Xie8b050d32014-02-20 18:08:58 +08002802
Miao Xied1433de2014-02-20 18:08:59 +08002803 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05002804 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002805 blk_finish_plug(&plug);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002806 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages,
2807 mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05002808 btrfs_wait_logged_extents(trans, log, log_transid);
Miao Xie8b050d32014-02-20 18:08:58 +08002809 wait_log_commit(trans, log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002810 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002811 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002812 if (!ret)
2813 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002814 goto out;
2815 }
Miao Xied1433de2014-02-20 18:08:59 +08002816 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002817 atomic_set(&log_root_tree->log_commit[index2], 1);
2818
Chris Mason12fcfd22009-03-24 10:24:20 -04002819 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2820 wait_log_commit(trans, log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002821 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002822 }
Yan Zheng7237f182009-01-21 12:54:03 -05002823
Chris Mason12fcfd22009-03-24 10:24:20 -04002824 wait_for_writer(trans, log_root_tree);
2825
2826 /*
2827 * now that we've moved on to the tree of log tree roots,
2828 * check the full commit flag again
2829 */
Miao Xie995946d2014-04-02 19:51:06 +08002830 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002831 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002832 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002833 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002834 mutex_unlock(&log_root_tree->log_mutex);
2835 ret = -EAGAIN;
2836 goto out_wake_log_root;
2837 }
Yan Zheng7237f182009-01-21 12:54:03 -05002838
Miao Xiec6adc9c2013-05-28 10:05:39 +00002839 ret = btrfs_write_marked_extents(log_root_tree,
2840 &log_root_tree->dirty_log_pages,
2841 EXTENT_DIRTY | EXTENT_NEW);
2842 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002843 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002844 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002845 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002846 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002847 mutex_unlock(&log_root_tree->log_mutex);
2848 goto out_wake_log_root;
2849 }
Filipe Manana5ab5e442014-11-13 16:59:53 +00002850 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2851 if (!ret)
2852 ret = btrfs_wait_marked_extents(log_root_tree,
2853 &log_root_tree->dirty_log_pages,
2854 EXTENT_NEW | EXTENT_DIRTY);
2855 if (ret) {
2856 btrfs_set_log_full_commit(root->fs_info, trans);
2857 btrfs_free_logged_extents(log, log_transid);
2858 mutex_unlock(&log_root_tree->log_mutex);
2859 goto out_wake_log_root;
2860 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05002861 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002862
David Sterba6c417612011-04-13 15:41:04 +02002863 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002864 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002865 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002866 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002867
Yan Zheng7237f182009-01-21 12:54:03 -05002868 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002869 mutex_unlock(&log_root_tree->log_mutex);
2870
2871 /*
2872 * nobody else is going to jump in and write the the ctree
2873 * super here because the log_commit atomic below is protecting
2874 * us. We must be called with a transaction handle pinning
2875 * the running transaction open, so a full commit can't hop
2876 * in and cause problems either.
2877 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002878 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002879 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002880 btrfs_set_log_full_commit(root->fs_info, trans);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002881 btrfs_abort_transaction(trans, root, ret);
2882 goto out_wake_log_root;
2883 }
Yan Zheng7237f182009-01-21 12:54:03 -05002884
Chris Mason257c62e2009-10-13 13:21:08 -04002885 mutex_lock(&root->log_mutex);
2886 if (root->last_log_commit < log_transid)
2887 root->last_log_commit = log_transid;
2888 mutex_unlock(&root->log_mutex);
2889
Chris Mason12fcfd22009-03-24 10:24:20 -04002890out_wake_log_root:
Miao Xie8b050d32014-02-20 18:08:58 +08002891 /*
2892 * We needn't get log_mutex here because we are sure all
2893 * the other tasks are blocked.
2894 */
2895 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2896
Miao Xied1433de2014-02-20 18:08:59 +08002897 mutex_lock(&log_root_tree->log_mutex);
2898 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002899 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002900 mutex_unlock(&log_root_tree->log_mutex);
2901
Yan Zheng7237f182009-01-21 12:54:03 -05002902 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2903 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002904out:
Miao Xie8b050d32014-02-20 18:08:58 +08002905 /* See above. */
2906 btrfs_remove_all_log_ctxs(root, index1, ret);
2907
Miao Xied1433de2014-02-20 18:08:59 +08002908 mutex_lock(&root->log_mutex);
2909 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002910 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002911 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002912
Yan Zheng7237f182009-01-21 12:54:03 -05002913 if (waitqueue_active(&root->log_commit_wait[index1]))
2914 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002915 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002916}
2917
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002918static void free_log_tree(struct btrfs_trans_handle *trans,
2919 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002920{
2921 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002922 u64 start;
2923 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002924 struct walk_control wc = {
2925 .free = 1,
2926 .process_func = process_one_buffer
2927 };
2928
Josef Bacik681ae502013-10-07 15:11:00 -04002929 ret = walk_log_tree(trans, log, &wc);
2930 /* I don't think this can happen but just in case */
2931 if (ret)
2932 btrfs_abort_transaction(trans, log, ret);
Chris Masone02119d2008-09-05 16:13:11 -04002933
Chris Masond3977122009-01-05 21:25:51 -05002934 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002935 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002936 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2937 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002938 if (ret)
2939 break;
2940
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002941 clear_extent_bits(&log->dirty_log_pages, start, end,
2942 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002943 }
2944
Josef Bacik2ab28f32012-10-12 15:27:49 -04002945 /*
2946 * We may have short-circuited the log tree with the full commit logic
2947 * and left ordered extents on our list, so clear these out to keep us
2948 * from leaking inodes and memory.
2949 */
2950 btrfs_free_logged_extents(log, 0);
2951 btrfs_free_logged_extents(log, 1);
2952
Yan Zheng7237f182009-01-21 12:54:03 -05002953 free_extent_buffer(log->node);
2954 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002955}
2956
2957/*
2958 * free all the extents used by the tree log. This should be called
2959 * at commit time of the full transaction
2960 */
2961int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2962{
2963 if (root->log_root) {
2964 free_log_tree(trans, root->log_root);
2965 root->log_root = NULL;
2966 }
2967 return 0;
2968}
2969
2970int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2971 struct btrfs_fs_info *fs_info)
2972{
2973 if (fs_info->log_root_tree) {
2974 free_log_tree(trans, fs_info->log_root_tree);
2975 fs_info->log_root_tree = NULL;
2976 }
Chris Masone02119d2008-09-05 16:13:11 -04002977 return 0;
2978}
2979
2980/*
Chris Masone02119d2008-09-05 16:13:11 -04002981 * If both a file and directory are logged, and unlinks or renames are
2982 * mixed in, we have a few interesting corners:
2983 *
2984 * create file X in dir Y
2985 * link file X to X.link in dir Y
2986 * fsync file X
2987 * unlink file X but leave X.link
2988 * fsync dir Y
2989 *
2990 * After a crash we would expect only X.link to exist. But file X
2991 * didn't get fsync'd again so the log has back refs for X and X.link.
2992 *
2993 * We solve this by removing directory entries and inode backrefs from the
2994 * log when a file that was logged in the current transaction is
2995 * unlinked. Any later fsync will include the updated log entries, and
2996 * we'll be able to reconstruct the proper directory items from backrefs.
2997 *
2998 * This optimizations allows us to avoid relogging the entire inode
2999 * or the entire directory.
3000 */
3001int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3002 struct btrfs_root *root,
3003 const char *name, int name_len,
3004 struct inode *dir, u64 index)
3005{
3006 struct btrfs_root *log;
3007 struct btrfs_dir_item *di;
3008 struct btrfs_path *path;
3009 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003010 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003011 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003012 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003013
Chris Mason3a5f1d42008-09-11 15:53:37 -04003014 if (BTRFS_I(dir)->logged_trans < trans->transid)
3015 return 0;
3016
Chris Masone02119d2008-09-05 16:13:11 -04003017 ret = join_running_log_trans(root);
3018 if (ret)
3019 return 0;
3020
3021 mutex_lock(&BTRFS_I(dir)->log_mutex);
3022
3023 log = root->log_root;
3024 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003025 if (!path) {
3026 err = -ENOMEM;
3027 goto out_unlock;
3028 }
liubo2a29edc2011-01-26 06:22:08 +00003029
Li Zefan33345d012011-04-20 10:31:50 +08003030 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003031 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003032 if (IS_ERR(di)) {
3033 err = PTR_ERR(di);
3034 goto fail;
3035 }
3036 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003037 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3038 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003039 if (ret) {
3040 err = ret;
3041 goto fail;
3042 }
Chris Masone02119d2008-09-05 16:13:11 -04003043 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003044 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08003045 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04003046 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003047 if (IS_ERR(di)) {
3048 err = PTR_ERR(di);
3049 goto fail;
3050 }
3051 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04003052 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3053 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04003054 if (ret) {
3055 err = ret;
3056 goto fail;
3057 }
Chris Masone02119d2008-09-05 16:13:11 -04003058 }
3059
3060 /* update the directory size in the log to reflect the names
3061 * we have removed
3062 */
3063 if (bytes_del) {
3064 struct btrfs_key key;
3065
Li Zefan33345d012011-04-20 10:31:50 +08003066 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04003067 key.offset = 0;
3068 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02003069 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003070
3071 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003072 if (ret < 0) {
3073 err = ret;
3074 goto fail;
3075 }
Chris Masone02119d2008-09-05 16:13:11 -04003076 if (ret == 0) {
3077 struct btrfs_inode_item *item;
3078 u64 i_size;
3079
3080 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3081 struct btrfs_inode_item);
3082 i_size = btrfs_inode_size(path->nodes[0], item);
3083 if (i_size > bytes_del)
3084 i_size -= bytes_del;
3085 else
3086 i_size = 0;
3087 btrfs_set_inode_size(path->nodes[0], item, i_size);
3088 btrfs_mark_buffer_dirty(path->nodes[0]);
3089 } else
3090 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003091 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003092 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003093fail:
Chris Masone02119d2008-09-05 16:13:11 -04003094 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04003095out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04003096 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003097 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003098 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003099 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003100 } else if (ret < 0)
3101 btrfs_abort_transaction(trans, root, ret);
3102
Chris Mason12fcfd22009-03-24 10:24:20 -04003103 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003104
Andi Kleen411fc6b2010-10-29 15:14:31 -04003105 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003106}
3107
3108/* see comments for btrfs_del_dir_entries_in_log */
3109int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3110 struct btrfs_root *root,
3111 const char *name, int name_len,
3112 struct inode *inode, u64 dirid)
3113{
3114 struct btrfs_root *log;
3115 u64 index;
3116 int ret;
3117
Chris Mason3a5f1d42008-09-11 15:53:37 -04003118 if (BTRFS_I(inode)->logged_trans < trans->transid)
3119 return 0;
3120
Chris Masone02119d2008-09-05 16:13:11 -04003121 ret = join_running_log_trans(root);
3122 if (ret)
3123 return 0;
3124 log = root->log_root;
3125 mutex_lock(&BTRFS_I(inode)->log_mutex);
3126
Li Zefan33345d012011-04-20 10:31:50 +08003127 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04003128 dirid, &index);
3129 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003130 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08003131 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003132 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003133 } else if (ret < 0 && ret != -ENOENT)
3134 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04003135 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04003136
Chris Masone02119d2008-09-05 16:13:11 -04003137 return ret;
3138}
3139
3140/*
3141 * creates a range item in the log for 'dirid'. first_offset and
3142 * last_offset tell us which parts of the key space the log should
3143 * be considered authoritative for.
3144 */
3145static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3146 struct btrfs_root *log,
3147 struct btrfs_path *path,
3148 int key_type, u64 dirid,
3149 u64 first_offset, u64 last_offset)
3150{
3151 int ret;
3152 struct btrfs_key key;
3153 struct btrfs_dir_log_item *item;
3154
3155 key.objectid = dirid;
3156 key.offset = first_offset;
3157 if (key_type == BTRFS_DIR_ITEM_KEY)
3158 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3159 else
3160 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3161 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003162 if (ret)
3163 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003164
3165 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3166 struct btrfs_dir_log_item);
3167 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3168 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003169 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003170 return 0;
3171}
3172
3173/*
3174 * log all the items included in the current transaction for a given
3175 * directory. This also creates the range items in the log tree required
3176 * to replay anything deleted before the fsync
3177 */
3178static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3179 struct btrfs_root *root, struct inode *inode,
3180 struct btrfs_path *path,
3181 struct btrfs_path *dst_path, int key_type,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003182 struct btrfs_log_ctx *ctx,
Chris Masone02119d2008-09-05 16:13:11 -04003183 u64 min_offset, u64 *last_offset_ret)
3184{
3185 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003186 struct btrfs_root *log = root->log_root;
3187 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003188 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003189 int ret;
3190 int i;
3191 int nritems;
3192 u64 first_offset = min_offset;
3193 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08003194 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003195
3196 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003197
Li Zefan33345d012011-04-20 10:31:50 +08003198 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003199 min_key.type = key_type;
3200 min_key.offset = min_offset;
3201
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003202 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003203
3204 /*
3205 * we didn't find anything from this transaction, see if there
3206 * is anything at all
3207 */
Li Zefan33345d012011-04-20 10:31:50 +08003208 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3209 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003210 min_key.type = key_type;
3211 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003212 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003213 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3214 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003215 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003216 return ret;
3217 }
Li Zefan33345d012011-04-20 10:31:50 +08003218 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003219
3220 /* if ret == 0 there are items for this type,
3221 * create a range to tell us the last key of this type.
3222 * otherwise, there are no items in this directory after
3223 * *min_offset, and we create a range to indicate that.
3224 */
3225 if (ret == 0) {
3226 struct btrfs_key tmp;
3227 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3228 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003229 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003230 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003231 }
3232 goto done;
3233 }
3234
3235 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003236 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003237 if (ret == 0) {
3238 struct btrfs_key tmp;
3239 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3240 if (key_type == tmp.type) {
3241 first_offset = tmp.offset;
3242 ret = overwrite_item(trans, log, dst_path,
3243 path->nodes[0], path->slots[0],
3244 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003245 if (ret) {
3246 err = ret;
3247 goto done;
3248 }
Chris Masone02119d2008-09-05 16:13:11 -04003249 }
3250 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003251 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003252
3253 /* find the first key from this transaction again */
3254 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303255 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003256 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003257
3258 /*
3259 * we have a block from this transaction, log every item in it
3260 * from our directory
3261 */
Chris Masond3977122009-01-05 21:25:51 -05003262 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003263 struct btrfs_key tmp;
3264 src = path->nodes[0];
3265 nritems = btrfs_header_nritems(src);
3266 for (i = path->slots[0]; i < nritems; i++) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003267 struct btrfs_dir_item *di;
3268
Chris Masone02119d2008-09-05 16:13:11 -04003269 btrfs_item_key_to_cpu(src, &min_key, i);
3270
Li Zefan33345d012011-04-20 10:31:50 +08003271 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003272 goto done;
3273 ret = overwrite_item(trans, log, dst_path, src, i,
3274 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003275 if (ret) {
3276 err = ret;
3277 goto done;
3278 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003279
3280 /*
3281 * We must make sure that when we log a directory entry,
3282 * the corresponding inode, after log replay, has a
3283 * matching link count. For example:
3284 *
3285 * touch foo
3286 * mkdir mydir
3287 * sync
3288 * ln foo mydir/bar
3289 * xfs_io -c "fsync" mydir
3290 * <crash>
3291 * <mount fs and log replay>
3292 *
3293 * Would result in a fsync log that when replayed, our
3294 * file inode would have a link count of 1, but we get
3295 * two directory entries pointing to the same inode.
3296 * After removing one of the names, it would not be
3297 * possible to remove the other name, which resulted
3298 * always in stale file handle errors, and would not
3299 * be possible to rmdir the parent directory, since
3300 * its i_size could never decrement to the value
3301 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3302 */
3303 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3304 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3305 if (ctx &&
3306 (btrfs_dir_transid(src, di) == trans->transid ||
3307 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3308 tmp.type != BTRFS_ROOT_ITEM_KEY)
3309 ctx->log_new_dentries = true;
Chris Masone02119d2008-09-05 16:13:11 -04003310 }
3311 path->slots[0] = nritems;
3312
3313 /*
3314 * look ahead to the next item and see if it is also
3315 * from this directory and from this transaction
3316 */
3317 ret = btrfs_next_leaf(root, path);
3318 if (ret == 1) {
3319 last_offset = (u64)-1;
3320 goto done;
3321 }
3322 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003323 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003324 last_offset = (u64)-1;
3325 goto done;
3326 }
3327 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3328 ret = overwrite_item(trans, log, dst_path,
3329 path->nodes[0], path->slots[0],
3330 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003331 if (ret)
3332 err = ret;
3333 else
3334 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003335 goto done;
3336 }
3337 }
3338done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003339 btrfs_release_path(path);
3340 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003341
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003342 if (err == 0) {
3343 *last_offset_ret = last_offset;
3344 /*
3345 * insert the log range keys to indicate where the log
3346 * is valid
3347 */
3348 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003349 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003350 if (ret)
3351 err = ret;
3352 }
3353 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003354}
3355
3356/*
3357 * logging directories is very similar to logging inodes, We find all the items
3358 * from the current transaction and write them to the log.
3359 *
3360 * The recovery code scans the directory in the subvolume, and if it finds a
3361 * key in the range logged that is not present in the log tree, then it means
3362 * that dir entry was unlinked during the transaction.
3363 *
3364 * In order for that scan to work, we must include one key smaller than
3365 * the smallest logged by this transaction and one key larger than the largest
3366 * key logged by this transaction.
3367 */
3368static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3369 struct btrfs_root *root, struct inode *inode,
3370 struct btrfs_path *path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003371 struct btrfs_path *dst_path,
3372 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003373{
3374 u64 min_key;
3375 u64 max_key;
3376 int ret;
3377 int key_type = BTRFS_DIR_ITEM_KEY;
3378
3379again:
3380 min_key = 0;
3381 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003382 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003383 ret = log_dir_items(trans, root, inode, path,
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00003384 dst_path, key_type, ctx, min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003385 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003386 if (ret)
3387 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003388 if (max_key == (u64)-1)
3389 break;
3390 min_key = max_key + 1;
3391 }
3392
3393 if (key_type == BTRFS_DIR_ITEM_KEY) {
3394 key_type = BTRFS_DIR_INDEX_KEY;
3395 goto again;
3396 }
3397 return 0;
3398}
3399
3400/*
3401 * a helper function to drop items from the log before we relog an
3402 * inode. max_key_type indicates the highest item type to remove.
3403 * This cannot be run for file data extents because it does not
3404 * free the extents they point to.
3405 */
3406static int drop_objectid_items(struct btrfs_trans_handle *trans,
3407 struct btrfs_root *log,
3408 struct btrfs_path *path,
3409 u64 objectid, int max_key_type)
3410{
3411 int ret;
3412 struct btrfs_key key;
3413 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003414 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003415
3416 key.objectid = objectid;
3417 key.type = max_key_type;
3418 key.offset = (u64)-1;
3419
Chris Masond3977122009-01-05 21:25:51 -05003420 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003421 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003422 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003423 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003424 break;
3425
3426 if (path->slots[0] == 0)
3427 break;
3428
3429 path->slots[0]--;
3430 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3431 path->slots[0]);
3432
3433 if (found_key.objectid != objectid)
3434 break;
3435
Josef Bacik18ec90d2012-09-28 11:56:28 -04003436 found_key.offset = 0;
3437 found_key.type = 0;
3438 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3439 &start_slot);
3440
3441 ret = btrfs_del_items(trans, log, path, start_slot,
3442 path->slots[0] - start_slot + 1);
3443 /*
3444 * If start slot isn't 0 then we don't need to re-search, we've
3445 * found the last guy with the objectid in this tree.
3446 */
3447 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003448 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003449 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003450 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003451 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003452 if (ret > 0)
3453 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003454 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003455}
3456
Josef Bacik94edf4a2012-09-25 14:56:25 -04003457static void fill_inode_item(struct btrfs_trans_handle *trans,
3458 struct extent_buffer *leaf,
3459 struct btrfs_inode_item *item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003460 struct inode *inode, int log_inode_only,
3461 u64 logged_isize)
Josef Bacik94edf4a2012-09-25 14:56:25 -04003462{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003463 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003464
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003465 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003466
3467 if (log_inode_only) {
3468 /* set the generation to zero so the recover code
3469 * can tell the difference between an logging
3470 * just to say 'this inode exists' and a logging
3471 * to say 'update this inode with these values'
3472 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003473 btrfs_set_token_inode_generation(leaf, item, 0, &token);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003474 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003475 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003476 btrfs_set_token_inode_generation(leaf, item,
3477 BTRFS_I(inode)->generation,
3478 &token);
3479 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003480 }
3481
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003482 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3483 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3484 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3485 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3486
David Sterbaa937b972014-12-12 17:39:12 +01003487 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003488 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003489 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003490 inode->i_atime.tv_nsec, &token);
3491
David Sterbaa937b972014-12-12 17:39:12 +01003492 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003493 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003494 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003495 inode->i_mtime.tv_nsec, &token);
3496
David Sterbaa937b972014-12-12 17:39:12 +01003497 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003498 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003499 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003500 inode->i_ctime.tv_nsec, &token);
3501
3502 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3503 &token);
3504
3505 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3506 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3507 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3508 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3509 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003510}
3511
Josef Bacika95249b2012-10-11 16:17:34 -04003512static int log_inode_item(struct btrfs_trans_handle *trans,
3513 struct btrfs_root *log, struct btrfs_path *path,
3514 struct inode *inode)
3515{
3516 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003517 int ret;
3518
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003519 ret = btrfs_insert_empty_item(trans, log, path,
3520 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003521 sizeof(*inode_item));
3522 if (ret && ret != -EEXIST)
3523 return ret;
3524 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3525 struct btrfs_inode_item);
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003526 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003527 btrfs_release_path(path);
3528 return 0;
3529}
3530
Chris Mason31ff1cd2008-09-11 16:17:57 -04003531static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003532 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003533 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003534 struct btrfs_path *src_path, u64 *last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003535 int start_slot, int nr, int inode_only,
3536 u64 logged_isize)
Chris Mason31ff1cd2008-09-11 16:17:57 -04003537{
3538 unsigned long src_offset;
3539 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003540 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003541 struct btrfs_file_extent_item *extent;
3542 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003543 struct extent_buffer *src = src_path->nodes[0];
3544 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003545 int ret;
3546 struct btrfs_key *ins_keys;
3547 u32 *ins_sizes;
3548 char *ins_data;
3549 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003550 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003551 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003552 bool has_extents = false;
Filipe Manana74121f72014-08-07 12:00:44 +01003553 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003554 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003555
3556 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003557
3558 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3559 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003560 if (!ins_data)
3561 return -ENOMEM;
3562
Josef Bacik16e75492013-10-22 12:18:51 -04003563 first_key.objectid = (u64)-1;
3564
Chris Mason31ff1cd2008-09-11 16:17:57 -04003565 ins_sizes = (u32 *)ins_data;
3566 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3567
3568 for (i = 0; i < nr; i++) {
3569 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3570 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3571 }
3572 ret = btrfs_insert_empty_items(trans, log, dst_path,
3573 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003574 if (ret) {
3575 kfree(ins_data);
3576 return ret;
3577 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003578
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003579 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003580 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3581 dst_path->slots[0]);
3582
3583 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3584
Josef Bacik16e75492013-10-22 12:18:51 -04003585 if ((i == (nr - 1)))
3586 last_key = ins_keys[i];
3587
Josef Bacik94edf4a2012-09-25 14:56:25 -04003588 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003589 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3590 dst_path->slots[0],
3591 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003592 fill_inode_item(trans, dst_path->nodes[0], inode_item,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00003593 inode, inode_only == LOG_INODE_EXISTS,
3594 logged_isize);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003595 } else {
3596 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3597 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003598 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003599
Josef Bacik16e75492013-10-22 12:18:51 -04003600 /*
3601 * We set need_find_last_extent here in case we know we were
3602 * processing other items and then walk into the first extent in
3603 * the inode. If we don't hit an extent then nothing changes,
3604 * we'll do the last search the next time around.
3605 */
3606 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3607 has_extents = true;
Filipe Manana74121f72014-08-07 12:00:44 +01003608 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003609 first_key = ins_keys[i];
3610 } else {
3611 need_find_last_extent = false;
3612 }
3613
Chris Mason31ff1cd2008-09-11 16:17:57 -04003614 /* take a reference on file data extents so that truncates
3615 * or deletes of this inode don't have to relog the inode
3616 * again
3617 */
David Sterba962a2982014-06-04 18:41:45 +02003618 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003619 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003620 int found_type;
3621 extent = btrfs_item_ptr(src, start_slot + i,
3622 struct btrfs_file_extent_item);
3623
liubo8e531cd2011-05-06 10:36:09 +08003624 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3625 continue;
3626
Chris Mason31ff1cd2008-09-11 16:17:57 -04003627 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003628 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003629 u64 ds, dl, cs, cl;
3630 ds = btrfs_file_extent_disk_bytenr(src,
3631 extent);
3632 /* ds == 0 is a hole */
3633 if (ds == 0)
3634 continue;
3635
3636 dl = btrfs_file_extent_disk_num_bytes(src,
3637 extent);
3638 cs = btrfs_file_extent_offset(src, extent);
3639 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003640 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003641 if (btrfs_file_extent_compression(src,
3642 extent)) {
3643 cs = 0;
3644 cl = dl;
3645 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003646
3647 ret = btrfs_lookup_csums_range(
3648 log->fs_info->csum_root,
3649 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003650 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003651 if (ret) {
3652 btrfs_release_path(dst_path);
3653 kfree(ins_data);
3654 return ret;
3655 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003656 }
3657 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003658 }
3659
3660 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003661 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003662 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003663
3664 /*
3665 * we have to do this after the loop above to avoid changing the
3666 * log tree while trying to change the log tree.
3667 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003668 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003669 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003670 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3671 struct btrfs_ordered_sum,
3672 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003673 if (!ret)
3674 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003675 list_del(&sums->list);
3676 kfree(sums);
3677 }
Josef Bacik16e75492013-10-22 12:18:51 -04003678
3679 if (!has_extents)
3680 return ret;
3681
Filipe Manana74121f72014-08-07 12:00:44 +01003682 if (need_find_last_extent && *last_extent == first_key.offset) {
3683 /*
3684 * We don't have any leafs between our current one and the one
3685 * we processed before that can have file extent items for our
3686 * inode (and have a generation number smaller than our current
3687 * transaction id).
3688 */
3689 need_find_last_extent = false;
3690 }
3691
Josef Bacik16e75492013-10-22 12:18:51 -04003692 /*
3693 * Because we use btrfs_search_forward we could skip leaves that were
3694 * not modified and then assume *last_extent is valid when it really
3695 * isn't. So back up to the previous leaf and read the end of the last
3696 * extent before we go and fill in holes.
3697 */
3698 if (need_find_last_extent) {
3699 u64 len;
3700
3701 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3702 if (ret < 0)
3703 return ret;
3704 if (ret)
3705 goto fill_holes;
3706 if (src_path->slots[0])
3707 src_path->slots[0]--;
3708 src = src_path->nodes[0];
3709 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3710 if (key.objectid != btrfs_ino(inode) ||
3711 key.type != BTRFS_EXTENT_DATA_KEY)
3712 goto fill_holes;
3713 extent = btrfs_item_ptr(src, src_path->slots[0],
3714 struct btrfs_file_extent_item);
3715 if (btrfs_file_extent_type(src, extent) ==
3716 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003717 len = btrfs_file_extent_inline_len(src,
3718 src_path->slots[0],
3719 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003720 *last_extent = ALIGN(key.offset + len,
3721 log->sectorsize);
3722 } else {
3723 len = btrfs_file_extent_num_bytes(src, extent);
3724 *last_extent = key.offset + len;
3725 }
3726 }
3727fill_holes:
3728 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3729 * things could have happened
3730 *
3731 * 1) A merge could have happened, so we could currently be on a leaf
3732 * that holds what we were copying in the first place.
3733 * 2) A split could have happened, and now not all of the items we want
3734 * are on the same leaf.
3735 *
3736 * So we need to adjust how we search for holes, we need to drop the
3737 * path and re-search for the first extent key we found, and then walk
3738 * forward until we hit the last one we copied.
3739 */
3740 if (need_find_last_extent) {
3741 /* btrfs_prev_leaf could return 1 without releasing the path */
3742 btrfs_release_path(src_path);
3743 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3744 src_path, 0, 0);
3745 if (ret < 0)
3746 return ret;
3747 ASSERT(ret == 0);
3748 src = src_path->nodes[0];
3749 i = src_path->slots[0];
3750 } else {
3751 i = start_slot;
3752 }
3753
3754 /*
3755 * Ok so here we need to go through and fill in any holes we may have
3756 * to make sure that holes are punched for those areas in case they had
3757 * extents previously.
3758 */
3759 while (!done) {
3760 u64 offset, len;
3761 u64 extent_end;
3762
3763 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3764 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3765 if (ret < 0)
3766 return ret;
3767 ASSERT(ret == 0);
3768 src = src_path->nodes[0];
3769 i = 0;
3770 }
3771
3772 btrfs_item_key_to_cpu(src, &key, i);
3773 if (!btrfs_comp_cpu_keys(&key, &last_key))
3774 done = true;
3775 if (key.objectid != btrfs_ino(inode) ||
3776 key.type != BTRFS_EXTENT_DATA_KEY) {
3777 i++;
3778 continue;
3779 }
3780 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3781 if (btrfs_file_extent_type(src, extent) ==
3782 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003783 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003784 extent_end = ALIGN(key.offset + len, log->sectorsize);
3785 } else {
3786 len = btrfs_file_extent_num_bytes(src, extent);
3787 extent_end = key.offset + len;
3788 }
3789 i++;
3790
3791 if (*last_extent == key.offset) {
3792 *last_extent = extent_end;
3793 continue;
3794 }
3795 offset = *last_extent;
3796 len = key.offset - *last_extent;
3797 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3798 offset, 0, 0, len, 0, len, 0,
3799 0, 0);
3800 if (ret)
3801 break;
Filipe Manana74121f72014-08-07 12:00:44 +01003802 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003803 }
3804 /*
3805 * Need to let the callers know we dropped the path so they should
3806 * re-search.
3807 */
3808 if (!ret && need_find_last_extent)
3809 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003810 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003811}
3812
Josef Bacik5dc562c2012-08-17 13:14:17 -04003813static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3814{
3815 struct extent_map *em1, *em2;
3816
3817 em1 = list_entry(a, struct extent_map, list);
3818 em2 = list_entry(b, struct extent_map, list);
3819
3820 if (em1->start < em2->start)
3821 return -1;
3822 else if (em1->start > em2->start)
3823 return 1;
3824 return 0;
3825}
3826
Filipe Manana8407f552014-09-05 15:14:39 +01003827static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3828 struct inode *inode,
3829 struct btrfs_root *root,
3830 const struct extent_map *em,
3831 const struct list_head *logged_list,
3832 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003833{
Josef Bacik2ab28f32012-10-12 15:27:49 -04003834 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01003835 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003836 u64 mod_start = em->mod_start;
3837 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003838 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003839 u64 csum_offset;
3840 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003841 LIST_HEAD(ordered_sums);
3842 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003843
Filipe Manana8407f552014-09-05 15:14:39 +01003844 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04003845
Filipe Manana8407f552014-09-05 15:14:39 +01003846 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3847 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003848 return 0;
3849
Josef Bacik2ab28f32012-10-12 15:27:49 -04003850 /*
Filipe Manana8407f552014-09-05 15:14:39 +01003851 * Wait far any ordered extent that covers our extent map. If it
3852 * finishes without an error, first check and see if our csums are on
3853 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04003854 */
Miao Xie827463c2014-01-14 20:31:51 +08003855 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003856 struct btrfs_ordered_sum *sum;
3857
3858 if (!mod_len)
3859 break;
3860
Josef Bacik2ab28f32012-10-12 15:27:49 -04003861 if (ordered->file_offset + ordered->len <= mod_start ||
3862 mod_start + mod_len <= ordered->file_offset)
3863 continue;
3864
Filipe Manana8407f552014-09-05 15:14:39 +01003865 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3866 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3867 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3868 const u64 start = ordered->file_offset;
3869 const u64 end = ordered->file_offset + ordered->len - 1;
3870
3871 WARN_ON(ordered->inode != inode);
3872 filemap_fdatawrite_range(inode->i_mapping, start, end);
3873 }
3874
3875 wait_event(ordered->wait,
3876 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3877 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3878
3879 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00003880 /*
3881 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3882 * i_mapping flags, so that the next fsync won't get
3883 * an outdated io error too.
3884 */
3885 btrfs_inode_check_errors(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01003886 *ordered_io_error = true;
3887 break;
3888 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003889 /*
3890 * We are going to copy all the csums on this ordered extent, so
3891 * go ahead and adjust mod_start and mod_len in case this
3892 * ordered extent has already been logged.
3893 */
3894 if (ordered->file_offset > mod_start) {
3895 if (ordered->file_offset + ordered->len >=
3896 mod_start + mod_len)
3897 mod_len = ordered->file_offset - mod_start;
3898 /*
3899 * If we have this case
3900 *
3901 * |--------- logged extent ---------|
3902 * |----- ordered extent ----|
3903 *
3904 * Just don't mess with mod_start and mod_len, we'll
3905 * just end up logging more csums than we need and it
3906 * will be ok.
3907 */
3908 } else {
3909 if (ordered->file_offset + ordered->len <
3910 mod_start + mod_len) {
3911 mod_len = (mod_start + mod_len) -
3912 (ordered->file_offset + ordered->len);
3913 mod_start = ordered->file_offset +
3914 ordered->len;
3915 } else {
3916 mod_len = 0;
3917 }
3918 }
3919
Filipe Manana8407f552014-09-05 15:14:39 +01003920 if (skip_csum)
3921 continue;
3922
Josef Bacik2ab28f32012-10-12 15:27:49 -04003923 /*
3924 * To keep us from looping for the above case of an ordered
3925 * extent that falls inside of the logged extent.
3926 */
3927 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3928 &ordered->flags))
3929 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003930
Josef Bacik2ab28f32012-10-12 15:27:49 -04003931 list_for_each_entry(sum, &ordered->list, list) {
3932 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08003933 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01003934 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003935 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003936 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003937
Filipe Manana8407f552014-09-05 15:14:39 +01003938 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04003939 return ret;
3940
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00003941 if (em->compress_type) {
3942 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01003943 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00003944 } else {
3945 csum_offset = mod_start - em->start;
3946 csum_len = mod_len;
3947 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003948
Josef Bacik70c8a912012-10-11 16:54:30 -04003949 /* block start is already adjusted for the file extent offset. */
3950 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3951 em->block_start + csum_offset,
3952 em->block_start + csum_offset +
3953 csum_len - 1, &ordered_sums, 0);
3954 if (ret)
3955 return ret;
3956
3957 while (!list_empty(&ordered_sums)) {
3958 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3959 struct btrfs_ordered_sum,
3960 list);
3961 if (!ret)
3962 ret = btrfs_csum_file_blocks(trans, log, sums);
3963 list_del(&sums->list);
3964 kfree(sums);
3965 }
3966
3967 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003968}
3969
Filipe Manana8407f552014-09-05 15:14:39 +01003970static int log_one_extent(struct btrfs_trans_handle *trans,
3971 struct inode *inode, struct btrfs_root *root,
3972 const struct extent_map *em,
3973 struct btrfs_path *path,
3974 const struct list_head *logged_list,
3975 struct btrfs_log_ctx *ctx)
3976{
3977 struct btrfs_root *log = root->log_root;
3978 struct btrfs_file_extent_item *fi;
3979 struct extent_buffer *leaf;
3980 struct btrfs_map_token token;
3981 struct btrfs_key key;
3982 u64 extent_offset = em->start - em->orig_start;
3983 u64 block_len;
3984 int ret;
3985 int extent_inserted = 0;
3986 bool ordered_io_err = false;
3987
3988 ret = wait_ordered_extents(trans, inode, root, em, logged_list,
3989 &ordered_io_err);
3990 if (ret)
3991 return ret;
3992
3993 if (ordered_io_err) {
3994 ctx->io_err = -EIO;
3995 return 0;
3996 }
3997
3998 btrfs_init_map_token(&token);
3999
4000 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
4001 em->start + em->len, NULL, 0, 1,
4002 sizeof(*fi), &extent_inserted);
4003 if (ret)
4004 return ret;
4005
4006 if (!extent_inserted) {
4007 key.objectid = btrfs_ino(inode);
4008 key.type = BTRFS_EXTENT_DATA_KEY;
4009 key.offset = em->start;
4010
4011 ret = btrfs_insert_empty_item(trans, log, path, &key,
4012 sizeof(*fi));
4013 if (ret)
4014 return ret;
4015 }
4016 leaf = path->nodes[0];
4017 fi = btrfs_item_ptr(leaf, path->slots[0],
4018 struct btrfs_file_extent_item);
4019
Josef Bacik50d9aa92014-11-21 14:52:38 -05004020 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01004021 &token);
4022 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4023 btrfs_set_token_file_extent_type(leaf, fi,
4024 BTRFS_FILE_EXTENT_PREALLOC,
4025 &token);
4026 else
4027 btrfs_set_token_file_extent_type(leaf, fi,
4028 BTRFS_FILE_EXTENT_REG,
4029 &token);
4030
4031 block_len = max(em->block_len, em->orig_block_len);
4032 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4033 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4034 em->block_start,
4035 &token);
4036 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4037 &token);
4038 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4039 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4040 em->block_start -
4041 extent_offset, &token);
4042 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4043 &token);
4044 } else {
4045 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4046 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4047 &token);
4048 }
4049
4050 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4051 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4052 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4053 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4054 &token);
4055 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4056 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4057 btrfs_mark_buffer_dirty(leaf);
4058
4059 btrfs_release_path(path);
4060
4061 return ret;
4062}
4063
Josef Bacik5dc562c2012-08-17 13:14:17 -04004064static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4065 struct btrfs_root *root,
4066 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08004067 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01004068 struct list_head *logged_list,
4069 struct btrfs_log_ctx *ctx)
Josef Bacik5dc562c2012-08-17 13:14:17 -04004070{
Josef Bacik5dc562c2012-08-17 13:14:17 -04004071 struct extent_map *em, *n;
4072 struct list_head extents;
4073 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4074 u64 test_gen;
4075 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04004076 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004077
4078 INIT_LIST_HEAD(&extents);
4079
Josef Bacik5dc562c2012-08-17 13:14:17 -04004080 write_lock(&tree->lock);
4081 test_gen = root->fs_info->last_trans_committed;
4082
4083 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4084 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004085
4086 /*
4087 * Just an arbitrary number, this can be really CPU intensive
4088 * once we start getting a lot of extents, and really once we
4089 * have a bunch of extents we just want to commit since it will
4090 * be faster.
4091 */
4092 if (++num > 32768) {
4093 list_del_init(&tree->modified_extents);
4094 ret = -EFBIG;
4095 goto process;
4096 }
4097
Josef Bacik5dc562c2012-08-17 13:14:17 -04004098 if (em->generation <= test_gen)
4099 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004100 /* Need a ref to keep it from getting evicted from cache */
4101 atomic_inc(&em->refs);
4102 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004103 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004104 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004105 }
4106
4107 list_sort(NULL, &extents, extent_cmp);
4108
Josef Bacik2ab28f32012-10-12 15:27:49 -04004109process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04004110 while (!list_empty(&extents)) {
4111 em = list_entry(extents.next, struct extent_map, list);
4112
4113 list_del_init(&em->list);
4114
4115 /*
4116 * If we had an error we just need to delete everybody from our
4117 * private list.
4118 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04004119 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05004120 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004121 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004122 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04004123 }
4124
4125 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004126
Filipe Manana8407f552014-09-05 15:14:39 +01004127 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4128 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04004129 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05004130 clear_em_logging(tree, em);
4131 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004132 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04004133 WARN_ON(!list_empty(&extents));
4134 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004135
Josef Bacik5dc562c2012-08-17 13:14:17 -04004136 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004137 return ret;
4138}
4139
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004140static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
4141 struct btrfs_path *path, u64 *size_ret)
4142{
4143 struct btrfs_key key;
4144 int ret;
4145
4146 key.objectid = btrfs_ino(inode);
4147 key.type = BTRFS_INODE_ITEM_KEY;
4148 key.offset = 0;
4149
4150 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4151 if (ret < 0) {
4152 return ret;
4153 } else if (ret > 0) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004154 *size_ret = 0;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004155 } else {
4156 struct btrfs_inode_item *item;
4157
4158 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4159 struct btrfs_inode_item);
4160 *size_ret = btrfs_inode_size(path->nodes[0], item);
4161 }
4162
4163 btrfs_release_path(path);
4164 return 0;
4165}
4166
Filipe Manana36283bf2015-06-20 00:44:51 +01004167/*
4168 * At the moment we always log all xattrs. This is to figure out at log replay
4169 * time which xattrs must have their deletion replayed. If a xattr is missing
4170 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4171 * because if a xattr is deleted, the inode is fsynced and a power failure
4172 * happens, causing the log to be replayed the next time the fs is mounted,
4173 * we want the xattr to not exist anymore (same behaviour as other filesystems
4174 * with a journal, ext3/4, xfs, f2fs, etc).
4175 */
4176static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4177 struct btrfs_root *root,
4178 struct inode *inode,
4179 struct btrfs_path *path,
4180 struct btrfs_path *dst_path)
4181{
4182 int ret;
4183 struct btrfs_key key;
4184 const u64 ino = btrfs_ino(inode);
4185 int ins_nr = 0;
4186 int start_slot = 0;
4187
4188 key.objectid = ino;
4189 key.type = BTRFS_XATTR_ITEM_KEY;
4190 key.offset = 0;
4191
4192 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4193 if (ret < 0)
4194 return ret;
4195
4196 while (true) {
4197 int slot = path->slots[0];
4198 struct extent_buffer *leaf = path->nodes[0];
4199 int nritems = btrfs_header_nritems(leaf);
4200
4201 if (slot >= nritems) {
4202 if (ins_nr > 0) {
4203 u64 last_extent = 0;
4204
4205 ret = copy_items(trans, inode, dst_path, path,
4206 &last_extent, start_slot,
4207 ins_nr, 1, 0);
4208 /* can't be 1, extent items aren't processed */
4209 ASSERT(ret <= 0);
4210 if (ret < 0)
4211 return ret;
4212 ins_nr = 0;
4213 }
4214 ret = btrfs_next_leaf(root, path);
4215 if (ret < 0)
4216 return ret;
4217 else if (ret > 0)
4218 break;
4219 continue;
4220 }
4221
4222 btrfs_item_key_to_cpu(leaf, &key, slot);
4223 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4224 break;
4225
4226 if (ins_nr == 0)
4227 start_slot = slot;
4228 ins_nr++;
4229 path->slots[0]++;
4230 cond_resched();
4231 }
4232 if (ins_nr > 0) {
4233 u64 last_extent = 0;
4234
4235 ret = copy_items(trans, inode, dst_path, path,
4236 &last_extent, start_slot,
4237 ins_nr, 1, 0);
4238 /* can't be 1, extent items aren't processed */
4239 ASSERT(ret <= 0);
4240 if (ret < 0)
4241 return ret;
4242 }
4243
4244 return 0;
4245}
4246
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004247/*
4248 * If the no holes feature is enabled we need to make sure any hole between the
4249 * last extent and the i_size of our inode is explicitly marked in the log. This
4250 * is to make sure that doing something like:
4251 *
4252 * 1) create file with 128Kb of data
4253 * 2) truncate file to 64Kb
4254 * 3) truncate file to 256Kb
4255 * 4) fsync file
4256 * 5) <crash/power failure>
4257 * 6) mount fs and trigger log replay
4258 *
4259 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4260 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4261 * file correspond to a hole. The presence of explicit holes in a log tree is
4262 * what guarantees that log replay will remove/adjust file extent items in the
4263 * fs/subvol tree.
4264 *
4265 * Here we do not need to care about holes between extents, that is already done
4266 * by copy_items(). We also only need to do this in the full sync path, where we
4267 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4268 * lookup the list of modified extent maps and if any represents a hole, we
4269 * insert a corresponding extent representing a hole in the log tree.
4270 */
4271static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4272 struct btrfs_root *root,
4273 struct inode *inode,
4274 struct btrfs_path *path)
4275{
4276 int ret;
4277 struct btrfs_key key;
4278 u64 hole_start;
4279 u64 hole_size;
4280 struct extent_buffer *leaf;
4281 struct btrfs_root *log = root->log_root;
4282 const u64 ino = btrfs_ino(inode);
4283 const u64 i_size = i_size_read(inode);
4284
4285 if (!btrfs_fs_incompat(root->fs_info, NO_HOLES))
4286 return 0;
4287
4288 key.objectid = ino;
4289 key.type = BTRFS_EXTENT_DATA_KEY;
4290 key.offset = (u64)-1;
4291
4292 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4293 ASSERT(ret != 0);
4294 if (ret < 0)
4295 return ret;
4296
4297 ASSERT(path->slots[0] > 0);
4298 path->slots[0]--;
4299 leaf = path->nodes[0];
4300 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4301
4302 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4303 /* inode does not have any extents */
4304 hole_start = 0;
4305 hole_size = i_size;
4306 } else {
4307 struct btrfs_file_extent_item *extent;
4308 u64 len;
4309
4310 /*
4311 * If there's an extent beyond i_size, an explicit hole was
4312 * already inserted by copy_items().
4313 */
4314 if (key.offset >= i_size)
4315 return 0;
4316
4317 extent = btrfs_item_ptr(leaf, path->slots[0],
4318 struct btrfs_file_extent_item);
4319
4320 if (btrfs_file_extent_type(leaf, extent) ==
4321 BTRFS_FILE_EXTENT_INLINE) {
4322 len = btrfs_file_extent_inline_len(leaf,
4323 path->slots[0],
4324 extent);
4325 ASSERT(len == i_size);
4326 return 0;
4327 }
4328
4329 len = btrfs_file_extent_num_bytes(leaf, extent);
4330 /* Last extent goes beyond i_size, no need to log a hole. */
4331 if (key.offset + len > i_size)
4332 return 0;
4333 hole_start = key.offset + len;
4334 hole_size = i_size - hole_start;
4335 }
4336 btrfs_release_path(path);
4337
4338 /* Last extent ends at i_size. */
4339 if (hole_size == 0)
4340 return 0;
4341
4342 hole_size = ALIGN(hole_size, root->sectorsize);
4343 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4344 hole_size, 0, hole_size, 0, 0, 0);
4345 return ret;
4346}
4347
Chris Masone02119d2008-09-05 16:13:11 -04004348/* log a single inode in the tree log.
4349 * At least one parent directory for this inode must exist in the tree
4350 * or be logged already.
4351 *
4352 * Any items from this inode changed by the current transaction are copied
4353 * to the log tree. An extra reference is taken on any extents in this
4354 * file, allowing us to avoid a whole pile of corner cases around logging
4355 * blocks that have been removed from the tree.
4356 *
4357 * See LOG_INODE_ALL and related defines for a description of what inode_only
4358 * does.
4359 *
4360 * This handles both files and directories.
4361 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004362static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004363 struct btrfs_root *root, struct inode *inode,
4364 int inode_only,
4365 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01004366 const loff_t end,
4367 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004368{
4369 struct btrfs_path *path;
4370 struct btrfs_path *dst_path;
4371 struct btrfs_key min_key;
4372 struct btrfs_key max_key;
4373 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004374 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08004375 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04004376 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004377 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004378 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004379 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004380 int ins_start_slot = 0;
4381 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004382 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08004383 u64 ino = btrfs_ino(inode);
Filipe Manana49dae1b2014-09-06 22:34:39 +01004384 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004385 u64 logged_isize = 0;
Filipe Mananae4545de2015-06-17 12:49:23 +01004386 bool need_log_inode_item = true;
Chris Masone02119d2008-09-05 16:13:11 -04004387
Chris Masone02119d2008-09-05 16:13:11 -04004388 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004389 if (!path)
4390 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04004391 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00004392 if (!dst_path) {
4393 btrfs_free_path(path);
4394 return -ENOMEM;
4395 }
Chris Masone02119d2008-09-05 16:13:11 -04004396
Li Zefan33345d012011-04-20 10:31:50 +08004397 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04004398 min_key.type = BTRFS_INODE_ITEM_KEY;
4399 min_key.offset = 0;
4400
Li Zefan33345d012011-04-20 10:31:50 +08004401 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04004402
Chris Mason12fcfd22009-03-24 10:24:20 -04004403
Josef Bacik5dc562c2012-08-17 13:14:17 -04004404 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00004405 if (S_ISDIR(inode->i_mode) ||
4406 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4407 &BTRFS_I(inode)->runtime_flags) &&
4408 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04004409 max_key.type = BTRFS_XATTR_ITEM_KEY;
4410 else
4411 max_key.type = (u8)-1;
4412 max_key.offset = (u64)-1;
4413
Filipe Manana2c2c4522015-01-13 16:40:04 +00004414 /*
4415 * Only run delayed items if we are a dir or a new file.
4416 * Otherwise commit the delayed inode only, which is needed in
4417 * order for the log replay code to mark inodes for link count
4418 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4419 */
Josef Bacik94edf4a2012-09-25 14:56:25 -04004420 if (S_ISDIR(inode->i_mode) ||
Filipe Manana2c2c4522015-01-13 16:40:04 +00004421 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
Josef Bacik94edf4a2012-09-25 14:56:25 -04004422 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004423 else
4424 ret = btrfs_commit_inode_delayed_inode(inode);
4425
4426 if (ret) {
4427 btrfs_free_path(path);
4428 btrfs_free_path(dst_path);
4429 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004430 }
4431
Chris Masone02119d2008-09-05 16:13:11 -04004432 mutex_lock(&BTRFS_I(inode)->log_mutex);
4433
Filipe Manana08702952014-11-13 17:00:35 +00004434 btrfs_get_logged_extents(inode, &logged_list, start, end);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004435
Chris Masone02119d2008-09-05 16:13:11 -04004436 /*
4437 * a brute force approach to making sure we get the most uptodate
4438 * copies of everything.
4439 */
4440 if (S_ISDIR(inode->i_mode)) {
4441 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4442
Filipe Manana4f764e52015-02-23 19:53:35 +00004443 if (inode_only == LOG_INODE_EXISTS)
4444 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004445 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004446 } else {
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004447 if (inode_only == LOG_INODE_EXISTS) {
4448 /*
4449 * Make sure the new inode item we write to the log has
4450 * the same isize as the current one (if it exists).
4451 * This is necessary to prevent data loss after log
4452 * replay, and also to prevent doing a wrong expanding
4453 * truncate - for e.g. create file, write 4K into offset
4454 * 0, fsync, write 4K into offset 4096, add hard link,
4455 * fsync some other file (to sync log), power fail - if
4456 * we use the inode's current i_size, after log replay
4457 * we get a 8Kb file, with the last 4Kb extent as a hole
4458 * (zeroes), as if an expanding truncate happened,
4459 * instead of getting a file of 4Kb only.
4460 */
4461 err = logged_inode_size(log, inode, path,
4462 &logged_isize);
4463 if (err)
4464 goto out_unlock;
4465 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004466 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4467 &BTRFS_I(inode)->runtime_flags)) {
4468 if (inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004469 max_key.type = BTRFS_XATTR_ITEM_KEY;
Filipe Mananaa7429942015-02-13 16:56:14 +00004470 ret = drop_objectid_items(trans, log, path, ino,
4471 max_key.type);
4472 } else {
4473 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4474 &BTRFS_I(inode)->runtime_flags);
4475 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4476 &BTRFS_I(inode)->runtime_flags);
Chris Mason28ed1342014-12-17 09:41:04 -08004477 while(1) {
4478 ret = btrfs_truncate_inode_items(trans,
4479 log, inode, 0, 0);
4480 if (ret != -EAGAIN)
4481 break;
4482 }
Filipe Mananaa7429942015-02-13 16:56:14 +00004483 }
Filipe Manana4f764e52015-02-23 19:53:35 +00004484 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4485 &BTRFS_I(inode)->runtime_flags) ||
Josef Bacik6cfab852013-11-12 16:25:58 -05004486 inode_only == LOG_INODE_EXISTS) {
Filipe Manana4f764e52015-02-23 19:53:35 +00004487 if (inode_only == LOG_INODE_ALL)
Josef Bacika95249b2012-10-11 16:17:34 -04004488 fast_search = true;
Filipe Manana4f764e52015-02-23 19:53:35 +00004489 max_key.type = BTRFS_XATTR_ITEM_KEY;
Josef Bacika95249b2012-10-11 16:17:34 -04004490 ret = drop_objectid_items(trans, log, path, ino,
4491 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004492 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004493 if (inode_only == LOG_INODE_ALL)
4494 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004495 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004496 }
Josef Bacika95249b2012-10-11 16:17:34 -04004497
Chris Masone02119d2008-09-05 16:13:11 -04004498 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004499 if (ret) {
4500 err = ret;
4501 goto out_unlock;
4502 }
Chris Masone02119d2008-09-05 16:13:11 -04004503
Chris Masond3977122009-01-05 21:25:51 -05004504 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004505 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004506 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004507 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04004508 if (ret != 0)
4509 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004510again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004511 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004512 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004513 break;
4514 if (min_key.type > max_key.type)
4515 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004516
Filipe Mananae4545de2015-06-17 12:49:23 +01004517 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4518 need_log_inode_item = false;
4519
Filipe Manana36283bf2015-06-20 00:44:51 +01004520 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
4521 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
4522 if (ins_nr == 0)
4523 goto next_slot;
4524 ret = copy_items(trans, inode, dst_path, path,
4525 &last_extent, ins_start_slot,
4526 ins_nr, inode_only, logged_isize);
4527 if (ret < 0) {
4528 err = ret;
4529 goto out_unlock;
4530 }
4531 ins_nr = 0;
4532 if (ret) {
4533 btrfs_release_path(path);
4534 continue;
4535 }
4536 goto next_slot;
4537 }
4538
Chris Masone02119d2008-09-05 16:13:11 -04004539 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004540 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4541 ins_nr++;
4542 goto next_slot;
4543 } else if (!ins_nr) {
4544 ins_start_slot = path->slots[0];
4545 ins_nr = 1;
4546 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004547 }
4548
Josef Bacik16e75492013-10-22 12:18:51 -04004549 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004550 ins_start_slot, ins_nr, inode_only,
4551 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004552 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004553 err = ret;
4554 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02004555 }
4556 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04004557 ins_nr = 0;
4558 btrfs_release_path(path);
4559 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004560 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004561 ins_nr = 1;
4562 ins_start_slot = path->slots[0];
4563next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004564
Chris Mason3a5f1d42008-09-11 15:53:37 -04004565 nritems = btrfs_header_nritems(path->nodes[0]);
4566 path->slots[0]++;
4567 if (path->slots[0] < nritems) {
4568 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4569 path->slots[0]);
4570 goto again;
4571 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004572 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004573 ret = copy_items(trans, inode, dst_path, path,
4574 &last_extent, ins_start_slot,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004575 ins_nr, inode_only, logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004576 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004577 err = ret;
4578 goto out_unlock;
4579 }
Josef Bacik16e75492013-10-22 12:18:51 -04004580 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004581 ins_nr = 0;
4582 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004583 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04004584
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004585 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004586 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004587 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004588 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004589 min_key.offset = 0;
4590 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004591 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004592 }
Chris Masone02119d2008-09-05 16:13:11 -04004593 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004594 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004595 ret = copy_items(trans, inode, dst_path, path, &last_extent,
Filipe Manana1a4bcf42015-02-13 12:30:56 +00004596 ins_start_slot, ins_nr, inode_only,
4597 logged_isize);
Josef Bacik16e75492013-10-22 12:18:51 -04004598 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004599 err = ret;
4600 goto out_unlock;
4601 }
Josef Bacik16e75492013-10-22 12:18:51 -04004602 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004603 ins_nr = 0;
4604 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004605
Filipe Manana36283bf2015-06-20 00:44:51 +01004606 btrfs_release_path(path);
4607 btrfs_release_path(dst_path);
4608 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
4609 if (err)
4610 goto out_unlock;
Filipe Mananaa89ca6f2015-06-25 04:17:46 +01004611 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
4612 btrfs_release_path(path);
4613 btrfs_release_path(dst_path);
4614 err = btrfs_log_trailing_hole(trans, root, inode, path);
4615 if (err)
4616 goto out_unlock;
4617 }
Josef Bacika95249b2012-10-11 16:17:34 -04004618log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004619 btrfs_release_path(path);
4620 btrfs_release_path(dst_path);
Filipe Mananae4545de2015-06-17 12:49:23 +01004621 if (need_log_inode_item) {
4622 err = log_inode_item(trans, log, dst_path, inode);
4623 if (err)
4624 goto out_unlock;
4625 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004626 if (fast_search) {
Filipe Mananab38ef712014-11-13 17:01:45 +00004627 /*
4628 * Some ordered extents started by fsync might have completed
4629 * before we collected the ordered extents in logged_list, which
4630 * means they're gone, not in our logged_list nor in the inode's
4631 * ordered tree. We want the application/user space to know an
4632 * error happened while attempting to persist file data so that
4633 * it can take proper action. If such error happened, we leave
4634 * without writing to the log tree and the fsync must report the
4635 * file data write error and not commit the current transaction.
4636 */
4637 err = btrfs_inode_check_errors(inode);
4638 if (err) {
4639 ctx->io_err = err;
4640 goto out_unlock;
4641 }
Miao Xie827463c2014-01-14 20:31:51 +08004642 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Manana8407f552014-09-05 15:14:39 +01004643 &logged_list, ctx);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004644 if (ret) {
4645 err = ret;
4646 goto out_unlock;
4647 }
Josef Bacikd006a042013-11-12 20:54:09 -05004648 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004649 struct extent_map *em, *n;
4650
Filipe Manana49dae1b2014-09-06 22:34:39 +01004651 write_lock(&em_tree->lock);
4652 /*
4653 * We can't just remove every em if we're called for a ranged
4654 * fsync - that is, one that doesn't cover the whole possible
4655 * file range (0 to LLONG_MAX). This is because we can have
4656 * em's that fall outside the range we're logging and therefore
4657 * their ordered operations haven't completed yet
4658 * (btrfs_finish_ordered_io() not invoked yet). This means we
4659 * didn't get their respective file extent item in the fs/subvol
4660 * tree yet, and need to let the next fast fsync (one which
4661 * consults the list of modified extent maps) find the em so
4662 * that it logs a matching file extent item and waits for the
4663 * respective ordered operation to complete (if it's still
4664 * running).
4665 *
4666 * Removing every em outside the range we're logging would make
4667 * the next fast fsync not log their matching file extent items,
4668 * therefore making us lose data after a log replay.
4669 */
4670 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4671 list) {
4672 const u64 mod_end = em->mod_start + em->mod_len - 1;
4673
4674 if (em->mod_start >= start && mod_end <= end)
4675 list_del_init(&em->list);
4676 }
4677 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004678 }
4679
Chris Mason9623f9a2008-09-11 17:42:42 -04004680 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004681 ret = log_directory_changes(trans, root, inode, path, dst_path,
4682 ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004683 if (ret) {
4684 err = ret;
4685 goto out_unlock;
4686 }
Chris Masone02119d2008-09-05 16:13:11 -04004687 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01004688
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004689 spin_lock(&BTRFS_I(inode)->lock);
Filipe Manana125c4cf92014-09-11 21:22:14 +01004690 BTRFS_I(inode)->logged_trans = trans->transid;
4691 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004692 spin_unlock(&BTRFS_I(inode)->lock);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004693out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08004694 if (unlikely(err))
4695 btrfs_put_logged_extents(&logged_list);
4696 else
4697 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04004698 mutex_unlock(&BTRFS_I(inode)->log_mutex);
4699
4700 btrfs_free_path(path);
4701 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004702 return err;
Chris Masone02119d2008-09-05 16:13:11 -04004703}
4704
Chris Mason12fcfd22009-03-24 10:24:20 -04004705/*
4706 * follow the dentry parent pointers up the chain and see if any
4707 * of the directories in it require a full commit before they can
4708 * be logged. Returns zero if nothing special needs to be done or 1 if
4709 * a full commit is required.
4710 */
4711static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
4712 struct inode *inode,
4713 struct dentry *parent,
4714 struct super_block *sb,
4715 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04004716{
Chris Mason12fcfd22009-03-24 10:24:20 -04004717 int ret = 0;
4718 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00004719 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04004720 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04004721
Chris Masonaf4176b2009-03-24 10:24:31 -04004722 /*
4723 * for regular files, if its inode is already on disk, we don't
4724 * have to worry about the parents at all. This is because
4725 * we can use the last_unlink_trans field to record renames
4726 * and other fun in this file.
4727 */
4728 if (S_ISREG(inode->i_mode) &&
4729 BTRFS_I(inode)->generation <= last_committed &&
4730 BTRFS_I(inode)->last_unlink_trans <= last_committed)
4731 goto out;
4732
Chris Mason12fcfd22009-03-24 10:24:20 -04004733 if (!S_ISDIR(inode->i_mode)) {
David Howells2b0143b2015-03-17 22:25:59 +00004734 if (!parent || d_really_is_negative(parent) || sb != d_inode(parent)->i_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04004735 goto out;
David Howells2b0143b2015-03-17 22:25:59 +00004736 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04004737 }
4738
4739 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04004740 /*
4741 * If we are logging a directory then we start with our inode,
4742 * not our parents inode, so we need to skipp setting the
4743 * logged_trans so that further down in the log code we don't
4744 * think this inode has already been logged.
4745 */
4746 if (inode != orig_inode)
4747 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04004748 smp_mb();
4749
4750 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
4751 root = BTRFS_I(inode)->root;
4752
4753 /*
4754 * make sure any commits to the log are forced
4755 * to be full commits
4756 */
Miao Xie995946d2014-04-02 19:51:06 +08004757 btrfs_set_log_full_commit(root->fs_info, trans);
Chris Mason12fcfd22009-03-24 10:24:20 -04004758 ret = 1;
4759 break;
4760 }
4761
David Howells2b0143b2015-03-17 22:25:59 +00004762 if (!parent || d_really_is_negative(parent) || sb != d_inode(parent)->i_sb)
Chris Mason12fcfd22009-03-24 10:24:20 -04004763 break;
4764
Yan, Zheng76dda932009-09-21 16:00:26 -04004765 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04004766 break;
4767
Josef Bacik6a912212010-11-20 09:48:00 +00004768 parent = dget_parent(parent);
4769 dput(old_parent);
4770 old_parent = parent;
David Howells2b0143b2015-03-17 22:25:59 +00004771 inode = d_inode(parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04004772
4773 }
Josef Bacik6a912212010-11-20 09:48:00 +00004774 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04004775out:
Chris Masone02119d2008-09-05 16:13:11 -04004776 return ret;
4777}
4778
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00004779struct btrfs_dir_list {
4780 u64 ino;
4781 struct list_head list;
4782};
4783
4784/*
4785 * Log the inodes of the new dentries of a directory. See log_dir_items() for
4786 * details about the why it is needed.
4787 * This is a recursive operation - if an existing dentry corresponds to a
4788 * directory, that directory's new entries are logged too (same behaviour as
4789 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
4790 * the dentries point to we do not lock their i_mutex, otherwise lockdep
4791 * complains about the following circular lock dependency / possible deadlock:
4792 *
4793 * CPU0 CPU1
4794 * ---- ----
4795 * lock(&type->i_mutex_dir_key#3/2);
4796 * lock(sb_internal#2);
4797 * lock(&type->i_mutex_dir_key#3/2);
4798 * lock(&sb->s_type->i_mutex_key#14);
4799 *
4800 * Where sb_internal is the lock (a counter that works as a lock) acquired by
4801 * sb_start_intwrite() in btrfs_start_transaction().
4802 * Not locking i_mutex of the inodes is still safe because:
4803 *
4804 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
4805 * that while logging the inode new references (names) are added or removed
4806 * from the inode, leaving the logged inode item with a link count that does
4807 * not match the number of logged inode reference items. This is fine because
4808 * at log replay time we compute the real number of links and correct the
4809 * link count in the inode item (see replay_one_buffer() and
4810 * link_to_fixup_dir());
4811 *
4812 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
4813 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
4814 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
4815 * has a size that doesn't match the sum of the lengths of all the logged
4816 * names. This does not result in a problem because if a dir_item key is
4817 * logged but its matching dir_index key is not logged, at log replay time we
4818 * don't use it to replay the respective name (see replay_one_name()). On the
4819 * other hand if only the dir_index key ends up being logged, the respective
4820 * name is added to the fs/subvol tree with both the dir_item and dir_index
4821 * keys created (see replay_one_name()).
4822 * The directory's inode item with a wrong i_size is not a problem as well,
4823 * since we don't use it at log replay time to set the i_size in the inode
4824 * item of the fs/subvol tree (see overwrite_item()).
4825 */
4826static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
4827 struct btrfs_root *root,
4828 struct inode *start_inode,
4829 struct btrfs_log_ctx *ctx)
4830{
4831 struct btrfs_root *log = root->log_root;
4832 struct btrfs_path *path;
4833 LIST_HEAD(dir_list);
4834 struct btrfs_dir_list *dir_elem;
4835 int ret = 0;
4836
4837 path = btrfs_alloc_path();
4838 if (!path)
4839 return -ENOMEM;
4840
4841 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
4842 if (!dir_elem) {
4843 btrfs_free_path(path);
4844 return -ENOMEM;
4845 }
4846 dir_elem->ino = btrfs_ino(start_inode);
4847 list_add_tail(&dir_elem->list, &dir_list);
4848
4849 while (!list_empty(&dir_list)) {
4850 struct extent_buffer *leaf;
4851 struct btrfs_key min_key;
4852 int nritems;
4853 int i;
4854
4855 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
4856 list);
4857 if (ret)
4858 goto next_dir_inode;
4859
4860 min_key.objectid = dir_elem->ino;
4861 min_key.type = BTRFS_DIR_ITEM_KEY;
4862 min_key.offset = 0;
4863again:
4864 btrfs_release_path(path);
4865 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
4866 if (ret < 0) {
4867 goto next_dir_inode;
4868 } else if (ret > 0) {
4869 ret = 0;
4870 goto next_dir_inode;
4871 }
4872
4873process_leaf:
4874 leaf = path->nodes[0];
4875 nritems = btrfs_header_nritems(leaf);
4876 for (i = path->slots[0]; i < nritems; i++) {
4877 struct btrfs_dir_item *di;
4878 struct btrfs_key di_key;
4879 struct inode *di_inode;
4880 struct btrfs_dir_list *new_dir_elem;
4881 int log_mode = LOG_INODE_EXISTS;
4882 int type;
4883
4884 btrfs_item_key_to_cpu(leaf, &min_key, i);
4885 if (min_key.objectid != dir_elem->ino ||
4886 min_key.type != BTRFS_DIR_ITEM_KEY)
4887 goto next_dir_inode;
4888
4889 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
4890 type = btrfs_dir_type(leaf, di);
4891 if (btrfs_dir_transid(leaf, di) < trans->transid &&
4892 type != BTRFS_FT_DIR)
4893 continue;
4894 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
4895 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
4896 continue;
4897
4898 di_inode = btrfs_iget(root->fs_info->sb, &di_key,
4899 root, NULL);
4900 if (IS_ERR(di_inode)) {
4901 ret = PTR_ERR(di_inode);
4902 goto next_dir_inode;
4903 }
4904
4905 if (btrfs_inode_in_log(di_inode, trans->transid)) {
4906 iput(di_inode);
4907 continue;
4908 }
4909
4910 ctx->log_new_dentries = false;
4911 if (type == BTRFS_FT_DIR)
4912 log_mode = LOG_INODE_ALL;
4913 btrfs_release_path(path);
4914 ret = btrfs_log_inode(trans, root, di_inode,
4915 log_mode, 0, LLONG_MAX, ctx);
4916 iput(di_inode);
4917 if (ret)
4918 goto next_dir_inode;
4919 if (ctx->log_new_dentries) {
4920 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
4921 GFP_NOFS);
4922 if (!new_dir_elem) {
4923 ret = -ENOMEM;
4924 goto next_dir_inode;
4925 }
4926 new_dir_elem->ino = di_key.objectid;
4927 list_add_tail(&new_dir_elem->list, &dir_list);
4928 }
4929 break;
4930 }
4931 if (i == nritems) {
4932 ret = btrfs_next_leaf(log, path);
4933 if (ret < 0) {
4934 goto next_dir_inode;
4935 } else if (ret > 0) {
4936 ret = 0;
4937 goto next_dir_inode;
4938 }
4939 goto process_leaf;
4940 }
4941 if (min_key.offset < (u64)-1) {
4942 min_key.offset++;
4943 goto again;
4944 }
4945next_dir_inode:
4946 list_del(&dir_elem->list);
4947 kfree(dir_elem);
4948 }
4949
4950 btrfs_free_path(path);
4951 return ret;
4952}
4953
Filipe Manana18aa0922015-08-05 16:49:08 +01004954static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
4955 struct inode *inode,
4956 struct btrfs_log_ctx *ctx)
4957{
4958 int ret;
4959 struct btrfs_path *path;
4960 struct btrfs_key key;
4961 struct btrfs_root *root = BTRFS_I(inode)->root;
4962 const u64 ino = btrfs_ino(inode);
4963
4964 path = btrfs_alloc_path();
4965 if (!path)
4966 return -ENOMEM;
4967 path->skip_locking = 1;
4968 path->search_commit_root = 1;
4969
4970 key.objectid = ino;
4971 key.type = BTRFS_INODE_REF_KEY;
4972 key.offset = 0;
4973 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4974 if (ret < 0)
4975 goto out;
4976
4977 while (true) {
4978 struct extent_buffer *leaf = path->nodes[0];
4979 int slot = path->slots[0];
4980 u32 cur_offset = 0;
4981 u32 item_size;
4982 unsigned long ptr;
4983
4984 if (slot >= btrfs_header_nritems(leaf)) {
4985 ret = btrfs_next_leaf(root, path);
4986 if (ret < 0)
4987 goto out;
4988 else if (ret > 0)
4989 break;
4990 continue;
4991 }
4992
4993 btrfs_item_key_to_cpu(leaf, &key, slot);
4994 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
4995 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
4996 break;
4997
4998 item_size = btrfs_item_size_nr(leaf, slot);
4999 ptr = btrfs_item_ptr_offset(leaf, slot);
5000 while (cur_offset < item_size) {
5001 struct btrfs_key inode_key;
5002 struct inode *dir_inode;
5003
5004 inode_key.type = BTRFS_INODE_ITEM_KEY;
5005 inode_key.offset = 0;
5006
5007 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5008 struct btrfs_inode_extref *extref;
5009
5010 extref = (struct btrfs_inode_extref *)
5011 (ptr + cur_offset);
5012 inode_key.objectid = btrfs_inode_extref_parent(
5013 leaf, extref);
5014 cur_offset += sizeof(*extref);
5015 cur_offset += btrfs_inode_extref_name_len(leaf,
5016 extref);
5017 } else {
5018 inode_key.objectid = key.offset;
5019 cur_offset = item_size;
5020 }
5021
5022 dir_inode = btrfs_iget(root->fs_info->sb, &inode_key,
5023 root, NULL);
5024 /* If parent inode was deleted, skip it. */
5025 if (IS_ERR(dir_inode))
5026 continue;
5027
5028 ret = btrfs_log_inode(trans, root, dir_inode,
5029 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
5030 iput(dir_inode);
5031 if (ret)
5032 goto out;
5033 }
5034 path->slots[0]++;
5035 }
5036 ret = 0;
5037out:
5038 btrfs_free_path(path);
5039 return ret;
5040}
5041
Chris Masone02119d2008-09-05 16:13:11 -04005042/*
5043 * helper function around btrfs_log_inode to make sure newly created
5044 * parent directories also end up in the log. A minimal inode and backref
5045 * only logging is done of any parent directories that are older than
5046 * the last committed transaction
5047 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005048static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5049 struct btrfs_root *root, struct inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005050 struct dentry *parent,
5051 const loff_t start,
5052 const loff_t end,
5053 int exists_only,
Miao Xie8b050d32014-02-20 18:08:58 +08005054 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005055{
Chris Mason12fcfd22009-03-24 10:24:20 -04005056 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04005057 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00005058 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04005059 int ret = 0;
5060 u64 last_committed = root->fs_info->last_trans_committed;
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005061 bool log_dentries = false;
5062 struct inode *orig_inode = inode;
Chris Mason12fcfd22009-03-24 10:24:20 -04005063
5064 sb = inode->i_sb;
5065
Sage Weil3a5e1402009-04-02 16:49:40 -04005066 if (btrfs_test_opt(root, NOTREELOG)) {
5067 ret = 1;
5068 goto end_no_trans;
5069 }
5070
Miao Xie995946d2014-04-02 19:51:06 +08005071 /*
5072 * The prev transaction commit doesn't complete, we need do
5073 * full commit by ourselves.
5074 */
Chris Mason12fcfd22009-03-24 10:24:20 -04005075 if (root->fs_info->last_trans_log_full_commit >
5076 root->fs_info->last_trans_committed) {
5077 ret = 1;
5078 goto end_no_trans;
5079 }
5080
Yan, Zheng76dda932009-09-21 16:00:26 -04005081 if (root != BTRFS_I(inode)->root ||
5082 btrfs_root_refs(&root->root_item) == 0) {
5083 ret = 1;
5084 goto end_no_trans;
5085 }
5086
Chris Mason12fcfd22009-03-24 10:24:20 -04005087 ret = check_parent_dirs_for_sync(trans, inode, parent,
5088 sb, last_committed);
5089 if (ret)
5090 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04005091
Josef Bacik22ee6982012-05-29 16:57:49 -04005092 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04005093 ret = BTRFS_NO_LOG_SYNC;
5094 goto end_no_trans;
5095 }
5096
Miao Xie8b050d32014-02-20 18:08:58 +08005097 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005098 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08005099 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005100
Filipe Manana8407f552014-09-05 15:14:39 +01005101 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005102 if (ret)
5103 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005104
Chris Masonaf4176b2009-03-24 10:24:31 -04005105 /*
5106 * for regular files, if its inode is already on disk, we don't
5107 * have to worry about the parents at all. This is because
5108 * we can use the last_unlink_trans field to record renames
5109 * and other fun in this file.
5110 */
5111 if (S_ISREG(inode->i_mode) &&
5112 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005113 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
5114 ret = 0;
5115 goto end_trans;
5116 }
Chris Masonaf4176b2009-03-24 10:24:31 -04005117
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005118 if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
5119 log_dentries = true;
5120
Filipe Manana18aa0922015-08-05 16:49:08 +01005121 /*
5122 * On unlink we must make sure all our current and old parent directores
5123 * inodes are fully logged. This is to prevent leaving dangling
5124 * directory index entries in directories that were our parents but are
5125 * not anymore. Not doing this results in old parent directory being
5126 * impossible to delete after log replay (rmdir will always fail with
5127 * error -ENOTEMPTY).
5128 *
5129 * Example 1:
5130 *
5131 * mkdir testdir
5132 * touch testdir/foo
5133 * ln testdir/foo testdir/bar
5134 * sync
5135 * unlink testdir/bar
5136 * xfs_io -c fsync testdir/foo
5137 * <power failure>
5138 * mount fs, triggers log replay
5139 *
5140 * If we don't log the parent directory (testdir), after log replay the
5141 * directory still has an entry pointing to the file inode using the bar
5142 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5143 * the file inode has a link count of 1.
5144 *
5145 * Example 2:
5146 *
5147 * mkdir testdir
5148 * touch foo
5149 * ln foo testdir/foo2
5150 * ln foo testdir/foo3
5151 * sync
5152 * unlink testdir/foo3
5153 * xfs_io -c fsync foo
5154 * <power failure>
5155 * mount fs, triggers log replay
5156 *
5157 * Similar as the first example, after log replay the parent directory
5158 * testdir still has an entry pointing to the inode file with name foo3
5159 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5160 * and has a link count of 2.
5161 */
5162 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
5163 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5164 if (ret)
5165 goto end_trans;
5166 }
5167
Chris Masond3977122009-01-05 21:25:51 -05005168 while (1) {
David Howells2b0143b2015-03-17 22:25:59 +00005169 if (!parent || d_really_is_negative(parent) || sb != d_inode(parent)->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04005170 break;
5171
David Howells2b0143b2015-03-17 22:25:59 +00005172 inode = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005173 if (root != BTRFS_I(inode)->root)
5174 break;
5175
Filipe Manana18aa0922015-08-05 16:49:08 +01005176 if (BTRFS_I(inode)->generation > last_committed) {
5177 ret = btrfs_log_inode(trans, root, inode,
5178 LOG_INODE_EXISTS,
Filipe Manana8407f552014-09-05 15:14:39 +01005179 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005180 if (ret)
5181 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04005182 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005183 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04005184 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04005185
Josef Bacik6a912212010-11-20 09:48:00 +00005186 parent = dget_parent(parent);
5187 dput(old_parent);
5188 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04005189 }
Filipe Manana2f2ff0e2015-03-20 17:19:46 +00005190 if (log_dentries)
5191 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5192 else
5193 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005194end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00005195 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005196 if (ret < 0) {
Miao Xie995946d2014-04-02 19:51:06 +08005197 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005198 ret = 1;
5199 }
Miao Xie8b050d32014-02-20 18:08:58 +08005200
5201 if (ret)
5202 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04005203 btrfs_end_log_trans(root);
5204end_no_trans:
5205 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005206}
5207
5208/*
5209 * it is not safe to log dentry if the chunk root has added new
5210 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5211 * If this returns 1, you must commit the transaction to safely get your
5212 * data on disk.
5213 */
5214int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08005215 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005216 const loff_t start,
5217 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08005218 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04005219{
Josef Bacik6a912212010-11-20 09:48:00 +00005220 struct dentry *parent = dget_parent(dentry);
5221 int ret;
5222
David Howells2b0143b2015-03-17 22:25:59 +00005223 ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
Filipe Manana49dae1b2014-09-06 22:34:39 +01005224 start, end, 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00005225 dput(parent);
5226
5227 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005228}
5229
5230/*
5231 * should be called during mount to recover any replay any log trees
5232 * from the FS
5233 */
5234int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5235{
5236 int ret;
5237 struct btrfs_path *path;
5238 struct btrfs_trans_handle *trans;
5239 struct btrfs_key key;
5240 struct btrfs_key found_key;
5241 struct btrfs_key tmp_key;
5242 struct btrfs_root *log;
5243 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5244 struct walk_control wc = {
5245 .process_func = process_one_buffer,
5246 .stage = 0,
5247 };
5248
Chris Masone02119d2008-09-05 16:13:11 -04005249 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005250 if (!path)
5251 return -ENOMEM;
5252
5253 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04005254
Yan, Zheng4a500fd2010-05-16 10:49:59 -04005255 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005256 if (IS_ERR(trans)) {
5257 ret = PTR_ERR(trans);
5258 goto error;
5259 }
Chris Masone02119d2008-09-05 16:13:11 -04005260
5261 wc.trans = trans;
5262 wc.pin = 1;
5263
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00005264 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005265 if (ret) {
5266 btrfs_error(fs_info, ret, "Failed to pin buffers while "
5267 "recovering log root tree.");
5268 goto error;
5269 }
Chris Masone02119d2008-09-05 16:13:11 -04005270
5271again:
5272 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5273 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02005274 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04005275
Chris Masond3977122009-01-05 21:25:51 -05005276 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04005277 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005278
5279 if (ret < 0) {
5280 btrfs_error(fs_info, ret,
5281 "Couldn't find tree log root.");
5282 goto error;
5283 }
Chris Masone02119d2008-09-05 16:13:11 -04005284 if (ret > 0) {
5285 if (path->slots[0] == 0)
5286 break;
5287 path->slots[0]--;
5288 }
5289 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5290 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02005291 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005292 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5293 break;
5294
Miao Xiecb517ea2013-05-15 07:48:19 +00005295 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005296 if (IS_ERR(log)) {
5297 ret = PTR_ERR(log);
5298 btrfs_error(fs_info, ret,
5299 "Couldn't read tree log root.");
5300 goto error;
5301 }
Chris Masone02119d2008-09-05 16:13:11 -04005302
5303 tmp_key.objectid = found_key.offset;
5304 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5305 tmp_key.offset = (u64)-1;
5306
5307 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005308 if (IS_ERR(wc.replay_dest)) {
5309 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005310 free_extent_buffer(log->node);
5311 free_extent_buffer(log->commit_root);
5312 kfree(log);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005313 btrfs_error(fs_info, ret, "Couldn't read target root "
5314 "for tree log recovery.");
5315 goto error;
5316 }
Chris Masone02119d2008-09-05 16:13:11 -04005317
Yan Zheng07d400a2009-01-06 11:42:00 -05005318 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005319 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04005320 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04005321
Josef Bacikb50c6e22013-04-25 15:55:30 -04005322 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04005323 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5324 path);
Chris Masone02119d2008-09-05 16:13:11 -04005325 }
Chris Masone02119d2008-09-05 16:13:11 -04005326
5327 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05005328 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04005329 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04005330 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04005331 kfree(log);
5332
Josef Bacikb50c6e22013-04-25 15:55:30 -04005333 if (ret)
5334 goto error;
5335
Chris Masone02119d2008-09-05 16:13:11 -04005336 if (found_key.offset == 0)
5337 break;
5338 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005339 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04005340
5341 /* step one is to pin it all, step two is to replay just inodes */
5342 if (wc.pin) {
5343 wc.pin = 0;
5344 wc.process_func = replay_one_buffer;
5345 wc.stage = LOG_WALK_REPLAY_INODES;
5346 goto again;
5347 }
5348 /* step three is to replay everything */
5349 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5350 wc.stage++;
5351 goto again;
5352 }
5353
5354 btrfs_free_path(path);
5355
Josef Bacikabefa552013-04-24 16:40:05 -04005356 /* step 4: commit the transaction, which also unpins the blocks */
5357 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
5358 if (ret)
5359 return ret;
5360
Chris Masone02119d2008-09-05 16:13:11 -04005361 free_extent_buffer(log_root_tree->node);
5362 log_root_tree->log_root = NULL;
5363 fs_info->log_root_recovering = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005364 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005365
Josef Bacikabefa552013-04-24 16:40:05 -04005366 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005367error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04005368 if (wc.trans)
5369 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005370 btrfs_free_path(path);
5371 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04005372}
Chris Mason12fcfd22009-03-24 10:24:20 -04005373
5374/*
5375 * there are some corner cases where we want to force a full
5376 * commit instead of allowing a directory to be logged.
5377 *
5378 * They revolve around files there were unlinked from the directory, and
5379 * this function updates the parent directory so that a full commit is
5380 * properly done if it is fsync'd later after the unlinks are done.
5381 */
5382void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
5383 struct inode *dir, struct inode *inode,
5384 int for_rename)
5385{
5386 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005387 * when we're logging a file, if it hasn't been renamed
5388 * or unlinked, and its inode is fully committed on disk,
5389 * we don't have to worry about walking up the directory chain
5390 * to log its parents.
5391 *
5392 * So, we use the last_unlink_trans field to put this transid
5393 * into the file. When the file is logged we check it and
5394 * don't log the parents if the file is fully on disk.
5395 */
5396 if (S_ISREG(inode->i_mode))
5397 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5398
5399 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005400 * if this directory was already logged any new
5401 * names for this file/dir will get recorded
5402 */
5403 smp_mb();
5404 if (BTRFS_I(dir)->logged_trans == trans->transid)
5405 return;
5406
5407 /*
5408 * if the inode we're about to unlink was logged,
5409 * the log will be properly updated for any new names
5410 */
5411 if (BTRFS_I(inode)->logged_trans == trans->transid)
5412 return;
5413
5414 /*
5415 * when renaming files across directories, if the directory
5416 * there we're unlinking from gets fsync'd later on, there's
5417 * no way to find the destination directory later and fsync it
5418 * properly. So, we have to be conservative and force commits
5419 * so the new name gets discovered.
5420 */
5421 if (for_rename)
5422 goto record;
5423
5424 /* we can safely do the unlink without any special recording */
5425 return;
5426
5427record:
5428 BTRFS_I(dir)->last_unlink_trans = trans->transid;
5429}
5430
5431/*
5432 * Call this after adding a new name for a file and it will properly
5433 * update the log to reflect the new name.
5434 *
5435 * It will return zero if all goes well, and it will return 1 if a
5436 * full transaction commit is required.
5437 */
5438int btrfs_log_new_name(struct btrfs_trans_handle *trans,
5439 struct inode *inode, struct inode *old_dir,
5440 struct dentry *parent)
5441{
5442 struct btrfs_root * root = BTRFS_I(inode)->root;
5443
5444 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04005445 * this will force the logging code to walk the dentry chain
5446 * up for the file
5447 */
5448 if (S_ISREG(inode->i_mode))
5449 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5450
5451 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04005452 * if this inode hasn't been logged and directory we're renaming it
5453 * from hasn't been logged, we don't need to log it
5454 */
5455 if (BTRFS_I(inode)->logged_trans <=
5456 root->fs_info->last_trans_committed &&
5457 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
5458 root->fs_info->last_trans_committed))
5459 return 0;
5460
Filipe Manana49dae1b2014-09-06 22:34:39 +01005461 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
5462 LLONG_MAX, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04005463}
5464