1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2008 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/slab.h>
8 #include <linux/blkdev.h>
9 #include <linux/list_sort.h>
10 #include <linux/iversion.h>
15 #include "print-tree.h"
17 #include "compression.h"
19 #include "inode-map.h"
21 /* magic values for the inode_only field in btrfs_log_inode:
23 * LOG_INODE_ALL means to log everything
24 * LOG_INODE_EXISTS means to log just enough to recreate the inode
27 #define LOG_INODE_ALL 0
28 #define LOG_INODE_EXISTS 1
29 #define LOG_OTHER_INODE 2
32 * directory trouble cases
34 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
35 * log, we must force a full commit before doing an fsync of the directory
36 * where the unlink was done.
37 * ---> record transid of last unlink/rename per directory
41 * rename foo/some_dir foo2/some_dir
43 * fsync foo/some_dir/some_file
45 * The fsync above will unlink the original some_dir without recording
46 * it in its new location (foo2). After a crash, some_dir will be gone
47 * unless the fsync of some_file forces a full commit
49 * 2) we must log any new names for any file or dir that is in the fsync
50 * log. ---> check inode while renaming/linking.
52 * 2a) we must log any new names for any file or dir during rename
53 * when the directory they are being removed from was logged.
54 * ---> check inode and old parent dir during rename
56 * 2a is actually the more important variant. With the extra logging
57 * a crash might unlink the old name without recreating the new one
59 * 3) after a crash, we must go through any directories with a link count
60 * of zero and redo the rm -rf
67 * The directory f1 was fully removed from the FS, but fsync was never
68 * called on f1, only its parent dir. After a crash the rm -rf must
69 * be replayed. This must be able to recurse down the entire
70 * directory tree. The inode link count fixup code takes care of the
75 * stages for the tree walking. The first
76 * stage (0) is to only pin down the blocks we find
77 * the second stage (1) is to make sure that all the inodes
78 * we find in the log are created in the subvolume.
80 * The last stage is to deal with directories and links and extents
81 * and all the other fun semantics
83 #define LOG_WALK_PIN_ONLY 0
84 #define LOG_WALK_REPLAY_INODES 1
85 #define LOG_WALK_REPLAY_DIR_INDEX 2
86 #define LOG_WALK_REPLAY_ALL 3
88 static int btrfs_log_inode(struct btrfs_trans_handle
*trans
,
89 struct btrfs_root
*root
, struct btrfs_inode
*inode
,
93 struct btrfs_log_ctx
*ctx
);
94 static int link_to_fixup_dir(struct btrfs_trans_handle
*trans
,
95 struct btrfs_root
*root
,
96 struct btrfs_path
*path
, u64 objectid
);
97 static noinline
int replay_dir_deletes(struct btrfs_trans_handle
*trans
,
98 struct btrfs_root
*root
,
99 struct btrfs_root
*log
,
100 struct btrfs_path
*path
,
101 u64 dirid
, int del_all
);
104 * tree logging is a special write ahead log used to make sure that
105 * fsyncs and O_SYNCs can happen without doing full tree commits.
107 * Full tree commits are expensive because they require commonly
108 * modified blocks to be recowed, creating many dirty pages in the
109 * extent tree an 4x-6x higher write load than ext3.
111 * Instead of doing a tree commit on every fsync, we use the
112 * key ranges and transaction ids to find items for a given file or directory
113 * that have changed in this transaction. Those items are copied into
114 * a special tree (one per subvolume root), that tree is written to disk
115 * and then the fsync is considered complete.
117 * After a crash, items are copied out of the log-tree back into the
118 * subvolume tree. Any file data extents found are recorded in the extent
119 * allocation tree, and the log-tree freed.
121 * The log tree is read three times, once to pin down all the extents it is
122 * using in ram and once, once to create all the inodes logged in the tree
123 * and once to do all the other items.
127 * start a sub transaction and setup the log tree
128 * this increments the log tree writer count to make the people
129 * syncing the tree wait for us to finish
131 static int start_log_trans(struct btrfs_trans_handle
*trans
,
132 struct btrfs_root
*root
,
133 struct btrfs_log_ctx
*ctx
)
135 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
138 mutex_lock(&root
->log_mutex
);
140 if (root
->log_root
) {
141 if (btrfs_need_log_full_commit(fs_info
, trans
)) {
146 if (!root
->log_start_pid
) {
147 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS
, &root
->state
);
148 root
->log_start_pid
= current
->pid
;
149 } else if (root
->log_start_pid
!= current
->pid
) {
150 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS
, &root
->state
);
153 mutex_lock(&fs_info
->tree_log_mutex
);
154 if (!fs_info
->log_root_tree
)
155 ret
= btrfs_init_log_root_tree(trans
, fs_info
);
156 mutex_unlock(&fs_info
->tree_log_mutex
);
160 ret
= btrfs_add_log_tree(trans
, root
);
164 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS
, &root
->state
);
165 root
->log_start_pid
= current
->pid
;
168 atomic_inc(&root
->log_batch
);
169 atomic_inc(&root
->log_writers
);
171 int index
= root
->log_transid
% 2;
172 list_add_tail(&ctx
->list
, &root
->log_ctxs
[index
]);
173 ctx
->log_transid
= root
->log_transid
;
177 mutex_unlock(&root
->log_mutex
);
182 * returns 0 if there was a log transaction running and we were able
183 * to join, or returns -ENOENT if there were not transactions
186 static int join_running_log_trans(struct btrfs_root
*root
)
194 mutex_lock(&root
->log_mutex
);
195 if (root
->log_root
) {
197 atomic_inc(&root
->log_writers
);
199 mutex_unlock(&root
->log_mutex
);
204 * This either makes the current running log transaction wait
205 * until you call btrfs_end_log_trans() or it makes any future
206 * log transactions wait until you call btrfs_end_log_trans()
208 void btrfs_pin_log_trans(struct btrfs_root
*root
)
210 mutex_lock(&root
->log_mutex
);
211 atomic_inc(&root
->log_writers
);
212 mutex_unlock(&root
->log_mutex
);
216 * indicate we're done making changes to the log tree
217 * and wake up anyone waiting to do a sync
219 void btrfs_end_log_trans(struct btrfs_root
*root
)
221 if (atomic_dec_and_test(&root
->log_writers
)) {
222 /* atomic_dec_and_test implies a barrier */
223 cond_wake_up_nomb(&root
->log_writer_wait
);
229 * the walk control struct is used to pass state down the chain when
230 * processing the log tree. The stage field tells us which part
231 * of the log tree processing we are currently doing. The others
232 * are state fields used for that specific part
234 struct walk_control
{
235 /* should we free the extent on disk when done? This is used
236 * at transaction commit time while freeing a log tree
240 /* should we write out the extent buffer? This is used
241 * while flushing the log tree to disk during a sync
245 /* should we wait for the extent buffer io to finish? Also used
246 * while flushing the log tree to disk for a sync
250 /* pin only walk, we record which extents on disk belong to the
255 /* what stage of the replay code we're currently in */
259 * Ignore any items from the inode currently being processed. Needs
260 * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in
261 * the LOG_WALK_REPLAY_INODES stage.
263 bool ignore_cur_inode
;
265 /* the root we are currently replaying */
266 struct btrfs_root
*replay_dest
;
268 /* the trans handle for the current replay */
269 struct btrfs_trans_handle
*trans
;
271 /* the function that gets used to process blocks we find in the
272 * tree. Note the extent_buffer might not be up to date when it is
273 * passed in, and it must be checked or read if you need the data
276 int (*process_func
)(struct btrfs_root
*log
, struct extent_buffer
*eb
,
277 struct walk_control
*wc
, u64 gen
, int level
);
281 * process_func used to pin down extents, write them or wait on them
283 static int process_one_buffer(struct btrfs_root
*log
,
284 struct extent_buffer
*eb
,
285 struct walk_control
*wc
, u64 gen
, int level
)
287 struct btrfs_fs_info
*fs_info
= log
->fs_info
;
291 * If this fs is mixed then we need to be able to process the leaves to
292 * pin down any logged extents, so we have to read the block.
294 if (btrfs_fs_incompat(fs_info
, MIXED_GROUPS
)) {
295 ret
= btrfs_read_buffer(eb
, gen
, level
, NULL
);
301 ret
= btrfs_pin_extent_for_log_replay(fs_info
, eb
->start
,
304 if (!ret
&& btrfs_buffer_uptodate(eb
, gen
, 0)) {
305 if (wc
->pin
&& btrfs_header_level(eb
) == 0)
306 ret
= btrfs_exclude_logged_extents(fs_info
, eb
);
308 btrfs_write_tree_block(eb
);
310 btrfs_wait_tree_block_writeback(eb
);
316 * Item overwrite used by replay and tree logging. eb, slot and key all refer
317 * to the src data we are copying out.
319 * root is the tree we are copying into, and path is a scratch
320 * path for use in this function (it should be released on entry and
321 * will be released on exit).
323 * If the key is already in the destination tree the existing item is
324 * overwritten. If the existing item isn't big enough, it is extended.
325 * If it is too large, it is truncated.
327 * If the key isn't in the destination yet, a new item is inserted.
329 static noinline
int overwrite_item(struct btrfs_trans_handle
*trans
,
330 struct btrfs_root
*root
,
331 struct btrfs_path
*path
,
332 struct extent_buffer
*eb
, int slot
,
333 struct btrfs_key
*key
)
335 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
338 u64 saved_i_size
= 0;
339 int save_old_i_size
= 0;
340 unsigned long src_ptr
;
341 unsigned long dst_ptr
;
342 int overwrite_root
= 0;
343 bool inode_item
= key
->type
== BTRFS_INODE_ITEM_KEY
;
345 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
348 item_size
= btrfs_item_size_nr(eb
, slot
);
349 src_ptr
= btrfs_item_ptr_offset(eb
, slot
);
351 /* look for the key in the destination tree */
352 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
359 u32 dst_size
= btrfs_item_size_nr(path
->nodes
[0],
361 if (dst_size
!= item_size
)
364 if (item_size
== 0) {
365 btrfs_release_path(path
);
368 dst_copy
= kmalloc(item_size
, GFP_NOFS
);
369 src_copy
= kmalloc(item_size
, GFP_NOFS
);
370 if (!dst_copy
|| !src_copy
) {
371 btrfs_release_path(path
);
377 read_extent_buffer(eb
, src_copy
, src_ptr
, item_size
);
379 dst_ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
380 read_extent_buffer(path
->nodes
[0], dst_copy
, dst_ptr
,
382 ret
= memcmp(dst_copy
, src_copy
, item_size
);
387 * they have the same contents, just return, this saves
388 * us from cowing blocks in the destination tree and doing
389 * extra writes that may not have been done by a previous
393 btrfs_release_path(path
);
398 * We need to load the old nbytes into the inode so when we
399 * replay the extents we've logged we get the right nbytes.
402 struct btrfs_inode_item
*item
;
406 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
407 struct btrfs_inode_item
);
408 nbytes
= btrfs_inode_nbytes(path
->nodes
[0], item
);
409 item
= btrfs_item_ptr(eb
, slot
,
410 struct btrfs_inode_item
);
411 btrfs_set_inode_nbytes(eb
, item
, nbytes
);
414 * If this is a directory we need to reset the i_size to
415 * 0 so that we can set it up properly when replaying
416 * the rest of the items in this log.
418 mode
= btrfs_inode_mode(eb
, item
);
420 btrfs_set_inode_size(eb
, item
, 0);
422 } else if (inode_item
) {
423 struct btrfs_inode_item
*item
;
427 * New inode, set nbytes to 0 so that the nbytes comes out
428 * properly when we replay the extents.
430 item
= btrfs_item_ptr(eb
, slot
, struct btrfs_inode_item
);
431 btrfs_set_inode_nbytes(eb
, item
, 0);
434 * If this is a directory we need to reset the i_size to 0 so
435 * that we can set it up properly when replaying the rest of
436 * the items in this log.
438 mode
= btrfs_inode_mode(eb
, item
);
440 btrfs_set_inode_size(eb
, item
, 0);
443 btrfs_release_path(path
);
444 /* try to insert the key into the destination tree */
445 path
->skip_release_on_error
= 1;
446 ret
= btrfs_insert_empty_item(trans
, root
, path
,
448 path
->skip_release_on_error
= 0;
450 /* make sure any existing item is the correct size */
451 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
) {
453 found_size
= btrfs_item_size_nr(path
->nodes
[0],
455 if (found_size
> item_size
)
456 btrfs_truncate_item(fs_info
, path
, item_size
, 1);
457 else if (found_size
< item_size
)
458 btrfs_extend_item(fs_info
, path
,
459 item_size
- found_size
);
463 dst_ptr
= btrfs_item_ptr_offset(path
->nodes
[0],
466 /* don't overwrite an existing inode if the generation number
467 * was logged as zero. This is done when the tree logging code
468 * is just logging an inode to make sure it exists after recovery.
470 * Also, don't overwrite i_size on directories during replay.
471 * log replay inserts and removes directory items based on the
472 * state of the tree found in the subvolume, and i_size is modified
475 if (key
->type
== BTRFS_INODE_ITEM_KEY
&& ret
== -EEXIST
) {
476 struct btrfs_inode_item
*src_item
;
477 struct btrfs_inode_item
*dst_item
;
479 src_item
= (struct btrfs_inode_item
*)src_ptr
;
480 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
482 if (btrfs_inode_generation(eb
, src_item
) == 0) {
483 struct extent_buffer
*dst_eb
= path
->nodes
[0];
484 const u64 ino_size
= btrfs_inode_size(eb
, src_item
);
487 * For regular files an ino_size == 0 is used only when
488 * logging that an inode exists, as part of a directory
489 * fsync, and the inode wasn't fsynced before. In this
490 * case don't set the size of the inode in the fs/subvol
491 * tree, otherwise we would be throwing valid data away.
493 if (S_ISREG(btrfs_inode_mode(eb
, src_item
)) &&
494 S_ISREG(btrfs_inode_mode(dst_eb
, dst_item
)) &&
496 struct btrfs_map_token token
;
498 btrfs_init_map_token(&token
);
499 btrfs_set_token_inode_size(dst_eb
, dst_item
,
505 if (overwrite_root
&&
506 S_ISDIR(btrfs_inode_mode(eb
, src_item
)) &&
507 S_ISDIR(btrfs_inode_mode(path
->nodes
[0], dst_item
))) {
509 saved_i_size
= btrfs_inode_size(path
->nodes
[0],
514 copy_extent_buffer(path
->nodes
[0], eb
, dst_ptr
,
517 if (save_old_i_size
) {
518 struct btrfs_inode_item
*dst_item
;
519 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
520 btrfs_set_inode_size(path
->nodes
[0], dst_item
, saved_i_size
);
523 /* make sure the generation is filled in */
524 if (key
->type
== BTRFS_INODE_ITEM_KEY
) {
525 struct btrfs_inode_item
*dst_item
;
526 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
527 if (btrfs_inode_generation(path
->nodes
[0], dst_item
) == 0) {
528 btrfs_set_inode_generation(path
->nodes
[0], dst_item
,
533 btrfs_mark_buffer_dirty(path
->nodes
[0]);
534 btrfs_release_path(path
);
539 * simple helper to read an inode off the disk from a given root
540 * This can only be called for subvolume roots and not for the log
542 static noinline
struct inode
*read_one_inode(struct btrfs_root
*root
,
545 struct btrfs_key key
;
548 key
.objectid
= objectid
;
549 key
.type
= BTRFS_INODE_ITEM_KEY
;
551 inode
= btrfs_iget(root
->fs_info
->sb
, &key
, root
, NULL
);
557 /* replays a single extent in 'eb' at 'slot' with 'key' into the
558 * subvolume 'root'. path is released on entry and should be released
561 * extents in the log tree have not been allocated out of the extent
562 * tree yet. So, this completes the allocation, taking a reference
563 * as required if the extent already exists or creating a new extent
564 * if it isn't in the extent allocation tree yet.
566 * The extent is inserted into the file, dropping any existing extents
567 * from the file that overlap the new one.
569 static noinline
int replay_one_extent(struct btrfs_trans_handle
*trans
,
570 struct btrfs_root
*root
,
571 struct btrfs_path
*path
,
572 struct extent_buffer
*eb
, int slot
,
573 struct btrfs_key
*key
)
575 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
578 u64 start
= key
->offset
;
580 struct btrfs_file_extent_item
*item
;
581 struct inode
*inode
= NULL
;
585 item
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
586 found_type
= btrfs_file_extent_type(eb
, item
);
588 if (found_type
== BTRFS_FILE_EXTENT_REG
||
589 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
590 nbytes
= btrfs_file_extent_num_bytes(eb
, item
);
591 extent_end
= start
+ nbytes
;
594 * We don't add to the inodes nbytes if we are prealloc or a
597 if (btrfs_file_extent_disk_bytenr(eb
, item
) == 0)
599 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
600 size
= btrfs_file_extent_ram_bytes(eb
, item
);
601 nbytes
= btrfs_file_extent_ram_bytes(eb
, item
);
602 extent_end
= ALIGN(start
+ size
,
603 fs_info
->sectorsize
);
609 inode
= read_one_inode(root
, key
->objectid
);
616 * first check to see if we already have this extent in the
617 * file. This must be done before the btrfs_drop_extents run
618 * so we don't try to drop this extent.
620 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
621 btrfs_ino(BTRFS_I(inode
)), start
, 0);
624 (found_type
== BTRFS_FILE_EXTENT_REG
||
625 found_type
== BTRFS_FILE_EXTENT_PREALLOC
)) {
626 struct btrfs_file_extent_item cmp1
;
627 struct btrfs_file_extent_item cmp2
;
628 struct btrfs_file_extent_item
*existing
;
629 struct extent_buffer
*leaf
;
631 leaf
= path
->nodes
[0];
632 existing
= btrfs_item_ptr(leaf
, path
->slots
[0],
633 struct btrfs_file_extent_item
);
635 read_extent_buffer(eb
, &cmp1
, (unsigned long)item
,
637 read_extent_buffer(leaf
, &cmp2
, (unsigned long)existing
,
641 * we already have a pointer to this exact extent,
642 * we don't have to do anything
644 if (memcmp(&cmp1
, &cmp2
, sizeof(cmp1
)) == 0) {
645 btrfs_release_path(path
);
649 btrfs_release_path(path
);
651 /* drop any overlapping extents */
652 ret
= btrfs_drop_extents(trans
, root
, inode
, start
, extent_end
, 1);
656 if (found_type
== BTRFS_FILE_EXTENT_REG
||
657 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
659 unsigned long dest_offset
;
660 struct btrfs_key ins
;
662 if (btrfs_file_extent_disk_bytenr(eb
, item
) == 0 &&
663 btrfs_fs_incompat(fs_info
, NO_HOLES
))
666 ret
= btrfs_insert_empty_item(trans
, root
, path
, key
,
670 dest_offset
= btrfs_item_ptr_offset(path
->nodes
[0],
672 copy_extent_buffer(path
->nodes
[0], eb
, dest_offset
,
673 (unsigned long)item
, sizeof(*item
));
675 ins
.objectid
= btrfs_file_extent_disk_bytenr(eb
, item
);
676 ins
.offset
= btrfs_file_extent_disk_num_bytes(eb
, item
);
677 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
678 offset
= key
->offset
- btrfs_file_extent_offset(eb
, item
);
681 * Manually record dirty extent, as here we did a shallow
682 * file extent item copy and skip normal backref update,
683 * but modifying extent tree all by ourselves.
684 * So need to manually record dirty extent for qgroup,
685 * as the owner of the file extent changed from log tree
686 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
688 ret
= btrfs_qgroup_trace_extent(trans
,
689 btrfs_file_extent_disk_bytenr(eb
, item
),
690 btrfs_file_extent_disk_num_bytes(eb
, item
),
695 if (ins
.objectid
> 0) {
698 LIST_HEAD(ordered_sums
);
700 * is this extent already allocated in the extent
701 * allocation tree? If so, just add a reference
703 ret
= btrfs_lookup_data_extent(fs_info
, ins
.objectid
,
706 ret
= btrfs_inc_extent_ref(trans
, root
,
707 ins
.objectid
, ins
.offset
,
708 0, root
->root_key
.objectid
,
709 key
->objectid
, offset
);
714 * insert the extent pointer in the extent
717 ret
= btrfs_alloc_logged_file_extent(trans
,
718 root
->root_key
.objectid
,
719 key
->objectid
, offset
, &ins
);
723 btrfs_release_path(path
);
725 if (btrfs_file_extent_compression(eb
, item
)) {
726 csum_start
= ins
.objectid
;
727 csum_end
= csum_start
+ ins
.offset
;
729 csum_start
= ins
.objectid
+
730 btrfs_file_extent_offset(eb
, item
);
731 csum_end
= csum_start
+
732 btrfs_file_extent_num_bytes(eb
, item
);
735 ret
= btrfs_lookup_csums_range(root
->log_root
,
736 csum_start
, csum_end
- 1,
741 * Now delete all existing cums in the csum root that
742 * cover our range. We do this because we can have an
743 * extent that is completely referenced by one file
744 * extent item and partially referenced by another
745 * file extent item (like after using the clone or
746 * extent_same ioctls). In this case if we end up doing
747 * the replay of the one that partially references the
748 * extent first, and we do not do the csum deletion
749 * below, we can get 2 csum items in the csum tree that
750 * overlap each other. For example, imagine our log has
751 * the two following file extent items:
753 * key (257 EXTENT_DATA 409600)
754 * extent data disk byte 12845056 nr 102400
755 * extent data offset 20480 nr 20480 ram 102400
757 * key (257 EXTENT_DATA 819200)
758 * extent data disk byte 12845056 nr 102400
759 * extent data offset 0 nr 102400 ram 102400
761 * Where the second one fully references the 100K extent
762 * that starts at disk byte 12845056, and the log tree
763 * has a single csum item that covers the entire range
766 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
768 * After the first file extent item is replayed, the
769 * csum tree gets the following csum item:
771 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
773 * Which covers the 20K sub-range starting at offset 20K
774 * of our extent. Now when we replay the second file
775 * extent item, if we do not delete existing csum items
776 * that cover any of its blocks, we end up getting two
777 * csum items in our csum tree that overlap each other:
779 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
780 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
782 * Which is a problem, because after this anyone trying
783 * to lookup up for the checksum of any block of our
784 * extent starting at an offset of 40K or higher, will
785 * end up looking at the second csum item only, which
786 * does not contain the checksum for any block starting
787 * at offset 40K or higher of our extent.
789 while (!list_empty(&ordered_sums
)) {
790 struct btrfs_ordered_sum
*sums
;
791 sums
= list_entry(ordered_sums
.next
,
792 struct btrfs_ordered_sum
,
795 ret
= btrfs_del_csums(trans
, fs_info
,
799 ret
= btrfs_csum_file_blocks(trans
,
800 fs_info
->csum_root
, sums
);
801 list_del(&sums
->list
);
807 btrfs_release_path(path
);
809 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
810 /* inline extents are easy, we just overwrite them */
811 ret
= overwrite_item(trans
, root
, path
, eb
, slot
, key
);
816 inode_add_bytes(inode
, nbytes
);
818 ret
= btrfs_update_inode(trans
, root
, inode
);
826 * when cleaning up conflicts between the directory names in the
827 * subvolume, directory names in the log and directory names in the
828 * inode back references, we may have to unlink inodes from directories.
830 * This is a helper function to do the unlink of a specific directory
833 static noinline
int drop_one_dir_item(struct btrfs_trans_handle
*trans
,
834 struct btrfs_root
*root
,
835 struct btrfs_path
*path
,
836 struct btrfs_inode
*dir
,
837 struct btrfs_dir_item
*di
)
842 struct extent_buffer
*leaf
;
843 struct btrfs_key location
;
846 leaf
= path
->nodes
[0];
848 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
849 name_len
= btrfs_dir_name_len(leaf
, di
);
850 name
= kmalloc(name_len
, GFP_NOFS
);
854 read_extent_buffer(leaf
, name
, (unsigned long)(di
+ 1), name_len
);
855 btrfs_release_path(path
);
857 inode
= read_one_inode(root
, location
.objectid
);
863 ret
= link_to_fixup_dir(trans
, root
, path
, location
.objectid
);
867 ret
= btrfs_unlink_inode(trans
, root
, dir
, BTRFS_I(inode
), name
,
872 ret
= btrfs_run_delayed_items(trans
);
880 * helper function to see if a given name and sequence number found
881 * in an inode back reference are already in a directory and correctly
882 * point to this inode
884 static noinline
int inode_in_dir(struct btrfs_root
*root
,
885 struct btrfs_path
*path
,
886 u64 dirid
, u64 objectid
, u64 index
,
887 const char *name
, int name_len
)
889 struct btrfs_dir_item
*di
;
890 struct btrfs_key location
;
893 di
= btrfs_lookup_dir_index_item(NULL
, root
, path
, dirid
,
894 index
, name
, name_len
, 0);
895 if (di
&& !IS_ERR(di
)) {
896 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
897 if (location
.objectid
!= objectid
)
901 btrfs_release_path(path
);
903 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dirid
, name
, name_len
, 0);
904 if (di
&& !IS_ERR(di
)) {
905 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
906 if (location
.objectid
!= objectid
)
912 btrfs_release_path(path
);
917 * helper function to check a log tree for a named back reference in
918 * an inode. This is used to decide if a back reference that is
919 * found in the subvolume conflicts with what we find in the log.
921 * inode backreferences may have multiple refs in a single item,
922 * during replay we process one reference at a time, and we don't
923 * want to delete valid links to a file from the subvolume if that
924 * link is also in the log.
926 static noinline
int backref_in_log(struct btrfs_root
*log
,
927 struct btrfs_key
*key
,
929 const char *name
, int namelen
)
931 struct btrfs_path
*path
;
932 struct btrfs_inode_ref
*ref
;
934 unsigned long ptr_end
;
935 unsigned long name_ptr
;
941 path
= btrfs_alloc_path();
945 ret
= btrfs_search_slot(NULL
, log
, key
, path
, 0, 0);
949 ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
951 if (key
->type
== BTRFS_INODE_EXTREF_KEY
) {
952 if (btrfs_find_name_in_ext_backref(path
->nodes
[0],
955 name
, namelen
, NULL
))
961 item_size
= btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]);
962 ptr_end
= ptr
+ item_size
;
963 while (ptr
< ptr_end
) {
964 ref
= (struct btrfs_inode_ref
*)ptr
;
965 found_name_len
= btrfs_inode_ref_name_len(path
->nodes
[0], ref
);
966 if (found_name_len
== namelen
) {
967 name_ptr
= (unsigned long)(ref
+ 1);
968 ret
= memcmp_extent_buffer(path
->nodes
[0], name
,
975 ptr
= (unsigned long)(ref
+ 1) + found_name_len
;
978 btrfs_free_path(path
);
982 static inline int __add_inode_ref(struct btrfs_trans_handle
*trans
,
983 struct btrfs_root
*root
,
984 struct btrfs_path
*path
,
985 struct btrfs_root
*log_root
,
986 struct btrfs_inode
*dir
,
987 struct btrfs_inode
*inode
,
988 u64 inode_objectid
, u64 parent_objectid
,
989 u64 ref_index
, char *name
, int namelen
,
995 struct extent_buffer
*leaf
;
996 struct btrfs_dir_item
*di
;
997 struct btrfs_key search_key
;
998 struct btrfs_inode_extref
*extref
;
1001 /* Search old style refs */
1002 search_key
.objectid
= inode_objectid
;
1003 search_key
.type
= BTRFS_INODE_REF_KEY
;
1004 search_key
.offset
= parent_objectid
;
1005 ret
= btrfs_search_slot(NULL
, root
, &search_key
, path
, 0, 0);
1007 struct btrfs_inode_ref
*victim_ref
;
1009 unsigned long ptr_end
;
1011 leaf
= path
->nodes
[0];
1013 /* are we trying to overwrite a back ref for the root directory
1014 * if so, just jump out, we're done
1016 if (search_key
.objectid
== search_key
.offset
)
1019 /* check all the names in this back reference to see
1020 * if they are in the log. if so, we allow them to stay
1021 * otherwise they must be unlinked as a conflict
1023 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
1024 ptr_end
= ptr
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1025 while (ptr
< ptr_end
) {
1026 victim_ref
= (struct btrfs_inode_ref
*)ptr
;
1027 victim_name_len
= btrfs_inode_ref_name_len(leaf
,
1029 victim_name
= kmalloc(victim_name_len
, GFP_NOFS
);
1033 read_extent_buffer(leaf
, victim_name
,
1034 (unsigned long)(victim_ref
+ 1),
1037 if (!backref_in_log(log_root
, &search_key
,
1041 inc_nlink(&inode
->vfs_inode
);
1042 btrfs_release_path(path
);
1044 ret
= btrfs_unlink_inode(trans
, root
, dir
, inode
,
1045 victim_name
, victim_name_len
);
1049 ret
= btrfs_run_delayed_items(trans
);
1057 ptr
= (unsigned long)(victim_ref
+ 1) + victim_name_len
;
1061 * NOTE: we have searched root tree and checked the
1062 * corresponding ref, it does not need to check again.
1066 btrfs_release_path(path
);
1068 /* Same search but for extended refs */
1069 extref
= btrfs_lookup_inode_extref(NULL
, root
, path
, name
, namelen
,
1070 inode_objectid
, parent_objectid
, 0,
1072 if (!IS_ERR_OR_NULL(extref
)) {
1076 struct inode
*victim_parent
;
1078 leaf
= path
->nodes
[0];
1080 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1081 base
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
1083 while (cur_offset
< item_size
) {
1084 extref
= (struct btrfs_inode_extref
*)(base
+ cur_offset
);
1086 victim_name_len
= btrfs_inode_extref_name_len(leaf
, extref
);
1088 if (btrfs_inode_extref_parent(leaf
, extref
) != parent_objectid
)
1091 victim_name
= kmalloc(victim_name_len
, GFP_NOFS
);
1094 read_extent_buffer(leaf
, victim_name
, (unsigned long)&extref
->name
,
1097 search_key
.objectid
= inode_objectid
;
1098 search_key
.type
= BTRFS_INODE_EXTREF_KEY
;
1099 search_key
.offset
= btrfs_extref_hash(parent_objectid
,
1103 if (!backref_in_log(log_root
, &search_key
,
1104 parent_objectid
, victim_name
,
1107 victim_parent
= read_one_inode(root
,
1109 if (victim_parent
) {
1110 inc_nlink(&inode
->vfs_inode
);
1111 btrfs_release_path(path
);
1113 ret
= btrfs_unlink_inode(trans
, root
,
1114 BTRFS_I(victim_parent
),
1119 ret
= btrfs_run_delayed_items(
1122 iput(victim_parent
);
1131 cur_offset
+= victim_name_len
+ sizeof(*extref
);
1135 btrfs_release_path(path
);
1137 /* look for a conflicting sequence number */
1138 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, btrfs_ino(dir
),
1139 ref_index
, name
, namelen
, 0);
1140 if (di
&& !IS_ERR(di
)) {
1141 ret
= drop_one_dir_item(trans
, root
, path
, dir
, di
);
1145 btrfs_release_path(path
);
1147 /* look for a conflicing name */
1148 di
= btrfs_lookup_dir_item(trans
, root
, path
, btrfs_ino(dir
),
1150 if (di
&& !IS_ERR(di
)) {
1151 ret
= drop_one_dir_item(trans
, root
, path
, dir
, di
);
1155 btrfs_release_path(path
);
1160 static int extref_get_fields(struct extent_buffer
*eb
, unsigned long ref_ptr
,
1161 u32
*namelen
, char **name
, u64
*index
,
1162 u64
*parent_objectid
)
1164 struct btrfs_inode_extref
*extref
;
1166 extref
= (struct btrfs_inode_extref
*)ref_ptr
;
1168 *namelen
= btrfs_inode_extref_name_len(eb
, extref
);
1169 *name
= kmalloc(*namelen
, GFP_NOFS
);
1173 read_extent_buffer(eb
, *name
, (unsigned long)&extref
->name
,
1177 *index
= btrfs_inode_extref_index(eb
, extref
);
1178 if (parent_objectid
)
1179 *parent_objectid
= btrfs_inode_extref_parent(eb
, extref
);
1184 static int ref_get_fields(struct extent_buffer
*eb
, unsigned long ref_ptr
,
1185 u32
*namelen
, char **name
, u64
*index
)
1187 struct btrfs_inode_ref
*ref
;
1189 ref
= (struct btrfs_inode_ref
*)ref_ptr
;
1191 *namelen
= btrfs_inode_ref_name_len(eb
, ref
);
1192 *name
= kmalloc(*namelen
, GFP_NOFS
);
1196 read_extent_buffer(eb
, *name
, (unsigned long)(ref
+ 1), *namelen
);
1199 *index
= btrfs_inode_ref_index(eb
, ref
);
1205 * Take an inode reference item from the log tree and iterate all names from the
1206 * inode reference item in the subvolume tree with the same key (if it exists).
1207 * For any name that is not in the inode reference item from the log tree, do a
1208 * proper unlink of that name (that is, remove its entry from the inode
1209 * reference item and both dir index keys).
1211 static int unlink_old_inode_refs(struct btrfs_trans_handle
*trans
,
1212 struct btrfs_root
*root
,
1213 struct btrfs_path
*path
,
1214 struct btrfs_inode
*inode
,
1215 struct extent_buffer
*log_eb
,
1217 struct btrfs_key
*key
)
1220 unsigned long ref_ptr
;
1221 unsigned long ref_end
;
1222 struct extent_buffer
*eb
;
1225 btrfs_release_path(path
);
1226 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
1234 eb
= path
->nodes
[0];
1235 ref_ptr
= btrfs_item_ptr_offset(eb
, path
->slots
[0]);
1236 ref_end
= ref_ptr
+ btrfs_item_size_nr(eb
, path
->slots
[0]);
1237 while (ref_ptr
< ref_end
) {
1242 if (key
->type
== BTRFS_INODE_EXTREF_KEY
) {
1243 ret
= extref_get_fields(eb
, ref_ptr
, &namelen
, &name
,
1246 parent_id
= key
->offset
;
1247 ret
= ref_get_fields(eb
, ref_ptr
, &namelen
, &name
,
1253 if (key
->type
== BTRFS_INODE_EXTREF_KEY
)
1254 ret
= btrfs_find_name_in_ext_backref(log_eb
, log_slot
,
1258 ret
= btrfs_find_name_in_backref(log_eb
, log_slot
, name
,
1264 btrfs_release_path(path
);
1265 dir
= read_one_inode(root
, parent_id
);
1271 ret
= btrfs_unlink_inode(trans
, root
, BTRFS_I(dir
),
1272 inode
, name
, namelen
);
1282 if (key
->type
== BTRFS_INODE_EXTREF_KEY
)
1283 ref_ptr
+= sizeof(struct btrfs_inode_extref
);
1285 ref_ptr
+= sizeof(struct btrfs_inode_ref
);
1289 btrfs_release_path(path
);
1293 static int btrfs_inode_ref_exists(struct inode
*inode
, struct inode
*dir
,
1294 const u8 ref_type
, const char *name
,
1297 struct btrfs_key key
;
1298 struct btrfs_path
*path
;
1299 const u64 parent_id
= btrfs_ino(BTRFS_I(dir
));
1302 path
= btrfs_alloc_path();
1306 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
1307 key
.type
= ref_type
;
1308 if (key
.type
== BTRFS_INODE_REF_KEY
)
1309 key
.offset
= parent_id
;
1311 key
.offset
= btrfs_extref_hash(parent_id
, name
, namelen
);
1313 ret
= btrfs_search_slot(NULL
, BTRFS_I(inode
)->root
, &key
, path
, 0, 0);
1320 if (key
.type
== BTRFS_INODE_EXTREF_KEY
)
1321 ret
= btrfs_find_name_in_ext_backref(path
->nodes
[0],
1322 path
->slots
[0], parent_id
,
1323 name
, namelen
, NULL
);
1325 ret
= btrfs_find_name_in_backref(path
->nodes
[0], path
->slots
[0],
1326 name
, namelen
, NULL
);
1329 btrfs_free_path(path
);
1334 * replay one inode back reference item found in the log tree.
1335 * eb, slot and key refer to the buffer and key found in the log tree.
1336 * root is the destination we are replaying into, and path is for temp
1337 * use by this function. (it should be released on return).
1339 static noinline
int add_inode_ref(struct btrfs_trans_handle
*trans
,
1340 struct btrfs_root
*root
,
1341 struct btrfs_root
*log
,
1342 struct btrfs_path
*path
,
1343 struct extent_buffer
*eb
, int slot
,
1344 struct btrfs_key
*key
)
1346 struct inode
*dir
= NULL
;
1347 struct inode
*inode
= NULL
;
1348 unsigned long ref_ptr
;
1349 unsigned long ref_end
;
1353 int search_done
= 0;
1354 int log_ref_ver
= 0;
1355 u64 parent_objectid
;
1358 int ref_struct_size
;
1360 ref_ptr
= btrfs_item_ptr_offset(eb
, slot
);
1361 ref_end
= ref_ptr
+ btrfs_item_size_nr(eb
, slot
);
1363 if (key
->type
== BTRFS_INODE_EXTREF_KEY
) {
1364 struct btrfs_inode_extref
*r
;
1366 ref_struct_size
= sizeof(struct btrfs_inode_extref
);
1368 r
= (struct btrfs_inode_extref
*)ref_ptr
;
1369 parent_objectid
= btrfs_inode_extref_parent(eb
, r
);
1371 ref_struct_size
= sizeof(struct btrfs_inode_ref
);
1372 parent_objectid
= key
->offset
;
1374 inode_objectid
= key
->objectid
;
1377 * it is possible that we didn't log all the parent directories
1378 * for a given inode. If we don't find the dir, just don't
1379 * copy the back ref in. The link count fixup code will take
1382 dir
= read_one_inode(root
, parent_objectid
);
1388 inode
= read_one_inode(root
, inode_objectid
);
1394 while (ref_ptr
< ref_end
) {
1396 ret
= extref_get_fields(eb
, ref_ptr
, &namelen
, &name
,
1397 &ref_index
, &parent_objectid
);
1399 * parent object can change from one array
1403 dir
= read_one_inode(root
, parent_objectid
);
1409 ret
= ref_get_fields(eb
, ref_ptr
, &namelen
, &name
,
1415 /* if we already have a perfect match, we're done */
1416 if (!inode_in_dir(root
, path
, btrfs_ino(BTRFS_I(dir
)),
1417 btrfs_ino(BTRFS_I(inode
)), ref_index
,
1420 * look for a conflicting back reference in the
1421 * metadata. if we find one we have to unlink that name
1422 * of the file before we add our new link. Later on, we
1423 * overwrite any existing back reference, and we don't
1424 * want to create dangling pointers in the directory.
1428 ret
= __add_inode_ref(trans
, root
, path
, log
,
1433 ref_index
, name
, namelen
,
1443 * If a reference item already exists for this inode
1444 * with the same parent and name, but different index,
1445 * drop it and the corresponding directory index entries
1446 * from the parent before adding the new reference item
1447 * and dir index entries, otherwise we would fail with
1448 * -EEXIST returned from btrfs_add_link() below.
1450 ret
= btrfs_inode_ref_exists(inode
, dir
, key
->type
,
1453 ret
= btrfs_unlink_inode(trans
, root
,
1458 * If we dropped the link count to 0, bump it so
1459 * that later the iput() on the inode will not
1460 * free it. We will fixup the link count later.
1462 if (!ret
&& inode
->i_nlink
== 0)
1468 /* insert our name */
1469 ret
= btrfs_add_link(trans
, BTRFS_I(dir
),
1471 name
, namelen
, 0, ref_index
);
1475 btrfs_update_inode(trans
, root
, inode
);
1478 ref_ptr
= (unsigned long)(ref_ptr
+ ref_struct_size
) + namelen
;
1488 * Before we overwrite the inode reference item in the subvolume tree
1489 * with the item from the log tree, we must unlink all names from the
1490 * parent directory that are in the subvolume's tree inode reference
1491 * item, otherwise we end up with an inconsistent subvolume tree where
1492 * dir index entries exist for a name but there is no inode reference
1493 * item with the same name.
1495 ret
= unlink_old_inode_refs(trans
, root
, path
, BTRFS_I(inode
), eb
, slot
,
1500 /* finally write the back reference in the inode */
1501 ret
= overwrite_item(trans
, root
, path
, eb
, slot
, key
);
1503 btrfs_release_path(path
);
1510 static int insert_orphan_item(struct btrfs_trans_handle
*trans
,
1511 struct btrfs_root
*root
, u64 ino
)
1515 ret
= btrfs_insert_orphan_item(trans
, root
, ino
);
1522 static int count_inode_extrefs(struct btrfs_root
*root
,
1523 struct btrfs_inode
*inode
, struct btrfs_path
*path
)
1527 unsigned int nlink
= 0;
1530 u64 inode_objectid
= btrfs_ino(inode
);
1533 struct btrfs_inode_extref
*extref
;
1534 struct extent_buffer
*leaf
;
1537 ret
= btrfs_find_one_extref(root
, inode_objectid
, offset
, path
,
1542 leaf
= path
->nodes
[0];
1543 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1544 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
1547 while (cur_offset
< item_size
) {
1548 extref
= (struct btrfs_inode_extref
*) (ptr
+ cur_offset
);
1549 name_len
= btrfs_inode_extref_name_len(leaf
, extref
);
1553 cur_offset
+= name_len
+ sizeof(*extref
);
1557 btrfs_release_path(path
);
1559 btrfs_release_path(path
);
1561 if (ret
< 0 && ret
!= -ENOENT
)
1566 static int count_inode_refs(struct btrfs_root
*root
,
1567 struct btrfs_inode
*inode
, struct btrfs_path
*path
)
1570 struct btrfs_key key
;
1571 unsigned int nlink
= 0;
1573 unsigned long ptr_end
;
1575 u64 ino
= btrfs_ino(inode
);
1578 key
.type
= BTRFS_INODE_REF_KEY
;
1579 key
.offset
= (u64
)-1;
1582 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1586 if (path
->slots
[0] == 0)
1591 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
1593 if (key
.objectid
!= ino
||
1594 key
.type
!= BTRFS_INODE_REF_KEY
)
1596 ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
1597 ptr_end
= ptr
+ btrfs_item_size_nr(path
->nodes
[0],
1599 while (ptr
< ptr_end
) {
1600 struct btrfs_inode_ref
*ref
;
1602 ref
= (struct btrfs_inode_ref
*)ptr
;
1603 name_len
= btrfs_inode_ref_name_len(path
->nodes
[0],
1605 ptr
= (unsigned long)(ref
+ 1) + name_len
;
1609 if (key
.offset
== 0)
1611 if (path
->slots
[0] > 0) {
1616 btrfs_release_path(path
);
1618 btrfs_release_path(path
);
1624 * There are a few corners where the link count of the file can't
1625 * be properly maintained during replay. So, instead of adding
1626 * lots of complexity to the log code, we just scan the backrefs
1627 * for any file that has been through replay.
1629 * The scan will update the link count on the inode to reflect the
1630 * number of back refs found. If it goes down to zero, the iput
1631 * will free the inode.
1633 static noinline
int fixup_inode_link_count(struct btrfs_trans_handle
*trans
,
1634 struct btrfs_root
*root
,
1635 struct inode
*inode
)
1637 struct btrfs_path
*path
;
1640 u64 ino
= btrfs_ino(BTRFS_I(inode
));
1642 path
= btrfs_alloc_path();
1646 ret
= count_inode_refs(root
, BTRFS_I(inode
), path
);
1652 ret
= count_inode_extrefs(root
, BTRFS_I(inode
), path
);
1660 if (nlink
!= inode
->i_nlink
) {
1661 set_nlink(inode
, nlink
);
1662 btrfs_update_inode(trans
, root
, inode
);
1664 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1666 if (inode
->i_nlink
== 0) {
1667 if (S_ISDIR(inode
->i_mode
)) {
1668 ret
= replay_dir_deletes(trans
, root
, NULL
, path
,
1673 ret
= insert_orphan_item(trans
, root
, ino
);
1677 btrfs_free_path(path
);
1681 static noinline
int fixup_inode_link_counts(struct btrfs_trans_handle
*trans
,
1682 struct btrfs_root
*root
,
1683 struct btrfs_path
*path
)
1686 struct btrfs_key key
;
1687 struct inode
*inode
;
1689 key
.objectid
= BTRFS_TREE_LOG_FIXUP_OBJECTID
;
1690 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
1691 key
.offset
= (u64
)-1;
1693 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1698 if (path
->slots
[0] == 0)
1703 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1704 if (key
.objectid
!= BTRFS_TREE_LOG_FIXUP_OBJECTID
||
1705 key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
1708 ret
= btrfs_del_item(trans
, root
, path
);
1712 btrfs_release_path(path
);
1713 inode
= read_one_inode(root
, key
.offset
);
1717 ret
= fixup_inode_link_count(trans
, root
, inode
);
1723 * fixup on a directory may create new entries,
1724 * make sure we always look for the highset possible
1727 key
.offset
= (u64
)-1;
1731 btrfs_release_path(path
);
1737 * record a given inode in the fixup dir so we can check its link
1738 * count when replay is done. The link count is incremented here
1739 * so the inode won't go away until we check it
1741 static noinline
int link_to_fixup_dir(struct btrfs_trans_handle
*trans
,
1742 struct btrfs_root
*root
,
1743 struct btrfs_path
*path
,
1746 struct btrfs_key key
;
1748 struct inode
*inode
;
1750 inode
= read_one_inode(root
, objectid
);
1754 key
.objectid
= BTRFS_TREE_LOG_FIXUP_OBJECTID
;
1755 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
1756 key
.offset
= objectid
;
1758 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1760 btrfs_release_path(path
);
1762 if (!inode
->i_nlink
)
1763 set_nlink(inode
, 1);
1766 ret
= btrfs_update_inode(trans
, root
, inode
);
1767 } else if (ret
== -EEXIST
) {
1770 BUG(); /* Logic Error */
1778 * when replaying the log for a directory, we only insert names
1779 * for inodes that actually exist. This means an fsync on a directory
1780 * does not implicitly fsync all the new files in it
1782 static noinline
int insert_one_name(struct btrfs_trans_handle
*trans
,
1783 struct btrfs_root
*root
,
1784 u64 dirid
, u64 index
,
1785 char *name
, int name_len
,
1786 struct btrfs_key
*location
)
1788 struct inode
*inode
;
1792 inode
= read_one_inode(root
, location
->objectid
);
1796 dir
= read_one_inode(root
, dirid
);
1802 ret
= btrfs_add_link(trans
, BTRFS_I(dir
), BTRFS_I(inode
), name
,
1803 name_len
, 1, index
);
1805 /* FIXME, put inode into FIXUP list */
1813 * Return true if an inode reference exists in the log for the given name,
1814 * inode and parent inode.
1816 static bool name_in_log_ref(struct btrfs_root
*log_root
,
1817 const char *name
, const int name_len
,
1818 const u64 dirid
, const u64 ino
)
1820 struct btrfs_key search_key
;
1822 search_key
.objectid
= ino
;
1823 search_key
.type
= BTRFS_INODE_REF_KEY
;
1824 search_key
.offset
= dirid
;
1825 if (backref_in_log(log_root
, &search_key
, dirid
, name
, name_len
))
1828 search_key
.type
= BTRFS_INODE_EXTREF_KEY
;
1829 search_key
.offset
= btrfs_extref_hash(dirid
, name
, name_len
);
1830 if (backref_in_log(log_root
, &search_key
, dirid
, name
, name_len
))
1837 * take a single entry in a log directory item and replay it into
1840 * if a conflicting item exists in the subdirectory already,
1841 * the inode it points to is unlinked and put into the link count
1844 * If a name from the log points to a file or directory that does
1845 * not exist in the FS, it is skipped. fsyncs on directories
1846 * do not force down inodes inside that directory, just changes to the
1847 * names or unlinks in a directory.
1849 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1850 * non-existing inode) and 1 if the name was replayed.
1852 static noinline
int replay_one_name(struct btrfs_trans_handle
*trans
,
1853 struct btrfs_root
*root
,
1854 struct btrfs_path
*path
,
1855 struct extent_buffer
*eb
,
1856 struct btrfs_dir_item
*di
,
1857 struct btrfs_key
*key
)
1861 struct btrfs_dir_item
*dst_di
;
1862 struct btrfs_key found_key
;
1863 struct btrfs_key log_key
;
1868 bool update_size
= (key
->type
== BTRFS_DIR_INDEX_KEY
);
1869 bool name_added
= false;
1871 dir
= read_one_inode(root
, key
->objectid
);
1875 name_len
= btrfs_dir_name_len(eb
, di
);
1876 name
= kmalloc(name_len
, GFP_NOFS
);
1882 log_type
= btrfs_dir_type(eb
, di
);
1883 read_extent_buffer(eb
, name
, (unsigned long)(di
+ 1),
1886 btrfs_dir_item_key_to_cpu(eb
, di
, &log_key
);
1887 exists
= btrfs_lookup_inode(trans
, root
, path
, &log_key
, 0);
1892 btrfs_release_path(path
);
1894 if (key
->type
== BTRFS_DIR_ITEM_KEY
) {
1895 dst_di
= btrfs_lookup_dir_item(trans
, root
, path
, key
->objectid
,
1897 } else if (key
->type
== BTRFS_DIR_INDEX_KEY
) {
1898 dst_di
= btrfs_lookup_dir_index_item(trans
, root
, path
,
1907 if (IS_ERR_OR_NULL(dst_di
)) {
1908 /* we need a sequence number to insert, so we only
1909 * do inserts for the BTRFS_DIR_INDEX_KEY types
1911 if (key
->type
!= BTRFS_DIR_INDEX_KEY
)
1916 btrfs_dir_item_key_to_cpu(path
->nodes
[0], dst_di
, &found_key
);
1917 /* the existing item matches the logged item */
1918 if (found_key
.objectid
== log_key
.objectid
&&
1919 found_key
.type
== log_key
.type
&&
1920 found_key
.offset
== log_key
.offset
&&
1921 btrfs_dir_type(path
->nodes
[0], dst_di
) == log_type
) {
1922 update_size
= false;
1927 * don't drop the conflicting directory entry if the inode
1928 * for the new entry doesn't exist
1933 ret
= drop_one_dir_item(trans
, root
, path
, BTRFS_I(dir
), dst_di
);
1937 if (key
->type
== BTRFS_DIR_INDEX_KEY
)
1940 btrfs_release_path(path
);
1941 if (!ret
&& update_size
) {
1942 btrfs_i_size_write(BTRFS_I(dir
), dir
->i_size
+ name_len
* 2);
1943 ret
= btrfs_update_inode(trans
, root
, dir
);
1947 if (!ret
&& name_added
)
1952 if (name_in_log_ref(root
->log_root
, name
, name_len
,
1953 key
->objectid
, log_key
.objectid
)) {
1954 /* The dentry will be added later. */
1956 update_size
= false;
1959 btrfs_release_path(path
);
1960 ret
= insert_one_name(trans
, root
, key
->objectid
, key
->offset
,
1961 name
, name_len
, &log_key
);
1962 if (ret
&& ret
!= -ENOENT
&& ret
!= -EEXIST
)
1966 update_size
= false;
1972 * find all the names in a directory item and reconcile them into
1973 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1974 * one name in a directory item, but the same code gets used for
1975 * both directory index types
1977 static noinline
int replay_one_dir_item(struct btrfs_trans_handle
*trans
,
1978 struct btrfs_root
*root
,
1979 struct btrfs_path
*path
,
1980 struct extent_buffer
*eb
, int slot
,
1981 struct btrfs_key
*key
)
1984 u32 item_size
= btrfs_item_size_nr(eb
, slot
);
1985 struct btrfs_dir_item
*di
;
1988 unsigned long ptr_end
;
1989 struct btrfs_path
*fixup_path
= NULL
;
1991 ptr
= btrfs_item_ptr_offset(eb
, slot
);
1992 ptr_end
= ptr
+ item_size
;
1993 while (ptr
< ptr_end
) {
1994 di
= (struct btrfs_dir_item
*)ptr
;
1995 name_len
= btrfs_dir_name_len(eb
, di
);
1996 ret
= replay_one_name(trans
, root
, path
, eb
, di
, key
);
1999 ptr
= (unsigned long)(di
+ 1);
2003 * If this entry refers to a non-directory (directories can not
2004 * have a link count > 1) and it was added in the transaction
2005 * that was not committed, make sure we fixup the link count of
2006 * the inode it the entry points to. Otherwise something like
2007 * the following would result in a directory pointing to an
2008 * inode with a wrong link that does not account for this dir
2016 * ln testdir/bar testdir/bar_link
2017 * ln testdir/foo testdir/foo_link
2018 * xfs_io -c "fsync" testdir/bar
2022 * mount fs, log replay happens
2024 * File foo would remain with a link count of 1 when it has two
2025 * entries pointing to it in the directory testdir. This would
2026 * make it impossible to ever delete the parent directory has
2027 * it would result in stale dentries that can never be deleted.
2029 if (ret
== 1 && btrfs_dir_type(eb
, di
) != BTRFS_FT_DIR
) {
2030 struct btrfs_key di_key
;
2033 fixup_path
= btrfs_alloc_path();
2040 btrfs_dir_item_key_to_cpu(eb
, di
, &di_key
);
2041 ret
= link_to_fixup_dir(trans
, root
, fixup_path
,
2048 btrfs_free_path(fixup_path
);
2053 * directory replay has two parts. There are the standard directory
2054 * items in the log copied from the subvolume, and range items
2055 * created in the log while the subvolume was logged.
2057 * The range items tell us which parts of the key space the log
2058 * is authoritative for. During replay, if a key in the subvolume
2059 * directory is in a logged range item, but not actually in the log
2060 * that means it was deleted from the directory before the fsync
2061 * and should be removed.
2063 static noinline
int find_dir_range(struct btrfs_root
*root
,
2064 struct btrfs_path
*path
,
2065 u64 dirid
, int key_type
,
2066 u64
*start_ret
, u64
*end_ret
)
2068 struct btrfs_key key
;
2070 struct btrfs_dir_log_item
*item
;
2074 if (*start_ret
== (u64
)-1)
2077 key
.objectid
= dirid
;
2078 key
.type
= key_type
;
2079 key
.offset
= *start_ret
;
2081 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2085 if (path
->slots
[0] == 0)
2090 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
2092 if (key
.type
!= key_type
|| key
.objectid
!= dirid
) {
2096 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2097 struct btrfs_dir_log_item
);
2098 found_end
= btrfs_dir_log_end(path
->nodes
[0], item
);
2100 if (*start_ret
>= key
.offset
&& *start_ret
<= found_end
) {
2102 *start_ret
= key
.offset
;
2103 *end_ret
= found_end
;
2108 /* check the next slot in the tree to see if it is a valid item */
2109 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2111 if (path
->slots
[0] >= nritems
) {
2112 ret
= btrfs_next_leaf(root
, path
);
2117 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
2119 if (key
.type
!= key_type
|| key
.objectid
!= dirid
) {
2123 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2124 struct btrfs_dir_log_item
);
2125 found_end
= btrfs_dir_log_end(path
->nodes
[0], item
);
2126 *start_ret
= key
.offset
;
2127 *end_ret
= found_end
;
2130 btrfs_release_path(path
);
2135 * this looks for a given directory item in the log. If the directory
2136 * item is not in the log, the item is removed and the inode it points
2139 static noinline
int check_item_in_log(struct btrfs_trans_handle
*trans
,
2140 struct btrfs_root
*root
,
2141 struct btrfs_root
*log
,
2142 struct btrfs_path
*path
,
2143 struct btrfs_path
*log_path
,
2145 struct btrfs_key
*dir_key
)
2148 struct extent_buffer
*eb
;
2151 struct btrfs_dir_item
*di
;
2152 struct btrfs_dir_item
*log_di
;
2155 unsigned long ptr_end
;
2157 struct inode
*inode
;
2158 struct btrfs_key location
;
2161 eb
= path
->nodes
[0];
2162 slot
= path
->slots
[0];
2163 item_size
= btrfs_item_size_nr(eb
, slot
);
2164 ptr
= btrfs_item_ptr_offset(eb
, slot
);
2165 ptr_end
= ptr
+ item_size
;
2166 while (ptr
< ptr_end
) {
2167 di
= (struct btrfs_dir_item
*)ptr
;
2168 name_len
= btrfs_dir_name_len(eb
, di
);
2169 name
= kmalloc(name_len
, GFP_NOFS
);
2174 read_extent_buffer(eb
, name
, (unsigned long)(di
+ 1),
2177 if (log
&& dir_key
->type
== BTRFS_DIR_ITEM_KEY
) {
2178 log_di
= btrfs_lookup_dir_item(trans
, log
, log_path
,
2181 } else if (log
&& dir_key
->type
== BTRFS_DIR_INDEX_KEY
) {
2182 log_di
= btrfs_lookup_dir_index_item(trans
, log
,
2188 if (!log_di
|| log_di
== ERR_PTR(-ENOENT
)) {
2189 btrfs_dir_item_key_to_cpu(eb
, di
, &location
);
2190 btrfs_release_path(path
);
2191 btrfs_release_path(log_path
);
2192 inode
= read_one_inode(root
, location
.objectid
);
2198 ret
= link_to_fixup_dir(trans
, root
,
2199 path
, location
.objectid
);
2207 ret
= btrfs_unlink_inode(trans
, root
, BTRFS_I(dir
),
2208 BTRFS_I(inode
), name
, name_len
);
2210 ret
= btrfs_run_delayed_items(trans
);
2216 /* there might still be more names under this key
2217 * check and repeat if required
2219 ret
= btrfs_search_slot(NULL
, root
, dir_key
, path
,
2225 } else if (IS_ERR(log_di
)) {
2227 return PTR_ERR(log_di
);
2229 btrfs_release_path(log_path
);
2232 ptr
= (unsigned long)(di
+ 1);
2237 btrfs_release_path(path
);
2238 btrfs_release_path(log_path
);
2242 static int replay_xattr_deletes(struct btrfs_trans_handle
*trans
,
2243 struct btrfs_root
*root
,
2244 struct btrfs_root
*log
,
2245 struct btrfs_path
*path
,
2248 struct btrfs_key search_key
;
2249 struct btrfs_path
*log_path
;
2254 log_path
= btrfs_alloc_path();
2258 search_key
.objectid
= ino
;
2259 search_key
.type
= BTRFS_XATTR_ITEM_KEY
;
2260 search_key
.offset
= 0;
2262 ret
= btrfs_search_slot(NULL
, root
, &search_key
, path
, 0, 0);
2266 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2267 for (i
= path
->slots
[0]; i
< nritems
; i
++) {
2268 struct btrfs_key key
;
2269 struct btrfs_dir_item
*di
;
2270 struct btrfs_dir_item
*log_di
;
2274 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, i
);
2275 if (key
.objectid
!= ino
|| key
.type
!= BTRFS_XATTR_ITEM_KEY
) {
2280 di
= btrfs_item_ptr(path
->nodes
[0], i
, struct btrfs_dir_item
);
2281 total_size
= btrfs_item_size_nr(path
->nodes
[0], i
);
2283 while (cur
< total_size
) {
2284 u16 name_len
= btrfs_dir_name_len(path
->nodes
[0], di
);
2285 u16 data_len
= btrfs_dir_data_len(path
->nodes
[0], di
);
2286 u32 this_len
= sizeof(*di
) + name_len
+ data_len
;
2289 name
= kmalloc(name_len
, GFP_NOFS
);
2294 read_extent_buffer(path
->nodes
[0], name
,
2295 (unsigned long)(di
+ 1), name_len
);
2297 log_di
= btrfs_lookup_xattr(NULL
, log
, log_path
, ino
,
2299 btrfs_release_path(log_path
);
2301 /* Doesn't exist in log tree, so delete it. */
2302 btrfs_release_path(path
);
2303 di
= btrfs_lookup_xattr(trans
, root
, path
, ino
,
2304 name
, name_len
, -1);
2311 ret
= btrfs_delete_one_dir_name(trans
, root
,
2315 btrfs_release_path(path
);
2320 if (IS_ERR(log_di
)) {
2321 ret
= PTR_ERR(log_di
);
2325 di
= (struct btrfs_dir_item
*)((char *)di
+ this_len
);
2328 ret
= btrfs_next_leaf(root
, path
);
2334 btrfs_free_path(log_path
);
2335 btrfs_release_path(path
);
2341 * deletion replay happens before we copy any new directory items
2342 * out of the log or out of backreferences from inodes. It
2343 * scans the log to find ranges of keys that log is authoritative for,
2344 * and then scans the directory to find items in those ranges that are
2345 * not present in the log.
2347 * Anything we don't find in the log is unlinked and removed from the
2350 static noinline
int replay_dir_deletes(struct btrfs_trans_handle
*trans
,
2351 struct btrfs_root
*root
,
2352 struct btrfs_root
*log
,
2353 struct btrfs_path
*path
,
2354 u64 dirid
, int del_all
)
2358 int key_type
= BTRFS_DIR_LOG_ITEM_KEY
;
2360 struct btrfs_key dir_key
;
2361 struct btrfs_key found_key
;
2362 struct btrfs_path
*log_path
;
2365 dir_key
.objectid
= dirid
;
2366 dir_key
.type
= BTRFS_DIR_ITEM_KEY
;
2367 log_path
= btrfs_alloc_path();
2371 dir
= read_one_inode(root
, dirid
);
2372 /* it isn't an error if the inode isn't there, that can happen
2373 * because we replay the deletes before we copy in the inode item
2377 btrfs_free_path(log_path
);
2385 range_end
= (u64
)-1;
2387 ret
= find_dir_range(log
, path
, dirid
, key_type
,
2388 &range_start
, &range_end
);
2393 dir_key
.offset
= range_start
;
2396 ret
= btrfs_search_slot(NULL
, root
, &dir_key
, path
,
2401 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2402 if (path
->slots
[0] >= nritems
) {
2403 ret
= btrfs_next_leaf(root
, path
);
2409 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2411 if (found_key
.objectid
!= dirid
||
2412 found_key
.type
!= dir_key
.type
)
2415 if (found_key
.offset
> range_end
)
2418 ret
= check_item_in_log(trans
, root
, log
, path
,
2423 if (found_key
.offset
== (u64
)-1)
2425 dir_key
.offset
= found_key
.offset
+ 1;
2427 btrfs_release_path(path
);
2428 if (range_end
== (u64
)-1)
2430 range_start
= range_end
+ 1;
2435 if (key_type
== BTRFS_DIR_LOG_ITEM_KEY
) {
2436 key_type
= BTRFS_DIR_LOG_INDEX_KEY
;
2437 dir_key
.type
= BTRFS_DIR_INDEX_KEY
;
2438 btrfs_release_path(path
);
2442 btrfs_release_path(path
);
2443 btrfs_free_path(log_path
);
2449 * the process_func used to replay items from the log tree. This
2450 * gets called in two different stages. The first stage just looks
2451 * for inodes and makes sure they are all copied into the subvolume.
2453 * The second stage copies all the other item types from the log into
2454 * the subvolume. The two stage approach is slower, but gets rid of
2455 * lots of complexity around inodes referencing other inodes that exist
2456 * only in the log (references come from either directory items or inode
2459 static int replay_one_buffer(struct btrfs_root
*log
, struct extent_buffer
*eb
,
2460 struct walk_control
*wc
, u64 gen
, int level
)
2463 struct btrfs_path
*path
;
2464 struct btrfs_root
*root
= wc
->replay_dest
;
2465 struct btrfs_key key
;
2469 ret
= btrfs_read_buffer(eb
, gen
, level
, NULL
);
2473 level
= btrfs_header_level(eb
);
2478 path
= btrfs_alloc_path();
2482 nritems
= btrfs_header_nritems(eb
);
2483 for (i
= 0; i
< nritems
; i
++) {
2484 btrfs_item_key_to_cpu(eb
, &key
, i
);
2486 /* inode keys are done during the first stage */
2487 if (key
.type
== BTRFS_INODE_ITEM_KEY
&&
2488 wc
->stage
== LOG_WALK_REPLAY_INODES
) {
2489 struct btrfs_inode_item
*inode_item
;
2492 inode_item
= btrfs_item_ptr(eb
, i
,
2493 struct btrfs_inode_item
);
2495 * If we have a tmpfile (O_TMPFILE) that got fsync'ed
2496 * and never got linked before the fsync, skip it, as
2497 * replaying it is pointless since it would be deleted
2498 * later. We skip logging tmpfiles, but it's always
2499 * possible we are replaying a log created with a kernel
2500 * that used to log tmpfiles.
2502 if (btrfs_inode_nlink(eb
, inode_item
) == 0) {
2503 wc
->ignore_cur_inode
= true;
2506 wc
->ignore_cur_inode
= false;
2508 ret
= replay_xattr_deletes(wc
->trans
, root
, log
,
2509 path
, key
.objectid
);
2512 mode
= btrfs_inode_mode(eb
, inode_item
);
2513 if (S_ISDIR(mode
)) {
2514 ret
= replay_dir_deletes(wc
->trans
,
2515 root
, log
, path
, key
.objectid
, 0);
2519 ret
= overwrite_item(wc
->trans
, root
, path
,
2525 * Before replaying extents, truncate the inode to its
2526 * size. We need to do it now and not after log replay
2527 * because before an fsync we can have prealloc extents
2528 * added beyond the inode's i_size. If we did it after,
2529 * through orphan cleanup for example, we would drop
2530 * those prealloc extents just after replaying them.
2532 if (S_ISREG(mode
)) {
2533 struct inode
*inode
;
2536 inode
= read_one_inode(root
, key
.objectid
);
2541 from
= ALIGN(i_size_read(inode
),
2542 root
->fs_info
->sectorsize
);
2543 ret
= btrfs_drop_extents(wc
->trans
, root
, inode
,
2546 /* Update the inode's nbytes. */
2547 ret
= btrfs_update_inode(wc
->trans
,
2555 ret
= link_to_fixup_dir(wc
->trans
, root
,
2556 path
, key
.objectid
);
2561 if (wc
->ignore_cur_inode
)
2564 if (key
.type
== BTRFS_DIR_INDEX_KEY
&&
2565 wc
->stage
== LOG_WALK_REPLAY_DIR_INDEX
) {
2566 ret
= replay_one_dir_item(wc
->trans
, root
, path
,
2572 if (wc
->stage
< LOG_WALK_REPLAY_ALL
)
2575 /* these keys are simply copied */
2576 if (key
.type
== BTRFS_XATTR_ITEM_KEY
) {
2577 ret
= overwrite_item(wc
->trans
, root
, path
,
2581 } else if (key
.type
== BTRFS_INODE_REF_KEY
||
2582 key
.type
== BTRFS_INODE_EXTREF_KEY
) {
2583 ret
= add_inode_ref(wc
->trans
, root
, log
, path
,
2585 if (ret
&& ret
!= -ENOENT
)
2588 } else if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
2589 ret
= replay_one_extent(wc
->trans
, root
, path
,
2593 } else if (key
.type
== BTRFS_DIR_ITEM_KEY
) {
2594 ret
= replay_one_dir_item(wc
->trans
, root
, path
,
2600 btrfs_free_path(path
);
2604 static noinline
int walk_down_log_tree(struct btrfs_trans_handle
*trans
,
2605 struct btrfs_root
*root
,
2606 struct btrfs_path
*path
, int *level
,
2607 struct walk_control
*wc
)
2609 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2613 struct extent_buffer
*next
;
2614 struct extent_buffer
*cur
;
2615 struct extent_buffer
*parent
;
2619 WARN_ON(*level
< 0);
2620 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2622 while (*level
> 0) {
2623 struct btrfs_key first_key
;
2625 WARN_ON(*level
< 0);
2626 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2627 cur
= path
->nodes
[*level
];
2629 WARN_ON(btrfs_header_level(cur
) != *level
);
2631 if (path
->slots
[*level
] >=
2632 btrfs_header_nritems(cur
))
2635 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
2636 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
2637 btrfs_node_key_to_cpu(cur
, &first_key
, path
->slots
[*level
]);
2638 blocksize
= fs_info
->nodesize
;
2640 parent
= path
->nodes
[*level
];
2641 root_owner
= btrfs_header_owner(parent
);
2643 next
= btrfs_find_create_tree_block(fs_info
, bytenr
);
2645 return PTR_ERR(next
);
2648 ret
= wc
->process_func(root
, next
, wc
, ptr_gen
,
2651 free_extent_buffer(next
);
2655 path
->slots
[*level
]++;
2657 ret
= btrfs_read_buffer(next
, ptr_gen
,
2658 *level
- 1, &first_key
);
2660 free_extent_buffer(next
);
2665 btrfs_tree_lock(next
);
2666 btrfs_set_lock_blocking(next
);
2667 clean_tree_block(fs_info
, next
);
2668 btrfs_wait_tree_block_writeback(next
);
2669 btrfs_tree_unlock(next
);
2671 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY
, &next
->bflags
))
2672 clear_extent_buffer_dirty(next
);
2675 WARN_ON(root_owner
!=
2676 BTRFS_TREE_LOG_OBJECTID
);
2677 ret
= btrfs_free_and_pin_reserved_extent(
2681 free_extent_buffer(next
);
2685 free_extent_buffer(next
);
2688 ret
= btrfs_read_buffer(next
, ptr_gen
, *level
- 1, &first_key
);
2690 free_extent_buffer(next
);
2694 WARN_ON(*level
<= 0);
2695 if (path
->nodes
[*level
-1])
2696 free_extent_buffer(path
->nodes
[*level
-1]);
2697 path
->nodes
[*level
-1] = next
;
2698 *level
= btrfs_header_level(next
);
2699 path
->slots
[*level
] = 0;
2702 WARN_ON(*level
< 0);
2703 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2705 path
->slots
[*level
] = btrfs_header_nritems(path
->nodes
[*level
]);
2711 static noinline
int walk_up_log_tree(struct btrfs_trans_handle
*trans
,
2712 struct btrfs_root
*root
,
2713 struct btrfs_path
*path
, int *level
,
2714 struct walk_control
*wc
)
2716 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2722 for (i
= *level
; i
< BTRFS_MAX_LEVEL
- 1 && path
->nodes
[i
]; i
++) {
2723 slot
= path
->slots
[i
];
2724 if (slot
+ 1 < btrfs_header_nritems(path
->nodes
[i
])) {
2727 WARN_ON(*level
== 0);
2730 struct extent_buffer
*parent
;
2731 if (path
->nodes
[*level
] == root
->node
)
2732 parent
= path
->nodes
[*level
];
2734 parent
= path
->nodes
[*level
+ 1];
2736 root_owner
= btrfs_header_owner(parent
);
2737 ret
= wc
->process_func(root
, path
->nodes
[*level
], wc
,
2738 btrfs_header_generation(path
->nodes
[*level
]),
2744 struct extent_buffer
*next
;
2746 next
= path
->nodes
[*level
];
2749 btrfs_tree_lock(next
);
2750 btrfs_set_lock_blocking(next
);
2751 clean_tree_block(fs_info
, next
);
2752 btrfs_wait_tree_block_writeback(next
);
2753 btrfs_tree_unlock(next
);
2755 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY
, &next
->bflags
))
2756 clear_extent_buffer_dirty(next
);
2759 WARN_ON(root_owner
!= BTRFS_TREE_LOG_OBJECTID
);
2760 ret
= btrfs_free_and_pin_reserved_extent(
2762 path
->nodes
[*level
]->start
,
2763 path
->nodes
[*level
]->len
);
2767 free_extent_buffer(path
->nodes
[*level
]);
2768 path
->nodes
[*level
] = NULL
;
2776 * drop the reference count on the tree rooted at 'snap'. This traverses
2777 * the tree freeing any blocks that have a ref count of zero after being
2780 static int walk_log_tree(struct btrfs_trans_handle
*trans
,
2781 struct btrfs_root
*log
, struct walk_control
*wc
)
2783 struct btrfs_fs_info
*fs_info
= log
->fs_info
;
2787 struct btrfs_path
*path
;
2790 path
= btrfs_alloc_path();
2794 level
= btrfs_header_level(log
->node
);
2796 path
->nodes
[level
] = log
->node
;
2797 extent_buffer_get(log
->node
);
2798 path
->slots
[level
] = 0;
2801 wret
= walk_down_log_tree(trans
, log
, path
, &level
, wc
);
2809 wret
= walk_up_log_tree(trans
, log
, path
, &level
, wc
);
2818 /* was the root node processed? if not, catch it here */
2819 if (path
->nodes
[orig_level
]) {
2820 ret
= wc
->process_func(log
, path
->nodes
[orig_level
], wc
,
2821 btrfs_header_generation(path
->nodes
[orig_level
]),
2826 struct extent_buffer
*next
;
2828 next
= path
->nodes
[orig_level
];
2831 btrfs_tree_lock(next
);
2832 btrfs_set_lock_blocking(next
);
2833 clean_tree_block(fs_info
, next
);
2834 btrfs_wait_tree_block_writeback(next
);
2835 btrfs_tree_unlock(next
);
2837 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY
, &next
->bflags
))
2838 clear_extent_buffer_dirty(next
);
2841 WARN_ON(log
->root_key
.objectid
!=
2842 BTRFS_TREE_LOG_OBJECTID
);
2843 ret
= btrfs_free_and_pin_reserved_extent(fs_info
,
2844 next
->start
, next
->len
);
2851 btrfs_free_path(path
);
2856 * helper function to update the item for a given subvolumes log root
2857 * in the tree of log roots
2859 static int update_log_root(struct btrfs_trans_handle
*trans
,
2860 struct btrfs_root
*log
)
2862 struct btrfs_fs_info
*fs_info
= log
->fs_info
;
2865 if (log
->log_transid
== 1) {
2866 /* insert root item on the first sync */
2867 ret
= btrfs_insert_root(trans
, fs_info
->log_root_tree
,
2868 &log
->root_key
, &log
->root_item
);
2870 ret
= btrfs_update_root(trans
, fs_info
->log_root_tree
,
2871 &log
->root_key
, &log
->root_item
);
2876 static void wait_log_commit(struct btrfs_root
*root
, int transid
)
2879 int index
= transid
% 2;
2882 * we only allow two pending log transactions at a time,
2883 * so we know that if ours is more than 2 older than the
2884 * current transaction, we're done
2887 prepare_to_wait(&root
->log_commit_wait
[index
],
2888 &wait
, TASK_UNINTERRUPTIBLE
);
2890 if (!(root
->log_transid_committed
< transid
&&
2891 atomic_read(&root
->log_commit
[index
])))
2894 mutex_unlock(&root
->log_mutex
);
2896 mutex_lock(&root
->log_mutex
);
2898 finish_wait(&root
->log_commit_wait
[index
], &wait
);
2901 static void wait_for_writer(struct btrfs_root
*root
)
2906 prepare_to_wait(&root
->log_writer_wait
, &wait
,
2907 TASK_UNINTERRUPTIBLE
);
2908 if (!atomic_read(&root
->log_writers
))
2911 mutex_unlock(&root
->log_mutex
);
2913 mutex_lock(&root
->log_mutex
);
2915 finish_wait(&root
->log_writer_wait
, &wait
);
2918 static inline void btrfs_remove_log_ctx(struct btrfs_root
*root
,
2919 struct btrfs_log_ctx
*ctx
)
2924 mutex_lock(&root
->log_mutex
);
2925 list_del_init(&ctx
->list
);
2926 mutex_unlock(&root
->log_mutex
);
2930 * Invoked in log mutex context, or be sure there is no other task which
2931 * can access the list.
2933 static inline void btrfs_remove_all_log_ctxs(struct btrfs_root
*root
,
2934 int index
, int error
)
2936 struct btrfs_log_ctx
*ctx
;
2937 struct btrfs_log_ctx
*safe
;
2939 list_for_each_entry_safe(ctx
, safe
, &root
->log_ctxs
[index
], list
) {
2940 list_del_init(&ctx
->list
);
2941 ctx
->log_ret
= error
;
2944 INIT_LIST_HEAD(&root
->log_ctxs
[index
]);
2948 * btrfs_sync_log does sends a given tree log down to the disk and
2949 * updates the super blocks to record it. When this call is done,
2950 * you know that any inodes previously logged are safely on disk only
2953 * Any other return value means you need to call btrfs_commit_transaction.
2954 * Some of the edge cases for fsyncing directories that have had unlinks
2955 * or renames done in the past mean that sometimes the only safe
2956 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2957 * that has happened.
2959 int btrfs_sync_log(struct btrfs_trans_handle
*trans
,
2960 struct btrfs_root
*root
, struct btrfs_log_ctx
*ctx
)
2966 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2967 struct btrfs_root
*log
= root
->log_root
;
2968 struct btrfs_root
*log_root_tree
= fs_info
->log_root_tree
;
2969 int log_transid
= 0;
2970 struct btrfs_log_ctx root_log_ctx
;
2971 struct blk_plug plug
;
2973 mutex_lock(&root
->log_mutex
);
2974 log_transid
= ctx
->log_transid
;
2975 if (root
->log_transid_committed
>= log_transid
) {
2976 mutex_unlock(&root
->log_mutex
);
2977 return ctx
->log_ret
;
2980 index1
= log_transid
% 2;
2981 if (atomic_read(&root
->log_commit
[index1
])) {
2982 wait_log_commit(root
, log_transid
);
2983 mutex_unlock(&root
->log_mutex
);
2984 return ctx
->log_ret
;
2986 ASSERT(log_transid
== root
->log_transid
);
2987 atomic_set(&root
->log_commit
[index1
], 1);
2989 /* wait for previous tree log sync to complete */
2990 if (atomic_read(&root
->log_commit
[(index1
+ 1) % 2]))
2991 wait_log_commit(root
, log_transid
- 1);
2994 int batch
= atomic_read(&root
->log_batch
);
2995 /* when we're on an ssd, just kick the log commit out */
2996 if (!btrfs_test_opt(fs_info
, SSD
) &&
2997 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS
, &root
->state
)) {
2998 mutex_unlock(&root
->log_mutex
);
2999 schedule_timeout_uninterruptible(1);
3000 mutex_lock(&root
->log_mutex
);
3002 wait_for_writer(root
);
3003 if (batch
== atomic_read(&root
->log_batch
))
3007 /* bail out if we need to do a full commit */
3008 if (btrfs_need_log_full_commit(fs_info
, trans
)) {
3010 mutex_unlock(&root
->log_mutex
);
3014 if (log_transid
% 2 == 0)
3015 mark
= EXTENT_DIRTY
;
3019 /* we start IO on all the marked extents here, but we don't actually
3020 * wait for them until later.
3022 blk_start_plug(&plug
);
3023 ret
= btrfs_write_marked_extents(fs_info
, &log
->dirty_log_pages
, mark
);
3025 blk_finish_plug(&plug
);
3026 btrfs_abort_transaction(trans
, ret
);
3027 btrfs_set_log_full_commit(fs_info
, trans
);
3028 mutex_unlock(&root
->log_mutex
);
3032 btrfs_set_root_node(&log
->root_item
, log
->node
);
3034 root
->log_transid
++;
3035 log
->log_transid
= root
->log_transid
;
3036 root
->log_start_pid
= 0;
3038 * IO has been started, blocks of the log tree have WRITTEN flag set
3039 * in their headers. new modifications of the log will be written to
3040 * new positions. so it's safe to allow log writers to go in.
3042 mutex_unlock(&root
->log_mutex
);
3044 btrfs_init_log_ctx(&root_log_ctx
, NULL
);
3046 mutex_lock(&log_root_tree
->log_mutex
);
3047 atomic_inc(&log_root_tree
->log_batch
);
3048 atomic_inc(&log_root_tree
->log_writers
);
3050 index2
= log_root_tree
->log_transid
% 2;
3051 list_add_tail(&root_log_ctx
.list
, &log_root_tree
->log_ctxs
[index2
]);
3052 root_log_ctx
.log_transid
= log_root_tree
->log_transid
;
3054 mutex_unlock(&log_root_tree
->log_mutex
);
3056 ret
= update_log_root(trans
, log
);
3058 mutex_lock(&log_root_tree
->log_mutex
);
3059 if (atomic_dec_and_test(&log_root_tree
->log_writers
)) {
3060 /* atomic_dec_and_test implies a barrier */
3061 cond_wake_up_nomb(&log_root_tree
->log_writer_wait
);
3065 if (!list_empty(&root_log_ctx
.list
))
3066 list_del_init(&root_log_ctx
.list
);
3068 blk_finish_plug(&plug
);
3069 btrfs_set_log_full_commit(fs_info
, trans
);
3071 if (ret
!= -ENOSPC
) {
3072 btrfs_abort_transaction(trans
, ret
);
3073 mutex_unlock(&log_root_tree
->log_mutex
);
3076 btrfs_wait_tree_log_extents(log
, mark
);
3077 mutex_unlock(&log_root_tree
->log_mutex
);
3082 if (log_root_tree
->log_transid_committed
>= root_log_ctx
.log_transid
) {
3083 blk_finish_plug(&plug
);
3084 list_del_init(&root_log_ctx
.list
);
3085 mutex_unlock(&log_root_tree
->log_mutex
);
3086 ret
= root_log_ctx
.log_ret
;
3090 index2
= root_log_ctx
.log_transid
% 2;
3091 if (atomic_read(&log_root_tree
->log_commit
[index2
])) {
3092 blk_finish_plug(&plug
);
3093 ret
= btrfs_wait_tree_log_extents(log
, mark
);
3094 wait_log_commit(log_root_tree
,
3095 root_log_ctx
.log_transid
);
3096 mutex_unlock(&log_root_tree
->log_mutex
);
3098 ret
= root_log_ctx
.log_ret
;
3101 ASSERT(root_log_ctx
.log_transid
== log_root_tree
->log_transid
);
3102 atomic_set(&log_root_tree
->log_commit
[index2
], 1);
3104 if (atomic_read(&log_root_tree
->log_commit
[(index2
+ 1) % 2])) {
3105 wait_log_commit(log_root_tree
,
3106 root_log_ctx
.log_transid
- 1);
3109 wait_for_writer(log_root_tree
);
3112 * now that we've moved on to the tree of log tree roots,
3113 * check the full commit flag again
3115 if (btrfs_need_log_full_commit(fs_info
, trans
)) {
3116 blk_finish_plug(&plug
);
3117 btrfs_wait_tree_log_extents(log
, mark
);
3118 mutex_unlock(&log_root_tree
->log_mutex
);
3120 goto out_wake_log_root
;
3123 ret
= btrfs_write_marked_extents(fs_info
,
3124 &log_root_tree
->dirty_log_pages
,
3125 EXTENT_DIRTY
| EXTENT_NEW
);
3126 blk_finish_plug(&plug
);
3128 btrfs_set_log_full_commit(fs_info
, trans
);
3129 btrfs_abort_transaction(trans
, ret
);
3130 mutex_unlock(&log_root_tree
->log_mutex
);
3131 goto out_wake_log_root
;
3133 ret
= btrfs_wait_tree_log_extents(log
, mark
);
3135 ret
= btrfs_wait_tree_log_extents(log_root_tree
,
3136 EXTENT_NEW
| EXTENT_DIRTY
);
3138 btrfs_set_log_full_commit(fs_info
, trans
);
3139 mutex_unlock(&log_root_tree
->log_mutex
);
3140 goto out_wake_log_root
;
3143 btrfs_set_super_log_root(fs_info
->super_for_commit
,
3144 log_root_tree
->node
->start
);
3145 btrfs_set_super_log_root_level(fs_info
->super_for_commit
,
3146 btrfs_header_level(log_root_tree
->node
));
3148 log_root_tree
->log_transid
++;
3149 mutex_unlock(&log_root_tree
->log_mutex
);
3152 * nobody else is going to jump in and write the the ctree
3153 * super here because the log_commit atomic below is protecting
3154 * us. We must be called with a transaction handle pinning
3155 * the running transaction open, so a full commit can't hop
3156 * in and cause problems either.
3158 ret
= write_all_supers(fs_info
, 1);
3160 btrfs_set_log_full_commit(fs_info
, trans
);
3161 btrfs_abort_transaction(trans
, ret
);
3162 goto out_wake_log_root
;
3165 mutex_lock(&root
->log_mutex
);
3166 if (root
->last_log_commit
< log_transid
)
3167 root
->last_log_commit
= log_transid
;
3168 mutex_unlock(&root
->log_mutex
);
3171 mutex_lock(&log_root_tree
->log_mutex
);
3172 btrfs_remove_all_log_ctxs(log_root_tree
, index2
, ret
);
3174 log_root_tree
->log_transid_committed
++;
3175 atomic_set(&log_root_tree
->log_commit
[index2
], 0);
3176 mutex_unlock(&log_root_tree
->log_mutex
);
3179 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3180 * all the updates above are seen by the woken threads. It might not be
3181 * necessary, but proving that seems to be hard.
3183 cond_wake_up(&log_root_tree
->log_commit_wait
[index2
]);
3185 mutex_lock(&root
->log_mutex
);
3186 btrfs_remove_all_log_ctxs(root
, index1
, ret
);
3187 root
->log_transid_committed
++;
3188 atomic_set(&root
->log_commit
[index1
], 0);
3189 mutex_unlock(&root
->log_mutex
);
3192 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3193 * all the updates above are seen by the woken threads. It might not be
3194 * necessary, but proving that seems to be hard.
3196 cond_wake_up(&root
->log_commit_wait
[index1
]);
3200 static void free_log_tree(struct btrfs_trans_handle
*trans
,
3201 struct btrfs_root
*log
)
3206 struct walk_control wc
= {
3208 .process_func
= process_one_buffer
3211 ret
= walk_log_tree(trans
, log
, &wc
);
3214 btrfs_abort_transaction(trans
, ret
);
3216 btrfs_handle_fs_error(log
->fs_info
, ret
, NULL
);
3220 ret
= find_first_extent_bit(&log
->dirty_log_pages
,
3222 EXTENT_DIRTY
| EXTENT_NEW
| EXTENT_NEED_WAIT
,
3227 clear_extent_bits(&log
->dirty_log_pages
, start
, end
,
3228 EXTENT_DIRTY
| EXTENT_NEW
| EXTENT_NEED_WAIT
);
3231 free_extent_buffer(log
->node
);
3236 * free all the extents used by the tree log. This should be called
3237 * at commit time of the full transaction
3239 int btrfs_free_log(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
)
3241 if (root
->log_root
) {
3242 free_log_tree(trans
, root
->log_root
);
3243 root
->log_root
= NULL
;
3248 int btrfs_free_log_root_tree(struct btrfs_trans_handle
*trans
,
3249 struct btrfs_fs_info
*fs_info
)
3251 if (fs_info
->log_root_tree
) {
3252 free_log_tree(trans
, fs_info
->log_root_tree
);
3253 fs_info
->log_root_tree
= NULL
;
3259 * If both a file and directory are logged, and unlinks or renames are
3260 * mixed in, we have a few interesting corners:
3262 * create file X in dir Y
3263 * link file X to X.link in dir Y
3265 * unlink file X but leave X.link
3268 * After a crash we would expect only X.link to exist. But file X
3269 * didn't get fsync'd again so the log has back refs for X and X.link.
3271 * We solve this by removing directory entries and inode backrefs from the
3272 * log when a file that was logged in the current transaction is
3273 * unlinked. Any later fsync will include the updated log entries, and
3274 * we'll be able to reconstruct the proper directory items from backrefs.
3276 * This optimizations allows us to avoid relogging the entire inode
3277 * or the entire directory.
3279 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle
*trans
,
3280 struct btrfs_root
*root
,
3281 const char *name
, int name_len
,
3282 struct btrfs_inode
*dir
, u64 index
)
3284 struct btrfs_root
*log
;
3285 struct btrfs_dir_item
*di
;
3286 struct btrfs_path
*path
;
3290 u64 dir_ino
= btrfs_ino(dir
);
3292 if (dir
->logged_trans
< trans
->transid
)
3295 ret
= join_running_log_trans(root
);
3299 mutex_lock(&dir
->log_mutex
);
3301 log
= root
->log_root
;
3302 path
= btrfs_alloc_path();
3308 di
= btrfs_lookup_dir_item(trans
, log
, path
, dir_ino
,
3309 name
, name_len
, -1);
3315 ret
= btrfs_delete_one_dir_name(trans
, log
, path
, di
);
3316 bytes_del
+= name_len
;
3322 btrfs_release_path(path
);
3323 di
= btrfs_lookup_dir_index_item(trans
, log
, path
, dir_ino
,
3324 index
, name
, name_len
, -1);
3330 ret
= btrfs_delete_one_dir_name(trans
, log
, path
, di
);
3331 bytes_del
+= name_len
;
3338 /* update the directory size in the log to reflect the names
3342 struct btrfs_key key
;
3344 key
.objectid
= dir_ino
;
3346 key
.type
= BTRFS_INODE_ITEM_KEY
;
3347 btrfs_release_path(path
);
3349 ret
= btrfs_search_slot(trans
, log
, &key
, path
, 0, 1);
3355 struct btrfs_inode_item
*item
;
3358 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3359 struct btrfs_inode_item
);
3360 i_size
= btrfs_inode_size(path
->nodes
[0], item
);
3361 if (i_size
> bytes_del
)
3362 i_size
-= bytes_del
;
3365 btrfs_set_inode_size(path
->nodes
[0], item
, i_size
);
3366 btrfs_mark_buffer_dirty(path
->nodes
[0]);
3369 btrfs_release_path(path
);
3372 btrfs_free_path(path
);
3374 mutex_unlock(&dir
->log_mutex
);
3375 if (ret
== -ENOSPC
) {
3376 btrfs_set_log_full_commit(root
->fs_info
, trans
);
3379 btrfs_abort_transaction(trans
, ret
);
3381 btrfs_end_log_trans(root
);
3386 /* see comments for btrfs_del_dir_entries_in_log */
3387 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle
*trans
,
3388 struct btrfs_root
*root
,
3389 const char *name
, int name_len
,
3390 struct btrfs_inode
*inode
, u64 dirid
)
3392 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3393 struct btrfs_root
*log
;
3397 if (inode
->logged_trans
< trans
->transid
)
3400 ret
= join_running_log_trans(root
);
3403 log
= root
->log_root
;
3404 mutex_lock(&inode
->log_mutex
);
3406 ret
= btrfs_del_inode_ref(trans
, log
, name
, name_len
, btrfs_ino(inode
),
3408 mutex_unlock(&inode
->log_mutex
);
3409 if (ret
== -ENOSPC
) {
3410 btrfs_set_log_full_commit(fs_info
, trans
);
3412 } else if (ret
< 0 && ret
!= -ENOENT
)
3413 btrfs_abort_transaction(trans
, ret
);
3414 btrfs_end_log_trans(root
);
3420 * creates a range item in the log for 'dirid'. first_offset and
3421 * last_offset tell us which parts of the key space the log should
3422 * be considered authoritative for.
3424 static noinline
int insert_dir_log_key(struct btrfs_trans_handle
*trans
,
3425 struct btrfs_root
*log
,
3426 struct btrfs_path
*path
,
3427 int key_type
, u64 dirid
,
3428 u64 first_offset
, u64 last_offset
)
3431 struct btrfs_key key
;
3432 struct btrfs_dir_log_item
*item
;
3434 key
.objectid
= dirid
;
3435 key
.offset
= first_offset
;
3436 if (key_type
== BTRFS_DIR_ITEM_KEY
)
3437 key
.type
= BTRFS_DIR_LOG_ITEM_KEY
;
3439 key
.type
= BTRFS_DIR_LOG_INDEX_KEY
;
3440 ret
= btrfs_insert_empty_item(trans
, log
, path
, &key
, sizeof(*item
));
3444 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3445 struct btrfs_dir_log_item
);
3446 btrfs_set_dir_log_end(path
->nodes
[0], item
, last_offset
);
3447 btrfs_mark_buffer_dirty(path
->nodes
[0]);
3448 btrfs_release_path(path
);
3453 * log all the items included in the current transaction for a given
3454 * directory. This also creates the range items in the log tree required
3455 * to replay anything deleted before the fsync
3457 static noinline
int log_dir_items(struct btrfs_trans_handle
*trans
,
3458 struct btrfs_root
*root
, struct btrfs_inode
*inode
,
3459 struct btrfs_path
*path
,
3460 struct btrfs_path
*dst_path
, int key_type
,
3461 struct btrfs_log_ctx
*ctx
,
3462 u64 min_offset
, u64
*last_offset_ret
)
3464 struct btrfs_key min_key
;
3465 struct btrfs_root
*log
= root
->log_root
;
3466 struct extent_buffer
*src
;
3471 u64 first_offset
= min_offset
;
3472 u64 last_offset
= (u64
)-1;
3473 u64 ino
= btrfs_ino(inode
);
3475 log
= root
->log_root
;
3477 min_key
.objectid
= ino
;
3478 min_key
.type
= key_type
;
3479 min_key
.offset
= min_offset
;
3481 ret
= btrfs_search_forward(root
, &min_key
, path
, trans
->transid
);
3484 * we didn't find anything from this transaction, see if there
3485 * is anything at all
3487 if (ret
!= 0 || min_key
.objectid
!= ino
|| min_key
.type
!= key_type
) {
3488 min_key
.objectid
= ino
;
3489 min_key
.type
= key_type
;
3490 min_key
.offset
= (u64
)-1;
3491 btrfs_release_path(path
);
3492 ret
= btrfs_search_slot(NULL
, root
, &min_key
, path
, 0, 0);
3494 btrfs_release_path(path
);
3497 ret
= btrfs_previous_item(root
, path
, ino
, key_type
);
3499 /* if ret == 0 there are items for this type,
3500 * create a range to tell us the last key of this type.
3501 * otherwise, there are no items in this directory after
3502 * *min_offset, and we create a range to indicate that.
3505 struct btrfs_key tmp
;
3506 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
,
3508 if (key_type
== tmp
.type
)
3509 first_offset
= max(min_offset
, tmp
.offset
) + 1;
3514 /* go backward to find any previous key */
3515 ret
= btrfs_previous_item(root
, path
, ino
, key_type
);
3517 struct btrfs_key tmp
;
3518 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
, path
->slots
[0]);
3519 if (key_type
== tmp
.type
) {
3520 first_offset
= tmp
.offset
;
3521 ret
= overwrite_item(trans
, log
, dst_path
,
3522 path
->nodes
[0], path
->slots
[0],
3530 btrfs_release_path(path
);
3532 /* find the first key from this transaction again */
3533 ret
= btrfs_search_slot(NULL
, root
, &min_key
, path
, 0, 0);
3534 if (WARN_ON(ret
!= 0))
3538 * we have a block from this transaction, log every item in it
3539 * from our directory
3542 struct btrfs_key tmp
;
3543 src
= path
->nodes
[0];
3544 nritems
= btrfs_header_nritems(src
);
3545 for (i
= path
->slots
[0]; i
< nritems
; i
++) {
3546 struct btrfs_dir_item
*di
;
3548 btrfs_item_key_to_cpu(src
, &min_key
, i
);
3550 if (min_key
.objectid
!= ino
|| min_key
.type
!= key_type
)
3552 ret
= overwrite_item(trans
, log
, dst_path
, src
, i
,
3560 * We must make sure that when we log a directory entry,
3561 * the corresponding inode, after log replay, has a
3562 * matching link count. For example:
3568 * xfs_io -c "fsync" mydir
3570 * <mount fs and log replay>
3572 * Would result in a fsync log that when replayed, our
3573 * file inode would have a link count of 1, but we get
3574 * two directory entries pointing to the same inode.
3575 * After removing one of the names, it would not be
3576 * possible to remove the other name, which resulted
3577 * always in stale file handle errors, and would not
3578 * be possible to rmdir the parent directory, since
3579 * its i_size could never decrement to the value
3580 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3582 di
= btrfs_item_ptr(src
, i
, struct btrfs_dir_item
);
3583 btrfs_dir_item_key_to_cpu(src
, di
, &tmp
);
3585 (btrfs_dir_transid(src
, di
) == trans
->transid
||
3586 btrfs_dir_type(src
, di
) == BTRFS_FT_DIR
) &&
3587 tmp
.type
!= BTRFS_ROOT_ITEM_KEY
)
3588 ctx
->log_new_dentries
= true;
3590 path
->slots
[0] = nritems
;
3593 * look ahead to the next item and see if it is also
3594 * from this directory and from this transaction
3596 ret
= btrfs_next_leaf(root
, path
);
3599 last_offset
= (u64
)-1;
3604 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
, path
->slots
[0]);
3605 if (tmp
.objectid
!= ino
|| tmp
.type
!= key_type
) {
3606 last_offset
= (u64
)-1;
3609 if (btrfs_header_generation(path
->nodes
[0]) != trans
->transid
) {
3610 ret
= overwrite_item(trans
, log
, dst_path
,
3611 path
->nodes
[0], path
->slots
[0],
3616 last_offset
= tmp
.offset
;
3621 btrfs_release_path(path
);
3622 btrfs_release_path(dst_path
);
3625 *last_offset_ret
= last_offset
;
3627 * insert the log range keys to indicate where the log
3630 ret
= insert_dir_log_key(trans
, log
, path
, key_type
,
3631 ino
, first_offset
, last_offset
);
3639 * logging directories is very similar to logging inodes, We find all the items
3640 * from the current transaction and write them to the log.
3642 * The recovery code scans the directory in the subvolume, and if it finds a
3643 * key in the range logged that is not present in the log tree, then it means
3644 * that dir entry was unlinked during the transaction.
3646 * In order for that scan to work, we must include one key smaller than
3647 * the smallest logged by this transaction and one key larger than the largest
3648 * key logged by this transaction.
3650 static noinline
int log_directory_changes(struct btrfs_trans_handle
*trans
,
3651 struct btrfs_root
*root
, struct btrfs_inode
*inode
,
3652 struct btrfs_path
*path
,
3653 struct btrfs_path
*dst_path
,
3654 struct btrfs_log_ctx
*ctx
)
3659 int key_type
= BTRFS_DIR_ITEM_KEY
;
3665 ret
= log_dir_items(trans
, root
, inode
, path
, dst_path
, key_type
,
3666 ctx
, min_key
, &max_key
);
3669 if (max_key
== (u64
)-1)
3671 min_key
= max_key
+ 1;
3674 if (key_type
== BTRFS_DIR_ITEM_KEY
) {
3675 key_type
= BTRFS_DIR_INDEX_KEY
;
3682 * a helper function to drop items from the log before we relog an
3683 * inode. max_key_type indicates the highest item type to remove.
3684 * This cannot be run for file data extents because it does not
3685 * free the extents they point to.
3687 static int drop_objectid_items(struct btrfs_trans_handle
*trans
,
3688 struct btrfs_root
*log
,
3689 struct btrfs_path
*path
,
3690 u64 objectid
, int max_key_type
)
3693 struct btrfs_key key
;
3694 struct btrfs_key found_key
;
3697 key
.objectid
= objectid
;
3698 key
.type
= max_key_type
;
3699 key
.offset
= (u64
)-1;
3702 ret
= btrfs_search_slot(trans
, log
, &key
, path
, -1, 1);
3703 BUG_ON(ret
== 0); /* Logic error */
3707 if (path
->slots
[0] == 0)
3711 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
3714 if (found_key
.objectid
!= objectid
)
3717 found_key
.offset
= 0;
3719 ret
= btrfs_bin_search(path
->nodes
[0], &found_key
, 0,
3722 ret
= btrfs_del_items(trans
, log
, path
, start_slot
,
3723 path
->slots
[0] - start_slot
+ 1);
3725 * If start slot isn't 0 then we don't need to re-search, we've
3726 * found the last guy with the objectid in this tree.
3728 if (ret
|| start_slot
!= 0)
3730 btrfs_release_path(path
);
3732 btrfs_release_path(path
);
3738 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3739 struct extent_buffer
*leaf
,
3740 struct btrfs_inode_item
*item
,
3741 struct inode
*inode
, int log_inode_only
,
3744 struct btrfs_map_token token
;
3746 btrfs_init_map_token(&token
);
3748 if (log_inode_only
) {
3749 /* set the generation to zero so the recover code
3750 * can tell the difference between an logging
3751 * just to say 'this inode exists' and a logging
3752 * to say 'update this inode with these values'
3754 btrfs_set_token_inode_generation(leaf
, item
, 0, &token
);
3755 btrfs_set_token_inode_size(leaf
, item
, logged_isize
, &token
);
3757 btrfs_set_token_inode_generation(leaf
, item
,
3758 BTRFS_I(inode
)->generation
,
3760 btrfs_set_token_inode_size(leaf
, item
, inode
->i_size
, &token
);
3763 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3764 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3765 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3766 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3768 btrfs_set_token_timespec_sec(leaf
, &item
->atime
,
3769 inode
->i_atime
.tv_sec
, &token
);
3770 btrfs_set_token_timespec_nsec(leaf
, &item
->atime
,
3771 inode
->i_atime
.tv_nsec
, &token
);
3773 btrfs_set_token_timespec_sec(leaf
, &item
->mtime
,
3774 inode
->i_mtime
.tv_sec
, &token
);
3775 btrfs_set_token_timespec_nsec(leaf
, &item
->mtime
,
3776 inode
->i_mtime
.tv_nsec
, &token
);
3778 btrfs_set_token_timespec_sec(leaf
, &item
->ctime
,
3779 inode
->i_ctime
.tv_sec
, &token
);
3780 btrfs_set_token_timespec_nsec(leaf
, &item
->ctime
,
3781 inode
->i_ctime
.tv_nsec
, &token
);
3783 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3786 btrfs_set_token_inode_sequence(leaf
, item
,
3787 inode_peek_iversion(inode
), &token
);
3788 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3789 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3790 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3791 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3794 static int log_inode_item(struct btrfs_trans_handle
*trans
,
3795 struct btrfs_root
*log
, struct btrfs_path
*path
,
3796 struct btrfs_inode
*inode
)
3798 struct btrfs_inode_item
*inode_item
;
3801 ret
= btrfs_insert_empty_item(trans
, log
, path
,
3802 &inode
->location
, sizeof(*inode_item
));
3803 if (ret
&& ret
!= -EEXIST
)
3805 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3806 struct btrfs_inode_item
);
3807 fill_inode_item(trans
, path
->nodes
[0], inode_item
, &inode
->vfs_inode
,
3809 btrfs_release_path(path
);
3813 static noinline
int copy_items(struct btrfs_trans_handle
*trans
,
3814 struct btrfs_inode
*inode
,
3815 struct btrfs_path
*dst_path
,
3816 struct btrfs_path
*src_path
, u64
*last_extent
,
3817 int start_slot
, int nr
, int inode_only
,
3820 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
3821 unsigned long src_offset
;
3822 unsigned long dst_offset
;
3823 struct btrfs_root
*log
= inode
->root
->log_root
;
3824 struct btrfs_file_extent_item
*extent
;
3825 struct btrfs_inode_item
*inode_item
;
3826 struct extent_buffer
*src
= src_path
->nodes
[0];
3827 struct btrfs_key first_key
, last_key
, key
;
3829 struct btrfs_key
*ins_keys
;
3833 struct list_head ordered_sums
;
3834 int skip_csum
= inode
->flags
& BTRFS_INODE_NODATASUM
;
3835 bool has_extents
= false;
3836 bool need_find_last_extent
= true;
3839 INIT_LIST_HEAD(&ordered_sums
);
3841 ins_data
= kmalloc(nr
* sizeof(struct btrfs_key
) +
3842 nr
* sizeof(u32
), GFP_NOFS
);
3846 first_key
.objectid
= (u64
)-1;
3848 ins_sizes
= (u32
*)ins_data
;
3849 ins_keys
= (struct btrfs_key
*)(ins_data
+ nr
* sizeof(u32
));
3851 for (i
= 0; i
< nr
; i
++) {
3852 ins_sizes
[i
] = btrfs_item_size_nr(src
, i
+ start_slot
);
3853 btrfs_item_key_to_cpu(src
, ins_keys
+ i
, i
+ start_slot
);
3855 ret
= btrfs_insert_empty_items(trans
, log
, dst_path
,
3856 ins_keys
, ins_sizes
, nr
);
3862 for (i
= 0; i
< nr
; i
++, dst_path
->slots
[0]++) {
3863 dst_offset
= btrfs_item_ptr_offset(dst_path
->nodes
[0],
3864 dst_path
->slots
[0]);
3866 src_offset
= btrfs_item_ptr_offset(src
, start_slot
+ i
);
3869 last_key
= ins_keys
[i
];
3871 if (ins_keys
[i
].type
== BTRFS_INODE_ITEM_KEY
) {
3872 inode_item
= btrfs_item_ptr(dst_path
->nodes
[0],
3874 struct btrfs_inode_item
);
3875 fill_inode_item(trans
, dst_path
->nodes
[0], inode_item
,
3877 inode_only
== LOG_INODE_EXISTS
,
3880 copy_extent_buffer(dst_path
->nodes
[0], src
, dst_offset
,
3881 src_offset
, ins_sizes
[i
]);
3885 * We set need_find_last_extent here in case we know we were
3886 * processing other items and then walk into the first extent in
3887 * the inode. If we don't hit an extent then nothing changes,
3888 * we'll do the last search the next time around.
3890 if (ins_keys
[i
].type
== BTRFS_EXTENT_DATA_KEY
) {
3892 if (first_key
.objectid
== (u64
)-1)
3893 first_key
= ins_keys
[i
];
3895 need_find_last_extent
= false;
3898 /* take a reference on file data extents so that truncates
3899 * or deletes of this inode don't have to relog the inode
3902 if (ins_keys
[i
].type
== BTRFS_EXTENT_DATA_KEY
&&
3905 extent
= btrfs_item_ptr(src
, start_slot
+ i
,
3906 struct btrfs_file_extent_item
);
3908 if (btrfs_file_extent_generation(src
, extent
) < trans
->transid
)
3911 found_type
= btrfs_file_extent_type(src
, extent
);
3912 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
3914 ds
= btrfs_file_extent_disk_bytenr(src
,
3916 /* ds == 0 is a hole */
3920 dl
= btrfs_file_extent_disk_num_bytes(src
,
3922 cs
= btrfs_file_extent_offset(src
, extent
);
3923 cl
= btrfs_file_extent_num_bytes(src
,
3925 if (btrfs_file_extent_compression(src
,
3931 ret
= btrfs_lookup_csums_range(
3933 ds
+ cs
, ds
+ cs
+ cl
- 1,
3936 btrfs_release_path(dst_path
);
3944 btrfs_mark_buffer_dirty(dst_path
->nodes
[0]);
3945 btrfs_release_path(dst_path
);
3949 * we have to do this after the loop above to avoid changing the
3950 * log tree while trying to change the log tree.
3953 while (!list_empty(&ordered_sums
)) {
3954 struct btrfs_ordered_sum
*sums
= list_entry(ordered_sums
.next
,
3955 struct btrfs_ordered_sum
,
3958 ret
= btrfs_csum_file_blocks(trans
, log
, sums
);
3959 list_del(&sums
->list
);
3966 if (need_find_last_extent
&& *last_extent
== first_key
.offset
) {
3968 * We don't have any leafs between our current one and the one
3969 * we processed before that can have file extent items for our
3970 * inode (and have a generation number smaller than our current
3973 need_find_last_extent
= false;
3977 * Because we use btrfs_search_forward we could skip leaves that were
3978 * not modified and then assume *last_extent is valid when it really
3979 * isn't. So back up to the previous leaf and read the end of the last
3980 * extent before we go and fill in holes.
3982 if (need_find_last_extent
) {
3985 ret
= btrfs_prev_leaf(inode
->root
, src_path
);
3990 if (src_path
->slots
[0])
3991 src_path
->slots
[0]--;
3992 src
= src_path
->nodes
[0];
3993 btrfs_item_key_to_cpu(src
, &key
, src_path
->slots
[0]);
3994 if (key
.objectid
!= btrfs_ino(inode
) ||
3995 key
.type
!= BTRFS_EXTENT_DATA_KEY
)
3997 extent
= btrfs_item_ptr(src
, src_path
->slots
[0],
3998 struct btrfs_file_extent_item
);
3999 if (btrfs_file_extent_type(src
, extent
) ==
4000 BTRFS_FILE_EXTENT_INLINE
) {
4001 len
= btrfs_file_extent_ram_bytes(src
, extent
);
4002 *last_extent
= ALIGN(key
.offset
+ len
,
4003 fs_info
->sectorsize
);
4005 len
= btrfs_file_extent_num_bytes(src
, extent
);
4006 *last_extent
= key
.offset
+ len
;
4010 /* So we did prev_leaf, now we need to move to the next leaf, but a few
4011 * things could have happened
4013 * 1) A merge could have happened, so we could currently be on a leaf
4014 * that holds what we were copying in the first place.
4015 * 2) A split could have happened, and now not all of the items we want
4016 * are on the same leaf.
4018 * So we need to adjust how we search for holes, we need to drop the
4019 * path and re-search for the first extent key we found, and then walk
4020 * forward until we hit the last one we copied.
4022 if (need_find_last_extent
) {
4023 /* btrfs_prev_leaf could return 1 without releasing the path */
4024 btrfs_release_path(src_path
);
4025 ret
= btrfs_search_slot(NULL
, inode
->root
, &first_key
,
4030 src
= src_path
->nodes
[0];
4031 i
= src_path
->slots
[0];
4037 * Ok so here we need to go through and fill in any holes we may have
4038 * to make sure that holes are punched for those areas in case they had
4039 * extents previously.
4045 if (i
>= btrfs_header_nritems(src_path
->nodes
[0])) {
4046 ret
= btrfs_next_leaf(inode
->root
, src_path
);
4050 src
= src_path
->nodes
[0];
4052 need_find_last_extent
= true;
4055 btrfs_item_key_to_cpu(src
, &key
, i
);
4056 if (!btrfs_comp_cpu_keys(&key
, &last_key
))
4058 if (key
.objectid
!= btrfs_ino(inode
) ||
4059 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
4063 extent
= btrfs_item_ptr(src
, i
, struct btrfs_file_extent_item
);
4064 if (btrfs_file_extent_type(src
, extent
) ==
4065 BTRFS_FILE_EXTENT_INLINE
) {
4066 len
= btrfs_file_extent_ram_bytes(src
, extent
);
4067 extent_end
= ALIGN(key
.offset
+ len
,
4068 fs_info
->sectorsize
);
4070 len
= btrfs_file_extent_num_bytes(src
, extent
);
4071 extent_end
= key
.offset
+ len
;
4075 if (*last_extent
== key
.offset
) {
4076 *last_extent
= extent_end
;
4079 offset
= *last_extent
;
4080 len
= key
.offset
- *last_extent
;
4081 ret
= btrfs_insert_file_extent(trans
, log
, btrfs_ino(inode
),
4082 offset
, 0, 0, len
, 0, len
, 0, 0, 0);
4085 *last_extent
= extent_end
;
4089 * Check if there is a hole between the last extent found in our leaf
4090 * and the first extent in the next leaf. If there is one, we need to
4091 * log an explicit hole so that at replay time we can punch the hole.
4094 key
.objectid
== btrfs_ino(inode
) &&
4095 key
.type
== BTRFS_EXTENT_DATA_KEY
&&
4096 i
== btrfs_header_nritems(src_path
->nodes
[0])) {
4097 ret
= btrfs_next_leaf(inode
->root
, src_path
);
4098 need_find_last_extent
= true;
4101 } else if (ret
== 0) {
4102 btrfs_item_key_to_cpu(src_path
->nodes
[0], &key
,
4103 src_path
->slots
[0]);
4104 if (key
.objectid
== btrfs_ino(inode
) &&
4105 key
.type
== BTRFS_EXTENT_DATA_KEY
&&
4106 *last_extent
< key
.offset
) {
4107 const u64 len
= key
.offset
- *last_extent
;
4109 ret
= btrfs_insert_file_extent(trans
, log
,
4118 * Need to let the callers know we dropped the path so they should
4121 if (!ret
&& need_find_last_extent
)
4126 static int extent_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
4128 struct extent_map
*em1
, *em2
;
4130 em1
= list_entry(a
, struct extent_map
, list
);
4131 em2
= list_entry(b
, struct extent_map
, list
);
4133 if (em1
->start
< em2
->start
)
4135 else if (em1
->start
> em2
->start
)
4140 static int log_extent_csums(struct btrfs_trans_handle
*trans
,
4141 struct btrfs_inode
*inode
,
4142 struct btrfs_root
*log_root
,
4143 const struct extent_map
*em
)
4147 LIST_HEAD(ordered_sums
);
4150 if (inode
->flags
& BTRFS_INODE_NODATASUM
||
4151 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
4152 em
->block_start
== EXTENT_MAP_HOLE
)
4155 /* If we're compressed we have to save the entire range of csums. */
4156 if (em
->compress_type
) {
4158 csum_len
= max(em
->block_len
, em
->orig_block_len
);
4160 csum_offset
= em
->mod_start
- em
->start
;
4161 csum_len
= em
->mod_len
;
4164 /* block start is already adjusted for the file extent offset. */
4165 ret
= btrfs_lookup_csums_range(trans
->fs_info
->csum_root
,
4166 em
->block_start
+ csum_offset
,
4167 em
->block_start
+ csum_offset
+
4168 csum_len
- 1, &ordered_sums
, 0);
4172 while (!list_empty(&ordered_sums
)) {
4173 struct btrfs_ordered_sum
*sums
= list_entry(ordered_sums
.next
,
4174 struct btrfs_ordered_sum
,
4177 ret
= btrfs_csum_file_blocks(trans
, log_root
, sums
);
4178 list_del(&sums
->list
);
4185 static int log_one_extent(struct btrfs_trans_handle
*trans
,
4186 struct btrfs_inode
*inode
, struct btrfs_root
*root
,
4187 const struct extent_map
*em
,
4188 struct btrfs_path
*path
,
4189 struct btrfs_log_ctx
*ctx
)
4191 struct btrfs_root
*log
= root
->log_root
;
4192 struct btrfs_file_extent_item
*fi
;
4193 struct extent_buffer
*leaf
;
4194 struct btrfs_map_token token
;
4195 struct btrfs_key key
;
4196 u64 extent_offset
= em
->start
- em
->orig_start
;
4199 int extent_inserted
= 0;
4201 ret
= log_extent_csums(trans
, inode
, log
, em
);
4205 btrfs_init_map_token(&token
);
4207 ret
= __btrfs_drop_extents(trans
, log
, &inode
->vfs_inode
, path
, em
->start
,
4208 em
->start
+ em
->len
, NULL
, 0, 1,
4209 sizeof(*fi
), &extent_inserted
);
4213 if (!extent_inserted
) {
4214 key
.objectid
= btrfs_ino(inode
);
4215 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4216 key
.offset
= em
->start
;
4218 ret
= btrfs_insert_empty_item(trans
, log
, path
, &key
,
4223 leaf
= path
->nodes
[0];
4224 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4225 struct btrfs_file_extent_item
);
4227 btrfs_set_token_file_extent_generation(leaf
, fi
, trans
->transid
,
4229 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
4230 btrfs_set_token_file_extent_type(leaf
, fi
,
4231 BTRFS_FILE_EXTENT_PREALLOC
,
4234 btrfs_set_token_file_extent_type(leaf
, fi
,
4235 BTRFS_FILE_EXTENT_REG
,
4238 block_len
= max(em
->block_len
, em
->orig_block_len
);
4239 if (em
->compress_type
!= BTRFS_COMPRESS_NONE
) {
4240 btrfs_set_token_file_extent_disk_bytenr(leaf
, fi
,
4243 btrfs_set_token_file_extent_disk_num_bytes(leaf
, fi
, block_len
,
4245 } else if (em
->block_start
< EXTENT_MAP_LAST_BYTE
) {
4246 btrfs_set_token_file_extent_disk_bytenr(leaf
, fi
,
4248 extent_offset
, &token
);
4249 btrfs_set_token_file_extent_disk_num_bytes(leaf
, fi
, block_len
,
4252 btrfs_set_token_file_extent_disk_bytenr(leaf
, fi
, 0, &token
);
4253 btrfs_set_token_file_extent_disk_num_bytes(leaf
, fi
, 0,
4257 btrfs_set_token_file_extent_offset(leaf
, fi
, extent_offset
, &token
);
4258 btrfs_set_token_file_extent_num_bytes(leaf
, fi
, em
->len
, &token
);
4259 btrfs_set_token_file_extent_ram_bytes(leaf
, fi
, em
->ram_bytes
, &token
);
4260 btrfs_set_token_file_extent_compression(leaf
, fi
, em
->compress_type
,
4262 btrfs_set_token_file_extent_encryption(leaf
, fi
, 0, &token
);
4263 btrfs_set_token_file_extent_other_encoding(leaf
, fi
, 0, &token
);
4264 btrfs_mark_buffer_dirty(leaf
);
4266 btrfs_release_path(path
);
4272 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4273 * lose them after doing a fast fsync and replaying the log. We scan the
4274 * subvolume's root instead of iterating the inode's extent map tree because
4275 * otherwise we can log incorrect extent items based on extent map conversion.
4276 * That can happen due to the fact that extent maps are merged when they
4277 * are not in the extent map tree's list of modified extents.
4279 static int btrfs_log_prealloc_extents(struct btrfs_trans_handle
*trans
,
4280 struct btrfs_inode
*inode
,
4281 struct btrfs_path
*path
)
4283 struct btrfs_root
*root
= inode
->root
;
4284 struct btrfs_key key
;
4285 const u64 i_size
= i_size_read(&inode
->vfs_inode
);
4286 const u64 ino
= btrfs_ino(inode
);
4287 struct btrfs_path
*dst_path
= NULL
;
4288 u64 last_extent
= (u64
)-1;
4293 if (!(inode
->flags
& BTRFS_INODE_PREALLOC
))
4297 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4298 key
.offset
= i_size
;
4299 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4304 struct extent_buffer
*leaf
= path
->nodes
[0];
4305 int slot
= path
->slots
[0];
4307 if (slot
>= btrfs_header_nritems(leaf
)) {
4309 ret
= copy_items(trans
, inode
, dst_path
, path
,
4310 &last_extent
, start_slot
,
4316 ret
= btrfs_next_leaf(root
, path
);
4326 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
4327 if (key
.objectid
> ino
)
4329 if (WARN_ON_ONCE(key
.objectid
< ino
) ||
4330 key
.type
< BTRFS_EXTENT_DATA_KEY
||
4331 key
.offset
< i_size
) {
4335 if (last_extent
== (u64
)-1) {
4336 last_extent
= key
.offset
;
4338 * Avoid logging extent items logged in past fsync calls
4339 * and leading to duplicate keys in the log tree.
4342 ret
= btrfs_truncate_inode_items(trans
,
4346 BTRFS_EXTENT_DATA_KEY
);
4347 } while (ret
== -EAGAIN
);
4356 dst_path
= btrfs_alloc_path();
4364 ret
= copy_items(trans
, inode
, dst_path
, path
, &last_extent
,
4365 start_slot
, ins_nr
, 1, 0);
4370 btrfs_release_path(path
);
4371 btrfs_free_path(dst_path
);
4375 static int btrfs_log_changed_extents(struct btrfs_trans_handle
*trans
,
4376 struct btrfs_root
*root
,
4377 struct btrfs_inode
*inode
,
4378 struct btrfs_path
*path
,
4379 struct btrfs_log_ctx
*ctx
,
4383 struct extent_map
*em
, *n
;
4384 struct list_head extents
;
4385 struct extent_map_tree
*tree
= &inode
->extent_tree
;
4386 u64 logged_start
, logged_end
;
4391 INIT_LIST_HEAD(&extents
);
4393 write_lock(&tree
->lock
);
4394 test_gen
= root
->fs_info
->last_trans_committed
;
4395 logged_start
= start
;
4398 list_for_each_entry_safe(em
, n
, &tree
->modified_extents
, list
) {
4400 * Skip extents outside our logging range. It's important to do
4401 * it for correctness because if we don't ignore them, we may
4402 * log them before their ordered extent completes, and therefore
4403 * we could log them without logging their respective checksums
4404 * (the checksum items are added to the csum tree at the very
4405 * end of btrfs_finish_ordered_io()). Also leave such extents
4406 * outside of our range in the list, since we may have another
4407 * ranged fsync in the near future that needs them. If an extent
4408 * outside our range corresponds to a hole, log it to avoid
4409 * leaving gaps between extents (fsck will complain when we are
4410 * not using the NO_HOLES feature).
4412 if ((em
->start
> end
|| em
->start
+ em
->len
<= start
) &&
4413 em
->block_start
!= EXTENT_MAP_HOLE
)
4416 list_del_init(&em
->list
);
4418 * Just an arbitrary number, this can be really CPU intensive
4419 * once we start getting a lot of extents, and really once we
4420 * have a bunch of extents we just want to commit since it will
4423 if (++num
> 32768) {
4424 list_del_init(&tree
->modified_extents
);
4429 if (em
->generation
<= test_gen
)
4432 /* We log prealloc extents beyond eof later. */
4433 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) &&
4434 em
->start
>= i_size_read(&inode
->vfs_inode
))
4437 if (em
->start
< logged_start
)
4438 logged_start
= em
->start
;
4439 if ((em
->start
+ em
->len
- 1) > logged_end
)
4440 logged_end
= em
->start
+ em
->len
- 1;
4442 /* Need a ref to keep it from getting evicted from cache */
4443 refcount_inc(&em
->refs
);
4444 set_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
4445 list_add_tail(&em
->list
, &extents
);
4449 list_sort(NULL
, &extents
, extent_cmp
);
4451 while (!list_empty(&extents
)) {
4452 em
= list_entry(extents
.next
, struct extent_map
, list
);
4454 list_del_init(&em
->list
);
4457 * If we had an error we just need to delete everybody from our
4461 clear_em_logging(tree
, em
);
4462 free_extent_map(em
);
4466 write_unlock(&tree
->lock
);
4468 ret
= log_one_extent(trans
, inode
, root
, em
, path
, ctx
);
4469 write_lock(&tree
->lock
);
4470 clear_em_logging(tree
, em
);
4471 free_extent_map(em
);
4473 WARN_ON(!list_empty(&extents
));
4474 write_unlock(&tree
->lock
);
4476 btrfs_release_path(path
);
4478 ret
= btrfs_log_prealloc_extents(trans
, inode
, path
);
4483 static int logged_inode_size(struct btrfs_root
*log
, struct btrfs_inode
*inode
,
4484 struct btrfs_path
*path
, u64
*size_ret
)
4486 struct btrfs_key key
;
4489 key
.objectid
= btrfs_ino(inode
);
4490 key
.type
= BTRFS_INODE_ITEM_KEY
;
4493 ret
= btrfs_search_slot(NULL
, log
, &key
, path
, 0, 0);
4496 } else if (ret
> 0) {
4499 struct btrfs_inode_item
*item
;
4501 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4502 struct btrfs_inode_item
);
4503 *size_ret
= btrfs_inode_size(path
->nodes
[0], item
);
4506 btrfs_release_path(path
);
4511 * At the moment we always log all xattrs. This is to figure out at log replay
4512 * time which xattrs must have their deletion replayed. If a xattr is missing
4513 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4514 * because if a xattr is deleted, the inode is fsynced and a power failure
4515 * happens, causing the log to be replayed the next time the fs is mounted,
4516 * we want the xattr to not exist anymore (same behaviour as other filesystems
4517 * with a journal, ext3/4, xfs, f2fs, etc).
4519 static int btrfs_log_all_xattrs(struct btrfs_trans_handle
*trans
,
4520 struct btrfs_root
*root
,
4521 struct btrfs_inode
*inode
,
4522 struct btrfs_path
*path
,
4523 struct btrfs_path
*dst_path
)
4526 struct btrfs_key key
;
4527 const u64 ino
= btrfs_ino(inode
);
4532 key
.type
= BTRFS_XATTR_ITEM_KEY
;
4535 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4540 int slot
= path
->slots
[0];
4541 struct extent_buffer
*leaf
= path
->nodes
[0];
4542 int nritems
= btrfs_header_nritems(leaf
);
4544 if (slot
>= nritems
) {
4546 u64 last_extent
= 0;
4548 ret
= copy_items(trans
, inode
, dst_path
, path
,
4549 &last_extent
, start_slot
,
4551 /* can't be 1, extent items aren't processed */
4557 ret
= btrfs_next_leaf(root
, path
);
4565 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
4566 if (key
.objectid
!= ino
|| key
.type
!= BTRFS_XATTR_ITEM_KEY
)
4576 u64 last_extent
= 0;
4578 ret
= copy_items(trans
, inode
, dst_path
, path
,
4579 &last_extent
, start_slot
,
4581 /* can't be 1, extent items aren't processed */
4591 * If the no holes feature is enabled we need to make sure any hole between the
4592 * last extent and the i_size of our inode is explicitly marked in the log. This
4593 * is to make sure that doing something like:
4595 * 1) create file with 128Kb of data
4596 * 2) truncate file to 64Kb
4597 * 3) truncate file to 256Kb
4599 * 5) <crash/power failure>
4600 * 6) mount fs and trigger log replay
4602 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4603 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4604 * file correspond to a hole. The presence of explicit holes in a log tree is
4605 * what guarantees that log replay will remove/adjust file extent items in the
4608 * Here we do not need to care about holes between extents, that is already done
4609 * by copy_items(). We also only need to do this in the full sync path, where we
4610 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4611 * lookup the list of modified extent maps and if any represents a hole, we
4612 * insert a corresponding extent representing a hole in the log tree.
4614 static int btrfs_log_trailing_hole(struct btrfs_trans_handle
*trans
,
4615 struct btrfs_root
*root
,
4616 struct btrfs_inode
*inode
,
4617 struct btrfs_path
*path
)
4619 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4621 struct btrfs_key key
;
4624 struct extent_buffer
*leaf
;
4625 struct btrfs_root
*log
= root
->log_root
;
4626 const u64 ino
= btrfs_ino(inode
);
4627 const u64 i_size
= i_size_read(&inode
->vfs_inode
);
4629 if (!btrfs_fs_incompat(fs_info
, NO_HOLES
))
4633 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4634 key
.offset
= (u64
)-1;
4636 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4641 ASSERT(path
->slots
[0] > 0);
4643 leaf
= path
->nodes
[0];
4644 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4646 if (key
.objectid
!= ino
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
4647 /* inode does not have any extents */
4651 struct btrfs_file_extent_item
*extent
;
4655 * If there's an extent beyond i_size, an explicit hole was
4656 * already inserted by copy_items().
4658 if (key
.offset
>= i_size
)
4661 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
4662 struct btrfs_file_extent_item
);
4664 if (btrfs_file_extent_type(leaf
, extent
) ==
4665 BTRFS_FILE_EXTENT_INLINE
) {
4666 len
= btrfs_file_extent_ram_bytes(leaf
, extent
);
4667 ASSERT(len
== i_size
||
4668 (len
== fs_info
->sectorsize
&&
4669 btrfs_file_extent_compression(leaf
, extent
) !=
4670 BTRFS_COMPRESS_NONE
) ||
4671 (len
< i_size
&& i_size
< fs_info
->sectorsize
));
4675 len
= btrfs_file_extent_num_bytes(leaf
, extent
);
4676 /* Last extent goes beyond i_size, no need to log a hole. */
4677 if (key
.offset
+ len
> i_size
)
4679 hole_start
= key
.offset
+ len
;
4680 hole_size
= i_size
- hole_start
;
4682 btrfs_release_path(path
);
4684 /* Last extent ends at i_size. */
4688 hole_size
= ALIGN(hole_size
, fs_info
->sectorsize
);
4689 ret
= btrfs_insert_file_extent(trans
, log
, ino
, hole_start
, 0, 0,
4690 hole_size
, 0, hole_size
, 0, 0, 0);
4695 * When we are logging a new inode X, check if it doesn't have a reference that
4696 * matches the reference from some other inode Y created in a past transaction
4697 * and that was renamed in the current transaction. If we don't do this, then at
4698 * log replay time we can lose inode Y (and all its files if it's a directory):
4701 * echo "hello world" > /mnt/x/foobar
4704 * mkdir /mnt/x # or touch /mnt/x
4705 * xfs_io -c fsync /mnt/x
4707 * mount fs, trigger log replay
4709 * After the log replay procedure, we would lose the first directory and all its
4710 * files (file foobar).
4711 * For the case where inode Y is not a directory we simply end up losing it:
4713 * echo "123" > /mnt/foo
4715 * mv /mnt/foo /mnt/bar
4716 * echo "abc" > /mnt/foo
4717 * xfs_io -c fsync /mnt/foo
4720 * We also need this for cases where a snapshot entry is replaced by some other
4721 * entry (file or directory) otherwise we end up with an unreplayable log due to
4722 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4723 * if it were a regular entry:
4726 * btrfs subvolume snapshot /mnt /mnt/x/snap
4727 * btrfs subvolume delete /mnt/x/snap
4730 * fsync /mnt/x or fsync some new file inside it
4733 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4734 * the same transaction.
4736 static int btrfs_check_ref_name_override(struct extent_buffer
*eb
,
4738 const struct btrfs_key
*key
,
4739 struct btrfs_inode
*inode
,
4743 struct btrfs_path
*search_path
;
4746 u32 item_size
= btrfs_item_size_nr(eb
, slot
);
4748 unsigned long ptr
= btrfs_item_ptr_offset(eb
, slot
);
4750 search_path
= btrfs_alloc_path();
4753 search_path
->search_commit_root
= 1;
4754 search_path
->skip_locking
= 1;
4756 while (cur_offset
< item_size
) {
4760 unsigned long name_ptr
;
4761 struct btrfs_dir_item
*di
;
4763 if (key
->type
== BTRFS_INODE_REF_KEY
) {
4764 struct btrfs_inode_ref
*iref
;
4766 iref
= (struct btrfs_inode_ref
*)(ptr
+ cur_offset
);
4767 parent
= key
->offset
;
4768 this_name_len
= btrfs_inode_ref_name_len(eb
, iref
);
4769 name_ptr
= (unsigned long)(iref
+ 1);
4770 this_len
= sizeof(*iref
) + this_name_len
;
4772 struct btrfs_inode_extref
*extref
;
4774 extref
= (struct btrfs_inode_extref
*)(ptr
+
4776 parent
= btrfs_inode_extref_parent(eb
, extref
);
4777 this_name_len
= btrfs_inode_extref_name_len(eb
, extref
);
4778 name_ptr
= (unsigned long)&extref
->name
;
4779 this_len
= sizeof(*extref
) + this_name_len
;
4782 if (this_name_len
> name_len
) {
4785 new_name
= krealloc(name
, this_name_len
, GFP_NOFS
);
4790 name_len
= this_name_len
;
4794 read_extent_buffer(eb
, name
, name_ptr
, this_name_len
);
4795 di
= btrfs_lookup_dir_item(NULL
, inode
->root
, search_path
,
4796 parent
, name
, this_name_len
, 0);
4797 if (di
&& !IS_ERR(di
)) {
4798 struct btrfs_key di_key
;
4800 btrfs_dir_item_key_to_cpu(search_path
->nodes
[0],
4802 if (di_key
.type
== BTRFS_INODE_ITEM_KEY
) {
4804 *other_ino
= di_key
.objectid
;
4809 } else if (IS_ERR(di
)) {
4813 btrfs_release_path(search_path
);
4815 cur_offset
+= this_len
;
4819 btrfs_free_path(search_path
);
4824 /* log a single inode in the tree log.
4825 * At least one parent directory for this inode must exist in the tree
4826 * or be logged already.
4828 * Any items from this inode changed by the current transaction are copied
4829 * to the log tree. An extra reference is taken on any extents in this
4830 * file, allowing us to avoid a whole pile of corner cases around logging
4831 * blocks that have been removed from the tree.
4833 * See LOG_INODE_ALL and related defines for a description of what inode_only
4836 * This handles both files and directories.
4838 static int btrfs_log_inode(struct btrfs_trans_handle
*trans
,
4839 struct btrfs_root
*root
, struct btrfs_inode
*inode
,
4843 struct btrfs_log_ctx
*ctx
)
4845 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4846 struct btrfs_path
*path
;
4847 struct btrfs_path
*dst_path
;
4848 struct btrfs_key min_key
;
4849 struct btrfs_key max_key
;
4850 struct btrfs_root
*log
= root
->log_root
;
4851 u64 last_extent
= 0;
4855 int ins_start_slot
= 0;
4857 bool fast_search
= false;
4858 u64 ino
= btrfs_ino(inode
);
4859 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
4860 u64 logged_isize
= 0;
4861 bool need_log_inode_item
= true;
4862 bool xattrs_logged
= false;
4864 path
= btrfs_alloc_path();
4867 dst_path
= btrfs_alloc_path();
4869 btrfs_free_path(path
);
4873 min_key
.objectid
= ino
;
4874 min_key
.type
= BTRFS_INODE_ITEM_KEY
;
4877 max_key
.objectid
= ino
;
4880 /* today the code can only do partial logging of directories */
4881 if (S_ISDIR(inode
->vfs_inode
.i_mode
) ||
4882 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4883 &inode
->runtime_flags
) &&
4884 inode_only
>= LOG_INODE_EXISTS
))
4885 max_key
.type
= BTRFS_XATTR_ITEM_KEY
;
4887 max_key
.type
= (u8
)-1;
4888 max_key
.offset
= (u64
)-1;
4891 * Only run delayed items if we are a dir or a new file.
4892 * Otherwise commit the delayed inode only, which is needed in
4893 * order for the log replay code to mark inodes for link count
4894 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4896 if (S_ISDIR(inode
->vfs_inode
.i_mode
) ||
4897 inode
->generation
> fs_info
->last_trans_committed
)
4898 ret
= btrfs_commit_inode_delayed_items(trans
, inode
);
4900 ret
= btrfs_commit_inode_delayed_inode(inode
);
4903 btrfs_free_path(path
);
4904 btrfs_free_path(dst_path
);
4908 if (inode_only
== LOG_OTHER_INODE
) {
4909 inode_only
= LOG_INODE_EXISTS
;
4910 mutex_lock_nested(&inode
->log_mutex
, SINGLE_DEPTH_NESTING
);
4912 mutex_lock(&inode
->log_mutex
);
4916 * a brute force approach to making sure we get the most uptodate
4917 * copies of everything.
4919 if (S_ISDIR(inode
->vfs_inode
.i_mode
)) {
4920 int max_key_type
= BTRFS_DIR_LOG_INDEX_KEY
;
4922 if (inode_only
== LOG_INODE_EXISTS
)
4923 max_key_type
= BTRFS_XATTR_ITEM_KEY
;
4924 ret
= drop_objectid_items(trans
, log
, path
, ino
, max_key_type
);
4926 if (inode_only
== LOG_INODE_EXISTS
) {
4928 * Make sure the new inode item we write to the log has
4929 * the same isize as the current one (if it exists).
4930 * This is necessary to prevent data loss after log
4931 * replay, and also to prevent doing a wrong expanding
4932 * truncate - for e.g. create file, write 4K into offset
4933 * 0, fsync, write 4K into offset 4096, add hard link,
4934 * fsync some other file (to sync log), power fail - if
4935 * we use the inode's current i_size, after log replay
4936 * we get a 8Kb file, with the last 4Kb extent as a hole
4937 * (zeroes), as if an expanding truncate happened,
4938 * instead of getting a file of 4Kb only.
4940 err
= logged_inode_size(log
, inode
, path
, &logged_isize
);
4944 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4945 &inode
->runtime_flags
)) {
4946 if (inode_only
== LOG_INODE_EXISTS
) {
4947 max_key
.type
= BTRFS_XATTR_ITEM_KEY
;
4948 ret
= drop_objectid_items(trans
, log
, path
, ino
,
4951 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4952 &inode
->runtime_flags
);
4953 clear_bit(BTRFS_INODE_COPY_EVERYTHING
,
4954 &inode
->runtime_flags
);
4956 ret
= btrfs_truncate_inode_items(trans
,
4957 log
, &inode
->vfs_inode
, 0, 0);
4962 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING
,
4963 &inode
->runtime_flags
) ||
4964 inode_only
== LOG_INODE_EXISTS
) {
4965 if (inode_only
== LOG_INODE_ALL
)
4967 max_key
.type
= BTRFS_XATTR_ITEM_KEY
;
4968 ret
= drop_objectid_items(trans
, log
, path
, ino
,
4971 if (inode_only
== LOG_INODE_ALL
)
4984 ret
= btrfs_search_forward(root
, &min_key
,
4985 path
, trans
->transid
);
4993 /* note, ins_nr might be > 0 here, cleanup outside the loop */
4994 if (min_key
.objectid
!= ino
)
4996 if (min_key
.type
> max_key
.type
)
4999 if (min_key
.type
== BTRFS_INODE_ITEM_KEY
)
5000 need_log_inode_item
= false;
5002 if ((min_key
.type
== BTRFS_INODE_REF_KEY
||
5003 min_key
.type
== BTRFS_INODE_EXTREF_KEY
) &&
5004 inode
->generation
== trans
->transid
) {
5007 ret
= btrfs_check_ref_name_override(path
->nodes
[0],
5008 path
->slots
[0], &min_key
, inode
,
5013 } else if (ret
> 0 && ctx
&&
5014 other_ino
!= btrfs_ino(BTRFS_I(ctx
->inode
))) {
5015 struct btrfs_key inode_key
;
5016 struct inode
*other_inode
;
5022 ins_start_slot
= path
->slots
[0];
5024 ret
= copy_items(trans
, inode
, dst_path
, path
,
5025 &last_extent
, ins_start_slot
,
5033 btrfs_release_path(path
);
5034 inode_key
.objectid
= other_ino
;
5035 inode_key
.type
= BTRFS_INODE_ITEM_KEY
;
5036 inode_key
.offset
= 0;
5037 other_inode
= btrfs_iget(fs_info
->sb
,
5041 * If the other inode that had a conflicting dir
5042 * entry was deleted in the current transaction,
5043 * we don't need to do more work nor fallback to
5044 * a transaction commit.
5046 if (other_inode
== ERR_PTR(-ENOENT
)) {
5048 } else if (IS_ERR(other_inode
)) {
5049 err
= PTR_ERR(other_inode
);
5053 * We are safe logging the other inode without
5054 * acquiring its i_mutex as long as we log with
5055 * the LOG_INODE_EXISTS mode. We're safe against
5056 * concurrent renames of the other inode as well
5057 * because during a rename we pin the log and
5058 * update the log with the new name before we
5061 err
= btrfs_log_inode(trans
, root
,
5062 BTRFS_I(other_inode
),
5063 LOG_OTHER_INODE
, 0, LLONG_MAX
,
5073 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5074 if (min_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
5077 ret
= copy_items(trans
, inode
, dst_path
, path
,
5078 &last_extent
, ins_start_slot
,
5079 ins_nr
, inode_only
, logged_isize
);
5086 btrfs_release_path(path
);
5092 if (ins_nr
&& ins_start_slot
+ ins_nr
== path
->slots
[0]) {
5095 } else if (!ins_nr
) {
5096 ins_start_slot
= path
->slots
[0];
5101 ret
= copy_items(trans
, inode
, dst_path
, path
, &last_extent
,
5102 ins_start_slot
, ins_nr
, inode_only
,
5110 btrfs_release_path(path
);
5114 ins_start_slot
= path
->slots
[0];
5117 nritems
= btrfs_header_nritems(path
->nodes
[0]);
5119 if (path
->slots
[0] < nritems
) {
5120 btrfs_item_key_to_cpu(path
->nodes
[0], &min_key
,
5125 ret
= copy_items(trans
, inode
, dst_path
, path
,
5126 &last_extent
, ins_start_slot
,
5127 ins_nr
, inode_only
, logged_isize
);
5135 btrfs_release_path(path
);
5137 if (min_key
.offset
< (u64
)-1) {
5139 } else if (min_key
.type
< max_key
.type
) {
5147 ret
= copy_items(trans
, inode
, dst_path
, path
, &last_extent
,
5148 ins_start_slot
, ins_nr
, inode_only
,
5158 btrfs_release_path(path
);
5159 btrfs_release_path(dst_path
);
5160 err
= btrfs_log_all_xattrs(trans
, root
, inode
, path
, dst_path
);
5163 xattrs_logged
= true;
5164 if (max_key
.type
>= BTRFS_EXTENT_DATA_KEY
&& !fast_search
) {
5165 btrfs_release_path(path
);
5166 btrfs_release_path(dst_path
);
5167 err
= btrfs_log_trailing_hole(trans
, root
, inode
, path
);
5172 btrfs_release_path(path
);
5173 btrfs_release_path(dst_path
);
5174 if (need_log_inode_item
) {
5175 err
= log_inode_item(trans
, log
, dst_path
, inode
);
5176 if (!err
&& !xattrs_logged
) {
5177 err
= btrfs_log_all_xattrs(trans
, root
, inode
, path
,
5179 btrfs_release_path(path
);
5185 ret
= btrfs_log_changed_extents(trans
, root
, inode
, dst_path
,
5191 } else if (inode_only
== LOG_INODE_ALL
) {
5192 struct extent_map
*em
, *n
;
5194 write_lock(&em_tree
->lock
);
5196 * We can't just remove every em if we're called for a ranged
5197 * fsync - that is, one that doesn't cover the whole possible
5198 * file range (0 to LLONG_MAX). This is because we can have
5199 * em's that fall outside the range we're logging and therefore
5200 * their ordered operations haven't completed yet
5201 * (btrfs_finish_ordered_io() not invoked yet). This means we
5202 * didn't get their respective file extent item in the fs/subvol
5203 * tree yet, and need to let the next fast fsync (one which
5204 * consults the list of modified extent maps) find the em so
5205 * that it logs a matching file extent item and waits for the
5206 * respective ordered operation to complete (if it's still
5209 * Removing every em outside the range we're logging would make
5210 * the next fast fsync not log their matching file extent items,
5211 * therefore making us lose data after a log replay.
5213 list_for_each_entry_safe(em
, n
, &em_tree
->modified_extents
,
5215 const u64 mod_end
= em
->mod_start
+ em
->mod_len
- 1;
5217 if (em
->mod_start
>= start
&& mod_end
<= end
)
5218 list_del_init(&em
->list
);
5220 write_unlock(&em_tree
->lock
);
5223 if (inode_only
== LOG_INODE_ALL
&& S_ISDIR(inode
->vfs_inode
.i_mode
)) {
5224 ret
= log_directory_changes(trans
, root
, inode
, path
, dst_path
,
5232 spin_lock(&inode
->lock
);
5233 inode
->logged_trans
= trans
->transid
;
5234 inode
->last_log_commit
= inode
->last_sub_trans
;
5235 spin_unlock(&inode
->lock
);
5237 mutex_unlock(&inode
->log_mutex
);
5239 btrfs_free_path(path
);
5240 btrfs_free_path(dst_path
);
5245 * Check if we must fallback to a transaction commit when logging an inode.
5246 * This must be called after logging the inode and is used only in the context
5247 * when fsyncing an inode requires the need to log some other inode - in which
5248 * case we can't lock the i_mutex of each other inode we need to log as that
5249 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5250 * log inodes up or down in the hierarchy) or rename operations for example. So
5251 * we take the log_mutex of the inode after we have logged it and then check for
5252 * its last_unlink_trans value - this is safe because any task setting
5253 * last_unlink_trans must take the log_mutex and it must do this before it does
5254 * the actual unlink operation, so if we do this check before a concurrent task
5255 * sets last_unlink_trans it means we've logged a consistent version/state of
5256 * all the inode items, otherwise we are not sure and must do a transaction
5257 * commit (the concurrent task might have only updated last_unlink_trans before
5258 * we logged the inode or it might have also done the unlink).
5260 static bool btrfs_must_commit_transaction(struct btrfs_trans_handle
*trans
,
5261 struct btrfs_inode
*inode
)
5263 struct btrfs_fs_info
*fs_info
= inode
->root
->fs_info
;
5266 mutex_lock(&inode
->log_mutex
);
5267 if (inode
->last_unlink_trans
> fs_info
->last_trans_committed
) {
5269 * Make sure any commits to the log are forced to be full
5272 btrfs_set_log_full_commit(fs_info
, trans
);
5275 mutex_unlock(&inode
->log_mutex
);
5281 * follow the dentry parent pointers up the chain and see if any
5282 * of the directories in it require a full commit before they can
5283 * be logged. Returns zero if nothing special needs to be done or 1 if
5284 * a full commit is required.
5286 static noinline
int check_parent_dirs_for_sync(struct btrfs_trans_handle
*trans
,
5287 struct btrfs_inode
*inode
,
5288 struct dentry
*parent
,
5289 struct super_block
*sb
,
5293 struct dentry
*old_parent
= NULL
;
5294 struct btrfs_inode
*orig_inode
= inode
;
5297 * for regular files, if its inode is already on disk, we don't
5298 * have to worry about the parents at all. This is because
5299 * we can use the last_unlink_trans field to record renames
5300 * and other fun in this file.
5302 if (S_ISREG(inode
->vfs_inode
.i_mode
) &&
5303 inode
->generation
<= last_committed
&&
5304 inode
->last_unlink_trans
<= last_committed
)
5307 if (!S_ISDIR(inode
->vfs_inode
.i_mode
)) {
5308 if (!parent
|| d_really_is_negative(parent
) || sb
!= parent
->d_sb
)
5310 inode
= BTRFS_I(d_inode(parent
));
5315 * If we are logging a directory then we start with our inode,
5316 * not our parent's inode, so we need to skip setting the
5317 * logged_trans so that further down in the log code we don't
5318 * think this inode has already been logged.
5320 if (inode
!= orig_inode
)
5321 inode
->logged_trans
= trans
->transid
;
5324 if (btrfs_must_commit_transaction(trans
, inode
)) {
5329 if (!parent
|| d_really_is_negative(parent
) || sb
!= parent
->d_sb
)
5332 if (IS_ROOT(parent
)) {
5333 inode
= BTRFS_I(d_inode(parent
));
5334 if (btrfs_must_commit_transaction(trans
, inode
))
5339 parent
= dget_parent(parent
);
5341 old_parent
= parent
;
5342 inode
= BTRFS_I(d_inode(parent
));
5350 struct btrfs_dir_list
{
5352 struct list_head list
;
5356 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5357 * details about the why it is needed.
5358 * This is a recursive operation - if an existing dentry corresponds to a
5359 * directory, that directory's new entries are logged too (same behaviour as
5360 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5361 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5362 * complains about the following circular lock dependency / possible deadlock:
5366 * lock(&type->i_mutex_dir_key#3/2);
5367 * lock(sb_internal#2);
5368 * lock(&type->i_mutex_dir_key#3/2);
5369 * lock(&sb->s_type->i_mutex_key#14);
5371 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5372 * sb_start_intwrite() in btrfs_start_transaction().
5373 * Not locking i_mutex of the inodes is still safe because:
5375 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5376 * that while logging the inode new references (names) are added or removed
5377 * from the inode, leaving the logged inode item with a link count that does
5378 * not match the number of logged inode reference items. This is fine because
5379 * at log replay time we compute the real number of links and correct the
5380 * link count in the inode item (see replay_one_buffer() and
5381 * link_to_fixup_dir());
5383 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5384 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5385 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5386 * has a size that doesn't match the sum of the lengths of all the logged
5387 * names. This does not result in a problem because if a dir_item key is
5388 * logged but its matching dir_index key is not logged, at log replay time we
5389 * don't use it to replay the respective name (see replay_one_name()). On the
5390 * other hand if only the dir_index key ends up being logged, the respective
5391 * name is added to the fs/subvol tree with both the dir_item and dir_index
5392 * keys created (see replay_one_name()).
5393 * The directory's inode item with a wrong i_size is not a problem as well,
5394 * since we don't use it at log replay time to set the i_size in the inode
5395 * item of the fs/subvol tree (see overwrite_item()).
5397 static int log_new_dir_dentries(struct btrfs_trans_handle
*trans
,
5398 struct btrfs_root
*root
,
5399 struct btrfs_inode
*start_inode
,
5400 struct btrfs_log_ctx
*ctx
)
5402 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5403 struct btrfs_root
*log
= root
->log_root
;
5404 struct btrfs_path
*path
;
5405 LIST_HEAD(dir_list
);
5406 struct btrfs_dir_list
*dir_elem
;
5409 path
= btrfs_alloc_path();
5413 dir_elem
= kmalloc(sizeof(*dir_elem
), GFP_NOFS
);
5415 btrfs_free_path(path
);
5418 dir_elem
->ino
= btrfs_ino(start_inode
);
5419 list_add_tail(&dir_elem
->list
, &dir_list
);
5421 while (!list_empty(&dir_list
)) {
5422 struct extent_buffer
*leaf
;
5423 struct btrfs_key min_key
;
5427 dir_elem
= list_first_entry(&dir_list
, struct btrfs_dir_list
,
5430 goto next_dir_inode
;
5432 min_key
.objectid
= dir_elem
->ino
;
5433 min_key
.type
= BTRFS_DIR_ITEM_KEY
;
5436 btrfs_release_path(path
);
5437 ret
= btrfs_search_forward(log
, &min_key
, path
, trans
->transid
);
5439 goto next_dir_inode
;
5440 } else if (ret
> 0) {
5442 goto next_dir_inode
;
5446 leaf
= path
->nodes
[0];
5447 nritems
= btrfs_header_nritems(leaf
);
5448 for (i
= path
->slots
[0]; i
< nritems
; i
++) {
5449 struct btrfs_dir_item
*di
;
5450 struct btrfs_key di_key
;
5451 struct inode
*di_inode
;
5452 struct btrfs_dir_list
*new_dir_elem
;
5453 int log_mode
= LOG_INODE_EXISTS
;
5456 btrfs_item_key_to_cpu(leaf
, &min_key
, i
);
5457 if (min_key
.objectid
!= dir_elem
->ino
||
5458 min_key
.type
!= BTRFS_DIR_ITEM_KEY
)
5459 goto next_dir_inode
;
5461 di
= btrfs_item_ptr(leaf
, i
, struct btrfs_dir_item
);
5462 type
= btrfs_dir_type(leaf
, di
);
5463 if (btrfs_dir_transid(leaf
, di
) < trans
->transid
&&
5464 type
!= BTRFS_FT_DIR
)
5466 btrfs_dir_item_key_to_cpu(leaf
, di
, &di_key
);
5467 if (di_key
.type
== BTRFS_ROOT_ITEM_KEY
)
5470 btrfs_release_path(path
);
5471 di_inode
= btrfs_iget(fs_info
->sb
, &di_key
, root
, NULL
);
5472 if (IS_ERR(di_inode
)) {
5473 ret
= PTR_ERR(di_inode
);
5474 goto next_dir_inode
;
5477 if (btrfs_inode_in_log(BTRFS_I(di_inode
), trans
->transid
)) {
5482 ctx
->log_new_dentries
= false;
5483 if (type
== BTRFS_FT_DIR
|| type
== BTRFS_FT_SYMLINK
)
5484 log_mode
= LOG_INODE_ALL
;
5485 ret
= btrfs_log_inode(trans
, root
, BTRFS_I(di_inode
),
5486 log_mode
, 0, LLONG_MAX
, ctx
);
5488 btrfs_must_commit_transaction(trans
, BTRFS_I(di_inode
)))
5492 goto next_dir_inode
;
5493 if (ctx
->log_new_dentries
) {
5494 new_dir_elem
= kmalloc(sizeof(*new_dir_elem
),
5496 if (!new_dir_elem
) {
5498 goto next_dir_inode
;
5500 new_dir_elem
->ino
= di_key
.objectid
;
5501 list_add_tail(&new_dir_elem
->list
, &dir_list
);
5506 ret
= btrfs_next_leaf(log
, path
);
5508 goto next_dir_inode
;
5509 } else if (ret
> 0) {
5511 goto next_dir_inode
;
5515 if (min_key
.offset
< (u64
)-1) {
5520 list_del(&dir_elem
->list
);
5524 btrfs_free_path(path
);
5528 static int btrfs_log_all_parents(struct btrfs_trans_handle
*trans
,
5529 struct btrfs_inode
*inode
,
5530 struct btrfs_log_ctx
*ctx
)
5532 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
5534 struct btrfs_path
*path
;
5535 struct btrfs_key key
;
5536 struct btrfs_root
*root
= inode
->root
;
5537 const u64 ino
= btrfs_ino(inode
);
5539 path
= btrfs_alloc_path();
5542 path
->skip_locking
= 1;
5543 path
->search_commit_root
= 1;
5546 key
.type
= BTRFS_INODE_REF_KEY
;
5548 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5553 struct extent_buffer
*leaf
= path
->nodes
[0];
5554 int slot
= path
->slots
[0];
5559 if (slot
>= btrfs_header_nritems(leaf
)) {
5560 ret
= btrfs_next_leaf(root
, path
);
5568 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5569 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5570 if (key
.objectid
!= ino
|| key
.type
> BTRFS_INODE_EXTREF_KEY
)
5573 item_size
= btrfs_item_size_nr(leaf
, slot
);
5574 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
5575 while (cur_offset
< item_size
) {
5576 struct btrfs_key inode_key
;
5577 struct inode
*dir_inode
;
5579 inode_key
.type
= BTRFS_INODE_ITEM_KEY
;
5580 inode_key
.offset
= 0;
5582 if (key
.type
== BTRFS_INODE_EXTREF_KEY
) {
5583 struct btrfs_inode_extref
*extref
;
5585 extref
= (struct btrfs_inode_extref
*)
5587 inode_key
.objectid
= btrfs_inode_extref_parent(
5589 cur_offset
+= sizeof(*extref
);
5590 cur_offset
+= btrfs_inode_extref_name_len(leaf
,
5593 inode_key
.objectid
= key
.offset
;
5594 cur_offset
= item_size
;
5597 dir_inode
= btrfs_iget(fs_info
->sb
, &inode_key
,
5600 * If the parent inode was deleted, return an error to
5601 * fallback to a transaction commit. This is to prevent
5602 * getting an inode that was moved from one parent A to
5603 * a parent B, got its former parent A deleted and then
5604 * it got fsync'ed, from existing at both parents after
5605 * a log replay (and the old parent still existing).
5612 * mv /mnt/B/bar /mnt/A/bar
5613 * mv -T /mnt/A /mnt/B
5617 * If we ignore the old parent B which got deleted,
5618 * after a log replay we would have file bar linked
5619 * at both parents and the old parent B would still
5622 if (IS_ERR(dir_inode
)) {
5623 ret
= PTR_ERR(dir_inode
);
5628 ctx
->log_new_dentries
= false;
5629 ret
= btrfs_log_inode(trans
, root
, BTRFS_I(dir_inode
),
5630 LOG_INODE_ALL
, 0, LLONG_MAX
, ctx
);
5632 btrfs_must_commit_transaction(trans
, BTRFS_I(dir_inode
)))
5634 if (!ret
&& ctx
&& ctx
->log_new_dentries
)
5635 ret
= log_new_dir_dentries(trans
, root
,
5636 BTRFS_I(dir_inode
), ctx
);
5645 btrfs_free_path(path
);
5650 * helper function around btrfs_log_inode to make sure newly created
5651 * parent directories also end up in the log. A minimal inode and backref
5652 * only logging is done of any parent directories that are older than
5653 * the last committed transaction
5655 static int btrfs_log_inode_parent(struct btrfs_trans_handle
*trans
,
5656 struct btrfs_inode
*inode
,
5657 struct dentry
*parent
,
5661 struct btrfs_log_ctx
*ctx
)
5663 struct btrfs_root
*root
= inode
->root
;
5664 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5665 struct super_block
*sb
;
5666 struct dentry
*old_parent
= NULL
;
5668 u64 last_committed
= fs_info
->last_trans_committed
;
5669 bool log_dentries
= false;
5670 struct btrfs_inode
*orig_inode
= inode
;
5672 sb
= inode
->vfs_inode
.i_sb
;
5674 if (btrfs_test_opt(fs_info
, NOTREELOG
)) {
5680 * The prev transaction commit doesn't complete, we need do
5681 * full commit by ourselves.
5683 if (fs_info
->last_trans_log_full_commit
>
5684 fs_info
->last_trans_committed
) {
5689 if (btrfs_root_refs(&root
->root_item
) == 0) {
5694 ret
= check_parent_dirs_for_sync(trans
, inode
, parent
, sb
,
5700 * Skip already logged inodes or inodes corresponding to tmpfiles
5701 * (since logging them is pointless, a link count of 0 means they
5702 * will never be accessible).
5704 if (btrfs_inode_in_log(inode
, trans
->transid
) ||
5705 inode
->vfs_inode
.i_nlink
== 0) {
5706 ret
= BTRFS_NO_LOG_SYNC
;
5710 ret
= start_log_trans(trans
, root
, ctx
);
5714 ret
= btrfs_log_inode(trans
, root
, inode
, inode_only
, start
, end
, ctx
);
5719 * for regular files, if its inode is already on disk, we don't
5720 * have to worry about the parents at all. This is because
5721 * we can use the last_unlink_trans field to record renames
5722 * and other fun in this file.
5724 if (S_ISREG(inode
->vfs_inode
.i_mode
) &&
5725 inode
->generation
<= last_committed
&&
5726 inode
->last_unlink_trans
<= last_committed
) {
5731 if (S_ISDIR(inode
->vfs_inode
.i_mode
) && ctx
&& ctx
->log_new_dentries
)
5732 log_dentries
= true;
5735 * On unlink we must make sure all our current and old parent directory
5736 * inodes are fully logged. This is to prevent leaving dangling
5737 * directory index entries in directories that were our parents but are
5738 * not anymore. Not doing this results in old parent directory being
5739 * impossible to delete after log replay (rmdir will always fail with
5740 * error -ENOTEMPTY).
5746 * ln testdir/foo testdir/bar
5748 * unlink testdir/bar
5749 * xfs_io -c fsync testdir/foo
5751 * mount fs, triggers log replay
5753 * If we don't log the parent directory (testdir), after log replay the
5754 * directory still has an entry pointing to the file inode using the bar
5755 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5756 * the file inode has a link count of 1.
5762 * ln foo testdir/foo2
5763 * ln foo testdir/foo3
5765 * unlink testdir/foo3
5766 * xfs_io -c fsync foo
5768 * mount fs, triggers log replay
5770 * Similar as the first example, after log replay the parent directory
5771 * testdir still has an entry pointing to the inode file with name foo3
5772 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5773 * and has a link count of 2.
5775 if (inode
->last_unlink_trans
> last_committed
) {
5776 ret
= btrfs_log_all_parents(trans
, orig_inode
, ctx
);
5782 if (!parent
|| d_really_is_negative(parent
) || sb
!= parent
->d_sb
)
5785 inode
= BTRFS_I(d_inode(parent
));
5786 if (root
!= inode
->root
)
5789 if (inode
->generation
> last_committed
) {
5790 ret
= btrfs_log_inode(trans
, root
, inode
,
5791 LOG_INODE_EXISTS
, 0, LLONG_MAX
, ctx
);
5795 if (IS_ROOT(parent
))
5798 parent
= dget_parent(parent
);
5800 old_parent
= parent
;
5803 ret
= log_new_dir_dentries(trans
, root
, orig_inode
, ctx
);
5809 btrfs_set_log_full_commit(fs_info
, trans
);
5814 btrfs_remove_log_ctx(root
, ctx
);
5815 btrfs_end_log_trans(root
);
5821 * it is not safe to log dentry if the chunk root has added new
5822 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5823 * If this returns 1, you must commit the transaction to safely get your
5826 int btrfs_log_dentry_safe(struct btrfs_trans_handle
*trans
,
5827 struct dentry
*dentry
,
5830 struct btrfs_log_ctx
*ctx
)
5832 struct dentry
*parent
= dget_parent(dentry
);
5835 ret
= btrfs_log_inode_parent(trans
, BTRFS_I(d_inode(dentry
)), parent
,
5836 start
, end
, LOG_INODE_ALL
, ctx
);
5843 * should be called during mount to recover any replay any log trees
5846 int btrfs_recover_log_trees(struct btrfs_root
*log_root_tree
)
5849 struct btrfs_path
*path
;
5850 struct btrfs_trans_handle
*trans
;
5851 struct btrfs_key key
;
5852 struct btrfs_key found_key
;
5853 struct btrfs_key tmp_key
;
5854 struct btrfs_root
*log
;
5855 struct btrfs_fs_info
*fs_info
= log_root_tree
->fs_info
;
5856 struct walk_control wc
= {
5857 .process_func
= process_one_buffer
,
5861 path
= btrfs_alloc_path();
5865 set_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
);
5867 trans
= btrfs_start_transaction(fs_info
->tree_root
, 0);
5868 if (IS_ERR(trans
)) {
5869 ret
= PTR_ERR(trans
);
5876 ret
= walk_log_tree(trans
, log_root_tree
, &wc
);
5878 btrfs_handle_fs_error(fs_info
, ret
,
5879 "Failed to pin buffers while recovering log root tree.");
5884 key
.objectid
= BTRFS_TREE_LOG_OBJECTID
;
5885 key
.offset
= (u64
)-1;
5886 key
.type
= BTRFS_ROOT_ITEM_KEY
;
5889 ret
= btrfs_search_slot(NULL
, log_root_tree
, &key
, path
, 0, 0);
5892 btrfs_handle_fs_error(fs_info
, ret
,
5893 "Couldn't find tree log root.");
5897 if (path
->slots
[0] == 0)
5901 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
5903 btrfs_release_path(path
);
5904 if (found_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
5907 log
= btrfs_read_fs_root(log_root_tree
, &found_key
);
5910 btrfs_handle_fs_error(fs_info
, ret
,
5911 "Couldn't read tree log root.");
5915 tmp_key
.objectid
= found_key
.offset
;
5916 tmp_key
.type
= BTRFS_ROOT_ITEM_KEY
;
5917 tmp_key
.offset
= (u64
)-1;
5919 wc
.replay_dest
= btrfs_read_fs_root_no_name(fs_info
, &tmp_key
);
5920 if (IS_ERR(wc
.replay_dest
)) {
5921 ret
= PTR_ERR(wc
.replay_dest
);
5922 free_extent_buffer(log
->node
);
5923 free_extent_buffer(log
->commit_root
);
5925 btrfs_handle_fs_error(fs_info
, ret
,
5926 "Couldn't read target root for tree log recovery.");
5930 wc
.replay_dest
->log_root
= log
;
5931 btrfs_record_root_in_trans(trans
, wc
.replay_dest
);
5932 ret
= walk_log_tree(trans
, log
, &wc
);
5934 if (!ret
&& wc
.stage
== LOG_WALK_REPLAY_ALL
) {
5935 ret
= fixup_inode_link_counts(trans
, wc
.replay_dest
,
5939 if (!ret
&& wc
.stage
== LOG_WALK_REPLAY_ALL
) {
5940 struct btrfs_root
*root
= wc
.replay_dest
;
5942 btrfs_release_path(path
);
5945 * We have just replayed everything, and the highest
5946 * objectid of fs roots probably has changed in case
5947 * some inode_item's got replayed.
5949 * root->objectid_mutex is not acquired as log replay
5950 * could only happen during mount.
5952 ret
= btrfs_find_highest_objectid(root
,
5953 &root
->highest_objectid
);
5956 key
.offset
= found_key
.offset
- 1;
5957 wc
.replay_dest
->log_root
= NULL
;
5958 free_extent_buffer(log
->node
);
5959 free_extent_buffer(log
->commit_root
);
5965 if (found_key
.offset
== 0)
5968 btrfs_release_path(path
);
5970 /* step one is to pin it all, step two is to replay just inodes */
5973 wc
.process_func
= replay_one_buffer
;
5974 wc
.stage
= LOG_WALK_REPLAY_INODES
;
5977 /* step three is to replay everything */
5978 if (wc
.stage
< LOG_WALK_REPLAY_ALL
) {
5983 btrfs_free_path(path
);
5985 /* step 4: commit the transaction, which also unpins the blocks */
5986 ret
= btrfs_commit_transaction(trans
);
5990 free_extent_buffer(log_root_tree
->node
);
5991 log_root_tree
->log_root
= NULL
;
5992 clear_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
);
5993 kfree(log_root_tree
);
5998 btrfs_end_transaction(wc
.trans
);
5999 btrfs_free_path(path
);
6004 * there are some corner cases where we want to force a full
6005 * commit instead of allowing a directory to be logged.
6007 * They revolve around files there were unlinked from the directory, and
6008 * this function updates the parent directory so that a full commit is
6009 * properly done if it is fsync'd later after the unlinks are done.
6011 * Must be called before the unlink operations (updates to the subvolume tree,
6012 * inodes, etc) are done.
6014 void btrfs_record_unlink_dir(struct btrfs_trans_handle
*trans
,
6015 struct btrfs_inode
*dir
, struct btrfs_inode
*inode
,
6019 * when we're logging a file, if it hasn't been renamed
6020 * or unlinked, and its inode is fully committed on disk,
6021 * we don't have to worry about walking up the directory chain
6022 * to log its parents.
6024 * So, we use the last_unlink_trans field to put this transid
6025 * into the file. When the file is logged we check it and
6026 * don't log the parents if the file is fully on disk.
6028 mutex_lock(&inode
->log_mutex
);
6029 inode
->last_unlink_trans
= trans
->transid
;
6030 mutex_unlock(&inode
->log_mutex
);
6033 * if this directory was already logged any new
6034 * names for this file/dir will get recorded
6037 if (dir
->logged_trans
== trans
->transid
)
6041 * if the inode we're about to unlink was logged,
6042 * the log will be properly updated for any new names
6044 if (inode
->logged_trans
== trans
->transid
)
6048 * when renaming files across directories, if the directory
6049 * there we're unlinking from gets fsync'd later on, there's
6050 * no way to find the destination directory later and fsync it
6051 * properly. So, we have to be conservative and force commits
6052 * so the new name gets discovered.
6057 /* we can safely do the unlink without any special recording */
6061 mutex_lock(&dir
->log_mutex
);
6062 dir
->last_unlink_trans
= trans
->transid
;
6063 mutex_unlock(&dir
->log_mutex
);
6067 * Make sure that if someone attempts to fsync the parent directory of a deleted
6068 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6069 * that after replaying the log tree of the parent directory's root we will not
6070 * see the snapshot anymore and at log replay time we will not see any log tree
6071 * corresponding to the deleted snapshot's root, which could lead to replaying
6072 * it after replaying the log tree of the parent directory (which would replay
6073 * the snapshot delete operation).
6075 * Must be called before the actual snapshot destroy operation (updates to the
6076 * parent root and tree of tree roots trees, etc) are done.
6078 void btrfs_record_snapshot_destroy(struct btrfs_trans_handle
*trans
,
6079 struct btrfs_inode
*dir
)
6081 mutex_lock(&dir
->log_mutex
);
6082 dir
->last_unlink_trans
= trans
->transid
;
6083 mutex_unlock(&dir
->log_mutex
);
6087 * Call this after adding a new name for a file and it will properly
6088 * update the log to reflect the new name.
6090 * @ctx can not be NULL when @sync_log is false, and should be NULL when it's
6091 * true (because it's not used).
6093 * Return value depends on whether @sync_log is true or false.
6094 * When true: returns BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6095 * committed by the caller, and BTRFS_DONT_NEED_TRANS_COMMIT
6097 * When false: returns BTRFS_DONT_NEED_LOG_SYNC if the caller does not need to
6098 * to sync the log, BTRFS_NEED_LOG_SYNC if it needs to sync the log,
6099 * or BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
6100 * committed (without attempting to sync the log).
6102 int btrfs_log_new_name(struct btrfs_trans_handle
*trans
,
6103 struct btrfs_inode
*inode
, struct btrfs_inode
*old_dir
,
6104 struct dentry
*parent
,
6105 bool sync_log
, struct btrfs_log_ctx
*ctx
)
6107 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
6111 * this will force the logging code to walk the dentry chain
6114 if (!S_ISDIR(inode
->vfs_inode
.i_mode
))
6115 inode
->last_unlink_trans
= trans
->transid
;
6118 * if this inode hasn't been logged and directory we're renaming it
6119 * from hasn't been logged, we don't need to log it
6121 if (inode
->logged_trans
<= fs_info
->last_trans_committed
&&
6122 (!old_dir
|| old_dir
->logged_trans
<= fs_info
->last_trans_committed
))
6123 return sync_log
? BTRFS_DONT_NEED_TRANS_COMMIT
:
6124 BTRFS_DONT_NEED_LOG_SYNC
;
6127 struct btrfs_log_ctx ctx2
;
6129 btrfs_init_log_ctx(&ctx2
, &inode
->vfs_inode
);
6130 ret
= btrfs_log_inode_parent(trans
, inode
, parent
, 0, LLONG_MAX
,
6131 LOG_INODE_EXISTS
, &ctx2
);
6132 if (ret
== BTRFS_NO_LOG_SYNC
)
6133 return BTRFS_DONT_NEED_TRANS_COMMIT
;
6135 return BTRFS_NEED_TRANS_COMMIT
;
6137 ret
= btrfs_sync_log(trans
, inode
->root
, &ctx2
);
6139 return BTRFS_NEED_TRANS_COMMIT
;
6140 return BTRFS_DONT_NEED_TRANS_COMMIT
;
6144 ret
= btrfs_log_inode_parent(trans
, inode
, parent
, 0, LLONG_MAX
,
6145 LOG_INODE_EXISTS
, ctx
);
6146 if (ret
== BTRFS_NO_LOG_SYNC
)
6147 return BTRFS_DONT_NEED_LOG_SYNC
;
6149 return BTRFS_NEED_TRANS_COMMIT
;
6151 return BTRFS_NEED_LOG_SYNC
;