blob: 79f05bc9d6ec7f8606a7fd11083652f11a529b74 [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{
Miao Xie8b050d32014-02-20 18:08:58 +0800143 int index;
Chris Masone02119d2008-09-05 16:13:11 -0400144 int ret;
Yan Zheng7237f182009-01-21 12:54:03 -0500145
146 mutex_lock(&root->log_mutex);
147 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 }
Josef Bacikff782e02009-10-08 15:30:04 -0400152 if (!root->log_start_pid) {
153 root->log_start_pid = current->pid;
Miao Xie27cdeb72014-04-02 19:51:05 +0800154 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400155 } else if (root->log_start_pid != current->pid) {
Miao Xie27cdeb72014-04-02 19:51:05 +0800156 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Josef Bacikff782e02009-10-08 15:30:04 -0400157 }
158
Miao Xie2ecb7922012-09-06 04:04:27 -0600159 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500160 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800161 if (ctx) {
162 index = root->log_transid % 2;
163 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800164 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800165 }
Yan Zheng7237f182009-01-21 12:54:03 -0500166 mutex_unlock(&root->log_mutex);
167 return 0;
168 }
Miao Xiee87ac132014-02-20 18:08:53 +0800169
170 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400171 mutex_lock(&root->fs_info->tree_log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800172 if (!root->fs_info->log_root_tree)
Chris Masone02119d2008-09-05 16:13:11 -0400173 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Miao Xiee87ac132014-02-20 18:08:53 +0800174 mutex_unlock(&root->fs_info->tree_log_mutex);
175 if (ret)
176 goto out;
177
178 if (!root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400179 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400180 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800181 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400182 }
Miao Xie27cdeb72014-04-02 19:51:05 +0800183 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
Miao Xiee87ac132014-02-20 18:08:53 +0800184 root->log_start_pid = current->pid;
Miao Xie2ecb7922012-09-06 04:04:27 -0600185 atomic_inc(&root->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -0500186 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800187 if (ctx) {
188 index = root->log_transid % 2;
189 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800190 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800191 }
Miao Xiee87ac132014-02-20 18:08:53 +0800192out:
Yan Zheng7237f182009-01-21 12:54:03 -0500193 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800194 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400195}
196
197/*
198 * returns 0 if there was a log transaction running and we were able
199 * to join, or returns -ENOENT if there were not transactions
200 * in progress
201 */
202static int join_running_log_trans(struct btrfs_root *root)
203{
204 int ret = -ENOENT;
205
206 smp_mb();
207 if (!root->log_root)
208 return -ENOENT;
209
Yan Zheng7237f182009-01-21 12:54:03 -0500210 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400211 if (root->log_root) {
212 ret = 0;
Yan Zheng7237f182009-01-21 12:54:03 -0500213 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400214 }
Yan Zheng7237f182009-01-21 12:54:03 -0500215 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400216 return ret;
217}
218
219/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400220 * This either makes the current running log transaction wait
221 * until you call btrfs_end_log_trans() or it makes any future
222 * log transactions wait until you call btrfs_end_log_trans()
223 */
224int btrfs_pin_log_trans(struct btrfs_root *root)
225{
226 int ret = -ENOENT;
227
228 mutex_lock(&root->log_mutex);
229 atomic_inc(&root->log_writers);
230 mutex_unlock(&root->log_mutex);
231 return ret;
232}
233
234/*
Chris Masone02119d2008-09-05 16:13:11 -0400235 * indicate we're done making changes to the log tree
236 * and wake up anyone waiting to do a sync
237 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100238void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400239{
Yan Zheng7237f182009-01-21 12:54:03 -0500240 if (atomic_dec_and_test(&root->log_writers)) {
241 smp_mb();
242 if (waitqueue_active(&root->log_writer_wait))
243 wake_up(&root->log_writer_wait);
244 }
Chris Masone02119d2008-09-05 16:13:11 -0400245}
246
247
248/*
249 * the walk control struct is used to pass state down the chain when
250 * processing the log tree. The stage field tells us which part
251 * of the log tree processing we are currently doing. The others
252 * are state fields used for that specific part
253 */
254struct walk_control {
255 /* should we free the extent on disk when done? This is used
256 * at transaction commit time while freeing a log tree
257 */
258 int free;
259
260 /* should we write out the extent buffer? This is used
261 * while flushing the log tree to disk during a sync
262 */
263 int write;
264
265 /* should we wait for the extent buffer io to finish? Also used
266 * while flushing the log tree to disk for a sync
267 */
268 int wait;
269
270 /* pin only walk, we record which extents on disk belong to the
271 * log trees
272 */
273 int pin;
274
275 /* what stage of the replay code we're currently in */
276 int stage;
277
278 /* the root we are currently replaying */
279 struct btrfs_root *replay_dest;
280
281 /* the trans handle for the current replay */
282 struct btrfs_trans_handle *trans;
283
284 /* the function that gets used to process blocks we find in the
285 * tree. Note the extent_buffer might not be up to date when it is
286 * passed in, and it must be checked or read if you need the data
287 * inside it
288 */
289 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
290 struct walk_control *wc, u64 gen);
291};
292
293/*
294 * process_func used to pin down extents, write them or wait on them
295 */
296static int process_one_buffer(struct btrfs_root *log,
297 struct extent_buffer *eb,
298 struct walk_control *wc, u64 gen)
299{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400300 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400301
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400302 /*
303 * If this fs is mixed then we need to be able to process the leaves to
304 * pin down any logged extents, so we have to read the block.
305 */
306 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
307 ret = btrfs_read_buffer(eb, gen);
308 if (ret)
309 return ret;
310 }
311
Josef Bacikb50c6e22013-04-25 15:55:30 -0400312 if (wc->pin)
313 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
314 eb->start, eb->len);
315
316 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400317 if (wc->pin && btrfs_header_level(eb) == 0)
318 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400319 if (wc->write)
320 btrfs_write_tree_block(eb);
321 if (wc->wait)
322 btrfs_wait_tree_block_writeback(eb);
323 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400324 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400325}
326
327/*
328 * Item overwrite used by replay and tree logging. eb, slot and key all refer
329 * to the src data we are copying out.
330 *
331 * root is the tree we are copying into, and path is a scratch
332 * path for use in this function (it should be released on entry and
333 * will be released on exit).
334 *
335 * If the key is already in the destination tree the existing item is
336 * overwritten. If the existing item isn't big enough, it is extended.
337 * If it is too large, it is truncated.
338 *
339 * If the key isn't in the destination yet, a new item is inserted.
340 */
341static noinline int overwrite_item(struct btrfs_trans_handle *trans,
342 struct btrfs_root *root,
343 struct btrfs_path *path,
344 struct extent_buffer *eb, int slot,
345 struct btrfs_key *key)
346{
347 int ret;
348 u32 item_size;
349 u64 saved_i_size = 0;
350 int save_old_i_size = 0;
351 unsigned long src_ptr;
352 unsigned long dst_ptr;
353 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000354 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400355
356 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
357 overwrite_root = 1;
358
359 item_size = btrfs_item_size_nr(eb, slot);
360 src_ptr = btrfs_item_ptr_offset(eb, slot);
361
362 /* look for the key in the destination tree */
363 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000364 if (ret < 0)
365 return ret;
366
Chris Masone02119d2008-09-05 16:13:11 -0400367 if (ret == 0) {
368 char *src_copy;
369 char *dst_copy;
370 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
371 path->slots[0]);
372 if (dst_size != item_size)
373 goto insert;
374
375 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200376 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400377 return 0;
378 }
379 dst_copy = kmalloc(item_size, GFP_NOFS);
380 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000381 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200382 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000383 kfree(dst_copy);
384 kfree(src_copy);
385 return -ENOMEM;
386 }
Chris Masone02119d2008-09-05 16:13:11 -0400387
388 read_extent_buffer(eb, src_copy, src_ptr, item_size);
389
390 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
391 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
392 item_size);
393 ret = memcmp(dst_copy, src_copy, item_size);
394
395 kfree(dst_copy);
396 kfree(src_copy);
397 /*
398 * they have the same contents, just return, this saves
399 * us from cowing blocks in the destination tree and doing
400 * extra writes that may not have been done by a previous
401 * sync
402 */
403 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200404 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400405 return 0;
406 }
407
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000408 /*
409 * We need to load the old nbytes into the inode so when we
410 * replay the extents we've logged we get the right nbytes.
411 */
412 if (inode_item) {
413 struct btrfs_inode_item *item;
414 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400415 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000416
417 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
418 struct btrfs_inode_item);
419 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
420 item = btrfs_item_ptr(eb, slot,
421 struct btrfs_inode_item);
422 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400423
424 /*
425 * If this is a directory we need to reset the i_size to
426 * 0 so that we can set it up properly when replaying
427 * the rest of the items in this log.
428 */
429 mode = btrfs_inode_mode(eb, item);
430 if (S_ISDIR(mode))
431 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000432 }
433 } else if (inode_item) {
434 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400435 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000436
437 /*
438 * New inode, set nbytes to 0 so that the nbytes comes out
439 * properly when we replay the extents.
440 */
441 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
442 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400443
444 /*
445 * If this is a directory we need to reset the i_size to 0 so
446 * that we can set it up properly when replaying the rest of
447 * the items in this log.
448 */
449 mode = btrfs_inode_mode(eb, item);
450 if (S_ISDIR(mode))
451 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400452 }
453insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200454 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400455 /* try to insert the key into the destination tree */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000456 path->skip_release_on_error = 1;
Chris Masone02119d2008-09-05 16:13:11 -0400457 ret = btrfs_insert_empty_item(trans, root, path,
458 key, item_size);
Filipe Mananadf8d1162015-01-14 01:52:25 +0000459 path->skip_release_on_error = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400460
461 /* make sure any existing item is the correct size */
Filipe Mananadf8d1162015-01-14 01:52:25 +0000462 if (ret == -EEXIST || ret == -EOVERFLOW) {
Chris Masone02119d2008-09-05 16:13:11 -0400463 u32 found_size;
464 found_size = btrfs_item_size_nr(path->nodes[0],
465 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100466 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000467 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100468 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000469 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100470 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400471 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400472 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400473 }
474 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
475 path->slots[0]);
476
477 /* don't overwrite an existing inode if the generation number
478 * was logged as zero. This is done when the tree logging code
479 * is just logging an inode to make sure it exists after recovery.
480 *
481 * Also, don't overwrite i_size on directories during replay.
482 * log replay inserts and removes directory items based on the
483 * state of the tree found in the subvolume, and i_size is modified
484 * as it goes
485 */
486 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
487 struct btrfs_inode_item *src_item;
488 struct btrfs_inode_item *dst_item;
489
490 src_item = (struct btrfs_inode_item *)src_ptr;
491 dst_item = (struct btrfs_inode_item *)dst_ptr;
492
493 if (btrfs_inode_generation(eb, src_item) == 0)
494 goto no_copy;
495
496 if (overwrite_root &&
497 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
498 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
499 save_old_i_size = 1;
500 saved_i_size = btrfs_inode_size(path->nodes[0],
501 dst_item);
502 }
503 }
504
505 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
506 src_ptr, item_size);
507
508 if (save_old_i_size) {
509 struct btrfs_inode_item *dst_item;
510 dst_item = (struct btrfs_inode_item *)dst_ptr;
511 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
512 }
513
514 /* make sure the generation is filled in */
515 if (key->type == BTRFS_INODE_ITEM_KEY) {
516 struct btrfs_inode_item *dst_item;
517 dst_item = (struct btrfs_inode_item *)dst_ptr;
518 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
519 btrfs_set_inode_generation(path->nodes[0], dst_item,
520 trans->transid);
521 }
522 }
523no_copy:
524 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200525 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400526 return 0;
527}
528
529/*
530 * simple helper to read an inode off the disk from a given root
531 * This can only be called for subvolume roots and not for the log
532 */
533static noinline struct inode *read_one_inode(struct btrfs_root *root,
534 u64 objectid)
535{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400536 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400537 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400538
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400539 key.objectid = objectid;
540 key.type = BTRFS_INODE_ITEM_KEY;
541 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000542 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400543 if (IS_ERR(inode)) {
544 inode = NULL;
545 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400546 iput(inode);
547 inode = NULL;
548 }
549 return inode;
550}
551
552/* replays a single extent in 'eb' at 'slot' with 'key' into the
553 * subvolume 'root'. path is released on entry and should be released
554 * on exit.
555 *
556 * extents in the log tree have not been allocated out of the extent
557 * tree yet. So, this completes the allocation, taking a reference
558 * as required if the extent already exists or creating a new extent
559 * if it isn't in the extent allocation tree yet.
560 *
561 * The extent is inserted into the file, dropping any existing extents
562 * from the file that overlap the new one.
563 */
564static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
565 struct btrfs_root *root,
566 struct btrfs_path *path,
567 struct extent_buffer *eb, int slot,
568 struct btrfs_key *key)
569{
570 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400571 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400572 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000573 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400574 struct btrfs_file_extent_item *item;
575 struct inode *inode = NULL;
576 unsigned long size;
577 int ret = 0;
578
579 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
580 found_type = btrfs_file_extent_type(eb, item);
581
Yan Zhengd899e052008-10-30 14:25:28 -0400582 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000583 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
584 nbytes = btrfs_file_extent_num_bytes(eb, item);
585 extent_end = start + nbytes;
586
587 /*
588 * We don't add to the inodes nbytes if we are prealloc or a
589 * hole.
590 */
591 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
592 nbytes = 0;
593 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800594 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000595 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000596 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400597 } else {
598 ret = 0;
599 goto out;
600 }
601
602 inode = read_one_inode(root, key->objectid);
603 if (!inode) {
604 ret = -EIO;
605 goto out;
606 }
607
608 /*
609 * first check to see if we already have this extent in the
610 * file. This must be done before the btrfs_drop_extents run
611 * so we don't try to drop this extent.
612 */
Li Zefan33345d012011-04-20 10:31:50 +0800613 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400614 start, 0);
615
Yan Zhengd899e052008-10-30 14:25:28 -0400616 if (ret == 0 &&
617 (found_type == BTRFS_FILE_EXTENT_REG ||
618 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400619 struct btrfs_file_extent_item cmp1;
620 struct btrfs_file_extent_item cmp2;
621 struct btrfs_file_extent_item *existing;
622 struct extent_buffer *leaf;
623
624 leaf = path->nodes[0];
625 existing = btrfs_item_ptr(leaf, path->slots[0],
626 struct btrfs_file_extent_item);
627
628 read_extent_buffer(eb, &cmp1, (unsigned long)item,
629 sizeof(cmp1));
630 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
631 sizeof(cmp2));
632
633 /*
634 * we already have a pointer to this exact extent,
635 * we don't have to do anything
636 */
637 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200638 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400639 goto out;
640 }
641 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200642 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400643
644 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400645 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400646 if (ret)
647 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400648
Yan Zheng07d400a2009-01-06 11:42:00 -0500649 if (found_type == BTRFS_FILE_EXTENT_REG ||
650 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400651 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500652 unsigned long dest_offset;
653 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400654
Yan Zheng07d400a2009-01-06 11:42:00 -0500655 ret = btrfs_insert_empty_item(trans, root, path, key,
656 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400657 if (ret)
658 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500659 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
660 path->slots[0]);
661 copy_extent_buffer(path->nodes[0], eb, dest_offset,
662 (unsigned long)item, sizeof(*item));
663
664 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
665 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
666 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400667 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500668
669 if (ins.objectid > 0) {
670 u64 csum_start;
671 u64 csum_end;
672 LIST_HEAD(ordered_sums);
673 /*
674 * is this extent already allocated in the extent
675 * allocation tree? If so, just add a reference
676 */
Filipe Manana1a4ed8f2014-10-27 10:44:24 +0000677 ret = btrfs_lookup_data_extent(root, ins.objectid,
Yan Zheng07d400a2009-01-06 11:42:00 -0500678 ins.offset);
679 if (ret == 0) {
680 ret = btrfs_inc_extent_ref(trans, root,
681 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400682 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200683 key->objectid, offset, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400684 if (ret)
685 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500686 } else {
687 /*
688 * insert the extent pointer in the extent
689 * allocation tree
690 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400691 ret = btrfs_alloc_logged_file_extent(trans,
692 root, root->root_key.objectid,
693 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400694 if (ret)
695 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500696 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200697 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500698
699 if (btrfs_file_extent_compression(eb, item)) {
700 csum_start = ins.objectid;
701 csum_end = csum_start + ins.offset;
702 } else {
703 csum_start = ins.objectid +
704 btrfs_file_extent_offset(eb, item);
705 csum_end = csum_start +
706 btrfs_file_extent_num_bytes(eb, item);
707 }
708
709 ret = btrfs_lookup_csums_range(root->log_root,
710 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100711 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400712 if (ret)
713 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500714 while (!list_empty(&ordered_sums)) {
715 struct btrfs_ordered_sum *sums;
716 sums = list_entry(ordered_sums.next,
717 struct btrfs_ordered_sum,
718 list);
Josef Bacik36508602013-04-25 16:23:32 -0400719 if (!ret)
720 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500721 root->fs_info->csum_root,
722 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500723 list_del(&sums->list);
724 kfree(sums);
725 }
Josef Bacik36508602013-04-25 16:23:32 -0400726 if (ret)
727 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500728 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200729 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500730 }
731 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
732 /* inline extents are easy, we just overwrite them */
733 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400734 if (ret)
735 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500736 }
737
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000738 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600739 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400740out:
741 if (inode)
742 iput(inode);
743 return ret;
744}
745
746/*
747 * when cleaning up conflicts between the directory names in the
748 * subvolume, directory names in the log and directory names in the
749 * inode back references, we may have to unlink inodes from directories.
750 *
751 * This is a helper function to do the unlink of a specific directory
752 * item
753 */
754static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
755 struct btrfs_root *root,
756 struct btrfs_path *path,
757 struct inode *dir,
758 struct btrfs_dir_item *di)
759{
760 struct inode *inode;
761 char *name;
762 int name_len;
763 struct extent_buffer *leaf;
764 struct btrfs_key location;
765 int ret;
766
767 leaf = path->nodes[0];
768
769 btrfs_dir_item_key_to_cpu(leaf, di, &location);
770 name_len = btrfs_dir_name_len(leaf, di);
771 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000772 if (!name)
773 return -ENOMEM;
774
Chris Masone02119d2008-09-05 16:13:11 -0400775 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200776 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400777
778 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000779 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400780 ret = -EIO;
781 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000782 }
Chris Masone02119d2008-09-05 16:13:11 -0400783
Yan Zhengec051c02009-01-05 15:43:42 -0500784 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400785 if (ret)
786 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400787
Chris Masone02119d2008-09-05 16:13:11 -0400788 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400789 if (ret)
790 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100791 else
792 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400793out:
794 kfree(name);
795 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400796 return ret;
797}
798
799/*
800 * helper function to see if a given name and sequence number found
801 * in an inode back reference are already in a directory and correctly
802 * point to this inode
803 */
804static noinline int inode_in_dir(struct btrfs_root *root,
805 struct btrfs_path *path,
806 u64 dirid, u64 objectid, u64 index,
807 const char *name, int name_len)
808{
809 struct btrfs_dir_item *di;
810 struct btrfs_key location;
811 int match = 0;
812
813 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
814 index, name, name_len, 0);
815 if (di && !IS_ERR(di)) {
816 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
817 if (location.objectid != objectid)
818 goto out;
819 } else
820 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200821 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400822
823 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
824 if (di && !IS_ERR(di)) {
825 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
826 if (location.objectid != objectid)
827 goto out;
828 } else
829 goto out;
830 match = 1;
831out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200832 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400833 return match;
834}
835
836/*
837 * helper function to check a log tree for a named back reference in
838 * an inode. This is used to decide if a back reference that is
839 * found in the subvolume conflicts with what we find in the log.
840 *
841 * inode backreferences may have multiple refs in a single item,
842 * during replay we process one reference at a time, and we don't
843 * want to delete valid links to a file from the subvolume if that
844 * link is also in the log.
845 */
846static noinline int backref_in_log(struct btrfs_root *log,
847 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700848 u64 ref_objectid,
Filipe Mananadf8d1162015-01-14 01:52:25 +0000849 const char *name, int namelen)
Chris Masone02119d2008-09-05 16:13:11 -0400850{
851 struct btrfs_path *path;
852 struct btrfs_inode_ref *ref;
853 unsigned long ptr;
854 unsigned long ptr_end;
855 unsigned long name_ptr;
856 int found_name_len;
857 int item_size;
858 int ret;
859 int match = 0;
860
861 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000862 if (!path)
863 return -ENOMEM;
864
Chris Masone02119d2008-09-05 16:13:11 -0400865 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
866 if (ret != 0)
867 goto out;
868
Chris Masone02119d2008-09-05 16:13:11 -0400869 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700870
871 if (key->type == BTRFS_INODE_EXTREF_KEY) {
872 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
873 name, namelen, NULL))
874 match = 1;
875
876 goto out;
877 }
878
879 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400880 ptr_end = ptr + item_size;
881 while (ptr < ptr_end) {
882 ref = (struct btrfs_inode_ref *)ptr;
883 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
884 if (found_name_len == namelen) {
885 name_ptr = (unsigned long)(ref + 1);
886 ret = memcmp_extent_buffer(path->nodes[0], name,
887 name_ptr, namelen);
888 if (ret == 0) {
889 match = 1;
890 goto out;
891 }
892 }
893 ptr = (unsigned long)(ref + 1) + found_name_len;
894 }
895out:
896 btrfs_free_path(path);
897 return match;
898}
899
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700900static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
901 struct btrfs_root *root,
902 struct btrfs_path *path,
903 struct btrfs_root *log_root,
904 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700905 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700906 u64 inode_objectid, u64 parent_objectid,
907 u64 ref_index, char *name, int namelen,
908 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700909{
910 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700911 char *victim_name;
912 int victim_name_len;
913 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700914 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700915 struct btrfs_key search_key;
916 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700917
Mark Fashehf1863732012-08-08 11:32:27 -0700918again:
919 /* Search old style refs */
920 search_key.objectid = inode_objectid;
921 search_key.type = BTRFS_INODE_REF_KEY;
922 search_key.offset = parent_objectid;
923 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700924 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700925 struct btrfs_inode_ref *victim_ref;
926 unsigned long ptr;
927 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -0700928
929 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700930
931 /* are we trying to overwrite a back ref for the root directory
932 * if so, just jump out, we're done
933 */
Mark Fashehf1863732012-08-08 11:32:27 -0700934 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700935 return 1;
936
937 /* check all the names in this back reference to see
938 * if they are in the log. if so, we allow them to stay
939 * otherwise they must be unlinked as a conflict
940 */
941 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
942 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
943 while (ptr < ptr_end) {
944 victim_ref = (struct btrfs_inode_ref *)ptr;
945 victim_name_len = btrfs_inode_ref_name_len(leaf,
946 victim_ref);
947 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400948 if (!victim_name)
949 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700950
951 read_extent_buffer(leaf, victim_name,
952 (unsigned long)(victim_ref + 1),
953 victim_name_len);
954
Mark Fashehf1863732012-08-08 11:32:27 -0700955 if (!backref_in_log(log_root, &search_key,
956 parent_objectid,
957 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700958 victim_name_len)) {
Zach Brown8b558c52013-10-16 12:10:34 -0700959 inc_nlink(inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700960 btrfs_release_path(path);
961
962 ret = btrfs_unlink_inode(trans, root, dir,
963 inode, victim_name,
964 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -0700965 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -0400966 if (ret)
967 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100968 ret = btrfs_run_delayed_items(trans, root);
969 if (ret)
970 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700971 *search_done = 1;
972 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700973 }
974 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -0700975
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700976 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
977 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700978
979 /*
980 * NOTE: we have searched root tree and checked the
981 * coresponding ref, it does not need to check again.
982 */
983 *search_done = 1;
984 }
985 btrfs_release_path(path);
986
Mark Fashehf1863732012-08-08 11:32:27 -0700987 /* Same search but for extended refs */
988 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
989 inode_objectid, parent_objectid, 0,
990 0);
991 if (!IS_ERR_OR_NULL(extref)) {
992 u32 item_size;
993 u32 cur_offset = 0;
994 unsigned long base;
995 struct inode *victim_parent;
996
997 leaf = path->nodes[0];
998
999 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1000 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1001
1002 while (cur_offset < item_size) {
1003 extref = (struct btrfs_inode_extref *)base + cur_offset;
1004
1005 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1006
1007 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1008 goto next;
1009
1010 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001011 if (!victim_name)
1012 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001013 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1014 victim_name_len);
1015
1016 search_key.objectid = inode_objectid;
1017 search_key.type = BTRFS_INODE_EXTREF_KEY;
1018 search_key.offset = btrfs_extref_hash(parent_objectid,
1019 victim_name,
1020 victim_name_len);
1021 ret = 0;
1022 if (!backref_in_log(log_root, &search_key,
1023 parent_objectid, victim_name,
1024 victim_name_len)) {
1025 ret = -ENOENT;
1026 victim_parent = read_one_inode(root,
1027 parent_objectid);
1028 if (victim_parent) {
Zach Brown8b558c52013-10-16 12:10:34 -07001029 inc_nlink(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001030 btrfs_release_path(path);
1031
1032 ret = btrfs_unlink_inode(trans, root,
1033 victim_parent,
1034 inode,
1035 victim_name,
1036 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001037 if (!ret)
1038 ret = btrfs_run_delayed_items(
1039 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001040 }
Mark Fashehf1863732012-08-08 11:32:27 -07001041 iput(victim_parent);
1042 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001043 if (ret)
1044 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001045 *search_done = 1;
1046 goto again;
1047 }
1048 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001049 if (ret)
1050 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001051next:
1052 cur_offset += victim_name_len + sizeof(*extref);
1053 }
1054 *search_done = 1;
1055 }
1056 btrfs_release_path(path);
1057
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001058 /* look for a conflicting sequence number */
1059 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001060 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001061 if (di && !IS_ERR(di)) {
1062 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001063 if (ret)
1064 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001065 }
1066 btrfs_release_path(path);
1067
1068 /* look for a conflicing name */
1069 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1070 name, namelen, 0);
1071 if (di && !IS_ERR(di)) {
1072 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001073 if (ret)
1074 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001075 }
1076 btrfs_release_path(path);
1077
1078 return 0;
1079}
Chris Masone02119d2008-09-05 16:13:11 -04001080
Mark Fashehf1863732012-08-08 11:32:27 -07001081static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1082 u32 *namelen, char **name, u64 *index,
1083 u64 *parent_objectid)
1084{
1085 struct btrfs_inode_extref *extref;
1086
1087 extref = (struct btrfs_inode_extref *)ref_ptr;
1088
1089 *namelen = btrfs_inode_extref_name_len(eb, extref);
1090 *name = kmalloc(*namelen, GFP_NOFS);
1091 if (*name == NULL)
1092 return -ENOMEM;
1093
1094 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1095 *namelen);
1096
1097 *index = btrfs_inode_extref_index(eb, extref);
1098 if (parent_objectid)
1099 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1100
1101 return 0;
1102}
1103
1104static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1105 u32 *namelen, char **name, u64 *index)
1106{
1107 struct btrfs_inode_ref *ref;
1108
1109 ref = (struct btrfs_inode_ref *)ref_ptr;
1110
1111 *namelen = btrfs_inode_ref_name_len(eb, ref);
1112 *name = kmalloc(*namelen, GFP_NOFS);
1113 if (*name == NULL)
1114 return -ENOMEM;
1115
1116 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1117
1118 *index = btrfs_inode_ref_index(eb, ref);
1119
1120 return 0;
1121}
1122
Chris Masone02119d2008-09-05 16:13:11 -04001123/*
1124 * replay one inode back reference item found in the log tree.
1125 * eb, slot and key refer to the buffer and key found in the log tree.
1126 * root is the destination we are replaying into, and path is for temp
1127 * use by this function. (it should be released on return).
1128 */
1129static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1130 struct btrfs_root *root,
1131 struct btrfs_root *log,
1132 struct btrfs_path *path,
1133 struct extent_buffer *eb, int slot,
1134 struct btrfs_key *key)
1135{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001136 struct inode *dir = NULL;
1137 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001138 unsigned long ref_ptr;
1139 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001140 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001141 int namelen;
1142 int ret;
liuboc622ae62011-03-26 08:01:12 -04001143 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001144 int log_ref_ver = 0;
1145 u64 parent_objectid;
1146 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001147 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001148 int ref_struct_size;
1149
1150 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1151 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1152
1153 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1154 struct btrfs_inode_extref *r;
1155
1156 ref_struct_size = sizeof(struct btrfs_inode_extref);
1157 log_ref_ver = 1;
1158 r = (struct btrfs_inode_extref *)ref_ptr;
1159 parent_objectid = btrfs_inode_extref_parent(eb, r);
1160 } else {
1161 ref_struct_size = sizeof(struct btrfs_inode_ref);
1162 parent_objectid = key->offset;
1163 }
1164 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001165
Chris Masone02119d2008-09-05 16:13:11 -04001166 /*
1167 * it is possible that we didn't log all the parent directories
1168 * for a given inode. If we don't find the dir, just don't
1169 * copy the back ref in. The link count fixup code will take
1170 * care of the rest
1171 */
Mark Fashehf1863732012-08-08 11:32:27 -07001172 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001173 if (!dir) {
1174 ret = -ENOENT;
1175 goto out;
1176 }
Chris Masone02119d2008-09-05 16:13:11 -04001177
Mark Fashehf1863732012-08-08 11:32:27 -07001178 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001179 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001180 ret = -EIO;
1181 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001182 }
Chris Masone02119d2008-09-05 16:13:11 -04001183
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001184 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001185 if (log_ref_ver) {
1186 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1187 &ref_index, &parent_objectid);
1188 /*
1189 * parent object can change from one array
1190 * item to another.
1191 */
1192 if (!dir)
1193 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001194 if (!dir) {
1195 ret = -ENOENT;
1196 goto out;
1197 }
Mark Fashehf1863732012-08-08 11:32:27 -07001198 } else {
1199 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1200 &ref_index);
1201 }
1202 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001203 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001204
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001205 /* if we already have a perfect match, we're done */
1206 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001207 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001208 /*
1209 * look for a conflicting back reference in the
1210 * metadata. if we find one we have to unlink that name
1211 * of the file before we add our new link. Later on, we
1212 * overwrite any existing back reference, and we don't
1213 * want to create dangling pointers in the directory.
1214 */
Chris Masone02119d2008-09-05 16:13:11 -04001215
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001216 if (!search_done) {
1217 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001218 dir, inode, eb,
1219 inode_objectid,
1220 parent_objectid,
1221 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001222 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001223 if (ret) {
1224 if (ret == 1)
1225 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001226 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001227 }
Chris Masone02119d2008-09-05 16:13:11 -04001228 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001229
1230 /* insert our name */
1231 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001232 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001233 if (ret)
1234 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001235
1236 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001237 }
liuboc622ae62011-03-26 08:01:12 -04001238
Mark Fashehf1863732012-08-08 11:32:27 -07001239 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001240 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001241 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001242 if (log_ref_ver) {
1243 iput(dir);
1244 dir = NULL;
1245 }
Chris Masone02119d2008-09-05 16:13:11 -04001246 }
Chris Masone02119d2008-09-05 16:13:11 -04001247
1248 /* finally write the back reference in the inode */
1249 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001250out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001251 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001252 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001253 iput(dir);
1254 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001255 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001256}
1257
Yan, Zhengc71bf092009-11-12 09:34:40 +00001258static int insert_orphan_item(struct btrfs_trans_handle *trans,
David Sterba9c4f61f2015-01-02 19:12:57 +01001259 struct btrfs_root *root, u64 ino)
Yan, Zhengc71bf092009-11-12 09:34:40 +00001260{
1261 int ret;
David Sterba381cf652015-01-02 18:45:16 +01001262
David Sterba9c4f61f2015-01-02 19:12:57 +01001263 ret = btrfs_insert_orphan_item(trans, root, ino);
1264 if (ret == -EEXIST)
1265 ret = 0;
David Sterba381cf652015-01-02 18:45:16 +01001266
Yan, Zhengc71bf092009-11-12 09:34:40 +00001267 return ret;
1268}
1269
Mark Fashehf1863732012-08-08 11:32:27 -07001270static int count_inode_extrefs(struct btrfs_root *root,
1271 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001272{
Mark Fashehf1863732012-08-08 11:32:27 -07001273 int ret = 0;
1274 int name_len;
1275 unsigned int nlink = 0;
1276 u32 item_size;
1277 u32 cur_offset = 0;
1278 u64 inode_objectid = btrfs_ino(inode);
1279 u64 offset = 0;
1280 unsigned long ptr;
1281 struct btrfs_inode_extref *extref;
1282 struct extent_buffer *leaf;
1283
1284 while (1) {
1285 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1286 &extref, &offset);
1287 if (ret)
1288 break;
1289
1290 leaf = path->nodes[0];
1291 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1292 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Filipe Manana2c2c4522015-01-13 16:40:04 +00001293 cur_offset = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001294
1295 while (cur_offset < item_size) {
1296 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1297 name_len = btrfs_inode_extref_name_len(leaf, extref);
1298
1299 nlink++;
1300
1301 cur_offset += name_len + sizeof(*extref);
1302 }
1303
1304 offset++;
1305 btrfs_release_path(path);
1306 }
1307 btrfs_release_path(path);
1308
Filipe Manana2c2c4522015-01-13 16:40:04 +00001309 if (ret < 0 && ret != -ENOENT)
Mark Fashehf1863732012-08-08 11:32:27 -07001310 return ret;
1311 return nlink;
1312}
1313
1314static int count_inode_refs(struct btrfs_root *root,
1315 struct inode *inode, struct btrfs_path *path)
1316{
Chris Masone02119d2008-09-05 16:13:11 -04001317 int ret;
1318 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001319 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001320 unsigned long ptr;
1321 unsigned long ptr_end;
1322 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001323 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001324
Li Zefan33345d012011-04-20 10:31:50 +08001325 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001326 key.type = BTRFS_INODE_REF_KEY;
1327 key.offset = (u64)-1;
1328
Chris Masond3977122009-01-05 21:25:51 -05001329 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001330 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1331 if (ret < 0)
1332 break;
1333 if (ret > 0) {
1334 if (path->slots[0] == 0)
1335 break;
1336 path->slots[0]--;
1337 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001338process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001339 btrfs_item_key_to_cpu(path->nodes[0], &key,
1340 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001341 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001342 key.type != BTRFS_INODE_REF_KEY)
1343 break;
1344 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1345 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1346 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001347 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001348 struct btrfs_inode_ref *ref;
1349
1350 ref = (struct btrfs_inode_ref *)ptr;
1351 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1352 ref);
1353 ptr = (unsigned long)(ref + 1) + name_len;
1354 nlink++;
1355 }
1356
1357 if (key.offset == 0)
1358 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001359 if (path->slots[0] > 0) {
1360 path->slots[0]--;
1361 goto process_slot;
1362 }
Chris Masone02119d2008-09-05 16:13:11 -04001363 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001364 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001365 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001366 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001367
1368 return nlink;
1369}
1370
1371/*
1372 * There are a few corners where the link count of the file can't
1373 * be properly maintained during replay. So, instead of adding
1374 * lots of complexity to the log code, we just scan the backrefs
1375 * for any file that has been through replay.
1376 *
1377 * The scan will update the link count on the inode to reflect the
1378 * number of back refs found. If it goes down to zero, the iput
1379 * will free the inode.
1380 */
1381static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1382 struct btrfs_root *root,
1383 struct inode *inode)
1384{
1385 struct btrfs_path *path;
1386 int ret;
1387 u64 nlink = 0;
1388 u64 ino = btrfs_ino(inode);
1389
1390 path = btrfs_alloc_path();
1391 if (!path)
1392 return -ENOMEM;
1393
1394 ret = count_inode_refs(root, inode, path);
1395 if (ret < 0)
1396 goto out;
1397
1398 nlink = ret;
1399
1400 ret = count_inode_extrefs(root, inode, path);
Mark Fashehf1863732012-08-08 11:32:27 -07001401 if (ret < 0)
1402 goto out;
1403
1404 nlink += ret;
1405
1406 ret = 0;
1407
Chris Masone02119d2008-09-05 16:13:11 -04001408 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001409 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001410 btrfs_update_inode(trans, root, inode);
1411 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001412 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001413
Yan, Zhengc71bf092009-11-12 09:34:40 +00001414 if (inode->i_nlink == 0) {
1415 if (S_ISDIR(inode->i_mode)) {
1416 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001417 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001418 if (ret)
1419 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001420 }
Li Zefan33345d012011-04-20 10:31:50 +08001421 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001422 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001423
Mark Fashehf1863732012-08-08 11:32:27 -07001424out:
1425 btrfs_free_path(path);
1426 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001427}
1428
1429static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1430 struct btrfs_root *root,
1431 struct btrfs_path *path)
1432{
1433 int ret;
1434 struct btrfs_key key;
1435 struct inode *inode;
1436
1437 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1438 key.type = BTRFS_ORPHAN_ITEM_KEY;
1439 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001440 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001441 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1442 if (ret < 0)
1443 break;
1444
1445 if (ret == 1) {
1446 if (path->slots[0] == 0)
1447 break;
1448 path->slots[0]--;
1449 }
1450
1451 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1452 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1453 key.type != BTRFS_ORPHAN_ITEM_KEY)
1454 break;
1455
1456 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001457 if (ret)
1458 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001459
David Sterbab3b4aa72011-04-21 01:20:15 +02001460 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001461 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001462 if (!inode)
1463 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001464
1465 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001466 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001467 if (ret)
1468 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001469
Chris Mason12fcfd22009-03-24 10:24:20 -04001470 /*
1471 * fixup on a directory may create new entries,
1472 * make sure we always look for the highset possible
1473 * offset
1474 */
1475 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001476 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001477 ret = 0;
1478out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001479 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001480 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001481}
1482
1483
1484/*
1485 * record a given inode in the fixup dir so we can check its link
1486 * count when replay is done. The link count is incremented here
1487 * so the inode won't go away until we check it
1488 */
1489static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1490 struct btrfs_root *root,
1491 struct btrfs_path *path,
1492 u64 objectid)
1493{
1494 struct btrfs_key key;
1495 int ret = 0;
1496 struct inode *inode;
1497
1498 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001499 if (!inode)
1500 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001501
1502 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02001503 key.type = BTRFS_ORPHAN_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04001504 key.offset = objectid;
1505
1506 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1507
David Sterbab3b4aa72011-04-21 01:20:15 +02001508 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001509 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001510 if (!inode->i_nlink)
1511 set_nlink(inode, 1);
1512 else
Zach Brown8b558c52013-10-16 12:10:34 -07001513 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001514 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001515 } else if (ret == -EEXIST) {
1516 ret = 0;
1517 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001518 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001519 }
1520 iput(inode);
1521
1522 return ret;
1523}
1524
1525/*
1526 * when replaying the log for a directory, we only insert names
1527 * for inodes that actually exist. This means an fsync on a directory
1528 * does not implicitly fsync all the new files in it
1529 */
1530static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1531 struct btrfs_root *root,
1532 struct btrfs_path *path,
1533 u64 dirid, u64 index,
1534 char *name, int name_len, u8 type,
1535 struct btrfs_key *location)
1536{
1537 struct inode *inode;
1538 struct inode *dir;
1539 int ret;
1540
1541 inode = read_one_inode(root, location->objectid);
1542 if (!inode)
1543 return -ENOENT;
1544
1545 dir = read_one_inode(root, dirid);
1546 if (!dir) {
1547 iput(inode);
1548 return -EIO;
1549 }
Josef Bacikd5554382013-09-11 14:17:00 -04001550
Chris Masone02119d2008-09-05 16:13:11 -04001551 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1552
1553 /* FIXME, put inode into FIXUP list */
1554
1555 iput(inode);
1556 iput(dir);
1557 return ret;
1558}
1559
1560/*
Filipe Mananadf8d1162015-01-14 01:52:25 +00001561 * Return true if an inode reference exists in the log for the given name,
1562 * inode and parent inode.
1563 */
1564static bool name_in_log_ref(struct btrfs_root *log_root,
1565 const char *name, const int name_len,
1566 const u64 dirid, const u64 ino)
1567{
1568 struct btrfs_key search_key;
1569
1570 search_key.objectid = ino;
1571 search_key.type = BTRFS_INODE_REF_KEY;
1572 search_key.offset = dirid;
1573 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1574 return true;
1575
1576 search_key.type = BTRFS_INODE_EXTREF_KEY;
1577 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1578 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1579 return true;
1580
1581 return false;
1582}
1583
1584/*
Chris Masone02119d2008-09-05 16:13:11 -04001585 * take a single entry in a log directory item and replay it into
1586 * the subvolume.
1587 *
1588 * if a conflicting item exists in the subdirectory already,
1589 * the inode it points to is unlinked and put into the link count
1590 * fix up tree.
1591 *
1592 * If a name from the log points to a file or directory that does
1593 * not exist in the FS, it is skipped. fsyncs on directories
1594 * do not force down inodes inside that directory, just changes to the
1595 * names or unlinks in a directory.
1596 */
1597static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1598 struct btrfs_root *root,
1599 struct btrfs_path *path,
1600 struct extent_buffer *eb,
1601 struct btrfs_dir_item *di,
1602 struct btrfs_key *key)
1603{
1604 char *name;
1605 int name_len;
1606 struct btrfs_dir_item *dst_di;
1607 struct btrfs_key found_key;
1608 struct btrfs_key log_key;
1609 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001610 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001611 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001612 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001613 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Chris Masone02119d2008-09-05 16:13:11 -04001614
1615 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001616 if (!dir)
1617 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001618
1619 name_len = btrfs_dir_name_len(eb, di);
1620 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001621 if (!name) {
1622 ret = -ENOMEM;
1623 goto out;
1624 }
liubo2a29edc2011-01-26 06:22:08 +00001625
Chris Masone02119d2008-09-05 16:13:11 -04001626 log_type = btrfs_dir_type(eb, di);
1627 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1628 name_len);
1629
1630 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001631 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1632 if (exists == 0)
1633 exists = 1;
1634 else
1635 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001636 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001637
Chris Masone02119d2008-09-05 16:13:11 -04001638 if (key->type == BTRFS_DIR_ITEM_KEY) {
1639 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1640 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001641 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001642 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1643 key->objectid,
1644 key->offset, name,
1645 name_len, 1);
1646 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001647 /* Corruption */
1648 ret = -EINVAL;
1649 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001650 }
David Sterbac7040052011-04-19 18:00:01 +02001651 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001652 /* we need a sequence number to insert, so we only
1653 * do inserts for the BTRFS_DIR_INDEX_KEY types
1654 */
1655 if (key->type != BTRFS_DIR_INDEX_KEY)
1656 goto out;
1657 goto insert;
1658 }
1659
1660 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1661 /* the existing item matches the logged item */
1662 if (found_key.objectid == log_key.objectid &&
1663 found_key.type == log_key.type &&
1664 found_key.offset == log_key.offset &&
1665 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
Filipe Mananaa2cc11d2014-09-08 22:53:18 +01001666 update_size = false;
Chris Masone02119d2008-09-05 16:13:11 -04001667 goto out;
1668 }
1669
1670 /*
1671 * don't drop the conflicting directory entry if the inode
1672 * for the new entry doesn't exist
1673 */
Chris Mason4bef0842008-09-08 11:18:08 -04001674 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001675 goto out;
1676
Chris Masone02119d2008-09-05 16:13:11 -04001677 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001678 if (ret)
1679 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001680
1681 if (key->type == BTRFS_DIR_INDEX_KEY)
1682 goto insert;
1683out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001684 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001685 if (!ret && update_size) {
1686 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1687 ret = btrfs_update_inode(trans, root, dir);
1688 }
Chris Masone02119d2008-09-05 16:13:11 -04001689 kfree(name);
1690 iput(dir);
Josef Bacik36508602013-04-25 16:23:32 -04001691 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001692
1693insert:
Filipe Mananadf8d1162015-01-14 01:52:25 +00001694 if (name_in_log_ref(root->log_root, name, name_len,
1695 key->objectid, log_key.objectid)) {
1696 /* The dentry will be added later. */
1697 ret = 0;
1698 update_size = false;
1699 goto out;
1700 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001701 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001702 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1703 name, name_len, log_type, &log_key);
Filipe Mananadf8d1162015-01-14 01:52:25 +00001704 if (ret && ret != -ENOENT && ret != -EEXIST)
Josef Bacik36508602013-04-25 16:23:32 -04001705 goto out;
Josef Bacikd5554382013-09-11 14:17:00 -04001706 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001707 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001708 goto out;
1709}
1710
1711/*
1712 * find all the names in a directory item and reconcile them into
1713 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1714 * one name in a directory item, but the same code gets used for
1715 * both directory index types
1716 */
1717static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1718 struct btrfs_root *root,
1719 struct btrfs_path *path,
1720 struct extent_buffer *eb, int slot,
1721 struct btrfs_key *key)
1722{
1723 int ret;
1724 u32 item_size = btrfs_item_size_nr(eb, slot);
1725 struct btrfs_dir_item *di;
1726 int name_len;
1727 unsigned long ptr;
1728 unsigned long ptr_end;
1729
1730 ptr = btrfs_item_ptr_offset(eb, slot);
1731 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001732 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001733 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001734 if (verify_dir_item(root, eb, di))
1735 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001736 name_len = btrfs_dir_name_len(eb, di);
1737 ret = replay_one_name(trans, root, path, eb, di, key);
Josef Bacik36508602013-04-25 16:23:32 -04001738 if (ret)
1739 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001740 ptr = (unsigned long)(di + 1);
1741 ptr += name_len;
1742 }
1743 return 0;
1744}
1745
1746/*
1747 * directory replay has two parts. There are the standard directory
1748 * items in the log copied from the subvolume, and range items
1749 * created in the log while the subvolume was logged.
1750 *
1751 * The range items tell us which parts of the key space the log
1752 * is authoritative for. During replay, if a key in the subvolume
1753 * directory is in a logged range item, but not actually in the log
1754 * that means it was deleted from the directory before the fsync
1755 * and should be removed.
1756 */
1757static noinline int find_dir_range(struct btrfs_root *root,
1758 struct btrfs_path *path,
1759 u64 dirid, int key_type,
1760 u64 *start_ret, u64 *end_ret)
1761{
1762 struct btrfs_key key;
1763 u64 found_end;
1764 struct btrfs_dir_log_item *item;
1765 int ret;
1766 int nritems;
1767
1768 if (*start_ret == (u64)-1)
1769 return 1;
1770
1771 key.objectid = dirid;
1772 key.type = key_type;
1773 key.offset = *start_ret;
1774
1775 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1776 if (ret < 0)
1777 goto out;
1778 if (ret > 0) {
1779 if (path->slots[0] == 0)
1780 goto out;
1781 path->slots[0]--;
1782 }
1783 if (ret != 0)
1784 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1785
1786 if (key.type != key_type || key.objectid != dirid) {
1787 ret = 1;
1788 goto next;
1789 }
1790 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1791 struct btrfs_dir_log_item);
1792 found_end = btrfs_dir_log_end(path->nodes[0], item);
1793
1794 if (*start_ret >= key.offset && *start_ret <= found_end) {
1795 ret = 0;
1796 *start_ret = key.offset;
1797 *end_ret = found_end;
1798 goto out;
1799 }
1800 ret = 1;
1801next:
1802 /* check the next slot in the tree to see if it is a valid item */
1803 nritems = btrfs_header_nritems(path->nodes[0]);
1804 if (path->slots[0] >= nritems) {
1805 ret = btrfs_next_leaf(root, path);
1806 if (ret)
1807 goto out;
1808 } else {
1809 path->slots[0]++;
1810 }
1811
1812 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1813
1814 if (key.type != key_type || key.objectid != dirid) {
1815 ret = 1;
1816 goto out;
1817 }
1818 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1819 struct btrfs_dir_log_item);
1820 found_end = btrfs_dir_log_end(path->nodes[0], item);
1821 *start_ret = key.offset;
1822 *end_ret = found_end;
1823 ret = 0;
1824out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001825 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001826 return ret;
1827}
1828
1829/*
1830 * this looks for a given directory item in the log. If the directory
1831 * item is not in the log, the item is removed and the inode it points
1832 * to is unlinked
1833 */
1834static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1835 struct btrfs_root *root,
1836 struct btrfs_root *log,
1837 struct btrfs_path *path,
1838 struct btrfs_path *log_path,
1839 struct inode *dir,
1840 struct btrfs_key *dir_key)
1841{
1842 int ret;
1843 struct extent_buffer *eb;
1844 int slot;
1845 u32 item_size;
1846 struct btrfs_dir_item *di;
1847 struct btrfs_dir_item *log_di;
1848 int name_len;
1849 unsigned long ptr;
1850 unsigned long ptr_end;
1851 char *name;
1852 struct inode *inode;
1853 struct btrfs_key location;
1854
1855again:
1856 eb = path->nodes[0];
1857 slot = path->slots[0];
1858 item_size = btrfs_item_size_nr(eb, slot);
1859 ptr = btrfs_item_ptr_offset(eb, slot);
1860 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001861 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001862 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001863 if (verify_dir_item(root, eb, di)) {
1864 ret = -EIO;
1865 goto out;
1866 }
1867
Chris Masone02119d2008-09-05 16:13:11 -04001868 name_len = btrfs_dir_name_len(eb, di);
1869 name = kmalloc(name_len, GFP_NOFS);
1870 if (!name) {
1871 ret = -ENOMEM;
1872 goto out;
1873 }
1874 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1875 name_len);
1876 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001877 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001878 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1879 dir_key->objectid,
1880 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001881 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001882 log_di = btrfs_lookup_dir_index_item(trans, log,
1883 log_path,
1884 dir_key->objectid,
1885 dir_key->offset,
1886 name, name_len, 0);
1887 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001888 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04001889 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001890 btrfs_release_path(path);
1891 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001892 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001893 if (!inode) {
1894 kfree(name);
1895 return -EIO;
1896 }
Chris Masone02119d2008-09-05 16:13:11 -04001897
1898 ret = link_to_fixup_dir(trans, root,
1899 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04001900 if (ret) {
1901 kfree(name);
1902 iput(inode);
1903 goto out;
1904 }
1905
Zach Brown8b558c52013-10-16 12:10:34 -07001906 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001907 ret = btrfs_unlink_inode(trans, root, dir, inode,
1908 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04001909 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001910 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001911 kfree(name);
1912 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001913 if (ret)
1914 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001915
1916 /* there might still be more names under this key
1917 * check and repeat if required
1918 */
1919 ret = btrfs_search_slot(NULL, root, dir_key, path,
1920 0, 0);
1921 if (ret == 0)
1922 goto again;
1923 ret = 0;
1924 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001925 } else if (IS_ERR(log_di)) {
1926 kfree(name);
1927 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04001928 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001929 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001930 kfree(name);
1931
1932 ptr = (unsigned long)(di + 1);
1933 ptr += name_len;
1934 }
1935 ret = 0;
1936out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001937 btrfs_release_path(path);
1938 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001939 return ret;
1940}
1941
1942/*
1943 * deletion replay happens before we copy any new directory items
1944 * out of the log or out of backreferences from inodes. It
1945 * scans the log to find ranges of keys that log is authoritative for,
1946 * and then scans the directory to find items in those ranges that are
1947 * not present in the log.
1948 *
1949 * Anything we don't find in the log is unlinked and removed from the
1950 * directory.
1951 */
1952static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1953 struct btrfs_root *root,
1954 struct btrfs_root *log,
1955 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001956 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001957{
1958 u64 range_start;
1959 u64 range_end;
1960 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1961 int ret = 0;
1962 struct btrfs_key dir_key;
1963 struct btrfs_key found_key;
1964 struct btrfs_path *log_path;
1965 struct inode *dir;
1966
1967 dir_key.objectid = dirid;
1968 dir_key.type = BTRFS_DIR_ITEM_KEY;
1969 log_path = btrfs_alloc_path();
1970 if (!log_path)
1971 return -ENOMEM;
1972
1973 dir = read_one_inode(root, dirid);
1974 /* it isn't an error if the inode isn't there, that can happen
1975 * because we replay the deletes before we copy in the inode item
1976 * from the log
1977 */
1978 if (!dir) {
1979 btrfs_free_path(log_path);
1980 return 0;
1981 }
1982again:
1983 range_start = 0;
1984 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001985 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001986 if (del_all)
1987 range_end = (u64)-1;
1988 else {
1989 ret = find_dir_range(log, path, dirid, key_type,
1990 &range_start, &range_end);
1991 if (ret != 0)
1992 break;
1993 }
Chris Masone02119d2008-09-05 16:13:11 -04001994
1995 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001996 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001997 int nritems;
1998 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1999 0, 0);
2000 if (ret < 0)
2001 goto out;
2002
2003 nritems = btrfs_header_nritems(path->nodes[0]);
2004 if (path->slots[0] >= nritems) {
2005 ret = btrfs_next_leaf(root, path);
2006 if (ret)
2007 break;
2008 }
2009 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2010 path->slots[0]);
2011 if (found_key.objectid != dirid ||
2012 found_key.type != dir_key.type)
2013 goto next_type;
2014
2015 if (found_key.offset > range_end)
2016 break;
2017
2018 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04002019 log_path, dir,
2020 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04002021 if (ret)
2022 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002023 if (found_key.offset == (u64)-1)
2024 break;
2025 dir_key.offset = found_key.offset + 1;
2026 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002027 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002028 if (range_end == (u64)-1)
2029 break;
2030 range_start = range_end + 1;
2031 }
2032
2033next_type:
2034 ret = 0;
2035 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2036 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2037 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002038 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002039 goto again;
2040 }
2041out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002042 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002043 btrfs_free_path(log_path);
2044 iput(dir);
2045 return ret;
2046}
2047
2048/*
2049 * the process_func used to replay items from the log tree. This
2050 * gets called in two different stages. The first stage just looks
2051 * for inodes and makes sure they are all copied into the subvolume.
2052 *
2053 * The second stage copies all the other item types from the log into
2054 * the subvolume. The two stage approach is slower, but gets rid of
2055 * lots of complexity around inodes referencing other inodes that exist
2056 * only in the log (references come from either directory items or inode
2057 * back refs).
2058 */
2059static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2060 struct walk_control *wc, u64 gen)
2061{
2062 int nritems;
2063 struct btrfs_path *path;
2064 struct btrfs_root *root = wc->replay_dest;
2065 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002066 int level;
2067 int i;
2068 int ret;
2069
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002070 ret = btrfs_read_buffer(eb, gen);
2071 if (ret)
2072 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002073
2074 level = btrfs_header_level(eb);
2075
2076 if (level != 0)
2077 return 0;
2078
2079 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002080 if (!path)
2081 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002082
2083 nritems = btrfs_header_nritems(eb);
2084 for (i = 0; i < nritems; i++) {
2085 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002086
2087 /* inode keys are done during the first stage */
2088 if (key.type == BTRFS_INODE_ITEM_KEY &&
2089 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002090 struct btrfs_inode_item *inode_item;
2091 u32 mode;
2092
2093 inode_item = btrfs_item_ptr(eb, i,
2094 struct btrfs_inode_item);
2095 mode = btrfs_inode_mode(eb, inode_item);
2096 if (S_ISDIR(mode)) {
2097 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002098 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002099 if (ret)
2100 break;
Chris Masone02119d2008-09-05 16:13:11 -04002101 }
2102 ret = overwrite_item(wc->trans, root, path,
2103 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002104 if (ret)
2105 break;
Chris Masone02119d2008-09-05 16:13:11 -04002106
Yan, Zhengc71bf092009-11-12 09:34:40 +00002107 /* for regular files, make sure corresponding
2108 * orhpan item exist. extents past the new EOF
2109 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002110 */
2111 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002112 ret = insert_orphan_item(wc->trans, root,
2113 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002114 if (ret)
2115 break;
Chris Masone02119d2008-09-05 16:13:11 -04002116 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002117
Chris Masone02119d2008-09-05 16:13:11 -04002118 ret = link_to_fixup_dir(wc->trans, root,
2119 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002120 if (ret)
2121 break;
Chris Masone02119d2008-09-05 16:13:11 -04002122 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002123
2124 if (key.type == BTRFS_DIR_INDEX_KEY &&
2125 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2126 ret = replay_one_dir_item(wc->trans, root, path,
2127 eb, i, &key);
2128 if (ret)
2129 break;
2130 }
2131
Chris Masone02119d2008-09-05 16:13:11 -04002132 if (wc->stage < LOG_WALK_REPLAY_ALL)
2133 continue;
2134
2135 /* these keys are simply copied */
2136 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2137 ret = overwrite_item(wc->trans, root, path,
2138 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002139 if (ret)
2140 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002141 } else if (key.type == BTRFS_INODE_REF_KEY ||
2142 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002143 ret = add_inode_ref(wc->trans, root, log, path,
2144 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002145 if (ret && ret != -ENOENT)
2146 break;
2147 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002148 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2149 ret = replay_one_extent(wc->trans, root, path,
2150 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002151 if (ret)
2152 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002153 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002154 ret = replay_one_dir_item(wc->trans, root, path,
2155 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002156 if (ret)
2157 break;
Chris Masone02119d2008-09-05 16:13:11 -04002158 }
2159 }
2160 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002161 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002162}
2163
Chris Masond3977122009-01-05 21:25:51 -05002164static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002165 struct btrfs_root *root,
2166 struct btrfs_path *path, int *level,
2167 struct walk_control *wc)
2168{
2169 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002170 u64 bytenr;
2171 u64 ptr_gen;
2172 struct extent_buffer *next;
2173 struct extent_buffer *cur;
2174 struct extent_buffer *parent;
2175 u32 blocksize;
2176 int ret = 0;
2177
2178 WARN_ON(*level < 0);
2179 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2180
Chris Masond3977122009-01-05 21:25:51 -05002181 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002182 WARN_ON(*level < 0);
2183 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2184 cur = path->nodes[*level];
2185
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302186 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002187
2188 if (path->slots[*level] >=
2189 btrfs_header_nritems(cur))
2190 break;
2191
2192 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2193 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
David Sterba707e8a02014-06-04 19:22:26 +02002194 blocksize = root->nodesize;
Chris Masone02119d2008-09-05 16:13:11 -04002195
2196 parent = path->nodes[*level];
2197 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002198
David Sterbaa83fffb2014-06-15 02:39:54 +02002199 next = btrfs_find_create_tree_block(root, bytenr);
liubo2a29edc2011-01-26 06:22:08 +00002200 if (!next)
2201 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002202
Chris Masone02119d2008-09-05 16:13:11 -04002203 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002204 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002205 if (ret) {
2206 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002207 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002208 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002209
Chris Masone02119d2008-09-05 16:13:11 -04002210 path->slots[*level]++;
2211 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002212 ret = btrfs_read_buffer(next, ptr_gen);
2213 if (ret) {
2214 free_extent_buffer(next);
2215 return ret;
2216 }
Chris Masone02119d2008-09-05 16:13:11 -04002217
Josef Bacik681ae502013-10-07 15:11:00 -04002218 if (trans) {
2219 btrfs_tree_lock(next);
2220 btrfs_set_lock_blocking(next);
2221 clean_tree_block(trans, root, next);
2222 btrfs_wait_tree_block_writeback(next);
2223 btrfs_tree_unlock(next);
2224 }
Chris Masone02119d2008-09-05 16:13:11 -04002225
Chris Masone02119d2008-09-05 16:13:11 -04002226 WARN_ON(root_owner !=
2227 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002228 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002229 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002230 if (ret) {
2231 free_extent_buffer(next);
2232 return ret;
2233 }
Chris Masone02119d2008-09-05 16:13:11 -04002234 }
2235 free_extent_buffer(next);
2236 continue;
2237 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002238 ret = btrfs_read_buffer(next, ptr_gen);
2239 if (ret) {
2240 free_extent_buffer(next);
2241 return ret;
2242 }
Chris Masone02119d2008-09-05 16:13:11 -04002243
2244 WARN_ON(*level <= 0);
2245 if (path->nodes[*level-1])
2246 free_extent_buffer(path->nodes[*level-1]);
2247 path->nodes[*level-1] = next;
2248 *level = btrfs_header_level(next);
2249 path->slots[*level] = 0;
2250 cond_resched();
2251 }
2252 WARN_ON(*level < 0);
2253 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2254
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002255 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002256
2257 cond_resched();
2258 return 0;
2259}
2260
Chris Masond3977122009-01-05 21:25:51 -05002261static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002262 struct btrfs_root *root,
2263 struct btrfs_path *path, int *level,
2264 struct walk_control *wc)
2265{
2266 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002267 int i;
2268 int slot;
2269 int ret;
2270
Chris Masond3977122009-01-05 21:25:51 -05002271 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002272 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002273 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002274 path->slots[i]++;
2275 *level = i;
2276 WARN_ON(*level == 0);
2277 return 0;
2278 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002279 struct extent_buffer *parent;
2280 if (path->nodes[*level] == root->node)
2281 parent = path->nodes[*level];
2282 else
2283 parent = path->nodes[*level + 1];
2284
2285 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002286 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002287 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002288 if (ret)
2289 return ret;
2290
Chris Masone02119d2008-09-05 16:13:11 -04002291 if (wc->free) {
2292 struct extent_buffer *next;
2293
2294 next = path->nodes[*level];
2295
Josef Bacik681ae502013-10-07 15:11:00 -04002296 if (trans) {
2297 btrfs_tree_lock(next);
2298 btrfs_set_lock_blocking(next);
2299 clean_tree_block(trans, root, next);
2300 btrfs_wait_tree_block_writeback(next);
2301 btrfs_tree_unlock(next);
2302 }
Chris Masone02119d2008-09-05 16:13:11 -04002303
Chris Masone02119d2008-09-05 16:13:11 -04002304 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002305 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002306 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002307 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002308 if (ret)
2309 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002310 }
2311 free_extent_buffer(path->nodes[*level]);
2312 path->nodes[*level] = NULL;
2313 *level = i + 1;
2314 }
2315 }
2316 return 1;
2317}
2318
2319/*
2320 * drop the reference count on the tree rooted at 'snap'. This traverses
2321 * the tree freeing any blocks that have a ref count of zero after being
2322 * decremented.
2323 */
2324static int walk_log_tree(struct btrfs_trans_handle *trans,
2325 struct btrfs_root *log, struct walk_control *wc)
2326{
2327 int ret = 0;
2328 int wret;
2329 int level;
2330 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002331 int orig_level;
2332
2333 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002334 if (!path)
2335 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002336
2337 level = btrfs_header_level(log->node);
2338 orig_level = level;
2339 path->nodes[level] = log->node;
2340 extent_buffer_get(log->node);
2341 path->slots[level] = 0;
2342
Chris Masond3977122009-01-05 21:25:51 -05002343 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002344 wret = walk_down_log_tree(trans, log, path, &level, wc);
2345 if (wret > 0)
2346 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002347 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002348 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002349 goto out;
2350 }
Chris Masone02119d2008-09-05 16:13:11 -04002351
2352 wret = walk_up_log_tree(trans, log, path, &level, wc);
2353 if (wret > 0)
2354 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002355 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002356 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002357 goto out;
2358 }
Chris Masone02119d2008-09-05 16:13:11 -04002359 }
2360
2361 /* was the root node processed? if not, catch it here */
2362 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002363 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002364 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002365 if (ret)
2366 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002367 if (wc->free) {
2368 struct extent_buffer *next;
2369
2370 next = path->nodes[orig_level];
2371
Josef Bacik681ae502013-10-07 15:11:00 -04002372 if (trans) {
2373 btrfs_tree_lock(next);
2374 btrfs_set_lock_blocking(next);
2375 clean_tree_block(trans, log, next);
2376 btrfs_wait_tree_block_writeback(next);
2377 btrfs_tree_unlock(next);
2378 }
Chris Masone02119d2008-09-05 16:13:11 -04002379
Chris Masone02119d2008-09-05 16:13:11 -04002380 WARN_ON(log->root_key.objectid !=
2381 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002382 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002383 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002384 if (ret)
2385 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002386 }
2387 }
2388
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002389out:
Chris Masone02119d2008-09-05 16:13:11 -04002390 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002391 return ret;
2392}
2393
Yan Zheng7237f182009-01-21 12:54:03 -05002394/*
2395 * helper function to update the item for a given subvolumes log root
2396 * in the tree of log roots
2397 */
2398static int update_log_root(struct btrfs_trans_handle *trans,
2399 struct btrfs_root *log)
2400{
2401 int ret;
2402
2403 if (log->log_transid == 1) {
2404 /* insert root item on the first sync */
2405 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2406 &log->root_key, &log->root_item);
2407 } else {
2408 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2409 &log->root_key, &log->root_item);
2410 }
2411 return ret;
2412}
2413
Miao Xie8b050d32014-02-20 18:08:58 +08002414static void wait_log_commit(struct btrfs_trans_handle *trans,
2415 struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002416{
2417 DEFINE_WAIT(wait);
Yan Zheng7237f182009-01-21 12:54:03 -05002418 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002419
Yan Zheng7237f182009-01-21 12:54:03 -05002420 /*
2421 * we only allow two pending log transactions at a time,
2422 * so we know that if ours is more than 2 older than the
2423 * current transaction, we're done
2424 */
Chris Masone02119d2008-09-05 16:13:11 -04002425 do {
Yan Zheng7237f182009-01-21 12:54:03 -05002426 prepare_to_wait(&root->log_commit_wait[index],
2427 &wait, TASK_UNINTERRUPTIBLE);
2428 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002429
Miao Xied1433de2014-02-20 18:08:59 +08002430 if (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002431 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002432 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002433
Yan Zheng7237f182009-01-21 12:54:03 -05002434 finish_wait(&root->log_commit_wait[index], &wait);
2435 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002436 } while (root->log_transid_committed < transid &&
Yan Zheng7237f182009-01-21 12:54:03 -05002437 atomic_read(&root->log_commit[index]));
Yan Zheng7237f182009-01-21 12:54:03 -05002438}
2439
Jeff Mahoney143bede2012-03-01 14:56:26 +01002440static void wait_for_writer(struct btrfs_trans_handle *trans,
2441 struct btrfs_root *root)
Yan Zheng7237f182009-01-21 12:54:03 -05002442{
2443 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002444
2445 while (atomic_read(&root->log_writers)) {
Yan Zheng7237f182009-01-21 12:54:03 -05002446 prepare_to_wait(&root->log_writer_wait,
2447 &wait, TASK_UNINTERRUPTIBLE);
2448 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002449 if (atomic_read(&root->log_writers))
Yan Zheng7237f182009-01-21 12:54:03 -05002450 schedule();
2451 mutex_lock(&root->log_mutex);
2452 finish_wait(&root->log_writer_wait, &wait);
2453 }
Chris Masone02119d2008-09-05 16:13:11 -04002454}
2455
Miao Xie8b050d32014-02-20 18:08:58 +08002456static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2457 struct btrfs_log_ctx *ctx)
2458{
2459 if (!ctx)
2460 return;
2461
2462 mutex_lock(&root->log_mutex);
2463 list_del_init(&ctx->list);
2464 mutex_unlock(&root->log_mutex);
2465}
2466
2467/*
2468 * Invoked in log mutex context, or be sure there is no other task which
2469 * can access the list.
2470 */
2471static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2472 int index, int error)
2473{
2474 struct btrfs_log_ctx *ctx;
2475
2476 if (!error) {
2477 INIT_LIST_HEAD(&root->log_ctxs[index]);
2478 return;
2479 }
2480
2481 list_for_each_entry(ctx, &root->log_ctxs[index], list)
2482 ctx->log_ret = error;
2483
2484 INIT_LIST_HEAD(&root->log_ctxs[index]);
2485}
2486
Chris Masone02119d2008-09-05 16:13:11 -04002487/*
2488 * btrfs_sync_log does sends a given tree log down to the disk and
2489 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002490 * you know that any inodes previously logged are safely on disk only
2491 * if it returns 0.
2492 *
2493 * Any other return value means you need to call btrfs_commit_transaction.
2494 * Some of the edge cases for fsyncing directories that have had unlinks
2495 * or renames done in the past mean that sometimes the only safe
2496 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2497 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002498 */
2499int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002500 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002501{
Yan Zheng7237f182009-01-21 12:54:03 -05002502 int index1;
2503 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002504 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002505 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002506 struct btrfs_root *log = root->log_root;
Yan Zheng7237f182009-01-21 12:54:03 -05002507 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002508 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002509 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002510 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002511
Yan Zheng7237f182009-01-21 12:54:03 -05002512 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002513 log_transid = ctx->log_transid;
2514 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f182009-01-21 12:54:03 -05002515 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002516 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002517 }
Miao Xied1433de2014-02-20 18:08:59 +08002518
2519 index1 = log_transid % 2;
2520 if (atomic_read(&root->log_commit[index1])) {
2521 wait_log_commit(trans, root, log_transid);
2522 mutex_unlock(&root->log_mutex);
2523 return ctx->log_ret;
2524 }
2525 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002526 atomic_set(&root->log_commit[index1], 1);
2527
2528 /* wait for previous tree log sync to complete */
2529 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Miao Xied1433de2014-02-20 18:08:59 +08002530 wait_log_commit(trans, root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002531
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002532 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002533 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002534 /* when we're on an ssd, just kick the log commit out */
Miao Xie27cdeb72014-04-02 19:51:05 +08002535 if (!btrfs_test_opt(root, SSD) &&
2536 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002537 mutex_unlock(&root->log_mutex);
2538 schedule_timeout_uninterruptible(1);
2539 mutex_lock(&root->log_mutex);
2540 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002541 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002542 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002543 break;
2544 }
Chris Masond0c803c2008-09-11 16:17:57 -04002545
Chris Mason12fcfd22009-03-24 10:24:20 -04002546 /* bail out if we need to do a full commit */
Miao Xie995946d2014-04-02 19:51:06 +08002547 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002548 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002549 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002550 mutex_unlock(&root->log_mutex);
2551 goto out;
2552 }
2553
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002554 if (log_transid % 2 == 0)
2555 mark = EXTENT_DIRTY;
2556 else
2557 mark = EXTENT_NEW;
2558
Chris Mason690587d2009-10-13 13:29:19 -04002559 /* we start IO on all the marked extents here, but we don't actually
2560 * wait for them until later.
2561 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002562 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002563 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002564 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002565 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002566 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002567 btrfs_free_logged_extents(log, log_transid);
Miao Xie995946d2014-04-02 19:51:06 +08002568 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002569 mutex_unlock(&root->log_mutex);
2570 goto out;
2571 }
Yan Zheng7237f182009-01-21 12:54:03 -05002572
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002573 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f182009-01-21 12:54:03 -05002574
Yan Zheng7237f182009-01-21 12:54:03 -05002575 root->log_transid++;
2576 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002577 root->log_start_pid = 0;
Yan Zheng7237f182009-01-21 12:54:03 -05002578 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002579 * IO has been started, blocks of the log tree have WRITTEN flag set
2580 * in their headers. new modifications of the log will be written to
2581 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f182009-01-21 12:54:03 -05002582 */
2583 mutex_unlock(&root->log_mutex);
2584
Miao Xied1433de2014-02-20 18:08:59 +08002585 btrfs_init_log_ctx(&root_log_ctx);
2586
Yan Zheng7237f182009-01-21 12:54:03 -05002587 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002588 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f182009-01-21 12:54:03 -05002589 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002590
2591 index2 = log_root_tree->log_transid % 2;
2592 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2593 root_log_ctx.log_transid = log_root_tree->log_transid;
2594
Yan Zheng7237f182009-01-21 12:54:03 -05002595 mutex_unlock(&log_root_tree->log_mutex);
2596
2597 ret = update_log_root(trans, log);
Yan Zheng7237f182009-01-21 12:54:03 -05002598
2599 mutex_lock(&log_root_tree->log_mutex);
2600 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2601 smp_mb();
2602 if (waitqueue_active(&log_root_tree->log_writer_wait))
2603 wake_up(&log_root_tree->log_writer_wait);
2604 }
2605
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002606 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002607 if (!list_empty(&root_log_ctx.list))
2608 list_del_init(&root_log_ctx.list);
2609
Miao Xiec6adc9c2013-05-28 10:05:39 +00002610 blk_finish_plug(&plug);
Miao Xie995946d2014-04-02 19:51:06 +08002611 btrfs_set_log_full_commit(root->fs_info, trans);
2612
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002613 if (ret != -ENOSPC) {
2614 btrfs_abort_transaction(trans, root, ret);
2615 mutex_unlock(&log_root_tree->log_mutex);
2616 goto out;
2617 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002618 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002619 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002620 mutex_unlock(&log_root_tree->log_mutex);
2621 ret = -EAGAIN;
2622 goto out;
2623 }
2624
Miao Xied1433de2014-02-20 18:08:59 +08002625 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
2626 mutex_unlock(&log_root_tree->log_mutex);
2627 ret = root_log_ctx.log_ret;
2628 goto out;
2629 }
Miao Xie8b050d32014-02-20 18:08:58 +08002630
Miao Xied1433de2014-02-20 18:08:59 +08002631 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f182009-01-21 12:54:03 -05002632 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002633 blk_finish_plug(&plug);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002634 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages,
2635 mark);
Josef Bacik50d9aa92014-11-21 14:52:38 -05002636 btrfs_wait_logged_extents(trans, log, log_transid);
Miao Xie8b050d32014-02-20 18:08:58 +08002637 wait_log_commit(trans, log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002638 root_log_ctx.log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002639 mutex_unlock(&log_root_tree->log_mutex);
Filipe Manana5ab5e442014-11-13 16:59:53 +00002640 if (!ret)
2641 ret = root_log_ctx.log_ret;
Yan Zheng7237f182009-01-21 12:54:03 -05002642 goto out;
2643 }
Miao Xied1433de2014-02-20 18:08:59 +08002644 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f182009-01-21 12:54:03 -05002645 atomic_set(&log_root_tree->log_commit[index2], 1);
2646
Chris Mason12fcfd22009-03-24 10:24:20 -04002647 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2648 wait_log_commit(trans, log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002649 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002650 }
Yan Zheng7237f182009-01-21 12:54:03 -05002651
Chris Mason12fcfd22009-03-24 10:24:20 -04002652 wait_for_writer(trans, log_root_tree);
2653
2654 /*
2655 * now that we've moved on to the tree of log tree roots,
2656 * check the full commit flag again
2657 */
Miao Xie995946d2014-04-02 19:51:06 +08002658 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002659 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002660 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002661 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002662 mutex_unlock(&log_root_tree->log_mutex);
2663 ret = -EAGAIN;
2664 goto out_wake_log_root;
2665 }
Yan Zheng7237f182009-01-21 12:54:03 -05002666
Miao Xiec6adc9c2013-05-28 10:05:39 +00002667 ret = btrfs_write_marked_extents(log_root_tree,
2668 &log_root_tree->dirty_log_pages,
2669 EXTENT_DIRTY | EXTENT_NEW);
2670 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002671 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002672 btrfs_set_log_full_commit(root->fs_info, trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002673 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002674 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002675 mutex_unlock(&log_root_tree->log_mutex);
2676 goto out_wake_log_root;
2677 }
Filipe Manana5ab5e442014-11-13 16:59:53 +00002678 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2679 if (!ret)
2680 ret = btrfs_wait_marked_extents(log_root_tree,
2681 &log_root_tree->dirty_log_pages,
2682 EXTENT_NEW | EXTENT_DIRTY);
2683 if (ret) {
2684 btrfs_set_log_full_commit(root->fs_info, trans);
2685 btrfs_free_logged_extents(log, log_transid);
2686 mutex_unlock(&log_root_tree->log_mutex);
2687 goto out_wake_log_root;
2688 }
Josef Bacik50d9aa92014-11-21 14:52:38 -05002689 btrfs_wait_logged_extents(trans, log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002690
David Sterba6c417612011-04-13 15:41:04 +02002691 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002692 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002693 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f182009-01-21 12:54:03 -05002694 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002695
Yan Zheng7237f182009-01-21 12:54:03 -05002696 log_root_tree->log_transid++;
Yan Zheng7237f182009-01-21 12:54:03 -05002697 mutex_unlock(&log_root_tree->log_mutex);
2698
2699 /*
2700 * nobody else is going to jump in and write the the ctree
2701 * super here because the log_commit atomic below is protecting
2702 * us. We must be called with a transaction handle pinning
2703 * the running transaction open, so a full commit can't hop
2704 * in and cause problems either.
2705 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002706 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002707 if (ret) {
Miao Xie995946d2014-04-02 19:51:06 +08002708 btrfs_set_log_full_commit(root->fs_info, trans);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002709 btrfs_abort_transaction(trans, root, ret);
2710 goto out_wake_log_root;
2711 }
Yan Zheng7237f182009-01-21 12:54:03 -05002712
Chris Mason257c62e2009-10-13 13:21:08 -04002713 mutex_lock(&root->log_mutex);
2714 if (root->last_log_commit < log_transid)
2715 root->last_log_commit = log_transid;
2716 mutex_unlock(&root->log_mutex);
2717
Chris Mason12fcfd22009-03-24 10:24:20 -04002718out_wake_log_root:
Miao Xie8b050d32014-02-20 18:08:58 +08002719 /*
2720 * We needn't get log_mutex here because we are sure all
2721 * the other tasks are blocked.
2722 */
2723 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2724
Miao Xied1433de2014-02-20 18:08:59 +08002725 mutex_lock(&log_root_tree->log_mutex);
2726 log_root_tree->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002727 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002728 mutex_unlock(&log_root_tree->log_mutex);
2729
Yan Zheng7237f182009-01-21 12:54:03 -05002730 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2731 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002732out:
Miao Xie8b050d32014-02-20 18:08:58 +08002733 /* See above. */
2734 btrfs_remove_all_log_ctxs(root, index1, ret);
2735
Miao Xied1433de2014-02-20 18:08:59 +08002736 mutex_lock(&root->log_mutex);
2737 root->log_transid_committed++;
Yan Zheng7237f182009-01-21 12:54:03 -05002738 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002739 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002740
Yan Zheng7237f182009-01-21 12:54:03 -05002741 if (waitqueue_active(&root->log_commit_wait[index1]))
2742 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002743 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002744}
2745
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002746static void free_log_tree(struct btrfs_trans_handle *trans,
2747 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002748{
2749 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002750 u64 start;
2751 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002752 struct walk_control wc = {
2753 .free = 1,
2754 .process_func = process_one_buffer
2755 };
2756
Josef Bacik681ae502013-10-07 15:11:00 -04002757 ret = walk_log_tree(trans, log, &wc);
2758 /* I don't think this can happen but just in case */
2759 if (ret)
2760 btrfs_abort_transaction(trans, log, ret);
Chris Masone02119d2008-09-05 16:13:11 -04002761
Chris Masond3977122009-01-05 21:25:51 -05002762 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002763 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002764 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2765 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002766 if (ret)
2767 break;
2768
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002769 clear_extent_bits(&log->dirty_log_pages, start, end,
2770 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002771 }
2772
Josef Bacik2ab28f32012-10-12 15:27:49 -04002773 /*
2774 * We may have short-circuited the log tree with the full commit logic
2775 * and left ordered extents on our list, so clear these out to keep us
2776 * from leaking inodes and memory.
2777 */
2778 btrfs_free_logged_extents(log, 0);
2779 btrfs_free_logged_extents(log, 1);
2780
Yan Zheng7237f182009-01-21 12:54:03 -05002781 free_extent_buffer(log->node);
2782 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002783}
2784
2785/*
2786 * free all the extents used by the tree log. This should be called
2787 * at commit time of the full transaction
2788 */
2789int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2790{
2791 if (root->log_root) {
2792 free_log_tree(trans, root->log_root);
2793 root->log_root = NULL;
2794 }
2795 return 0;
2796}
2797
2798int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2799 struct btrfs_fs_info *fs_info)
2800{
2801 if (fs_info->log_root_tree) {
2802 free_log_tree(trans, fs_info->log_root_tree);
2803 fs_info->log_root_tree = NULL;
2804 }
Chris Masone02119d2008-09-05 16:13:11 -04002805 return 0;
2806}
2807
2808/*
Chris Masone02119d2008-09-05 16:13:11 -04002809 * If both a file and directory are logged, and unlinks or renames are
2810 * mixed in, we have a few interesting corners:
2811 *
2812 * create file X in dir Y
2813 * link file X to X.link in dir Y
2814 * fsync file X
2815 * unlink file X but leave X.link
2816 * fsync dir Y
2817 *
2818 * After a crash we would expect only X.link to exist. But file X
2819 * didn't get fsync'd again so the log has back refs for X and X.link.
2820 *
2821 * We solve this by removing directory entries and inode backrefs from the
2822 * log when a file that was logged in the current transaction is
2823 * unlinked. Any later fsync will include the updated log entries, and
2824 * we'll be able to reconstruct the proper directory items from backrefs.
2825 *
2826 * This optimizations allows us to avoid relogging the entire inode
2827 * or the entire directory.
2828 */
2829int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2830 struct btrfs_root *root,
2831 const char *name, int name_len,
2832 struct inode *dir, u64 index)
2833{
2834 struct btrfs_root *log;
2835 struct btrfs_dir_item *di;
2836 struct btrfs_path *path;
2837 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002838 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002839 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002840 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002841
Chris Mason3a5f1d42008-09-11 15:53:37 -04002842 if (BTRFS_I(dir)->logged_trans < trans->transid)
2843 return 0;
2844
Chris Masone02119d2008-09-05 16:13:11 -04002845 ret = join_running_log_trans(root);
2846 if (ret)
2847 return 0;
2848
2849 mutex_lock(&BTRFS_I(dir)->log_mutex);
2850
2851 log = root->log_root;
2852 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002853 if (!path) {
2854 err = -ENOMEM;
2855 goto out_unlock;
2856 }
liubo2a29edc2011-01-26 06:22:08 +00002857
Li Zefan33345d012011-04-20 10:31:50 +08002858 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002859 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002860 if (IS_ERR(di)) {
2861 err = PTR_ERR(di);
2862 goto fail;
2863 }
2864 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002865 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2866 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002867 if (ret) {
2868 err = ret;
2869 goto fail;
2870 }
Chris Masone02119d2008-09-05 16:13:11 -04002871 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002872 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002873 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002874 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002875 if (IS_ERR(di)) {
2876 err = PTR_ERR(di);
2877 goto fail;
2878 }
2879 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002880 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2881 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002882 if (ret) {
2883 err = ret;
2884 goto fail;
2885 }
Chris Masone02119d2008-09-05 16:13:11 -04002886 }
2887
2888 /* update the directory size in the log to reflect the names
2889 * we have removed
2890 */
2891 if (bytes_del) {
2892 struct btrfs_key key;
2893
Li Zefan33345d012011-04-20 10:31:50 +08002894 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002895 key.offset = 0;
2896 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002897 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002898
2899 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002900 if (ret < 0) {
2901 err = ret;
2902 goto fail;
2903 }
Chris Masone02119d2008-09-05 16:13:11 -04002904 if (ret == 0) {
2905 struct btrfs_inode_item *item;
2906 u64 i_size;
2907
2908 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2909 struct btrfs_inode_item);
2910 i_size = btrfs_inode_size(path->nodes[0], item);
2911 if (i_size > bytes_del)
2912 i_size -= bytes_del;
2913 else
2914 i_size = 0;
2915 btrfs_set_inode_size(path->nodes[0], item, i_size);
2916 btrfs_mark_buffer_dirty(path->nodes[0]);
2917 } else
2918 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002919 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002920 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002921fail:
Chris Masone02119d2008-09-05 16:13:11 -04002922 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002923out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002924 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002925 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08002926 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002927 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002928 } else if (ret < 0)
2929 btrfs_abort_transaction(trans, root, ret);
2930
Chris Mason12fcfd22009-03-24 10:24:20 -04002931 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002932
Andi Kleen411fc6b2010-10-29 15:14:31 -04002933 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002934}
2935
2936/* see comments for btrfs_del_dir_entries_in_log */
2937int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2938 struct btrfs_root *root,
2939 const char *name, int name_len,
2940 struct inode *inode, u64 dirid)
2941{
2942 struct btrfs_root *log;
2943 u64 index;
2944 int ret;
2945
Chris Mason3a5f1d42008-09-11 15:53:37 -04002946 if (BTRFS_I(inode)->logged_trans < trans->transid)
2947 return 0;
2948
Chris Masone02119d2008-09-05 16:13:11 -04002949 ret = join_running_log_trans(root);
2950 if (ret)
2951 return 0;
2952 log = root->log_root;
2953 mutex_lock(&BTRFS_I(inode)->log_mutex);
2954
Li Zefan33345d012011-04-20 10:31:50 +08002955 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002956 dirid, &index);
2957 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002958 if (ret == -ENOSPC) {
Miao Xie995946d2014-04-02 19:51:06 +08002959 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002960 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002961 } else if (ret < 0 && ret != -ENOENT)
2962 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002963 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002964
Chris Masone02119d2008-09-05 16:13:11 -04002965 return ret;
2966}
2967
2968/*
2969 * creates a range item in the log for 'dirid'. first_offset and
2970 * last_offset tell us which parts of the key space the log should
2971 * be considered authoritative for.
2972 */
2973static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2974 struct btrfs_root *log,
2975 struct btrfs_path *path,
2976 int key_type, u64 dirid,
2977 u64 first_offset, u64 last_offset)
2978{
2979 int ret;
2980 struct btrfs_key key;
2981 struct btrfs_dir_log_item *item;
2982
2983 key.objectid = dirid;
2984 key.offset = first_offset;
2985 if (key_type == BTRFS_DIR_ITEM_KEY)
2986 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2987 else
2988 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2989 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002990 if (ret)
2991 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002992
2993 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2994 struct btrfs_dir_log_item);
2995 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2996 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002997 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002998 return 0;
2999}
3000
3001/*
3002 * log all the items included in the current transaction for a given
3003 * directory. This also creates the range items in the log tree required
3004 * to replay anything deleted before the fsync
3005 */
3006static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3007 struct btrfs_root *root, struct inode *inode,
3008 struct btrfs_path *path,
3009 struct btrfs_path *dst_path, int key_type,
3010 u64 min_offset, u64 *last_offset_ret)
3011{
3012 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04003013 struct btrfs_root *log = root->log_root;
3014 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003015 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003016 int ret;
3017 int i;
3018 int nritems;
3019 u64 first_offset = min_offset;
3020 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08003021 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003022
3023 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04003024
Li Zefan33345d012011-04-20 10:31:50 +08003025 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003026 min_key.type = key_type;
3027 min_key.offset = min_offset;
3028
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003029 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003030
3031 /*
3032 * we didn't find anything from this transaction, see if there
3033 * is anything at all
3034 */
Li Zefan33345d012011-04-20 10:31:50 +08003035 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3036 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003037 min_key.type = key_type;
3038 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02003039 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003040 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3041 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003042 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003043 return ret;
3044 }
Li Zefan33345d012011-04-20 10:31:50 +08003045 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003046
3047 /* if ret == 0 there are items for this type,
3048 * create a range to tell us the last key of this type.
3049 * otherwise, there are no items in this directory after
3050 * *min_offset, and we create a range to indicate that.
3051 */
3052 if (ret == 0) {
3053 struct btrfs_key tmp;
3054 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3055 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003056 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003057 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003058 }
3059 goto done;
3060 }
3061
3062 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003063 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003064 if (ret == 0) {
3065 struct btrfs_key tmp;
3066 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3067 if (key_type == tmp.type) {
3068 first_offset = tmp.offset;
3069 ret = overwrite_item(trans, log, dst_path,
3070 path->nodes[0], path->slots[0],
3071 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003072 if (ret) {
3073 err = ret;
3074 goto done;
3075 }
Chris Masone02119d2008-09-05 16:13:11 -04003076 }
3077 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003078 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003079
3080 /* find the first key from this transaction again */
3081 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303082 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003083 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003084
3085 /*
3086 * we have a block from this transaction, log every item in it
3087 * from our directory
3088 */
Chris Masond3977122009-01-05 21:25:51 -05003089 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003090 struct btrfs_key tmp;
3091 src = path->nodes[0];
3092 nritems = btrfs_header_nritems(src);
3093 for (i = path->slots[0]; i < nritems; i++) {
3094 btrfs_item_key_to_cpu(src, &min_key, i);
3095
Li Zefan33345d012011-04-20 10:31:50 +08003096 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003097 goto done;
3098 ret = overwrite_item(trans, log, dst_path, src, i,
3099 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003100 if (ret) {
3101 err = ret;
3102 goto done;
3103 }
Chris Masone02119d2008-09-05 16:13:11 -04003104 }
3105 path->slots[0] = nritems;
3106
3107 /*
3108 * look ahead to the next item and see if it is also
3109 * from this directory and from this transaction
3110 */
3111 ret = btrfs_next_leaf(root, path);
3112 if (ret == 1) {
3113 last_offset = (u64)-1;
3114 goto done;
3115 }
3116 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003117 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003118 last_offset = (u64)-1;
3119 goto done;
3120 }
3121 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3122 ret = overwrite_item(trans, log, dst_path,
3123 path->nodes[0], path->slots[0],
3124 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003125 if (ret)
3126 err = ret;
3127 else
3128 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003129 goto done;
3130 }
3131 }
3132done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003133 btrfs_release_path(path);
3134 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003135
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003136 if (err == 0) {
3137 *last_offset_ret = last_offset;
3138 /*
3139 * insert the log range keys to indicate where the log
3140 * is valid
3141 */
3142 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003143 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003144 if (ret)
3145 err = ret;
3146 }
3147 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003148}
3149
3150/*
3151 * logging directories is very similar to logging inodes, We find all the items
3152 * from the current transaction and write them to the log.
3153 *
3154 * The recovery code scans the directory in the subvolume, and if it finds a
3155 * key in the range logged that is not present in the log tree, then it means
3156 * that dir entry was unlinked during the transaction.
3157 *
3158 * In order for that scan to work, we must include one key smaller than
3159 * the smallest logged by this transaction and one key larger than the largest
3160 * key logged by this transaction.
3161 */
3162static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3163 struct btrfs_root *root, struct inode *inode,
3164 struct btrfs_path *path,
3165 struct btrfs_path *dst_path)
3166{
3167 u64 min_key;
3168 u64 max_key;
3169 int ret;
3170 int key_type = BTRFS_DIR_ITEM_KEY;
3171
3172again:
3173 min_key = 0;
3174 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003175 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003176 ret = log_dir_items(trans, root, inode, path,
3177 dst_path, key_type, min_key,
3178 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003179 if (ret)
3180 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003181 if (max_key == (u64)-1)
3182 break;
3183 min_key = max_key + 1;
3184 }
3185
3186 if (key_type == BTRFS_DIR_ITEM_KEY) {
3187 key_type = BTRFS_DIR_INDEX_KEY;
3188 goto again;
3189 }
3190 return 0;
3191}
3192
3193/*
3194 * a helper function to drop items from the log before we relog an
3195 * inode. max_key_type indicates the highest item type to remove.
3196 * This cannot be run for file data extents because it does not
3197 * free the extents they point to.
3198 */
3199static int drop_objectid_items(struct btrfs_trans_handle *trans,
3200 struct btrfs_root *log,
3201 struct btrfs_path *path,
3202 u64 objectid, int max_key_type)
3203{
3204 int ret;
3205 struct btrfs_key key;
3206 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003207 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003208
3209 key.objectid = objectid;
3210 key.type = max_key_type;
3211 key.offset = (u64)-1;
3212
Chris Masond3977122009-01-05 21:25:51 -05003213 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003214 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003215 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003216 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003217 break;
3218
3219 if (path->slots[0] == 0)
3220 break;
3221
3222 path->slots[0]--;
3223 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3224 path->slots[0]);
3225
3226 if (found_key.objectid != objectid)
3227 break;
3228
Josef Bacik18ec90d2012-09-28 11:56:28 -04003229 found_key.offset = 0;
3230 found_key.type = 0;
3231 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3232 &start_slot);
3233
3234 ret = btrfs_del_items(trans, log, path, start_slot,
3235 path->slots[0] - start_slot + 1);
3236 /*
3237 * If start slot isn't 0 then we don't need to re-search, we've
3238 * found the last guy with the objectid in this tree.
3239 */
3240 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003241 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003242 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003243 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003244 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003245 if (ret > 0)
3246 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003247 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003248}
3249
Josef Bacik94edf4a2012-09-25 14:56:25 -04003250static void fill_inode_item(struct btrfs_trans_handle *trans,
3251 struct extent_buffer *leaf,
3252 struct btrfs_inode_item *item,
3253 struct inode *inode, int log_inode_only)
3254{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003255 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003256
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003257 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003258
3259 if (log_inode_only) {
3260 /* set the generation to zero so the recover code
3261 * can tell the difference between an logging
3262 * just to say 'this inode exists' and a logging
3263 * to say 'update this inode with these values'
3264 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003265 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3266 btrfs_set_token_inode_size(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003267 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003268 btrfs_set_token_inode_generation(leaf, item,
3269 BTRFS_I(inode)->generation,
3270 &token);
3271 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003272 }
3273
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003274 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3275 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3276 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3277 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3278
David Sterbaa937b972014-12-12 17:39:12 +01003279 btrfs_set_token_timespec_sec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003280 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003281 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003282 inode->i_atime.tv_nsec, &token);
3283
David Sterbaa937b972014-12-12 17:39:12 +01003284 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003285 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003286 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003287 inode->i_mtime.tv_nsec, &token);
3288
David Sterbaa937b972014-12-12 17:39:12 +01003289 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003290 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003291 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003292 inode->i_ctime.tv_nsec, &token);
3293
3294 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3295 &token);
3296
3297 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3298 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3299 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3300 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3301 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003302}
3303
Josef Bacika95249b2012-10-11 16:17:34 -04003304static int log_inode_item(struct btrfs_trans_handle *trans,
3305 struct btrfs_root *log, struct btrfs_path *path,
3306 struct inode *inode)
3307{
3308 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003309 int ret;
3310
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003311 ret = btrfs_insert_empty_item(trans, log, path,
3312 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003313 sizeof(*inode_item));
3314 if (ret && ret != -EEXIST)
3315 return ret;
3316 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3317 struct btrfs_inode_item);
3318 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3319 btrfs_release_path(path);
3320 return 0;
3321}
3322
Chris Mason31ff1cd2008-09-11 16:17:57 -04003323static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003324 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003325 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003326 struct btrfs_path *src_path, u64 *last_extent,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003327 int start_slot, int nr, int inode_only)
3328{
3329 unsigned long src_offset;
3330 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003331 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003332 struct btrfs_file_extent_item *extent;
3333 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003334 struct extent_buffer *src = src_path->nodes[0];
3335 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003336 int ret;
3337 struct btrfs_key *ins_keys;
3338 u32 *ins_sizes;
3339 char *ins_data;
3340 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003341 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003342 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003343 bool has_extents = false;
Filipe Manana74121f72014-08-07 12:00:44 +01003344 bool need_find_last_extent = true;
Josef Bacik16e75492013-10-22 12:18:51 -04003345 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003346
3347 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003348
3349 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3350 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003351 if (!ins_data)
3352 return -ENOMEM;
3353
Josef Bacik16e75492013-10-22 12:18:51 -04003354 first_key.objectid = (u64)-1;
3355
Chris Mason31ff1cd2008-09-11 16:17:57 -04003356 ins_sizes = (u32 *)ins_data;
3357 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3358
3359 for (i = 0; i < nr; i++) {
3360 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3361 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3362 }
3363 ret = btrfs_insert_empty_items(trans, log, dst_path,
3364 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003365 if (ret) {
3366 kfree(ins_data);
3367 return ret;
3368 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003369
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003370 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003371 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3372 dst_path->slots[0]);
3373
3374 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3375
Josef Bacik16e75492013-10-22 12:18:51 -04003376 if ((i == (nr - 1)))
3377 last_key = ins_keys[i];
3378
Josef Bacik94edf4a2012-09-25 14:56:25 -04003379 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003380 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3381 dst_path->slots[0],
3382 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003383 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3384 inode, inode_only == LOG_INODE_EXISTS);
3385 } else {
3386 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3387 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003388 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003389
Josef Bacik16e75492013-10-22 12:18:51 -04003390 /*
3391 * We set need_find_last_extent here in case we know we were
3392 * processing other items and then walk into the first extent in
3393 * the inode. If we don't hit an extent then nothing changes,
3394 * we'll do the last search the next time around.
3395 */
3396 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3397 has_extents = true;
Filipe Manana74121f72014-08-07 12:00:44 +01003398 if (first_key.objectid == (u64)-1)
Josef Bacik16e75492013-10-22 12:18:51 -04003399 first_key = ins_keys[i];
3400 } else {
3401 need_find_last_extent = false;
3402 }
3403
Chris Mason31ff1cd2008-09-11 16:17:57 -04003404 /* take a reference on file data extents so that truncates
3405 * or deletes of this inode don't have to relog the inode
3406 * again
3407 */
David Sterba962a2982014-06-04 18:41:45 +02003408 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
Liu Bod2794402012-08-29 01:07:56 -06003409 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003410 int found_type;
3411 extent = btrfs_item_ptr(src, start_slot + i,
3412 struct btrfs_file_extent_item);
3413
liubo8e531cd2011-05-06 10:36:09 +08003414 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3415 continue;
3416
Chris Mason31ff1cd2008-09-11 16:17:57 -04003417 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003418 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003419 u64 ds, dl, cs, cl;
3420 ds = btrfs_file_extent_disk_bytenr(src,
3421 extent);
3422 /* ds == 0 is a hole */
3423 if (ds == 0)
3424 continue;
3425
3426 dl = btrfs_file_extent_disk_num_bytes(src,
3427 extent);
3428 cs = btrfs_file_extent_offset(src, extent);
3429 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003430 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003431 if (btrfs_file_extent_compression(src,
3432 extent)) {
3433 cs = 0;
3434 cl = dl;
3435 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003436
3437 ret = btrfs_lookup_csums_range(
3438 log->fs_info->csum_root,
3439 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003440 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003441 if (ret) {
3442 btrfs_release_path(dst_path);
3443 kfree(ins_data);
3444 return ret;
3445 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003446 }
3447 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003448 }
3449
3450 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003451 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003452 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003453
3454 /*
3455 * we have to do this after the loop above to avoid changing the
3456 * log tree while trying to change the log tree.
3457 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003458 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003459 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003460 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3461 struct btrfs_ordered_sum,
3462 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003463 if (!ret)
3464 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003465 list_del(&sums->list);
3466 kfree(sums);
3467 }
Josef Bacik16e75492013-10-22 12:18:51 -04003468
3469 if (!has_extents)
3470 return ret;
3471
Filipe Manana74121f72014-08-07 12:00:44 +01003472 if (need_find_last_extent && *last_extent == first_key.offset) {
3473 /*
3474 * We don't have any leafs between our current one and the one
3475 * we processed before that can have file extent items for our
3476 * inode (and have a generation number smaller than our current
3477 * transaction id).
3478 */
3479 need_find_last_extent = false;
3480 }
3481
Josef Bacik16e75492013-10-22 12:18:51 -04003482 /*
3483 * Because we use btrfs_search_forward we could skip leaves that were
3484 * not modified and then assume *last_extent is valid when it really
3485 * isn't. So back up to the previous leaf and read the end of the last
3486 * extent before we go and fill in holes.
3487 */
3488 if (need_find_last_extent) {
3489 u64 len;
3490
3491 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3492 if (ret < 0)
3493 return ret;
3494 if (ret)
3495 goto fill_holes;
3496 if (src_path->slots[0])
3497 src_path->slots[0]--;
3498 src = src_path->nodes[0];
3499 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3500 if (key.objectid != btrfs_ino(inode) ||
3501 key.type != BTRFS_EXTENT_DATA_KEY)
3502 goto fill_holes;
3503 extent = btrfs_item_ptr(src, src_path->slots[0],
3504 struct btrfs_file_extent_item);
3505 if (btrfs_file_extent_type(src, extent) ==
3506 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003507 len = btrfs_file_extent_inline_len(src,
3508 src_path->slots[0],
3509 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003510 *last_extent = ALIGN(key.offset + len,
3511 log->sectorsize);
3512 } else {
3513 len = btrfs_file_extent_num_bytes(src, extent);
3514 *last_extent = key.offset + len;
3515 }
3516 }
3517fill_holes:
3518 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3519 * things could have happened
3520 *
3521 * 1) A merge could have happened, so we could currently be on a leaf
3522 * that holds what we were copying in the first place.
3523 * 2) A split could have happened, and now not all of the items we want
3524 * are on the same leaf.
3525 *
3526 * So we need to adjust how we search for holes, we need to drop the
3527 * path and re-search for the first extent key we found, and then walk
3528 * forward until we hit the last one we copied.
3529 */
3530 if (need_find_last_extent) {
3531 /* btrfs_prev_leaf could return 1 without releasing the path */
3532 btrfs_release_path(src_path);
3533 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3534 src_path, 0, 0);
3535 if (ret < 0)
3536 return ret;
3537 ASSERT(ret == 0);
3538 src = src_path->nodes[0];
3539 i = src_path->slots[0];
3540 } else {
3541 i = start_slot;
3542 }
3543
3544 /*
3545 * Ok so here we need to go through and fill in any holes we may have
3546 * to make sure that holes are punched for those areas in case they had
3547 * extents previously.
3548 */
3549 while (!done) {
3550 u64 offset, len;
3551 u64 extent_end;
3552
3553 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3554 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3555 if (ret < 0)
3556 return ret;
3557 ASSERT(ret == 0);
3558 src = src_path->nodes[0];
3559 i = 0;
3560 }
3561
3562 btrfs_item_key_to_cpu(src, &key, i);
3563 if (!btrfs_comp_cpu_keys(&key, &last_key))
3564 done = true;
3565 if (key.objectid != btrfs_ino(inode) ||
3566 key.type != BTRFS_EXTENT_DATA_KEY) {
3567 i++;
3568 continue;
3569 }
3570 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3571 if (btrfs_file_extent_type(src, extent) ==
3572 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003573 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003574 extent_end = ALIGN(key.offset + len, log->sectorsize);
3575 } else {
3576 len = btrfs_file_extent_num_bytes(src, extent);
3577 extent_end = key.offset + len;
3578 }
3579 i++;
3580
3581 if (*last_extent == key.offset) {
3582 *last_extent = extent_end;
3583 continue;
3584 }
3585 offset = *last_extent;
3586 len = key.offset - *last_extent;
3587 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3588 offset, 0, 0, len, 0, len, 0,
3589 0, 0);
3590 if (ret)
3591 break;
Filipe Manana74121f72014-08-07 12:00:44 +01003592 *last_extent = extent_end;
Josef Bacik16e75492013-10-22 12:18:51 -04003593 }
3594 /*
3595 * Need to let the callers know we dropped the path so they should
3596 * re-search.
3597 */
3598 if (!ret && need_find_last_extent)
3599 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003600 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003601}
3602
Josef Bacik5dc562c2012-08-17 13:14:17 -04003603static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3604{
3605 struct extent_map *em1, *em2;
3606
3607 em1 = list_entry(a, struct extent_map, list);
3608 em2 = list_entry(b, struct extent_map, list);
3609
3610 if (em1->start < em2->start)
3611 return -1;
3612 else if (em1->start > em2->start)
3613 return 1;
3614 return 0;
3615}
3616
Filipe Manana8407f552014-09-05 15:14:39 +01003617static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3618 struct inode *inode,
3619 struct btrfs_root *root,
3620 const struct extent_map *em,
3621 const struct list_head *logged_list,
3622 bool *ordered_io_error)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003623{
Josef Bacik2ab28f32012-10-12 15:27:49 -04003624 struct btrfs_ordered_extent *ordered;
Filipe Manana8407f552014-09-05 15:14:39 +01003625 struct btrfs_root *log = root->log_root;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003626 u64 mod_start = em->mod_start;
3627 u64 mod_len = em->mod_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003628 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003629 u64 csum_offset;
3630 u64 csum_len;
Filipe Manana8407f552014-09-05 15:14:39 +01003631 LIST_HEAD(ordered_sums);
3632 int ret = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003633
Filipe Manana8407f552014-09-05 15:14:39 +01003634 *ordered_io_error = false;
Josef Bacik70c8a912012-10-11 16:54:30 -04003635
Filipe Manana8407f552014-09-05 15:14:39 +01003636 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3637 em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003638 return 0;
3639
Josef Bacik2ab28f32012-10-12 15:27:49 -04003640 /*
Filipe Manana8407f552014-09-05 15:14:39 +01003641 * Wait far any ordered extent that covers our extent map. If it
3642 * finishes without an error, first check and see if our csums are on
3643 * our outstanding ordered extents.
Josef Bacik2ab28f32012-10-12 15:27:49 -04003644 */
Miao Xie827463c2014-01-14 20:31:51 +08003645 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003646 struct btrfs_ordered_sum *sum;
3647
3648 if (!mod_len)
3649 break;
3650
Josef Bacik2ab28f32012-10-12 15:27:49 -04003651 if (ordered->file_offset + ordered->len <= mod_start ||
3652 mod_start + mod_len <= ordered->file_offset)
3653 continue;
3654
Filipe Manana8407f552014-09-05 15:14:39 +01003655 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3656 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3657 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3658 const u64 start = ordered->file_offset;
3659 const u64 end = ordered->file_offset + ordered->len - 1;
3660
3661 WARN_ON(ordered->inode != inode);
3662 filemap_fdatawrite_range(inode->i_mapping, start, end);
3663 }
3664
3665 wait_event(ordered->wait,
3666 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3667 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3668
3669 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
Filipe Mananab38ef712014-11-13 17:01:45 +00003670 /*
3671 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3672 * i_mapping flags, so that the next fsync won't get
3673 * an outdated io error too.
3674 */
3675 btrfs_inode_check_errors(inode);
Filipe Manana8407f552014-09-05 15:14:39 +01003676 *ordered_io_error = true;
3677 break;
3678 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003679 /*
3680 * We are going to copy all the csums on this ordered extent, so
3681 * go ahead and adjust mod_start and mod_len in case this
3682 * ordered extent has already been logged.
3683 */
3684 if (ordered->file_offset > mod_start) {
3685 if (ordered->file_offset + ordered->len >=
3686 mod_start + mod_len)
3687 mod_len = ordered->file_offset - mod_start;
3688 /*
3689 * If we have this case
3690 *
3691 * |--------- logged extent ---------|
3692 * |----- ordered extent ----|
3693 *
3694 * Just don't mess with mod_start and mod_len, we'll
3695 * just end up logging more csums than we need and it
3696 * will be ok.
3697 */
3698 } else {
3699 if (ordered->file_offset + ordered->len <
3700 mod_start + mod_len) {
3701 mod_len = (mod_start + mod_len) -
3702 (ordered->file_offset + ordered->len);
3703 mod_start = ordered->file_offset +
3704 ordered->len;
3705 } else {
3706 mod_len = 0;
3707 }
3708 }
3709
Filipe Manana8407f552014-09-05 15:14:39 +01003710 if (skip_csum)
3711 continue;
3712
Josef Bacik2ab28f32012-10-12 15:27:49 -04003713 /*
3714 * To keep us from looping for the above case of an ordered
3715 * extent that falls inside of the logged extent.
3716 */
3717 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3718 &ordered->flags))
3719 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003720
Miao Xie23c671a2014-01-14 20:31:52 +08003721 if (ordered->csum_bytes_left) {
3722 btrfs_start_ordered_extent(inode, ordered, 0);
3723 wait_event(ordered->wait,
3724 ordered->csum_bytes_left == 0);
3725 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003726
3727 list_for_each_entry(sum, &ordered->list, list) {
3728 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08003729 if (ret)
Filipe Manana8407f552014-09-05 15:14:39 +01003730 break;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003731 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003732 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003733
Filipe Manana8407f552014-09-05 15:14:39 +01003734 if (*ordered_io_error || !mod_len || ret || skip_csum)
Josef Bacik2ab28f32012-10-12 15:27:49 -04003735 return ret;
3736
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00003737 if (em->compress_type) {
3738 csum_offset = 0;
Filipe Manana8407f552014-09-05 15:14:39 +01003739 csum_len = max(em->block_len, em->orig_block_len);
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00003740 } else {
3741 csum_offset = mod_start - em->start;
3742 csum_len = mod_len;
3743 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003744
Josef Bacik70c8a912012-10-11 16:54:30 -04003745 /* block start is already adjusted for the file extent offset. */
3746 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3747 em->block_start + csum_offset,
3748 em->block_start + csum_offset +
3749 csum_len - 1, &ordered_sums, 0);
3750 if (ret)
3751 return ret;
3752
3753 while (!list_empty(&ordered_sums)) {
3754 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3755 struct btrfs_ordered_sum,
3756 list);
3757 if (!ret)
3758 ret = btrfs_csum_file_blocks(trans, log, sums);
3759 list_del(&sums->list);
3760 kfree(sums);
3761 }
3762
3763 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003764}
3765
Filipe Manana8407f552014-09-05 15:14:39 +01003766static int log_one_extent(struct btrfs_trans_handle *trans,
3767 struct inode *inode, struct btrfs_root *root,
3768 const struct extent_map *em,
3769 struct btrfs_path *path,
3770 const struct list_head *logged_list,
3771 struct btrfs_log_ctx *ctx)
3772{
3773 struct btrfs_root *log = root->log_root;
3774 struct btrfs_file_extent_item *fi;
3775 struct extent_buffer *leaf;
3776 struct btrfs_map_token token;
3777 struct btrfs_key key;
3778 u64 extent_offset = em->start - em->orig_start;
3779 u64 block_len;
3780 int ret;
3781 int extent_inserted = 0;
3782 bool ordered_io_err = false;
3783
3784 ret = wait_ordered_extents(trans, inode, root, em, logged_list,
3785 &ordered_io_err);
3786 if (ret)
3787 return ret;
3788
3789 if (ordered_io_err) {
3790 ctx->io_err = -EIO;
3791 return 0;
3792 }
3793
3794 btrfs_init_map_token(&token);
3795
3796 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3797 em->start + em->len, NULL, 0, 1,
3798 sizeof(*fi), &extent_inserted);
3799 if (ret)
3800 return ret;
3801
3802 if (!extent_inserted) {
3803 key.objectid = btrfs_ino(inode);
3804 key.type = BTRFS_EXTENT_DATA_KEY;
3805 key.offset = em->start;
3806
3807 ret = btrfs_insert_empty_item(trans, log, path, &key,
3808 sizeof(*fi));
3809 if (ret)
3810 return ret;
3811 }
3812 leaf = path->nodes[0];
3813 fi = btrfs_item_ptr(leaf, path->slots[0],
3814 struct btrfs_file_extent_item);
3815
Josef Bacik50d9aa92014-11-21 14:52:38 -05003816 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
Filipe Manana8407f552014-09-05 15:14:39 +01003817 &token);
3818 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
3819 btrfs_set_token_file_extent_type(leaf, fi,
3820 BTRFS_FILE_EXTENT_PREALLOC,
3821 &token);
3822 else
3823 btrfs_set_token_file_extent_type(leaf, fi,
3824 BTRFS_FILE_EXTENT_REG,
3825 &token);
3826
3827 block_len = max(em->block_len, em->orig_block_len);
3828 if (em->compress_type != BTRFS_COMPRESS_NONE) {
3829 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3830 em->block_start,
3831 &token);
3832 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3833 &token);
3834 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
3835 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3836 em->block_start -
3837 extent_offset, &token);
3838 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3839 &token);
3840 } else {
3841 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3842 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3843 &token);
3844 }
3845
3846 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
3847 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
3848 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
3849 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3850 &token);
3851 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3852 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
3853 btrfs_mark_buffer_dirty(leaf);
3854
3855 btrfs_release_path(path);
3856
3857 return ret;
3858}
3859
Josef Bacik5dc562c2012-08-17 13:14:17 -04003860static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3861 struct btrfs_root *root,
3862 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08003863 struct btrfs_path *path,
Filipe Manana8407f552014-09-05 15:14:39 +01003864 struct list_head *logged_list,
3865 struct btrfs_log_ctx *ctx)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003866{
Josef Bacik5dc562c2012-08-17 13:14:17 -04003867 struct extent_map *em, *n;
3868 struct list_head extents;
3869 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3870 u64 test_gen;
3871 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003872 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003873
3874 INIT_LIST_HEAD(&extents);
3875
Josef Bacik5dc562c2012-08-17 13:14:17 -04003876 write_lock(&tree->lock);
3877 test_gen = root->fs_info->last_trans_committed;
3878
3879 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3880 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003881
3882 /*
3883 * Just an arbitrary number, this can be really CPU intensive
3884 * once we start getting a lot of extents, and really once we
3885 * have a bunch of extents we just want to commit since it will
3886 * be faster.
3887 */
3888 if (++num > 32768) {
3889 list_del_init(&tree->modified_extents);
3890 ret = -EFBIG;
3891 goto process;
3892 }
3893
Josef Bacik5dc562c2012-08-17 13:14:17 -04003894 if (em->generation <= test_gen)
3895 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003896 /* Need a ref to keep it from getting evicted from cache */
3897 atomic_inc(&em->refs);
3898 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003899 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003900 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003901 }
3902
3903 list_sort(NULL, &extents, extent_cmp);
3904
Josef Bacik2ab28f32012-10-12 15:27:49 -04003905process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003906 while (!list_empty(&extents)) {
3907 em = list_entry(extents.next, struct extent_map, list);
3908
3909 list_del_init(&em->list);
3910
3911 /*
3912 * If we had an error we just need to delete everybody from our
3913 * private list.
3914 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003915 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05003916 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003917 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003918 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003919 }
3920
3921 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003922
Filipe Manana8407f552014-09-05 15:14:39 +01003923 ret = log_one_extent(trans, inode, root, em, path, logged_list,
3924 ctx);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003925 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05003926 clear_em_logging(tree, em);
3927 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003928 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003929 WARN_ON(!list_empty(&extents));
3930 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003931
Josef Bacik5dc562c2012-08-17 13:14:17 -04003932 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003933 return ret;
3934}
3935
Chris Masone02119d2008-09-05 16:13:11 -04003936/* log a single inode in the tree log.
3937 * At least one parent directory for this inode must exist in the tree
3938 * or be logged already.
3939 *
3940 * Any items from this inode changed by the current transaction are copied
3941 * to the log tree. An extra reference is taken on any extents in this
3942 * file, allowing us to avoid a whole pile of corner cases around logging
3943 * blocks that have been removed from the tree.
3944 *
3945 * See LOG_INODE_ALL and related defines for a description of what inode_only
3946 * does.
3947 *
3948 * This handles both files and directories.
3949 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003950static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Filipe Manana49dae1b2014-09-06 22:34:39 +01003951 struct btrfs_root *root, struct inode *inode,
3952 int inode_only,
3953 const loff_t start,
Filipe Manana8407f552014-09-05 15:14:39 +01003954 const loff_t end,
3955 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04003956{
3957 struct btrfs_path *path;
3958 struct btrfs_path *dst_path;
3959 struct btrfs_key min_key;
3960 struct btrfs_key max_key;
3961 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003962 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08003963 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04003964 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003965 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003966 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003967 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003968 int ins_start_slot = 0;
3969 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003970 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003971 u64 ino = btrfs_ino(inode);
Filipe Manana49dae1b2014-09-06 22:34:39 +01003972 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masone02119d2008-09-05 16:13:11 -04003973
Chris Masone02119d2008-09-05 16:13:11 -04003974 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003975 if (!path)
3976 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003977 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003978 if (!dst_path) {
3979 btrfs_free_path(path);
3980 return -ENOMEM;
3981 }
Chris Masone02119d2008-09-05 16:13:11 -04003982
Li Zefan33345d012011-04-20 10:31:50 +08003983 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003984 min_key.type = BTRFS_INODE_ITEM_KEY;
3985 min_key.offset = 0;
3986
Li Zefan33345d012011-04-20 10:31:50 +08003987 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003988
Chris Mason12fcfd22009-03-24 10:24:20 -04003989
Josef Bacik5dc562c2012-08-17 13:14:17 -04003990 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00003991 if (S_ISDIR(inode->i_mode) ||
3992 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3993 &BTRFS_I(inode)->runtime_flags) &&
3994 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04003995 max_key.type = BTRFS_XATTR_ITEM_KEY;
3996 else
3997 max_key.type = (u8)-1;
3998 max_key.offset = (u64)-1;
3999
Filipe Manana2c2c4522015-01-13 16:40:04 +00004000 /*
4001 * Only run delayed items if we are a dir or a new file.
4002 * Otherwise commit the delayed inode only, which is needed in
4003 * order for the log replay code to mark inodes for link count
4004 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4005 */
Josef Bacik94edf4a2012-09-25 14:56:25 -04004006 if (S_ISDIR(inode->i_mode) ||
Filipe Manana2c2c4522015-01-13 16:40:04 +00004007 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
Josef Bacik94edf4a2012-09-25 14:56:25 -04004008 ret = btrfs_commit_inode_delayed_items(trans, inode);
Filipe Manana2c2c4522015-01-13 16:40:04 +00004009 else
4010 ret = btrfs_commit_inode_delayed_inode(inode);
4011
4012 if (ret) {
4013 btrfs_free_path(path);
4014 btrfs_free_path(dst_path);
4015 return ret;
Miao Xie16cdcec2011-04-22 18:12:22 +08004016 }
4017
Chris Masone02119d2008-09-05 16:13:11 -04004018 mutex_lock(&BTRFS_I(inode)->log_mutex);
4019
Filipe Manana08702952014-11-13 17:00:35 +00004020 btrfs_get_logged_extents(inode, &logged_list, start, end);
Josef Bacik2ab28f32012-10-12 15:27:49 -04004021
Chris Masone02119d2008-09-05 16:13:11 -04004022 /*
4023 * a brute force approach to making sure we get the most uptodate
4024 * copies of everything.
4025 */
4026 if (S_ISDIR(inode->i_mode)) {
4027 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4028
4029 if (inode_only == LOG_INODE_EXISTS)
4030 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08004031 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04004032 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004033 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4034 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacike9976152012-10-11 15:53:56 -04004035 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4036 &BTRFS_I(inode)->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004037 ret = btrfs_truncate_inode_items(trans, log,
4038 inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04004039 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Josef Bacik6cfab852013-11-12 16:25:58 -05004040 &BTRFS_I(inode)->runtime_flags) ||
4041 inode_only == LOG_INODE_EXISTS) {
Josef Bacika95249b2012-10-11 16:17:34 -04004042 if (inode_only == LOG_INODE_ALL)
4043 fast_search = true;
4044 max_key.type = BTRFS_XATTR_ITEM_KEY;
4045 ret = drop_objectid_items(trans, log, path, ino,
4046 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004047 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00004048 if (inode_only == LOG_INODE_ALL)
4049 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04004050 ret = log_inode_item(trans, log, dst_path, inode);
4051 if (ret) {
4052 err = ret;
4053 goto out_unlock;
4054 }
4055 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004056 }
Josef Bacika95249b2012-10-11 16:17:34 -04004057
Chris Masone02119d2008-09-05 16:13:11 -04004058 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004059 if (ret) {
4060 err = ret;
4061 goto out_unlock;
4062 }
Chris Masone02119d2008-09-05 16:13:11 -04004063
Chris Masond3977122009-01-05 21:25:51 -05004064 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04004065 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004066 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004067 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04004068 if (ret != 0)
4069 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04004070again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04004071 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08004072 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04004073 break;
4074 if (min_key.type > max_key.type)
4075 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004076
Chris Masone02119d2008-09-05 16:13:11 -04004077 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04004078 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4079 ins_nr++;
4080 goto next_slot;
4081 } else if (!ins_nr) {
4082 ins_start_slot = path->slots[0];
4083 ins_nr = 1;
4084 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04004085 }
4086
Josef Bacik16e75492013-10-22 12:18:51 -04004087 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4088 ins_start_slot, ins_nr, inode_only);
4089 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004090 err = ret;
4091 goto out_unlock;
Rasmus Villemoesa71db862014-06-20 21:51:43 +02004092 }
4093 if (ret) {
Josef Bacik16e75492013-10-22 12:18:51 -04004094 ins_nr = 0;
4095 btrfs_release_path(path);
4096 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004097 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004098 ins_nr = 1;
4099 ins_start_slot = path->slots[0];
4100next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04004101
Chris Mason3a5f1d42008-09-11 15:53:37 -04004102 nritems = btrfs_header_nritems(path->nodes[0]);
4103 path->slots[0]++;
4104 if (path->slots[0] < nritems) {
4105 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4106 path->slots[0]);
4107 goto again;
4108 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004109 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004110 ret = copy_items(trans, inode, dst_path, path,
4111 &last_extent, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04004112 ins_nr, inode_only);
Josef Bacik16e75492013-10-22 12:18:51 -04004113 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004114 err = ret;
4115 goto out_unlock;
4116 }
Josef Bacik16e75492013-10-22 12:18:51 -04004117 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004118 ins_nr = 0;
4119 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004120 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04004121
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004122 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004123 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004124 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004125 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004126 min_key.offset = 0;
4127 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004128 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004129 }
Chris Masone02119d2008-09-05 16:13:11 -04004130 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004131 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004132 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4133 ins_start_slot, ins_nr, inode_only);
4134 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004135 err = ret;
4136 goto out_unlock;
4137 }
Josef Bacik16e75492013-10-22 12:18:51 -04004138 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004139 ins_nr = 0;
4140 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004141
Josef Bacika95249b2012-10-11 16:17:34 -04004142log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004143 btrfs_release_path(path);
4144 btrfs_release_path(dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004145 if (fast_search) {
Filipe Mananab38ef712014-11-13 17:01:45 +00004146 /*
4147 * Some ordered extents started by fsync might have completed
4148 * before we collected the ordered extents in logged_list, which
4149 * means they're gone, not in our logged_list nor in the inode's
4150 * ordered tree. We want the application/user space to know an
4151 * error happened while attempting to persist file data so that
4152 * it can take proper action. If such error happened, we leave
4153 * without writing to the log tree and the fsync must report the
4154 * file data write error and not commit the current transaction.
4155 */
4156 err = btrfs_inode_check_errors(inode);
4157 if (err) {
4158 ctx->io_err = err;
4159 goto out_unlock;
4160 }
Miao Xie827463c2014-01-14 20:31:51 +08004161 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
Filipe Manana8407f552014-09-05 15:14:39 +01004162 &logged_list, ctx);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004163 if (ret) {
4164 err = ret;
4165 goto out_unlock;
4166 }
Josef Bacikd006a042013-11-12 20:54:09 -05004167 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004168 struct extent_map *em, *n;
4169
Filipe Manana49dae1b2014-09-06 22:34:39 +01004170 write_lock(&em_tree->lock);
4171 /*
4172 * We can't just remove every em if we're called for a ranged
4173 * fsync - that is, one that doesn't cover the whole possible
4174 * file range (0 to LLONG_MAX). This is because we can have
4175 * em's that fall outside the range we're logging and therefore
4176 * their ordered operations haven't completed yet
4177 * (btrfs_finish_ordered_io() not invoked yet). This means we
4178 * didn't get their respective file extent item in the fs/subvol
4179 * tree yet, and need to let the next fast fsync (one which
4180 * consults the list of modified extent maps) find the em so
4181 * that it logs a matching file extent item and waits for the
4182 * respective ordered operation to complete (if it's still
4183 * running).
4184 *
4185 * Removing every em outside the range we're logging would make
4186 * the next fast fsync not log their matching file extent items,
4187 * therefore making us lose data after a log replay.
4188 */
4189 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4190 list) {
4191 const u64 mod_end = em->mod_start + em->mod_len - 1;
4192
4193 if (em->mod_start >= start && mod_end <= end)
4194 list_del_init(&em->list);
4195 }
4196 write_unlock(&em_tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004197 }
4198
Chris Mason9623f9a2008-09-11 17:42:42 -04004199 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04004200 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004201 if (ret) {
4202 err = ret;
4203 goto out_unlock;
4204 }
Chris Masone02119d2008-09-05 16:13:11 -04004205 }
Filipe Manana49dae1b2014-09-06 22:34:39 +01004206
Filipe Manana125c4cf92014-09-11 21:22:14 +01004207 BTRFS_I(inode)->logged_trans = trans->transid;
4208 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004209out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08004210 if (unlikely(err))
4211 btrfs_put_logged_extents(&logged_list);
4212 else
4213 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04004214 mutex_unlock(&BTRFS_I(inode)->log_mutex);
4215
4216 btrfs_free_path(path);
4217 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004218 return err;
Chris Masone02119d2008-09-05 16:13:11 -04004219}
4220
Chris Mason12fcfd22009-03-24 10:24:20 -04004221/*
4222 * follow the dentry parent pointers up the chain and see if any
4223 * of the directories in it require a full commit before they can
4224 * be logged. Returns zero if nothing special needs to be done or 1 if
4225 * a full commit is required.
4226 */
4227static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
4228 struct inode *inode,
4229 struct dentry *parent,
4230 struct super_block *sb,
4231 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04004232{
Chris Mason12fcfd22009-03-24 10:24:20 -04004233 int ret = 0;
4234 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00004235 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04004236 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04004237
Chris Masonaf4176b2009-03-24 10:24:31 -04004238 /*
4239 * for regular files, if its inode is already on disk, we don't
4240 * have to worry about the parents at all. This is because
4241 * we can use the last_unlink_trans field to record renames
4242 * and other fun in this file.
4243 */
4244 if (S_ISREG(inode->i_mode) &&
4245 BTRFS_I(inode)->generation <= last_committed &&
4246 BTRFS_I(inode)->last_unlink_trans <= last_committed)
4247 goto out;
4248
Chris Mason12fcfd22009-03-24 10:24:20 -04004249 if (!S_ISDIR(inode->i_mode)) {
4250 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4251 goto out;
4252 inode = parent->d_inode;
4253 }
4254
4255 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04004256 /*
4257 * If we are logging a directory then we start with our inode,
4258 * not our parents inode, so we need to skipp setting the
4259 * logged_trans so that further down in the log code we don't
4260 * think this inode has already been logged.
4261 */
4262 if (inode != orig_inode)
4263 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04004264 smp_mb();
4265
4266 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
4267 root = BTRFS_I(inode)->root;
4268
4269 /*
4270 * make sure any commits to the log are forced
4271 * to be full commits
4272 */
Miao Xie995946d2014-04-02 19:51:06 +08004273 btrfs_set_log_full_commit(root->fs_info, trans);
Chris Mason12fcfd22009-03-24 10:24:20 -04004274 ret = 1;
4275 break;
4276 }
4277
4278 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4279 break;
4280
Yan, Zheng76dda932009-09-21 16:00:26 -04004281 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04004282 break;
4283
Josef Bacik6a912212010-11-20 09:48:00 +00004284 parent = dget_parent(parent);
4285 dput(old_parent);
4286 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04004287 inode = parent->d_inode;
4288
4289 }
Josef Bacik6a912212010-11-20 09:48:00 +00004290 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04004291out:
Chris Masone02119d2008-09-05 16:13:11 -04004292 return ret;
4293}
4294
4295/*
4296 * helper function around btrfs_log_inode to make sure newly created
4297 * parent directories also end up in the log. A minimal inode and backref
4298 * only logging is done of any parent directories that are older than
4299 * the last committed transaction
4300 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004301static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
4302 struct btrfs_root *root, struct inode *inode,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004303 struct dentry *parent,
4304 const loff_t start,
4305 const loff_t end,
4306 int exists_only,
Miao Xie8b050d32014-02-20 18:08:58 +08004307 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004308{
Chris Mason12fcfd22009-03-24 10:24:20 -04004309 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04004310 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00004311 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04004312 int ret = 0;
4313 u64 last_committed = root->fs_info->last_trans_committed;
Filipe Mananad36808e2015-01-10 10:56:48 +00004314 const struct dentry * const first_parent = parent;
4315 const bool did_unlink = (BTRFS_I(inode)->last_unlink_trans >
4316 last_committed);
Chris Mason12fcfd22009-03-24 10:24:20 -04004317
4318 sb = inode->i_sb;
4319
Sage Weil3a5e1402009-04-02 16:49:40 -04004320 if (btrfs_test_opt(root, NOTREELOG)) {
4321 ret = 1;
4322 goto end_no_trans;
4323 }
4324
Miao Xie995946d2014-04-02 19:51:06 +08004325 /*
4326 * The prev transaction commit doesn't complete, we need do
4327 * full commit by ourselves.
4328 */
Chris Mason12fcfd22009-03-24 10:24:20 -04004329 if (root->fs_info->last_trans_log_full_commit >
4330 root->fs_info->last_trans_committed) {
4331 ret = 1;
4332 goto end_no_trans;
4333 }
4334
Yan, Zheng76dda932009-09-21 16:00:26 -04004335 if (root != BTRFS_I(inode)->root ||
4336 btrfs_root_refs(&root->root_item) == 0) {
4337 ret = 1;
4338 goto end_no_trans;
4339 }
4340
Chris Mason12fcfd22009-03-24 10:24:20 -04004341 ret = check_parent_dirs_for_sync(trans, inode, parent,
4342 sb, last_committed);
4343 if (ret)
4344 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04004345
Josef Bacik22ee6982012-05-29 16:57:49 -04004346 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04004347 ret = BTRFS_NO_LOG_SYNC;
4348 goto end_no_trans;
4349 }
4350
Miao Xie8b050d32014-02-20 18:08:58 +08004351 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004352 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08004353 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004354
Filipe Manana8407f552014-09-05 15:14:39 +01004355 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004356 if (ret)
4357 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004358
Chris Masonaf4176b2009-03-24 10:24:31 -04004359 /*
4360 * for regular files, if its inode is already on disk, we don't
4361 * have to worry about the parents at all. This is because
4362 * we can use the last_unlink_trans field to record renames
4363 * and other fun in this file.
4364 */
4365 if (S_ISREG(inode->i_mode) &&
4366 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004367 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
4368 ret = 0;
4369 goto end_trans;
4370 }
Chris Masonaf4176b2009-03-24 10:24:31 -04004371
Chris Masond3977122009-01-05 21:25:51 -05004372 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04004373 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04004374 break;
4375
Chris Mason12fcfd22009-03-24 10:24:20 -04004376 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004377 if (root != BTRFS_I(inode)->root)
4378 break;
4379
Filipe Mananad36808e2015-01-10 10:56:48 +00004380 /*
4381 * On unlink we must make sure our immediate parent directory
4382 * inode is fully logged. This is to prevent leaving dangling
4383 * directory index entries and a wrong directory inode's i_size.
4384 * Not doing so can result in a directory being impossible to
4385 * delete after log replay (rmdir will always fail with error
4386 * -ENOTEMPTY).
4387 */
4388 if (did_unlink && parent == first_parent)
4389 inode_only = LOG_INODE_ALL;
4390 else
4391 inode_only = LOG_INODE_EXISTS;
4392
Chris Mason12fcfd22009-03-24 10:24:20 -04004393 if (BTRFS_I(inode)->generation >
Filipe Mananad36808e2015-01-10 10:56:48 +00004394 root->fs_info->last_trans_committed ||
4395 inode_only == LOG_INODE_ALL) {
Filipe Manana49dae1b2014-09-06 22:34:39 +01004396 ret = btrfs_log_inode(trans, root, inode, inode_only,
Filipe Manana8407f552014-09-05 15:14:39 +01004397 0, LLONG_MAX, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004398 if (ret)
4399 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004400 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004401 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04004402 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04004403
Josef Bacik6a912212010-11-20 09:48:00 +00004404 parent = dget_parent(parent);
4405 dput(old_parent);
4406 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04004407 }
Chris Mason12fcfd22009-03-24 10:24:20 -04004408 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004409end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00004410 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004411 if (ret < 0) {
Miao Xie995946d2014-04-02 19:51:06 +08004412 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004413 ret = 1;
4414 }
Miao Xie8b050d32014-02-20 18:08:58 +08004415
4416 if (ret)
4417 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04004418 btrfs_end_log_trans(root);
4419end_no_trans:
4420 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004421}
4422
4423/*
4424 * it is not safe to log dentry if the chunk root has added new
4425 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
4426 * If this returns 1, you must commit the transaction to safely get your
4427 * data on disk.
4428 */
4429int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08004430 struct btrfs_root *root, struct dentry *dentry,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004431 const loff_t start,
4432 const loff_t end,
Miao Xie8b050d32014-02-20 18:08:58 +08004433 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004434{
Josef Bacik6a912212010-11-20 09:48:00 +00004435 struct dentry *parent = dget_parent(dentry);
4436 int ret;
4437
Miao Xie8b050d32014-02-20 18:08:58 +08004438 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent,
Filipe Manana49dae1b2014-09-06 22:34:39 +01004439 start, end, 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00004440 dput(parent);
4441
4442 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004443}
4444
4445/*
4446 * should be called during mount to recover any replay any log trees
4447 * from the FS
4448 */
4449int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
4450{
4451 int ret;
4452 struct btrfs_path *path;
4453 struct btrfs_trans_handle *trans;
4454 struct btrfs_key key;
4455 struct btrfs_key found_key;
4456 struct btrfs_key tmp_key;
4457 struct btrfs_root *log;
4458 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
4459 struct walk_control wc = {
4460 .process_func = process_one_buffer,
4461 .stage = 0,
4462 };
4463
Chris Masone02119d2008-09-05 16:13:11 -04004464 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004465 if (!path)
4466 return -ENOMEM;
4467
4468 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004469
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004470 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004471 if (IS_ERR(trans)) {
4472 ret = PTR_ERR(trans);
4473 goto error;
4474 }
Chris Masone02119d2008-09-05 16:13:11 -04004475
4476 wc.trans = trans;
4477 wc.pin = 1;
4478
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004479 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004480 if (ret) {
4481 btrfs_error(fs_info, ret, "Failed to pin buffers while "
4482 "recovering log root tree.");
4483 goto error;
4484 }
Chris Masone02119d2008-09-05 16:13:11 -04004485
4486again:
4487 key.objectid = BTRFS_TREE_LOG_OBJECTID;
4488 key.offset = (u64)-1;
David Sterba962a2982014-06-04 18:41:45 +02004489 key.type = BTRFS_ROOT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -04004490
Chris Masond3977122009-01-05 21:25:51 -05004491 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004492 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004493
4494 if (ret < 0) {
4495 btrfs_error(fs_info, ret,
4496 "Couldn't find tree log root.");
4497 goto error;
4498 }
Chris Masone02119d2008-09-05 16:13:11 -04004499 if (ret > 0) {
4500 if (path->slots[0] == 0)
4501 break;
4502 path->slots[0]--;
4503 }
4504 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4505 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004506 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004507 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4508 break;
4509
Miao Xiecb517ea2013-05-15 07:48:19 +00004510 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004511 if (IS_ERR(log)) {
4512 ret = PTR_ERR(log);
4513 btrfs_error(fs_info, ret,
4514 "Couldn't read tree log root.");
4515 goto error;
4516 }
Chris Masone02119d2008-09-05 16:13:11 -04004517
4518 tmp_key.objectid = found_key.offset;
4519 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
4520 tmp_key.offset = (u64)-1;
4521
4522 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004523 if (IS_ERR(wc.replay_dest)) {
4524 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004525 free_extent_buffer(log->node);
4526 free_extent_buffer(log->commit_root);
4527 kfree(log);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004528 btrfs_error(fs_info, ret, "Couldn't read target root "
4529 "for tree log recovery.");
4530 goto error;
4531 }
Chris Masone02119d2008-09-05 16:13:11 -04004532
Yan Zheng07d400a2009-01-06 11:42:00 -05004533 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004534 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04004535 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04004536
Josef Bacikb50c6e22013-04-25 15:55:30 -04004537 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04004538 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4539 path);
Chris Masone02119d2008-09-05 16:13:11 -04004540 }
Chris Masone02119d2008-09-05 16:13:11 -04004541
4542 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05004543 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04004544 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04004545 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04004546 kfree(log);
4547
Josef Bacikb50c6e22013-04-25 15:55:30 -04004548 if (ret)
4549 goto error;
4550
Chris Masone02119d2008-09-05 16:13:11 -04004551 if (found_key.offset == 0)
4552 break;
4553 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004554 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004555
4556 /* step one is to pin it all, step two is to replay just inodes */
4557 if (wc.pin) {
4558 wc.pin = 0;
4559 wc.process_func = replay_one_buffer;
4560 wc.stage = LOG_WALK_REPLAY_INODES;
4561 goto again;
4562 }
4563 /* step three is to replay everything */
4564 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4565 wc.stage++;
4566 goto again;
4567 }
4568
4569 btrfs_free_path(path);
4570
Josef Bacikabefa552013-04-24 16:40:05 -04004571 /* step 4: commit the transaction, which also unpins the blocks */
4572 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
4573 if (ret)
4574 return ret;
4575
Chris Masone02119d2008-09-05 16:13:11 -04004576 free_extent_buffer(log_root_tree->node);
4577 log_root_tree->log_root = NULL;
4578 fs_info->log_root_recovering = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004579 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004580
Josef Bacikabefa552013-04-24 16:40:05 -04004581 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004582error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04004583 if (wc.trans)
4584 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004585 btrfs_free_path(path);
4586 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004587}
Chris Mason12fcfd22009-03-24 10:24:20 -04004588
4589/*
4590 * there are some corner cases where we want to force a full
4591 * commit instead of allowing a directory to be logged.
4592 *
4593 * They revolve around files there were unlinked from the directory, and
4594 * this function updates the parent directory so that a full commit is
4595 * properly done if it is fsync'd later after the unlinks are done.
4596 */
4597void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4598 struct inode *dir, struct inode *inode,
4599 int for_rename)
4600{
4601 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004602 * when we're logging a file, if it hasn't been renamed
4603 * or unlinked, and its inode is fully committed on disk,
4604 * we don't have to worry about walking up the directory chain
4605 * to log its parents.
4606 *
4607 * So, we use the last_unlink_trans field to put this transid
4608 * into the file. When the file is logged we check it and
4609 * don't log the parents if the file is fully on disk.
4610 */
4611 if (S_ISREG(inode->i_mode))
4612 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4613
4614 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004615 * if this directory was already logged any new
4616 * names for this file/dir will get recorded
4617 */
4618 smp_mb();
4619 if (BTRFS_I(dir)->logged_trans == trans->transid)
4620 return;
4621
4622 /*
4623 * if the inode we're about to unlink was logged,
4624 * the log will be properly updated for any new names
4625 */
4626 if (BTRFS_I(inode)->logged_trans == trans->transid)
4627 return;
4628
4629 /*
4630 * when renaming files across directories, if the directory
4631 * there we're unlinking from gets fsync'd later on, there's
4632 * no way to find the destination directory later and fsync it
4633 * properly. So, we have to be conservative and force commits
4634 * so the new name gets discovered.
4635 */
4636 if (for_rename)
4637 goto record;
4638
4639 /* we can safely do the unlink without any special recording */
4640 return;
4641
4642record:
4643 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4644}
4645
4646/*
4647 * Call this after adding a new name for a file and it will properly
4648 * update the log to reflect the new name.
4649 *
4650 * It will return zero if all goes well, and it will return 1 if a
4651 * full transaction commit is required.
4652 */
4653int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4654 struct inode *inode, struct inode *old_dir,
4655 struct dentry *parent)
4656{
4657 struct btrfs_root * root = BTRFS_I(inode)->root;
4658
4659 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004660 * this will force the logging code to walk the dentry chain
4661 * up for the file
4662 */
4663 if (S_ISREG(inode->i_mode))
4664 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4665
4666 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004667 * if this inode hasn't been logged and directory we're renaming it
4668 * from hasn't been logged, we don't need to log it
4669 */
4670 if (BTRFS_I(inode)->logged_trans <=
4671 root->fs_info->last_trans_committed &&
4672 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4673 root->fs_info->last_trans_committed))
4674 return 0;
4675
Filipe Manana49dae1b2014-09-06 22:34:39 +01004676 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
4677 LLONG_MAX, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04004678}
4679