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>
21 #include "transaction.h"
24 #include "print-tree.h"
28 /* magic values for the inode_only field in btrfs_log_inode:
30 * LOG_INODE_ALL means to log everything
31 * LOG_INODE_EXISTS means to log just enough to recreate the inode
34 #define LOG_INODE_ALL 0
35 #define LOG_INODE_EXISTS 1
38 * stages for the tree walking. The first
39 * stage (0) is to only pin down the blocks we find
40 * the second stage (1) is to make sure that all the inodes
41 * we find in the log are created in the subvolume.
43 * The last stage is to deal with directories and links and extents
44 * and all the other fun semantics
46 #define LOG_WALK_PIN_ONLY 0
47 #define LOG_WALK_REPLAY_INODES 1
48 #define LOG_WALK_REPLAY_ALL 2
50 static int __btrfs_log_inode(struct btrfs_trans_handle
*trans
,
51 struct btrfs_root
*root
, struct inode
*inode
,
53 static int link_to_fixup_dir(struct btrfs_trans_handle
*trans
,
54 struct btrfs_root
*root
,
55 struct btrfs_path
*path
, u64 objectid
);
58 * tree logging is a special write ahead log used to make sure that
59 * fsyncs and O_SYNCs can happen without doing full tree commits.
61 * Full tree commits are expensive because they require commonly
62 * modified blocks to be recowed, creating many dirty pages in the
63 * extent tree an 4x-6x higher write load than ext3.
65 * Instead of doing a tree commit on every fsync, we use the
66 * key ranges and transaction ids to find items for a given file or directory
67 * that have changed in this transaction. Those items are copied into
68 * a special tree (one per subvolume root), that tree is written to disk
69 * and then the fsync is considered complete.
71 * After a crash, items are copied out of the log-tree back into the
72 * subvolume tree. Any file data extents found are recorded in the extent
73 * allocation tree, and the log-tree freed.
75 * The log tree is read three times, once to pin down all the extents it is
76 * using in ram and once, once to create all the inodes logged in the tree
77 * and once to do all the other items.
81 * btrfs_add_log_tree adds a new per-subvolume log tree into the
82 * tree of log tree roots. This must be called with a tree log transaction
83 * running (see start_log_trans).
85 static int btrfs_add_log_tree(struct btrfs_trans_handle
*trans
,
86 struct btrfs_root
*root
)
89 struct btrfs_root_item root_item
;
90 struct btrfs_inode_item
*inode_item
;
91 struct extent_buffer
*leaf
;
92 struct btrfs_root
*new_root
= root
;
94 u64 objectid
= root
->root_key
.objectid
;
96 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
, 0,
97 BTRFS_TREE_LOG_OBJECTID
,
98 trans
->transid
, 0, 0, 0);
104 btrfs_set_header_nritems(leaf
, 0);
105 btrfs_set_header_level(leaf
, 0);
106 btrfs_set_header_bytenr(leaf
, leaf
->start
);
107 btrfs_set_header_generation(leaf
, trans
->transid
);
108 btrfs_set_header_owner(leaf
, BTRFS_TREE_LOG_OBJECTID
);
110 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
111 (unsigned long)btrfs_header_fsid(leaf
),
113 btrfs_mark_buffer_dirty(leaf
);
115 inode_item
= &root_item
.inode
;
116 memset(inode_item
, 0, sizeof(*inode_item
));
117 inode_item
->generation
= cpu_to_le64(1);
118 inode_item
->size
= cpu_to_le64(3);
119 inode_item
->nlink
= cpu_to_le32(1);
120 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
121 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
123 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
124 btrfs_set_root_generation(&root_item
, trans
->transid
);
125 btrfs_set_root_level(&root_item
, 0);
126 btrfs_set_root_refs(&root_item
, 0);
127 btrfs_set_root_used(&root_item
, 0);
129 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
130 root_item
.drop_level
= 0;
132 btrfs_tree_unlock(leaf
);
133 free_extent_buffer(leaf
);
136 btrfs_set_root_dirid(&root_item
, 0);
138 key
.objectid
= BTRFS_TREE_LOG_OBJECTID
;
139 key
.offset
= objectid
;
140 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
141 ret
= btrfs_insert_root(trans
, root
->fs_info
->log_root_tree
, &key
,
146 new_root
= btrfs_read_fs_root_no_radix(root
->fs_info
->log_root_tree
,
150 WARN_ON(root
->log_root
);
151 root
->log_root
= new_root
;
154 * log trees do not get reference counted because they go away
155 * before a real commit is actually done. They do store pointers
156 * to file data extents, and those reference counts still get
157 * updated (along with back refs to the log tree).
159 new_root
->ref_cows
= 0;
160 new_root
->last_trans
= trans
->transid
;
163 * we need to make sure the root block for this new tree
164 * is marked as dirty in the dirty_log_pages tree. This
165 * is how it gets flushed down to disk at tree log commit time.
167 * the tree logging mutex keeps others from coming in and changing
168 * the new_root->node, so we can safely access it here
170 set_extent_dirty(&new_root
->dirty_log_pages
, new_root
->node
->start
,
171 new_root
->node
->start
+ new_root
->node
->len
- 1,
179 * start a sub transaction and setup the log tree
180 * this increments the log tree writer count to make the people
181 * syncing the tree wait for us to finish
183 static int start_log_trans(struct btrfs_trans_handle
*trans
,
184 struct btrfs_root
*root
)
187 mutex_lock(&root
->fs_info
->tree_log_mutex
);
188 if (!root
->fs_info
->log_root_tree
) {
189 ret
= btrfs_init_log_root_tree(trans
, root
->fs_info
);
192 if (!root
->log_root
) {
193 ret
= btrfs_add_log_tree(trans
, root
);
196 atomic_inc(&root
->fs_info
->tree_log_writers
);
197 root
->fs_info
->tree_log_batch
++;
198 mutex_unlock(&root
->fs_info
->tree_log_mutex
);
203 * returns 0 if there was a log transaction running and we were able
204 * to join, or returns -ENOENT if there were not transactions
207 static int join_running_log_trans(struct btrfs_root
*root
)
215 mutex_lock(&root
->fs_info
->tree_log_mutex
);
216 if (root
->log_root
) {
218 atomic_inc(&root
->fs_info
->tree_log_writers
);
219 root
->fs_info
->tree_log_batch
++;
221 mutex_unlock(&root
->fs_info
->tree_log_mutex
);
226 * indicate we're done making changes to the log tree
227 * and wake up anyone waiting to do a sync
229 static int end_log_trans(struct btrfs_root
*root
)
231 atomic_dec(&root
->fs_info
->tree_log_writers
);
233 if (waitqueue_active(&root
->fs_info
->tree_log_wait
))
234 wake_up(&root
->fs_info
->tree_log_wait
);
240 * the walk control struct is used to pass state down the chain when
241 * processing the log tree. The stage field tells us which part
242 * of the log tree processing we are currently doing. The others
243 * are state fields used for that specific part
245 struct walk_control
{
246 /* should we free the extent on disk when done? This is used
247 * at transaction commit time while freeing a log tree
251 /* should we write out the extent buffer? This is used
252 * while flushing the log tree to disk during a sync
256 /* should we wait for the extent buffer io to finish? Also used
257 * while flushing the log tree to disk for a sync
261 /* pin only walk, we record which extents on disk belong to the
266 /* what stage of the replay code we're currently in */
269 /* the root we are currently replaying */
270 struct btrfs_root
*replay_dest
;
272 /* the trans handle for the current replay */
273 struct btrfs_trans_handle
*trans
;
275 /* the function that gets used to process blocks we find in the
276 * tree. Note the extent_buffer might not be up to date when it is
277 * passed in, and it must be checked or read if you need the data
280 int (*process_func
)(struct btrfs_root
*log
, struct extent_buffer
*eb
,
281 struct walk_control
*wc
, u64 gen
);
285 * process_func used to pin down extents, write them or wait on them
287 static int process_one_buffer(struct btrfs_root
*log
,
288 struct extent_buffer
*eb
,
289 struct walk_control
*wc
, u64 gen
)
292 mutex_lock(&log
->fs_info
->pinned_mutex
);
293 btrfs_update_pinned_extents(log
->fs_info
->extent_root
,
294 eb
->start
, eb
->len
, 1);
295 mutex_unlock(&log
->fs_info
->pinned_mutex
);
298 if (btrfs_buffer_uptodate(eb
, gen
)) {
300 btrfs_write_tree_block(eb
);
302 btrfs_wait_tree_block_writeback(eb
);
308 * Item overwrite used by replay and tree logging. eb, slot and key all refer
309 * to the src data we are copying out.
311 * root is the tree we are copying into, and path is a scratch
312 * path for use in this function (it should be released on entry and
313 * will be released on exit).
315 * If the key is already in the destination tree the existing item is
316 * overwritten. If the existing item isn't big enough, it is extended.
317 * If it is too large, it is truncated.
319 * If the key isn't in the destination yet, a new item is inserted.
321 static noinline
int overwrite_item(struct btrfs_trans_handle
*trans
,
322 struct btrfs_root
*root
,
323 struct btrfs_path
*path
,
324 struct extent_buffer
*eb
, int slot
,
325 struct btrfs_key
*key
)
329 u64 saved_i_size
= 0;
330 int save_old_i_size
= 0;
331 unsigned long src_ptr
;
332 unsigned long dst_ptr
;
333 int overwrite_root
= 0;
335 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
338 item_size
= btrfs_item_size_nr(eb
, slot
);
339 src_ptr
= btrfs_item_ptr_offset(eb
, slot
);
341 /* look for the key in the destination tree */
342 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
346 u32 dst_size
= btrfs_item_size_nr(path
->nodes
[0],
348 if (dst_size
!= item_size
)
351 if (item_size
== 0) {
352 btrfs_release_path(root
, path
);
355 dst_copy
= kmalloc(item_size
, GFP_NOFS
);
356 src_copy
= kmalloc(item_size
, GFP_NOFS
);
358 read_extent_buffer(eb
, src_copy
, src_ptr
, item_size
);
360 dst_ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
361 read_extent_buffer(path
->nodes
[0], dst_copy
, dst_ptr
,
363 ret
= memcmp(dst_copy
, src_copy
, item_size
);
368 * they have the same contents, just return, this saves
369 * us from cowing blocks in the destination tree and doing
370 * extra writes that may not have been done by a previous
374 btrfs_release_path(root
, path
);
380 btrfs_release_path(root
, path
);
381 /* try to insert the key into the destination tree */
382 ret
= btrfs_insert_empty_item(trans
, root
, path
,
385 /* make sure any existing item is the correct size */
386 if (ret
== -EEXIST
) {
388 found_size
= btrfs_item_size_nr(path
->nodes
[0],
390 if (found_size
> item_size
) {
391 btrfs_truncate_item(trans
, root
, path
, item_size
, 1);
392 } else if (found_size
< item_size
) {
393 ret
= btrfs_extend_item(trans
, root
, path
,
394 item_size
- found_size
);
400 dst_ptr
= btrfs_item_ptr_offset(path
->nodes
[0],
403 /* don't overwrite an existing inode if the generation number
404 * was logged as zero. This is done when the tree logging code
405 * is just logging an inode to make sure it exists after recovery.
407 * Also, don't overwrite i_size on directories during replay.
408 * log replay inserts and removes directory items based on the
409 * state of the tree found in the subvolume, and i_size is modified
412 if (key
->type
== BTRFS_INODE_ITEM_KEY
&& ret
== -EEXIST
) {
413 struct btrfs_inode_item
*src_item
;
414 struct btrfs_inode_item
*dst_item
;
416 src_item
= (struct btrfs_inode_item
*)src_ptr
;
417 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
419 if (btrfs_inode_generation(eb
, src_item
) == 0)
422 if (overwrite_root
&&
423 S_ISDIR(btrfs_inode_mode(eb
, src_item
)) &&
424 S_ISDIR(btrfs_inode_mode(path
->nodes
[0], dst_item
))) {
426 saved_i_size
= btrfs_inode_size(path
->nodes
[0],
431 copy_extent_buffer(path
->nodes
[0], eb
, dst_ptr
,
434 if (save_old_i_size
) {
435 struct btrfs_inode_item
*dst_item
;
436 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
437 btrfs_set_inode_size(path
->nodes
[0], dst_item
, saved_i_size
);
440 /* make sure the generation is filled in */
441 if (key
->type
== BTRFS_INODE_ITEM_KEY
) {
442 struct btrfs_inode_item
*dst_item
;
443 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
444 if (btrfs_inode_generation(path
->nodes
[0], dst_item
) == 0) {
445 btrfs_set_inode_generation(path
->nodes
[0], dst_item
,
450 btrfs_mark_buffer_dirty(path
->nodes
[0]);
451 btrfs_release_path(root
, path
);
456 * simple helper to read an inode off the disk from a given root
457 * This can only be called for subvolume roots and not for the log
459 static noinline
struct inode
*read_one_inode(struct btrfs_root
*root
,
463 inode
= btrfs_iget_locked(root
->fs_info
->sb
, objectid
, root
);
464 if (inode
->i_state
& I_NEW
) {
465 BTRFS_I(inode
)->root
= root
;
466 BTRFS_I(inode
)->location
.objectid
= objectid
;
467 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
468 BTRFS_I(inode
)->location
.offset
= 0;
469 btrfs_read_locked_inode(inode
);
470 unlock_new_inode(inode
);
473 if (is_bad_inode(inode
)) {
480 /* replays a single extent in 'eb' at 'slot' with 'key' into the
481 * subvolume 'root'. path is released on entry and should be released
484 * extents in the log tree have not been allocated out of the extent
485 * tree yet. So, this completes the allocation, taking a reference
486 * as required if the extent already exists or creating a new extent
487 * if it isn't in the extent allocation tree yet.
489 * The extent is inserted into the file, dropping any existing extents
490 * from the file that overlap the new one.
492 static noinline
int replay_one_extent(struct btrfs_trans_handle
*trans
,
493 struct btrfs_root
*root
,
494 struct btrfs_path
*path
,
495 struct extent_buffer
*eb
, int slot
,
496 struct btrfs_key
*key
)
499 u64 mask
= root
->sectorsize
- 1;
502 u64 start
= key
->offset
;
504 struct btrfs_file_extent_item
*item
;
505 struct inode
*inode
= NULL
;
509 item
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
510 found_type
= btrfs_file_extent_type(eb
, item
);
512 if (found_type
== BTRFS_FILE_EXTENT_REG
||
513 found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
514 extent_end
= start
+ btrfs_file_extent_num_bytes(eb
, item
);
515 else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
516 size
= btrfs_file_extent_inline_len(eb
, item
);
517 extent_end
= (start
+ size
+ mask
) & ~mask
;
523 inode
= read_one_inode(root
, key
->objectid
);
530 * first check to see if we already have this extent in the
531 * file. This must be done before the btrfs_drop_extents run
532 * so we don't try to drop this extent.
534 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
538 (found_type
== BTRFS_FILE_EXTENT_REG
||
539 found_type
== BTRFS_FILE_EXTENT_PREALLOC
)) {
540 struct btrfs_file_extent_item cmp1
;
541 struct btrfs_file_extent_item cmp2
;
542 struct btrfs_file_extent_item
*existing
;
543 struct extent_buffer
*leaf
;
545 leaf
= path
->nodes
[0];
546 existing
= btrfs_item_ptr(leaf
, path
->slots
[0],
547 struct btrfs_file_extent_item
);
549 read_extent_buffer(eb
, &cmp1
, (unsigned long)item
,
551 read_extent_buffer(leaf
, &cmp2
, (unsigned long)existing
,
555 * we already have a pointer to this exact extent,
556 * we don't have to do anything
558 if (memcmp(&cmp1
, &cmp2
, sizeof(cmp1
)) == 0) {
559 btrfs_release_path(root
, path
);
563 btrfs_release_path(root
, path
);
565 saved_nbytes
= inode_get_bytes(inode
);
566 /* drop any overlapping extents */
567 ret
= btrfs_drop_extents(trans
, root
, inode
,
568 start
, extent_end
, start
, &alloc_hint
);
571 if (found_type
== BTRFS_FILE_EXTENT_REG
||
572 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
573 unsigned long dest_offset
;
574 struct btrfs_key ins
;
576 ret
= btrfs_insert_empty_item(trans
, root
, path
, key
,
579 dest_offset
= btrfs_item_ptr_offset(path
->nodes
[0],
581 copy_extent_buffer(path
->nodes
[0], eb
, dest_offset
,
582 (unsigned long)item
, sizeof(*item
));
584 ins
.objectid
= btrfs_file_extent_disk_bytenr(eb
, item
);
585 ins
.offset
= btrfs_file_extent_disk_num_bytes(eb
, item
);
586 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
588 if (ins
.objectid
> 0) {
591 LIST_HEAD(ordered_sums
);
593 * is this extent already allocated in the extent
594 * allocation tree? If so, just add a reference
596 ret
= btrfs_lookup_extent(root
, ins
.objectid
,
599 ret
= btrfs_inc_extent_ref(trans
, root
,
600 ins
.objectid
, ins
.offset
,
601 path
->nodes
[0]->start
,
602 root
->root_key
.objectid
,
603 trans
->transid
, key
->objectid
);
606 * insert the extent pointer in the extent
609 ret
= btrfs_alloc_logged_extent(trans
, root
,
610 path
->nodes
[0]->start
,
611 root
->root_key
.objectid
,
612 trans
->transid
, key
->objectid
,
616 btrfs_release_path(root
, path
);
618 if (btrfs_file_extent_compression(eb
, item
)) {
619 csum_start
= ins
.objectid
;
620 csum_end
= csum_start
+ ins
.offset
;
622 csum_start
= ins
.objectid
+
623 btrfs_file_extent_offset(eb
, item
);
624 csum_end
= csum_start
+
625 btrfs_file_extent_num_bytes(eb
, item
);
628 ret
= btrfs_lookup_csums_range(root
->log_root
,
629 csum_start
, csum_end
- 1,
632 while (!list_empty(&ordered_sums
)) {
633 struct btrfs_ordered_sum
*sums
;
634 sums
= list_entry(ordered_sums
.next
,
635 struct btrfs_ordered_sum
,
637 ret
= btrfs_csum_file_blocks(trans
,
638 root
->fs_info
->csum_root
,
641 list_del(&sums
->list
);
645 btrfs_release_path(root
, path
);
647 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
648 /* inline extents are easy, we just overwrite them */
649 ret
= overwrite_item(trans
, root
, path
, eb
, slot
, key
);
653 inode_set_bytes(inode
, saved_nbytes
);
654 btrfs_update_inode(trans
, root
, inode
);
662 * when cleaning up conflicts between the directory names in the
663 * subvolume, directory names in the log and directory names in the
664 * inode back references, we may have to unlink inodes from directories.
666 * This is a helper function to do the unlink of a specific directory
669 static noinline
int drop_one_dir_item(struct btrfs_trans_handle
*trans
,
670 struct btrfs_root
*root
,
671 struct btrfs_path
*path
,
673 struct btrfs_dir_item
*di
)
678 struct extent_buffer
*leaf
;
679 struct btrfs_key location
;
682 leaf
= path
->nodes
[0];
684 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
685 name_len
= btrfs_dir_name_len(leaf
, di
);
686 name
= kmalloc(name_len
, GFP_NOFS
);
687 read_extent_buffer(leaf
, name
, (unsigned long)(di
+ 1), name_len
);
688 btrfs_release_path(root
, path
);
690 inode
= read_one_inode(root
, location
.objectid
);
693 ret
= link_to_fixup_dir(trans
, root
, path
, location
.objectid
);
695 ret
= btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
704 * helper function to see if a given name and sequence number found
705 * in an inode back reference are already in a directory and correctly
706 * point to this inode
708 static noinline
int inode_in_dir(struct btrfs_root
*root
,
709 struct btrfs_path
*path
,
710 u64 dirid
, u64 objectid
, u64 index
,
711 const char *name
, int name_len
)
713 struct btrfs_dir_item
*di
;
714 struct btrfs_key location
;
717 di
= btrfs_lookup_dir_index_item(NULL
, root
, path
, dirid
,
718 index
, name
, name_len
, 0);
719 if (di
&& !IS_ERR(di
)) {
720 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
721 if (location
.objectid
!= objectid
)
725 btrfs_release_path(root
, path
);
727 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dirid
, name
, name_len
, 0);
728 if (di
&& !IS_ERR(di
)) {
729 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
730 if (location
.objectid
!= objectid
)
736 btrfs_release_path(root
, path
);
741 * helper function to check a log tree for a named back reference in
742 * an inode. This is used to decide if a back reference that is
743 * found in the subvolume conflicts with what we find in the log.
745 * inode backreferences may have multiple refs in a single item,
746 * during replay we process one reference at a time, and we don't
747 * want to delete valid links to a file from the subvolume if that
748 * link is also in the log.
750 static noinline
int backref_in_log(struct btrfs_root
*log
,
751 struct btrfs_key
*key
,
752 char *name
, int namelen
)
754 struct btrfs_path
*path
;
755 struct btrfs_inode_ref
*ref
;
757 unsigned long ptr_end
;
758 unsigned long name_ptr
;
764 path
= btrfs_alloc_path();
765 ret
= btrfs_search_slot(NULL
, log
, key
, path
, 0, 0);
769 item_size
= btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]);
770 ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
771 ptr_end
= ptr
+ item_size
;
772 while (ptr
< ptr_end
) {
773 ref
= (struct btrfs_inode_ref
*)ptr
;
774 found_name_len
= btrfs_inode_ref_name_len(path
->nodes
[0], ref
);
775 if (found_name_len
== namelen
) {
776 name_ptr
= (unsigned long)(ref
+ 1);
777 ret
= memcmp_extent_buffer(path
->nodes
[0], name
,
784 ptr
= (unsigned long)(ref
+ 1) + found_name_len
;
787 btrfs_free_path(path
);
793 * replay one inode back reference item found in the log tree.
794 * eb, slot and key refer to the buffer and key found in the log tree.
795 * root is the destination we are replaying into, and path is for temp
796 * use by this function. (it should be released on return).
798 static noinline
int add_inode_ref(struct btrfs_trans_handle
*trans
,
799 struct btrfs_root
*root
,
800 struct btrfs_root
*log
,
801 struct btrfs_path
*path
,
802 struct extent_buffer
*eb
, int slot
,
803 struct btrfs_key
*key
)
807 struct btrfs_key location
;
808 struct btrfs_inode_ref
*ref
;
809 struct btrfs_dir_item
*di
;
813 unsigned long ref_ptr
;
814 unsigned long ref_end
;
816 location
.objectid
= key
->objectid
;
817 location
.type
= BTRFS_INODE_ITEM_KEY
;
821 * it is possible that we didn't log all the parent directories
822 * for a given inode. If we don't find the dir, just don't
823 * copy the back ref in. The link count fixup code will take
826 dir
= read_one_inode(root
, key
->offset
);
830 inode
= read_one_inode(root
, key
->objectid
);
833 ref_ptr
= btrfs_item_ptr_offset(eb
, slot
);
834 ref_end
= ref_ptr
+ btrfs_item_size_nr(eb
, slot
);
837 ref
= (struct btrfs_inode_ref
*)ref_ptr
;
839 namelen
= btrfs_inode_ref_name_len(eb
, ref
);
840 name
= kmalloc(namelen
, GFP_NOFS
);
843 read_extent_buffer(eb
, name
, (unsigned long)(ref
+ 1), namelen
);
845 /* if we already have a perfect match, we're done */
846 if (inode_in_dir(root
, path
, dir
->i_ino
, inode
->i_ino
,
847 btrfs_inode_ref_index(eb
, ref
),
853 * look for a conflicting back reference in the metadata.
854 * if we find one we have to unlink that name of the file
855 * before we add our new link. Later on, we overwrite any
856 * existing back reference, and we don't want to create
857 * dangling pointers in the directory.
860 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
864 struct btrfs_inode_ref
*victim_ref
;
866 unsigned long ptr_end
;
867 struct extent_buffer
*leaf
= path
->nodes
[0];
869 /* are we trying to overwrite a back ref for the root directory
870 * if so, just jump out, we're done
872 if (key
->objectid
== key
->offset
)
875 /* check all the names in this back reference to see
876 * if they are in the log. if so, we allow them to stay
877 * otherwise they must be unlinked as a conflict
879 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
880 ptr_end
= ptr
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
881 while (ptr
< ptr_end
) {
882 victim_ref
= (struct btrfs_inode_ref
*)ptr
;
883 victim_name_len
= btrfs_inode_ref_name_len(leaf
,
885 victim_name
= kmalloc(victim_name_len
, GFP_NOFS
);
886 BUG_ON(!victim_name
);
888 read_extent_buffer(leaf
, victim_name
,
889 (unsigned long)(victim_ref
+ 1),
892 if (!backref_in_log(log
, key
, victim_name
,
894 btrfs_inc_nlink(inode
);
895 btrfs_release_path(root
, path
);
896 ret
= btrfs_unlink_inode(trans
, root
, dir
,
900 btrfs_release_path(root
, path
);
904 ptr
= (unsigned long)(victim_ref
+ 1) + victim_name_len
;
908 btrfs_release_path(root
, path
);
910 /* look for a conflicting sequence number */
911 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
912 btrfs_inode_ref_index(eb
, ref
),
914 if (di
&& !IS_ERR(di
)) {
915 ret
= drop_one_dir_item(trans
, root
, path
, dir
, di
);
918 btrfs_release_path(root
, path
);
921 /* look for a conflicting name */
922 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
924 if (di
&& !IS_ERR(di
)) {
925 ret
= drop_one_dir_item(trans
, root
, path
, dir
, di
);
928 btrfs_release_path(root
, path
);
930 /* insert our name */
931 ret
= btrfs_add_link(trans
, dir
, inode
, name
, namelen
, 0,
932 btrfs_inode_ref_index(eb
, ref
));
935 btrfs_update_inode(trans
, root
, inode
);
938 ref_ptr
= (unsigned long)(ref
+ 1) + namelen
;
940 if (ref_ptr
< ref_end
)
943 /* finally write the back reference in the inode */
944 ret
= overwrite_item(trans
, root
, path
, eb
, slot
, key
);
948 btrfs_release_path(root
, path
);
955 * There are a few corners where the link count of the file can't
956 * be properly maintained during replay. So, instead of adding
957 * lots of complexity to the log code, we just scan the backrefs
958 * for any file that has been through replay.
960 * The scan will update the link count on the inode to reflect the
961 * number of back refs found. If it goes down to zero, the iput
962 * will free the inode.
964 static noinline
int fixup_inode_link_count(struct btrfs_trans_handle
*trans
,
965 struct btrfs_root
*root
,
968 struct btrfs_path
*path
;
970 struct btrfs_key key
;
973 unsigned long ptr_end
;
976 key
.objectid
= inode
->i_ino
;
977 key
.type
= BTRFS_INODE_REF_KEY
;
978 key
.offset
= (u64
)-1;
980 path
= btrfs_alloc_path();
983 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
987 if (path
->slots
[0] == 0)
991 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
993 if (key
.objectid
!= inode
->i_ino
||
994 key
.type
!= BTRFS_INODE_REF_KEY
)
996 ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
997 ptr_end
= ptr
+ btrfs_item_size_nr(path
->nodes
[0],
999 while (ptr
< ptr_end
) {
1000 struct btrfs_inode_ref
*ref
;
1002 ref
= (struct btrfs_inode_ref
*)ptr
;
1003 name_len
= btrfs_inode_ref_name_len(path
->nodes
[0],
1005 ptr
= (unsigned long)(ref
+ 1) + name_len
;
1009 if (key
.offset
== 0)
1012 btrfs_release_path(root
, path
);
1014 btrfs_free_path(path
);
1015 if (nlink
!= inode
->i_nlink
) {
1016 inode
->i_nlink
= nlink
;
1017 btrfs_update_inode(trans
, root
, inode
);
1019 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1024 static noinline
int fixup_inode_link_counts(struct btrfs_trans_handle
*trans
,
1025 struct btrfs_root
*root
,
1026 struct btrfs_path
*path
)
1029 struct btrfs_key key
;
1030 struct inode
*inode
;
1032 key
.objectid
= BTRFS_TREE_LOG_FIXUP_OBJECTID
;
1033 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
1034 key
.offset
= (u64
)-1;
1036 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1041 if (path
->slots
[0] == 0)
1046 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1047 if (key
.objectid
!= BTRFS_TREE_LOG_FIXUP_OBJECTID
||
1048 key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
1051 ret
= btrfs_del_item(trans
, root
, path
);
1054 btrfs_release_path(root
, path
);
1055 inode
= read_one_inode(root
, key
.offset
);
1058 ret
= fixup_inode_link_count(trans
, root
, inode
);
1063 if (key
.offset
== 0)
1067 btrfs_release_path(root
, path
);
1073 * record a given inode in the fixup dir so we can check its link
1074 * count when replay is done. The link count is incremented here
1075 * so the inode won't go away until we check it
1077 static noinline
int link_to_fixup_dir(struct btrfs_trans_handle
*trans
,
1078 struct btrfs_root
*root
,
1079 struct btrfs_path
*path
,
1082 struct btrfs_key key
;
1084 struct inode
*inode
;
1086 inode
= read_one_inode(root
, objectid
);
1089 key
.objectid
= BTRFS_TREE_LOG_FIXUP_OBJECTID
;
1090 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
1091 key
.offset
= objectid
;
1093 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1095 btrfs_release_path(root
, path
);
1097 btrfs_inc_nlink(inode
);
1098 btrfs_update_inode(trans
, root
, inode
);
1099 } else if (ret
== -EEXIST
) {
1110 * when replaying the log for a directory, we only insert names
1111 * for inodes that actually exist. This means an fsync on a directory
1112 * does not implicitly fsync all the new files in it
1114 static noinline
int insert_one_name(struct btrfs_trans_handle
*trans
,
1115 struct btrfs_root
*root
,
1116 struct btrfs_path
*path
,
1117 u64 dirid
, u64 index
,
1118 char *name
, int name_len
, u8 type
,
1119 struct btrfs_key
*location
)
1121 struct inode
*inode
;
1125 inode
= read_one_inode(root
, location
->objectid
);
1129 dir
= read_one_inode(root
, dirid
);
1134 ret
= btrfs_add_link(trans
, dir
, inode
, name
, name_len
, 1, index
);
1136 /* FIXME, put inode into FIXUP list */
1144 * take a single entry in a log directory item and replay it into
1147 * if a conflicting item exists in the subdirectory already,
1148 * the inode it points to is unlinked and put into the link count
1151 * If a name from the log points to a file or directory that does
1152 * not exist in the FS, it is skipped. fsyncs on directories
1153 * do not force down inodes inside that directory, just changes to the
1154 * names or unlinks in a directory.
1156 static noinline
int replay_one_name(struct btrfs_trans_handle
*trans
,
1157 struct btrfs_root
*root
,
1158 struct btrfs_path
*path
,
1159 struct extent_buffer
*eb
,
1160 struct btrfs_dir_item
*di
,
1161 struct btrfs_key
*key
)
1165 struct btrfs_dir_item
*dst_di
;
1166 struct btrfs_key found_key
;
1167 struct btrfs_key log_key
;
1173 dir
= read_one_inode(root
, key
->objectid
);
1176 name_len
= btrfs_dir_name_len(eb
, di
);
1177 name
= kmalloc(name_len
, GFP_NOFS
);
1178 log_type
= btrfs_dir_type(eb
, di
);
1179 read_extent_buffer(eb
, name
, (unsigned long)(di
+ 1),
1182 btrfs_dir_item_key_to_cpu(eb
, di
, &log_key
);
1183 exists
= btrfs_lookup_inode(trans
, root
, path
, &log_key
, 0);
1188 btrfs_release_path(root
, path
);
1190 if (key
->type
== BTRFS_DIR_ITEM_KEY
) {
1191 dst_di
= btrfs_lookup_dir_item(trans
, root
, path
, key
->objectid
,
1193 } else if (key
->type
== BTRFS_DIR_INDEX_KEY
) {
1194 dst_di
= btrfs_lookup_dir_index_item(trans
, root
, path
,
1201 if (!dst_di
|| IS_ERR(dst_di
)) {
1202 /* we need a sequence number to insert, so we only
1203 * do inserts for the BTRFS_DIR_INDEX_KEY types
1205 if (key
->type
!= BTRFS_DIR_INDEX_KEY
)
1210 btrfs_dir_item_key_to_cpu(path
->nodes
[0], dst_di
, &found_key
);
1211 /* the existing item matches the logged item */
1212 if (found_key
.objectid
== log_key
.objectid
&&
1213 found_key
.type
== log_key
.type
&&
1214 found_key
.offset
== log_key
.offset
&&
1215 btrfs_dir_type(path
->nodes
[0], dst_di
) == log_type
) {
1220 * don't drop the conflicting directory entry if the inode
1221 * for the new entry doesn't exist
1226 ret
= drop_one_dir_item(trans
, root
, path
, dir
, dst_di
);
1229 if (key
->type
== BTRFS_DIR_INDEX_KEY
)
1232 btrfs_release_path(root
, path
);
1238 btrfs_release_path(root
, path
);
1239 ret
= insert_one_name(trans
, root
, path
, key
->objectid
, key
->offset
,
1240 name
, name_len
, log_type
, &log_key
);
1242 if (ret
&& ret
!= -ENOENT
)
1248 * find all the names in a directory item and reconcile them into
1249 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1250 * one name in a directory item, but the same code gets used for
1251 * both directory index types
1253 static noinline
int replay_one_dir_item(struct btrfs_trans_handle
*trans
,
1254 struct btrfs_root
*root
,
1255 struct btrfs_path
*path
,
1256 struct extent_buffer
*eb
, int slot
,
1257 struct btrfs_key
*key
)
1260 u32 item_size
= btrfs_item_size_nr(eb
, slot
);
1261 struct btrfs_dir_item
*di
;
1264 unsigned long ptr_end
;
1266 ptr
= btrfs_item_ptr_offset(eb
, slot
);
1267 ptr_end
= ptr
+ item_size
;
1268 while (ptr
< ptr_end
) {
1269 di
= (struct btrfs_dir_item
*)ptr
;
1270 name_len
= btrfs_dir_name_len(eb
, di
);
1271 ret
= replay_one_name(trans
, root
, path
, eb
, di
, key
);
1273 ptr
= (unsigned long)(di
+ 1);
1280 * directory replay has two parts. There are the standard directory
1281 * items in the log copied from the subvolume, and range items
1282 * created in the log while the subvolume was logged.
1284 * The range items tell us which parts of the key space the log
1285 * is authoritative for. During replay, if a key in the subvolume
1286 * directory is in a logged range item, but not actually in the log
1287 * that means it was deleted from the directory before the fsync
1288 * and should be removed.
1290 static noinline
int find_dir_range(struct btrfs_root
*root
,
1291 struct btrfs_path
*path
,
1292 u64 dirid
, int key_type
,
1293 u64
*start_ret
, u64
*end_ret
)
1295 struct btrfs_key key
;
1297 struct btrfs_dir_log_item
*item
;
1301 if (*start_ret
== (u64
)-1)
1304 key
.objectid
= dirid
;
1305 key
.type
= key_type
;
1306 key
.offset
= *start_ret
;
1308 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1312 if (path
->slots
[0] == 0)
1317 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1319 if (key
.type
!= key_type
|| key
.objectid
!= dirid
) {
1323 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1324 struct btrfs_dir_log_item
);
1325 found_end
= btrfs_dir_log_end(path
->nodes
[0], item
);
1327 if (*start_ret
>= key
.offset
&& *start_ret
<= found_end
) {
1329 *start_ret
= key
.offset
;
1330 *end_ret
= found_end
;
1335 /* check the next slot in the tree to see if it is a valid item */
1336 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1337 if (path
->slots
[0] >= nritems
) {
1338 ret
= btrfs_next_leaf(root
, path
);
1345 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1347 if (key
.type
!= key_type
|| key
.objectid
!= dirid
) {
1351 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1352 struct btrfs_dir_log_item
);
1353 found_end
= btrfs_dir_log_end(path
->nodes
[0], item
);
1354 *start_ret
= key
.offset
;
1355 *end_ret
= found_end
;
1358 btrfs_release_path(root
, path
);
1363 * this looks for a given directory item in the log. If the directory
1364 * item is not in the log, the item is removed and the inode it points
1367 static noinline
int check_item_in_log(struct btrfs_trans_handle
*trans
,
1368 struct btrfs_root
*root
,
1369 struct btrfs_root
*log
,
1370 struct btrfs_path
*path
,
1371 struct btrfs_path
*log_path
,
1373 struct btrfs_key
*dir_key
)
1376 struct extent_buffer
*eb
;
1379 struct btrfs_dir_item
*di
;
1380 struct btrfs_dir_item
*log_di
;
1383 unsigned long ptr_end
;
1385 struct inode
*inode
;
1386 struct btrfs_key location
;
1389 eb
= path
->nodes
[0];
1390 slot
= path
->slots
[0];
1391 item_size
= btrfs_item_size_nr(eb
, slot
);
1392 ptr
= btrfs_item_ptr_offset(eb
, slot
);
1393 ptr_end
= ptr
+ item_size
;
1394 while (ptr
< ptr_end
) {
1395 di
= (struct btrfs_dir_item
*)ptr
;
1396 name_len
= btrfs_dir_name_len(eb
, di
);
1397 name
= kmalloc(name_len
, GFP_NOFS
);
1402 read_extent_buffer(eb
, name
, (unsigned long)(di
+ 1),
1405 if (dir_key
->type
== BTRFS_DIR_ITEM_KEY
) {
1406 log_di
= btrfs_lookup_dir_item(trans
, log
, log_path
,
1409 } else if (dir_key
->type
== BTRFS_DIR_INDEX_KEY
) {
1410 log_di
= btrfs_lookup_dir_index_item(trans
, log
,
1416 if (!log_di
|| IS_ERR(log_di
)) {
1417 btrfs_dir_item_key_to_cpu(eb
, di
, &location
);
1418 btrfs_release_path(root
, path
);
1419 btrfs_release_path(log
, log_path
);
1420 inode
= read_one_inode(root
, location
.objectid
);
1423 ret
= link_to_fixup_dir(trans
, root
,
1424 path
, location
.objectid
);
1426 btrfs_inc_nlink(inode
);
1427 ret
= btrfs_unlink_inode(trans
, root
, dir
, inode
,
1433 /* there might still be more names under this key
1434 * check and repeat if required
1436 ret
= btrfs_search_slot(NULL
, root
, dir_key
, path
,
1443 btrfs_release_path(log
, log_path
);
1446 ptr
= (unsigned long)(di
+ 1);
1451 btrfs_release_path(root
, path
);
1452 btrfs_release_path(log
, log_path
);
1457 * deletion replay happens before we copy any new directory items
1458 * out of the log or out of backreferences from inodes. It
1459 * scans the log to find ranges of keys that log is authoritative for,
1460 * and then scans the directory to find items in those ranges that are
1461 * not present in the log.
1463 * Anything we don't find in the log is unlinked and removed from the
1466 static noinline
int replay_dir_deletes(struct btrfs_trans_handle
*trans
,
1467 struct btrfs_root
*root
,
1468 struct btrfs_root
*log
,
1469 struct btrfs_path
*path
,
1474 int key_type
= BTRFS_DIR_LOG_ITEM_KEY
;
1476 struct btrfs_key dir_key
;
1477 struct btrfs_key found_key
;
1478 struct btrfs_path
*log_path
;
1481 dir_key
.objectid
= dirid
;
1482 dir_key
.type
= BTRFS_DIR_ITEM_KEY
;
1483 log_path
= btrfs_alloc_path();
1487 dir
= read_one_inode(root
, dirid
);
1488 /* it isn't an error if the inode isn't there, that can happen
1489 * because we replay the deletes before we copy in the inode item
1493 btrfs_free_path(log_path
);
1500 ret
= find_dir_range(log
, path
, dirid
, key_type
,
1501 &range_start
, &range_end
);
1505 dir_key
.offset
= range_start
;
1508 ret
= btrfs_search_slot(NULL
, root
, &dir_key
, path
,
1513 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1514 if (path
->slots
[0] >= nritems
) {
1515 ret
= btrfs_next_leaf(root
, path
);
1519 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1521 if (found_key
.objectid
!= dirid
||
1522 found_key
.type
!= dir_key
.type
)
1525 if (found_key
.offset
> range_end
)
1528 ret
= check_item_in_log(trans
, root
, log
, path
,
1529 log_path
, dir
, &found_key
);
1531 if (found_key
.offset
== (u64
)-1)
1533 dir_key
.offset
= found_key
.offset
+ 1;
1535 btrfs_release_path(root
, path
);
1536 if (range_end
== (u64
)-1)
1538 range_start
= range_end
+ 1;
1543 if (key_type
== BTRFS_DIR_LOG_ITEM_KEY
) {
1544 key_type
= BTRFS_DIR_LOG_INDEX_KEY
;
1545 dir_key
.type
= BTRFS_DIR_INDEX_KEY
;
1546 btrfs_release_path(root
, path
);
1550 btrfs_release_path(root
, path
);
1551 btrfs_free_path(log_path
);
1557 * the process_func used to replay items from the log tree. This
1558 * gets called in two different stages. The first stage just looks
1559 * for inodes and makes sure they are all copied into the subvolume.
1561 * The second stage copies all the other item types from the log into
1562 * the subvolume. The two stage approach is slower, but gets rid of
1563 * lots of complexity around inodes referencing other inodes that exist
1564 * only in the log (references come from either directory items or inode
1567 static int replay_one_buffer(struct btrfs_root
*log
, struct extent_buffer
*eb
,
1568 struct walk_control
*wc
, u64 gen
)
1571 struct btrfs_path
*path
;
1572 struct btrfs_root
*root
= wc
->replay_dest
;
1573 struct btrfs_key key
;
1579 btrfs_read_buffer(eb
, gen
);
1581 level
= btrfs_header_level(eb
);
1586 path
= btrfs_alloc_path();
1589 nritems
= btrfs_header_nritems(eb
);
1590 for (i
= 0; i
< nritems
; i
++) {
1591 btrfs_item_key_to_cpu(eb
, &key
, i
);
1592 item_size
= btrfs_item_size_nr(eb
, i
);
1594 /* inode keys are done during the first stage */
1595 if (key
.type
== BTRFS_INODE_ITEM_KEY
&&
1596 wc
->stage
== LOG_WALK_REPLAY_INODES
) {
1597 struct inode
*inode
;
1598 struct btrfs_inode_item
*inode_item
;
1601 inode_item
= btrfs_item_ptr(eb
, i
,
1602 struct btrfs_inode_item
);
1603 mode
= btrfs_inode_mode(eb
, inode_item
);
1604 if (S_ISDIR(mode
)) {
1605 ret
= replay_dir_deletes(wc
->trans
,
1606 root
, log
, path
, key
.objectid
);
1609 ret
= overwrite_item(wc
->trans
, root
, path
,
1613 /* for regular files, truncate away
1614 * extents past the new EOF
1616 if (S_ISREG(mode
)) {
1617 inode
= read_one_inode(root
,
1621 ret
= btrfs_truncate_inode_items(wc
->trans
,
1622 root
, inode
, inode
->i_size
,
1623 BTRFS_EXTENT_DATA_KEY
);
1627 ret
= link_to_fixup_dir(wc
->trans
, root
,
1628 path
, key
.objectid
);
1631 if (wc
->stage
< LOG_WALK_REPLAY_ALL
)
1634 /* these keys are simply copied */
1635 if (key
.type
== BTRFS_XATTR_ITEM_KEY
) {
1636 ret
= overwrite_item(wc
->trans
, root
, path
,
1639 } else if (key
.type
== BTRFS_INODE_REF_KEY
) {
1640 ret
= add_inode_ref(wc
->trans
, root
, log
, path
,
1642 BUG_ON(ret
&& ret
!= -ENOENT
);
1643 } else if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
1644 ret
= replay_one_extent(wc
->trans
, root
, path
,
1647 } else if (key
.type
== BTRFS_DIR_ITEM_KEY
||
1648 key
.type
== BTRFS_DIR_INDEX_KEY
) {
1649 ret
= replay_one_dir_item(wc
->trans
, root
, path
,
1654 btrfs_free_path(path
);
1658 static noinline
int walk_down_log_tree(struct btrfs_trans_handle
*trans
,
1659 struct btrfs_root
*root
,
1660 struct btrfs_path
*path
, int *level
,
1661 struct walk_control
*wc
)
1667 struct extent_buffer
*next
;
1668 struct extent_buffer
*cur
;
1669 struct extent_buffer
*parent
;
1673 WARN_ON(*level
< 0);
1674 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1676 while (*level
> 0) {
1677 WARN_ON(*level
< 0);
1678 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1679 cur
= path
->nodes
[*level
];
1681 if (btrfs_header_level(cur
) != *level
)
1684 if (path
->slots
[*level
] >=
1685 btrfs_header_nritems(cur
))
1688 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
1689 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
1690 blocksize
= btrfs_level_size(root
, *level
- 1);
1692 parent
= path
->nodes
[*level
];
1693 root_owner
= btrfs_header_owner(parent
);
1694 root_gen
= btrfs_header_generation(parent
);
1696 next
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
1698 wc
->process_func(root
, next
, wc
, ptr_gen
);
1701 path
->slots
[*level
]++;
1703 btrfs_read_buffer(next
, ptr_gen
);
1705 btrfs_tree_lock(next
);
1706 clean_tree_block(trans
, root
, next
);
1707 btrfs_wait_tree_block_writeback(next
);
1708 btrfs_tree_unlock(next
);
1710 ret
= btrfs_drop_leaf_ref(trans
, root
, next
);
1713 WARN_ON(root_owner
!=
1714 BTRFS_TREE_LOG_OBJECTID
);
1715 ret
= btrfs_free_reserved_extent(root
,
1719 free_extent_buffer(next
);
1722 btrfs_read_buffer(next
, ptr_gen
);
1724 WARN_ON(*level
<= 0);
1725 if (path
->nodes
[*level
-1])
1726 free_extent_buffer(path
->nodes
[*level
-1]);
1727 path
->nodes
[*level
-1] = next
;
1728 *level
= btrfs_header_level(next
);
1729 path
->slots
[*level
] = 0;
1732 WARN_ON(*level
< 0);
1733 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1735 if (path
->nodes
[*level
] == root
->node
)
1736 parent
= path
->nodes
[*level
];
1738 parent
= path
->nodes
[*level
+ 1];
1740 bytenr
= path
->nodes
[*level
]->start
;
1742 blocksize
= btrfs_level_size(root
, *level
);
1743 root_owner
= btrfs_header_owner(parent
);
1744 root_gen
= btrfs_header_generation(parent
);
1746 wc
->process_func(root
, path
->nodes
[*level
], wc
,
1747 btrfs_header_generation(path
->nodes
[*level
]));
1750 next
= path
->nodes
[*level
];
1751 btrfs_tree_lock(next
);
1752 clean_tree_block(trans
, root
, next
);
1753 btrfs_wait_tree_block_writeback(next
);
1754 btrfs_tree_unlock(next
);
1757 ret
= btrfs_drop_leaf_ref(trans
, root
, next
);
1760 WARN_ON(root_owner
!= BTRFS_TREE_LOG_OBJECTID
);
1761 ret
= btrfs_free_reserved_extent(root
, bytenr
, blocksize
);
1764 free_extent_buffer(path
->nodes
[*level
]);
1765 path
->nodes
[*level
] = NULL
;
1772 static noinline
int walk_up_log_tree(struct btrfs_trans_handle
*trans
,
1773 struct btrfs_root
*root
,
1774 struct btrfs_path
*path
, int *level
,
1775 struct walk_control
*wc
)
1783 for (i
= *level
; i
< BTRFS_MAX_LEVEL
- 1 && path
->nodes
[i
]; i
++) {
1784 slot
= path
->slots
[i
];
1785 if (slot
< btrfs_header_nritems(path
->nodes
[i
]) - 1) {
1786 struct extent_buffer
*node
;
1787 node
= path
->nodes
[i
];
1790 WARN_ON(*level
== 0);
1793 struct extent_buffer
*parent
;
1794 if (path
->nodes
[*level
] == root
->node
)
1795 parent
= path
->nodes
[*level
];
1797 parent
= path
->nodes
[*level
+ 1];
1799 root_owner
= btrfs_header_owner(parent
);
1800 root_gen
= btrfs_header_generation(parent
);
1801 wc
->process_func(root
, path
->nodes
[*level
], wc
,
1802 btrfs_header_generation(path
->nodes
[*level
]));
1804 struct extent_buffer
*next
;
1806 next
= path
->nodes
[*level
];
1808 btrfs_tree_lock(next
);
1809 clean_tree_block(trans
, root
, next
);
1810 btrfs_wait_tree_block_writeback(next
);
1811 btrfs_tree_unlock(next
);
1814 ret
= btrfs_drop_leaf_ref(trans
, root
,
1819 WARN_ON(root_owner
!= BTRFS_TREE_LOG_OBJECTID
);
1820 ret
= btrfs_free_reserved_extent(root
,
1821 path
->nodes
[*level
]->start
,
1822 path
->nodes
[*level
]->len
);
1825 free_extent_buffer(path
->nodes
[*level
]);
1826 path
->nodes
[*level
] = NULL
;
1834 * drop the reference count on the tree rooted at 'snap'. This traverses
1835 * the tree freeing any blocks that have a ref count of zero after being
1838 static int walk_log_tree(struct btrfs_trans_handle
*trans
,
1839 struct btrfs_root
*log
, struct walk_control
*wc
)
1844 struct btrfs_path
*path
;
1848 path
= btrfs_alloc_path();
1851 level
= btrfs_header_level(log
->node
);
1853 path
->nodes
[level
] = log
->node
;
1854 extent_buffer_get(log
->node
);
1855 path
->slots
[level
] = 0;
1858 wret
= walk_down_log_tree(trans
, log
, path
, &level
, wc
);
1864 wret
= walk_up_log_tree(trans
, log
, path
, &level
, wc
);
1871 /* was the root node processed? if not, catch it here */
1872 if (path
->nodes
[orig_level
]) {
1873 wc
->process_func(log
, path
->nodes
[orig_level
], wc
,
1874 btrfs_header_generation(path
->nodes
[orig_level
]));
1876 struct extent_buffer
*next
;
1878 next
= path
->nodes
[orig_level
];
1880 btrfs_tree_lock(next
);
1881 clean_tree_block(trans
, log
, next
);
1882 btrfs_wait_tree_block_writeback(next
);
1883 btrfs_tree_unlock(next
);
1885 if (orig_level
== 0) {
1886 ret
= btrfs_drop_leaf_ref(trans
, log
,
1890 WARN_ON(log
->root_key
.objectid
!=
1891 BTRFS_TREE_LOG_OBJECTID
);
1892 ret
= btrfs_free_reserved_extent(log
, next
->start
,
1898 for (i
= 0; i
<= orig_level
; i
++) {
1899 if (path
->nodes
[i
]) {
1900 free_extent_buffer(path
->nodes
[i
]);
1901 path
->nodes
[i
] = NULL
;
1904 btrfs_free_path(path
);
1906 free_extent_buffer(log
->node
);
1910 static int wait_log_commit(struct btrfs_root
*log
)
1913 u64 transid
= log
->fs_info
->tree_log_transid
;
1916 prepare_to_wait(&log
->fs_info
->tree_log_wait
, &wait
,
1917 TASK_UNINTERRUPTIBLE
);
1918 mutex_unlock(&log
->fs_info
->tree_log_mutex
);
1919 if (atomic_read(&log
->fs_info
->tree_log_commit
))
1921 finish_wait(&log
->fs_info
->tree_log_wait
, &wait
);
1922 mutex_lock(&log
->fs_info
->tree_log_mutex
);
1923 } while (transid
== log
->fs_info
->tree_log_transid
&&
1924 atomic_read(&log
->fs_info
->tree_log_commit
));
1929 * btrfs_sync_log does sends a given tree log down to the disk and
1930 * updates the super blocks to record it. When this call is done,
1931 * you know that any inodes previously logged are safely on disk
1933 int btrfs_sync_log(struct btrfs_trans_handle
*trans
,
1934 struct btrfs_root
*root
)
1937 unsigned long batch
;
1938 struct btrfs_root
*log
= root
->log_root
;
1940 mutex_lock(&log
->fs_info
->tree_log_mutex
);
1941 if (atomic_read(&log
->fs_info
->tree_log_commit
)) {
1942 wait_log_commit(log
);
1945 atomic_set(&log
->fs_info
->tree_log_commit
, 1);
1948 batch
= log
->fs_info
->tree_log_batch
;
1949 mutex_unlock(&log
->fs_info
->tree_log_mutex
);
1950 schedule_timeout_uninterruptible(1);
1951 mutex_lock(&log
->fs_info
->tree_log_mutex
);
1953 while (atomic_read(&log
->fs_info
->tree_log_writers
)) {
1955 prepare_to_wait(&log
->fs_info
->tree_log_wait
, &wait
,
1956 TASK_UNINTERRUPTIBLE
);
1957 mutex_unlock(&log
->fs_info
->tree_log_mutex
);
1958 if (atomic_read(&log
->fs_info
->tree_log_writers
))
1960 mutex_lock(&log
->fs_info
->tree_log_mutex
);
1961 finish_wait(&log
->fs_info
->tree_log_wait
, &wait
);
1963 if (batch
== log
->fs_info
->tree_log_batch
)
1967 ret
= btrfs_write_and_wait_marked_extents(log
, &log
->dirty_log_pages
);
1969 ret
= btrfs_write_and_wait_marked_extents(root
->fs_info
->log_root_tree
,
1970 &root
->fs_info
->log_root_tree
->dirty_log_pages
);
1973 btrfs_set_super_log_root(&root
->fs_info
->super_for_commit
,
1974 log
->fs_info
->log_root_tree
->node
->start
);
1975 btrfs_set_super_log_root_level(&root
->fs_info
->super_for_commit
,
1976 btrfs_header_level(log
->fs_info
->log_root_tree
->node
));
1978 write_ctree_super(trans
, log
->fs_info
->tree_root
, 2);
1979 log
->fs_info
->tree_log_transid
++;
1980 log
->fs_info
->tree_log_batch
= 0;
1981 atomic_set(&log
->fs_info
->tree_log_commit
, 0);
1983 if (waitqueue_active(&log
->fs_info
->tree_log_wait
))
1984 wake_up(&log
->fs_info
->tree_log_wait
);
1986 mutex_unlock(&log
->fs_info
->tree_log_mutex
);
1990 /* * free all the extents used by the tree log. This should be called
1991 * at commit time of the full transaction
1993 int btrfs_free_log(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
)
1996 struct btrfs_root
*log
;
2000 struct walk_control wc
= {
2002 .process_func
= process_one_buffer
2005 if (!root
->log_root
|| root
->fs_info
->log_root_recovering
)
2008 log
= root
->log_root
;
2009 ret
= walk_log_tree(trans
, log
, &wc
);
2013 ret
= find_first_extent_bit(&log
->dirty_log_pages
,
2014 0, &start
, &end
, EXTENT_DIRTY
);
2018 clear_extent_dirty(&log
->dirty_log_pages
,
2019 start
, end
, GFP_NOFS
);
2022 log
= root
->log_root
;
2023 ret
= btrfs_del_root(trans
, root
->fs_info
->log_root_tree
,
2026 root
->log_root
= NULL
;
2027 kfree(root
->log_root
);
2032 * helper function to update the item for a given subvolumes log root
2033 * in the tree of log roots
2035 static int update_log_root(struct btrfs_trans_handle
*trans
,
2036 struct btrfs_root
*log
)
2038 u64 bytenr
= btrfs_root_bytenr(&log
->root_item
);
2041 if (log
->node
->start
== bytenr
)
2044 btrfs_set_root_bytenr(&log
->root_item
, log
->node
->start
);
2045 btrfs_set_root_generation(&log
->root_item
, trans
->transid
);
2046 btrfs_set_root_level(&log
->root_item
, btrfs_header_level(log
->node
));
2047 ret
= btrfs_update_root(trans
, log
->fs_info
->log_root_tree
,
2048 &log
->root_key
, &log
->root_item
);
2054 * If both a file and directory are logged, and unlinks or renames are
2055 * mixed in, we have a few interesting corners:
2057 * create file X in dir Y
2058 * link file X to X.link in dir Y
2060 * unlink file X but leave X.link
2063 * After a crash we would expect only X.link to exist. But file X
2064 * didn't get fsync'd again so the log has back refs for X and X.link.
2066 * We solve this by removing directory entries and inode backrefs from the
2067 * log when a file that was logged in the current transaction is
2068 * unlinked. Any later fsync will include the updated log entries, and
2069 * we'll be able to reconstruct the proper directory items from backrefs.
2071 * This optimizations allows us to avoid relogging the entire inode
2072 * or the entire directory.
2074 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle
*trans
,
2075 struct btrfs_root
*root
,
2076 const char *name
, int name_len
,
2077 struct inode
*dir
, u64 index
)
2079 struct btrfs_root
*log
;
2080 struct btrfs_dir_item
*di
;
2081 struct btrfs_path
*path
;
2085 if (BTRFS_I(dir
)->logged_trans
< trans
->transid
)
2088 ret
= join_running_log_trans(root
);
2092 mutex_lock(&BTRFS_I(dir
)->log_mutex
);
2094 log
= root
->log_root
;
2095 path
= btrfs_alloc_path();
2096 di
= btrfs_lookup_dir_item(trans
, log
, path
, dir
->i_ino
,
2097 name
, name_len
, -1);
2098 if (di
&& !IS_ERR(di
)) {
2099 ret
= btrfs_delete_one_dir_name(trans
, log
, path
, di
);
2100 bytes_del
+= name_len
;
2103 btrfs_release_path(log
, path
);
2104 di
= btrfs_lookup_dir_index_item(trans
, log
, path
, dir
->i_ino
,
2105 index
, name
, name_len
, -1);
2106 if (di
&& !IS_ERR(di
)) {
2107 ret
= btrfs_delete_one_dir_name(trans
, log
, path
, di
);
2108 bytes_del
+= name_len
;
2112 /* update the directory size in the log to reflect the names
2116 struct btrfs_key key
;
2118 key
.objectid
= dir
->i_ino
;
2120 key
.type
= BTRFS_INODE_ITEM_KEY
;
2121 btrfs_release_path(log
, path
);
2123 ret
= btrfs_search_slot(trans
, log
, &key
, path
, 0, 1);
2125 struct btrfs_inode_item
*item
;
2128 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2129 struct btrfs_inode_item
);
2130 i_size
= btrfs_inode_size(path
->nodes
[0], item
);
2131 if (i_size
> bytes_del
)
2132 i_size
-= bytes_del
;
2135 btrfs_set_inode_size(path
->nodes
[0], item
, i_size
);
2136 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2139 btrfs_release_path(log
, path
);
2142 btrfs_free_path(path
);
2143 mutex_unlock(&BTRFS_I(dir
)->log_mutex
);
2144 end_log_trans(root
);
2149 /* see comments for btrfs_del_dir_entries_in_log */
2150 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle
*trans
,
2151 struct btrfs_root
*root
,
2152 const char *name
, int name_len
,
2153 struct inode
*inode
, u64 dirid
)
2155 struct btrfs_root
*log
;
2159 if (BTRFS_I(inode
)->logged_trans
< trans
->transid
)
2162 ret
= join_running_log_trans(root
);
2165 log
= root
->log_root
;
2166 mutex_lock(&BTRFS_I(inode
)->log_mutex
);
2168 ret
= btrfs_del_inode_ref(trans
, log
, name
, name_len
, inode
->i_ino
,
2170 mutex_unlock(&BTRFS_I(inode
)->log_mutex
);
2171 end_log_trans(root
);
2177 * creates a range item in the log for 'dirid'. first_offset and
2178 * last_offset tell us which parts of the key space the log should
2179 * be considered authoritative for.
2181 static noinline
int insert_dir_log_key(struct btrfs_trans_handle
*trans
,
2182 struct btrfs_root
*log
,
2183 struct btrfs_path
*path
,
2184 int key_type
, u64 dirid
,
2185 u64 first_offset
, u64 last_offset
)
2188 struct btrfs_key key
;
2189 struct btrfs_dir_log_item
*item
;
2191 key
.objectid
= dirid
;
2192 key
.offset
= first_offset
;
2193 if (key_type
== BTRFS_DIR_ITEM_KEY
)
2194 key
.type
= BTRFS_DIR_LOG_ITEM_KEY
;
2196 key
.type
= BTRFS_DIR_LOG_INDEX_KEY
;
2197 ret
= btrfs_insert_empty_item(trans
, log
, path
, &key
, sizeof(*item
));
2200 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2201 struct btrfs_dir_log_item
);
2202 btrfs_set_dir_log_end(path
->nodes
[0], item
, last_offset
);
2203 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2204 btrfs_release_path(log
, path
);
2209 * log all the items included in the current transaction for a given
2210 * directory. This also creates the range items in the log tree required
2211 * to replay anything deleted before the fsync
2213 static noinline
int log_dir_items(struct btrfs_trans_handle
*trans
,
2214 struct btrfs_root
*root
, struct inode
*inode
,
2215 struct btrfs_path
*path
,
2216 struct btrfs_path
*dst_path
, int key_type
,
2217 u64 min_offset
, u64
*last_offset_ret
)
2219 struct btrfs_key min_key
;
2220 struct btrfs_key max_key
;
2221 struct btrfs_root
*log
= root
->log_root
;
2222 struct extent_buffer
*src
;
2226 u64 first_offset
= min_offset
;
2227 u64 last_offset
= (u64
)-1;
2229 log
= root
->log_root
;
2230 max_key
.objectid
= inode
->i_ino
;
2231 max_key
.offset
= (u64
)-1;
2232 max_key
.type
= key_type
;
2234 min_key
.objectid
= inode
->i_ino
;
2235 min_key
.type
= key_type
;
2236 min_key
.offset
= min_offset
;
2238 path
->keep_locks
= 1;
2240 ret
= btrfs_search_forward(root
, &min_key
, &max_key
,
2241 path
, 0, trans
->transid
);
2244 * we didn't find anything from this transaction, see if there
2245 * is anything at all
2247 if (ret
!= 0 || min_key
.objectid
!= inode
->i_ino
||
2248 min_key
.type
!= key_type
) {
2249 min_key
.objectid
= inode
->i_ino
;
2250 min_key
.type
= key_type
;
2251 min_key
.offset
= (u64
)-1;
2252 btrfs_release_path(root
, path
);
2253 ret
= btrfs_search_slot(NULL
, root
, &min_key
, path
, 0, 0);
2255 btrfs_release_path(root
, path
);
2258 ret
= btrfs_previous_item(root
, path
, inode
->i_ino
, key_type
);
2260 /* if ret == 0 there are items for this type,
2261 * create a range to tell us the last key of this type.
2262 * otherwise, there are no items in this directory after
2263 * *min_offset, and we create a range to indicate that.
2266 struct btrfs_key tmp
;
2267 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
,
2269 if (key_type
== tmp
.type
)
2270 first_offset
= max(min_offset
, tmp
.offset
) + 1;
2275 /* go backward to find any previous key */
2276 ret
= btrfs_previous_item(root
, path
, inode
->i_ino
, key_type
);
2278 struct btrfs_key tmp
;
2279 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
, path
->slots
[0]);
2280 if (key_type
== tmp
.type
) {
2281 first_offset
= tmp
.offset
;
2282 ret
= overwrite_item(trans
, log
, dst_path
,
2283 path
->nodes
[0], path
->slots
[0],
2287 btrfs_release_path(root
, path
);
2289 /* find the first key from this transaction again */
2290 ret
= btrfs_search_slot(NULL
, root
, &min_key
, path
, 0, 0);
2297 * we have a block from this transaction, log every item in it
2298 * from our directory
2301 struct btrfs_key tmp
;
2302 src
= path
->nodes
[0];
2303 nritems
= btrfs_header_nritems(src
);
2304 for (i
= path
->slots
[0]; i
< nritems
; i
++) {
2305 btrfs_item_key_to_cpu(src
, &min_key
, i
);
2307 if (min_key
.objectid
!= inode
->i_ino
||
2308 min_key
.type
!= key_type
)
2310 ret
= overwrite_item(trans
, log
, dst_path
, src
, i
,
2314 path
->slots
[0] = nritems
;
2317 * look ahead to the next item and see if it is also
2318 * from this directory and from this transaction
2320 ret
= btrfs_next_leaf(root
, path
);
2322 last_offset
= (u64
)-1;
2325 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
, path
->slots
[0]);
2326 if (tmp
.objectid
!= inode
->i_ino
|| tmp
.type
!= key_type
) {
2327 last_offset
= (u64
)-1;
2330 if (btrfs_header_generation(path
->nodes
[0]) != trans
->transid
) {
2331 ret
= overwrite_item(trans
, log
, dst_path
,
2332 path
->nodes
[0], path
->slots
[0],
2336 last_offset
= tmp
.offset
;
2341 *last_offset_ret
= last_offset
;
2342 btrfs_release_path(root
, path
);
2343 btrfs_release_path(log
, dst_path
);
2345 /* insert the log range keys to indicate where the log is valid */
2346 ret
= insert_dir_log_key(trans
, log
, path
, key_type
, inode
->i_ino
,
2347 first_offset
, last_offset
);
2353 * logging directories is very similar to logging inodes, We find all the items
2354 * from the current transaction and write them to the log.
2356 * The recovery code scans the directory in the subvolume, and if it finds a
2357 * key in the range logged that is not present in the log tree, then it means
2358 * that dir entry was unlinked during the transaction.
2360 * In order for that scan to work, we must include one key smaller than
2361 * the smallest logged by this transaction and one key larger than the largest
2362 * key logged by this transaction.
2364 static noinline
int log_directory_changes(struct btrfs_trans_handle
*trans
,
2365 struct btrfs_root
*root
, struct inode
*inode
,
2366 struct btrfs_path
*path
,
2367 struct btrfs_path
*dst_path
)
2372 int key_type
= BTRFS_DIR_ITEM_KEY
;
2378 ret
= log_dir_items(trans
, root
, inode
, path
,
2379 dst_path
, key_type
, min_key
,
2382 if (max_key
== (u64
)-1)
2384 min_key
= max_key
+ 1;
2387 if (key_type
== BTRFS_DIR_ITEM_KEY
) {
2388 key_type
= BTRFS_DIR_INDEX_KEY
;
2395 * a helper function to drop items from the log before we relog an
2396 * inode. max_key_type indicates the highest item type to remove.
2397 * This cannot be run for file data extents because it does not
2398 * free the extents they point to.
2400 static int drop_objectid_items(struct btrfs_trans_handle
*trans
,
2401 struct btrfs_root
*log
,
2402 struct btrfs_path
*path
,
2403 u64 objectid
, int max_key_type
)
2406 struct btrfs_key key
;
2407 struct btrfs_key found_key
;
2409 key
.objectid
= objectid
;
2410 key
.type
= max_key_type
;
2411 key
.offset
= (u64
)-1;
2414 ret
= btrfs_search_slot(trans
, log
, &key
, path
, -1, 1);
2419 if (path
->slots
[0] == 0)
2423 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2426 if (found_key
.objectid
!= objectid
)
2429 ret
= btrfs_del_item(trans
, log
, path
);
2431 btrfs_release_path(log
, path
);
2433 btrfs_release_path(log
, path
);
2437 static noinline
int copy_items(struct btrfs_trans_handle
*trans
,
2438 struct btrfs_root
*log
,
2439 struct btrfs_path
*dst_path
,
2440 struct extent_buffer
*src
,
2441 int start_slot
, int nr
, int inode_only
)
2443 unsigned long src_offset
;
2444 unsigned long dst_offset
;
2445 struct btrfs_file_extent_item
*extent
;
2446 struct btrfs_inode_item
*inode_item
;
2448 struct btrfs_key
*ins_keys
;
2452 struct list_head ordered_sums
;
2454 INIT_LIST_HEAD(&ordered_sums
);
2456 ins_data
= kmalloc(nr
* sizeof(struct btrfs_key
) +
2457 nr
* sizeof(u32
), GFP_NOFS
);
2458 ins_sizes
= (u32
*)ins_data
;
2459 ins_keys
= (struct btrfs_key
*)(ins_data
+ nr
* sizeof(u32
));
2461 for (i
= 0; i
< nr
; i
++) {
2462 ins_sizes
[i
] = btrfs_item_size_nr(src
, i
+ start_slot
);
2463 btrfs_item_key_to_cpu(src
, ins_keys
+ i
, i
+ start_slot
);
2465 ret
= btrfs_insert_empty_items(trans
, log
, dst_path
,
2466 ins_keys
, ins_sizes
, nr
);
2469 for (i
= 0; i
< nr
; i
++) {
2470 dst_offset
= btrfs_item_ptr_offset(dst_path
->nodes
[0],
2471 dst_path
->slots
[0]);
2473 src_offset
= btrfs_item_ptr_offset(src
, start_slot
+ i
);
2475 copy_extent_buffer(dst_path
->nodes
[0], src
, dst_offset
,
2476 src_offset
, ins_sizes
[i
]);
2478 if (inode_only
== LOG_INODE_EXISTS
&&
2479 ins_keys
[i
].type
== BTRFS_INODE_ITEM_KEY
) {
2480 inode_item
= btrfs_item_ptr(dst_path
->nodes
[0],
2482 struct btrfs_inode_item
);
2483 btrfs_set_inode_size(dst_path
->nodes
[0], inode_item
, 0);
2485 /* set the generation to zero so the recover code
2486 * can tell the difference between an logging
2487 * just to say 'this inode exists' and a logging
2488 * to say 'update this inode with these values'
2490 btrfs_set_inode_generation(dst_path
->nodes
[0],
2493 /* take a reference on file data extents so that truncates
2494 * or deletes of this inode don't have to relog the inode
2497 if (btrfs_key_type(ins_keys
+ i
) == BTRFS_EXTENT_DATA_KEY
) {
2499 extent
= btrfs_item_ptr(src
, start_slot
+ i
,
2500 struct btrfs_file_extent_item
);
2502 found_type
= btrfs_file_extent_type(src
, extent
);
2503 if (found_type
== BTRFS_FILE_EXTENT_REG
||
2504 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2505 u64 ds
= btrfs_file_extent_disk_bytenr(src
,
2507 u64 dl
= btrfs_file_extent_disk_num_bytes(src
,
2509 u64 cs
= btrfs_file_extent_offset(src
, extent
);
2510 u64 cl
= btrfs_file_extent_num_bytes(src
,
2512 if (btrfs_file_extent_compression(src
,
2517 /* ds == 0 is a hole */
2519 ret
= btrfs_inc_extent_ref(trans
, log
,
2521 dst_path
->nodes
[0]->start
,
2522 BTRFS_TREE_LOG_OBJECTID
,
2524 ins_keys
[i
].objectid
);
2526 ret
= btrfs_lookup_csums_range(
2527 log
->fs_info
->csum_root
,
2528 ds
+ cs
, ds
+ cs
+ cl
- 1,
2534 dst_path
->slots
[0]++;
2537 btrfs_mark_buffer_dirty(dst_path
->nodes
[0]);
2538 btrfs_release_path(log
, dst_path
);
2542 * we have to do this after the loop above to avoid changing the
2543 * log tree while trying to change the log tree.
2545 while (!list_empty(&ordered_sums
)) {
2546 struct btrfs_ordered_sum
*sums
= list_entry(ordered_sums
.next
,
2547 struct btrfs_ordered_sum
,
2549 ret
= btrfs_csum_file_blocks(trans
, log
, sums
);
2551 list_del(&sums
->list
);
2557 /* log a single inode in the tree log.
2558 * At least one parent directory for this inode must exist in the tree
2559 * or be logged already.
2561 * Any items from this inode changed by the current transaction are copied
2562 * to the log tree. An extra reference is taken on any extents in this
2563 * file, allowing us to avoid a whole pile of corner cases around logging
2564 * blocks that have been removed from the tree.
2566 * See LOG_INODE_ALL and related defines for a description of what inode_only
2569 * This handles both files and directories.
2571 static int __btrfs_log_inode(struct btrfs_trans_handle
*trans
,
2572 struct btrfs_root
*root
, struct inode
*inode
,
2575 struct btrfs_path
*path
;
2576 struct btrfs_path
*dst_path
;
2577 struct btrfs_key min_key
;
2578 struct btrfs_key max_key
;
2579 struct btrfs_root
*log
= root
->log_root
;
2580 struct extent_buffer
*src
= NULL
;
2584 int ins_start_slot
= 0;
2587 log
= root
->log_root
;
2589 path
= btrfs_alloc_path();
2590 dst_path
= btrfs_alloc_path();
2592 min_key
.objectid
= inode
->i_ino
;
2593 min_key
.type
= BTRFS_INODE_ITEM_KEY
;
2596 max_key
.objectid
= inode
->i_ino
;
2597 if (inode_only
== LOG_INODE_EXISTS
|| S_ISDIR(inode
->i_mode
))
2598 max_key
.type
= BTRFS_XATTR_ITEM_KEY
;
2600 max_key
.type
= (u8
)-1;
2601 max_key
.offset
= (u64
)-1;
2604 * if this inode has already been logged and we're in inode_only
2605 * mode, we don't want to delete the things that have already
2606 * been written to the log.
2608 * But, if the inode has been through an inode_only log,
2609 * the logged_trans field is not set. This allows us to catch
2610 * any new names for this inode in the backrefs by logging it
2613 if (inode_only
== LOG_INODE_EXISTS
&&
2614 BTRFS_I(inode
)->logged_trans
== trans
->transid
) {
2615 btrfs_free_path(path
);
2616 btrfs_free_path(dst_path
);
2619 mutex_lock(&BTRFS_I(inode
)->log_mutex
);
2622 * a brute force approach to making sure we get the most uptodate
2623 * copies of everything.
2625 if (S_ISDIR(inode
->i_mode
)) {
2626 int max_key_type
= BTRFS_DIR_LOG_INDEX_KEY
;
2628 if (inode_only
== LOG_INODE_EXISTS
)
2629 max_key_type
= BTRFS_XATTR_ITEM_KEY
;
2630 ret
= drop_objectid_items(trans
, log
, path
,
2631 inode
->i_ino
, max_key_type
);
2633 ret
= btrfs_truncate_inode_items(trans
, log
, inode
, 0, 0);
2636 path
->keep_locks
= 1;
2640 ret
= btrfs_search_forward(root
, &min_key
, &max_key
,
2641 path
, 0, trans
->transid
);
2645 /* note, ins_nr might be > 0 here, cleanup outside the loop */
2646 if (min_key
.objectid
!= inode
->i_ino
)
2648 if (min_key
.type
> max_key
.type
)
2651 src
= path
->nodes
[0];
2652 size
= btrfs_item_size_nr(src
, path
->slots
[0]);
2653 if (ins_nr
&& ins_start_slot
+ ins_nr
== path
->slots
[0]) {
2656 } else if (!ins_nr
) {
2657 ins_start_slot
= path
->slots
[0];
2662 ret
= copy_items(trans
, log
, dst_path
, src
, ins_start_slot
,
2663 ins_nr
, inode_only
);
2666 ins_start_slot
= path
->slots
[0];
2669 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2671 if (path
->slots
[0] < nritems
) {
2672 btrfs_item_key_to_cpu(path
->nodes
[0], &min_key
,
2677 ret
= copy_items(trans
, log
, dst_path
, src
,
2679 ins_nr
, inode_only
);
2683 btrfs_release_path(root
, path
);
2685 if (min_key
.offset
< (u64
)-1)
2687 else if (min_key
.type
< (u8
)-1)
2689 else if (min_key
.objectid
< (u64
)-1)
2695 ret
= copy_items(trans
, log
, dst_path
, src
,
2697 ins_nr
, inode_only
);
2702 if (inode_only
== LOG_INODE_ALL
&& S_ISDIR(inode
->i_mode
)) {
2703 btrfs_release_path(root
, path
);
2704 btrfs_release_path(log
, dst_path
);
2705 BTRFS_I(inode
)->log_dirty_trans
= 0;
2706 ret
= log_directory_changes(trans
, root
, inode
, path
, dst_path
);
2709 BTRFS_I(inode
)->logged_trans
= trans
->transid
;
2710 mutex_unlock(&BTRFS_I(inode
)->log_mutex
);
2712 btrfs_free_path(path
);
2713 btrfs_free_path(dst_path
);
2715 mutex_lock(&root
->fs_info
->tree_log_mutex
);
2716 ret
= update_log_root(trans
, log
);
2718 mutex_unlock(&root
->fs_info
->tree_log_mutex
);
2723 int btrfs_log_inode(struct btrfs_trans_handle
*trans
,
2724 struct btrfs_root
*root
, struct inode
*inode
,
2729 start_log_trans(trans
, root
);
2730 ret
= __btrfs_log_inode(trans
, root
, inode
, inode_only
);
2731 end_log_trans(root
);
2736 * helper function around btrfs_log_inode to make sure newly created
2737 * parent directories also end up in the log. A minimal inode and backref
2738 * only logging is done of any parent directories that are older than
2739 * the last committed transaction
2741 int btrfs_log_dentry(struct btrfs_trans_handle
*trans
,
2742 struct btrfs_root
*root
, struct dentry
*dentry
)
2744 int inode_only
= LOG_INODE_ALL
;
2745 struct super_block
*sb
;
2748 start_log_trans(trans
, root
);
2749 sb
= dentry
->d_inode
->i_sb
;
2751 ret
= __btrfs_log_inode(trans
, root
, dentry
->d_inode
,
2754 inode_only
= LOG_INODE_EXISTS
;
2756 dentry
= dentry
->d_parent
;
2757 if (!dentry
|| !dentry
->d_inode
|| sb
!= dentry
->d_inode
->i_sb
)
2760 if (BTRFS_I(dentry
->d_inode
)->generation
<=
2761 root
->fs_info
->last_trans_committed
)
2764 end_log_trans(root
);
2769 * it is not safe to log dentry if the chunk root has added new
2770 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
2771 * If this returns 1, you must commit the transaction to safely get your
2774 int btrfs_log_dentry_safe(struct btrfs_trans_handle
*trans
,
2775 struct btrfs_root
*root
, struct dentry
*dentry
)
2778 gen
= root
->fs_info
->last_trans_new_blockgroup
;
2779 if (gen
> root
->fs_info
->last_trans_committed
)
2782 return btrfs_log_dentry(trans
, root
, dentry
);
2786 * should be called during mount to recover any replay any log trees
2789 int btrfs_recover_log_trees(struct btrfs_root
*log_root_tree
)
2792 struct btrfs_path
*path
;
2793 struct btrfs_trans_handle
*trans
;
2794 struct btrfs_key key
;
2795 struct btrfs_key found_key
;
2796 struct btrfs_key tmp_key
;
2797 struct btrfs_root
*log
;
2798 struct btrfs_fs_info
*fs_info
= log_root_tree
->fs_info
;
2800 struct walk_control wc
= {
2801 .process_func
= process_one_buffer
,
2805 fs_info
->log_root_recovering
= 1;
2806 path
= btrfs_alloc_path();
2809 trans
= btrfs_start_transaction(fs_info
->tree_root
, 1);
2814 walk_log_tree(trans
, log_root_tree
, &wc
);
2817 key
.objectid
= BTRFS_TREE_LOG_OBJECTID
;
2818 key
.offset
= (u64
)-1;
2819 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2822 ret
= btrfs_search_slot(NULL
, log_root_tree
, &key
, path
, 0, 0);
2826 if (path
->slots
[0] == 0)
2830 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2832 btrfs_release_path(log_root_tree
, path
);
2833 if (found_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
2836 log
= btrfs_read_fs_root_no_radix(log_root_tree
,
2841 tmp_key
.objectid
= found_key
.offset
;
2842 tmp_key
.type
= BTRFS_ROOT_ITEM_KEY
;
2843 tmp_key
.offset
= (u64
)-1;
2845 wc
.replay_dest
= btrfs_read_fs_root_no_name(fs_info
, &tmp_key
);
2846 BUG_ON(!wc
.replay_dest
);
2848 wc
.replay_dest
->log_root
= log
;
2849 btrfs_record_root_in_trans(wc
.replay_dest
);
2850 ret
= walk_log_tree(trans
, log
, &wc
);
2853 if (wc
.stage
== LOG_WALK_REPLAY_ALL
) {
2854 ret
= fixup_inode_link_counts(trans
, wc
.replay_dest
,
2858 ret
= btrfs_find_highest_inode(wc
.replay_dest
, &highest_inode
);
2860 wc
.replay_dest
->highest_inode
= highest_inode
;
2861 wc
.replay_dest
->last_inode_alloc
= highest_inode
;
2864 key
.offset
= found_key
.offset
- 1;
2865 wc
.replay_dest
->log_root
= NULL
;
2866 free_extent_buffer(log
->node
);
2869 if (found_key
.offset
== 0)
2872 btrfs_release_path(log_root_tree
, path
);
2874 /* step one is to pin it all, step two is to replay just inodes */
2877 wc
.process_func
= replay_one_buffer
;
2878 wc
.stage
= LOG_WALK_REPLAY_INODES
;
2881 /* step three is to replay everything */
2882 if (wc
.stage
< LOG_WALK_REPLAY_ALL
) {
2887 btrfs_free_path(path
);
2889 free_extent_buffer(log_root_tree
->node
);
2890 log_root_tree
->log_root
= NULL
;
2891 fs_info
->log_root_recovering
= 0;
2893 /* step 4: commit the transaction, which also unpins the blocks */
2894 btrfs_commit_transaction(trans
, fs_info
->tree_root
);
2896 kfree(log_root_tree
);