2 * Copyright (C) 2008 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/list_sort.h>
23 #include "transaction.h"
26 #include "print-tree.h"
32 /* magic values for the inode_only field in btrfs_log_inode:
34 * LOG_INODE_ALL means to log everything
35 * LOG_INODE_EXISTS means to log just enough to recreate the inode
38 #define LOG_INODE_ALL 0
39 #define LOG_INODE_EXISTS 1
42 * directory trouble cases
44 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
45 * log, we must force a full commit before doing an fsync of the directory
46 * where the unlink was done.
47 * ---> record transid of last unlink/rename per directory
51 * rename foo/some_dir foo2/some_dir
53 * fsync foo/some_dir/some_file
55 * The fsync above will unlink the original some_dir without recording
56 * it in its new location (foo2). After a crash, some_dir will be gone
57 * unless the fsync of some_file forces a full commit
59 * 2) we must log any new names for any file or dir that is in the fsync
60 * log. ---> check inode while renaming/linking.
62 * 2a) we must log any new names for any file or dir during rename
63 * when the directory they are being removed from was logged.
64 * ---> check inode and old parent dir during rename
66 * 2a is actually the more important variant. With the extra logging
67 * a crash might unlink the old name without recreating the new one
69 * 3) after a crash, we must go through any directories with a link count
70 * of zero and redo the rm -rf
77 * The directory f1 was fully removed from the FS, but fsync was never
78 * called on f1, only its parent dir. After a crash the rm -rf must
79 * be replayed. This must be able to recurse down the entire
80 * directory tree. The inode link count fixup code takes care of the
85 * stages for the tree walking. The first
86 * stage (0) is to only pin down the blocks we find
87 * the second stage (1) is to make sure that all the inodes
88 * we find in the log are created in the subvolume.
90 * The last stage is to deal with directories and links and extents
91 * and all the other fun semantics
93 #define LOG_WALK_PIN_ONLY 0
94 #define LOG_WALK_REPLAY_INODES 1
95 #define LOG_WALK_REPLAY_ALL 2
97 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root, struct inode *inode,
100 static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
101 struct btrfs_root *root,
102 struct btrfs_path *path, u64 objectid);
103 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
104 struct btrfs_root *root,
105 struct btrfs_root *log,
106 struct btrfs_path *path,
107 u64 dirid, int del_all);
110 * tree logging is a special write ahead log used to make sure that
111 * fsyncs and O_SYNCs can happen without doing full tree commits.
113 * Full tree commits are expensive because they require commonly
114 * modified blocks to be recowed, creating many dirty pages in the
115 * extent tree an 4x-6x higher write load than ext3.
117 * Instead of doing a tree commit on every fsync, we use the
118 * key ranges and transaction ids to find items for a given file or directory
119 * that have changed in this transaction. Those items are copied into
120 * a special tree (one per subvolume root), that tree is written to disk
121 * and then the fsync is considered complete.
123 * After a crash, items are copied out of the log-tree back into the
124 * subvolume tree. Any file data extents found are recorded in the extent
125 * allocation tree, and the log-tree freed.
127 * The log tree is read three times, once to pin down all the extents it is
128 * using in ram and once, once to create all the inodes logged in the tree
129 * and once to do all the other items.
133 * start a sub transaction and setup the log tree
134 * this increments the log tree writer count to make the people
135 * syncing the tree wait for us to finish
137 static int start_log_trans(struct btrfs_trans_handle *trans,
138 struct btrfs_root *root)
143 mutex_lock(&root->log_mutex);
144 if (root->log_root) {
145 if (!root->log_start_pid) {
146 root->log_start_pid = current->pid;
147 root->log_multiple_pids = false;
148 } else if (root->log_start_pid != current->pid) {
149 root->log_multiple_pids = true;
152 atomic_inc(&root->log_batch);
153 atomic_inc(&root->log_writers);
154 mutex_unlock(&root->log_mutex);
157 root->log_multiple_pids = false;
158 root->log_start_pid = current->pid;
159 mutex_lock(&root->fs_info->tree_log_mutex);
160 if (!root->fs_info->log_root_tree) {
161 ret = btrfs_init_log_root_tree(trans, root->fs_info);
165 if (err == 0 && !root->log_root) {
166 ret = btrfs_add_log_tree(trans, root);
170 mutex_unlock(&root->fs_info->tree_log_mutex);
171 atomic_inc(&root->log_batch);
172 atomic_inc(&root->log_writers);
173 mutex_unlock(&root->log_mutex);
178 * returns 0 if there was a log transaction running and we were able
179 * to join, or returns -ENOENT if there were not transactions
182 static int join_running_log_trans(struct btrfs_root *root)
190 mutex_lock(&root->log_mutex);
191 if (root->log_root) {
193 atomic_inc(&root->log_writers);
195 mutex_unlock(&root->log_mutex);
200 * This either makes the current running log transaction wait
201 * until you call btrfs_end_log_trans() or it makes any future
202 * log transactions wait until you call btrfs_end_log_trans()
204 int btrfs_pin_log_trans(struct btrfs_root *root)
208 mutex_lock(&root->log_mutex);
209 atomic_inc(&root->log_writers);
210 mutex_unlock(&root->log_mutex);
215 * indicate we're done making changes to the log tree
216 * and wake up anyone waiting to do a sync
218 void btrfs_end_log_trans(struct btrfs_root *root)
220 if (atomic_dec_and_test(&root->log_writers)) {
222 if (waitqueue_active(&root->log_writer_wait))
223 wake_up(&root->log_writer_wait);
229 * the walk control struct is used to pass state down the chain when
230 * processing the log tree. The stage field tells us which part
231 * of the log tree processing we are currently doing. The others
232 * are state fields used for that specific part
234 struct walk_control {
235 /* should we free the extent on disk when done? This is used
236 * at transaction commit time while freeing a log tree
240 /* should we write out the extent buffer? This is used
241 * while flushing the log tree to disk during a sync
245 /* should we wait for the extent buffer io to finish? Also used
246 * while flushing the log tree to disk for a sync
250 /* pin only walk, we record which extents on disk belong to the
255 /* what stage of the replay code we're currently in */
258 /* the root we are currently replaying */
259 struct btrfs_root *replay_dest;
261 /* the trans handle for the current replay */
262 struct btrfs_trans_handle *trans;
264 /* the function that gets used to process blocks we find in the
265 * tree. Note the extent_buffer might not be up to date when it is
266 * passed in, and it must be checked or read if you need the data
269 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
270 struct walk_control *wc, u64 gen);
274 * process_func used to pin down extents, write them or wait on them
276 static int process_one_buffer(struct btrfs_root *log,
277 struct extent_buffer *eb,
278 struct walk_control *wc, u64 gen)
283 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
286 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
288 btrfs_write_tree_block(eb);
290 btrfs_wait_tree_block_writeback(eb);
296 * Item overwrite used by replay and tree logging. eb, slot and key all refer
297 * to the src data we are copying out.
299 * root is the tree we are copying into, and path is a scratch
300 * path for use in this function (it should be released on entry and
301 * will be released on exit).
303 * If the key is already in the destination tree the existing item is
304 * overwritten. If the existing item isn't big enough, it is extended.
305 * If it is too large, it is truncated.
307 * If the key isn't in the destination yet, a new item is inserted.
309 static noinline int overwrite_item(struct btrfs_trans_handle *trans,
310 struct btrfs_root *root,
311 struct btrfs_path *path,
312 struct extent_buffer *eb, int slot,
313 struct btrfs_key *key)
317 u64 saved_i_size = 0;
318 int save_old_i_size = 0;
319 unsigned long src_ptr;
320 unsigned long dst_ptr;
321 int overwrite_root = 0;
322 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
324 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
327 item_size = btrfs_item_size_nr(eb, slot);
328 src_ptr = btrfs_item_ptr_offset(eb, slot);
330 /* look for the key in the destination tree */
331 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
338 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
340 if (dst_size != item_size)
343 if (item_size == 0) {
344 btrfs_release_path(path);
347 dst_copy = kmalloc(item_size, GFP_NOFS);
348 src_copy = kmalloc(item_size, GFP_NOFS);
349 if (!dst_copy || !src_copy) {
350 btrfs_release_path(path);
356 read_extent_buffer(eb, src_copy, src_ptr, item_size);
358 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
359 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
361 ret = memcmp(dst_copy, src_copy, item_size);
366 * they have the same contents, just return, this saves
367 * us from cowing blocks in the destination tree and doing
368 * extra writes that may not have been done by a previous
372 btrfs_release_path(path);
377 * We need to load the old nbytes into the inode so when we
378 * replay the extents we've logged we get the right nbytes.
381 struct btrfs_inode_item *item;
384 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
385 struct btrfs_inode_item);
386 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
387 item = btrfs_item_ptr(eb, slot,
388 struct btrfs_inode_item);
389 btrfs_set_inode_nbytes(eb, item, nbytes);
391 } else if (inode_item) {
392 struct btrfs_inode_item *item;
395 * New inode, set nbytes to 0 so that the nbytes comes out
396 * properly when we replay the extents.
398 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
399 btrfs_set_inode_nbytes(eb, item, 0);
402 btrfs_release_path(path);
403 /* try to insert the key into the destination tree */
404 ret = btrfs_insert_empty_item(trans, root, path,
407 /* make sure any existing item is the correct size */
408 if (ret == -EEXIST) {
410 found_size = btrfs_item_size_nr(path->nodes[0],
412 if (found_size > item_size)
413 btrfs_truncate_item(root, path, item_size, 1);
414 else if (found_size < item_size)
415 btrfs_extend_item(root, path,
416 item_size - found_size);
420 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
423 /* don't overwrite an existing inode if the generation number
424 * was logged as zero. This is done when the tree logging code
425 * is just logging an inode to make sure it exists after recovery.
427 * Also, don't overwrite i_size on directories during replay.
428 * log replay inserts and removes directory items based on the
429 * state of the tree found in the subvolume, and i_size is modified
432 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
433 struct btrfs_inode_item *src_item;
434 struct btrfs_inode_item *dst_item;
436 src_item = (struct btrfs_inode_item *)src_ptr;
437 dst_item = (struct btrfs_inode_item *)dst_ptr;
439 if (btrfs_inode_generation(eb, src_item) == 0)
442 if (overwrite_root &&
443 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
444 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
446 saved_i_size = btrfs_inode_size(path->nodes[0],
451 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
454 if (save_old_i_size) {
455 struct btrfs_inode_item *dst_item;
456 dst_item = (struct btrfs_inode_item *)dst_ptr;
457 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
460 /* make sure the generation is filled in */
461 if (key->type == BTRFS_INODE_ITEM_KEY) {
462 struct btrfs_inode_item *dst_item;
463 dst_item = (struct btrfs_inode_item *)dst_ptr;
464 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
465 btrfs_set_inode_generation(path->nodes[0], dst_item,
470 btrfs_mark_buffer_dirty(path->nodes[0]);
471 btrfs_release_path(path);
476 * simple helper to read an inode off the disk from a given root
477 * This can only be called for subvolume roots and not for the log
479 static noinline struct inode *read_one_inode(struct btrfs_root *root,
482 struct btrfs_key key;
485 key.objectid = objectid;
486 key.type = BTRFS_INODE_ITEM_KEY;
488 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
491 } else if (is_bad_inode(inode)) {
498 /* replays a single extent in 'eb' at 'slot' with 'key' into the
499 * subvolume 'root'. path is released on entry and should be released
502 * extents in the log tree have not been allocated out of the extent
503 * tree yet. So, this completes the allocation, taking a reference
504 * as required if the extent already exists or creating a new extent
505 * if it isn't in the extent allocation tree yet.
507 * The extent is inserted into the file, dropping any existing extents
508 * from the file that overlap the new one.
510 static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
511 struct btrfs_root *root,
512 struct btrfs_path *path,
513 struct extent_buffer *eb, int slot,
514 struct btrfs_key *key)
518 u64 start = key->offset;
520 struct btrfs_file_extent_item *item;
521 struct inode *inode = NULL;
525 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
526 found_type = btrfs_file_extent_type(eb, item);
528 if (found_type == BTRFS_FILE_EXTENT_REG ||
529 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
530 nbytes = btrfs_file_extent_num_bytes(eb, item);
531 extent_end = start + nbytes;
534 * We don't add to the inodes nbytes if we are prealloc or a
537 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
539 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
540 size = btrfs_file_extent_inline_len(eb, item);
541 nbytes = btrfs_file_extent_ram_bytes(eb, item);
542 extent_end = ALIGN(start + size, root->sectorsize);
548 inode = read_one_inode(root, key->objectid);
555 * first check to see if we already have this extent in the
556 * file. This must be done before the btrfs_drop_extents run
557 * so we don't try to drop this extent.
559 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
563 (found_type == BTRFS_FILE_EXTENT_REG ||
564 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
565 struct btrfs_file_extent_item cmp1;
566 struct btrfs_file_extent_item cmp2;
567 struct btrfs_file_extent_item *existing;
568 struct extent_buffer *leaf;
570 leaf = path->nodes[0];
571 existing = btrfs_item_ptr(leaf, path->slots[0],
572 struct btrfs_file_extent_item);
574 read_extent_buffer(eb, &cmp1, (unsigned long)item,
576 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
580 * we already have a pointer to this exact extent,
581 * we don't have to do anything
583 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
584 btrfs_release_path(path);
588 btrfs_release_path(path);
590 /* drop any overlapping extents */
591 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
594 if (found_type == BTRFS_FILE_EXTENT_REG ||
595 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
597 unsigned long dest_offset;
598 struct btrfs_key ins;
600 ret = btrfs_insert_empty_item(trans, root, path, key,
603 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
605 copy_extent_buffer(path->nodes[0], eb, dest_offset,
606 (unsigned long)item, sizeof(*item));
608 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
609 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
610 ins.type = BTRFS_EXTENT_ITEM_KEY;
611 offset = key->offset - btrfs_file_extent_offset(eb, item);
613 if (ins.objectid > 0) {
616 LIST_HEAD(ordered_sums);
618 * is this extent already allocated in the extent
619 * allocation tree? If so, just add a reference
621 ret = btrfs_lookup_extent(root, ins.objectid,
624 ret = btrfs_inc_extent_ref(trans, root,
625 ins.objectid, ins.offset,
626 0, root->root_key.objectid,
627 key->objectid, offset, 0);
632 * insert the extent pointer in the extent
635 ret = btrfs_alloc_logged_file_extent(trans,
636 root, root->root_key.objectid,
637 key->objectid, offset, &ins);
641 btrfs_release_path(path);
643 if (btrfs_file_extent_compression(eb, item)) {
644 csum_start = ins.objectid;
645 csum_end = csum_start + ins.offset;
647 csum_start = ins.objectid +
648 btrfs_file_extent_offset(eb, item);
649 csum_end = csum_start +
650 btrfs_file_extent_num_bytes(eb, item);
653 ret = btrfs_lookup_csums_range(root->log_root,
654 csum_start, csum_end - 1,
657 while (!list_empty(&ordered_sums)) {
658 struct btrfs_ordered_sum *sums;
659 sums = list_entry(ordered_sums.next,
660 struct btrfs_ordered_sum,
662 ret = btrfs_csum_file_blocks(trans,
663 root->fs_info->csum_root,
666 list_del(&sums->list);
670 btrfs_release_path(path);
672 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
673 /* inline extents are easy, we just overwrite them */
674 ret = overwrite_item(trans, root, path, eb, slot, key);
678 inode_add_bytes(inode, nbytes);
679 ret = btrfs_update_inode(trans, root, inode);
687 * when cleaning up conflicts between the directory names in the
688 * subvolume, directory names in the log and directory names in the
689 * inode back references, we may have to unlink inodes from directories.
691 * This is a helper function to do the unlink of a specific directory
694 static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
695 struct btrfs_root *root,
696 struct btrfs_path *path,
698 struct btrfs_dir_item *di)
703 struct extent_buffer *leaf;
704 struct btrfs_key location;
707 leaf = path->nodes[0];
709 btrfs_dir_item_key_to_cpu(leaf, di, &location);
710 name_len = btrfs_dir_name_len(leaf, di);
711 name = kmalloc(name_len, GFP_NOFS);
715 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
716 btrfs_release_path(path);
718 inode = read_one_inode(root, location.objectid);
724 ret = link_to_fixup_dir(trans, root, path, location.objectid);
727 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
733 btrfs_run_delayed_items(trans, root);
738 * helper function to see if a given name and sequence number found
739 * in an inode back reference are already in a directory and correctly
740 * point to this inode
742 static noinline int inode_in_dir(struct btrfs_root *root,
743 struct btrfs_path *path,
744 u64 dirid, u64 objectid, u64 index,
745 const char *name, int name_len)
747 struct btrfs_dir_item *di;
748 struct btrfs_key location;
751 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
752 index, name, name_len, 0);
753 if (di && !IS_ERR(di)) {
754 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
755 if (location.objectid != objectid)
759 btrfs_release_path(path);
761 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
762 if (di && !IS_ERR(di)) {
763 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
764 if (location.objectid != objectid)
770 btrfs_release_path(path);
775 * helper function to check a log tree for a named back reference in
776 * an inode. This is used to decide if a back reference that is
777 * found in the subvolume conflicts with what we find in the log.
779 * inode backreferences may have multiple refs in a single item,
780 * during replay we process one reference at a time, and we don't
781 * want to delete valid links to a file from the subvolume if that
782 * link is also in the log.
784 static noinline int backref_in_log(struct btrfs_root *log,
785 struct btrfs_key *key,
787 char *name, int namelen)
789 struct btrfs_path *path;
790 struct btrfs_inode_ref *ref;
792 unsigned long ptr_end;
793 unsigned long name_ptr;
799 path = btrfs_alloc_path();
803 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
807 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
809 if (key->type == BTRFS_INODE_EXTREF_KEY) {
810 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
811 name, namelen, NULL))
817 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
818 ptr_end = ptr + item_size;
819 while (ptr < ptr_end) {
820 ref = (struct btrfs_inode_ref *)ptr;
821 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
822 if (found_name_len == namelen) {
823 name_ptr = (unsigned long)(ref + 1);
824 ret = memcmp_extent_buffer(path->nodes[0], name,
831 ptr = (unsigned long)(ref + 1) + found_name_len;
834 btrfs_free_path(path);
838 static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
839 struct btrfs_root *root,
840 struct btrfs_path *path,
841 struct btrfs_root *log_root,
842 struct inode *dir, struct inode *inode,
843 struct extent_buffer *eb,
844 u64 inode_objectid, u64 parent_objectid,
845 u64 ref_index, char *name, int namelen,
851 struct extent_buffer *leaf;
852 struct btrfs_dir_item *di;
853 struct btrfs_key search_key;
854 struct btrfs_inode_extref *extref;
857 /* Search old style refs */
858 search_key.objectid = inode_objectid;
859 search_key.type = BTRFS_INODE_REF_KEY;
860 search_key.offset = parent_objectid;
861 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
863 struct btrfs_inode_ref *victim_ref;
865 unsigned long ptr_end;
867 leaf = path->nodes[0];
869 /* are we trying to overwrite a back ref for the root directory
870 * if so, just jump out, we're done
872 if (search_key.objectid == search_key.offset)
875 /* check all the names in this back reference to see
876 * if they are in the log. if so, we allow them to stay
877 * otherwise they must be unlinked as a conflict
879 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
880 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
881 while (ptr < ptr_end) {
882 victim_ref = (struct btrfs_inode_ref *)ptr;
883 victim_name_len = btrfs_inode_ref_name_len(leaf,
885 victim_name = kmalloc(victim_name_len, GFP_NOFS);
886 BUG_ON(!victim_name);
888 read_extent_buffer(leaf, victim_name,
889 (unsigned long)(victim_ref + 1),
892 if (!backref_in_log(log_root, &search_key,
896 btrfs_inc_nlink(inode);
897 btrfs_release_path(path);
899 ret = btrfs_unlink_inode(trans, root, dir,
903 btrfs_run_delayed_items(trans, root);
910 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
915 * NOTE: we have searched root tree and checked the
916 * coresponding ref, it does not need to check again.
920 btrfs_release_path(path);
922 /* Same search but for extended refs */
923 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
924 inode_objectid, parent_objectid, 0,
926 if (!IS_ERR_OR_NULL(extref)) {
930 struct inode *victim_parent;
932 leaf = path->nodes[0];
934 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
935 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
937 while (cur_offset < item_size) {
938 extref = (struct btrfs_inode_extref *)base + cur_offset;
940 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
942 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
945 victim_name = kmalloc(victim_name_len, GFP_NOFS);
946 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
949 search_key.objectid = inode_objectid;
950 search_key.type = BTRFS_INODE_EXTREF_KEY;
951 search_key.offset = btrfs_extref_hash(parent_objectid,
955 if (!backref_in_log(log_root, &search_key,
956 parent_objectid, victim_name,
959 victim_parent = read_one_inode(root,
962 btrfs_inc_nlink(inode);
963 btrfs_release_path(path);
965 ret = btrfs_unlink_inode(trans, root,
970 btrfs_run_delayed_items(trans, root);
981 cur_offset += victim_name_len + sizeof(*extref);
985 btrfs_release_path(path);
987 /* look for a conflicting sequence number */
988 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
989 ref_index, name, namelen, 0);
990 if (di && !IS_ERR(di)) {
991 ret = drop_one_dir_item(trans, root, path, dir, di);
994 btrfs_release_path(path);
996 /* look for a conflicing name */
997 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
999 if (di && !IS_ERR(di)) {
1000 ret = drop_one_dir_item(trans, root, path, dir, di);
1003 btrfs_release_path(path);
1008 static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1009 u32 *namelen, char **name, u64 *index,
1010 u64 *parent_objectid)
1012 struct btrfs_inode_extref *extref;
1014 extref = (struct btrfs_inode_extref *)ref_ptr;
1016 *namelen = btrfs_inode_extref_name_len(eb, extref);
1017 *name = kmalloc(*namelen, GFP_NOFS);
1021 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1024 *index = btrfs_inode_extref_index(eb, extref);
1025 if (parent_objectid)
1026 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1031 static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1032 u32 *namelen, char **name, u64 *index)
1034 struct btrfs_inode_ref *ref;
1036 ref = (struct btrfs_inode_ref *)ref_ptr;
1038 *namelen = btrfs_inode_ref_name_len(eb, ref);
1039 *name = kmalloc(*namelen, GFP_NOFS);
1043 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1045 *index = btrfs_inode_ref_index(eb, ref);
1051 * replay one inode back reference item found in the log tree.
1052 * eb, slot and key refer to the buffer and key found in the log tree.
1053 * root is the destination we are replaying into, and path is for temp
1054 * use by this function. (it should be released on return).
1056 static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1057 struct btrfs_root *root,
1058 struct btrfs_root *log,
1059 struct btrfs_path *path,
1060 struct extent_buffer *eb, int slot,
1061 struct btrfs_key *key)
1064 struct inode *inode;
1065 unsigned long ref_ptr;
1066 unsigned long ref_end;
1070 int search_done = 0;
1071 int log_ref_ver = 0;
1072 u64 parent_objectid;
1075 int ref_struct_size;
1077 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1078 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1080 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1081 struct btrfs_inode_extref *r;
1083 ref_struct_size = sizeof(struct btrfs_inode_extref);
1085 r = (struct btrfs_inode_extref *)ref_ptr;
1086 parent_objectid = btrfs_inode_extref_parent(eb, r);
1088 ref_struct_size = sizeof(struct btrfs_inode_ref);
1089 parent_objectid = key->offset;
1091 inode_objectid = key->objectid;
1094 * it is possible that we didn't log all the parent directories
1095 * for a given inode. If we don't find the dir, just don't
1096 * copy the back ref in. The link count fixup code will take
1099 dir = read_one_inode(root, parent_objectid);
1103 inode = read_one_inode(root, inode_objectid);
1109 while (ref_ptr < ref_end) {
1111 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1112 &ref_index, &parent_objectid);
1114 * parent object can change from one array
1118 dir = read_one_inode(root, parent_objectid);
1122 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1128 /* if we already have a perfect match, we're done */
1129 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
1130 ref_index, name, namelen)) {
1132 * look for a conflicting back reference in the
1133 * metadata. if we find one we have to unlink that name
1134 * of the file before we add our new link. Later on, we
1135 * overwrite any existing back reference, and we don't
1136 * want to create dangling pointers in the directory.
1140 ret = __add_inode_ref(trans, root, path, log,
1144 ref_index, name, namelen,
1151 /* insert our name */
1152 ret = btrfs_add_link(trans, dir, inode, name, namelen,
1156 btrfs_update_inode(trans, root, inode);
1159 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
1167 /* finally write the back reference in the inode */
1168 ret = overwrite_item(trans, root, path, eb, slot, key);
1172 btrfs_release_path(path);
1178 static int insert_orphan_item(struct btrfs_trans_handle *trans,
1179 struct btrfs_root *root, u64 offset)
1182 ret = btrfs_find_orphan_item(root, offset);
1184 ret = btrfs_insert_orphan_item(trans, root, offset);
1188 static int count_inode_extrefs(struct btrfs_root *root,
1189 struct inode *inode, struct btrfs_path *path)
1193 unsigned int nlink = 0;
1196 u64 inode_objectid = btrfs_ino(inode);
1199 struct btrfs_inode_extref *extref;
1200 struct extent_buffer *leaf;
1203 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1208 leaf = path->nodes[0];
1209 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1210 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1212 while (cur_offset < item_size) {
1213 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1214 name_len = btrfs_inode_extref_name_len(leaf, extref);
1218 cur_offset += name_len + sizeof(*extref);
1222 btrfs_release_path(path);
1224 btrfs_release_path(path);
1231 static int count_inode_refs(struct btrfs_root *root,
1232 struct inode *inode, struct btrfs_path *path)
1235 struct btrfs_key key;
1236 unsigned int nlink = 0;
1238 unsigned long ptr_end;
1240 u64 ino = btrfs_ino(inode);
1243 key.type = BTRFS_INODE_REF_KEY;
1244 key.offset = (u64)-1;
1247 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1251 if (path->slots[0] == 0)
1255 btrfs_item_key_to_cpu(path->nodes[0], &key,
1257 if (key.objectid != ino ||
1258 key.type != BTRFS_INODE_REF_KEY)
1260 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1261 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1263 while (ptr < ptr_end) {
1264 struct btrfs_inode_ref *ref;
1266 ref = (struct btrfs_inode_ref *)ptr;
1267 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1269 ptr = (unsigned long)(ref + 1) + name_len;
1273 if (key.offset == 0)
1276 btrfs_release_path(path);
1278 btrfs_release_path(path);
1284 * There are a few corners where the link count of the file can't
1285 * be properly maintained during replay. So, instead of adding
1286 * lots of complexity to the log code, we just scan the backrefs
1287 * for any file that has been through replay.
1289 * The scan will update the link count on the inode to reflect the
1290 * number of back refs found. If it goes down to zero, the iput
1291 * will free the inode.
1293 static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1294 struct btrfs_root *root,
1295 struct inode *inode)
1297 struct btrfs_path *path;
1300 u64 ino = btrfs_ino(inode);
1302 path = btrfs_alloc_path();
1306 ret = count_inode_refs(root, inode, path);
1312 ret = count_inode_extrefs(root, inode, path);
1323 if (nlink != inode->i_nlink) {
1324 set_nlink(inode, nlink);
1325 btrfs_update_inode(trans, root, inode);
1327 BTRFS_I(inode)->index_cnt = (u64)-1;
1329 if (inode->i_nlink == 0) {
1330 if (S_ISDIR(inode->i_mode)) {
1331 ret = replay_dir_deletes(trans, root, NULL, path,
1335 ret = insert_orphan_item(trans, root, ino);
1340 btrfs_free_path(path);
1344 static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1345 struct btrfs_root *root,
1346 struct btrfs_path *path)
1349 struct btrfs_key key;
1350 struct inode *inode;
1352 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1353 key.type = BTRFS_ORPHAN_ITEM_KEY;
1354 key.offset = (u64)-1;
1356 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1361 if (path->slots[0] == 0)
1366 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1367 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1368 key.type != BTRFS_ORPHAN_ITEM_KEY)
1371 ret = btrfs_del_item(trans, root, path);
1375 btrfs_release_path(path);
1376 inode = read_one_inode(root, key.offset);
1380 ret = fixup_inode_link_count(trans, root, inode);
1386 * fixup on a directory may create new entries,
1387 * make sure we always look for the highset possible
1390 key.offset = (u64)-1;
1394 btrfs_release_path(path);
1400 * record a given inode in the fixup dir so we can check its link
1401 * count when replay is done. The link count is incremented here
1402 * so the inode won't go away until we check it
1404 static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1405 struct btrfs_root *root,
1406 struct btrfs_path *path,
1409 struct btrfs_key key;
1411 struct inode *inode;
1413 inode = read_one_inode(root, objectid);
1417 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1418 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1419 key.offset = objectid;
1421 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1423 btrfs_release_path(path);
1425 if (!inode->i_nlink)
1426 set_nlink(inode, 1);
1428 btrfs_inc_nlink(inode);
1429 ret = btrfs_update_inode(trans, root, inode);
1430 } else if (ret == -EEXIST) {
1441 * when replaying the log for a directory, we only insert names
1442 * for inodes that actually exist. This means an fsync on a directory
1443 * does not implicitly fsync all the new files in it
1445 static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1446 struct btrfs_root *root,
1447 struct btrfs_path *path,
1448 u64 dirid, u64 index,
1449 char *name, int name_len, u8 type,
1450 struct btrfs_key *location)
1452 struct inode *inode;
1456 inode = read_one_inode(root, location->objectid);
1460 dir = read_one_inode(root, dirid);
1465 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1467 /* FIXME, put inode into FIXUP list */
1475 * take a single entry in a log directory item and replay it into
1478 * if a conflicting item exists in the subdirectory already,
1479 * the inode it points to is unlinked and put into the link count
1482 * If a name from the log points to a file or directory that does
1483 * not exist in the FS, it is skipped. fsyncs on directories
1484 * do not force down inodes inside that directory, just changes to the
1485 * names or unlinks in a directory.
1487 static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1488 struct btrfs_root *root,
1489 struct btrfs_path *path,
1490 struct extent_buffer *eb,
1491 struct btrfs_dir_item *di,
1492 struct btrfs_key *key)
1496 struct btrfs_dir_item *dst_di;
1497 struct btrfs_key found_key;
1498 struct btrfs_key log_key;
1504 dir = read_one_inode(root, key->objectid);
1508 name_len = btrfs_dir_name_len(eb, di);
1509 name = kmalloc(name_len, GFP_NOFS);
1513 log_type = btrfs_dir_type(eb, di);
1514 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1517 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
1518 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1523 btrfs_release_path(path);
1525 if (key->type == BTRFS_DIR_ITEM_KEY) {
1526 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1528 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
1529 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1536 if (IS_ERR_OR_NULL(dst_di)) {
1537 /* we need a sequence number to insert, so we only
1538 * do inserts for the BTRFS_DIR_INDEX_KEY types
1540 if (key->type != BTRFS_DIR_INDEX_KEY)
1545 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1546 /* the existing item matches the logged item */
1547 if (found_key.objectid == log_key.objectid &&
1548 found_key.type == log_key.type &&
1549 found_key.offset == log_key.offset &&
1550 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1555 * don't drop the conflicting directory entry if the inode
1556 * for the new entry doesn't exist
1561 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1564 if (key->type == BTRFS_DIR_INDEX_KEY)
1567 btrfs_release_path(path);
1573 btrfs_release_path(path);
1574 ret = insert_one_name(trans, root, path, key->objectid, key->offset,
1575 name, name_len, log_type, &log_key);
1577 BUG_ON(ret && ret != -ENOENT);
1582 * find all the names in a directory item and reconcile them into
1583 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1584 * one name in a directory item, but the same code gets used for
1585 * both directory index types
1587 static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1588 struct btrfs_root *root,
1589 struct btrfs_path *path,
1590 struct extent_buffer *eb, int slot,
1591 struct btrfs_key *key)
1594 u32 item_size = btrfs_item_size_nr(eb, slot);
1595 struct btrfs_dir_item *di;
1598 unsigned long ptr_end;
1600 ptr = btrfs_item_ptr_offset(eb, slot);
1601 ptr_end = ptr + item_size;
1602 while (ptr < ptr_end) {
1603 di = (struct btrfs_dir_item *)ptr;
1604 if (verify_dir_item(root, eb, di))
1606 name_len = btrfs_dir_name_len(eb, di);
1607 ret = replay_one_name(trans, root, path, eb, di, key);
1609 ptr = (unsigned long)(di + 1);
1616 * directory replay has two parts. There are the standard directory
1617 * items in the log copied from the subvolume, and range items
1618 * created in the log while the subvolume was logged.
1620 * The range items tell us which parts of the key space the log
1621 * is authoritative for. During replay, if a key in the subvolume
1622 * directory is in a logged range item, but not actually in the log
1623 * that means it was deleted from the directory before the fsync
1624 * and should be removed.
1626 static noinline int find_dir_range(struct btrfs_root *root,
1627 struct btrfs_path *path,
1628 u64 dirid, int key_type,
1629 u64 *start_ret, u64 *end_ret)
1631 struct btrfs_key key;
1633 struct btrfs_dir_log_item *item;
1637 if (*start_ret == (u64)-1)
1640 key.objectid = dirid;
1641 key.type = key_type;
1642 key.offset = *start_ret;
1644 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1648 if (path->slots[0] == 0)
1653 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1655 if (key.type != key_type || key.objectid != dirid) {
1659 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1660 struct btrfs_dir_log_item);
1661 found_end = btrfs_dir_log_end(path->nodes[0], item);
1663 if (*start_ret >= key.offset && *start_ret <= found_end) {
1665 *start_ret = key.offset;
1666 *end_ret = found_end;
1671 /* check the next slot in the tree to see if it is a valid item */
1672 nritems = btrfs_header_nritems(path->nodes[0]);
1673 if (path->slots[0] >= nritems) {
1674 ret = btrfs_next_leaf(root, path);
1681 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1683 if (key.type != key_type || key.objectid != dirid) {
1687 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1688 struct btrfs_dir_log_item);
1689 found_end = btrfs_dir_log_end(path->nodes[0], item);
1690 *start_ret = key.offset;
1691 *end_ret = found_end;
1694 btrfs_release_path(path);
1699 * this looks for a given directory item in the log. If the directory
1700 * item is not in the log, the item is removed and the inode it points
1703 static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1704 struct btrfs_root *root,
1705 struct btrfs_root *log,
1706 struct btrfs_path *path,
1707 struct btrfs_path *log_path,
1709 struct btrfs_key *dir_key)
1712 struct extent_buffer *eb;
1715 struct btrfs_dir_item *di;
1716 struct btrfs_dir_item *log_di;
1719 unsigned long ptr_end;
1721 struct inode *inode;
1722 struct btrfs_key location;
1725 eb = path->nodes[0];
1726 slot = path->slots[0];
1727 item_size = btrfs_item_size_nr(eb, slot);
1728 ptr = btrfs_item_ptr_offset(eb, slot);
1729 ptr_end = ptr + item_size;
1730 while (ptr < ptr_end) {
1731 di = (struct btrfs_dir_item *)ptr;
1732 if (verify_dir_item(root, eb, di)) {
1737 name_len = btrfs_dir_name_len(eb, di);
1738 name = kmalloc(name_len, GFP_NOFS);
1743 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1746 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
1747 log_di = btrfs_lookup_dir_item(trans, log, log_path,
1750 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
1751 log_di = btrfs_lookup_dir_index_item(trans, log,
1757 if (IS_ERR_OR_NULL(log_di)) {
1758 btrfs_dir_item_key_to_cpu(eb, di, &location);
1759 btrfs_release_path(path);
1760 btrfs_release_path(log_path);
1761 inode = read_one_inode(root, location.objectid);
1767 ret = link_to_fixup_dir(trans, root,
1768 path, location.objectid);
1770 btrfs_inc_nlink(inode);
1771 ret = btrfs_unlink_inode(trans, root, dir, inode,
1775 btrfs_run_delayed_items(trans, root);
1780 /* there might still be more names under this key
1781 * check and repeat if required
1783 ret = btrfs_search_slot(NULL, root, dir_key, path,
1790 btrfs_release_path(log_path);
1793 ptr = (unsigned long)(di + 1);
1798 btrfs_release_path(path);
1799 btrfs_release_path(log_path);
1804 * deletion replay happens before we copy any new directory items
1805 * out of the log or out of backreferences from inodes. It
1806 * scans the log to find ranges of keys that log is authoritative for,
1807 * and then scans the directory to find items in those ranges that are
1808 * not present in the log.
1810 * Anything we don't find in the log is unlinked and removed from the
1813 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
1814 struct btrfs_root *root,
1815 struct btrfs_root *log,
1816 struct btrfs_path *path,
1817 u64 dirid, int del_all)
1821 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
1823 struct btrfs_key dir_key;
1824 struct btrfs_key found_key;
1825 struct btrfs_path *log_path;
1828 dir_key.objectid = dirid;
1829 dir_key.type = BTRFS_DIR_ITEM_KEY;
1830 log_path = btrfs_alloc_path();
1834 dir = read_one_inode(root, dirid);
1835 /* it isn't an error if the inode isn't there, that can happen
1836 * because we replay the deletes before we copy in the inode item
1840 btrfs_free_path(log_path);
1848 range_end = (u64)-1;
1850 ret = find_dir_range(log, path, dirid, key_type,
1851 &range_start, &range_end);
1856 dir_key.offset = range_start;
1859 ret = btrfs_search_slot(NULL, root, &dir_key, path,
1864 nritems = btrfs_header_nritems(path->nodes[0]);
1865 if (path->slots[0] >= nritems) {
1866 ret = btrfs_next_leaf(root, path);
1870 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1872 if (found_key.objectid != dirid ||
1873 found_key.type != dir_key.type)
1876 if (found_key.offset > range_end)
1879 ret = check_item_in_log(trans, root, log, path,
1883 if (found_key.offset == (u64)-1)
1885 dir_key.offset = found_key.offset + 1;
1887 btrfs_release_path(path);
1888 if (range_end == (u64)-1)
1890 range_start = range_end + 1;
1895 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
1896 key_type = BTRFS_DIR_LOG_INDEX_KEY;
1897 dir_key.type = BTRFS_DIR_INDEX_KEY;
1898 btrfs_release_path(path);
1902 btrfs_release_path(path);
1903 btrfs_free_path(log_path);
1909 * the process_func used to replay items from the log tree. This
1910 * gets called in two different stages. The first stage just looks
1911 * for inodes and makes sure they are all copied into the subvolume.
1913 * The second stage copies all the other item types from the log into
1914 * the subvolume. The two stage approach is slower, but gets rid of
1915 * lots of complexity around inodes referencing other inodes that exist
1916 * only in the log (references come from either directory items or inode
1919 static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
1920 struct walk_control *wc, u64 gen)
1923 struct btrfs_path *path;
1924 struct btrfs_root *root = wc->replay_dest;
1925 struct btrfs_key key;
1930 ret = btrfs_read_buffer(eb, gen);
1934 level = btrfs_header_level(eb);
1939 path = btrfs_alloc_path();
1943 nritems = btrfs_header_nritems(eb);
1944 for (i = 0; i < nritems; i++) {
1945 btrfs_item_key_to_cpu(eb, &key, i);
1947 /* inode keys are done during the first stage */
1948 if (key.type == BTRFS_INODE_ITEM_KEY &&
1949 wc->stage == LOG_WALK_REPLAY_INODES) {
1950 struct btrfs_inode_item *inode_item;
1953 inode_item = btrfs_item_ptr(eb, i,
1954 struct btrfs_inode_item);
1955 mode = btrfs_inode_mode(eb, inode_item);
1956 if (S_ISDIR(mode)) {
1957 ret = replay_dir_deletes(wc->trans,
1958 root, log, path, key.objectid, 0);
1962 ret = overwrite_item(wc->trans, root, path,
1967 /* for regular files, make sure corresponding
1968 * orhpan item exist. extents past the new EOF
1969 * will be truncated later by orphan cleanup.
1971 if (S_ISREG(mode)) {
1972 ret = insert_orphan_item(wc->trans, root,
1978 ret = link_to_fixup_dir(wc->trans, root,
1979 path, key.objectid);
1983 if (wc->stage < LOG_WALK_REPLAY_ALL)
1986 /* these keys are simply copied */
1987 if (key.type == BTRFS_XATTR_ITEM_KEY) {
1988 ret = overwrite_item(wc->trans, root, path,
1992 } else if (key.type == BTRFS_INODE_REF_KEY) {
1993 ret = add_inode_ref(wc->trans, root, log, path,
1995 if (ret && ret != -ENOENT)
1998 } else if (key.type == BTRFS_INODE_EXTREF_KEY) {
1999 ret = add_inode_ref(wc->trans, root, log, path,
2001 if (ret && ret != -ENOENT)
2004 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2005 ret = replay_one_extent(wc->trans, root, path,
2009 } else if (key.type == BTRFS_DIR_ITEM_KEY ||
2010 key.type == BTRFS_DIR_INDEX_KEY) {
2011 ret = replay_one_dir_item(wc->trans, root, path,
2017 btrfs_free_path(path);
2021 static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2022 struct btrfs_root *root,
2023 struct btrfs_path *path, int *level,
2024 struct walk_control *wc)
2029 struct extent_buffer *next;
2030 struct extent_buffer *cur;
2031 struct extent_buffer *parent;
2035 WARN_ON(*level < 0);
2036 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2038 while (*level > 0) {
2039 WARN_ON(*level < 0);
2040 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2041 cur = path->nodes[*level];
2043 if (btrfs_header_level(cur) != *level)
2046 if (path->slots[*level] >=
2047 btrfs_header_nritems(cur))
2050 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2051 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2052 blocksize = btrfs_level_size(root, *level - 1);
2054 parent = path->nodes[*level];
2055 root_owner = btrfs_header_owner(parent);
2057 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
2062 ret = wc->process_func(root, next, wc, ptr_gen);
2064 free_extent_buffer(next);
2068 path->slots[*level]++;
2070 ret = btrfs_read_buffer(next, ptr_gen);
2072 free_extent_buffer(next);
2076 btrfs_tree_lock(next);
2077 btrfs_set_lock_blocking(next);
2078 clean_tree_block(trans, root, next);
2079 btrfs_wait_tree_block_writeback(next);
2080 btrfs_tree_unlock(next);
2082 WARN_ON(root_owner !=
2083 BTRFS_TREE_LOG_OBJECTID);
2084 ret = btrfs_free_and_pin_reserved_extent(root,
2086 BUG_ON(ret); /* -ENOMEM or logic errors */
2088 free_extent_buffer(next);
2091 ret = btrfs_read_buffer(next, ptr_gen);
2093 free_extent_buffer(next);
2097 WARN_ON(*level <= 0);
2098 if (path->nodes[*level-1])
2099 free_extent_buffer(path->nodes[*level-1]);
2100 path->nodes[*level-1] = next;
2101 *level = btrfs_header_level(next);
2102 path->slots[*level] = 0;
2105 WARN_ON(*level < 0);
2106 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2108 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
2114 static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
2115 struct btrfs_root *root,
2116 struct btrfs_path *path, int *level,
2117 struct walk_control *wc)
2124 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2125 slot = path->slots[i];
2126 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
2129 WARN_ON(*level == 0);
2132 struct extent_buffer *parent;
2133 if (path->nodes[*level] == root->node)
2134 parent = path->nodes[*level];
2136 parent = path->nodes[*level + 1];
2138 root_owner = btrfs_header_owner(parent);
2139 ret = wc->process_func(root, path->nodes[*level], wc,
2140 btrfs_header_generation(path->nodes[*level]));
2145 struct extent_buffer *next;
2147 next = path->nodes[*level];
2149 btrfs_tree_lock(next);
2150 btrfs_set_lock_blocking(next);
2151 clean_tree_block(trans, root, next);
2152 btrfs_wait_tree_block_writeback(next);
2153 btrfs_tree_unlock(next);
2155 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
2156 ret = btrfs_free_and_pin_reserved_extent(root,
2157 path->nodes[*level]->start,
2158 path->nodes[*level]->len);
2161 free_extent_buffer(path->nodes[*level]);
2162 path->nodes[*level] = NULL;
2170 * drop the reference count on the tree rooted at 'snap'. This traverses
2171 * the tree freeing any blocks that have a ref count of zero after being
2174 static int walk_log_tree(struct btrfs_trans_handle *trans,
2175 struct btrfs_root *log, struct walk_control *wc)
2180 struct btrfs_path *path;
2183 path = btrfs_alloc_path();
2187 level = btrfs_header_level(log->node);
2189 path->nodes[level] = log->node;
2190 extent_buffer_get(log->node);
2191 path->slots[level] = 0;
2194 wret = walk_down_log_tree(trans, log, path, &level, wc);
2202 wret = walk_up_log_tree(trans, log, path, &level, wc);
2211 /* was the root node processed? if not, catch it here */
2212 if (path->nodes[orig_level]) {
2213 ret = wc->process_func(log, path->nodes[orig_level], wc,
2214 btrfs_header_generation(path->nodes[orig_level]));
2218 struct extent_buffer *next;
2220 next = path->nodes[orig_level];
2222 btrfs_tree_lock(next);
2223 btrfs_set_lock_blocking(next);
2224 clean_tree_block(trans, log, next);
2225 btrfs_wait_tree_block_writeback(next);
2226 btrfs_tree_unlock(next);
2228 WARN_ON(log->root_key.objectid !=
2229 BTRFS_TREE_LOG_OBJECTID);
2230 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
2232 BUG_ON(ret); /* -ENOMEM or logic errors */
2237 btrfs_free_path(path);
2242 * helper function to update the item for a given subvolumes log root
2243 * in the tree of log roots
2245 static int update_log_root(struct btrfs_trans_handle *trans,
2246 struct btrfs_root *log)
2250 if (log->log_transid == 1) {
2251 /* insert root item on the first sync */
2252 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2253 &log->root_key, &log->root_item);
2255 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2256 &log->root_key, &log->root_item);
2261 static int wait_log_commit(struct btrfs_trans_handle *trans,
2262 struct btrfs_root *root, unsigned long transid)
2265 int index = transid % 2;
2268 * we only allow two pending log transactions at a time,
2269 * so we know that if ours is more than 2 older than the
2270 * current transaction, we're done
2273 prepare_to_wait(&root->log_commit_wait[index],
2274 &wait, TASK_UNINTERRUPTIBLE);
2275 mutex_unlock(&root->log_mutex);
2277 if (root->fs_info->last_trans_log_full_commit !=
2278 trans->transid && root->log_transid < transid + 2 &&
2279 atomic_read(&root->log_commit[index]))
2282 finish_wait(&root->log_commit_wait[index], &wait);
2283 mutex_lock(&root->log_mutex);
2284 } while (root->fs_info->last_trans_log_full_commit !=
2285 trans->transid && root->log_transid < transid + 2 &&
2286 atomic_read(&root->log_commit[index]));
2290 static void wait_for_writer(struct btrfs_trans_handle *trans,
2291 struct btrfs_root *root)
2294 while (root->fs_info->last_trans_log_full_commit !=
2295 trans->transid && atomic_read(&root->log_writers)) {
2296 prepare_to_wait(&root->log_writer_wait,
2297 &wait, TASK_UNINTERRUPTIBLE);
2298 mutex_unlock(&root->log_mutex);
2299 if (root->fs_info->last_trans_log_full_commit !=
2300 trans->transid && atomic_read(&root->log_writers))
2302 mutex_lock(&root->log_mutex);
2303 finish_wait(&root->log_writer_wait, &wait);
2308 * btrfs_sync_log does sends a given tree log down to the disk and
2309 * updates the super blocks to record it. When this call is done,
2310 * you know that any inodes previously logged are safely on disk only
2313 * Any other return value means you need to call btrfs_commit_transaction.
2314 * Some of the edge cases for fsyncing directories that have had unlinks
2315 * or renames done in the past mean that sometimes the only safe
2316 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2317 * that has happened.
2319 int btrfs_sync_log(struct btrfs_trans_handle *trans,
2320 struct btrfs_root *root)
2326 struct btrfs_root *log = root->log_root;
2327 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
2328 unsigned long log_transid = 0;
2330 mutex_lock(&root->log_mutex);
2331 log_transid = root->log_transid;
2332 index1 = root->log_transid % 2;
2333 if (atomic_read(&root->log_commit[index1])) {
2334 wait_log_commit(trans, root, root->log_transid);
2335 mutex_unlock(&root->log_mutex);
2338 atomic_set(&root->log_commit[index1], 1);
2340 /* wait for previous tree log sync to complete */
2341 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
2342 wait_log_commit(trans, root, root->log_transid - 1);
2344 int batch = atomic_read(&root->log_batch);
2345 /* when we're on an ssd, just kick the log commit out */
2346 if (!btrfs_test_opt(root, SSD) && root->log_multiple_pids) {
2347 mutex_unlock(&root->log_mutex);
2348 schedule_timeout_uninterruptible(1);
2349 mutex_lock(&root->log_mutex);
2351 wait_for_writer(trans, root);
2352 if (batch == atomic_read(&root->log_batch))
2356 /* bail out if we need to do a full commit */
2357 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2359 btrfs_free_logged_extents(log, log_transid);
2360 mutex_unlock(&root->log_mutex);
2364 if (log_transid % 2 == 0)
2365 mark = EXTENT_DIRTY;
2369 /* we start IO on all the marked extents here, but we don't actually
2370 * wait for them until later.
2372 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
2374 btrfs_abort_transaction(trans, root, ret);
2375 btrfs_free_logged_extents(log, log_transid);
2376 mutex_unlock(&root->log_mutex);
2380 btrfs_set_root_node(&log->root_item, log->node);
2382 root->log_transid++;
2383 log->log_transid = root->log_transid;
2384 root->log_start_pid = 0;
2387 * IO has been started, blocks of the log tree have WRITTEN flag set
2388 * in their headers. new modifications of the log will be written to
2389 * new positions. so it's safe to allow log writers to go in.
2391 mutex_unlock(&root->log_mutex);
2393 mutex_lock(&log_root_tree->log_mutex);
2394 atomic_inc(&log_root_tree->log_batch);
2395 atomic_inc(&log_root_tree->log_writers);
2396 mutex_unlock(&log_root_tree->log_mutex);
2398 ret = update_log_root(trans, log);
2400 mutex_lock(&log_root_tree->log_mutex);
2401 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2403 if (waitqueue_active(&log_root_tree->log_writer_wait))
2404 wake_up(&log_root_tree->log_writer_wait);
2408 if (ret != -ENOSPC) {
2409 btrfs_abort_transaction(trans, root, ret);
2410 mutex_unlock(&log_root_tree->log_mutex);
2413 root->fs_info->last_trans_log_full_commit = trans->transid;
2414 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2415 btrfs_free_logged_extents(log, log_transid);
2416 mutex_unlock(&log_root_tree->log_mutex);
2421 index2 = log_root_tree->log_transid % 2;
2422 if (atomic_read(&log_root_tree->log_commit[index2])) {
2423 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2424 wait_log_commit(trans, log_root_tree,
2425 log_root_tree->log_transid);
2426 btrfs_free_logged_extents(log, log_transid);
2427 mutex_unlock(&log_root_tree->log_mutex);
2431 atomic_set(&log_root_tree->log_commit[index2], 1);
2433 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2434 wait_log_commit(trans, log_root_tree,
2435 log_root_tree->log_transid - 1);
2438 wait_for_writer(trans, log_root_tree);
2441 * now that we've moved on to the tree of log tree roots,
2442 * check the full commit flag again
2444 if (root->fs_info->last_trans_log_full_commit == trans->transid) {
2445 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2446 btrfs_free_logged_extents(log, log_transid);
2447 mutex_unlock(&log_root_tree->log_mutex);
2449 goto out_wake_log_root;
2452 ret = btrfs_write_and_wait_marked_extents(log_root_tree,
2453 &log_root_tree->dirty_log_pages,
2454 EXTENT_DIRTY | EXTENT_NEW);
2456 btrfs_abort_transaction(trans, root, ret);
2457 btrfs_free_logged_extents(log, log_transid);
2458 mutex_unlock(&log_root_tree->log_mutex);
2459 goto out_wake_log_root;
2461 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2462 btrfs_wait_logged_extents(log, log_transid);
2464 btrfs_set_super_log_root(root->fs_info->super_for_commit,
2465 log_root_tree->node->start);
2466 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
2467 btrfs_header_level(log_root_tree->node));
2469 log_root_tree->log_transid++;
2472 mutex_unlock(&log_root_tree->log_mutex);
2475 * nobody else is going to jump in and write the the ctree
2476 * super here because the log_commit atomic below is protecting
2477 * us. We must be called with a transaction handle pinning
2478 * the running transaction open, so a full commit can't hop
2479 * in and cause problems either.
2481 btrfs_scrub_pause_super(root);
2482 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
2483 btrfs_scrub_continue_super(root);
2485 btrfs_abort_transaction(trans, root, ret);
2486 goto out_wake_log_root;
2489 mutex_lock(&root->log_mutex);
2490 if (root->last_log_commit < log_transid)
2491 root->last_log_commit = log_transid;
2492 mutex_unlock(&root->log_mutex);
2495 atomic_set(&log_root_tree->log_commit[index2], 0);
2497 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2498 wake_up(&log_root_tree->log_commit_wait[index2]);
2500 atomic_set(&root->log_commit[index1], 0);
2502 if (waitqueue_active(&root->log_commit_wait[index1]))
2503 wake_up(&root->log_commit_wait[index1]);
2507 static void free_log_tree(struct btrfs_trans_handle *trans,
2508 struct btrfs_root *log)
2513 struct walk_control wc = {
2515 .process_func = process_one_buffer
2519 ret = walk_log_tree(trans, log, &wc);
2524 ret = find_first_extent_bit(&log->dirty_log_pages,
2525 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
2530 clear_extent_bits(&log->dirty_log_pages, start, end,
2531 EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
2535 * We may have short-circuited the log tree with the full commit logic
2536 * and left ordered extents on our list, so clear these out to keep us
2537 * from leaking inodes and memory.
2539 btrfs_free_logged_extents(log, 0);
2540 btrfs_free_logged_extents(log, 1);
2542 free_extent_buffer(log->node);
2547 * free all the extents used by the tree log. This should be called
2548 * at commit time of the full transaction
2550 int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
2552 if (root->log_root) {
2553 free_log_tree(trans, root->log_root);
2554 root->log_root = NULL;
2559 int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
2560 struct btrfs_fs_info *fs_info)
2562 if (fs_info->log_root_tree) {
2563 free_log_tree(trans, fs_info->log_root_tree);
2564 fs_info->log_root_tree = NULL;
2570 * If both a file and directory are logged, and unlinks or renames are
2571 * mixed in, we have a few interesting corners:
2573 * create file X in dir Y
2574 * link file X to X.link in dir Y
2576 * unlink file X but leave X.link
2579 * After a crash we would expect only X.link to exist. But file X
2580 * didn't get fsync'd again so the log has back refs for X and X.link.
2582 * We solve this by removing directory entries and inode backrefs from the
2583 * log when a file that was logged in the current transaction is
2584 * unlinked. Any later fsync will include the updated log entries, and
2585 * we'll be able to reconstruct the proper directory items from backrefs.
2587 * This optimizations allows us to avoid relogging the entire inode
2588 * or the entire directory.
2590 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
2591 struct btrfs_root *root,
2592 const char *name, int name_len,
2593 struct inode *dir, u64 index)
2595 struct btrfs_root *log;
2596 struct btrfs_dir_item *di;
2597 struct btrfs_path *path;
2601 u64 dir_ino = btrfs_ino(dir);
2603 if (BTRFS_I(dir)->logged_trans < trans->transid)
2606 ret = join_running_log_trans(root);
2610 mutex_lock(&BTRFS_I(dir)->log_mutex);
2612 log = root->log_root;
2613 path = btrfs_alloc_path();
2619 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
2620 name, name_len, -1);
2626 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2627 bytes_del += name_len;
2630 btrfs_release_path(path);
2631 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
2632 index, name, name_len, -1);
2638 ret = btrfs_delete_one_dir_name(trans, log, path, di);
2639 bytes_del += name_len;
2643 /* update the directory size in the log to reflect the names
2647 struct btrfs_key key;
2649 key.objectid = dir_ino;
2651 key.type = BTRFS_INODE_ITEM_KEY;
2652 btrfs_release_path(path);
2654 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
2660 struct btrfs_inode_item *item;
2663 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2664 struct btrfs_inode_item);
2665 i_size = btrfs_inode_size(path->nodes[0], item);
2666 if (i_size > bytes_del)
2667 i_size -= bytes_del;
2670 btrfs_set_inode_size(path->nodes[0], item, i_size);
2671 btrfs_mark_buffer_dirty(path->nodes[0]);
2674 btrfs_release_path(path);
2677 btrfs_free_path(path);
2679 mutex_unlock(&BTRFS_I(dir)->log_mutex);
2680 if (ret == -ENOSPC) {
2681 root->fs_info->last_trans_log_full_commit = trans->transid;
2684 btrfs_abort_transaction(trans, root, ret);
2686 btrfs_end_log_trans(root);
2691 /* see comments for btrfs_del_dir_entries_in_log */
2692 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
2693 struct btrfs_root *root,
2694 const char *name, int name_len,
2695 struct inode *inode, u64 dirid)
2697 struct btrfs_root *log;
2701 if (BTRFS_I(inode)->logged_trans < trans->transid)
2704 ret = join_running_log_trans(root);
2707 log = root->log_root;
2708 mutex_lock(&BTRFS_I(inode)->log_mutex);
2710 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
2712 mutex_unlock(&BTRFS_I(inode)->log_mutex);
2713 if (ret == -ENOSPC) {
2714 root->fs_info->last_trans_log_full_commit = trans->transid;
2716 } else if (ret < 0 && ret != -ENOENT)
2717 btrfs_abort_transaction(trans, root, ret);
2718 btrfs_end_log_trans(root);
2724 * creates a range item in the log for 'dirid'. first_offset and
2725 * last_offset tell us which parts of the key space the log should
2726 * be considered authoritative for.
2728 static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
2729 struct btrfs_root *log,
2730 struct btrfs_path *path,
2731 int key_type, u64 dirid,
2732 u64 first_offset, u64 last_offset)
2735 struct btrfs_key key;
2736 struct btrfs_dir_log_item *item;
2738 key.objectid = dirid;
2739 key.offset = first_offset;
2740 if (key_type == BTRFS_DIR_ITEM_KEY)
2741 key.type = BTRFS_DIR_LOG_ITEM_KEY;
2743 key.type = BTRFS_DIR_LOG_INDEX_KEY;
2744 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
2748 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2749 struct btrfs_dir_log_item);
2750 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
2751 btrfs_mark_buffer_dirty(path->nodes[0]);
2752 btrfs_release_path(path);
2757 * log all the items included in the current transaction for a given
2758 * directory. This also creates the range items in the log tree required
2759 * to replay anything deleted before the fsync
2761 static noinline int log_dir_items(struct btrfs_trans_handle *trans,
2762 struct btrfs_root *root, struct inode *inode,
2763 struct btrfs_path *path,
2764 struct btrfs_path *dst_path, int key_type,
2765 u64 min_offset, u64 *last_offset_ret)
2767 struct btrfs_key min_key;
2768 struct btrfs_key max_key;
2769 struct btrfs_root *log = root->log_root;
2770 struct extent_buffer *src;
2775 u64 first_offset = min_offset;
2776 u64 last_offset = (u64)-1;
2777 u64 ino = btrfs_ino(inode);
2779 log = root->log_root;
2780 max_key.objectid = ino;
2781 max_key.offset = (u64)-1;
2782 max_key.type = key_type;
2784 min_key.objectid = ino;
2785 min_key.type = key_type;
2786 min_key.offset = min_offset;
2788 path->keep_locks = 1;
2790 ret = btrfs_search_forward(root, &min_key, &max_key,
2791 path, trans->transid);
2794 * we didn't find anything from this transaction, see if there
2795 * is anything at all
2797 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
2798 min_key.objectid = ino;
2799 min_key.type = key_type;
2800 min_key.offset = (u64)-1;
2801 btrfs_release_path(path);
2802 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2804 btrfs_release_path(path);
2807 ret = btrfs_previous_item(root, path, ino, key_type);
2809 /* if ret == 0 there are items for this type,
2810 * create a range to tell us the last key of this type.
2811 * otherwise, there are no items in this directory after
2812 * *min_offset, and we create a range to indicate that.
2815 struct btrfs_key tmp;
2816 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
2818 if (key_type == tmp.type)
2819 first_offset = max(min_offset, tmp.offset) + 1;
2824 /* go backward to find any previous key */
2825 ret = btrfs_previous_item(root, path, ino, key_type);
2827 struct btrfs_key tmp;
2828 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2829 if (key_type == tmp.type) {
2830 first_offset = tmp.offset;
2831 ret = overwrite_item(trans, log, dst_path,
2832 path->nodes[0], path->slots[0],
2840 btrfs_release_path(path);
2842 /* find the first key from this transaction again */
2843 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
2850 * we have a block from this transaction, log every item in it
2851 * from our directory
2854 struct btrfs_key tmp;
2855 src = path->nodes[0];
2856 nritems = btrfs_header_nritems(src);
2857 for (i = path->slots[0]; i < nritems; i++) {
2858 btrfs_item_key_to_cpu(src, &min_key, i);
2860 if (min_key.objectid != ino || min_key.type != key_type)
2862 ret = overwrite_item(trans, log, dst_path, src, i,
2869 path->slots[0] = nritems;
2872 * look ahead to the next item and see if it is also
2873 * from this directory and from this transaction
2875 ret = btrfs_next_leaf(root, path);
2877 last_offset = (u64)-1;
2880 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
2881 if (tmp.objectid != ino || tmp.type != key_type) {
2882 last_offset = (u64)-1;
2885 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
2886 ret = overwrite_item(trans, log, dst_path,
2887 path->nodes[0], path->slots[0],
2892 last_offset = tmp.offset;
2897 btrfs_release_path(path);
2898 btrfs_release_path(dst_path);
2901 *last_offset_ret = last_offset;
2903 * insert the log range keys to indicate where the log
2906 ret = insert_dir_log_key(trans, log, path, key_type,
2907 ino, first_offset, last_offset);
2915 * logging directories is very similar to logging inodes, We find all the items
2916 * from the current transaction and write them to the log.
2918 * The recovery code scans the directory in the subvolume, and if it finds a
2919 * key in the range logged that is not present in the log tree, then it means
2920 * that dir entry was unlinked during the transaction.
2922 * In order for that scan to work, we must include one key smaller than
2923 * the smallest logged by this transaction and one key larger than the largest
2924 * key logged by this transaction.
2926 static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
2927 struct btrfs_root *root, struct inode *inode,
2928 struct btrfs_path *path,
2929 struct btrfs_path *dst_path)
2934 int key_type = BTRFS_DIR_ITEM_KEY;
2940 ret = log_dir_items(trans, root, inode, path,
2941 dst_path, key_type, min_key,
2945 if (max_key == (u64)-1)
2947 min_key = max_key + 1;
2950 if (key_type == BTRFS_DIR_ITEM_KEY) {
2951 key_type = BTRFS_DIR_INDEX_KEY;
2958 * a helper function to drop items from the log before we relog an
2959 * inode. max_key_type indicates the highest item type to remove.
2960 * This cannot be run for file data extents because it does not
2961 * free the extents they point to.
2963 static int drop_objectid_items(struct btrfs_trans_handle *trans,
2964 struct btrfs_root *log,
2965 struct btrfs_path *path,
2966 u64 objectid, int max_key_type)
2969 struct btrfs_key key;
2970 struct btrfs_key found_key;
2973 key.objectid = objectid;
2974 key.type = max_key_type;
2975 key.offset = (u64)-1;
2978 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
2983 if (path->slots[0] == 0)
2987 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2990 if (found_key.objectid != objectid)
2993 found_key.offset = 0;
2995 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
2998 ret = btrfs_del_items(trans, log, path, start_slot,
2999 path->slots[0] - start_slot + 1);
3001 * If start slot isn't 0 then we don't need to re-search, we've
3002 * found the last guy with the objectid in this tree.
3004 if (ret || start_slot != 0)
3006 btrfs_release_path(path);
3008 btrfs_release_path(path);
3014 static void fill_inode_item(struct btrfs_trans_handle *trans,
3015 struct extent_buffer *leaf,
3016 struct btrfs_inode_item *item,
3017 struct inode *inode, int log_inode_only)
3019 struct btrfs_map_token token;
3021 btrfs_init_map_token(&token);
3023 if (log_inode_only) {
3024 /* set the generation to zero so the recover code
3025 * can tell the difference between an logging
3026 * just to say 'this inode exists' and a logging
3027 * to say 'update this inode with these values'
3029 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3030 btrfs_set_token_inode_size(leaf, item, 0, &token);
3032 btrfs_set_token_inode_generation(leaf, item,
3033 BTRFS_I(inode)->generation,
3035 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
3038 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3039 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3040 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3041 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3043 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3044 inode->i_atime.tv_sec, &token);
3045 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3046 inode->i_atime.tv_nsec, &token);
3048 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3049 inode->i_mtime.tv_sec, &token);
3050 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3051 inode->i_mtime.tv_nsec, &token);
3053 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3054 inode->i_ctime.tv_sec, &token);
3055 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3056 inode->i_ctime.tv_nsec, &token);
3058 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3061 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3062 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3063 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3064 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3065 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
3068 static int log_inode_item(struct btrfs_trans_handle *trans,
3069 struct btrfs_root *log, struct btrfs_path *path,
3070 struct inode *inode)
3072 struct btrfs_inode_item *inode_item;
3073 struct btrfs_key key;
3076 memcpy(&key, &BTRFS_I(inode)->location, sizeof(key));
3077 ret = btrfs_insert_empty_item(trans, log, path, &key,
3078 sizeof(*inode_item));
3079 if (ret && ret != -EEXIST)
3081 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3082 struct btrfs_inode_item);
3083 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0);
3084 btrfs_release_path(path);
3088 static noinline int copy_items(struct btrfs_trans_handle *trans,
3089 struct inode *inode,
3090 struct btrfs_path *dst_path,
3091 struct extent_buffer *src,
3092 int start_slot, int nr, int inode_only)
3094 unsigned long src_offset;
3095 unsigned long dst_offset;
3096 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
3097 struct btrfs_file_extent_item *extent;
3098 struct btrfs_inode_item *inode_item;
3100 struct btrfs_key *ins_keys;
3104 struct list_head ordered_sums;
3105 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
3107 INIT_LIST_HEAD(&ordered_sums);
3109 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3110 nr * sizeof(u32), GFP_NOFS);
3114 ins_sizes = (u32 *)ins_data;
3115 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3117 for (i = 0; i < nr; i++) {
3118 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3119 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3121 ret = btrfs_insert_empty_items(trans, log, dst_path,
3122 ins_keys, ins_sizes, nr);
3128 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
3129 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3130 dst_path->slots[0]);
3132 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3134 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
3135 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3137 struct btrfs_inode_item);
3138 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3139 inode, inode_only == LOG_INODE_EXISTS);
3141 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3142 src_offset, ins_sizes[i]);
3145 /* take a reference on file data extents so that truncates
3146 * or deletes of this inode don't have to relog the inode
3149 if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY &&
3152 extent = btrfs_item_ptr(src, start_slot + i,
3153 struct btrfs_file_extent_item);
3155 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3158 found_type = btrfs_file_extent_type(src, extent);
3159 if (found_type == BTRFS_FILE_EXTENT_REG) {
3161 ds = btrfs_file_extent_disk_bytenr(src,
3163 /* ds == 0 is a hole */
3167 dl = btrfs_file_extent_disk_num_bytes(src,
3169 cs = btrfs_file_extent_offset(src, extent);
3170 cl = btrfs_file_extent_num_bytes(src,
3172 if (btrfs_file_extent_compression(src,
3178 ret = btrfs_lookup_csums_range(
3179 log->fs_info->csum_root,
3180 ds + cs, ds + cs + cl - 1,
3187 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
3188 btrfs_release_path(dst_path);
3192 * we have to do this after the loop above to avoid changing the
3193 * log tree while trying to change the log tree.
3196 while (!list_empty(&ordered_sums)) {
3197 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3198 struct btrfs_ordered_sum,
3201 ret = btrfs_csum_file_blocks(trans, log, sums);
3202 list_del(&sums->list);
3208 static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3210 struct extent_map *em1, *em2;
3212 em1 = list_entry(a, struct extent_map, list);
3213 em2 = list_entry(b, struct extent_map, list);
3215 if (em1->start < em2->start)
3217 else if (em1->start > em2->start)
3222 static int log_one_extent(struct btrfs_trans_handle *trans,
3223 struct inode *inode, struct btrfs_root *root,
3224 struct extent_map *em, struct btrfs_path *path)
3226 struct btrfs_root *log = root->log_root;
3227 struct btrfs_file_extent_item *fi;
3228 struct extent_buffer *leaf;
3229 struct btrfs_ordered_extent *ordered;
3230 struct list_head ordered_sums;
3231 struct btrfs_map_token token;
3232 struct btrfs_key key;
3233 u64 mod_start = em->mod_start;
3234 u64 mod_len = em->mod_len;
3237 u64 extent_offset = em->start - em->orig_start;
3240 int index = log->log_transid % 2;
3241 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
3243 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
3244 em->start + em->len, NULL, 0);
3248 INIT_LIST_HEAD(&ordered_sums);
3249 btrfs_init_map_token(&token);
3250 key.objectid = btrfs_ino(inode);
3251 key.type = BTRFS_EXTENT_DATA_KEY;
3252 key.offset = em->start;
3254 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*fi));
3257 leaf = path->nodes[0];
3258 fi = btrfs_item_ptr(leaf, path->slots[0],
3259 struct btrfs_file_extent_item);
3261 btrfs_set_token_file_extent_generation(leaf, fi, em->generation,
3263 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3265 btrfs_set_token_file_extent_type(leaf, fi,
3266 BTRFS_FILE_EXTENT_PREALLOC,
3269 btrfs_set_token_file_extent_type(leaf, fi,
3270 BTRFS_FILE_EXTENT_REG,
3272 if (em->block_start == 0)
3276 block_len = max(em->block_len, em->orig_block_len);
3277 if (em->compress_type != BTRFS_COMPRESS_NONE) {
3278 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3281 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3283 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
3284 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
3286 extent_offset, &token);
3287 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
3290 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
3291 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
3295 btrfs_set_token_file_extent_offset(leaf, fi,
3296 em->start - em->orig_start,
3298 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
3299 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
3300 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
3302 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
3303 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
3304 btrfs_mark_buffer_dirty(leaf);
3306 btrfs_release_path(path);
3314 if (em->compress_type) {
3316 csum_len = block_len;
3320 * First check and see if our csums are on our outstanding ordered
3324 spin_lock_irq(&log->log_extents_lock[index]);
3325 list_for_each_entry(ordered, &log->logged_list[index], log_list) {
3326 struct btrfs_ordered_sum *sum;
3331 if (ordered->inode != inode)
3334 if (ordered->file_offset + ordered->len <= mod_start ||
3335 mod_start + mod_len <= ordered->file_offset)
3339 * We are going to copy all the csums on this ordered extent, so
3340 * go ahead and adjust mod_start and mod_len in case this
3341 * ordered extent has already been logged.
3343 if (ordered->file_offset > mod_start) {
3344 if (ordered->file_offset + ordered->len >=
3345 mod_start + mod_len)
3346 mod_len = ordered->file_offset - mod_start;
3348 * If we have this case
3350 * |--------- logged extent ---------|
3351 * |----- ordered extent ----|
3353 * Just don't mess with mod_start and mod_len, we'll
3354 * just end up logging more csums than we need and it
3358 if (ordered->file_offset + ordered->len <
3359 mod_start + mod_len) {
3360 mod_len = (mod_start + mod_len) -
3361 (ordered->file_offset + ordered->len);
3362 mod_start = ordered->file_offset +
3370 * To keep us from looping for the above case of an ordered
3371 * extent that falls inside of the logged extent.
3373 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
3376 atomic_inc(&ordered->refs);
3377 spin_unlock_irq(&log->log_extents_lock[index]);
3379 * we've dropped the lock, we must either break or
3380 * start over after this.
3383 wait_event(ordered->wait, ordered->csum_bytes_left == 0);
3385 list_for_each_entry(sum, &ordered->list, list) {
3386 ret = btrfs_csum_file_blocks(trans, log, sum);
3388 btrfs_put_ordered_extent(ordered);
3392 btrfs_put_ordered_extent(ordered);
3396 spin_unlock_irq(&log->log_extents_lock[index]);
3399 if (!mod_len || ret)
3402 csum_offset = mod_start - em->start;
3405 /* block start is already adjusted for the file extent offset. */
3406 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3407 em->block_start + csum_offset,
3408 em->block_start + csum_offset +
3409 csum_len - 1, &ordered_sums, 0);
3413 while (!list_empty(&ordered_sums)) {
3414 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3415 struct btrfs_ordered_sum,
3418 ret = btrfs_csum_file_blocks(trans, log, sums);
3419 list_del(&sums->list);
3426 static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3427 struct btrfs_root *root,
3428 struct inode *inode,
3429 struct btrfs_path *path)
3431 struct extent_map *em, *n;
3432 struct list_head extents;
3433 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3438 INIT_LIST_HEAD(&extents);
3440 write_lock(&tree->lock);
3441 test_gen = root->fs_info->last_trans_committed;
3443 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
3444 list_del_init(&em->list);
3447 * Just an arbitrary number, this can be really CPU intensive
3448 * once we start getting a lot of extents, and really once we
3449 * have a bunch of extents we just want to commit since it will
3452 if (++num > 32768) {
3453 list_del_init(&tree->modified_extents);
3458 if (em->generation <= test_gen)
3460 /* Need a ref to keep it from getting evicted from cache */
3461 atomic_inc(&em->refs);
3462 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
3463 list_add_tail(&em->list, &extents);
3467 list_sort(NULL, &extents, extent_cmp);
3470 while (!list_empty(&extents)) {
3471 em = list_entry(extents.next, struct extent_map, list);
3473 list_del_init(&em->list);
3476 * If we had an error we just need to delete everybody from our
3480 clear_em_logging(tree, em);
3481 free_extent_map(em);
3485 write_unlock(&tree->lock);
3487 ret = log_one_extent(trans, inode, root, em, path);
3488 write_lock(&tree->lock);
3489 clear_em_logging(tree, em);
3490 free_extent_map(em);
3492 WARN_ON(!list_empty(&extents));
3493 write_unlock(&tree->lock);
3495 btrfs_release_path(path);
3499 /* log a single inode in the tree log.
3500 * At least one parent directory for this inode must exist in the tree
3501 * or be logged already.
3503 * Any items from this inode changed by the current transaction are copied
3504 * to the log tree. An extra reference is taken on any extents in this
3505 * file, allowing us to avoid a whole pile of corner cases around logging
3506 * blocks that have been removed from the tree.
3508 * See LOG_INODE_ALL and related defines for a description of what inode_only
3511 * This handles both files and directories.
3513 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
3514 struct btrfs_root *root, struct inode *inode,
3517 struct btrfs_path *path;
3518 struct btrfs_path *dst_path;
3519 struct btrfs_key min_key;
3520 struct btrfs_key max_key;
3521 struct btrfs_root *log = root->log_root;
3522 struct extent_buffer *src = NULL;
3526 int ins_start_slot = 0;
3528 bool fast_search = false;
3529 u64 ino = btrfs_ino(inode);
3531 path = btrfs_alloc_path();
3534 dst_path = btrfs_alloc_path();
3536 btrfs_free_path(path);
3540 min_key.objectid = ino;
3541 min_key.type = BTRFS_INODE_ITEM_KEY;
3544 max_key.objectid = ino;
3547 /* today the code can only do partial logging of directories */
3548 if (S_ISDIR(inode->i_mode) ||
3549 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3550 &BTRFS_I(inode)->runtime_flags) &&
3551 inode_only == LOG_INODE_EXISTS))
3552 max_key.type = BTRFS_XATTR_ITEM_KEY;
3554 max_key.type = (u8)-1;
3555 max_key.offset = (u64)-1;
3557 /* Only run delayed items if we are a dir or a new file */
3558 if (S_ISDIR(inode->i_mode) ||
3559 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) {
3560 ret = btrfs_commit_inode_delayed_items(trans, inode);
3562 btrfs_free_path(path);
3563 btrfs_free_path(dst_path);
3568 mutex_lock(&BTRFS_I(inode)->log_mutex);
3570 btrfs_get_logged_extents(log, inode);
3573 * a brute force approach to making sure we get the most uptodate
3574 * copies of everything.
3576 if (S_ISDIR(inode->i_mode)) {
3577 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
3579 if (inode_only == LOG_INODE_EXISTS)
3580 max_key_type = BTRFS_XATTR_ITEM_KEY;
3581 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
3583 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3584 &BTRFS_I(inode)->runtime_flags)) {
3585 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3586 &BTRFS_I(inode)->runtime_flags);
3587 ret = btrfs_truncate_inode_items(trans, log,
3589 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
3590 &BTRFS_I(inode)->runtime_flags)) {
3591 if (inode_only == LOG_INODE_ALL)
3593 max_key.type = BTRFS_XATTR_ITEM_KEY;
3594 ret = drop_objectid_items(trans, log, path, ino,
3597 if (inode_only == LOG_INODE_ALL)
3599 ret = log_inode_item(trans, log, dst_path, inode);
3612 path->keep_locks = 1;
3616 ret = btrfs_search_forward(root, &min_key, &max_key,
3617 path, trans->transid);
3621 /* note, ins_nr might be > 0 here, cleanup outside the loop */
3622 if (min_key.objectid != ino)
3624 if (min_key.type > max_key.type)
3627 src = path->nodes[0];
3628 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
3631 } else if (!ins_nr) {
3632 ins_start_slot = path->slots[0];
3637 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
3638 ins_nr, inode_only);
3644 ins_start_slot = path->slots[0];
3647 nritems = btrfs_header_nritems(path->nodes[0]);
3649 if (path->slots[0] < nritems) {
3650 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
3655 ret = copy_items(trans, inode, dst_path, src,
3657 ins_nr, inode_only);
3664 btrfs_release_path(path);
3666 if (min_key.offset < (u64)-1)
3668 else if (min_key.type < (u8)-1)
3670 else if (min_key.objectid < (u64)-1)
3676 ret = copy_items(trans, inode, dst_path, src, ins_start_slot,
3677 ins_nr, inode_only);
3687 btrfs_release_path(dst_path);
3688 ret = btrfs_log_changed_extents(trans, root, inode, dst_path);
3694 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
3695 struct extent_map *em, *n;
3697 write_lock(&tree->lock);
3698 list_for_each_entry_safe(em, n, &tree->modified_extents, list)
3699 list_del_init(&em->list);
3700 write_unlock(&tree->lock);
3703 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
3704 btrfs_release_path(path);
3705 btrfs_release_path(dst_path);
3706 ret = log_directory_changes(trans, root, inode, path, dst_path);
3712 BTRFS_I(inode)->logged_trans = trans->transid;
3713 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
3716 btrfs_free_logged_extents(log, log->log_transid);
3717 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3719 btrfs_free_path(path);
3720 btrfs_free_path(dst_path);
3725 * follow the dentry parent pointers up the chain and see if any
3726 * of the directories in it require a full commit before they can
3727 * be logged. Returns zero if nothing special needs to be done or 1 if
3728 * a full commit is required.
3730 static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
3731 struct inode *inode,
3732 struct dentry *parent,
3733 struct super_block *sb,
3737 struct btrfs_root *root;
3738 struct dentry *old_parent = NULL;
3741 * for regular files, if its inode is already on disk, we don't
3742 * have to worry about the parents at all. This is because
3743 * we can use the last_unlink_trans field to record renames
3744 * and other fun in this file.
3746 if (S_ISREG(inode->i_mode) &&
3747 BTRFS_I(inode)->generation <= last_committed &&
3748 BTRFS_I(inode)->last_unlink_trans <= last_committed)
3751 if (!S_ISDIR(inode->i_mode)) {
3752 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3754 inode = parent->d_inode;
3758 BTRFS_I(inode)->logged_trans = trans->transid;
3761 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
3762 root = BTRFS_I(inode)->root;
3765 * make sure any commits to the log are forced
3766 * to be full commits
3768 root->fs_info->last_trans_log_full_commit =
3774 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3777 if (IS_ROOT(parent))
3780 parent = dget_parent(parent);
3782 old_parent = parent;
3783 inode = parent->d_inode;
3792 * helper function around btrfs_log_inode to make sure newly created
3793 * parent directories also end up in the log. A minimal inode and backref
3794 * only logging is done of any parent directories that are older than
3795 * the last committed transaction
3797 int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
3798 struct btrfs_root *root, struct inode *inode,
3799 struct dentry *parent, int exists_only)
3801 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
3802 struct super_block *sb;
3803 struct dentry *old_parent = NULL;
3805 u64 last_committed = root->fs_info->last_trans_committed;
3809 if (btrfs_test_opt(root, NOTREELOG)) {
3814 if (root->fs_info->last_trans_log_full_commit >
3815 root->fs_info->last_trans_committed) {
3820 if (root != BTRFS_I(inode)->root ||
3821 btrfs_root_refs(&root->root_item) == 0) {
3826 ret = check_parent_dirs_for_sync(trans, inode, parent,
3827 sb, last_committed);
3831 if (btrfs_inode_in_log(inode, trans->transid)) {
3832 ret = BTRFS_NO_LOG_SYNC;
3836 ret = start_log_trans(trans, root);
3840 ret = btrfs_log_inode(trans, root, inode, inode_only);
3845 * for regular files, if its inode is already on disk, we don't
3846 * have to worry about the parents at all. This is because
3847 * we can use the last_unlink_trans field to record renames
3848 * and other fun in this file.
3850 if (S_ISREG(inode->i_mode) &&
3851 BTRFS_I(inode)->generation <= last_committed &&
3852 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
3857 inode_only = LOG_INODE_EXISTS;
3859 if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
3862 inode = parent->d_inode;
3863 if (root != BTRFS_I(inode)->root)
3866 if (BTRFS_I(inode)->generation >
3867 root->fs_info->last_trans_committed) {
3868 ret = btrfs_log_inode(trans, root, inode, inode_only);
3872 if (IS_ROOT(parent))
3875 parent = dget_parent(parent);
3877 old_parent = parent;
3883 root->fs_info->last_trans_log_full_commit = trans->transid;
3886 btrfs_end_log_trans(root);
3892 * it is not safe to log dentry if the chunk root has added new
3893 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
3894 * If this returns 1, you must commit the transaction to safely get your
3897 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
3898 struct btrfs_root *root, struct dentry *dentry)
3900 struct dentry *parent = dget_parent(dentry);
3903 ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
3910 * should be called during mount to recover any replay any log trees
3913 int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
3916 struct btrfs_path *path;
3917 struct btrfs_trans_handle *trans;
3918 struct btrfs_key key;
3919 struct btrfs_key found_key;
3920 struct btrfs_key tmp_key;
3921 struct btrfs_root *log;
3922 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
3923 struct walk_control wc = {
3924 .process_func = process_one_buffer,
3928 path = btrfs_alloc_path();
3932 fs_info->log_root_recovering = 1;
3934 trans = btrfs_start_transaction(fs_info->tree_root, 0);
3935 if (IS_ERR(trans)) {
3936 ret = PTR_ERR(trans);
3943 ret = walk_log_tree(trans, log_root_tree, &wc);
3945 btrfs_error(fs_info, ret, "Failed to pin buffers while "
3946 "recovering log root tree.");
3951 key.objectid = BTRFS_TREE_LOG_OBJECTID;
3952 key.offset = (u64)-1;
3953 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3956 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
3959 btrfs_error(fs_info, ret,
3960 "Couldn't find tree log root.");
3964 if (path->slots[0] == 0)
3968 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3970 btrfs_release_path(path);
3971 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
3974 log = btrfs_read_fs_root_no_radix(log_root_tree,
3978 btrfs_error(fs_info, ret,
3979 "Couldn't read tree log root.");
3983 tmp_key.objectid = found_key.offset;
3984 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
3985 tmp_key.offset = (u64)-1;
3987 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
3988 if (IS_ERR(wc.replay_dest)) {
3989 ret = PTR_ERR(wc.replay_dest);
3990 free_extent_buffer(log->node);
3991 free_extent_buffer(log->commit_root);
3993 btrfs_error(fs_info, ret, "Couldn't read target root "
3994 "for tree log recovery.");
3998 wc.replay_dest->log_root = log;
3999 btrfs_record_root_in_trans(trans, wc.replay_dest);
4000 ret = walk_log_tree(trans, log, &wc);
4002 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
4003 ret = fixup_inode_link_counts(trans, wc.replay_dest,
4007 key.offset = found_key.offset - 1;
4008 wc.replay_dest->log_root = NULL;
4009 free_extent_buffer(log->node);
4010 free_extent_buffer(log->commit_root);
4016 if (found_key.offset == 0)
4019 btrfs_release_path(path);
4021 /* step one is to pin it all, step two is to replay just inodes */
4024 wc.process_func = replay_one_buffer;
4025 wc.stage = LOG_WALK_REPLAY_INODES;
4028 /* step three is to replay everything */
4029 if (wc.stage < LOG_WALK_REPLAY_ALL) {
4034 btrfs_free_path(path);
4036 /* step 4: commit the transaction, which also unpins the blocks */
4037 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
4041 free_extent_buffer(log_root_tree->node);
4042 log_root_tree->log_root = NULL;
4043 fs_info->log_root_recovering = 0;
4044 kfree(log_root_tree);
4049 btrfs_end_transaction(wc.trans, fs_info->tree_root);
4050 btrfs_free_path(path);
4055 * there are some corner cases where we want to force a full
4056 * commit instead of allowing a directory to be logged.
4058 * They revolve around files there were unlinked from the directory, and
4059 * this function updates the parent directory so that a full commit is
4060 * properly done if it is fsync'd later after the unlinks are done.
4062 void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
4063 struct inode *dir, struct inode *inode,
4067 * when we're logging a file, if it hasn't been renamed
4068 * or unlinked, and its inode is fully committed on disk,
4069 * we don't have to worry about walking up the directory chain
4070 * to log its parents.
4072 * So, we use the last_unlink_trans field to put this transid
4073 * into the file. When the file is logged we check it and
4074 * don't log the parents if the file is fully on disk.
4076 if (S_ISREG(inode->i_mode))
4077 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4080 * if this directory was already logged any new
4081 * names for this file/dir will get recorded
4084 if (BTRFS_I(dir)->logged_trans == trans->transid)
4088 * if the inode we're about to unlink was logged,
4089 * the log will be properly updated for any new names
4091 if (BTRFS_I(inode)->logged_trans == trans->transid)
4095 * when renaming files across directories, if the directory
4096 * there we're unlinking from gets fsync'd later on, there's
4097 * no way to find the destination directory later and fsync it
4098 * properly. So, we have to be conservative and force commits
4099 * so the new name gets discovered.
4104 /* we can safely do the unlink without any special recording */
4108 BTRFS_I(dir)->last_unlink_trans = trans->transid;
4112 * Call this after adding a new name for a file and it will properly
4113 * update the log to reflect the new name.
4115 * It will return zero if all goes well, and it will return 1 if a
4116 * full transaction commit is required.
4118 int btrfs_log_new_name(struct btrfs_trans_handle *trans,
4119 struct inode *inode, struct inode *old_dir,
4120 struct dentry *parent)
4122 struct btrfs_root * root = BTRFS_I(inode)->root;
4125 * this will force the logging code to walk the dentry chain
4128 if (S_ISREG(inode->i_mode))
4129 BTRFS_I(inode)->last_unlink_trans = trans->transid;
4132 * if this inode hasn't been logged and directory we're renaming it
4133 * from hasn't been logged, we don't need to log it
4135 if (BTRFS_I(inode)->logged_trans <=
4136 root->fs_info->last_trans_committed &&
4137 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
4138 root->fs_info->last_trans_committed))
4141 return btrfs_log_inode_parent(trans, root, inode, parent, 1);