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 int btrfs_pin_log_trans(struct btrfs_root
*root
)
212 mutex_lock(&root
->log_mutex
);
213 atomic_inc(&root
->log_writers
);
214 mutex_unlock(&root
->log_mutex
);
219 * indicate we're done making changes to the log tree
220 * and wake up anyone waiting to do a sync
222 void btrfs_end_log_trans(struct btrfs_root
*root
)
224 if (atomic_dec_and_test(&root
->log_writers
)) {
225 /* atomic_dec_and_test implies a barrier */
226 cond_wake_up_nomb(&root
->log_writer_wait
);
232 * the walk control struct is used to pass state down the chain when
233 * processing the log tree. The stage field tells us which part
234 * of the log tree processing we are currently doing. The others
235 * are state fields used for that specific part
237 struct walk_control
{
238 /* should we free the extent on disk when done? This is used
239 * at transaction commit time while freeing a log tree
243 /* should we write out the extent buffer? This is used
244 * while flushing the log tree to disk during a sync
248 /* should we wait for the extent buffer io to finish? Also used
249 * while flushing the log tree to disk for a sync
253 /* pin only walk, we record which extents on disk belong to the
258 /* what stage of the replay code we're currently in */
261 /* the root we are currently replaying */
262 struct btrfs_root
*replay_dest
;
264 /* the trans handle for the current replay */
265 struct btrfs_trans_handle
*trans
;
267 /* the function that gets used to process blocks we find in the
268 * tree. Note the extent_buffer might not be up to date when it is
269 * passed in, and it must be checked or read if you need the data
272 int (*process_func
)(struct btrfs_root
*log
, struct extent_buffer
*eb
,
273 struct walk_control
*wc
, u64 gen
, int level
);
277 * process_func used to pin down extents, write them or wait on them
279 static int process_one_buffer(struct btrfs_root
*log
,
280 struct extent_buffer
*eb
,
281 struct walk_control
*wc
, u64 gen
, int level
)
283 struct btrfs_fs_info
*fs_info
= log
->fs_info
;
287 * If this fs is mixed then we need to be able to process the leaves to
288 * pin down any logged extents, so we have to read the block.
290 if (btrfs_fs_incompat(fs_info
, MIXED_GROUPS
)) {
291 ret
= btrfs_read_buffer(eb
, gen
, level
, NULL
);
297 ret
= btrfs_pin_extent_for_log_replay(fs_info
, eb
->start
,
300 if (!ret
&& btrfs_buffer_uptodate(eb
, gen
, 0)) {
301 if (wc
->pin
&& btrfs_header_level(eb
) == 0)
302 ret
= btrfs_exclude_logged_extents(fs_info
, eb
);
304 btrfs_write_tree_block(eb
);
306 btrfs_wait_tree_block_writeback(eb
);
312 * Item overwrite used by replay and tree logging. eb, slot and key all refer
313 * to the src data we are copying out.
315 * root is the tree we are copying into, and path is a scratch
316 * path for use in this function (it should be released on entry and
317 * will be released on exit).
319 * If the key is already in the destination tree the existing item is
320 * overwritten. If the existing item isn't big enough, it is extended.
321 * If it is too large, it is truncated.
323 * If the key isn't in the destination yet, a new item is inserted.
325 static noinline
int overwrite_item(struct btrfs_trans_handle
*trans
,
326 struct btrfs_root
*root
,
327 struct btrfs_path
*path
,
328 struct extent_buffer
*eb
, int slot
,
329 struct btrfs_key
*key
)
331 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
334 u64 saved_i_size
= 0;
335 int save_old_i_size
= 0;
336 unsigned long src_ptr
;
337 unsigned long dst_ptr
;
338 int overwrite_root
= 0;
339 bool inode_item
= key
->type
== BTRFS_INODE_ITEM_KEY
;
341 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
344 item_size
= btrfs_item_size_nr(eb
, slot
);
345 src_ptr
= btrfs_item_ptr_offset(eb
, slot
);
347 /* look for the key in the destination tree */
348 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
355 u32 dst_size
= btrfs_item_size_nr(path
->nodes
[0],
357 if (dst_size
!= item_size
)
360 if (item_size
== 0) {
361 btrfs_release_path(path
);
364 dst_copy
= kmalloc(item_size
, GFP_NOFS
);
365 src_copy
= kmalloc(item_size
, GFP_NOFS
);
366 if (!dst_copy
|| !src_copy
) {
367 btrfs_release_path(path
);
373 read_extent_buffer(eb
, src_copy
, src_ptr
, item_size
);
375 dst_ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
376 read_extent_buffer(path
->nodes
[0], dst_copy
, dst_ptr
,
378 ret
= memcmp(dst_copy
, src_copy
, item_size
);
383 * they have the same contents, just return, this saves
384 * us from cowing blocks in the destination tree and doing
385 * extra writes that may not have been done by a previous
389 btrfs_release_path(path
);
394 * We need to load the old nbytes into the inode so when we
395 * replay the extents we've logged we get the right nbytes.
398 struct btrfs_inode_item
*item
;
402 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
403 struct btrfs_inode_item
);
404 nbytes
= btrfs_inode_nbytes(path
->nodes
[0], item
);
405 item
= btrfs_item_ptr(eb
, slot
,
406 struct btrfs_inode_item
);
407 btrfs_set_inode_nbytes(eb
, item
, nbytes
);
410 * If this is a directory we need to reset the i_size to
411 * 0 so that we can set it up properly when replaying
412 * the rest of the items in this log.
414 mode
= btrfs_inode_mode(eb
, item
);
416 btrfs_set_inode_size(eb
, item
, 0);
418 } else if (inode_item
) {
419 struct btrfs_inode_item
*item
;
423 * New inode, set nbytes to 0 so that the nbytes comes out
424 * properly when we replay the extents.
426 item
= btrfs_item_ptr(eb
, slot
, struct btrfs_inode_item
);
427 btrfs_set_inode_nbytes(eb
, item
, 0);
430 * If this is a directory we need to reset the i_size to 0 so
431 * that we can set it up properly when replaying the rest of
432 * the items in this log.
434 mode
= btrfs_inode_mode(eb
, item
);
436 btrfs_set_inode_size(eb
, item
, 0);
439 btrfs_release_path(path
);
440 /* try to insert the key into the destination tree */
441 path
->skip_release_on_error
= 1;
442 ret
= btrfs_insert_empty_item(trans
, root
, path
,
444 path
->skip_release_on_error
= 0;
446 /* make sure any existing item is the correct size */
447 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
) {
449 found_size
= btrfs_item_size_nr(path
->nodes
[0],
451 if (found_size
> item_size
)
452 btrfs_truncate_item(fs_info
, path
, item_size
, 1);
453 else if (found_size
< item_size
)
454 btrfs_extend_item(fs_info
, path
,
455 item_size
- found_size
);
459 dst_ptr
= btrfs_item_ptr_offset(path
->nodes
[0],
462 /* don't overwrite an existing inode if the generation number
463 * was logged as zero. This is done when the tree logging code
464 * is just logging an inode to make sure it exists after recovery.
466 * Also, don't overwrite i_size on directories during replay.
467 * log replay inserts and removes directory items based on the
468 * state of the tree found in the subvolume, and i_size is modified
471 if (key
->type
== BTRFS_INODE_ITEM_KEY
&& ret
== -EEXIST
) {
472 struct btrfs_inode_item
*src_item
;
473 struct btrfs_inode_item
*dst_item
;
475 src_item
= (struct btrfs_inode_item
*)src_ptr
;
476 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
478 if (btrfs_inode_generation(eb
, src_item
) == 0) {
479 struct extent_buffer
*dst_eb
= path
->nodes
[0];
480 const u64 ino_size
= btrfs_inode_size(eb
, src_item
);
483 * For regular files an ino_size == 0 is used only when
484 * logging that an inode exists, as part of a directory
485 * fsync, and the inode wasn't fsynced before. In this
486 * case don't set the size of the inode in the fs/subvol
487 * tree, otherwise we would be throwing valid data away.
489 if (S_ISREG(btrfs_inode_mode(eb
, src_item
)) &&
490 S_ISREG(btrfs_inode_mode(dst_eb
, dst_item
)) &&
492 struct btrfs_map_token token
;
494 btrfs_init_map_token(&token
);
495 btrfs_set_token_inode_size(dst_eb
, dst_item
,
501 if (overwrite_root
&&
502 S_ISDIR(btrfs_inode_mode(eb
, src_item
)) &&
503 S_ISDIR(btrfs_inode_mode(path
->nodes
[0], dst_item
))) {
505 saved_i_size
= btrfs_inode_size(path
->nodes
[0],
510 copy_extent_buffer(path
->nodes
[0], eb
, dst_ptr
,
513 if (save_old_i_size
) {
514 struct btrfs_inode_item
*dst_item
;
515 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
516 btrfs_set_inode_size(path
->nodes
[0], dst_item
, saved_i_size
);
519 /* make sure the generation is filled in */
520 if (key
->type
== BTRFS_INODE_ITEM_KEY
) {
521 struct btrfs_inode_item
*dst_item
;
522 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
523 if (btrfs_inode_generation(path
->nodes
[0], dst_item
) == 0) {
524 btrfs_set_inode_generation(path
->nodes
[0], dst_item
,
529 btrfs_mark_buffer_dirty(path
->nodes
[0]);
530 btrfs_release_path(path
);
535 * simple helper to read an inode off the disk from a given root
536 * This can only be called for subvolume roots and not for the log
538 static noinline
struct inode
*read_one_inode(struct btrfs_root
*root
,
541 struct btrfs_key key
;
544 key
.objectid
= objectid
;
545 key
.type
= BTRFS_INODE_ITEM_KEY
;
547 inode
= btrfs_iget(root
->fs_info
->sb
, &key
, root
, NULL
);
550 } else if (is_bad_inode(inode
)) {
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_inline_len(eb
, slot
, 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
, fs_info
,
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
,
719 root
->root_key
.objectid
,
720 key
->objectid
, offset
, &ins
);
724 btrfs_release_path(path
);
726 if (btrfs_file_extent_compression(eb
, item
)) {
727 csum_start
= ins
.objectid
;
728 csum_end
= csum_start
+ ins
.offset
;
730 csum_start
= ins
.objectid
+
731 btrfs_file_extent_offset(eb
, item
);
732 csum_end
= csum_start
+
733 btrfs_file_extent_num_bytes(eb
, item
);
736 ret
= btrfs_lookup_csums_range(root
->log_root
,
737 csum_start
, csum_end
- 1,
742 * Now delete all existing cums in the csum root that
743 * cover our range. We do this because we can have an
744 * extent that is completely referenced by one file
745 * extent item and partially referenced by another
746 * file extent item (like after using the clone or
747 * extent_same ioctls). In this case if we end up doing
748 * the replay of the one that partially references the
749 * extent first, and we do not do the csum deletion
750 * below, we can get 2 csum items in the csum tree that
751 * overlap each other. For example, imagine our log has
752 * the two following file extent items:
754 * key (257 EXTENT_DATA 409600)
755 * extent data disk byte 12845056 nr 102400
756 * extent data offset 20480 nr 20480 ram 102400
758 * key (257 EXTENT_DATA 819200)
759 * extent data disk byte 12845056 nr 102400
760 * extent data offset 0 nr 102400 ram 102400
762 * Where the second one fully references the 100K extent
763 * that starts at disk byte 12845056, and the log tree
764 * has a single csum item that covers the entire range
767 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
769 * After the first file extent item is replayed, the
770 * csum tree gets the following csum item:
772 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
774 * Which covers the 20K sub-range starting at offset 20K
775 * of our extent. Now when we replay the second file
776 * extent item, if we do not delete existing csum items
777 * that cover any of its blocks, we end up getting two
778 * csum items in our csum tree that overlap each other:
780 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
781 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
783 * Which is a problem, because after this anyone trying
784 * to lookup up for the checksum of any block of our
785 * extent starting at an offset of 40K or higher, will
786 * end up looking at the second csum item only, which
787 * does not contain the checksum for any block starting
788 * at offset 40K or higher of our extent.
790 while (!list_empty(&ordered_sums
)) {
791 struct btrfs_ordered_sum
*sums
;
792 sums
= list_entry(ordered_sums
.next
,
793 struct btrfs_ordered_sum
,
796 ret
= btrfs_del_csums(trans
, fs_info
,
800 ret
= btrfs_csum_file_blocks(trans
,
801 fs_info
->csum_root
, sums
);
802 list_del(&sums
->list
);
808 btrfs_release_path(path
);
810 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
811 /* inline extents are easy, we just overwrite them */
812 ret
= overwrite_item(trans
, root
, path
, eb
, slot
, key
);
817 inode_add_bytes(inode
, nbytes
);
819 ret
= btrfs_update_inode(trans
, root
, inode
);
827 * when cleaning up conflicts between the directory names in the
828 * subvolume, directory names in the log and directory names in the
829 * inode back references, we may have to unlink inodes from directories.
831 * This is a helper function to do the unlink of a specific directory
834 static noinline
int drop_one_dir_item(struct btrfs_trans_handle
*trans
,
835 struct btrfs_root
*root
,
836 struct btrfs_path
*path
,
837 struct btrfs_inode
*dir
,
838 struct btrfs_dir_item
*di
)
843 struct extent_buffer
*leaf
;
844 struct btrfs_key location
;
847 leaf
= path
->nodes
[0];
849 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
850 name_len
= btrfs_dir_name_len(leaf
, di
);
851 name
= kmalloc(name_len
, GFP_NOFS
);
855 read_extent_buffer(leaf
, name
, (unsigned long)(di
+ 1), name_len
);
856 btrfs_release_path(path
);
858 inode
= read_one_inode(root
, location
.objectid
);
864 ret
= link_to_fixup_dir(trans
, root
, path
, location
.objectid
);
868 ret
= btrfs_unlink_inode(trans
, root
, dir
, BTRFS_I(inode
), name
,
873 ret
= btrfs_run_delayed_items(trans
);
881 * helper function to see if a given name and sequence number found
882 * in an inode back reference are already in a directory and correctly
883 * point to this inode
885 static noinline
int inode_in_dir(struct btrfs_root
*root
,
886 struct btrfs_path
*path
,
887 u64 dirid
, u64 objectid
, u64 index
,
888 const char *name
, int name_len
)
890 struct btrfs_dir_item
*di
;
891 struct btrfs_key location
;
894 di
= btrfs_lookup_dir_index_item(NULL
, root
, path
, dirid
,
895 index
, name
, name_len
, 0);
896 if (di
&& !IS_ERR(di
)) {
897 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
898 if (location
.objectid
!= objectid
)
902 btrfs_release_path(path
);
904 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dirid
, name
, name_len
, 0);
905 if (di
&& !IS_ERR(di
)) {
906 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
907 if (location
.objectid
!= objectid
)
913 btrfs_release_path(path
);
918 * helper function to check a log tree for a named back reference in
919 * an inode. This is used to decide if a back reference that is
920 * found in the subvolume conflicts with what we find in the log.
922 * inode backreferences may have multiple refs in a single item,
923 * during replay we process one reference at a time, and we don't
924 * want to delete valid links to a file from the subvolume if that
925 * link is also in the log.
927 static noinline
int backref_in_log(struct btrfs_root
*log
,
928 struct btrfs_key
*key
,
930 const char *name
, int namelen
)
932 struct btrfs_path
*path
;
933 struct btrfs_inode_ref
*ref
;
935 unsigned long ptr_end
;
936 unsigned long name_ptr
;
942 path
= btrfs_alloc_path();
946 ret
= btrfs_search_slot(NULL
, log
, key
, path
, 0, 0);
950 ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
952 if (key
->type
== BTRFS_INODE_EXTREF_KEY
) {
953 if (btrfs_find_name_in_ext_backref(path
->nodes
[0],
956 name
, namelen
, NULL
))
962 item_size
= btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]);
963 ptr_end
= ptr
+ item_size
;
964 while (ptr
< ptr_end
) {
965 ref
= (struct btrfs_inode_ref
*)ptr
;
966 found_name_len
= btrfs_inode_ref_name_len(path
->nodes
[0], ref
);
967 if (found_name_len
== namelen
) {
968 name_ptr
= (unsigned long)(ref
+ 1);
969 ret
= memcmp_extent_buffer(path
->nodes
[0], name
,
976 ptr
= (unsigned long)(ref
+ 1) + found_name_len
;
979 btrfs_free_path(path
);
983 static inline int __add_inode_ref(struct btrfs_trans_handle
*trans
,
984 struct btrfs_root
*root
,
985 struct btrfs_path
*path
,
986 struct btrfs_root
*log_root
,
987 struct btrfs_inode
*dir
,
988 struct btrfs_inode
*inode
,
989 u64 inode_objectid
, u64 parent_objectid
,
990 u64 ref_index
, char *name
, int namelen
,
996 struct extent_buffer
*leaf
;
997 struct btrfs_dir_item
*di
;
998 struct btrfs_key search_key
;
999 struct btrfs_inode_extref
*extref
;
1002 /* Search old style refs */
1003 search_key
.objectid
= inode_objectid
;
1004 search_key
.type
= BTRFS_INODE_REF_KEY
;
1005 search_key
.offset
= parent_objectid
;
1006 ret
= btrfs_search_slot(NULL
, root
, &search_key
, path
, 0, 0);
1008 struct btrfs_inode_ref
*victim_ref
;
1010 unsigned long ptr_end
;
1012 leaf
= path
->nodes
[0];
1014 /* are we trying to overwrite a back ref for the root directory
1015 * if so, just jump out, we're done
1017 if (search_key
.objectid
== search_key
.offset
)
1020 /* check all the names in this back reference to see
1021 * if they are in the log. if so, we allow them to stay
1022 * otherwise they must be unlinked as a conflict
1024 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
1025 ptr_end
= ptr
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
1026 while (ptr
< ptr_end
) {
1027 victim_ref
= (struct btrfs_inode_ref
*)ptr
;
1028 victim_name_len
= btrfs_inode_ref_name_len(leaf
,
1030 victim_name
= kmalloc(victim_name_len
, GFP_NOFS
);
1034 read_extent_buffer(leaf
, victim_name
,
1035 (unsigned long)(victim_ref
+ 1),
1038 if (!backref_in_log(log_root
, &search_key
,
1042 inc_nlink(&inode
->vfs_inode
);
1043 btrfs_release_path(path
);
1045 ret
= btrfs_unlink_inode(trans
, root
, dir
, inode
,
1046 victim_name
, victim_name_len
);
1050 ret
= btrfs_run_delayed_items(trans
);
1058 ptr
= (unsigned long)(victim_ref
+ 1) + victim_name_len
;
1062 * NOTE: we have searched root tree and checked the
1063 * corresponding ref, it does not need to check again.
1067 btrfs_release_path(path
);
1069 /* Same search but for extended refs */
1070 extref
= btrfs_lookup_inode_extref(NULL
, root
, path
, name
, namelen
,
1071 inode_objectid
, parent_objectid
, 0,
1073 if (!IS_ERR_OR_NULL(extref
)) {
1077 struct inode
*victim_parent
;
1079 leaf
= path
->nodes
[0];
1081 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1082 base
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
1084 while (cur_offset
< item_size
) {
1085 extref
= (struct btrfs_inode_extref
*)(base
+ cur_offset
);
1087 victim_name_len
= btrfs_inode_extref_name_len(leaf
, extref
);
1089 if (btrfs_inode_extref_parent(leaf
, extref
) != parent_objectid
)
1092 victim_name
= kmalloc(victim_name_len
, GFP_NOFS
);
1095 read_extent_buffer(leaf
, victim_name
, (unsigned long)&extref
->name
,
1098 search_key
.objectid
= inode_objectid
;
1099 search_key
.type
= BTRFS_INODE_EXTREF_KEY
;
1100 search_key
.offset
= btrfs_extref_hash(parent_objectid
,
1104 if (!backref_in_log(log_root
, &search_key
,
1105 parent_objectid
, victim_name
,
1108 victim_parent
= read_one_inode(root
,
1110 if (victim_parent
) {
1111 inc_nlink(&inode
->vfs_inode
);
1112 btrfs_release_path(path
);
1114 ret
= btrfs_unlink_inode(trans
, root
,
1115 BTRFS_I(victim_parent
),
1120 ret
= btrfs_run_delayed_items(
1123 iput(victim_parent
);
1132 cur_offset
+= victim_name_len
+ sizeof(*extref
);
1136 btrfs_release_path(path
);
1138 /* look for a conflicting sequence number */
1139 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, btrfs_ino(dir
),
1140 ref_index
, name
, namelen
, 0);
1141 if (di
&& !IS_ERR(di
)) {
1142 ret
= drop_one_dir_item(trans
, root
, path
, dir
, di
);
1146 btrfs_release_path(path
);
1148 /* look for a conflicing name */
1149 di
= btrfs_lookup_dir_item(trans
, root
, path
, btrfs_ino(dir
),
1151 if (di
&& !IS_ERR(di
)) {
1152 ret
= drop_one_dir_item(trans
, root
, path
, dir
, di
);
1156 btrfs_release_path(path
);
1161 static int extref_get_fields(struct extent_buffer
*eb
, unsigned long ref_ptr
,
1162 u32
*namelen
, char **name
, u64
*index
,
1163 u64
*parent_objectid
)
1165 struct btrfs_inode_extref
*extref
;
1167 extref
= (struct btrfs_inode_extref
*)ref_ptr
;
1169 *namelen
= btrfs_inode_extref_name_len(eb
, extref
);
1170 *name
= kmalloc(*namelen
, GFP_NOFS
);
1174 read_extent_buffer(eb
, *name
, (unsigned long)&extref
->name
,
1178 *index
= btrfs_inode_extref_index(eb
, extref
);
1179 if (parent_objectid
)
1180 *parent_objectid
= btrfs_inode_extref_parent(eb
, extref
);
1185 static int ref_get_fields(struct extent_buffer
*eb
, unsigned long ref_ptr
,
1186 u32
*namelen
, char **name
, u64
*index
)
1188 struct btrfs_inode_ref
*ref
;
1190 ref
= (struct btrfs_inode_ref
*)ref_ptr
;
1192 *namelen
= btrfs_inode_ref_name_len(eb
, ref
);
1193 *name
= kmalloc(*namelen
, GFP_NOFS
);
1197 read_extent_buffer(eb
, *name
, (unsigned long)(ref
+ 1), *namelen
);
1200 *index
= btrfs_inode_ref_index(eb
, ref
);
1206 * Take an inode reference item from the log tree and iterate all names from the
1207 * inode reference item in the subvolume tree with the same key (if it exists).
1208 * For any name that is not in the inode reference item from the log tree, do a
1209 * proper unlink of that name (that is, remove its entry from the inode
1210 * reference item and both dir index keys).
1212 static int unlink_old_inode_refs(struct btrfs_trans_handle
*trans
,
1213 struct btrfs_root
*root
,
1214 struct btrfs_path
*path
,
1215 struct btrfs_inode
*inode
,
1216 struct extent_buffer
*log_eb
,
1218 struct btrfs_key
*key
)
1221 unsigned long ref_ptr
;
1222 unsigned long ref_end
;
1223 struct extent_buffer
*eb
;
1226 btrfs_release_path(path
);
1227 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
1235 eb
= path
->nodes
[0];
1236 ref_ptr
= btrfs_item_ptr_offset(eb
, path
->slots
[0]);
1237 ref_end
= ref_ptr
+ btrfs_item_size_nr(eb
, path
->slots
[0]);
1238 while (ref_ptr
< ref_end
) {
1243 if (key
->type
== BTRFS_INODE_EXTREF_KEY
) {
1244 ret
= extref_get_fields(eb
, ref_ptr
, &namelen
, &name
,
1247 parent_id
= key
->offset
;
1248 ret
= ref_get_fields(eb
, ref_ptr
, &namelen
, &name
,
1254 if (key
->type
== BTRFS_INODE_EXTREF_KEY
)
1255 ret
= btrfs_find_name_in_ext_backref(log_eb
, log_slot
,
1259 ret
= btrfs_find_name_in_backref(log_eb
, log_slot
, name
,
1265 btrfs_release_path(path
);
1266 dir
= read_one_inode(root
, parent_id
);
1272 ret
= btrfs_unlink_inode(trans
, root
, BTRFS_I(dir
),
1273 inode
, name
, namelen
);
1283 if (key
->type
== BTRFS_INODE_EXTREF_KEY
)
1284 ref_ptr
+= sizeof(struct btrfs_inode_extref
);
1286 ref_ptr
+= sizeof(struct btrfs_inode_ref
);
1290 btrfs_release_path(path
);
1294 static int btrfs_inode_ref_exists(struct inode
*inode
, struct inode
*dir
,
1295 const u8 ref_type
, const char *name
,
1298 struct btrfs_key key
;
1299 struct btrfs_path
*path
;
1300 const u64 parent_id
= btrfs_ino(BTRFS_I(dir
));
1303 path
= btrfs_alloc_path();
1307 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
1308 key
.type
= ref_type
;
1309 if (key
.type
== BTRFS_INODE_REF_KEY
)
1310 key
.offset
= parent_id
;
1312 key
.offset
= btrfs_extref_hash(parent_id
, name
, namelen
);
1314 ret
= btrfs_search_slot(NULL
, BTRFS_I(inode
)->root
, &key
, path
, 0, 0);
1321 if (key
.type
== BTRFS_INODE_EXTREF_KEY
)
1322 ret
= btrfs_find_name_in_ext_backref(path
->nodes
[0],
1323 path
->slots
[0], parent_id
,
1324 name
, namelen
, NULL
);
1326 ret
= btrfs_find_name_in_backref(path
->nodes
[0], path
->slots
[0],
1327 name
, namelen
, NULL
);
1330 btrfs_free_path(path
);
1335 * replay one inode back reference item found in the log tree.
1336 * eb, slot and key refer to the buffer and key found in the log tree.
1337 * root is the destination we are replaying into, and path is for temp
1338 * use by this function. (it should be released on return).
1340 static noinline
int add_inode_ref(struct btrfs_trans_handle
*trans
,
1341 struct btrfs_root
*root
,
1342 struct btrfs_root
*log
,
1343 struct btrfs_path
*path
,
1344 struct extent_buffer
*eb
, int slot
,
1345 struct btrfs_key
*key
)
1347 struct inode
*dir
= NULL
;
1348 struct inode
*inode
= NULL
;
1349 unsigned long ref_ptr
;
1350 unsigned long ref_end
;
1354 int search_done
= 0;
1355 int log_ref_ver
= 0;
1356 u64 parent_objectid
;
1359 int ref_struct_size
;
1361 ref_ptr
= btrfs_item_ptr_offset(eb
, slot
);
1362 ref_end
= ref_ptr
+ btrfs_item_size_nr(eb
, slot
);
1364 if (key
->type
== BTRFS_INODE_EXTREF_KEY
) {
1365 struct btrfs_inode_extref
*r
;
1367 ref_struct_size
= sizeof(struct btrfs_inode_extref
);
1369 r
= (struct btrfs_inode_extref
*)ref_ptr
;
1370 parent_objectid
= btrfs_inode_extref_parent(eb
, r
);
1372 ref_struct_size
= sizeof(struct btrfs_inode_ref
);
1373 parent_objectid
= key
->offset
;
1375 inode_objectid
= key
->objectid
;
1378 * it is possible that we didn't log all the parent directories
1379 * for a given inode. If we don't find the dir, just don't
1380 * copy the back ref in. The link count fixup code will take
1383 dir
= read_one_inode(root
, parent_objectid
);
1389 inode
= read_one_inode(root
, inode_objectid
);
1395 while (ref_ptr
< ref_end
) {
1397 ret
= extref_get_fields(eb
, ref_ptr
, &namelen
, &name
,
1398 &ref_index
, &parent_objectid
);
1400 * parent object can change from one array
1404 dir
= read_one_inode(root
, parent_objectid
);
1410 ret
= ref_get_fields(eb
, ref_ptr
, &namelen
, &name
,
1416 /* if we already have a perfect match, we're done */
1417 if (!inode_in_dir(root
, path
, btrfs_ino(BTRFS_I(dir
)),
1418 btrfs_ino(BTRFS_I(inode
)), ref_index
,
1421 * look for a conflicting back reference in the
1422 * metadata. if we find one we have to unlink that name
1423 * of the file before we add our new link. Later on, we
1424 * overwrite any existing back reference, and we don't
1425 * want to create dangling pointers in the directory.
1429 ret
= __add_inode_ref(trans
, root
, path
, log
,
1434 ref_index
, name
, namelen
,
1444 * If a reference item already exists for this inode
1445 * with the same parent and name, but different index,
1446 * drop it and the corresponding directory index entries
1447 * from the parent before adding the new reference item
1448 * and dir index entries, otherwise we would fail with
1449 * -EEXIST returned from btrfs_add_link() below.
1451 ret
= btrfs_inode_ref_exists(inode
, dir
, key
->type
,
1454 ret
= btrfs_unlink_inode(trans
, root
,
1459 * If we dropped the link count to 0, bump it so
1460 * that later the iput() on the inode will not
1461 * free it. We will fixup the link count later.
1463 if (!ret
&& inode
->i_nlink
== 0)
1469 /* insert our name */
1470 ret
= btrfs_add_link(trans
, BTRFS_I(dir
),
1472 name
, namelen
, 0, ref_index
);
1476 btrfs_update_inode(trans
, root
, inode
);
1479 ref_ptr
= (unsigned long)(ref_ptr
+ ref_struct_size
) + namelen
;
1489 * Before we overwrite the inode reference item in the subvolume tree
1490 * with the item from the log tree, we must unlink all names from the
1491 * parent directory that are in the subvolume's tree inode reference
1492 * item, otherwise we end up with an inconsistent subvolume tree where
1493 * dir index entries exist for a name but there is no inode reference
1494 * item with the same name.
1496 ret
= unlink_old_inode_refs(trans
, root
, path
, BTRFS_I(inode
), eb
, slot
,
1501 /* finally write the back reference in the inode */
1502 ret
= overwrite_item(trans
, root
, path
, eb
, slot
, key
);
1504 btrfs_release_path(path
);
1511 static int insert_orphan_item(struct btrfs_trans_handle
*trans
,
1512 struct btrfs_root
*root
, u64 ino
)
1516 ret
= btrfs_insert_orphan_item(trans
, root
, ino
);
1523 static int count_inode_extrefs(struct btrfs_root
*root
,
1524 struct btrfs_inode
*inode
, struct btrfs_path
*path
)
1528 unsigned int nlink
= 0;
1531 u64 inode_objectid
= btrfs_ino(inode
);
1534 struct btrfs_inode_extref
*extref
;
1535 struct extent_buffer
*leaf
;
1538 ret
= btrfs_find_one_extref(root
, inode_objectid
, offset
, path
,
1543 leaf
= path
->nodes
[0];
1544 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1545 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
1548 while (cur_offset
< item_size
) {
1549 extref
= (struct btrfs_inode_extref
*) (ptr
+ cur_offset
);
1550 name_len
= btrfs_inode_extref_name_len(leaf
, extref
);
1554 cur_offset
+= name_len
+ sizeof(*extref
);
1558 btrfs_release_path(path
);
1560 btrfs_release_path(path
);
1562 if (ret
< 0 && ret
!= -ENOENT
)
1567 static int count_inode_refs(struct btrfs_root
*root
,
1568 struct btrfs_inode
*inode
, struct btrfs_path
*path
)
1571 struct btrfs_key key
;
1572 unsigned int nlink
= 0;
1574 unsigned long ptr_end
;
1576 u64 ino
= btrfs_ino(inode
);
1579 key
.type
= BTRFS_INODE_REF_KEY
;
1580 key
.offset
= (u64
)-1;
1583 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1587 if (path
->slots
[0] == 0)
1592 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
1594 if (key
.objectid
!= ino
||
1595 key
.type
!= BTRFS_INODE_REF_KEY
)
1597 ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
1598 ptr_end
= ptr
+ btrfs_item_size_nr(path
->nodes
[0],
1600 while (ptr
< ptr_end
) {
1601 struct btrfs_inode_ref
*ref
;
1603 ref
= (struct btrfs_inode_ref
*)ptr
;
1604 name_len
= btrfs_inode_ref_name_len(path
->nodes
[0],
1606 ptr
= (unsigned long)(ref
+ 1) + name_len
;
1610 if (key
.offset
== 0)
1612 if (path
->slots
[0] > 0) {
1617 btrfs_release_path(path
);
1619 btrfs_release_path(path
);
1625 * There are a few corners where the link count of the file can't
1626 * be properly maintained during replay. So, instead of adding
1627 * lots of complexity to the log code, we just scan the backrefs
1628 * for any file that has been through replay.
1630 * The scan will update the link count on the inode to reflect the
1631 * number of back refs found. If it goes down to zero, the iput
1632 * will free the inode.
1634 static noinline
int fixup_inode_link_count(struct btrfs_trans_handle
*trans
,
1635 struct btrfs_root
*root
,
1636 struct inode
*inode
)
1638 struct btrfs_path
*path
;
1641 u64 ino
= btrfs_ino(BTRFS_I(inode
));
1643 path
= btrfs_alloc_path();
1647 ret
= count_inode_refs(root
, BTRFS_I(inode
), path
);
1653 ret
= count_inode_extrefs(root
, BTRFS_I(inode
), path
);
1661 if (nlink
!= inode
->i_nlink
) {
1662 set_nlink(inode
, nlink
);
1663 btrfs_update_inode(trans
, root
, inode
);
1665 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1667 if (inode
->i_nlink
== 0) {
1668 if (S_ISDIR(inode
->i_mode
)) {
1669 ret
= replay_dir_deletes(trans
, root
, NULL
, path
,
1674 ret
= insert_orphan_item(trans
, root
, ino
);
1678 btrfs_free_path(path
);
1682 static noinline
int fixup_inode_link_counts(struct btrfs_trans_handle
*trans
,
1683 struct btrfs_root
*root
,
1684 struct btrfs_path
*path
)
1687 struct btrfs_key key
;
1688 struct inode
*inode
;
1690 key
.objectid
= BTRFS_TREE_LOG_FIXUP_OBJECTID
;
1691 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
1692 key
.offset
= (u64
)-1;
1694 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1699 if (path
->slots
[0] == 0)
1704 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1705 if (key
.objectid
!= BTRFS_TREE_LOG_FIXUP_OBJECTID
||
1706 key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
1709 ret
= btrfs_del_item(trans
, root
, path
);
1713 btrfs_release_path(path
);
1714 inode
= read_one_inode(root
, key
.offset
);
1718 ret
= fixup_inode_link_count(trans
, root
, inode
);
1724 * fixup on a directory may create new entries,
1725 * make sure we always look for the highset possible
1728 key
.offset
= (u64
)-1;
1732 btrfs_release_path(path
);
1738 * record a given inode in the fixup dir so we can check its link
1739 * count when replay is done. The link count is incremented here
1740 * so the inode won't go away until we check it
1742 static noinline
int link_to_fixup_dir(struct btrfs_trans_handle
*trans
,
1743 struct btrfs_root
*root
,
1744 struct btrfs_path
*path
,
1747 struct btrfs_key key
;
1749 struct inode
*inode
;
1751 inode
= read_one_inode(root
, objectid
);
1755 key
.objectid
= BTRFS_TREE_LOG_FIXUP_OBJECTID
;
1756 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
1757 key
.offset
= objectid
;
1759 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1761 btrfs_release_path(path
);
1763 if (!inode
->i_nlink
)
1764 set_nlink(inode
, 1);
1767 ret
= btrfs_update_inode(trans
, root
, inode
);
1768 } else if (ret
== -EEXIST
) {
1771 BUG(); /* Logic Error */
1779 * when replaying the log for a directory, we only insert names
1780 * for inodes that actually exist. This means an fsync on a directory
1781 * does not implicitly fsync all the new files in it
1783 static noinline
int insert_one_name(struct btrfs_trans_handle
*trans
,
1784 struct btrfs_root
*root
,
1785 u64 dirid
, u64 index
,
1786 char *name
, int name_len
,
1787 struct btrfs_key
*location
)
1789 struct inode
*inode
;
1793 inode
= read_one_inode(root
, location
->objectid
);
1797 dir
= read_one_inode(root
, dirid
);
1803 ret
= btrfs_add_link(trans
, BTRFS_I(dir
), BTRFS_I(inode
), name
,
1804 name_len
, 1, index
);
1806 /* FIXME, put inode into FIXUP list */
1814 * Return true if an inode reference exists in the log for the given name,
1815 * inode and parent inode.
1817 static bool name_in_log_ref(struct btrfs_root
*log_root
,
1818 const char *name
, const int name_len
,
1819 const u64 dirid
, const u64 ino
)
1821 struct btrfs_key search_key
;
1823 search_key
.objectid
= ino
;
1824 search_key
.type
= BTRFS_INODE_REF_KEY
;
1825 search_key
.offset
= dirid
;
1826 if (backref_in_log(log_root
, &search_key
, dirid
, name
, name_len
))
1829 search_key
.type
= BTRFS_INODE_EXTREF_KEY
;
1830 search_key
.offset
= btrfs_extref_hash(dirid
, name
, name_len
);
1831 if (backref_in_log(log_root
, &search_key
, dirid
, name
, name_len
))
1838 * take a single entry in a log directory item and replay it into
1841 * if a conflicting item exists in the subdirectory already,
1842 * the inode it points to is unlinked and put into the link count
1845 * If a name from the log points to a file or directory that does
1846 * not exist in the FS, it is skipped. fsyncs on directories
1847 * do not force down inodes inside that directory, just changes to the
1848 * names or unlinks in a directory.
1850 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1851 * non-existing inode) and 1 if the name was replayed.
1853 static noinline
int replay_one_name(struct btrfs_trans_handle
*trans
,
1854 struct btrfs_root
*root
,
1855 struct btrfs_path
*path
,
1856 struct extent_buffer
*eb
,
1857 struct btrfs_dir_item
*di
,
1858 struct btrfs_key
*key
)
1862 struct btrfs_dir_item
*dst_di
;
1863 struct btrfs_key found_key
;
1864 struct btrfs_key log_key
;
1869 bool update_size
= (key
->type
== BTRFS_DIR_INDEX_KEY
);
1870 bool name_added
= false;
1872 dir
= read_one_inode(root
, key
->objectid
);
1876 name_len
= btrfs_dir_name_len(eb
, di
);
1877 name
= kmalloc(name_len
, GFP_NOFS
);
1883 log_type
= btrfs_dir_type(eb
, di
);
1884 read_extent_buffer(eb
, name
, (unsigned long)(di
+ 1),
1887 btrfs_dir_item_key_to_cpu(eb
, di
, &log_key
);
1888 exists
= btrfs_lookup_inode(trans
, root
, path
, &log_key
, 0);
1893 btrfs_release_path(path
);
1895 if (key
->type
== BTRFS_DIR_ITEM_KEY
) {
1896 dst_di
= btrfs_lookup_dir_item(trans
, root
, path
, key
->objectid
,
1898 } else if (key
->type
== BTRFS_DIR_INDEX_KEY
) {
1899 dst_di
= btrfs_lookup_dir_index_item(trans
, root
, path
,
1908 if (IS_ERR_OR_NULL(dst_di
)) {
1909 /* we need a sequence number to insert, so we only
1910 * do inserts for the BTRFS_DIR_INDEX_KEY types
1912 if (key
->type
!= BTRFS_DIR_INDEX_KEY
)
1917 btrfs_dir_item_key_to_cpu(path
->nodes
[0], dst_di
, &found_key
);
1918 /* the existing item matches the logged item */
1919 if (found_key
.objectid
== log_key
.objectid
&&
1920 found_key
.type
== log_key
.type
&&
1921 found_key
.offset
== log_key
.offset
&&
1922 btrfs_dir_type(path
->nodes
[0], dst_di
) == log_type
) {
1923 update_size
= false;
1928 * don't drop the conflicting directory entry if the inode
1929 * for the new entry doesn't exist
1934 ret
= drop_one_dir_item(trans
, root
, path
, BTRFS_I(dir
), dst_di
);
1938 if (key
->type
== BTRFS_DIR_INDEX_KEY
)
1941 btrfs_release_path(path
);
1942 if (!ret
&& update_size
) {
1943 btrfs_i_size_write(BTRFS_I(dir
), dir
->i_size
+ name_len
* 2);
1944 ret
= btrfs_update_inode(trans
, root
, dir
);
1948 if (!ret
&& name_added
)
1953 if (name_in_log_ref(root
->log_root
, name
, name_len
,
1954 key
->objectid
, log_key
.objectid
)) {
1955 /* The dentry will be added later. */
1957 update_size
= false;
1960 btrfs_release_path(path
);
1961 ret
= insert_one_name(trans
, root
, key
->objectid
, key
->offset
,
1962 name
, name_len
, &log_key
);
1963 if (ret
&& ret
!= -ENOENT
&& ret
!= -EEXIST
)
1967 update_size
= false;
1973 * find all the names in a directory item and reconcile them into
1974 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1975 * one name in a directory item, but the same code gets used for
1976 * both directory index types
1978 static noinline
int replay_one_dir_item(struct btrfs_trans_handle
*trans
,
1979 struct btrfs_root
*root
,
1980 struct btrfs_path
*path
,
1981 struct extent_buffer
*eb
, int slot
,
1982 struct btrfs_key
*key
)
1985 u32 item_size
= btrfs_item_size_nr(eb
, slot
);
1986 struct btrfs_dir_item
*di
;
1989 unsigned long ptr_end
;
1990 struct btrfs_path
*fixup_path
= NULL
;
1992 ptr
= btrfs_item_ptr_offset(eb
, slot
);
1993 ptr_end
= ptr
+ item_size
;
1994 while (ptr
< ptr_end
) {
1995 di
= (struct btrfs_dir_item
*)ptr
;
1996 name_len
= btrfs_dir_name_len(eb
, di
);
1997 ret
= replay_one_name(trans
, root
, path
, eb
, di
, key
);
2000 ptr
= (unsigned long)(di
+ 1);
2004 * If this entry refers to a non-directory (directories can not
2005 * have a link count > 1) and it was added in the transaction
2006 * that was not committed, make sure we fixup the link count of
2007 * the inode it the entry points to. Otherwise something like
2008 * the following would result in a directory pointing to an
2009 * inode with a wrong link that does not account for this dir
2017 * ln testdir/bar testdir/bar_link
2018 * ln testdir/foo testdir/foo_link
2019 * xfs_io -c "fsync" testdir/bar
2023 * mount fs, log replay happens
2025 * File foo would remain with a link count of 1 when it has two
2026 * entries pointing to it in the directory testdir. This would
2027 * make it impossible to ever delete the parent directory has
2028 * it would result in stale dentries that can never be deleted.
2030 if (ret
== 1 && btrfs_dir_type(eb
, di
) != BTRFS_FT_DIR
) {
2031 struct btrfs_key di_key
;
2034 fixup_path
= btrfs_alloc_path();
2041 btrfs_dir_item_key_to_cpu(eb
, di
, &di_key
);
2042 ret
= link_to_fixup_dir(trans
, root
, fixup_path
,
2049 btrfs_free_path(fixup_path
);
2054 * directory replay has two parts. There are the standard directory
2055 * items in the log copied from the subvolume, and range items
2056 * created in the log while the subvolume was logged.
2058 * The range items tell us which parts of the key space the log
2059 * is authoritative for. During replay, if a key in the subvolume
2060 * directory is in a logged range item, but not actually in the log
2061 * that means it was deleted from the directory before the fsync
2062 * and should be removed.
2064 static noinline
int find_dir_range(struct btrfs_root
*root
,
2065 struct btrfs_path
*path
,
2066 u64 dirid
, int key_type
,
2067 u64
*start_ret
, u64
*end_ret
)
2069 struct btrfs_key key
;
2071 struct btrfs_dir_log_item
*item
;
2075 if (*start_ret
== (u64
)-1)
2078 key
.objectid
= dirid
;
2079 key
.type
= key_type
;
2080 key
.offset
= *start_ret
;
2082 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2086 if (path
->slots
[0] == 0)
2091 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
2093 if (key
.type
!= key_type
|| key
.objectid
!= dirid
) {
2097 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2098 struct btrfs_dir_log_item
);
2099 found_end
= btrfs_dir_log_end(path
->nodes
[0], item
);
2101 if (*start_ret
>= key
.offset
&& *start_ret
<= found_end
) {
2103 *start_ret
= key
.offset
;
2104 *end_ret
= found_end
;
2109 /* check the next slot in the tree to see if it is a valid item */
2110 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2112 if (path
->slots
[0] >= nritems
) {
2113 ret
= btrfs_next_leaf(root
, path
);
2118 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
2120 if (key
.type
!= key_type
|| key
.objectid
!= dirid
) {
2124 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2125 struct btrfs_dir_log_item
);
2126 found_end
= btrfs_dir_log_end(path
->nodes
[0], item
);
2127 *start_ret
= key
.offset
;
2128 *end_ret
= found_end
;
2131 btrfs_release_path(path
);
2136 * this looks for a given directory item in the log. If the directory
2137 * item is not in the log, the item is removed and the inode it points
2140 static noinline
int check_item_in_log(struct btrfs_trans_handle
*trans
,
2141 struct btrfs_root
*root
,
2142 struct btrfs_root
*log
,
2143 struct btrfs_path
*path
,
2144 struct btrfs_path
*log_path
,
2146 struct btrfs_key
*dir_key
)
2149 struct extent_buffer
*eb
;
2152 struct btrfs_dir_item
*di
;
2153 struct btrfs_dir_item
*log_di
;
2156 unsigned long ptr_end
;
2158 struct inode
*inode
;
2159 struct btrfs_key location
;
2162 eb
= path
->nodes
[0];
2163 slot
= path
->slots
[0];
2164 item_size
= btrfs_item_size_nr(eb
, slot
);
2165 ptr
= btrfs_item_ptr_offset(eb
, slot
);
2166 ptr_end
= ptr
+ item_size
;
2167 while (ptr
< ptr_end
) {
2168 di
= (struct btrfs_dir_item
*)ptr
;
2169 name_len
= btrfs_dir_name_len(eb
, di
);
2170 name
= kmalloc(name_len
, GFP_NOFS
);
2175 read_extent_buffer(eb
, name
, (unsigned long)(di
+ 1),
2178 if (log
&& dir_key
->type
== BTRFS_DIR_ITEM_KEY
) {
2179 log_di
= btrfs_lookup_dir_item(trans
, log
, log_path
,
2182 } else if (log
&& dir_key
->type
== BTRFS_DIR_INDEX_KEY
) {
2183 log_di
= btrfs_lookup_dir_index_item(trans
, log
,
2189 if (!log_di
|| (IS_ERR(log_di
) && PTR_ERR(log_di
) == -ENOENT
)) {
2190 btrfs_dir_item_key_to_cpu(eb
, di
, &location
);
2191 btrfs_release_path(path
);
2192 btrfs_release_path(log_path
);
2193 inode
= read_one_inode(root
, location
.objectid
);
2199 ret
= link_to_fixup_dir(trans
, root
,
2200 path
, location
.objectid
);
2208 ret
= btrfs_unlink_inode(trans
, root
, BTRFS_I(dir
),
2209 BTRFS_I(inode
), name
, name_len
);
2211 ret
= btrfs_run_delayed_items(trans
);
2217 /* there might still be more names under this key
2218 * check and repeat if required
2220 ret
= btrfs_search_slot(NULL
, root
, dir_key
, path
,
2226 } else if (IS_ERR(log_di
)) {
2228 return PTR_ERR(log_di
);
2230 btrfs_release_path(log_path
);
2233 ptr
= (unsigned long)(di
+ 1);
2238 btrfs_release_path(path
);
2239 btrfs_release_path(log_path
);
2243 static int replay_xattr_deletes(struct btrfs_trans_handle
*trans
,
2244 struct btrfs_root
*root
,
2245 struct btrfs_root
*log
,
2246 struct btrfs_path
*path
,
2249 struct btrfs_key search_key
;
2250 struct btrfs_path
*log_path
;
2255 log_path
= btrfs_alloc_path();
2259 search_key
.objectid
= ino
;
2260 search_key
.type
= BTRFS_XATTR_ITEM_KEY
;
2261 search_key
.offset
= 0;
2263 ret
= btrfs_search_slot(NULL
, root
, &search_key
, path
, 0, 0);
2267 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2268 for (i
= path
->slots
[0]; i
< nritems
; i
++) {
2269 struct btrfs_key key
;
2270 struct btrfs_dir_item
*di
;
2271 struct btrfs_dir_item
*log_di
;
2275 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, i
);
2276 if (key
.objectid
!= ino
|| key
.type
!= BTRFS_XATTR_ITEM_KEY
) {
2281 di
= btrfs_item_ptr(path
->nodes
[0], i
, struct btrfs_dir_item
);
2282 total_size
= btrfs_item_size_nr(path
->nodes
[0], i
);
2284 while (cur
< total_size
) {
2285 u16 name_len
= btrfs_dir_name_len(path
->nodes
[0], di
);
2286 u16 data_len
= btrfs_dir_data_len(path
->nodes
[0], di
);
2287 u32 this_len
= sizeof(*di
) + name_len
+ data_len
;
2290 name
= kmalloc(name_len
, GFP_NOFS
);
2295 read_extent_buffer(path
->nodes
[0], name
,
2296 (unsigned long)(di
+ 1), name_len
);
2298 log_di
= btrfs_lookup_xattr(NULL
, log
, log_path
, ino
,
2300 btrfs_release_path(log_path
);
2302 /* Doesn't exist in log tree, so delete it. */
2303 btrfs_release_path(path
);
2304 di
= btrfs_lookup_xattr(trans
, root
, path
, ino
,
2305 name
, name_len
, -1);
2312 ret
= btrfs_delete_one_dir_name(trans
, root
,
2316 btrfs_release_path(path
);
2321 if (IS_ERR(log_di
)) {
2322 ret
= PTR_ERR(log_di
);
2326 di
= (struct btrfs_dir_item
*)((char *)di
+ this_len
);
2329 ret
= btrfs_next_leaf(root
, path
);
2335 btrfs_free_path(log_path
);
2336 btrfs_release_path(path
);
2342 * deletion replay happens before we copy any new directory items
2343 * out of the log or out of backreferences from inodes. It
2344 * scans the log to find ranges of keys that log is authoritative for,
2345 * and then scans the directory to find items in those ranges that are
2346 * not present in the log.
2348 * Anything we don't find in the log is unlinked and removed from the
2351 static noinline
int replay_dir_deletes(struct btrfs_trans_handle
*trans
,
2352 struct btrfs_root
*root
,
2353 struct btrfs_root
*log
,
2354 struct btrfs_path
*path
,
2355 u64 dirid
, int del_all
)
2359 int key_type
= BTRFS_DIR_LOG_ITEM_KEY
;
2361 struct btrfs_key dir_key
;
2362 struct btrfs_key found_key
;
2363 struct btrfs_path
*log_path
;
2366 dir_key
.objectid
= dirid
;
2367 dir_key
.type
= BTRFS_DIR_ITEM_KEY
;
2368 log_path
= btrfs_alloc_path();
2372 dir
= read_one_inode(root
, dirid
);
2373 /* it isn't an error if the inode isn't there, that can happen
2374 * because we replay the deletes before we copy in the inode item
2378 btrfs_free_path(log_path
);
2386 range_end
= (u64
)-1;
2388 ret
= find_dir_range(log
, path
, dirid
, key_type
,
2389 &range_start
, &range_end
);
2394 dir_key
.offset
= range_start
;
2397 ret
= btrfs_search_slot(NULL
, root
, &dir_key
, path
,
2402 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2403 if (path
->slots
[0] >= nritems
) {
2404 ret
= btrfs_next_leaf(root
, path
);
2410 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2412 if (found_key
.objectid
!= dirid
||
2413 found_key
.type
!= dir_key
.type
)
2416 if (found_key
.offset
> range_end
)
2419 ret
= check_item_in_log(trans
, root
, log
, path
,
2424 if (found_key
.offset
== (u64
)-1)
2426 dir_key
.offset
= found_key
.offset
+ 1;
2428 btrfs_release_path(path
);
2429 if (range_end
== (u64
)-1)
2431 range_start
= range_end
+ 1;
2436 if (key_type
== BTRFS_DIR_LOG_ITEM_KEY
) {
2437 key_type
= BTRFS_DIR_LOG_INDEX_KEY
;
2438 dir_key
.type
= BTRFS_DIR_INDEX_KEY
;
2439 btrfs_release_path(path
);
2443 btrfs_release_path(path
);
2444 btrfs_free_path(log_path
);
2450 * the process_func used to replay items from the log tree. This
2451 * gets called in two different stages. The first stage just looks
2452 * for inodes and makes sure they are all copied into the subvolume.
2454 * The second stage copies all the other item types from the log into
2455 * the subvolume. The two stage approach is slower, but gets rid of
2456 * lots of complexity around inodes referencing other inodes that exist
2457 * only in the log (references come from either directory items or inode
2460 static int replay_one_buffer(struct btrfs_root
*log
, struct extent_buffer
*eb
,
2461 struct walk_control
*wc
, u64 gen
, int level
)
2464 struct btrfs_path
*path
;
2465 struct btrfs_root
*root
= wc
->replay_dest
;
2466 struct btrfs_key key
;
2470 ret
= btrfs_read_buffer(eb
, gen
, level
, NULL
);
2474 level
= btrfs_header_level(eb
);
2479 path
= btrfs_alloc_path();
2483 nritems
= btrfs_header_nritems(eb
);
2484 for (i
= 0; i
< nritems
; i
++) {
2485 btrfs_item_key_to_cpu(eb
, &key
, i
);
2487 /* inode keys are done during the first stage */
2488 if (key
.type
== BTRFS_INODE_ITEM_KEY
&&
2489 wc
->stage
== LOG_WALK_REPLAY_INODES
) {
2490 struct btrfs_inode_item
*inode_item
;
2493 inode_item
= btrfs_item_ptr(eb
, i
,
2494 struct btrfs_inode_item
);
2495 ret
= replay_xattr_deletes(wc
->trans
, root
, log
,
2496 path
, key
.objectid
);
2499 mode
= btrfs_inode_mode(eb
, inode_item
);
2500 if (S_ISDIR(mode
)) {
2501 ret
= replay_dir_deletes(wc
->trans
,
2502 root
, log
, path
, key
.objectid
, 0);
2506 ret
= overwrite_item(wc
->trans
, root
, path
,
2512 * Before replaying extents, truncate the inode to its
2513 * size. We need to do it now and not after log replay
2514 * because before an fsync we can have prealloc extents
2515 * added beyond the inode's i_size. If we did it after,
2516 * through orphan cleanup for example, we would drop
2517 * those prealloc extents just after replaying them.
2519 if (S_ISREG(mode
)) {
2520 struct inode
*inode
;
2523 inode
= read_one_inode(root
, key
.objectid
);
2528 from
= ALIGN(i_size_read(inode
),
2529 root
->fs_info
->sectorsize
);
2530 ret
= btrfs_drop_extents(wc
->trans
, root
, inode
,
2533 * If the nlink count is zero here, the iput
2534 * will free the inode. We bump it to make
2535 * sure it doesn't get freed until the link
2536 * count fixup is done.
2539 if (inode
->i_nlink
== 0)
2541 /* Update link count and nbytes. */
2542 ret
= btrfs_update_inode(wc
->trans
,
2550 ret
= link_to_fixup_dir(wc
->trans
, root
,
2551 path
, key
.objectid
);
2556 if (key
.type
== BTRFS_DIR_INDEX_KEY
&&
2557 wc
->stage
== LOG_WALK_REPLAY_DIR_INDEX
) {
2558 ret
= replay_one_dir_item(wc
->trans
, root
, path
,
2564 if (wc
->stage
< LOG_WALK_REPLAY_ALL
)
2567 /* these keys are simply copied */
2568 if (key
.type
== BTRFS_XATTR_ITEM_KEY
) {
2569 ret
= overwrite_item(wc
->trans
, root
, path
,
2573 } else if (key
.type
== BTRFS_INODE_REF_KEY
||
2574 key
.type
== BTRFS_INODE_EXTREF_KEY
) {
2575 ret
= add_inode_ref(wc
->trans
, root
, log
, path
,
2577 if (ret
&& ret
!= -ENOENT
)
2580 } else if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
2581 ret
= replay_one_extent(wc
->trans
, root
, path
,
2585 } else if (key
.type
== BTRFS_DIR_ITEM_KEY
) {
2586 ret
= replay_one_dir_item(wc
->trans
, root
, path
,
2592 btrfs_free_path(path
);
2596 static noinline
int walk_down_log_tree(struct btrfs_trans_handle
*trans
,
2597 struct btrfs_root
*root
,
2598 struct btrfs_path
*path
, int *level
,
2599 struct walk_control
*wc
)
2601 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2605 struct extent_buffer
*next
;
2606 struct extent_buffer
*cur
;
2607 struct extent_buffer
*parent
;
2611 WARN_ON(*level
< 0);
2612 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2614 while (*level
> 0) {
2615 struct btrfs_key first_key
;
2617 WARN_ON(*level
< 0);
2618 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2619 cur
= path
->nodes
[*level
];
2621 WARN_ON(btrfs_header_level(cur
) != *level
);
2623 if (path
->slots
[*level
] >=
2624 btrfs_header_nritems(cur
))
2627 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
2628 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
2629 btrfs_node_key_to_cpu(cur
, &first_key
, path
->slots
[*level
]);
2630 blocksize
= fs_info
->nodesize
;
2632 parent
= path
->nodes
[*level
];
2633 root_owner
= btrfs_header_owner(parent
);
2635 next
= btrfs_find_create_tree_block(fs_info
, bytenr
);
2637 return PTR_ERR(next
);
2640 ret
= wc
->process_func(root
, next
, wc
, ptr_gen
,
2643 free_extent_buffer(next
);
2647 path
->slots
[*level
]++;
2649 ret
= btrfs_read_buffer(next
, ptr_gen
,
2650 *level
- 1, &first_key
);
2652 free_extent_buffer(next
);
2657 btrfs_tree_lock(next
);
2658 btrfs_set_lock_blocking(next
);
2659 clean_tree_block(fs_info
, next
);
2660 btrfs_wait_tree_block_writeback(next
);
2661 btrfs_tree_unlock(next
);
2663 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY
, &next
->bflags
))
2664 clear_extent_buffer_dirty(next
);
2667 WARN_ON(root_owner
!=
2668 BTRFS_TREE_LOG_OBJECTID
);
2669 ret
= btrfs_free_and_pin_reserved_extent(
2673 free_extent_buffer(next
);
2677 free_extent_buffer(next
);
2680 ret
= btrfs_read_buffer(next
, ptr_gen
, *level
- 1, &first_key
);
2682 free_extent_buffer(next
);
2686 WARN_ON(*level
<= 0);
2687 if (path
->nodes
[*level
-1])
2688 free_extent_buffer(path
->nodes
[*level
-1]);
2689 path
->nodes
[*level
-1] = next
;
2690 *level
= btrfs_header_level(next
);
2691 path
->slots
[*level
] = 0;
2694 WARN_ON(*level
< 0);
2695 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2697 path
->slots
[*level
] = btrfs_header_nritems(path
->nodes
[*level
]);
2703 static noinline
int walk_up_log_tree(struct btrfs_trans_handle
*trans
,
2704 struct btrfs_root
*root
,
2705 struct btrfs_path
*path
, int *level
,
2706 struct walk_control
*wc
)
2708 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2714 for (i
= *level
; i
< BTRFS_MAX_LEVEL
- 1 && path
->nodes
[i
]; i
++) {
2715 slot
= path
->slots
[i
];
2716 if (slot
+ 1 < btrfs_header_nritems(path
->nodes
[i
])) {
2719 WARN_ON(*level
== 0);
2722 struct extent_buffer
*parent
;
2723 if (path
->nodes
[*level
] == root
->node
)
2724 parent
= path
->nodes
[*level
];
2726 parent
= path
->nodes
[*level
+ 1];
2728 root_owner
= btrfs_header_owner(parent
);
2729 ret
= wc
->process_func(root
, path
->nodes
[*level
], wc
,
2730 btrfs_header_generation(path
->nodes
[*level
]),
2736 struct extent_buffer
*next
;
2738 next
= path
->nodes
[*level
];
2741 btrfs_tree_lock(next
);
2742 btrfs_set_lock_blocking(next
);
2743 clean_tree_block(fs_info
, next
);
2744 btrfs_wait_tree_block_writeback(next
);
2745 btrfs_tree_unlock(next
);
2747 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY
, &next
->bflags
))
2748 clear_extent_buffer_dirty(next
);
2751 WARN_ON(root_owner
!= BTRFS_TREE_LOG_OBJECTID
);
2752 ret
= btrfs_free_and_pin_reserved_extent(
2754 path
->nodes
[*level
]->start
,
2755 path
->nodes
[*level
]->len
);
2759 free_extent_buffer(path
->nodes
[*level
]);
2760 path
->nodes
[*level
] = NULL
;
2768 * drop the reference count on the tree rooted at 'snap'. This traverses
2769 * the tree freeing any blocks that have a ref count of zero after being
2772 static int walk_log_tree(struct btrfs_trans_handle
*trans
,
2773 struct btrfs_root
*log
, struct walk_control
*wc
)
2775 struct btrfs_fs_info
*fs_info
= log
->fs_info
;
2779 struct btrfs_path
*path
;
2782 path
= btrfs_alloc_path();
2786 level
= btrfs_header_level(log
->node
);
2788 path
->nodes
[level
] = log
->node
;
2789 extent_buffer_get(log
->node
);
2790 path
->slots
[level
] = 0;
2793 wret
= walk_down_log_tree(trans
, log
, path
, &level
, wc
);
2801 wret
= walk_up_log_tree(trans
, log
, path
, &level
, wc
);
2810 /* was the root node processed? if not, catch it here */
2811 if (path
->nodes
[orig_level
]) {
2812 ret
= wc
->process_func(log
, path
->nodes
[orig_level
], wc
,
2813 btrfs_header_generation(path
->nodes
[orig_level
]),
2818 struct extent_buffer
*next
;
2820 next
= path
->nodes
[orig_level
];
2823 btrfs_tree_lock(next
);
2824 btrfs_set_lock_blocking(next
);
2825 clean_tree_block(fs_info
, next
);
2826 btrfs_wait_tree_block_writeback(next
);
2827 btrfs_tree_unlock(next
);
2829 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY
, &next
->bflags
))
2830 clear_extent_buffer_dirty(next
);
2833 WARN_ON(log
->root_key
.objectid
!=
2834 BTRFS_TREE_LOG_OBJECTID
);
2835 ret
= btrfs_free_and_pin_reserved_extent(fs_info
,
2836 next
->start
, next
->len
);
2843 btrfs_free_path(path
);
2848 * helper function to update the item for a given subvolumes log root
2849 * in the tree of log roots
2851 static int update_log_root(struct btrfs_trans_handle
*trans
,
2852 struct btrfs_root
*log
)
2854 struct btrfs_fs_info
*fs_info
= log
->fs_info
;
2857 if (log
->log_transid
== 1) {
2858 /* insert root item on the first sync */
2859 ret
= btrfs_insert_root(trans
, fs_info
->log_root_tree
,
2860 &log
->root_key
, &log
->root_item
);
2862 ret
= btrfs_update_root(trans
, fs_info
->log_root_tree
,
2863 &log
->root_key
, &log
->root_item
);
2868 static void wait_log_commit(struct btrfs_root
*root
, int transid
)
2871 int index
= transid
% 2;
2874 * we only allow two pending log transactions at a time,
2875 * so we know that if ours is more than 2 older than the
2876 * current transaction, we're done
2879 prepare_to_wait(&root
->log_commit_wait
[index
],
2880 &wait
, TASK_UNINTERRUPTIBLE
);
2882 if (!(root
->log_transid_committed
< transid
&&
2883 atomic_read(&root
->log_commit
[index
])))
2886 mutex_unlock(&root
->log_mutex
);
2888 mutex_lock(&root
->log_mutex
);
2890 finish_wait(&root
->log_commit_wait
[index
], &wait
);
2893 static void wait_for_writer(struct btrfs_root
*root
)
2898 prepare_to_wait(&root
->log_writer_wait
, &wait
,
2899 TASK_UNINTERRUPTIBLE
);
2900 if (!atomic_read(&root
->log_writers
))
2903 mutex_unlock(&root
->log_mutex
);
2905 mutex_lock(&root
->log_mutex
);
2907 finish_wait(&root
->log_writer_wait
, &wait
);
2910 static inline void btrfs_remove_log_ctx(struct btrfs_root
*root
,
2911 struct btrfs_log_ctx
*ctx
)
2916 mutex_lock(&root
->log_mutex
);
2917 list_del_init(&ctx
->list
);
2918 mutex_unlock(&root
->log_mutex
);
2922 * Invoked in log mutex context, or be sure there is no other task which
2923 * can access the list.
2925 static inline void btrfs_remove_all_log_ctxs(struct btrfs_root
*root
,
2926 int index
, int error
)
2928 struct btrfs_log_ctx
*ctx
;
2929 struct btrfs_log_ctx
*safe
;
2931 list_for_each_entry_safe(ctx
, safe
, &root
->log_ctxs
[index
], list
) {
2932 list_del_init(&ctx
->list
);
2933 ctx
->log_ret
= error
;
2936 INIT_LIST_HEAD(&root
->log_ctxs
[index
]);
2940 * btrfs_sync_log does sends a given tree log down to the disk and
2941 * updates the super blocks to record it. When this call is done,
2942 * you know that any inodes previously logged are safely on disk only
2945 * Any other return value means you need to call btrfs_commit_transaction.
2946 * Some of the edge cases for fsyncing directories that have had unlinks
2947 * or renames done in the past mean that sometimes the only safe
2948 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2949 * that has happened.
2951 int btrfs_sync_log(struct btrfs_trans_handle
*trans
,
2952 struct btrfs_root
*root
, struct btrfs_log_ctx
*ctx
)
2958 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2959 struct btrfs_root
*log
= root
->log_root
;
2960 struct btrfs_root
*log_root_tree
= fs_info
->log_root_tree
;
2961 int log_transid
= 0;
2962 struct btrfs_log_ctx root_log_ctx
;
2963 struct blk_plug plug
;
2965 mutex_lock(&root
->log_mutex
);
2966 log_transid
= ctx
->log_transid
;
2967 if (root
->log_transid_committed
>= log_transid
) {
2968 mutex_unlock(&root
->log_mutex
);
2969 return ctx
->log_ret
;
2972 index1
= log_transid
% 2;
2973 if (atomic_read(&root
->log_commit
[index1
])) {
2974 wait_log_commit(root
, log_transid
);
2975 mutex_unlock(&root
->log_mutex
);
2976 return ctx
->log_ret
;
2978 ASSERT(log_transid
== root
->log_transid
);
2979 atomic_set(&root
->log_commit
[index1
], 1);
2981 /* wait for previous tree log sync to complete */
2982 if (atomic_read(&root
->log_commit
[(index1
+ 1) % 2]))
2983 wait_log_commit(root
, log_transid
- 1);
2986 int batch
= atomic_read(&root
->log_batch
);
2987 /* when we're on an ssd, just kick the log commit out */
2988 if (!btrfs_test_opt(fs_info
, SSD
) &&
2989 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS
, &root
->state
)) {
2990 mutex_unlock(&root
->log_mutex
);
2991 schedule_timeout_uninterruptible(1);
2992 mutex_lock(&root
->log_mutex
);
2994 wait_for_writer(root
);
2995 if (batch
== atomic_read(&root
->log_batch
))
2999 /* bail out if we need to do a full commit */
3000 if (btrfs_need_log_full_commit(fs_info
, trans
)) {
3002 btrfs_free_logged_extents(log
, log_transid
);
3003 mutex_unlock(&root
->log_mutex
);
3007 if (log_transid
% 2 == 0)
3008 mark
= EXTENT_DIRTY
;
3012 /* we start IO on all the marked extents here, but we don't actually
3013 * wait for them until later.
3015 blk_start_plug(&plug
);
3016 ret
= btrfs_write_marked_extents(fs_info
, &log
->dirty_log_pages
, mark
);
3018 blk_finish_plug(&plug
);
3019 btrfs_abort_transaction(trans
, ret
);
3020 btrfs_free_logged_extents(log
, log_transid
);
3021 btrfs_set_log_full_commit(fs_info
, trans
);
3022 mutex_unlock(&root
->log_mutex
);
3026 btrfs_set_root_node(&log
->root_item
, log
->node
);
3028 root
->log_transid
++;
3029 log
->log_transid
= root
->log_transid
;
3030 root
->log_start_pid
= 0;
3032 * IO has been started, blocks of the log tree have WRITTEN flag set
3033 * in their headers. new modifications of the log will be written to
3034 * new positions. so it's safe to allow log writers to go in.
3036 mutex_unlock(&root
->log_mutex
);
3038 btrfs_init_log_ctx(&root_log_ctx
, NULL
);
3040 mutex_lock(&log_root_tree
->log_mutex
);
3041 atomic_inc(&log_root_tree
->log_batch
);
3042 atomic_inc(&log_root_tree
->log_writers
);
3044 index2
= log_root_tree
->log_transid
% 2;
3045 list_add_tail(&root_log_ctx
.list
, &log_root_tree
->log_ctxs
[index2
]);
3046 root_log_ctx
.log_transid
= log_root_tree
->log_transid
;
3048 mutex_unlock(&log_root_tree
->log_mutex
);
3050 ret
= update_log_root(trans
, log
);
3052 mutex_lock(&log_root_tree
->log_mutex
);
3053 if (atomic_dec_and_test(&log_root_tree
->log_writers
)) {
3054 /* atomic_dec_and_test implies a barrier */
3055 cond_wake_up_nomb(&log_root_tree
->log_writer_wait
);
3059 if (!list_empty(&root_log_ctx
.list
))
3060 list_del_init(&root_log_ctx
.list
);
3062 blk_finish_plug(&plug
);
3063 btrfs_set_log_full_commit(fs_info
, trans
);
3065 if (ret
!= -ENOSPC
) {
3066 btrfs_abort_transaction(trans
, ret
);
3067 mutex_unlock(&log_root_tree
->log_mutex
);
3070 btrfs_wait_tree_log_extents(log
, mark
);
3071 btrfs_free_logged_extents(log
, log_transid
);
3072 mutex_unlock(&log_root_tree
->log_mutex
);
3077 if (log_root_tree
->log_transid_committed
>= root_log_ctx
.log_transid
) {
3078 blk_finish_plug(&plug
);
3079 list_del_init(&root_log_ctx
.list
);
3080 mutex_unlock(&log_root_tree
->log_mutex
);
3081 ret
= root_log_ctx
.log_ret
;
3085 index2
= root_log_ctx
.log_transid
% 2;
3086 if (atomic_read(&log_root_tree
->log_commit
[index2
])) {
3087 blk_finish_plug(&plug
);
3088 ret
= btrfs_wait_tree_log_extents(log
, mark
);
3089 btrfs_wait_logged_extents(trans
, log
, log_transid
);
3090 wait_log_commit(log_root_tree
,
3091 root_log_ctx
.log_transid
);
3092 mutex_unlock(&log_root_tree
->log_mutex
);
3094 ret
= root_log_ctx
.log_ret
;
3097 ASSERT(root_log_ctx
.log_transid
== log_root_tree
->log_transid
);
3098 atomic_set(&log_root_tree
->log_commit
[index2
], 1);
3100 if (atomic_read(&log_root_tree
->log_commit
[(index2
+ 1) % 2])) {
3101 wait_log_commit(log_root_tree
,
3102 root_log_ctx
.log_transid
- 1);
3105 wait_for_writer(log_root_tree
);
3108 * now that we've moved on to the tree of log tree roots,
3109 * check the full commit flag again
3111 if (btrfs_need_log_full_commit(fs_info
, trans
)) {
3112 blk_finish_plug(&plug
);
3113 btrfs_wait_tree_log_extents(log
, mark
);
3114 btrfs_free_logged_extents(log
, log_transid
);
3115 mutex_unlock(&log_root_tree
->log_mutex
);
3117 goto out_wake_log_root
;
3120 ret
= btrfs_write_marked_extents(fs_info
,
3121 &log_root_tree
->dirty_log_pages
,
3122 EXTENT_DIRTY
| EXTENT_NEW
);
3123 blk_finish_plug(&plug
);
3125 btrfs_set_log_full_commit(fs_info
, trans
);
3126 btrfs_abort_transaction(trans
, ret
);
3127 btrfs_free_logged_extents(log
, log_transid
);
3128 mutex_unlock(&log_root_tree
->log_mutex
);
3129 goto out_wake_log_root
;
3131 ret
= btrfs_wait_tree_log_extents(log
, mark
);
3133 ret
= btrfs_wait_tree_log_extents(log_root_tree
,
3134 EXTENT_NEW
| EXTENT_DIRTY
);
3136 btrfs_set_log_full_commit(fs_info
, trans
);
3137 btrfs_free_logged_extents(log
, log_transid
);
3138 mutex_unlock(&log_root_tree
->log_mutex
);
3139 goto out_wake_log_root
;
3141 btrfs_wait_logged_extents(trans
, log
, log_transid
);
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
);
3212 /* I don't think this can happen but just in case */
3214 btrfs_abort_transaction(trans
, ret
);
3217 ret
= find_first_extent_bit(&log
->dirty_log_pages
,
3219 EXTENT_DIRTY
| EXTENT_NEW
| EXTENT_NEED_WAIT
,
3224 clear_extent_bits(&log
->dirty_log_pages
, start
, end
,
3225 EXTENT_DIRTY
| EXTENT_NEW
| EXTENT_NEED_WAIT
);
3229 * We may have short-circuited the log tree with the full commit logic
3230 * and left ordered extents on our list, so clear these out to keep us
3231 * from leaking inodes and memory.
3233 btrfs_free_logged_extents(log
, 0);
3234 btrfs_free_logged_extents(log
, 1);
3236 free_extent_buffer(log
->node
);
3241 * free all the extents used by the tree log. This should be called
3242 * at commit time of the full transaction
3244 int btrfs_free_log(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
)
3246 if (root
->log_root
) {
3247 free_log_tree(trans
, root
->log_root
);
3248 root
->log_root
= NULL
;
3253 int btrfs_free_log_root_tree(struct btrfs_trans_handle
*trans
,
3254 struct btrfs_fs_info
*fs_info
)
3256 if (fs_info
->log_root_tree
) {
3257 free_log_tree(trans
, fs_info
->log_root_tree
);
3258 fs_info
->log_root_tree
= NULL
;
3264 * If both a file and directory are logged, and unlinks or renames are
3265 * mixed in, we have a few interesting corners:
3267 * create file X in dir Y
3268 * link file X to X.link in dir Y
3270 * unlink file X but leave X.link
3273 * After a crash we would expect only X.link to exist. But file X
3274 * didn't get fsync'd again so the log has back refs for X and X.link.
3276 * We solve this by removing directory entries and inode backrefs from the
3277 * log when a file that was logged in the current transaction is
3278 * unlinked. Any later fsync will include the updated log entries, and
3279 * we'll be able to reconstruct the proper directory items from backrefs.
3281 * This optimizations allows us to avoid relogging the entire inode
3282 * or the entire directory.
3284 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle
*trans
,
3285 struct btrfs_root
*root
,
3286 const char *name
, int name_len
,
3287 struct btrfs_inode
*dir
, u64 index
)
3289 struct btrfs_root
*log
;
3290 struct btrfs_dir_item
*di
;
3291 struct btrfs_path
*path
;
3295 u64 dir_ino
= btrfs_ino(dir
);
3297 if (dir
->logged_trans
< trans
->transid
)
3300 ret
= join_running_log_trans(root
);
3304 mutex_lock(&dir
->log_mutex
);
3306 log
= root
->log_root
;
3307 path
= btrfs_alloc_path();
3313 di
= btrfs_lookup_dir_item(trans
, log
, path
, dir_ino
,
3314 name
, name_len
, -1);
3320 ret
= btrfs_delete_one_dir_name(trans
, log
, path
, di
);
3321 bytes_del
+= name_len
;
3327 btrfs_release_path(path
);
3328 di
= btrfs_lookup_dir_index_item(trans
, log
, path
, dir_ino
,
3329 index
, name
, name_len
, -1);
3335 ret
= btrfs_delete_one_dir_name(trans
, log
, path
, di
);
3336 bytes_del
+= name_len
;
3343 /* update the directory size in the log to reflect the names
3347 struct btrfs_key key
;
3349 key
.objectid
= dir_ino
;
3351 key
.type
= BTRFS_INODE_ITEM_KEY
;
3352 btrfs_release_path(path
);
3354 ret
= btrfs_search_slot(trans
, log
, &key
, path
, 0, 1);
3360 struct btrfs_inode_item
*item
;
3363 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3364 struct btrfs_inode_item
);
3365 i_size
= btrfs_inode_size(path
->nodes
[0], item
);
3366 if (i_size
> bytes_del
)
3367 i_size
-= bytes_del
;
3370 btrfs_set_inode_size(path
->nodes
[0], item
, i_size
);
3371 btrfs_mark_buffer_dirty(path
->nodes
[0]);
3374 btrfs_release_path(path
);
3377 btrfs_free_path(path
);
3379 mutex_unlock(&dir
->log_mutex
);
3380 if (ret
== -ENOSPC
) {
3381 btrfs_set_log_full_commit(root
->fs_info
, trans
);
3384 btrfs_abort_transaction(trans
, ret
);
3386 btrfs_end_log_trans(root
);
3391 /* see comments for btrfs_del_dir_entries_in_log */
3392 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle
*trans
,
3393 struct btrfs_root
*root
,
3394 const char *name
, int name_len
,
3395 struct btrfs_inode
*inode
, u64 dirid
)
3397 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3398 struct btrfs_root
*log
;
3402 if (inode
->logged_trans
< trans
->transid
)
3405 ret
= join_running_log_trans(root
);
3408 log
= root
->log_root
;
3409 mutex_lock(&inode
->log_mutex
);
3411 ret
= btrfs_del_inode_ref(trans
, log
, name
, name_len
, btrfs_ino(inode
),
3413 mutex_unlock(&inode
->log_mutex
);
3414 if (ret
== -ENOSPC
) {
3415 btrfs_set_log_full_commit(fs_info
, trans
);
3417 } else if (ret
< 0 && ret
!= -ENOENT
)
3418 btrfs_abort_transaction(trans
, ret
);
3419 btrfs_end_log_trans(root
);
3425 * creates a range item in the log for 'dirid'. first_offset and
3426 * last_offset tell us which parts of the key space the log should
3427 * be considered authoritative for.
3429 static noinline
int insert_dir_log_key(struct btrfs_trans_handle
*trans
,
3430 struct btrfs_root
*log
,
3431 struct btrfs_path
*path
,
3432 int key_type
, u64 dirid
,
3433 u64 first_offset
, u64 last_offset
)
3436 struct btrfs_key key
;
3437 struct btrfs_dir_log_item
*item
;
3439 key
.objectid
= dirid
;
3440 key
.offset
= first_offset
;
3441 if (key_type
== BTRFS_DIR_ITEM_KEY
)
3442 key
.type
= BTRFS_DIR_LOG_ITEM_KEY
;
3444 key
.type
= BTRFS_DIR_LOG_INDEX_KEY
;
3445 ret
= btrfs_insert_empty_item(trans
, log
, path
, &key
, sizeof(*item
));
3449 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3450 struct btrfs_dir_log_item
);
3451 btrfs_set_dir_log_end(path
->nodes
[0], item
, last_offset
);
3452 btrfs_mark_buffer_dirty(path
->nodes
[0]);
3453 btrfs_release_path(path
);
3458 * log all the items included in the current transaction for a given
3459 * directory. This also creates the range items in the log tree required
3460 * to replay anything deleted before the fsync
3462 static noinline
int log_dir_items(struct btrfs_trans_handle
*trans
,
3463 struct btrfs_root
*root
, struct btrfs_inode
*inode
,
3464 struct btrfs_path
*path
,
3465 struct btrfs_path
*dst_path
, int key_type
,
3466 struct btrfs_log_ctx
*ctx
,
3467 u64 min_offset
, u64
*last_offset_ret
)
3469 struct btrfs_key min_key
;
3470 struct btrfs_root
*log
= root
->log_root
;
3471 struct extent_buffer
*src
;
3476 u64 first_offset
= min_offset
;
3477 u64 last_offset
= (u64
)-1;
3478 u64 ino
= btrfs_ino(inode
);
3480 log
= root
->log_root
;
3482 min_key
.objectid
= ino
;
3483 min_key
.type
= key_type
;
3484 min_key
.offset
= min_offset
;
3486 ret
= btrfs_search_forward(root
, &min_key
, path
, trans
->transid
);
3489 * we didn't find anything from this transaction, see if there
3490 * is anything at all
3492 if (ret
!= 0 || min_key
.objectid
!= ino
|| min_key
.type
!= key_type
) {
3493 min_key
.objectid
= ino
;
3494 min_key
.type
= key_type
;
3495 min_key
.offset
= (u64
)-1;
3496 btrfs_release_path(path
);
3497 ret
= btrfs_search_slot(NULL
, root
, &min_key
, path
, 0, 0);
3499 btrfs_release_path(path
);
3502 ret
= btrfs_previous_item(root
, path
, ino
, key_type
);
3504 /* if ret == 0 there are items for this type,
3505 * create a range to tell us the last key of this type.
3506 * otherwise, there are no items in this directory after
3507 * *min_offset, and we create a range to indicate that.
3510 struct btrfs_key tmp
;
3511 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
,
3513 if (key_type
== tmp
.type
)
3514 first_offset
= max(min_offset
, tmp
.offset
) + 1;
3519 /* go backward to find any previous key */
3520 ret
= btrfs_previous_item(root
, path
, ino
, key_type
);
3522 struct btrfs_key tmp
;
3523 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
, path
->slots
[0]);
3524 if (key_type
== tmp
.type
) {
3525 first_offset
= tmp
.offset
;
3526 ret
= overwrite_item(trans
, log
, dst_path
,
3527 path
->nodes
[0], path
->slots
[0],
3535 btrfs_release_path(path
);
3537 /* find the first key from this transaction again */
3538 ret
= btrfs_search_slot(NULL
, root
, &min_key
, path
, 0, 0);
3539 if (WARN_ON(ret
!= 0))
3543 * we have a block from this transaction, log every item in it
3544 * from our directory
3547 struct btrfs_key tmp
;
3548 src
= path
->nodes
[0];
3549 nritems
= btrfs_header_nritems(src
);
3550 for (i
= path
->slots
[0]; i
< nritems
; i
++) {
3551 struct btrfs_dir_item
*di
;
3553 btrfs_item_key_to_cpu(src
, &min_key
, i
);
3555 if (min_key
.objectid
!= ino
|| min_key
.type
!= key_type
)
3557 ret
= overwrite_item(trans
, log
, dst_path
, src
, i
,
3565 * We must make sure that when we log a directory entry,
3566 * the corresponding inode, after log replay, has a
3567 * matching link count. For example:
3573 * xfs_io -c "fsync" mydir
3575 * <mount fs and log replay>
3577 * Would result in a fsync log that when replayed, our
3578 * file inode would have a link count of 1, but we get
3579 * two directory entries pointing to the same inode.
3580 * After removing one of the names, it would not be
3581 * possible to remove the other name, which resulted
3582 * always in stale file handle errors, and would not
3583 * be possible to rmdir the parent directory, since
3584 * its i_size could never decrement to the value
3585 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3587 di
= btrfs_item_ptr(src
, i
, struct btrfs_dir_item
);
3588 btrfs_dir_item_key_to_cpu(src
, di
, &tmp
);
3590 (btrfs_dir_transid(src
, di
) == trans
->transid
||
3591 btrfs_dir_type(src
, di
) == BTRFS_FT_DIR
) &&
3592 tmp
.type
!= BTRFS_ROOT_ITEM_KEY
)
3593 ctx
->log_new_dentries
= true;
3595 path
->slots
[0] = nritems
;
3598 * look ahead to the next item and see if it is also
3599 * from this directory and from this transaction
3601 ret
= btrfs_next_leaf(root
, path
);
3604 last_offset
= (u64
)-1;
3609 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
, path
->slots
[0]);
3610 if (tmp
.objectid
!= ino
|| tmp
.type
!= key_type
) {
3611 last_offset
= (u64
)-1;
3614 if (btrfs_header_generation(path
->nodes
[0]) != trans
->transid
) {
3615 ret
= overwrite_item(trans
, log
, dst_path
,
3616 path
->nodes
[0], path
->slots
[0],
3621 last_offset
= tmp
.offset
;
3626 btrfs_release_path(path
);
3627 btrfs_release_path(dst_path
);
3630 *last_offset_ret
= last_offset
;
3632 * insert the log range keys to indicate where the log
3635 ret
= insert_dir_log_key(trans
, log
, path
, key_type
,
3636 ino
, first_offset
, last_offset
);
3644 * logging directories is very similar to logging inodes, We find all the items
3645 * from the current transaction and write them to the log.
3647 * The recovery code scans the directory in the subvolume, and if it finds a
3648 * key in the range logged that is not present in the log tree, then it means
3649 * that dir entry was unlinked during the transaction.
3651 * In order for that scan to work, we must include one key smaller than
3652 * the smallest logged by this transaction and one key larger than the largest
3653 * key logged by this transaction.
3655 static noinline
int log_directory_changes(struct btrfs_trans_handle
*trans
,
3656 struct btrfs_root
*root
, struct btrfs_inode
*inode
,
3657 struct btrfs_path
*path
,
3658 struct btrfs_path
*dst_path
,
3659 struct btrfs_log_ctx
*ctx
)
3664 int key_type
= BTRFS_DIR_ITEM_KEY
;
3670 ret
= log_dir_items(trans
, root
, inode
, path
, dst_path
, key_type
,
3671 ctx
, min_key
, &max_key
);
3674 if (max_key
== (u64
)-1)
3676 min_key
= max_key
+ 1;
3679 if (key_type
== BTRFS_DIR_ITEM_KEY
) {
3680 key_type
= BTRFS_DIR_INDEX_KEY
;
3687 * a helper function to drop items from the log before we relog an
3688 * inode. max_key_type indicates the highest item type to remove.
3689 * This cannot be run for file data extents because it does not
3690 * free the extents they point to.
3692 static int drop_objectid_items(struct btrfs_trans_handle
*trans
,
3693 struct btrfs_root
*log
,
3694 struct btrfs_path
*path
,
3695 u64 objectid
, int max_key_type
)
3698 struct btrfs_key key
;
3699 struct btrfs_key found_key
;
3702 key
.objectid
= objectid
;
3703 key
.type
= max_key_type
;
3704 key
.offset
= (u64
)-1;
3707 ret
= btrfs_search_slot(trans
, log
, &key
, path
, -1, 1);
3708 BUG_ON(ret
== 0); /* Logic error */
3712 if (path
->slots
[0] == 0)
3716 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
3719 if (found_key
.objectid
!= objectid
)
3722 found_key
.offset
= 0;
3724 ret
= btrfs_bin_search(path
->nodes
[0], &found_key
, 0,
3727 ret
= btrfs_del_items(trans
, log
, path
, start_slot
,
3728 path
->slots
[0] - start_slot
+ 1);
3730 * If start slot isn't 0 then we don't need to re-search, we've
3731 * found the last guy with the objectid in this tree.
3733 if (ret
|| start_slot
!= 0)
3735 btrfs_release_path(path
);
3737 btrfs_release_path(path
);
3743 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3744 struct extent_buffer
*leaf
,
3745 struct btrfs_inode_item
*item
,
3746 struct inode
*inode
, int log_inode_only
,
3749 struct btrfs_map_token token
;
3751 btrfs_init_map_token(&token
);
3753 if (log_inode_only
) {
3754 /* set the generation to zero so the recover code
3755 * can tell the difference between an logging
3756 * just to say 'this inode exists' and a logging
3757 * to say 'update this inode with these values'
3759 btrfs_set_token_inode_generation(leaf
, item
, 0, &token
);
3760 btrfs_set_token_inode_size(leaf
, item
, logged_isize
, &token
);
3762 btrfs_set_token_inode_generation(leaf
, item
,
3763 BTRFS_I(inode
)->generation
,
3765 btrfs_set_token_inode_size(leaf
, item
, inode
->i_size
, &token
);
3768 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3769 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3770 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3771 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3773 btrfs_set_token_timespec_sec(leaf
, &item
->atime
,
3774 inode
->i_atime
.tv_sec
, &token
);
3775 btrfs_set_token_timespec_nsec(leaf
, &item
->atime
,
3776 inode
->i_atime
.tv_nsec
, &token
);
3778 btrfs_set_token_timespec_sec(leaf
, &item
->mtime
,
3779 inode
->i_mtime
.tv_sec
, &token
);
3780 btrfs_set_token_timespec_nsec(leaf
, &item
->mtime
,
3781 inode
->i_mtime
.tv_nsec
, &token
);
3783 btrfs_set_token_timespec_sec(leaf
, &item
->ctime
,
3784 inode
->i_ctime
.tv_sec
, &token
);
3785 btrfs_set_token_timespec_nsec(leaf
, &item
->ctime
,
3786 inode
->i_ctime
.tv_nsec
, &token
);
3788 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3791 btrfs_set_token_inode_sequence(leaf
, item
,
3792 inode_peek_iversion(inode
), &token
);
3793 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3794 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3795 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3796 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3799 static int log_inode_item(struct btrfs_trans_handle
*trans
,
3800 struct btrfs_root
*log
, struct btrfs_path
*path
,
3801 struct btrfs_inode
*inode
)
3803 struct btrfs_inode_item
*inode_item
;
3806 ret
= btrfs_insert_empty_item(trans
, log
, path
,
3807 &inode
->location
, sizeof(*inode_item
));
3808 if (ret
&& ret
!= -EEXIST
)
3810 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3811 struct btrfs_inode_item
);
3812 fill_inode_item(trans
, path
->nodes
[0], inode_item
, &inode
->vfs_inode
,
3814 btrfs_release_path(path
);
3818 static noinline
int copy_items(struct btrfs_trans_handle
*trans
,
3819 struct btrfs_inode
*inode
,
3820 struct btrfs_path
*dst_path
,
3821 struct btrfs_path
*src_path
, u64
*last_extent
,
3822 int start_slot
, int nr
, int inode_only
,
3825 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->vfs_inode
.i_sb
);
3826 unsigned long src_offset
;
3827 unsigned long dst_offset
;
3828 struct btrfs_root
*log
= inode
->root
->log_root
;
3829 struct btrfs_file_extent_item
*extent
;
3830 struct btrfs_inode_item
*inode_item
;
3831 struct extent_buffer
*src
= src_path
->nodes
[0];
3832 struct btrfs_key first_key
, last_key
, key
;
3834 struct btrfs_key
*ins_keys
;
3838 struct list_head ordered_sums
;
3839 int skip_csum
= inode
->flags
& BTRFS_INODE_NODATASUM
;
3840 bool has_extents
= false;
3841 bool need_find_last_extent
= true;
3844 INIT_LIST_HEAD(&ordered_sums
);
3846 ins_data
= kmalloc(nr
* sizeof(struct btrfs_key
) +
3847 nr
* sizeof(u32
), GFP_NOFS
);
3851 first_key
.objectid
= (u64
)-1;
3853 ins_sizes
= (u32
*)ins_data
;
3854 ins_keys
= (struct btrfs_key
*)(ins_data
+ nr
* sizeof(u32
));
3856 for (i
= 0; i
< nr
; i
++) {
3857 ins_sizes
[i
] = btrfs_item_size_nr(src
, i
+ start_slot
);
3858 btrfs_item_key_to_cpu(src
, ins_keys
+ i
, i
+ start_slot
);
3860 ret
= btrfs_insert_empty_items(trans
, log
, dst_path
,
3861 ins_keys
, ins_sizes
, nr
);
3867 for (i
= 0; i
< nr
; i
++, dst_path
->slots
[0]++) {
3868 dst_offset
= btrfs_item_ptr_offset(dst_path
->nodes
[0],
3869 dst_path
->slots
[0]);
3871 src_offset
= btrfs_item_ptr_offset(src
, start_slot
+ i
);
3874 last_key
= ins_keys
[i
];
3876 if (ins_keys
[i
].type
== BTRFS_INODE_ITEM_KEY
) {
3877 inode_item
= btrfs_item_ptr(dst_path
->nodes
[0],
3879 struct btrfs_inode_item
);
3880 fill_inode_item(trans
, dst_path
->nodes
[0], inode_item
,
3882 inode_only
== LOG_INODE_EXISTS
,
3885 copy_extent_buffer(dst_path
->nodes
[0], src
, dst_offset
,
3886 src_offset
, ins_sizes
[i
]);
3890 * We set need_find_last_extent here in case we know we were
3891 * processing other items and then walk into the first extent in
3892 * the inode. If we don't hit an extent then nothing changes,
3893 * we'll do the last search the next time around.
3895 if (ins_keys
[i
].type
== BTRFS_EXTENT_DATA_KEY
) {
3897 if (first_key
.objectid
== (u64
)-1)
3898 first_key
= ins_keys
[i
];
3900 need_find_last_extent
= false;
3903 /* take a reference on file data extents so that truncates
3904 * or deletes of this inode don't have to relog the inode
3907 if (ins_keys
[i
].type
== BTRFS_EXTENT_DATA_KEY
&&
3910 extent
= btrfs_item_ptr(src
, start_slot
+ i
,
3911 struct btrfs_file_extent_item
);
3913 if (btrfs_file_extent_generation(src
, extent
) < trans
->transid
)
3916 found_type
= btrfs_file_extent_type(src
, extent
);
3917 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
3919 ds
= btrfs_file_extent_disk_bytenr(src
,
3921 /* ds == 0 is a hole */
3925 dl
= btrfs_file_extent_disk_num_bytes(src
,
3927 cs
= btrfs_file_extent_offset(src
, extent
);
3928 cl
= btrfs_file_extent_num_bytes(src
,
3930 if (btrfs_file_extent_compression(src
,
3936 ret
= btrfs_lookup_csums_range(
3938 ds
+ cs
, ds
+ cs
+ cl
- 1,
3941 btrfs_release_path(dst_path
);
3949 btrfs_mark_buffer_dirty(dst_path
->nodes
[0]);
3950 btrfs_release_path(dst_path
);
3954 * we have to do this after the loop above to avoid changing the
3955 * log tree while trying to change the log tree.
3958 while (!list_empty(&ordered_sums
)) {
3959 struct btrfs_ordered_sum
*sums
= list_entry(ordered_sums
.next
,
3960 struct btrfs_ordered_sum
,
3963 ret
= btrfs_csum_file_blocks(trans
, log
, sums
);
3964 list_del(&sums
->list
);
3971 if (need_find_last_extent
&& *last_extent
== first_key
.offset
) {
3973 * We don't have any leafs between our current one and the one
3974 * we processed before that can have file extent items for our
3975 * inode (and have a generation number smaller than our current
3978 need_find_last_extent
= false;
3982 * Because we use btrfs_search_forward we could skip leaves that were
3983 * not modified and then assume *last_extent is valid when it really
3984 * isn't. So back up to the previous leaf and read the end of the last
3985 * extent before we go and fill in holes.
3987 if (need_find_last_extent
) {
3990 ret
= btrfs_prev_leaf(inode
->root
, src_path
);
3995 if (src_path
->slots
[0])
3996 src_path
->slots
[0]--;
3997 src
= src_path
->nodes
[0];
3998 btrfs_item_key_to_cpu(src
, &key
, src_path
->slots
[0]);
3999 if (key
.objectid
!= btrfs_ino(inode
) ||
4000 key
.type
!= BTRFS_EXTENT_DATA_KEY
)
4002 extent
= btrfs_item_ptr(src
, src_path
->slots
[0],
4003 struct btrfs_file_extent_item
);
4004 if (btrfs_file_extent_type(src
, extent
) ==
4005 BTRFS_FILE_EXTENT_INLINE
) {
4006 len
= btrfs_file_extent_inline_len(src
,
4009 *last_extent
= ALIGN(key
.offset
+ len
,
4010 fs_info
->sectorsize
);
4012 len
= btrfs_file_extent_num_bytes(src
, extent
);
4013 *last_extent
= key
.offset
+ len
;
4017 /* So we did prev_leaf, now we need to move to the next leaf, but a few
4018 * things could have happened
4020 * 1) A merge could have happened, so we could currently be on a leaf
4021 * that holds what we were copying in the first place.
4022 * 2) A split could have happened, and now not all of the items we want
4023 * are on the same leaf.
4025 * So we need to adjust how we search for holes, we need to drop the
4026 * path and re-search for the first extent key we found, and then walk
4027 * forward until we hit the last one we copied.
4029 if (need_find_last_extent
) {
4030 /* btrfs_prev_leaf could return 1 without releasing the path */
4031 btrfs_release_path(src_path
);
4032 ret
= btrfs_search_slot(NULL
, inode
->root
, &first_key
,
4037 src
= src_path
->nodes
[0];
4038 i
= src_path
->slots
[0];
4044 * Ok so here we need to go through and fill in any holes we may have
4045 * to make sure that holes are punched for those areas in case they had
4046 * extents previously.
4052 if (i
>= btrfs_header_nritems(src_path
->nodes
[0])) {
4053 ret
= btrfs_next_leaf(inode
->root
, src_path
);
4057 src
= src_path
->nodes
[0];
4059 need_find_last_extent
= true;
4062 btrfs_item_key_to_cpu(src
, &key
, i
);
4063 if (!btrfs_comp_cpu_keys(&key
, &last_key
))
4065 if (key
.objectid
!= btrfs_ino(inode
) ||
4066 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
4070 extent
= btrfs_item_ptr(src
, i
, struct btrfs_file_extent_item
);
4071 if (btrfs_file_extent_type(src
, extent
) ==
4072 BTRFS_FILE_EXTENT_INLINE
) {
4073 len
= btrfs_file_extent_inline_len(src
, i
, extent
);
4074 extent_end
= ALIGN(key
.offset
+ len
,
4075 fs_info
->sectorsize
);
4077 len
= btrfs_file_extent_num_bytes(src
, extent
);
4078 extent_end
= key
.offset
+ len
;
4082 if (*last_extent
== key
.offset
) {
4083 *last_extent
= extent_end
;
4086 offset
= *last_extent
;
4087 len
= key
.offset
- *last_extent
;
4088 ret
= btrfs_insert_file_extent(trans
, log
, btrfs_ino(inode
),
4089 offset
, 0, 0, len
, 0, len
, 0, 0, 0);
4092 *last_extent
= extent_end
;
4096 * Check if there is a hole between the last extent found in our leaf
4097 * and the first extent in the next leaf. If there is one, we need to
4098 * log an explicit hole so that at replay time we can punch the hole.
4101 key
.objectid
== btrfs_ino(inode
) &&
4102 key
.type
== BTRFS_EXTENT_DATA_KEY
&&
4103 i
== btrfs_header_nritems(src_path
->nodes
[0])) {
4104 ret
= btrfs_next_leaf(inode
->root
, src_path
);
4105 need_find_last_extent
= true;
4108 } else if (ret
== 0) {
4109 btrfs_item_key_to_cpu(src_path
->nodes
[0], &key
,
4110 src_path
->slots
[0]);
4111 if (key
.objectid
== btrfs_ino(inode
) &&
4112 key
.type
== BTRFS_EXTENT_DATA_KEY
&&
4113 *last_extent
< key
.offset
) {
4114 const u64 len
= key
.offset
- *last_extent
;
4116 ret
= btrfs_insert_file_extent(trans
, log
,
4125 * Need to let the callers know we dropped the path so they should
4128 if (!ret
&& need_find_last_extent
)
4133 static int extent_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
4135 struct extent_map
*em1
, *em2
;
4137 em1
= list_entry(a
, struct extent_map
, list
);
4138 em2
= list_entry(b
, struct extent_map
, list
);
4140 if (em1
->start
< em2
->start
)
4142 else if (em1
->start
> em2
->start
)
4147 static int wait_ordered_extents(struct btrfs_trans_handle
*trans
,
4148 struct inode
*inode
,
4149 struct btrfs_root
*root
,
4150 const struct extent_map
*em
,
4151 const struct list_head
*logged_list
,
4152 bool *ordered_io_error
)
4154 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4155 struct btrfs_ordered_extent
*ordered
;
4156 struct btrfs_root
*log
= root
->log_root
;
4157 u64 mod_start
= em
->mod_start
;
4158 u64 mod_len
= em
->mod_len
;
4159 const bool skip_csum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
4162 LIST_HEAD(ordered_sums
);
4165 *ordered_io_error
= false;
4167 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
4168 em
->block_start
== EXTENT_MAP_HOLE
)
4172 * Wait far any ordered extent that covers our extent map. If it
4173 * finishes without an error, first check and see if our csums are on
4174 * our outstanding ordered extents.
4176 list_for_each_entry(ordered
, logged_list
, log_list
) {
4177 struct btrfs_ordered_sum
*sum
;
4182 if (ordered
->file_offset
+ ordered
->len
<= mod_start
||
4183 mod_start
+ mod_len
<= ordered
->file_offset
)
4186 if (!test_bit(BTRFS_ORDERED_IO_DONE
, &ordered
->flags
) &&
4187 !test_bit(BTRFS_ORDERED_IOERR
, &ordered
->flags
) &&
4188 !test_bit(BTRFS_ORDERED_DIRECT
, &ordered
->flags
)) {
4189 const u64 start
= ordered
->file_offset
;
4190 const u64 end
= ordered
->file_offset
+ ordered
->len
- 1;
4192 WARN_ON(ordered
->inode
!= inode
);
4193 filemap_fdatawrite_range(inode
->i_mapping
, start
, end
);
4196 wait_event(ordered
->wait
,
4197 (test_bit(BTRFS_ORDERED_IO_DONE
, &ordered
->flags
) ||
4198 test_bit(BTRFS_ORDERED_IOERR
, &ordered
->flags
)));
4200 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered
->flags
)) {
4202 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
4203 * i_mapping flags, so that the next fsync won't get
4204 * an outdated io error too.
4206 filemap_check_errors(inode
->i_mapping
);
4207 *ordered_io_error
= true;
4211 * We are going to copy all the csums on this ordered extent, so
4212 * go ahead and adjust mod_start and mod_len in case this
4213 * ordered extent has already been logged.
4215 if (ordered
->file_offset
> mod_start
) {
4216 if (ordered
->file_offset
+ ordered
->len
>=
4217 mod_start
+ mod_len
)
4218 mod_len
= ordered
->file_offset
- mod_start
;
4220 * If we have this case
4222 * |--------- logged extent ---------|
4223 * |----- ordered extent ----|
4225 * Just don't mess with mod_start and mod_len, we'll
4226 * just end up logging more csums than we need and it
4230 if (ordered
->file_offset
+ ordered
->len
<
4231 mod_start
+ mod_len
) {
4232 mod_len
= (mod_start
+ mod_len
) -
4233 (ordered
->file_offset
+ ordered
->len
);
4234 mod_start
= ordered
->file_offset
+
4245 * To keep us from looping for the above case of an ordered
4246 * extent that falls inside of the logged extent.
4248 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM
,
4252 list_for_each_entry(sum
, &ordered
->list
, list
) {
4253 ret
= btrfs_csum_file_blocks(trans
, log
, sum
);
4259 if (*ordered_io_error
|| !mod_len
|| ret
|| skip_csum
)
4262 if (em
->compress_type
) {
4264 csum_len
= max(em
->block_len
, em
->orig_block_len
);
4266 csum_offset
= mod_start
- em
->start
;
4270 /* block start is already adjusted for the file extent offset. */
4271 ret
= btrfs_lookup_csums_range(fs_info
->csum_root
,
4272 em
->block_start
+ csum_offset
,
4273 em
->block_start
+ csum_offset
+
4274 csum_len
- 1, &ordered_sums
, 0);
4278 while (!list_empty(&ordered_sums
)) {
4279 struct btrfs_ordered_sum
*sums
= list_entry(ordered_sums
.next
,
4280 struct btrfs_ordered_sum
,
4283 ret
= btrfs_csum_file_blocks(trans
, log
, sums
);
4284 list_del(&sums
->list
);
4291 static int log_one_extent(struct btrfs_trans_handle
*trans
,
4292 struct btrfs_inode
*inode
, struct btrfs_root
*root
,
4293 const struct extent_map
*em
,
4294 struct btrfs_path
*path
,
4295 const struct list_head
*logged_list
,
4296 struct btrfs_log_ctx
*ctx
)
4298 struct btrfs_root
*log
= root
->log_root
;
4299 struct btrfs_file_extent_item
*fi
;
4300 struct extent_buffer
*leaf
;
4301 struct btrfs_map_token token
;
4302 struct btrfs_key key
;
4303 u64 extent_offset
= em
->start
- em
->orig_start
;
4306 int extent_inserted
= 0;
4307 bool ordered_io_err
= false;
4309 ret
= wait_ordered_extents(trans
, &inode
->vfs_inode
, root
, em
,
4310 logged_list
, &ordered_io_err
);
4314 if (ordered_io_err
) {
4319 btrfs_init_map_token(&token
);
4321 ret
= __btrfs_drop_extents(trans
, log
, &inode
->vfs_inode
, path
, em
->start
,
4322 em
->start
+ em
->len
, NULL
, 0, 1,
4323 sizeof(*fi
), &extent_inserted
);
4327 if (!extent_inserted
) {
4328 key
.objectid
= btrfs_ino(inode
);
4329 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4330 key
.offset
= em
->start
;
4332 ret
= btrfs_insert_empty_item(trans
, log
, path
, &key
,
4337 leaf
= path
->nodes
[0];
4338 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4339 struct btrfs_file_extent_item
);
4341 btrfs_set_token_file_extent_generation(leaf
, fi
, trans
->transid
,
4343 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
4344 btrfs_set_token_file_extent_type(leaf
, fi
,
4345 BTRFS_FILE_EXTENT_PREALLOC
,
4348 btrfs_set_token_file_extent_type(leaf
, fi
,
4349 BTRFS_FILE_EXTENT_REG
,
4352 block_len
= max(em
->block_len
, em
->orig_block_len
);
4353 if (em
->compress_type
!= BTRFS_COMPRESS_NONE
) {
4354 btrfs_set_token_file_extent_disk_bytenr(leaf
, fi
,
4357 btrfs_set_token_file_extent_disk_num_bytes(leaf
, fi
, block_len
,
4359 } else if (em
->block_start
< EXTENT_MAP_LAST_BYTE
) {
4360 btrfs_set_token_file_extent_disk_bytenr(leaf
, fi
,
4362 extent_offset
, &token
);
4363 btrfs_set_token_file_extent_disk_num_bytes(leaf
, fi
, block_len
,
4366 btrfs_set_token_file_extent_disk_bytenr(leaf
, fi
, 0, &token
);
4367 btrfs_set_token_file_extent_disk_num_bytes(leaf
, fi
, 0,
4371 btrfs_set_token_file_extent_offset(leaf
, fi
, extent_offset
, &token
);
4372 btrfs_set_token_file_extent_num_bytes(leaf
, fi
, em
->len
, &token
);
4373 btrfs_set_token_file_extent_ram_bytes(leaf
, fi
, em
->ram_bytes
, &token
);
4374 btrfs_set_token_file_extent_compression(leaf
, fi
, em
->compress_type
,
4376 btrfs_set_token_file_extent_encryption(leaf
, fi
, 0, &token
);
4377 btrfs_set_token_file_extent_other_encoding(leaf
, fi
, 0, &token
);
4378 btrfs_mark_buffer_dirty(leaf
);
4380 btrfs_release_path(path
);
4386 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4387 * lose them after doing a fast fsync and replaying the log. We scan the
4388 * subvolume's root instead of iterating the inode's extent map tree because
4389 * otherwise we can log incorrect extent items based on extent map conversion.
4390 * That can happen due to the fact that extent maps are merged when they
4391 * are not in the extent map tree's list of modified extents.
4393 static int btrfs_log_prealloc_extents(struct btrfs_trans_handle
*trans
,
4394 struct btrfs_inode
*inode
,
4395 struct btrfs_path
*path
)
4397 struct btrfs_root
*root
= inode
->root
;
4398 struct btrfs_key key
;
4399 const u64 i_size
= i_size_read(&inode
->vfs_inode
);
4400 const u64 ino
= btrfs_ino(inode
);
4401 struct btrfs_path
*dst_path
= NULL
;
4402 u64 last_extent
= (u64
)-1;
4407 if (!(inode
->flags
& BTRFS_INODE_PREALLOC
))
4411 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4412 key
.offset
= i_size
;
4413 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4418 struct extent_buffer
*leaf
= path
->nodes
[0];
4419 int slot
= path
->slots
[0];
4421 if (slot
>= btrfs_header_nritems(leaf
)) {
4423 ret
= copy_items(trans
, inode
, dst_path
, path
,
4424 &last_extent
, start_slot
,
4430 ret
= btrfs_next_leaf(root
, path
);
4440 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
4441 if (key
.objectid
> ino
)
4443 if (WARN_ON_ONCE(key
.objectid
< ino
) ||
4444 key
.type
< BTRFS_EXTENT_DATA_KEY
||
4445 key
.offset
< i_size
) {
4449 if (last_extent
== (u64
)-1) {
4450 last_extent
= key
.offset
;
4452 * Avoid logging extent items logged in past fsync calls
4453 * and leading to duplicate keys in the log tree.
4456 ret
= btrfs_truncate_inode_items(trans
,
4460 BTRFS_EXTENT_DATA_KEY
);
4461 } while (ret
== -EAGAIN
);
4470 dst_path
= btrfs_alloc_path();
4478 ret
= copy_items(trans
, inode
, dst_path
, path
, &last_extent
,
4479 start_slot
, ins_nr
, 1, 0);
4484 btrfs_release_path(path
);
4485 btrfs_free_path(dst_path
);
4489 static int btrfs_log_changed_extents(struct btrfs_trans_handle
*trans
,
4490 struct btrfs_root
*root
,
4491 struct btrfs_inode
*inode
,
4492 struct btrfs_path
*path
,
4493 struct list_head
*logged_list
,
4494 struct btrfs_log_ctx
*ctx
,
4498 struct extent_map
*em
, *n
;
4499 struct list_head extents
;
4500 struct extent_map_tree
*tree
= &inode
->extent_tree
;
4501 u64 logged_start
, logged_end
;
4506 INIT_LIST_HEAD(&extents
);
4508 down_write(&inode
->dio_sem
);
4509 write_lock(&tree
->lock
);
4510 test_gen
= root
->fs_info
->last_trans_committed
;
4511 logged_start
= start
;
4514 list_for_each_entry_safe(em
, n
, &tree
->modified_extents
, list
) {
4515 list_del_init(&em
->list
);
4517 * Just an arbitrary number, this can be really CPU intensive
4518 * once we start getting a lot of extents, and really once we
4519 * have a bunch of extents we just want to commit since it will
4522 if (++num
> 32768) {
4523 list_del_init(&tree
->modified_extents
);
4528 if (em
->generation
<= test_gen
)
4531 /* We log prealloc extents beyond eof later. */
4532 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) &&
4533 em
->start
>= i_size_read(&inode
->vfs_inode
))
4536 if (em
->start
< logged_start
)
4537 logged_start
= em
->start
;
4538 if ((em
->start
+ em
->len
- 1) > logged_end
)
4539 logged_end
= em
->start
+ em
->len
- 1;
4541 /* Need a ref to keep it from getting evicted from cache */
4542 refcount_inc(&em
->refs
);
4543 set_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
4544 list_add_tail(&em
->list
, &extents
);
4548 list_sort(NULL
, &extents
, extent_cmp
);
4549 btrfs_get_logged_extents(inode
, logged_list
, logged_start
, logged_end
);
4551 * Some ordered extents started by fsync might have completed
4552 * before we could collect them into the list logged_list, which
4553 * means they're gone, not in our logged_list nor in the inode's
4554 * ordered tree. We want the application/user space to know an
4555 * error happened while attempting to persist file data so that
4556 * it can take proper action. If such error happened, we leave
4557 * without writing to the log tree and the fsync must report the
4558 * file data write error and not commit the current transaction.
4560 ret
= filemap_check_errors(inode
->vfs_inode
.i_mapping
);
4564 while (!list_empty(&extents
)) {
4565 em
= list_entry(extents
.next
, struct extent_map
, list
);
4567 list_del_init(&em
->list
);
4570 * If we had an error we just need to delete everybody from our
4574 clear_em_logging(tree
, em
);
4575 free_extent_map(em
);
4579 write_unlock(&tree
->lock
);
4581 ret
= log_one_extent(trans
, inode
, root
, em
, path
, logged_list
,
4583 write_lock(&tree
->lock
);
4584 clear_em_logging(tree
, em
);
4585 free_extent_map(em
);
4587 WARN_ON(!list_empty(&extents
));
4588 write_unlock(&tree
->lock
);
4589 up_write(&inode
->dio_sem
);
4591 btrfs_release_path(path
);
4593 ret
= btrfs_log_prealloc_extents(trans
, inode
, path
);
4598 static int logged_inode_size(struct btrfs_root
*log
, struct btrfs_inode
*inode
,
4599 struct btrfs_path
*path
, u64
*size_ret
)
4601 struct btrfs_key key
;
4604 key
.objectid
= btrfs_ino(inode
);
4605 key
.type
= BTRFS_INODE_ITEM_KEY
;
4608 ret
= btrfs_search_slot(NULL
, log
, &key
, path
, 0, 0);
4611 } else if (ret
> 0) {
4614 struct btrfs_inode_item
*item
;
4616 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4617 struct btrfs_inode_item
);
4618 *size_ret
= btrfs_inode_size(path
->nodes
[0], item
);
4621 btrfs_release_path(path
);
4626 * At the moment we always log all xattrs. This is to figure out at log replay
4627 * time which xattrs must have their deletion replayed. If a xattr is missing
4628 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4629 * because if a xattr is deleted, the inode is fsynced and a power failure
4630 * happens, causing the log to be replayed the next time the fs is mounted,
4631 * we want the xattr to not exist anymore (same behaviour as other filesystems
4632 * with a journal, ext3/4, xfs, f2fs, etc).
4634 static int btrfs_log_all_xattrs(struct btrfs_trans_handle
*trans
,
4635 struct btrfs_root
*root
,
4636 struct btrfs_inode
*inode
,
4637 struct btrfs_path
*path
,
4638 struct btrfs_path
*dst_path
)
4641 struct btrfs_key key
;
4642 const u64 ino
= btrfs_ino(inode
);
4647 key
.type
= BTRFS_XATTR_ITEM_KEY
;
4650 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4655 int slot
= path
->slots
[0];
4656 struct extent_buffer
*leaf
= path
->nodes
[0];
4657 int nritems
= btrfs_header_nritems(leaf
);
4659 if (slot
>= nritems
) {
4661 u64 last_extent
= 0;
4663 ret
= copy_items(trans
, inode
, dst_path
, path
,
4664 &last_extent
, start_slot
,
4666 /* can't be 1, extent items aren't processed */
4672 ret
= btrfs_next_leaf(root
, path
);
4680 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
4681 if (key
.objectid
!= ino
|| key
.type
!= BTRFS_XATTR_ITEM_KEY
)
4691 u64 last_extent
= 0;
4693 ret
= copy_items(trans
, inode
, dst_path
, path
,
4694 &last_extent
, start_slot
,
4696 /* can't be 1, extent items aren't processed */
4706 * If the no holes feature is enabled we need to make sure any hole between the
4707 * last extent and the i_size of our inode is explicitly marked in the log. This
4708 * is to make sure that doing something like:
4710 * 1) create file with 128Kb of data
4711 * 2) truncate file to 64Kb
4712 * 3) truncate file to 256Kb
4714 * 5) <crash/power failure>
4715 * 6) mount fs and trigger log replay
4717 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4718 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4719 * file correspond to a hole. The presence of explicit holes in a log tree is
4720 * what guarantees that log replay will remove/adjust file extent items in the
4723 * Here we do not need to care about holes between extents, that is already done
4724 * by copy_items(). We also only need to do this in the full sync path, where we
4725 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4726 * lookup the list of modified extent maps and if any represents a hole, we
4727 * insert a corresponding extent representing a hole in the log tree.
4729 static int btrfs_log_trailing_hole(struct btrfs_trans_handle
*trans
,
4730 struct btrfs_root
*root
,
4731 struct btrfs_inode
*inode
,
4732 struct btrfs_path
*path
)
4734 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4736 struct btrfs_key key
;
4739 struct extent_buffer
*leaf
;
4740 struct btrfs_root
*log
= root
->log_root
;
4741 const u64 ino
= btrfs_ino(inode
);
4742 const u64 i_size
= i_size_read(&inode
->vfs_inode
);
4744 if (!btrfs_fs_incompat(fs_info
, NO_HOLES
))
4748 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4749 key
.offset
= (u64
)-1;
4751 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4756 ASSERT(path
->slots
[0] > 0);
4758 leaf
= path
->nodes
[0];
4759 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4761 if (key
.objectid
!= ino
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
4762 /* inode does not have any extents */
4766 struct btrfs_file_extent_item
*extent
;
4770 * If there's an extent beyond i_size, an explicit hole was
4771 * already inserted by copy_items().
4773 if (key
.offset
>= i_size
)
4776 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
4777 struct btrfs_file_extent_item
);
4779 if (btrfs_file_extent_type(leaf
, extent
) ==
4780 BTRFS_FILE_EXTENT_INLINE
) {
4781 len
= btrfs_file_extent_inline_len(leaf
,
4784 ASSERT(len
== i_size
||
4785 (len
== fs_info
->sectorsize
&&
4786 btrfs_file_extent_compression(leaf
, extent
) !=
4787 BTRFS_COMPRESS_NONE
));
4791 len
= btrfs_file_extent_num_bytes(leaf
, extent
);
4792 /* Last extent goes beyond i_size, no need to log a hole. */
4793 if (key
.offset
+ len
> i_size
)
4795 hole_start
= key
.offset
+ len
;
4796 hole_size
= i_size
- hole_start
;
4798 btrfs_release_path(path
);
4800 /* Last extent ends at i_size. */
4804 hole_size
= ALIGN(hole_size
, fs_info
->sectorsize
);
4805 ret
= btrfs_insert_file_extent(trans
, log
, ino
, hole_start
, 0, 0,
4806 hole_size
, 0, hole_size
, 0, 0, 0);
4811 * When we are logging a new inode X, check if it doesn't have a reference that
4812 * matches the reference from some other inode Y created in a past transaction
4813 * and that was renamed in the current transaction. If we don't do this, then at
4814 * log replay time we can lose inode Y (and all its files if it's a directory):
4817 * echo "hello world" > /mnt/x/foobar
4820 * mkdir /mnt/x # or touch /mnt/x
4821 * xfs_io -c fsync /mnt/x
4823 * mount fs, trigger log replay
4825 * After the log replay procedure, we would lose the first directory and all its
4826 * files (file foobar).
4827 * For the case where inode Y is not a directory we simply end up losing it:
4829 * echo "123" > /mnt/foo
4831 * mv /mnt/foo /mnt/bar
4832 * echo "abc" > /mnt/foo
4833 * xfs_io -c fsync /mnt/foo
4836 * We also need this for cases where a snapshot entry is replaced by some other
4837 * entry (file or directory) otherwise we end up with an unreplayable log due to
4838 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4839 * if it were a regular entry:
4842 * btrfs subvolume snapshot /mnt /mnt/x/snap
4843 * btrfs subvolume delete /mnt/x/snap
4846 * fsync /mnt/x or fsync some new file inside it
4849 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4850 * the same transaction.
4852 static int btrfs_check_ref_name_override(struct extent_buffer
*eb
,
4854 const struct btrfs_key
*key
,
4855 struct btrfs_inode
*inode
,
4859 struct btrfs_path
*search_path
;
4862 u32 item_size
= btrfs_item_size_nr(eb
, slot
);
4864 unsigned long ptr
= btrfs_item_ptr_offset(eb
, slot
);
4866 search_path
= btrfs_alloc_path();
4869 search_path
->search_commit_root
= 1;
4870 search_path
->skip_locking
= 1;
4872 while (cur_offset
< item_size
) {
4876 unsigned long name_ptr
;
4877 struct btrfs_dir_item
*di
;
4879 if (key
->type
== BTRFS_INODE_REF_KEY
) {
4880 struct btrfs_inode_ref
*iref
;
4882 iref
= (struct btrfs_inode_ref
*)(ptr
+ cur_offset
);
4883 parent
= key
->offset
;
4884 this_name_len
= btrfs_inode_ref_name_len(eb
, iref
);
4885 name_ptr
= (unsigned long)(iref
+ 1);
4886 this_len
= sizeof(*iref
) + this_name_len
;
4888 struct btrfs_inode_extref
*extref
;
4890 extref
= (struct btrfs_inode_extref
*)(ptr
+
4892 parent
= btrfs_inode_extref_parent(eb
, extref
);
4893 this_name_len
= btrfs_inode_extref_name_len(eb
, extref
);
4894 name_ptr
= (unsigned long)&extref
->name
;
4895 this_len
= sizeof(*extref
) + this_name_len
;
4898 if (this_name_len
> name_len
) {
4901 new_name
= krealloc(name
, this_name_len
, GFP_NOFS
);
4906 name_len
= this_name_len
;
4910 read_extent_buffer(eb
, name
, name_ptr
, this_name_len
);
4911 di
= btrfs_lookup_dir_item(NULL
, inode
->root
, search_path
,
4912 parent
, name
, this_name_len
, 0);
4913 if (di
&& !IS_ERR(di
)) {
4914 struct btrfs_key di_key
;
4916 btrfs_dir_item_key_to_cpu(search_path
->nodes
[0],
4918 if (di_key
.type
== BTRFS_INODE_ITEM_KEY
) {
4920 *other_ino
= di_key
.objectid
;
4925 } else if (IS_ERR(di
)) {
4929 btrfs_release_path(search_path
);
4931 cur_offset
+= this_len
;
4935 btrfs_free_path(search_path
);
4940 /* log a single inode in the tree log.
4941 * At least one parent directory for this inode must exist in the tree
4942 * or be logged already.
4944 * Any items from this inode changed by the current transaction are copied
4945 * to the log tree. An extra reference is taken on any extents in this
4946 * file, allowing us to avoid a whole pile of corner cases around logging
4947 * blocks that have been removed from the tree.
4949 * See LOG_INODE_ALL and related defines for a description of what inode_only
4952 * This handles both files and directories.
4954 static int btrfs_log_inode(struct btrfs_trans_handle
*trans
,
4955 struct btrfs_root
*root
, struct btrfs_inode
*inode
,
4959 struct btrfs_log_ctx
*ctx
)
4961 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4962 struct btrfs_path
*path
;
4963 struct btrfs_path
*dst_path
;
4964 struct btrfs_key min_key
;
4965 struct btrfs_key max_key
;
4966 struct btrfs_root
*log
= root
->log_root
;
4967 LIST_HEAD(logged_list
);
4968 u64 last_extent
= 0;
4972 int ins_start_slot
= 0;
4974 bool fast_search
= false;
4975 u64 ino
= btrfs_ino(inode
);
4976 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
4977 u64 logged_isize
= 0;
4978 bool need_log_inode_item
= true;
4979 bool xattrs_logged
= false;
4981 path
= btrfs_alloc_path();
4984 dst_path
= btrfs_alloc_path();
4986 btrfs_free_path(path
);
4990 min_key
.objectid
= ino
;
4991 min_key
.type
= BTRFS_INODE_ITEM_KEY
;
4994 max_key
.objectid
= ino
;
4997 /* today the code can only do partial logging of directories */
4998 if (S_ISDIR(inode
->vfs_inode
.i_mode
) ||
4999 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
5000 &inode
->runtime_flags
) &&
5001 inode_only
>= LOG_INODE_EXISTS
))
5002 max_key
.type
= BTRFS_XATTR_ITEM_KEY
;
5004 max_key
.type
= (u8
)-1;
5005 max_key
.offset
= (u64
)-1;
5008 * Only run delayed items if we are a dir or a new file.
5009 * Otherwise commit the delayed inode only, which is needed in
5010 * order for the log replay code to mark inodes for link count
5011 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
5013 if (S_ISDIR(inode
->vfs_inode
.i_mode
) ||
5014 inode
->generation
> fs_info
->last_trans_committed
)
5015 ret
= btrfs_commit_inode_delayed_items(trans
, inode
);
5017 ret
= btrfs_commit_inode_delayed_inode(inode
);
5020 btrfs_free_path(path
);
5021 btrfs_free_path(dst_path
);
5025 if (inode_only
== LOG_OTHER_INODE
) {
5026 inode_only
= LOG_INODE_EXISTS
;
5027 mutex_lock_nested(&inode
->log_mutex
, SINGLE_DEPTH_NESTING
);
5029 mutex_lock(&inode
->log_mutex
);
5033 * a brute force approach to making sure we get the most uptodate
5034 * copies of everything.
5036 if (S_ISDIR(inode
->vfs_inode
.i_mode
)) {
5037 int max_key_type
= BTRFS_DIR_LOG_INDEX_KEY
;
5039 if (inode_only
== LOG_INODE_EXISTS
)
5040 max_key_type
= BTRFS_XATTR_ITEM_KEY
;
5041 ret
= drop_objectid_items(trans
, log
, path
, ino
, max_key_type
);
5043 if (inode_only
== LOG_INODE_EXISTS
) {
5045 * Make sure the new inode item we write to the log has
5046 * the same isize as the current one (if it exists).
5047 * This is necessary to prevent data loss after log
5048 * replay, and also to prevent doing a wrong expanding
5049 * truncate - for e.g. create file, write 4K into offset
5050 * 0, fsync, write 4K into offset 4096, add hard link,
5051 * fsync some other file (to sync log), power fail - if
5052 * we use the inode's current i_size, after log replay
5053 * we get a 8Kb file, with the last 4Kb extent as a hole
5054 * (zeroes), as if an expanding truncate happened,
5055 * instead of getting a file of 4Kb only.
5057 err
= logged_inode_size(log
, inode
, path
, &logged_isize
);
5061 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
5062 &inode
->runtime_flags
)) {
5063 if (inode_only
== LOG_INODE_EXISTS
) {
5064 max_key
.type
= BTRFS_XATTR_ITEM_KEY
;
5065 ret
= drop_objectid_items(trans
, log
, path
, ino
,
5068 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
5069 &inode
->runtime_flags
);
5070 clear_bit(BTRFS_INODE_COPY_EVERYTHING
,
5071 &inode
->runtime_flags
);
5073 ret
= btrfs_truncate_inode_items(trans
,
5074 log
, &inode
->vfs_inode
, 0, 0);
5079 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING
,
5080 &inode
->runtime_flags
) ||
5081 inode_only
== LOG_INODE_EXISTS
) {
5082 if (inode_only
== LOG_INODE_ALL
)
5084 max_key
.type
= BTRFS_XATTR_ITEM_KEY
;
5085 ret
= drop_objectid_items(trans
, log
, path
, ino
,
5088 if (inode_only
== LOG_INODE_ALL
)
5101 ret
= btrfs_search_forward(root
, &min_key
,
5102 path
, trans
->transid
);
5110 /* note, ins_nr might be > 0 here, cleanup outside the loop */
5111 if (min_key
.objectid
!= ino
)
5113 if (min_key
.type
> max_key
.type
)
5116 if (min_key
.type
== BTRFS_INODE_ITEM_KEY
)
5117 need_log_inode_item
= false;
5119 if ((min_key
.type
== BTRFS_INODE_REF_KEY
||
5120 min_key
.type
== BTRFS_INODE_EXTREF_KEY
) &&
5121 inode
->generation
== trans
->transid
) {
5124 ret
= btrfs_check_ref_name_override(path
->nodes
[0],
5125 path
->slots
[0], &min_key
, inode
,
5130 } else if (ret
> 0 && ctx
&&
5131 other_ino
!= btrfs_ino(BTRFS_I(ctx
->inode
))) {
5132 struct btrfs_key inode_key
;
5133 struct inode
*other_inode
;
5139 ins_start_slot
= path
->slots
[0];
5141 ret
= copy_items(trans
, inode
, dst_path
, path
,
5142 &last_extent
, ins_start_slot
,
5150 btrfs_release_path(path
);
5151 inode_key
.objectid
= other_ino
;
5152 inode_key
.type
= BTRFS_INODE_ITEM_KEY
;
5153 inode_key
.offset
= 0;
5154 other_inode
= btrfs_iget(fs_info
->sb
,
5158 * If the other inode that had a conflicting dir
5159 * entry was deleted in the current transaction,
5160 * we don't need to do more work nor fallback to
5161 * a transaction commit.
5163 if (IS_ERR(other_inode
) &&
5164 PTR_ERR(other_inode
) == -ENOENT
) {
5166 } else if (IS_ERR(other_inode
)) {
5167 err
= PTR_ERR(other_inode
);
5171 * We are safe logging the other inode without
5172 * acquiring its i_mutex as long as we log with
5173 * the LOG_INODE_EXISTS mode. We're safe against
5174 * concurrent renames of the other inode as well
5175 * because during a rename we pin the log and
5176 * update the log with the new name before we
5179 err
= btrfs_log_inode(trans
, root
,
5180 BTRFS_I(other_inode
),
5181 LOG_OTHER_INODE
, 0, LLONG_MAX
,
5191 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5192 if (min_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
5195 ret
= copy_items(trans
, inode
, dst_path
, path
,
5196 &last_extent
, ins_start_slot
,
5197 ins_nr
, inode_only
, logged_isize
);
5204 btrfs_release_path(path
);
5210 if (ins_nr
&& ins_start_slot
+ ins_nr
== path
->slots
[0]) {
5213 } else if (!ins_nr
) {
5214 ins_start_slot
= path
->slots
[0];
5219 ret
= copy_items(trans
, inode
, dst_path
, path
, &last_extent
,
5220 ins_start_slot
, ins_nr
, inode_only
,
5228 btrfs_release_path(path
);
5232 ins_start_slot
= path
->slots
[0];
5235 nritems
= btrfs_header_nritems(path
->nodes
[0]);
5237 if (path
->slots
[0] < nritems
) {
5238 btrfs_item_key_to_cpu(path
->nodes
[0], &min_key
,
5243 ret
= copy_items(trans
, inode
, dst_path
, path
,
5244 &last_extent
, ins_start_slot
,
5245 ins_nr
, inode_only
, logged_isize
);
5253 btrfs_release_path(path
);
5255 if (min_key
.offset
< (u64
)-1) {
5257 } else if (min_key
.type
< max_key
.type
) {
5265 ret
= copy_items(trans
, inode
, dst_path
, path
, &last_extent
,
5266 ins_start_slot
, ins_nr
, inode_only
,
5276 btrfs_release_path(path
);
5277 btrfs_release_path(dst_path
);
5278 err
= btrfs_log_all_xattrs(trans
, root
, inode
, path
, dst_path
);
5281 xattrs_logged
= true;
5282 if (max_key
.type
>= BTRFS_EXTENT_DATA_KEY
&& !fast_search
) {
5283 btrfs_release_path(path
);
5284 btrfs_release_path(dst_path
);
5285 err
= btrfs_log_trailing_hole(trans
, root
, inode
, path
);
5290 btrfs_release_path(path
);
5291 btrfs_release_path(dst_path
);
5292 if (need_log_inode_item
) {
5293 err
= log_inode_item(trans
, log
, dst_path
, inode
);
5294 if (!err
&& !xattrs_logged
) {
5295 err
= btrfs_log_all_xattrs(trans
, root
, inode
, path
,
5297 btrfs_release_path(path
);
5303 ret
= btrfs_log_changed_extents(trans
, root
, inode
, dst_path
,
5304 &logged_list
, ctx
, start
, end
);
5309 } else if (inode_only
== LOG_INODE_ALL
) {
5310 struct extent_map
*em
, *n
;
5312 write_lock(&em_tree
->lock
);
5314 * We can't just remove every em if we're called for a ranged
5315 * fsync - that is, one that doesn't cover the whole possible
5316 * file range (0 to LLONG_MAX). This is because we can have
5317 * em's that fall outside the range we're logging and therefore
5318 * their ordered operations haven't completed yet
5319 * (btrfs_finish_ordered_io() not invoked yet). This means we
5320 * didn't get their respective file extent item in the fs/subvol
5321 * tree yet, and need to let the next fast fsync (one which
5322 * consults the list of modified extent maps) find the em so
5323 * that it logs a matching file extent item and waits for the
5324 * respective ordered operation to complete (if it's still
5327 * Removing every em outside the range we're logging would make
5328 * the next fast fsync not log their matching file extent items,
5329 * therefore making us lose data after a log replay.
5331 list_for_each_entry_safe(em
, n
, &em_tree
->modified_extents
,
5333 const u64 mod_end
= em
->mod_start
+ em
->mod_len
- 1;
5335 if (em
->mod_start
>= start
&& mod_end
<= end
)
5336 list_del_init(&em
->list
);
5338 write_unlock(&em_tree
->lock
);
5341 if (inode_only
== LOG_INODE_ALL
&& S_ISDIR(inode
->vfs_inode
.i_mode
)) {
5342 ret
= log_directory_changes(trans
, root
, inode
, path
, dst_path
,
5350 spin_lock(&inode
->lock
);
5351 inode
->logged_trans
= trans
->transid
;
5352 inode
->last_log_commit
= inode
->last_sub_trans
;
5353 spin_unlock(&inode
->lock
);
5356 btrfs_put_logged_extents(&logged_list
);
5358 btrfs_submit_logged_extents(&logged_list
, log
);
5359 mutex_unlock(&inode
->log_mutex
);
5361 btrfs_free_path(path
);
5362 btrfs_free_path(dst_path
);
5367 * Check if we must fallback to a transaction commit when logging an inode.
5368 * This must be called after logging the inode and is used only in the context
5369 * when fsyncing an inode requires the need to log some other inode - in which
5370 * case we can't lock the i_mutex of each other inode we need to log as that
5371 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5372 * log inodes up or down in the hierarchy) or rename operations for example. So
5373 * we take the log_mutex of the inode after we have logged it and then check for
5374 * its last_unlink_trans value - this is safe because any task setting
5375 * last_unlink_trans must take the log_mutex and it must do this before it does
5376 * the actual unlink operation, so if we do this check before a concurrent task
5377 * sets last_unlink_trans it means we've logged a consistent version/state of
5378 * all the inode items, otherwise we are not sure and must do a transaction
5379 * commit (the concurrent task might have only updated last_unlink_trans before
5380 * we logged the inode or it might have also done the unlink).
5382 static bool btrfs_must_commit_transaction(struct btrfs_trans_handle
*trans
,
5383 struct btrfs_inode
*inode
)
5385 struct btrfs_fs_info
*fs_info
= inode
->root
->fs_info
;
5388 mutex_lock(&inode
->log_mutex
);
5389 if (inode
->last_unlink_trans
> fs_info
->last_trans_committed
) {
5391 * Make sure any commits to the log are forced to be full
5394 btrfs_set_log_full_commit(fs_info
, trans
);
5397 mutex_unlock(&inode
->log_mutex
);
5403 * follow the dentry parent pointers up the chain and see if any
5404 * of the directories in it require a full commit before they can
5405 * be logged. Returns zero if nothing special needs to be done or 1 if
5406 * a full commit is required.
5408 static noinline
int check_parent_dirs_for_sync(struct btrfs_trans_handle
*trans
,
5409 struct btrfs_inode
*inode
,
5410 struct dentry
*parent
,
5411 struct super_block
*sb
,
5415 struct dentry
*old_parent
= NULL
;
5416 struct btrfs_inode
*orig_inode
= inode
;
5419 * for regular files, if its inode is already on disk, we don't
5420 * have to worry about the parents at all. This is because
5421 * we can use the last_unlink_trans field to record renames
5422 * and other fun in this file.
5424 if (S_ISREG(inode
->vfs_inode
.i_mode
) &&
5425 inode
->generation
<= last_committed
&&
5426 inode
->last_unlink_trans
<= last_committed
)
5429 if (!S_ISDIR(inode
->vfs_inode
.i_mode
)) {
5430 if (!parent
|| d_really_is_negative(parent
) || sb
!= parent
->d_sb
)
5432 inode
= BTRFS_I(d_inode(parent
));
5437 * If we are logging a directory then we start with our inode,
5438 * not our parent's inode, so we need to skip setting the
5439 * logged_trans so that further down in the log code we don't
5440 * think this inode has already been logged.
5442 if (inode
!= orig_inode
)
5443 inode
->logged_trans
= trans
->transid
;
5446 if (btrfs_must_commit_transaction(trans
, inode
)) {
5451 if (!parent
|| d_really_is_negative(parent
) || sb
!= parent
->d_sb
)
5454 if (IS_ROOT(parent
)) {
5455 inode
= BTRFS_I(d_inode(parent
));
5456 if (btrfs_must_commit_transaction(trans
, inode
))
5461 parent
= dget_parent(parent
);
5463 old_parent
= parent
;
5464 inode
= BTRFS_I(d_inode(parent
));
5472 struct btrfs_dir_list
{
5474 struct list_head list
;
5478 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5479 * details about the why it is needed.
5480 * This is a recursive operation - if an existing dentry corresponds to a
5481 * directory, that directory's new entries are logged too (same behaviour as
5482 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5483 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5484 * complains about the following circular lock dependency / possible deadlock:
5488 * lock(&type->i_mutex_dir_key#3/2);
5489 * lock(sb_internal#2);
5490 * lock(&type->i_mutex_dir_key#3/2);
5491 * lock(&sb->s_type->i_mutex_key#14);
5493 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5494 * sb_start_intwrite() in btrfs_start_transaction().
5495 * Not locking i_mutex of the inodes is still safe because:
5497 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5498 * that while logging the inode new references (names) are added or removed
5499 * from the inode, leaving the logged inode item with a link count that does
5500 * not match the number of logged inode reference items. This is fine because
5501 * at log replay time we compute the real number of links and correct the
5502 * link count in the inode item (see replay_one_buffer() and
5503 * link_to_fixup_dir());
5505 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5506 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5507 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5508 * has a size that doesn't match the sum of the lengths of all the logged
5509 * names. This does not result in a problem because if a dir_item key is
5510 * logged but its matching dir_index key is not logged, at log replay time we
5511 * don't use it to replay the respective name (see replay_one_name()). On the
5512 * other hand if only the dir_index key ends up being logged, the respective
5513 * name is added to the fs/subvol tree with both the dir_item and dir_index
5514 * keys created (see replay_one_name()).
5515 * The directory's inode item with a wrong i_size is not a problem as well,
5516 * since we don't use it at log replay time to set the i_size in the inode
5517 * item of the fs/subvol tree (see overwrite_item()).
5519 static int log_new_dir_dentries(struct btrfs_trans_handle
*trans
,
5520 struct btrfs_root
*root
,
5521 struct btrfs_inode
*start_inode
,
5522 struct btrfs_log_ctx
*ctx
)
5524 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5525 struct btrfs_root
*log
= root
->log_root
;
5526 struct btrfs_path
*path
;
5527 LIST_HEAD(dir_list
);
5528 struct btrfs_dir_list
*dir_elem
;
5531 path
= btrfs_alloc_path();
5535 dir_elem
= kmalloc(sizeof(*dir_elem
), GFP_NOFS
);
5537 btrfs_free_path(path
);
5540 dir_elem
->ino
= btrfs_ino(start_inode
);
5541 list_add_tail(&dir_elem
->list
, &dir_list
);
5543 while (!list_empty(&dir_list
)) {
5544 struct extent_buffer
*leaf
;
5545 struct btrfs_key min_key
;
5549 dir_elem
= list_first_entry(&dir_list
, struct btrfs_dir_list
,
5552 goto next_dir_inode
;
5554 min_key
.objectid
= dir_elem
->ino
;
5555 min_key
.type
= BTRFS_DIR_ITEM_KEY
;
5558 btrfs_release_path(path
);
5559 ret
= btrfs_search_forward(log
, &min_key
, path
, trans
->transid
);
5561 goto next_dir_inode
;
5562 } else if (ret
> 0) {
5564 goto next_dir_inode
;
5568 leaf
= path
->nodes
[0];
5569 nritems
= btrfs_header_nritems(leaf
);
5570 for (i
= path
->slots
[0]; i
< nritems
; i
++) {
5571 struct btrfs_dir_item
*di
;
5572 struct btrfs_key di_key
;
5573 struct inode
*di_inode
;
5574 struct btrfs_dir_list
*new_dir_elem
;
5575 int log_mode
= LOG_INODE_EXISTS
;
5578 btrfs_item_key_to_cpu(leaf
, &min_key
, i
);
5579 if (min_key
.objectid
!= dir_elem
->ino
||
5580 min_key
.type
!= BTRFS_DIR_ITEM_KEY
)
5581 goto next_dir_inode
;
5583 di
= btrfs_item_ptr(leaf
, i
, struct btrfs_dir_item
);
5584 type
= btrfs_dir_type(leaf
, di
);
5585 if (btrfs_dir_transid(leaf
, di
) < trans
->transid
&&
5586 type
!= BTRFS_FT_DIR
)
5588 btrfs_dir_item_key_to_cpu(leaf
, di
, &di_key
);
5589 if (di_key
.type
== BTRFS_ROOT_ITEM_KEY
)
5592 btrfs_release_path(path
);
5593 di_inode
= btrfs_iget(fs_info
->sb
, &di_key
, root
, NULL
);
5594 if (IS_ERR(di_inode
)) {
5595 ret
= PTR_ERR(di_inode
);
5596 goto next_dir_inode
;
5599 if (btrfs_inode_in_log(BTRFS_I(di_inode
), trans
->transid
)) {
5604 ctx
->log_new_dentries
= false;
5605 if (type
== BTRFS_FT_DIR
|| type
== BTRFS_FT_SYMLINK
)
5606 log_mode
= LOG_INODE_ALL
;
5607 ret
= btrfs_log_inode(trans
, root
, BTRFS_I(di_inode
),
5608 log_mode
, 0, LLONG_MAX
, ctx
);
5610 btrfs_must_commit_transaction(trans
, BTRFS_I(di_inode
)))
5614 goto next_dir_inode
;
5615 if (ctx
->log_new_dentries
) {
5616 new_dir_elem
= kmalloc(sizeof(*new_dir_elem
),
5618 if (!new_dir_elem
) {
5620 goto next_dir_inode
;
5622 new_dir_elem
->ino
= di_key
.objectid
;
5623 list_add_tail(&new_dir_elem
->list
, &dir_list
);
5628 ret
= btrfs_next_leaf(log
, path
);
5630 goto next_dir_inode
;
5631 } else if (ret
> 0) {
5633 goto next_dir_inode
;
5637 if (min_key
.offset
< (u64
)-1) {
5642 list_del(&dir_elem
->list
);
5646 btrfs_free_path(path
);
5650 static int btrfs_log_all_parents(struct btrfs_trans_handle
*trans
,
5651 struct btrfs_inode
*inode
,
5652 struct btrfs_log_ctx
*ctx
)
5654 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->vfs_inode
.i_sb
);
5656 struct btrfs_path
*path
;
5657 struct btrfs_key key
;
5658 struct btrfs_root
*root
= inode
->root
;
5659 const u64 ino
= btrfs_ino(inode
);
5661 path
= btrfs_alloc_path();
5664 path
->skip_locking
= 1;
5665 path
->search_commit_root
= 1;
5668 key
.type
= BTRFS_INODE_REF_KEY
;
5670 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5675 struct extent_buffer
*leaf
= path
->nodes
[0];
5676 int slot
= path
->slots
[0];
5681 if (slot
>= btrfs_header_nritems(leaf
)) {
5682 ret
= btrfs_next_leaf(root
, path
);
5690 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5691 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5692 if (key
.objectid
!= ino
|| key
.type
> BTRFS_INODE_EXTREF_KEY
)
5695 item_size
= btrfs_item_size_nr(leaf
, slot
);
5696 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
5697 while (cur_offset
< item_size
) {
5698 struct btrfs_key inode_key
;
5699 struct inode
*dir_inode
;
5701 inode_key
.type
= BTRFS_INODE_ITEM_KEY
;
5702 inode_key
.offset
= 0;
5704 if (key
.type
== BTRFS_INODE_EXTREF_KEY
) {
5705 struct btrfs_inode_extref
*extref
;
5707 extref
= (struct btrfs_inode_extref
*)
5709 inode_key
.objectid
= btrfs_inode_extref_parent(
5711 cur_offset
+= sizeof(*extref
);
5712 cur_offset
+= btrfs_inode_extref_name_len(leaf
,
5715 inode_key
.objectid
= key
.offset
;
5716 cur_offset
= item_size
;
5719 dir_inode
= btrfs_iget(fs_info
->sb
, &inode_key
,
5721 /* If parent inode was deleted, skip it. */
5722 if (IS_ERR(dir_inode
))
5726 ctx
->log_new_dentries
= false;
5727 ret
= btrfs_log_inode(trans
, root
, BTRFS_I(dir_inode
),
5728 LOG_INODE_ALL
, 0, LLONG_MAX
, ctx
);
5730 btrfs_must_commit_transaction(trans
, BTRFS_I(dir_inode
)))
5732 if (!ret
&& ctx
&& ctx
->log_new_dentries
)
5733 ret
= log_new_dir_dentries(trans
, root
,
5734 BTRFS_I(dir_inode
), ctx
);
5743 btrfs_free_path(path
);
5748 * helper function around btrfs_log_inode to make sure newly created
5749 * parent directories also end up in the log. A minimal inode and backref
5750 * only logging is done of any parent directories that are older than
5751 * the last committed transaction
5753 static int btrfs_log_inode_parent(struct btrfs_trans_handle
*trans
,
5754 struct btrfs_inode
*inode
,
5755 struct dentry
*parent
,
5759 struct btrfs_log_ctx
*ctx
)
5761 struct btrfs_root
*root
= inode
->root
;
5762 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5763 struct super_block
*sb
;
5764 struct dentry
*old_parent
= NULL
;
5766 u64 last_committed
= fs_info
->last_trans_committed
;
5767 bool log_dentries
= false;
5768 struct btrfs_inode
*orig_inode
= inode
;
5770 sb
= inode
->vfs_inode
.i_sb
;
5772 if (btrfs_test_opt(fs_info
, NOTREELOG
)) {
5778 * The prev transaction commit doesn't complete, we need do
5779 * full commit by ourselves.
5781 if (fs_info
->last_trans_log_full_commit
>
5782 fs_info
->last_trans_committed
) {
5787 if (btrfs_root_refs(&root
->root_item
) == 0) {
5792 ret
= check_parent_dirs_for_sync(trans
, inode
, parent
, sb
,
5797 if (btrfs_inode_in_log(inode
, trans
->transid
)) {
5798 ret
= BTRFS_NO_LOG_SYNC
;
5802 ret
= start_log_trans(trans
, root
, ctx
);
5806 ret
= btrfs_log_inode(trans
, root
, inode
, inode_only
, start
, end
, ctx
);
5811 * for regular files, if its inode is already on disk, we don't
5812 * have to worry about the parents at all. This is because
5813 * we can use the last_unlink_trans field to record renames
5814 * and other fun in this file.
5816 if (S_ISREG(inode
->vfs_inode
.i_mode
) &&
5817 inode
->generation
<= last_committed
&&
5818 inode
->last_unlink_trans
<= last_committed
) {
5823 if (S_ISDIR(inode
->vfs_inode
.i_mode
) && ctx
&& ctx
->log_new_dentries
)
5824 log_dentries
= true;
5827 * On unlink we must make sure all our current and old parent directory
5828 * inodes are fully logged. This is to prevent leaving dangling
5829 * directory index entries in directories that were our parents but are
5830 * not anymore. Not doing this results in old parent directory being
5831 * impossible to delete after log replay (rmdir will always fail with
5832 * error -ENOTEMPTY).
5838 * ln testdir/foo testdir/bar
5840 * unlink testdir/bar
5841 * xfs_io -c fsync testdir/foo
5843 * mount fs, triggers log replay
5845 * If we don't log the parent directory (testdir), after log replay the
5846 * directory still has an entry pointing to the file inode using the bar
5847 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5848 * the file inode has a link count of 1.
5854 * ln foo testdir/foo2
5855 * ln foo testdir/foo3
5857 * unlink testdir/foo3
5858 * xfs_io -c fsync foo
5860 * mount fs, triggers log replay
5862 * Similar as the first example, after log replay the parent directory
5863 * testdir still has an entry pointing to the inode file with name foo3
5864 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5865 * and has a link count of 2.
5867 if (inode
->last_unlink_trans
> last_committed
) {
5868 ret
= btrfs_log_all_parents(trans
, orig_inode
, ctx
);
5874 if (!parent
|| d_really_is_negative(parent
) || sb
!= parent
->d_sb
)
5877 inode
= BTRFS_I(d_inode(parent
));
5878 if (root
!= inode
->root
)
5881 if (inode
->generation
> last_committed
) {
5882 ret
= btrfs_log_inode(trans
, root
, inode
,
5883 LOG_INODE_EXISTS
, 0, LLONG_MAX
, ctx
);
5887 if (IS_ROOT(parent
))
5890 parent
= dget_parent(parent
);
5892 old_parent
= parent
;
5895 ret
= log_new_dir_dentries(trans
, root
, orig_inode
, ctx
);
5901 btrfs_set_log_full_commit(fs_info
, trans
);
5906 btrfs_remove_log_ctx(root
, ctx
);
5907 btrfs_end_log_trans(root
);
5913 * it is not safe to log dentry if the chunk root has added new
5914 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5915 * If this returns 1, you must commit the transaction to safely get your
5918 int btrfs_log_dentry_safe(struct btrfs_trans_handle
*trans
,
5919 struct dentry
*dentry
,
5922 struct btrfs_log_ctx
*ctx
)
5924 struct dentry
*parent
= dget_parent(dentry
);
5927 ret
= btrfs_log_inode_parent(trans
, BTRFS_I(d_inode(dentry
)), parent
,
5928 start
, end
, LOG_INODE_ALL
, ctx
);
5935 * should be called during mount to recover any replay any log trees
5938 int btrfs_recover_log_trees(struct btrfs_root
*log_root_tree
)
5941 struct btrfs_path
*path
;
5942 struct btrfs_trans_handle
*trans
;
5943 struct btrfs_key key
;
5944 struct btrfs_key found_key
;
5945 struct btrfs_key tmp_key
;
5946 struct btrfs_root
*log
;
5947 struct btrfs_fs_info
*fs_info
= log_root_tree
->fs_info
;
5948 struct walk_control wc
= {
5949 .process_func
= process_one_buffer
,
5953 path
= btrfs_alloc_path();
5957 set_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
);
5959 trans
= btrfs_start_transaction(fs_info
->tree_root
, 0);
5960 if (IS_ERR(trans
)) {
5961 ret
= PTR_ERR(trans
);
5968 ret
= walk_log_tree(trans
, log_root_tree
, &wc
);
5970 btrfs_handle_fs_error(fs_info
, ret
,
5971 "Failed to pin buffers while recovering log root tree.");
5976 key
.objectid
= BTRFS_TREE_LOG_OBJECTID
;
5977 key
.offset
= (u64
)-1;
5978 key
.type
= BTRFS_ROOT_ITEM_KEY
;
5981 ret
= btrfs_search_slot(NULL
, log_root_tree
, &key
, path
, 0, 0);
5984 btrfs_handle_fs_error(fs_info
, ret
,
5985 "Couldn't find tree log root.");
5989 if (path
->slots
[0] == 0)
5993 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
5995 btrfs_release_path(path
);
5996 if (found_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
5999 log
= btrfs_read_fs_root(log_root_tree
, &found_key
);
6002 btrfs_handle_fs_error(fs_info
, ret
,
6003 "Couldn't read tree log root.");
6007 tmp_key
.objectid
= found_key
.offset
;
6008 tmp_key
.type
= BTRFS_ROOT_ITEM_KEY
;
6009 tmp_key
.offset
= (u64
)-1;
6011 wc
.replay_dest
= btrfs_read_fs_root_no_name(fs_info
, &tmp_key
);
6012 if (IS_ERR(wc
.replay_dest
)) {
6013 ret
= PTR_ERR(wc
.replay_dest
);
6014 free_extent_buffer(log
->node
);
6015 free_extent_buffer(log
->commit_root
);
6017 btrfs_handle_fs_error(fs_info
, ret
,
6018 "Couldn't read target root for tree log recovery.");
6022 wc
.replay_dest
->log_root
= log
;
6023 btrfs_record_root_in_trans(trans
, wc
.replay_dest
);
6024 ret
= walk_log_tree(trans
, log
, &wc
);
6026 if (!ret
&& wc
.stage
== LOG_WALK_REPLAY_ALL
) {
6027 ret
= fixup_inode_link_counts(trans
, wc
.replay_dest
,
6031 if (!ret
&& wc
.stage
== LOG_WALK_REPLAY_ALL
) {
6032 struct btrfs_root
*root
= wc
.replay_dest
;
6034 btrfs_release_path(path
);
6037 * We have just replayed everything, and the highest
6038 * objectid of fs roots probably has changed in case
6039 * some inode_item's got replayed.
6041 * root->objectid_mutex is not acquired as log replay
6042 * could only happen during mount.
6044 ret
= btrfs_find_highest_objectid(root
,
6045 &root
->highest_objectid
);
6048 key
.offset
= found_key
.offset
- 1;
6049 wc
.replay_dest
->log_root
= NULL
;
6050 free_extent_buffer(log
->node
);
6051 free_extent_buffer(log
->commit_root
);
6057 if (found_key
.offset
== 0)
6060 btrfs_release_path(path
);
6062 /* step one is to pin it all, step two is to replay just inodes */
6065 wc
.process_func
= replay_one_buffer
;
6066 wc
.stage
= LOG_WALK_REPLAY_INODES
;
6069 /* step three is to replay everything */
6070 if (wc
.stage
< LOG_WALK_REPLAY_ALL
) {
6075 btrfs_free_path(path
);
6077 /* step 4: commit the transaction, which also unpins the blocks */
6078 ret
= btrfs_commit_transaction(trans
);
6082 free_extent_buffer(log_root_tree
->node
);
6083 log_root_tree
->log_root
= NULL
;
6084 clear_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
);
6085 kfree(log_root_tree
);
6090 btrfs_end_transaction(wc
.trans
);
6091 btrfs_free_path(path
);
6096 * there are some corner cases where we want to force a full
6097 * commit instead of allowing a directory to be logged.
6099 * They revolve around files there were unlinked from the directory, and
6100 * this function updates the parent directory so that a full commit is
6101 * properly done if it is fsync'd later after the unlinks are done.
6103 * Must be called before the unlink operations (updates to the subvolume tree,
6104 * inodes, etc) are done.
6106 void btrfs_record_unlink_dir(struct btrfs_trans_handle
*trans
,
6107 struct btrfs_inode
*dir
, struct btrfs_inode
*inode
,
6111 * when we're logging a file, if it hasn't been renamed
6112 * or unlinked, and its inode is fully committed on disk,
6113 * we don't have to worry about walking up the directory chain
6114 * to log its parents.
6116 * So, we use the last_unlink_trans field to put this transid
6117 * into the file. When the file is logged we check it and
6118 * don't log the parents if the file is fully on disk.
6120 mutex_lock(&inode
->log_mutex
);
6121 inode
->last_unlink_trans
= trans
->transid
;
6122 mutex_unlock(&inode
->log_mutex
);
6125 * if this directory was already logged any new
6126 * names for this file/dir will get recorded
6129 if (dir
->logged_trans
== trans
->transid
)
6133 * if the inode we're about to unlink was logged,
6134 * the log will be properly updated for any new names
6136 if (inode
->logged_trans
== trans
->transid
)
6140 * when renaming files across directories, if the directory
6141 * there we're unlinking from gets fsync'd later on, there's
6142 * no way to find the destination directory later and fsync it
6143 * properly. So, we have to be conservative and force commits
6144 * so the new name gets discovered.
6149 /* we can safely do the unlink without any special recording */
6153 mutex_lock(&dir
->log_mutex
);
6154 dir
->last_unlink_trans
= trans
->transid
;
6155 mutex_unlock(&dir
->log_mutex
);
6159 * Make sure that if someone attempts to fsync the parent directory of a deleted
6160 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6161 * that after replaying the log tree of the parent directory's root we will not
6162 * see the snapshot anymore and at log replay time we will not see any log tree
6163 * corresponding to the deleted snapshot's root, which could lead to replaying
6164 * it after replaying the log tree of the parent directory (which would replay
6165 * the snapshot delete operation).
6167 * Must be called before the actual snapshot destroy operation (updates to the
6168 * parent root and tree of tree roots trees, etc) are done.
6170 void btrfs_record_snapshot_destroy(struct btrfs_trans_handle
*trans
,
6171 struct btrfs_inode
*dir
)
6173 mutex_lock(&dir
->log_mutex
);
6174 dir
->last_unlink_trans
= trans
->transid
;
6175 mutex_unlock(&dir
->log_mutex
);
6179 * Call this after adding a new name for a file and it will properly
6180 * update the log to reflect the new name.
6182 * It will return zero if all goes well, and it will return 1 if a
6183 * full transaction commit is required.
6185 int btrfs_log_new_name(struct btrfs_trans_handle
*trans
,
6186 struct btrfs_inode
*inode
, struct btrfs_inode
*old_dir
,
6187 struct dentry
*parent
)
6189 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->vfs_inode
.i_sb
);
6192 * this will force the logging code to walk the dentry chain
6195 if (!S_ISDIR(inode
->vfs_inode
.i_mode
))
6196 inode
->last_unlink_trans
= trans
->transid
;
6199 * if this inode hasn't been logged and directory we're renaming it
6200 * from hasn't been logged, we don't need to log it
6202 if (inode
->logged_trans
<= fs_info
->last_trans_committed
&&
6203 (!old_dir
|| old_dir
->logged_trans
<= fs_info
->last_trans_committed
))
6206 return btrfs_log_inode_parent(trans
, inode
, parent
, 0, LLONG_MAX
,
6207 LOG_INODE_EXISTS
, NULL
);