1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2008 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/slab.h>
8 #include <linux/blkdev.h>
9 #include <linux/list_sort.h>
10 #include <linux/iversion.h>
16 #include "print-tree.h"
18 #include "compression.h"
20 #include "inode-map.h"
21 #include "block-group.h"
22 #include "space-info.h"
24 /* magic values for the inode_only field in btrfs_log_inode:
26 * LOG_INODE_ALL means to log everything
27 * LOG_INODE_EXISTS means to log just enough to recreate the inode
38 * directory trouble cases
40 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
41 * log, we must force a full commit before doing an fsync of the directory
42 * where the unlink was done.
43 * ---> record transid of last unlink/rename per directory
47 * rename foo/some_dir foo2/some_dir
49 * fsync foo/some_dir/some_file
51 * The fsync above will unlink the original some_dir without recording
52 * it in its new location (foo2). After a crash, some_dir will be gone
53 * unless the fsync of some_file forces a full commit
55 * 2) we must log any new names for any file or dir that is in the fsync
56 * log. ---> check inode while renaming/linking.
58 * 2a) we must log any new names for any file or dir during rename
59 * when the directory they are being removed from was logged.
60 * ---> check inode and old parent dir during rename
62 * 2a is actually the more important variant. With the extra logging
63 * a crash might unlink the old name without recreating the new one
65 * 3) after a crash, we must go through any directories with a link count
66 * of zero and redo the rm -rf
73 * The directory f1 was fully removed from the FS, but fsync was never
74 * called on f1, only its parent dir. After a crash the rm -rf must
75 * be replayed. This must be able to recurse down the entire
76 * directory tree. The inode link count fixup code takes care of the
81 * stages for the tree walking. The first
82 * stage (0) is to only pin down the blocks we find
83 * the second stage (1) is to make sure that all the inodes
84 * we find in the log are created in the subvolume.
86 * The last stage is to deal with directories and links and extents
87 * and all the other fun semantics
91 LOG_WALK_REPLAY_INODES,
92 LOG_WALK_REPLAY_DIR_INDEX,
96 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
97 struct btrfs_root *root, struct btrfs_inode *inode,
99 struct btrfs_log_ctx *ctx);
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,
139 struct btrfs_log_ctx *ctx)
141 struct btrfs_fs_info *fs_info = root->fs_info;
144 mutex_lock(&root->log_mutex);
146 if (root->log_root) {
147 if (btrfs_need_log_full_commit(trans)) {
152 if (!root->log_start_pid) {
153 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
154 root->log_start_pid = current->pid;
155 } else if (root->log_start_pid != current->pid) {
156 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
159 mutex_lock(&fs_info->tree_log_mutex);
160 if (!fs_info->log_root_tree)
161 ret = btrfs_init_log_root_tree(trans, fs_info);
162 mutex_unlock(&fs_info->tree_log_mutex);
166 ret = btrfs_add_log_tree(trans, root);
170 set_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
171 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
172 root->log_start_pid = current->pid;
175 atomic_inc(&root->log_writers);
176 if (ctx && !ctx->logging_new_name) {
177 int index = root->log_transid % 2;
178 list_add_tail(&ctx->list, &root->log_ctxs[index]);
179 ctx->log_transid = root->log_transid;
183 mutex_unlock(&root->log_mutex);
188 * returns 0 if there was a log transaction running and we were able
189 * to join, or returns -ENOENT if there were not transactions
192 static int join_running_log_trans(struct btrfs_root *root)
196 if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state))
199 mutex_lock(&root->log_mutex);
200 if (root->log_root) {
202 atomic_inc(&root->log_writers);
204 mutex_unlock(&root->log_mutex);
209 * This either makes the current running log transaction wait
210 * until you call btrfs_end_log_trans() or it makes any future
211 * log transactions wait until you call btrfs_end_log_trans()
213 void btrfs_pin_log_trans(struct btrfs_root *root)
215 atomic_inc(&root->log_writers);
219 * indicate we're done making changes to the log tree
220 * and wake up anyone waiting to do a sync
222 void btrfs_end_log_trans(struct btrfs_root *root)
224 if (atomic_dec_and_test(&root->log_writers)) {
225 /* atomic_dec_and_test implies a barrier */
226 cond_wake_up_nomb(&root->log_writer_wait);
230 static int btrfs_write_tree_block(struct extent_buffer *buf)
232 return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start,
233 buf->start + buf->len - 1);
236 static void btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
238 filemap_fdatawait_range(buf->pages[0]->mapping,
239 buf->start, buf->start + buf->len - 1);
243 * the walk control struct is used to pass state down the chain when
244 * processing the log tree. The stage field tells us which part
245 * of the log tree processing we are currently doing. The others
246 * are state fields used for that specific part
248 struct walk_control {
249 /* should we free the extent on disk when done? This is used
250 * at transaction commit time while freeing a log tree
254 /* should we write out the extent buffer? This is used
255 * while flushing the log tree to disk during a sync
259 /* should we wait for the extent buffer io to finish? Also used
260 * while flushing the log tree to disk for a sync
264 /* pin only walk, we record which extents on disk belong to the
269 /* what stage of the replay code we're currently in */
273 * Ignore any items from the inode currently being processed. Needs
274 * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in
275 * the LOG_WALK_REPLAY_INODES stage.
277 bool ignore_cur_inode;
279 /* the root we are currently replaying */
280 struct btrfs_root *replay_dest;
282 /* the trans handle for the current replay */
283 struct btrfs_trans_handle *trans;
285 /* the function that gets used to process blocks we find in the
286 * tree. Note the extent_buffer might not be up to date when it is
287 * passed in, and it must be checked or read if you need the data
290 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
291 struct walk_control *wc, u64 gen, int level);
295 * process_func used to pin down extents, write them or wait on them
297 static int process_one_buffer(struct btrfs_root *log,
298 struct extent_buffer *eb,
299 struct walk_control *wc, u64 gen, int level)
301 struct btrfs_fs_info *fs_info = log->fs_info;
305 * If this fs is mixed then we need to be able to process the leaves to
306 * pin down any logged extents, so we have to read the block.
308 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
309 ret = btrfs_read_buffer(eb, gen, level, NULL);
315 ret = btrfs_pin_extent_for_log_replay(wc->trans, eb->start,
318 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
319 if (wc->pin && btrfs_header_level(eb) == 0)
320 ret = btrfs_exclude_logged_extents(eb);
322 btrfs_write_tree_block(eb);
324 btrfs_wait_tree_block_writeback(eb);
330 * Item overwrite used by replay and tree logging. eb, slot and key all refer
331 * to the src data we are copying out.
333 * root is the tree we are copying into, and path is a scratch
334 * path for use in this function (it should be released on entry and
335 * will be released on exit).
337 * If the key is already in the destination tree the existing item is
338 * overwritten. If the existing item isn't big enough, it is extended.
339 * If it is too large, it is truncated.
341 * If the key isn't in the destination yet, a new item is inserted.
343 static noinline int overwrite_item(struct btrfs_trans_handle *trans,
344 struct btrfs_root *root,
345 struct btrfs_path *path,
346 struct extent_buffer *eb, int slot,
347 struct btrfs_key *key)
351 u64 saved_i_size = 0;
352 int save_old_i_size = 0;
353 unsigned long src_ptr;
354 unsigned long dst_ptr;
355 int overwrite_root = 0;
356 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
358 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
361 item_size = btrfs_item_size_nr(eb, slot);
362 src_ptr = btrfs_item_ptr_offset(eb, slot);
364 /* look for the key in the destination tree */
365 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
372 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
374 if (dst_size != item_size)
377 if (item_size == 0) {
378 btrfs_release_path(path);
381 dst_copy = kmalloc(item_size, GFP_NOFS);
382 src_copy = kmalloc(item_size, GFP_NOFS);
383 if (!dst_copy || !src_copy) {
384 btrfs_release_path(path);
390 read_extent_buffer(eb, src_copy, src_ptr, item_size);
392 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
393 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
395 ret = memcmp(dst_copy, src_copy, item_size);
400 * they have the same contents, just return, this saves
401 * us from cowing blocks in the destination tree and doing
402 * extra writes that may not have been done by a previous
406 btrfs_release_path(path);
411 * We need to load the old nbytes into the inode so when we
412 * replay the extents we've logged we get the right nbytes.
415 struct btrfs_inode_item *item;
419 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
420 struct btrfs_inode_item);
421 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
422 item = btrfs_item_ptr(eb, slot,
423 struct btrfs_inode_item);
424 btrfs_set_inode_nbytes(eb, item, nbytes);
427 * If this is a directory we need to reset the i_size to
428 * 0 so that we can set it up properly when replaying
429 * the rest of the items in this log.
431 mode = btrfs_inode_mode(eb, item);
433 btrfs_set_inode_size(eb, item, 0);
435 } else if (inode_item) {
436 struct btrfs_inode_item *item;
440 * New inode, set nbytes to 0 so that the nbytes comes out
441 * properly when we replay the extents.
443 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
444 btrfs_set_inode_nbytes(eb, item, 0);
447 * If this is a directory we need to reset the i_size to 0 so
448 * that we can set it up properly when replaying the rest of
449 * the items in this log.
451 mode = btrfs_inode_mode(eb, item);
453 btrfs_set_inode_size(eb, item, 0);
456 btrfs_release_path(path);
457 /* try to insert the key into the destination tree */
458 path->skip_release_on_error = 1;
459 ret = btrfs_insert_empty_item(trans, root, path,
461 path->skip_release_on_error = 0;
463 /* make sure any existing item is the correct size */
464 if (ret == -EEXIST || ret == -EOVERFLOW) {
466 found_size = btrfs_item_size_nr(path->nodes[0],
468 if (found_size > item_size)
469 btrfs_truncate_item(path, item_size, 1);
470 else if (found_size < item_size)
471 btrfs_extend_item(path, item_size - found_size);
475 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
478 /* don't overwrite an existing inode if the generation number
479 * was logged as zero. This is done when the tree logging code
480 * is just logging an inode to make sure it exists after recovery.
482 * Also, don't overwrite i_size on directories during replay.
483 * log replay inserts and removes directory items based on the
484 * state of the tree found in the subvolume, and i_size is modified
487 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
488 struct btrfs_inode_item *src_item;
489 struct btrfs_inode_item *dst_item;
491 src_item = (struct btrfs_inode_item *)src_ptr;
492 dst_item = (struct btrfs_inode_item *)dst_ptr;
494 if (btrfs_inode_generation(eb, src_item) == 0) {
495 struct extent_buffer *dst_eb = path->nodes[0];
496 const u64 ino_size = btrfs_inode_size(eb, src_item);
499 * For regular files an ino_size == 0 is used only when
500 * logging that an inode exists, as part of a directory
501 * fsync, and the inode wasn't fsynced before. In this
502 * case don't set the size of the inode in the fs/subvol
503 * tree, otherwise we would be throwing valid data away.
505 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
506 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
508 btrfs_set_inode_size(dst_eb, dst_item, ino_size);
512 if (overwrite_root &&
513 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
514 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
516 saved_i_size = btrfs_inode_size(path->nodes[0],
521 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
524 if (save_old_i_size) {
525 struct btrfs_inode_item *dst_item;
526 dst_item = (struct btrfs_inode_item *)dst_ptr;
527 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
530 /* make sure the generation is filled in */
531 if (key->type == BTRFS_INODE_ITEM_KEY) {
532 struct btrfs_inode_item *dst_item;
533 dst_item = (struct btrfs_inode_item *)dst_ptr;
534 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
535 btrfs_set_inode_generation(path->nodes[0], dst_item,
540 btrfs_mark_buffer_dirty(path->nodes[0]);
541 btrfs_release_path(path);
546 * simple helper to read an inode off the disk from a given root
547 * This can only be called for subvolume roots and not for the log
549 static noinline struct inode *read_one_inode(struct btrfs_root *root,
554 inode = btrfs_iget(root->fs_info->sb, objectid, root);
560 /* replays a single extent in 'eb' at 'slot' with 'key' into the
561 * subvolume 'root'. path is released on entry and should be released
564 * extents in the log tree have not been allocated out of the extent
565 * tree yet. So, this completes the allocation, taking a reference
566 * as required if the extent already exists or creating a new extent
567 * if it isn't in the extent allocation tree yet.
569 * The extent is inserted into the file, dropping any existing extents
570 * from the file that overlap the new one.
572 static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
573 struct btrfs_root *root,
574 struct btrfs_path *path,
575 struct extent_buffer *eb, int slot,
576 struct btrfs_key *key)
578 struct btrfs_drop_extents_args drop_args = { 0 };
579 struct btrfs_fs_info *fs_info = root->fs_info;
582 u64 start = key->offset;
584 struct btrfs_file_extent_item *item;
585 struct inode *inode = NULL;
589 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
590 found_type = btrfs_file_extent_type(eb, item);
592 if (found_type == BTRFS_FILE_EXTENT_REG ||
593 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
594 nbytes = btrfs_file_extent_num_bytes(eb, item);
595 extent_end = start + nbytes;
598 * We don't add to the inodes nbytes if we are prealloc or a
601 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
603 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
604 size = btrfs_file_extent_ram_bytes(eb, item);
605 nbytes = btrfs_file_extent_ram_bytes(eb, item);
606 extent_end = ALIGN(start + size,
607 fs_info->sectorsize);
613 inode = read_one_inode(root, key->objectid);
620 * first check to see if we already have this extent in the
621 * file. This must be done before the btrfs_drop_extents run
622 * so we don't try to drop this extent.
624 ret = btrfs_lookup_file_extent(trans, root, path,
625 btrfs_ino(BTRFS_I(inode)), start, 0);
628 (found_type == BTRFS_FILE_EXTENT_REG ||
629 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
630 struct btrfs_file_extent_item cmp1;
631 struct btrfs_file_extent_item cmp2;
632 struct btrfs_file_extent_item *existing;
633 struct extent_buffer *leaf;
635 leaf = path->nodes[0];
636 existing = btrfs_item_ptr(leaf, path->slots[0],
637 struct btrfs_file_extent_item);
639 read_extent_buffer(eb, &cmp1, (unsigned long)item,
641 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
645 * we already have a pointer to this exact extent,
646 * we don't have to do anything
648 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
649 btrfs_release_path(path);
653 btrfs_release_path(path);
655 /* drop any overlapping extents */
656 drop_args.start = start;
657 drop_args.end = extent_end;
658 drop_args.drop_cache = true;
659 ret = btrfs_drop_extents(trans, root, BTRFS_I(inode), &drop_args);
663 if (found_type == BTRFS_FILE_EXTENT_REG ||
664 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
666 unsigned long dest_offset;
667 struct btrfs_key ins;
669 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
670 btrfs_fs_incompat(fs_info, NO_HOLES))
673 ret = btrfs_insert_empty_item(trans, root, path, key,
677 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
679 copy_extent_buffer(path->nodes[0], eb, dest_offset,
680 (unsigned long)item, sizeof(*item));
682 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
683 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
684 ins.type = BTRFS_EXTENT_ITEM_KEY;
685 offset = key->offset - btrfs_file_extent_offset(eb, item);
688 * Manually record dirty extent, as here we did a shallow
689 * file extent item copy and skip normal backref update,
690 * but modifying extent tree all by ourselves.
691 * So need to manually record dirty extent for qgroup,
692 * as the owner of the file extent changed from log tree
693 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
695 ret = btrfs_qgroup_trace_extent(trans,
696 btrfs_file_extent_disk_bytenr(eb, item),
697 btrfs_file_extent_disk_num_bytes(eb, item),
702 if (ins.objectid > 0) {
703 struct btrfs_ref ref = { 0 };
706 LIST_HEAD(ordered_sums);
709 * is this extent already allocated in the extent
710 * allocation tree? If so, just add a reference
712 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
715 btrfs_init_generic_ref(&ref,
716 BTRFS_ADD_DELAYED_REF,
717 ins.objectid, ins.offset, 0);
718 btrfs_init_data_ref(&ref,
719 root->root_key.objectid,
720 key->objectid, offset);
721 ret = btrfs_inc_extent_ref(trans, &ref);
726 * insert the extent pointer in the extent
729 ret = btrfs_alloc_logged_file_extent(trans,
730 root->root_key.objectid,
731 key->objectid, offset, &ins);
735 btrfs_release_path(path);
737 if (btrfs_file_extent_compression(eb, item)) {
738 csum_start = ins.objectid;
739 csum_end = csum_start + ins.offset;
741 csum_start = ins.objectid +
742 btrfs_file_extent_offset(eb, item);
743 csum_end = csum_start +
744 btrfs_file_extent_num_bytes(eb, item);
747 ret = btrfs_lookup_csums_range(root->log_root,
748 csum_start, csum_end - 1,
753 * Now delete all existing cums in the csum root that
754 * cover our range. We do this because we can have an
755 * extent that is completely referenced by one file
756 * extent item and partially referenced by another
757 * file extent item (like after using the clone or
758 * extent_same ioctls). In this case if we end up doing
759 * the replay of the one that partially references the
760 * extent first, and we do not do the csum deletion
761 * below, we can get 2 csum items in the csum tree that
762 * overlap each other. For example, imagine our log has
763 * the two following file extent items:
765 * key (257 EXTENT_DATA 409600)
766 * extent data disk byte 12845056 nr 102400
767 * extent data offset 20480 nr 20480 ram 102400
769 * key (257 EXTENT_DATA 819200)
770 * extent data disk byte 12845056 nr 102400
771 * extent data offset 0 nr 102400 ram 102400
773 * Where the second one fully references the 100K extent
774 * that starts at disk byte 12845056, and the log tree
775 * has a single csum item that covers the entire range
778 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
780 * After the first file extent item is replayed, the
781 * csum tree gets the following csum item:
783 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
785 * Which covers the 20K sub-range starting at offset 20K
786 * of our extent. Now when we replay the second file
787 * extent item, if we do not delete existing csum items
788 * that cover any of its blocks, we end up getting two
789 * csum items in our csum tree that overlap each other:
791 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
792 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
794 * Which is a problem, because after this anyone trying
795 * to lookup up for the checksum of any block of our
796 * extent starting at an offset of 40K or higher, will
797 * end up looking at the second csum item only, which
798 * does not contain the checksum for any block starting
799 * at offset 40K or higher of our extent.
801 while (!list_empty(&ordered_sums)) {
802 struct btrfs_ordered_sum *sums;
803 sums = list_entry(ordered_sums.next,
804 struct btrfs_ordered_sum,
807 ret = btrfs_del_csums(trans,
812 ret = btrfs_csum_file_blocks(trans,
813 fs_info->csum_root, sums);
814 list_del(&sums->list);
820 btrfs_release_path(path);
822 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
823 /* inline extents are easy, we just overwrite them */
824 ret = overwrite_item(trans, root, path, eb, slot, key);
829 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start,
835 btrfs_update_inode_bytes(BTRFS_I(inode), nbytes, drop_args.bytes_found);
836 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
844 * when cleaning up conflicts between the directory names in the
845 * subvolume, directory names in the log and directory names in the
846 * inode back references, we may have to unlink inodes from directories.
848 * This is a helper function to do the unlink of a specific directory
851 static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
852 struct btrfs_root *root,
853 struct btrfs_path *path,
854 struct btrfs_inode *dir,
855 struct btrfs_dir_item *di)
860 struct extent_buffer *leaf;
861 struct btrfs_key location;
864 leaf = path->nodes[0];
866 btrfs_dir_item_key_to_cpu(leaf, di, &location);
867 name_len = btrfs_dir_name_len(leaf, di);
868 name = kmalloc(name_len, GFP_NOFS);
872 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
873 btrfs_release_path(path);
875 inode = read_one_inode(root, location.objectid);
881 ret = link_to_fixup_dir(trans, root, path, location.objectid);
885 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
890 ret = btrfs_run_delayed_items(trans);
898 * helper function to see if a given name and sequence number found
899 * in an inode back reference are already in a directory and correctly
900 * point to this inode
902 static noinline int inode_in_dir(struct btrfs_root *root,
903 struct btrfs_path *path,
904 u64 dirid, u64 objectid, u64 index,
905 const char *name, int name_len)
907 struct btrfs_dir_item *di;
908 struct btrfs_key location;
911 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
912 index, name, name_len, 0);
913 if (di && !IS_ERR(di)) {
914 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
915 if (location.objectid != objectid)
919 btrfs_release_path(path);
921 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
922 if (di && !IS_ERR(di)) {
923 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
924 if (location.objectid != objectid)
930 btrfs_release_path(path);
935 * helper function to check a log tree for a named back reference in
936 * an inode. This is used to decide if a back reference that is
937 * found in the subvolume conflicts with what we find in the log.
939 * inode backreferences may have multiple refs in a single item,
940 * during replay we process one reference at a time, and we don't
941 * want to delete valid links to a file from the subvolume if that
942 * link is also in the log.
944 static noinline int backref_in_log(struct btrfs_root *log,
945 struct btrfs_key *key,
947 const char *name, int namelen)
949 struct btrfs_path *path;
952 path = btrfs_alloc_path();
956 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
959 } else if (ret == 1) {
964 if (key->type == BTRFS_INODE_EXTREF_KEY)
965 ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
970 ret = !!btrfs_find_name_in_backref(path->nodes[0],
974 btrfs_free_path(path);
978 static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
979 struct btrfs_root *root,
980 struct btrfs_path *path,
981 struct btrfs_root *log_root,
982 struct btrfs_inode *dir,
983 struct btrfs_inode *inode,
984 u64 inode_objectid, u64 parent_objectid,
985 u64 ref_index, char *name, int namelen,
991 struct extent_buffer *leaf;
992 struct btrfs_dir_item *di;
993 struct btrfs_key search_key;
994 struct btrfs_inode_extref *extref;
997 /* Search old style refs */
998 search_key.objectid = inode_objectid;
999 search_key.type = BTRFS_INODE_REF_KEY;
1000 search_key.offset = parent_objectid;
1001 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
1003 struct btrfs_inode_ref *victim_ref;
1005 unsigned long ptr_end;
1007 leaf = path->nodes[0];
1009 /* are we trying to overwrite a back ref for the root directory
1010 * if so, just jump out, we're done
1012 if (search_key.objectid == search_key.offset)
1015 /* check all the names in this back reference to see
1016 * if they are in the log. if so, we allow them to stay
1017 * otherwise they must be unlinked as a conflict
1019 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1020 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1021 while (ptr < ptr_end) {
1022 victim_ref = (struct btrfs_inode_ref *)ptr;
1023 victim_name_len = btrfs_inode_ref_name_len(leaf,
1025 victim_name = kmalloc(victim_name_len, GFP_NOFS);
1029 read_extent_buffer(leaf, victim_name,
1030 (unsigned long)(victim_ref + 1),
1033 ret = backref_in_log(log_root, &search_key,
1034 parent_objectid, victim_name,
1040 inc_nlink(&inode->vfs_inode);
1041 btrfs_release_path(path);
1043 ret = btrfs_unlink_inode(trans, root, dir, inode,
1044 victim_name, victim_name_len);
1048 ret = btrfs_run_delayed_items(trans);
1056 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1060 * NOTE: we have searched root tree and checked the
1061 * corresponding ref, it does not need to check again.
1065 btrfs_release_path(path);
1067 /* Same search but for extended refs */
1068 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1069 inode_objectid, parent_objectid, 0,
1071 if (!IS_ERR_OR_NULL(extref)) {
1075 struct inode *victim_parent;
1077 leaf = path->nodes[0];
1079 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1080 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1082 while (cur_offset < item_size) {
1083 extref = (struct btrfs_inode_extref *)(base + cur_offset);
1085 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1087 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1090 victim_name = kmalloc(victim_name_len, GFP_NOFS);
1093 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1096 search_key.objectid = inode_objectid;
1097 search_key.type = BTRFS_INODE_EXTREF_KEY;
1098 search_key.offset = btrfs_extref_hash(parent_objectid,
1101 ret = backref_in_log(log_root, &search_key,
1102 parent_objectid, victim_name,
1108 victim_parent = read_one_inode(root,
1110 if (victim_parent) {
1111 inc_nlink(&inode->vfs_inode);
1112 btrfs_release_path(path);
1114 ret = btrfs_unlink_inode(trans, root,
1115 BTRFS_I(victim_parent),
1120 ret = btrfs_run_delayed_items(
1123 iput(victim_parent);
1132 cur_offset += victim_name_len + sizeof(*extref);
1136 btrfs_release_path(path);
1138 /* look for a conflicting sequence number */
1139 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
1140 ref_index, name, namelen, 0);
1141 if (di && !IS_ERR(di)) {
1142 ret = drop_one_dir_item(trans, root, path, dir, di);
1146 btrfs_release_path(path);
1148 /* look for a conflicting name */
1149 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1151 if (di && !IS_ERR(di)) {
1152 ret = drop_one_dir_item(trans, root, path, dir, di);
1156 btrfs_release_path(path);
1161 static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1162 u32 *namelen, char **name, u64 *index,
1163 u64 *parent_objectid)
1165 struct btrfs_inode_extref *extref;
1167 extref = (struct btrfs_inode_extref *)ref_ptr;
1169 *namelen = btrfs_inode_extref_name_len(eb, extref);
1170 *name = kmalloc(*namelen, GFP_NOFS);
1174 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1178 *index = btrfs_inode_extref_index(eb, extref);
1179 if (parent_objectid)
1180 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1185 static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1186 u32 *namelen, char **name, u64 *index)
1188 struct btrfs_inode_ref *ref;
1190 ref = (struct btrfs_inode_ref *)ref_ptr;
1192 *namelen = btrfs_inode_ref_name_len(eb, ref);
1193 *name = kmalloc(*namelen, GFP_NOFS);
1197 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1200 *index = btrfs_inode_ref_index(eb, ref);
1206 * Take an inode reference item from the log tree and iterate all names from the
1207 * inode reference item in the subvolume tree with the same key (if it exists).
1208 * For any name that is not in the inode reference item from the log tree, do a
1209 * proper unlink of that name (that is, remove its entry from the inode
1210 * reference item and both dir index keys).
1212 static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1213 struct btrfs_root *root,
1214 struct btrfs_path *path,
1215 struct btrfs_inode *inode,
1216 struct extent_buffer *log_eb,
1218 struct btrfs_key *key)
1221 unsigned long ref_ptr;
1222 unsigned long ref_end;
1223 struct extent_buffer *eb;
1226 btrfs_release_path(path);
1227 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1235 eb = path->nodes[0];
1236 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1237 ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1238 while (ref_ptr < ref_end) {
1243 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1244 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1247 parent_id = key->offset;
1248 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1254 if (key->type == BTRFS_INODE_EXTREF_KEY)
1255 ret = !!btrfs_find_name_in_ext_backref(log_eb, log_slot,
1259 ret = !!btrfs_find_name_in_backref(log_eb, log_slot,
1265 btrfs_release_path(path);
1266 dir = read_one_inode(root, parent_id);
1272 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
1273 inode, name, namelen);
1283 if (key->type == BTRFS_INODE_EXTREF_KEY)
1284 ref_ptr += sizeof(struct btrfs_inode_extref);
1286 ref_ptr += sizeof(struct btrfs_inode_ref);
1290 btrfs_release_path(path);
1294 static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
1295 const u8 ref_type, const char *name,
1298 struct btrfs_key key;
1299 struct btrfs_path *path;
1300 const u64 parent_id = btrfs_ino(BTRFS_I(dir));
1303 path = btrfs_alloc_path();
1307 key.objectid = btrfs_ino(BTRFS_I(inode));
1308 key.type = ref_type;
1309 if (key.type == BTRFS_INODE_REF_KEY)
1310 key.offset = parent_id;
1312 key.offset = btrfs_extref_hash(parent_id, name, namelen);
1314 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
1321 if (key.type == BTRFS_INODE_EXTREF_KEY)
1322 ret = !!btrfs_find_name_in_ext_backref(path->nodes[0],
1323 path->slots[0], parent_id, name, namelen);
1325 ret = !!btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
1329 btrfs_free_path(path);
1333 static int add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1334 struct inode *dir, struct inode *inode, const char *name,
1335 int namelen, u64 ref_index)
1337 struct btrfs_dir_item *dir_item;
1338 struct btrfs_key key;
1339 struct btrfs_path *path;
1340 struct inode *other_inode = NULL;
1343 path = btrfs_alloc_path();
1347 dir_item = btrfs_lookup_dir_item(NULL, root, path,
1348 btrfs_ino(BTRFS_I(dir)),
1351 btrfs_release_path(path);
1353 } else if (IS_ERR(dir_item)) {
1354 ret = PTR_ERR(dir_item);
1359 * Our inode's dentry collides with the dentry of another inode which is
1360 * in the log but not yet processed since it has a higher inode number.
1361 * So delete that other dentry.
1363 btrfs_dir_item_key_to_cpu(path->nodes[0], dir_item, &key);
1364 btrfs_release_path(path);
1365 other_inode = read_one_inode(root, key.objectid);
1370 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir), BTRFS_I(other_inode),
1375 * If we dropped the link count to 0, bump it so that later the iput()
1376 * on the inode will not free it. We will fixup the link count later.
1378 if (other_inode->i_nlink == 0)
1379 inc_nlink(other_inode);
1381 ret = btrfs_run_delayed_items(trans);
1385 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
1386 name, namelen, 0, ref_index);
1389 btrfs_free_path(path);
1395 * replay one inode back reference item found in the log tree.
1396 * eb, slot and key refer to the buffer and key found in the log tree.
1397 * root is the destination we are replaying into, and path is for temp
1398 * use by this function. (it should be released on return).
1400 static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1401 struct btrfs_root *root,
1402 struct btrfs_root *log,
1403 struct btrfs_path *path,
1404 struct extent_buffer *eb, int slot,
1405 struct btrfs_key *key)
1407 struct inode *dir = NULL;
1408 struct inode *inode = NULL;
1409 unsigned long ref_ptr;
1410 unsigned long ref_end;
1414 int search_done = 0;
1415 int log_ref_ver = 0;
1416 u64 parent_objectid;
1419 int ref_struct_size;
1421 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1422 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1424 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1425 struct btrfs_inode_extref *r;
1427 ref_struct_size = sizeof(struct btrfs_inode_extref);
1429 r = (struct btrfs_inode_extref *)ref_ptr;
1430 parent_objectid = btrfs_inode_extref_parent(eb, r);
1432 ref_struct_size = sizeof(struct btrfs_inode_ref);
1433 parent_objectid = key->offset;
1435 inode_objectid = key->objectid;
1438 * it is possible that we didn't log all the parent directories
1439 * for a given inode. If we don't find the dir, just don't
1440 * copy the back ref in. The link count fixup code will take
1443 dir = read_one_inode(root, parent_objectid);
1449 inode = read_one_inode(root, inode_objectid);
1455 while (ref_ptr < ref_end) {
1457 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1458 &ref_index, &parent_objectid);
1460 * parent object can change from one array
1464 dir = read_one_inode(root, parent_objectid);
1470 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1476 /* if we already have a perfect match, we're done */
1477 if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1478 btrfs_ino(BTRFS_I(inode)), ref_index,
1481 * look for a conflicting back reference in the
1482 * metadata. if we find one we have to unlink that name
1483 * of the file before we add our new link. Later on, we
1484 * overwrite any existing back reference, and we don't
1485 * want to create dangling pointers in the directory.
1489 ret = __add_inode_ref(trans, root, path, log,
1494 ref_index, name, namelen,
1504 * If a reference item already exists for this inode
1505 * with the same parent and name, but different index,
1506 * drop it and the corresponding directory index entries
1507 * from the parent before adding the new reference item
1508 * and dir index entries, otherwise we would fail with
1509 * -EEXIST returned from btrfs_add_link() below.
1511 ret = btrfs_inode_ref_exists(inode, dir, key->type,
1514 ret = btrfs_unlink_inode(trans, root,
1519 * If we dropped the link count to 0, bump it so
1520 * that later the iput() on the inode will not
1521 * free it. We will fixup the link count later.
1523 if (!ret && inode->i_nlink == 0)
1529 /* insert our name */
1530 ret = add_link(trans, root, dir, inode, name, namelen,
1535 btrfs_update_inode(trans, root, BTRFS_I(inode));
1538 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
1548 * Before we overwrite the inode reference item in the subvolume tree
1549 * with the item from the log tree, we must unlink all names from the
1550 * parent directory that are in the subvolume's tree inode reference
1551 * item, otherwise we end up with an inconsistent subvolume tree where
1552 * dir index entries exist for a name but there is no inode reference
1553 * item with the same name.
1555 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1560 /* finally write the back reference in the inode */
1561 ret = overwrite_item(trans, root, path, eb, slot, key);
1563 btrfs_release_path(path);
1570 static int count_inode_extrefs(struct btrfs_root *root,
1571 struct btrfs_inode *inode, struct btrfs_path *path)
1575 unsigned int nlink = 0;
1578 u64 inode_objectid = btrfs_ino(inode);
1581 struct btrfs_inode_extref *extref;
1582 struct extent_buffer *leaf;
1585 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1590 leaf = path->nodes[0];
1591 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1592 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1595 while (cur_offset < item_size) {
1596 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1597 name_len = btrfs_inode_extref_name_len(leaf, extref);
1601 cur_offset += name_len + sizeof(*extref);
1605 btrfs_release_path(path);
1607 btrfs_release_path(path);
1609 if (ret < 0 && ret != -ENOENT)
1614 static int count_inode_refs(struct btrfs_root *root,
1615 struct btrfs_inode *inode, struct btrfs_path *path)
1618 struct btrfs_key key;
1619 unsigned int nlink = 0;
1621 unsigned long ptr_end;
1623 u64 ino = btrfs_ino(inode);
1626 key.type = BTRFS_INODE_REF_KEY;
1627 key.offset = (u64)-1;
1630 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1634 if (path->slots[0] == 0)
1639 btrfs_item_key_to_cpu(path->nodes[0], &key,
1641 if (key.objectid != ino ||
1642 key.type != BTRFS_INODE_REF_KEY)
1644 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1645 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1647 while (ptr < ptr_end) {
1648 struct btrfs_inode_ref *ref;
1650 ref = (struct btrfs_inode_ref *)ptr;
1651 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1653 ptr = (unsigned long)(ref + 1) + name_len;
1657 if (key.offset == 0)
1659 if (path->slots[0] > 0) {
1664 btrfs_release_path(path);
1666 btrfs_release_path(path);
1672 * There are a few corners where the link count of the file can't
1673 * be properly maintained during replay. So, instead of adding
1674 * lots of complexity to the log code, we just scan the backrefs
1675 * for any file that has been through replay.
1677 * The scan will update the link count on the inode to reflect the
1678 * number of back refs found. If it goes down to zero, the iput
1679 * will free the inode.
1681 static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1682 struct btrfs_root *root,
1683 struct inode *inode)
1685 struct btrfs_path *path;
1688 u64 ino = btrfs_ino(BTRFS_I(inode));
1690 path = btrfs_alloc_path();
1694 ret = count_inode_refs(root, BTRFS_I(inode), path);
1700 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
1708 if (nlink != inode->i_nlink) {
1709 set_nlink(inode, nlink);
1710 btrfs_update_inode(trans, root, BTRFS_I(inode));
1712 BTRFS_I(inode)->index_cnt = (u64)-1;
1714 if (inode->i_nlink == 0) {
1715 if (S_ISDIR(inode->i_mode)) {
1716 ret = replay_dir_deletes(trans, root, NULL, path,
1721 ret = btrfs_insert_orphan_item(trans, root, ino);
1727 btrfs_free_path(path);
1731 static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1732 struct btrfs_root *root,
1733 struct btrfs_path *path)
1736 struct btrfs_key key;
1737 struct inode *inode;
1739 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1740 key.type = BTRFS_ORPHAN_ITEM_KEY;
1741 key.offset = (u64)-1;
1743 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1748 if (path->slots[0] == 0)
1753 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1754 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1755 key.type != BTRFS_ORPHAN_ITEM_KEY)
1758 ret = btrfs_del_item(trans, root, path);
1762 btrfs_release_path(path);
1763 inode = read_one_inode(root, key.offset);
1767 ret = fixup_inode_link_count(trans, root, inode);
1773 * fixup on a directory may create new entries,
1774 * make sure we always look for the highset possible
1777 key.offset = (u64)-1;
1781 btrfs_release_path(path);
1787 * record a given inode in the fixup dir so we can check its link
1788 * count when replay is done. The link count is incremented here
1789 * so the inode won't go away until we check it
1791 static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1792 struct btrfs_root *root,
1793 struct btrfs_path *path,
1796 struct btrfs_key key;
1798 struct inode *inode;
1800 inode = read_one_inode(root, objectid);
1804 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1805 key.type = BTRFS_ORPHAN_ITEM_KEY;
1806 key.offset = objectid;
1808 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1810 btrfs_release_path(path);
1812 if (!inode->i_nlink)
1813 set_nlink(inode, 1);
1816 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
1817 } else if (ret == -EEXIST) {
1820 BUG(); /* Logic Error */
1828 * when replaying the log for a directory, we only insert names
1829 * for inodes that actually exist. This means an fsync on a directory
1830 * does not implicitly fsync all the new files in it
1832 static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1833 struct btrfs_root *root,
1834 u64 dirid, u64 index,
1835 char *name, int name_len,
1836 struct btrfs_key *location)
1838 struct inode *inode;
1842 inode = read_one_inode(root, location->objectid);
1846 dir = read_one_inode(root, dirid);
1852 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1853 name_len, 1, index);
1855 /* FIXME, put inode into FIXUP list */
1863 * take a single entry in a log directory item and replay it into
1866 * if a conflicting item exists in the subdirectory already,
1867 * the inode it points to is unlinked and put into the link count
1870 * If a name from the log points to a file or directory that does
1871 * not exist in the FS, it is skipped. fsyncs on directories
1872 * do not force down inodes inside that directory, just changes to the
1873 * names or unlinks in a directory.
1875 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1876 * non-existing inode) and 1 if the name was replayed.
1878 static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1879 struct btrfs_root *root,
1880 struct btrfs_path *path,
1881 struct extent_buffer *eb,
1882 struct btrfs_dir_item *di,
1883 struct btrfs_key *key)
1887 struct btrfs_dir_item *dst_di;
1888 struct btrfs_key found_key;
1889 struct btrfs_key log_key;
1894 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
1895 bool name_added = false;
1897 dir = read_one_inode(root, key->objectid);
1901 name_len = btrfs_dir_name_len(eb, di);
1902 name = kmalloc(name_len, GFP_NOFS);
1908 log_type = btrfs_dir_type(eb, di);
1909 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1912 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
1913 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1918 btrfs_release_path(path);
1920 if (key->type == BTRFS_DIR_ITEM_KEY) {
1921 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1923 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
1924 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1933 if (IS_ERR_OR_NULL(dst_di)) {
1934 /* we need a sequence number to insert, so we only
1935 * do inserts for the BTRFS_DIR_INDEX_KEY types
1937 if (key->type != BTRFS_DIR_INDEX_KEY)
1942 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1943 /* the existing item matches the logged item */
1944 if (found_key.objectid == log_key.objectid &&
1945 found_key.type == log_key.type &&
1946 found_key.offset == log_key.offset &&
1947 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1948 update_size = false;
1953 * don't drop the conflicting directory entry if the inode
1954 * for the new entry doesn't exist
1959 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
1963 if (key->type == BTRFS_DIR_INDEX_KEY)
1966 btrfs_release_path(path);
1967 if (!ret && update_size) {
1968 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
1969 ret = btrfs_update_inode(trans, root, BTRFS_I(dir));
1973 if (!ret && name_added)
1979 * Check if the inode reference exists in the log for the given name,
1980 * inode and parent inode
1982 found_key.objectid = log_key.objectid;
1983 found_key.type = BTRFS_INODE_REF_KEY;
1984 found_key.offset = key->objectid;
1985 ret = backref_in_log(root->log_root, &found_key, 0, name, name_len);
1989 /* The dentry will be added later. */
1991 update_size = false;
1995 found_key.objectid = log_key.objectid;
1996 found_key.type = BTRFS_INODE_EXTREF_KEY;
1997 found_key.offset = key->objectid;
1998 ret = backref_in_log(root->log_root, &found_key, key->objectid, name,
2003 /* The dentry will be added later. */
2005 update_size = false;
2008 btrfs_release_path(path);
2009 ret = insert_one_name(trans, root, key->objectid, key->offset,
2010 name, name_len, &log_key);
2011 if (ret && ret != -ENOENT && ret != -EEXIST)
2015 update_size = false;
2021 * find all the names in a directory item and reconcile them into
2022 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
2023 * one name in a directory item, but the same code gets used for
2024 * both directory index types
2026 static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
2027 struct btrfs_root *root,
2028 struct btrfs_path *path,
2029 struct extent_buffer *eb, int slot,
2030 struct btrfs_key *key)
2033 u32 item_size = btrfs_item_size_nr(eb, slot);
2034 struct btrfs_dir_item *di;
2037 unsigned long ptr_end;
2038 struct btrfs_path *fixup_path = NULL;
2040 ptr = btrfs_item_ptr_offset(eb, slot);
2041 ptr_end = ptr + item_size;
2042 while (ptr < ptr_end) {
2043 di = (struct btrfs_dir_item *)ptr;
2044 name_len = btrfs_dir_name_len(eb, di);
2045 ret = replay_one_name(trans, root, path, eb, di, key);
2048 ptr = (unsigned long)(di + 1);
2052 * If this entry refers to a non-directory (directories can not
2053 * have a link count > 1) and it was added in the transaction
2054 * that was not committed, make sure we fixup the link count of
2055 * the inode it the entry points to. Otherwise something like
2056 * the following would result in a directory pointing to an
2057 * inode with a wrong link that does not account for this dir
2065 * ln testdir/bar testdir/bar_link
2066 * ln testdir/foo testdir/foo_link
2067 * xfs_io -c "fsync" testdir/bar
2071 * mount fs, log replay happens
2073 * File foo would remain with a link count of 1 when it has two
2074 * entries pointing to it in the directory testdir. This would
2075 * make it impossible to ever delete the parent directory has
2076 * it would result in stale dentries that can never be deleted.
2078 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
2079 struct btrfs_key di_key;
2082 fixup_path = btrfs_alloc_path();
2089 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2090 ret = link_to_fixup_dir(trans, root, fixup_path,
2097 btrfs_free_path(fixup_path);
2102 * directory replay has two parts. There are the standard directory
2103 * items in the log copied from the subvolume, and range items
2104 * created in the log while the subvolume was logged.
2106 * The range items tell us which parts of the key space the log
2107 * is authoritative for. During replay, if a key in the subvolume
2108 * directory is in a logged range item, but not actually in the log
2109 * that means it was deleted from the directory before the fsync
2110 * and should be removed.
2112 static noinline int find_dir_range(struct btrfs_root *root,
2113 struct btrfs_path *path,
2114 u64 dirid, int key_type,
2115 u64 *start_ret, u64 *end_ret)
2117 struct btrfs_key key;
2119 struct btrfs_dir_log_item *item;
2123 if (*start_ret == (u64)-1)
2126 key.objectid = dirid;
2127 key.type = key_type;
2128 key.offset = *start_ret;
2130 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2134 if (path->slots[0] == 0)
2139 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2141 if (key.type != key_type || key.objectid != dirid) {
2145 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2146 struct btrfs_dir_log_item);
2147 found_end = btrfs_dir_log_end(path->nodes[0], item);
2149 if (*start_ret >= key.offset && *start_ret <= found_end) {
2151 *start_ret = key.offset;
2152 *end_ret = found_end;
2157 /* check the next slot in the tree to see if it is a valid item */
2158 nritems = btrfs_header_nritems(path->nodes[0]);
2160 if (path->slots[0] >= nritems) {
2161 ret = btrfs_next_leaf(root, path);
2166 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2168 if (key.type != key_type || key.objectid != dirid) {
2172 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2173 struct btrfs_dir_log_item);
2174 found_end = btrfs_dir_log_end(path->nodes[0], item);
2175 *start_ret = key.offset;
2176 *end_ret = found_end;
2179 btrfs_release_path(path);
2184 * this looks for a given directory item in the log. If the directory
2185 * item is not in the log, the item is removed and the inode it points
2188 static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2189 struct btrfs_root *root,
2190 struct btrfs_root *log,
2191 struct btrfs_path *path,
2192 struct btrfs_path *log_path,
2194 struct btrfs_key *dir_key)
2197 struct extent_buffer *eb;
2200 struct btrfs_dir_item *di;
2201 struct btrfs_dir_item *log_di;
2204 unsigned long ptr_end;
2206 struct inode *inode;
2207 struct btrfs_key location;
2210 eb = path->nodes[0];
2211 slot = path->slots[0];
2212 item_size = btrfs_item_size_nr(eb, slot);
2213 ptr = btrfs_item_ptr_offset(eb, slot);
2214 ptr_end = ptr + item_size;
2215 while (ptr < ptr_end) {
2216 di = (struct btrfs_dir_item *)ptr;
2217 name_len = btrfs_dir_name_len(eb, di);
2218 name = kmalloc(name_len, GFP_NOFS);
2223 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2226 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
2227 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2230 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
2231 log_di = btrfs_lookup_dir_index_item(trans, log,
2237 if (!log_di || log_di == ERR_PTR(-ENOENT)) {
2238 btrfs_dir_item_key_to_cpu(eb, di, &location);
2239 btrfs_release_path(path);
2240 btrfs_release_path(log_path);
2241 inode = read_one_inode(root, location.objectid);
2247 ret = link_to_fixup_dir(trans, root,
2248 path, location.objectid);
2256 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2257 BTRFS_I(inode), name, name_len);
2259 ret = btrfs_run_delayed_items(trans);
2265 /* there might still be more names under this key
2266 * check and repeat if required
2268 ret = btrfs_search_slot(NULL, root, dir_key, path,
2274 } else if (IS_ERR(log_di)) {
2276 return PTR_ERR(log_di);
2278 btrfs_release_path(log_path);
2281 ptr = (unsigned long)(di + 1);
2286 btrfs_release_path(path);
2287 btrfs_release_path(log_path);
2291 static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2292 struct btrfs_root *root,
2293 struct btrfs_root *log,
2294 struct btrfs_path *path,
2297 struct btrfs_key search_key;
2298 struct btrfs_path *log_path;
2303 log_path = btrfs_alloc_path();
2307 search_key.objectid = ino;
2308 search_key.type = BTRFS_XATTR_ITEM_KEY;
2309 search_key.offset = 0;
2311 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2315 nritems = btrfs_header_nritems(path->nodes[0]);
2316 for (i = path->slots[0]; i < nritems; i++) {
2317 struct btrfs_key key;
2318 struct btrfs_dir_item *di;
2319 struct btrfs_dir_item *log_di;
2323 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2324 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2329 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2330 total_size = btrfs_item_size_nr(path->nodes[0], i);
2332 while (cur < total_size) {
2333 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2334 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2335 u32 this_len = sizeof(*di) + name_len + data_len;
2338 name = kmalloc(name_len, GFP_NOFS);
2343 read_extent_buffer(path->nodes[0], name,
2344 (unsigned long)(di + 1), name_len);
2346 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2348 btrfs_release_path(log_path);
2350 /* Doesn't exist in log tree, so delete it. */
2351 btrfs_release_path(path);
2352 di = btrfs_lookup_xattr(trans, root, path, ino,
2353 name, name_len, -1);
2360 ret = btrfs_delete_one_dir_name(trans, root,
2364 btrfs_release_path(path);
2369 if (IS_ERR(log_di)) {
2370 ret = PTR_ERR(log_di);
2374 di = (struct btrfs_dir_item *)((char *)di + this_len);
2377 ret = btrfs_next_leaf(root, path);
2383 btrfs_free_path(log_path);
2384 btrfs_release_path(path);
2390 * deletion replay happens before we copy any new directory items
2391 * out of the log or out of backreferences from inodes. It
2392 * scans the log to find ranges of keys that log is authoritative for,
2393 * and then scans the directory to find items in those ranges that are
2394 * not present in the log.
2396 * Anything we don't find in the log is unlinked and removed from the
2399 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2400 struct btrfs_root *root,
2401 struct btrfs_root *log,
2402 struct btrfs_path *path,
2403 u64 dirid, int del_all)
2407 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2409 struct btrfs_key dir_key;
2410 struct btrfs_key found_key;
2411 struct btrfs_path *log_path;
2414 dir_key.objectid = dirid;
2415 dir_key.type = BTRFS_DIR_ITEM_KEY;
2416 log_path = btrfs_alloc_path();
2420 dir = read_one_inode(root, dirid);
2421 /* it isn't an error if the inode isn't there, that can happen
2422 * because we replay the deletes before we copy in the inode item
2426 btrfs_free_path(log_path);
2434 range_end = (u64)-1;
2436 ret = find_dir_range(log, path, dirid, key_type,
2437 &range_start, &range_end);
2442 dir_key.offset = range_start;
2445 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2450 nritems = btrfs_header_nritems(path->nodes[0]);
2451 if (path->slots[0] >= nritems) {
2452 ret = btrfs_next_leaf(root, path);
2458 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2460 if (found_key.objectid != dirid ||
2461 found_key.type != dir_key.type)
2464 if (found_key.offset > range_end)
2467 ret = check_item_in_log(trans, root, log, path,
2472 if (found_key.offset == (u64)-1)
2474 dir_key.offset = found_key.offset + 1;
2476 btrfs_release_path(path);
2477 if (range_end == (u64)-1)
2479 range_start = range_end + 1;
2484 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2485 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2486 dir_key.type = BTRFS_DIR_INDEX_KEY;
2487 btrfs_release_path(path);
2491 btrfs_release_path(path);
2492 btrfs_free_path(log_path);
2498 * the process_func used to replay items from the log tree. This
2499 * gets called in two different stages. The first stage just looks
2500 * for inodes and makes sure they are all copied into the subvolume.
2502 * The second stage copies all the other item types from the log into
2503 * the subvolume. The two stage approach is slower, but gets rid of
2504 * lots of complexity around inodes referencing other inodes that exist
2505 * only in the log (references come from either directory items or inode
2508 static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2509 struct walk_control *wc, u64 gen, int level)
2512 struct btrfs_path *path;
2513 struct btrfs_root *root = wc->replay_dest;
2514 struct btrfs_key key;
2518 ret = btrfs_read_buffer(eb, gen, level, NULL);
2522 level = btrfs_header_level(eb);
2527 path = btrfs_alloc_path();
2531 nritems = btrfs_header_nritems(eb);
2532 for (i = 0; i < nritems; i++) {
2533 btrfs_item_key_to_cpu(eb, &key, i);
2535 /* inode keys are done during the first stage */
2536 if (key.type == BTRFS_INODE_ITEM_KEY &&
2537 wc->stage == LOG_WALK_REPLAY_INODES) {
2538 struct btrfs_inode_item *inode_item;
2541 inode_item = btrfs_item_ptr(eb, i,
2542 struct btrfs_inode_item);
2544 * If we have a tmpfile (O_TMPFILE) that got fsync'ed
2545 * and never got linked before the fsync, skip it, as
2546 * replaying it is pointless since it would be deleted
2547 * later. We skip logging tmpfiles, but it's always
2548 * possible we are replaying a log created with a kernel
2549 * that used to log tmpfiles.
2551 if (btrfs_inode_nlink(eb, inode_item) == 0) {
2552 wc->ignore_cur_inode = true;
2555 wc->ignore_cur_inode = false;
2557 ret = replay_xattr_deletes(wc->trans, root, log,
2558 path, key.objectid);
2561 mode = btrfs_inode_mode(eb, inode_item);
2562 if (S_ISDIR(mode)) {
2563 ret = replay_dir_deletes(wc->trans,
2564 root, log, path, key.objectid, 0);
2568 ret = overwrite_item(wc->trans, root, path,
2574 * Before replaying extents, truncate the inode to its
2575 * size. We need to do it now and not after log replay
2576 * because before an fsync we can have prealloc extents
2577 * added beyond the inode's i_size. If we did it after,
2578 * through orphan cleanup for example, we would drop
2579 * those prealloc extents just after replaying them.
2581 if (S_ISREG(mode)) {
2582 struct btrfs_drop_extents_args drop_args = { 0 };
2583 struct inode *inode;
2586 inode = read_one_inode(root, key.objectid);
2591 from = ALIGN(i_size_read(inode),
2592 root->fs_info->sectorsize);
2593 drop_args.start = from;
2594 drop_args.end = (u64)-1;
2595 drop_args.drop_cache = true;
2596 ret = btrfs_drop_extents(wc->trans, root,
2600 inode_sub_bytes(inode,
2601 drop_args.bytes_found);
2602 /* Update the inode's nbytes. */
2603 ret = btrfs_update_inode(wc->trans,
2604 root, BTRFS_I(inode));
2611 ret = link_to_fixup_dir(wc->trans, root,
2612 path, key.objectid);
2617 if (wc->ignore_cur_inode)
2620 if (key.type == BTRFS_DIR_INDEX_KEY &&
2621 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2622 ret = replay_one_dir_item(wc->trans, root, path,
2628 if (wc->stage < LOG_WALK_REPLAY_ALL)
2631 /* these keys are simply copied */
2632 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2633 ret = overwrite_item(wc->trans, root, path,
2637 } else if (key.type == BTRFS_INODE_REF_KEY ||
2638 key.type == BTRFS_INODE_EXTREF_KEY) {
2639 ret = add_inode_ref(wc->trans, root, log, path,
2641 if (ret && ret != -ENOENT)
2644 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2645 ret = replay_one_extent(wc->trans, root, path,
2649 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
2650 ret = replay_one_dir_item(wc->trans, root, path,
2656 btrfs_free_path(path);
2661 * Correctly adjust the reserved bytes occupied by a log tree extent buffer
2663 static void unaccount_log_buffer(struct btrfs_fs_info *fs_info, u64 start)
2665 struct btrfs_block_group *cache;
2667 cache = btrfs_lookup_block_group(fs_info, start);
2669 btrfs_err(fs_info, "unable to find block group for %llu", start);
2673 spin_lock(&cache->space_info->lock);
2674 spin_lock(&cache->lock);
2675 cache->reserved -= fs_info->nodesize;
2676 cache->space_info->bytes_reserved -= fs_info->nodesize;
2677 spin_unlock(&cache->lock);
2678 spin_unlock(&cache->space_info->lock);
2680 btrfs_put_block_group(cache);
2683 static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2684 struct btrfs_root *root,
2685 struct btrfs_path *path, int *level,
2686 struct walk_control *wc)
2688 struct btrfs_fs_info *fs_info = root->fs_info;
2691 struct extent_buffer *next;
2692 struct extent_buffer *cur;
2696 while (*level > 0) {
2697 struct btrfs_key first_key;
2699 cur = path->nodes[*level];
2701 WARN_ON(btrfs_header_level(cur) != *level);
2703 if (path->slots[*level] >=
2704 btrfs_header_nritems(cur))
2707 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2708 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2709 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
2710 blocksize = fs_info->nodesize;
2712 next = btrfs_find_create_tree_block(fs_info, bytenr,
2713 btrfs_header_owner(cur),
2716 return PTR_ERR(next);
2719 ret = wc->process_func(root, next, wc, ptr_gen,
2722 free_extent_buffer(next);
2726 path->slots[*level]++;
2728 ret = btrfs_read_buffer(next, ptr_gen,
2729 *level - 1, &first_key);
2731 free_extent_buffer(next);
2736 btrfs_tree_lock(next);
2737 btrfs_clean_tree_block(next);
2738 btrfs_wait_tree_block_writeback(next);
2739 btrfs_tree_unlock(next);
2740 ret = btrfs_pin_reserved_extent(trans,
2743 free_extent_buffer(next);
2747 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2748 clear_extent_buffer_dirty(next);
2749 unaccount_log_buffer(fs_info, bytenr);
2752 free_extent_buffer(next);
2755 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
2757 free_extent_buffer(next);
2761 if (path->nodes[*level-1])
2762 free_extent_buffer(path->nodes[*level-1]);
2763 path->nodes[*level-1] = next;
2764 *level = btrfs_header_level(next);
2765 path->slots[*level] = 0;
2768 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
2774 static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
2775 struct btrfs_root *root,
2776 struct btrfs_path *path, int *level,
2777 struct walk_control *wc)
2779 struct btrfs_fs_info *fs_info = root->fs_info;
2784 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2785 slot = path->slots[i];
2786 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
2789 WARN_ON(*level == 0);
2792 ret = wc->process_func(root, path->nodes[*level], wc,
2793 btrfs_header_generation(path->nodes[*level]),
2799 struct extent_buffer *next;
2801 next = path->nodes[*level];
2804 btrfs_tree_lock(next);
2805 btrfs_clean_tree_block(next);
2806 btrfs_wait_tree_block_writeback(next);
2807 btrfs_tree_unlock(next);
2808 ret = btrfs_pin_reserved_extent(trans,
2809 path->nodes[*level]->start,
2810 path->nodes[*level]->len);
2814 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2815 clear_extent_buffer_dirty(next);
2817 unaccount_log_buffer(fs_info,
2818 path->nodes[*level]->start);
2821 free_extent_buffer(path->nodes[*level]);
2822 path->nodes[*level] = NULL;
2830 * drop the reference count on the tree rooted at 'snap'. This traverses
2831 * the tree freeing any blocks that have a ref count of zero after being
2834 static int walk_log_tree(struct btrfs_trans_handle *trans,
2835 struct btrfs_root *log, struct walk_control *wc)
2837 struct btrfs_fs_info *fs_info = log->fs_info;
2841 struct btrfs_path *path;
2844 path = btrfs_alloc_path();
2848 level = btrfs_header_level(log->node);
2850 path->nodes[level] = log->node;
2851 atomic_inc(&log->node->refs);
2852 path->slots[level] = 0;
2855 wret = walk_down_log_tree(trans, log, path, &level, wc);
2863 wret = walk_up_log_tree(trans, log, path, &level, wc);
2872 /* was the root node processed? if not, catch it here */
2873 if (path->nodes[orig_level]) {
2874 ret = wc->process_func(log, path->nodes[orig_level], wc,
2875 btrfs_header_generation(path->nodes[orig_level]),
2880 struct extent_buffer *next;
2882 next = path->nodes[orig_level];
2885 btrfs_tree_lock(next);
2886 btrfs_clean_tree_block(next);
2887 btrfs_wait_tree_block_writeback(next);
2888 btrfs_tree_unlock(next);
2889 ret = btrfs_pin_reserved_extent(trans,
2890 next->start, next->len);
2894 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2895 clear_extent_buffer_dirty(next);
2896 unaccount_log_buffer(fs_info, next->start);
2902 btrfs_free_path(path);
2907 * helper function to update the item for a given subvolumes log root
2908 * in the tree of log roots
2910 static int update_log_root(struct btrfs_trans_handle *trans,
2911 struct btrfs_root *log,
2912 struct btrfs_root_item *root_item)
2914 struct btrfs_fs_info *fs_info = log->fs_info;
2917 if (log->log_transid == 1) {
2918 /* insert root item on the first sync */
2919 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
2920 &log->root_key, root_item);
2922 ret = btrfs_update_root(trans, fs_info->log_root_tree,
2923 &log->root_key, root_item);
2928 static void wait_log_commit(struct btrfs_root *root, int transid)
2931 int index = transid % 2;
2934 * we only allow two pending log transactions at a time,
2935 * so we know that if ours is more than 2 older than the
2936 * current transaction, we're done
2939 prepare_to_wait(&root->log_commit_wait[index],
2940 &wait, TASK_UNINTERRUPTIBLE);
2942 if (!(root->log_transid_committed < transid &&
2943 atomic_read(&root->log_commit[index])))
2946 mutex_unlock(&root->log_mutex);
2948 mutex_lock(&root->log_mutex);
2950 finish_wait(&root->log_commit_wait[index], &wait);
2953 static void wait_for_writer(struct btrfs_root *root)
2958 prepare_to_wait(&root->log_writer_wait, &wait,
2959 TASK_UNINTERRUPTIBLE);
2960 if (!atomic_read(&root->log_writers))
2963 mutex_unlock(&root->log_mutex);
2965 mutex_lock(&root->log_mutex);
2967 finish_wait(&root->log_writer_wait, &wait);
2970 static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2971 struct btrfs_log_ctx *ctx)
2976 mutex_lock(&root->log_mutex);
2977 list_del_init(&ctx->list);
2978 mutex_unlock(&root->log_mutex);
2982 * Invoked in log mutex context, or be sure there is no other task which
2983 * can access the list.
2985 static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2986 int index, int error)
2988 struct btrfs_log_ctx *ctx;
2989 struct btrfs_log_ctx *safe;
2991 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2992 list_del_init(&ctx->list);
2993 ctx->log_ret = error;
2996 INIT_LIST_HEAD(&root->log_ctxs[index]);
3000 * btrfs_sync_log does sends a given tree log down to the disk and
3001 * updates the super blocks to record it. When this call is done,
3002 * you know that any inodes previously logged are safely on disk only
3005 * Any other return value means you need to call btrfs_commit_transaction.
3006 * Some of the edge cases for fsyncing directories that have had unlinks
3007 * or renames done in the past mean that sometimes the only safe
3008 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
3009 * that has happened.
3011 int btrfs_sync_log(struct btrfs_trans_handle *trans,
3012 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
3018 struct btrfs_fs_info *fs_info = root->fs_info;
3019 struct btrfs_root *log = root->log_root;
3020 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
3021 struct btrfs_root_item new_root_item;
3022 int log_transid = 0;
3023 struct btrfs_log_ctx root_log_ctx;
3024 struct blk_plug plug;
3026 mutex_lock(&root->log_mutex);
3027 log_transid = ctx->log_transid;
3028 if (root->log_transid_committed >= log_transid) {
3029 mutex_unlock(&root->log_mutex);
3030 return ctx->log_ret;
3033 index1 = log_transid % 2;
3034 if (atomic_read(&root->log_commit[index1])) {
3035 wait_log_commit(root, log_transid);
3036 mutex_unlock(&root->log_mutex);
3037 return ctx->log_ret;
3039 ASSERT(log_transid == root->log_transid);
3040 atomic_set(&root->log_commit[index1], 1);
3042 /* wait for previous tree log sync to complete */
3043 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
3044 wait_log_commit(root, log_transid - 1);
3047 int batch = atomic_read(&root->log_batch);
3048 /* when we're on an ssd, just kick the log commit out */
3049 if (!btrfs_test_opt(fs_info, SSD) &&
3050 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
3051 mutex_unlock(&root->log_mutex);
3052 schedule_timeout_uninterruptible(1);
3053 mutex_lock(&root->log_mutex);
3055 wait_for_writer(root);
3056 if (batch == atomic_read(&root->log_batch))
3060 /* bail out if we need to do a full commit */
3061 if (btrfs_need_log_full_commit(trans)) {
3063 mutex_unlock(&root->log_mutex);
3067 if (log_transid % 2 == 0)
3068 mark = EXTENT_DIRTY;
3072 /* we start IO on all the marked extents here, but we don't actually
3073 * wait for them until later.
3075 blk_start_plug(&plug);
3076 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
3078 blk_finish_plug(&plug);
3079 btrfs_abort_transaction(trans, ret);
3080 btrfs_set_log_full_commit(trans);
3081 mutex_unlock(&root->log_mutex);
3086 * We _must_ update under the root->log_mutex in order to make sure we
3087 * have a consistent view of the log root we are trying to commit at
3090 * We _must_ copy this into a local copy, because we are not holding the
3091 * log_root_tree->log_mutex yet. This is important because when we
3092 * commit the log_root_tree we must have a consistent view of the
3093 * log_root_tree when we update the super block to point at the
3094 * log_root_tree bytenr. If we update the log_root_tree here we'll race
3095 * with the commit and possibly point at the new block which we may not
3098 btrfs_set_root_node(&log->root_item, log->node);
3099 memcpy(&new_root_item, &log->root_item, sizeof(new_root_item));
3101 root->log_transid++;
3102 log->log_transid = root->log_transid;
3103 root->log_start_pid = 0;
3105 * IO has been started, blocks of the log tree have WRITTEN flag set
3106 * in their headers. new modifications of the log will be written to
3107 * new positions. so it's safe to allow log writers to go in.
3109 mutex_unlock(&root->log_mutex);
3111 btrfs_init_log_ctx(&root_log_ctx, NULL);
3113 mutex_lock(&log_root_tree->log_mutex);
3115 index2 = log_root_tree->log_transid % 2;
3116 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
3117 root_log_ctx.log_transid = log_root_tree->log_transid;
3120 * Now we are safe to update the log_root_tree because we're under the
3121 * log_mutex, and we're a current writer so we're holding the commit
3122 * open until we drop the log_mutex.
3124 ret = update_log_root(trans, log, &new_root_item);
3126 if (!list_empty(&root_log_ctx.list))
3127 list_del_init(&root_log_ctx.list);
3129 blk_finish_plug(&plug);
3130 btrfs_set_log_full_commit(trans);
3132 if (ret != -ENOSPC) {
3133 btrfs_abort_transaction(trans, ret);
3134 mutex_unlock(&log_root_tree->log_mutex);
3137 btrfs_wait_tree_log_extents(log, mark);
3138 mutex_unlock(&log_root_tree->log_mutex);
3143 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
3144 blk_finish_plug(&plug);
3145 list_del_init(&root_log_ctx.list);
3146 mutex_unlock(&log_root_tree->log_mutex);
3147 ret = root_log_ctx.log_ret;
3151 index2 = root_log_ctx.log_transid % 2;
3152 if (atomic_read(&log_root_tree->log_commit[index2])) {
3153 blk_finish_plug(&plug);
3154 ret = btrfs_wait_tree_log_extents(log, mark);
3155 wait_log_commit(log_root_tree,
3156 root_log_ctx.log_transid);
3157 mutex_unlock(&log_root_tree->log_mutex);
3159 ret = root_log_ctx.log_ret;
3162 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
3163 atomic_set(&log_root_tree->log_commit[index2], 1);
3165 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
3166 wait_log_commit(log_root_tree,
3167 root_log_ctx.log_transid - 1);
3171 * now that we've moved on to the tree of log tree roots,
3172 * check the full commit flag again
3174 if (btrfs_need_log_full_commit(trans)) {
3175 blk_finish_plug(&plug);
3176 btrfs_wait_tree_log_extents(log, mark);
3177 mutex_unlock(&log_root_tree->log_mutex);
3179 goto out_wake_log_root;
3182 ret = btrfs_write_marked_extents(fs_info,
3183 &log_root_tree->dirty_log_pages,
3184 EXTENT_DIRTY | EXTENT_NEW);
3185 blk_finish_plug(&plug);
3187 btrfs_set_log_full_commit(trans);
3188 btrfs_abort_transaction(trans, ret);
3189 mutex_unlock(&log_root_tree->log_mutex);
3190 goto out_wake_log_root;
3192 ret = btrfs_wait_tree_log_extents(log, mark);
3194 ret = btrfs_wait_tree_log_extents(log_root_tree,
3195 EXTENT_NEW | EXTENT_DIRTY);
3197 btrfs_set_log_full_commit(trans);
3198 mutex_unlock(&log_root_tree->log_mutex);
3199 goto out_wake_log_root;
3202 btrfs_set_super_log_root(fs_info->super_for_commit,
3203 log_root_tree->node->start);
3204 btrfs_set_super_log_root_level(fs_info->super_for_commit,
3205 btrfs_header_level(log_root_tree->node));
3207 log_root_tree->log_transid++;
3208 mutex_unlock(&log_root_tree->log_mutex);
3211 * Nobody else is going to jump in and write the ctree
3212 * super here because the log_commit atomic below is protecting
3213 * us. We must be called with a transaction handle pinning
3214 * the running transaction open, so a full commit can't hop
3215 * in and cause problems either.
3217 ret = write_all_supers(fs_info, 1);
3219 btrfs_set_log_full_commit(trans);
3220 btrfs_abort_transaction(trans, ret);
3221 goto out_wake_log_root;
3224 mutex_lock(&root->log_mutex);
3225 if (root->last_log_commit < log_transid)
3226 root->last_log_commit = log_transid;
3227 mutex_unlock(&root->log_mutex);
3230 mutex_lock(&log_root_tree->log_mutex);
3231 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3233 log_root_tree->log_transid_committed++;
3234 atomic_set(&log_root_tree->log_commit[index2], 0);
3235 mutex_unlock(&log_root_tree->log_mutex);
3238 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3239 * all the updates above are seen by the woken threads. It might not be
3240 * necessary, but proving that seems to be hard.
3242 cond_wake_up(&log_root_tree->log_commit_wait[index2]);
3244 mutex_lock(&root->log_mutex);
3245 btrfs_remove_all_log_ctxs(root, index1, ret);
3246 root->log_transid_committed++;
3247 atomic_set(&root->log_commit[index1], 0);
3248 mutex_unlock(&root->log_mutex);
3251 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3252 * all the updates above are seen by the woken threads. It might not be
3253 * necessary, but proving that seems to be hard.
3255 cond_wake_up(&root->log_commit_wait[index1]);
3259 static void free_log_tree(struct btrfs_trans_handle *trans,
3260 struct btrfs_root *log)
3263 struct walk_control wc = {
3265 .process_func = process_one_buffer
3268 ret = walk_log_tree(trans, log, &wc);
3271 btrfs_abort_transaction(trans, ret);
3273 btrfs_handle_fs_error(log->fs_info, ret, NULL);
3276 clear_extent_bits(&log->dirty_log_pages, 0, (u64)-1,
3277 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
3278 extent_io_tree_release(&log->log_csum_range);
3279 btrfs_put_root(log);
3283 * free all the extents used by the tree log. This should be called
3284 * at commit time of the full transaction
3286 int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3288 if (root->log_root) {
3289 free_log_tree(trans, root->log_root);
3290 root->log_root = NULL;
3291 clear_bit(BTRFS_ROOT_HAS_LOG_TREE, &root->state);
3296 int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3297 struct btrfs_fs_info *fs_info)
3299 if (fs_info->log_root_tree) {
3300 free_log_tree(trans, fs_info->log_root_tree);
3301 fs_info->log_root_tree = NULL;
3307 * Check if an inode was logged in the current transaction. We can't always rely
3308 * on an inode's logged_trans value, because it's an in-memory only field and
3309 * therefore not persisted. This means that its value is lost if the inode gets
3310 * evicted and loaded again from disk (in which case it has a value of 0, and
3311 * certainly it is smaller then any possible transaction ID), when that happens
3312 * the full_sync flag is set in the inode's runtime flags, so on that case we
3313 * assume eviction happened and ignore the logged_trans value, assuming the
3314 * worst case, that the inode was logged before in the current transaction.
3316 static bool inode_logged(struct btrfs_trans_handle *trans,
3317 struct btrfs_inode *inode)
3319 if (inode->logged_trans == trans->transid)
3322 if (inode->last_trans == trans->transid &&
3323 test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) &&
3324 !test_bit(BTRFS_FS_LOG_RECOVERING, &trans->fs_info->flags))
3331 * If both a file and directory are logged, and unlinks or renames are
3332 * mixed in, we have a few interesting corners:
3334 * create file X in dir Y
3335 * link file X to X.link in dir Y
3337 * unlink file X but leave X.link
3340 * After a crash we would expect only X.link to exist. But file X
3341 * didn't get fsync'd again so the log has back refs for X and X.link.
3343 * We solve this by removing directory entries and inode backrefs from the
3344 * log when a file that was logged in the current transaction is
3345 * unlinked. Any later fsync will include the updated log entries, and
3346 * we'll be able to reconstruct the proper directory items from backrefs.
3348 * This optimizations allows us to avoid relogging the entire inode
3349 * or the entire directory.
3351 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3352 struct btrfs_root *root,
3353 const char *name, int name_len,
3354 struct btrfs_inode *dir, u64 index)
3356 struct btrfs_root *log;
3357 struct btrfs_dir_item *di;
3358 struct btrfs_path *path;
3362 u64 dir_ino = btrfs_ino(dir);
3364 if (!inode_logged(trans, dir))
3367 ret = join_running_log_trans(root);
3371 mutex_lock(&dir->log_mutex);
3373 log = root->log_root;
3374 path = btrfs_alloc_path();
3380 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
3381 name, name_len, -1);
3387 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3388 bytes_del += name_len;
3394 btrfs_release_path(path);
3395 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
3396 index, name, name_len, -1);
3402 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3403 bytes_del += name_len;
3410 /* update the directory size in the log to reflect the names
3414 struct btrfs_key key;
3416 key.objectid = dir_ino;
3418 key.type = BTRFS_INODE_ITEM_KEY;
3419 btrfs_release_path(path);
3421 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
3427 struct btrfs_inode_item *item;
3430 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3431 struct btrfs_inode_item);
3432 i_size = btrfs_inode_size(path->nodes[0], item);
3433 if (i_size > bytes_del)
3434 i_size -= bytes_del;
3437 btrfs_set_inode_size(path->nodes[0], item, i_size);
3438 btrfs_mark_buffer_dirty(path->nodes[0]);
3441 btrfs_release_path(path);
3444 btrfs_free_path(path);
3446 mutex_unlock(&dir->log_mutex);
3447 if (err == -ENOSPC) {
3448 btrfs_set_log_full_commit(trans);
3450 } else if (err < 0 && err != -ENOENT) {
3451 /* ENOENT can be returned if the entry hasn't been fsynced yet */
3452 btrfs_abort_transaction(trans, err);
3455 btrfs_end_log_trans(root);
3460 /* see comments for btrfs_del_dir_entries_in_log */
3461 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3462 struct btrfs_root *root,
3463 const char *name, int name_len,
3464 struct btrfs_inode *inode, u64 dirid)
3466 struct btrfs_root *log;
3470 if (!inode_logged(trans, inode))
3473 ret = join_running_log_trans(root);
3476 log = root->log_root;
3477 mutex_lock(&inode->log_mutex);
3479 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
3481 mutex_unlock(&inode->log_mutex);
3482 if (ret == -ENOSPC) {
3483 btrfs_set_log_full_commit(trans);
3485 } else if (ret < 0 && ret != -ENOENT)
3486 btrfs_abort_transaction(trans, ret);
3487 btrfs_end_log_trans(root);
3493 * creates a range item in the log for 'dirid'. first_offset and
3494 * last_offset tell us which parts of the key space the log should
3495 * be considered authoritative for.
3497 static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3498 struct btrfs_root *log,
3499 struct btrfs_path *path,
3500 int key_type, u64 dirid,
3501 u64 first_offset, u64 last_offset)
3504 struct btrfs_key key;
3505 struct btrfs_dir_log_item *item;
3507 key.objectid = dirid;
3508 key.offset = first_offset;
3509 if (key_type == BTRFS_DIR_ITEM_KEY)
3510 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3512 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3513 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
3517 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3518 struct btrfs_dir_log_item);
3519 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3520 btrfs_mark_buffer_dirty(path->nodes[0]);
3521 btrfs_release_path(path);
3526 * log all the items included in the current transaction for a given
3527 * directory. This also creates the range items in the log tree required
3528 * to replay anything deleted before the fsync
3530 static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3531 struct btrfs_root *root, struct btrfs_inode *inode,
3532 struct btrfs_path *path,
3533 struct btrfs_path *dst_path, int key_type,
3534 struct btrfs_log_ctx *ctx,
3535 u64 min_offset, u64 *last_offset_ret)
3537 struct btrfs_key min_key;
3538 struct btrfs_root *log = root->log_root;
3539 struct extent_buffer *src;
3544 u64 first_offset = min_offset;
3545 u64 last_offset = (u64)-1;
3546 u64 ino = btrfs_ino(inode);
3548 log = root->log_root;
3550 min_key.objectid = ino;
3551 min_key.type = key_type;
3552 min_key.offset = min_offset;
3554 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
3557 * we didn't find anything from this transaction, see if there
3558 * is anything at all
3560 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3561 min_key.objectid = ino;
3562 min_key.type = key_type;
3563 min_key.offset = (u64)-1;
3564 btrfs_release_path(path);
3565 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3567 btrfs_release_path(path);
3570 ret = btrfs_previous_item(root, path, ino, key_type);
3572 /* if ret == 0 there are items for this type,
3573 * create a range to tell us the last key of this type.
3574 * otherwise, there are no items in this directory after
3575 * *min_offset, and we create a range to indicate that.
3578 struct btrfs_key tmp;
3579 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3581 if (key_type == tmp.type)
3582 first_offset = max(min_offset, tmp.offset) + 1;
3587 /* go backward to find any previous key */
3588 ret = btrfs_previous_item(root, path, ino, key_type);
3590 struct btrfs_key tmp;
3591 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3592 if (key_type == tmp.type) {
3593 first_offset = tmp.offset;
3594 ret = overwrite_item(trans, log, dst_path,
3595 path->nodes[0], path->slots[0],
3603 btrfs_release_path(path);
3606 * Find the first key from this transaction again. See the note for
3607 * log_new_dir_dentries, if we're logging a directory recursively we
3608 * won't be holding its i_mutex, which means we can modify the directory
3609 * while we're logging it. If we remove an entry between our first
3610 * search and this search we'll not find the key again and can just
3614 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3619 * we have a block from this transaction, log every item in it
3620 * from our directory
3623 struct btrfs_key tmp;
3624 src = path->nodes[0];
3625 nritems = btrfs_header_nritems(src);
3626 for (i = path->slots[0]; i < nritems; i++) {
3627 struct btrfs_dir_item *di;
3629 btrfs_item_key_to_cpu(src, &min_key, i);
3631 if (min_key.objectid != ino || min_key.type != key_type)
3634 if (need_resched()) {
3635 btrfs_release_path(path);
3640 ret = overwrite_item(trans, log, dst_path, src, i,
3648 * We must make sure that when we log a directory entry,
3649 * the corresponding inode, after log replay, has a
3650 * matching link count. For example:
3656 * xfs_io -c "fsync" mydir
3658 * <mount fs and log replay>
3660 * Would result in a fsync log that when replayed, our
3661 * file inode would have a link count of 1, but we get
3662 * two directory entries pointing to the same inode.
3663 * After removing one of the names, it would not be
3664 * possible to remove the other name, which resulted
3665 * always in stale file handle errors, and would not
3666 * be possible to rmdir the parent directory, since
3667 * its i_size could never decrement to the value
3668 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3670 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3671 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3673 (btrfs_dir_transid(src, di) == trans->transid ||
3674 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3675 tmp.type != BTRFS_ROOT_ITEM_KEY)
3676 ctx->log_new_dentries = true;
3678 path->slots[0] = nritems;
3681 * look ahead to the next item and see if it is also
3682 * from this directory and from this transaction
3684 ret = btrfs_next_leaf(root, path);
3687 last_offset = (u64)-1;
3692 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3693 if (tmp.objectid != ino || tmp.type != key_type) {
3694 last_offset = (u64)-1;
3697 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3698 ret = overwrite_item(trans, log, dst_path,
3699 path->nodes[0], path->slots[0],
3704 last_offset = tmp.offset;
3709 btrfs_release_path(path);
3710 btrfs_release_path(dst_path);
3713 *last_offset_ret = last_offset;
3715 * insert the log range keys to indicate where the log
3718 ret = insert_dir_log_key(trans, log, path, key_type,
3719 ino, first_offset, last_offset);
3727 * logging directories is very similar to logging inodes, We find all the items
3728 * from the current transaction and write them to the log.
3730 * The recovery code scans the directory in the subvolume, and if it finds a
3731 * key in the range logged that is not present in the log tree, then it means
3732 * that dir entry was unlinked during the transaction.
3734 * In order for that scan to work, we must include one key smaller than
3735 * the smallest logged by this transaction and one key larger than the largest
3736 * key logged by this transaction.
3738 static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3739 struct btrfs_root *root, struct btrfs_inode *inode,
3740 struct btrfs_path *path,
3741 struct btrfs_path *dst_path,
3742 struct btrfs_log_ctx *ctx)
3747 int key_type = BTRFS_DIR_ITEM_KEY;
3753 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3754 ctx, min_key, &max_key);
3757 if (max_key == (u64)-1)
3759 min_key = max_key + 1;
3762 if (key_type == BTRFS_DIR_ITEM_KEY) {
3763 key_type = BTRFS_DIR_INDEX_KEY;
3770 * a helper function to drop items from the log before we relog an
3771 * inode. max_key_type indicates the highest item type to remove.
3772 * This cannot be run for file data extents because it does not
3773 * free the extents they point to.
3775 static int drop_objectid_items(struct btrfs_trans_handle *trans,
3776 struct btrfs_root *log,
3777 struct btrfs_path *path,
3778 u64 objectid, int max_key_type)
3781 struct btrfs_key key;
3782 struct btrfs_key found_key;
3785 key.objectid = objectid;
3786 key.type = max_key_type;
3787 key.offset = (u64)-1;
3790 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
3791 BUG_ON(ret == 0); /* Logic error */
3795 if (path->slots[0] == 0)
3799 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3802 if (found_key.objectid != objectid)
3805 found_key.offset = 0;
3807 ret = btrfs_bin_search(path->nodes[0], &found_key, &start_slot);
3811 ret = btrfs_del_items(trans, log, path, start_slot,
3812 path->slots[0] - start_slot + 1);
3814 * If start slot isn't 0 then we don't need to re-search, we've
3815 * found the last guy with the objectid in this tree.
3817 if (ret || start_slot != 0)
3819 btrfs_release_path(path);
3821 btrfs_release_path(path);
3827 static void fill_inode_item(struct btrfs_trans_handle *trans,
3828 struct extent_buffer *leaf,
3829 struct btrfs_inode_item *item,
3830 struct inode *inode, int log_inode_only,
3833 struct btrfs_map_token token;
3835 btrfs_init_map_token(&token, leaf);
3837 if (log_inode_only) {
3838 /* set the generation to zero so the recover code
3839 * can tell the difference between an logging
3840 * just to say 'this inode exists' and a logging
3841 * to say 'update this inode with these values'
3843 btrfs_set_token_inode_generation(&token, item, 0);
3844 btrfs_set_token_inode_size(&token, item, logged_isize);
3846 btrfs_set_token_inode_generation(&token, item,
3847 BTRFS_I(inode)->generation);
3848 btrfs_set_token_inode_size(&token, item, inode->i_size);
3851 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3852 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3853 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3854 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
3856 btrfs_set_token_timespec_sec(&token, &item->atime,
3857 inode->i_atime.tv_sec);
3858 btrfs_set_token_timespec_nsec(&token, &item->atime,
3859 inode->i_atime.tv_nsec);
3861 btrfs_set_token_timespec_sec(&token, &item->mtime,
3862 inode->i_mtime.tv_sec);
3863 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3864 inode->i_mtime.tv_nsec);
3866 btrfs_set_token_timespec_sec(&token, &item->ctime,
3867 inode->i_ctime.tv_sec);
3868 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3869 inode->i_ctime.tv_nsec);
3871 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3873 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3874 btrfs_set_token_inode_transid(&token, item, trans->transid);
3875 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3876 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3877 btrfs_set_token_inode_block_group(&token, item, 0);
3880 static int log_inode_item(struct btrfs_trans_handle *trans,
3881 struct btrfs_root *log, struct btrfs_path *path,
3882 struct btrfs_inode *inode)
3884 struct btrfs_inode_item *inode_item;
3887 ret = btrfs_insert_empty_item(trans, log, path,
3888 &inode->location, sizeof(*inode_item));
3889 if (ret && ret != -EEXIST)
3891 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3892 struct btrfs_inode_item);
3893 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3895 btrfs_release_path(path);
3899 static int log_csums(struct btrfs_trans_handle *trans,
3900 struct btrfs_inode *inode,
3901 struct btrfs_root *log_root,
3902 struct btrfs_ordered_sum *sums)
3904 const u64 lock_end = sums->bytenr + sums->len - 1;
3905 struct extent_state *cached_state = NULL;
3909 * If this inode was not used for reflink operations in the current
3910 * transaction with new extents, then do the fast path, no need to
3911 * worry about logging checksum items with overlapping ranges.
3913 if (inode->last_reflink_trans < trans->transid)
3914 return btrfs_csum_file_blocks(trans, log_root, sums);
3917 * Serialize logging for checksums. This is to avoid racing with the
3918 * same checksum being logged by another task that is logging another
3919 * file which happens to refer to the same extent as well. Such races
3920 * can leave checksum items in the log with overlapping ranges.
3922 ret = lock_extent_bits(&log_root->log_csum_range, sums->bytenr,
3923 lock_end, &cached_state);
3927 * Due to extent cloning, we might have logged a csum item that covers a
3928 * subrange of a cloned extent, and later we can end up logging a csum
3929 * item for a larger subrange of the same extent or the entire range.
3930 * This would leave csum items in the log tree that cover the same range
3931 * and break the searches for checksums in the log tree, resulting in
3932 * some checksums missing in the fs/subvolume tree. So just delete (or
3933 * trim and adjust) any existing csum items in the log for this range.
3935 ret = btrfs_del_csums(trans, log_root, sums->bytenr, sums->len);
3937 ret = btrfs_csum_file_blocks(trans, log_root, sums);
3939 unlock_extent_cached(&log_root->log_csum_range, sums->bytenr, lock_end,
3945 static noinline int copy_items(struct btrfs_trans_handle *trans,
3946 struct btrfs_inode *inode,
3947 struct btrfs_path *dst_path,
3948 struct btrfs_path *src_path,
3949 int start_slot, int nr, int inode_only,
3952 struct btrfs_fs_info *fs_info = trans->fs_info;
3953 unsigned long src_offset;
3954 unsigned long dst_offset;
3955 struct btrfs_root *log = inode->root->log_root;
3956 struct btrfs_file_extent_item *extent;
3957 struct btrfs_inode_item *inode_item;
3958 struct extent_buffer *src = src_path->nodes[0];
3960 struct btrfs_key *ins_keys;
3964 struct list_head ordered_sums;
3965 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
3967 INIT_LIST_HEAD(&ordered_sums);
3969 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3970 nr * sizeof(u32), GFP_NOFS);
3974 ins_sizes = (u32 *)ins_data;
3975 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3977 for (i = 0; i < nr; i++) {
3978 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3979 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3981 ret = btrfs_insert_empty_items(trans, log, dst_path,
3982 ins_keys, ins_sizes, nr);
3988 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
3989 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3990 dst_path->slots[0]);
3992 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3994 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
3995 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3997 struct btrfs_inode_item);
3998 fill_inode_item(trans, dst_path->nodes[0], inode_item,
4000 inode_only == LOG_INODE_EXISTS,
4003 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
4004 src_offset, ins_sizes[i]);
4007 /* take a reference on file data extents so that truncates
4008 * or deletes of this inode don't have to relog the inode
4011 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
4014 extent = btrfs_item_ptr(src, start_slot + i,
4015 struct btrfs_file_extent_item);
4017 if (btrfs_file_extent_generation(src, extent) < trans->transid)
4020 found_type = btrfs_file_extent_type(src, extent);
4021 if (found_type == BTRFS_FILE_EXTENT_REG) {
4023 ds = btrfs_file_extent_disk_bytenr(src,
4025 /* ds == 0 is a hole */
4029 dl = btrfs_file_extent_disk_num_bytes(src,
4031 cs = btrfs_file_extent_offset(src, extent);
4032 cl = btrfs_file_extent_num_bytes(src,
4034 if (btrfs_file_extent_compression(src,
4040 ret = btrfs_lookup_csums_range(
4042 ds + cs, ds + cs + cl - 1,
4050 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
4051 btrfs_release_path(dst_path);
4055 * we have to do this after the loop above to avoid changing the
4056 * log tree while trying to change the log tree.
4058 while (!list_empty(&ordered_sums)) {
4059 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4060 struct btrfs_ordered_sum,
4063 ret = log_csums(trans, inode, log, sums);
4064 list_del(&sums->list);
4071 static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
4073 struct extent_map *em1, *em2;
4075 em1 = list_entry(a, struct extent_map, list);
4076 em2 = list_entry(b, struct extent_map, list);
4078 if (em1->start < em2->start)
4080 else if (em1->start > em2->start)
4085 static int log_extent_csums(struct btrfs_trans_handle *trans,
4086 struct btrfs_inode *inode,
4087 struct btrfs_root *log_root,
4088 const struct extent_map *em,
4089 struct btrfs_log_ctx *ctx)
4091 struct btrfs_ordered_extent *ordered;
4094 u64 mod_start = em->mod_start;
4095 u64 mod_len = em->mod_len;
4096 LIST_HEAD(ordered_sums);
4099 if (inode->flags & BTRFS_INODE_NODATASUM ||
4100 test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
4101 em->block_start == EXTENT_MAP_HOLE)
4104 list_for_each_entry(ordered, &ctx->ordered_extents, log_list) {
4105 const u64 ordered_end = ordered->file_offset + ordered->num_bytes;
4106 const u64 mod_end = mod_start + mod_len;
4107 struct btrfs_ordered_sum *sums;
4112 if (ordered_end <= mod_start)
4114 if (mod_end <= ordered->file_offset)
4118 * We are going to copy all the csums on this ordered extent, so
4119 * go ahead and adjust mod_start and mod_len in case this ordered
4120 * extent has already been logged.
4122 if (ordered->file_offset > mod_start) {
4123 if (ordered_end >= mod_end)
4124 mod_len = ordered->file_offset - mod_start;
4126 * If we have this case
4128 * |--------- logged extent ---------|
4129 * |----- ordered extent ----|
4131 * Just don't mess with mod_start and mod_len, we'll
4132 * just end up logging more csums than we need and it
4136 if (ordered_end < mod_end) {
4137 mod_len = mod_end - ordered_end;
4138 mod_start = ordered_end;
4145 * To keep us from looping for the above case of an ordered
4146 * extent that falls inside of the logged extent.
4148 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM, &ordered->flags))
4151 list_for_each_entry(sums, &ordered->list, list) {
4152 ret = log_csums(trans, inode, log_root, sums);
4158 /* We're done, found all csums in the ordered extents. */
4162 /* If we're compressed we have to save the entire range of csums. */
4163 if (em->compress_type) {
4165 csum_len = max(em->block_len, em->orig_block_len);
4167 csum_offset = mod_start - em->start;
4171 /* block start is already adjusted for the file extent offset. */
4172 ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
4173 em->block_start + csum_offset,
4174 em->block_start + csum_offset +
4175 csum_len - 1, &ordered_sums, 0);
4179 while (!list_empty(&ordered_sums)) {
4180 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4181 struct btrfs_ordered_sum,
4184 ret = log_csums(trans, inode, log_root, sums);
4185 list_del(&sums->list);
4192 static int log_one_extent(struct btrfs_trans_handle *trans,
4193 struct btrfs_inode *inode, struct btrfs_root *root,
4194 const struct extent_map *em,
4195 struct btrfs_path *path,
4196 struct btrfs_log_ctx *ctx)
4198 struct btrfs_drop_extents_args drop_args = { 0 };
4199 struct btrfs_root *log = root->log_root;
4200 struct btrfs_file_extent_item *fi;
4201 struct extent_buffer *leaf;
4202 struct btrfs_map_token token;
4203 struct btrfs_key key;
4204 u64 extent_offset = em->start - em->orig_start;
4208 ret = log_extent_csums(trans, inode, log, em, ctx);
4212 drop_args.path = path;
4213 drop_args.start = em->start;
4214 drop_args.end = em->start + em->len;
4215 drop_args.replace_extent = true;
4216 drop_args.extent_item_size = sizeof(*fi);
4217 ret = btrfs_drop_extents(trans, log, inode, &drop_args);
4221 if (!drop_args.extent_inserted) {
4222 key.objectid = btrfs_ino(inode);
4223 key.type = BTRFS_EXTENT_DATA_KEY;
4224 key.offset = em->start;
4226 ret = btrfs_insert_empty_item(trans, log, path, &key,
4231 leaf = path->nodes[0];
4232 btrfs_init_map_token(&token, leaf);
4233 fi = btrfs_item_ptr(leaf, path->slots[0],
4234 struct btrfs_file_extent_item);
4236 btrfs_set_token_file_extent_generation(&token, fi, trans->transid);
4237 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4238 btrfs_set_token_file_extent_type(&token, fi,
4239 BTRFS_FILE_EXTENT_PREALLOC);
4241 btrfs_set_token_file_extent_type(&token, fi,
4242 BTRFS_FILE_EXTENT_REG);
4244 block_len = max(em->block_len, em->orig_block_len);
4245 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4246 btrfs_set_token_file_extent_disk_bytenr(&token, fi,
4248 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
4249 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4250 btrfs_set_token_file_extent_disk_bytenr(&token, fi,
4253 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, block_len);
4255 btrfs_set_token_file_extent_disk_bytenr(&token, fi, 0);
4256 btrfs_set_token_file_extent_disk_num_bytes(&token, fi, 0);
4259 btrfs_set_token_file_extent_offset(&token, fi, extent_offset);
4260 btrfs_set_token_file_extent_num_bytes(&token, fi, em->len);
4261 btrfs_set_token_file_extent_ram_bytes(&token, fi, em->ram_bytes);
4262 btrfs_set_token_file_extent_compression(&token, fi, em->compress_type);
4263 btrfs_set_token_file_extent_encryption(&token, fi, 0);
4264 btrfs_set_token_file_extent_other_encoding(&token, fi, 0);
4265 btrfs_mark_buffer_dirty(leaf);
4267 btrfs_release_path(path);
4273 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4274 * lose them after doing a fast fsync and replaying the log. We scan the
4275 * subvolume's root instead of iterating the inode's extent map tree because
4276 * otherwise we can log incorrect extent items based on extent map conversion.
4277 * That can happen due to the fact that extent maps are merged when they
4278 * are not in the extent map tree's list of modified extents.
4280 static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4281 struct btrfs_inode *inode,
4282 struct btrfs_path *path)
4284 struct btrfs_root *root = inode->root;
4285 struct btrfs_key key;
4286 const u64 i_size = i_size_read(&inode->vfs_inode);
4287 const u64 ino = btrfs_ino(inode);
4288 struct btrfs_path *dst_path = NULL;
4289 bool dropped_extents = false;
4290 u64 truncate_offset = i_size;
4291 struct extent_buffer *leaf;
4297 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4301 key.type = BTRFS_EXTENT_DATA_KEY;
4302 key.offset = i_size;
4303 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4308 * We must check if there is a prealloc extent that starts before the
4309 * i_size and crosses the i_size boundary. This is to ensure later we
4310 * truncate down to the end of that extent and not to the i_size, as
4311 * otherwise we end up losing part of the prealloc extent after a log
4312 * replay and with an implicit hole if there is another prealloc extent
4313 * that starts at an offset beyond i_size.
4315 ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
4320 struct btrfs_file_extent_item *ei;
4322 leaf = path->nodes[0];
4323 slot = path->slots[0];
4324 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4326 if (btrfs_file_extent_type(leaf, ei) ==
4327 BTRFS_FILE_EXTENT_PREALLOC) {
4330 btrfs_item_key_to_cpu(leaf, &key, slot);
4331 extent_end = key.offset +
4332 btrfs_file_extent_num_bytes(leaf, ei);
4334 if (extent_end > i_size)
4335 truncate_offset = extent_end;
4342 leaf = path->nodes[0];
4343 slot = path->slots[0];
4345 if (slot >= btrfs_header_nritems(leaf)) {
4347 ret = copy_items(trans, inode, dst_path, path,
4348 start_slot, ins_nr, 1, 0);
4353 ret = btrfs_next_leaf(root, path);
4363 btrfs_item_key_to_cpu(leaf, &key, slot);
4364 if (key.objectid > ino)
4366 if (WARN_ON_ONCE(key.objectid < ino) ||
4367 key.type < BTRFS_EXTENT_DATA_KEY ||
4368 key.offset < i_size) {
4372 if (!dropped_extents) {
4374 * Avoid logging extent items logged in past fsync calls
4375 * and leading to duplicate keys in the log tree.
4378 ret = btrfs_truncate_inode_items(trans,
4380 inode, truncate_offset,
4381 BTRFS_EXTENT_DATA_KEY);
4382 } while (ret == -EAGAIN);
4385 dropped_extents = true;
4392 dst_path = btrfs_alloc_path();
4400 ret = copy_items(trans, inode, dst_path, path,
4401 start_slot, ins_nr, 1, 0);
4403 btrfs_release_path(path);
4404 btrfs_free_path(dst_path);
4408 static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4409 struct btrfs_root *root,
4410 struct btrfs_inode *inode,
4411 struct btrfs_path *path,
4412 struct btrfs_log_ctx *ctx)
4414 struct btrfs_ordered_extent *ordered;
4415 struct btrfs_ordered_extent *tmp;
4416 struct extent_map *em, *n;
4417 struct list_head extents;
4418 struct extent_map_tree *tree = &inode->extent_tree;
4423 INIT_LIST_HEAD(&extents);
4425 write_lock(&tree->lock);
4426 test_gen = root->fs_info->last_trans_committed;
4428 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4429 list_del_init(&em->list);
4431 * Just an arbitrary number, this can be really CPU intensive
4432 * once we start getting a lot of extents, and really once we
4433 * have a bunch of extents we just want to commit since it will
4436 if (++num > 32768) {
4437 list_del_init(&tree->modified_extents);
4442 if (em->generation <= test_gen)
4445 /* We log prealloc extents beyond eof later. */
4446 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4447 em->start >= i_size_read(&inode->vfs_inode))
4450 /* Need a ref to keep it from getting evicted from cache */
4451 refcount_inc(&em->refs);
4452 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
4453 list_add_tail(&em->list, &extents);
4457 list_sort(NULL, &extents, extent_cmp);
4459 while (!list_empty(&extents)) {
4460 em = list_entry(extents.next, struct extent_map, list);
4462 list_del_init(&em->list);
4465 * If we had an error we just need to delete everybody from our
4469 clear_em_logging(tree, em);
4470 free_extent_map(em);
4474 write_unlock(&tree->lock);
4476 ret = log_one_extent(trans, inode, root, em, path, ctx);
4477 write_lock(&tree->lock);
4478 clear_em_logging(tree, em);
4479 free_extent_map(em);
4481 WARN_ON(!list_empty(&extents));
4482 write_unlock(&tree->lock);
4484 btrfs_release_path(path);
4486 ret = btrfs_log_prealloc_extents(trans, inode, path);
4491 * We have logged all extents successfully, now make sure the commit of
4492 * the current transaction waits for the ordered extents to complete
4493 * before it commits and wipes out the log trees, otherwise we would
4494 * lose data if an ordered extents completes after the transaction
4495 * commits and a power failure happens after the transaction commit.
4497 list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
4498 list_del_init(&ordered->log_list);
4499 set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
4501 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
4502 spin_lock_irq(&inode->ordered_tree.lock);
4503 if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
4504 set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
4505 atomic_inc(&trans->transaction->pending_ordered);
4507 spin_unlock_irq(&inode->ordered_tree.lock);
4509 btrfs_put_ordered_extent(ordered);
4515 static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
4516 struct btrfs_path *path, u64 *size_ret)
4518 struct btrfs_key key;
4521 key.objectid = btrfs_ino(inode);
4522 key.type = BTRFS_INODE_ITEM_KEY;
4525 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4528 } else if (ret > 0) {
4531 struct btrfs_inode_item *item;
4533 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4534 struct btrfs_inode_item);
4535 *size_ret = btrfs_inode_size(path->nodes[0], item);
4537 * If the in-memory inode's i_size is smaller then the inode
4538 * size stored in the btree, return the inode's i_size, so
4539 * that we get a correct inode size after replaying the log
4540 * when before a power failure we had a shrinking truncate
4541 * followed by addition of a new name (rename / new hard link).
4542 * Otherwise return the inode size from the btree, to avoid
4543 * data loss when replaying a log due to previously doing a
4544 * write that expands the inode's size and logging a new name
4545 * immediately after.
4547 if (*size_ret > inode->vfs_inode.i_size)
4548 *size_ret = inode->vfs_inode.i_size;
4551 btrfs_release_path(path);
4556 * At the moment we always log all xattrs. This is to figure out at log replay
4557 * time which xattrs must have their deletion replayed. If a xattr is missing
4558 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4559 * because if a xattr is deleted, the inode is fsynced and a power failure
4560 * happens, causing the log to be replayed the next time the fs is mounted,
4561 * we want the xattr to not exist anymore (same behaviour as other filesystems
4562 * with a journal, ext3/4, xfs, f2fs, etc).
4564 static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4565 struct btrfs_root *root,
4566 struct btrfs_inode *inode,
4567 struct btrfs_path *path,
4568 struct btrfs_path *dst_path)
4571 struct btrfs_key key;
4572 const u64 ino = btrfs_ino(inode);
4575 bool found_xattrs = false;
4577 if (test_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags))
4581 key.type = BTRFS_XATTR_ITEM_KEY;
4584 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4589 int slot = path->slots[0];
4590 struct extent_buffer *leaf = path->nodes[0];
4591 int nritems = btrfs_header_nritems(leaf);
4593 if (slot >= nritems) {
4595 ret = copy_items(trans, inode, dst_path, path,
4596 start_slot, ins_nr, 1, 0);
4601 ret = btrfs_next_leaf(root, path);
4609 btrfs_item_key_to_cpu(leaf, &key, slot);
4610 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4617 found_xattrs = true;
4621 ret = copy_items(trans, inode, dst_path, path,
4622 start_slot, ins_nr, 1, 0);
4628 set_bit(BTRFS_INODE_NO_XATTRS, &inode->runtime_flags);
4634 * When using the NO_HOLES feature if we punched a hole that causes the
4635 * deletion of entire leafs or all the extent items of the first leaf (the one
4636 * that contains the inode item and references) we may end up not processing
4637 * any extents, because there are no leafs with a generation matching the
4638 * current transaction that have extent items for our inode. So we need to find
4639 * if any holes exist and then log them. We also need to log holes after any
4640 * truncate operation that changes the inode's size.
4642 static int btrfs_log_holes(struct btrfs_trans_handle *trans,
4643 struct btrfs_root *root,
4644 struct btrfs_inode *inode,
4645 struct btrfs_path *path)
4647 struct btrfs_fs_info *fs_info = root->fs_info;
4648 struct btrfs_key key;
4649 const u64 ino = btrfs_ino(inode);
4650 const u64 i_size = i_size_read(&inode->vfs_inode);
4651 u64 prev_extent_end = 0;
4654 if (!btrfs_fs_incompat(fs_info, NO_HOLES) || i_size == 0)
4658 key.type = BTRFS_EXTENT_DATA_KEY;
4661 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4666 struct extent_buffer *leaf = path->nodes[0];
4668 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4669 ret = btrfs_next_leaf(root, path);
4676 leaf = path->nodes[0];
4679 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4680 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
4683 /* We have a hole, log it. */
4684 if (prev_extent_end < key.offset) {
4685 const u64 hole_len = key.offset - prev_extent_end;
4688 * Release the path to avoid deadlocks with other code
4689 * paths that search the root while holding locks on
4690 * leafs from the log root.
4692 btrfs_release_path(path);
4693 ret = btrfs_insert_file_extent(trans, root->log_root,
4694 ino, prev_extent_end, 0,
4695 0, hole_len, 0, hole_len,
4701 * Search for the same key again in the root. Since it's
4702 * an extent item and we are holding the inode lock, the
4703 * key must still exist. If it doesn't just emit warning
4704 * and return an error to fall back to a transaction
4707 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4710 if (WARN_ON(ret > 0))
4712 leaf = path->nodes[0];
4715 prev_extent_end = btrfs_file_extent_end(path);
4720 if (prev_extent_end < i_size) {
4723 btrfs_release_path(path);
4724 hole_len = ALIGN(i_size - prev_extent_end, fs_info->sectorsize);
4725 ret = btrfs_insert_file_extent(trans, root->log_root,
4726 ino, prev_extent_end, 0, 0,
4727 hole_len, 0, hole_len,
4737 * When we are logging a new inode X, check if it doesn't have a reference that
4738 * matches the reference from some other inode Y created in a past transaction
4739 * and that was renamed in the current transaction. If we don't do this, then at
4740 * log replay time we can lose inode Y (and all its files if it's a directory):
4743 * echo "hello world" > /mnt/x/foobar
4746 * mkdir /mnt/x # or touch /mnt/x
4747 * xfs_io -c fsync /mnt/x
4749 * mount fs, trigger log replay
4751 * After the log replay procedure, we would lose the first directory and all its
4752 * files (file foobar).
4753 * For the case where inode Y is not a directory we simply end up losing it:
4755 * echo "123" > /mnt/foo
4757 * mv /mnt/foo /mnt/bar
4758 * echo "abc" > /mnt/foo
4759 * xfs_io -c fsync /mnt/foo
4762 * We also need this for cases where a snapshot entry is replaced by some other
4763 * entry (file or directory) otherwise we end up with an unreplayable log due to
4764 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4765 * if it were a regular entry:
4768 * btrfs subvolume snapshot /mnt /mnt/x/snap
4769 * btrfs subvolume delete /mnt/x/snap
4772 * fsync /mnt/x or fsync some new file inside it
4775 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4776 * the same transaction.
4778 static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4780 const struct btrfs_key *key,
4781 struct btrfs_inode *inode,
4782 u64 *other_ino, u64 *other_parent)
4785 struct btrfs_path *search_path;
4788 u32 item_size = btrfs_item_size_nr(eb, slot);
4790 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4792 search_path = btrfs_alloc_path();
4795 search_path->search_commit_root = 1;
4796 search_path->skip_locking = 1;
4798 while (cur_offset < item_size) {
4802 unsigned long name_ptr;
4803 struct btrfs_dir_item *di;
4805 if (key->type == BTRFS_INODE_REF_KEY) {
4806 struct btrfs_inode_ref *iref;
4808 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4809 parent = key->offset;
4810 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4811 name_ptr = (unsigned long)(iref + 1);
4812 this_len = sizeof(*iref) + this_name_len;
4814 struct btrfs_inode_extref *extref;
4816 extref = (struct btrfs_inode_extref *)(ptr +
4818 parent = btrfs_inode_extref_parent(eb, extref);
4819 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4820 name_ptr = (unsigned long)&extref->name;
4821 this_len = sizeof(*extref) + this_name_len;
4824 if (this_name_len > name_len) {
4827 new_name = krealloc(name, this_name_len, GFP_NOFS);
4832 name_len = this_name_len;
4836 read_extent_buffer(eb, name, name_ptr, this_name_len);
4837 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4838 parent, name, this_name_len, 0);
4839 if (di && !IS_ERR(di)) {
4840 struct btrfs_key di_key;
4842 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4844 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4845 if (di_key.objectid != key->objectid) {
4847 *other_ino = di_key.objectid;
4848 *other_parent = parent;
4856 } else if (IS_ERR(di)) {
4860 btrfs_release_path(search_path);
4862 cur_offset += this_len;
4866 btrfs_free_path(search_path);
4871 struct btrfs_ino_list {
4874 struct list_head list;
4877 static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
4878 struct btrfs_root *root,
4879 struct btrfs_path *path,
4880 struct btrfs_log_ctx *ctx,
4881 u64 ino, u64 parent)
4883 struct btrfs_ino_list *ino_elem;
4884 LIST_HEAD(inode_list);
4887 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
4890 ino_elem->ino = ino;
4891 ino_elem->parent = parent;
4892 list_add_tail(&ino_elem->list, &inode_list);
4894 while (!list_empty(&inode_list)) {
4895 struct btrfs_fs_info *fs_info = root->fs_info;
4896 struct btrfs_key key;
4897 struct inode *inode;
4899 ino_elem = list_first_entry(&inode_list, struct btrfs_ino_list,
4901 ino = ino_elem->ino;
4902 parent = ino_elem->parent;
4903 list_del(&ino_elem->list);
4908 btrfs_release_path(path);
4910 inode = btrfs_iget(fs_info->sb, ino, root);
4912 * If the other inode that had a conflicting dir entry was
4913 * deleted in the current transaction, we need to log its parent
4916 if (IS_ERR(inode)) {
4917 ret = PTR_ERR(inode);
4918 if (ret == -ENOENT) {
4919 inode = btrfs_iget(fs_info->sb, parent, root);
4920 if (IS_ERR(inode)) {
4921 ret = PTR_ERR(inode);
4923 ret = btrfs_log_inode(trans, root,
4925 LOG_OTHER_INODE_ALL,
4927 btrfs_add_delayed_iput(inode);
4933 * If the inode was already logged skip it - otherwise we can
4934 * hit an infinite loop. Example:
4936 * From the commit root (previous transaction) we have the
4939 * inode 257 a directory
4940 * inode 258 with references "zz" and "zz_link" on inode 257
4941 * inode 259 with reference "a" on inode 257
4943 * And in the current (uncommitted) transaction we have:
4945 * inode 257 a directory, unchanged
4946 * inode 258 with references "a" and "a2" on inode 257
4947 * inode 259 with reference "zz_link" on inode 257
4948 * inode 261 with reference "zz" on inode 257
4950 * When logging inode 261 the following infinite loop could
4951 * happen if we don't skip already logged inodes:
4953 * - we detect inode 258 as a conflicting inode, with inode 261
4954 * on reference "zz", and log it;
4956 * - we detect inode 259 as a conflicting inode, with inode 258
4957 * on reference "a", and log it;
4959 * - we detect inode 258 as a conflicting inode, with inode 259
4960 * on reference "zz_link", and log it - again! After this we
4961 * repeat the above steps forever.
4963 spin_lock(&BTRFS_I(inode)->lock);
4965 * Check the inode's logged_trans only instead of
4966 * btrfs_inode_in_log(). This is because the last_log_commit of
4967 * the inode is not updated when we only log that it exists and
4968 * it has the full sync bit set (see btrfs_log_inode()).
4970 if (BTRFS_I(inode)->logged_trans == trans->transid) {
4971 spin_unlock(&BTRFS_I(inode)->lock);
4972 btrfs_add_delayed_iput(inode);
4975 spin_unlock(&BTRFS_I(inode)->lock);
4977 * We are safe logging the other inode without acquiring its
4978 * lock as long as we log with the LOG_INODE_EXISTS mode. We
4979 * are safe against concurrent renames of the other inode as
4980 * well because during a rename we pin the log and update the
4981 * log with the new name before we unpin it.
4983 ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
4984 LOG_OTHER_INODE, ctx);
4986 btrfs_add_delayed_iput(inode);
4991 key.type = BTRFS_INODE_REF_KEY;
4993 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4995 btrfs_add_delayed_iput(inode);
5000 struct extent_buffer *leaf = path->nodes[0];
5001 int slot = path->slots[0];
5003 u64 other_parent = 0;
5005 if (slot >= btrfs_header_nritems(leaf)) {
5006 ret = btrfs_next_leaf(root, path);
5009 } else if (ret > 0) {
5016 btrfs_item_key_to_cpu(leaf, &key, slot);
5017 if (key.objectid != ino ||
5018 (key.type != BTRFS_INODE_REF_KEY &&
5019 key.type != BTRFS_INODE_EXTREF_KEY)) {
5024 ret = btrfs_check_ref_name_override(leaf, slot, &key,
5025 BTRFS_I(inode), &other_ino,
5030 ino_elem = kmalloc(sizeof(*ino_elem), GFP_NOFS);
5035 ino_elem->ino = other_ino;
5036 ino_elem->parent = other_parent;
5037 list_add_tail(&ino_elem->list, &inode_list);
5042 btrfs_add_delayed_iput(inode);
5048 static int copy_inode_items_to_log(struct btrfs_trans_handle *trans,
5049 struct btrfs_inode *inode,
5050 struct btrfs_key *min_key,
5051 const struct btrfs_key *max_key,
5052 struct btrfs_path *path,
5053 struct btrfs_path *dst_path,
5054 const u64 logged_isize,
5055 const bool recursive_logging,
5056 const int inode_only,
5057 struct btrfs_log_ctx *ctx,
5058 bool *need_log_inode_item)
5060 struct btrfs_root *root = inode->root;
5061 int ins_start_slot = 0;
5066 ret = btrfs_search_forward(root, min_key, path, trans->transid);
5074 /* Note, ins_nr might be > 0 here, cleanup outside the loop */
5075 if (min_key->objectid != max_key->objectid)
5077 if (min_key->type > max_key->type)
5080 if (min_key->type == BTRFS_INODE_ITEM_KEY)
5081 *need_log_inode_item = false;
5083 if ((min_key->type == BTRFS_INODE_REF_KEY ||
5084 min_key->type == BTRFS_INODE_EXTREF_KEY) &&
5085 inode->generation == trans->transid &&
5086 !recursive_logging) {
5088 u64 other_parent = 0;
5090 ret = btrfs_check_ref_name_override(path->nodes[0],
5091 path->slots[0], min_key, inode,
5092 &other_ino, &other_parent);
5095 } else if (ret > 0 && ctx &&
5096 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
5101 ins_start_slot = path->slots[0];
5103 ret = copy_items(trans, inode, dst_path, path,
5104 ins_start_slot, ins_nr,
5105 inode_only, logged_isize);
5110 ret = log_conflicting_inodes(trans, root, path,
5111 ctx, other_ino, other_parent);
5114 btrfs_release_path(path);
5119 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5120 if (min_key->type == BTRFS_XATTR_ITEM_KEY) {
5123 ret = copy_items(trans, inode, dst_path, path,
5125 ins_nr, inode_only, logged_isize);
5132 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5135 } else if (!ins_nr) {
5136 ins_start_slot = path->slots[0];
5141 ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
5142 ins_nr, inode_only, logged_isize);
5146 ins_start_slot = path->slots[0];
5149 if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
5150 btrfs_item_key_to_cpu(path->nodes[0], min_key,
5155 ret = copy_items(trans, inode, dst_path, path,
5156 ins_start_slot, ins_nr, inode_only,
5162 btrfs_release_path(path);
5164 if (min_key->offset < (u64)-1) {
5166 } else if (min_key->type < max_key->type) {
5168 min_key->offset = 0;
5174 ret = copy_items(trans, inode, dst_path, path, ins_start_slot,
5175 ins_nr, inode_only, logged_isize);
5180 /* log a single inode in the tree log.
5181 * At least one parent directory for this inode must exist in the tree
5182 * or be logged already.
5184 * Any items from this inode changed by the current transaction are copied
5185 * to the log tree. An extra reference is taken on any extents in this
5186 * file, allowing us to avoid a whole pile of corner cases around logging
5187 * blocks that have been removed from the tree.
5189 * See LOG_INODE_ALL and related defines for a description of what inode_only
5192 * This handles both files and directories.
5194 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
5195 struct btrfs_root *root, struct btrfs_inode *inode,
5197 struct btrfs_log_ctx *ctx)
5199 struct btrfs_path *path;
5200 struct btrfs_path *dst_path;
5201 struct btrfs_key min_key;
5202 struct btrfs_key max_key;
5203 struct btrfs_root *log = root->log_root;
5206 bool fast_search = false;
5207 u64 ino = btrfs_ino(inode);
5208 struct extent_map_tree *em_tree = &inode->extent_tree;
5209 u64 logged_isize = 0;
5210 bool need_log_inode_item = true;
5211 bool xattrs_logged = false;
5212 bool recursive_logging = false;
5214 path = btrfs_alloc_path();
5217 dst_path = btrfs_alloc_path();
5219 btrfs_free_path(path);
5223 min_key.objectid = ino;
5224 min_key.type = BTRFS_INODE_ITEM_KEY;
5227 max_key.objectid = ino;
5230 /* today the code can only do partial logging of directories */
5231 if (S_ISDIR(inode->vfs_inode.i_mode) ||
5232 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5233 &inode->runtime_flags) &&
5234 inode_only >= LOG_INODE_EXISTS))
5235 max_key.type = BTRFS_XATTR_ITEM_KEY;
5237 max_key.type = (u8)-1;
5238 max_key.offset = (u64)-1;
5241 * Only run delayed items if we are a directory. We want to make sure
5242 * all directory indexes hit the fs/subvolume tree so we can find them
5243 * and figure out which index ranges have to be logged.
5245 * Otherwise commit the delayed inode only if the full sync flag is set,
5246 * as we want to make sure an up to date version is in the subvolume
5247 * tree so copy_inode_items_to_log() / copy_items() can find it and copy
5248 * it to the log tree. For a non full sync, we always log the inode item
5249 * based on the in-memory struct btrfs_inode which is always up to date.
5251 if (S_ISDIR(inode->vfs_inode.i_mode))
5252 ret = btrfs_commit_inode_delayed_items(trans, inode);
5253 else if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags))
5254 ret = btrfs_commit_inode_delayed_inode(inode);
5257 btrfs_free_path(path);
5258 btrfs_free_path(dst_path);
5262 if (inode_only == LOG_OTHER_INODE || inode_only == LOG_OTHER_INODE_ALL) {
5263 recursive_logging = true;
5264 if (inode_only == LOG_OTHER_INODE)
5265 inode_only = LOG_INODE_EXISTS;
5267 inode_only = LOG_INODE_ALL;
5268 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
5270 mutex_lock(&inode->log_mutex);
5274 * a brute force approach to making sure we get the most uptodate
5275 * copies of everything.
5277 if (S_ISDIR(inode->vfs_inode.i_mode)) {
5278 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
5280 if (inode_only == LOG_INODE_EXISTS)
5281 max_key_type = BTRFS_XATTR_ITEM_KEY;
5282 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
5284 if (inode_only == LOG_INODE_EXISTS) {
5286 * Make sure the new inode item we write to the log has
5287 * the same isize as the current one (if it exists).
5288 * This is necessary to prevent data loss after log
5289 * replay, and also to prevent doing a wrong expanding
5290 * truncate - for e.g. create file, write 4K into offset
5291 * 0, fsync, write 4K into offset 4096, add hard link,
5292 * fsync some other file (to sync log), power fail - if
5293 * we use the inode's current i_size, after log replay
5294 * we get a 8Kb file, with the last 4Kb extent as a hole
5295 * (zeroes), as if an expanding truncate happened,
5296 * instead of getting a file of 4Kb only.
5298 err = logged_inode_size(log, inode, path, &logged_isize);
5302 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5303 &inode->runtime_flags)) {
5304 if (inode_only == LOG_INODE_EXISTS) {
5305 max_key.type = BTRFS_XATTR_ITEM_KEY;
5306 ret = drop_objectid_items(trans, log, path, ino,
5309 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5310 &inode->runtime_flags);
5311 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
5312 &inode->runtime_flags);
5314 ret = btrfs_truncate_inode_items(trans,
5320 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
5321 &inode->runtime_flags) ||
5322 inode_only == LOG_INODE_EXISTS) {
5323 if (inode_only == LOG_INODE_ALL)
5325 max_key.type = BTRFS_XATTR_ITEM_KEY;
5326 ret = drop_objectid_items(trans, log, path, ino,
5329 if (inode_only == LOG_INODE_ALL)
5340 err = copy_inode_items_to_log(trans, inode, &min_key, &max_key,
5341 path, dst_path, logged_isize,
5342 recursive_logging, inode_only, ctx,
5343 &need_log_inode_item);
5347 btrfs_release_path(path);
5348 btrfs_release_path(dst_path);
5349 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
5352 xattrs_logged = true;
5353 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5354 btrfs_release_path(path);
5355 btrfs_release_path(dst_path);
5356 err = btrfs_log_holes(trans, root, inode, path);
5361 btrfs_release_path(path);
5362 btrfs_release_path(dst_path);
5363 if (need_log_inode_item) {
5364 err = log_inode_item(trans, log, dst_path, inode);
5365 if (!err && !xattrs_logged) {
5366 err = btrfs_log_all_xattrs(trans, root, inode, path,
5368 btrfs_release_path(path);
5374 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
5380 } else if (inode_only == LOG_INODE_ALL) {
5381 struct extent_map *em, *n;
5383 write_lock(&em_tree->lock);
5384 list_for_each_entry_safe(em, n, &em_tree->modified_extents, list)
5385 list_del_init(&em->list);
5386 write_unlock(&em_tree->lock);
5389 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5390 ret = log_directory_changes(trans, root, inode, path, dst_path,
5399 * If we are logging that an ancestor inode exists as part of logging a
5400 * new name from a link or rename operation, don't mark the inode as
5401 * logged - otherwise if an explicit fsync is made against an ancestor,
5402 * the fsync considers the inode in the log and doesn't sync the log,
5403 * resulting in the ancestor missing after a power failure unless the
5404 * log was synced as part of an fsync against any other unrelated inode.
5405 * So keep it simple for this case and just don't flag the ancestors as
5409 !(S_ISDIR(inode->vfs_inode.i_mode) && ctx->logging_new_name &&
5410 &inode->vfs_inode != ctx->inode)) {
5411 spin_lock(&inode->lock);
5412 inode->logged_trans = trans->transid;
5414 * Don't update last_log_commit if we logged that an inode exists
5415 * after it was loaded to memory (full_sync bit set).
5416 * This is to prevent data loss when we do a write to the inode,
5417 * then the inode gets evicted after all delalloc was flushed,
5418 * then we log it exists (due to a rename for example) and then
5419 * fsync it. This last fsync would do nothing (not logging the
5420 * extents previously written).
5422 if (inode_only != LOG_INODE_EXISTS ||
5423 !test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags))
5424 inode->last_log_commit = inode->last_sub_trans;
5425 spin_unlock(&inode->lock);
5428 mutex_unlock(&inode->log_mutex);
5430 btrfs_free_path(path);
5431 btrfs_free_path(dst_path);
5436 * Check if we must fallback to a transaction commit when logging an inode.
5437 * This must be called after logging the inode and is used only in the context
5438 * when fsyncing an inode requires the need to log some other inode - in which
5439 * case we can't lock the i_mutex of each other inode we need to log as that
5440 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5441 * log inodes up or down in the hierarchy) or rename operations for example. So
5442 * we take the log_mutex of the inode after we have logged it and then check for
5443 * its last_unlink_trans value - this is safe because any task setting
5444 * last_unlink_trans must take the log_mutex and it must do this before it does
5445 * the actual unlink operation, so if we do this check before a concurrent task
5446 * sets last_unlink_trans it means we've logged a consistent version/state of
5447 * all the inode items, otherwise we are not sure and must do a transaction
5448 * commit (the concurrent task might have only updated last_unlink_trans before
5449 * we logged the inode or it might have also done the unlink).
5451 static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5452 struct btrfs_inode *inode)
5454 struct btrfs_fs_info *fs_info = inode->root->fs_info;
5457 mutex_lock(&inode->log_mutex);
5458 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
5460 * Make sure any commits to the log are forced to be full
5463 btrfs_set_log_full_commit(trans);
5466 mutex_unlock(&inode->log_mutex);
5472 * follow the dentry parent pointers up the chain and see if any
5473 * of the directories in it require a full commit before they can
5474 * be logged. Returns zero if nothing special needs to be done or 1 if
5475 * a full commit is required.
5477 static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5478 struct btrfs_inode *inode,
5479 struct dentry *parent,
5480 struct super_block *sb,
5484 struct dentry *old_parent = NULL;
5487 * for regular files, if its inode is already on disk, we don't
5488 * have to worry about the parents at all. This is because
5489 * we can use the last_unlink_trans field to record renames
5490 * and other fun in this file.
5492 if (S_ISREG(inode->vfs_inode.i_mode) &&
5493 inode->generation <= last_committed &&
5494 inode->last_unlink_trans <= last_committed)
5497 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
5498 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5500 inode = BTRFS_I(d_inode(parent));
5504 if (btrfs_must_commit_transaction(trans, inode)) {
5509 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5512 if (IS_ROOT(parent)) {
5513 inode = BTRFS_I(d_inode(parent));
5514 if (btrfs_must_commit_transaction(trans, inode))
5519 parent = dget_parent(parent);
5521 old_parent = parent;
5522 inode = BTRFS_I(d_inode(parent));
5530 struct btrfs_dir_list {
5532 struct list_head list;
5536 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5537 * details about the why it is needed.
5538 * This is a recursive operation - if an existing dentry corresponds to a
5539 * directory, that directory's new entries are logged too (same behaviour as
5540 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5541 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5542 * complains about the following circular lock dependency / possible deadlock:
5546 * lock(&type->i_mutex_dir_key#3/2);
5547 * lock(sb_internal#2);
5548 * lock(&type->i_mutex_dir_key#3/2);
5549 * lock(&sb->s_type->i_mutex_key#14);
5551 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5552 * sb_start_intwrite() in btrfs_start_transaction().
5553 * Not locking i_mutex of the inodes is still safe because:
5555 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5556 * that while logging the inode new references (names) are added or removed
5557 * from the inode, leaving the logged inode item with a link count that does
5558 * not match the number of logged inode reference items. This is fine because
5559 * at log replay time we compute the real number of links and correct the
5560 * link count in the inode item (see replay_one_buffer() and
5561 * link_to_fixup_dir());
5563 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5564 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5565 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5566 * has a size that doesn't match the sum of the lengths of all the logged
5567 * names. This does not result in a problem because if a dir_item key is
5568 * logged but its matching dir_index key is not logged, at log replay time we
5569 * don't use it to replay the respective name (see replay_one_name()). On the
5570 * other hand if only the dir_index key ends up being logged, the respective
5571 * name is added to the fs/subvol tree with both the dir_item and dir_index
5572 * keys created (see replay_one_name()).
5573 * The directory's inode item with a wrong i_size is not a problem as well,
5574 * since we don't use it at log replay time to set the i_size in the inode
5575 * item of the fs/subvol tree (see overwrite_item()).
5577 static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5578 struct btrfs_root *root,
5579 struct btrfs_inode *start_inode,
5580 struct btrfs_log_ctx *ctx)
5582 struct btrfs_fs_info *fs_info = root->fs_info;
5583 struct btrfs_root *log = root->log_root;
5584 struct btrfs_path *path;
5585 LIST_HEAD(dir_list);
5586 struct btrfs_dir_list *dir_elem;
5589 path = btrfs_alloc_path();
5593 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5595 btrfs_free_path(path);
5598 dir_elem->ino = btrfs_ino(start_inode);
5599 list_add_tail(&dir_elem->list, &dir_list);
5601 while (!list_empty(&dir_list)) {
5602 struct extent_buffer *leaf;
5603 struct btrfs_key min_key;
5607 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5610 goto next_dir_inode;
5612 min_key.objectid = dir_elem->ino;
5613 min_key.type = BTRFS_DIR_ITEM_KEY;
5616 btrfs_release_path(path);
5617 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5619 goto next_dir_inode;
5620 } else if (ret > 0) {
5622 goto next_dir_inode;
5626 leaf = path->nodes[0];
5627 nritems = btrfs_header_nritems(leaf);
5628 for (i = path->slots[0]; i < nritems; i++) {
5629 struct btrfs_dir_item *di;
5630 struct btrfs_key di_key;
5631 struct inode *di_inode;
5632 struct btrfs_dir_list *new_dir_elem;
5633 int log_mode = LOG_INODE_EXISTS;
5636 btrfs_item_key_to_cpu(leaf, &min_key, i);
5637 if (min_key.objectid != dir_elem->ino ||
5638 min_key.type != BTRFS_DIR_ITEM_KEY)
5639 goto next_dir_inode;
5641 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5642 type = btrfs_dir_type(leaf, di);
5643 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5644 type != BTRFS_FT_DIR)
5646 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5647 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5650 btrfs_release_path(path);
5651 di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root);
5652 if (IS_ERR(di_inode)) {
5653 ret = PTR_ERR(di_inode);
5654 goto next_dir_inode;
5657 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
5658 btrfs_add_delayed_iput(di_inode);
5662 ctx->log_new_dentries = false;
5663 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
5664 log_mode = LOG_INODE_ALL;
5665 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
5668 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
5670 btrfs_add_delayed_iput(di_inode);
5672 goto next_dir_inode;
5673 if (ctx->log_new_dentries) {
5674 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5676 if (!new_dir_elem) {
5678 goto next_dir_inode;
5680 new_dir_elem->ino = di_key.objectid;
5681 list_add_tail(&new_dir_elem->list, &dir_list);
5686 ret = btrfs_next_leaf(log, path);
5688 goto next_dir_inode;
5689 } else if (ret > 0) {
5691 goto next_dir_inode;
5695 if (min_key.offset < (u64)-1) {
5700 list_del(&dir_elem->list);
5704 btrfs_free_path(path);
5708 static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5709 struct btrfs_inode *inode,
5710 struct btrfs_log_ctx *ctx)
5712 struct btrfs_fs_info *fs_info = trans->fs_info;
5714 struct btrfs_path *path;
5715 struct btrfs_key key;
5716 struct btrfs_root *root = inode->root;
5717 const u64 ino = btrfs_ino(inode);
5719 path = btrfs_alloc_path();
5722 path->skip_locking = 1;
5723 path->search_commit_root = 1;
5726 key.type = BTRFS_INODE_REF_KEY;
5728 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5733 struct extent_buffer *leaf = path->nodes[0];
5734 int slot = path->slots[0];
5739 if (slot >= btrfs_header_nritems(leaf)) {
5740 ret = btrfs_next_leaf(root, path);
5748 btrfs_item_key_to_cpu(leaf, &key, slot);
5749 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5750 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5753 item_size = btrfs_item_size_nr(leaf, slot);
5754 ptr = btrfs_item_ptr_offset(leaf, slot);
5755 while (cur_offset < item_size) {
5756 struct btrfs_key inode_key;
5757 struct inode *dir_inode;
5759 inode_key.type = BTRFS_INODE_ITEM_KEY;
5760 inode_key.offset = 0;
5762 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5763 struct btrfs_inode_extref *extref;
5765 extref = (struct btrfs_inode_extref *)
5767 inode_key.objectid = btrfs_inode_extref_parent(
5769 cur_offset += sizeof(*extref);
5770 cur_offset += btrfs_inode_extref_name_len(leaf,
5773 inode_key.objectid = key.offset;
5774 cur_offset = item_size;
5777 dir_inode = btrfs_iget(fs_info->sb, inode_key.objectid,
5780 * If the parent inode was deleted, return an error to
5781 * fallback to a transaction commit. This is to prevent
5782 * getting an inode that was moved from one parent A to
5783 * a parent B, got its former parent A deleted and then
5784 * it got fsync'ed, from existing at both parents after
5785 * a log replay (and the old parent still existing).
5792 * mv /mnt/B/bar /mnt/A/bar
5793 * mv -T /mnt/A /mnt/B
5797 * If we ignore the old parent B which got deleted,
5798 * after a log replay we would have file bar linked
5799 * at both parents and the old parent B would still
5802 if (IS_ERR(dir_inode)) {
5803 ret = PTR_ERR(dir_inode);
5808 ctx->log_new_dentries = false;
5809 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
5810 LOG_INODE_ALL, ctx);
5812 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
5814 if (!ret && ctx && ctx->log_new_dentries)
5815 ret = log_new_dir_dentries(trans, root,
5816 BTRFS_I(dir_inode), ctx);
5817 btrfs_add_delayed_iput(dir_inode);
5825 btrfs_free_path(path);
5829 static int log_new_ancestors(struct btrfs_trans_handle *trans,
5830 struct btrfs_root *root,
5831 struct btrfs_path *path,
5832 struct btrfs_log_ctx *ctx)
5834 struct btrfs_key found_key;
5836 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
5839 struct btrfs_fs_info *fs_info = root->fs_info;
5840 const u64 last_committed = fs_info->last_trans_committed;
5841 struct extent_buffer *leaf = path->nodes[0];
5842 int slot = path->slots[0];
5843 struct btrfs_key search_key;
5844 struct inode *inode;
5848 btrfs_release_path(path);
5850 ino = found_key.offset;
5852 search_key.objectid = found_key.offset;
5853 search_key.type = BTRFS_INODE_ITEM_KEY;
5854 search_key.offset = 0;
5855 inode = btrfs_iget(fs_info->sb, ino, root);
5857 return PTR_ERR(inode);
5859 if (BTRFS_I(inode)->generation > last_committed)
5860 ret = btrfs_log_inode(trans, root, BTRFS_I(inode),
5861 LOG_INODE_EXISTS, ctx);
5862 btrfs_add_delayed_iput(inode);
5866 if (search_key.objectid == BTRFS_FIRST_FREE_OBJECTID)
5869 search_key.type = BTRFS_INODE_REF_KEY;
5870 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
5874 leaf = path->nodes[0];
5875 slot = path->slots[0];
5876 if (slot >= btrfs_header_nritems(leaf)) {
5877 ret = btrfs_next_leaf(root, path);
5882 leaf = path->nodes[0];
5883 slot = path->slots[0];
5886 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5887 if (found_key.objectid != search_key.objectid ||
5888 found_key.type != BTRFS_INODE_REF_KEY)
5894 static int log_new_ancestors_fast(struct btrfs_trans_handle *trans,
5895 struct btrfs_inode *inode,
5896 struct dentry *parent,
5897 struct btrfs_log_ctx *ctx)
5899 struct btrfs_root *root = inode->root;
5900 struct btrfs_fs_info *fs_info = root->fs_info;
5901 struct dentry *old_parent = NULL;
5902 struct super_block *sb = inode->vfs_inode.i_sb;
5906 if (!parent || d_really_is_negative(parent) ||
5910 inode = BTRFS_I(d_inode(parent));
5911 if (root != inode->root)
5914 if (inode->generation > fs_info->last_trans_committed) {
5915 ret = btrfs_log_inode(trans, root, inode,
5916 LOG_INODE_EXISTS, ctx);
5920 if (IS_ROOT(parent))
5923 parent = dget_parent(parent);
5925 old_parent = parent;
5932 static int log_all_new_ancestors(struct btrfs_trans_handle *trans,
5933 struct btrfs_inode *inode,
5934 struct dentry *parent,
5935 struct btrfs_log_ctx *ctx)
5937 struct btrfs_root *root = inode->root;
5938 const u64 ino = btrfs_ino(inode);
5939 struct btrfs_path *path;
5940 struct btrfs_key search_key;
5944 * For a single hard link case, go through a fast path that does not
5945 * need to iterate the fs/subvolume tree.
5947 if (inode->vfs_inode.i_nlink < 2)
5948 return log_new_ancestors_fast(trans, inode, parent, ctx);
5950 path = btrfs_alloc_path();
5954 search_key.objectid = ino;
5955 search_key.type = BTRFS_INODE_REF_KEY;
5956 search_key.offset = 0;
5958 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
5965 struct extent_buffer *leaf = path->nodes[0];
5966 int slot = path->slots[0];
5967 struct btrfs_key found_key;
5969 if (slot >= btrfs_header_nritems(leaf)) {
5970 ret = btrfs_next_leaf(root, path);
5978 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5979 if (found_key.objectid != ino ||
5980 found_key.type > BTRFS_INODE_EXTREF_KEY)
5984 * Don't deal with extended references because they are rare
5985 * cases and too complex to deal with (we would need to keep
5986 * track of which subitem we are processing for each item in
5987 * this loop, etc). So just return some error to fallback to
5988 * a transaction commit.
5990 if (found_key.type == BTRFS_INODE_EXTREF_KEY) {
5996 * Logging ancestors needs to do more searches on the fs/subvol
5997 * tree, so it releases the path as needed to avoid deadlocks.
5998 * Keep track of the last inode ref key and resume from that key
5999 * after logging all new ancestors for the current hard link.
6001 memcpy(&search_key, &found_key, sizeof(search_key));
6003 ret = log_new_ancestors(trans, root, path, ctx);
6006 btrfs_release_path(path);
6011 btrfs_free_path(path);
6016 * helper function around btrfs_log_inode to make sure newly created
6017 * parent directories also end up in the log. A minimal inode and backref
6018 * only logging is done of any parent directories that are older than
6019 * the last committed transaction
6021 static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
6022 struct btrfs_inode *inode,
6023 struct dentry *parent,
6025 struct btrfs_log_ctx *ctx)
6027 struct btrfs_root *root = inode->root;
6028 struct btrfs_fs_info *fs_info = root->fs_info;
6029 struct super_block *sb;
6031 u64 last_committed = fs_info->last_trans_committed;
6032 bool log_dentries = false;
6034 sb = inode->vfs_inode.i_sb;
6036 if (btrfs_test_opt(fs_info, NOTREELOG)) {
6042 * The prev transaction commit doesn't complete, we need do
6043 * full commit by ourselves.
6045 if (fs_info->last_trans_log_full_commit >
6046 fs_info->last_trans_committed) {
6051 if (btrfs_root_refs(&root->root_item) == 0) {
6056 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
6062 * Skip already logged inodes or inodes corresponding to tmpfiles
6063 * (since logging them is pointless, a link count of 0 means they
6064 * will never be accessible).
6066 if (btrfs_inode_in_log(inode, trans->transid) ||
6067 inode->vfs_inode.i_nlink == 0) {
6068 ret = BTRFS_NO_LOG_SYNC;
6072 ret = start_log_trans(trans, root, ctx);
6076 ret = btrfs_log_inode(trans, root, inode, inode_only, ctx);
6081 * for regular files, if its inode is already on disk, we don't
6082 * have to worry about the parents at all. This is because
6083 * we can use the last_unlink_trans field to record renames
6084 * and other fun in this file.
6086 if (S_ISREG(inode->vfs_inode.i_mode) &&
6087 inode->generation <= last_committed &&
6088 inode->last_unlink_trans <= last_committed) {
6093 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
6094 log_dentries = true;
6097 * On unlink we must make sure all our current and old parent directory
6098 * inodes are fully logged. This is to prevent leaving dangling
6099 * directory index entries in directories that were our parents but are
6100 * not anymore. Not doing this results in old parent directory being
6101 * impossible to delete after log replay (rmdir will always fail with
6102 * error -ENOTEMPTY).
6108 * ln testdir/foo testdir/bar
6110 * unlink testdir/bar
6111 * xfs_io -c fsync testdir/foo
6113 * mount fs, triggers log replay
6115 * If we don't log the parent directory (testdir), after log replay the
6116 * directory still has an entry pointing to the file inode using the bar
6117 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
6118 * the file inode has a link count of 1.
6124 * ln foo testdir/foo2
6125 * ln foo testdir/foo3
6127 * unlink testdir/foo3
6128 * xfs_io -c fsync foo
6130 * mount fs, triggers log replay
6132 * Similar as the first example, after log replay the parent directory
6133 * testdir still has an entry pointing to the inode file with name foo3
6134 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
6135 * and has a link count of 2.
6137 if (inode->last_unlink_trans > last_committed) {
6138 ret = btrfs_log_all_parents(trans, inode, ctx);
6143 ret = log_all_new_ancestors(trans, inode, parent, ctx);
6148 ret = log_new_dir_dentries(trans, root, inode, ctx);
6153 btrfs_set_log_full_commit(trans);
6158 btrfs_remove_log_ctx(root, ctx);
6159 btrfs_end_log_trans(root);
6165 * it is not safe to log dentry if the chunk root has added new
6166 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
6167 * If this returns 1, you must commit the transaction to safely get your
6170 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
6171 struct dentry *dentry,
6172 struct btrfs_log_ctx *ctx)
6174 struct dentry *parent = dget_parent(dentry);
6177 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
6178 LOG_INODE_ALL, ctx);
6185 * should be called during mount to recover any replay any log trees
6188 int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
6191 struct btrfs_path *path;
6192 struct btrfs_trans_handle *trans;
6193 struct btrfs_key key;
6194 struct btrfs_key found_key;
6195 struct btrfs_root *log;
6196 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
6197 struct walk_control wc = {
6198 .process_func = process_one_buffer,
6199 .stage = LOG_WALK_PIN_ONLY,
6202 path = btrfs_alloc_path();
6206 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
6208 trans = btrfs_start_transaction(fs_info->tree_root, 0);
6209 if (IS_ERR(trans)) {
6210 ret = PTR_ERR(trans);
6217 ret = walk_log_tree(trans, log_root_tree, &wc);
6219 btrfs_handle_fs_error(fs_info, ret,
6220 "Failed to pin buffers while recovering log root tree.");
6225 key.objectid = BTRFS_TREE_LOG_OBJECTID;
6226 key.offset = (u64)-1;
6227 key.type = BTRFS_ROOT_ITEM_KEY;
6230 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
6233 btrfs_handle_fs_error(fs_info, ret,
6234 "Couldn't find tree log root.");
6238 if (path->slots[0] == 0)
6242 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
6244 btrfs_release_path(path);
6245 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
6248 log = btrfs_read_tree_root(log_root_tree, &found_key);
6251 btrfs_handle_fs_error(fs_info, ret,
6252 "Couldn't read tree log root.");
6256 wc.replay_dest = btrfs_get_fs_root(fs_info, found_key.offset,
6258 if (IS_ERR(wc.replay_dest)) {
6259 ret = PTR_ERR(wc.replay_dest);
6262 * We didn't find the subvol, likely because it was
6263 * deleted. This is ok, simply skip this log and go to
6266 * We need to exclude the root because we can't have
6267 * other log replays overwriting this log as we'll read
6268 * it back in a few more times. This will keep our
6269 * block from being modified, and we'll just bail for
6270 * each subsequent pass.
6273 ret = btrfs_pin_extent_for_log_replay(trans,
6276 btrfs_put_root(log);
6280 btrfs_handle_fs_error(fs_info, ret,
6281 "Couldn't read target root for tree log recovery.");
6285 wc.replay_dest->log_root = log;
6286 btrfs_record_root_in_trans(trans, wc.replay_dest);
6287 ret = walk_log_tree(trans, log, &wc);
6289 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
6290 ret = fixup_inode_link_counts(trans, wc.replay_dest,
6294 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
6295 struct btrfs_root *root = wc.replay_dest;
6297 btrfs_release_path(path);
6300 * We have just replayed everything, and the highest
6301 * objectid of fs roots probably has changed in case
6302 * some inode_item's got replayed.
6304 * root->objectid_mutex is not acquired as log replay
6305 * could only happen during mount.
6307 ret = btrfs_find_highest_objectid(root,
6308 &root->highest_objectid);
6311 wc.replay_dest->log_root = NULL;
6312 btrfs_put_root(wc.replay_dest);
6313 btrfs_put_root(log);
6318 if (found_key.offset == 0)
6320 key.offset = found_key.offset - 1;
6322 btrfs_release_path(path);
6324 /* step one is to pin it all, step two is to replay just inodes */
6327 wc.process_func = replay_one_buffer;
6328 wc.stage = LOG_WALK_REPLAY_INODES;
6331 /* step three is to replay everything */
6332 if (wc.stage < LOG_WALK_REPLAY_ALL) {
6337 btrfs_free_path(path);
6339 /* step 4: commit the transaction, which also unpins the blocks */
6340 ret = btrfs_commit_transaction(trans);
6344 log_root_tree->log_root = NULL;
6345 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
6346 btrfs_put_root(log_root_tree);
6351 btrfs_end_transaction(wc.trans);
6352 btrfs_free_path(path);
6357 * there are some corner cases where we want to force a full
6358 * commit instead of allowing a directory to be logged.
6360 * They revolve around files there were unlinked from the directory, and
6361 * this function updates the parent directory so that a full commit is
6362 * properly done if it is fsync'd later after the unlinks are done.
6364 * Must be called before the unlink operations (updates to the subvolume tree,
6365 * inodes, etc) are done.
6367 void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
6368 struct btrfs_inode *dir, struct btrfs_inode *inode,
6372 * when we're logging a file, if it hasn't been renamed
6373 * or unlinked, and its inode is fully committed on disk,
6374 * we don't have to worry about walking up the directory chain
6375 * to log its parents.
6377 * So, we use the last_unlink_trans field to put this transid
6378 * into the file. When the file is logged we check it and
6379 * don't log the parents if the file is fully on disk.
6381 mutex_lock(&inode->log_mutex);
6382 inode->last_unlink_trans = trans->transid;
6383 mutex_unlock(&inode->log_mutex);
6386 * if this directory was already logged any new
6387 * names for this file/dir will get recorded
6389 if (dir->logged_trans == trans->transid)
6393 * if the inode we're about to unlink was logged,
6394 * the log will be properly updated for any new names
6396 if (inode->logged_trans == trans->transid)
6400 * when renaming files across directories, if the directory
6401 * there we're unlinking from gets fsync'd later on, there's
6402 * no way to find the destination directory later and fsync it
6403 * properly. So, we have to be conservative and force commits
6404 * so the new name gets discovered.
6409 /* we can safely do the unlink without any special recording */
6413 mutex_lock(&dir->log_mutex);
6414 dir->last_unlink_trans = trans->transid;
6415 mutex_unlock(&dir->log_mutex);
6419 * Make sure that if someone attempts to fsync the parent directory of a deleted
6420 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6421 * that after replaying the log tree of the parent directory's root we will not
6422 * see the snapshot anymore and at log replay time we will not see any log tree
6423 * corresponding to the deleted snapshot's root, which could lead to replaying
6424 * it after replaying the log tree of the parent directory (which would replay
6425 * the snapshot delete operation).
6427 * Must be called before the actual snapshot destroy operation (updates to the
6428 * parent root and tree of tree roots trees, etc) are done.
6430 void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
6431 struct btrfs_inode *dir)
6433 mutex_lock(&dir->log_mutex);
6434 dir->last_unlink_trans = trans->transid;
6435 mutex_unlock(&dir->log_mutex);
6439 * Call this after adding a new name for a file and it will properly
6440 * update the log to reflect the new name.
6442 void btrfs_log_new_name(struct btrfs_trans_handle *trans,
6443 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
6444 struct dentry *parent)
6446 struct btrfs_fs_info *fs_info = trans->fs_info;
6447 struct btrfs_log_ctx ctx;
6450 * this will force the logging code to walk the dentry chain
6453 if (!S_ISDIR(inode->vfs_inode.i_mode))
6454 inode->last_unlink_trans = trans->transid;
6457 * if this inode hasn't been logged and directory we're renaming it
6458 * from hasn't been logged, we don't need to log it
6460 if (inode->logged_trans <= fs_info->last_trans_committed &&
6461 (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
6464 btrfs_init_log_ctx(&ctx, &inode->vfs_inode);
6465 ctx.logging_new_name = true;
6467 * We don't care about the return value. If we fail to log the new name
6468 * then we know the next attempt to sync the log will fallback to a full
6469 * transaction commit (due to a call to btrfs_set_log_full_commit()), so
6470 * we don't need to worry about getting a log committed that has an
6471 * inconsistent state after a rename operation.
6473 btrfs_log_inode_parent(trans, inode, parent, LOG_INODE_EXISTS, &ctx);