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"
27 /* magic values for the inode_only field in btrfs_log_inode:
29 * LOG_INODE_ALL means to log everything
30 * LOG_INODE_EXISTS means to log just enough to recreate the inode
33 #define LOG_INODE_ALL 0
34 #define LOG_INODE_EXISTS 1
37 * stages for the tree walking. The first
38 * stage (0) is to only pin down the blocks we find
39 * the second stage (1) is to make sure that all the inodes
40 * we find in the log are created in the subvolume.
42 * The last stage is to deal with directories and links and extents
43 * and all the other fun semantics
45 #define LOG_WALK_PIN_ONLY 0
46 #define LOG_WALK_REPLAY_INODES 1
47 #define LOG_WALK_REPLAY_ALL 2
49 static int __btrfs_log_inode(struct btrfs_trans_handle
*trans
,
50 struct btrfs_root
*root
, struct inode
*inode
,
54 * tree logging is a special write ahead log used to make sure that
55 * fsyncs and O_SYNCs can happen without doing full tree commits.
57 * Full tree commits are expensive because they require commonly
58 * modified blocks to be recowed, creating many dirty pages in the
59 * extent tree an 4x-6x higher write load than ext3.
61 * Instead of doing a tree commit on every fsync, we use the
62 * key ranges and transaction ids to find items for a given file or directory
63 * that have changed in this transaction. Those items are copied into
64 * a special tree (one per subvolume root), that tree is written to disk
65 * and then the fsync is considered complete.
67 * After a crash, items are copied out of the log-tree back into the
68 * subvolume tree. Any file data extents found are recorded in the extent
69 * allocation tree, and the log-tree freed.
71 * The log tree is read three times, once to pin down all the extents it is
72 * using in ram and once, once to create all the inodes logged in the tree
73 * and once to do all the other items.
77 * btrfs_add_log_tree adds a new per-subvolume log tree into the
78 * tree of log tree roots. This must be called with a tree log transaction
79 * running (see start_log_trans).
81 int btrfs_add_log_tree(struct btrfs_trans_handle
*trans
,
82 struct btrfs_root
*root
)
85 struct btrfs_root_item root_item
;
86 struct btrfs_inode_item
*inode_item
;
87 struct extent_buffer
*leaf
;
88 struct btrfs_root
*new_root
= root
;
90 u64 objectid
= root
->root_key
.objectid
;
92 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
, 0,
93 BTRFS_TREE_LOG_OBJECTID
,
94 trans
->transid
, 0, 0, 0);
100 btrfs_set_header_nritems(leaf
, 0);
101 btrfs_set_header_level(leaf
, 0);
102 btrfs_set_header_bytenr(leaf
, leaf
->start
);
103 btrfs_set_header_generation(leaf
, trans
->transid
);
104 btrfs_set_header_owner(leaf
, BTRFS_TREE_LOG_OBJECTID
);
106 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
107 (unsigned long)btrfs_header_fsid(leaf
),
109 btrfs_mark_buffer_dirty(leaf
);
111 inode_item
= &root_item
.inode
;
112 memset(inode_item
, 0, sizeof(*inode_item
));
113 inode_item
->generation
= cpu_to_le64(1);
114 inode_item
->size
= cpu_to_le64(3);
115 inode_item
->nlink
= cpu_to_le32(1);
116 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
117 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
119 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
120 btrfs_set_root_generation(&root_item
, trans
->transid
);
121 btrfs_set_root_level(&root_item
, 0);
122 btrfs_set_root_refs(&root_item
, 0);
123 btrfs_set_root_used(&root_item
, 0);
125 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
126 root_item
.drop_level
= 0;
128 btrfs_tree_unlock(leaf
);
129 free_extent_buffer(leaf
);
132 btrfs_set_root_dirid(&root_item
, 0);
134 key
.objectid
= BTRFS_TREE_LOG_OBJECTID
;
135 key
.offset
= objectid
;
136 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
137 ret
= btrfs_insert_root(trans
, root
->fs_info
->log_root_tree
, &key
,
142 new_root
= btrfs_read_fs_root_no_radix(root
->fs_info
->log_root_tree
,
146 WARN_ON(root
->log_root
);
147 root
->log_root
= new_root
;
150 * log trees do not get reference counted because they go away
151 * before a real commit is actually done. They do store pointers
152 * to file data extents, and those reference counts still get
153 * updated (along with back refs to the log tree).
155 new_root
->ref_cows
= 0;
156 new_root
->last_trans
= trans
->transid
;
162 * start a sub transaction and setup the log tree
163 * this increments the log tree writer count to make the people
164 * syncing the tree wait for us to finish
166 static int start_log_trans(struct btrfs_trans_handle
*trans
,
167 struct btrfs_root
*root
)
170 mutex_lock(&root
->fs_info
->tree_log_mutex
);
171 if (!root
->fs_info
->log_root_tree
) {
172 ret
= btrfs_init_log_root_tree(trans
, root
->fs_info
);
175 if (!root
->log_root
) {
176 ret
= btrfs_add_log_tree(trans
, root
);
179 atomic_inc(&root
->fs_info
->tree_log_writers
);
180 root
->fs_info
->tree_log_batch
++;
181 mutex_unlock(&root
->fs_info
->tree_log_mutex
);
186 * returns 0 if there was a log transaction running and we were able
187 * to join, or returns -ENOENT if there were not transactions
190 static int join_running_log_trans(struct btrfs_root
*root
)
198 mutex_lock(&root
->fs_info
->tree_log_mutex
);
199 if (root
->log_root
) {
201 atomic_inc(&root
->fs_info
->tree_log_writers
);
202 root
->fs_info
->tree_log_batch
++;
204 mutex_unlock(&root
->fs_info
->tree_log_mutex
);
209 * indicate we're done making changes to the log tree
210 * and wake up anyone waiting to do a sync
212 static int end_log_trans(struct btrfs_root
*root
)
214 atomic_dec(&root
->fs_info
->tree_log_writers
);
216 if (waitqueue_active(&root
->fs_info
->tree_log_wait
))
217 wake_up(&root
->fs_info
->tree_log_wait
);
223 * the walk control struct is used to pass state down the chain when
224 * processing the log tree. The stage field tells us which part
225 * of the log tree processing we are currently doing. The others
226 * are state fields used for that specific part
228 struct walk_control
{
229 /* should we free the extent on disk when done? This is used
230 * at transaction commit time while freeing a log tree
234 /* should we write out the extent buffer? This is used
235 * while flushing the log tree to disk during a sync
239 /* should we wait for the extent buffer io to finish? Also used
240 * while flushing the log tree to disk for a sync
244 /* pin only walk, we record which extents on disk belong to the
249 /* what stage of the replay code we're currently in */
252 /* the root we are currently replaying */
253 struct btrfs_root
*replay_dest
;
255 /* the trans handle for the current replay */
256 struct btrfs_trans_handle
*trans
;
258 /* the function that gets used to process blocks we find in the
259 * tree. Note the extent_buffer might not be up to date when it is
260 * passed in, and it must be checked or read if you need the data
263 int (*process_func
)(struct btrfs_root
*log
, struct extent_buffer
*eb
,
264 struct walk_control
*wc
, u64 gen
);
268 * process_func used to pin down extents, write them or wait on them
270 static int process_one_buffer(struct btrfs_root
*log
,
271 struct extent_buffer
*eb
,
272 struct walk_control
*wc
, u64 gen
)
275 mutex_lock(&log
->fs_info
->pinned_mutex
);
276 btrfs_update_pinned_extents(log
->fs_info
->extent_root
,
277 eb
->start
, eb
->len
, 1);
278 mutex_unlock(&log
->fs_info
->pinned_mutex
);
281 if (btrfs_buffer_uptodate(eb
, gen
)) {
283 btrfs_write_tree_block(eb
);
285 btrfs_wait_tree_block_writeback(eb
);
291 * Item overwrite used by replay and tree logging. eb, slot and key all refer
292 * to the src data we are copying out.
294 * root is the tree we are copying into, and path is a scratch
295 * path for use in this function (it should be released on entry and
296 * will be released on exit).
298 * If the key is already in the destination tree the existing item is
299 * overwritten. If the existing item isn't big enough, it is extended.
300 * If it is too large, it is truncated.
302 * If the key isn't in the destination yet, a new item is inserted.
304 static noinline
int overwrite_item(struct btrfs_trans_handle
*trans
,
305 struct btrfs_root
*root
,
306 struct btrfs_path
*path
,
307 struct extent_buffer
*eb
, int slot
,
308 struct btrfs_key
*key
)
312 u64 saved_i_size
= 0;
313 int save_old_i_size
= 0;
314 unsigned long src_ptr
;
315 unsigned long dst_ptr
;
316 int overwrite_root
= 0;
318 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
321 item_size
= btrfs_item_size_nr(eb
, slot
);
322 src_ptr
= btrfs_item_ptr_offset(eb
, slot
);
324 /* look for the key in the destination tree */
325 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
329 u32 dst_size
= btrfs_item_size_nr(path
->nodes
[0],
331 if (dst_size
!= item_size
)
334 if (item_size
== 0) {
335 btrfs_release_path(root
, path
);
338 dst_copy
= kmalloc(item_size
, GFP_NOFS
);
339 src_copy
= kmalloc(item_size
, GFP_NOFS
);
341 read_extent_buffer(eb
, src_copy
, src_ptr
, item_size
);
343 dst_ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
344 read_extent_buffer(path
->nodes
[0], dst_copy
, dst_ptr
,
346 ret
= memcmp(dst_copy
, src_copy
, item_size
);
351 * they have the same contents, just return, this saves
352 * us from cowing blocks in the destination tree and doing
353 * extra writes that may not have been done by a previous
357 btrfs_release_path(root
, path
);
363 btrfs_release_path(root
, path
);
364 /* try to insert the key into the destination tree */
365 ret
= btrfs_insert_empty_item(trans
, root
, path
,
368 /* make sure any existing item is the correct size */
369 if (ret
== -EEXIST
) {
371 found_size
= btrfs_item_size_nr(path
->nodes
[0],
373 if (found_size
> item_size
) {
374 btrfs_truncate_item(trans
, root
, path
, item_size
, 1);
375 } else if (found_size
< item_size
) {
376 ret
= btrfs_del_item(trans
, root
,
380 btrfs_release_path(root
, path
);
381 ret
= btrfs_insert_empty_item(trans
,
382 root
, path
, key
, item_size
);
388 dst_ptr
= btrfs_item_ptr_offset(path
->nodes
[0],
391 /* don't overwrite an existing inode if the generation number
392 * was logged as zero. This is done when the tree logging code
393 * is just logging an inode to make sure it exists after recovery.
395 * Also, don't overwrite i_size on directories during replay.
396 * log replay inserts and removes directory items based on the
397 * state of the tree found in the subvolume, and i_size is modified
400 if (key
->type
== BTRFS_INODE_ITEM_KEY
&& ret
== -EEXIST
) {
401 struct btrfs_inode_item
*src_item
;
402 struct btrfs_inode_item
*dst_item
;
404 src_item
= (struct btrfs_inode_item
*)src_ptr
;
405 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
407 if (btrfs_inode_generation(eb
, src_item
) == 0)
410 if (overwrite_root
&&
411 S_ISDIR(btrfs_inode_mode(eb
, src_item
)) &&
412 S_ISDIR(btrfs_inode_mode(path
->nodes
[0], dst_item
))) {
414 saved_i_size
= btrfs_inode_size(path
->nodes
[0],
419 copy_extent_buffer(path
->nodes
[0], eb
, dst_ptr
,
422 if (save_old_i_size
) {
423 struct btrfs_inode_item
*dst_item
;
424 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
425 btrfs_set_inode_size(path
->nodes
[0], dst_item
, saved_i_size
);
428 /* make sure the generation is filled in */
429 if (key
->type
== BTRFS_INODE_ITEM_KEY
) {
430 struct btrfs_inode_item
*dst_item
;
431 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
432 if (btrfs_inode_generation(path
->nodes
[0], dst_item
) == 0) {
433 btrfs_set_inode_generation(path
->nodes
[0], dst_item
,
438 if (overwrite_root
&&
439 key
->type
== BTRFS_EXTENT_DATA_KEY
) {
441 struct btrfs_file_extent_item
*fi
;
443 fi
= (struct btrfs_file_extent_item
*)dst_ptr
;
444 extent_type
= btrfs_file_extent_type(path
->nodes
[0], fi
);
445 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
446 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
447 struct btrfs_key ins
;
448 ins
.objectid
= btrfs_file_extent_disk_bytenr(
450 ins
.offset
= btrfs_file_extent_disk_num_bytes(
452 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
455 * is this extent already allocated in the extent
456 * allocation tree? If so, just add a reference
458 ret
= btrfs_lookup_extent(root
, ins
.objectid
,
461 ret
= btrfs_inc_extent_ref(trans
, root
,
462 ins
.objectid
, ins
.offset
,
463 path
->nodes
[0]->start
,
464 root
->root_key
.objectid
,
465 trans
->transid
, key
->objectid
);
468 * insert the extent pointer in the extent
471 ret
= btrfs_alloc_logged_extent(trans
, root
,
472 path
->nodes
[0]->start
,
473 root
->root_key
.objectid
,
474 trans
->transid
, key
->objectid
,
481 btrfs_mark_buffer_dirty(path
->nodes
[0]);
482 btrfs_release_path(root
, path
);
487 * simple helper to read an inode off the disk from a given root
488 * This can only be called for subvolume roots and not for the log
490 static noinline
struct inode
*read_one_inode(struct btrfs_root
*root
,
494 inode
= btrfs_iget_locked(root
->fs_info
->sb
, objectid
, root
);
495 if (inode
->i_state
& I_NEW
) {
496 BTRFS_I(inode
)->root
= root
;
497 BTRFS_I(inode
)->location
.objectid
= objectid
;
498 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
499 BTRFS_I(inode
)->location
.offset
= 0;
500 btrfs_read_locked_inode(inode
);
501 unlock_new_inode(inode
);
504 if (is_bad_inode(inode
)) {
511 /* replays a single extent in 'eb' at 'slot' with 'key' into the
512 * subvolume 'root'. path is released on entry and should be released
515 * extents in the log tree have not been allocated out of the extent
516 * tree yet. So, this completes the allocation, taking a reference
517 * as required if the extent already exists or creating a new extent
518 * if it isn't in the extent allocation tree yet.
520 * The extent is inserted into the file, dropping any existing extents
521 * from the file that overlap the new one.
523 static noinline
int replay_one_extent(struct btrfs_trans_handle
*trans
,
524 struct btrfs_root
*root
,
525 struct btrfs_path
*path
,
526 struct extent_buffer
*eb
, int slot
,
527 struct btrfs_key
*key
)
530 u64 mask
= root
->sectorsize
- 1;
533 u64 start
= key
->offset
;
534 struct btrfs_file_extent_item
*item
;
535 struct inode
*inode
= NULL
;
539 item
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
540 found_type
= btrfs_file_extent_type(eb
, item
);
542 if (found_type
== BTRFS_FILE_EXTENT_REG
||
543 found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
544 extent_end
= start
+ btrfs_file_extent_num_bytes(eb
, item
);
545 else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
546 size
= btrfs_file_extent_inline_len(eb
, item
);
547 extent_end
= (start
+ size
+ mask
) & ~mask
;
553 inode
= read_one_inode(root
, key
->objectid
);
560 * first check to see if we already have this extent in the
561 * file. This must be done before the btrfs_drop_extents run
562 * so we don't try to drop this extent.
564 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
568 (found_type
== BTRFS_FILE_EXTENT_REG
||
569 found_type
== BTRFS_FILE_EXTENT_PREALLOC
)) {
570 struct btrfs_file_extent_item cmp1
;
571 struct btrfs_file_extent_item cmp2
;
572 struct btrfs_file_extent_item
*existing
;
573 struct extent_buffer
*leaf
;
575 leaf
= path
->nodes
[0];
576 existing
= btrfs_item_ptr(leaf
, path
->slots
[0],
577 struct btrfs_file_extent_item
);
579 read_extent_buffer(eb
, &cmp1
, (unsigned long)item
,
581 read_extent_buffer(leaf
, &cmp2
, (unsigned long)existing
,
585 * we already have a pointer to this exact extent,
586 * we don't have to do anything
588 if (memcmp(&cmp1
, &cmp2
, sizeof(cmp1
)) == 0) {
589 btrfs_release_path(root
, path
);
593 btrfs_release_path(root
, path
);
595 /* drop any overlapping extents */
596 ret
= btrfs_drop_extents(trans
, root
, inode
,
597 start
, extent_end
, start
, &alloc_hint
);
600 /* insert the extent */
601 ret
= overwrite_item(trans
, root
, path
, eb
, slot
, key
);
604 /* btrfs_drop_extents changes i_bytes & i_blocks, update it here */
605 inode_add_bytes(inode
, extent_end
- start
);
606 btrfs_update_inode(trans
, root
, inode
);
614 * when cleaning up conflicts between the directory names in the
615 * subvolume, directory names in the log and directory names in the
616 * inode back references, we may have to unlink inodes from directories.
618 * This is a helper function to do the unlink of a specific directory
621 static noinline
int drop_one_dir_item(struct btrfs_trans_handle
*trans
,
622 struct btrfs_root
*root
,
623 struct btrfs_path
*path
,
625 struct btrfs_dir_item
*di
)
630 struct extent_buffer
*leaf
;
631 struct btrfs_key location
;
634 leaf
= path
->nodes
[0];
636 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
637 name_len
= btrfs_dir_name_len(leaf
, di
);
638 name
= kmalloc(name_len
, GFP_NOFS
);
639 read_extent_buffer(leaf
, name
, (unsigned long)(di
+ 1), name_len
);
640 btrfs_release_path(root
, path
);
642 inode
= read_one_inode(root
, location
.objectid
);
645 btrfs_inc_nlink(inode
);
646 ret
= btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
654 * helper function to see if a given name and sequence number found
655 * in an inode back reference are already in a directory and correctly
656 * point to this inode
658 static noinline
int inode_in_dir(struct btrfs_root
*root
,
659 struct btrfs_path
*path
,
660 u64 dirid
, u64 objectid
, u64 index
,
661 const char *name
, int name_len
)
663 struct btrfs_dir_item
*di
;
664 struct btrfs_key location
;
667 di
= btrfs_lookup_dir_index_item(NULL
, root
, path
, dirid
,
668 index
, name
, name_len
, 0);
669 if (di
&& !IS_ERR(di
)) {
670 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
671 if (location
.objectid
!= objectid
)
675 btrfs_release_path(root
, path
);
677 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dirid
, name
, name_len
, 0);
678 if (di
&& !IS_ERR(di
)) {
679 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
680 if (location
.objectid
!= objectid
)
686 btrfs_release_path(root
, path
);
691 * helper function to check a log tree for a named back reference in
692 * an inode. This is used to decide if a back reference that is
693 * found in the subvolume conflicts with what we find in the log.
695 * inode backreferences may have multiple refs in a single item,
696 * during replay we process one reference at a time, and we don't
697 * want to delete valid links to a file from the subvolume if that
698 * link is also in the log.
700 static noinline
int backref_in_log(struct btrfs_root
*log
,
701 struct btrfs_key
*key
,
702 char *name
, int namelen
)
704 struct btrfs_path
*path
;
705 struct btrfs_inode_ref
*ref
;
707 unsigned long ptr_end
;
708 unsigned long name_ptr
;
714 path
= btrfs_alloc_path();
715 ret
= btrfs_search_slot(NULL
, log
, key
, path
, 0, 0);
719 item_size
= btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]);
720 ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
721 ptr_end
= ptr
+ item_size
;
722 while (ptr
< ptr_end
) {
723 ref
= (struct btrfs_inode_ref
*)ptr
;
724 found_name_len
= btrfs_inode_ref_name_len(path
->nodes
[0], ref
);
725 if (found_name_len
== namelen
) {
726 name_ptr
= (unsigned long)(ref
+ 1);
727 ret
= memcmp_extent_buffer(path
->nodes
[0], name
,
734 ptr
= (unsigned long)(ref
+ 1) + found_name_len
;
737 btrfs_free_path(path
);
743 * replay one inode back reference item found in the log tree.
744 * eb, slot and key refer to the buffer and key found in the log tree.
745 * root is the destination we are replaying into, and path is for temp
746 * use by this function. (it should be released on return).
748 static noinline
int add_inode_ref(struct btrfs_trans_handle
*trans
,
749 struct btrfs_root
*root
,
750 struct btrfs_root
*log
,
751 struct btrfs_path
*path
,
752 struct extent_buffer
*eb
, int slot
,
753 struct btrfs_key
*key
)
757 struct btrfs_key location
;
758 struct btrfs_inode_ref
*ref
;
759 struct btrfs_dir_item
*di
;
763 unsigned long ref_ptr
;
764 unsigned long ref_end
;
766 location
.objectid
= key
->objectid
;
767 location
.type
= BTRFS_INODE_ITEM_KEY
;
771 * it is possible that we didn't log all the parent directories
772 * for a given inode. If we don't find the dir, just don't
773 * copy the back ref in. The link count fixup code will take
776 dir
= read_one_inode(root
, key
->offset
);
780 inode
= read_one_inode(root
, key
->objectid
);
783 ref_ptr
= btrfs_item_ptr_offset(eb
, slot
);
784 ref_end
= ref_ptr
+ btrfs_item_size_nr(eb
, slot
);
787 ref
= (struct btrfs_inode_ref
*)ref_ptr
;
789 namelen
= btrfs_inode_ref_name_len(eb
, ref
);
790 name
= kmalloc(namelen
, GFP_NOFS
);
793 read_extent_buffer(eb
, name
, (unsigned long)(ref
+ 1), namelen
);
795 /* if we already have a perfect match, we're done */
796 if (inode_in_dir(root
, path
, dir
->i_ino
, inode
->i_ino
,
797 btrfs_inode_ref_index(eb
, ref
),
803 * look for a conflicting back reference in the metadata.
804 * if we find one we have to unlink that name of the file
805 * before we add our new link. Later on, we overwrite any
806 * existing back reference, and we don't want to create
807 * dangling pointers in the directory.
810 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
814 struct btrfs_inode_ref
*victim_ref
;
816 unsigned long ptr_end
;
817 struct extent_buffer
*leaf
= path
->nodes
[0];
819 /* are we trying to overwrite a back ref for the root directory
820 * if so, just jump out, we're done
822 if (key
->objectid
== key
->offset
)
825 /* check all the names in this back reference to see
826 * if they are in the log. if so, we allow them to stay
827 * otherwise they must be unlinked as a conflict
829 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
830 ptr_end
= ptr
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
831 while(ptr
< ptr_end
) {
832 victim_ref
= (struct btrfs_inode_ref
*)ptr
;
833 victim_name_len
= btrfs_inode_ref_name_len(leaf
,
835 victim_name
= kmalloc(victim_name_len
, GFP_NOFS
);
836 BUG_ON(!victim_name
);
838 read_extent_buffer(leaf
, victim_name
,
839 (unsigned long)(victim_ref
+ 1),
842 if (!backref_in_log(log
, key
, victim_name
,
844 btrfs_inc_nlink(inode
);
845 btrfs_release_path(root
, path
);
846 ret
= btrfs_unlink_inode(trans
, root
, dir
,
850 btrfs_release_path(root
, path
);
854 ptr
= (unsigned long)(victim_ref
+ 1) + victim_name_len
;
858 btrfs_release_path(root
, path
);
860 /* look for a conflicting sequence number */
861 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir
->i_ino
,
862 btrfs_inode_ref_index(eb
, ref
),
864 if (di
&& !IS_ERR(di
)) {
865 ret
= drop_one_dir_item(trans
, root
, path
, dir
, di
);
868 btrfs_release_path(root
, path
);
871 /* look for a conflicting name */
872 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir
->i_ino
,
874 if (di
&& !IS_ERR(di
)) {
875 ret
= drop_one_dir_item(trans
, root
, path
, dir
, di
);
878 btrfs_release_path(root
, path
);
880 /* insert our name */
881 ret
= btrfs_add_link(trans
, dir
, inode
, name
, namelen
, 0,
882 btrfs_inode_ref_index(eb
, ref
));
885 btrfs_update_inode(trans
, root
, inode
);
888 ref_ptr
= (unsigned long)(ref
+ 1) + namelen
;
890 if (ref_ptr
< ref_end
)
893 /* finally write the back reference in the inode */
894 ret
= overwrite_item(trans
, root
, path
, eb
, slot
, key
);
898 btrfs_release_path(root
, path
);
905 * replay one csum item from the log tree into the subvolume 'root'
906 * eb, slot and key all refer to the log tree
907 * path is for temp use by this function and should be released on return
909 * This copies the checksums out of the log tree and inserts them into
910 * the subvolume. Any existing checksums for this range in the file
911 * are overwritten, and new items are added where required.
913 * We keep this simple by reusing the btrfs_ordered_sum code from
914 * the data=ordered mode. This basically means making a copy
915 * of all the checksums in ram, which we have to do anyway for kmap
918 * The copy is then sent down to btrfs_csum_file_blocks, which
919 * does all the hard work of finding existing items in the file
920 * or adding new ones.
922 static noinline
int replay_one_csum(struct btrfs_trans_handle
*trans
,
923 struct btrfs_root
*root
,
924 struct btrfs_path
*path
,
925 struct extent_buffer
*eb
, int slot
,
926 struct btrfs_key
*key
)
929 u32 item_size
= btrfs_item_size_nr(eb
, slot
);
931 unsigned long file_bytes
;
932 struct btrfs_ordered_sum
*sums
;
933 struct btrfs_sector_sum
*sector_sum
;
937 file_bytes
= (item_size
/ BTRFS_CRC32_SIZE
) * root
->sectorsize
;
938 inode
= read_one_inode(root
, key
->objectid
);
943 sums
= kzalloc(btrfs_ordered_sum_size(root
, file_bytes
), GFP_NOFS
);
949 INIT_LIST_HEAD(&sums
->list
);
950 sums
->len
= file_bytes
;
951 sums
->file_offset
= key
->offset
;
954 * copy all the sums into the ordered sum struct
956 sector_sum
= sums
->sums
;
957 cur_offset
= key
->offset
;
958 ptr
= btrfs_item_ptr_offset(eb
, slot
);
959 while(item_size
> 0) {
960 sector_sum
->offset
= cur_offset
;
961 read_extent_buffer(eb
, §or_sum
->sum
, ptr
, BTRFS_CRC32_SIZE
);
963 item_size
-= BTRFS_CRC32_SIZE
;
964 ptr
+= BTRFS_CRC32_SIZE
;
965 cur_offset
+= root
->sectorsize
;
968 /* let btrfs_csum_file_blocks add them into the file */
969 ret
= btrfs_csum_file_blocks(trans
, root
, inode
, sums
);
977 * There are a few corners where the link count of the file can't
978 * be properly maintained during replay. So, instead of adding
979 * lots of complexity to the log code, we just scan the backrefs
980 * for any file that has been through replay.
982 * The scan will update the link count on the inode to reflect the
983 * number of back refs found. If it goes down to zero, the iput
984 * will free the inode.
986 static noinline
int fixup_inode_link_count(struct btrfs_trans_handle
*trans
,
987 struct btrfs_root
*root
,
990 struct btrfs_path
*path
;
992 struct btrfs_key key
;
995 unsigned long ptr_end
;
998 key
.objectid
= inode
->i_ino
;
999 key
.type
= BTRFS_INODE_REF_KEY
;
1000 key
.offset
= (u64
)-1;
1002 path
= btrfs_alloc_path();
1005 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1009 if (path
->slots
[0] == 0)
1013 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
1015 if (key
.objectid
!= inode
->i_ino
||
1016 key
.type
!= BTRFS_INODE_REF_KEY
)
1018 ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
1019 ptr_end
= ptr
+ btrfs_item_size_nr(path
->nodes
[0],
1021 while(ptr
< ptr_end
) {
1022 struct btrfs_inode_ref
*ref
;
1024 ref
= (struct btrfs_inode_ref
*)ptr
;
1025 name_len
= btrfs_inode_ref_name_len(path
->nodes
[0],
1027 ptr
= (unsigned long)(ref
+ 1) + name_len
;
1031 if (key
.offset
== 0)
1034 btrfs_release_path(root
, path
);
1036 btrfs_free_path(path
);
1037 if (nlink
!= inode
->i_nlink
) {
1038 inode
->i_nlink
= nlink
;
1039 btrfs_update_inode(trans
, root
, inode
);
1041 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1046 static noinline
int fixup_inode_link_counts(struct btrfs_trans_handle
*trans
,
1047 struct btrfs_root
*root
,
1048 struct btrfs_path
*path
)
1051 struct btrfs_key key
;
1052 struct inode
*inode
;
1054 key
.objectid
= BTRFS_TREE_LOG_FIXUP_OBJECTID
;
1055 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
1056 key
.offset
= (u64
)-1;
1058 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1063 if (path
->slots
[0] == 0)
1068 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1069 if (key
.objectid
!= BTRFS_TREE_LOG_FIXUP_OBJECTID
||
1070 key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
1073 ret
= btrfs_del_item(trans
, root
, path
);
1076 btrfs_release_path(root
, path
);
1077 inode
= read_one_inode(root
, key
.offset
);
1080 ret
= fixup_inode_link_count(trans
, root
, inode
);
1085 if (key
.offset
== 0)
1089 btrfs_release_path(root
, path
);
1095 * record a given inode in the fixup dir so we can check its link
1096 * count when replay is done. The link count is incremented here
1097 * so the inode won't go away until we check it
1099 static noinline
int link_to_fixup_dir(struct btrfs_trans_handle
*trans
,
1100 struct btrfs_root
*root
,
1101 struct btrfs_path
*path
,
1104 struct btrfs_key key
;
1106 struct inode
*inode
;
1108 inode
= read_one_inode(root
, objectid
);
1111 key
.objectid
= BTRFS_TREE_LOG_FIXUP_OBJECTID
;
1112 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
1113 key
.offset
= objectid
;
1115 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1117 btrfs_release_path(root
, path
);
1119 btrfs_inc_nlink(inode
);
1120 btrfs_update_inode(trans
, root
, inode
);
1121 } else if (ret
== -EEXIST
) {
1132 * when replaying the log for a directory, we only insert names
1133 * for inodes that actually exist. This means an fsync on a directory
1134 * does not implicitly fsync all the new files in it
1136 static noinline
int insert_one_name(struct btrfs_trans_handle
*trans
,
1137 struct btrfs_root
*root
,
1138 struct btrfs_path
*path
,
1139 u64 dirid
, u64 index
,
1140 char *name
, int name_len
, u8 type
,
1141 struct btrfs_key
*location
)
1143 struct inode
*inode
;
1147 inode
= read_one_inode(root
, location
->objectid
);
1151 dir
= read_one_inode(root
, dirid
);
1156 ret
= btrfs_add_link(trans
, dir
, inode
, name
, name_len
, 1, index
);
1158 /* FIXME, put inode into FIXUP list */
1166 * take a single entry in a log directory item and replay it into
1169 * if a conflicting item exists in the subdirectory already,
1170 * the inode it points to is unlinked and put into the link count
1173 * If a name from the log points to a file or directory that does
1174 * not exist in the FS, it is skipped. fsyncs on directories
1175 * do not force down inodes inside that directory, just changes to the
1176 * names or unlinks in a directory.
1178 static noinline
int replay_one_name(struct btrfs_trans_handle
*trans
,
1179 struct btrfs_root
*root
,
1180 struct btrfs_path
*path
,
1181 struct extent_buffer
*eb
,
1182 struct btrfs_dir_item
*di
,
1183 struct btrfs_key
*key
)
1187 struct btrfs_dir_item
*dst_di
;
1188 struct btrfs_key found_key
;
1189 struct btrfs_key log_key
;
1195 dir
= read_one_inode(root
, key
->objectid
);
1198 name_len
= btrfs_dir_name_len(eb
, di
);
1199 name
= kmalloc(name_len
, GFP_NOFS
);
1200 log_type
= btrfs_dir_type(eb
, di
);
1201 read_extent_buffer(eb
, name
, (unsigned long)(di
+ 1),
1204 btrfs_dir_item_key_to_cpu(eb
, di
, &log_key
);
1205 exists
= btrfs_lookup_inode(trans
, root
, path
, &log_key
, 0);
1210 btrfs_release_path(root
, path
);
1212 if (key
->type
== BTRFS_DIR_ITEM_KEY
) {
1213 dst_di
= btrfs_lookup_dir_item(trans
, root
, path
, key
->objectid
,
1216 else if (key
->type
== BTRFS_DIR_INDEX_KEY
) {
1217 dst_di
= btrfs_lookup_dir_index_item(trans
, root
, path
,
1224 if (!dst_di
|| IS_ERR(dst_di
)) {
1225 /* we need a sequence number to insert, so we only
1226 * do inserts for the BTRFS_DIR_INDEX_KEY types
1228 if (key
->type
!= BTRFS_DIR_INDEX_KEY
)
1233 btrfs_dir_item_key_to_cpu(path
->nodes
[0], dst_di
, &found_key
);
1234 /* the existing item matches the logged item */
1235 if (found_key
.objectid
== log_key
.objectid
&&
1236 found_key
.type
== log_key
.type
&&
1237 found_key
.offset
== log_key
.offset
&&
1238 btrfs_dir_type(path
->nodes
[0], dst_di
) == log_type
) {
1243 * don't drop the conflicting directory entry if the inode
1244 * for the new entry doesn't exist
1249 ret
= drop_one_dir_item(trans
, root
, path
, dir
, dst_di
);
1252 if (key
->type
== BTRFS_DIR_INDEX_KEY
)
1255 btrfs_release_path(root
, path
);
1261 btrfs_release_path(root
, path
);
1262 ret
= insert_one_name(trans
, root
, path
, key
->objectid
, key
->offset
,
1263 name
, name_len
, log_type
, &log_key
);
1265 if (ret
&& ret
!= -ENOENT
)
1271 * find all the names in a directory item and reconcile them into
1272 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1273 * one name in a directory item, but the same code gets used for
1274 * both directory index types
1276 static noinline
int replay_one_dir_item(struct btrfs_trans_handle
*trans
,
1277 struct btrfs_root
*root
,
1278 struct btrfs_path
*path
,
1279 struct extent_buffer
*eb
, int slot
,
1280 struct btrfs_key
*key
)
1283 u32 item_size
= btrfs_item_size_nr(eb
, slot
);
1284 struct btrfs_dir_item
*di
;
1287 unsigned long ptr_end
;
1289 ptr
= btrfs_item_ptr_offset(eb
, slot
);
1290 ptr_end
= ptr
+ item_size
;
1291 while(ptr
< ptr_end
) {
1292 di
= (struct btrfs_dir_item
*)ptr
;
1293 name_len
= btrfs_dir_name_len(eb
, di
);
1294 ret
= replay_one_name(trans
, root
, path
, eb
, di
, key
);
1296 ptr
= (unsigned long)(di
+ 1);
1303 * directory replay has two parts. There are the standard directory
1304 * items in the log copied from the subvolume, and range items
1305 * created in the log while the subvolume was logged.
1307 * The range items tell us which parts of the key space the log
1308 * is authoritative for. During replay, if a key in the subvolume
1309 * directory is in a logged range item, but not actually in the log
1310 * that means it was deleted from the directory before the fsync
1311 * and should be removed.
1313 static noinline
int find_dir_range(struct btrfs_root
*root
,
1314 struct btrfs_path
*path
,
1315 u64 dirid
, int key_type
,
1316 u64
*start_ret
, u64
*end_ret
)
1318 struct btrfs_key key
;
1320 struct btrfs_dir_log_item
*item
;
1324 if (*start_ret
== (u64
)-1)
1327 key
.objectid
= dirid
;
1328 key
.type
= key_type
;
1329 key
.offset
= *start_ret
;
1331 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1335 if (path
->slots
[0] == 0)
1340 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1342 if (key
.type
!= key_type
|| key
.objectid
!= dirid
) {
1346 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1347 struct btrfs_dir_log_item
);
1348 found_end
= btrfs_dir_log_end(path
->nodes
[0], item
);
1350 if (*start_ret
>= key
.offset
&& *start_ret
<= found_end
) {
1352 *start_ret
= key
.offset
;
1353 *end_ret
= found_end
;
1358 /* check the next slot in the tree to see if it is a valid item */
1359 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1360 if (path
->slots
[0] >= nritems
) {
1361 ret
= btrfs_next_leaf(root
, path
);
1368 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1370 if (key
.type
!= key_type
|| key
.objectid
!= dirid
) {
1374 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1375 struct btrfs_dir_log_item
);
1376 found_end
= btrfs_dir_log_end(path
->nodes
[0], item
);
1377 *start_ret
= key
.offset
;
1378 *end_ret
= found_end
;
1381 btrfs_release_path(root
, path
);
1386 * this looks for a given directory item in the log. If the directory
1387 * item is not in the log, the item is removed and the inode it points
1390 static noinline
int check_item_in_log(struct btrfs_trans_handle
*trans
,
1391 struct btrfs_root
*root
,
1392 struct btrfs_root
*log
,
1393 struct btrfs_path
*path
,
1394 struct btrfs_path
*log_path
,
1396 struct btrfs_key
*dir_key
)
1399 struct extent_buffer
*eb
;
1402 struct btrfs_dir_item
*di
;
1403 struct btrfs_dir_item
*log_di
;
1406 unsigned long ptr_end
;
1408 struct inode
*inode
;
1409 struct btrfs_key location
;
1412 eb
= path
->nodes
[0];
1413 slot
= path
->slots
[0];
1414 item_size
= btrfs_item_size_nr(eb
, slot
);
1415 ptr
= btrfs_item_ptr_offset(eb
, slot
);
1416 ptr_end
= ptr
+ item_size
;
1417 while(ptr
< ptr_end
) {
1418 di
= (struct btrfs_dir_item
*)ptr
;
1419 name_len
= btrfs_dir_name_len(eb
, di
);
1420 name
= kmalloc(name_len
, GFP_NOFS
);
1425 read_extent_buffer(eb
, name
, (unsigned long)(di
+ 1),
1428 if (dir_key
->type
== BTRFS_DIR_ITEM_KEY
) {
1429 log_di
= btrfs_lookup_dir_item(trans
, log
, log_path
,
1432 } else if (dir_key
->type
== BTRFS_DIR_INDEX_KEY
) {
1433 log_di
= btrfs_lookup_dir_index_item(trans
, log
,
1439 if (!log_di
|| IS_ERR(log_di
)) {
1440 btrfs_dir_item_key_to_cpu(eb
, di
, &location
);
1441 btrfs_release_path(root
, path
);
1442 btrfs_release_path(log
, log_path
);
1443 inode
= read_one_inode(root
, location
.objectid
);
1446 ret
= link_to_fixup_dir(trans
, root
,
1447 path
, location
.objectid
);
1449 btrfs_inc_nlink(inode
);
1450 ret
= btrfs_unlink_inode(trans
, root
, dir
, inode
,
1456 /* there might still be more names under this key
1457 * check and repeat if required
1459 ret
= btrfs_search_slot(NULL
, root
, dir_key
, path
,
1466 btrfs_release_path(log
, log_path
);
1469 ptr
= (unsigned long)(di
+ 1);
1474 btrfs_release_path(root
, path
);
1475 btrfs_release_path(log
, log_path
);
1480 * deletion replay happens before we copy any new directory items
1481 * out of the log or out of backreferences from inodes. It
1482 * scans the log to find ranges of keys that log is authoritative for,
1483 * and then scans the directory to find items in those ranges that are
1484 * not present in the log.
1486 * Anything we don't find in the log is unlinked and removed from the
1489 static noinline
int replay_dir_deletes(struct btrfs_trans_handle
*trans
,
1490 struct btrfs_root
*root
,
1491 struct btrfs_root
*log
,
1492 struct btrfs_path
*path
,
1497 int key_type
= BTRFS_DIR_LOG_ITEM_KEY
;
1499 struct btrfs_key dir_key
;
1500 struct btrfs_key found_key
;
1501 struct btrfs_path
*log_path
;
1504 dir_key
.objectid
= dirid
;
1505 dir_key
.type
= BTRFS_DIR_ITEM_KEY
;
1506 log_path
= btrfs_alloc_path();
1510 dir
= read_one_inode(root
, dirid
);
1511 /* it isn't an error if the inode isn't there, that can happen
1512 * because we replay the deletes before we copy in the inode item
1516 btrfs_free_path(log_path
);
1523 ret
= find_dir_range(log
, path
, dirid
, key_type
,
1524 &range_start
, &range_end
);
1528 dir_key
.offset
= range_start
;
1531 ret
= btrfs_search_slot(NULL
, root
, &dir_key
, path
,
1536 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1537 if (path
->slots
[0] >= nritems
) {
1538 ret
= btrfs_next_leaf(root
, path
);
1542 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1544 if (found_key
.objectid
!= dirid
||
1545 found_key
.type
!= dir_key
.type
)
1548 if (found_key
.offset
> range_end
)
1551 ret
= check_item_in_log(trans
, root
, log
, path
,
1552 log_path
, dir
, &found_key
);
1554 if (found_key
.offset
== (u64
)-1)
1556 dir_key
.offset
= found_key
.offset
+ 1;
1558 btrfs_release_path(root
, path
);
1559 if (range_end
== (u64
)-1)
1561 range_start
= range_end
+ 1;
1566 if (key_type
== BTRFS_DIR_LOG_ITEM_KEY
) {
1567 key_type
= BTRFS_DIR_LOG_INDEX_KEY
;
1568 dir_key
.type
= BTRFS_DIR_INDEX_KEY
;
1569 btrfs_release_path(root
, path
);
1573 btrfs_release_path(root
, path
);
1574 btrfs_free_path(log_path
);
1580 * the process_func used to replay items from the log tree. This
1581 * gets called in two different stages. The first stage just looks
1582 * for inodes and makes sure they are all copied into the subvolume.
1584 * The second stage copies all the other item types from the log into
1585 * the subvolume. The two stage approach is slower, but gets rid of
1586 * lots of complexity around inodes referencing other inodes that exist
1587 * only in the log (references come from either directory items or inode
1590 static int replay_one_buffer(struct btrfs_root
*log
, struct extent_buffer
*eb
,
1591 struct walk_control
*wc
, u64 gen
)
1594 struct btrfs_path
*path
;
1595 struct btrfs_root
*root
= wc
->replay_dest
;
1596 struct btrfs_key key
;
1602 btrfs_read_buffer(eb
, gen
);
1604 level
= btrfs_header_level(eb
);
1609 path
= btrfs_alloc_path();
1612 nritems
= btrfs_header_nritems(eb
);
1613 for (i
= 0; i
< nritems
; i
++) {
1614 btrfs_item_key_to_cpu(eb
, &key
, i
);
1615 item_size
= btrfs_item_size_nr(eb
, i
);
1617 /* inode keys are done during the first stage */
1618 if (key
.type
== BTRFS_INODE_ITEM_KEY
&&
1619 wc
->stage
== LOG_WALK_REPLAY_INODES
) {
1620 struct inode
*inode
;
1621 struct btrfs_inode_item
*inode_item
;
1624 inode_item
= btrfs_item_ptr(eb
, i
,
1625 struct btrfs_inode_item
);
1626 mode
= btrfs_inode_mode(eb
, inode_item
);
1627 if (S_ISDIR(mode
)) {
1628 ret
= replay_dir_deletes(wc
->trans
,
1629 root
, log
, path
, key
.objectid
);
1632 ret
= overwrite_item(wc
->trans
, root
, path
,
1636 /* for regular files, truncate away
1637 * extents past the new EOF
1639 if (S_ISREG(mode
)) {
1640 inode
= read_one_inode(root
,
1644 ret
= btrfs_truncate_inode_items(wc
->trans
,
1645 root
, inode
, inode
->i_size
,
1646 BTRFS_EXTENT_DATA_KEY
);
1650 ret
= link_to_fixup_dir(wc
->trans
, root
,
1651 path
, key
.objectid
);
1654 if (wc
->stage
< LOG_WALK_REPLAY_ALL
)
1657 /* these keys are simply copied */
1658 if (key
.type
== BTRFS_XATTR_ITEM_KEY
) {
1659 ret
= overwrite_item(wc
->trans
, root
, path
,
1662 } else if (key
.type
== BTRFS_INODE_REF_KEY
) {
1663 ret
= add_inode_ref(wc
->trans
, root
, log
, path
,
1665 BUG_ON(ret
&& ret
!= -ENOENT
);
1666 } else if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
1667 ret
= replay_one_extent(wc
->trans
, root
, path
,
1670 } else if (key
.type
== BTRFS_CSUM_ITEM_KEY
) {
1671 ret
= replay_one_csum(wc
->trans
, root
, path
,
1674 } else if (key
.type
== BTRFS_DIR_ITEM_KEY
||
1675 key
.type
== BTRFS_DIR_INDEX_KEY
) {
1676 ret
= replay_one_dir_item(wc
->trans
, root
, path
,
1681 btrfs_free_path(path
);
1685 static int noinline
walk_down_log_tree(struct btrfs_trans_handle
*trans
,
1686 struct btrfs_root
*root
,
1687 struct btrfs_path
*path
, int *level
,
1688 struct walk_control
*wc
)
1694 struct extent_buffer
*next
;
1695 struct extent_buffer
*cur
;
1696 struct extent_buffer
*parent
;
1700 WARN_ON(*level
< 0);
1701 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1704 WARN_ON(*level
< 0);
1705 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1706 cur
= path
->nodes
[*level
];
1708 if (btrfs_header_level(cur
) != *level
)
1711 if (path
->slots
[*level
] >=
1712 btrfs_header_nritems(cur
))
1715 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
1716 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
1717 blocksize
= btrfs_level_size(root
, *level
- 1);
1719 parent
= path
->nodes
[*level
];
1720 root_owner
= btrfs_header_owner(parent
);
1721 root_gen
= btrfs_header_generation(parent
);
1723 next
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
1725 wc
->process_func(root
, next
, wc
, ptr_gen
);
1728 path
->slots
[*level
]++;
1730 btrfs_read_buffer(next
, ptr_gen
);
1732 btrfs_tree_lock(next
);
1733 clean_tree_block(trans
, root
, next
);
1734 btrfs_wait_tree_block_writeback(next
);
1735 btrfs_tree_unlock(next
);
1737 ret
= btrfs_drop_leaf_ref(trans
, root
, next
);
1740 WARN_ON(root_owner
!=
1741 BTRFS_TREE_LOG_OBJECTID
);
1742 ret
= btrfs_free_reserved_extent(root
,
1746 free_extent_buffer(next
);
1749 btrfs_read_buffer(next
, ptr_gen
);
1751 WARN_ON(*level
<= 0);
1752 if (path
->nodes
[*level
-1])
1753 free_extent_buffer(path
->nodes
[*level
-1]);
1754 path
->nodes
[*level
-1] = next
;
1755 *level
= btrfs_header_level(next
);
1756 path
->slots
[*level
] = 0;
1759 WARN_ON(*level
< 0);
1760 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1762 if (path
->nodes
[*level
] == root
->node
) {
1763 parent
= path
->nodes
[*level
];
1765 parent
= path
->nodes
[*level
+ 1];
1767 bytenr
= path
->nodes
[*level
]->start
;
1769 blocksize
= btrfs_level_size(root
, *level
);
1770 root_owner
= btrfs_header_owner(parent
);
1771 root_gen
= btrfs_header_generation(parent
);
1773 wc
->process_func(root
, path
->nodes
[*level
], wc
,
1774 btrfs_header_generation(path
->nodes
[*level
]));
1777 next
= path
->nodes
[*level
];
1778 btrfs_tree_lock(next
);
1779 clean_tree_block(trans
, root
, next
);
1780 btrfs_wait_tree_block_writeback(next
);
1781 btrfs_tree_unlock(next
);
1784 ret
= btrfs_drop_leaf_ref(trans
, root
, next
);
1787 WARN_ON(root_owner
!= BTRFS_TREE_LOG_OBJECTID
);
1788 ret
= btrfs_free_reserved_extent(root
, bytenr
, blocksize
);
1791 free_extent_buffer(path
->nodes
[*level
]);
1792 path
->nodes
[*level
] = NULL
;
1799 static int noinline
walk_up_log_tree(struct btrfs_trans_handle
*trans
,
1800 struct btrfs_root
*root
,
1801 struct btrfs_path
*path
, int *level
,
1802 struct walk_control
*wc
)
1810 for(i
= *level
; i
< BTRFS_MAX_LEVEL
- 1 && path
->nodes
[i
]; i
++) {
1811 slot
= path
->slots
[i
];
1812 if (slot
< btrfs_header_nritems(path
->nodes
[i
]) - 1) {
1813 struct extent_buffer
*node
;
1814 node
= path
->nodes
[i
];
1817 WARN_ON(*level
== 0);
1820 struct extent_buffer
*parent
;
1821 if (path
->nodes
[*level
] == root
->node
)
1822 parent
= path
->nodes
[*level
];
1824 parent
= path
->nodes
[*level
+ 1];
1826 root_owner
= btrfs_header_owner(parent
);
1827 root_gen
= btrfs_header_generation(parent
);
1828 wc
->process_func(root
, path
->nodes
[*level
], wc
,
1829 btrfs_header_generation(path
->nodes
[*level
]));
1831 struct extent_buffer
*next
;
1833 next
= path
->nodes
[*level
];
1835 btrfs_tree_lock(next
);
1836 clean_tree_block(trans
, root
, next
);
1837 btrfs_wait_tree_block_writeback(next
);
1838 btrfs_tree_unlock(next
);
1841 ret
= btrfs_drop_leaf_ref(trans
, root
,
1846 WARN_ON(root_owner
!= BTRFS_TREE_LOG_OBJECTID
);
1847 ret
= btrfs_free_reserved_extent(root
,
1848 path
->nodes
[*level
]->start
,
1849 path
->nodes
[*level
]->len
);
1852 free_extent_buffer(path
->nodes
[*level
]);
1853 path
->nodes
[*level
] = NULL
;
1861 * drop the reference count on the tree rooted at 'snap'. This traverses
1862 * the tree freeing any blocks that have a ref count of zero after being
1865 static int walk_log_tree(struct btrfs_trans_handle
*trans
,
1866 struct btrfs_root
*log
, struct walk_control
*wc
)
1871 struct btrfs_path
*path
;
1875 path
= btrfs_alloc_path();
1878 level
= btrfs_header_level(log
->node
);
1880 path
->nodes
[level
] = log
->node
;
1881 extent_buffer_get(log
->node
);
1882 path
->slots
[level
] = 0;
1885 wret
= walk_down_log_tree(trans
, log
, path
, &level
, wc
);
1891 wret
= walk_up_log_tree(trans
, log
, path
, &level
, wc
);
1898 /* was the root node processed? if not, catch it here */
1899 if (path
->nodes
[orig_level
]) {
1900 wc
->process_func(log
, path
->nodes
[orig_level
], wc
,
1901 btrfs_header_generation(path
->nodes
[orig_level
]));
1903 struct extent_buffer
*next
;
1905 next
= path
->nodes
[orig_level
];
1907 btrfs_tree_lock(next
);
1908 clean_tree_block(trans
, log
, next
);
1909 btrfs_wait_tree_block_writeback(next
);
1910 btrfs_tree_unlock(next
);
1912 if (orig_level
== 0) {
1913 ret
= btrfs_drop_leaf_ref(trans
, log
,
1917 WARN_ON(log
->root_key
.objectid
!=
1918 BTRFS_TREE_LOG_OBJECTID
);
1919 ret
= btrfs_free_reserved_extent(log
, next
->start
,
1925 for (i
= 0; i
<= orig_level
; i
++) {
1926 if (path
->nodes
[i
]) {
1927 free_extent_buffer(path
->nodes
[i
]);
1928 path
->nodes
[i
] = NULL
;
1931 btrfs_free_path(path
);
1933 free_extent_buffer(log
->node
);
1937 int wait_log_commit(struct btrfs_root
*log
)
1940 u64 transid
= log
->fs_info
->tree_log_transid
;
1943 prepare_to_wait(&log
->fs_info
->tree_log_wait
, &wait
,
1944 TASK_UNINTERRUPTIBLE
);
1945 mutex_unlock(&log
->fs_info
->tree_log_mutex
);
1946 if (atomic_read(&log
->fs_info
->tree_log_commit
))
1948 finish_wait(&log
->fs_info
->tree_log_wait
, &wait
);
1949 mutex_lock(&log
->fs_info
->tree_log_mutex
);
1950 } while(transid
== log
->fs_info
->tree_log_transid
&&
1951 atomic_read(&log
->fs_info
->tree_log_commit
));
1956 * btrfs_sync_log does sends a given tree log down to the disk and
1957 * updates the super blocks to record it. When this call is done,
1958 * you know that any inodes previously logged are safely on disk
1960 int btrfs_sync_log(struct btrfs_trans_handle
*trans
,
1961 struct btrfs_root
*root
)
1964 unsigned long batch
;
1965 struct btrfs_root
*log
= root
->log_root
;
1967 mutex_lock(&log
->fs_info
->tree_log_mutex
);
1968 if (atomic_read(&log
->fs_info
->tree_log_commit
)) {
1969 wait_log_commit(log
);
1972 atomic_set(&log
->fs_info
->tree_log_commit
, 1);
1975 batch
= log
->fs_info
->tree_log_batch
;
1976 mutex_unlock(&log
->fs_info
->tree_log_mutex
);
1977 schedule_timeout_uninterruptible(1);
1978 mutex_lock(&log
->fs_info
->tree_log_mutex
);
1980 while(atomic_read(&log
->fs_info
->tree_log_writers
)) {
1982 prepare_to_wait(&log
->fs_info
->tree_log_wait
, &wait
,
1983 TASK_UNINTERRUPTIBLE
);
1984 mutex_unlock(&log
->fs_info
->tree_log_mutex
);
1985 if (atomic_read(&log
->fs_info
->tree_log_writers
))
1987 mutex_lock(&log
->fs_info
->tree_log_mutex
);
1988 finish_wait(&log
->fs_info
->tree_log_wait
, &wait
);
1990 if (batch
== log
->fs_info
->tree_log_batch
)
1994 ret
= btrfs_write_and_wait_marked_extents(log
, &log
->dirty_log_pages
);
1996 ret
= btrfs_write_and_wait_marked_extents(root
->fs_info
->log_root_tree
,
1997 &root
->fs_info
->log_root_tree
->dirty_log_pages
);
2000 btrfs_set_super_log_root(&root
->fs_info
->super_for_commit
,
2001 log
->fs_info
->log_root_tree
->node
->start
);
2002 btrfs_set_super_log_root_level(&root
->fs_info
->super_for_commit
,
2003 btrfs_header_level(log
->fs_info
->log_root_tree
->node
));
2005 write_ctree_super(trans
, log
->fs_info
->tree_root
);
2006 log
->fs_info
->tree_log_transid
++;
2007 log
->fs_info
->tree_log_batch
= 0;
2008 atomic_set(&log
->fs_info
->tree_log_commit
, 0);
2010 if (waitqueue_active(&log
->fs_info
->tree_log_wait
))
2011 wake_up(&log
->fs_info
->tree_log_wait
);
2013 mutex_unlock(&log
->fs_info
->tree_log_mutex
);
2018 /* * free all the extents used by the tree log. This should be called
2019 * at commit time of the full transaction
2021 int btrfs_free_log(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
)
2024 struct btrfs_root
*log
;
2028 struct walk_control wc
= {
2030 .process_func
= process_one_buffer
2033 if (!root
->log_root
)
2036 log
= root
->log_root
;
2037 ret
= walk_log_tree(trans
, log
, &wc
);
2041 ret
= find_first_extent_bit(&log
->dirty_log_pages
,
2042 0, &start
, &end
, EXTENT_DIRTY
);
2046 clear_extent_dirty(&log
->dirty_log_pages
,
2047 start
, end
, GFP_NOFS
);
2050 log
= root
->log_root
;
2051 ret
= btrfs_del_root(trans
, root
->fs_info
->log_root_tree
,
2054 root
->log_root
= NULL
;
2055 kfree(root
->log_root
);
2060 * helper function to update the item for a given subvolumes log root
2061 * in the tree of log roots
2063 static int update_log_root(struct btrfs_trans_handle
*trans
,
2064 struct btrfs_root
*log
)
2066 u64 bytenr
= btrfs_root_bytenr(&log
->root_item
);
2069 if (log
->node
->start
== bytenr
)
2072 btrfs_set_root_bytenr(&log
->root_item
, log
->node
->start
);
2073 btrfs_set_root_generation(&log
->root_item
, trans
->transid
);
2074 btrfs_set_root_level(&log
->root_item
, btrfs_header_level(log
->node
));
2075 ret
= btrfs_update_root(trans
, log
->fs_info
->log_root_tree
,
2076 &log
->root_key
, &log
->root_item
);
2082 * If both a file and directory are logged, and unlinks or renames are
2083 * mixed in, we have a few interesting corners:
2085 * create file X in dir Y
2086 * link file X to X.link in dir Y
2088 * unlink file X but leave X.link
2091 * After a crash we would expect only X.link to exist. But file X
2092 * didn't get fsync'd again so the log has back refs for X and X.link.
2094 * We solve this by removing directory entries and inode backrefs from the
2095 * log when a file that was logged in the current transaction is
2096 * unlinked. Any later fsync will include the updated log entries, and
2097 * we'll be able to reconstruct the proper directory items from backrefs.
2099 * This optimizations allows us to avoid relogging the entire inode
2100 * or the entire directory.
2102 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle
*trans
,
2103 struct btrfs_root
*root
,
2104 const char *name
, int name_len
,
2105 struct inode
*dir
, u64 index
)
2107 struct btrfs_root
*log
;
2108 struct btrfs_dir_item
*di
;
2109 struct btrfs_path
*path
;
2113 if (BTRFS_I(dir
)->logged_trans
< trans
->transid
)
2116 ret
= join_running_log_trans(root
);
2120 mutex_lock(&BTRFS_I(dir
)->log_mutex
);
2122 log
= root
->log_root
;
2123 path
= btrfs_alloc_path();
2124 di
= btrfs_lookup_dir_item(trans
, log
, path
, dir
->i_ino
,
2125 name
, name_len
, -1);
2126 if (di
&& !IS_ERR(di
)) {
2127 ret
= btrfs_delete_one_dir_name(trans
, log
, path
, di
);
2128 bytes_del
+= name_len
;
2131 btrfs_release_path(log
, path
);
2132 di
= btrfs_lookup_dir_index_item(trans
, log
, path
, dir
->i_ino
,
2133 index
, name
, name_len
, -1);
2134 if (di
&& !IS_ERR(di
)) {
2135 ret
= btrfs_delete_one_dir_name(trans
, log
, path
, di
);
2136 bytes_del
+= name_len
;
2140 /* update the directory size in the log to reflect the names
2144 struct btrfs_key key
;
2146 key
.objectid
= dir
->i_ino
;
2148 key
.type
= BTRFS_INODE_ITEM_KEY
;
2149 btrfs_release_path(log
, path
);
2151 ret
= btrfs_search_slot(trans
, log
, &key
, path
, 0, 1);
2153 struct btrfs_inode_item
*item
;
2156 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2157 struct btrfs_inode_item
);
2158 i_size
= btrfs_inode_size(path
->nodes
[0], item
);
2159 if (i_size
> bytes_del
)
2160 i_size
-= bytes_del
;
2163 btrfs_set_inode_size(path
->nodes
[0], item
, i_size
);
2164 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2167 btrfs_release_path(log
, path
);
2170 btrfs_free_path(path
);
2171 mutex_unlock(&BTRFS_I(dir
)->log_mutex
);
2172 end_log_trans(root
);
2177 /* see comments for btrfs_del_dir_entries_in_log */
2178 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle
*trans
,
2179 struct btrfs_root
*root
,
2180 const char *name
, int name_len
,
2181 struct inode
*inode
, u64 dirid
)
2183 struct btrfs_root
*log
;
2187 if (BTRFS_I(inode
)->logged_trans
< trans
->transid
)
2190 ret
= join_running_log_trans(root
);
2193 log
= root
->log_root
;
2194 mutex_lock(&BTRFS_I(inode
)->log_mutex
);
2196 ret
= btrfs_del_inode_ref(trans
, log
, name
, name_len
, inode
->i_ino
,
2198 mutex_unlock(&BTRFS_I(inode
)->log_mutex
);
2199 end_log_trans(root
);
2205 * creates a range item in the log for 'dirid'. first_offset and
2206 * last_offset tell us which parts of the key space the log should
2207 * be considered authoritative for.
2209 static noinline
int insert_dir_log_key(struct btrfs_trans_handle
*trans
,
2210 struct btrfs_root
*log
,
2211 struct btrfs_path
*path
,
2212 int key_type
, u64 dirid
,
2213 u64 first_offset
, u64 last_offset
)
2216 struct btrfs_key key
;
2217 struct btrfs_dir_log_item
*item
;
2219 key
.objectid
= dirid
;
2220 key
.offset
= first_offset
;
2221 if (key_type
== BTRFS_DIR_ITEM_KEY
)
2222 key
.type
= BTRFS_DIR_LOG_ITEM_KEY
;
2224 key
.type
= BTRFS_DIR_LOG_INDEX_KEY
;
2225 ret
= btrfs_insert_empty_item(trans
, log
, path
, &key
, sizeof(*item
));
2228 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2229 struct btrfs_dir_log_item
);
2230 btrfs_set_dir_log_end(path
->nodes
[0], item
, last_offset
);
2231 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2232 btrfs_release_path(log
, path
);
2237 * log all the items included in the current transaction for a given
2238 * directory. This also creates the range items in the log tree required
2239 * to replay anything deleted before the fsync
2241 static noinline
int log_dir_items(struct btrfs_trans_handle
*trans
,
2242 struct btrfs_root
*root
, struct inode
*inode
,
2243 struct btrfs_path
*path
,
2244 struct btrfs_path
*dst_path
, int key_type
,
2245 u64 min_offset
, u64
*last_offset_ret
)
2247 struct btrfs_key min_key
;
2248 struct btrfs_key max_key
;
2249 struct btrfs_root
*log
= root
->log_root
;
2250 struct extent_buffer
*src
;
2254 u64 first_offset
= min_offset
;
2255 u64 last_offset
= (u64
)-1;
2257 log
= root
->log_root
;
2258 max_key
.objectid
= inode
->i_ino
;
2259 max_key
.offset
= (u64
)-1;
2260 max_key
.type
= key_type
;
2262 min_key
.objectid
= inode
->i_ino
;
2263 min_key
.type
= key_type
;
2264 min_key
.offset
= min_offset
;
2266 path
->keep_locks
= 1;
2268 ret
= btrfs_search_forward(root
, &min_key
, &max_key
,
2269 path
, 0, trans
->transid
);
2272 * we didn't find anything from this transaction, see if there
2273 * is anything at all
2275 if (ret
!= 0 || min_key
.objectid
!= inode
->i_ino
||
2276 min_key
.type
!= key_type
) {
2277 min_key
.objectid
= inode
->i_ino
;
2278 min_key
.type
= key_type
;
2279 min_key
.offset
= (u64
)-1;
2280 btrfs_release_path(root
, path
);
2281 ret
= btrfs_search_slot(NULL
, root
, &min_key
, path
, 0, 0);
2283 btrfs_release_path(root
, path
);
2286 ret
= btrfs_previous_item(root
, path
, inode
->i_ino
, key_type
);
2288 /* if ret == 0 there are items for this type,
2289 * create a range to tell us the last key of this type.
2290 * otherwise, there are no items in this directory after
2291 * *min_offset, and we create a range to indicate that.
2294 struct btrfs_key tmp
;
2295 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
,
2297 if (key_type
== tmp
.type
) {
2298 first_offset
= max(min_offset
, tmp
.offset
) + 1;
2304 /* go backward to find any previous key */
2305 ret
= btrfs_previous_item(root
, path
, inode
->i_ino
, key_type
);
2307 struct btrfs_key tmp
;
2308 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
, path
->slots
[0]);
2309 if (key_type
== tmp
.type
) {
2310 first_offset
= tmp
.offset
;
2311 ret
= overwrite_item(trans
, log
, dst_path
,
2312 path
->nodes
[0], path
->slots
[0],
2316 btrfs_release_path(root
, path
);
2318 /* find the first key from this transaction again */
2319 ret
= btrfs_search_slot(NULL
, root
, &min_key
, path
, 0, 0);
2326 * we have a block from this transaction, log every item in it
2327 * from our directory
2330 struct btrfs_key tmp
;
2331 src
= path
->nodes
[0];
2332 nritems
= btrfs_header_nritems(src
);
2333 for (i
= path
->slots
[0]; i
< nritems
; i
++) {
2334 btrfs_item_key_to_cpu(src
, &min_key
, i
);
2336 if (min_key
.objectid
!= inode
->i_ino
||
2337 min_key
.type
!= key_type
)
2339 ret
= overwrite_item(trans
, log
, dst_path
, src
, i
,
2343 path
->slots
[0] = nritems
;
2346 * look ahead to the next item and see if it is also
2347 * from this directory and from this transaction
2349 ret
= btrfs_next_leaf(root
, path
);
2351 last_offset
= (u64
)-1;
2354 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
, path
->slots
[0]);
2355 if (tmp
.objectid
!= inode
->i_ino
|| tmp
.type
!= key_type
) {
2356 last_offset
= (u64
)-1;
2359 if (btrfs_header_generation(path
->nodes
[0]) != trans
->transid
) {
2360 ret
= overwrite_item(trans
, log
, dst_path
,
2361 path
->nodes
[0], path
->slots
[0],
2365 last_offset
= tmp
.offset
;
2370 *last_offset_ret
= last_offset
;
2371 btrfs_release_path(root
, path
);
2372 btrfs_release_path(log
, dst_path
);
2374 /* insert the log range keys to indicate where the log is valid */
2375 ret
= insert_dir_log_key(trans
, log
, path
, key_type
, inode
->i_ino
,
2376 first_offset
, last_offset
);
2382 * logging directories is very similar to logging inodes, We find all the items
2383 * from the current transaction and write them to the log.
2385 * The recovery code scans the directory in the subvolume, and if it finds a
2386 * key in the range logged that is not present in the log tree, then it means
2387 * that dir entry was unlinked during the transaction.
2389 * In order for that scan to work, we must include one key smaller than
2390 * the smallest logged by this transaction and one key larger than the largest
2391 * key logged by this transaction.
2393 static noinline
int log_directory_changes(struct btrfs_trans_handle
*trans
,
2394 struct btrfs_root
*root
, struct inode
*inode
,
2395 struct btrfs_path
*path
,
2396 struct btrfs_path
*dst_path
)
2401 int key_type
= BTRFS_DIR_ITEM_KEY
;
2407 ret
= log_dir_items(trans
, root
, inode
, path
,
2408 dst_path
, key_type
, min_key
,
2411 if (max_key
== (u64
)-1)
2413 min_key
= max_key
+ 1;
2416 if (key_type
== BTRFS_DIR_ITEM_KEY
) {
2417 key_type
= BTRFS_DIR_INDEX_KEY
;
2424 * a helper function to drop items from the log before we relog an
2425 * inode. max_key_type indicates the highest item type to remove.
2426 * This cannot be run for file data extents because it does not
2427 * free the extents they point to.
2429 static int drop_objectid_items(struct btrfs_trans_handle
*trans
,
2430 struct btrfs_root
*log
,
2431 struct btrfs_path
*path
,
2432 u64 objectid
, int max_key_type
)
2435 struct btrfs_key key
;
2436 struct btrfs_key found_key
;
2438 key
.objectid
= objectid
;
2439 key
.type
= max_key_type
;
2440 key
.offset
= (u64
)-1;
2443 ret
= btrfs_search_slot(trans
, log
, &key
, path
, -1, 1);
2448 if (path
->slots
[0] == 0)
2452 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2455 if (found_key
.objectid
!= objectid
)
2458 ret
= btrfs_del_item(trans
, log
, path
);
2460 btrfs_release_path(log
, path
);
2462 btrfs_release_path(log
, path
);
2466 static noinline
int copy_items(struct btrfs_trans_handle
*trans
,
2467 struct btrfs_root
*log
,
2468 struct btrfs_path
*dst_path
,
2469 struct extent_buffer
*src
,
2470 int start_slot
, int nr
, int inode_only
)
2472 unsigned long src_offset
;
2473 unsigned long dst_offset
;
2474 struct btrfs_file_extent_item
*extent
;
2475 struct btrfs_inode_item
*inode_item
;
2477 struct btrfs_key
*ins_keys
;
2482 ins_data
= kmalloc(nr
* sizeof(struct btrfs_key
) +
2483 nr
* sizeof(u32
), GFP_NOFS
);
2484 ins_sizes
= (u32
*)ins_data
;
2485 ins_keys
= (struct btrfs_key
*)(ins_data
+ nr
* sizeof(u32
));
2487 for (i
= 0; i
< nr
; i
++) {
2488 ins_sizes
[i
] = btrfs_item_size_nr(src
, i
+ start_slot
);
2489 btrfs_item_key_to_cpu(src
, ins_keys
+ i
, i
+ start_slot
);
2491 ret
= btrfs_insert_empty_items(trans
, log
, dst_path
,
2492 ins_keys
, ins_sizes
, nr
);
2495 for (i
= 0; i
< nr
; i
++) {
2496 dst_offset
= btrfs_item_ptr_offset(dst_path
->nodes
[0],
2497 dst_path
->slots
[0]);
2499 src_offset
= btrfs_item_ptr_offset(src
, start_slot
+ i
);
2501 copy_extent_buffer(dst_path
->nodes
[0], src
, dst_offset
,
2502 src_offset
, ins_sizes
[i
]);
2504 if (inode_only
== LOG_INODE_EXISTS
&&
2505 ins_keys
[i
].type
== BTRFS_INODE_ITEM_KEY
) {
2506 inode_item
= btrfs_item_ptr(dst_path
->nodes
[0],
2508 struct btrfs_inode_item
);
2509 btrfs_set_inode_size(dst_path
->nodes
[0], inode_item
, 0);
2511 /* set the generation to zero so the recover code
2512 * can tell the difference between an logging
2513 * just to say 'this inode exists' and a logging
2514 * to say 'update this inode with these values'
2516 btrfs_set_inode_generation(dst_path
->nodes
[0],
2519 /* take a reference on file data extents so that truncates
2520 * or deletes of this inode don't have to relog the inode
2523 if (btrfs_key_type(ins_keys
+ i
) == BTRFS_EXTENT_DATA_KEY
) {
2525 extent
= btrfs_item_ptr(src
, start_slot
+ i
,
2526 struct btrfs_file_extent_item
);
2528 found_type
= btrfs_file_extent_type(src
, extent
);
2529 if (found_type
== BTRFS_FILE_EXTENT_REG
||
2530 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2531 u64 ds
= btrfs_file_extent_disk_bytenr(src
,
2533 u64 dl
= btrfs_file_extent_disk_num_bytes(src
,
2535 /* ds == 0 is a hole */
2537 ret
= btrfs_inc_extent_ref(trans
, log
,
2539 dst_path
->nodes
[0]->start
,
2540 BTRFS_TREE_LOG_OBJECTID
,
2542 ins_keys
[i
].objectid
);
2547 dst_path
->slots
[0]++;
2550 btrfs_mark_buffer_dirty(dst_path
->nodes
[0]);
2551 btrfs_release_path(log
, dst_path
);
2556 /* log a single inode in the tree log.
2557 * At least one parent directory for this inode must exist in the tree
2558 * or be logged already.
2560 * Any items from this inode changed by the current transaction are copied
2561 * to the log tree. An extra reference is taken on any extents in this
2562 * file, allowing us to avoid a whole pile of corner cases around logging
2563 * blocks that have been removed from the tree.
2565 * See LOG_INODE_ALL and related defines for a description of what inode_only
2568 * This handles both files and directories.
2570 static int __btrfs_log_inode(struct btrfs_trans_handle
*trans
,
2571 struct btrfs_root
*root
, struct inode
*inode
,
2574 struct btrfs_path
*path
;
2575 struct btrfs_path
*dst_path
;
2576 struct btrfs_key min_key
;
2577 struct btrfs_key max_key
;
2578 struct btrfs_root
*log
= root
->log_root
;
2579 struct extent_buffer
*src
= NULL
;
2583 int ins_start_slot
= 0;
2586 log
= root
->log_root
;
2588 path
= btrfs_alloc_path();
2589 dst_path
= btrfs_alloc_path();
2591 min_key
.objectid
= inode
->i_ino
;
2592 min_key
.type
= BTRFS_INODE_ITEM_KEY
;
2595 max_key
.objectid
= inode
->i_ino
;
2596 if (inode_only
== LOG_INODE_EXISTS
|| S_ISDIR(inode
->i_mode
))
2597 max_key
.type
= BTRFS_XATTR_ITEM_KEY
;
2599 max_key
.type
= (u8
)-1;
2600 max_key
.offset
= (u64
)-1;
2603 * if this inode has already been logged and we're in inode_only
2604 * mode, we don't want to delete the things that have already
2605 * been written to the log.
2607 * But, if the inode has been through an inode_only log,
2608 * the logged_trans field is not set. This allows us to catch
2609 * any new names for this inode in the backrefs by logging it
2612 if (inode_only
== LOG_INODE_EXISTS
&&
2613 BTRFS_I(inode
)->logged_trans
== trans
->transid
) {
2614 btrfs_free_path(path
);
2615 btrfs_free_path(dst_path
);
2618 mutex_lock(&BTRFS_I(inode
)->log_mutex
);
2621 * a brute force approach to making sure we get the most uptodate
2622 * copies of everything.
2624 if (S_ISDIR(inode
->i_mode
)) {
2625 int max_key_type
= BTRFS_DIR_LOG_INDEX_KEY
;
2627 if (inode_only
== LOG_INODE_EXISTS
)
2628 max_key_type
= BTRFS_XATTR_ITEM_KEY
;
2629 ret
= drop_objectid_items(trans
, log
, path
,
2630 inode
->i_ino
, max_key_type
);
2632 ret
= btrfs_truncate_inode_items(trans
, log
, inode
, 0, 0);
2635 path
->keep_locks
= 1;
2639 ret
= btrfs_search_forward(root
, &min_key
, &max_key
,
2640 path
, 0, trans
->transid
);
2644 /* note, ins_nr might be > 0 here, cleanup outside the loop */
2645 if (min_key
.objectid
!= inode
->i_ino
)
2647 if (min_key
.type
> max_key
.type
)
2650 src
= path
->nodes
[0];
2651 size
= btrfs_item_size_nr(src
, path
->slots
[0]);
2652 if (ins_nr
&& ins_start_slot
+ ins_nr
== path
->slots
[0]) {
2655 } else if (!ins_nr
) {
2656 ins_start_slot
= path
->slots
[0];
2661 ret
= copy_items(trans
, log
, dst_path
, src
, ins_start_slot
,
2662 ins_nr
, inode_only
);
2665 ins_start_slot
= path
->slots
[0];
2668 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2670 if (path
->slots
[0] < nritems
) {
2671 btrfs_item_key_to_cpu(path
->nodes
[0], &min_key
,
2676 ret
= copy_items(trans
, log
, dst_path
, src
,
2678 ins_nr
, inode_only
);
2682 btrfs_release_path(root
, path
);
2684 if (min_key
.offset
< (u64
)-1)
2686 else if (min_key
.type
< (u8
)-1)
2688 else if (min_key
.objectid
< (u64
)-1)
2694 ret
= copy_items(trans
, log
, dst_path
, src
,
2696 ins_nr
, inode_only
);
2701 if (inode_only
== LOG_INODE_ALL
&& S_ISDIR(inode
->i_mode
)) {
2702 btrfs_release_path(root
, path
);
2703 btrfs_release_path(log
, dst_path
);
2704 BTRFS_I(inode
)->log_dirty_trans
= 0;
2705 ret
= log_directory_changes(trans
, root
, inode
, path
, dst_path
);
2708 BTRFS_I(inode
)->logged_trans
= trans
->transid
;
2709 mutex_unlock(&BTRFS_I(inode
)->log_mutex
);
2711 btrfs_free_path(path
);
2712 btrfs_free_path(dst_path
);
2714 mutex_lock(&root
->fs_info
->tree_log_mutex
);
2715 ret
= update_log_root(trans
, log
);
2717 mutex_unlock(&root
->fs_info
->tree_log_mutex
);
2722 int btrfs_log_inode(struct btrfs_trans_handle
*trans
,
2723 struct btrfs_root
*root
, struct inode
*inode
,
2728 start_log_trans(trans
, root
);
2729 ret
= __btrfs_log_inode(trans
, root
, inode
, inode_only
);
2730 end_log_trans(root
);
2735 * helper function around btrfs_log_inode to make sure newly created
2736 * parent directories also end up in the log. A minimal inode and backref
2737 * only logging is done of any parent directories that are older than
2738 * the last committed transaction
2740 int btrfs_log_dentry(struct btrfs_trans_handle
*trans
,
2741 struct btrfs_root
*root
, struct dentry
*dentry
)
2743 int inode_only
= LOG_INODE_ALL
;
2744 struct super_block
*sb
;
2747 start_log_trans(trans
, root
);
2748 sb
= dentry
->d_inode
->i_sb
;
2750 ret
= __btrfs_log_inode(trans
, root
, dentry
->d_inode
,
2753 inode_only
= LOG_INODE_EXISTS
;
2755 dentry
= dentry
->d_parent
;
2756 if (!dentry
|| !dentry
->d_inode
|| sb
!= dentry
->d_inode
->i_sb
)
2759 if (BTRFS_I(dentry
->d_inode
)->generation
<=
2760 root
->fs_info
->last_trans_committed
)
2763 end_log_trans(root
);
2768 * it is not safe to log dentry if the chunk root has added new
2769 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
2770 * If this returns 1, you must commit the transaction to safely get your
2773 int btrfs_log_dentry_safe(struct btrfs_trans_handle
*trans
,
2774 struct btrfs_root
*root
, struct dentry
*dentry
)
2777 gen
= root
->fs_info
->last_trans_new_blockgroup
;
2778 if (gen
> root
->fs_info
->last_trans_committed
)
2781 return btrfs_log_dentry(trans
, root
, dentry
);
2785 * should be called during mount to recover any replay any log trees
2788 int btrfs_recover_log_trees(struct btrfs_root
*log_root_tree
)
2791 struct btrfs_path
*path
;
2792 struct btrfs_trans_handle
*trans
;
2793 struct btrfs_key key
;
2794 struct btrfs_key found_key
;
2795 struct btrfs_key tmp_key
;
2796 struct btrfs_root
*log
;
2797 struct btrfs_fs_info
*fs_info
= log_root_tree
->fs_info
;
2799 struct walk_control wc
= {
2800 .process_func
= process_one_buffer
,
2804 fs_info
->log_root_recovering
= 1;
2805 path
= btrfs_alloc_path();
2808 trans
= btrfs_start_transaction(fs_info
->tree_root
, 1);
2813 walk_log_tree(trans
, log_root_tree
, &wc
);
2816 key
.objectid
= BTRFS_TREE_LOG_OBJECTID
;
2817 key
.offset
= (u64
)-1;
2818 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
2821 ret
= btrfs_search_slot(NULL
, log_root_tree
, &key
, path
, 0, 0);
2825 if (path
->slots
[0] == 0)
2829 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2831 btrfs_release_path(log_root_tree
, path
);
2832 if (found_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
2835 log
= btrfs_read_fs_root_no_radix(log_root_tree
,
2840 tmp_key
.objectid
= found_key
.offset
;
2841 tmp_key
.type
= BTRFS_ROOT_ITEM_KEY
;
2842 tmp_key
.offset
= (u64
)-1;
2844 wc
.replay_dest
= btrfs_read_fs_root_no_name(fs_info
, &tmp_key
);
2846 BUG_ON(!wc
.replay_dest
);
2848 btrfs_record_root_in_trans(wc
.replay_dest
);
2849 ret
= walk_log_tree(trans
, log
, &wc
);
2852 if (wc
.stage
== LOG_WALK_REPLAY_ALL
) {
2853 ret
= fixup_inode_link_counts(trans
, wc
.replay_dest
,
2857 ret
= btrfs_find_highest_inode(wc
.replay_dest
, &highest_inode
);
2859 wc
.replay_dest
->highest_inode
= highest_inode
;
2860 wc
.replay_dest
->last_inode_alloc
= highest_inode
;
2863 key
.offset
= found_key
.offset
- 1;
2864 free_extent_buffer(log
->node
);
2867 if (found_key
.offset
== 0)
2870 btrfs_release_path(log_root_tree
, path
);
2872 /* step one is to pin it all, step two is to replay just inodes */
2875 wc
.process_func
= replay_one_buffer
;
2876 wc
.stage
= LOG_WALK_REPLAY_INODES
;
2879 /* step three is to replay everything */
2880 if (wc
.stage
< LOG_WALK_REPLAY_ALL
) {
2885 btrfs_free_path(path
);
2887 free_extent_buffer(log_root_tree
->node
);
2888 log_root_tree
->log_root
= NULL
;
2889 fs_info
->log_root_recovering
= 0;
2891 /* step 4: commit the transaction, which also unpins the blocks */
2892 btrfs_commit_transaction(trans
, fs_info
->tree_root
);
2894 kfree(log_root_tree
);