2 * Copyright (C) 2008 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
21 #include "transaction.h"
24 #include "print-tree.h"
28 /* magic values for the inode_only field in btrfs_log_inode:
30 * LOG_INODE_ALL means to log everything
31 * LOG_INODE_EXISTS means to log just enough to recreate the inode
34 #define LOG_INODE_ALL 0
35 #define LOG_INODE_EXISTS 1
38 * stages for the tree walking. The first
39 * stage (0) is to only pin down the blocks we find
40 * the second stage (1) is to make sure that all the inodes
41 * we find in the log are created in the subvolume.
43 * The last stage is to deal with directories and links and extents
44 * and all the other fun semantics
46 #define LOG_WALK_PIN_ONLY 0
47 #define LOG_WALK_REPLAY_INODES 1
48 #define LOG_WALK_REPLAY_ALL 2
50 static int __btrfs_log_inode(struct btrfs_trans_handle *trans,
51 struct btrfs_root *root, struct inode *inode,
53 static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
54 struct btrfs_root *root,
55 struct btrfs_path *path, u64 objectid);
58 * tree logging is a special write ahead log used to make sure that
59 * fsyncs and O_SYNCs can happen without doing full tree commits.
61 * Full tree commits are expensive because they require commonly
62 * modified blocks to be recowed, creating many dirty pages in the
63 * extent tree an 4x-6x higher write load than ext3.
65 * Instead of doing a tree commit on every fsync, we use the
66 * key ranges and transaction ids to find items for a given file or directory
67 * that have changed in this transaction. Those items are copied into
68 * a special tree (one per subvolume root), that tree is written to disk
69 * and then the fsync is considered complete.
71 * After a crash, items are copied out of the log-tree back into the
72 * subvolume tree. Any file data extents found are recorded in the extent
73 * allocation tree, and the log-tree freed.
75 * The log tree is read three times, once to pin down all the extents it is
76 * using in ram and once, once to create all the inodes logged in the tree
77 * and once to do all the other items.
81 * start a sub transaction and setup the log tree
82 * this increments the log tree writer count to make the people
83 * syncing the tree wait for us to finish
85 static int start_log_trans(struct btrfs_trans_handle *trans,
86 struct btrfs_root *root)
90 mutex_lock(&root->log_mutex);
93 atomic_inc(&root->log_writers);
94 mutex_unlock(&root->log_mutex);
97 mutex_lock(&root->fs_info->tree_log_mutex);
98 if (!root->fs_info->log_root_tree) {
99 ret = btrfs_init_log_root_tree(trans, root->fs_info);
102 if (!root->log_root) {
103 ret = btrfs_add_log_tree(trans, root);
106 mutex_unlock(&root->fs_info->tree_log_mutex);
108 atomic_inc(&root->log_writers);
109 mutex_unlock(&root->log_mutex);
114 * returns 0 if there was a log transaction running and we were able
115 * to join, or returns -ENOENT if there were not transactions
118 static int join_running_log_trans(struct btrfs_root *root)
126 mutex_lock(&root->log_mutex);
127 if (root->log_root) {
129 atomic_inc(&root->log_writers);
131 mutex_unlock(&root->log_mutex);
136 * indicate we're done making changes to the log tree
137 * and wake up anyone waiting to do a sync
139 static int end_log_trans(struct btrfs_root *root)
141 if (atomic_dec_and_test(&root->log_writers)) {
143 if (waitqueue_active(&root->log_writer_wait))
144 wake_up(&root->log_writer_wait);
151 * the walk control struct is used to pass state down the chain when
152 * processing the log tree. The stage field tells us which part
153 * of the log tree processing we are currently doing. The others
154 * are state fields used for that specific part
156 struct walk_control {
157 /* should we free the extent on disk when done? This is used
158 * at transaction commit time while freeing a log tree
162 /* should we write out the extent buffer? This is used
163 * while flushing the log tree to disk during a sync
167 /* should we wait for the extent buffer io to finish? Also used
168 * while flushing the log tree to disk for a sync
172 /* pin only walk, we record which extents on disk belong to the
177 /* what stage of the replay code we're currently in */
180 /* the root we are currently replaying */
181 struct btrfs_root *replay_dest;
183 /* the trans handle for the current replay */
184 struct btrfs_trans_handle *trans;
186 /* the function that gets used to process blocks we find in the
187 * tree. Note the extent_buffer might not be up to date when it is
188 * passed in, and it must be checked or read if you need the data
191 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
192 struct walk_control *wc, u64 gen);
196 * process_func used to pin down extents, write them or wait on them
198 static int process_one_buffer(struct btrfs_root *log,
199 struct extent_buffer *eb,
200 struct walk_control *wc, u64 gen)
203 mutex_lock(&log->fs_info->pinned_mutex);
204 btrfs_update_pinned_extents(log->fs_info->extent_root,
205 eb->start, eb->len, 1);
206 mutex_unlock(&log->fs_info->pinned_mutex);
209 if (btrfs_buffer_uptodate(eb, gen)) {
211 btrfs_write_tree_block(eb);
213 btrfs_wait_tree_block_writeback(eb);
219 * Item overwrite used by replay and tree logging. eb, slot and key all refer
220 * to the src data we are copying out.
222 * root is the tree we are copying into, and path is a scratch
223 * path for use in this function (it should be released on entry and
224 * will be released on exit).
226 * If the key is already in the destination tree the existing item is
227 * overwritten. If the existing item isn't big enough, it is extended.
228 * If it is too large, it is truncated.
230 * If the key isn't in the destination yet, a new item is inserted.
232 static noinline int overwrite_item(struct btrfs_trans_handle *trans,
233 struct btrfs_root *root,
234 struct btrfs_path *path,
235 struct extent_buffer *eb, int slot,
236 struct btrfs_key *key)
240 u64 saved_i_size = 0;
241 int save_old_i_size = 0;
242 unsigned long src_ptr;
243 unsigned long dst_ptr;
244 int overwrite_root = 0;
246 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
249 item_size = btrfs_item_size_nr(eb, slot);
250 src_ptr = btrfs_item_ptr_offset(eb, slot);
252 /* look for the key in the destination tree */
253 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
257 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
259 if (dst_size != item_size)
262 if (item_size == 0) {
263 btrfs_release_path(root, path);
266 dst_copy = kmalloc(item_size, GFP_NOFS);
267 src_copy = kmalloc(item_size, GFP_NOFS);
269 read_extent_buffer(eb, src_copy, src_ptr, item_size);
271 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
272 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
274 ret = memcmp(dst_copy, src_copy, item_size);
279 * they have the same contents, just return, this saves
280 * us from cowing blocks in the destination tree and doing
281 * extra writes that may not have been done by a previous
285 btrfs_release_path(root, path);
291 btrfs_release_path(root, path);
292 /* try to insert the key into the destination tree */
293 ret = btrfs_insert_empty_item(trans, root, path,
296 /* make sure any existing item is the correct size */
297 if (ret == -EEXIST) {
299 found_size = btrfs_item_size_nr(path->nodes[0],
301 if (found_size > item_size) {
302 btrfs_truncate_item(trans, root, path, item_size, 1);
303 } else if (found_size < item_size) {
304 ret = btrfs_extend_item(trans, root, path,
305 item_size - found_size);
311 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
314 /* don't overwrite an existing inode if the generation number
315 * was logged as zero. This is done when the tree logging code
316 * is just logging an inode to make sure it exists after recovery.
318 * Also, don't overwrite i_size on directories during replay.
319 * log replay inserts and removes directory items based on the
320 * state of the tree found in the subvolume, and i_size is modified
323 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
324 struct btrfs_inode_item *src_item;
325 struct btrfs_inode_item *dst_item;
327 src_item = (struct btrfs_inode_item *)src_ptr;
328 dst_item = (struct btrfs_inode_item *)dst_ptr;
330 if (btrfs_inode_generation(eb, src_item) == 0)
333 if (overwrite_root &&
334 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
335 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
337 saved_i_size = btrfs_inode_size(path->nodes[0],
342 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
345 if (save_old_i_size) {
346 struct btrfs_inode_item *dst_item;
347 dst_item = (struct btrfs_inode_item *)dst_ptr;
348 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
351 /* make sure the generation is filled in */
352 if (key->type == BTRFS_INODE_ITEM_KEY) {
353 struct btrfs_inode_item *dst_item;
354 dst_item = (struct btrfs_inode_item *)dst_ptr;
355 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
356 btrfs_set_inode_generation(path->nodes[0], dst_item,
361 btrfs_mark_buffer_dirty(path->nodes[0]);
362 btrfs_release_path(root, path);
367 * simple helper to read an inode off the disk from a given root
368 * This can only be called for subvolume roots and not for the log
370 static noinline struct inode *read_one_inode(struct btrfs_root *root,
374 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
375 if (inode->i_state & I_NEW) {
376 BTRFS_I(inode)->root = root;
377 BTRFS_I(inode)->location.objectid = objectid;
378 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
379 BTRFS_I(inode)->location.offset = 0;
380 btrfs_read_locked_inode(inode);
381 unlock_new_inode(inode);
384 if (is_bad_inode(inode)) {
391 /* replays a single extent in 'eb' at 'slot' with 'key' into the
392 * subvolume 'root'. path is released on entry and should be released
395 * extents in the log tree have not been allocated out of the extent
396 * tree yet. So, this completes the allocation, taking a reference
397 * as required if the extent already exists or creating a new extent
398 * if it isn't in the extent allocation tree yet.
400 * The extent is inserted into the file, dropping any existing extents
401 * from the file that overlap the new one.
403 static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
404 struct btrfs_root *root,
405 struct btrfs_path *path,
406 struct extent_buffer *eb, int slot,
407 struct btrfs_key *key)
410 u64 mask = root->sectorsize - 1;
413 u64 start = key->offset;
415 struct btrfs_file_extent_item *item;
416 struct inode *inode = NULL;
420 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
421 found_type = btrfs_file_extent_type(eb, item);
423 if (found_type == BTRFS_FILE_EXTENT_REG ||
424 found_type == BTRFS_FILE_EXTENT_PREALLOC)
425 extent_end = start + btrfs_file_extent_num_bytes(eb, item);
426 else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
427 size = btrfs_file_extent_inline_len(eb, item);
428 extent_end = (start + size + mask) & ~mask;
434 inode = read_one_inode(root, key->objectid);
441 * first check to see if we already have this extent in the
442 * file. This must be done before the btrfs_drop_extents run
443 * so we don't try to drop this extent.
445 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
449 (found_type == BTRFS_FILE_EXTENT_REG ||
450 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
451 struct btrfs_file_extent_item cmp1;
452 struct btrfs_file_extent_item cmp2;
453 struct btrfs_file_extent_item *existing;
454 struct extent_buffer *leaf;
456 leaf = path->nodes[0];
457 existing = btrfs_item_ptr(leaf, path->slots[0],
458 struct btrfs_file_extent_item);
460 read_extent_buffer(eb, &cmp1, (unsigned long)item,
462 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
466 * we already have a pointer to this exact extent,
467 * we don't have to do anything
469 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
470 btrfs_release_path(root, path);
474 btrfs_release_path(root, path);
476 saved_nbytes = inode_get_bytes(inode);
477 /* drop any overlapping extents */
478 ret = btrfs_drop_extents(trans, root, inode,
479 start, extent_end, start, &alloc_hint);
482 if (found_type == BTRFS_FILE_EXTENT_REG ||
483 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
484 unsigned long dest_offset;
485 struct btrfs_key ins;
487 ret = btrfs_insert_empty_item(trans, root, path, key,
490 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
492 copy_extent_buffer(path->nodes[0], eb, dest_offset,
493 (unsigned long)item, sizeof(*item));
495 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
496 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
497 ins.type = BTRFS_EXTENT_ITEM_KEY;
499 if (ins.objectid > 0) {
502 LIST_HEAD(ordered_sums);
504 * is this extent already allocated in the extent
505 * allocation tree? If so, just add a reference
507 ret = btrfs_lookup_extent(root, ins.objectid,
510 ret = btrfs_inc_extent_ref(trans, root,
511 ins.objectid, ins.offset,
512 path->nodes[0]->start,
513 root->root_key.objectid,
514 trans->transid, key->objectid);
517 * insert the extent pointer in the extent
520 ret = btrfs_alloc_logged_extent(trans, root,
521 path->nodes[0]->start,
522 root->root_key.objectid,
523 trans->transid, key->objectid,
527 btrfs_release_path(root, path);
529 if (btrfs_file_extent_compression(eb, item)) {
530 csum_start = ins.objectid;
531 csum_end = csum_start + ins.offset;
533 csum_start = ins.objectid +
534 btrfs_file_extent_offset(eb, item);
535 csum_end = csum_start +
536 btrfs_file_extent_num_bytes(eb, item);
539 ret = btrfs_lookup_csums_range(root->log_root,
540 csum_start, csum_end - 1,
543 while (!list_empty(&ordered_sums)) {
544 struct btrfs_ordered_sum *sums;
545 sums = list_entry(ordered_sums.next,
546 struct btrfs_ordered_sum,
548 ret = btrfs_csum_file_blocks(trans,
549 root->fs_info->csum_root,
552 list_del(&sums->list);
556 btrfs_release_path(root, path);
558 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
559 /* inline extents are easy, we just overwrite them */
560 ret = overwrite_item(trans, root, path, eb, slot, key);
564 inode_set_bytes(inode, saved_nbytes);
565 btrfs_update_inode(trans, root, inode);
573 * when cleaning up conflicts between the directory names in the
574 * subvolume, directory names in the log and directory names in the
575 * inode back references, we may have to unlink inodes from directories.
577 * This is a helper function to do the unlink of a specific directory
580 static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
581 struct btrfs_root *root,
582 struct btrfs_path *path,
584 struct btrfs_dir_item *di)
589 struct extent_buffer *leaf;
590 struct btrfs_key location;
593 leaf = path->nodes[0];
595 btrfs_dir_item_key_to_cpu(leaf, di, &location);
596 name_len = btrfs_dir_name_len(leaf, di);
597 name = kmalloc(name_len, GFP_NOFS);
598 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
599 btrfs_release_path(root, path);
601 inode = read_one_inode(root, location.objectid);
604 ret = link_to_fixup_dir(trans, root, path, location.objectid);
606 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
615 * helper function to see if a given name and sequence number found
616 * in an inode back reference are already in a directory and correctly
617 * point to this inode
619 static noinline int inode_in_dir(struct btrfs_root *root,
620 struct btrfs_path *path,
621 u64 dirid, u64 objectid, u64 index,
622 const char *name, int name_len)
624 struct btrfs_dir_item *di;
625 struct btrfs_key location;
628 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
629 index, name, name_len, 0);
630 if (di && !IS_ERR(di)) {
631 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
632 if (location.objectid != objectid)
636 btrfs_release_path(root, path);
638 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
639 if (di && !IS_ERR(di)) {
640 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
641 if (location.objectid != objectid)
647 btrfs_release_path(root, path);
652 * helper function to check a log tree for a named back reference in
653 * an inode. This is used to decide if a back reference that is
654 * found in the subvolume conflicts with what we find in the log.
656 * inode backreferences may have multiple refs in a single item,
657 * during replay we process one reference at a time, and we don't
658 * want to delete valid links to a file from the subvolume if that
659 * link is also in the log.
661 static noinline int backref_in_log(struct btrfs_root *log,
662 struct btrfs_key *key,
663 char *name, int namelen)
665 struct btrfs_path *path;
666 struct btrfs_inode_ref *ref;
668 unsigned long ptr_end;
669 unsigned long name_ptr;
675 path = btrfs_alloc_path();
676 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
680 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
681 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
682 ptr_end = ptr + item_size;
683 while (ptr < ptr_end) {
684 ref = (struct btrfs_inode_ref *)ptr;
685 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
686 if (found_name_len == namelen) {
687 name_ptr = (unsigned long)(ref + 1);
688 ret = memcmp_extent_buffer(path->nodes[0], name,
695 ptr = (unsigned long)(ref + 1) + found_name_len;
698 btrfs_free_path(path);
704 * replay one inode back reference item found in the log tree.
705 * eb, slot and key refer to the buffer and key found in the log tree.
706 * root is the destination we are replaying into, and path is for temp
707 * use by this function. (it should be released on return).
709 static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
710 struct btrfs_root *root,
711 struct btrfs_root *log,
712 struct btrfs_path *path,
713 struct extent_buffer *eb, int slot,
714 struct btrfs_key *key)
718 struct btrfs_key location;
719 struct btrfs_inode_ref *ref;
720 struct btrfs_dir_item *di;
724 unsigned long ref_ptr;
725 unsigned long ref_end;
727 location.objectid = key->objectid;
728 location.type = BTRFS_INODE_ITEM_KEY;
732 * it is possible that we didn't log all the parent directories
733 * for a given inode. If we don't find the dir, just don't
734 * copy the back ref in. The link count fixup code will take
737 dir = read_one_inode(root, key->offset);
741 inode = read_one_inode(root, key->objectid);
744 ref_ptr = btrfs_item_ptr_offset(eb, slot);
745 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
748 ref = (struct btrfs_inode_ref *)ref_ptr;
750 namelen = btrfs_inode_ref_name_len(eb, ref);
751 name = kmalloc(namelen, GFP_NOFS);
754 read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen);
756 /* if we already have a perfect match, we're done */
757 if (inode_in_dir(root, path, dir->i_ino, inode->i_ino,
758 btrfs_inode_ref_index(eb, ref),
764 * look for a conflicting back reference in the metadata.
765 * if we find one we have to unlink that name of the file
766 * before we add our new link. Later on, we overwrite any
767 * existing back reference, and we don't want to create
768 * dangling pointers in the directory.
771 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
775 struct btrfs_inode_ref *victim_ref;
777 unsigned long ptr_end;
778 struct extent_buffer *leaf = path->nodes[0];
780 /* are we trying to overwrite a back ref for the root directory
781 * if so, just jump out, we're done
783 if (key->objectid == key->offset)
786 /* check all the names in this back reference to see
787 * if they are in the log. if so, we allow them to stay
788 * otherwise they must be unlinked as a conflict
790 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
791 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
792 while (ptr < ptr_end) {
793 victim_ref = (struct btrfs_inode_ref *)ptr;
794 victim_name_len = btrfs_inode_ref_name_len(leaf,
796 victim_name = kmalloc(victim_name_len, GFP_NOFS);
797 BUG_ON(!victim_name);
799 read_extent_buffer(leaf, victim_name,
800 (unsigned long)(victim_ref + 1),
803 if (!backref_in_log(log, key, victim_name,
805 btrfs_inc_nlink(inode);
806 btrfs_release_path(root, path);
807 ret = btrfs_unlink_inode(trans, root, dir,
811 btrfs_release_path(root, path);
815 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
819 btrfs_release_path(root, path);
821 /* look for a conflicting sequence number */
822 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
823 btrfs_inode_ref_index(eb, ref),
825 if (di && !IS_ERR(di)) {
826 ret = drop_one_dir_item(trans, root, path, dir, di);
829 btrfs_release_path(root, path);
832 /* look for a conflicting name */
833 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
835 if (di && !IS_ERR(di)) {
836 ret = drop_one_dir_item(trans, root, path, dir, di);
839 btrfs_release_path(root, path);
841 /* insert our name */
842 ret = btrfs_add_link(trans, dir, inode, name, namelen, 0,
843 btrfs_inode_ref_index(eb, ref));
846 btrfs_update_inode(trans, root, inode);
849 ref_ptr = (unsigned long)(ref + 1) + namelen;
851 if (ref_ptr < ref_end)
854 /* finally write the back reference in the inode */
855 ret = overwrite_item(trans, root, path, eb, slot, key);
859 btrfs_release_path(root, path);
866 * There are a few corners where the link count of the file can't
867 * be properly maintained during replay. So, instead of adding
868 * lots of complexity to the log code, we just scan the backrefs
869 * for any file that has been through replay.
871 * The scan will update the link count on the inode to reflect the
872 * number of back refs found. If it goes down to zero, the iput
873 * will free the inode.
875 static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
876 struct btrfs_root *root,
879 struct btrfs_path *path;
881 struct btrfs_key key;
884 unsigned long ptr_end;
887 key.objectid = inode->i_ino;
888 key.type = BTRFS_INODE_REF_KEY;
889 key.offset = (u64)-1;
891 path = btrfs_alloc_path();
894 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
898 if (path->slots[0] == 0)
902 btrfs_item_key_to_cpu(path->nodes[0], &key,
904 if (key.objectid != inode->i_ino ||
905 key.type != BTRFS_INODE_REF_KEY)
907 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
908 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
910 while (ptr < ptr_end) {
911 struct btrfs_inode_ref *ref;
913 ref = (struct btrfs_inode_ref *)ptr;
914 name_len = btrfs_inode_ref_name_len(path->nodes[0],
916 ptr = (unsigned long)(ref + 1) + name_len;
923 btrfs_release_path(root, path);
925 btrfs_free_path(path);
926 if (nlink != inode->i_nlink) {
927 inode->i_nlink = nlink;
928 btrfs_update_inode(trans, root, inode);
930 BTRFS_I(inode)->index_cnt = (u64)-1;
935 static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
936 struct btrfs_root *root,
937 struct btrfs_path *path)
940 struct btrfs_key key;
943 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
944 key.type = BTRFS_ORPHAN_ITEM_KEY;
945 key.offset = (u64)-1;
947 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
952 if (path->slots[0] == 0)
957 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
958 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
959 key.type != BTRFS_ORPHAN_ITEM_KEY)
962 ret = btrfs_del_item(trans, root, path);
965 btrfs_release_path(root, path);
966 inode = read_one_inode(root, key.offset);
969 ret = fixup_inode_link_count(trans, root, inode);
978 btrfs_release_path(root, path);
984 * record a given inode in the fixup dir so we can check its link
985 * count when replay is done. The link count is incremented here
986 * so the inode won't go away until we check it
988 static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
989 struct btrfs_root *root,
990 struct btrfs_path *path,
993 struct btrfs_key key;
997 inode = read_one_inode(root, objectid);
1000 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1001 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1002 key.offset = objectid;
1004 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1006 btrfs_release_path(root, path);
1008 btrfs_inc_nlink(inode);
1009 btrfs_update_inode(trans, root, inode);
1010 } else if (ret == -EEXIST) {
1021 * when replaying the log for a directory, we only insert names
1022 * for inodes that actually exist. This means an fsync on a directory
1023 * does not implicitly fsync all the new files in it
1025 static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1026 struct btrfs_root *root,
1027 struct btrfs_path *path,
1028 u64 dirid, u64 index,
1029 char *name, int name_len, u8 type,
1030 struct btrfs_key *location)
1032 struct inode *inode;
1036 inode = read_one_inode(root, location->objectid);
1040 dir = read_one_inode(root, dirid);
1045 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1047 /* FIXME, put inode into FIXUP list */
1055 * take a single entry in a log directory item and replay it into
1058 * if a conflicting item exists in the subdirectory already,
1059 * the inode it points to is unlinked and put into the link count
1062 * If a name from the log points to a file or directory that does
1063 * not exist in the FS, it is skipped. fsyncs on directories
1064 * do not force down inodes inside that directory, just changes to the
1065 * names or unlinks in a directory.
1067 static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1068 struct btrfs_root *root,
1069 struct btrfs_path *path,
1070 struct extent_buffer *eb,
1071 struct btrfs_dir_item *di,
1072 struct btrfs_key *key)
1076 struct btrfs_dir_item *dst_di;
1077 struct btrfs_key found_key;
1078 struct btrfs_key log_key;
1084 dir = read_one_inode(root, key->objectid);
1087 name_len = btrfs_dir_name_len(eb, di);
1088 name = kmalloc(name_len, GFP_NOFS);
1089 log_type = btrfs_dir_type(eb, di);
1090 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1093 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
1094 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1099 btrfs_release_path(root, path);
1101 if (key->type == BTRFS_DIR_ITEM_KEY) {
1102 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1104 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
1105 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1112 if (!dst_di || IS_ERR(dst_di)) {
1113 /* we need a sequence number to insert, so we only
1114 * do inserts for the BTRFS_DIR_INDEX_KEY types
1116 if (key->type != BTRFS_DIR_INDEX_KEY)
1121 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1122 /* the existing item matches the logged item */
1123 if (found_key.objectid == log_key.objectid &&
1124 found_key.type == log_key.type &&
1125 found_key.offset == log_key.offset &&
1126 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1131 * don't drop the conflicting directory entry if the inode
1132 * for the new entry doesn't exist
1137 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1140 if (key->type == BTRFS_DIR_INDEX_KEY)
1143 btrfs_release_path(root, path);
1149 btrfs_release_path(root, path);
1150 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1151 name, name_len, log_type, &log_key);
1153 if (ret && ret != -ENOENT)
1159 * find all the names in a directory item and reconcile them into
1160 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1161 * one name in a directory item, but the same code gets used for
1162 * both directory index types
1164 static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1165 struct btrfs_root *root,
1166 struct btrfs_path *path,
1167 struct extent_buffer *eb, int slot,
1168 struct btrfs_key *key)
1171 u32 item_size = btrfs_item_size_nr(eb, slot);
1172 struct btrfs_dir_item *di;
1175 unsigned long ptr_end;
1177 ptr = btrfs_item_ptr_offset(eb, slot);
1178 ptr_end = ptr + item_size;
1179 while (ptr < ptr_end) {
1180 di = (struct btrfs_dir_item *)ptr;
1181 name_len = btrfs_dir_name_len(eb, di);
1182 ret = replay_one_name(trans, root, path, eb, di, key);
1184 ptr = (unsigned long)(di + 1);
1191 * directory replay has two parts. There are the standard directory
1192 * items in the log copied from the subvolume, and range items
1193 * created in the log while the subvolume was logged.
1195 * The range items tell us which parts of the key space the log
1196 * is authoritative for. During replay, if a key in the subvolume
1197 * directory is in a logged range item, but not actually in the log
1198 * that means it was deleted from the directory before the fsync
1199 * and should be removed.
1201 static noinline int find_dir_range(struct btrfs_root *root,
1202 struct btrfs_path *path,
1203 u64 dirid, int key_type,
1204 u64 *start_ret, u64 *end_ret)
1206 struct btrfs_key key;
1208 struct btrfs_dir_log_item *item;
1212 if (*start_ret == (u64)-1)
1215 key.objectid = dirid;
1216 key.type = key_type;
1217 key.offset = *start_ret;
1219 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1223 if (path->slots[0] == 0)
1228 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1230 if (key.type != key_type || key.objectid != dirid) {
1234 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1235 struct btrfs_dir_log_item);
1236 found_end = btrfs_dir_log_end(path->nodes[0], item);
1238 if (*start_ret >= key.offset && *start_ret <= found_end) {
1240 *start_ret = key.offset;
1241 *end_ret = found_end;
1246 /* check the next slot in the tree to see if it is a valid item */
1247 nritems = btrfs_header_nritems(path->nodes[0]);
1248 if (path->slots[0] >= nritems) {
1249 ret = btrfs_next_leaf(root, path);
1256 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1258 if (key.type != key_type || key.objectid != dirid) {
1262 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1263 struct btrfs_dir_log_item);
1264 found_end = btrfs_dir_log_end(path->nodes[0], item);
1265 *start_ret = key.offset;
1266 *end_ret = found_end;
1269 btrfs_release_path(root, path);
1274 * this looks for a given directory item in the log. If the directory
1275 * item is not in the log, the item is removed and the inode it points
1278 static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1279 struct btrfs_root *root,
1280 struct btrfs_root *log,
1281 struct btrfs_path *path,
1282 struct btrfs_path *log_path,
1284 struct btrfs_key *dir_key)
1287 struct extent_buffer *eb;
1290 struct btrfs_dir_item *di;
1291 struct btrfs_dir_item *log_di;
1294 unsigned long ptr_end;
1296 struct inode *inode;
1297 struct btrfs_key location;
1300 eb = path->nodes[0];
1301 slot = path->slots[0];
1302 item_size = btrfs_item_size_nr(eb, slot);
1303 ptr = btrfs_item_ptr_offset(eb, slot);
1304 ptr_end = ptr + item_size;
1305 while (ptr < ptr_end) {
1306 di = (struct btrfs_dir_item *)ptr;
1307 name_len = btrfs_dir_name_len(eb, di);
1308 name = kmalloc(name_len, GFP_NOFS);
1313 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1316 if (dir_key->type == BTRFS_DIR_ITEM_KEY) {
1317 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1320 } else if (dir_key->type == BTRFS_DIR_INDEX_KEY) {
1321 log_di = btrfs_lookup_dir_index_item(trans, log,
1327 if (!log_di || IS_ERR(log_di)) {
1328 btrfs_dir_item_key_to_cpu(eb, di, &location);
1329 btrfs_release_path(root, path);
1330 btrfs_release_path(log, log_path);
1331 inode = read_one_inode(root, location.objectid);
1334 ret = link_to_fixup_dir(trans, root,
1335 path, location.objectid);
1337 btrfs_inc_nlink(inode);
1338 ret = btrfs_unlink_inode(trans, root, dir, inode,
1344 /* there might still be more names under this key
1345 * check and repeat if required
1347 ret = btrfs_search_slot(NULL, root, dir_key, path,
1354 btrfs_release_path(log, log_path);
1357 ptr = (unsigned long)(di + 1);
1362 btrfs_release_path(root, path);
1363 btrfs_release_path(log, log_path);
1368 * deletion replay happens before we copy any new directory items
1369 * out of the log or out of backreferences from inodes. It
1370 * scans the log to find ranges of keys that log is authoritative for,
1371 * and then scans the directory to find items in those ranges that are
1372 * not present in the log.
1374 * Anything we don't find in the log is unlinked and removed from the
1377 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1378 struct btrfs_root *root,
1379 struct btrfs_root *log,
1380 struct btrfs_path *path,
1385 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1387 struct btrfs_key dir_key;
1388 struct btrfs_key found_key;
1389 struct btrfs_path *log_path;
1392 dir_key.objectid = dirid;
1393 dir_key.type = BTRFS_DIR_ITEM_KEY;
1394 log_path = btrfs_alloc_path();
1398 dir = read_one_inode(root, dirid);
1399 /* it isn't an error if the inode isn't there, that can happen
1400 * because we replay the deletes before we copy in the inode item
1404 btrfs_free_path(log_path);
1411 ret = find_dir_range(log, path, dirid, key_type,
1412 &range_start, &range_end);
1416 dir_key.offset = range_start;
1419 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1424 nritems = btrfs_header_nritems(path->nodes[0]);
1425 if (path->slots[0] >= nritems) {
1426 ret = btrfs_next_leaf(root, path);
1430 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1432 if (found_key.objectid != dirid ||
1433 found_key.type != dir_key.type)
1436 if (found_key.offset > range_end)
1439 ret = check_item_in_log(trans, root, log, path,
1440 log_path, dir, &found_key);
1442 if (found_key.offset == (u64)-1)
1444 dir_key.offset = found_key.offset + 1;
1446 btrfs_release_path(root, path);
1447 if (range_end == (u64)-1)
1449 range_start = range_end + 1;
1454 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1455 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1456 dir_key.type = BTRFS_DIR_INDEX_KEY;
1457 btrfs_release_path(root, path);
1461 btrfs_release_path(root, path);
1462 btrfs_free_path(log_path);
1468 * the process_func used to replay items from the log tree. This
1469 * gets called in two different stages. The first stage just looks
1470 * for inodes and makes sure they are all copied into the subvolume.
1472 * The second stage copies all the other item types from the log into
1473 * the subvolume. The two stage approach is slower, but gets rid of
1474 * lots of complexity around inodes referencing other inodes that exist
1475 * only in the log (references come from either directory items or inode
1478 static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1479 struct walk_control *wc, u64 gen)
1482 struct btrfs_path *path;
1483 struct btrfs_root *root = wc->replay_dest;
1484 struct btrfs_key key;
1490 btrfs_read_buffer(eb, gen);
1492 level = btrfs_header_level(eb);
1497 path = btrfs_alloc_path();
1500 nritems = btrfs_header_nritems(eb);
1501 for (i = 0; i < nritems; i++) {
1502 btrfs_item_key_to_cpu(eb, &key, i);
1503 item_size = btrfs_item_size_nr(eb, i);
1505 /* inode keys are done during the first stage */
1506 if (key.type == BTRFS_INODE_ITEM_KEY &&
1507 wc->stage == LOG_WALK_REPLAY_INODES) {
1508 struct inode *inode;
1509 struct btrfs_inode_item *inode_item;
1512 inode_item = btrfs_item_ptr(eb, i,
1513 struct btrfs_inode_item);
1514 mode = btrfs_inode_mode(eb, inode_item);
1515 if (S_ISDIR(mode)) {
1516 ret = replay_dir_deletes(wc->trans,
1517 root, log, path, key.objectid);
1520 ret = overwrite_item(wc->trans, root, path,
1524 /* for regular files, truncate away
1525 * extents past the new EOF
1527 if (S_ISREG(mode)) {
1528 inode = read_one_inode(root,
1532 ret = btrfs_truncate_inode_items(wc->trans,
1533 root, inode, inode->i_size,
1534 BTRFS_EXTENT_DATA_KEY);
1538 ret = link_to_fixup_dir(wc->trans, root,
1539 path, key.objectid);
1542 if (wc->stage < LOG_WALK_REPLAY_ALL)
1545 /* these keys are simply copied */
1546 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1547 ret = overwrite_item(wc->trans, root, path,
1550 } else if (key.type == BTRFS_INODE_REF_KEY) {
1551 ret = add_inode_ref(wc->trans, root, log, path,
1553 BUG_ON(ret && ret != -ENOENT);
1554 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
1555 ret = replay_one_extent(wc->trans, root, path,
1558 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
1559 key.type == BTRFS_DIR_INDEX_KEY) {
1560 ret = replay_one_dir_item(wc->trans, root, path,
1565 btrfs_free_path(path);
1569 static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
1570 struct btrfs_root *root,
1571 struct btrfs_path *path, int *level,
1572 struct walk_control *wc)
1578 struct extent_buffer *next;
1579 struct extent_buffer *cur;
1580 struct extent_buffer *parent;
1584 WARN_ON(*level < 0);
1585 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1587 while (*level > 0) {
1588 WARN_ON(*level < 0);
1589 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1590 cur = path->nodes[*level];
1592 if (btrfs_header_level(cur) != *level)
1595 if (path->slots[*level] >=
1596 btrfs_header_nritems(cur))
1599 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1600 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1601 blocksize = btrfs_level_size(root, *level - 1);
1603 parent = path->nodes[*level];
1604 root_owner = btrfs_header_owner(parent);
1605 root_gen = btrfs_header_generation(parent);
1607 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
1609 wc->process_func(root, next, wc, ptr_gen);
1612 path->slots[*level]++;
1614 btrfs_read_buffer(next, ptr_gen);
1616 btrfs_tree_lock(next);
1617 clean_tree_block(trans, root, next);
1618 btrfs_wait_tree_block_writeback(next);
1619 btrfs_tree_unlock(next);
1621 ret = btrfs_drop_leaf_ref(trans, root, next);
1624 WARN_ON(root_owner !=
1625 BTRFS_TREE_LOG_OBJECTID);
1626 ret = btrfs_free_reserved_extent(root,
1630 free_extent_buffer(next);
1633 btrfs_read_buffer(next, ptr_gen);
1635 WARN_ON(*level <= 0);
1636 if (path->nodes[*level-1])
1637 free_extent_buffer(path->nodes[*level-1]);
1638 path->nodes[*level-1] = next;
1639 *level = btrfs_header_level(next);
1640 path->slots[*level] = 0;
1643 WARN_ON(*level < 0);
1644 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1646 if (path->nodes[*level] == root->node)
1647 parent = path->nodes[*level];
1649 parent = path->nodes[*level + 1];
1651 bytenr = path->nodes[*level]->start;
1653 blocksize = btrfs_level_size(root, *level);
1654 root_owner = btrfs_header_owner(parent);
1655 root_gen = btrfs_header_generation(parent);
1657 wc->process_func(root, path->nodes[*level], wc,
1658 btrfs_header_generation(path->nodes[*level]));
1661 next = path->nodes[*level];
1662 btrfs_tree_lock(next);
1663 clean_tree_block(trans, root, next);
1664 btrfs_wait_tree_block_writeback(next);
1665 btrfs_tree_unlock(next);
1668 ret = btrfs_drop_leaf_ref(trans, root, next);
1671 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
1672 ret = btrfs_free_reserved_extent(root, bytenr, blocksize);
1675 free_extent_buffer(path->nodes[*level]);
1676 path->nodes[*level] = NULL;
1683 static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
1684 struct btrfs_root *root,
1685 struct btrfs_path *path, int *level,
1686 struct walk_control *wc)
1694 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1695 slot = path->slots[i];
1696 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1697 struct extent_buffer *node;
1698 node = path->nodes[i];
1701 WARN_ON(*level == 0);
1704 struct extent_buffer *parent;
1705 if (path->nodes[*level] == root->node)
1706 parent = path->nodes[*level];
1708 parent = path->nodes[*level + 1];
1710 root_owner = btrfs_header_owner(parent);
1711 root_gen = btrfs_header_generation(parent);
1712 wc->process_func(root, path->nodes[*level], wc,
1713 btrfs_header_generation(path->nodes[*level]));
1715 struct extent_buffer *next;
1717 next = path->nodes[*level];
1719 btrfs_tree_lock(next);
1720 clean_tree_block(trans, root, next);
1721 btrfs_wait_tree_block_writeback(next);
1722 btrfs_tree_unlock(next);
1725 ret = btrfs_drop_leaf_ref(trans, root,
1730 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
1731 ret = btrfs_free_reserved_extent(root,
1732 path->nodes[*level]->start,
1733 path->nodes[*level]->len);
1736 free_extent_buffer(path->nodes[*level]);
1737 path->nodes[*level] = NULL;
1745 * drop the reference count on the tree rooted at 'snap'. This traverses
1746 * the tree freeing any blocks that have a ref count of zero after being
1749 static int walk_log_tree(struct btrfs_trans_handle *trans,
1750 struct btrfs_root *log, struct walk_control *wc)
1755 struct btrfs_path *path;
1759 path = btrfs_alloc_path();
1762 level = btrfs_header_level(log->node);
1764 path->nodes[level] = log->node;
1765 extent_buffer_get(log->node);
1766 path->slots[level] = 0;
1769 wret = walk_down_log_tree(trans, log, path, &level, wc);
1775 wret = walk_up_log_tree(trans, log, path, &level, wc);
1782 /* was the root node processed? if not, catch it here */
1783 if (path->nodes[orig_level]) {
1784 wc->process_func(log, path->nodes[orig_level], wc,
1785 btrfs_header_generation(path->nodes[orig_level]));
1787 struct extent_buffer *next;
1789 next = path->nodes[orig_level];
1791 btrfs_tree_lock(next);
1792 clean_tree_block(trans, log, next);
1793 btrfs_wait_tree_block_writeback(next);
1794 btrfs_tree_unlock(next);
1796 if (orig_level == 0) {
1797 ret = btrfs_drop_leaf_ref(trans, log,
1801 WARN_ON(log->root_key.objectid !=
1802 BTRFS_TREE_LOG_OBJECTID);
1803 ret = btrfs_free_reserved_extent(log, next->start,
1809 for (i = 0; i <= orig_level; i++) {
1810 if (path->nodes[i]) {
1811 free_extent_buffer(path->nodes[i]);
1812 path->nodes[i] = NULL;
1815 btrfs_free_path(path);
1820 * helper function to update the item for a given subvolumes log root
1821 * in the tree of log roots
1823 static int update_log_root(struct btrfs_trans_handle *trans,
1824 struct btrfs_root *log)
1828 if (log->log_transid == 1) {
1829 /* insert root item on the first sync */
1830 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
1831 &log->root_key, &log->root_item);
1833 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
1834 &log->root_key, &log->root_item);
1839 static int wait_log_commit(struct btrfs_root *root, unsigned long transid)
1842 int index = transid % 2;
1845 * we only allow two pending log transactions at a time,
1846 * so we know that if ours is more than 2 older than the
1847 * current transaction, we're done
1850 prepare_to_wait(&root->log_commit_wait[index],
1851 &wait, TASK_UNINTERRUPTIBLE);
1852 mutex_unlock(&root->log_mutex);
1853 if (root->log_transid < transid + 2 &&
1854 atomic_read(&root->log_commit[index]))
1856 finish_wait(&root->log_commit_wait[index], &wait);
1857 mutex_lock(&root->log_mutex);
1858 } while (root->log_transid < transid + 2 &&
1859 atomic_read(&root->log_commit[index]));
1863 static int wait_for_writer(struct btrfs_root *root)
1866 while (atomic_read(&root->log_writers)) {
1867 prepare_to_wait(&root->log_writer_wait,
1868 &wait, TASK_UNINTERRUPTIBLE);
1869 mutex_unlock(&root->log_mutex);
1870 if (atomic_read(&root->log_writers))
1872 mutex_lock(&root->log_mutex);
1873 finish_wait(&root->log_writer_wait, &wait);
1879 * btrfs_sync_log does sends a given tree log down to the disk and
1880 * updates the super blocks to record it. When this call is done,
1881 * you know that any inodes previously logged are safely on disk
1883 int btrfs_sync_log(struct btrfs_trans_handle *trans,
1884 struct btrfs_root *root)
1889 struct btrfs_root *log = root->log_root;
1890 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
1892 mutex_lock(&root->log_mutex);
1893 index1 = root->log_transid % 2;
1894 if (atomic_read(&root->log_commit[index1])) {
1895 wait_log_commit(root, root->log_transid);
1896 mutex_unlock(&root->log_mutex);
1899 atomic_set(&root->log_commit[index1], 1);
1901 /* wait for previous tree log sync to complete */
1902 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
1903 wait_log_commit(root, root->log_transid - 1);
1906 unsigned long batch = root->log_batch;
1907 mutex_unlock(&root->log_mutex);
1908 schedule_timeout_uninterruptible(1);
1909 mutex_lock(&root->log_mutex);
1910 wait_for_writer(root);
1911 if (batch == root->log_batch)
1915 ret = btrfs_write_and_wait_marked_extents(log, &log->dirty_log_pages);
1918 btrfs_set_root_bytenr(&log->root_item, log->node->start);
1919 btrfs_set_root_generation(&log->root_item, trans->transid);
1920 btrfs_set_root_level(&log->root_item, btrfs_header_level(log->node));
1922 root->log_batch = 0;
1923 root->log_transid++;
1924 log->log_transid = root->log_transid;
1927 * log tree has been flushed to disk, new modifications of
1928 * the log will be written to new positions. so it's safe to
1929 * allow log writers to go in.
1931 mutex_unlock(&root->log_mutex);
1933 mutex_lock(&log_root_tree->log_mutex);
1934 log_root_tree->log_batch++;
1935 atomic_inc(&log_root_tree->log_writers);
1936 mutex_unlock(&log_root_tree->log_mutex);
1938 ret = update_log_root(trans, log);
1941 mutex_lock(&log_root_tree->log_mutex);
1942 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
1944 if (waitqueue_active(&log_root_tree->log_writer_wait))
1945 wake_up(&log_root_tree->log_writer_wait);
1948 index2 = log_root_tree->log_transid % 2;
1949 if (atomic_read(&log_root_tree->log_commit[index2])) {
1950 wait_log_commit(log_root_tree, log_root_tree->log_transid);
1951 mutex_unlock(&log_root_tree->log_mutex);
1954 atomic_set(&log_root_tree->log_commit[index2], 1);
1956 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2]))
1957 wait_log_commit(log_root_tree, log_root_tree->log_transid - 1);
1959 wait_for_writer(log_root_tree);
1961 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
1962 &log_root_tree->dirty_log_pages);
1965 btrfs_set_super_log_root(&root->fs_info->super_for_commit,
1966 log_root_tree->node->start);
1967 btrfs_set_super_log_root_level(&root->fs_info->super_for_commit,
1968 btrfs_header_level(log_root_tree->node));
1970 log_root_tree->log_batch = 0;
1971 log_root_tree->log_transid++;
1974 mutex_unlock(&log_root_tree->log_mutex);
1977 * nobody else is going to jump in and write the the ctree
1978 * super here because the log_commit atomic below is protecting
1979 * us. We must be called with a transaction handle pinning
1980 * the running transaction open, so a full commit can't hop
1981 * in and cause problems either.
1983 write_ctree_super(trans, root->fs_info->tree_root, 2);
1985 atomic_set(&log_root_tree->log_commit[index2], 0);
1987 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
1988 wake_up(&log_root_tree->log_commit_wait[index2]);
1990 atomic_set(&root->log_commit[index1], 0);
1992 if (waitqueue_active(&root->log_commit_wait[index1]))
1993 wake_up(&root->log_commit_wait[index1]);
1997 /* * free all the extents used by the tree log. This should be called
1998 * at commit time of the full transaction
2000 int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2003 struct btrfs_root *log;
2007 struct walk_control wc = {
2009 .process_func = process_one_buffer
2012 if (!root->log_root || root->fs_info->log_root_recovering)
2015 log = root->log_root;
2016 ret = walk_log_tree(trans, log, &wc);
2020 ret = find_first_extent_bit(&log->dirty_log_pages,
2021 0, &start, &end, EXTENT_DIRTY);
2025 clear_extent_dirty(&log->dirty_log_pages,
2026 start, end, GFP_NOFS);
2029 if (log->log_transid > 0) {
2030 ret = btrfs_del_root(trans, root->fs_info->log_root_tree,
2034 root->log_root = NULL;
2035 free_extent_buffer(log->node);
2041 * If both a file and directory are logged, and unlinks or renames are
2042 * mixed in, we have a few interesting corners:
2044 * create file X in dir Y
2045 * link file X to X.link in dir Y
2047 * unlink file X but leave X.link
2050 * After a crash we would expect only X.link to exist. But file X
2051 * didn't get fsync'd again so the log has back refs for X and X.link.
2053 * We solve this by removing directory entries and inode backrefs from the
2054 * log when a file that was logged in the current transaction is
2055 * unlinked. Any later fsync will include the updated log entries, and
2056 * we'll be able to reconstruct the proper directory items from backrefs.
2058 * This optimizations allows us to avoid relogging the entire inode
2059 * or the entire directory.
2061 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2062 struct btrfs_root *root,
2063 const char *name, int name_len,
2064 struct inode *dir, u64 index)
2066 struct btrfs_root *log;
2067 struct btrfs_dir_item *di;
2068 struct btrfs_path *path;
2072 if (BTRFS_I(dir)->logged_trans < trans->transid)
2075 ret = join_running_log_trans(root);
2079 mutex_lock(&BTRFS_I(dir)->log_mutex);
2081 log = root->log_root;
2082 path = btrfs_alloc_path();
2083 di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino,
2084 name, name_len, -1);
2085 if (di && !IS_ERR(di)) {
2086 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2087 bytes_del += name_len;
2090 btrfs_release_path(log, path);
2091 di = btrfs_lookup_dir_index_item(trans, log, path, dir->i_ino,
2092 index, name, name_len, -1);
2093 if (di && !IS_ERR(di)) {
2094 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2095 bytes_del += name_len;
2099 /* update the directory size in the log to reflect the names
2103 struct btrfs_key key;
2105 key.objectid = dir->i_ino;
2107 key.type = BTRFS_INODE_ITEM_KEY;
2108 btrfs_release_path(log, path);
2110 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
2112 struct btrfs_inode_item *item;
2115 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2116 struct btrfs_inode_item);
2117 i_size = btrfs_inode_size(path->nodes[0], item);
2118 if (i_size > bytes_del)
2119 i_size -= bytes_del;
2122 btrfs_set_inode_size(path->nodes[0], item, i_size);
2123 btrfs_mark_buffer_dirty(path->nodes[0]);
2126 btrfs_release_path(log, path);
2129 btrfs_free_path(path);
2130 mutex_unlock(&BTRFS_I(dir)->log_mutex);
2131 end_log_trans(root);
2136 /* see comments for btrfs_del_dir_entries_in_log */
2137 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2138 struct btrfs_root *root,
2139 const char *name, int name_len,
2140 struct inode *inode, u64 dirid)
2142 struct btrfs_root *log;
2146 if (BTRFS_I(inode)->logged_trans < trans->transid)
2149 ret = join_running_log_trans(root);
2152 log = root->log_root;
2153 mutex_lock(&BTRFS_I(inode)->log_mutex);
2155 ret = btrfs_del_inode_ref(trans, log, name, name_len, inode->i_ino,
2157 mutex_unlock(&BTRFS_I(inode)->log_mutex);
2158 end_log_trans(root);
2164 * creates a range item in the log for 'dirid'. first_offset and
2165 * last_offset tell us which parts of the key space the log should
2166 * be considered authoritative for.
2168 static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2169 struct btrfs_root *log,
2170 struct btrfs_path *path,
2171 int key_type, u64 dirid,
2172 u64 first_offset, u64 last_offset)
2175 struct btrfs_key key;
2176 struct btrfs_dir_log_item *item;
2178 key.objectid = dirid;
2179 key.offset = first_offset;
2180 if (key_type == BTRFS_DIR_ITEM_KEY)
2181 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2183 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2184 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
2187 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2188 struct btrfs_dir_log_item);
2189 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2190 btrfs_mark_buffer_dirty(path->nodes[0]);
2191 btrfs_release_path(log, path);
2196 * log all the items included in the current transaction for a given
2197 * directory. This also creates the range items in the log tree required
2198 * to replay anything deleted before the fsync
2200 static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2201 struct btrfs_root *root, struct inode *inode,
2202 struct btrfs_path *path,
2203 struct btrfs_path *dst_path, int key_type,
2204 u64 min_offset, u64 *last_offset_ret)
2206 struct btrfs_key min_key;
2207 struct btrfs_key max_key;
2208 struct btrfs_root *log = root->log_root;
2209 struct extent_buffer *src;
2213 u64 first_offset = min_offset;
2214 u64 last_offset = (u64)-1;
2216 log = root->log_root;
2217 max_key.objectid = inode->i_ino;
2218 max_key.offset = (u64)-1;
2219 max_key.type = key_type;
2221 min_key.objectid = inode->i_ino;
2222 min_key.type = key_type;
2223 min_key.offset = min_offset;
2225 path->keep_locks = 1;
2227 ret = btrfs_search_forward(root, &min_key, &max_key,
2228 path, 0, trans->transid);
2231 * we didn't find anything from this transaction, see if there
2232 * is anything at all
2234 if (ret != 0 || min_key.objectid != inode->i_ino ||
2235 min_key.type != key_type) {
2236 min_key.objectid = inode->i_ino;
2237 min_key.type = key_type;
2238 min_key.offset = (u64)-1;
2239 btrfs_release_path(root, path);
2240 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2242 btrfs_release_path(root, path);
2245 ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
2247 /* if ret == 0 there are items for this type,
2248 * create a range to tell us the last key of this type.
2249 * otherwise, there are no items in this directory after
2250 * *min_offset, and we create a range to indicate that.
2253 struct btrfs_key tmp;
2254 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2256 if (key_type == tmp.type)
2257 first_offset = max(min_offset, tmp.offset) + 1;
2262 /* go backward to find any previous key */
2263 ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
2265 struct btrfs_key tmp;
2266 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2267 if (key_type == tmp.type) {
2268 first_offset = tmp.offset;
2269 ret = overwrite_item(trans, log, dst_path,
2270 path->nodes[0], path->slots[0],
2274 btrfs_release_path(root, path);
2276 /* find the first key from this transaction again */
2277 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2284 * we have a block from this transaction, log every item in it
2285 * from our directory
2288 struct btrfs_key tmp;
2289 src = path->nodes[0];
2290 nritems = btrfs_header_nritems(src);
2291 for (i = path->slots[0]; i < nritems; i++) {
2292 btrfs_item_key_to_cpu(src, &min_key, i);
2294 if (min_key.objectid != inode->i_ino ||
2295 min_key.type != key_type)
2297 ret = overwrite_item(trans, log, dst_path, src, i,
2301 path->slots[0] = nritems;
2304 * look ahead to the next item and see if it is also
2305 * from this directory and from this transaction
2307 ret = btrfs_next_leaf(root, path);
2309 last_offset = (u64)-1;
2312 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2313 if (tmp.objectid != inode->i_ino || tmp.type != key_type) {
2314 last_offset = (u64)-1;
2317 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2318 ret = overwrite_item(trans, log, dst_path,
2319 path->nodes[0], path->slots[0],
2323 last_offset = tmp.offset;
2328 *last_offset_ret = last_offset;
2329 btrfs_release_path(root, path);
2330 btrfs_release_path(log, dst_path);
2332 /* insert the log range keys to indicate where the log is valid */
2333 ret = insert_dir_log_key(trans, log, path, key_type, inode->i_ino,
2334 first_offset, last_offset);
2340 * logging directories is very similar to logging inodes, We find all the items
2341 * from the current transaction and write them to the log.
2343 * The recovery code scans the directory in the subvolume, and if it finds a
2344 * key in the range logged that is not present in the log tree, then it means
2345 * that dir entry was unlinked during the transaction.
2347 * In order for that scan to work, we must include one key smaller than
2348 * the smallest logged by this transaction and one key larger than the largest
2349 * key logged by this transaction.
2351 static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2352 struct btrfs_root *root, struct inode *inode,
2353 struct btrfs_path *path,
2354 struct btrfs_path *dst_path)
2359 int key_type = BTRFS_DIR_ITEM_KEY;
2365 ret = log_dir_items(trans, root, inode, path,
2366 dst_path, key_type, min_key,
2369 if (max_key == (u64)-1)
2371 min_key = max_key + 1;
2374 if (key_type == BTRFS_DIR_ITEM_KEY) {
2375 key_type = BTRFS_DIR_INDEX_KEY;
2382 * a helper function to drop items from the log before we relog an
2383 * inode. max_key_type indicates the highest item type to remove.
2384 * This cannot be run for file data extents because it does not
2385 * free the extents they point to.
2387 static int drop_objectid_items(struct btrfs_trans_handle *trans,
2388 struct btrfs_root *log,
2389 struct btrfs_path *path,
2390 u64 objectid, int max_key_type)
2393 struct btrfs_key key;
2394 struct btrfs_key found_key;
2396 key.objectid = objectid;
2397 key.type = max_key_type;
2398 key.offset = (u64)-1;
2401 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
2406 if (path->slots[0] == 0)
2410 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2413 if (found_key.objectid != objectid)
2416 ret = btrfs_del_item(trans, log, path);
2418 btrfs_release_path(log, path);
2420 btrfs_release_path(log, path);
2424 static noinline int copy_items(struct btrfs_trans_handle *trans,
2425 struct btrfs_root *log,
2426 struct btrfs_path *dst_path,
2427 struct extent_buffer *src,
2428 int start_slot, int nr, int inode_only)
2430 unsigned long src_offset;
2431 unsigned long dst_offset;
2432 struct btrfs_file_extent_item *extent;
2433 struct btrfs_inode_item *inode_item;
2435 struct btrfs_key *ins_keys;
2439 struct list_head ordered_sums;
2441 INIT_LIST_HEAD(&ordered_sums);
2443 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
2444 nr * sizeof(u32), GFP_NOFS);
2445 ins_sizes = (u32 *)ins_data;
2446 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
2448 for (i = 0; i < nr; i++) {
2449 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
2450 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
2452 ret = btrfs_insert_empty_items(trans, log, dst_path,
2453 ins_keys, ins_sizes, nr);
2456 for (i = 0; i < nr; i++) {
2457 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
2458 dst_path->slots[0]);
2460 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
2462 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
2463 src_offset, ins_sizes[i]);
2465 if (inode_only == LOG_INODE_EXISTS &&
2466 ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
2467 inode_item = btrfs_item_ptr(dst_path->nodes[0],
2469 struct btrfs_inode_item);
2470 btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
2472 /* set the generation to zero so the recover code
2473 * can tell the difference between an logging
2474 * just to say 'this inode exists' and a logging
2475 * to say 'update this inode with these values'
2477 btrfs_set_inode_generation(dst_path->nodes[0],
2480 /* take a reference on file data extents so that truncates
2481 * or deletes of this inode don't have to relog the inode
2484 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) {
2486 extent = btrfs_item_ptr(src, start_slot + i,
2487 struct btrfs_file_extent_item);
2489 found_type = btrfs_file_extent_type(src, extent);
2490 if (found_type == BTRFS_FILE_EXTENT_REG ||
2491 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
2492 u64 ds = btrfs_file_extent_disk_bytenr(src,
2494 u64 dl = btrfs_file_extent_disk_num_bytes(src,
2496 u64 cs = btrfs_file_extent_offset(src, extent);
2497 u64 cl = btrfs_file_extent_num_bytes(src,
2499 if (btrfs_file_extent_compression(src,
2504 /* ds == 0 is a hole */
2506 ret = btrfs_inc_extent_ref(trans, log,
2508 dst_path->nodes[0]->start,
2509 BTRFS_TREE_LOG_OBJECTID,
2511 ins_keys[i].objectid);
2513 ret = btrfs_lookup_csums_range(
2514 log->fs_info->csum_root,
2515 ds + cs, ds + cs + cl - 1,
2521 dst_path->slots[0]++;
2524 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
2525 btrfs_release_path(log, dst_path);
2529 * we have to do this after the loop above to avoid changing the
2530 * log tree while trying to change the log tree.
2532 while (!list_empty(&ordered_sums)) {
2533 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
2534 struct btrfs_ordered_sum,
2536 ret = btrfs_csum_file_blocks(trans, log, sums);
2538 list_del(&sums->list);
2544 /* log a single inode in the tree log.
2545 * At least one parent directory for this inode must exist in the tree
2546 * or be logged already.
2548 * Any items from this inode changed by the current transaction are copied
2549 * to the log tree. An extra reference is taken on any extents in this
2550 * file, allowing us to avoid a whole pile of corner cases around logging
2551 * blocks that have been removed from the tree.
2553 * See LOG_INODE_ALL and related defines for a description of what inode_only
2556 * This handles both files and directories.
2558 static int __btrfs_log_inode(struct btrfs_trans_handle *trans,
2559 struct btrfs_root *root, struct inode *inode,
2562 struct btrfs_path *path;
2563 struct btrfs_path *dst_path;
2564 struct btrfs_key min_key;
2565 struct btrfs_key max_key;
2566 struct btrfs_root *log = root->log_root;
2567 struct extent_buffer *src = NULL;
2571 int ins_start_slot = 0;
2574 log = root->log_root;
2576 path = btrfs_alloc_path();
2577 dst_path = btrfs_alloc_path();
2579 min_key.objectid = inode->i_ino;
2580 min_key.type = BTRFS_INODE_ITEM_KEY;
2583 max_key.objectid = inode->i_ino;
2584 if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
2585 max_key.type = BTRFS_XATTR_ITEM_KEY;
2587 max_key.type = (u8)-1;
2588 max_key.offset = (u64)-1;
2591 * if this inode has already been logged and we're in inode_only
2592 * mode, we don't want to delete the things that have already
2593 * been written to the log.
2595 * But, if the inode has been through an inode_only log,
2596 * the logged_trans field is not set. This allows us to catch
2597 * any new names for this inode in the backrefs by logging it
2600 if (inode_only == LOG_INODE_EXISTS &&
2601 BTRFS_I(inode)->logged_trans == trans->transid) {
2602 btrfs_free_path(path);
2603 btrfs_free_path(dst_path);
2606 mutex_lock(&BTRFS_I(inode)->log_mutex);
2609 * a brute force approach to making sure we get the most uptodate
2610 * copies of everything.
2612 if (S_ISDIR(inode->i_mode)) {
2613 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
2615 if (inode_only == LOG_INODE_EXISTS)
2616 max_key_type = BTRFS_XATTR_ITEM_KEY;
2617 ret = drop_objectid_items(trans, log, path,
2618 inode->i_ino, max_key_type);
2620 ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
2623 path->keep_locks = 1;
2627 ret = btrfs_search_forward(root, &min_key, &max_key,
2628 path, 0, trans->transid);
2632 /* note, ins_nr might be > 0 here, cleanup outside the loop */
2633 if (min_key.objectid != inode->i_ino)
2635 if (min_key.type > max_key.type)
2638 src = path->nodes[0];
2639 size = btrfs_item_size_nr(src, path->slots[0]);
2640 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
2643 } else if (!ins_nr) {
2644 ins_start_slot = path->slots[0];
2649 ret = copy_items(trans, log, dst_path, src, ins_start_slot,
2650 ins_nr, inode_only);
2653 ins_start_slot = path->slots[0];
2656 nritems = btrfs_header_nritems(path->nodes[0]);
2658 if (path->slots[0] < nritems) {
2659 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
2664 ret = copy_items(trans, log, dst_path, src,
2666 ins_nr, inode_only);
2670 btrfs_release_path(root, path);
2672 if (min_key.offset < (u64)-1)
2674 else if (min_key.type < (u8)-1)
2676 else if (min_key.objectid < (u64)-1)
2682 ret = copy_items(trans, log, dst_path, src,
2684 ins_nr, inode_only);
2689 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
2690 btrfs_release_path(root, path);
2691 btrfs_release_path(log, dst_path);
2692 BTRFS_I(inode)->log_dirty_trans = 0;
2693 ret = log_directory_changes(trans, root, inode, path, dst_path);
2696 BTRFS_I(inode)->logged_trans = trans->transid;
2697 mutex_unlock(&BTRFS_I(inode)->log_mutex);
2699 btrfs_free_path(path);
2700 btrfs_free_path(dst_path);
2705 int btrfs_log_inode(struct btrfs_trans_handle *trans,
2706 struct btrfs_root *root, struct inode *inode,
2711 start_log_trans(trans, root);
2712 ret = __btrfs_log_inode(trans, root, inode, inode_only);
2713 end_log_trans(root);
2718 * helper function around btrfs_log_inode to make sure newly created
2719 * parent directories also end up in the log. A minimal inode and backref
2720 * only logging is done of any parent directories that are older than
2721 * the last committed transaction
2723 int btrfs_log_dentry(struct btrfs_trans_handle *trans,
2724 struct btrfs_root *root, struct dentry *dentry)
2726 int inode_only = LOG_INODE_ALL;
2727 struct super_block *sb;
2730 start_log_trans(trans, root);
2731 sb = dentry->d_inode->i_sb;
2733 ret = __btrfs_log_inode(trans, root, dentry->d_inode,
2736 inode_only = LOG_INODE_EXISTS;
2738 dentry = dentry->d_parent;
2739 if (!dentry || !dentry->d_inode || sb != dentry->d_inode->i_sb)
2742 if (BTRFS_I(dentry->d_inode)->generation <=
2743 root->fs_info->last_trans_committed)
2746 end_log_trans(root);
2751 * it is not safe to log dentry if the chunk root has added new
2752 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
2753 * If this returns 1, you must commit the transaction to safely get your
2756 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
2757 struct btrfs_root *root, struct dentry *dentry)
2760 gen = root->fs_info->last_trans_new_blockgroup;
2761 if (gen > root->fs_info->last_trans_committed)
2764 return btrfs_log_dentry(trans, root, dentry);
2768 * should be called during mount to recover any replay any log trees
2771 int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
2774 struct btrfs_path *path;
2775 struct btrfs_trans_handle *trans;
2776 struct btrfs_key key;
2777 struct btrfs_key found_key;
2778 struct btrfs_key tmp_key;
2779 struct btrfs_root *log;
2780 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
2782 struct walk_control wc = {
2783 .process_func = process_one_buffer,
2787 fs_info->log_root_recovering = 1;
2788 path = btrfs_alloc_path();
2791 trans = btrfs_start_transaction(fs_info->tree_root, 1);
2796 walk_log_tree(trans, log_root_tree, &wc);
2799 key.objectid = BTRFS_TREE_LOG_OBJECTID;
2800 key.offset = (u64)-1;
2801 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2804 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
2808 if (path->slots[0] == 0)
2812 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2814 btrfs_release_path(log_root_tree, path);
2815 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
2818 log = btrfs_read_fs_root_no_radix(log_root_tree,
2823 tmp_key.objectid = found_key.offset;
2824 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
2825 tmp_key.offset = (u64)-1;
2827 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
2828 BUG_ON(!wc.replay_dest);
2830 wc.replay_dest->log_root = log;
2831 btrfs_record_root_in_trans(wc.replay_dest);
2832 ret = walk_log_tree(trans, log, &wc);
2835 if (wc.stage == LOG_WALK_REPLAY_ALL) {
2836 ret = fixup_inode_link_counts(trans, wc.replay_dest,
2840 ret = btrfs_find_highest_inode(wc.replay_dest, &highest_inode);
2842 wc.replay_dest->highest_inode = highest_inode;
2843 wc.replay_dest->last_inode_alloc = highest_inode;
2846 key.offset = found_key.offset - 1;
2847 wc.replay_dest->log_root = NULL;
2848 free_extent_buffer(log->node);
2851 if (found_key.offset == 0)
2854 btrfs_release_path(log_root_tree, path);
2856 /* step one is to pin it all, step two is to replay just inodes */
2859 wc.process_func = replay_one_buffer;
2860 wc.stage = LOG_WALK_REPLAY_INODES;
2863 /* step three is to replay everything */
2864 if (wc.stage < LOG_WALK_REPLAY_ALL) {
2869 btrfs_free_path(path);
2871 free_extent_buffer(log_root_tree->node);
2872 log_root_tree->log_root = NULL;
2873 fs_info->log_root_recovering = 0;
2875 /* step 4: commit the transaction, which also unpins the blocks */
2876 btrfs_commit_transaction(trans, fs_info->tree_root);
2878 kfree(log_root_tree);