2 * Copyright (C) 2008 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/list_sort.h>
23 #include "transaction.h"
26 #include "print-tree.h"
32 /* magic values for the inode_only field in btrfs_log_inode:
34 * LOG_INODE_ALL means to log everything
35 * LOG_INODE_EXISTS means to log just enough to recreate the inode
38 #define LOG_INODE_ALL 0
39 #define LOG_INODE_EXISTS 1
42 * directory trouble cases
44 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
45 * log, we must force a full commit before doing an fsync of the directory
46 * where the unlink was done.
47 * ---> record transid of last unlink/rename per directory
51 * rename foo/some_dir foo2/some_dir
53 * fsync foo/some_dir/some_file
55 * The fsync above will unlink the original some_dir without recording
56 * it in its new location (foo2). After a crash, some_dir will be gone
57 * unless the fsync of some_file forces a full commit
59 * 2) we must log any new names for any file or dir that is in the fsync
60 * log. ---> check inode while renaming/linking.
62 * 2a) we must log any new names for any file or dir during rename
63 * when the directory they are being removed from was logged.
64 * ---> check inode and old parent dir during rename
66 * 2a is actually the more important variant. With the extra logging
67 * a crash might unlink the old name without recreating the new one
69 * 3) after a crash, we must go through any directories with a link count
70 * of zero and redo the rm -rf
77 * The directory f1 was fully removed from the FS, but fsync was never
78 * called on f1, only its parent dir. After a crash the rm -rf must
79 * be replayed. This must be able to recurse down the entire
80 * directory tree. The inode link count fixup code takes care of the
85 * stages for the tree walking. The first
86 * stage (0) is to only pin down the blocks we find
87 * the second stage (1) is to make sure that all the inodes
88 * we find in the log are created in the subvolume.
90 * The last stage is to deal with directories and links and extents
91 * and all the other fun semantics
93 #define LOG_WALK_PIN_ONLY 0
94 #define LOG_WALK_REPLAY_INODES 1
95 #define LOG_WALK_REPLAY_ALL 2
97 static int btrfs_log_inode(struct btrfs_trans_handle
*trans
,
98 struct btrfs_root
*root
, struct inode
*inode
,
100 static int link_to_fixup_dir(struct btrfs_trans_handle
*trans
,
101 struct btrfs_root
*root
,
102 struct btrfs_path
*path
, u64 objectid
);
103 static noinline
int replay_dir_deletes(struct btrfs_trans_handle
*trans
,
104 struct btrfs_root
*root
,
105 struct btrfs_root
*log
,
106 struct btrfs_path
*path
,
107 u64 dirid
, int del_all
);
110 * tree logging is a special write ahead log used to make sure that
111 * fsyncs and O_SYNCs can happen without doing full tree commits.
113 * Full tree commits are expensive because they require commonly
114 * modified blocks to be recowed, creating many dirty pages in the
115 * extent tree an 4x-6x higher write load than ext3.
117 * Instead of doing a tree commit on every fsync, we use the
118 * key ranges and transaction ids to find items for a given file or directory
119 * that have changed in this transaction. Those items are copied into
120 * a special tree (one per subvolume root), that tree is written to disk
121 * and then the fsync is considered complete.
123 * After a crash, items are copied out of the log-tree back into the
124 * subvolume tree. Any file data extents found are recorded in the extent
125 * allocation tree, and the log-tree freed.
127 * The log tree is read three times, once to pin down all the extents it is
128 * using in ram and once, once to create all the inodes logged in the tree
129 * and once to do all the other items.
133 * start a sub transaction and setup the log tree
134 * this increments the log tree writer count to make the people
135 * syncing the tree wait for us to finish
137 static int start_log_trans(struct btrfs_trans_handle
*trans
,
138 struct btrfs_root
*root
)
143 mutex_lock(&root
->log_mutex
);
144 if (root
->log_root
) {
145 if (!root
->log_start_pid
) {
146 root
->log_start_pid
= current
->pid
;
147 root
->log_multiple_pids
= false;
148 } else if (root
->log_start_pid
!= current
->pid
) {
149 root
->log_multiple_pids
= true;
152 atomic_inc(&root
->log_batch
);
153 atomic_inc(&root
->log_writers
);
154 mutex_unlock(&root
->log_mutex
);
157 root
->log_multiple_pids
= false;
158 root
->log_start_pid
= current
->pid
;
159 mutex_lock(&root
->fs_info
->tree_log_mutex
);
160 if (!root
->fs_info
->log_root_tree
) {
161 ret
= btrfs_init_log_root_tree(trans
, root
->fs_info
);
165 if (err
== 0 && !root
->log_root
) {
166 ret
= btrfs_add_log_tree(trans
, root
);
170 mutex_unlock(&root
->fs_info
->tree_log_mutex
);
171 atomic_inc(&root
->log_batch
);
172 atomic_inc(&root
->log_writers
);
173 mutex_unlock(&root
->log_mutex
);
178 * returns 0 if there was a log transaction running and we were able
179 * to join, or returns -ENOENT if there were not transactions
182 static int join_running_log_trans(struct btrfs_root
*root
)
190 mutex_lock(&root
->log_mutex
);
191 if (root
->log_root
) {
193 atomic_inc(&root
->log_writers
);
195 mutex_unlock(&root
->log_mutex
);
200 * This either makes the current running log transaction wait
201 * until you call btrfs_end_log_trans() or it makes any future
202 * log transactions wait until you call btrfs_end_log_trans()
204 int btrfs_pin_log_trans(struct btrfs_root
*root
)
208 mutex_lock(&root
->log_mutex
);
209 atomic_inc(&root
->log_writers
);
210 mutex_unlock(&root
->log_mutex
);
215 * indicate we're done making changes to the log tree
216 * and wake up anyone waiting to do a sync
218 void btrfs_end_log_trans(struct btrfs_root
*root
)
220 if (atomic_dec_and_test(&root
->log_writers
)) {
222 if (waitqueue_active(&root
->log_writer_wait
))
223 wake_up(&root
->log_writer_wait
);
229 * the walk control struct is used to pass state down the chain when
230 * processing the log tree. The stage field tells us which part
231 * of the log tree processing we are currently doing. The others
232 * are state fields used for that specific part
234 struct walk_control
{
235 /* should we free the extent on disk when done? This is used
236 * at transaction commit time while freeing a log tree
240 /* should we write out the extent buffer? This is used
241 * while flushing the log tree to disk during a sync
245 /* should we wait for the extent buffer io to finish? Also used
246 * while flushing the log tree to disk for a sync
250 /* pin only walk, we record which extents on disk belong to the
255 /* what stage of the replay code we're currently in */
258 /* the root we are currently replaying */
259 struct btrfs_root
*replay_dest
;
261 /* the trans handle for the current replay */
262 struct btrfs_trans_handle
*trans
;
264 /* the function that gets used to process blocks we find in the
265 * tree. Note the extent_buffer might not be up to date when it is
266 * passed in, and it must be checked or read if you need the data
269 int (*process_func
)(struct btrfs_root
*log
, struct extent_buffer
*eb
,
270 struct walk_control
*wc
, u64 gen
);
274 * process_func used to pin down extents, write them or wait on them
276 static int process_one_buffer(struct btrfs_root
*log
,
277 struct extent_buffer
*eb
,
278 struct walk_control
*wc
, u64 gen
)
281 btrfs_pin_extent_for_log_replay(log
->fs_info
->extent_root
,
284 if (btrfs_buffer_uptodate(eb
, gen
, 0)) {
286 btrfs_write_tree_block(eb
);
288 btrfs_wait_tree_block_writeback(eb
);
294 * Item overwrite used by replay and tree logging. eb, slot and key all refer
295 * to the src data we are copying out.
297 * root is the tree we are copying into, and path is a scratch
298 * path for use in this function (it should be released on entry and
299 * will be released on exit).
301 * If the key is already in the destination tree the existing item is
302 * overwritten. If the existing item isn't big enough, it is extended.
303 * If it is too large, it is truncated.
305 * If the key isn't in the destination yet, a new item is inserted.
307 static noinline
int overwrite_item(struct btrfs_trans_handle
*trans
,
308 struct btrfs_root
*root
,
309 struct btrfs_path
*path
,
310 struct extent_buffer
*eb
, int slot
,
311 struct btrfs_key
*key
)
315 u64 saved_i_size
= 0;
316 int save_old_i_size
= 0;
317 unsigned long src_ptr
;
318 unsigned long dst_ptr
;
319 int overwrite_root
= 0;
321 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
324 item_size
= btrfs_item_size_nr(eb
, slot
);
325 src_ptr
= btrfs_item_ptr_offset(eb
, slot
);
327 /* look for the key in the destination tree */
328 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
332 u32 dst_size
= btrfs_item_size_nr(path
->nodes
[0],
334 if (dst_size
!= item_size
)
337 if (item_size
== 0) {
338 btrfs_release_path(path
);
341 dst_copy
= kmalloc(item_size
, GFP_NOFS
);
342 src_copy
= kmalloc(item_size
, GFP_NOFS
);
343 if (!dst_copy
|| !src_copy
) {
344 btrfs_release_path(path
);
350 read_extent_buffer(eb
, src_copy
, src_ptr
, item_size
);
352 dst_ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
353 read_extent_buffer(path
->nodes
[0], dst_copy
, dst_ptr
,
355 ret
= memcmp(dst_copy
, src_copy
, item_size
);
360 * they have the same contents, just return, this saves
361 * us from cowing blocks in the destination tree and doing
362 * extra writes that may not have been done by a previous
366 btrfs_release_path(path
);
372 btrfs_release_path(path
);
373 /* try to insert the key into the destination tree */
374 ret
= btrfs_insert_empty_item(trans
, root
, path
,
377 /* make sure any existing item is the correct size */
378 if (ret
== -EEXIST
) {
380 found_size
= btrfs_item_size_nr(path
->nodes
[0],
382 if (found_size
> item_size
)
383 btrfs_truncate_item(trans
, root
, path
, item_size
, 1);
384 else if (found_size
< item_size
)
385 btrfs_extend_item(trans
, root
, path
,
386 item_size
- found_size
);
390 dst_ptr
= btrfs_item_ptr_offset(path
->nodes
[0],
393 /* don't overwrite an existing inode if the generation number
394 * was logged as zero. This is done when the tree logging code
395 * is just logging an inode to make sure it exists after recovery.
397 * Also, don't overwrite i_size on directories during replay.
398 * log replay inserts and removes directory items based on the
399 * state of the tree found in the subvolume, and i_size is modified
402 if (key
->type
== BTRFS_INODE_ITEM_KEY
&& ret
== -EEXIST
) {
403 struct btrfs_inode_item
*src_item
;
404 struct btrfs_inode_item
*dst_item
;
406 src_item
= (struct btrfs_inode_item
*)src_ptr
;
407 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
409 if (btrfs_inode_generation(eb
, src_item
) == 0)
412 if (overwrite_root
&&
413 S_ISDIR(btrfs_inode_mode(eb
, src_item
)) &&
414 S_ISDIR(btrfs_inode_mode(path
->nodes
[0], dst_item
))) {
416 saved_i_size
= btrfs_inode_size(path
->nodes
[0],
421 copy_extent_buffer(path
->nodes
[0], eb
, dst_ptr
,
424 if (save_old_i_size
) {
425 struct btrfs_inode_item
*dst_item
;
426 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
427 btrfs_set_inode_size(path
->nodes
[0], dst_item
, saved_i_size
);
430 /* make sure the generation is filled in */
431 if (key
->type
== BTRFS_INODE_ITEM_KEY
) {
432 struct btrfs_inode_item
*dst_item
;
433 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
434 if (btrfs_inode_generation(path
->nodes
[0], dst_item
) == 0) {
435 btrfs_set_inode_generation(path
->nodes
[0], dst_item
,
440 btrfs_mark_buffer_dirty(path
->nodes
[0]);
441 btrfs_release_path(path
);
446 * simple helper to read an inode off the disk from a given root
447 * This can only be called for subvolume roots and not for the log
449 static noinline
struct inode
*read_one_inode(struct btrfs_root
*root
,
452 struct btrfs_key key
;
455 key
.objectid
= objectid
;
456 key
.type
= BTRFS_INODE_ITEM_KEY
;
458 inode
= btrfs_iget(root
->fs_info
->sb
, &key
, root
, NULL
);
461 } else if (is_bad_inode(inode
)) {
468 /* replays a single extent in 'eb' at 'slot' with 'key' into the
469 * subvolume 'root'. path is released on entry and should be released
472 * extents in the log tree have not been allocated out of the extent
473 * tree yet. So, this completes the allocation, taking a reference
474 * as required if the extent already exists or creating a new extent
475 * if it isn't in the extent allocation tree yet.
477 * The extent is inserted into the file, dropping any existing extents
478 * from the file that overlap the new one.
480 static noinline
int replay_one_extent(struct btrfs_trans_handle
*trans
,
481 struct btrfs_root
*root
,
482 struct btrfs_path
*path
,
483 struct extent_buffer
*eb
, int slot
,
484 struct btrfs_key
*key
)
487 u64 mask
= root
->sectorsize
- 1;
489 u64 start
= key
->offset
;
491 struct btrfs_file_extent_item
*item
;
492 struct inode
*inode
= NULL
;
496 item
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
497 found_type
= btrfs_file_extent_type(eb
, item
);
499 if (found_type
== BTRFS_FILE_EXTENT_REG
||
500 found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
501 extent_end
= start
+ btrfs_file_extent_num_bytes(eb
, item
);
502 else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
503 size
= btrfs_file_extent_inline_len(eb
, item
);
504 extent_end
= (start
+ size
+ mask
) & ~mask
;
510 inode
= read_one_inode(root
, key
->objectid
);
517 * first check to see if we already have this extent in the
518 * file. This must be done before the btrfs_drop_extents run
519 * so we don't try to drop this extent.
521 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
525 (found_type
== BTRFS_FILE_EXTENT_REG
||
526 found_type
== BTRFS_FILE_EXTENT_PREALLOC
)) {
527 struct btrfs_file_extent_item cmp1
;
528 struct btrfs_file_extent_item cmp2
;
529 struct btrfs_file_extent_item
*existing
;
530 struct extent_buffer
*leaf
;
532 leaf
= path
->nodes
[0];
533 existing
= btrfs_item_ptr(leaf
, path
->slots
[0],
534 struct btrfs_file_extent_item
);
536 read_extent_buffer(eb
, &cmp1
, (unsigned long)item
,
538 read_extent_buffer(leaf
, &cmp2
, (unsigned long)existing
,
542 * we already have a pointer to this exact extent,
543 * we don't have to do anything
545 if (memcmp(&cmp1
, &cmp2
, sizeof(cmp1
)) == 0) {
546 btrfs_release_path(path
);
550 btrfs_release_path(path
);
552 saved_nbytes
= inode_get_bytes(inode
);
553 /* drop any overlapping extents */
554 ret
= btrfs_drop_extents(trans
, root
, inode
, start
, extent_end
, 1);
557 if (found_type
== BTRFS_FILE_EXTENT_REG
||
558 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
560 unsigned long dest_offset
;
561 struct btrfs_key ins
;
563 ret
= btrfs_insert_empty_item(trans
, root
, path
, key
,
566 dest_offset
= btrfs_item_ptr_offset(path
->nodes
[0],
568 copy_extent_buffer(path
->nodes
[0], eb
, dest_offset
,
569 (unsigned long)item
, sizeof(*item
));
571 ins
.objectid
= btrfs_file_extent_disk_bytenr(eb
, item
);
572 ins
.offset
= btrfs_file_extent_disk_num_bytes(eb
, item
);
573 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
574 offset
= key
->offset
- btrfs_file_extent_offset(eb
, item
);
576 if (ins
.objectid
> 0) {
579 LIST_HEAD(ordered_sums
);
581 * is this extent already allocated in the extent
582 * allocation tree? If so, just add a reference
584 ret
= btrfs_lookup_extent(root
, ins
.objectid
,
587 ret
= btrfs_inc_extent_ref(trans
, root
,
588 ins
.objectid
, ins
.offset
,
589 0, root
->root_key
.objectid
,
590 key
->objectid
, offset
, 0);
594 * insert the extent pointer in the extent
597 ret
= btrfs_alloc_logged_file_extent(trans
,
598 root
, root
->root_key
.objectid
,
599 key
->objectid
, offset
, &ins
);
602 btrfs_release_path(path
);
604 if (btrfs_file_extent_compression(eb
, item
)) {
605 csum_start
= ins
.objectid
;
606 csum_end
= csum_start
+ ins
.offset
;
608 csum_start
= ins
.objectid
+
609 btrfs_file_extent_offset(eb
, item
);
610 csum_end
= csum_start
+
611 btrfs_file_extent_num_bytes(eb
, item
);
614 ret
= btrfs_lookup_csums_range(root
->log_root
,
615 csum_start
, csum_end
- 1,
618 while (!list_empty(&ordered_sums
)) {
619 struct btrfs_ordered_sum
*sums
;
620 sums
= list_entry(ordered_sums
.next
,
621 struct btrfs_ordered_sum
,
623 ret
= btrfs_csum_file_blocks(trans
,
624 root
->fs_info
->csum_root
,
627 list_del(&sums
->list
);
631 btrfs_release_path(path
);
633 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
634 /* inline extents are easy, we just overwrite them */
635 ret
= overwrite_item(trans
, root
, path
, eb
, slot
, key
);
639 inode_set_bytes(inode
, saved_nbytes
);
640 ret
= btrfs_update_inode(trans
, root
, inode
);
648 * when cleaning up conflicts between the directory names in the
649 * subvolume, directory names in the log and directory names in the
650 * inode back references, we may have to unlink inodes from directories.
652 * This is a helper function to do the unlink of a specific directory
655 static noinline
int drop_one_dir_item(struct btrfs_trans_handle
*trans
,
656 struct btrfs_root
*root
,
657 struct btrfs_path
*path
,
659 struct btrfs_dir_item
*di
)
664 struct extent_buffer
*leaf
;
665 struct btrfs_key location
;
668 leaf
= path
->nodes
[0];
670 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
671 name_len
= btrfs_dir_name_len(leaf
, di
);
672 name
= kmalloc(name_len
, GFP_NOFS
);
676 read_extent_buffer(leaf
, name
, (unsigned long)(di
+ 1), name_len
);
677 btrfs_release_path(path
);
679 inode
= read_one_inode(root
, location
.objectid
);
685 ret
= link_to_fixup_dir(trans
, root
, path
, location
.objectid
);
688 ret
= btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
694 btrfs_run_delayed_items(trans
, root
);
699 * helper function to see if a given name and sequence number found
700 * in an inode back reference are already in a directory and correctly
701 * point to this inode
703 static noinline
int inode_in_dir(struct btrfs_root
*root
,
704 struct btrfs_path
*path
,
705 u64 dirid
, u64 objectid
, u64 index
,
706 const char *name
, int name_len
)
708 struct btrfs_dir_item
*di
;
709 struct btrfs_key location
;
712 di
= btrfs_lookup_dir_index_item(NULL
, root
, path
, dirid
,
713 index
, name
, name_len
, 0);
714 if (di
&& !IS_ERR(di
)) {
715 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
716 if (location
.objectid
!= objectid
)
720 btrfs_release_path(path
);
722 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dirid
, name
, name_len
, 0);
723 if (di
&& !IS_ERR(di
)) {
724 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
725 if (location
.objectid
!= objectid
)
731 btrfs_release_path(path
);
736 * helper function to check a log tree for a named back reference in
737 * an inode. This is used to decide if a back reference that is
738 * found in the subvolume conflicts with what we find in the log.
740 * inode backreferences may have multiple refs in a single item,
741 * during replay we process one reference at a time, and we don't
742 * want to delete valid links to a file from the subvolume if that
743 * link is also in the log.
745 static noinline
int backref_in_log(struct btrfs_root
*log
,
746 struct btrfs_key
*key
,
748 char *name
, int namelen
)
750 struct btrfs_path
*path
;
751 struct btrfs_inode_ref
*ref
;
753 unsigned long ptr_end
;
754 unsigned long name_ptr
;
760 path
= btrfs_alloc_path();
764 ret
= btrfs_search_slot(NULL
, log
, key
, path
, 0, 0);
768 ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
770 if (key
->type
== BTRFS_INODE_EXTREF_KEY
) {
771 if (btrfs_find_name_in_ext_backref(path
, ref_objectid
,
772 name
, namelen
, NULL
))
778 item_size
= btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]);
779 ptr_end
= ptr
+ item_size
;
780 while (ptr
< ptr_end
) {
781 ref
= (struct btrfs_inode_ref
*)ptr
;
782 found_name_len
= btrfs_inode_ref_name_len(path
->nodes
[0], ref
);
783 if (found_name_len
== namelen
) {
784 name_ptr
= (unsigned long)(ref
+ 1);
785 ret
= memcmp_extent_buffer(path
->nodes
[0], name
,
792 ptr
= (unsigned long)(ref
+ 1) + found_name_len
;
795 btrfs_free_path(path
);
799 static inline int __add_inode_ref(struct btrfs_trans_handle
*trans
,
800 struct btrfs_root
*root
,
801 struct btrfs_path
*path
,
802 struct btrfs_root
*log_root
,
803 struct inode
*dir
, struct inode
*inode
,
804 struct extent_buffer
*eb
,
805 u64 inode_objectid
, u64 parent_objectid
,
806 u64 ref_index
, char *name
, int namelen
,
812 struct extent_buffer
*leaf
;
813 struct btrfs_dir_item
*di
;
814 struct btrfs_key search_key
;
815 struct btrfs_inode_extref
*extref
;
818 /* Search old style refs */
819 search_key
.objectid
= inode_objectid
;
820 search_key
.type
= BTRFS_INODE_REF_KEY
;
821 search_key
.offset
= parent_objectid
;
822 ret
= btrfs_search_slot(NULL
, root
, &search_key
, path
, 0, 0);
824 struct btrfs_inode_ref
*victim_ref
;
826 unsigned long ptr_end
;
828 leaf
= path
->nodes
[0];
830 /* are we trying to overwrite a back ref for the root directory
831 * if so, just jump out, we're done
833 if (search_key
.objectid
== search_key
.offset
)
836 /* check all the names in this back reference to see
837 * if they are in the log. if so, we allow them to stay
838 * otherwise they must be unlinked as a conflict
840 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
841 ptr_end
= ptr
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
842 while (ptr
< ptr_end
) {
843 victim_ref
= (struct btrfs_inode_ref
*)ptr
;
844 victim_name_len
= btrfs_inode_ref_name_len(leaf
,
846 victim_name
= kmalloc(victim_name_len
, GFP_NOFS
);
847 BUG_ON(!victim_name
);
849 read_extent_buffer(leaf
, victim_name
,
850 (unsigned long)(victim_ref
+ 1),
853 if (!backref_in_log(log_root
, &search_key
,
857 btrfs_inc_nlink(inode
);
858 btrfs_release_path(path
);
860 ret
= btrfs_unlink_inode(trans
, root
, dir
,
864 btrfs_run_delayed_items(trans
, root
);
871 ptr
= (unsigned long)(victim_ref
+ 1) + victim_name_len
;
876 * NOTE: we have searched root tree and checked the
877 * coresponding ref, it does not need to check again.
881 btrfs_release_path(path
);
883 /* Same search but for extended refs */
884 extref
= btrfs_lookup_inode_extref(NULL
, root
, path
, name
, namelen
,
885 inode_objectid
, parent_objectid
, 0,
887 if (!IS_ERR_OR_NULL(extref
)) {
891 struct inode
*victim_parent
;
893 leaf
= path
->nodes
[0];
895 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
896 base
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
898 while (cur_offset
< item_size
) {
899 extref
= (struct btrfs_inode_extref
*)base
+ cur_offset
;
901 victim_name_len
= btrfs_inode_extref_name_len(leaf
, extref
);
903 if (btrfs_inode_extref_parent(leaf
, extref
) != parent_objectid
)
906 victim_name
= kmalloc(victim_name_len
, GFP_NOFS
);
907 read_extent_buffer(leaf
, victim_name
, (unsigned long)&extref
->name
,
910 search_key
.objectid
= inode_objectid
;
911 search_key
.type
= BTRFS_INODE_EXTREF_KEY
;
912 search_key
.offset
= btrfs_extref_hash(parent_objectid
,
916 if (!backref_in_log(log_root
, &search_key
,
917 parent_objectid
, victim_name
,
920 victim_parent
= read_one_inode(root
,
923 btrfs_inc_nlink(inode
);
924 btrfs_release_path(path
);
926 ret
= btrfs_unlink_inode(trans
, root
,
931 btrfs_run_delayed_items(trans
, root
);
942 cur_offset
+= victim_name_len
+ sizeof(*extref
);
946 btrfs_release_path(path
);
948 /* look for a conflicting sequence number */
949 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, btrfs_ino(dir
),
950 ref_index
, name
, namelen
, 0);
951 if (di
&& !IS_ERR(di
)) {
952 ret
= drop_one_dir_item(trans
, root
, path
, dir
, di
);
955 btrfs_release_path(path
);
957 /* look for a conflicing name */
958 di
= btrfs_lookup_dir_item(trans
, root
, path
, btrfs_ino(dir
),
960 if (di
&& !IS_ERR(di
)) {
961 ret
= drop_one_dir_item(trans
, root
, path
, dir
, di
);
964 btrfs_release_path(path
);
969 static int extref_get_fields(struct extent_buffer
*eb
, unsigned long ref_ptr
,
970 u32
*namelen
, char **name
, u64
*index
,
971 u64
*parent_objectid
)
973 struct btrfs_inode_extref
*extref
;
975 extref
= (struct btrfs_inode_extref
*)ref_ptr
;
977 *namelen
= btrfs_inode_extref_name_len(eb
, extref
);
978 *name
= kmalloc(*namelen
, GFP_NOFS
);
982 read_extent_buffer(eb
, *name
, (unsigned long)&extref
->name
,
985 *index
= btrfs_inode_extref_index(eb
, extref
);
987 *parent_objectid
= btrfs_inode_extref_parent(eb
, extref
);
992 static int ref_get_fields(struct extent_buffer
*eb
, unsigned long ref_ptr
,
993 u32
*namelen
, char **name
, u64
*index
)
995 struct btrfs_inode_ref
*ref
;
997 ref
= (struct btrfs_inode_ref
*)ref_ptr
;
999 *namelen
= btrfs_inode_ref_name_len(eb
, ref
);
1000 *name
= kmalloc(*namelen
, GFP_NOFS
);
1004 read_extent_buffer(eb
, *name
, (unsigned long)(ref
+ 1), *namelen
);
1006 *index
= btrfs_inode_ref_index(eb
, ref
);
1012 * replay one inode back reference item found in the log tree.
1013 * eb, slot and key refer to the buffer and key found in the log tree.
1014 * root is the destination we are replaying into, and path is for temp
1015 * use by this function. (it should be released on return).
1017 static noinline
int add_inode_ref(struct btrfs_trans_handle
*trans
,
1018 struct btrfs_root
*root
,
1019 struct btrfs_root
*log
,
1020 struct btrfs_path
*path
,
1021 struct extent_buffer
*eb
, int slot
,
1022 struct btrfs_key
*key
)
1025 struct inode
*inode
;
1026 unsigned long ref_ptr
;
1027 unsigned long ref_end
;
1031 int search_done
= 0;
1032 int log_ref_ver
= 0;
1033 u64 parent_objectid
;
1036 int ref_struct_size
;
1038 ref_ptr
= btrfs_item_ptr_offset(eb
, slot
);
1039 ref_end
= ref_ptr
+ btrfs_item_size_nr(eb
, slot
);
1041 if (key
->type
== BTRFS_INODE_EXTREF_KEY
) {
1042 struct btrfs_inode_extref
*r
;
1044 ref_struct_size
= sizeof(struct btrfs_inode_extref
);
1046 r
= (struct btrfs_inode_extref
*)ref_ptr
;
1047 parent_objectid
= btrfs_inode_extref_parent(eb
, r
);
1049 ref_struct_size
= sizeof(struct btrfs_inode_ref
);
1050 parent_objectid
= key
->offset
;
1052 inode_objectid
= key
->objectid
;
1055 * it is possible that we didn't log all the parent directories
1056 * for a given inode. If we don't find the dir, just don't
1057 * copy the back ref in. The link count fixup code will take
1060 dir
= read_one_inode(root
, parent_objectid
);
1064 inode
= read_one_inode(root
, inode_objectid
);
1070 while (ref_ptr
< ref_end
) {
1072 ret
= extref_get_fields(eb
, ref_ptr
, &namelen
, &name
,
1073 &ref_index
, &parent_objectid
);
1075 * parent object can change from one array
1079 dir
= read_one_inode(root
, parent_objectid
);
1083 ret
= ref_get_fields(eb
, ref_ptr
, &namelen
, &name
,
1089 /* if we already have a perfect match, we're done */
1090 if (!inode_in_dir(root
, path
, btrfs_ino(dir
), btrfs_ino(inode
),
1091 ref_index
, name
, namelen
)) {
1093 * look for a conflicting back reference in the
1094 * metadata. if we find one we have to unlink that name
1095 * of the file before we add our new link. Later on, we
1096 * overwrite any existing back reference, and we don't
1097 * want to create dangling pointers in the directory.
1101 ret
= __add_inode_ref(trans
, root
, path
, log
,
1105 ref_index
, name
, namelen
,
1112 /* insert our name */
1113 ret
= btrfs_add_link(trans
, dir
, inode
, name
, namelen
,
1117 btrfs_update_inode(trans
, root
, inode
);
1120 ref_ptr
= (unsigned long)(ref_ptr
+ ref_struct_size
) + namelen
;
1128 /* finally write the back reference in the inode */
1129 ret
= overwrite_item(trans
, root
, path
, eb
, slot
, key
);
1133 btrfs_release_path(path
);
1139 static int insert_orphan_item(struct btrfs_trans_handle
*trans
,
1140 struct btrfs_root
*root
, u64 offset
)
1143 ret
= btrfs_find_orphan_item(root
, offset
);
1145 ret
= btrfs_insert_orphan_item(trans
, root
, offset
);
1149 static int count_inode_extrefs(struct btrfs_root
*root
,
1150 struct inode
*inode
, struct btrfs_path
*path
)
1154 unsigned int nlink
= 0;
1157 u64 inode_objectid
= btrfs_ino(inode
);
1160 struct btrfs_inode_extref
*extref
;
1161 struct extent_buffer
*leaf
;
1164 ret
= btrfs_find_one_extref(root
, inode_objectid
, offset
, path
,
1169 leaf
= path
->nodes
[0];
1170 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1171 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
1173 while (cur_offset
< item_size
) {
1174 extref
= (struct btrfs_inode_extref
*) (ptr
+ cur_offset
);
1175 name_len
= btrfs_inode_extref_name_len(leaf
, extref
);
1179 cur_offset
+= name_len
+ sizeof(*extref
);
1183 btrfs_release_path(path
);
1185 btrfs_release_path(path
);
1192 static int count_inode_refs(struct btrfs_root
*root
,
1193 struct inode
*inode
, struct btrfs_path
*path
)
1196 struct btrfs_key key
;
1197 unsigned int nlink
= 0;
1199 unsigned long ptr_end
;
1201 u64 ino
= btrfs_ino(inode
);
1204 key
.type
= BTRFS_INODE_REF_KEY
;
1205 key
.offset
= (u64
)-1;
1208 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1212 if (path
->slots
[0] == 0)
1216 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
1218 if (key
.objectid
!= ino
||
1219 key
.type
!= BTRFS_INODE_REF_KEY
)
1221 ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
1222 ptr_end
= ptr
+ btrfs_item_size_nr(path
->nodes
[0],
1224 while (ptr
< ptr_end
) {
1225 struct btrfs_inode_ref
*ref
;
1227 ref
= (struct btrfs_inode_ref
*)ptr
;
1228 name_len
= btrfs_inode_ref_name_len(path
->nodes
[0],
1230 ptr
= (unsigned long)(ref
+ 1) + name_len
;
1234 if (key
.offset
== 0)
1237 btrfs_release_path(path
);
1239 btrfs_release_path(path
);
1245 * There are a few corners where the link count of the file can't
1246 * be properly maintained during replay. So, instead of adding
1247 * lots of complexity to the log code, we just scan the backrefs
1248 * for any file that has been through replay.
1250 * The scan will update the link count on the inode to reflect the
1251 * number of back refs found. If it goes down to zero, the iput
1252 * will free the inode.
1254 static noinline
int fixup_inode_link_count(struct btrfs_trans_handle
*trans
,
1255 struct btrfs_root
*root
,
1256 struct inode
*inode
)
1258 struct btrfs_path
*path
;
1261 u64 ino
= btrfs_ino(inode
);
1263 path
= btrfs_alloc_path();
1267 ret
= count_inode_refs(root
, inode
, path
);
1273 ret
= count_inode_extrefs(root
, inode
, path
);
1284 if (nlink
!= inode
->i_nlink
) {
1285 set_nlink(inode
, nlink
);
1286 btrfs_update_inode(trans
, root
, inode
);
1288 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1290 if (inode
->i_nlink
== 0) {
1291 if (S_ISDIR(inode
->i_mode
)) {
1292 ret
= replay_dir_deletes(trans
, root
, NULL
, path
,
1296 ret
= insert_orphan_item(trans
, root
, ino
);
1301 btrfs_free_path(path
);
1305 static noinline
int fixup_inode_link_counts(struct btrfs_trans_handle
*trans
,
1306 struct btrfs_root
*root
,
1307 struct btrfs_path
*path
)
1310 struct btrfs_key key
;
1311 struct inode
*inode
;
1313 key
.objectid
= BTRFS_TREE_LOG_FIXUP_OBJECTID
;
1314 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
1315 key
.offset
= (u64
)-1;
1317 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1322 if (path
->slots
[0] == 0)
1327 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1328 if (key
.objectid
!= BTRFS_TREE_LOG_FIXUP_OBJECTID
||
1329 key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
1332 ret
= btrfs_del_item(trans
, root
, path
);
1336 btrfs_release_path(path
);
1337 inode
= read_one_inode(root
, key
.offset
);
1341 ret
= fixup_inode_link_count(trans
, root
, inode
);
1347 * fixup on a directory may create new entries,
1348 * make sure we always look for the highset possible
1351 key
.offset
= (u64
)-1;
1355 btrfs_release_path(path
);
1361 * record a given inode in the fixup dir so we can check its link
1362 * count when replay is done. The link count is incremented here
1363 * so the inode won't go away until we check it
1365 static noinline
int link_to_fixup_dir(struct btrfs_trans_handle
*trans
,
1366 struct btrfs_root
*root
,
1367 struct btrfs_path
*path
,
1370 struct btrfs_key key
;
1372 struct inode
*inode
;
1374 inode
= read_one_inode(root
, objectid
);
1378 key
.objectid
= BTRFS_TREE_LOG_FIXUP_OBJECTID
;
1379 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
1380 key
.offset
= objectid
;
1382 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1384 btrfs_release_path(path
);
1386 btrfs_inc_nlink(inode
);
1387 ret
= btrfs_update_inode(trans
, root
, inode
);
1388 } else if (ret
== -EEXIST
) {
1399 * when replaying the log for a directory, we only insert names
1400 * for inodes that actually exist. This means an fsync on a directory
1401 * does not implicitly fsync all the new files in it
1403 static noinline
int insert_one_name(struct btrfs_trans_handle
*trans
,
1404 struct btrfs_root
*root
,
1405 struct btrfs_path
*path
,
1406 u64 dirid
, u64 index
,
1407 char *name
, int name_len
, u8 type
,
1408 struct btrfs_key
*location
)
1410 struct inode
*inode
;
1414 inode
= read_one_inode(root
, location
->objectid
);
1418 dir
= read_one_inode(root
, dirid
);
1423 ret
= btrfs_add_link(trans
, dir
, inode
, name
, name_len
, 1, index
);
1425 /* FIXME, put inode into FIXUP list */
1433 * take a single entry in a log directory item and replay it into
1436 * if a conflicting item exists in the subdirectory already,
1437 * the inode it points to is unlinked and put into the link count
1440 * If a name from the log points to a file or directory that does
1441 * not exist in the FS, it is skipped. fsyncs on directories
1442 * do not force down inodes inside that directory, just changes to the
1443 * names or unlinks in a directory.
1445 static noinline
int replay_one_name(struct btrfs_trans_handle
*trans
,
1446 struct btrfs_root
*root
,
1447 struct btrfs_path
*path
,
1448 struct extent_buffer
*eb
,
1449 struct btrfs_dir_item
*di
,
1450 struct btrfs_key
*key
)
1454 struct btrfs_dir_item
*dst_di
;
1455 struct btrfs_key found_key
;
1456 struct btrfs_key log_key
;
1462 dir
= read_one_inode(root
, key
->objectid
);
1466 name_len
= btrfs_dir_name_len(eb
, di
);
1467 name
= kmalloc(name_len
, GFP_NOFS
);
1471 log_type
= btrfs_dir_type(eb
, di
);
1472 read_extent_buffer(eb
, name
, (unsigned long)(di
+ 1),
1475 btrfs_dir_item_key_to_cpu(eb
, di
, &log_key
);
1476 exists
= btrfs_lookup_inode(trans
, root
, path
, &log_key
, 0);
1481 btrfs_release_path(path
);
1483 if (key
->type
== BTRFS_DIR_ITEM_KEY
) {
1484 dst_di
= btrfs_lookup_dir_item(trans
, root
, path
, key
->objectid
,
1486 } else if (key
->type
== BTRFS_DIR_INDEX_KEY
) {
1487 dst_di
= btrfs_lookup_dir_index_item(trans
, root
, path
,
1494 if (IS_ERR_OR_NULL(dst_di
)) {
1495 /* we need a sequence number to insert, so we only
1496 * do inserts for the BTRFS_DIR_INDEX_KEY types
1498 if (key
->type
!= BTRFS_DIR_INDEX_KEY
)
1503 btrfs_dir_item_key_to_cpu(path
->nodes
[0], dst_di
, &found_key
);
1504 /* the existing item matches the logged item */
1505 if (found_key
.objectid
== log_key
.objectid
&&
1506 found_key
.type
== log_key
.type
&&
1507 found_key
.offset
== log_key
.offset
&&
1508 btrfs_dir_type(path
->nodes
[0], dst_di
) == log_type
) {
1513 * don't drop the conflicting directory entry if the inode
1514 * for the new entry doesn't exist
1519 ret
= drop_one_dir_item(trans
, root
, path
, dir
, dst_di
);
1522 if (key
->type
== BTRFS_DIR_INDEX_KEY
)
1525 btrfs_release_path(path
);
1531 btrfs_release_path(path
);
1532 ret
= insert_one_name(trans
, root
, path
, key
->objectid
, key
->offset
,
1533 name
, name_len
, log_type
, &log_key
);
1535 BUG_ON(ret
&& ret
!= -ENOENT
);
1540 * find all the names in a directory item and reconcile them into
1541 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1542 * one name in a directory item, but the same code gets used for
1543 * both directory index types
1545 static noinline
int replay_one_dir_item(struct btrfs_trans_handle
*trans
,
1546 struct btrfs_root
*root
,
1547 struct btrfs_path
*path
,
1548 struct extent_buffer
*eb
, int slot
,
1549 struct btrfs_key
*key
)
1552 u32 item_size
= btrfs_item_size_nr(eb
, slot
);
1553 struct btrfs_dir_item
*di
;
1556 unsigned long ptr_end
;
1558 ptr
= btrfs_item_ptr_offset(eb
, slot
);
1559 ptr_end
= ptr
+ item_size
;
1560 while (ptr
< ptr_end
) {
1561 di
= (struct btrfs_dir_item
*)ptr
;
1562 if (verify_dir_item(root
, eb
, di
))
1564 name_len
= btrfs_dir_name_len(eb
, di
);
1565 ret
= replay_one_name(trans
, root
, path
, eb
, di
, key
);
1567 ptr
= (unsigned long)(di
+ 1);
1574 * directory replay has two parts. There are the standard directory
1575 * items in the log copied from the subvolume, and range items
1576 * created in the log while the subvolume was logged.
1578 * The range items tell us which parts of the key space the log
1579 * is authoritative for. During replay, if a key in the subvolume
1580 * directory is in a logged range item, but not actually in the log
1581 * that means it was deleted from the directory before the fsync
1582 * and should be removed.
1584 static noinline
int find_dir_range(struct btrfs_root
*root
,
1585 struct btrfs_path
*path
,
1586 u64 dirid
, int key_type
,
1587 u64
*start_ret
, u64
*end_ret
)
1589 struct btrfs_key key
;
1591 struct btrfs_dir_log_item
*item
;
1595 if (*start_ret
== (u64
)-1)
1598 key
.objectid
= dirid
;
1599 key
.type
= key_type
;
1600 key
.offset
= *start_ret
;
1602 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1606 if (path
->slots
[0] == 0)
1611 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1613 if (key
.type
!= key_type
|| key
.objectid
!= dirid
) {
1617 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1618 struct btrfs_dir_log_item
);
1619 found_end
= btrfs_dir_log_end(path
->nodes
[0], item
);
1621 if (*start_ret
>= key
.offset
&& *start_ret
<= found_end
) {
1623 *start_ret
= key
.offset
;
1624 *end_ret
= found_end
;
1629 /* check the next slot in the tree to see if it is a valid item */
1630 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1631 if (path
->slots
[0] >= nritems
) {
1632 ret
= btrfs_next_leaf(root
, path
);
1639 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1641 if (key
.type
!= key_type
|| key
.objectid
!= dirid
) {
1645 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1646 struct btrfs_dir_log_item
);
1647 found_end
= btrfs_dir_log_end(path
->nodes
[0], item
);
1648 *start_ret
= key
.offset
;
1649 *end_ret
= found_end
;
1652 btrfs_release_path(path
);
1657 * this looks for a given directory item in the log. If the directory
1658 * item is not in the log, the item is removed and the inode it points
1661 static noinline
int check_item_in_log(struct btrfs_trans_handle
*trans
,
1662 struct btrfs_root
*root
,
1663 struct btrfs_root
*log
,
1664 struct btrfs_path
*path
,
1665 struct btrfs_path
*log_path
,
1667 struct btrfs_key
*dir_key
)
1670 struct extent_buffer
*eb
;
1673 struct btrfs_dir_item
*di
;
1674 struct btrfs_dir_item
*log_di
;
1677 unsigned long ptr_end
;
1679 struct inode
*inode
;
1680 struct btrfs_key location
;
1683 eb
= path
->nodes
[0];
1684 slot
= path
->slots
[0];
1685 item_size
= btrfs_item_size_nr(eb
, slot
);
1686 ptr
= btrfs_item_ptr_offset(eb
, slot
);
1687 ptr_end
= ptr
+ item_size
;
1688 while (ptr
< ptr_end
) {
1689 di
= (struct btrfs_dir_item
*)ptr
;
1690 if (verify_dir_item(root
, eb
, di
)) {
1695 name_len
= btrfs_dir_name_len(eb
, di
);
1696 name
= kmalloc(name_len
, GFP_NOFS
);
1701 read_extent_buffer(eb
, name
, (unsigned long)(di
+ 1),
1704 if (log
&& dir_key
->type
== BTRFS_DIR_ITEM_KEY
) {
1705 log_di
= btrfs_lookup_dir_item(trans
, log
, log_path
,
1708 } else if (log
&& dir_key
->type
== BTRFS_DIR_INDEX_KEY
) {
1709 log_di
= btrfs_lookup_dir_index_item(trans
, log
,
1715 if (IS_ERR_OR_NULL(log_di
)) {
1716 btrfs_dir_item_key_to_cpu(eb
, di
, &location
);
1717 btrfs_release_path(path
);
1718 btrfs_release_path(log_path
);
1719 inode
= read_one_inode(root
, location
.objectid
);
1725 ret
= link_to_fixup_dir(trans
, root
,
1726 path
, location
.objectid
);
1728 btrfs_inc_nlink(inode
);
1729 ret
= btrfs_unlink_inode(trans
, root
, dir
, inode
,
1733 btrfs_run_delayed_items(trans
, root
);
1738 /* there might still be more names under this key
1739 * check and repeat if required
1741 ret
= btrfs_search_slot(NULL
, root
, dir_key
, path
,
1748 btrfs_release_path(log_path
);
1751 ptr
= (unsigned long)(di
+ 1);
1756 btrfs_release_path(path
);
1757 btrfs_release_path(log_path
);
1762 * deletion replay happens before we copy any new directory items
1763 * out of the log or out of backreferences from inodes. It
1764 * scans the log to find ranges of keys that log is authoritative for,
1765 * and then scans the directory to find items in those ranges that are
1766 * not present in the log.
1768 * Anything we don't find in the log is unlinked and removed from the
1771 static noinline
int replay_dir_deletes(struct btrfs_trans_handle
*trans
,
1772 struct btrfs_root
*root
,
1773 struct btrfs_root
*log
,
1774 struct btrfs_path
*path
,
1775 u64 dirid
, int del_all
)
1779 int key_type
= BTRFS_DIR_LOG_ITEM_KEY
;
1781 struct btrfs_key dir_key
;
1782 struct btrfs_key found_key
;
1783 struct btrfs_path
*log_path
;
1786 dir_key
.objectid
= dirid
;
1787 dir_key
.type
= BTRFS_DIR_ITEM_KEY
;
1788 log_path
= btrfs_alloc_path();
1792 dir
= read_one_inode(root
, dirid
);
1793 /* it isn't an error if the inode isn't there, that can happen
1794 * because we replay the deletes before we copy in the inode item
1798 btrfs_free_path(log_path
);
1806 range_end
= (u64
)-1;
1808 ret
= find_dir_range(log
, path
, dirid
, key_type
,
1809 &range_start
, &range_end
);
1814 dir_key
.offset
= range_start
;
1817 ret
= btrfs_search_slot(NULL
, root
, &dir_key
, path
,
1822 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1823 if (path
->slots
[0] >= nritems
) {
1824 ret
= btrfs_next_leaf(root
, path
);
1828 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1830 if (found_key
.objectid
!= dirid
||
1831 found_key
.type
!= dir_key
.type
)
1834 if (found_key
.offset
> range_end
)
1837 ret
= check_item_in_log(trans
, root
, log
, path
,
1841 if (found_key
.offset
== (u64
)-1)
1843 dir_key
.offset
= found_key
.offset
+ 1;
1845 btrfs_release_path(path
);
1846 if (range_end
== (u64
)-1)
1848 range_start
= range_end
+ 1;
1853 if (key_type
== BTRFS_DIR_LOG_ITEM_KEY
) {
1854 key_type
= BTRFS_DIR_LOG_INDEX_KEY
;
1855 dir_key
.type
= BTRFS_DIR_INDEX_KEY
;
1856 btrfs_release_path(path
);
1860 btrfs_release_path(path
);
1861 btrfs_free_path(log_path
);
1867 * the process_func used to replay items from the log tree. This
1868 * gets called in two different stages. The first stage just looks
1869 * for inodes and makes sure they are all copied into the subvolume.
1871 * The second stage copies all the other item types from the log into
1872 * the subvolume. The two stage approach is slower, but gets rid of
1873 * lots of complexity around inodes referencing other inodes that exist
1874 * only in the log (references come from either directory items or inode
1877 static int replay_one_buffer(struct btrfs_root
*log
, struct extent_buffer
*eb
,
1878 struct walk_control
*wc
, u64 gen
)
1881 struct btrfs_path
*path
;
1882 struct btrfs_root
*root
= wc
->replay_dest
;
1883 struct btrfs_key key
;
1888 ret
= btrfs_read_buffer(eb
, gen
);
1892 level
= btrfs_header_level(eb
);
1897 path
= btrfs_alloc_path();
1901 nritems
= btrfs_header_nritems(eb
);
1902 for (i
= 0; i
< nritems
; i
++) {
1903 btrfs_item_key_to_cpu(eb
, &key
, i
);
1905 /* inode keys are done during the first stage */
1906 if (key
.type
== BTRFS_INODE_ITEM_KEY
&&
1907 wc
->stage
== LOG_WALK_REPLAY_INODES
) {
1908 struct btrfs_inode_item
*inode_item
;
1911 inode_item
= btrfs_item_ptr(eb
, i
,
1912 struct btrfs_inode_item
);
1913 mode
= btrfs_inode_mode(eb
, inode_item
);
1914 if (S_ISDIR(mode
)) {
1915 ret
= replay_dir_deletes(wc
->trans
,
1916 root
, log
, path
, key
.objectid
, 0);
1919 ret
= overwrite_item(wc
->trans
, root
, path
,
1923 /* for regular files, make sure corresponding
1924 * orhpan item exist. extents past the new EOF
1925 * will be truncated later by orphan cleanup.
1927 if (S_ISREG(mode
)) {
1928 ret
= insert_orphan_item(wc
->trans
, root
,
1933 ret
= link_to_fixup_dir(wc
->trans
, root
,
1934 path
, key
.objectid
);
1937 if (wc
->stage
< LOG_WALK_REPLAY_ALL
)
1940 /* these keys are simply copied */
1941 if (key
.type
== BTRFS_XATTR_ITEM_KEY
) {
1942 ret
= overwrite_item(wc
->trans
, root
, path
,
1945 } else if (key
.type
== BTRFS_INODE_REF_KEY
) {
1946 ret
= add_inode_ref(wc
->trans
, root
, log
, path
,
1948 BUG_ON(ret
&& ret
!= -ENOENT
);
1949 } else if (key
.type
== BTRFS_INODE_EXTREF_KEY
) {
1950 ret
= add_inode_ref(wc
->trans
, root
, log
, path
,
1952 BUG_ON(ret
&& ret
!= -ENOENT
);
1953 } else if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
1954 ret
= replay_one_extent(wc
->trans
, root
, path
,
1957 } else if (key
.type
== BTRFS_DIR_ITEM_KEY
||
1958 key
.type
== BTRFS_DIR_INDEX_KEY
) {
1959 ret
= replay_one_dir_item(wc
->trans
, root
, path
,
1964 btrfs_free_path(path
);
1968 static noinline
int walk_down_log_tree(struct btrfs_trans_handle
*trans
,
1969 struct btrfs_root
*root
,
1970 struct btrfs_path
*path
, int *level
,
1971 struct walk_control
*wc
)
1976 struct extent_buffer
*next
;
1977 struct extent_buffer
*cur
;
1978 struct extent_buffer
*parent
;
1982 WARN_ON(*level
< 0);
1983 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1985 while (*level
> 0) {
1986 WARN_ON(*level
< 0);
1987 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1988 cur
= path
->nodes
[*level
];
1990 if (btrfs_header_level(cur
) != *level
)
1993 if (path
->slots
[*level
] >=
1994 btrfs_header_nritems(cur
))
1997 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
1998 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
1999 blocksize
= btrfs_level_size(root
, *level
- 1);
2001 parent
= path
->nodes
[*level
];
2002 root_owner
= btrfs_header_owner(parent
);
2004 next
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
2009 ret
= wc
->process_func(root
, next
, wc
, ptr_gen
);
2013 path
->slots
[*level
]++;
2015 ret
= btrfs_read_buffer(next
, ptr_gen
);
2017 free_extent_buffer(next
);
2021 btrfs_tree_lock(next
);
2022 btrfs_set_lock_blocking(next
);
2023 clean_tree_block(trans
, root
, next
);
2024 btrfs_wait_tree_block_writeback(next
);
2025 btrfs_tree_unlock(next
);
2027 WARN_ON(root_owner
!=
2028 BTRFS_TREE_LOG_OBJECTID
);
2029 ret
= btrfs_free_and_pin_reserved_extent(root
,
2031 BUG_ON(ret
); /* -ENOMEM or logic errors */
2033 free_extent_buffer(next
);
2036 ret
= btrfs_read_buffer(next
, ptr_gen
);
2038 free_extent_buffer(next
);
2042 WARN_ON(*level
<= 0);
2043 if (path
->nodes
[*level
-1])
2044 free_extent_buffer(path
->nodes
[*level
-1]);
2045 path
->nodes
[*level
-1] = next
;
2046 *level
= btrfs_header_level(next
);
2047 path
->slots
[*level
] = 0;
2050 WARN_ON(*level
< 0);
2051 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2053 path
->slots
[*level
] = btrfs_header_nritems(path
->nodes
[*level
]);
2059 static noinline
int walk_up_log_tree(struct btrfs_trans_handle
*trans
,
2060 struct btrfs_root
*root
,
2061 struct btrfs_path
*path
, int *level
,
2062 struct walk_control
*wc
)
2069 for (i
= *level
; i
< BTRFS_MAX_LEVEL
- 1 && path
->nodes
[i
]; i
++) {
2070 slot
= path
->slots
[i
];
2071 if (slot
+ 1 < btrfs_header_nritems(path
->nodes
[i
])) {
2074 WARN_ON(*level
== 0);
2077 struct extent_buffer
*parent
;
2078 if (path
->nodes
[*level
] == root
->node
)
2079 parent
= path
->nodes
[*level
];
2081 parent
= path
->nodes
[*level
+ 1];
2083 root_owner
= btrfs_header_owner(parent
);
2084 ret
= wc
->process_func(root
, path
->nodes
[*level
], wc
,
2085 btrfs_header_generation(path
->nodes
[*level
]));
2090 struct extent_buffer
*next
;
2092 next
= path
->nodes
[*level
];
2094 btrfs_tree_lock(next
);
2095 btrfs_set_lock_blocking(next
);
2096 clean_tree_block(trans
, root
, next
);
2097 btrfs_wait_tree_block_writeback(next
);
2098 btrfs_tree_unlock(next
);
2100 WARN_ON(root_owner
!= BTRFS_TREE_LOG_OBJECTID
);
2101 ret
= btrfs_free_and_pin_reserved_extent(root
,
2102 path
->nodes
[*level
]->start
,
2103 path
->nodes
[*level
]->len
);
2106 free_extent_buffer(path
->nodes
[*level
]);
2107 path
->nodes
[*level
] = NULL
;
2115 * drop the reference count on the tree rooted at 'snap'. This traverses
2116 * the tree freeing any blocks that have a ref count of zero after being
2119 static int walk_log_tree(struct btrfs_trans_handle
*trans
,
2120 struct btrfs_root
*log
, struct walk_control
*wc
)
2125 struct btrfs_path
*path
;
2129 path
= btrfs_alloc_path();
2133 level
= btrfs_header_level(log
->node
);
2135 path
->nodes
[level
] = log
->node
;
2136 extent_buffer_get(log
->node
);
2137 path
->slots
[level
] = 0;
2140 wret
= walk_down_log_tree(trans
, log
, path
, &level
, wc
);
2148 wret
= walk_up_log_tree(trans
, log
, path
, &level
, wc
);
2157 /* was the root node processed? if not, catch it here */
2158 if (path
->nodes
[orig_level
]) {
2159 ret
= wc
->process_func(log
, path
->nodes
[orig_level
], wc
,
2160 btrfs_header_generation(path
->nodes
[orig_level
]));
2164 struct extent_buffer
*next
;
2166 next
= path
->nodes
[orig_level
];
2168 btrfs_tree_lock(next
);
2169 btrfs_set_lock_blocking(next
);
2170 clean_tree_block(trans
, log
, next
);
2171 btrfs_wait_tree_block_writeback(next
);
2172 btrfs_tree_unlock(next
);
2174 WARN_ON(log
->root_key
.objectid
!=
2175 BTRFS_TREE_LOG_OBJECTID
);
2176 ret
= btrfs_free_and_pin_reserved_extent(log
, next
->start
,
2178 BUG_ON(ret
); /* -ENOMEM or logic errors */
2183 for (i
= 0; i
<= orig_level
; i
++) {
2184 if (path
->nodes
[i
]) {
2185 free_extent_buffer(path
->nodes
[i
]);
2186 path
->nodes
[i
] = NULL
;
2189 btrfs_free_path(path
);
2194 * helper function to update the item for a given subvolumes log root
2195 * in the tree of log roots
2197 static int update_log_root(struct btrfs_trans_handle
*trans
,
2198 struct btrfs_root
*log
)
2202 if (log
->log_transid
== 1) {
2203 /* insert root item on the first sync */
2204 ret
= btrfs_insert_root(trans
, log
->fs_info
->log_root_tree
,
2205 &log
->root_key
, &log
->root_item
);
2207 ret
= btrfs_update_root(trans
, log
->fs_info
->log_root_tree
,
2208 &log
->root_key
, &log
->root_item
);
2213 static int wait_log_commit(struct btrfs_trans_handle
*trans
,
2214 struct btrfs_root
*root
, unsigned long transid
)
2217 int index
= transid
% 2;
2220 * we only allow two pending log transactions at a time,
2221 * so we know that if ours is more than 2 older than the
2222 * current transaction, we're done
2225 prepare_to_wait(&root
->log_commit_wait
[index
],
2226 &wait
, TASK_UNINTERRUPTIBLE
);
2227 mutex_unlock(&root
->log_mutex
);
2229 if (root
->fs_info
->last_trans_log_full_commit
!=
2230 trans
->transid
&& root
->log_transid
< transid
+ 2 &&
2231 atomic_read(&root
->log_commit
[index
]))
2234 finish_wait(&root
->log_commit_wait
[index
], &wait
);
2235 mutex_lock(&root
->log_mutex
);
2236 } while (root
->fs_info
->last_trans_log_full_commit
!=
2237 trans
->transid
&& root
->log_transid
< transid
+ 2 &&
2238 atomic_read(&root
->log_commit
[index
]));
2242 static void wait_for_writer(struct btrfs_trans_handle
*trans
,
2243 struct btrfs_root
*root
)
2246 while (root
->fs_info
->last_trans_log_full_commit
!=
2247 trans
->transid
&& atomic_read(&root
->log_writers
)) {
2248 prepare_to_wait(&root
->log_writer_wait
,
2249 &wait
, TASK_UNINTERRUPTIBLE
);
2250 mutex_unlock(&root
->log_mutex
);
2251 if (root
->fs_info
->last_trans_log_full_commit
!=
2252 trans
->transid
&& atomic_read(&root
->log_writers
))
2254 mutex_lock(&root
->log_mutex
);
2255 finish_wait(&root
->log_writer_wait
, &wait
);
2260 * btrfs_sync_log does sends a given tree log down to the disk and
2261 * updates the super blocks to record it. When this call is done,
2262 * you know that any inodes previously logged are safely on disk only
2265 * Any other return value means you need to call btrfs_commit_transaction.
2266 * Some of the edge cases for fsyncing directories that have had unlinks
2267 * or renames done in the past mean that sometimes the only safe
2268 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2269 * that has happened.
2271 int btrfs_sync_log(struct btrfs_trans_handle
*trans
,
2272 struct btrfs_root
*root
)
2278 struct btrfs_root
*log
= root
->log_root
;
2279 struct btrfs_root
*log_root_tree
= root
->fs_info
->log_root_tree
;
2280 unsigned long log_transid
= 0;
2282 mutex_lock(&root
->log_mutex
);
2283 log_transid
= root
->log_transid
;
2284 index1
= root
->log_transid
% 2;
2285 if (atomic_read(&root
->log_commit
[index1
])) {
2286 wait_log_commit(trans
, root
, root
->log_transid
);
2287 mutex_unlock(&root
->log_mutex
);
2290 atomic_set(&root
->log_commit
[index1
], 1);
2292 /* wait for previous tree log sync to complete */
2293 if (atomic_read(&root
->log_commit
[(index1
+ 1) % 2]))
2294 wait_log_commit(trans
, root
, root
->log_transid
- 1);
2296 int batch
= atomic_read(&root
->log_batch
);
2297 /* when we're on an ssd, just kick the log commit out */
2298 if (!btrfs_test_opt(root
, SSD
) && root
->log_multiple_pids
) {
2299 mutex_unlock(&root
->log_mutex
);
2300 schedule_timeout_uninterruptible(1);
2301 mutex_lock(&root
->log_mutex
);
2303 wait_for_writer(trans
, root
);
2304 if (batch
== atomic_read(&root
->log_batch
))
2308 /* bail out if we need to do a full commit */
2309 if (root
->fs_info
->last_trans_log_full_commit
== trans
->transid
) {
2311 btrfs_free_logged_extents(log
, log_transid
);
2312 mutex_unlock(&root
->log_mutex
);
2316 if (log_transid
% 2 == 0)
2317 mark
= EXTENT_DIRTY
;
2321 /* we start IO on all the marked extents here, but we don't actually
2322 * wait for them until later.
2324 ret
= btrfs_write_marked_extents(log
, &log
->dirty_log_pages
, mark
);
2326 btrfs_abort_transaction(trans
, root
, ret
);
2327 btrfs_free_logged_extents(log
, log_transid
);
2328 mutex_unlock(&root
->log_mutex
);
2332 btrfs_set_root_node(&log
->root_item
, log
->node
);
2334 root
->log_transid
++;
2335 log
->log_transid
= root
->log_transid
;
2336 root
->log_start_pid
= 0;
2339 * IO has been started, blocks of the log tree have WRITTEN flag set
2340 * in their headers. new modifications of the log will be written to
2341 * new positions. so it's safe to allow log writers to go in.
2343 mutex_unlock(&root
->log_mutex
);
2345 mutex_lock(&log_root_tree
->log_mutex
);
2346 atomic_inc(&log_root_tree
->log_batch
);
2347 atomic_inc(&log_root_tree
->log_writers
);
2348 mutex_unlock(&log_root_tree
->log_mutex
);
2350 ret
= update_log_root(trans
, log
);
2352 mutex_lock(&log_root_tree
->log_mutex
);
2353 if (atomic_dec_and_test(&log_root_tree
->log_writers
)) {
2355 if (waitqueue_active(&log_root_tree
->log_writer_wait
))
2356 wake_up(&log_root_tree
->log_writer_wait
);
2360 if (ret
!= -ENOSPC
) {
2361 btrfs_abort_transaction(trans
, root
, ret
);
2362 mutex_unlock(&log_root_tree
->log_mutex
);
2365 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
2366 btrfs_wait_marked_extents(log
, &log
->dirty_log_pages
, mark
);
2367 btrfs_free_logged_extents(log
, log_transid
);
2368 mutex_unlock(&log_root_tree
->log_mutex
);
2373 index2
= log_root_tree
->log_transid
% 2;
2374 if (atomic_read(&log_root_tree
->log_commit
[index2
])) {
2375 btrfs_wait_marked_extents(log
, &log
->dirty_log_pages
, mark
);
2376 wait_log_commit(trans
, log_root_tree
,
2377 log_root_tree
->log_transid
);
2378 btrfs_free_logged_extents(log
, log_transid
);
2379 mutex_unlock(&log_root_tree
->log_mutex
);
2383 atomic_set(&log_root_tree
->log_commit
[index2
], 1);
2385 if (atomic_read(&log_root_tree
->log_commit
[(index2
+ 1) % 2])) {
2386 wait_log_commit(trans
, log_root_tree
,
2387 log_root_tree
->log_transid
- 1);
2390 wait_for_writer(trans
, log_root_tree
);
2393 * now that we've moved on to the tree of log tree roots,
2394 * check the full commit flag again
2396 if (root
->fs_info
->last_trans_log_full_commit
== trans
->transid
) {
2397 btrfs_wait_marked_extents(log
, &log
->dirty_log_pages
, mark
);
2398 btrfs_free_logged_extents(log
, log_transid
);
2399 mutex_unlock(&log_root_tree
->log_mutex
);
2401 goto out_wake_log_root
;
2404 ret
= btrfs_write_and_wait_marked_extents(log_root_tree
,
2405 &log_root_tree
->dirty_log_pages
,
2406 EXTENT_DIRTY
| EXTENT_NEW
);
2408 btrfs_abort_transaction(trans
, root
, ret
);
2409 btrfs_free_logged_extents(log
, log_transid
);
2410 mutex_unlock(&log_root_tree
->log_mutex
);
2411 goto out_wake_log_root
;
2413 btrfs_wait_marked_extents(log
, &log
->dirty_log_pages
, mark
);
2414 btrfs_wait_logged_extents(log
, log_transid
);
2416 btrfs_set_super_log_root(root
->fs_info
->super_for_commit
,
2417 log_root_tree
->node
->start
);
2418 btrfs_set_super_log_root_level(root
->fs_info
->super_for_commit
,
2419 btrfs_header_level(log_root_tree
->node
));
2421 log_root_tree
->log_transid
++;
2424 mutex_unlock(&log_root_tree
->log_mutex
);
2427 * nobody else is going to jump in and write the the ctree
2428 * super here because the log_commit atomic below is protecting
2429 * us. We must be called with a transaction handle pinning
2430 * the running transaction open, so a full commit can't hop
2431 * in and cause problems either.
2433 btrfs_scrub_pause_super(root
);
2434 ret
= write_ctree_super(trans
, root
->fs_info
->tree_root
, 1);
2435 btrfs_scrub_continue_super(root
);
2437 btrfs_abort_transaction(trans
, root
, ret
);
2438 goto out_wake_log_root
;
2441 mutex_lock(&root
->log_mutex
);
2442 if (root
->last_log_commit
< log_transid
)
2443 root
->last_log_commit
= log_transid
;
2444 mutex_unlock(&root
->log_mutex
);
2447 atomic_set(&log_root_tree
->log_commit
[index2
], 0);
2449 if (waitqueue_active(&log_root_tree
->log_commit_wait
[index2
]))
2450 wake_up(&log_root_tree
->log_commit_wait
[index2
]);
2452 atomic_set(&root
->log_commit
[index1
], 0);
2454 if (waitqueue_active(&root
->log_commit_wait
[index1
]))
2455 wake_up(&root
->log_commit_wait
[index1
]);
2459 static void free_log_tree(struct btrfs_trans_handle
*trans
,
2460 struct btrfs_root
*log
)
2465 struct walk_control wc
= {
2467 .process_func
= process_one_buffer
2470 ret
= walk_log_tree(trans
, log
, &wc
);
2474 ret
= find_first_extent_bit(&log
->dirty_log_pages
,
2475 0, &start
, &end
, EXTENT_DIRTY
| EXTENT_NEW
,
2480 clear_extent_bits(&log
->dirty_log_pages
, start
, end
,
2481 EXTENT_DIRTY
| EXTENT_NEW
, GFP_NOFS
);
2485 * We may have short-circuited the log tree with the full commit logic
2486 * and left ordered extents on our list, so clear these out to keep us
2487 * from leaking inodes and memory.
2489 btrfs_free_logged_extents(log
, 0);
2490 btrfs_free_logged_extents(log
, 1);
2492 free_extent_buffer(log
->node
);
2497 * free all the extents used by the tree log. This should be called
2498 * at commit time of the full transaction
2500 int btrfs_free_log(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
)
2502 if (root
->log_root
) {
2503 free_log_tree(trans
, root
->log_root
);
2504 root
->log_root
= NULL
;
2509 int btrfs_free_log_root_tree(struct btrfs_trans_handle
*trans
,
2510 struct btrfs_fs_info
*fs_info
)
2512 if (fs_info
->log_root_tree
) {
2513 free_log_tree(trans
, fs_info
->log_root_tree
);
2514 fs_info
->log_root_tree
= NULL
;
2520 * If both a file and directory are logged, and unlinks or renames are
2521 * mixed in, we have a few interesting corners:
2523 * create file X in dir Y
2524 * link file X to X.link in dir Y
2526 * unlink file X but leave X.link
2529 * After a crash we would expect only X.link to exist. But file X
2530 * didn't get fsync'd again so the log has back refs for X and X.link.
2532 * We solve this by removing directory entries and inode backrefs from the
2533 * log when a file that was logged in the current transaction is
2534 * unlinked. Any later fsync will include the updated log entries, and
2535 * we'll be able to reconstruct the proper directory items from backrefs.
2537 * This optimizations allows us to avoid relogging the entire inode
2538 * or the entire directory.
2540 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle
*trans
,
2541 struct btrfs_root
*root
,
2542 const char *name
, int name_len
,
2543 struct inode
*dir
, u64 index
)
2545 struct btrfs_root
*log
;
2546 struct btrfs_dir_item
*di
;
2547 struct btrfs_path
*path
;
2551 u64 dir_ino
= btrfs_ino(dir
);
2553 if (BTRFS_I(dir
)->logged_trans
< trans
->transid
)
2556 ret
= join_running_log_trans(root
);
2560 mutex_lock(&BTRFS_I(dir
)->log_mutex
);
2562 log
= root
->log_root
;
2563 path
= btrfs_alloc_path();
2569 di
= btrfs_lookup_dir_item(trans
, log
, path
, dir_ino
,
2570 name
, name_len
, -1);
2576 ret
= btrfs_delete_one_dir_name(trans
, log
, path
, di
);
2577 bytes_del
+= name_len
;
2580 btrfs_release_path(path
);
2581 di
= btrfs_lookup_dir_index_item(trans
, log
, path
, dir_ino
,
2582 index
, name
, name_len
, -1);
2588 ret
= btrfs_delete_one_dir_name(trans
, log
, path
, di
);
2589 bytes_del
+= name_len
;
2593 /* update the directory size in the log to reflect the names
2597 struct btrfs_key key
;
2599 key
.objectid
= dir_ino
;
2601 key
.type
= BTRFS_INODE_ITEM_KEY
;
2602 btrfs_release_path(path
);
2604 ret
= btrfs_search_slot(trans
, log
, &key
, path
, 0, 1);
2610 struct btrfs_inode_item
*item
;
2613 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2614 struct btrfs_inode_item
);
2615 i_size
= btrfs_inode_size(path
->nodes
[0], item
);
2616 if (i_size
> bytes_del
)
2617 i_size
-= bytes_del
;
2620 btrfs_set_inode_size(path
->nodes
[0], item
, i_size
);
2621 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2624 btrfs_release_path(path
);
2627 btrfs_free_path(path
);
2629 mutex_unlock(&BTRFS_I(dir
)->log_mutex
);
2630 if (ret
== -ENOSPC
) {
2631 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
2634 btrfs_abort_transaction(trans
, root
, ret
);
2636 btrfs_end_log_trans(root
);
2641 /* see comments for btrfs_del_dir_entries_in_log */
2642 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle
*trans
,
2643 struct btrfs_root
*root
,
2644 const char *name
, int name_len
,
2645 struct inode
*inode
, u64 dirid
)
2647 struct btrfs_root
*log
;
2651 if (BTRFS_I(inode
)->logged_trans
< trans
->transid
)
2654 ret
= join_running_log_trans(root
);
2657 log
= root
->log_root
;
2658 mutex_lock(&BTRFS_I(inode
)->log_mutex
);
2660 ret
= btrfs_del_inode_ref(trans
, log
, name
, name_len
, btrfs_ino(inode
),
2662 mutex_unlock(&BTRFS_I(inode
)->log_mutex
);
2663 if (ret
== -ENOSPC
) {
2664 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
2666 } else if (ret
< 0 && ret
!= -ENOENT
)
2667 btrfs_abort_transaction(trans
, root
, ret
);
2668 btrfs_end_log_trans(root
);
2674 * creates a range item in the log for 'dirid'. first_offset and
2675 * last_offset tell us which parts of the key space the log should
2676 * be considered authoritative for.
2678 static noinline
int insert_dir_log_key(struct btrfs_trans_handle
*trans
,
2679 struct btrfs_root
*log
,
2680 struct btrfs_path
*path
,
2681 int key_type
, u64 dirid
,
2682 u64 first_offset
, u64 last_offset
)
2685 struct btrfs_key key
;
2686 struct btrfs_dir_log_item
*item
;
2688 key
.objectid
= dirid
;
2689 key
.offset
= first_offset
;
2690 if (key_type
== BTRFS_DIR_ITEM_KEY
)
2691 key
.type
= BTRFS_DIR_LOG_ITEM_KEY
;
2693 key
.type
= BTRFS_DIR_LOG_INDEX_KEY
;
2694 ret
= btrfs_insert_empty_item(trans
, log
, path
, &key
, sizeof(*item
));
2698 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2699 struct btrfs_dir_log_item
);
2700 btrfs_set_dir_log_end(path
->nodes
[0], item
, last_offset
);
2701 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2702 btrfs_release_path(path
);
2707 * log all the items included in the current transaction for a given
2708 * directory. This also creates the range items in the log tree required
2709 * to replay anything deleted before the fsync
2711 static noinline
int log_dir_items(struct btrfs_trans_handle
*trans
,
2712 struct btrfs_root
*root
, struct inode
*inode
,
2713 struct btrfs_path
*path
,
2714 struct btrfs_path
*dst_path
, int key_type
,
2715 u64 min_offset
, u64
*last_offset_ret
)
2717 struct btrfs_key min_key
;
2718 struct btrfs_key max_key
;
2719 struct btrfs_root
*log
= root
->log_root
;
2720 struct extent_buffer
*src
;
2725 u64 first_offset
= min_offset
;
2726 u64 last_offset
= (u64
)-1;
2727 u64 ino
= btrfs_ino(inode
);
2729 log
= root
->log_root
;
2730 max_key
.objectid
= ino
;
2731 max_key
.offset
= (u64
)-1;
2732 max_key
.type
= key_type
;
2734 min_key
.objectid
= ino
;
2735 min_key
.type
= key_type
;
2736 min_key
.offset
= min_offset
;
2738 path
->keep_locks
= 1;
2740 ret
= btrfs_search_forward(root
, &min_key
, &max_key
,
2741 path
, trans
->transid
);
2744 * we didn't find anything from this transaction, see if there
2745 * is anything at all
2747 if (ret
!= 0 || min_key
.objectid
!= ino
|| min_key
.type
!= key_type
) {
2748 min_key
.objectid
= ino
;
2749 min_key
.type
= key_type
;
2750 min_key
.offset
= (u64
)-1;
2751 btrfs_release_path(path
);
2752 ret
= btrfs_search_slot(NULL
, root
, &min_key
, path
, 0, 0);
2754 btrfs_release_path(path
);
2757 ret
= btrfs_previous_item(root
, path
, ino
, key_type
);
2759 /* if ret == 0 there are items for this type,
2760 * create a range to tell us the last key of this type.
2761 * otherwise, there are no items in this directory after
2762 * *min_offset, and we create a range to indicate that.
2765 struct btrfs_key tmp
;
2766 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
,
2768 if (key_type
== tmp
.type
)
2769 first_offset
= max(min_offset
, tmp
.offset
) + 1;
2774 /* go backward to find any previous key */
2775 ret
= btrfs_previous_item(root
, path
, ino
, key_type
);
2777 struct btrfs_key tmp
;
2778 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
, path
->slots
[0]);
2779 if (key_type
== tmp
.type
) {
2780 first_offset
= tmp
.offset
;
2781 ret
= overwrite_item(trans
, log
, dst_path
,
2782 path
->nodes
[0], path
->slots
[0],
2790 btrfs_release_path(path
);
2792 /* find the first key from this transaction again */
2793 ret
= btrfs_search_slot(NULL
, root
, &min_key
, path
, 0, 0);
2800 * we have a block from this transaction, log every item in it
2801 * from our directory
2804 struct btrfs_key tmp
;
2805 src
= path
->nodes
[0];
2806 nritems
= btrfs_header_nritems(src
);
2807 for (i
= path
->slots
[0]; i
< nritems
; i
++) {
2808 btrfs_item_key_to_cpu(src
, &min_key
, i
);
2810 if (min_key
.objectid
!= ino
|| min_key
.type
!= key_type
)
2812 ret
= overwrite_item(trans
, log
, dst_path
, src
, i
,
2819 path
->slots
[0] = nritems
;
2822 * look ahead to the next item and see if it is also
2823 * from this directory and from this transaction
2825 ret
= btrfs_next_leaf(root
, path
);
2827 last_offset
= (u64
)-1;
2830 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
, path
->slots
[0]);
2831 if (tmp
.objectid
!= ino
|| tmp
.type
!= key_type
) {
2832 last_offset
= (u64
)-1;
2835 if (btrfs_header_generation(path
->nodes
[0]) != trans
->transid
) {
2836 ret
= overwrite_item(trans
, log
, dst_path
,
2837 path
->nodes
[0], path
->slots
[0],
2842 last_offset
= tmp
.offset
;
2847 btrfs_release_path(path
);
2848 btrfs_release_path(dst_path
);
2851 *last_offset_ret
= last_offset
;
2853 * insert the log range keys to indicate where the log
2856 ret
= insert_dir_log_key(trans
, log
, path
, key_type
,
2857 ino
, first_offset
, last_offset
);
2865 * logging directories is very similar to logging inodes, We find all the items
2866 * from the current transaction and write them to the log.
2868 * The recovery code scans the directory in the subvolume, and if it finds a
2869 * key in the range logged that is not present in the log tree, then it means
2870 * that dir entry was unlinked during the transaction.
2872 * In order for that scan to work, we must include one key smaller than
2873 * the smallest logged by this transaction and one key larger than the largest
2874 * key logged by this transaction.
2876 static noinline
int log_directory_changes(struct btrfs_trans_handle
*trans
,
2877 struct btrfs_root
*root
, struct inode
*inode
,
2878 struct btrfs_path
*path
,
2879 struct btrfs_path
*dst_path
)
2884 int key_type
= BTRFS_DIR_ITEM_KEY
;
2890 ret
= log_dir_items(trans
, root
, inode
, path
,
2891 dst_path
, key_type
, min_key
,
2895 if (max_key
== (u64
)-1)
2897 min_key
= max_key
+ 1;
2900 if (key_type
== BTRFS_DIR_ITEM_KEY
) {
2901 key_type
= BTRFS_DIR_INDEX_KEY
;
2908 * a helper function to drop items from the log before we relog an
2909 * inode. max_key_type indicates the highest item type to remove.
2910 * This cannot be run for file data extents because it does not
2911 * free the extents they point to.
2913 static int drop_objectid_items(struct btrfs_trans_handle
*trans
,
2914 struct btrfs_root
*log
,
2915 struct btrfs_path
*path
,
2916 u64 objectid
, int max_key_type
)
2919 struct btrfs_key key
;
2920 struct btrfs_key found_key
;
2923 key
.objectid
= objectid
;
2924 key
.type
= max_key_type
;
2925 key
.offset
= (u64
)-1;
2928 ret
= btrfs_search_slot(trans
, log
, &key
, path
, -1, 1);
2933 if (path
->slots
[0] == 0)
2937 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2940 if (found_key
.objectid
!= objectid
)
2943 found_key
.offset
= 0;
2945 ret
= btrfs_bin_search(path
->nodes
[0], &found_key
, 0,
2948 ret
= btrfs_del_items(trans
, log
, path
, start_slot
,
2949 path
->slots
[0] - start_slot
+ 1);
2951 * If start slot isn't 0 then we don't need to re-search, we've
2952 * found the last guy with the objectid in this tree.
2954 if (ret
|| start_slot
!= 0)
2956 btrfs_release_path(path
);
2958 btrfs_release_path(path
);
2964 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
2965 struct extent_buffer
*leaf
,
2966 struct btrfs_inode_item
*item
,
2967 struct inode
*inode
, int log_inode_only
)
2969 struct btrfs_map_token token
;
2971 btrfs_init_map_token(&token
);
2973 if (log_inode_only
) {
2974 /* set the generation to zero so the recover code
2975 * can tell the difference between an logging
2976 * just to say 'this inode exists' and a logging
2977 * to say 'update this inode with these values'
2979 btrfs_set_token_inode_generation(leaf
, item
, 0, &token
);
2980 btrfs_set_token_inode_size(leaf
, item
, 0, &token
);
2982 btrfs_set_token_inode_generation(leaf
, item
,
2983 BTRFS_I(inode
)->generation
,
2985 btrfs_set_token_inode_size(leaf
, item
, inode
->i_size
, &token
);
2988 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
2989 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
2990 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
2991 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
2993 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_atime(item
),
2994 inode
->i_atime
.tv_sec
, &token
);
2995 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_atime(item
),
2996 inode
->i_atime
.tv_nsec
, &token
);
2998 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_mtime(item
),
2999 inode
->i_mtime
.tv_sec
, &token
);
3000 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
3001 inode
->i_mtime
.tv_nsec
, &token
);
3003 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_ctime(item
),
3004 inode
->i_ctime
.tv_sec
, &token
);
3005 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
3006 inode
->i_ctime
.tv_nsec
, &token
);
3008 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3011 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3012 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3013 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3014 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3015 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3018 static int log_inode_item(struct btrfs_trans_handle
*trans
,
3019 struct btrfs_root
*log
, struct btrfs_path
*path
,
3020 struct inode
*inode
)
3022 struct btrfs_inode_item
*inode_item
;
3023 struct btrfs_key key
;
3026 memcpy(&key
, &BTRFS_I(inode
)->location
, sizeof(key
));
3027 ret
= btrfs_insert_empty_item(trans
, log
, path
, &key
,
3028 sizeof(*inode_item
));
3029 if (ret
&& ret
!= -EEXIST
)
3031 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3032 struct btrfs_inode_item
);
3033 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
, 0);
3034 btrfs_release_path(path
);
3038 static noinline
int copy_items(struct btrfs_trans_handle
*trans
,
3039 struct inode
*inode
,
3040 struct btrfs_path
*dst_path
,
3041 struct extent_buffer
*src
,
3042 int start_slot
, int nr
, int inode_only
)
3044 unsigned long src_offset
;
3045 unsigned long dst_offset
;
3046 struct btrfs_root
*log
= BTRFS_I(inode
)->root
->log_root
;
3047 struct btrfs_file_extent_item
*extent
;
3048 struct btrfs_inode_item
*inode_item
;
3050 struct btrfs_key
*ins_keys
;
3054 struct list_head ordered_sums
;
3055 int skip_csum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
3057 INIT_LIST_HEAD(&ordered_sums
);
3059 ins_data
= kmalloc(nr
* sizeof(struct btrfs_key
) +
3060 nr
* sizeof(u32
), GFP_NOFS
);
3064 ins_sizes
= (u32
*)ins_data
;
3065 ins_keys
= (struct btrfs_key
*)(ins_data
+ nr
* sizeof(u32
));
3067 for (i
= 0; i
< nr
; i
++) {
3068 ins_sizes
[i
] = btrfs_item_size_nr(src
, i
+ start_slot
);
3069 btrfs_item_key_to_cpu(src
, ins_keys
+ i
, i
+ start_slot
);
3071 ret
= btrfs_insert_empty_items(trans
, log
, dst_path
,
3072 ins_keys
, ins_sizes
, nr
);
3078 for (i
= 0; i
< nr
; i
++, dst_path
->slots
[0]++) {
3079 dst_offset
= btrfs_item_ptr_offset(dst_path
->nodes
[0],
3080 dst_path
->slots
[0]);
3082 src_offset
= btrfs_item_ptr_offset(src
, start_slot
+ i
);
3084 if (ins_keys
[i
].type
== BTRFS_INODE_ITEM_KEY
) {
3085 inode_item
= btrfs_item_ptr(dst_path
->nodes
[0],
3087 struct btrfs_inode_item
);
3088 fill_inode_item(trans
, dst_path
->nodes
[0], inode_item
,
3089 inode
, inode_only
== LOG_INODE_EXISTS
);
3091 copy_extent_buffer(dst_path
->nodes
[0], src
, dst_offset
,
3092 src_offset
, ins_sizes
[i
]);
3095 /* take a reference on file data extents so that truncates
3096 * or deletes of this inode don't have to relog the inode
3099 if (btrfs_key_type(ins_keys
+ i
) == BTRFS_EXTENT_DATA_KEY
&&
3102 extent
= btrfs_item_ptr(src
, start_slot
+ i
,
3103 struct btrfs_file_extent_item
);
3105 if (btrfs_file_extent_generation(src
, extent
) < trans
->transid
)
3108 found_type
= btrfs_file_extent_type(src
, extent
);
3109 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
3111 ds
= btrfs_file_extent_disk_bytenr(src
,
3113 /* ds == 0 is a hole */
3117 dl
= btrfs_file_extent_disk_num_bytes(src
,
3119 cs
= btrfs_file_extent_offset(src
, extent
);
3120 cl
= btrfs_file_extent_num_bytes(src
,
3122 if (btrfs_file_extent_compression(src
,
3128 ret
= btrfs_lookup_csums_range(
3129 log
->fs_info
->csum_root
,
3130 ds
+ cs
, ds
+ cs
+ cl
- 1,
3137 btrfs_mark_buffer_dirty(dst_path
->nodes
[0]);
3138 btrfs_release_path(dst_path
);
3142 * we have to do this after the loop above to avoid changing the
3143 * log tree while trying to change the log tree.
3146 while (!list_empty(&ordered_sums
)) {
3147 struct btrfs_ordered_sum
*sums
= list_entry(ordered_sums
.next
,
3148 struct btrfs_ordered_sum
,
3151 ret
= btrfs_csum_file_blocks(trans
, log
, sums
);
3152 list_del(&sums
->list
);
3158 static int extent_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
3160 struct extent_map
*em1
, *em2
;
3162 em1
= list_entry(a
, struct extent_map
, list
);
3163 em2
= list_entry(b
, struct extent_map
, list
);
3165 if (em1
->start
< em2
->start
)
3167 else if (em1
->start
> em2
->start
)
3172 static int drop_adjacent_extents(struct btrfs_trans_handle
*trans
,
3173 struct btrfs_root
*root
, struct inode
*inode
,
3174 struct extent_map
*em
,
3175 struct btrfs_path
*path
)
3177 struct btrfs_file_extent_item
*fi
;
3178 struct extent_buffer
*leaf
;
3179 struct btrfs_key key
, new_key
;
3180 struct btrfs_map_token token
;
3182 u64 extent_offset
= 0;
3189 btrfs_init_map_token(&token
);
3190 leaf
= path
->nodes
[0];
3192 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
3194 ret
= btrfs_del_items(trans
, root
, path
,
3201 ret
= btrfs_next_leaf_write(trans
, root
, path
, 1);
3206 leaf
= path
->nodes
[0];
3209 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3210 if (key
.objectid
!= btrfs_ino(inode
) ||
3211 key
.type
!= BTRFS_EXTENT_DATA_KEY
||
3212 key
.offset
>= em
->start
+ em
->len
)
3215 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3216 struct btrfs_file_extent_item
);
3217 extent_type
= btrfs_token_file_extent_type(leaf
, fi
, &token
);
3218 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
3219 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
3220 extent_offset
= btrfs_token_file_extent_offset(leaf
,
3222 extent_end
= key
.offset
+
3223 btrfs_token_file_extent_num_bytes(leaf
, fi
,
3225 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3226 extent_end
= key
.offset
+
3227 btrfs_file_extent_inline_len(leaf
, fi
);
3232 if (extent_end
<= em
->len
+ em
->start
) {
3234 del_slot
= path
->slots
[0];
3241 * Ok so we'll ignore previous items if we log a new extent,
3242 * which can lead to overlapping extents, so if we have an
3243 * existing extent we want to adjust we _have_ to check the next
3244 * guy to make sure we even need this extent anymore, this keeps
3245 * us from panicing in set_item_key_safe.
3247 if (path
->slots
[0] < btrfs_header_nritems(leaf
) - 1) {
3248 struct btrfs_key tmp_key
;
3250 btrfs_item_key_to_cpu(leaf
, &tmp_key
,
3251 path
->slots
[0] + 1);
3252 if (tmp_key
.objectid
== btrfs_ino(inode
) &&
3253 tmp_key
.type
== BTRFS_EXTENT_DATA_KEY
&&
3254 tmp_key
.offset
<= em
->start
+ em
->len
) {
3256 del_slot
= path
->slots
[0];
3262 BUG_ON(extent_type
== BTRFS_FILE_EXTENT_INLINE
);
3263 memcpy(&new_key
, &key
, sizeof(new_key
));
3264 new_key
.offset
= em
->start
+ em
->len
;
3265 btrfs_set_item_key_safe(trans
, root
, path
, &new_key
);
3266 extent_offset
+= em
->start
+ em
->len
- key
.offset
;
3267 btrfs_set_token_file_extent_offset(leaf
, fi
, extent_offset
,
3269 btrfs_set_token_file_extent_num_bytes(leaf
, fi
, extent_end
-
3270 (em
->start
+ em
->len
),
3272 btrfs_mark_buffer_dirty(leaf
);
3276 ret
= btrfs_del_items(trans
, root
, path
, del_slot
, del_nr
);
3281 static int log_one_extent(struct btrfs_trans_handle
*trans
,
3282 struct inode
*inode
, struct btrfs_root
*root
,
3283 struct extent_map
*em
, struct btrfs_path
*path
)
3285 struct btrfs_root
*log
= root
->log_root
;
3286 struct btrfs_file_extent_item
*fi
;
3287 struct extent_buffer
*leaf
;
3288 struct btrfs_ordered_extent
*ordered
;
3289 struct list_head ordered_sums
;
3290 struct btrfs_map_token token
;
3291 struct btrfs_key key
;
3292 u64 mod_start
= em
->mod_start
;
3293 u64 mod_len
= em
->mod_len
;
3296 u64 extent_offset
= em
->start
- em
->orig_start
;
3299 int index
= log
->log_transid
% 2;
3300 bool skip_csum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
3302 INIT_LIST_HEAD(&ordered_sums
);
3303 btrfs_init_map_token(&token
);
3304 key
.objectid
= btrfs_ino(inode
);
3305 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3306 key
.offset
= em
->start
;
3307 path
->really_keep_locks
= 1;
3309 ret
= btrfs_insert_empty_item(trans
, log
, path
, &key
, sizeof(*fi
));
3310 if (ret
&& ret
!= -EEXIST
) {
3311 path
->really_keep_locks
= 0;
3314 leaf
= path
->nodes
[0];
3315 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3316 struct btrfs_file_extent_item
);
3317 btrfs_set_token_file_extent_generation(leaf
, fi
, em
->generation
,
3319 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
3321 btrfs_set_token_file_extent_type(leaf
, fi
,
3322 BTRFS_FILE_EXTENT_PREALLOC
,
3325 btrfs_set_token_file_extent_type(leaf
, fi
,
3326 BTRFS_FILE_EXTENT_REG
,
3328 if (em
->block_start
== 0)
3332 block_len
= max(em
->block_len
, em
->orig_block_len
);
3333 if (em
->compress_type
!= BTRFS_COMPRESS_NONE
) {
3334 btrfs_set_token_file_extent_disk_bytenr(leaf
, fi
,
3337 btrfs_set_token_file_extent_disk_num_bytes(leaf
, fi
, block_len
,
3339 } else if (em
->block_start
< EXTENT_MAP_LAST_BYTE
) {
3340 btrfs_set_token_file_extent_disk_bytenr(leaf
, fi
,
3342 extent_offset
, &token
);
3343 btrfs_set_token_file_extent_disk_num_bytes(leaf
, fi
, block_len
,
3346 btrfs_set_token_file_extent_disk_bytenr(leaf
, fi
, 0, &token
);
3347 btrfs_set_token_file_extent_disk_num_bytes(leaf
, fi
, 0,
3351 btrfs_set_token_file_extent_offset(leaf
, fi
,
3352 em
->start
- em
->orig_start
,
3354 btrfs_set_token_file_extent_num_bytes(leaf
, fi
, em
->len
, &token
);
3355 btrfs_set_token_file_extent_ram_bytes(leaf
, fi
, em
->len
, &token
);
3356 btrfs_set_token_file_extent_compression(leaf
, fi
, em
->compress_type
,
3358 btrfs_set_token_file_extent_encryption(leaf
, fi
, 0, &token
);
3359 btrfs_set_token_file_extent_other_encoding(leaf
, fi
, 0, &token
);
3360 btrfs_mark_buffer_dirty(leaf
);
3363 * Have to check the extent to the right of us to make sure it doesn't
3364 * fall in our current range. We're ok if the previous extent is in our
3365 * range since the recovery stuff will run us in key order and thus just
3366 * drop the part we overwrote.
3368 ret
= drop_adjacent_extents(trans
, log
, inode
, em
, path
);
3369 btrfs_release_path(path
);
3370 path
->really_keep_locks
= 0;
3378 if (em
->compress_type
) {
3380 csum_len
= block_len
;
3384 * First check and see if our csums are on our outstanding ordered
3388 spin_lock_irq(&log
->log_extents_lock
[index
]);
3389 list_for_each_entry(ordered
, &log
->logged_list
[index
], log_list
) {
3390 struct btrfs_ordered_sum
*sum
;
3395 if (ordered
->inode
!= inode
)
3398 if (ordered
->file_offset
+ ordered
->len
<= mod_start
||
3399 mod_start
+ mod_len
<= ordered
->file_offset
)
3403 * We are going to copy all the csums on this ordered extent, so
3404 * go ahead and adjust mod_start and mod_len in case this
3405 * ordered extent has already been logged.
3407 if (ordered
->file_offset
> mod_start
) {
3408 if (ordered
->file_offset
+ ordered
->len
>=
3409 mod_start
+ mod_len
)
3410 mod_len
= ordered
->file_offset
- mod_start
;
3412 * If we have this case
3414 * |--------- logged extent ---------|
3415 * |----- ordered extent ----|
3417 * Just don't mess with mod_start and mod_len, we'll
3418 * just end up logging more csums than we need and it
3422 if (ordered
->file_offset
+ ordered
->len
<
3423 mod_start
+ mod_len
) {
3424 mod_len
= (mod_start
+ mod_len
) -
3425 (ordered
->file_offset
+ ordered
->len
);
3426 mod_start
= ordered
->file_offset
+
3434 * To keep us from looping for the above case of an ordered
3435 * extent that falls inside of the logged extent.
3437 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM
,
3440 atomic_inc(&ordered
->refs
);
3441 spin_unlock_irq(&log
->log_extents_lock
[index
]);
3443 * we've dropped the lock, we must either break or
3444 * start over after this.
3447 wait_event(ordered
->wait
, ordered
->csum_bytes_left
== 0);
3449 list_for_each_entry(sum
, &ordered
->list
, list
) {
3450 ret
= btrfs_csum_file_blocks(trans
, log
, sum
);
3452 btrfs_put_ordered_extent(ordered
);
3456 btrfs_put_ordered_extent(ordered
);
3460 spin_unlock_irq(&log
->log_extents_lock
[index
]);
3463 if (!mod_len
|| ret
)
3466 csum_offset
= mod_start
- em
->start
;
3469 /* block start is already adjusted for the file extent offset. */
3470 ret
= btrfs_lookup_csums_range(log
->fs_info
->csum_root
,
3471 em
->block_start
+ csum_offset
,
3472 em
->block_start
+ csum_offset
+
3473 csum_len
- 1, &ordered_sums
, 0);
3477 while (!list_empty(&ordered_sums
)) {
3478 struct btrfs_ordered_sum
*sums
= list_entry(ordered_sums
.next
,
3479 struct btrfs_ordered_sum
,
3482 ret
= btrfs_csum_file_blocks(trans
, log
, sums
);
3483 list_del(&sums
->list
);
3490 static int btrfs_log_changed_extents(struct btrfs_trans_handle
*trans
,
3491 struct btrfs_root
*root
,
3492 struct inode
*inode
,
3493 struct btrfs_path
*path
)
3495 struct extent_map
*em
, *n
;
3496 struct list_head extents
;
3497 struct extent_map_tree
*tree
= &BTRFS_I(inode
)->extent_tree
;
3502 INIT_LIST_HEAD(&extents
);
3504 write_lock(&tree
->lock
);
3505 test_gen
= root
->fs_info
->last_trans_committed
;
3507 list_for_each_entry_safe(em
, n
, &tree
->modified_extents
, list
) {
3508 list_del_init(&em
->list
);
3511 * Just an arbitrary number, this can be really CPU intensive
3512 * once we start getting a lot of extents, and really once we
3513 * have a bunch of extents we just want to commit since it will
3516 if (++num
> 32768) {
3517 list_del_init(&tree
->modified_extents
);
3522 if (em
->generation
<= test_gen
)
3524 /* Need a ref to keep it from getting evicted from cache */
3525 atomic_inc(&em
->refs
);
3526 set_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
3527 list_add_tail(&em
->list
, &extents
);
3531 list_sort(NULL
, &extents
, extent_cmp
);
3534 while (!list_empty(&extents
)) {
3535 em
= list_entry(extents
.next
, struct extent_map
, list
);
3537 list_del_init(&em
->list
);
3540 * If we had an error we just need to delete everybody from our
3544 clear_em_logging(tree
, em
);
3545 free_extent_map(em
);
3549 write_unlock(&tree
->lock
);
3551 ret
= log_one_extent(trans
, inode
, root
, em
, path
);
3552 write_lock(&tree
->lock
);
3553 clear_em_logging(tree
, em
);
3554 free_extent_map(em
);
3556 WARN_ON(!list_empty(&extents
));
3557 write_unlock(&tree
->lock
);
3559 btrfs_release_path(path
);
3563 /* log a single inode in the tree log.
3564 * At least one parent directory for this inode must exist in the tree
3565 * or be logged already.
3567 * Any items from this inode changed by the current transaction are copied
3568 * to the log tree. An extra reference is taken on any extents in this
3569 * file, allowing us to avoid a whole pile of corner cases around logging
3570 * blocks that have been removed from the tree.
3572 * See LOG_INODE_ALL and related defines for a description of what inode_only
3575 * This handles both files and directories.
3577 static int btrfs_log_inode(struct btrfs_trans_handle
*trans
,
3578 struct btrfs_root
*root
, struct inode
*inode
,
3581 struct btrfs_path
*path
;
3582 struct btrfs_path
*dst_path
;
3583 struct btrfs_key min_key
;
3584 struct btrfs_key max_key
;
3585 struct btrfs_root
*log
= root
->log_root
;
3586 struct extent_buffer
*src
= NULL
;
3590 int ins_start_slot
= 0;
3592 bool fast_search
= false;
3593 u64 ino
= btrfs_ino(inode
);
3595 log
= root
->log_root
;
3597 path
= btrfs_alloc_path();
3600 dst_path
= btrfs_alloc_path();
3602 btrfs_free_path(path
);
3606 min_key
.objectid
= ino
;
3607 min_key
.type
= BTRFS_INODE_ITEM_KEY
;
3610 max_key
.objectid
= ino
;
3613 /* today the code can only do partial logging of directories */
3614 if (S_ISDIR(inode
->i_mode
) ||
3615 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3616 &BTRFS_I(inode
)->runtime_flags
) &&
3617 inode_only
== LOG_INODE_EXISTS
))
3618 max_key
.type
= BTRFS_XATTR_ITEM_KEY
;
3620 max_key
.type
= (u8
)-1;
3621 max_key
.offset
= (u64
)-1;
3623 /* Only run delayed items if we are a dir or a new file */
3624 if (S_ISDIR(inode
->i_mode
) ||
3625 BTRFS_I(inode
)->generation
> root
->fs_info
->last_trans_committed
) {
3626 ret
= btrfs_commit_inode_delayed_items(trans
, inode
);
3628 btrfs_free_path(path
);
3629 btrfs_free_path(dst_path
);
3634 mutex_lock(&BTRFS_I(inode
)->log_mutex
);
3636 btrfs_get_logged_extents(log
, inode
);
3639 * a brute force approach to making sure we get the most uptodate
3640 * copies of everything.
3642 if (S_ISDIR(inode
->i_mode
)) {
3643 int max_key_type
= BTRFS_DIR_LOG_INDEX_KEY
;
3645 if (inode_only
== LOG_INODE_EXISTS
)
3646 max_key_type
= BTRFS_XATTR_ITEM_KEY
;
3647 ret
= drop_objectid_items(trans
, log
, path
, ino
, max_key_type
);
3649 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3650 &BTRFS_I(inode
)->runtime_flags
)) {
3651 clear_bit(BTRFS_INODE_COPY_EVERYTHING
,
3652 &BTRFS_I(inode
)->runtime_flags
);
3653 ret
= btrfs_truncate_inode_items(trans
, log
,
3655 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING
,
3656 &BTRFS_I(inode
)->runtime_flags
)) {
3657 if (inode_only
== LOG_INODE_ALL
)
3659 max_key
.type
= BTRFS_XATTR_ITEM_KEY
;
3660 ret
= drop_objectid_items(trans
, log
, path
, ino
,
3663 if (inode_only
== LOG_INODE_ALL
)
3665 ret
= log_inode_item(trans
, log
, dst_path
, inode
);
3678 path
->keep_locks
= 1;
3682 ret
= btrfs_search_forward(root
, &min_key
, &max_key
,
3683 path
, trans
->transid
);
3687 /* note, ins_nr might be > 0 here, cleanup outside the loop */
3688 if (min_key
.objectid
!= ino
)
3690 if (min_key
.type
> max_key
.type
)
3693 src
= path
->nodes
[0];
3694 if (ins_nr
&& ins_start_slot
+ ins_nr
== path
->slots
[0]) {
3697 } else if (!ins_nr
) {
3698 ins_start_slot
= path
->slots
[0];
3703 ret
= copy_items(trans
, inode
, dst_path
, src
, ins_start_slot
,
3704 ins_nr
, inode_only
);
3710 ins_start_slot
= path
->slots
[0];
3713 nritems
= btrfs_header_nritems(path
->nodes
[0]);
3715 if (path
->slots
[0] < nritems
) {
3716 btrfs_item_key_to_cpu(path
->nodes
[0], &min_key
,
3721 ret
= copy_items(trans
, inode
, dst_path
, src
,
3723 ins_nr
, inode_only
);
3730 btrfs_release_path(path
);
3732 if (min_key
.offset
< (u64
)-1)
3734 else if (min_key
.type
< (u8
)-1)
3736 else if (min_key
.objectid
< (u64
)-1)
3742 ret
= copy_items(trans
, inode
, dst_path
, src
, ins_start_slot
,
3743 ins_nr
, inode_only
);
3753 btrfs_release_path(dst_path
);
3754 ret
= btrfs_log_changed_extents(trans
, root
, inode
, dst_path
);
3760 struct extent_map_tree
*tree
= &BTRFS_I(inode
)->extent_tree
;
3761 struct extent_map
*em
, *n
;
3763 write_lock(&tree
->lock
);
3764 list_for_each_entry_safe(em
, n
, &tree
->modified_extents
, list
)
3765 list_del_init(&em
->list
);
3766 write_unlock(&tree
->lock
);
3769 if (inode_only
== LOG_INODE_ALL
&& S_ISDIR(inode
->i_mode
)) {
3770 btrfs_release_path(path
);
3771 btrfs_release_path(dst_path
);
3772 ret
= log_directory_changes(trans
, root
, inode
, path
, dst_path
);
3778 BTRFS_I(inode
)->logged_trans
= trans
->transid
;
3779 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->last_sub_trans
;
3782 btrfs_free_logged_extents(log
, log
->log_transid
);
3783 mutex_unlock(&BTRFS_I(inode
)->log_mutex
);
3785 btrfs_free_path(path
);
3786 btrfs_free_path(dst_path
);
3791 * follow the dentry parent pointers up the chain and see if any
3792 * of the directories in it require a full commit before they can
3793 * be logged. Returns zero if nothing special needs to be done or 1 if
3794 * a full commit is required.
3796 static noinline
int check_parent_dirs_for_sync(struct btrfs_trans_handle
*trans
,
3797 struct inode
*inode
,
3798 struct dentry
*parent
,
3799 struct super_block
*sb
,
3803 struct btrfs_root
*root
;
3804 struct dentry
*old_parent
= NULL
;
3807 * for regular files, if its inode is already on disk, we don't
3808 * have to worry about the parents at all. This is because
3809 * we can use the last_unlink_trans field to record renames
3810 * and other fun in this file.
3812 if (S_ISREG(inode
->i_mode
) &&
3813 BTRFS_I(inode
)->generation
<= last_committed
&&
3814 BTRFS_I(inode
)->last_unlink_trans
<= last_committed
)
3817 if (!S_ISDIR(inode
->i_mode
)) {
3818 if (!parent
|| !parent
->d_inode
|| sb
!= parent
->d_inode
->i_sb
)
3820 inode
= parent
->d_inode
;
3824 BTRFS_I(inode
)->logged_trans
= trans
->transid
;
3827 if (BTRFS_I(inode
)->last_unlink_trans
> last_committed
) {
3828 root
= BTRFS_I(inode
)->root
;
3831 * make sure any commits to the log are forced
3832 * to be full commits
3834 root
->fs_info
->last_trans_log_full_commit
=
3840 if (!parent
|| !parent
->d_inode
|| sb
!= parent
->d_inode
->i_sb
)
3843 if (IS_ROOT(parent
))
3846 parent
= dget_parent(parent
);
3848 old_parent
= parent
;
3849 inode
= parent
->d_inode
;
3858 * helper function around btrfs_log_inode to make sure newly created
3859 * parent directories also end up in the log. A minimal inode and backref
3860 * only logging is done of any parent directories that are older than
3861 * the last committed transaction
3863 int btrfs_log_inode_parent(struct btrfs_trans_handle
*trans
,
3864 struct btrfs_root
*root
, struct inode
*inode
,
3865 struct dentry
*parent
, int exists_only
)
3867 int inode_only
= exists_only
? LOG_INODE_EXISTS
: LOG_INODE_ALL
;
3868 struct super_block
*sb
;
3869 struct dentry
*old_parent
= NULL
;
3871 u64 last_committed
= root
->fs_info
->last_trans_committed
;
3875 if (btrfs_test_opt(root
, NOTREELOG
)) {
3880 if (root
->fs_info
->last_trans_log_full_commit
>
3881 root
->fs_info
->last_trans_committed
) {
3886 if (root
!= BTRFS_I(inode
)->root
||
3887 btrfs_root_refs(&root
->root_item
) == 0) {
3892 ret
= check_parent_dirs_for_sync(trans
, inode
, parent
,
3893 sb
, last_committed
);
3897 if (btrfs_inode_in_log(inode
, trans
->transid
)) {
3898 ret
= BTRFS_NO_LOG_SYNC
;
3902 ret
= start_log_trans(trans
, root
);
3906 ret
= btrfs_log_inode(trans
, root
, inode
, inode_only
);
3911 * for regular files, if its inode is already on disk, we don't
3912 * have to worry about the parents at all. This is because
3913 * we can use the last_unlink_trans field to record renames
3914 * and other fun in this file.
3916 if (S_ISREG(inode
->i_mode
) &&
3917 BTRFS_I(inode
)->generation
<= last_committed
&&
3918 BTRFS_I(inode
)->last_unlink_trans
<= last_committed
) {
3923 inode_only
= LOG_INODE_EXISTS
;
3925 if (!parent
|| !parent
->d_inode
|| sb
!= parent
->d_inode
->i_sb
)
3928 inode
= parent
->d_inode
;
3929 if (root
!= BTRFS_I(inode
)->root
)
3932 if (BTRFS_I(inode
)->generation
>
3933 root
->fs_info
->last_trans_committed
) {
3934 ret
= btrfs_log_inode(trans
, root
, inode
, inode_only
);
3938 if (IS_ROOT(parent
))
3941 parent
= dget_parent(parent
);
3943 old_parent
= parent
;
3949 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
3952 btrfs_end_log_trans(root
);
3958 * it is not safe to log dentry if the chunk root has added new
3959 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3960 * If this returns 1, you must commit the transaction to safely get your
3963 int btrfs_log_dentry_safe(struct btrfs_trans_handle
*trans
,
3964 struct btrfs_root
*root
, struct dentry
*dentry
)
3966 struct dentry
*parent
= dget_parent(dentry
);
3969 ret
= btrfs_log_inode_parent(trans
, root
, dentry
->d_inode
, parent
, 0);
3976 * should be called during mount to recover any replay any log trees
3979 int btrfs_recover_log_trees(struct btrfs_root
*log_root_tree
)
3982 struct btrfs_path
*path
;
3983 struct btrfs_trans_handle
*trans
;
3984 struct btrfs_key key
;
3985 struct btrfs_key found_key
;
3986 struct btrfs_key tmp_key
;
3987 struct btrfs_root
*log
;
3988 struct btrfs_fs_info
*fs_info
= log_root_tree
->fs_info
;
3989 struct walk_control wc
= {
3990 .process_func
= process_one_buffer
,
3994 path
= btrfs_alloc_path();
3998 fs_info
->log_root_recovering
= 1;
4000 trans
= btrfs_start_transaction(fs_info
->tree_root
, 0);
4001 if (IS_ERR(trans
)) {
4002 ret
= PTR_ERR(trans
);
4009 ret
= walk_log_tree(trans
, log_root_tree
, &wc
);
4011 btrfs_error(fs_info
, ret
, "Failed to pin buffers while "
4012 "recovering log root tree.");
4017 key
.objectid
= BTRFS_TREE_LOG_OBJECTID
;
4018 key
.offset
= (u64
)-1;
4019 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
4022 ret
= btrfs_search_slot(NULL
, log_root_tree
, &key
, path
, 0, 0);
4025 btrfs_error(fs_info
, ret
,
4026 "Couldn't find tree log root.");
4030 if (path
->slots
[0] == 0)
4034 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
4036 btrfs_release_path(path
);
4037 if (found_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
4040 log
= btrfs_read_fs_root_no_radix(log_root_tree
,
4044 btrfs_error(fs_info
, ret
,
4045 "Couldn't read tree log root.");
4049 tmp_key
.objectid
= found_key
.offset
;
4050 tmp_key
.type
= BTRFS_ROOT_ITEM_KEY
;
4051 tmp_key
.offset
= (u64
)-1;
4053 wc
.replay_dest
= btrfs_read_fs_root_no_name(fs_info
, &tmp_key
);
4054 if (IS_ERR(wc
.replay_dest
)) {
4055 ret
= PTR_ERR(wc
.replay_dest
);
4056 btrfs_error(fs_info
, ret
, "Couldn't read target root "
4057 "for tree log recovery.");
4061 wc
.replay_dest
->log_root
= log
;
4062 btrfs_record_root_in_trans(trans
, wc
.replay_dest
);
4063 ret
= walk_log_tree(trans
, log
, &wc
);
4066 if (wc
.stage
== LOG_WALK_REPLAY_ALL
) {
4067 ret
= fixup_inode_link_counts(trans
, wc
.replay_dest
,
4072 key
.offset
= found_key
.offset
- 1;
4073 wc
.replay_dest
->log_root
= NULL
;
4074 free_extent_buffer(log
->node
);
4075 free_extent_buffer(log
->commit_root
);
4078 if (found_key
.offset
== 0)
4081 btrfs_release_path(path
);
4083 /* step one is to pin it all, step two is to replay just inodes */
4086 wc
.process_func
= replay_one_buffer
;
4087 wc
.stage
= LOG_WALK_REPLAY_INODES
;
4090 /* step three is to replay everything */
4091 if (wc
.stage
< LOG_WALK_REPLAY_ALL
) {
4096 btrfs_free_path(path
);
4098 free_extent_buffer(log_root_tree
->node
);
4099 log_root_tree
->log_root
= NULL
;
4100 fs_info
->log_root_recovering
= 0;
4102 /* step 4: commit the transaction, which also unpins the blocks */
4103 btrfs_commit_transaction(trans
, fs_info
->tree_root
);
4105 kfree(log_root_tree
);
4109 btrfs_free_path(path
);
4114 * there are some corner cases where we want to force a full
4115 * commit instead of allowing a directory to be logged.
4117 * They revolve around files there were unlinked from the directory, and
4118 * this function updates the parent directory so that a full commit is
4119 * properly done if it is fsync'd later after the unlinks are done.
4121 void btrfs_record_unlink_dir(struct btrfs_trans_handle
*trans
,
4122 struct inode
*dir
, struct inode
*inode
,
4126 * when we're logging a file, if it hasn't been renamed
4127 * or unlinked, and its inode is fully committed on disk,
4128 * we don't have to worry about walking up the directory chain
4129 * to log its parents.
4131 * So, we use the last_unlink_trans field to put this transid
4132 * into the file. When the file is logged we check it and
4133 * don't log the parents if the file is fully on disk.
4135 if (S_ISREG(inode
->i_mode
))
4136 BTRFS_I(inode
)->last_unlink_trans
= trans
->transid
;
4139 * if this directory was already logged any new
4140 * names for this file/dir will get recorded
4143 if (BTRFS_I(dir
)->logged_trans
== trans
->transid
)
4147 * if the inode we're about to unlink was logged,
4148 * the log will be properly updated for any new names
4150 if (BTRFS_I(inode
)->logged_trans
== trans
->transid
)
4154 * when renaming files across directories, if the directory
4155 * there we're unlinking from gets fsync'd later on, there's
4156 * no way to find the destination directory later and fsync it
4157 * properly. So, we have to be conservative and force commits
4158 * so the new name gets discovered.
4163 /* we can safely do the unlink without any special recording */
4167 BTRFS_I(dir
)->last_unlink_trans
= trans
->transid
;
4171 * Call this after adding a new name for a file and it will properly
4172 * update the log to reflect the new name.
4174 * It will return zero if all goes well, and it will return 1 if a
4175 * full transaction commit is required.
4177 int btrfs_log_new_name(struct btrfs_trans_handle
*trans
,
4178 struct inode
*inode
, struct inode
*old_dir
,
4179 struct dentry
*parent
)
4181 struct btrfs_root
* root
= BTRFS_I(inode
)->root
;
4184 * this will force the logging code to walk the dentry chain
4187 if (S_ISREG(inode
->i_mode
))
4188 BTRFS_I(inode
)->last_unlink_trans
= trans
->transid
;
4191 * if this inode hasn't been logged and directory we're renaming it
4192 * from hasn't been logged, we don't need to log it
4194 if (BTRFS_I(inode
)->logged_trans
<=
4195 root
->fs_info
->last_trans_committed
&&
4196 (!old_dir
|| BTRFS_I(old_dir
)->logged_trans
<=
4197 root
->fs_info
->last_trans_committed
))
4200 return btrfs_log_inode_parent(trans
, root
, inode
, parent
, 1);