2 * Copyright (C) 2008 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/blkdev.h>
22 #include <linux/list_sort.h>
24 #include "transaction.h"
27 #include "print-tree.h"
33 /* magic values for the inode_only field in btrfs_log_inode:
35 * LOG_INODE_ALL means to log everything
36 * LOG_INODE_EXISTS means to log just enough to recreate the inode
39 #define LOG_INODE_ALL 0
40 #define LOG_INODE_EXISTS 1
43 * directory trouble cases
45 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
46 * log, we must force a full commit before doing an fsync of the directory
47 * where the unlink was done.
48 * ---> record transid of last unlink/rename per directory
52 * rename foo/some_dir foo2/some_dir
54 * fsync foo/some_dir/some_file
56 * The fsync above will unlink the original some_dir without recording
57 * it in its new location (foo2). After a crash, some_dir will be gone
58 * unless the fsync of some_file forces a full commit
60 * 2) we must log any new names for any file or dir that is in the fsync
61 * log. ---> check inode while renaming/linking.
63 * 2a) we must log any new names for any file or dir during rename
64 * when the directory they are being removed from was logged.
65 * ---> check inode and old parent dir during rename
67 * 2a is actually the more important variant. With the extra logging
68 * a crash might unlink the old name without recreating the new one
70 * 3) after a crash, we must go through any directories with a link count
71 * of zero and redo the rm -rf
78 * The directory f1 was fully removed from the FS, but fsync was never
79 * called on f1, only its parent dir. After a crash the rm -rf must
80 * be replayed. This must be able to recurse down the entire
81 * directory tree. The inode link count fixup code takes care of the
86 * stages for the tree walking. The first
87 * stage (0) is to only pin down the blocks we find
88 * the second stage (1) is to make sure that all the inodes
89 * we find in the log are created in the subvolume.
91 * The last stage is to deal with directories and links and extents
92 * and all the other fun semantics
94 #define LOG_WALK_PIN_ONLY 0
95 #define LOG_WALK_REPLAY_INODES 1
96 #define LOG_WALK_REPLAY_DIR_INDEX 2
97 #define LOG_WALK_REPLAY_ALL 3
99 static int btrfs_log_inode(struct btrfs_trans_handle
*trans
,
100 struct btrfs_root
*root
, struct inode
*inode
,
102 static int link_to_fixup_dir(struct btrfs_trans_handle
*trans
,
103 struct btrfs_root
*root
,
104 struct btrfs_path
*path
, u64 objectid
);
105 static noinline
int replay_dir_deletes(struct btrfs_trans_handle
*trans
,
106 struct btrfs_root
*root
,
107 struct btrfs_root
*log
,
108 struct btrfs_path
*path
,
109 u64 dirid
, int del_all
);
112 * tree logging is a special write ahead log used to make sure that
113 * fsyncs and O_SYNCs can happen without doing full tree commits.
115 * Full tree commits are expensive because they require commonly
116 * modified blocks to be recowed, creating many dirty pages in the
117 * extent tree an 4x-6x higher write load than ext3.
119 * Instead of doing a tree commit on every fsync, we use the
120 * key ranges and transaction ids to find items for a given file or directory
121 * that have changed in this transaction. Those items are copied into
122 * a special tree (one per subvolume root), that tree is written to disk
123 * and then the fsync is considered complete.
125 * After a crash, items are copied out of the log-tree back into the
126 * subvolume tree. Any file data extents found are recorded in the extent
127 * allocation tree, and the log-tree freed.
129 * The log tree is read three times, once to pin down all the extents it is
130 * using in ram and once, once to create all the inodes logged in the tree
131 * and once to do all the other items.
135 * start a sub transaction and setup the log tree
136 * this increments the log tree writer count to make the people
137 * syncing the tree wait for us to finish
139 static int start_log_trans(struct btrfs_trans_handle
*trans
,
140 struct btrfs_root
*root
)
145 mutex_lock(&root
->log_mutex
);
146 if (root
->log_root
) {
147 if (!root
->log_start_pid
) {
148 root
->log_start_pid
= current
->pid
;
149 root
->log_multiple_pids
= false;
150 } else if (root
->log_start_pid
!= current
->pid
) {
151 root
->log_multiple_pids
= true;
154 atomic_inc(&root
->log_batch
);
155 atomic_inc(&root
->log_writers
);
156 mutex_unlock(&root
->log_mutex
);
159 root
->log_multiple_pids
= false;
160 root
->log_start_pid
= current
->pid
;
161 mutex_lock(&root
->fs_info
->tree_log_mutex
);
162 if (!root
->fs_info
->log_root_tree
) {
163 ret
= btrfs_init_log_root_tree(trans
, root
->fs_info
);
167 if (err
== 0 && !root
->log_root
) {
168 ret
= btrfs_add_log_tree(trans
, root
);
172 mutex_unlock(&root
->fs_info
->tree_log_mutex
);
173 atomic_inc(&root
->log_batch
);
174 atomic_inc(&root
->log_writers
);
175 mutex_unlock(&root
->log_mutex
);
180 * returns 0 if there was a log transaction running and we were able
181 * to join, or returns -ENOENT if there were not transactions
184 static int join_running_log_trans(struct btrfs_root
*root
)
192 mutex_lock(&root
->log_mutex
);
193 if (root
->log_root
) {
195 atomic_inc(&root
->log_writers
);
197 mutex_unlock(&root
->log_mutex
);
202 * This either makes the current running log transaction wait
203 * until you call btrfs_end_log_trans() or it makes any future
204 * log transactions wait until you call btrfs_end_log_trans()
206 int btrfs_pin_log_trans(struct btrfs_root
*root
)
210 mutex_lock(&root
->log_mutex
);
211 atomic_inc(&root
->log_writers
);
212 mutex_unlock(&root
->log_mutex
);
217 * indicate we're done making changes to the log tree
218 * and wake up anyone waiting to do a sync
220 void btrfs_end_log_trans(struct btrfs_root
*root
)
222 if (atomic_dec_and_test(&root
->log_writers
)) {
224 if (waitqueue_active(&root
->log_writer_wait
))
225 wake_up(&root
->log_writer_wait
);
231 * the walk control struct is used to pass state down the chain when
232 * processing the log tree. The stage field tells us which part
233 * of the log tree processing we are currently doing. The others
234 * are state fields used for that specific part
236 struct walk_control
{
237 /* should we free the extent on disk when done? This is used
238 * at transaction commit time while freeing a log tree
242 /* should we write out the extent buffer? This is used
243 * while flushing the log tree to disk during a sync
247 /* should we wait for the extent buffer io to finish? Also used
248 * while flushing the log tree to disk for a sync
252 /* pin only walk, we record which extents on disk belong to the
257 /* what stage of the replay code we're currently in */
260 /* the root we are currently replaying */
261 struct btrfs_root
*replay_dest
;
263 /* the trans handle for the current replay */
264 struct btrfs_trans_handle
*trans
;
266 /* the function that gets used to process blocks we find in the
267 * tree. Note the extent_buffer might not be up to date when it is
268 * passed in, and it must be checked or read if you need the data
271 int (*process_func
)(struct btrfs_root
*log
, struct extent_buffer
*eb
,
272 struct walk_control
*wc
, u64 gen
);
276 * process_func used to pin down extents, write them or wait on them
278 static int process_one_buffer(struct btrfs_root
*log
,
279 struct extent_buffer
*eb
,
280 struct walk_control
*wc
, u64 gen
)
285 * If this fs is mixed then we need to be able to process the leaves to
286 * pin down any logged extents, so we have to read the block.
288 if (btrfs_fs_incompat(log
->fs_info
, MIXED_GROUPS
)) {
289 ret
= btrfs_read_buffer(eb
, gen
);
295 ret
= btrfs_pin_extent_for_log_replay(log
->fs_info
->extent_root
,
298 if (!ret
&& btrfs_buffer_uptodate(eb
, gen
, 0)) {
299 if (wc
->pin
&& btrfs_header_level(eb
) == 0)
300 ret
= btrfs_exclude_logged_extents(log
, eb
);
302 btrfs_write_tree_block(eb
);
304 btrfs_wait_tree_block_writeback(eb
);
310 * Item overwrite used by replay and tree logging. eb, slot and key all refer
311 * to the src data we are copying out.
313 * root is the tree we are copying into, and path is a scratch
314 * path for use in this function (it should be released on entry and
315 * will be released on exit).
317 * If the key is already in the destination tree the existing item is
318 * overwritten. If the existing item isn't big enough, it is extended.
319 * If it is too large, it is truncated.
321 * If the key isn't in the destination yet, a new item is inserted.
323 static noinline
int overwrite_item(struct btrfs_trans_handle
*trans
,
324 struct btrfs_root
*root
,
325 struct btrfs_path
*path
,
326 struct extent_buffer
*eb
, int slot
,
327 struct btrfs_key
*key
)
331 u64 saved_i_size
= 0;
332 int save_old_i_size
= 0;
333 unsigned long src_ptr
;
334 unsigned long dst_ptr
;
335 int overwrite_root
= 0;
336 bool inode_item
= key
->type
== BTRFS_INODE_ITEM_KEY
;
338 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
341 item_size
= btrfs_item_size_nr(eb
, slot
);
342 src_ptr
= btrfs_item_ptr_offset(eb
, slot
);
344 /* look for the key in the destination tree */
345 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
352 u32 dst_size
= btrfs_item_size_nr(path
->nodes
[0],
354 if (dst_size
!= item_size
)
357 if (item_size
== 0) {
358 btrfs_release_path(path
);
361 dst_copy
= kmalloc(item_size
, GFP_NOFS
);
362 src_copy
= kmalloc(item_size
, GFP_NOFS
);
363 if (!dst_copy
|| !src_copy
) {
364 btrfs_release_path(path
);
370 read_extent_buffer(eb
, src_copy
, src_ptr
, item_size
);
372 dst_ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
373 read_extent_buffer(path
->nodes
[0], dst_copy
, dst_ptr
,
375 ret
= memcmp(dst_copy
, src_copy
, item_size
);
380 * they have the same contents, just return, this saves
381 * us from cowing blocks in the destination tree and doing
382 * extra writes that may not have been done by a previous
386 btrfs_release_path(path
);
391 * We need to load the old nbytes into the inode so when we
392 * replay the extents we've logged we get the right nbytes.
395 struct btrfs_inode_item
*item
;
398 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
399 struct btrfs_inode_item
);
400 nbytes
= btrfs_inode_nbytes(path
->nodes
[0], item
);
401 item
= btrfs_item_ptr(eb
, slot
,
402 struct btrfs_inode_item
);
403 btrfs_set_inode_nbytes(eb
, item
, nbytes
);
405 } else if (inode_item
) {
406 struct btrfs_inode_item
*item
;
409 * New inode, set nbytes to 0 so that the nbytes comes out
410 * properly when we replay the extents.
412 item
= btrfs_item_ptr(eb
, slot
, struct btrfs_inode_item
);
413 btrfs_set_inode_nbytes(eb
, item
, 0);
416 btrfs_release_path(path
);
417 /* try to insert the key into the destination tree */
418 ret
= btrfs_insert_empty_item(trans
, root
, path
,
421 /* make sure any existing item is the correct size */
422 if (ret
== -EEXIST
) {
424 found_size
= btrfs_item_size_nr(path
->nodes
[0],
426 if (found_size
> item_size
)
427 btrfs_truncate_item(root
, path
, item_size
, 1);
428 else if (found_size
< item_size
)
429 btrfs_extend_item(root
, path
,
430 item_size
- found_size
);
434 dst_ptr
= btrfs_item_ptr_offset(path
->nodes
[0],
437 /* don't overwrite an existing inode if the generation number
438 * was logged as zero. This is done when the tree logging code
439 * is just logging an inode to make sure it exists after recovery.
441 * Also, don't overwrite i_size on directories during replay.
442 * log replay inserts and removes directory items based on the
443 * state of the tree found in the subvolume, and i_size is modified
446 if (key
->type
== BTRFS_INODE_ITEM_KEY
&& ret
== -EEXIST
) {
447 struct btrfs_inode_item
*src_item
;
448 struct btrfs_inode_item
*dst_item
;
450 src_item
= (struct btrfs_inode_item
*)src_ptr
;
451 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
453 if (btrfs_inode_generation(eb
, src_item
) == 0)
456 if (overwrite_root
&&
457 S_ISDIR(btrfs_inode_mode(eb
, src_item
)) &&
458 S_ISDIR(btrfs_inode_mode(path
->nodes
[0], dst_item
))) {
460 saved_i_size
= btrfs_inode_size(path
->nodes
[0],
465 copy_extent_buffer(path
->nodes
[0], eb
, dst_ptr
,
468 if (save_old_i_size
) {
469 struct btrfs_inode_item
*dst_item
;
470 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
471 btrfs_set_inode_size(path
->nodes
[0], dst_item
, saved_i_size
);
474 /* make sure the generation is filled in */
475 if (key
->type
== BTRFS_INODE_ITEM_KEY
) {
476 struct btrfs_inode_item
*dst_item
;
477 dst_item
= (struct btrfs_inode_item
*)dst_ptr
;
478 if (btrfs_inode_generation(path
->nodes
[0], dst_item
) == 0) {
479 btrfs_set_inode_generation(path
->nodes
[0], dst_item
,
484 btrfs_mark_buffer_dirty(path
->nodes
[0]);
485 btrfs_release_path(path
);
490 * simple helper to read an inode off the disk from a given root
491 * This can only be called for subvolume roots and not for the log
493 static noinline
struct inode
*read_one_inode(struct btrfs_root
*root
,
496 struct btrfs_key key
;
499 key
.objectid
= objectid
;
500 key
.type
= BTRFS_INODE_ITEM_KEY
;
502 inode
= btrfs_iget(root
->fs_info
->sb
, &key
, root
, NULL
);
505 } else if (is_bad_inode(inode
)) {
512 /* replays a single extent in 'eb' at 'slot' with 'key' into the
513 * subvolume 'root'. path is released on entry and should be released
516 * extents in the log tree have not been allocated out of the extent
517 * tree yet. So, this completes the allocation, taking a reference
518 * as required if the extent already exists or creating a new extent
519 * if it isn't in the extent allocation tree yet.
521 * The extent is inserted into the file, dropping any existing extents
522 * from the file that overlap the new one.
524 static noinline
int replay_one_extent(struct btrfs_trans_handle
*trans
,
525 struct btrfs_root
*root
,
526 struct btrfs_path
*path
,
527 struct extent_buffer
*eb
, int slot
,
528 struct btrfs_key
*key
)
532 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 nbytes
= btrfs_file_extent_num_bytes(eb
, item
);
545 extent_end
= start
+ nbytes
;
548 * We don't add to the inodes nbytes if we are prealloc or a
551 if (btrfs_file_extent_disk_bytenr(eb
, item
) == 0)
553 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
554 size
= btrfs_file_extent_inline_len(eb
, item
);
555 nbytes
= btrfs_file_extent_ram_bytes(eb
, item
);
556 extent_end
= ALIGN(start
+ size
, root
->sectorsize
);
562 inode
= read_one_inode(root
, key
->objectid
);
569 * first check to see if we already have this extent in the
570 * file. This must be done before the btrfs_drop_extents run
571 * so we don't try to drop this extent.
573 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
577 (found_type
== BTRFS_FILE_EXTENT_REG
||
578 found_type
== BTRFS_FILE_EXTENT_PREALLOC
)) {
579 struct btrfs_file_extent_item cmp1
;
580 struct btrfs_file_extent_item cmp2
;
581 struct btrfs_file_extent_item
*existing
;
582 struct extent_buffer
*leaf
;
584 leaf
= path
->nodes
[0];
585 existing
= btrfs_item_ptr(leaf
, path
->slots
[0],
586 struct btrfs_file_extent_item
);
588 read_extent_buffer(eb
, &cmp1
, (unsigned long)item
,
590 read_extent_buffer(leaf
, &cmp2
, (unsigned long)existing
,
594 * we already have a pointer to this exact extent,
595 * we don't have to do anything
597 if (memcmp(&cmp1
, &cmp2
, sizeof(cmp1
)) == 0) {
598 btrfs_release_path(path
);
602 btrfs_release_path(path
);
604 /* drop any overlapping extents */
605 ret
= btrfs_drop_extents(trans
, root
, inode
, start
, extent_end
, 1);
609 if (found_type
== BTRFS_FILE_EXTENT_REG
||
610 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
612 unsigned long dest_offset
;
613 struct btrfs_key ins
;
615 ret
= btrfs_insert_empty_item(trans
, root
, path
, key
,
619 dest_offset
= btrfs_item_ptr_offset(path
->nodes
[0],
621 copy_extent_buffer(path
->nodes
[0], eb
, dest_offset
,
622 (unsigned long)item
, sizeof(*item
));
624 ins
.objectid
= btrfs_file_extent_disk_bytenr(eb
, item
);
625 ins
.offset
= btrfs_file_extent_disk_num_bytes(eb
, item
);
626 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
627 offset
= key
->offset
- btrfs_file_extent_offset(eb
, item
);
629 if (ins
.objectid
> 0) {
632 LIST_HEAD(ordered_sums
);
634 * is this extent already allocated in the extent
635 * allocation tree? If so, just add a reference
637 ret
= btrfs_lookup_extent(root
, ins
.objectid
,
640 ret
= btrfs_inc_extent_ref(trans
, root
,
641 ins
.objectid
, ins
.offset
,
642 0, root
->root_key
.objectid
,
643 key
->objectid
, offset
, 0);
648 * insert the extent pointer in the extent
651 ret
= btrfs_alloc_logged_file_extent(trans
,
652 root
, root
->root_key
.objectid
,
653 key
->objectid
, offset
, &ins
);
657 btrfs_release_path(path
);
659 if (btrfs_file_extent_compression(eb
, item
)) {
660 csum_start
= ins
.objectid
;
661 csum_end
= csum_start
+ ins
.offset
;
663 csum_start
= ins
.objectid
+
664 btrfs_file_extent_offset(eb
, item
);
665 csum_end
= csum_start
+
666 btrfs_file_extent_num_bytes(eb
, item
);
669 ret
= btrfs_lookup_csums_range(root
->log_root
,
670 csum_start
, csum_end
- 1,
674 while (!list_empty(&ordered_sums
)) {
675 struct btrfs_ordered_sum
*sums
;
676 sums
= list_entry(ordered_sums
.next
,
677 struct btrfs_ordered_sum
,
680 ret
= btrfs_csum_file_blocks(trans
,
681 root
->fs_info
->csum_root
,
683 list_del(&sums
->list
);
689 btrfs_release_path(path
);
691 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
692 /* inline extents are easy, we just overwrite them */
693 ret
= overwrite_item(trans
, root
, path
, eb
, slot
, key
);
698 inode_add_bytes(inode
, nbytes
);
699 ret
= btrfs_update_inode(trans
, root
, inode
);
707 * when cleaning up conflicts between the directory names in the
708 * subvolume, directory names in the log and directory names in the
709 * inode back references, we may have to unlink inodes from directories.
711 * This is a helper function to do the unlink of a specific directory
714 static noinline
int drop_one_dir_item(struct btrfs_trans_handle
*trans
,
715 struct btrfs_root
*root
,
716 struct btrfs_path
*path
,
718 struct btrfs_dir_item
*di
)
723 struct extent_buffer
*leaf
;
724 struct btrfs_key location
;
727 leaf
= path
->nodes
[0];
729 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
730 name_len
= btrfs_dir_name_len(leaf
, di
);
731 name
= kmalloc(name_len
, GFP_NOFS
);
735 read_extent_buffer(leaf
, name
, (unsigned long)(di
+ 1), name_len
);
736 btrfs_release_path(path
);
738 inode
= read_one_inode(root
, location
.objectid
);
744 ret
= link_to_fixup_dir(trans
, root
, path
, location
.objectid
);
748 ret
= btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
752 ret
= btrfs_run_delayed_items(trans
, root
);
760 * helper function to see if a given name and sequence number found
761 * in an inode back reference are already in a directory and correctly
762 * point to this inode
764 static noinline
int inode_in_dir(struct btrfs_root
*root
,
765 struct btrfs_path
*path
,
766 u64 dirid
, u64 objectid
, u64 index
,
767 const char *name
, int name_len
)
769 struct btrfs_dir_item
*di
;
770 struct btrfs_key location
;
773 di
= btrfs_lookup_dir_index_item(NULL
, root
, path
, dirid
,
774 index
, name
, name_len
, 0);
775 if (di
&& !IS_ERR(di
)) {
776 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
777 if (location
.objectid
!= objectid
)
781 btrfs_release_path(path
);
783 di
= btrfs_lookup_dir_item(NULL
, root
, path
, dirid
, name
, name_len
, 0);
784 if (di
&& !IS_ERR(di
)) {
785 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &location
);
786 if (location
.objectid
!= objectid
)
792 btrfs_release_path(path
);
797 * helper function to check a log tree for a named back reference in
798 * an inode. This is used to decide if a back reference that is
799 * found in the subvolume conflicts with what we find in the log.
801 * inode backreferences may have multiple refs in a single item,
802 * during replay we process one reference at a time, and we don't
803 * want to delete valid links to a file from the subvolume if that
804 * link is also in the log.
806 static noinline
int backref_in_log(struct btrfs_root
*log
,
807 struct btrfs_key
*key
,
809 char *name
, int namelen
)
811 struct btrfs_path
*path
;
812 struct btrfs_inode_ref
*ref
;
814 unsigned long ptr_end
;
815 unsigned long name_ptr
;
821 path
= btrfs_alloc_path();
825 ret
= btrfs_search_slot(NULL
, log
, key
, path
, 0, 0);
829 ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
831 if (key
->type
== BTRFS_INODE_EXTREF_KEY
) {
832 if (btrfs_find_name_in_ext_backref(path
, ref_objectid
,
833 name
, namelen
, NULL
))
839 item_size
= btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]);
840 ptr_end
= ptr
+ item_size
;
841 while (ptr
< ptr_end
) {
842 ref
= (struct btrfs_inode_ref
*)ptr
;
843 found_name_len
= btrfs_inode_ref_name_len(path
->nodes
[0], ref
);
844 if (found_name_len
== namelen
) {
845 name_ptr
= (unsigned long)(ref
+ 1);
846 ret
= memcmp_extent_buffer(path
->nodes
[0], name
,
853 ptr
= (unsigned long)(ref
+ 1) + found_name_len
;
856 btrfs_free_path(path
);
860 static inline int __add_inode_ref(struct btrfs_trans_handle
*trans
,
861 struct btrfs_root
*root
,
862 struct btrfs_path
*path
,
863 struct btrfs_root
*log_root
,
864 struct inode
*dir
, struct inode
*inode
,
865 struct extent_buffer
*eb
,
866 u64 inode_objectid
, u64 parent_objectid
,
867 u64 ref_index
, char *name
, int namelen
,
873 struct extent_buffer
*leaf
;
874 struct btrfs_dir_item
*di
;
875 struct btrfs_key search_key
;
876 struct btrfs_inode_extref
*extref
;
879 /* Search old style refs */
880 search_key
.objectid
= inode_objectid
;
881 search_key
.type
= BTRFS_INODE_REF_KEY
;
882 search_key
.offset
= parent_objectid
;
883 ret
= btrfs_search_slot(NULL
, root
, &search_key
, path
, 0, 0);
885 struct btrfs_inode_ref
*victim_ref
;
887 unsigned long ptr_end
;
889 leaf
= path
->nodes
[0];
891 /* are we trying to overwrite a back ref for the root directory
892 * if so, just jump out, we're done
894 if (search_key
.objectid
== search_key
.offset
)
897 /* check all the names in this back reference to see
898 * if they are in the log. if so, we allow them to stay
899 * otherwise they must be unlinked as a conflict
901 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
902 ptr_end
= ptr
+ btrfs_item_size_nr(leaf
, path
->slots
[0]);
903 while (ptr
< ptr_end
) {
904 victim_ref
= (struct btrfs_inode_ref
*)ptr
;
905 victim_name_len
= btrfs_inode_ref_name_len(leaf
,
907 victim_name
= kmalloc(victim_name_len
, GFP_NOFS
);
911 read_extent_buffer(leaf
, victim_name
,
912 (unsigned long)(victim_ref
+ 1),
915 if (!backref_in_log(log_root
, &search_key
,
919 btrfs_inc_nlink(inode
);
920 btrfs_release_path(path
);
922 ret
= btrfs_unlink_inode(trans
, root
, dir
,
928 ret
= btrfs_run_delayed_items(trans
, root
);
936 ptr
= (unsigned long)(victim_ref
+ 1) + victim_name_len
;
940 * NOTE: we have searched root tree and checked the
941 * coresponding ref, it does not need to check again.
945 btrfs_release_path(path
);
947 /* Same search but for extended refs */
948 extref
= btrfs_lookup_inode_extref(NULL
, root
, path
, name
, namelen
,
949 inode_objectid
, parent_objectid
, 0,
951 if (!IS_ERR_OR_NULL(extref
)) {
955 struct inode
*victim_parent
;
957 leaf
= path
->nodes
[0];
959 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
960 base
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
962 while (cur_offset
< item_size
) {
963 extref
= (struct btrfs_inode_extref
*)base
+ cur_offset
;
965 victim_name_len
= btrfs_inode_extref_name_len(leaf
, extref
);
967 if (btrfs_inode_extref_parent(leaf
, extref
) != parent_objectid
)
970 victim_name
= kmalloc(victim_name_len
, GFP_NOFS
);
973 read_extent_buffer(leaf
, victim_name
, (unsigned long)&extref
->name
,
976 search_key
.objectid
= inode_objectid
;
977 search_key
.type
= BTRFS_INODE_EXTREF_KEY
;
978 search_key
.offset
= btrfs_extref_hash(parent_objectid
,
982 if (!backref_in_log(log_root
, &search_key
,
983 parent_objectid
, victim_name
,
986 victim_parent
= read_one_inode(root
,
989 btrfs_inc_nlink(inode
);
990 btrfs_release_path(path
);
992 ret
= btrfs_unlink_inode(trans
, root
,
998 ret
= btrfs_run_delayed_items(
1001 iput(victim_parent
);
1012 cur_offset
+= victim_name_len
+ sizeof(*extref
);
1016 btrfs_release_path(path
);
1018 /* look for a conflicting sequence number */
1019 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, btrfs_ino(dir
),
1020 ref_index
, name
, namelen
, 0);
1021 if (di
&& !IS_ERR(di
)) {
1022 ret
= drop_one_dir_item(trans
, root
, path
, dir
, di
);
1026 btrfs_release_path(path
);
1028 /* look for a conflicing name */
1029 di
= btrfs_lookup_dir_item(trans
, root
, path
, btrfs_ino(dir
),
1031 if (di
&& !IS_ERR(di
)) {
1032 ret
= drop_one_dir_item(trans
, root
, path
, dir
, di
);
1036 btrfs_release_path(path
);
1041 static int extref_get_fields(struct extent_buffer
*eb
, unsigned long ref_ptr
,
1042 u32
*namelen
, char **name
, u64
*index
,
1043 u64
*parent_objectid
)
1045 struct btrfs_inode_extref
*extref
;
1047 extref
= (struct btrfs_inode_extref
*)ref_ptr
;
1049 *namelen
= btrfs_inode_extref_name_len(eb
, extref
);
1050 *name
= kmalloc(*namelen
, GFP_NOFS
);
1054 read_extent_buffer(eb
, *name
, (unsigned long)&extref
->name
,
1057 *index
= btrfs_inode_extref_index(eb
, extref
);
1058 if (parent_objectid
)
1059 *parent_objectid
= btrfs_inode_extref_parent(eb
, extref
);
1064 static int ref_get_fields(struct extent_buffer
*eb
, unsigned long ref_ptr
,
1065 u32
*namelen
, char **name
, u64
*index
)
1067 struct btrfs_inode_ref
*ref
;
1069 ref
= (struct btrfs_inode_ref
*)ref_ptr
;
1071 *namelen
= btrfs_inode_ref_name_len(eb
, ref
);
1072 *name
= kmalloc(*namelen
, GFP_NOFS
);
1076 read_extent_buffer(eb
, *name
, (unsigned long)(ref
+ 1), *namelen
);
1078 *index
= btrfs_inode_ref_index(eb
, ref
);
1084 * replay one inode back reference item found in the log tree.
1085 * eb, slot and key refer to the buffer and key found in the log tree.
1086 * root is the destination we are replaying into, and path is for temp
1087 * use by this function. (it should be released on return).
1089 static noinline
int add_inode_ref(struct btrfs_trans_handle
*trans
,
1090 struct btrfs_root
*root
,
1091 struct btrfs_root
*log
,
1092 struct btrfs_path
*path
,
1093 struct extent_buffer
*eb
, int slot
,
1094 struct btrfs_key
*key
)
1097 struct inode
*inode
;
1098 unsigned long ref_ptr
;
1099 unsigned long ref_end
;
1103 int search_done
= 0;
1104 int log_ref_ver
= 0;
1105 u64 parent_objectid
;
1108 int ref_struct_size
;
1110 ref_ptr
= btrfs_item_ptr_offset(eb
, slot
);
1111 ref_end
= ref_ptr
+ btrfs_item_size_nr(eb
, slot
);
1113 if (key
->type
== BTRFS_INODE_EXTREF_KEY
) {
1114 struct btrfs_inode_extref
*r
;
1116 ref_struct_size
= sizeof(struct btrfs_inode_extref
);
1118 r
= (struct btrfs_inode_extref
*)ref_ptr
;
1119 parent_objectid
= btrfs_inode_extref_parent(eb
, r
);
1121 ref_struct_size
= sizeof(struct btrfs_inode_ref
);
1122 parent_objectid
= key
->offset
;
1124 inode_objectid
= key
->objectid
;
1127 * it is possible that we didn't log all the parent directories
1128 * for a given inode. If we don't find the dir, just don't
1129 * copy the back ref in. The link count fixup code will take
1132 dir
= read_one_inode(root
, parent_objectid
);
1136 inode
= read_one_inode(root
, inode_objectid
);
1142 while (ref_ptr
< ref_end
) {
1144 ret
= extref_get_fields(eb
, ref_ptr
, &namelen
, &name
,
1145 &ref_index
, &parent_objectid
);
1147 * parent object can change from one array
1151 dir
= read_one_inode(root
, parent_objectid
);
1155 ret
= ref_get_fields(eb
, ref_ptr
, &namelen
, &name
,
1161 /* if we already have a perfect match, we're done */
1162 if (!inode_in_dir(root
, path
, btrfs_ino(dir
), btrfs_ino(inode
),
1163 ref_index
, name
, namelen
)) {
1165 * look for a conflicting back reference in the
1166 * metadata. if we find one we have to unlink that name
1167 * of the file before we add our new link. Later on, we
1168 * overwrite any existing back reference, and we don't
1169 * want to create dangling pointers in the directory.
1173 ret
= __add_inode_ref(trans
, root
, path
, log
,
1177 ref_index
, name
, namelen
,
1187 /* insert our name */
1188 ret
= btrfs_add_link(trans
, dir
, inode
, name
, namelen
,
1193 btrfs_update_inode(trans
, root
, inode
);
1196 ref_ptr
= (unsigned long)(ref_ptr
+ ref_struct_size
) + namelen
;
1204 /* finally write the back reference in the inode */
1205 ret
= overwrite_item(trans
, root
, path
, eb
, slot
, key
);
1207 btrfs_release_path(path
);
1213 static int insert_orphan_item(struct btrfs_trans_handle
*trans
,
1214 struct btrfs_root
*root
, u64 offset
)
1217 ret
= btrfs_find_orphan_item(root
, offset
);
1219 ret
= btrfs_insert_orphan_item(trans
, root
, offset
);
1223 static int count_inode_extrefs(struct btrfs_root
*root
,
1224 struct inode
*inode
, struct btrfs_path
*path
)
1228 unsigned int nlink
= 0;
1231 u64 inode_objectid
= btrfs_ino(inode
);
1234 struct btrfs_inode_extref
*extref
;
1235 struct extent_buffer
*leaf
;
1238 ret
= btrfs_find_one_extref(root
, inode_objectid
, offset
, path
,
1243 leaf
= path
->nodes
[0];
1244 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
1245 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
1247 while (cur_offset
< item_size
) {
1248 extref
= (struct btrfs_inode_extref
*) (ptr
+ cur_offset
);
1249 name_len
= btrfs_inode_extref_name_len(leaf
, extref
);
1253 cur_offset
+= name_len
+ sizeof(*extref
);
1257 btrfs_release_path(path
);
1259 btrfs_release_path(path
);
1266 static int count_inode_refs(struct btrfs_root
*root
,
1267 struct inode
*inode
, struct btrfs_path
*path
)
1270 struct btrfs_key key
;
1271 unsigned int nlink
= 0;
1273 unsigned long ptr_end
;
1275 u64 ino
= btrfs_ino(inode
);
1278 key
.type
= BTRFS_INODE_REF_KEY
;
1279 key
.offset
= (u64
)-1;
1282 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1286 if (path
->slots
[0] == 0)
1290 btrfs_item_key_to_cpu(path
->nodes
[0], &key
,
1292 if (key
.objectid
!= ino
||
1293 key
.type
!= BTRFS_INODE_REF_KEY
)
1295 ptr
= btrfs_item_ptr_offset(path
->nodes
[0], path
->slots
[0]);
1296 ptr_end
= ptr
+ btrfs_item_size_nr(path
->nodes
[0],
1298 while (ptr
< ptr_end
) {
1299 struct btrfs_inode_ref
*ref
;
1301 ref
= (struct btrfs_inode_ref
*)ptr
;
1302 name_len
= btrfs_inode_ref_name_len(path
->nodes
[0],
1304 ptr
= (unsigned long)(ref
+ 1) + name_len
;
1308 if (key
.offset
== 0)
1311 btrfs_release_path(path
);
1313 btrfs_release_path(path
);
1319 * There are a few corners where the link count of the file can't
1320 * be properly maintained during replay. So, instead of adding
1321 * lots of complexity to the log code, we just scan the backrefs
1322 * for any file that has been through replay.
1324 * The scan will update the link count on the inode to reflect the
1325 * number of back refs found. If it goes down to zero, the iput
1326 * will free the inode.
1328 static noinline
int fixup_inode_link_count(struct btrfs_trans_handle
*trans
,
1329 struct btrfs_root
*root
,
1330 struct inode
*inode
)
1332 struct btrfs_path
*path
;
1335 u64 ino
= btrfs_ino(inode
);
1337 path
= btrfs_alloc_path();
1341 ret
= count_inode_refs(root
, inode
, path
);
1347 ret
= count_inode_extrefs(root
, inode
, path
);
1358 if (nlink
!= inode
->i_nlink
) {
1359 set_nlink(inode
, nlink
);
1360 btrfs_update_inode(trans
, root
, inode
);
1362 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
1364 if (inode
->i_nlink
== 0) {
1365 if (S_ISDIR(inode
->i_mode
)) {
1366 ret
= replay_dir_deletes(trans
, root
, NULL
, path
,
1371 ret
= insert_orphan_item(trans
, root
, ino
);
1375 btrfs_free_path(path
);
1379 static noinline
int fixup_inode_link_counts(struct btrfs_trans_handle
*trans
,
1380 struct btrfs_root
*root
,
1381 struct btrfs_path
*path
)
1384 struct btrfs_key key
;
1385 struct inode
*inode
;
1387 key
.objectid
= BTRFS_TREE_LOG_FIXUP_OBJECTID
;
1388 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
1389 key
.offset
= (u64
)-1;
1391 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1396 if (path
->slots
[0] == 0)
1401 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1402 if (key
.objectid
!= BTRFS_TREE_LOG_FIXUP_OBJECTID
||
1403 key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
1406 ret
= btrfs_del_item(trans
, root
, path
);
1410 btrfs_release_path(path
);
1411 inode
= read_one_inode(root
, key
.offset
);
1415 ret
= fixup_inode_link_count(trans
, root
, inode
);
1421 * fixup on a directory may create new entries,
1422 * make sure we always look for the highset possible
1425 key
.offset
= (u64
)-1;
1429 btrfs_release_path(path
);
1435 * record a given inode in the fixup dir so we can check its link
1436 * count when replay is done. The link count is incremented here
1437 * so the inode won't go away until we check it
1439 static noinline
int link_to_fixup_dir(struct btrfs_trans_handle
*trans
,
1440 struct btrfs_root
*root
,
1441 struct btrfs_path
*path
,
1444 struct btrfs_key key
;
1446 struct inode
*inode
;
1448 inode
= read_one_inode(root
, objectid
);
1452 key
.objectid
= BTRFS_TREE_LOG_FIXUP_OBJECTID
;
1453 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
1454 key
.offset
= objectid
;
1456 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, 0);
1458 btrfs_release_path(path
);
1460 if (!inode
->i_nlink
)
1461 set_nlink(inode
, 1);
1463 btrfs_inc_nlink(inode
);
1464 ret
= btrfs_update_inode(trans
, root
, inode
);
1465 } else if (ret
== -EEXIST
) {
1468 BUG(); /* Logic Error */
1476 * when replaying the log for a directory, we only insert names
1477 * for inodes that actually exist. This means an fsync on a directory
1478 * does not implicitly fsync all the new files in it
1480 static noinline
int insert_one_name(struct btrfs_trans_handle
*trans
,
1481 struct btrfs_root
*root
,
1482 struct btrfs_path
*path
,
1483 u64 dirid
, u64 index
,
1484 char *name
, int name_len
, u8 type
,
1485 struct btrfs_key
*location
)
1487 struct inode
*inode
;
1491 inode
= read_one_inode(root
, location
->objectid
);
1495 dir
= read_one_inode(root
, dirid
);
1500 ret
= btrfs_add_link(trans
, dir
, inode
, name
, name_len
, 1, index
);
1502 /* FIXME, put inode into FIXUP list */
1510 * take a single entry in a log directory item and replay it into
1513 * if a conflicting item exists in the subdirectory already,
1514 * the inode it points to is unlinked and put into the link count
1517 * If a name from the log points to a file or directory that does
1518 * not exist in the FS, it is skipped. fsyncs on directories
1519 * do not force down inodes inside that directory, just changes to the
1520 * names or unlinks in a directory.
1522 static noinline
int replay_one_name(struct btrfs_trans_handle
*trans
,
1523 struct btrfs_root
*root
,
1524 struct btrfs_path
*path
,
1525 struct extent_buffer
*eb
,
1526 struct btrfs_dir_item
*di
,
1527 struct btrfs_key
*key
)
1531 struct btrfs_dir_item
*dst_di
;
1532 struct btrfs_key found_key
;
1533 struct btrfs_key log_key
;
1539 dir
= read_one_inode(root
, key
->objectid
);
1543 name_len
= btrfs_dir_name_len(eb
, di
);
1544 name
= kmalloc(name_len
, GFP_NOFS
);
1550 log_type
= btrfs_dir_type(eb
, di
);
1551 read_extent_buffer(eb
, name
, (unsigned long)(di
+ 1),
1554 btrfs_dir_item_key_to_cpu(eb
, di
, &log_key
);
1555 exists
= btrfs_lookup_inode(trans
, root
, path
, &log_key
, 0);
1560 btrfs_release_path(path
);
1562 if (key
->type
== BTRFS_DIR_ITEM_KEY
) {
1563 dst_di
= btrfs_lookup_dir_item(trans
, root
, path
, key
->objectid
,
1565 } else if (key
->type
== BTRFS_DIR_INDEX_KEY
) {
1566 dst_di
= btrfs_lookup_dir_index_item(trans
, root
, path
,
1575 if (IS_ERR_OR_NULL(dst_di
)) {
1576 /* we need a sequence number to insert, so we only
1577 * do inserts for the BTRFS_DIR_INDEX_KEY types
1579 if (key
->type
!= BTRFS_DIR_INDEX_KEY
)
1584 btrfs_dir_item_key_to_cpu(path
->nodes
[0], dst_di
, &found_key
);
1585 /* the existing item matches the logged item */
1586 if (found_key
.objectid
== log_key
.objectid
&&
1587 found_key
.type
== log_key
.type
&&
1588 found_key
.offset
== log_key
.offset
&&
1589 btrfs_dir_type(path
->nodes
[0], dst_di
) == log_type
) {
1594 * don't drop the conflicting directory entry if the inode
1595 * for the new entry doesn't exist
1600 ret
= drop_one_dir_item(trans
, root
, path
, dir
, dst_di
);
1604 if (key
->type
== BTRFS_DIR_INDEX_KEY
)
1607 btrfs_release_path(path
);
1613 btrfs_release_path(path
);
1614 ret
= insert_one_name(trans
, root
, path
, key
->objectid
, key
->offset
,
1615 name
, name_len
, log_type
, &log_key
);
1616 if (ret
&& ret
!= -ENOENT
)
1623 * find all the names in a directory item and reconcile them into
1624 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1625 * one name in a directory item, but the same code gets used for
1626 * both directory index types
1628 static noinline
int replay_one_dir_item(struct btrfs_trans_handle
*trans
,
1629 struct btrfs_root
*root
,
1630 struct btrfs_path
*path
,
1631 struct extent_buffer
*eb
, int slot
,
1632 struct btrfs_key
*key
)
1635 u32 item_size
= btrfs_item_size_nr(eb
, slot
);
1636 struct btrfs_dir_item
*di
;
1639 unsigned long ptr_end
;
1641 ptr
= btrfs_item_ptr_offset(eb
, slot
);
1642 ptr_end
= ptr
+ item_size
;
1643 while (ptr
< ptr_end
) {
1644 di
= (struct btrfs_dir_item
*)ptr
;
1645 if (verify_dir_item(root
, eb
, di
))
1647 name_len
= btrfs_dir_name_len(eb
, di
);
1648 ret
= replay_one_name(trans
, root
, path
, eb
, di
, key
);
1651 ptr
= (unsigned long)(di
+ 1);
1658 * directory replay has two parts. There are the standard directory
1659 * items in the log copied from the subvolume, and range items
1660 * created in the log while the subvolume was logged.
1662 * The range items tell us which parts of the key space the log
1663 * is authoritative for. During replay, if a key in the subvolume
1664 * directory is in a logged range item, but not actually in the log
1665 * that means it was deleted from the directory before the fsync
1666 * and should be removed.
1668 static noinline
int find_dir_range(struct btrfs_root
*root
,
1669 struct btrfs_path
*path
,
1670 u64 dirid
, int key_type
,
1671 u64
*start_ret
, u64
*end_ret
)
1673 struct btrfs_key key
;
1675 struct btrfs_dir_log_item
*item
;
1679 if (*start_ret
== (u64
)-1)
1682 key
.objectid
= dirid
;
1683 key
.type
= key_type
;
1684 key
.offset
= *start_ret
;
1686 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1690 if (path
->slots
[0] == 0)
1695 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1697 if (key
.type
!= key_type
|| key
.objectid
!= dirid
) {
1701 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1702 struct btrfs_dir_log_item
);
1703 found_end
= btrfs_dir_log_end(path
->nodes
[0], item
);
1705 if (*start_ret
>= key
.offset
&& *start_ret
<= found_end
) {
1707 *start_ret
= key
.offset
;
1708 *end_ret
= found_end
;
1713 /* check the next slot in the tree to see if it is a valid item */
1714 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1715 if (path
->slots
[0] >= nritems
) {
1716 ret
= btrfs_next_leaf(root
, path
);
1723 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1725 if (key
.type
!= key_type
|| key
.objectid
!= dirid
) {
1729 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1730 struct btrfs_dir_log_item
);
1731 found_end
= btrfs_dir_log_end(path
->nodes
[0], item
);
1732 *start_ret
= key
.offset
;
1733 *end_ret
= found_end
;
1736 btrfs_release_path(path
);
1741 * this looks for a given directory item in the log. If the directory
1742 * item is not in the log, the item is removed and the inode it points
1745 static noinline
int check_item_in_log(struct btrfs_trans_handle
*trans
,
1746 struct btrfs_root
*root
,
1747 struct btrfs_root
*log
,
1748 struct btrfs_path
*path
,
1749 struct btrfs_path
*log_path
,
1751 struct btrfs_key
*dir_key
)
1754 struct extent_buffer
*eb
;
1757 struct btrfs_dir_item
*di
;
1758 struct btrfs_dir_item
*log_di
;
1761 unsigned long ptr_end
;
1763 struct inode
*inode
;
1764 struct btrfs_key location
;
1767 eb
= path
->nodes
[0];
1768 slot
= path
->slots
[0];
1769 item_size
= btrfs_item_size_nr(eb
, slot
);
1770 ptr
= btrfs_item_ptr_offset(eb
, slot
);
1771 ptr_end
= ptr
+ item_size
;
1772 while (ptr
< ptr_end
) {
1773 di
= (struct btrfs_dir_item
*)ptr
;
1774 if (verify_dir_item(root
, eb
, di
)) {
1779 name_len
= btrfs_dir_name_len(eb
, di
);
1780 name
= kmalloc(name_len
, GFP_NOFS
);
1785 read_extent_buffer(eb
, name
, (unsigned long)(di
+ 1),
1788 if (log
&& dir_key
->type
== BTRFS_DIR_ITEM_KEY
) {
1789 log_di
= btrfs_lookup_dir_item(trans
, log
, log_path
,
1792 } else if (log
&& dir_key
->type
== BTRFS_DIR_INDEX_KEY
) {
1793 log_di
= btrfs_lookup_dir_index_item(trans
, log
,
1799 if (IS_ERR_OR_NULL(log_di
)) {
1800 btrfs_dir_item_key_to_cpu(eb
, di
, &location
);
1801 btrfs_release_path(path
);
1802 btrfs_release_path(log_path
);
1803 inode
= read_one_inode(root
, location
.objectid
);
1809 ret
= link_to_fixup_dir(trans
, root
,
1810 path
, location
.objectid
);
1817 btrfs_inc_nlink(inode
);
1818 ret
= btrfs_unlink_inode(trans
, root
, dir
, inode
,
1821 ret
= btrfs_run_delayed_items(trans
, root
);
1827 /* there might still be more names under this key
1828 * check and repeat if required
1830 ret
= btrfs_search_slot(NULL
, root
, dir_key
, path
,
1837 btrfs_release_path(log_path
);
1840 ptr
= (unsigned long)(di
+ 1);
1845 btrfs_release_path(path
);
1846 btrfs_release_path(log_path
);
1851 * deletion replay happens before we copy any new directory items
1852 * out of the log or out of backreferences from inodes. It
1853 * scans the log to find ranges of keys that log is authoritative for,
1854 * and then scans the directory to find items in those ranges that are
1855 * not present in the log.
1857 * Anything we don't find in the log is unlinked and removed from the
1860 static noinline
int replay_dir_deletes(struct btrfs_trans_handle
*trans
,
1861 struct btrfs_root
*root
,
1862 struct btrfs_root
*log
,
1863 struct btrfs_path
*path
,
1864 u64 dirid
, int del_all
)
1868 int key_type
= BTRFS_DIR_LOG_ITEM_KEY
;
1870 struct btrfs_key dir_key
;
1871 struct btrfs_key found_key
;
1872 struct btrfs_path
*log_path
;
1875 dir_key
.objectid
= dirid
;
1876 dir_key
.type
= BTRFS_DIR_ITEM_KEY
;
1877 log_path
= btrfs_alloc_path();
1881 dir
= read_one_inode(root
, dirid
);
1882 /* it isn't an error if the inode isn't there, that can happen
1883 * because we replay the deletes before we copy in the inode item
1887 btrfs_free_path(log_path
);
1895 range_end
= (u64
)-1;
1897 ret
= find_dir_range(log
, path
, dirid
, key_type
,
1898 &range_start
, &range_end
);
1903 dir_key
.offset
= range_start
;
1906 ret
= btrfs_search_slot(NULL
, root
, &dir_key
, path
,
1911 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1912 if (path
->slots
[0] >= nritems
) {
1913 ret
= btrfs_next_leaf(root
, path
);
1917 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1919 if (found_key
.objectid
!= dirid
||
1920 found_key
.type
!= dir_key
.type
)
1923 if (found_key
.offset
> range_end
)
1926 ret
= check_item_in_log(trans
, root
, log
, path
,
1931 if (found_key
.offset
== (u64
)-1)
1933 dir_key
.offset
= found_key
.offset
+ 1;
1935 btrfs_release_path(path
);
1936 if (range_end
== (u64
)-1)
1938 range_start
= range_end
+ 1;
1943 if (key_type
== BTRFS_DIR_LOG_ITEM_KEY
) {
1944 key_type
= BTRFS_DIR_LOG_INDEX_KEY
;
1945 dir_key
.type
= BTRFS_DIR_INDEX_KEY
;
1946 btrfs_release_path(path
);
1950 btrfs_release_path(path
);
1951 btrfs_free_path(log_path
);
1957 * the process_func used to replay items from the log tree. This
1958 * gets called in two different stages. The first stage just looks
1959 * for inodes and makes sure they are all copied into the subvolume.
1961 * The second stage copies all the other item types from the log into
1962 * the subvolume. The two stage approach is slower, but gets rid of
1963 * lots of complexity around inodes referencing other inodes that exist
1964 * only in the log (references come from either directory items or inode
1967 static int replay_one_buffer(struct btrfs_root
*log
, struct extent_buffer
*eb
,
1968 struct walk_control
*wc
, u64 gen
)
1971 struct btrfs_path
*path
;
1972 struct btrfs_root
*root
= wc
->replay_dest
;
1973 struct btrfs_key key
;
1978 ret
= btrfs_read_buffer(eb
, gen
);
1982 level
= btrfs_header_level(eb
);
1987 path
= btrfs_alloc_path();
1991 nritems
= btrfs_header_nritems(eb
);
1992 for (i
= 0; i
< nritems
; i
++) {
1993 btrfs_item_key_to_cpu(eb
, &key
, i
);
1995 /* inode keys are done during the first stage */
1996 if (key
.type
== BTRFS_INODE_ITEM_KEY
&&
1997 wc
->stage
== LOG_WALK_REPLAY_INODES
) {
1998 struct btrfs_inode_item
*inode_item
;
2001 inode_item
= btrfs_item_ptr(eb
, i
,
2002 struct btrfs_inode_item
);
2003 mode
= btrfs_inode_mode(eb
, inode_item
);
2004 if (S_ISDIR(mode
)) {
2005 ret
= replay_dir_deletes(wc
->trans
,
2006 root
, log
, path
, key
.objectid
, 0);
2010 ret
= overwrite_item(wc
->trans
, root
, path
,
2015 /* for regular files, make sure corresponding
2016 * orhpan item exist. extents past the new EOF
2017 * will be truncated later by orphan cleanup.
2019 if (S_ISREG(mode
)) {
2020 ret
= insert_orphan_item(wc
->trans
, root
,
2026 ret
= link_to_fixup_dir(wc
->trans
, root
,
2027 path
, key
.objectid
);
2032 if (key
.type
== BTRFS_DIR_INDEX_KEY
&&
2033 wc
->stage
== LOG_WALK_REPLAY_DIR_INDEX
) {
2034 ret
= replay_one_dir_item(wc
->trans
, root
, path
,
2040 if (wc
->stage
< LOG_WALK_REPLAY_ALL
)
2043 /* these keys are simply copied */
2044 if (key
.type
== BTRFS_XATTR_ITEM_KEY
) {
2045 ret
= overwrite_item(wc
->trans
, root
, path
,
2049 } else if (key
.type
== BTRFS_INODE_REF_KEY
||
2050 key
.type
== BTRFS_INODE_EXTREF_KEY
) {
2051 ret
= add_inode_ref(wc
->trans
, root
, log
, path
,
2053 if (ret
&& ret
!= -ENOENT
)
2056 } else if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
2057 ret
= replay_one_extent(wc
->trans
, root
, path
,
2061 } else if (key
.type
== BTRFS_DIR_ITEM_KEY
) {
2062 ret
= replay_one_dir_item(wc
->trans
, root
, path
,
2068 btrfs_free_path(path
);
2072 static noinline
int walk_down_log_tree(struct btrfs_trans_handle
*trans
,
2073 struct btrfs_root
*root
,
2074 struct btrfs_path
*path
, int *level
,
2075 struct walk_control
*wc
)
2080 struct extent_buffer
*next
;
2081 struct extent_buffer
*cur
;
2082 struct extent_buffer
*parent
;
2086 WARN_ON(*level
< 0);
2087 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2089 while (*level
> 0) {
2090 WARN_ON(*level
< 0);
2091 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2092 cur
= path
->nodes
[*level
];
2094 if (btrfs_header_level(cur
) != *level
)
2097 if (path
->slots
[*level
] >=
2098 btrfs_header_nritems(cur
))
2101 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
2102 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
2103 blocksize
= btrfs_level_size(root
, *level
- 1);
2105 parent
= path
->nodes
[*level
];
2106 root_owner
= btrfs_header_owner(parent
);
2108 next
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
2113 ret
= wc
->process_func(root
, next
, wc
, ptr_gen
);
2115 free_extent_buffer(next
);
2119 path
->slots
[*level
]++;
2121 ret
= btrfs_read_buffer(next
, ptr_gen
);
2123 free_extent_buffer(next
);
2127 btrfs_tree_lock(next
);
2128 btrfs_set_lock_blocking(next
);
2129 clean_tree_block(trans
, root
, next
);
2130 btrfs_wait_tree_block_writeback(next
);
2131 btrfs_tree_unlock(next
);
2133 WARN_ON(root_owner
!=
2134 BTRFS_TREE_LOG_OBJECTID
);
2135 ret
= btrfs_free_and_pin_reserved_extent(root
,
2138 free_extent_buffer(next
);
2142 free_extent_buffer(next
);
2145 ret
= btrfs_read_buffer(next
, ptr_gen
);
2147 free_extent_buffer(next
);
2151 WARN_ON(*level
<= 0);
2152 if (path
->nodes
[*level
-1])
2153 free_extent_buffer(path
->nodes
[*level
-1]);
2154 path
->nodes
[*level
-1] = next
;
2155 *level
= btrfs_header_level(next
);
2156 path
->slots
[*level
] = 0;
2159 WARN_ON(*level
< 0);
2160 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2162 path
->slots
[*level
] = btrfs_header_nritems(path
->nodes
[*level
]);
2168 static noinline
int walk_up_log_tree(struct btrfs_trans_handle
*trans
,
2169 struct btrfs_root
*root
,
2170 struct btrfs_path
*path
, int *level
,
2171 struct walk_control
*wc
)
2178 for (i
= *level
; i
< BTRFS_MAX_LEVEL
- 1 && path
->nodes
[i
]; i
++) {
2179 slot
= path
->slots
[i
];
2180 if (slot
+ 1 < btrfs_header_nritems(path
->nodes
[i
])) {
2183 WARN_ON(*level
== 0);
2186 struct extent_buffer
*parent
;
2187 if (path
->nodes
[*level
] == root
->node
)
2188 parent
= path
->nodes
[*level
];
2190 parent
= path
->nodes
[*level
+ 1];
2192 root_owner
= btrfs_header_owner(parent
);
2193 ret
= wc
->process_func(root
, path
->nodes
[*level
], wc
,
2194 btrfs_header_generation(path
->nodes
[*level
]));
2199 struct extent_buffer
*next
;
2201 next
= path
->nodes
[*level
];
2203 btrfs_tree_lock(next
);
2204 btrfs_set_lock_blocking(next
);
2205 clean_tree_block(trans
, root
, next
);
2206 btrfs_wait_tree_block_writeback(next
);
2207 btrfs_tree_unlock(next
);
2209 WARN_ON(root_owner
!= BTRFS_TREE_LOG_OBJECTID
);
2210 ret
= btrfs_free_and_pin_reserved_extent(root
,
2211 path
->nodes
[*level
]->start
,
2212 path
->nodes
[*level
]->len
);
2216 free_extent_buffer(path
->nodes
[*level
]);
2217 path
->nodes
[*level
] = NULL
;
2225 * drop the reference count on the tree rooted at 'snap'. This traverses
2226 * the tree freeing any blocks that have a ref count of zero after being
2229 static int walk_log_tree(struct btrfs_trans_handle
*trans
,
2230 struct btrfs_root
*log
, struct walk_control
*wc
)
2235 struct btrfs_path
*path
;
2238 path
= btrfs_alloc_path();
2242 level
= btrfs_header_level(log
->node
);
2244 path
->nodes
[level
] = log
->node
;
2245 extent_buffer_get(log
->node
);
2246 path
->slots
[level
] = 0;
2249 wret
= walk_down_log_tree(trans
, log
, path
, &level
, wc
);
2257 wret
= walk_up_log_tree(trans
, log
, path
, &level
, wc
);
2266 /* was the root node processed? if not, catch it here */
2267 if (path
->nodes
[orig_level
]) {
2268 ret
= wc
->process_func(log
, path
->nodes
[orig_level
], wc
,
2269 btrfs_header_generation(path
->nodes
[orig_level
]));
2273 struct extent_buffer
*next
;
2275 next
= path
->nodes
[orig_level
];
2277 btrfs_tree_lock(next
);
2278 btrfs_set_lock_blocking(next
);
2279 clean_tree_block(trans
, log
, next
);
2280 btrfs_wait_tree_block_writeback(next
);
2281 btrfs_tree_unlock(next
);
2283 WARN_ON(log
->root_key
.objectid
!=
2284 BTRFS_TREE_LOG_OBJECTID
);
2285 ret
= btrfs_free_and_pin_reserved_extent(log
, next
->start
,
2293 btrfs_free_path(path
);
2298 * helper function to update the item for a given subvolumes log root
2299 * in the tree of log roots
2301 static int update_log_root(struct btrfs_trans_handle
*trans
,
2302 struct btrfs_root
*log
)
2306 if (log
->log_transid
== 1) {
2307 /* insert root item on the first sync */
2308 ret
= btrfs_insert_root(trans
, log
->fs_info
->log_root_tree
,
2309 &log
->root_key
, &log
->root_item
);
2311 ret
= btrfs_update_root(trans
, log
->fs_info
->log_root_tree
,
2312 &log
->root_key
, &log
->root_item
);
2317 static int wait_log_commit(struct btrfs_trans_handle
*trans
,
2318 struct btrfs_root
*root
, unsigned long transid
)
2321 int index
= transid
% 2;
2324 * we only allow two pending log transactions at a time,
2325 * so we know that if ours is more than 2 older than the
2326 * current transaction, we're done
2329 prepare_to_wait(&root
->log_commit_wait
[index
],
2330 &wait
, TASK_UNINTERRUPTIBLE
);
2331 mutex_unlock(&root
->log_mutex
);
2333 if (root
->fs_info
->last_trans_log_full_commit
!=
2334 trans
->transid
&& root
->log_transid
< transid
+ 2 &&
2335 atomic_read(&root
->log_commit
[index
]))
2338 finish_wait(&root
->log_commit_wait
[index
], &wait
);
2339 mutex_lock(&root
->log_mutex
);
2340 } while (root
->fs_info
->last_trans_log_full_commit
!=
2341 trans
->transid
&& root
->log_transid
< transid
+ 2 &&
2342 atomic_read(&root
->log_commit
[index
]));
2346 static void wait_for_writer(struct btrfs_trans_handle
*trans
,
2347 struct btrfs_root
*root
)
2350 while (root
->fs_info
->last_trans_log_full_commit
!=
2351 trans
->transid
&& atomic_read(&root
->log_writers
)) {
2352 prepare_to_wait(&root
->log_writer_wait
,
2353 &wait
, TASK_UNINTERRUPTIBLE
);
2354 mutex_unlock(&root
->log_mutex
);
2355 if (root
->fs_info
->last_trans_log_full_commit
!=
2356 trans
->transid
&& atomic_read(&root
->log_writers
))
2358 mutex_lock(&root
->log_mutex
);
2359 finish_wait(&root
->log_writer_wait
, &wait
);
2364 * btrfs_sync_log does sends a given tree log down to the disk and
2365 * updates the super blocks to record it. When this call is done,
2366 * you know that any inodes previously logged are safely on disk only
2369 * Any other return value means you need to call btrfs_commit_transaction.
2370 * Some of the edge cases for fsyncing directories that have had unlinks
2371 * or renames done in the past mean that sometimes the only safe
2372 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2373 * that has happened.
2375 int btrfs_sync_log(struct btrfs_trans_handle
*trans
,
2376 struct btrfs_root
*root
)
2382 struct btrfs_root
*log
= root
->log_root
;
2383 struct btrfs_root
*log_root_tree
= root
->fs_info
->log_root_tree
;
2384 unsigned long log_transid
= 0;
2385 struct blk_plug plug
;
2387 mutex_lock(&root
->log_mutex
);
2388 log_transid
= root
->log_transid
;
2389 index1
= root
->log_transid
% 2;
2390 if (atomic_read(&root
->log_commit
[index1
])) {
2391 wait_log_commit(trans
, root
, root
->log_transid
);
2392 mutex_unlock(&root
->log_mutex
);
2395 atomic_set(&root
->log_commit
[index1
], 1);
2397 /* wait for previous tree log sync to complete */
2398 if (atomic_read(&root
->log_commit
[(index1
+ 1) % 2]))
2399 wait_log_commit(trans
, root
, root
->log_transid
- 1);
2401 int batch
= atomic_read(&root
->log_batch
);
2402 /* when we're on an ssd, just kick the log commit out */
2403 if (!btrfs_test_opt(root
, SSD
) && root
->log_multiple_pids
) {
2404 mutex_unlock(&root
->log_mutex
);
2405 schedule_timeout_uninterruptible(1);
2406 mutex_lock(&root
->log_mutex
);
2408 wait_for_writer(trans
, root
);
2409 if (batch
== atomic_read(&root
->log_batch
))
2413 /* bail out if we need to do a full commit */
2414 if (root
->fs_info
->last_trans_log_full_commit
== trans
->transid
) {
2416 btrfs_free_logged_extents(log
, log_transid
);
2417 mutex_unlock(&root
->log_mutex
);
2421 if (log_transid
% 2 == 0)
2422 mark
= EXTENT_DIRTY
;
2426 /* we start IO on all the marked extents here, but we don't actually
2427 * wait for them until later.
2429 blk_start_plug(&plug
);
2430 ret
= btrfs_write_marked_extents(log
, &log
->dirty_log_pages
, mark
);
2432 blk_finish_plug(&plug
);
2433 btrfs_abort_transaction(trans
, root
, ret
);
2434 btrfs_free_logged_extents(log
, log_transid
);
2435 mutex_unlock(&root
->log_mutex
);
2439 btrfs_set_root_node(&log
->root_item
, log
->node
);
2441 root
->log_transid
++;
2442 log
->log_transid
= root
->log_transid
;
2443 root
->log_start_pid
= 0;
2446 * IO has been started, blocks of the log tree have WRITTEN flag set
2447 * in their headers. new modifications of the log will be written to
2448 * new positions. so it's safe to allow log writers to go in.
2450 mutex_unlock(&root
->log_mutex
);
2452 mutex_lock(&log_root_tree
->log_mutex
);
2453 atomic_inc(&log_root_tree
->log_batch
);
2454 atomic_inc(&log_root_tree
->log_writers
);
2455 mutex_unlock(&log_root_tree
->log_mutex
);
2457 ret
= update_log_root(trans
, log
);
2459 mutex_lock(&log_root_tree
->log_mutex
);
2460 if (atomic_dec_and_test(&log_root_tree
->log_writers
)) {
2462 if (waitqueue_active(&log_root_tree
->log_writer_wait
))
2463 wake_up(&log_root_tree
->log_writer_wait
);
2467 blk_finish_plug(&plug
);
2468 if (ret
!= -ENOSPC
) {
2469 btrfs_abort_transaction(trans
, root
, ret
);
2470 mutex_unlock(&log_root_tree
->log_mutex
);
2473 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
2474 btrfs_wait_marked_extents(log
, &log
->dirty_log_pages
, mark
);
2475 btrfs_free_logged_extents(log
, log_transid
);
2476 mutex_unlock(&log_root_tree
->log_mutex
);
2481 index2
= log_root_tree
->log_transid
% 2;
2482 if (atomic_read(&log_root_tree
->log_commit
[index2
])) {
2483 blk_finish_plug(&plug
);
2484 btrfs_wait_marked_extents(log
, &log
->dirty_log_pages
, mark
);
2485 wait_log_commit(trans
, log_root_tree
,
2486 log_root_tree
->log_transid
);
2487 btrfs_free_logged_extents(log
, log_transid
);
2488 mutex_unlock(&log_root_tree
->log_mutex
);
2492 atomic_set(&log_root_tree
->log_commit
[index2
], 1);
2494 if (atomic_read(&log_root_tree
->log_commit
[(index2
+ 1) % 2])) {
2495 wait_log_commit(trans
, log_root_tree
,
2496 log_root_tree
->log_transid
- 1);
2499 wait_for_writer(trans
, log_root_tree
);
2502 * now that we've moved on to the tree of log tree roots,
2503 * check the full commit flag again
2505 if (root
->fs_info
->last_trans_log_full_commit
== trans
->transid
) {
2506 blk_finish_plug(&plug
);
2507 btrfs_wait_marked_extents(log
, &log
->dirty_log_pages
, mark
);
2508 btrfs_free_logged_extents(log
, log_transid
);
2509 mutex_unlock(&log_root_tree
->log_mutex
);
2511 goto out_wake_log_root
;
2514 ret
= btrfs_write_marked_extents(log_root_tree
,
2515 &log_root_tree
->dirty_log_pages
,
2516 EXTENT_DIRTY
| EXTENT_NEW
);
2517 blk_finish_plug(&plug
);
2519 btrfs_abort_transaction(trans
, root
, ret
);
2520 btrfs_free_logged_extents(log
, log_transid
);
2521 mutex_unlock(&log_root_tree
->log_mutex
);
2522 goto out_wake_log_root
;
2524 btrfs_wait_marked_extents(log
, &log
->dirty_log_pages
, mark
);
2525 btrfs_wait_marked_extents(log_root_tree
,
2526 &log_root_tree
->dirty_log_pages
,
2527 EXTENT_NEW
| EXTENT_DIRTY
);
2528 btrfs_wait_logged_extents(log
, log_transid
);
2530 btrfs_set_super_log_root(root
->fs_info
->super_for_commit
,
2531 log_root_tree
->node
->start
);
2532 btrfs_set_super_log_root_level(root
->fs_info
->super_for_commit
,
2533 btrfs_header_level(log_root_tree
->node
));
2535 log_root_tree
->log_transid
++;
2538 mutex_unlock(&log_root_tree
->log_mutex
);
2541 * nobody else is going to jump in and write the the ctree
2542 * super here because the log_commit atomic below is protecting
2543 * us. We must be called with a transaction handle pinning
2544 * the running transaction open, so a full commit can't hop
2545 * in and cause problems either.
2547 btrfs_scrub_pause_super(root
);
2548 ret
= write_ctree_super(trans
, root
->fs_info
->tree_root
, 1);
2549 btrfs_scrub_continue_super(root
);
2551 btrfs_abort_transaction(trans
, root
, ret
);
2552 goto out_wake_log_root
;
2555 mutex_lock(&root
->log_mutex
);
2556 if (root
->last_log_commit
< log_transid
)
2557 root
->last_log_commit
= log_transid
;
2558 mutex_unlock(&root
->log_mutex
);
2561 atomic_set(&log_root_tree
->log_commit
[index2
], 0);
2563 if (waitqueue_active(&log_root_tree
->log_commit_wait
[index2
]))
2564 wake_up(&log_root_tree
->log_commit_wait
[index2
]);
2566 atomic_set(&root
->log_commit
[index1
], 0);
2568 if (waitqueue_active(&root
->log_commit_wait
[index1
]))
2569 wake_up(&root
->log_commit_wait
[index1
]);
2573 static void free_log_tree(struct btrfs_trans_handle
*trans
,
2574 struct btrfs_root
*log
)
2579 struct walk_control wc
= {
2581 .process_func
= process_one_buffer
2585 ret
= walk_log_tree(trans
, log
, &wc
);
2587 /* I don't think this can happen but just in case */
2589 btrfs_abort_transaction(trans
, log
, ret
);
2593 ret
= find_first_extent_bit(&log
->dirty_log_pages
,
2594 0, &start
, &end
, EXTENT_DIRTY
| EXTENT_NEW
,
2599 clear_extent_bits(&log
->dirty_log_pages
, start
, end
,
2600 EXTENT_DIRTY
| EXTENT_NEW
, GFP_NOFS
);
2604 * We may have short-circuited the log tree with the full commit logic
2605 * and left ordered extents on our list, so clear these out to keep us
2606 * from leaking inodes and memory.
2608 btrfs_free_logged_extents(log
, 0);
2609 btrfs_free_logged_extents(log
, 1);
2611 free_extent_buffer(log
->node
);
2616 * free all the extents used by the tree log. This should be called
2617 * at commit time of the full transaction
2619 int btrfs_free_log(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
)
2621 if (root
->log_root
) {
2622 free_log_tree(trans
, root
->log_root
);
2623 root
->log_root
= NULL
;
2628 int btrfs_free_log_root_tree(struct btrfs_trans_handle
*trans
,
2629 struct btrfs_fs_info
*fs_info
)
2631 if (fs_info
->log_root_tree
) {
2632 free_log_tree(trans
, fs_info
->log_root_tree
);
2633 fs_info
->log_root_tree
= NULL
;
2639 * If both a file and directory are logged, and unlinks or renames are
2640 * mixed in, we have a few interesting corners:
2642 * create file X in dir Y
2643 * link file X to X.link in dir Y
2645 * unlink file X but leave X.link
2648 * After a crash we would expect only X.link to exist. But file X
2649 * didn't get fsync'd again so the log has back refs for X and X.link.
2651 * We solve this by removing directory entries and inode backrefs from the
2652 * log when a file that was logged in the current transaction is
2653 * unlinked. Any later fsync will include the updated log entries, and
2654 * we'll be able to reconstruct the proper directory items from backrefs.
2656 * This optimizations allows us to avoid relogging the entire inode
2657 * or the entire directory.
2659 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle
*trans
,
2660 struct btrfs_root
*root
,
2661 const char *name
, int name_len
,
2662 struct inode
*dir
, u64 index
)
2664 struct btrfs_root
*log
;
2665 struct btrfs_dir_item
*di
;
2666 struct btrfs_path
*path
;
2670 u64 dir_ino
= btrfs_ino(dir
);
2672 if (BTRFS_I(dir
)->logged_trans
< trans
->transid
)
2675 ret
= join_running_log_trans(root
);
2679 mutex_lock(&BTRFS_I(dir
)->log_mutex
);
2681 log
= root
->log_root
;
2682 path
= btrfs_alloc_path();
2688 di
= btrfs_lookup_dir_item(trans
, log
, path
, dir_ino
,
2689 name
, name_len
, -1);
2695 ret
= btrfs_delete_one_dir_name(trans
, log
, path
, di
);
2696 bytes_del
+= name_len
;
2702 btrfs_release_path(path
);
2703 di
= btrfs_lookup_dir_index_item(trans
, log
, path
, dir_ino
,
2704 index
, name
, name_len
, -1);
2710 ret
= btrfs_delete_one_dir_name(trans
, log
, path
, di
);
2711 bytes_del
+= name_len
;
2718 /* update the directory size in the log to reflect the names
2722 struct btrfs_key key
;
2724 key
.objectid
= dir_ino
;
2726 key
.type
= BTRFS_INODE_ITEM_KEY
;
2727 btrfs_release_path(path
);
2729 ret
= btrfs_search_slot(trans
, log
, &key
, path
, 0, 1);
2735 struct btrfs_inode_item
*item
;
2738 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2739 struct btrfs_inode_item
);
2740 i_size
= btrfs_inode_size(path
->nodes
[0], item
);
2741 if (i_size
> bytes_del
)
2742 i_size
-= bytes_del
;
2745 btrfs_set_inode_size(path
->nodes
[0], item
, i_size
);
2746 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2749 btrfs_release_path(path
);
2752 btrfs_free_path(path
);
2754 mutex_unlock(&BTRFS_I(dir
)->log_mutex
);
2755 if (ret
== -ENOSPC
) {
2756 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
2759 btrfs_abort_transaction(trans
, root
, ret
);
2761 btrfs_end_log_trans(root
);
2766 /* see comments for btrfs_del_dir_entries_in_log */
2767 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle
*trans
,
2768 struct btrfs_root
*root
,
2769 const char *name
, int name_len
,
2770 struct inode
*inode
, u64 dirid
)
2772 struct btrfs_root
*log
;
2776 if (BTRFS_I(inode
)->logged_trans
< trans
->transid
)
2779 ret
= join_running_log_trans(root
);
2782 log
= root
->log_root
;
2783 mutex_lock(&BTRFS_I(inode
)->log_mutex
);
2785 ret
= btrfs_del_inode_ref(trans
, log
, name
, name_len
, btrfs_ino(inode
),
2787 mutex_unlock(&BTRFS_I(inode
)->log_mutex
);
2788 if (ret
== -ENOSPC
) {
2789 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
2791 } else if (ret
< 0 && ret
!= -ENOENT
)
2792 btrfs_abort_transaction(trans
, root
, ret
);
2793 btrfs_end_log_trans(root
);
2799 * creates a range item in the log for 'dirid'. first_offset and
2800 * last_offset tell us which parts of the key space the log should
2801 * be considered authoritative for.
2803 static noinline
int insert_dir_log_key(struct btrfs_trans_handle
*trans
,
2804 struct btrfs_root
*log
,
2805 struct btrfs_path
*path
,
2806 int key_type
, u64 dirid
,
2807 u64 first_offset
, u64 last_offset
)
2810 struct btrfs_key key
;
2811 struct btrfs_dir_log_item
*item
;
2813 key
.objectid
= dirid
;
2814 key
.offset
= first_offset
;
2815 if (key_type
== BTRFS_DIR_ITEM_KEY
)
2816 key
.type
= BTRFS_DIR_LOG_ITEM_KEY
;
2818 key
.type
= BTRFS_DIR_LOG_INDEX_KEY
;
2819 ret
= btrfs_insert_empty_item(trans
, log
, path
, &key
, sizeof(*item
));
2823 item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2824 struct btrfs_dir_log_item
);
2825 btrfs_set_dir_log_end(path
->nodes
[0], item
, last_offset
);
2826 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2827 btrfs_release_path(path
);
2832 * log all the items included in the current transaction for a given
2833 * directory. This also creates the range items in the log tree required
2834 * to replay anything deleted before the fsync
2836 static noinline
int log_dir_items(struct btrfs_trans_handle
*trans
,
2837 struct btrfs_root
*root
, struct inode
*inode
,
2838 struct btrfs_path
*path
,
2839 struct btrfs_path
*dst_path
, int key_type
,
2840 u64 min_offset
, u64
*last_offset_ret
)
2842 struct btrfs_key min_key
;
2843 struct btrfs_key max_key
;
2844 struct btrfs_root
*log
= root
->log_root
;
2845 struct extent_buffer
*src
;
2850 u64 first_offset
= min_offset
;
2851 u64 last_offset
= (u64
)-1;
2852 u64 ino
= btrfs_ino(inode
);
2854 log
= root
->log_root
;
2855 max_key
.objectid
= ino
;
2856 max_key
.offset
= (u64
)-1;
2857 max_key
.type
= key_type
;
2859 min_key
.objectid
= ino
;
2860 min_key
.type
= key_type
;
2861 min_key
.offset
= min_offset
;
2863 path
->keep_locks
= 1;
2865 ret
= btrfs_search_forward(root
, &min_key
, &max_key
,
2866 path
, trans
->transid
);
2869 * we didn't find anything from this transaction, see if there
2870 * is anything at all
2872 if (ret
!= 0 || min_key
.objectid
!= ino
|| min_key
.type
!= key_type
) {
2873 min_key
.objectid
= ino
;
2874 min_key
.type
= key_type
;
2875 min_key
.offset
= (u64
)-1;
2876 btrfs_release_path(path
);
2877 ret
= btrfs_search_slot(NULL
, root
, &min_key
, path
, 0, 0);
2879 btrfs_release_path(path
);
2882 ret
= btrfs_previous_item(root
, path
, ino
, key_type
);
2884 /* if ret == 0 there are items for this type,
2885 * create a range to tell us the last key of this type.
2886 * otherwise, there are no items in this directory after
2887 * *min_offset, and we create a range to indicate that.
2890 struct btrfs_key tmp
;
2891 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
,
2893 if (key_type
== tmp
.type
)
2894 first_offset
= max(min_offset
, tmp
.offset
) + 1;
2899 /* go backward to find any previous key */
2900 ret
= btrfs_previous_item(root
, path
, ino
, key_type
);
2902 struct btrfs_key tmp
;
2903 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
, path
->slots
[0]);
2904 if (key_type
== tmp
.type
) {
2905 first_offset
= tmp
.offset
;
2906 ret
= overwrite_item(trans
, log
, dst_path
,
2907 path
->nodes
[0], path
->slots
[0],
2915 btrfs_release_path(path
);
2917 /* find the first key from this transaction again */
2918 ret
= btrfs_search_slot(NULL
, root
, &min_key
, path
, 0, 0);
2925 * we have a block from this transaction, log every item in it
2926 * from our directory
2929 struct btrfs_key tmp
;
2930 src
= path
->nodes
[0];
2931 nritems
= btrfs_header_nritems(src
);
2932 for (i
= path
->slots
[0]; i
< nritems
; i
++) {
2933 btrfs_item_key_to_cpu(src
, &min_key
, i
);
2935 if (min_key
.objectid
!= ino
|| min_key
.type
!= key_type
)
2937 ret
= overwrite_item(trans
, log
, dst_path
, src
, i
,
2944 path
->slots
[0] = nritems
;
2947 * look ahead to the next item and see if it is also
2948 * from this directory and from this transaction
2950 ret
= btrfs_next_leaf(root
, path
);
2952 last_offset
= (u64
)-1;
2955 btrfs_item_key_to_cpu(path
->nodes
[0], &tmp
, path
->slots
[0]);
2956 if (tmp
.objectid
!= ino
|| tmp
.type
!= key_type
) {
2957 last_offset
= (u64
)-1;
2960 if (btrfs_header_generation(path
->nodes
[0]) != trans
->transid
) {
2961 ret
= overwrite_item(trans
, log
, dst_path
,
2962 path
->nodes
[0], path
->slots
[0],
2967 last_offset
= tmp
.offset
;
2972 btrfs_release_path(path
);
2973 btrfs_release_path(dst_path
);
2976 *last_offset_ret
= last_offset
;
2978 * insert the log range keys to indicate where the log
2981 ret
= insert_dir_log_key(trans
, log
, path
, key_type
,
2982 ino
, first_offset
, last_offset
);
2990 * logging directories is very similar to logging inodes, We find all the items
2991 * from the current transaction and write them to the log.
2993 * The recovery code scans the directory in the subvolume, and if it finds a
2994 * key in the range logged that is not present in the log tree, then it means
2995 * that dir entry was unlinked during the transaction.
2997 * In order for that scan to work, we must include one key smaller than
2998 * the smallest logged by this transaction and one key larger than the largest
2999 * key logged by this transaction.
3001 static noinline
int log_directory_changes(struct btrfs_trans_handle
*trans
,
3002 struct btrfs_root
*root
, struct inode
*inode
,
3003 struct btrfs_path
*path
,
3004 struct btrfs_path
*dst_path
)
3009 int key_type
= BTRFS_DIR_ITEM_KEY
;
3015 ret
= log_dir_items(trans
, root
, inode
, path
,
3016 dst_path
, key_type
, min_key
,
3020 if (max_key
== (u64
)-1)
3022 min_key
= max_key
+ 1;
3025 if (key_type
== BTRFS_DIR_ITEM_KEY
) {
3026 key_type
= BTRFS_DIR_INDEX_KEY
;
3033 * a helper function to drop items from the log before we relog an
3034 * inode. max_key_type indicates the highest item type to remove.
3035 * This cannot be run for file data extents because it does not
3036 * free the extents they point to.
3038 static int drop_objectid_items(struct btrfs_trans_handle
*trans
,
3039 struct btrfs_root
*log
,
3040 struct btrfs_path
*path
,
3041 u64 objectid
, int max_key_type
)
3044 struct btrfs_key key
;
3045 struct btrfs_key found_key
;
3048 key
.objectid
= objectid
;
3049 key
.type
= max_key_type
;
3050 key
.offset
= (u64
)-1;
3053 ret
= btrfs_search_slot(trans
, log
, &key
, path
, -1, 1);
3054 BUG_ON(ret
== 0); /* Logic error */
3058 if (path
->slots
[0] == 0)
3062 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
3065 if (found_key
.objectid
!= objectid
)
3068 found_key
.offset
= 0;
3070 ret
= btrfs_bin_search(path
->nodes
[0], &found_key
, 0,
3073 ret
= btrfs_del_items(trans
, log
, path
, start_slot
,
3074 path
->slots
[0] - start_slot
+ 1);
3076 * If start slot isn't 0 then we don't need to re-search, we've
3077 * found the last guy with the objectid in this tree.
3079 if (ret
|| start_slot
!= 0)
3081 btrfs_release_path(path
);
3083 btrfs_release_path(path
);
3089 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3090 struct extent_buffer
*leaf
,
3091 struct btrfs_inode_item
*item
,
3092 struct inode
*inode
, int log_inode_only
)
3094 struct btrfs_map_token token
;
3096 btrfs_init_map_token(&token
);
3098 if (log_inode_only
) {
3099 /* set the generation to zero so the recover code
3100 * can tell the difference between an logging
3101 * just to say 'this inode exists' and a logging
3102 * to say 'update this inode with these values'
3104 btrfs_set_token_inode_generation(leaf
, item
, 0, &token
);
3105 btrfs_set_token_inode_size(leaf
, item
, 0, &token
);
3107 btrfs_set_token_inode_generation(leaf
, item
,
3108 BTRFS_I(inode
)->generation
,
3110 btrfs_set_token_inode_size(leaf
, item
, inode
->i_size
, &token
);
3113 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3114 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3115 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3116 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3118 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_atime(item
),
3119 inode
->i_atime
.tv_sec
, &token
);
3120 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_atime(item
),
3121 inode
->i_atime
.tv_nsec
, &token
);
3123 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_mtime(item
),
3124 inode
->i_mtime
.tv_sec
, &token
);
3125 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
3126 inode
->i_mtime
.tv_nsec
, &token
);
3128 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_ctime(item
),
3129 inode
->i_ctime
.tv_sec
, &token
);
3130 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
3131 inode
->i_ctime
.tv_nsec
, &token
);
3133 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3136 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3137 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3138 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3139 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3140 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3143 static int log_inode_item(struct btrfs_trans_handle
*trans
,
3144 struct btrfs_root
*log
, struct btrfs_path
*path
,
3145 struct inode
*inode
)
3147 struct btrfs_inode_item
*inode_item
;
3148 struct btrfs_key key
;
3151 memcpy(&key
, &BTRFS_I(inode
)->location
, sizeof(key
));
3152 ret
= btrfs_insert_empty_item(trans
, log
, path
, &key
,
3153 sizeof(*inode_item
));
3154 if (ret
&& ret
!= -EEXIST
)
3156 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3157 struct btrfs_inode_item
);
3158 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
, 0);
3159 btrfs_release_path(path
);
3163 static noinline
int copy_items(struct btrfs_trans_handle
*trans
,
3164 struct inode
*inode
,
3165 struct btrfs_path
*dst_path
,
3166 struct extent_buffer
*src
,
3167 int start_slot
, int nr
, int inode_only
)
3169 unsigned long src_offset
;
3170 unsigned long dst_offset
;
3171 struct btrfs_root
*log
= BTRFS_I(inode
)->root
->log_root
;
3172 struct btrfs_file_extent_item
*extent
;
3173 struct btrfs_inode_item
*inode_item
;
3175 struct btrfs_key
*ins_keys
;
3179 struct list_head ordered_sums
;
3180 int skip_csum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
3182 INIT_LIST_HEAD(&ordered_sums
);
3184 ins_data
= kmalloc(nr
* sizeof(struct btrfs_key
) +
3185 nr
* sizeof(u32
), GFP_NOFS
);
3189 ins_sizes
= (u32
*)ins_data
;
3190 ins_keys
= (struct btrfs_key
*)(ins_data
+ nr
* sizeof(u32
));
3192 for (i
= 0; i
< nr
; i
++) {
3193 ins_sizes
[i
] = btrfs_item_size_nr(src
, i
+ start_slot
);
3194 btrfs_item_key_to_cpu(src
, ins_keys
+ i
, i
+ start_slot
);
3196 ret
= btrfs_insert_empty_items(trans
, log
, dst_path
,
3197 ins_keys
, ins_sizes
, nr
);
3203 for (i
= 0; i
< nr
; i
++, dst_path
->slots
[0]++) {
3204 dst_offset
= btrfs_item_ptr_offset(dst_path
->nodes
[0],
3205 dst_path
->slots
[0]);
3207 src_offset
= btrfs_item_ptr_offset(src
, start_slot
+ i
);
3209 if (ins_keys
[i
].type
== BTRFS_INODE_ITEM_KEY
) {
3210 inode_item
= btrfs_item_ptr(dst_path
->nodes
[0],
3212 struct btrfs_inode_item
);
3213 fill_inode_item(trans
, dst_path
->nodes
[0], inode_item
,
3214 inode
, inode_only
== LOG_INODE_EXISTS
);
3216 copy_extent_buffer(dst_path
->nodes
[0], src
, dst_offset
,
3217 src_offset
, ins_sizes
[i
]);
3220 /* take a reference on file data extents so that truncates
3221 * or deletes of this inode don't have to relog the inode
3224 if (btrfs_key_type(ins_keys
+ i
) == BTRFS_EXTENT_DATA_KEY
&&
3227 extent
= btrfs_item_ptr(src
, start_slot
+ i
,
3228 struct btrfs_file_extent_item
);
3230 if (btrfs_file_extent_generation(src
, extent
) < trans
->transid
)
3233 found_type
= btrfs_file_extent_type(src
, extent
);
3234 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
3236 ds
= btrfs_file_extent_disk_bytenr(src
,
3238 /* ds == 0 is a hole */
3242 dl
= btrfs_file_extent_disk_num_bytes(src
,
3244 cs
= btrfs_file_extent_offset(src
, extent
);
3245 cl
= btrfs_file_extent_num_bytes(src
,
3247 if (btrfs_file_extent_compression(src
,
3253 ret
= btrfs_lookup_csums_range(
3254 log
->fs_info
->csum_root
,
3255 ds
+ cs
, ds
+ cs
+ cl
- 1,
3258 btrfs_release_path(dst_path
);
3266 btrfs_mark_buffer_dirty(dst_path
->nodes
[0]);
3267 btrfs_release_path(dst_path
);
3271 * we have to do this after the loop above to avoid changing the
3272 * log tree while trying to change the log tree.
3275 while (!list_empty(&ordered_sums
)) {
3276 struct btrfs_ordered_sum
*sums
= list_entry(ordered_sums
.next
,
3277 struct btrfs_ordered_sum
,
3280 ret
= btrfs_csum_file_blocks(trans
, log
, sums
);
3281 list_del(&sums
->list
);
3287 static int extent_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
3289 struct extent_map
*em1
, *em2
;
3291 em1
= list_entry(a
, struct extent_map
, list
);
3292 em2
= list_entry(b
, struct extent_map
, list
);
3294 if (em1
->start
< em2
->start
)
3296 else if (em1
->start
> em2
->start
)
3301 static int log_one_extent(struct btrfs_trans_handle
*trans
,
3302 struct inode
*inode
, struct btrfs_root
*root
,
3303 struct extent_map
*em
, struct btrfs_path
*path
)
3305 struct btrfs_root
*log
= root
->log_root
;
3306 struct btrfs_file_extent_item
*fi
;
3307 struct extent_buffer
*leaf
;
3308 struct btrfs_ordered_extent
*ordered
;
3309 struct list_head ordered_sums
;
3310 struct btrfs_map_token token
;
3311 struct btrfs_key key
;
3312 u64 mod_start
= em
->mod_start
;
3313 u64 mod_len
= em
->mod_len
;
3316 u64 extent_offset
= em
->start
- em
->orig_start
;
3319 int index
= log
->log_transid
% 2;
3320 bool skip_csum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
3322 ret
= __btrfs_drop_extents(trans
, log
, inode
, path
, em
->start
,
3323 em
->start
+ em
->len
, NULL
, 0);
3327 INIT_LIST_HEAD(&ordered_sums
);
3328 btrfs_init_map_token(&token
);
3329 key
.objectid
= btrfs_ino(inode
);
3330 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3331 key
.offset
= em
->start
;
3333 ret
= btrfs_insert_empty_item(trans
, log
, path
, &key
, sizeof(*fi
));
3336 leaf
= path
->nodes
[0];
3337 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3338 struct btrfs_file_extent_item
);
3340 btrfs_set_token_file_extent_generation(leaf
, fi
, em
->generation
,
3342 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
3344 btrfs_set_token_file_extent_type(leaf
, fi
,
3345 BTRFS_FILE_EXTENT_PREALLOC
,
3348 btrfs_set_token_file_extent_type(leaf
, fi
,
3349 BTRFS_FILE_EXTENT_REG
,
3351 if (em
->block_start
== 0)
3355 block_len
= max(em
->block_len
, em
->orig_block_len
);
3356 if (em
->compress_type
!= BTRFS_COMPRESS_NONE
) {
3357 btrfs_set_token_file_extent_disk_bytenr(leaf
, fi
,
3360 btrfs_set_token_file_extent_disk_num_bytes(leaf
, fi
, block_len
,
3362 } else if (em
->block_start
< EXTENT_MAP_LAST_BYTE
) {
3363 btrfs_set_token_file_extent_disk_bytenr(leaf
, fi
,
3365 extent_offset
, &token
);
3366 btrfs_set_token_file_extent_disk_num_bytes(leaf
, fi
, block_len
,
3369 btrfs_set_token_file_extent_disk_bytenr(leaf
, fi
, 0, &token
);
3370 btrfs_set_token_file_extent_disk_num_bytes(leaf
, fi
, 0,
3374 btrfs_set_token_file_extent_offset(leaf
, fi
,
3375 em
->start
- em
->orig_start
,
3377 btrfs_set_token_file_extent_num_bytes(leaf
, fi
, em
->len
, &token
);
3378 btrfs_set_token_file_extent_ram_bytes(leaf
, fi
, em
->ram_bytes
, &token
);
3379 btrfs_set_token_file_extent_compression(leaf
, fi
, em
->compress_type
,
3381 btrfs_set_token_file_extent_encryption(leaf
, fi
, 0, &token
);
3382 btrfs_set_token_file_extent_other_encoding(leaf
, fi
, 0, &token
);
3383 btrfs_mark_buffer_dirty(leaf
);
3385 btrfs_release_path(path
);
3393 if (em
->compress_type
) {
3395 csum_len
= block_len
;
3399 * First check and see if our csums are on our outstanding ordered
3403 spin_lock_irq(&log
->log_extents_lock
[index
]);
3404 list_for_each_entry(ordered
, &log
->logged_list
[index
], log_list
) {
3405 struct btrfs_ordered_sum
*sum
;
3410 if (ordered
->inode
!= inode
)
3413 if (ordered
->file_offset
+ ordered
->len
<= mod_start
||
3414 mod_start
+ mod_len
<= ordered
->file_offset
)
3418 * We are going to copy all the csums on this ordered extent, so
3419 * go ahead and adjust mod_start and mod_len in case this
3420 * ordered extent has already been logged.
3422 if (ordered
->file_offset
> mod_start
) {
3423 if (ordered
->file_offset
+ ordered
->len
>=
3424 mod_start
+ mod_len
)
3425 mod_len
= ordered
->file_offset
- mod_start
;
3427 * If we have this case
3429 * |--------- logged extent ---------|
3430 * |----- ordered extent ----|
3432 * Just don't mess with mod_start and mod_len, we'll
3433 * just end up logging more csums than we need and it
3437 if (ordered
->file_offset
+ ordered
->len
<
3438 mod_start
+ mod_len
) {
3439 mod_len
= (mod_start
+ mod_len
) -
3440 (ordered
->file_offset
+ ordered
->len
);
3441 mod_start
= ordered
->file_offset
+
3449 * To keep us from looping for the above case of an ordered
3450 * extent that falls inside of the logged extent.
3452 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM
,
3455 atomic_inc(&ordered
->refs
);
3456 spin_unlock_irq(&log
->log_extents_lock
[index
]);
3458 * we've dropped the lock, we must either break or
3459 * start over after this.
3462 wait_event(ordered
->wait
, ordered
->csum_bytes_left
== 0);
3464 list_for_each_entry(sum
, &ordered
->list
, list
) {
3465 ret
= btrfs_csum_file_blocks(trans
, log
, sum
);
3467 btrfs_put_ordered_extent(ordered
);
3471 btrfs_put_ordered_extent(ordered
);
3475 spin_unlock_irq(&log
->log_extents_lock
[index
]);
3478 if (!mod_len
|| ret
)
3481 csum_offset
= mod_start
- em
->start
;
3484 /* block start is already adjusted for the file extent offset. */
3485 ret
= btrfs_lookup_csums_range(log
->fs_info
->csum_root
,
3486 em
->block_start
+ csum_offset
,
3487 em
->block_start
+ csum_offset
+
3488 csum_len
- 1, &ordered_sums
, 0);
3492 while (!list_empty(&ordered_sums
)) {
3493 struct btrfs_ordered_sum
*sums
= list_entry(ordered_sums
.next
,
3494 struct btrfs_ordered_sum
,
3497 ret
= btrfs_csum_file_blocks(trans
, log
, sums
);
3498 list_del(&sums
->list
);
3505 static int btrfs_log_changed_extents(struct btrfs_trans_handle
*trans
,
3506 struct btrfs_root
*root
,
3507 struct inode
*inode
,
3508 struct btrfs_path
*path
)
3510 struct extent_map
*em
, *n
;
3511 struct list_head extents
;
3512 struct extent_map_tree
*tree
= &BTRFS_I(inode
)->extent_tree
;
3517 INIT_LIST_HEAD(&extents
);
3519 write_lock(&tree
->lock
);
3520 test_gen
= root
->fs_info
->last_trans_committed
;
3522 list_for_each_entry_safe(em
, n
, &tree
->modified_extents
, list
) {
3523 list_del_init(&em
->list
);
3526 * Just an arbitrary number, this can be really CPU intensive
3527 * once we start getting a lot of extents, and really once we
3528 * have a bunch of extents we just want to commit since it will
3531 if (++num
> 32768) {
3532 list_del_init(&tree
->modified_extents
);
3537 if (em
->generation
<= test_gen
)
3539 /* Need a ref to keep it from getting evicted from cache */
3540 atomic_inc(&em
->refs
);
3541 set_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
3542 list_add_tail(&em
->list
, &extents
);
3546 list_sort(NULL
, &extents
, extent_cmp
);
3549 while (!list_empty(&extents
)) {
3550 em
= list_entry(extents
.next
, struct extent_map
, list
);
3552 list_del_init(&em
->list
);
3555 * If we had an error we just need to delete everybody from our
3559 clear_em_logging(tree
, em
);
3560 free_extent_map(em
);
3564 write_unlock(&tree
->lock
);
3566 ret
= log_one_extent(trans
, inode
, root
, em
, path
);
3567 write_lock(&tree
->lock
);
3568 clear_em_logging(tree
, em
);
3569 free_extent_map(em
);
3571 WARN_ON(!list_empty(&extents
));
3572 write_unlock(&tree
->lock
);
3574 btrfs_release_path(path
);
3578 /* log a single inode in the tree log.
3579 * At least one parent directory for this inode must exist in the tree
3580 * or be logged already.
3582 * Any items from this inode changed by the current transaction are copied
3583 * to the log tree. An extra reference is taken on any extents in this
3584 * file, allowing us to avoid a whole pile of corner cases around logging
3585 * blocks that have been removed from the tree.
3587 * See LOG_INODE_ALL and related defines for a description of what inode_only
3590 * This handles both files and directories.
3592 static int btrfs_log_inode(struct btrfs_trans_handle
*trans
,
3593 struct btrfs_root
*root
, struct inode
*inode
,
3596 struct btrfs_path
*path
;
3597 struct btrfs_path
*dst_path
;
3598 struct btrfs_key min_key
;
3599 struct btrfs_key max_key
;
3600 struct btrfs_root
*log
= root
->log_root
;
3601 struct extent_buffer
*src
= NULL
;
3605 int ins_start_slot
= 0;
3607 bool fast_search
= false;
3608 u64 ino
= btrfs_ino(inode
);
3610 path
= btrfs_alloc_path();
3613 dst_path
= btrfs_alloc_path();
3615 btrfs_free_path(path
);
3619 min_key
.objectid
= ino
;
3620 min_key
.type
= BTRFS_INODE_ITEM_KEY
;
3623 max_key
.objectid
= ino
;
3626 /* today the code can only do partial logging of directories */
3627 if (S_ISDIR(inode
->i_mode
) ||
3628 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3629 &BTRFS_I(inode
)->runtime_flags
) &&
3630 inode_only
== LOG_INODE_EXISTS
))
3631 max_key
.type
= BTRFS_XATTR_ITEM_KEY
;
3633 max_key
.type
= (u8
)-1;
3634 max_key
.offset
= (u64
)-1;
3636 /* Only run delayed items if we are a dir or a new file */
3637 if (S_ISDIR(inode
->i_mode
) ||
3638 BTRFS_I(inode
)->generation
> root
->fs_info
->last_trans_committed
) {
3639 ret
= btrfs_commit_inode_delayed_items(trans
, inode
);
3641 btrfs_free_path(path
);
3642 btrfs_free_path(dst_path
);
3647 mutex_lock(&BTRFS_I(inode
)->log_mutex
);
3649 btrfs_get_logged_extents(log
, inode
);
3652 * a brute force approach to making sure we get the most uptodate
3653 * copies of everything.
3655 if (S_ISDIR(inode
->i_mode
)) {
3656 int max_key_type
= BTRFS_DIR_LOG_INDEX_KEY
;
3658 if (inode_only
== LOG_INODE_EXISTS
)
3659 max_key_type
= BTRFS_XATTR_ITEM_KEY
;
3660 ret
= drop_objectid_items(trans
, log
, path
, ino
, max_key_type
);
3662 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3663 &BTRFS_I(inode
)->runtime_flags
)) {
3664 clear_bit(BTRFS_INODE_COPY_EVERYTHING
,
3665 &BTRFS_I(inode
)->runtime_flags
);
3666 ret
= btrfs_truncate_inode_items(trans
, log
,
3668 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING
,
3669 &BTRFS_I(inode
)->runtime_flags
)) {
3670 if (inode_only
== LOG_INODE_ALL
)
3672 max_key
.type
= BTRFS_XATTR_ITEM_KEY
;
3673 ret
= drop_objectid_items(trans
, log
, path
, ino
,
3676 if (inode_only
== LOG_INODE_ALL
)
3678 ret
= log_inode_item(trans
, log
, dst_path
, inode
);
3691 path
->keep_locks
= 1;
3695 ret
= btrfs_search_forward(root
, &min_key
, &max_key
,
3696 path
, trans
->transid
);
3700 /* note, ins_nr might be > 0 here, cleanup outside the loop */
3701 if (min_key
.objectid
!= ino
)
3703 if (min_key
.type
> max_key
.type
)
3706 src
= path
->nodes
[0];
3707 if (ins_nr
&& ins_start_slot
+ ins_nr
== path
->slots
[0]) {
3710 } else if (!ins_nr
) {
3711 ins_start_slot
= path
->slots
[0];
3716 ret
= copy_items(trans
, inode
, dst_path
, src
, ins_start_slot
,
3717 ins_nr
, inode_only
);
3723 ins_start_slot
= path
->slots
[0];
3726 nritems
= btrfs_header_nritems(path
->nodes
[0]);
3728 if (path
->slots
[0] < nritems
) {
3729 btrfs_item_key_to_cpu(path
->nodes
[0], &min_key
,
3734 ret
= copy_items(trans
, inode
, dst_path
, src
,
3736 ins_nr
, inode_only
);
3743 btrfs_release_path(path
);
3745 if (min_key
.offset
< (u64
)-1)
3747 else if (min_key
.type
< (u8
)-1)
3749 else if (min_key
.objectid
< (u64
)-1)
3755 ret
= copy_items(trans
, inode
, dst_path
, src
, ins_start_slot
,
3756 ins_nr
, inode_only
);
3765 btrfs_release_path(path
);
3766 btrfs_release_path(dst_path
);
3768 ret
= btrfs_log_changed_extents(trans
, root
, inode
, dst_path
);
3774 struct extent_map_tree
*tree
= &BTRFS_I(inode
)->extent_tree
;
3775 struct extent_map
*em
, *n
;
3777 write_lock(&tree
->lock
);
3778 list_for_each_entry_safe(em
, n
, &tree
->modified_extents
, list
)
3779 list_del_init(&em
->list
);
3780 write_unlock(&tree
->lock
);
3783 if (inode_only
== LOG_INODE_ALL
&& S_ISDIR(inode
->i_mode
)) {
3784 ret
= log_directory_changes(trans
, root
, inode
, path
, dst_path
);
3790 BTRFS_I(inode
)->logged_trans
= trans
->transid
;
3791 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->last_sub_trans
;
3794 btrfs_free_logged_extents(log
, log
->log_transid
);
3795 mutex_unlock(&BTRFS_I(inode
)->log_mutex
);
3797 btrfs_free_path(path
);
3798 btrfs_free_path(dst_path
);
3803 * follow the dentry parent pointers up the chain and see if any
3804 * of the directories in it require a full commit before they can
3805 * be logged. Returns zero if nothing special needs to be done or 1 if
3806 * a full commit is required.
3808 static noinline
int check_parent_dirs_for_sync(struct btrfs_trans_handle
*trans
,
3809 struct inode
*inode
,
3810 struct dentry
*parent
,
3811 struct super_block
*sb
,
3815 struct btrfs_root
*root
;
3816 struct dentry
*old_parent
= NULL
;
3817 struct inode
*orig_inode
= inode
;
3820 * for regular files, if its inode is already on disk, we don't
3821 * have to worry about the parents at all. This is because
3822 * we can use the last_unlink_trans field to record renames
3823 * and other fun in this file.
3825 if (S_ISREG(inode
->i_mode
) &&
3826 BTRFS_I(inode
)->generation
<= last_committed
&&
3827 BTRFS_I(inode
)->last_unlink_trans
<= last_committed
)
3830 if (!S_ISDIR(inode
->i_mode
)) {
3831 if (!parent
|| !parent
->d_inode
|| sb
!= parent
->d_inode
->i_sb
)
3833 inode
= parent
->d_inode
;
3838 * If we are logging a directory then we start with our inode,
3839 * not our parents inode, so we need to skipp setting the
3840 * logged_trans so that further down in the log code we don't
3841 * think this inode has already been logged.
3843 if (inode
!= orig_inode
)
3844 BTRFS_I(inode
)->logged_trans
= trans
->transid
;
3847 if (BTRFS_I(inode
)->last_unlink_trans
> last_committed
) {
3848 root
= BTRFS_I(inode
)->root
;
3851 * make sure any commits to the log are forced
3852 * to be full commits
3854 root
->fs_info
->last_trans_log_full_commit
=
3860 if (!parent
|| !parent
->d_inode
|| sb
!= parent
->d_inode
->i_sb
)
3863 if (IS_ROOT(parent
))
3866 parent
= dget_parent(parent
);
3868 old_parent
= parent
;
3869 inode
= parent
->d_inode
;
3878 * helper function around btrfs_log_inode to make sure newly created
3879 * parent directories also end up in the log. A minimal inode and backref
3880 * only logging is done of any parent directories that are older than
3881 * the last committed transaction
3883 static int btrfs_log_inode_parent(struct btrfs_trans_handle
*trans
,
3884 struct btrfs_root
*root
, struct inode
*inode
,
3885 struct dentry
*parent
, int exists_only
)
3887 int inode_only
= exists_only
? LOG_INODE_EXISTS
: LOG_INODE_ALL
;
3888 struct super_block
*sb
;
3889 struct dentry
*old_parent
= NULL
;
3891 u64 last_committed
= root
->fs_info
->last_trans_committed
;
3895 if (btrfs_test_opt(root
, NOTREELOG
)) {
3900 if (root
->fs_info
->last_trans_log_full_commit
>
3901 root
->fs_info
->last_trans_committed
) {
3906 if (root
!= BTRFS_I(inode
)->root
||
3907 btrfs_root_refs(&root
->root_item
) == 0) {
3912 ret
= check_parent_dirs_for_sync(trans
, inode
, parent
,
3913 sb
, last_committed
);
3917 if (btrfs_inode_in_log(inode
, trans
->transid
)) {
3918 ret
= BTRFS_NO_LOG_SYNC
;
3922 ret
= start_log_trans(trans
, root
);
3926 ret
= btrfs_log_inode(trans
, root
, inode
, inode_only
);
3931 * for regular files, if its inode is already on disk, we don't
3932 * have to worry about the parents at all. This is because
3933 * we can use the last_unlink_trans field to record renames
3934 * and other fun in this file.
3936 if (S_ISREG(inode
->i_mode
) &&
3937 BTRFS_I(inode
)->generation
<= last_committed
&&
3938 BTRFS_I(inode
)->last_unlink_trans
<= last_committed
) {
3943 inode_only
= LOG_INODE_EXISTS
;
3945 if (!parent
|| !parent
->d_inode
|| sb
!= parent
->d_inode
->i_sb
)
3948 inode
= parent
->d_inode
;
3949 if (root
!= BTRFS_I(inode
)->root
)
3952 if (BTRFS_I(inode
)->generation
>
3953 root
->fs_info
->last_trans_committed
) {
3954 ret
= btrfs_log_inode(trans
, root
, inode
, inode_only
);
3958 if (IS_ROOT(parent
))
3961 parent
= dget_parent(parent
);
3963 old_parent
= parent
;
3969 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
3972 btrfs_end_log_trans(root
);
3978 * it is not safe to log dentry if the chunk root has added new
3979 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3980 * If this returns 1, you must commit the transaction to safely get your
3983 int btrfs_log_dentry_safe(struct btrfs_trans_handle
*trans
,
3984 struct btrfs_root
*root
, struct dentry
*dentry
)
3986 struct dentry
*parent
= dget_parent(dentry
);
3989 ret
= btrfs_log_inode_parent(trans
, root
, dentry
->d_inode
, parent
, 0);
3996 * should be called during mount to recover any replay any log trees
3999 int btrfs_recover_log_trees(struct btrfs_root
*log_root_tree
)
4002 struct btrfs_path
*path
;
4003 struct btrfs_trans_handle
*trans
;
4004 struct btrfs_key key
;
4005 struct btrfs_key found_key
;
4006 struct btrfs_key tmp_key
;
4007 struct btrfs_root
*log
;
4008 struct btrfs_fs_info
*fs_info
= log_root_tree
->fs_info
;
4009 struct walk_control wc
= {
4010 .process_func
= process_one_buffer
,
4014 path
= btrfs_alloc_path();
4018 fs_info
->log_root_recovering
= 1;
4020 trans
= btrfs_start_transaction(fs_info
->tree_root
, 0);
4021 if (IS_ERR(trans
)) {
4022 ret
= PTR_ERR(trans
);
4029 ret
= walk_log_tree(trans
, log_root_tree
, &wc
);
4031 btrfs_error(fs_info
, ret
, "Failed to pin buffers while "
4032 "recovering log root tree.");
4037 key
.objectid
= BTRFS_TREE_LOG_OBJECTID
;
4038 key
.offset
= (u64
)-1;
4039 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
4042 ret
= btrfs_search_slot(NULL
, log_root_tree
, &key
, path
, 0, 0);
4045 btrfs_error(fs_info
, ret
,
4046 "Couldn't find tree log root.");
4050 if (path
->slots
[0] == 0)
4054 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
4056 btrfs_release_path(path
);
4057 if (found_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
4060 log
= btrfs_read_fs_root(log_root_tree
, &found_key
);
4063 btrfs_error(fs_info
, ret
,
4064 "Couldn't read tree log root.");
4068 tmp_key
.objectid
= found_key
.offset
;
4069 tmp_key
.type
= BTRFS_ROOT_ITEM_KEY
;
4070 tmp_key
.offset
= (u64
)-1;
4072 wc
.replay_dest
= btrfs_read_fs_root_no_name(fs_info
, &tmp_key
);
4073 if (IS_ERR(wc
.replay_dest
)) {
4074 ret
= PTR_ERR(wc
.replay_dest
);
4075 free_extent_buffer(log
->node
);
4076 free_extent_buffer(log
->commit_root
);
4078 btrfs_error(fs_info
, ret
, "Couldn't read target root "
4079 "for tree log recovery.");
4083 wc
.replay_dest
->log_root
= log
;
4084 btrfs_record_root_in_trans(trans
, wc
.replay_dest
);
4085 ret
= walk_log_tree(trans
, log
, &wc
);
4087 if (!ret
&& wc
.stage
== LOG_WALK_REPLAY_ALL
) {
4088 ret
= fixup_inode_link_counts(trans
, wc
.replay_dest
,
4092 key
.offset
= found_key
.offset
- 1;
4093 wc
.replay_dest
->log_root
= NULL
;
4094 free_extent_buffer(log
->node
);
4095 free_extent_buffer(log
->commit_root
);
4101 if (found_key
.offset
== 0)
4104 btrfs_release_path(path
);
4106 /* step one is to pin it all, step two is to replay just inodes */
4109 wc
.process_func
= replay_one_buffer
;
4110 wc
.stage
= LOG_WALK_REPLAY_INODES
;
4113 /* step three is to replay everything */
4114 if (wc
.stage
< LOG_WALK_REPLAY_ALL
) {
4119 btrfs_free_path(path
);
4121 /* step 4: commit the transaction, which also unpins the blocks */
4122 ret
= btrfs_commit_transaction(trans
, fs_info
->tree_root
);
4126 free_extent_buffer(log_root_tree
->node
);
4127 log_root_tree
->log_root
= NULL
;
4128 fs_info
->log_root_recovering
= 0;
4129 kfree(log_root_tree
);
4134 btrfs_end_transaction(wc
.trans
, fs_info
->tree_root
);
4135 btrfs_free_path(path
);
4140 * there are some corner cases where we want to force a full
4141 * commit instead of allowing a directory to be logged.
4143 * They revolve around files there were unlinked from the directory, and
4144 * this function updates the parent directory so that a full commit is
4145 * properly done if it is fsync'd later after the unlinks are done.
4147 void btrfs_record_unlink_dir(struct btrfs_trans_handle
*trans
,
4148 struct inode
*dir
, struct inode
*inode
,
4152 * when we're logging a file, if it hasn't been renamed
4153 * or unlinked, and its inode is fully committed on disk,
4154 * we don't have to worry about walking up the directory chain
4155 * to log its parents.
4157 * So, we use the last_unlink_trans field to put this transid
4158 * into the file. When the file is logged we check it and
4159 * don't log the parents if the file is fully on disk.
4161 if (S_ISREG(inode
->i_mode
))
4162 BTRFS_I(inode
)->last_unlink_trans
= trans
->transid
;
4165 * if this directory was already logged any new
4166 * names for this file/dir will get recorded
4169 if (BTRFS_I(dir
)->logged_trans
== trans
->transid
)
4173 * if the inode we're about to unlink was logged,
4174 * the log will be properly updated for any new names
4176 if (BTRFS_I(inode
)->logged_trans
== trans
->transid
)
4180 * when renaming files across directories, if the directory
4181 * there we're unlinking from gets fsync'd later on, there's
4182 * no way to find the destination directory later and fsync it
4183 * properly. So, we have to be conservative and force commits
4184 * so the new name gets discovered.
4189 /* we can safely do the unlink without any special recording */
4193 BTRFS_I(dir
)->last_unlink_trans
= trans
->transid
;
4197 * Call this after adding a new name for a file and it will properly
4198 * update the log to reflect the new name.
4200 * It will return zero if all goes well, and it will return 1 if a
4201 * full transaction commit is required.
4203 int btrfs_log_new_name(struct btrfs_trans_handle
*trans
,
4204 struct inode
*inode
, struct inode
*old_dir
,
4205 struct dentry
*parent
)
4207 struct btrfs_root
* root
= BTRFS_I(inode
)->root
;
4210 * this will force the logging code to walk the dentry chain
4213 if (S_ISREG(inode
->i_mode
))
4214 BTRFS_I(inode
)->last_unlink_trans
= trans
->transid
;
4217 * if this inode hasn't been logged and directory we're renaming it
4218 * from hasn't been logged, we don't need to log it
4220 if (BTRFS_I(inode
)->logged_trans
<=
4221 root
->fs_info
->last_trans_committed
&&
4222 (!old_dir
|| BTRFS_I(old_dir
)->logged_trans
<=
4223 root
->fs_info
->last_trans_committed
))
4226 return btrfs_log_inode_parent(trans
, root
, inode
, parent
, 1);