blob: 57d4ca7fd5558667779a6ef80cb6f30c4174784c [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>
Chris Masone02119d2008-09-05 16:13:11 -040023#include "ctree.h"
24#include "transaction.h"
25#include "disk-io.h"
26#include "locking.h"
27#include "print-tree.h"
Mark Fashehf1863732012-08-08 11:32:27 -070028#include "backref.h"
Christoph Hellwigb2950862008-12-02 09:54:17 -050029#include "tree-log.h"
Mark Fashehf1863732012-08-08 11:32:27 -070030#include "hash.h"
Chris Masone02119d2008-09-05 16:13:11 -040031
32/* magic values for the inode_only field in btrfs_log_inode:
33 *
34 * LOG_INODE_ALL means to log everything
35 * LOG_INODE_EXISTS means to log just enough to recreate the inode
36 * during log replay
37 */
38#define LOG_INODE_ALL 0
39#define LOG_INODE_EXISTS 1
40
41/*
Chris Mason12fcfd22009-03-24 10:24:20 -040042 * directory trouble cases
43 *
44 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
45 * log, we must force a full commit before doing an fsync of the directory
46 * where the unlink was done.
47 * ---> record transid of last unlink/rename per directory
48 *
49 * mkdir foo/some_dir
50 * normal commit
51 * rename foo/some_dir foo2/some_dir
52 * mkdir foo/some_dir
53 * fsync foo/some_dir/some_file
54 *
55 * The fsync above will unlink the original some_dir without recording
56 * it in its new location (foo2). After a crash, some_dir will be gone
57 * unless the fsync of some_file forces a full commit
58 *
59 * 2) we must log any new names for any file or dir that is in the fsync
60 * log. ---> check inode while renaming/linking.
61 *
62 * 2a) we must log any new names for any file or dir during rename
63 * when the directory they are being removed from was logged.
64 * ---> check inode and old parent dir during rename
65 *
66 * 2a is actually the more important variant. With the extra logging
67 * a crash might unlink the old name without recreating the new one
68 *
69 * 3) after a crash, we must go through any directories with a link count
70 * of zero and redo the rm -rf
71 *
72 * mkdir f1/foo
73 * normal commit
74 * rm -rf f1/foo
75 * fsync(f1)
76 *
77 * The directory f1 was fully removed from the FS, but fsync was never
78 * called on f1, only its parent dir. After a crash the rm -rf must
79 * be replayed. This must be able to recurse down the entire
80 * directory tree. The inode link count fixup code takes care of the
81 * ugly details.
82 */
83
84/*
Chris Masone02119d2008-09-05 16:13:11 -040085 * stages for the tree walking. The first
86 * stage (0) is to only pin down the blocks we find
87 * the second stage (1) is to make sure that all the inodes
88 * we find in the log are created in the subvolume.
89 *
90 * The last stage is to deal with directories and links and extents
91 * and all the other fun semantics
92 */
93#define LOG_WALK_PIN_ONLY 0
94#define LOG_WALK_REPLAY_INODES 1
Josef Bacikdd8e7212013-09-11 11:57:23 -040095#define LOG_WALK_REPLAY_DIR_INDEX 2
96#define LOG_WALK_REPLAY_ALL 3
Chris Masone02119d2008-09-05 16:13:11 -040097
Chris Mason12fcfd22009-03-24 10:24:20 -040098static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -040099 struct btrfs_root *root, struct inode *inode,
100 int inode_only);
Yan Zhengec051c02009-01-05 15:43:42 -0500101static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
102 struct btrfs_root *root,
103 struct btrfs_path *path, u64 objectid);
Chris Mason12fcfd22009-03-24 10:24:20 -0400104static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
105 struct btrfs_root *root,
106 struct btrfs_root *log,
107 struct btrfs_path *path,
108 u64 dirid, int del_all);
Chris Masone02119d2008-09-05 16:13:11 -0400109
110/*
111 * tree logging is a special write ahead log used to make sure that
112 * fsyncs and O_SYNCs can happen without doing full tree commits.
113 *
114 * Full tree commits are expensive because they require commonly
115 * modified blocks to be recowed, creating many dirty pages in the
116 * extent tree an 4x-6x higher write load than ext3.
117 *
118 * Instead of doing a tree commit on every fsync, we use the
119 * key ranges and transaction ids to find items for a given file or directory
120 * that have changed in this transaction. Those items are copied into
121 * a special tree (one per subvolume root), that tree is written to disk
122 * and then the fsync is considered complete.
123 *
124 * After a crash, items are copied out of the log-tree back into the
125 * subvolume tree. Any file data extents found are recorded in the extent
126 * allocation tree, and the log-tree freed.
127 *
128 * The log tree is read three times, once to pin down all the extents it is
129 * using in ram and once, once to create all the inodes logged in the tree
130 * and once to do all the other items.
131 */
132
133/*
Chris Masone02119d2008-09-05 16:13:11 -0400134 * start a sub transaction and setup the log tree
135 * this increments the log tree writer count to make the people
136 * syncing the tree wait for us to finish
137 */
138static int start_log_trans(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +0800139 struct btrfs_root *root,
140 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -0400141{
Miao Xie8b050d32014-02-20 18:08:58 +0800142 int index;
Chris Masone02119d2008-09-05 16:13:11 -0400143 int ret;
Yan Zheng7237f1832009-01-21 12:54:03 -0500144
145 mutex_lock(&root->log_mutex);
146 if (root->log_root) {
Josef Bacikff782e02009-10-08 15:30:04 -0400147 if (!root->log_start_pid) {
148 root->log_start_pid = current->pid;
149 root->log_multiple_pids = false;
150 } else if (root->log_start_pid != current->pid) {
151 root->log_multiple_pids = true;
152 }
153
Miao Xie2ecb7922012-09-06 04:04:27 -0600154 atomic_inc(&root->log_batch);
Yan Zheng7237f1832009-01-21 12:54:03 -0500155 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800156 if (ctx) {
157 index = root->log_transid % 2;
158 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800159 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800160 }
Yan Zheng7237f1832009-01-21 12:54:03 -0500161 mutex_unlock(&root->log_mutex);
162 return 0;
163 }
Miao Xiee87ac132014-02-20 18:08:53 +0800164
165 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400166 mutex_lock(&root->fs_info->tree_log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800167 if (!root->fs_info->log_root_tree)
Chris Masone02119d2008-09-05 16:13:11 -0400168 ret = btrfs_init_log_root_tree(trans, root->fs_info);
Miao Xiee87ac132014-02-20 18:08:53 +0800169 mutex_unlock(&root->fs_info->tree_log_mutex);
170 if (ret)
171 goto out;
172
173 if (!root->log_root) {
Chris Masone02119d2008-09-05 16:13:11 -0400174 ret = btrfs_add_log_tree(trans, root);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400175 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +0800176 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400177 }
Miao Xiee87ac132014-02-20 18:08:53 +0800178 root->log_multiple_pids = false;
179 root->log_start_pid = current->pid;
Miao Xie2ecb7922012-09-06 04:04:27 -0600180 atomic_inc(&root->log_batch);
Yan Zheng7237f1832009-01-21 12:54:03 -0500181 atomic_inc(&root->log_writers);
Miao Xie8b050d32014-02-20 18:08:58 +0800182 if (ctx) {
183 index = root->log_transid % 2;
184 list_add_tail(&ctx->list, &root->log_ctxs[index]);
Miao Xied1433de2014-02-20 18:08:59 +0800185 ctx->log_transid = root->log_transid;
Miao Xie8b050d32014-02-20 18:08:58 +0800186 }
Miao Xiee87ac132014-02-20 18:08:53 +0800187out:
Yan Zheng7237f1832009-01-21 12:54:03 -0500188 mutex_unlock(&root->log_mutex);
Miao Xiee87ac132014-02-20 18:08:53 +0800189 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400190}
191
192/*
193 * returns 0 if there was a log transaction running and we were able
194 * to join, or returns -ENOENT if there were not transactions
195 * in progress
196 */
197static int join_running_log_trans(struct btrfs_root *root)
198{
199 int ret = -ENOENT;
200
201 smp_mb();
202 if (!root->log_root)
203 return -ENOENT;
204
Yan Zheng7237f1832009-01-21 12:54:03 -0500205 mutex_lock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400206 if (root->log_root) {
207 ret = 0;
Yan Zheng7237f1832009-01-21 12:54:03 -0500208 atomic_inc(&root->log_writers);
Chris Masone02119d2008-09-05 16:13:11 -0400209 }
Yan Zheng7237f1832009-01-21 12:54:03 -0500210 mutex_unlock(&root->log_mutex);
Chris Masone02119d2008-09-05 16:13:11 -0400211 return ret;
212}
213
214/*
Chris Mason12fcfd22009-03-24 10:24:20 -0400215 * This either makes the current running log transaction wait
216 * until you call btrfs_end_log_trans() or it makes any future
217 * log transactions wait until you call btrfs_end_log_trans()
218 */
219int btrfs_pin_log_trans(struct btrfs_root *root)
220{
221 int ret = -ENOENT;
222
223 mutex_lock(&root->log_mutex);
224 atomic_inc(&root->log_writers);
225 mutex_unlock(&root->log_mutex);
226 return ret;
227}
228
229/*
Chris Masone02119d2008-09-05 16:13:11 -0400230 * indicate we're done making changes to the log tree
231 * and wake up anyone waiting to do a sync
232 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100233void btrfs_end_log_trans(struct btrfs_root *root)
Chris Masone02119d2008-09-05 16:13:11 -0400234{
Yan Zheng7237f1832009-01-21 12:54:03 -0500235 if (atomic_dec_and_test(&root->log_writers)) {
236 smp_mb();
237 if (waitqueue_active(&root->log_writer_wait))
238 wake_up(&root->log_writer_wait);
239 }
Chris Masone02119d2008-09-05 16:13:11 -0400240}
241
242
243/*
244 * the walk control struct is used to pass state down the chain when
245 * processing the log tree. The stage field tells us which part
246 * of the log tree processing we are currently doing. The others
247 * are state fields used for that specific part
248 */
249struct walk_control {
250 /* should we free the extent on disk when done? This is used
251 * at transaction commit time while freeing a log tree
252 */
253 int free;
254
255 /* should we write out the extent buffer? This is used
256 * while flushing the log tree to disk during a sync
257 */
258 int write;
259
260 /* should we wait for the extent buffer io to finish? Also used
261 * while flushing the log tree to disk for a sync
262 */
263 int wait;
264
265 /* pin only walk, we record which extents on disk belong to the
266 * log trees
267 */
268 int pin;
269
270 /* what stage of the replay code we're currently in */
271 int stage;
272
273 /* the root we are currently replaying */
274 struct btrfs_root *replay_dest;
275
276 /* the trans handle for the current replay */
277 struct btrfs_trans_handle *trans;
278
279 /* the function that gets used to process blocks we find in the
280 * tree. Note the extent_buffer might not be up to date when it is
281 * passed in, and it must be checked or read if you need the data
282 * inside it
283 */
284 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
285 struct walk_control *wc, u64 gen);
286};
287
288/*
289 * process_func used to pin down extents, write them or wait on them
290 */
291static int process_one_buffer(struct btrfs_root *log,
292 struct extent_buffer *eb,
293 struct walk_control *wc, u64 gen)
294{
Josef Bacikb50c6e22013-04-25 15:55:30 -0400295 int ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400296
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400297 /*
298 * If this fs is mixed then we need to be able to process the leaves to
299 * pin down any logged extents, so we have to read the block.
300 */
301 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
302 ret = btrfs_read_buffer(eb, gen);
303 if (ret)
304 return ret;
305 }
306
Josef Bacikb50c6e22013-04-25 15:55:30 -0400307 if (wc->pin)
308 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
309 eb->start, eb->len);
310
311 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
Josef Bacik8c2a1a32013-06-06 13:19:32 -0400312 if (wc->pin && btrfs_header_level(eb) == 0)
313 ret = btrfs_exclude_logged_extents(log, eb);
Chris Masone02119d2008-09-05 16:13:11 -0400314 if (wc->write)
315 btrfs_write_tree_block(eb);
316 if (wc->wait)
317 btrfs_wait_tree_block_writeback(eb);
318 }
Josef Bacikb50c6e22013-04-25 15:55:30 -0400319 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400320}
321
322/*
323 * Item overwrite used by replay and tree logging. eb, slot and key all refer
324 * to the src data we are copying out.
325 *
326 * root is the tree we are copying into, and path is a scratch
327 * path for use in this function (it should be released on entry and
328 * will be released on exit).
329 *
330 * If the key is already in the destination tree the existing item is
331 * overwritten. If the existing item isn't big enough, it is extended.
332 * If it is too large, it is truncated.
333 *
334 * If the key isn't in the destination yet, a new item is inserted.
335 */
336static noinline int overwrite_item(struct btrfs_trans_handle *trans,
337 struct btrfs_root *root,
338 struct btrfs_path *path,
339 struct extent_buffer *eb, int slot,
340 struct btrfs_key *key)
341{
342 int ret;
343 u32 item_size;
344 u64 saved_i_size = 0;
345 int save_old_i_size = 0;
346 unsigned long src_ptr;
347 unsigned long dst_ptr;
348 int overwrite_root = 0;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000349 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400350
351 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
352 overwrite_root = 1;
353
354 item_size = btrfs_item_size_nr(eb, slot);
355 src_ptr = btrfs_item_ptr_offset(eb, slot);
356
357 /* look for the key in the destination tree */
358 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000359 if (ret < 0)
360 return ret;
361
Chris Masone02119d2008-09-05 16:13:11 -0400362 if (ret == 0) {
363 char *src_copy;
364 char *dst_copy;
365 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
366 path->slots[0]);
367 if (dst_size != item_size)
368 goto insert;
369
370 if (item_size == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200371 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400372 return 0;
373 }
374 dst_copy = kmalloc(item_size, GFP_NOFS);
375 src_copy = kmalloc(item_size, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000376 if (!dst_copy || !src_copy) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200377 btrfs_release_path(path);
liubo2a29edc2011-01-26 06:22:08 +0000378 kfree(dst_copy);
379 kfree(src_copy);
380 return -ENOMEM;
381 }
Chris Masone02119d2008-09-05 16:13:11 -0400382
383 read_extent_buffer(eb, src_copy, src_ptr, item_size);
384
385 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
386 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
387 item_size);
388 ret = memcmp(dst_copy, src_copy, item_size);
389
390 kfree(dst_copy);
391 kfree(src_copy);
392 /*
393 * they have the same contents, just return, this saves
394 * us from cowing blocks in the destination tree and doing
395 * extra writes that may not have been done by a previous
396 * sync
397 */
398 if (ret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200399 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400400 return 0;
401 }
402
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000403 /*
404 * We need to load the old nbytes into the inode so when we
405 * replay the extents we've logged we get the right nbytes.
406 */
407 if (inode_item) {
408 struct btrfs_inode_item *item;
409 u64 nbytes;
Josef Bacikd5554382013-09-11 14:17:00 -0400410 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000411
412 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
413 struct btrfs_inode_item);
414 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
415 item = btrfs_item_ptr(eb, slot,
416 struct btrfs_inode_item);
417 btrfs_set_inode_nbytes(eb, item, nbytes);
Josef Bacikd5554382013-09-11 14:17:00 -0400418
419 /*
420 * If this is a directory we need to reset the i_size to
421 * 0 so that we can set it up properly when replaying
422 * the rest of the items in this log.
423 */
424 mode = btrfs_inode_mode(eb, item);
425 if (S_ISDIR(mode))
426 btrfs_set_inode_size(eb, item, 0);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000427 }
428 } else if (inode_item) {
429 struct btrfs_inode_item *item;
Josef Bacikd5554382013-09-11 14:17:00 -0400430 u32 mode;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000431
432 /*
433 * New inode, set nbytes to 0 so that the nbytes comes out
434 * properly when we replay the extents.
435 */
436 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
437 btrfs_set_inode_nbytes(eb, item, 0);
Josef Bacikd5554382013-09-11 14:17:00 -0400438
439 /*
440 * If this is a directory we need to reset the i_size to 0 so
441 * that we can set it up properly when replaying the rest of
442 * the items in this log.
443 */
444 mode = btrfs_inode_mode(eb, item);
445 if (S_ISDIR(mode))
446 btrfs_set_inode_size(eb, item, 0);
Chris Masone02119d2008-09-05 16:13:11 -0400447 }
448insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200449 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400450 /* try to insert the key into the destination tree */
451 ret = btrfs_insert_empty_item(trans, root, path,
452 key, item_size);
453
454 /* make sure any existing item is the correct size */
455 if (ret == -EEXIST) {
456 u32 found_size;
457 found_size = btrfs_item_size_nr(path->nodes[0],
458 path->slots[0]);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100459 if (found_size > item_size)
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000460 btrfs_truncate_item(root, path, item_size, 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100461 else if (found_size < item_size)
Tsutomu Itoh4b90c682013-04-16 05:18:49 +0000462 btrfs_extend_item(root, path,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100463 item_size - found_size);
Chris Masone02119d2008-09-05 16:13:11 -0400464 } else if (ret) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400465 return ret;
Chris Masone02119d2008-09-05 16:13:11 -0400466 }
467 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
468 path->slots[0]);
469
470 /* don't overwrite an existing inode if the generation number
471 * was logged as zero. This is done when the tree logging code
472 * is just logging an inode to make sure it exists after recovery.
473 *
474 * Also, don't overwrite i_size on directories during replay.
475 * log replay inserts and removes directory items based on the
476 * state of the tree found in the subvolume, and i_size is modified
477 * as it goes
478 */
479 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
480 struct btrfs_inode_item *src_item;
481 struct btrfs_inode_item *dst_item;
482
483 src_item = (struct btrfs_inode_item *)src_ptr;
484 dst_item = (struct btrfs_inode_item *)dst_ptr;
485
486 if (btrfs_inode_generation(eb, src_item) == 0)
487 goto no_copy;
488
489 if (overwrite_root &&
490 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
491 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
492 save_old_i_size = 1;
493 saved_i_size = btrfs_inode_size(path->nodes[0],
494 dst_item);
495 }
496 }
497
498 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
499 src_ptr, item_size);
500
501 if (save_old_i_size) {
502 struct btrfs_inode_item *dst_item;
503 dst_item = (struct btrfs_inode_item *)dst_ptr;
504 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
505 }
506
507 /* make sure the generation is filled in */
508 if (key->type == BTRFS_INODE_ITEM_KEY) {
509 struct btrfs_inode_item *dst_item;
510 dst_item = (struct btrfs_inode_item *)dst_ptr;
511 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
512 btrfs_set_inode_generation(path->nodes[0], dst_item,
513 trans->transid);
514 }
515 }
516no_copy:
517 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +0200518 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400519 return 0;
520}
521
522/*
523 * simple helper to read an inode off the disk from a given root
524 * This can only be called for subvolume roots and not for the log
525 */
526static noinline struct inode *read_one_inode(struct btrfs_root *root,
527 u64 objectid)
528{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400529 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -0400530 struct inode *inode;
Chris Masone02119d2008-09-05 16:13:11 -0400531
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400532 key.objectid = objectid;
533 key.type = BTRFS_INODE_ITEM_KEY;
534 key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +0000535 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400536 if (IS_ERR(inode)) {
537 inode = NULL;
538 } else if (is_bad_inode(inode)) {
Chris Masone02119d2008-09-05 16:13:11 -0400539 iput(inode);
540 inode = NULL;
541 }
542 return inode;
543}
544
545/* replays a single extent in 'eb' at 'slot' with 'key' into the
546 * subvolume 'root'. path is released on entry and should be released
547 * on exit.
548 *
549 * extents in the log tree have not been allocated out of the extent
550 * tree yet. So, this completes the allocation, taking a reference
551 * as required if the extent already exists or creating a new extent
552 * if it isn't in the extent allocation tree yet.
553 *
554 * The extent is inserted into the file, dropping any existing extents
555 * from the file that overlap the new one.
556 */
557static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
558 struct btrfs_root *root,
559 struct btrfs_path *path,
560 struct extent_buffer *eb, int slot,
561 struct btrfs_key *key)
562{
563 int found_type;
Chris Masone02119d2008-09-05 16:13:11 -0400564 u64 extent_end;
Chris Masone02119d2008-09-05 16:13:11 -0400565 u64 start = key->offset;
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000566 u64 nbytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -0400567 struct btrfs_file_extent_item *item;
568 struct inode *inode = NULL;
569 unsigned long size;
570 int ret = 0;
571
572 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
573 found_type = btrfs_file_extent_type(eb, item);
574
Yan Zhengd899e052008-10-30 14:25:28 -0400575 if (found_type == BTRFS_FILE_EXTENT_REG ||
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000576 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
577 nbytes = btrfs_file_extent_num_bytes(eb, item);
578 extent_end = start + nbytes;
579
580 /*
581 * We don't add to the inodes nbytes if we are prealloc or a
582 * hole.
583 */
584 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
585 nbytes = 0;
586 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -0800587 size = btrfs_file_extent_inline_len(eb, slot, item);
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000588 nbytes = btrfs_file_extent_ram_bytes(eb, item);
Qu Wenruofda28322013-02-26 08:10:22 +0000589 extent_end = ALIGN(start + size, root->sectorsize);
Chris Masone02119d2008-09-05 16:13:11 -0400590 } else {
591 ret = 0;
592 goto out;
593 }
594
595 inode = read_one_inode(root, key->objectid);
596 if (!inode) {
597 ret = -EIO;
598 goto out;
599 }
600
601 /*
602 * first check to see if we already have this extent in the
603 * file. This must be done before the btrfs_drop_extents run
604 * so we don't try to drop this extent.
605 */
Li Zefan33345d012011-04-20 10:31:50 +0800606 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -0400607 start, 0);
608
Yan Zhengd899e052008-10-30 14:25:28 -0400609 if (ret == 0 &&
610 (found_type == BTRFS_FILE_EXTENT_REG ||
611 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
Chris Masone02119d2008-09-05 16:13:11 -0400612 struct btrfs_file_extent_item cmp1;
613 struct btrfs_file_extent_item cmp2;
614 struct btrfs_file_extent_item *existing;
615 struct extent_buffer *leaf;
616
617 leaf = path->nodes[0];
618 existing = btrfs_item_ptr(leaf, path->slots[0],
619 struct btrfs_file_extent_item);
620
621 read_extent_buffer(eb, &cmp1, (unsigned long)item,
622 sizeof(cmp1));
623 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
624 sizeof(cmp2));
625
626 /*
627 * we already have a pointer to this exact extent,
628 * we don't have to do anything
629 */
630 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200631 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400632 goto out;
633 }
634 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200635 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400636
637 /* drop any overlapping extents */
Josef Bacik26714852012-08-29 12:24:27 -0400638 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
Josef Bacik36508602013-04-25 16:23:32 -0400639 if (ret)
640 goto out;
Chris Masone02119d2008-09-05 16:13:11 -0400641
Yan Zheng07d400a2009-01-06 11:42:00 -0500642 if (found_type == BTRFS_FILE_EXTENT_REG ||
643 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400644 u64 offset;
Yan Zheng07d400a2009-01-06 11:42:00 -0500645 unsigned long dest_offset;
646 struct btrfs_key ins;
Chris Masone02119d2008-09-05 16:13:11 -0400647
Yan Zheng07d400a2009-01-06 11:42:00 -0500648 ret = btrfs_insert_empty_item(trans, root, path, key,
649 sizeof(*item));
Josef Bacik36508602013-04-25 16:23:32 -0400650 if (ret)
651 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500652 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
653 path->slots[0]);
654 copy_extent_buffer(path->nodes[0], eb, dest_offset,
655 (unsigned long)item, sizeof(*item));
656
657 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
658 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
659 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400660 offset = key->offset - btrfs_file_extent_offset(eb, item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500661
662 if (ins.objectid > 0) {
663 u64 csum_start;
664 u64 csum_end;
665 LIST_HEAD(ordered_sums);
666 /*
667 * is this extent already allocated in the extent
668 * allocation tree? If so, just add a reference
669 */
670 ret = btrfs_lookup_extent(root, ins.objectid,
671 ins.offset);
672 if (ret == 0) {
673 ret = btrfs_inc_extent_ref(trans, root,
674 ins.objectid, ins.offset,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400675 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200676 key->objectid, offset, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400677 if (ret)
678 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500679 } else {
680 /*
681 * insert the extent pointer in the extent
682 * allocation tree
683 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400684 ret = btrfs_alloc_logged_file_extent(trans,
685 root, root->root_key.objectid,
686 key->objectid, offset, &ins);
Josef Bacikb50c6e22013-04-25 15:55:30 -0400687 if (ret)
688 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500689 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200690 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500691
692 if (btrfs_file_extent_compression(eb, item)) {
693 csum_start = ins.objectid;
694 csum_end = csum_start + ins.offset;
695 } else {
696 csum_start = ins.objectid +
697 btrfs_file_extent_offset(eb, item);
698 csum_end = csum_start +
699 btrfs_file_extent_num_bytes(eb, item);
700 }
701
702 ret = btrfs_lookup_csums_range(root->log_root,
703 csum_start, csum_end - 1,
Arne Jansena2de7332011-03-08 14:14:00 +0100704 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -0400705 if (ret)
706 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500707 while (!list_empty(&ordered_sums)) {
708 struct btrfs_ordered_sum *sums;
709 sums = list_entry(ordered_sums.next,
710 struct btrfs_ordered_sum,
711 list);
Josef Bacik36508602013-04-25 16:23:32 -0400712 if (!ret)
713 ret = btrfs_csum_file_blocks(trans,
Yan Zheng07d400a2009-01-06 11:42:00 -0500714 root->fs_info->csum_root,
715 sums);
Yan Zheng07d400a2009-01-06 11:42:00 -0500716 list_del(&sums->list);
717 kfree(sums);
718 }
Josef Bacik36508602013-04-25 16:23:32 -0400719 if (ret)
720 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500721 } else {
David Sterbab3b4aa72011-04-21 01:20:15 +0200722 btrfs_release_path(path);
Yan Zheng07d400a2009-01-06 11:42:00 -0500723 }
724 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
725 /* inline extents are easy, we just overwrite them */
726 ret = overwrite_item(trans, root, path, eb, slot, key);
Josef Bacik36508602013-04-25 16:23:32 -0400727 if (ret)
728 goto out;
Yan Zheng07d400a2009-01-06 11:42:00 -0500729 }
730
Josef Bacik4bc4bee2013-04-05 20:50:09 +0000731 inode_add_bytes(inode, nbytes);
Tsutomu Itohb9959292012-06-25 21:25:22 -0600732 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -0400733out:
734 if (inode)
735 iput(inode);
736 return ret;
737}
738
739/*
740 * when cleaning up conflicts between the directory names in the
741 * subvolume, directory names in the log and directory names in the
742 * inode back references, we may have to unlink inodes from directories.
743 *
744 * This is a helper function to do the unlink of a specific directory
745 * item
746 */
747static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
748 struct btrfs_root *root,
749 struct btrfs_path *path,
750 struct inode *dir,
751 struct btrfs_dir_item *di)
752{
753 struct inode *inode;
754 char *name;
755 int name_len;
756 struct extent_buffer *leaf;
757 struct btrfs_key location;
758 int ret;
759
760 leaf = path->nodes[0];
761
762 btrfs_dir_item_key_to_cpu(leaf, di, &location);
763 name_len = btrfs_dir_name_len(leaf, di);
764 name = kmalloc(name_len, GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +0000765 if (!name)
766 return -ENOMEM;
767
Chris Masone02119d2008-09-05 16:13:11 -0400768 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
David Sterbab3b4aa72011-04-21 01:20:15 +0200769 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400770
771 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000772 if (!inode) {
Josef Bacik36508602013-04-25 16:23:32 -0400773 ret = -EIO;
774 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +0000775 }
Chris Masone02119d2008-09-05 16:13:11 -0400776
Yan Zhengec051c02009-01-05 15:43:42 -0500777 ret = link_to_fixup_dir(trans, root, path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -0400778 if (ret)
779 goto out;
Chris Mason12fcfd22009-03-24 10:24:20 -0400780
Chris Masone02119d2008-09-05 16:13:11 -0400781 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -0400782 if (ret)
783 goto out;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100784 else
785 ret = btrfs_run_delayed_items(trans, root);
Josef Bacik36508602013-04-25 16:23:32 -0400786out:
787 kfree(name);
788 iput(inode);
Chris Masone02119d2008-09-05 16:13:11 -0400789 return ret;
790}
791
792/*
793 * helper function to see if a given name and sequence number found
794 * in an inode back reference are already in a directory and correctly
795 * point to this inode
796 */
797static noinline int inode_in_dir(struct btrfs_root *root,
798 struct btrfs_path *path,
799 u64 dirid, u64 objectid, u64 index,
800 const char *name, int name_len)
801{
802 struct btrfs_dir_item *di;
803 struct btrfs_key location;
804 int match = 0;
805
806 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
807 index, name, name_len, 0);
808 if (di && !IS_ERR(di)) {
809 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
810 if (location.objectid != objectid)
811 goto out;
812 } else
813 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +0200814 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400815
816 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
817 if (di && !IS_ERR(di)) {
818 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
819 if (location.objectid != objectid)
820 goto out;
821 } else
822 goto out;
823 match = 1;
824out:
David Sterbab3b4aa72011-04-21 01:20:15 +0200825 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -0400826 return match;
827}
828
829/*
830 * helper function to check a log tree for a named back reference in
831 * an inode. This is used to decide if a back reference that is
832 * found in the subvolume conflicts with what we find in the log.
833 *
834 * inode backreferences may have multiple refs in a single item,
835 * during replay we process one reference at a time, and we don't
836 * want to delete valid links to a file from the subvolume if that
837 * link is also in the log.
838 */
839static noinline int backref_in_log(struct btrfs_root *log,
840 struct btrfs_key *key,
Mark Fashehf1863732012-08-08 11:32:27 -0700841 u64 ref_objectid,
Chris Masone02119d2008-09-05 16:13:11 -0400842 char *name, int namelen)
843{
844 struct btrfs_path *path;
845 struct btrfs_inode_ref *ref;
846 unsigned long ptr;
847 unsigned long ptr_end;
848 unsigned long name_ptr;
849 int found_name_len;
850 int item_size;
851 int ret;
852 int match = 0;
853
854 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000855 if (!path)
856 return -ENOMEM;
857
Chris Masone02119d2008-09-05 16:13:11 -0400858 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
859 if (ret != 0)
860 goto out;
861
Chris Masone02119d2008-09-05 16:13:11 -0400862 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
Mark Fashehf1863732012-08-08 11:32:27 -0700863
864 if (key->type == BTRFS_INODE_EXTREF_KEY) {
865 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
866 name, namelen, NULL))
867 match = 1;
868
869 goto out;
870 }
871
872 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -0400873 ptr_end = ptr + item_size;
874 while (ptr < ptr_end) {
875 ref = (struct btrfs_inode_ref *)ptr;
876 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
877 if (found_name_len == namelen) {
878 name_ptr = (unsigned long)(ref + 1);
879 ret = memcmp_extent_buffer(path->nodes[0], name,
880 name_ptr, namelen);
881 if (ret == 0) {
882 match = 1;
883 goto out;
884 }
885 }
886 ptr = (unsigned long)(ref + 1) + found_name_len;
887 }
888out:
889 btrfs_free_path(path);
890 return match;
891}
892
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700893static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
894 struct btrfs_root *root,
895 struct btrfs_path *path,
896 struct btrfs_root *log_root,
897 struct inode *dir, struct inode *inode,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700898 struct extent_buffer *eb,
Mark Fashehf1863732012-08-08 11:32:27 -0700899 u64 inode_objectid, u64 parent_objectid,
900 u64 ref_index, char *name, int namelen,
901 int *search_done)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700902{
903 int ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700904 char *victim_name;
905 int victim_name_len;
906 struct extent_buffer *leaf;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700907 struct btrfs_dir_item *di;
Mark Fashehf1863732012-08-08 11:32:27 -0700908 struct btrfs_key search_key;
909 struct btrfs_inode_extref *extref;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700910
Mark Fashehf1863732012-08-08 11:32:27 -0700911again:
912 /* Search old style refs */
913 search_key.objectid = inode_objectid;
914 search_key.type = BTRFS_INODE_REF_KEY;
915 search_key.offset = parent_objectid;
916 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700917 if (ret == 0) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700918 struct btrfs_inode_ref *victim_ref;
919 unsigned long ptr;
920 unsigned long ptr_end;
Mark Fashehf1863732012-08-08 11:32:27 -0700921
922 leaf = path->nodes[0];
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700923
924 /* are we trying to overwrite a back ref for the root directory
925 * if so, just jump out, we're done
926 */
Mark Fashehf1863732012-08-08 11:32:27 -0700927 if (search_key.objectid == search_key.offset)
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700928 return 1;
929
930 /* check all the names in this back reference to see
931 * if they are in the log. if so, we allow them to stay
932 * otherwise they must be unlinked as a conflict
933 */
934 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
935 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
936 while (ptr < ptr_end) {
937 victim_ref = (struct btrfs_inode_ref *)ptr;
938 victim_name_len = btrfs_inode_ref_name_len(leaf,
939 victim_ref);
940 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -0400941 if (!victim_name)
942 return -ENOMEM;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700943
944 read_extent_buffer(leaf, victim_name,
945 (unsigned long)(victim_ref + 1),
946 victim_name_len);
947
Mark Fashehf1863732012-08-08 11:32:27 -0700948 if (!backref_in_log(log_root, &search_key,
949 parent_objectid,
950 victim_name,
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700951 victim_name_len)) {
Zach Brown8b558c52013-10-16 12:10:34 -0700952 inc_nlink(inode);
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700953 btrfs_release_path(path);
954
955 ret = btrfs_unlink_inode(trans, root, dir,
956 inode, victim_name,
957 victim_name_len);
Mark Fashehf1863732012-08-08 11:32:27 -0700958 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -0400959 if (ret)
960 return ret;
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +0100961 ret = btrfs_run_delayed_items(trans, root);
962 if (ret)
963 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -0700964 *search_done = 1;
965 goto again;
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700966 }
967 kfree(victim_name);
Mark Fashehf1863732012-08-08 11:32:27 -0700968
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700969 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
970 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -0700971
972 /*
973 * NOTE: we have searched root tree and checked the
974 * coresponding ref, it does not need to check again.
975 */
976 *search_done = 1;
977 }
978 btrfs_release_path(path);
979
Mark Fashehf1863732012-08-08 11:32:27 -0700980 /* Same search but for extended refs */
981 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
982 inode_objectid, parent_objectid, 0,
983 0);
984 if (!IS_ERR_OR_NULL(extref)) {
985 u32 item_size;
986 u32 cur_offset = 0;
987 unsigned long base;
988 struct inode *victim_parent;
989
990 leaf = path->nodes[0];
991
992 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
993 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
994
995 while (cur_offset < item_size) {
996 extref = (struct btrfs_inode_extref *)base + cur_offset;
997
998 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
999
1000 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1001 goto next;
1002
1003 victim_name = kmalloc(victim_name_len, GFP_NOFS);
Josef Bacik36508602013-04-25 16:23:32 -04001004 if (!victim_name)
1005 return -ENOMEM;
Mark Fashehf1863732012-08-08 11:32:27 -07001006 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1007 victim_name_len);
1008
1009 search_key.objectid = inode_objectid;
1010 search_key.type = BTRFS_INODE_EXTREF_KEY;
1011 search_key.offset = btrfs_extref_hash(parent_objectid,
1012 victim_name,
1013 victim_name_len);
1014 ret = 0;
1015 if (!backref_in_log(log_root, &search_key,
1016 parent_objectid, victim_name,
1017 victim_name_len)) {
1018 ret = -ENOENT;
1019 victim_parent = read_one_inode(root,
1020 parent_objectid);
1021 if (victim_parent) {
Zach Brown8b558c52013-10-16 12:10:34 -07001022 inc_nlink(inode);
Mark Fashehf1863732012-08-08 11:32:27 -07001023 btrfs_release_path(path);
1024
1025 ret = btrfs_unlink_inode(trans, root,
1026 victim_parent,
1027 inode,
1028 victim_name,
1029 victim_name_len);
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001030 if (!ret)
1031 ret = btrfs_run_delayed_items(
1032 trans, root);
Mark Fashehf1863732012-08-08 11:32:27 -07001033 }
Mark Fashehf1863732012-08-08 11:32:27 -07001034 iput(victim_parent);
1035 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001036 if (ret)
1037 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001038 *search_done = 1;
1039 goto again;
1040 }
1041 kfree(victim_name);
Josef Bacik36508602013-04-25 16:23:32 -04001042 if (ret)
1043 return ret;
Mark Fashehf1863732012-08-08 11:32:27 -07001044next:
1045 cur_offset += victim_name_len + sizeof(*extref);
1046 }
1047 *search_done = 1;
1048 }
1049 btrfs_release_path(path);
1050
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001051 /* look for a conflicting sequence number */
1052 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
Mark Fashehf1863732012-08-08 11:32:27 -07001053 ref_index, name, namelen, 0);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001054 if (di && !IS_ERR(di)) {
1055 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001056 if (ret)
1057 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001058 }
1059 btrfs_release_path(path);
1060
1061 /* look for a conflicing name */
1062 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1063 name, namelen, 0);
1064 if (di && !IS_ERR(di)) {
1065 ret = drop_one_dir_item(trans, root, path, dir, di);
Josef Bacik36508602013-04-25 16:23:32 -04001066 if (ret)
1067 return ret;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001068 }
1069 btrfs_release_path(path);
1070
1071 return 0;
1072}
Chris Masone02119d2008-09-05 16:13:11 -04001073
Mark Fashehf1863732012-08-08 11:32:27 -07001074static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1075 u32 *namelen, char **name, u64 *index,
1076 u64 *parent_objectid)
1077{
1078 struct btrfs_inode_extref *extref;
1079
1080 extref = (struct btrfs_inode_extref *)ref_ptr;
1081
1082 *namelen = btrfs_inode_extref_name_len(eb, extref);
1083 *name = kmalloc(*namelen, GFP_NOFS);
1084 if (*name == NULL)
1085 return -ENOMEM;
1086
1087 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1088 *namelen);
1089
1090 *index = btrfs_inode_extref_index(eb, extref);
1091 if (parent_objectid)
1092 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1093
1094 return 0;
1095}
1096
1097static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1098 u32 *namelen, char **name, u64 *index)
1099{
1100 struct btrfs_inode_ref *ref;
1101
1102 ref = (struct btrfs_inode_ref *)ref_ptr;
1103
1104 *namelen = btrfs_inode_ref_name_len(eb, ref);
1105 *name = kmalloc(*namelen, GFP_NOFS);
1106 if (*name == NULL)
1107 return -ENOMEM;
1108
1109 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1110
1111 *index = btrfs_inode_ref_index(eb, ref);
1112
1113 return 0;
1114}
1115
Chris Masone02119d2008-09-05 16:13:11 -04001116/*
1117 * replay one inode back reference item found in the log tree.
1118 * eb, slot and key refer to the buffer and key found in the log tree.
1119 * root is the destination we are replaying into, and path is for temp
1120 * use by this function. (it should be released on return).
1121 */
1122static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1123 struct btrfs_root *root,
1124 struct btrfs_root *log,
1125 struct btrfs_path *path,
1126 struct extent_buffer *eb, int slot,
1127 struct btrfs_key *key)
1128{
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001129 struct inode *dir = NULL;
1130 struct inode *inode = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04001131 unsigned long ref_ptr;
1132 unsigned long ref_end;
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001133 char *name = NULL;
liubo34f3e4f2011-08-06 08:35:23 +00001134 int namelen;
1135 int ret;
liuboc622ae62011-03-26 08:01:12 -04001136 int search_done = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001137 int log_ref_ver = 0;
1138 u64 parent_objectid;
1139 u64 inode_objectid;
Chris Masonf46dbe3de2012-10-09 11:17:20 -04001140 u64 ref_index = 0;
Mark Fashehf1863732012-08-08 11:32:27 -07001141 int ref_struct_size;
1142
1143 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1144 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1145
1146 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1147 struct btrfs_inode_extref *r;
1148
1149 ref_struct_size = sizeof(struct btrfs_inode_extref);
1150 log_ref_ver = 1;
1151 r = (struct btrfs_inode_extref *)ref_ptr;
1152 parent_objectid = btrfs_inode_extref_parent(eb, r);
1153 } else {
1154 ref_struct_size = sizeof(struct btrfs_inode_ref);
1155 parent_objectid = key->offset;
1156 }
1157 inode_objectid = key->objectid;
Chris Masone02119d2008-09-05 16:13:11 -04001158
Chris Masone02119d2008-09-05 16:13:11 -04001159 /*
1160 * it is possible that we didn't log all the parent directories
1161 * for a given inode. If we don't find the dir, just don't
1162 * copy the back ref in. The link count fixup code will take
1163 * care of the rest
1164 */
Mark Fashehf1863732012-08-08 11:32:27 -07001165 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001166 if (!dir) {
1167 ret = -ENOENT;
1168 goto out;
1169 }
Chris Masone02119d2008-09-05 16:13:11 -04001170
Mark Fashehf1863732012-08-08 11:32:27 -07001171 inode = read_one_inode(root, inode_objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001172 if (!inode) {
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001173 ret = -EIO;
1174 goto out;
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001175 }
Chris Masone02119d2008-09-05 16:13:11 -04001176
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001177 while (ref_ptr < ref_end) {
Mark Fashehf1863732012-08-08 11:32:27 -07001178 if (log_ref_ver) {
1179 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1180 &ref_index, &parent_objectid);
1181 /*
1182 * parent object can change from one array
1183 * item to another.
1184 */
1185 if (!dir)
1186 dir = read_one_inode(root, parent_objectid);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001187 if (!dir) {
1188 ret = -ENOENT;
1189 goto out;
1190 }
Mark Fashehf1863732012-08-08 11:32:27 -07001191 } else {
1192 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1193 &ref_index);
1194 }
1195 if (ret)
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001196 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001197
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001198 /* if we already have a perfect match, we're done */
1199 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
Mark Fashehf1863732012-08-08 11:32:27 -07001200 ref_index, name, namelen)) {
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001201 /*
1202 * look for a conflicting back reference in the
1203 * metadata. if we find one we have to unlink that name
1204 * of the file before we add our new link. Later on, we
1205 * overwrite any existing back reference, and we don't
1206 * want to create dangling pointers in the directory.
1207 */
Chris Masone02119d2008-09-05 16:13:11 -04001208
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001209 if (!search_done) {
1210 ret = __add_inode_ref(trans, root, path, log,
Mark Fashehf1863732012-08-08 11:32:27 -07001211 dir, inode, eb,
1212 inode_objectid,
1213 parent_objectid,
1214 ref_index, name, namelen,
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001215 &search_done);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001216 if (ret) {
1217 if (ret == 1)
1218 ret = 0;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001219 goto out;
Josef Bacik36508602013-04-25 16:23:32 -04001220 }
Chris Masone02119d2008-09-05 16:13:11 -04001221 }
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001222
1223 /* insert our name */
1224 ret = btrfs_add_link(trans, dir, inode, name, namelen,
Mark Fashehf1863732012-08-08 11:32:27 -07001225 0, ref_index);
Josef Bacik36508602013-04-25 16:23:32 -04001226 if (ret)
1227 goto out;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001228
1229 btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001230 }
liuboc622ae62011-03-26 08:01:12 -04001231
Mark Fashehf1863732012-08-08 11:32:27 -07001232 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001233 kfree(name);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001234 name = NULL;
Mark Fashehf1863732012-08-08 11:32:27 -07001235 if (log_ref_ver) {
1236 iput(dir);
1237 dir = NULL;
1238 }
Chris Masone02119d2008-09-05 16:13:11 -04001239 }
Chris Masone02119d2008-09-05 16:13:11 -04001240
1241 /* finally write the back reference in the inode */
1242 ret = overwrite_item(trans, root, path, eb, slot, key);
Jan Schmidt5a1d7842012-08-17 14:04:41 -07001243out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001244 btrfs_release_path(path);
Geyslan G. Bem03b2f082013-10-11 15:35:45 -03001245 kfree(name);
Chris Masone02119d2008-09-05 16:13:11 -04001246 iput(dir);
1247 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001248 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001249}
1250
Yan, Zhengc71bf092009-11-12 09:34:40 +00001251static int insert_orphan_item(struct btrfs_trans_handle *trans,
1252 struct btrfs_root *root, u64 offset)
1253{
1254 int ret;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08001255 ret = btrfs_find_item(root, NULL, BTRFS_ORPHAN_OBJECTID,
1256 offset, BTRFS_ORPHAN_ITEM_KEY, NULL);
Yan, Zhengc71bf092009-11-12 09:34:40 +00001257 if (ret > 0)
1258 ret = btrfs_insert_orphan_item(trans, root, offset);
1259 return ret;
1260}
1261
Mark Fashehf1863732012-08-08 11:32:27 -07001262static int count_inode_extrefs(struct btrfs_root *root,
1263 struct inode *inode, struct btrfs_path *path)
Chris Masone02119d2008-09-05 16:13:11 -04001264{
Mark Fashehf1863732012-08-08 11:32:27 -07001265 int ret = 0;
1266 int name_len;
1267 unsigned int nlink = 0;
1268 u32 item_size;
1269 u32 cur_offset = 0;
1270 u64 inode_objectid = btrfs_ino(inode);
1271 u64 offset = 0;
1272 unsigned long ptr;
1273 struct btrfs_inode_extref *extref;
1274 struct extent_buffer *leaf;
1275
1276 while (1) {
1277 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1278 &extref, &offset);
1279 if (ret)
1280 break;
1281
1282 leaf = path->nodes[0];
1283 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1284 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1285
1286 while (cur_offset < item_size) {
1287 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1288 name_len = btrfs_inode_extref_name_len(leaf, extref);
1289
1290 nlink++;
1291
1292 cur_offset += name_len + sizeof(*extref);
1293 }
1294
1295 offset++;
1296 btrfs_release_path(path);
1297 }
1298 btrfs_release_path(path);
1299
1300 if (ret < 0)
1301 return ret;
1302 return nlink;
1303}
1304
1305static int count_inode_refs(struct btrfs_root *root,
1306 struct inode *inode, struct btrfs_path *path)
1307{
Chris Masone02119d2008-09-05 16:13:11 -04001308 int ret;
1309 struct btrfs_key key;
Mark Fashehf1863732012-08-08 11:32:27 -07001310 unsigned int nlink = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001311 unsigned long ptr;
1312 unsigned long ptr_end;
1313 int name_len;
Li Zefan33345d012011-04-20 10:31:50 +08001314 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001315
Li Zefan33345d012011-04-20 10:31:50 +08001316 key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04001317 key.type = BTRFS_INODE_REF_KEY;
1318 key.offset = (u64)-1;
1319
Chris Masond3977122009-01-05 21:25:51 -05001320 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001321 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1322 if (ret < 0)
1323 break;
1324 if (ret > 0) {
1325 if (path->slots[0] == 0)
1326 break;
1327 path->slots[0]--;
1328 }
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001329process_slot:
Chris Masone02119d2008-09-05 16:13:11 -04001330 btrfs_item_key_to_cpu(path->nodes[0], &key,
1331 path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08001332 if (key.objectid != ino ||
Chris Masone02119d2008-09-05 16:13:11 -04001333 key.type != BTRFS_INODE_REF_KEY)
1334 break;
1335 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1336 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1337 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05001338 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001339 struct btrfs_inode_ref *ref;
1340
1341 ref = (struct btrfs_inode_ref *)ptr;
1342 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1343 ref);
1344 ptr = (unsigned long)(ref + 1) + name_len;
1345 nlink++;
1346 }
1347
1348 if (key.offset == 0)
1349 break;
Filipe David Borba Mananae93ae262013-10-14 22:49:11 +01001350 if (path->slots[0] > 0) {
1351 path->slots[0]--;
1352 goto process_slot;
1353 }
Chris Masone02119d2008-09-05 16:13:11 -04001354 key.offset--;
David Sterbab3b4aa72011-04-21 01:20:15 +02001355 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001356 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001357 btrfs_release_path(path);
Mark Fashehf1863732012-08-08 11:32:27 -07001358
1359 return nlink;
1360}
1361
1362/*
1363 * There are a few corners where the link count of the file can't
1364 * be properly maintained during replay. So, instead of adding
1365 * lots of complexity to the log code, we just scan the backrefs
1366 * for any file that has been through replay.
1367 *
1368 * The scan will update the link count on the inode to reflect the
1369 * number of back refs found. If it goes down to zero, the iput
1370 * will free the inode.
1371 */
1372static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1373 struct btrfs_root *root,
1374 struct inode *inode)
1375{
1376 struct btrfs_path *path;
1377 int ret;
1378 u64 nlink = 0;
1379 u64 ino = btrfs_ino(inode);
1380
1381 path = btrfs_alloc_path();
1382 if (!path)
1383 return -ENOMEM;
1384
1385 ret = count_inode_refs(root, inode, path);
1386 if (ret < 0)
1387 goto out;
1388
1389 nlink = ret;
1390
1391 ret = count_inode_extrefs(root, inode, path);
1392 if (ret == -ENOENT)
1393 ret = 0;
1394
1395 if (ret < 0)
1396 goto out;
1397
1398 nlink += ret;
1399
1400 ret = 0;
1401
Chris Masone02119d2008-09-05 16:13:11 -04001402 if (nlink != inode->i_nlink) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02001403 set_nlink(inode, nlink);
Chris Masone02119d2008-09-05 16:13:11 -04001404 btrfs_update_inode(trans, root, inode);
1405 }
Chris Mason8d5bf1c2008-09-11 15:51:21 -04001406 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001407
Yan, Zhengc71bf092009-11-12 09:34:40 +00001408 if (inode->i_nlink == 0) {
1409 if (S_ISDIR(inode->i_mode)) {
1410 ret = replay_dir_deletes(trans, root, NULL, path,
Li Zefan33345d012011-04-20 10:31:50 +08001411 ino, 1);
Josef Bacik36508602013-04-25 16:23:32 -04001412 if (ret)
1413 goto out;
Yan, Zhengc71bf092009-11-12 09:34:40 +00001414 }
Li Zefan33345d012011-04-20 10:31:50 +08001415 ret = insert_orphan_item(trans, root, ino);
Chris Mason12fcfd22009-03-24 10:24:20 -04001416 }
Chris Mason12fcfd22009-03-24 10:24:20 -04001417
Mark Fashehf1863732012-08-08 11:32:27 -07001418out:
1419 btrfs_free_path(path);
1420 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001421}
1422
1423static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1424 struct btrfs_root *root,
1425 struct btrfs_path *path)
1426{
1427 int ret;
1428 struct btrfs_key key;
1429 struct inode *inode;
1430
1431 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1432 key.type = BTRFS_ORPHAN_ITEM_KEY;
1433 key.offset = (u64)-1;
Chris Masond3977122009-01-05 21:25:51 -05001434 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001435 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1436 if (ret < 0)
1437 break;
1438
1439 if (ret == 1) {
1440 if (path->slots[0] == 0)
1441 break;
1442 path->slots[0]--;
1443 }
1444
1445 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1446 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1447 key.type != BTRFS_ORPHAN_ITEM_KEY)
1448 break;
1449
1450 ret = btrfs_del_item(trans, root, path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001451 if (ret)
1452 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001453
David Sterbab3b4aa72011-04-21 01:20:15 +02001454 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001455 inode = read_one_inode(root, key.offset);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001456 if (!inode)
1457 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001458
1459 ret = fixup_inode_link_count(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001460 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001461 if (ret)
1462 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001463
Chris Mason12fcfd22009-03-24 10:24:20 -04001464 /*
1465 * fixup on a directory may create new entries,
1466 * make sure we always look for the highset possible
1467 * offset
1468 */
1469 key.offset = (u64)-1;
Chris Masone02119d2008-09-05 16:13:11 -04001470 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001471 ret = 0;
1472out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001473 btrfs_release_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00001474 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001475}
1476
1477
1478/*
1479 * record a given inode in the fixup dir so we can check its link
1480 * count when replay is done. The link count is incremented here
1481 * so the inode won't go away until we check it
1482 */
1483static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1484 struct btrfs_root *root,
1485 struct btrfs_path *path,
1486 u64 objectid)
1487{
1488 struct btrfs_key key;
1489 int ret = 0;
1490 struct inode *inode;
1491
1492 inode = read_one_inode(root, objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001493 if (!inode)
1494 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001495
1496 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1497 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1498 key.offset = objectid;
1499
1500 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1501
David Sterbab3b4aa72011-04-21 01:20:15 +02001502 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001503 if (ret == 0) {
Josef Bacik9bf7a482013-03-01 13:35:47 -05001504 if (!inode->i_nlink)
1505 set_nlink(inode, 1);
1506 else
Zach Brown8b558c52013-10-16 12:10:34 -07001507 inc_nlink(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06001508 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001509 } else if (ret == -EEXIST) {
1510 ret = 0;
1511 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001512 BUG(); /* Logic Error */
Chris Masone02119d2008-09-05 16:13:11 -04001513 }
1514 iput(inode);
1515
1516 return ret;
1517}
1518
1519/*
1520 * when replaying the log for a directory, we only insert names
1521 * for inodes that actually exist. This means an fsync on a directory
1522 * does not implicitly fsync all the new files in it
1523 */
1524static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1525 struct btrfs_root *root,
1526 struct btrfs_path *path,
1527 u64 dirid, u64 index,
1528 char *name, int name_len, u8 type,
1529 struct btrfs_key *location)
1530{
1531 struct inode *inode;
1532 struct inode *dir;
1533 int ret;
1534
1535 inode = read_one_inode(root, location->objectid);
1536 if (!inode)
1537 return -ENOENT;
1538
1539 dir = read_one_inode(root, dirid);
1540 if (!dir) {
1541 iput(inode);
1542 return -EIO;
1543 }
Josef Bacikd5554382013-09-11 14:17:00 -04001544
Chris Masone02119d2008-09-05 16:13:11 -04001545 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1546
1547 /* FIXME, put inode into FIXUP list */
1548
1549 iput(inode);
1550 iput(dir);
1551 return ret;
1552}
1553
1554/*
1555 * take a single entry in a log directory item and replay it into
1556 * the subvolume.
1557 *
1558 * if a conflicting item exists in the subdirectory already,
1559 * the inode it points to is unlinked and put into the link count
1560 * fix up tree.
1561 *
1562 * If a name from the log points to a file or directory that does
1563 * not exist in the FS, it is skipped. fsyncs on directories
1564 * do not force down inodes inside that directory, just changes to the
1565 * names or unlinks in a directory.
1566 */
1567static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1568 struct btrfs_root *root,
1569 struct btrfs_path *path,
1570 struct extent_buffer *eb,
1571 struct btrfs_dir_item *di,
1572 struct btrfs_key *key)
1573{
1574 char *name;
1575 int name_len;
1576 struct btrfs_dir_item *dst_di;
1577 struct btrfs_key found_key;
1578 struct btrfs_key log_key;
1579 struct inode *dir;
Chris Masone02119d2008-09-05 16:13:11 -04001580 u8 log_type;
Chris Mason4bef0842008-09-08 11:18:08 -04001581 int exists;
Josef Bacik36508602013-04-25 16:23:32 -04001582 int ret = 0;
Josef Bacikd5554382013-09-11 14:17:00 -04001583 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
Chris Masone02119d2008-09-05 16:13:11 -04001584
1585 dir = read_one_inode(root, key->objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001586 if (!dir)
1587 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001588
1589 name_len = btrfs_dir_name_len(eb, di);
1590 name = kmalloc(name_len, GFP_NOFS);
Filipe David Borba Manana2bac3252013-08-04 19:58:57 +01001591 if (!name) {
1592 ret = -ENOMEM;
1593 goto out;
1594 }
liubo2a29edc2011-01-26 06:22:08 +00001595
Chris Masone02119d2008-09-05 16:13:11 -04001596 log_type = btrfs_dir_type(eb, di);
1597 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1598 name_len);
1599
1600 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
Chris Mason4bef0842008-09-08 11:18:08 -04001601 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1602 if (exists == 0)
1603 exists = 1;
1604 else
1605 exists = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02001606 btrfs_release_path(path);
Chris Mason4bef0842008-09-08 11:18:08 -04001607
Chris Masone02119d2008-09-05 16:13:11 -04001608 if (key->type == BTRFS_DIR_ITEM_KEY) {
1609 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1610 name, name_len, 1);
Chris Masond3977122009-01-05 21:25:51 -05001611 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001612 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1613 key->objectid,
1614 key->offset, name,
1615 name_len, 1);
1616 } else {
Josef Bacik36508602013-04-25 16:23:32 -04001617 /* Corruption */
1618 ret = -EINVAL;
1619 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001620 }
David Sterbac7040052011-04-19 18:00:01 +02001621 if (IS_ERR_OR_NULL(dst_di)) {
Chris Masone02119d2008-09-05 16:13:11 -04001622 /* we need a sequence number to insert, so we only
1623 * do inserts for the BTRFS_DIR_INDEX_KEY types
1624 */
1625 if (key->type != BTRFS_DIR_INDEX_KEY)
1626 goto out;
1627 goto insert;
1628 }
1629
1630 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1631 /* the existing item matches the logged item */
1632 if (found_key.objectid == log_key.objectid &&
1633 found_key.type == log_key.type &&
1634 found_key.offset == log_key.offset &&
1635 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1636 goto out;
1637 }
1638
1639 /*
1640 * don't drop the conflicting directory entry if the inode
1641 * for the new entry doesn't exist
1642 */
Chris Mason4bef0842008-09-08 11:18:08 -04001643 if (!exists)
Chris Masone02119d2008-09-05 16:13:11 -04001644 goto out;
1645
Chris Masone02119d2008-09-05 16:13:11 -04001646 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
Josef Bacik36508602013-04-25 16:23:32 -04001647 if (ret)
1648 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001649
1650 if (key->type == BTRFS_DIR_INDEX_KEY)
1651 goto insert;
1652out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001653 btrfs_release_path(path);
Josef Bacikd5554382013-09-11 14:17:00 -04001654 if (!ret && update_size) {
1655 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1656 ret = btrfs_update_inode(trans, root, dir);
1657 }
Chris Masone02119d2008-09-05 16:13:11 -04001658 kfree(name);
1659 iput(dir);
Josef Bacik36508602013-04-25 16:23:32 -04001660 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001661
1662insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02001663 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001664 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1665 name, name_len, log_type, &log_key);
Josef Bacik36508602013-04-25 16:23:32 -04001666 if (ret && ret != -ENOENT)
1667 goto out;
Josef Bacikd5554382013-09-11 14:17:00 -04001668 update_size = false;
Josef Bacik36508602013-04-25 16:23:32 -04001669 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001670 goto out;
1671}
1672
1673/*
1674 * find all the names in a directory item and reconcile them into
1675 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1676 * one name in a directory item, but the same code gets used for
1677 * both directory index types
1678 */
1679static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1680 struct btrfs_root *root,
1681 struct btrfs_path *path,
1682 struct extent_buffer *eb, int slot,
1683 struct btrfs_key *key)
1684{
1685 int ret;
1686 u32 item_size = btrfs_item_size_nr(eb, slot);
1687 struct btrfs_dir_item *di;
1688 int name_len;
1689 unsigned long ptr;
1690 unsigned long ptr_end;
1691
1692 ptr = btrfs_item_ptr_offset(eb, slot);
1693 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001694 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001695 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001696 if (verify_dir_item(root, eb, di))
1697 return -EIO;
Chris Masone02119d2008-09-05 16:13:11 -04001698 name_len = btrfs_dir_name_len(eb, di);
1699 ret = replay_one_name(trans, root, path, eb, di, key);
Josef Bacik36508602013-04-25 16:23:32 -04001700 if (ret)
1701 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04001702 ptr = (unsigned long)(di + 1);
1703 ptr += name_len;
1704 }
1705 return 0;
1706}
1707
1708/*
1709 * directory replay has two parts. There are the standard directory
1710 * items in the log copied from the subvolume, and range items
1711 * created in the log while the subvolume was logged.
1712 *
1713 * The range items tell us which parts of the key space the log
1714 * is authoritative for. During replay, if a key in the subvolume
1715 * directory is in a logged range item, but not actually in the log
1716 * that means it was deleted from the directory before the fsync
1717 * and should be removed.
1718 */
1719static noinline int find_dir_range(struct btrfs_root *root,
1720 struct btrfs_path *path,
1721 u64 dirid, int key_type,
1722 u64 *start_ret, u64 *end_ret)
1723{
1724 struct btrfs_key key;
1725 u64 found_end;
1726 struct btrfs_dir_log_item *item;
1727 int ret;
1728 int nritems;
1729
1730 if (*start_ret == (u64)-1)
1731 return 1;
1732
1733 key.objectid = dirid;
1734 key.type = key_type;
1735 key.offset = *start_ret;
1736
1737 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1738 if (ret < 0)
1739 goto out;
1740 if (ret > 0) {
1741 if (path->slots[0] == 0)
1742 goto out;
1743 path->slots[0]--;
1744 }
1745 if (ret != 0)
1746 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1747
1748 if (key.type != key_type || key.objectid != dirid) {
1749 ret = 1;
1750 goto next;
1751 }
1752 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1753 struct btrfs_dir_log_item);
1754 found_end = btrfs_dir_log_end(path->nodes[0], item);
1755
1756 if (*start_ret >= key.offset && *start_ret <= found_end) {
1757 ret = 0;
1758 *start_ret = key.offset;
1759 *end_ret = found_end;
1760 goto out;
1761 }
1762 ret = 1;
1763next:
1764 /* check the next slot in the tree to see if it is a valid item */
1765 nritems = btrfs_header_nritems(path->nodes[0]);
1766 if (path->slots[0] >= nritems) {
1767 ret = btrfs_next_leaf(root, path);
1768 if (ret)
1769 goto out;
1770 } else {
1771 path->slots[0]++;
1772 }
1773
1774 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1775
1776 if (key.type != key_type || key.objectid != dirid) {
1777 ret = 1;
1778 goto out;
1779 }
1780 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1781 struct btrfs_dir_log_item);
1782 found_end = btrfs_dir_log_end(path->nodes[0], item);
1783 *start_ret = key.offset;
1784 *end_ret = found_end;
1785 ret = 0;
1786out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001787 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001788 return ret;
1789}
1790
1791/*
1792 * this looks for a given directory item in the log. If the directory
1793 * item is not in the log, the item is removed and the inode it points
1794 * to is unlinked
1795 */
1796static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1797 struct btrfs_root *root,
1798 struct btrfs_root *log,
1799 struct btrfs_path *path,
1800 struct btrfs_path *log_path,
1801 struct inode *dir,
1802 struct btrfs_key *dir_key)
1803{
1804 int ret;
1805 struct extent_buffer *eb;
1806 int slot;
1807 u32 item_size;
1808 struct btrfs_dir_item *di;
1809 struct btrfs_dir_item *log_di;
1810 int name_len;
1811 unsigned long ptr;
1812 unsigned long ptr_end;
1813 char *name;
1814 struct inode *inode;
1815 struct btrfs_key location;
1816
1817again:
1818 eb = path->nodes[0];
1819 slot = path->slots[0];
1820 item_size = btrfs_item_size_nr(eb, slot);
1821 ptr = btrfs_item_ptr_offset(eb, slot);
1822 ptr_end = ptr + item_size;
Chris Masond3977122009-01-05 21:25:51 -05001823 while (ptr < ptr_end) {
Chris Masone02119d2008-09-05 16:13:11 -04001824 di = (struct btrfs_dir_item *)ptr;
Josef Bacik22a94d42011-03-16 16:47:17 -04001825 if (verify_dir_item(root, eb, di)) {
1826 ret = -EIO;
1827 goto out;
1828 }
1829
Chris Masone02119d2008-09-05 16:13:11 -04001830 name_len = btrfs_dir_name_len(eb, di);
1831 name = kmalloc(name_len, GFP_NOFS);
1832 if (!name) {
1833 ret = -ENOMEM;
1834 goto out;
1835 }
1836 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1837 name_len);
1838 log_di = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04001839 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001840 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1841 dir_key->objectid,
1842 name, name_len, 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04001843 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04001844 log_di = btrfs_lookup_dir_index_item(trans, log,
1845 log_path,
1846 dir_key->objectid,
1847 dir_key->offset,
1848 name, name_len, 0);
1849 }
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001850 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
Chris Masone02119d2008-09-05 16:13:11 -04001851 btrfs_dir_item_key_to_cpu(eb, di, &location);
David Sterbab3b4aa72011-04-21 01:20:15 +02001852 btrfs_release_path(path);
1853 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001854 inode = read_one_inode(root, location.objectid);
Tsutomu Itohc00e9492011-04-28 09:10:23 +00001855 if (!inode) {
1856 kfree(name);
1857 return -EIO;
1858 }
Chris Masone02119d2008-09-05 16:13:11 -04001859
1860 ret = link_to_fixup_dir(trans, root,
1861 path, location.objectid);
Josef Bacik36508602013-04-25 16:23:32 -04001862 if (ret) {
1863 kfree(name);
1864 iput(inode);
1865 goto out;
1866 }
1867
Zach Brown8b558c52013-10-16 12:10:34 -07001868 inc_nlink(inode);
Chris Masone02119d2008-09-05 16:13:11 -04001869 ret = btrfs_unlink_inode(trans, root, dir, inode,
1870 name, name_len);
Josef Bacik36508602013-04-25 16:23:32 -04001871 if (!ret)
Filipe David Borba Mananaada9af22013-08-05 09:25:47 +01001872 ret = btrfs_run_delayed_items(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001873 kfree(name);
1874 iput(inode);
Josef Bacik36508602013-04-25 16:23:32 -04001875 if (ret)
1876 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001877
1878 /* there might still be more names under this key
1879 * check and repeat if required
1880 */
1881 ret = btrfs_search_slot(NULL, root, dir_key, path,
1882 0, 0);
1883 if (ret == 0)
1884 goto again;
1885 ret = 0;
1886 goto out;
Filipe David Borba Manana269d0402013-10-28 17:39:21 +00001887 } else if (IS_ERR(log_di)) {
1888 kfree(name);
1889 return PTR_ERR(log_di);
Chris Masone02119d2008-09-05 16:13:11 -04001890 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001891 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001892 kfree(name);
1893
1894 ptr = (unsigned long)(di + 1);
1895 ptr += name_len;
1896 }
1897 ret = 0;
1898out:
David Sterbab3b4aa72011-04-21 01:20:15 +02001899 btrfs_release_path(path);
1900 btrfs_release_path(log_path);
Chris Masone02119d2008-09-05 16:13:11 -04001901 return ret;
1902}
1903
1904/*
1905 * deletion replay happens before we copy any new directory items
1906 * out of the log or out of backreferences from inodes. It
1907 * scans the log to find ranges of keys that log is authoritative for,
1908 * and then scans the directory to find items in those ranges that are
1909 * not present in the log.
1910 *
1911 * Anything we don't find in the log is unlinked and removed from the
1912 * directory.
1913 */
1914static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1915 struct btrfs_root *root,
1916 struct btrfs_root *log,
1917 struct btrfs_path *path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001918 u64 dirid, int del_all)
Chris Masone02119d2008-09-05 16:13:11 -04001919{
1920 u64 range_start;
1921 u64 range_end;
1922 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1923 int ret = 0;
1924 struct btrfs_key dir_key;
1925 struct btrfs_key found_key;
1926 struct btrfs_path *log_path;
1927 struct inode *dir;
1928
1929 dir_key.objectid = dirid;
1930 dir_key.type = BTRFS_DIR_ITEM_KEY;
1931 log_path = btrfs_alloc_path();
1932 if (!log_path)
1933 return -ENOMEM;
1934
1935 dir = read_one_inode(root, dirid);
1936 /* it isn't an error if the inode isn't there, that can happen
1937 * because we replay the deletes before we copy in the inode item
1938 * from the log
1939 */
1940 if (!dir) {
1941 btrfs_free_path(log_path);
1942 return 0;
1943 }
1944again:
1945 range_start = 0;
1946 range_end = 0;
Chris Masond3977122009-01-05 21:25:51 -05001947 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001948 if (del_all)
1949 range_end = (u64)-1;
1950 else {
1951 ret = find_dir_range(log, path, dirid, key_type,
1952 &range_start, &range_end);
1953 if (ret != 0)
1954 break;
1955 }
Chris Masone02119d2008-09-05 16:13:11 -04001956
1957 dir_key.offset = range_start;
Chris Masond3977122009-01-05 21:25:51 -05001958 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04001959 int nritems;
1960 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1961 0, 0);
1962 if (ret < 0)
1963 goto out;
1964
1965 nritems = btrfs_header_nritems(path->nodes[0]);
1966 if (path->slots[0] >= nritems) {
1967 ret = btrfs_next_leaf(root, path);
1968 if (ret)
1969 break;
1970 }
1971 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1972 path->slots[0]);
1973 if (found_key.objectid != dirid ||
1974 found_key.type != dir_key.type)
1975 goto next_type;
1976
1977 if (found_key.offset > range_end)
1978 break;
1979
1980 ret = check_item_in_log(trans, root, log, path,
Chris Mason12fcfd22009-03-24 10:24:20 -04001981 log_path, dir,
1982 &found_key);
Josef Bacik36508602013-04-25 16:23:32 -04001983 if (ret)
1984 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04001985 if (found_key.offset == (u64)-1)
1986 break;
1987 dir_key.offset = found_key.offset + 1;
1988 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001989 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001990 if (range_end == (u64)-1)
1991 break;
1992 range_start = range_end + 1;
1993 }
1994
1995next_type:
1996 ret = 0;
1997 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1998 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1999 dir_key.type = BTRFS_DIR_INDEX_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002000 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002001 goto again;
2002 }
2003out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002004 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002005 btrfs_free_path(log_path);
2006 iput(dir);
2007 return ret;
2008}
2009
2010/*
2011 * the process_func used to replay items from the log tree. This
2012 * gets called in two different stages. The first stage just looks
2013 * for inodes and makes sure they are all copied into the subvolume.
2014 *
2015 * The second stage copies all the other item types from the log into
2016 * the subvolume. The two stage approach is slower, but gets rid of
2017 * lots of complexity around inodes referencing other inodes that exist
2018 * only in the log (references come from either directory items or inode
2019 * back refs).
2020 */
2021static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2022 struct walk_control *wc, u64 gen)
2023{
2024 int nritems;
2025 struct btrfs_path *path;
2026 struct btrfs_root *root = wc->replay_dest;
2027 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002028 int level;
2029 int i;
2030 int ret;
2031
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002032 ret = btrfs_read_buffer(eb, gen);
2033 if (ret)
2034 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002035
2036 level = btrfs_header_level(eb);
2037
2038 if (level != 0)
2039 return 0;
2040
2041 path = btrfs_alloc_path();
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002042 if (!path)
2043 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002044
2045 nritems = btrfs_header_nritems(eb);
2046 for (i = 0; i < nritems; i++) {
2047 btrfs_item_key_to_cpu(eb, &key, i);
Chris Masone02119d2008-09-05 16:13:11 -04002048
2049 /* inode keys are done during the first stage */
2050 if (key.type == BTRFS_INODE_ITEM_KEY &&
2051 wc->stage == LOG_WALK_REPLAY_INODES) {
Chris Masone02119d2008-09-05 16:13:11 -04002052 struct btrfs_inode_item *inode_item;
2053 u32 mode;
2054
2055 inode_item = btrfs_item_ptr(eb, i,
2056 struct btrfs_inode_item);
2057 mode = btrfs_inode_mode(eb, inode_item);
2058 if (S_ISDIR(mode)) {
2059 ret = replay_dir_deletes(wc->trans,
Chris Mason12fcfd22009-03-24 10:24:20 -04002060 root, log, path, key.objectid, 0);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002061 if (ret)
2062 break;
Chris Masone02119d2008-09-05 16:13:11 -04002063 }
2064 ret = overwrite_item(wc->trans, root, path,
2065 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002066 if (ret)
2067 break;
Chris Masone02119d2008-09-05 16:13:11 -04002068
Yan, Zhengc71bf092009-11-12 09:34:40 +00002069 /* for regular files, make sure corresponding
2070 * orhpan item exist. extents past the new EOF
2071 * will be truncated later by orphan cleanup.
Chris Masone02119d2008-09-05 16:13:11 -04002072 */
2073 if (S_ISREG(mode)) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00002074 ret = insert_orphan_item(wc->trans, root,
2075 key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002076 if (ret)
2077 break;
Chris Masone02119d2008-09-05 16:13:11 -04002078 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00002079
Chris Masone02119d2008-09-05 16:13:11 -04002080 ret = link_to_fixup_dir(wc->trans, root,
2081 path, key.objectid);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002082 if (ret)
2083 break;
Chris Masone02119d2008-09-05 16:13:11 -04002084 }
Josef Bacikdd8e7212013-09-11 11:57:23 -04002085
2086 if (key.type == BTRFS_DIR_INDEX_KEY &&
2087 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2088 ret = replay_one_dir_item(wc->trans, root, path,
2089 eb, i, &key);
2090 if (ret)
2091 break;
2092 }
2093
Chris Masone02119d2008-09-05 16:13:11 -04002094 if (wc->stage < LOG_WALK_REPLAY_ALL)
2095 continue;
2096
2097 /* these keys are simply copied */
2098 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2099 ret = overwrite_item(wc->trans, root, path,
2100 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002101 if (ret)
2102 break;
Liu Bo2da1c662013-05-26 13:50:29 +00002103 } else if (key.type == BTRFS_INODE_REF_KEY ||
2104 key.type == BTRFS_INODE_EXTREF_KEY) {
Mark Fashehf1863732012-08-08 11:32:27 -07002105 ret = add_inode_ref(wc->trans, root, log, path,
2106 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002107 if (ret && ret != -ENOENT)
2108 break;
2109 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002110 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2111 ret = replay_one_extent(wc->trans, root, path,
2112 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002113 if (ret)
2114 break;
Josef Bacikdd8e7212013-09-11 11:57:23 -04002115 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
Chris Masone02119d2008-09-05 16:13:11 -04002116 ret = replay_one_dir_item(wc->trans, root, path,
2117 eb, i, &key);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002118 if (ret)
2119 break;
Chris Masone02119d2008-09-05 16:13:11 -04002120 }
2121 }
2122 btrfs_free_path(path);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002123 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002124}
2125
Chris Masond3977122009-01-05 21:25:51 -05002126static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002127 struct btrfs_root *root,
2128 struct btrfs_path *path, int *level,
2129 struct walk_control *wc)
2130{
2131 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002132 u64 bytenr;
2133 u64 ptr_gen;
2134 struct extent_buffer *next;
2135 struct extent_buffer *cur;
2136 struct extent_buffer *parent;
2137 u32 blocksize;
2138 int ret = 0;
2139
2140 WARN_ON(*level < 0);
2141 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2142
Chris Masond3977122009-01-05 21:25:51 -05002143 while (*level > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002144 WARN_ON(*level < 0);
2145 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2146 cur = path->nodes[*level];
2147
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302148 WARN_ON(btrfs_header_level(cur) != *level);
Chris Masone02119d2008-09-05 16:13:11 -04002149
2150 if (path->slots[*level] >=
2151 btrfs_header_nritems(cur))
2152 break;
2153
2154 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2155 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2156 blocksize = btrfs_level_size(root, *level - 1);
2157
2158 parent = path->nodes[*level];
2159 root_owner = btrfs_header_owner(parent);
Chris Masone02119d2008-09-05 16:13:11 -04002160
2161 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
liubo2a29edc2011-01-26 06:22:08 +00002162 if (!next)
2163 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002164
Chris Masone02119d2008-09-05 16:13:11 -04002165 if (*level == 1) {
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002166 ret = wc->process_func(root, next, wc, ptr_gen);
Josef Bacikb50c6e22013-04-25 15:55:30 -04002167 if (ret) {
2168 free_extent_buffer(next);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002169 return ret;
Josef Bacikb50c6e22013-04-25 15:55:30 -04002170 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002171
Chris Masone02119d2008-09-05 16:13:11 -04002172 path->slots[*level]++;
2173 if (wc->free) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002174 ret = btrfs_read_buffer(next, ptr_gen);
2175 if (ret) {
2176 free_extent_buffer(next);
2177 return ret;
2178 }
Chris Masone02119d2008-09-05 16:13:11 -04002179
Josef Bacik681ae502013-10-07 15:11:00 -04002180 if (trans) {
2181 btrfs_tree_lock(next);
2182 btrfs_set_lock_blocking(next);
2183 clean_tree_block(trans, root, next);
2184 btrfs_wait_tree_block_writeback(next);
2185 btrfs_tree_unlock(next);
2186 }
Chris Masone02119d2008-09-05 16:13:11 -04002187
Chris Masone02119d2008-09-05 16:13:11 -04002188 WARN_ON(root_owner !=
2189 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002190 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masond00aff02008-09-11 15:54:42 -04002191 bytenr, blocksize);
Josef Bacik36508602013-04-25 16:23:32 -04002192 if (ret) {
2193 free_extent_buffer(next);
2194 return ret;
2195 }
Chris Masone02119d2008-09-05 16:13:11 -04002196 }
2197 free_extent_buffer(next);
2198 continue;
2199 }
Tsutomu Itoh018642a2012-05-29 18:10:13 +09002200 ret = btrfs_read_buffer(next, ptr_gen);
2201 if (ret) {
2202 free_extent_buffer(next);
2203 return ret;
2204 }
Chris Masone02119d2008-09-05 16:13:11 -04002205
2206 WARN_ON(*level <= 0);
2207 if (path->nodes[*level-1])
2208 free_extent_buffer(path->nodes[*level-1]);
2209 path->nodes[*level-1] = next;
2210 *level = btrfs_header_level(next);
2211 path->slots[*level] = 0;
2212 cond_resched();
2213 }
2214 WARN_ON(*level < 0);
2215 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2216
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002217 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
Chris Masone02119d2008-09-05 16:13:11 -04002218
2219 cond_resched();
2220 return 0;
2221}
2222
Chris Masond3977122009-01-05 21:25:51 -05002223static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002224 struct btrfs_root *root,
2225 struct btrfs_path *path, int *level,
2226 struct walk_control *wc)
2227{
2228 u64 root_owner;
Chris Masone02119d2008-09-05 16:13:11 -04002229 int i;
2230 int slot;
2231 int ret;
2232
Chris Masond3977122009-01-05 21:25:51 -05002233 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Masone02119d2008-09-05 16:13:11 -04002234 slot = path->slots[i];
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002235 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
Chris Masone02119d2008-09-05 16:13:11 -04002236 path->slots[i]++;
2237 *level = i;
2238 WARN_ON(*level == 0);
2239 return 0;
2240 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002241 struct extent_buffer *parent;
2242 if (path->nodes[*level] == root->node)
2243 parent = path->nodes[*level];
2244 else
2245 parent = path->nodes[*level + 1];
2246
2247 root_owner = btrfs_header_owner(parent);
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002248 ret = wc->process_func(root, path->nodes[*level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002249 btrfs_header_generation(path->nodes[*level]));
Mark Fasheh1e5063d2011-07-12 10:46:06 -07002250 if (ret)
2251 return ret;
2252
Chris Masone02119d2008-09-05 16:13:11 -04002253 if (wc->free) {
2254 struct extent_buffer *next;
2255
2256 next = path->nodes[*level];
2257
Josef Bacik681ae502013-10-07 15:11:00 -04002258 if (trans) {
2259 btrfs_tree_lock(next);
2260 btrfs_set_lock_blocking(next);
2261 clean_tree_block(trans, root, next);
2262 btrfs_wait_tree_block_writeback(next);
2263 btrfs_tree_unlock(next);
2264 }
Chris Masone02119d2008-09-05 16:13:11 -04002265
Chris Masone02119d2008-09-05 16:13:11 -04002266 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002267 ret = btrfs_free_and_pin_reserved_extent(root,
Chris Masone02119d2008-09-05 16:13:11 -04002268 path->nodes[*level]->start,
Chris Masond00aff02008-09-11 15:54:42 -04002269 path->nodes[*level]->len);
Josef Bacik36508602013-04-25 16:23:32 -04002270 if (ret)
2271 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002272 }
2273 free_extent_buffer(path->nodes[*level]);
2274 path->nodes[*level] = NULL;
2275 *level = i + 1;
2276 }
2277 }
2278 return 1;
2279}
2280
2281/*
2282 * drop the reference count on the tree rooted at 'snap'. This traverses
2283 * the tree freeing any blocks that have a ref count of zero after being
2284 * decremented.
2285 */
2286static int walk_log_tree(struct btrfs_trans_handle *trans,
2287 struct btrfs_root *log, struct walk_control *wc)
2288{
2289 int ret = 0;
2290 int wret;
2291 int level;
2292 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -04002293 int orig_level;
2294
2295 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002296 if (!path)
2297 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04002298
2299 level = btrfs_header_level(log->node);
2300 orig_level = level;
2301 path->nodes[level] = log->node;
2302 extent_buffer_get(log->node);
2303 path->slots[level] = 0;
2304
Chris Masond3977122009-01-05 21:25:51 -05002305 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04002306 wret = walk_down_log_tree(trans, log, path, &level, wc);
2307 if (wret > 0)
2308 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002309 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002310 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002311 goto out;
2312 }
Chris Masone02119d2008-09-05 16:13:11 -04002313
2314 wret = walk_up_log_tree(trans, log, path, &level, wc);
2315 if (wret > 0)
2316 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002317 if (wret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002318 ret = wret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002319 goto out;
2320 }
Chris Masone02119d2008-09-05 16:13:11 -04002321 }
2322
2323 /* was the root node processed? if not, catch it here */
2324 if (path->nodes[orig_level]) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002325 ret = wc->process_func(log, path->nodes[orig_level], wc,
Chris Masone02119d2008-09-05 16:13:11 -04002326 btrfs_header_generation(path->nodes[orig_level]));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002327 if (ret)
2328 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002329 if (wc->free) {
2330 struct extent_buffer *next;
2331
2332 next = path->nodes[orig_level];
2333
Josef Bacik681ae502013-10-07 15:11:00 -04002334 if (trans) {
2335 btrfs_tree_lock(next);
2336 btrfs_set_lock_blocking(next);
2337 clean_tree_block(trans, log, next);
2338 btrfs_wait_tree_block_writeback(next);
2339 btrfs_tree_unlock(next);
2340 }
Chris Masone02119d2008-09-05 16:13:11 -04002341
Chris Masone02119d2008-09-05 16:13:11 -04002342 WARN_ON(log->root_key.objectid !=
2343 BTRFS_TREE_LOG_OBJECTID);
Chris Masone688b7252011-10-31 20:52:39 -04002344 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
Chris Masond00aff02008-09-11 15:54:42 -04002345 next->len);
Josef Bacik36508602013-04-25 16:23:32 -04002346 if (ret)
2347 goto out;
Chris Masone02119d2008-09-05 16:13:11 -04002348 }
2349 }
2350
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002351out:
Chris Masone02119d2008-09-05 16:13:11 -04002352 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002353 return ret;
2354}
2355
Yan Zheng7237f1832009-01-21 12:54:03 -05002356/*
2357 * helper function to update the item for a given subvolumes log root
2358 * in the tree of log roots
2359 */
2360static int update_log_root(struct btrfs_trans_handle *trans,
2361 struct btrfs_root *log)
2362{
2363 int ret;
2364
2365 if (log->log_transid == 1) {
2366 /* insert root item on the first sync */
2367 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2368 &log->root_key, &log->root_item);
2369 } else {
2370 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2371 &log->root_key, &log->root_item);
2372 }
2373 return ret;
2374}
2375
Miao Xie8b050d32014-02-20 18:08:58 +08002376static void wait_log_commit(struct btrfs_trans_handle *trans,
2377 struct btrfs_root *root, int transid)
Chris Masone02119d2008-09-05 16:13:11 -04002378{
2379 DEFINE_WAIT(wait);
Yan Zheng7237f1832009-01-21 12:54:03 -05002380 int index = transid % 2;
Chris Masone02119d2008-09-05 16:13:11 -04002381
Yan Zheng7237f1832009-01-21 12:54:03 -05002382 /*
2383 * we only allow two pending log transactions at a time,
2384 * so we know that if ours is more than 2 older than the
2385 * current transaction, we're done
2386 */
Chris Masone02119d2008-09-05 16:13:11 -04002387 do {
Yan Zheng7237f1832009-01-21 12:54:03 -05002388 prepare_to_wait(&root->log_commit_wait[index],
2389 &wait, TASK_UNINTERRUPTIBLE);
2390 mutex_unlock(&root->log_mutex);
Chris Mason12fcfd22009-03-24 10:24:20 -04002391
Miao Xied1433de2014-02-20 18:08:59 +08002392 if (root->log_transid_committed < transid &&
Yan Zheng7237f1832009-01-21 12:54:03 -05002393 atomic_read(&root->log_commit[index]))
Chris Masone02119d2008-09-05 16:13:11 -04002394 schedule();
Chris Mason12fcfd22009-03-24 10:24:20 -04002395
Yan Zheng7237f1832009-01-21 12:54:03 -05002396 finish_wait(&root->log_commit_wait[index], &wait);
2397 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002398 } while (root->log_transid_committed < transid &&
Yan Zheng7237f1832009-01-21 12:54:03 -05002399 atomic_read(&root->log_commit[index]));
Yan Zheng7237f1832009-01-21 12:54:03 -05002400}
2401
Jeff Mahoney143bede2012-03-01 14:56:26 +01002402static void wait_for_writer(struct btrfs_trans_handle *trans,
2403 struct btrfs_root *root)
Yan Zheng7237f1832009-01-21 12:54:03 -05002404{
2405 DEFINE_WAIT(wait);
Miao Xie8b050d32014-02-20 18:08:58 +08002406
2407 while (atomic_read(&root->log_writers)) {
Yan Zheng7237f1832009-01-21 12:54:03 -05002408 prepare_to_wait(&root->log_writer_wait,
2409 &wait, TASK_UNINTERRUPTIBLE);
2410 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002411 if (atomic_read(&root->log_writers))
Yan Zheng7237f1832009-01-21 12:54:03 -05002412 schedule();
2413 mutex_lock(&root->log_mutex);
2414 finish_wait(&root->log_writer_wait, &wait);
2415 }
Chris Masone02119d2008-09-05 16:13:11 -04002416}
2417
Miao Xie8b050d32014-02-20 18:08:58 +08002418static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2419 struct btrfs_log_ctx *ctx)
2420{
2421 if (!ctx)
2422 return;
2423
2424 mutex_lock(&root->log_mutex);
2425 list_del_init(&ctx->list);
2426 mutex_unlock(&root->log_mutex);
2427}
2428
2429/*
2430 * Invoked in log mutex context, or be sure there is no other task which
2431 * can access the list.
2432 */
2433static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2434 int index, int error)
2435{
2436 struct btrfs_log_ctx *ctx;
2437
2438 if (!error) {
2439 INIT_LIST_HEAD(&root->log_ctxs[index]);
2440 return;
2441 }
2442
2443 list_for_each_entry(ctx, &root->log_ctxs[index], list)
2444 ctx->log_ret = error;
2445
2446 INIT_LIST_HEAD(&root->log_ctxs[index]);
2447}
2448
Chris Masone02119d2008-09-05 16:13:11 -04002449/*
2450 * btrfs_sync_log does sends a given tree log down to the disk and
2451 * updates the super blocks to record it. When this call is done,
Chris Mason12fcfd22009-03-24 10:24:20 -04002452 * you know that any inodes previously logged are safely on disk only
2453 * if it returns 0.
2454 *
2455 * Any other return value means you need to call btrfs_commit_transaction.
2456 * Some of the edge cases for fsyncing directories that have had unlinks
2457 * or renames done in the past mean that sometimes the only safe
2458 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2459 * that has happened.
Chris Masone02119d2008-09-05 16:13:11 -04002460 */
2461int btrfs_sync_log(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08002462 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04002463{
Yan Zheng7237f1832009-01-21 12:54:03 -05002464 int index1;
2465 int index2;
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002466 int mark;
Chris Masone02119d2008-09-05 16:13:11 -04002467 int ret;
Chris Masone02119d2008-09-05 16:13:11 -04002468 struct btrfs_root *log = root->log_root;
Yan Zheng7237f1832009-01-21 12:54:03 -05002469 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
Miao Xiebb14a592014-02-20 18:08:56 +08002470 int log_transid = 0;
Miao Xie8b050d32014-02-20 18:08:58 +08002471 struct btrfs_log_ctx root_log_ctx;
Miao Xiec6adc9c2013-05-28 10:05:39 +00002472 struct blk_plug plug;
Chris Masone02119d2008-09-05 16:13:11 -04002473
Yan Zheng7237f1832009-01-21 12:54:03 -05002474 mutex_lock(&root->log_mutex);
Miao Xied1433de2014-02-20 18:08:59 +08002475 log_transid = ctx->log_transid;
2476 if (root->log_transid_committed >= log_transid) {
Yan Zheng7237f1832009-01-21 12:54:03 -05002477 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002478 return ctx->log_ret;
Chris Masone02119d2008-09-05 16:13:11 -04002479 }
Miao Xied1433de2014-02-20 18:08:59 +08002480
2481 index1 = log_transid % 2;
2482 if (atomic_read(&root->log_commit[index1])) {
2483 wait_log_commit(trans, root, log_transid);
2484 mutex_unlock(&root->log_mutex);
2485 return ctx->log_ret;
2486 }
2487 ASSERT(log_transid == root->log_transid);
Yan Zheng7237f1832009-01-21 12:54:03 -05002488 atomic_set(&root->log_commit[index1], 1);
2489
2490 /* wait for previous tree log sync to complete */
2491 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
Miao Xied1433de2014-02-20 18:08:59 +08002492 wait_log_commit(trans, root, log_transid - 1);
Miao Xie48cab2e2014-02-20 18:08:52 +08002493
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002494 while (1) {
Miao Xie2ecb7922012-09-06 04:04:27 -06002495 int batch = atomic_read(&root->log_batch);
Chris Masoncd354ad2011-10-20 15:45:37 -04002496 /* when we're on an ssd, just kick the log commit out */
2497 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
Yan, Zheng86df7eb2009-10-14 09:24:59 -04002498 mutex_unlock(&root->log_mutex);
2499 schedule_timeout_uninterruptible(1);
2500 mutex_lock(&root->log_mutex);
2501 }
Chris Mason12fcfd22009-03-24 10:24:20 -04002502 wait_for_writer(trans, root);
Miao Xie2ecb7922012-09-06 04:04:27 -06002503 if (batch == atomic_read(&root->log_batch))
Chris Masone02119d2008-09-05 16:13:11 -04002504 break;
2505 }
Chris Masond0c803c2008-09-11 16:17:57 -04002506
Chris Mason12fcfd22009-03-24 10:24:20 -04002507 /* bail out if we need to do a full commit */
Miao Xie5c902ba2014-02-20 18:08:51 +08002508 if (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) ==
2509 trans->transid) {
Chris Mason12fcfd22009-03-24 10:24:20 -04002510 ret = -EAGAIN;
Josef Bacik2ab28f32012-10-12 15:27:49 -04002511 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002512 mutex_unlock(&root->log_mutex);
2513 goto out;
2514 }
2515
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002516 if (log_transid % 2 == 0)
2517 mark = EXTENT_DIRTY;
2518 else
2519 mark = EXTENT_NEW;
2520
Chris Mason690587d2009-10-13 13:29:19 -04002521 /* we start IO on all the marked extents here, but we don't actually
2522 * wait for them until later.
2523 */
Miao Xiec6adc9c2013-05-28 10:05:39 +00002524 blk_start_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002525 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002526 if (ret) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002527 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002528 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002529 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002530 mutex_unlock(&root->log_mutex);
2531 goto out;
2532 }
Yan Zheng7237f1832009-01-21 12:54:03 -05002533
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002534 btrfs_set_root_node(&log->root_item, log->node);
Yan Zheng7237f1832009-01-21 12:54:03 -05002535
Yan Zheng7237f1832009-01-21 12:54:03 -05002536 root->log_transid++;
2537 log->log_transid = root->log_transid;
Josef Bacikff782e02009-10-08 15:30:04 -04002538 root->log_start_pid = 0;
Yan Zheng7237f1832009-01-21 12:54:03 -05002539 /*
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002540 * IO has been started, blocks of the log tree have WRITTEN flag set
2541 * in their headers. new modifications of the log will be written to
2542 * new positions. so it's safe to allow log writers to go in.
Yan Zheng7237f1832009-01-21 12:54:03 -05002543 */
2544 mutex_unlock(&root->log_mutex);
2545
Miao Xied1433de2014-02-20 18:08:59 +08002546 btrfs_init_log_ctx(&root_log_ctx);
2547
Yan Zheng7237f1832009-01-21 12:54:03 -05002548 mutex_lock(&log_root_tree->log_mutex);
Miao Xie2ecb7922012-09-06 04:04:27 -06002549 atomic_inc(&log_root_tree->log_batch);
Yan Zheng7237f1832009-01-21 12:54:03 -05002550 atomic_inc(&log_root_tree->log_writers);
Miao Xied1433de2014-02-20 18:08:59 +08002551
2552 index2 = log_root_tree->log_transid % 2;
2553 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2554 root_log_ctx.log_transid = log_root_tree->log_transid;
2555
Yan Zheng7237f1832009-01-21 12:54:03 -05002556 mutex_unlock(&log_root_tree->log_mutex);
2557
2558 ret = update_log_root(trans, log);
Yan Zheng7237f1832009-01-21 12:54:03 -05002559
2560 mutex_lock(&log_root_tree->log_mutex);
2561 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2562 smp_mb();
2563 if (waitqueue_active(&log_root_tree->log_writer_wait))
2564 wake_up(&log_root_tree->log_writer_wait);
2565 }
2566
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002567 if (ret) {
Miao Xied1433de2014-02-20 18:08:59 +08002568 if (!list_empty(&root_log_ctx.list))
2569 list_del_init(&root_log_ctx.list);
2570
Miao Xiec6adc9c2013-05-28 10:05:39 +00002571 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002572 if (ret != -ENOSPC) {
2573 btrfs_abort_transaction(trans, root, ret);
2574 mutex_unlock(&log_root_tree->log_mutex);
2575 goto out;
2576 }
Miao Xie5c902ba2014-02-20 18:08:51 +08002577 ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) =
2578 trans->transid;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002579 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002580 btrfs_free_logged_extents(log, log_transid);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002581 mutex_unlock(&log_root_tree->log_mutex);
2582 ret = -EAGAIN;
2583 goto out;
2584 }
2585
Miao Xied1433de2014-02-20 18:08:59 +08002586 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
2587 mutex_unlock(&log_root_tree->log_mutex);
2588 ret = root_log_ctx.log_ret;
2589 goto out;
2590 }
Miao Xie8b050d32014-02-20 18:08:58 +08002591
Miao Xied1433de2014-02-20 18:08:59 +08002592 index2 = root_log_ctx.log_transid % 2;
Yan Zheng7237f1832009-01-21 12:54:03 -05002593 if (atomic_read(&log_root_tree->log_commit[index2])) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002594 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002595 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Miao Xie8b050d32014-02-20 18:08:58 +08002596 wait_log_commit(trans, log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002597 root_log_ctx.log_transid);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002598 btrfs_free_logged_extents(log, log_transid);
Yan Zheng7237f1832009-01-21 12:54:03 -05002599 mutex_unlock(&log_root_tree->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002600 ret = root_log_ctx.log_ret;
Yan Zheng7237f1832009-01-21 12:54:03 -05002601 goto out;
2602 }
Miao Xied1433de2014-02-20 18:08:59 +08002603 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
Yan Zheng7237f1832009-01-21 12:54:03 -05002604 atomic_set(&log_root_tree->log_commit[index2], 1);
2605
Chris Mason12fcfd22009-03-24 10:24:20 -04002606 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2607 wait_log_commit(trans, log_root_tree,
Miao Xied1433de2014-02-20 18:08:59 +08002608 root_log_ctx.log_transid - 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04002609 }
Yan Zheng7237f1832009-01-21 12:54:03 -05002610
Chris Mason12fcfd22009-03-24 10:24:20 -04002611 wait_for_writer(trans, log_root_tree);
2612
2613 /*
2614 * now that we've moved on to the tree of log tree roots,
2615 * check the full commit flag again
2616 */
Miao Xie5c902ba2014-02-20 18:08:51 +08002617 if (ACCESS_ONCE(root->fs_info->last_trans_log_full_commit) ==
2618 trans->transid) {
Miao Xiec6adc9c2013-05-28 10:05:39 +00002619 blk_finish_plug(&plug);
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002620 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002621 btrfs_free_logged_extents(log, log_transid);
Chris Mason12fcfd22009-03-24 10:24:20 -04002622 mutex_unlock(&log_root_tree->log_mutex);
2623 ret = -EAGAIN;
2624 goto out_wake_log_root;
2625 }
Yan Zheng7237f1832009-01-21 12:54:03 -05002626
Miao Xiec6adc9c2013-05-28 10:05:39 +00002627 ret = btrfs_write_marked_extents(log_root_tree,
2628 &log_root_tree->dirty_log_pages,
2629 EXTENT_DIRTY | EXTENT_NEW);
2630 blk_finish_plug(&plug);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002631 if (ret) {
2632 btrfs_abort_transaction(trans, root, ret);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002633 btrfs_free_logged_extents(log, log_transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002634 mutex_unlock(&log_root_tree->log_mutex);
2635 goto out_wake_log_root;
2636 }
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002637 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
Miao Xiec6adc9c2013-05-28 10:05:39 +00002638 btrfs_wait_marked_extents(log_root_tree,
2639 &log_root_tree->dirty_log_pages,
2640 EXTENT_NEW | EXTENT_DIRTY);
Josef Bacik2ab28f32012-10-12 15:27:49 -04002641 btrfs_wait_logged_extents(log, log_transid);
Chris Masone02119d2008-09-05 16:13:11 -04002642
David Sterba6c417612011-04-13 15:41:04 +02002643 btrfs_set_super_log_root(root->fs_info->super_for_commit,
Yan Zheng7237f1832009-01-21 12:54:03 -05002644 log_root_tree->node->start);
David Sterba6c417612011-04-13 15:41:04 +02002645 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
Yan Zheng7237f1832009-01-21 12:54:03 -05002646 btrfs_header_level(log_root_tree->node));
Chris Masone02119d2008-09-05 16:13:11 -04002647
Yan Zheng7237f1832009-01-21 12:54:03 -05002648 log_root_tree->log_transid++;
Yan Zheng7237f1832009-01-21 12:54:03 -05002649 mutex_unlock(&log_root_tree->log_mutex);
2650
2651 /*
2652 * nobody else is going to jump in and write the the ctree
2653 * super here because the log_commit atomic below is protecting
2654 * us. We must be called with a transaction handle pinning
2655 * the running transaction open, so a full commit can't hop
2656 * in and cause problems either.
2657 */
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002658 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002659 if (ret) {
2660 btrfs_abort_transaction(trans, root, ret);
2661 goto out_wake_log_root;
2662 }
Yan Zheng7237f1832009-01-21 12:54:03 -05002663
Chris Mason257c62e2009-10-13 13:21:08 -04002664 mutex_lock(&root->log_mutex);
2665 if (root->last_log_commit < log_transid)
2666 root->last_log_commit = log_transid;
2667 mutex_unlock(&root->log_mutex);
2668
Chris Mason12fcfd22009-03-24 10:24:20 -04002669out_wake_log_root:
Miao Xie8b050d32014-02-20 18:08:58 +08002670 /*
2671 * We needn't get log_mutex here because we are sure all
2672 * the other tasks are blocked.
2673 */
2674 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2675
Miao Xied1433de2014-02-20 18:08:59 +08002676 mutex_lock(&log_root_tree->log_mutex);
2677 log_root_tree->log_transid_committed++;
Yan Zheng7237f1832009-01-21 12:54:03 -05002678 atomic_set(&log_root_tree->log_commit[index2], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002679 mutex_unlock(&log_root_tree->log_mutex);
2680
Yan Zheng7237f1832009-01-21 12:54:03 -05002681 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2682 wake_up(&log_root_tree->log_commit_wait[index2]);
Chris Masone02119d2008-09-05 16:13:11 -04002683out:
Miao Xie8b050d32014-02-20 18:08:58 +08002684 /* See above. */
2685 btrfs_remove_all_log_ctxs(root, index1, ret);
2686
Miao Xied1433de2014-02-20 18:08:59 +08002687 mutex_lock(&root->log_mutex);
2688 root->log_transid_committed++;
Yan Zheng7237f1832009-01-21 12:54:03 -05002689 atomic_set(&root->log_commit[index1], 0);
Miao Xied1433de2014-02-20 18:08:59 +08002690 mutex_unlock(&root->log_mutex);
Miao Xie8b050d32014-02-20 18:08:58 +08002691
Yan Zheng7237f1832009-01-21 12:54:03 -05002692 if (waitqueue_active(&root->log_commit_wait[index1]))
2693 wake_up(&root->log_commit_wait[index1]);
Chris Masonb31eabd2011-01-31 16:48:24 -05002694 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002695}
2696
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002697static void free_log_tree(struct btrfs_trans_handle *trans,
2698 struct btrfs_root *log)
Chris Masone02119d2008-09-05 16:13:11 -04002699{
2700 int ret;
Chris Masond0c803c2008-09-11 16:17:57 -04002701 u64 start;
2702 u64 end;
Chris Masone02119d2008-09-05 16:13:11 -04002703 struct walk_control wc = {
2704 .free = 1,
2705 .process_func = process_one_buffer
2706 };
2707
Josef Bacik681ae502013-10-07 15:11:00 -04002708 ret = walk_log_tree(trans, log, &wc);
2709 /* I don't think this can happen but just in case */
2710 if (ret)
2711 btrfs_abort_transaction(trans, log, ret);
Chris Masone02119d2008-09-05 16:13:11 -04002712
Chris Masond3977122009-01-05 21:25:51 -05002713 while (1) {
Chris Masond0c803c2008-09-11 16:17:57 -04002714 ret = find_first_extent_bit(&log->dirty_log_pages,
Josef Bacike6138872012-09-27 17:07:30 -04002715 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2716 NULL);
Chris Masond0c803c2008-09-11 16:17:57 -04002717 if (ret)
2718 break;
2719
Yan, Zheng8cef4e12009-11-12 09:33:26 +00002720 clear_extent_bits(&log->dirty_log_pages, start, end,
2721 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002722 }
2723
Josef Bacik2ab28f32012-10-12 15:27:49 -04002724 /*
2725 * We may have short-circuited the log tree with the full commit logic
2726 * and left ordered extents on our list, so clear these out to keep us
2727 * from leaking inodes and memory.
2728 */
2729 btrfs_free_logged_extents(log, 0);
2730 btrfs_free_logged_extents(log, 1);
2731
Yan Zheng7237f1832009-01-21 12:54:03 -05002732 free_extent_buffer(log->node);
2733 kfree(log);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002734}
2735
2736/*
2737 * free all the extents used by the tree log. This should be called
2738 * at commit time of the full transaction
2739 */
2740int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2741{
2742 if (root->log_root) {
2743 free_log_tree(trans, root->log_root);
2744 root->log_root = NULL;
2745 }
2746 return 0;
2747}
2748
2749int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2750 struct btrfs_fs_info *fs_info)
2751{
2752 if (fs_info->log_root_tree) {
2753 free_log_tree(trans, fs_info->log_root_tree);
2754 fs_info->log_root_tree = NULL;
2755 }
Chris Masone02119d2008-09-05 16:13:11 -04002756 return 0;
2757}
2758
2759/*
Chris Masone02119d2008-09-05 16:13:11 -04002760 * If both a file and directory are logged, and unlinks or renames are
2761 * mixed in, we have a few interesting corners:
2762 *
2763 * create file X in dir Y
2764 * link file X to X.link in dir Y
2765 * fsync file X
2766 * unlink file X but leave X.link
2767 * fsync dir Y
2768 *
2769 * After a crash we would expect only X.link to exist. But file X
2770 * didn't get fsync'd again so the log has back refs for X and X.link.
2771 *
2772 * We solve this by removing directory entries and inode backrefs from the
2773 * log when a file that was logged in the current transaction is
2774 * unlinked. Any later fsync will include the updated log entries, and
2775 * we'll be able to reconstruct the proper directory items from backrefs.
2776 *
2777 * This optimizations allows us to avoid relogging the entire inode
2778 * or the entire directory.
2779 */
2780int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2781 struct btrfs_root *root,
2782 const char *name, int name_len,
2783 struct inode *dir, u64 index)
2784{
2785 struct btrfs_root *log;
2786 struct btrfs_dir_item *di;
2787 struct btrfs_path *path;
2788 int ret;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002789 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002790 int bytes_del = 0;
Li Zefan33345d012011-04-20 10:31:50 +08002791 u64 dir_ino = btrfs_ino(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002792
Chris Mason3a5f1d42008-09-11 15:53:37 -04002793 if (BTRFS_I(dir)->logged_trans < trans->transid)
2794 return 0;
2795
Chris Masone02119d2008-09-05 16:13:11 -04002796 ret = join_running_log_trans(root);
2797 if (ret)
2798 return 0;
2799
2800 mutex_lock(&BTRFS_I(dir)->log_mutex);
2801
2802 log = root->log_root;
2803 path = btrfs_alloc_path();
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002804 if (!path) {
2805 err = -ENOMEM;
2806 goto out_unlock;
2807 }
liubo2a29edc2011-01-26 06:22:08 +00002808
Li Zefan33345d012011-04-20 10:31:50 +08002809 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002810 name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002811 if (IS_ERR(di)) {
2812 err = PTR_ERR(di);
2813 goto fail;
2814 }
2815 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002816 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2817 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002818 if (ret) {
2819 err = ret;
2820 goto fail;
2821 }
Chris Masone02119d2008-09-05 16:13:11 -04002822 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002823 btrfs_release_path(path);
Li Zefan33345d012011-04-20 10:31:50 +08002824 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
Chris Masone02119d2008-09-05 16:13:11 -04002825 index, name, name_len, -1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002826 if (IS_ERR(di)) {
2827 err = PTR_ERR(di);
2828 goto fail;
2829 }
2830 if (di) {
Chris Masone02119d2008-09-05 16:13:11 -04002831 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2832 bytes_del += name_len;
Josef Bacik36508602013-04-25 16:23:32 -04002833 if (ret) {
2834 err = ret;
2835 goto fail;
2836 }
Chris Masone02119d2008-09-05 16:13:11 -04002837 }
2838
2839 /* update the directory size in the log to reflect the names
2840 * we have removed
2841 */
2842 if (bytes_del) {
2843 struct btrfs_key key;
2844
Li Zefan33345d012011-04-20 10:31:50 +08002845 key.objectid = dir_ino;
Chris Masone02119d2008-09-05 16:13:11 -04002846 key.offset = 0;
2847 key.type = BTRFS_INODE_ITEM_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02002848 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002849
2850 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002851 if (ret < 0) {
2852 err = ret;
2853 goto fail;
2854 }
Chris Masone02119d2008-09-05 16:13:11 -04002855 if (ret == 0) {
2856 struct btrfs_inode_item *item;
2857 u64 i_size;
2858
2859 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2860 struct btrfs_inode_item);
2861 i_size = btrfs_inode_size(path->nodes[0], item);
2862 if (i_size > bytes_del)
2863 i_size -= bytes_del;
2864 else
2865 i_size = 0;
2866 btrfs_set_inode_size(path->nodes[0], item, i_size);
2867 btrfs_mark_buffer_dirty(path->nodes[0]);
2868 } else
2869 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02002870 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002871 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002872fail:
Chris Masone02119d2008-09-05 16:13:11 -04002873 btrfs_free_path(path);
Tsutomu Itoha62f44a2011-04-25 19:43:51 -04002874out_unlock:
Chris Masone02119d2008-09-05 16:13:11 -04002875 mutex_unlock(&BTRFS_I(dir)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002876 if (ret == -ENOSPC) {
2877 root->fs_info->last_trans_log_full_commit = trans->transid;
2878 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002879 } else if (ret < 0)
2880 btrfs_abort_transaction(trans, root, ret);
2881
Chris Mason12fcfd22009-03-24 10:24:20 -04002882 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002883
Andi Kleen411fc6b2010-10-29 15:14:31 -04002884 return err;
Chris Masone02119d2008-09-05 16:13:11 -04002885}
2886
2887/* see comments for btrfs_del_dir_entries_in_log */
2888int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2889 struct btrfs_root *root,
2890 const char *name, int name_len,
2891 struct inode *inode, u64 dirid)
2892{
2893 struct btrfs_root *log;
2894 u64 index;
2895 int ret;
2896
Chris Mason3a5f1d42008-09-11 15:53:37 -04002897 if (BTRFS_I(inode)->logged_trans < trans->transid)
2898 return 0;
2899
Chris Masone02119d2008-09-05 16:13:11 -04002900 ret = join_running_log_trans(root);
2901 if (ret)
2902 return 0;
2903 log = root->log_root;
2904 mutex_lock(&BTRFS_I(inode)->log_mutex);
2905
Li Zefan33345d012011-04-20 10:31:50 +08002906 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
Chris Masone02119d2008-09-05 16:13:11 -04002907 dirid, &index);
2908 mutex_unlock(&BTRFS_I(inode)->log_mutex);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002909 if (ret == -ENOSPC) {
2910 root->fs_info->last_trans_log_full_commit = trans->transid;
2911 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002912 } else if (ret < 0 && ret != -ENOENT)
2913 btrfs_abort_transaction(trans, root, ret);
Chris Mason12fcfd22009-03-24 10:24:20 -04002914 btrfs_end_log_trans(root);
Chris Masone02119d2008-09-05 16:13:11 -04002915
Chris Masone02119d2008-09-05 16:13:11 -04002916 return ret;
2917}
2918
2919/*
2920 * creates a range item in the log for 'dirid'. first_offset and
2921 * last_offset tell us which parts of the key space the log should
2922 * be considered authoritative for.
2923 */
2924static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2925 struct btrfs_root *log,
2926 struct btrfs_path *path,
2927 int key_type, u64 dirid,
2928 u64 first_offset, u64 last_offset)
2929{
2930 int ret;
2931 struct btrfs_key key;
2932 struct btrfs_dir_log_item *item;
2933
2934 key.objectid = dirid;
2935 key.offset = first_offset;
2936 if (key_type == BTRFS_DIR_ITEM_KEY)
2937 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2938 else
2939 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2940 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002941 if (ret)
2942 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04002943
2944 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2945 struct btrfs_dir_log_item);
2946 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2947 btrfs_mark_buffer_dirty(path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002948 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002949 return 0;
2950}
2951
2952/*
2953 * log all the items included in the current transaction for a given
2954 * directory. This also creates the range items in the log tree required
2955 * to replay anything deleted before the fsync
2956 */
2957static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2958 struct btrfs_root *root, struct inode *inode,
2959 struct btrfs_path *path,
2960 struct btrfs_path *dst_path, int key_type,
2961 u64 min_offset, u64 *last_offset_ret)
2962{
2963 struct btrfs_key min_key;
Chris Masone02119d2008-09-05 16:13:11 -04002964 struct btrfs_root *log = root->log_root;
2965 struct extent_buffer *src;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04002966 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04002967 int ret;
2968 int i;
2969 int nritems;
2970 u64 first_offset = min_offset;
2971 u64 last_offset = (u64)-1;
Li Zefan33345d012011-04-20 10:31:50 +08002972 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04002973
2974 log = root->log_root;
Chris Masone02119d2008-09-05 16:13:11 -04002975
Li Zefan33345d012011-04-20 10:31:50 +08002976 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002977 min_key.type = key_type;
2978 min_key.offset = min_offset;
2979
2980 path->keep_locks = 1;
2981
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002982 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04002983
2984 /*
2985 * we didn't find anything from this transaction, see if there
2986 * is anything at all
2987 */
Li Zefan33345d012011-04-20 10:31:50 +08002988 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2989 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04002990 min_key.type = key_type;
2991 min_key.offset = (u64)-1;
David Sterbab3b4aa72011-04-21 01:20:15 +02002992 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002993 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2994 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002995 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002996 return ret;
2997 }
Li Zefan33345d012011-04-20 10:31:50 +08002998 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04002999
3000 /* if ret == 0 there are items for this type,
3001 * create a range to tell us the last key of this type.
3002 * otherwise, there are no items in this directory after
3003 * *min_offset, and we create a range to indicate that.
3004 */
3005 if (ret == 0) {
3006 struct btrfs_key tmp;
3007 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3008 path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -05003009 if (key_type == tmp.type)
Chris Masone02119d2008-09-05 16:13:11 -04003010 first_offset = max(min_offset, tmp.offset) + 1;
Chris Masone02119d2008-09-05 16:13:11 -04003011 }
3012 goto done;
3013 }
3014
3015 /* go backward to find any previous key */
Li Zefan33345d012011-04-20 10:31:50 +08003016 ret = btrfs_previous_item(root, path, ino, key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003017 if (ret == 0) {
3018 struct btrfs_key tmp;
3019 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3020 if (key_type == tmp.type) {
3021 first_offset = tmp.offset;
3022 ret = overwrite_item(trans, log, dst_path,
3023 path->nodes[0], path->slots[0],
3024 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003025 if (ret) {
3026 err = ret;
3027 goto done;
3028 }
Chris Masone02119d2008-09-05 16:13:11 -04003029 }
3030 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003031 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003032
3033 /* find the first key from this transaction again */
3034 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303035 if (WARN_ON(ret != 0))
Chris Masone02119d2008-09-05 16:13:11 -04003036 goto done;
Chris Masone02119d2008-09-05 16:13:11 -04003037
3038 /*
3039 * we have a block from this transaction, log every item in it
3040 * from our directory
3041 */
Chris Masond3977122009-01-05 21:25:51 -05003042 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003043 struct btrfs_key tmp;
3044 src = path->nodes[0];
3045 nritems = btrfs_header_nritems(src);
3046 for (i = path->slots[0]; i < nritems; i++) {
3047 btrfs_item_key_to_cpu(src, &min_key, i);
3048
Li Zefan33345d012011-04-20 10:31:50 +08003049 if (min_key.objectid != ino || min_key.type != key_type)
Chris Masone02119d2008-09-05 16:13:11 -04003050 goto done;
3051 ret = overwrite_item(trans, log, dst_path, src, i,
3052 &min_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003053 if (ret) {
3054 err = ret;
3055 goto done;
3056 }
Chris Masone02119d2008-09-05 16:13:11 -04003057 }
3058 path->slots[0] = nritems;
3059
3060 /*
3061 * look ahead to the next item and see if it is also
3062 * from this directory and from this transaction
3063 */
3064 ret = btrfs_next_leaf(root, path);
3065 if (ret == 1) {
3066 last_offset = (u64)-1;
3067 goto done;
3068 }
3069 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +08003070 if (tmp.objectid != ino || tmp.type != key_type) {
Chris Masone02119d2008-09-05 16:13:11 -04003071 last_offset = (u64)-1;
3072 goto done;
3073 }
3074 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3075 ret = overwrite_item(trans, log, dst_path,
3076 path->nodes[0], path->slots[0],
3077 &tmp);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003078 if (ret)
3079 err = ret;
3080 else
3081 last_offset = tmp.offset;
Chris Masone02119d2008-09-05 16:13:11 -04003082 goto done;
3083 }
3084 }
3085done:
David Sterbab3b4aa72011-04-21 01:20:15 +02003086 btrfs_release_path(path);
3087 btrfs_release_path(dst_path);
Chris Masone02119d2008-09-05 16:13:11 -04003088
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003089 if (err == 0) {
3090 *last_offset_ret = last_offset;
3091 /*
3092 * insert the log range keys to indicate where the log
3093 * is valid
3094 */
3095 ret = insert_dir_log_key(trans, log, path, key_type,
Li Zefan33345d012011-04-20 10:31:50 +08003096 ino, first_offset, last_offset);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003097 if (ret)
3098 err = ret;
3099 }
3100 return err;
Chris Masone02119d2008-09-05 16:13:11 -04003101}
3102
3103/*
3104 * logging directories is very similar to logging inodes, We find all the items
3105 * from the current transaction and write them to the log.
3106 *
3107 * The recovery code scans the directory in the subvolume, and if it finds a
3108 * key in the range logged that is not present in the log tree, then it means
3109 * that dir entry was unlinked during the transaction.
3110 *
3111 * In order for that scan to work, we must include one key smaller than
3112 * the smallest logged by this transaction and one key larger than the largest
3113 * key logged by this transaction.
3114 */
3115static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3116 struct btrfs_root *root, struct inode *inode,
3117 struct btrfs_path *path,
3118 struct btrfs_path *dst_path)
3119{
3120 u64 min_key;
3121 u64 max_key;
3122 int ret;
3123 int key_type = BTRFS_DIR_ITEM_KEY;
3124
3125again:
3126 min_key = 0;
3127 max_key = 0;
Chris Masond3977122009-01-05 21:25:51 -05003128 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003129 ret = log_dir_items(trans, root, inode, path,
3130 dst_path, key_type, min_key,
3131 &max_key);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003132 if (ret)
3133 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003134 if (max_key == (u64)-1)
3135 break;
3136 min_key = max_key + 1;
3137 }
3138
3139 if (key_type == BTRFS_DIR_ITEM_KEY) {
3140 key_type = BTRFS_DIR_INDEX_KEY;
3141 goto again;
3142 }
3143 return 0;
3144}
3145
3146/*
3147 * a helper function to drop items from the log before we relog an
3148 * inode. max_key_type indicates the highest item type to remove.
3149 * This cannot be run for file data extents because it does not
3150 * free the extents they point to.
3151 */
3152static int drop_objectid_items(struct btrfs_trans_handle *trans,
3153 struct btrfs_root *log,
3154 struct btrfs_path *path,
3155 u64 objectid, int max_key_type)
3156{
3157 int ret;
3158 struct btrfs_key key;
3159 struct btrfs_key found_key;
Josef Bacik18ec90d2012-09-28 11:56:28 -04003160 int start_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003161
3162 key.objectid = objectid;
3163 key.type = max_key_type;
3164 key.offset = (u64)-1;
3165
Chris Masond3977122009-01-05 21:25:51 -05003166 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04003167 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
Josef Bacik36508602013-04-25 16:23:32 -04003168 BUG_ON(ret == 0); /* Logic error */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003169 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04003170 break;
3171
3172 if (path->slots[0] == 0)
3173 break;
3174
3175 path->slots[0]--;
3176 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3177 path->slots[0]);
3178
3179 if (found_key.objectid != objectid)
3180 break;
3181
Josef Bacik18ec90d2012-09-28 11:56:28 -04003182 found_key.offset = 0;
3183 found_key.type = 0;
3184 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3185 &start_slot);
3186
3187 ret = btrfs_del_items(trans, log, path, start_slot,
3188 path->slots[0] - start_slot + 1);
3189 /*
3190 * If start slot isn't 0 then we don't need to re-search, we've
3191 * found the last guy with the objectid in this tree.
3192 */
3193 if (ret || start_slot != 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00003194 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02003195 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003196 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003197 btrfs_release_path(path);
Josef Bacik5bdbeb22012-05-29 16:59:49 -04003198 if (ret > 0)
3199 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003200 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04003201}
3202
Josef Bacik94edf4a2012-09-25 14:56:25 -04003203static void fill_inode_item(struct btrfs_trans_handle *trans,
3204 struct extent_buffer *leaf,
3205 struct btrfs_inode_item *item,
3206 struct inode *inode, int log_inode_only)
3207{
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003208 struct btrfs_map_token token;
Josef Bacik94edf4a2012-09-25 14:56:25 -04003209
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003210 btrfs_init_map_token(&token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003211
3212 if (log_inode_only) {
3213 /* set the generation to zero so the recover code
3214 * can tell the difference between an logging
3215 * just to say 'this inode exists' and a logging
3216 * to say 'update this inode with these values'
3217 */
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003218 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3219 btrfs_set_token_inode_size(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003220 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003221 btrfs_set_token_inode_generation(leaf, item,
3222 BTRFS_I(inode)->generation,
3223 &token);
3224 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003225 }
3226
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003227 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3228 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3229 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3230 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3231
3232 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3233 inode->i_atime.tv_sec, &token);
3234 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3235 inode->i_atime.tv_nsec, &token);
3236
3237 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3238 inode->i_mtime.tv_sec, &token);
3239 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3240 inode->i_mtime.tv_nsec, &token);
3241
3242 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3243 inode->i_ctime.tv_sec, &token);
3244 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3245 inode->i_ctime.tv_nsec, &token);
3246
3247 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3248 &token);
3249
3250 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3251 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3252 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3253 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3254 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003255}
3256
Josef Bacika95249b2012-10-11 16:17:34 -04003257static int log_inode_item(struct btrfs_trans_handle *trans,
3258 struct btrfs_root *log, struct btrfs_path *path,
3259 struct inode *inode)
3260{
3261 struct btrfs_inode_item *inode_item;
Josef Bacika95249b2012-10-11 16:17:34 -04003262 int ret;
3263
Filipe David Borba Mananaefd0c402013-10-07 21:20:44 +01003264 ret = btrfs_insert_empty_item(trans, log, path,
3265 &BTRFS_I(inode)->location,
Josef Bacika95249b2012-10-11 16:17:34 -04003266 sizeof(*inode_item));
3267 if (ret && ret != -EEXIST)
3268 return ret;
3269 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3270 struct btrfs_inode_item);
3271 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3272 btrfs_release_path(path);
3273 return 0;
3274}
3275
Chris Mason31ff1cd2008-09-11 16:17:57 -04003276static noinline int copy_items(struct btrfs_trans_handle *trans,
Liu Bod2794402012-08-29 01:07:56 -06003277 struct inode *inode,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003278 struct btrfs_path *dst_path,
Josef Bacik16e75492013-10-22 12:18:51 -04003279 struct btrfs_path *src_path, u64 *last_extent,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003280 int start_slot, int nr, int inode_only)
3281{
3282 unsigned long src_offset;
3283 unsigned long dst_offset;
Liu Bod2794402012-08-29 01:07:56 -06003284 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003285 struct btrfs_file_extent_item *extent;
3286 struct btrfs_inode_item *inode_item;
Josef Bacik16e75492013-10-22 12:18:51 -04003287 struct extent_buffer *src = src_path->nodes[0];
3288 struct btrfs_key first_key, last_key, key;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003289 int ret;
3290 struct btrfs_key *ins_keys;
3291 u32 *ins_sizes;
3292 char *ins_data;
3293 int i;
Chris Masond20f7042008-12-08 16:58:54 -05003294 struct list_head ordered_sums;
Liu Bod2794402012-08-29 01:07:56 -06003295 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Josef Bacik16e75492013-10-22 12:18:51 -04003296 bool has_extents = false;
3297 bool need_find_last_extent = (*last_extent == 0);
3298 bool done = false;
Chris Masond20f7042008-12-08 16:58:54 -05003299
3300 INIT_LIST_HEAD(&ordered_sums);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003301
3302 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3303 nr * sizeof(u32), GFP_NOFS);
liubo2a29edc2011-01-26 06:22:08 +00003304 if (!ins_data)
3305 return -ENOMEM;
3306
Josef Bacik16e75492013-10-22 12:18:51 -04003307 first_key.objectid = (u64)-1;
3308
Chris Mason31ff1cd2008-09-11 16:17:57 -04003309 ins_sizes = (u32 *)ins_data;
3310 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3311
3312 for (i = 0; i < nr; i++) {
3313 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3314 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3315 }
3316 ret = btrfs_insert_empty_items(trans, log, dst_path,
3317 ins_keys, ins_sizes, nr);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003318 if (ret) {
3319 kfree(ins_data);
3320 return ret;
3321 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003322
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003323 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003324 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3325 dst_path->slots[0]);
3326
3327 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3328
Josef Bacik16e75492013-10-22 12:18:51 -04003329 if ((i == (nr - 1)))
3330 last_key = ins_keys[i];
3331
Josef Bacik94edf4a2012-09-25 14:56:25 -04003332 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003333 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3334 dst_path->slots[0],
3335 struct btrfs_inode_item);
Josef Bacik94edf4a2012-09-25 14:56:25 -04003336 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3337 inode, inode_only == LOG_INODE_EXISTS);
3338 } else {
3339 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3340 src_offset, ins_sizes[i]);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003341 }
Josef Bacik94edf4a2012-09-25 14:56:25 -04003342
Josef Bacik16e75492013-10-22 12:18:51 -04003343 /*
3344 * We set need_find_last_extent here in case we know we were
3345 * processing other items and then walk into the first extent in
3346 * the inode. If we don't hit an extent then nothing changes,
3347 * we'll do the last search the next time around.
3348 */
3349 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3350 has_extents = true;
3351 if (need_find_last_extent &&
3352 first_key.objectid == (u64)-1)
3353 first_key = ins_keys[i];
3354 } else {
3355 need_find_last_extent = false;
3356 }
3357
Chris Mason31ff1cd2008-09-11 16:17:57 -04003358 /* take a reference on file data extents so that truncates
3359 * or deletes of this inode don't have to relog the inode
3360 * again
3361 */
Liu Bod2794402012-08-29 01:07:56 -06003362 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
3363 !skip_csum) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003364 int found_type;
3365 extent = btrfs_item_ptr(src, start_slot + i,
3366 struct btrfs_file_extent_item);
3367
liubo8e531cd2011-05-06 10:36:09 +08003368 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3369 continue;
3370
Chris Mason31ff1cd2008-09-11 16:17:57 -04003371 found_type = btrfs_file_extent_type(src, extent);
Josef Bacik6f1fed72012-09-26 11:07:06 -04003372 if (found_type == BTRFS_FILE_EXTENT_REG) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003373 u64 ds, dl, cs, cl;
3374 ds = btrfs_file_extent_disk_bytenr(src,
3375 extent);
3376 /* ds == 0 is a hole */
3377 if (ds == 0)
3378 continue;
3379
3380 dl = btrfs_file_extent_disk_num_bytes(src,
3381 extent);
3382 cs = btrfs_file_extent_offset(src, extent);
3383 cl = btrfs_file_extent_num_bytes(src,
Joe Perchesa419aef2009-08-18 11:18:35 -07003384 extent);
Chris Mason580afd72008-12-08 19:15:39 -05003385 if (btrfs_file_extent_compression(src,
3386 extent)) {
3387 cs = 0;
3388 cl = dl;
3389 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003390
3391 ret = btrfs_lookup_csums_range(
3392 log->fs_info->csum_root,
3393 ds + cs, ds + cs + cl - 1,
Arne Jansena2de7332011-03-08 14:14:00 +01003394 &ordered_sums, 0);
Josef Bacik36508602013-04-25 16:23:32 -04003395 if (ret) {
3396 btrfs_release_path(dst_path);
3397 kfree(ins_data);
3398 return ret;
3399 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003400 }
3401 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003402 }
3403
3404 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003405 btrfs_release_path(dst_path);
Chris Mason31ff1cd2008-09-11 16:17:57 -04003406 kfree(ins_data);
Chris Masond20f7042008-12-08 16:58:54 -05003407
3408 /*
3409 * we have to do this after the loop above to avoid changing the
3410 * log tree while trying to change the log tree.
3411 */
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003412 ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003413 while (!list_empty(&ordered_sums)) {
Chris Masond20f7042008-12-08 16:58:54 -05003414 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3415 struct btrfs_ordered_sum,
3416 list);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003417 if (!ret)
3418 ret = btrfs_csum_file_blocks(trans, log, sums);
Chris Masond20f7042008-12-08 16:58:54 -05003419 list_del(&sums->list);
3420 kfree(sums);
3421 }
Josef Bacik16e75492013-10-22 12:18:51 -04003422
3423 if (!has_extents)
3424 return ret;
3425
3426 /*
3427 * Because we use btrfs_search_forward we could skip leaves that were
3428 * not modified and then assume *last_extent is valid when it really
3429 * isn't. So back up to the previous leaf and read the end of the last
3430 * extent before we go and fill in holes.
3431 */
3432 if (need_find_last_extent) {
3433 u64 len;
3434
3435 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3436 if (ret < 0)
3437 return ret;
3438 if (ret)
3439 goto fill_holes;
3440 if (src_path->slots[0])
3441 src_path->slots[0]--;
3442 src = src_path->nodes[0];
3443 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3444 if (key.objectid != btrfs_ino(inode) ||
3445 key.type != BTRFS_EXTENT_DATA_KEY)
3446 goto fill_holes;
3447 extent = btrfs_item_ptr(src, src_path->slots[0],
3448 struct btrfs_file_extent_item);
3449 if (btrfs_file_extent_type(src, extent) ==
3450 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003451 len = btrfs_file_extent_inline_len(src,
3452 src_path->slots[0],
3453 extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003454 *last_extent = ALIGN(key.offset + len,
3455 log->sectorsize);
3456 } else {
3457 len = btrfs_file_extent_num_bytes(src, extent);
3458 *last_extent = key.offset + len;
3459 }
3460 }
3461fill_holes:
3462 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3463 * things could have happened
3464 *
3465 * 1) A merge could have happened, so we could currently be on a leaf
3466 * that holds what we were copying in the first place.
3467 * 2) A split could have happened, and now not all of the items we want
3468 * are on the same leaf.
3469 *
3470 * So we need to adjust how we search for holes, we need to drop the
3471 * path and re-search for the first extent key we found, and then walk
3472 * forward until we hit the last one we copied.
3473 */
3474 if (need_find_last_extent) {
3475 /* btrfs_prev_leaf could return 1 without releasing the path */
3476 btrfs_release_path(src_path);
3477 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3478 src_path, 0, 0);
3479 if (ret < 0)
3480 return ret;
3481 ASSERT(ret == 0);
3482 src = src_path->nodes[0];
3483 i = src_path->slots[0];
3484 } else {
3485 i = start_slot;
3486 }
3487
3488 /*
3489 * Ok so here we need to go through and fill in any holes we may have
3490 * to make sure that holes are punched for those areas in case they had
3491 * extents previously.
3492 */
3493 while (!done) {
3494 u64 offset, len;
3495 u64 extent_end;
3496
3497 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3498 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3499 if (ret < 0)
3500 return ret;
3501 ASSERT(ret == 0);
3502 src = src_path->nodes[0];
3503 i = 0;
3504 }
3505
3506 btrfs_item_key_to_cpu(src, &key, i);
3507 if (!btrfs_comp_cpu_keys(&key, &last_key))
3508 done = true;
3509 if (key.objectid != btrfs_ino(inode) ||
3510 key.type != BTRFS_EXTENT_DATA_KEY) {
3511 i++;
3512 continue;
3513 }
3514 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3515 if (btrfs_file_extent_type(src, extent) ==
3516 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason514ac8a2014-01-03 21:07:00 -08003517 len = btrfs_file_extent_inline_len(src, i, extent);
Josef Bacik16e75492013-10-22 12:18:51 -04003518 extent_end = ALIGN(key.offset + len, log->sectorsize);
3519 } else {
3520 len = btrfs_file_extent_num_bytes(src, extent);
3521 extent_end = key.offset + len;
3522 }
3523 i++;
3524
3525 if (*last_extent == key.offset) {
3526 *last_extent = extent_end;
3527 continue;
3528 }
3529 offset = *last_extent;
3530 len = key.offset - *last_extent;
3531 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3532 offset, 0, 0, len, 0, len, 0,
3533 0, 0);
3534 if (ret)
3535 break;
3536 *last_extent = offset + len;
3537 }
3538 /*
3539 * Need to let the callers know we dropped the path so they should
3540 * re-search.
3541 */
3542 if (!ret && need_find_last_extent)
3543 ret = 1;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003544 return ret;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003545}
3546
Josef Bacik5dc562c2012-08-17 13:14:17 -04003547static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3548{
3549 struct extent_map *em1, *em2;
3550
3551 em1 = list_entry(a, struct extent_map, list);
3552 em2 = list_entry(b, struct extent_map, list);
3553
3554 if (em1->start < em2->start)
3555 return -1;
3556 else if (em1->start > em2->start)
3557 return 1;
3558 return 0;
3559}
3560
Josef Bacik5dc562c2012-08-17 13:14:17 -04003561static int log_one_extent(struct btrfs_trans_handle *trans,
3562 struct inode *inode, struct btrfs_root *root,
Miao Xie827463c2014-01-14 20:31:51 +08003563 struct extent_map *em, struct btrfs_path *path,
3564 struct list_head *logged_list)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003565{
3566 struct btrfs_root *log = root->log_root;
Josef Bacik70c8a912012-10-11 16:54:30 -04003567 struct btrfs_file_extent_item *fi;
3568 struct extent_buffer *leaf;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003569 struct btrfs_ordered_extent *ordered;
Josef Bacik70c8a912012-10-11 16:54:30 -04003570 struct list_head ordered_sums;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003571 struct btrfs_map_token token;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003572 struct btrfs_key key;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003573 u64 mod_start = em->mod_start;
3574 u64 mod_len = em->mod_len;
3575 u64 csum_offset;
3576 u64 csum_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04003577 u64 extent_offset = em->start - em->orig_start;
3578 u64 block_len;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003579 int ret;
Josef Bacik70c8a912012-10-11 16:54:30 -04003580 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003581 int extent_inserted = 0;
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003582
Josef Bacik70c8a912012-10-11 16:54:30 -04003583 INIT_LIST_HEAD(&ordered_sums);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003584 btrfs_init_map_token(&token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003585
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003586 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3587 em->start + em->len, NULL, 0, 1,
3588 sizeof(*fi), &extent_inserted);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04003589 if (ret)
Josef Bacik70c8a912012-10-11 16:54:30 -04003590 return ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00003591
3592 if (!extent_inserted) {
3593 key.objectid = btrfs_ino(inode);
3594 key.type = BTRFS_EXTENT_DATA_KEY;
3595 key.offset = em->start;
3596
3597 ret = btrfs_insert_empty_item(trans, log, path, &key,
3598 sizeof(*fi));
3599 if (ret)
3600 return ret;
3601 }
Josef Bacik70c8a912012-10-11 16:54:30 -04003602 leaf = path->nodes[0];
3603 fi = btrfs_item_ptr(leaf, path->slots[0],
3604 struct btrfs_file_extent_item);
Josef Bacik124fe662013-03-01 11:47:21 -05003605
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003606 btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
3607 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003608 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3609 skip_csum = true;
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003610 btrfs_set_token_file_extent_type(leaf, fi,
3611 BTRFS_FILE_EXTENT_PREALLOC,
3612 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003613 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003614 btrfs_set_token_file_extent_type(leaf, fi,
3615 BTRFS_FILE_EXTENT_REG,
3616 &token);
Josef Baciked9e8af2013-10-14 17:23:08 -04003617 if (em->block_start == EXTENT_MAP_HOLE)
Josef Bacik70c8a912012-10-11 16:54:30 -04003618 skip_csum = true;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003619 }
3620
Josef Bacik70c8a912012-10-11 16:54:30 -04003621 block_len = max(em->block_len, em->orig_block_len);
3622 if (em->compress_type != BTRFS_COMPRESS_NONE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003623 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3624 em->block_start,
3625 &token);
3626 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3627 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003628 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003629 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3630 em->block_start -
3631 extent_offset, &token);
3632 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3633 &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003634 } else {
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003635 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3636 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3637 &token);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003638 }
3639
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003640 btrfs_set_token_file_extent_offset(leaf, fi,
3641 em->start - em->orig_start,
3642 &token);
3643 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
Josef Bacikcc95bef2013-04-04 14:31:27 -04003644 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
Josef Bacik0b1c6cc2012-10-23 16:03:44 -04003645 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3646 &token);
3647 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3648 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
Josef Bacik70c8a912012-10-11 16:54:30 -04003649 btrfs_mark_buffer_dirty(leaf);
3650
Josef Bacik70c8a912012-10-11 16:54:30 -04003651 btrfs_release_path(path);
Josef Bacik70c8a912012-10-11 16:54:30 -04003652 if (ret) {
3653 return ret;
3654 }
3655
3656 if (skip_csum)
3657 return 0;
3658
Josef Bacik2ab28f32012-10-12 15:27:49 -04003659 /*
3660 * First check and see if our csums are on our outstanding ordered
3661 * extents.
3662 */
Miao Xie827463c2014-01-14 20:31:51 +08003663 list_for_each_entry(ordered, logged_list, log_list) {
Josef Bacik2ab28f32012-10-12 15:27:49 -04003664 struct btrfs_ordered_sum *sum;
3665
3666 if (!mod_len)
3667 break;
3668
Josef Bacik2ab28f32012-10-12 15:27:49 -04003669 if (ordered->file_offset + ordered->len <= mod_start ||
3670 mod_start + mod_len <= ordered->file_offset)
3671 continue;
3672
3673 /*
3674 * We are going to copy all the csums on this ordered extent, so
3675 * go ahead and adjust mod_start and mod_len in case this
3676 * ordered extent has already been logged.
3677 */
3678 if (ordered->file_offset > mod_start) {
3679 if (ordered->file_offset + ordered->len >=
3680 mod_start + mod_len)
3681 mod_len = ordered->file_offset - mod_start;
3682 /*
3683 * If we have this case
3684 *
3685 * |--------- logged extent ---------|
3686 * |----- ordered extent ----|
3687 *
3688 * Just don't mess with mod_start and mod_len, we'll
3689 * just end up logging more csums than we need and it
3690 * will be ok.
3691 */
3692 } else {
3693 if (ordered->file_offset + ordered->len <
3694 mod_start + mod_len) {
3695 mod_len = (mod_start + mod_len) -
3696 (ordered->file_offset + ordered->len);
3697 mod_start = ordered->file_offset +
3698 ordered->len;
3699 } else {
3700 mod_len = 0;
3701 }
3702 }
3703
3704 /*
3705 * To keep us from looping for the above case of an ordered
3706 * extent that falls inside of the logged extent.
3707 */
3708 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3709 &ordered->flags))
3710 continue;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003711
Miao Xie23c671a2014-01-14 20:31:52 +08003712 if (ordered->csum_bytes_left) {
3713 btrfs_start_ordered_extent(inode, ordered, 0);
3714 wait_event(ordered->wait,
3715 ordered->csum_bytes_left == 0);
3716 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003717
3718 list_for_each_entry(sum, &ordered->list, list) {
3719 ret = btrfs_csum_file_blocks(trans, log, sum);
Miao Xie827463c2014-01-14 20:31:51 +08003720 if (ret)
Josef Bacik2ab28f32012-10-12 15:27:49 -04003721 goto unlocked;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003722 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003723
3724 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003725unlocked:
3726
3727 if (!mod_len || ret)
3728 return ret;
3729
Filipe David Borba Manana488111a2013-10-28 16:30:29 +00003730 if (em->compress_type) {
3731 csum_offset = 0;
3732 csum_len = block_len;
3733 } else {
3734 csum_offset = mod_start - em->start;
3735 csum_len = mod_len;
3736 }
Josef Bacik2ab28f32012-10-12 15:27:49 -04003737
Josef Bacik70c8a912012-10-11 16:54:30 -04003738 /* block start is already adjusted for the file extent offset. */
3739 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3740 em->block_start + csum_offset,
3741 em->block_start + csum_offset +
3742 csum_len - 1, &ordered_sums, 0);
3743 if (ret)
3744 return ret;
3745
3746 while (!list_empty(&ordered_sums)) {
3747 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3748 struct btrfs_ordered_sum,
3749 list);
3750 if (!ret)
3751 ret = btrfs_csum_file_blocks(trans, log, sums);
3752 list_del(&sums->list);
3753 kfree(sums);
3754 }
3755
3756 return ret;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003757}
3758
3759static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3760 struct btrfs_root *root,
3761 struct inode *inode,
Miao Xie827463c2014-01-14 20:31:51 +08003762 struct btrfs_path *path,
3763 struct list_head *logged_list)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003764{
Josef Bacik5dc562c2012-08-17 13:14:17 -04003765 struct extent_map *em, *n;
3766 struct list_head extents;
3767 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3768 u64 test_gen;
3769 int ret = 0;
Josef Bacik2ab28f32012-10-12 15:27:49 -04003770 int num = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003771
3772 INIT_LIST_HEAD(&extents);
3773
Josef Bacik5dc562c2012-08-17 13:14:17 -04003774 write_lock(&tree->lock);
3775 test_gen = root->fs_info->last_trans_committed;
3776
3777 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3778 list_del_init(&em->list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003779
3780 /*
3781 * Just an arbitrary number, this can be really CPU intensive
3782 * once we start getting a lot of extents, and really once we
3783 * have a bunch of extents we just want to commit since it will
3784 * be faster.
3785 */
3786 if (++num > 32768) {
3787 list_del_init(&tree->modified_extents);
3788 ret = -EFBIG;
3789 goto process;
3790 }
3791
Josef Bacik5dc562c2012-08-17 13:14:17 -04003792 if (em->generation <= test_gen)
3793 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003794 /* Need a ref to keep it from getting evicted from cache */
3795 atomic_inc(&em->refs);
3796 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003797 list_add_tail(&em->list, &extents);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003798 num++;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003799 }
3800
3801 list_sort(NULL, &extents, extent_cmp);
3802
Josef Bacik2ab28f32012-10-12 15:27:49 -04003803process:
Josef Bacik5dc562c2012-08-17 13:14:17 -04003804 while (!list_empty(&extents)) {
3805 em = list_entry(extents.next, struct extent_map, list);
3806
3807 list_del_init(&em->list);
3808
3809 /*
3810 * If we had an error we just need to delete everybody from our
3811 * private list.
3812 */
Josef Bacikff44c6e2012-09-14 12:59:20 -04003813 if (ret) {
Josef Bacik201a9032013-01-24 12:02:07 -05003814 clear_em_logging(tree, em);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003815 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003816 continue;
Josef Bacikff44c6e2012-09-14 12:59:20 -04003817 }
3818
3819 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003820
Miao Xie827463c2014-01-14 20:31:51 +08003821 ret = log_one_extent(trans, inode, root, em, path, logged_list);
Josef Bacikff44c6e2012-09-14 12:59:20 -04003822 write_lock(&tree->lock);
Josef Bacik201a9032013-01-24 12:02:07 -05003823 clear_em_logging(tree, em);
3824 free_extent_map(em);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003825 }
Josef Bacikff44c6e2012-09-14 12:59:20 -04003826 WARN_ON(!list_empty(&extents));
3827 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003828
Josef Bacik5dc562c2012-08-17 13:14:17 -04003829 btrfs_release_path(path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003830 return ret;
3831}
3832
Chris Masone02119d2008-09-05 16:13:11 -04003833/* log a single inode in the tree log.
3834 * At least one parent directory for this inode must exist in the tree
3835 * or be logged already.
3836 *
3837 * Any items from this inode changed by the current transaction are copied
3838 * to the log tree. An extra reference is taken on any extents in this
3839 * file, allowing us to avoid a whole pile of corner cases around logging
3840 * blocks that have been removed from the tree.
3841 *
3842 * See LOG_INODE_ALL and related defines for a description of what inode_only
3843 * does.
3844 *
3845 * This handles both files and directories.
3846 */
Chris Mason12fcfd22009-03-24 10:24:20 -04003847static int btrfs_log_inode(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003848 struct btrfs_root *root, struct inode *inode,
3849 int inode_only)
3850{
3851 struct btrfs_path *path;
3852 struct btrfs_path *dst_path;
3853 struct btrfs_key min_key;
3854 struct btrfs_key max_key;
3855 struct btrfs_root *log = root->log_root;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003856 struct extent_buffer *src = NULL;
Miao Xie827463c2014-01-14 20:31:51 +08003857 LIST_HEAD(logged_list);
Josef Bacik16e75492013-10-22 12:18:51 -04003858 u64 last_extent = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003859 int err = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003860 int ret;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003861 int nritems;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003862 int ins_start_slot = 0;
3863 int ins_nr;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003864 bool fast_search = false;
Li Zefan33345d012011-04-20 10:31:50 +08003865 u64 ino = btrfs_ino(inode);
Chris Masone02119d2008-09-05 16:13:11 -04003866
Chris Masone02119d2008-09-05 16:13:11 -04003867 path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003868 if (!path)
3869 return -ENOMEM;
Chris Masone02119d2008-09-05 16:13:11 -04003870 dst_path = btrfs_alloc_path();
Tsutomu Itoh5df67082011-02-01 09:17:35 +00003871 if (!dst_path) {
3872 btrfs_free_path(path);
3873 return -ENOMEM;
3874 }
Chris Masone02119d2008-09-05 16:13:11 -04003875
Li Zefan33345d012011-04-20 10:31:50 +08003876 min_key.objectid = ino;
Chris Masone02119d2008-09-05 16:13:11 -04003877 min_key.type = BTRFS_INODE_ITEM_KEY;
3878 min_key.offset = 0;
3879
Li Zefan33345d012011-04-20 10:31:50 +08003880 max_key.objectid = ino;
Chris Mason12fcfd22009-03-24 10:24:20 -04003881
Chris Mason12fcfd22009-03-24 10:24:20 -04003882
Josef Bacik5dc562c2012-08-17 13:14:17 -04003883 /* today the code can only do partial logging of directories */
Miao Xie5269b672012-11-01 07:35:23 +00003884 if (S_ISDIR(inode->i_mode) ||
3885 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3886 &BTRFS_I(inode)->runtime_flags) &&
3887 inode_only == LOG_INODE_EXISTS))
Chris Masone02119d2008-09-05 16:13:11 -04003888 max_key.type = BTRFS_XATTR_ITEM_KEY;
3889 else
3890 max_key.type = (u8)-1;
3891 max_key.offset = (u64)-1;
3892
Josef Bacik94edf4a2012-09-25 14:56:25 -04003893 /* Only run delayed items if we are a dir or a new file */
3894 if (S_ISDIR(inode->i_mode) ||
3895 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
3896 ret = btrfs_commit_inode_delayed_items(trans, inode);
3897 if (ret) {
3898 btrfs_free_path(path);
3899 btrfs_free_path(dst_path);
3900 return ret;
3901 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003902 }
3903
Chris Masone02119d2008-09-05 16:13:11 -04003904 mutex_lock(&BTRFS_I(inode)->log_mutex);
3905
Miao Xie827463c2014-01-14 20:31:51 +08003906 btrfs_get_logged_extents(inode, &logged_list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04003907
Chris Masone02119d2008-09-05 16:13:11 -04003908 /*
3909 * a brute force approach to making sure we get the most uptodate
3910 * copies of everything.
3911 */
3912 if (S_ISDIR(inode->i_mode)) {
3913 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3914
3915 if (inode_only == LOG_INODE_EXISTS)
3916 max_key_type = BTRFS_XATTR_ITEM_KEY;
Li Zefan33345d012011-04-20 10:31:50 +08003917 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
Chris Masone02119d2008-09-05 16:13:11 -04003918 } else {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003919 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3920 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacike9976152012-10-11 15:53:56 -04003921 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3922 &BTRFS_I(inode)->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003923 ret = btrfs_truncate_inode_items(trans, log,
3924 inode, 0, 0);
Josef Bacika95249b2012-10-11 16:17:34 -04003925 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
Josef Bacik6cfab852013-11-12 16:25:58 -05003926 &BTRFS_I(inode)->runtime_flags) ||
3927 inode_only == LOG_INODE_EXISTS) {
Josef Bacika95249b2012-10-11 16:17:34 -04003928 if (inode_only == LOG_INODE_ALL)
3929 fast_search = true;
3930 max_key.type = BTRFS_XATTR_ITEM_KEY;
3931 ret = drop_objectid_items(trans, log, path, ino,
3932 max_key.type);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003933 } else {
Liu Bo183f37f2012-11-01 06:38:47 +00003934 if (inode_only == LOG_INODE_ALL)
3935 fast_search = true;
Josef Bacika95249b2012-10-11 16:17:34 -04003936 ret = log_inode_item(trans, log, dst_path, inode);
3937 if (ret) {
3938 err = ret;
3939 goto out_unlock;
3940 }
3941 goto log_extents;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003942 }
Josef Bacika95249b2012-10-11 16:17:34 -04003943
Chris Masone02119d2008-09-05 16:13:11 -04003944 }
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003945 if (ret) {
3946 err = ret;
3947 goto out_unlock;
3948 }
Chris Masone02119d2008-09-05 16:13:11 -04003949 path->keep_locks = 1;
3950
Chris Masond3977122009-01-05 21:25:51 -05003951 while (1) {
Chris Mason31ff1cd2008-09-11 16:17:57 -04003952 ins_nr = 0;
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003953 ret = btrfs_search_forward(root, &min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00003954 path, trans->transid);
Chris Masone02119d2008-09-05 16:13:11 -04003955 if (ret != 0)
3956 break;
Chris Mason3a5f1d42008-09-11 15:53:37 -04003957again:
Chris Mason31ff1cd2008-09-11 16:17:57 -04003958 /* note, ins_nr might be > 0 here, cleanup outside the loop */
Li Zefan33345d012011-04-20 10:31:50 +08003959 if (min_key.objectid != ino)
Chris Masone02119d2008-09-05 16:13:11 -04003960 break;
3961 if (min_key.type > max_key.type)
3962 break;
Chris Mason31ff1cd2008-09-11 16:17:57 -04003963
Chris Masone02119d2008-09-05 16:13:11 -04003964 src = path->nodes[0];
Chris Mason31ff1cd2008-09-11 16:17:57 -04003965 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3966 ins_nr++;
3967 goto next_slot;
3968 } else if (!ins_nr) {
3969 ins_start_slot = path->slots[0];
3970 ins_nr = 1;
3971 goto next_slot;
Chris Masone02119d2008-09-05 16:13:11 -04003972 }
3973
Josef Bacik16e75492013-10-22 12:18:51 -04003974 ret = copy_items(trans, inode, dst_path, path, &last_extent,
3975 ins_start_slot, ins_nr, inode_only);
3976 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003977 err = ret;
3978 goto out_unlock;
Josef Bacik16e75492013-10-22 12:18:51 -04003979 } if (ret) {
3980 ins_nr = 0;
3981 btrfs_release_path(path);
3982 continue;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04003983 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003984 ins_nr = 1;
3985 ins_start_slot = path->slots[0];
3986next_slot:
Chris Masone02119d2008-09-05 16:13:11 -04003987
Chris Mason3a5f1d42008-09-11 15:53:37 -04003988 nritems = btrfs_header_nritems(path->nodes[0]);
3989 path->slots[0]++;
3990 if (path->slots[0] < nritems) {
3991 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
3992 path->slots[0]);
3993 goto again;
3994 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04003995 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04003996 ret = copy_items(trans, inode, dst_path, path,
3997 &last_extent, ins_start_slot,
Chris Mason31ff1cd2008-09-11 16:17:57 -04003998 ins_nr, inode_only);
Josef Bacik16e75492013-10-22 12:18:51 -04003999 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004000 err = ret;
4001 goto out_unlock;
4002 }
Josef Bacik16e75492013-10-22 12:18:51 -04004003 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004004 ins_nr = 0;
4005 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004006 btrfs_release_path(path);
Chris Mason3a5f1d42008-09-11 15:53:37 -04004007
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004008 if (min_key.offset < (u64)-1) {
Chris Masone02119d2008-09-05 16:13:11 -04004009 min_key.offset++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004010 } else if (min_key.type < max_key.type) {
Chris Masone02119d2008-09-05 16:13:11 -04004011 min_key.type++;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004012 min_key.offset = 0;
4013 } else {
Chris Masone02119d2008-09-05 16:13:11 -04004014 break;
Filipe David Borba Manana3d41d702013-10-01 17:06:53 +01004015 }
Chris Masone02119d2008-09-05 16:13:11 -04004016 }
Chris Mason31ff1cd2008-09-11 16:17:57 -04004017 if (ins_nr) {
Josef Bacik16e75492013-10-22 12:18:51 -04004018 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4019 ins_start_slot, ins_nr, inode_only);
4020 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004021 err = ret;
4022 goto out_unlock;
4023 }
Josef Bacik16e75492013-10-22 12:18:51 -04004024 ret = 0;
Chris Mason31ff1cd2008-09-11 16:17:57 -04004025 ins_nr = 0;
4026 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04004027
Josef Bacika95249b2012-10-11 16:17:34 -04004028log_extents:
Josef Bacikf3b15cc2013-07-22 12:54:30 -04004029 btrfs_release_path(path);
4030 btrfs_release_path(dst_path);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004031 if (fast_search) {
Miao Xie827463c2014-01-14 20:31:51 +08004032 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
4033 &logged_list);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004034 if (ret) {
4035 err = ret;
4036 goto out_unlock;
4037 }
Josef Bacikd006a042013-11-12 20:54:09 -05004038 } else if (inode_only == LOG_INODE_ALL) {
Liu Bo06d3d222012-08-27 10:52:19 -06004039 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4040 struct extent_map *em, *n;
4041
Miao Xiebbe14262012-11-01 07:34:54 +00004042 write_lock(&tree->lock);
Liu Bo06d3d222012-08-27 10:52:19 -06004043 list_for_each_entry_safe(em, n, &tree->modified_extents, list)
4044 list_del_init(&em->list);
Miao Xiebbe14262012-11-01 07:34:54 +00004045 write_unlock(&tree->lock);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004046 }
4047
Chris Mason9623f9a2008-09-11 17:42:42 -04004048 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
Chris Masone02119d2008-09-05 16:13:11 -04004049 ret = log_directory_changes(trans, root, inode, path, dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004050 if (ret) {
4051 err = ret;
4052 goto out_unlock;
4053 }
Chris Masone02119d2008-09-05 16:13:11 -04004054 }
Chris Mason3a5f1d42008-09-11 15:53:37 -04004055 BTRFS_I(inode)->logged_trans = trans->transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06004056 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004057out_unlock:
Miao Xie827463c2014-01-14 20:31:51 +08004058 if (unlikely(err))
4059 btrfs_put_logged_extents(&logged_list);
4060 else
4061 btrfs_submit_logged_extents(&logged_list, log);
Chris Masone02119d2008-09-05 16:13:11 -04004062 mutex_unlock(&BTRFS_I(inode)->log_mutex);
4063
4064 btrfs_free_path(path);
4065 btrfs_free_path(dst_path);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004066 return err;
Chris Masone02119d2008-09-05 16:13:11 -04004067}
4068
Chris Mason12fcfd22009-03-24 10:24:20 -04004069/*
4070 * follow the dentry parent pointers up the chain and see if any
4071 * of the directories in it require a full commit before they can
4072 * be logged. Returns zero if nothing special needs to be done or 1 if
4073 * a full commit is required.
4074 */
4075static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
4076 struct inode *inode,
4077 struct dentry *parent,
4078 struct super_block *sb,
4079 u64 last_committed)
Chris Masone02119d2008-09-05 16:13:11 -04004080{
Chris Mason12fcfd22009-03-24 10:24:20 -04004081 int ret = 0;
4082 struct btrfs_root *root;
Josef Bacik6a912212010-11-20 09:48:00 +00004083 struct dentry *old_parent = NULL;
Josef Bacikde2b5302013-09-11 09:36:30 -04004084 struct inode *orig_inode = inode;
Chris Masone02119d2008-09-05 16:13:11 -04004085
Chris Masonaf4176b2009-03-24 10:24:31 -04004086 /*
4087 * for regular files, if its inode is already on disk, we don't
4088 * have to worry about the parents at all. This is because
4089 * we can use the last_unlink_trans field to record renames
4090 * and other fun in this file.
4091 */
4092 if (S_ISREG(inode->i_mode) &&
4093 BTRFS_I(inode)->generation <= last_committed &&
4094 BTRFS_I(inode)->last_unlink_trans <= last_committed)
4095 goto out;
4096
Chris Mason12fcfd22009-03-24 10:24:20 -04004097 if (!S_ISDIR(inode->i_mode)) {
4098 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4099 goto out;
4100 inode = parent->d_inode;
4101 }
4102
4103 while (1) {
Josef Bacikde2b5302013-09-11 09:36:30 -04004104 /*
4105 * If we are logging a directory then we start with our inode,
4106 * not our parents inode, so we need to skipp setting the
4107 * logged_trans so that further down in the log code we don't
4108 * think this inode has already been logged.
4109 */
4110 if (inode != orig_inode)
4111 BTRFS_I(inode)->logged_trans = trans->transid;
Chris Mason12fcfd22009-03-24 10:24:20 -04004112 smp_mb();
4113
4114 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
4115 root = BTRFS_I(inode)->root;
4116
4117 /*
4118 * make sure any commits to the log are forced
4119 * to be full commits
4120 */
4121 root->fs_info->last_trans_log_full_commit =
4122 trans->transid;
4123 ret = 1;
4124 break;
4125 }
4126
4127 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
4128 break;
4129
Yan, Zheng76dda932009-09-21 16:00:26 -04004130 if (IS_ROOT(parent))
Chris Mason12fcfd22009-03-24 10:24:20 -04004131 break;
4132
Josef Bacik6a912212010-11-20 09:48:00 +00004133 parent = dget_parent(parent);
4134 dput(old_parent);
4135 old_parent = parent;
Chris Mason12fcfd22009-03-24 10:24:20 -04004136 inode = parent->d_inode;
4137
4138 }
Josef Bacik6a912212010-11-20 09:48:00 +00004139 dput(old_parent);
Chris Mason12fcfd22009-03-24 10:24:20 -04004140out:
Chris Masone02119d2008-09-05 16:13:11 -04004141 return ret;
4142}
4143
4144/*
4145 * helper function around btrfs_log_inode to make sure newly created
4146 * parent directories also end up in the log. A minimal inode and backref
4147 * only logging is done of any parent directories that are older than
4148 * the last committed transaction
4149 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00004150static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
4151 struct btrfs_root *root, struct inode *inode,
Miao Xie8b050d32014-02-20 18:08:58 +08004152 struct dentry *parent, int exists_only,
4153 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004154{
Chris Mason12fcfd22009-03-24 10:24:20 -04004155 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
Chris Masone02119d2008-09-05 16:13:11 -04004156 struct super_block *sb;
Josef Bacik6a912212010-11-20 09:48:00 +00004157 struct dentry *old_parent = NULL;
Chris Mason12fcfd22009-03-24 10:24:20 -04004158 int ret = 0;
4159 u64 last_committed = root->fs_info->last_trans_committed;
4160
4161 sb = inode->i_sb;
4162
Sage Weil3a5e1402009-04-02 16:49:40 -04004163 if (btrfs_test_opt(root, NOTREELOG)) {
4164 ret = 1;
4165 goto end_no_trans;
4166 }
4167
Chris Mason12fcfd22009-03-24 10:24:20 -04004168 if (root->fs_info->last_trans_log_full_commit >
4169 root->fs_info->last_trans_committed) {
4170 ret = 1;
4171 goto end_no_trans;
4172 }
4173
Yan, Zheng76dda932009-09-21 16:00:26 -04004174 if (root != BTRFS_I(inode)->root ||
4175 btrfs_root_refs(&root->root_item) == 0) {
4176 ret = 1;
4177 goto end_no_trans;
4178 }
4179
Chris Mason12fcfd22009-03-24 10:24:20 -04004180 ret = check_parent_dirs_for_sync(trans, inode, parent,
4181 sb, last_committed);
4182 if (ret)
4183 goto end_no_trans;
Chris Masone02119d2008-09-05 16:13:11 -04004184
Josef Bacik22ee6982012-05-29 16:57:49 -04004185 if (btrfs_inode_in_log(inode, trans->transid)) {
Chris Mason257c62e2009-10-13 13:21:08 -04004186 ret = BTRFS_NO_LOG_SYNC;
4187 goto end_no_trans;
4188 }
4189
Miao Xie8b050d32014-02-20 18:08:58 +08004190 ret = start_log_trans(trans, root, ctx);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004191 if (ret)
Miao Xiee87ac132014-02-20 18:08:53 +08004192 goto end_no_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004193
4194 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004195 if (ret)
4196 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004197
Chris Masonaf4176b2009-03-24 10:24:31 -04004198 /*
4199 * for regular files, if its inode is already on disk, we don't
4200 * have to worry about the parents at all. This is because
4201 * we can use the last_unlink_trans field to record renames
4202 * and other fun in this file.
4203 */
4204 if (S_ISREG(inode->i_mode) &&
4205 BTRFS_I(inode)->generation <= last_committed &&
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004206 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
4207 ret = 0;
4208 goto end_trans;
4209 }
Chris Masonaf4176b2009-03-24 10:24:31 -04004210
4211 inode_only = LOG_INODE_EXISTS;
Chris Masond3977122009-01-05 21:25:51 -05004212 while (1) {
Chris Mason12fcfd22009-03-24 10:24:20 -04004213 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
Chris Masone02119d2008-09-05 16:13:11 -04004214 break;
4215
Chris Mason12fcfd22009-03-24 10:24:20 -04004216 inode = parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004217 if (root != BTRFS_I(inode)->root)
4218 break;
4219
Chris Mason12fcfd22009-03-24 10:24:20 -04004220 if (BTRFS_I(inode)->generation >
4221 root->fs_info->last_trans_committed) {
4222 ret = btrfs_log_inode(trans, root, inode, inode_only);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004223 if (ret)
4224 goto end_trans;
Chris Mason12fcfd22009-03-24 10:24:20 -04004225 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004226 if (IS_ROOT(parent))
Chris Masone02119d2008-09-05 16:13:11 -04004227 break;
Chris Mason12fcfd22009-03-24 10:24:20 -04004228
Josef Bacik6a912212010-11-20 09:48:00 +00004229 parent = dget_parent(parent);
4230 dput(old_parent);
4231 old_parent = parent;
Chris Masone02119d2008-09-05 16:13:11 -04004232 }
Chris Mason12fcfd22009-03-24 10:24:20 -04004233 ret = 0;
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004234end_trans:
Josef Bacik6a912212010-11-20 09:48:00 +00004235 dput(old_parent);
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004236 if (ret < 0) {
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004237 root->fs_info->last_trans_log_full_commit = trans->transid;
4238 ret = 1;
4239 }
Miao Xie8b050d32014-02-20 18:08:58 +08004240
4241 if (ret)
4242 btrfs_remove_log_ctx(root, ctx);
Chris Mason12fcfd22009-03-24 10:24:20 -04004243 btrfs_end_log_trans(root);
4244end_no_trans:
4245 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004246}
4247
4248/*
4249 * it is not safe to log dentry if the chunk root has added new
4250 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
4251 * If this returns 1, you must commit the transaction to safely get your
4252 * data on disk.
4253 */
4254int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
Miao Xie8b050d32014-02-20 18:08:58 +08004255 struct btrfs_root *root, struct dentry *dentry,
4256 struct btrfs_log_ctx *ctx)
Chris Masone02119d2008-09-05 16:13:11 -04004257{
Josef Bacik6a912212010-11-20 09:48:00 +00004258 struct dentry *parent = dget_parent(dentry);
4259 int ret;
4260
Miao Xie8b050d32014-02-20 18:08:58 +08004261 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent,
4262 0, ctx);
Josef Bacik6a912212010-11-20 09:48:00 +00004263 dput(parent);
4264
4265 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004266}
4267
4268/*
4269 * should be called during mount to recover any replay any log trees
4270 * from the FS
4271 */
4272int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
4273{
4274 int ret;
4275 struct btrfs_path *path;
4276 struct btrfs_trans_handle *trans;
4277 struct btrfs_key key;
4278 struct btrfs_key found_key;
4279 struct btrfs_key tmp_key;
4280 struct btrfs_root *log;
4281 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
4282 struct walk_control wc = {
4283 .process_func = process_one_buffer,
4284 .stage = 0,
4285 };
4286
Chris Masone02119d2008-09-05 16:13:11 -04004287 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004288 if (!path)
4289 return -ENOMEM;
4290
4291 fs_info->log_root_recovering = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004292
Yan, Zheng4a500fd2010-05-16 10:49:59 -04004293 trans = btrfs_start_transaction(fs_info->tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004294 if (IS_ERR(trans)) {
4295 ret = PTR_ERR(trans);
4296 goto error;
4297 }
Chris Masone02119d2008-09-05 16:13:11 -04004298
4299 wc.trans = trans;
4300 wc.pin = 1;
4301
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004302 ret = walk_log_tree(trans, log_root_tree, &wc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004303 if (ret) {
4304 btrfs_error(fs_info, ret, "Failed to pin buffers while "
4305 "recovering log root tree.");
4306 goto error;
4307 }
Chris Masone02119d2008-09-05 16:13:11 -04004308
4309again:
4310 key.objectid = BTRFS_TREE_LOG_OBJECTID;
4311 key.offset = (u64)-1;
4312 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
4313
Chris Masond3977122009-01-05 21:25:51 -05004314 while (1) {
Chris Masone02119d2008-09-05 16:13:11 -04004315 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004316
4317 if (ret < 0) {
4318 btrfs_error(fs_info, ret,
4319 "Couldn't find tree log root.");
4320 goto error;
4321 }
Chris Masone02119d2008-09-05 16:13:11 -04004322 if (ret > 0) {
4323 if (path->slots[0] == 0)
4324 break;
4325 path->slots[0]--;
4326 }
4327 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
4328 path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004329 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004330 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4331 break;
4332
Miao Xiecb517ea2013-05-15 07:48:19 +00004333 log = btrfs_read_fs_root(log_root_tree, &found_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004334 if (IS_ERR(log)) {
4335 ret = PTR_ERR(log);
4336 btrfs_error(fs_info, ret,
4337 "Couldn't read tree log root.");
4338 goto error;
4339 }
Chris Masone02119d2008-09-05 16:13:11 -04004340
4341 tmp_key.objectid = found_key.offset;
4342 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
4343 tmp_key.offset = (u64)-1;
4344
4345 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004346 if (IS_ERR(wc.replay_dest)) {
4347 ret = PTR_ERR(wc.replay_dest);
Josef Bacikb50c6e22013-04-25 15:55:30 -04004348 free_extent_buffer(log->node);
4349 free_extent_buffer(log->commit_root);
4350 kfree(log);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004351 btrfs_error(fs_info, ret, "Couldn't read target root "
4352 "for tree log recovery.");
4353 goto error;
4354 }
Chris Masone02119d2008-09-05 16:13:11 -04004355
Yan Zheng07d400a2009-01-06 11:42:00 -05004356 wc.replay_dest->log_root = log;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004357 btrfs_record_root_in_trans(trans, wc.replay_dest);
Chris Masone02119d2008-09-05 16:13:11 -04004358 ret = walk_log_tree(trans, log, &wc);
Chris Masone02119d2008-09-05 16:13:11 -04004359
Josef Bacikb50c6e22013-04-25 15:55:30 -04004360 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
Chris Masone02119d2008-09-05 16:13:11 -04004361 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4362 path);
Chris Masone02119d2008-09-05 16:13:11 -04004363 }
Chris Masone02119d2008-09-05 16:13:11 -04004364
4365 key.offset = found_key.offset - 1;
Yan Zheng07d400a2009-01-06 11:42:00 -05004366 wc.replay_dest->log_root = NULL;
Chris Masone02119d2008-09-05 16:13:11 -04004367 free_extent_buffer(log->node);
Chris Masonb263c2c2009-06-11 11:24:47 -04004368 free_extent_buffer(log->commit_root);
Chris Masone02119d2008-09-05 16:13:11 -04004369 kfree(log);
4370
Josef Bacikb50c6e22013-04-25 15:55:30 -04004371 if (ret)
4372 goto error;
4373
Chris Masone02119d2008-09-05 16:13:11 -04004374 if (found_key.offset == 0)
4375 break;
4376 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004377 btrfs_release_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004378
4379 /* step one is to pin it all, step two is to replay just inodes */
4380 if (wc.pin) {
4381 wc.pin = 0;
4382 wc.process_func = replay_one_buffer;
4383 wc.stage = LOG_WALK_REPLAY_INODES;
4384 goto again;
4385 }
4386 /* step three is to replay everything */
4387 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4388 wc.stage++;
4389 goto again;
4390 }
4391
4392 btrfs_free_path(path);
4393
Josef Bacikabefa552013-04-24 16:40:05 -04004394 /* step 4: commit the transaction, which also unpins the blocks */
4395 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
4396 if (ret)
4397 return ret;
4398
Chris Masone02119d2008-09-05 16:13:11 -04004399 free_extent_buffer(log_root_tree->node);
4400 log_root_tree->log_root = NULL;
4401 fs_info->log_root_recovering = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004402 kfree(log_root_tree);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004403
Josef Bacikabefa552013-04-24 16:40:05 -04004404 return 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004405error:
Josef Bacikb50c6e22013-04-25 15:55:30 -04004406 if (wc.trans)
4407 btrfs_end_transaction(wc.trans, fs_info->tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004408 btrfs_free_path(path);
4409 return ret;
Chris Masone02119d2008-09-05 16:13:11 -04004410}
Chris Mason12fcfd22009-03-24 10:24:20 -04004411
4412/*
4413 * there are some corner cases where we want to force a full
4414 * commit instead of allowing a directory to be logged.
4415 *
4416 * They revolve around files there were unlinked from the directory, and
4417 * this function updates the parent directory so that a full commit is
4418 * properly done if it is fsync'd later after the unlinks are done.
4419 */
4420void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4421 struct inode *dir, struct inode *inode,
4422 int for_rename)
4423{
4424 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004425 * when we're logging a file, if it hasn't been renamed
4426 * or unlinked, and its inode is fully committed on disk,
4427 * we don't have to worry about walking up the directory chain
4428 * to log its parents.
4429 *
4430 * So, we use the last_unlink_trans field to put this transid
4431 * into the file. When the file is logged we check it and
4432 * don't log the parents if the file is fully on disk.
4433 */
4434 if (S_ISREG(inode->i_mode))
4435 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4436
4437 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004438 * if this directory was already logged any new
4439 * names for this file/dir will get recorded
4440 */
4441 smp_mb();
4442 if (BTRFS_I(dir)->logged_trans == trans->transid)
4443 return;
4444
4445 /*
4446 * if the inode we're about to unlink was logged,
4447 * the log will be properly updated for any new names
4448 */
4449 if (BTRFS_I(inode)->logged_trans == trans->transid)
4450 return;
4451
4452 /*
4453 * when renaming files across directories, if the directory
4454 * there we're unlinking from gets fsync'd later on, there's
4455 * no way to find the destination directory later and fsync it
4456 * properly. So, we have to be conservative and force commits
4457 * so the new name gets discovered.
4458 */
4459 if (for_rename)
4460 goto record;
4461
4462 /* we can safely do the unlink without any special recording */
4463 return;
4464
4465record:
4466 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4467}
4468
4469/*
4470 * Call this after adding a new name for a file and it will properly
4471 * update the log to reflect the new name.
4472 *
4473 * It will return zero if all goes well, and it will return 1 if a
4474 * full transaction commit is required.
4475 */
4476int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4477 struct inode *inode, struct inode *old_dir,
4478 struct dentry *parent)
4479{
4480 struct btrfs_root * root = BTRFS_I(inode)->root;
4481
4482 /*
Chris Masonaf4176b2009-03-24 10:24:31 -04004483 * this will force the logging code to walk the dentry chain
4484 * up for the file
4485 */
4486 if (S_ISREG(inode->i_mode))
4487 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4488
4489 /*
Chris Mason12fcfd22009-03-24 10:24:20 -04004490 * if this inode hasn't been logged and directory we're renaming it
4491 * from hasn't been logged, we don't need to log it
4492 */
4493 if (BTRFS_I(inode)->logged_trans <=
4494 root->fs_info->last_trans_committed &&
4495 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4496 root->fs_info->last_trans_committed))
4497 return 0;
4498
Miao Xie8b050d32014-02-20 18:08:58 +08004499 return btrfs_log_inode_parent(trans, root, inode, parent, 1, NULL);
Chris Mason12fcfd22009-03-24 10:24:20 -04004500}
4501