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>
15 #include "print-tree.h"
17 #include "compression.h"
19 #include "inode-map.h"
21 /* magic values for the inode_only field in btrfs_log_inode:
23 * LOG_INODE_ALL means to log everything
24 * LOG_INODE_EXISTS means to log just enough to recreate the inode
27 #define LOG_INODE_ALL 0
28 #define LOG_INODE_EXISTS 1
29 #define LOG_OTHER_INODE 2
32 * directory trouble cases
34 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
35 * log, we must force a full commit before doing an fsync of the directory
36 * where the unlink was done.
37 * ---> record transid of last unlink/rename per directory
41 * rename foo/some_dir foo2/some_dir
43 * fsync foo/some_dir/some_file
45 * The fsync above will unlink the original some_dir without recording
46 * it in its new location (foo2). After a crash, some_dir will be gone
47 * unless the fsync of some_file forces a full commit
49 * 2) we must log any new names for any file or dir that is in the fsync
50 * log. ---> check inode while renaming/linking.
52 * 2a) we must log any new names for any file or dir during rename
53 * when the directory they are being removed from was logged.
54 * ---> check inode and old parent dir during rename
56 * 2a is actually the more important variant. With the extra logging
57 * a crash might unlink the old name without recreating the new one
59 * 3) after a crash, we must go through any directories with a link count
60 * of zero and redo the rm -rf
67 * The directory f1 was fully removed from the FS, but fsync was never
68 * called on f1, only its parent dir. After a crash the rm -rf must
69 * be replayed. This must be able to recurse down the entire
70 * directory tree. The inode link count fixup code takes care of the
75 * stages for the tree walking. The first
76 * stage (0) is to only pin down the blocks we find
77 * the second stage (1) is to make sure that all the inodes
78 * we find in the log are created in the subvolume.
80 * The last stage is to deal with directories and links and extents
81 * and all the other fun semantics
83 #define LOG_WALK_PIN_ONLY 0
84 #define LOG_WALK_REPLAY_INODES 1
85 #define LOG_WALK_REPLAY_DIR_INDEX 2
86 #define LOG_WALK_REPLAY_ALL 3
88 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
89 struct btrfs_root *root, struct btrfs_inode *inode,
93 struct btrfs_log_ctx *ctx);
94 static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
95 struct btrfs_root *root,
96 struct btrfs_path *path, u64 objectid);
97 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root,
99 struct btrfs_root *log,
100 struct btrfs_path *path,
101 u64 dirid, int del_all);
104 * tree logging is a special write ahead log used to make sure that
105 * fsyncs and O_SYNCs can happen without doing full tree commits.
107 * Full tree commits are expensive because they require commonly
108 * modified blocks to be recowed, creating many dirty pages in the
109 * extent tree an 4x-6x higher write load than ext3.
111 * Instead of doing a tree commit on every fsync, we use the
112 * key ranges and transaction ids to find items for a given file or directory
113 * that have changed in this transaction. Those items are copied into
114 * a special tree (one per subvolume root), that tree is written to disk
115 * and then the fsync is considered complete.
117 * After a crash, items are copied out of the log-tree back into the
118 * subvolume tree. Any file data extents found are recorded in the extent
119 * allocation tree, and the log-tree freed.
121 * The log tree is read three times, once to pin down all the extents it is
122 * using in ram and once, once to create all the inodes logged in the tree
123 * and once to do all the other items.
127 * start a sub transaction and setup the log tree
128 * this increments the log tree writer count to make the people
129 * syncing the tree wait for us to finish
131 static int start_log_trans(struct btrfs_trans_handle *trans,
132 struct btrfs_root *root,
133 struct btrfs_log_ctx *ctx)
135 struct btrfs_fs_info *fs_info = root->fs_info;
138 mutex_lock(&root->log_mutex);
140 if (root->log_root) {
141 if (btrfs_need_log_full_commit(fs_info, trans)) {
146 if (!root->log_start_pid) {
147 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
148 root->log_start_pid = current->pid;
149 } else if (root->log_start_pid != current->pid) {
150 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
153 mutex_lock(&fs_info->tree_log_mutex);
154 if (!fs_info->log_root_tree)
155 ret = btrfs_init_log_root_tree(trans, fs_info);
156 mutex_unlock(&fs_info->tree_log_mutex);
160 ret = btrfs_add_log_tree(trans, root);
164 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
165 root->log_start_pid = current->pid;
168 atomic_inc(&root->log_batch);
169 atomic_inc(&root->log_writers);
171 int index = root->log_transid % 2;
172 list_add_tail(&ctx->list, &root->log_ctxs[index]);
173 ctx->log_transid = root->log_transid;
177 mutex_unlock(&root->log_mutex);
182 * returns 0 if there was a log transaction running and we were able
183 * to join, or returns -ENOENT if there were not transactions
186 static int join_running_log_trans(struct btrfs_root *root)
194 mutex_lock(&root->log_mutex);
195 if (root->log_root) {
197 atomic_inc(&root->log_writers);
199 mutex_unlock(&root->log_mutex);
204 * This either makes the current running log transaction wait
205 * until you call btrfs_end_log_trans() or it makes any future
206 * log transactions wait until you call btrfs_end_log_trans()
208 int btrfs_pin_log_trans(struct btrfs_root *root)
212 mutex_lock(&root->log_mutex);
213 atomic_inc(&root->log_writers);
214 mutex_unlock(&root->log_mutex);
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);
232 * the walk control struct is used to pass state down the chain when
233 * processing the log tree. The stage field tells us which part
234 * of the log tree processing we are currently doing. The others
235 * are state fields used for that specific part
237 struct walk_control {
238 /* should we free the extent on disk when done? This is used
239 * at transaction commit time while freeing a log tree
243 /* should we write out the extent buffer? This is used
244 * while flushing the log tree to disk during a sync
248 /* should we wait for the extent buffer io to finish? Also used
249 * while flushing the log tree to disk for a sync
253 /* pin only walk, we record which extents on disk belong to the
258 /* what stage of the replay code we're currently in */
261 /* the root we are currently replaying */
262 struct btrfs_root *replay_dest;
264 /* the trans handle for the current replay */
265 struct btrfs_trans_handle *trans;
267 /* the function that gets used to process blocks we find in the
268 * tree. Note the extent_buffer might not be up to date when it is
269 * passed in, and it must be checked or read if you need the data
272 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
273 struct walk_control *wc, u64 gen, int level);
277 * process_func used to pin down extents, write them or wait on them
279 static int process_one_buffer(struct btrfs_root *log,
280 struct extent_buffer *eb,
281 struct walk_control *wc, u64 gen, int level)
283 struct btrfs_fs_info *fs_info = log->fs_info;
287 * If this fs is mixed then we need to be able to process the leaves to
288 * pin down any logged extents, so we have to read the block.
290 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
291 ret = btrfs_read_buffer(eb, gen, level, NULL);
297 ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
300 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
301 if (wc->pin && btrfs_header_level(eb) == 0)
302 ret = btrfs_exclude_logged_extents(fs_info, eb);
304 btrfs_write_tree_block(eb);
306 btrfs_wait_tree_block_writeback(eb);
312 * Item overwrite used by replay and tree logging. eb, slot and key all refer
313 * to the src data we are copying out.
315 * root is the tree we are copying into, and path is a scratch
316 * path for use in this function (it should be released on entry and
317 * will be released on exit).
319 * If the key is already in the destination tree the existing item is
320 * overwritten. If the existing item isn't big enough, it is extended.
321 * If it is too large, it is truncated.
323 * If the key isn't in the destination yet, a new item is inserted.
325 static noinline int overwrite_item(struct btrfs_trans_handle *trans,
326 struct btrfs_root *root,
327 struct btrfs_path *path,
328 struct extent_buffer *eb, int slot,
329 struct btrfs_key *key)
331 struct btrfs_fs_info *fs_info = root->fs_info;
334 u64 saved_i_size = 0;
335 int save_old_i_size = 0;
336 unsigned long src_ptr;
337 unsigned long dst_ptr;
338 int overwrite_root = 0;
339 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
341 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
344 item_size = btrfs_item_size_nr(eb, slot);
345 src_ptr = btrfs_item_ptr_offset(eb, slot);
347 /* look for the key in the destination tree */
348 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
355 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
357 if (dst_size != item_size)
360 if (item_size == 0) {
361 btrfs_release_path(path);
364 dst_copy = kmalloc(item_size, GFP_NOFS);
365 src_copy = kmalloc(item_size, GFP_NOFS);
366 if (!dst_copy || !src_copy) {
367 btrfs_release_path(path);
373 read_extent_buffer(eb, src_copy, src_ptr, item_size);
375 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
376 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
378 ret = memcmp(dst_copy, src_copy, item_size);
383 * they have the same contents, just return, this saves
384 * us from cowing blocks in the destination tree and doing
385 * extra writes that may not have been done by a previous
389 btrfs_release_path(path);
394 * We need to load the old nbytes into the inode so when we
395 * replay the extents we've logged we get the right nbytes.
398 struct btrfs_inode_item *item;
402 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
403 struct btrfs_inode_item);
404 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
405 item = btrfs_item_ptr(eb, slot,
406 struct btrfs_inode_item);
407 btrfs_set_inode_nbytes(eb, item, nbytes);
410 * If this is a directory we need to reset the i_size to
411 * 0 so that we can set it up properly when replaying
412 * the rest of the items in this log.
414 mode = btrfs_inode_mode(eb, item);
416 btrfs_set_inode_size(eb, item, 0);
418 } else if (inode_item) {
419 struct btrfs_inode_item *item;
423 * New inode, set nbytes to 0 so that the nbytes comes out
424 * properly when we replay the extents.
426 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
427 btrfs_set_inode_nbytes(eb, item, 0);
430 * If this is a directory we need to reset the i_size to 0 so
431 * that we can set it up properly when replaying the rest of
432 * the items in this log.
434 mode = btrfs_inode_mode(eb, item);
436 btrfs_set_inode_size(eb, item, 0);
439 btrfs_release_path(path);
440 /* try to insert the key into the destination tree */
441 path->skip_release_on_error = 1;
442 ret = btrfs_insert_empty_item(trans, root, path,
444 path->skip_release_on_error = 0;
446 /* make sure any existing item is the correct size */
447 if (ret == -EEXIST || ret == -EOVERFLOW) {
449 found_size = btrfs_item_size_nr(path->nodes[0],
451 if (found_size > item_size)
452 btrfs_truncate_item(fs_info, path, item_size, 1);
453 else if (found_size < item_size)
454 btrfs_extend_item(fs_info, path,
455 item_size - found_size);
459 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
462 /* don't overwrite an existing inode if the generation number
463 * was logged as zero. This is done when the tree logging code
464 * is just logging an inode to make sure it exists after recovery.
466 * Also, don't overwrite i_size on directories during replay.
467 * log replay inserts and removes directory items based on the
468 * state of the tree found in the subvolume, and i_size is modified
471 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
472 struct btrfs_inode_item *src_item;
473 struct btrfs_inode_item *dst_item;
475 src_item = (struct btrfs_inode_item *)src_ptr;
476 dst_item = (struct btrfs_inode_item *)dst_ptr;
478 if (btrfs_inode_generation(eb, src_item) == 0) {
479 struct extent_buffer *dst_eb = path->nodes[0];
480 const u64 ino_size = btrfs_inode_size(eb, src_item);
483 * For regular files an ino_size == 0 is used only when
484 * logging that an inode exists, as part of a directory
485 * fsync, and the inode wasn't fsynced before. In this
486 * case don't set the size of the inode in the fs/subvol
487 * tree, otherwise we would be throwing valid data away.
489 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
490 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
492 struct btrfs_map_token token;
494 btrfs_init_map_token(&token);
495 btrfs_set_token_inode_size(dst_eb, dst_item,
501 if (overwrite_root &&
502 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
503 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
505 saved_i_size = btrfs_inode_size(path->nodes[0],
510 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
513 if (save_old_i_size) {
514 struct btrfs_inode_item *dst_item;
515 dst_item = (struct btrfs_inode_item *)dst_ptr;
516 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
519 /* make sure the generation is filled in */
520 if (key->type == BTRFS_INODE_ITEM_KEY) {
521 struct btrfs_inode_item *dst_item;
522 dst_item = (struct btrfs_inode_item *)dst_ptr;
523 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
524 btrfs_set_inode_generation(path->nodes[0], dst_item,
529 btrfs_mark_buffer_dirty(path->nodes[0]);
530 btrfs_release_path(path);
535 * simple helper to read an inode off the disk from a given root
536 * This can only be called for subvolume roots and not for the log
538 static noinline struct inode *read_one_inode(struct btrfs_root *root,
541 struct btrfs_key key;
544 key.objectid = objectid;
545 key.type = BTRFS_INODE_ITEM_KEY;
547 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
550 } else if (is_bad_inode(inode)) {
557 /* replays a single extent in 'eb' at 'slot' with 'key' into the
558 * subvolume 'root'. path is released on entry and should be released
561 * extents in the log tree have not been allocated out of the extent
562 * tree yet. So, this completes the allocation, taking a reference
563 * as required if the extent already exists or creating a new extent
564 * if it isn't in the extent allocation tree yet.
566 * The extent is inserted into the file, dropping any existing extents
567 * from the file that overlap the new one.
569 static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
570 struct btrfs_root *root,
571 struct btrfs_path *path,
572 struct extent_buffer *eb, int slot,
573 struct btrfs_key *key)
575 struct btrfs_fs_info *fs_info = root->fs_info;
578 u64 start = key->offset;
580 struct btrfs_file_extent_item *item;
581 struct inode *inode = NULL;
585 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
586 found_type = btrfs_file_extent_type(eb, item);
588 if (found_type == BTRFS_FILE_EXTENT_REG ||
589 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
590 nbytes = btrfs_file_extent_num_bytes(eb, item);
591 extent_end = start + nbytes;
594 * We don't add to the inodes nbytes if we are prealloc or a
597 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
599 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
600 size = btrfs_file_extent_inline_len(eb, slot, item);
601 nbytes = btrfs_file_extent_ram_bytes(eb, item);
602 extent_end = ALIGN(start + size,
603 fs_info->sectorsize);
609 inode = read_one_inode(root, key->objectid);
616 * first check to see if we already have this extent in the
617 * file. This must be done before the btrfs_drop_extents run
618 * so we don't try to drop this extent.
620 ret = btrfs_lookup_file_extent(trans, root, path,
621 btrfs_ino(BTRFS_I(inode)), start, 0);
624 (found_type == BTRFS_FILE_EXTENT_REG ||
625 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
626 struct btrfs_file_extent_item cmp1;
627 struct btrfs_file_extent_item cmp2;
628 struct btrfs_file_extent_item *existing;
629 struct extent_buffer *leaf;
631 leaf = path->nodes[0];
632 existing = btrfs_item_ptr(leaf, path->slots[0],
633 struct btrfs_file_extent_item);
635 read_extent_buffer(eb, &cmp1, (unsigned long)item,
637 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
641 * we already have a pointer to this exact extent,
642 * we don't have to do anything
644 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
645 btrfs_release_path(path);
649 btrfs_release_path(path);
651 /* drop any overlapping extents */
652 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
656 if (found_type == BTRFS_FILE_EXTENT_REG ||
657 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
659 unsigned long dest_offset;
660 struct btrfs_key ins;
662 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
663 btrfs_fs_incompat(fs_info, NO_HOLES))
666 ret = btrfs_insert_empty_item(trans, root, path, key,
670 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
672 copy_extent_buffer(path->nodes[0], eb, dest_offset,
673 (unsigned long)item, sizeof(*item));
675 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
676 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
677 ins.type = BTRFS_EXTENT_ITEM_KEY;
678 offset = key->offset - btrfs_file_extent_offset(eb, item);
681 * Manually record dirty extent, as here we did a shallow
682 * file extent item copy and skip normal backref update,
683 * but modifying extent tree all by ourselves.
684 * So need to manually record dirty extent for qgroup,
685 * as the owner of the file extent changed from log tree
686 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
688 ret = btrfs_qgroup_trace_extent(trans, fs_info,
689 btrfs_file_extent_disk_bytenr(eb, item),
690 btrfs_file_extent_disk_num_bytes(eb, item),
695 if (ins.objectid > 0) {
698 LIST_HEAD(ordered_sums);
700 * is this extent already allocated in the extent
701 * allocation tree? If so, just add a reference
703 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
706 ret = btrfs_inc_extent_ref(trans, root,
707 ins.objectid, ins.offset,
708 0, root->root_key.objectid,
709 key->objectid, offset);
714 * insert the extent pointer in the extent
717 ret = btrfs_alloc_logged_file_extent(trans,
719 root->root_key.objectid,
720 key->objectid, offset, &ins);
724 btrfs_release_path(path);
726 if (btrfs_file_extent_compression(eb, item)) {
727 csum_start = ins.objectid;
728 csum_end = csum_start + ins.offset;
730 csum_start = ins.objectid +
731 btrfs_file_extent_offset(eb, item);
732 csum_end = csum_start +
733 btrfs_file_extent_num_bytes(eb, item);
736 ret = btrfs_lookup_csums_range(root->log_root,
737 csum_start, csum_end - 1,
742 * Now delete all existing cums in the csum root that
743 * cover our range. We do this because we can have an
744 * extent that is completely referenced by one file
745 * extent item and partially referenced by another
746 * file extent item (like after using the clone or
747 * extent_same ioctls). In this case if we end up doing
748 * the replay of the one that partially references the
749 * extent first, and we do not do the csum deletion
750 * below, we can get 2 csum items in the csum tree that
751 * overlap each other. For example, imagine our log has
752 * the two following file extent items:
754 * key (257 EXTENT_DATA 409600)
755 * extent data disk byte 12845056 nr 102400
756 * extent data offset 20480 nr 20480 ram 102400
758 * key (257 EXTENT_DATA 819200)
759 * extent data disk byte 12845056 nr 102400
760 * extent data offset 0 nr 102400 ram 102400
762 * Where the second one fully references the 100K extent
763 * that starts at disk byte 12845056, and the log tree
764 * has a single csum item that covers the entire range
767 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
769 * After the first file extent item is replayed, the
770 * csum tree gets the following csum item:
772 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
774 * Which covers the 20K sub-range starting at offset 20K
775 * of our extent. Now when we replay the second file
776 * extent item, if we do not delete existing csum items
777 * that cover any of its blocks, we end up getting two
778 * csum items in our csum tree that overlap each other:
780 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
781 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
783 * Which is a problem, because after this anyone trying
784 * to lookup up for the checksum of any block of our
785 * extent starting at an offset of 40K or higher, will
786 * end up looking at the second csum item only, which
787 * does not contain the checksum for any block starting
788 * at offset 40K or higher of our extent.
790 while (!list_empty(&ordered_sums)) {
791 struct btrfs_ordered_sum *sums;
792 sums = list_entry(ordered_sums.next,
793 struct btrfs_ordered_sum,
796 ret = btrfs_del_csums(trans, fs_info,
800 ret = btrfs_csum_file_blocks(trans,
801 fs_info->csum_root, sums);
802 list_del(&sums->list);
808 btrfs_release_path(path);
810 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
811 /* inline extents are easy, we just overwrite them */
812 ret = overwrite_item(trans, root, path, eb, slot, key);
817 inode_add_bytes(inode, nbytes);
819 ret = btrfs_update_inode(trans, root, inode);
827 * when cleaning up conflicts between the directory names in the
828 * subvolume, directory names in the log and directory names in the
829 * inode back references, we may have to unlink inodes from directories.
831 * This is a helper function to do the unlink of a specific directory
834 static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
835 struct btrfs_root *root,
836 struct btrfs_path *path,
837 struct btrfs_inode *dir,
838 struct btrfs_dir_item *di)
843 struct extent_buffer *leaf;
844 struct btrfs_key location;
847 leaf = path->nodes[0];
849 btrfs_dir_item_key_to_cpu(leaf, di, &location);
850 name_len = btrfs_dir_name_len(leaf, di);
851 name = kmalloc(name_len, GFP_NOFS);
855 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
856 btrfs_release_path(path);
858 inode = read_one_inode(root, location.objectid);
864 ret = link_to_fixup_dir(trans, root, path, location.objectid);
868 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
873 ret = btrfs_run_delayed_items(trans);
881 * helper function to see if a given name and sequence number found
882 * in an inode back reference are already in a directory and correctly
883 * point to this inode
885 static noinline int inode_in_dir(struct btrfs_root *root,
886 struct btrfs_path *path,
887 u64 dirid, u64 objectid, u64 index,
888 const char *name, int name_len)
890 struct btrfs_dir_item *di;
891 struct btrfs_key location;
894 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
895 index, name, name_len, 0);
896 if (di && !IS_ERR(di)) {
897 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
898 if (location.objectid != objectid)
902 btrfs_release_path(path);
904 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
905 if (di && !IS_ERR(di)) {
906 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
907 if (location.objectid != objectid)
913 btrfs_release_path(path);
918 * helper function to check a log tree for a named back reference in
919 * an inode. This is used to decide if a back reference that is
920 * found in the subvolume conflicts with what we find in the log.
922 * inode backreferences may have multiple refs in a single item,
923 * during replay we process one reference at a time, and we don't
924 * want to delete valid links to a file from the subvolume if that
925 * link is also in the log.
927 static noinline int backref_in_log(struct btrfs_root *log,
928 struct btrfs_key *key,
930 const char *name, int namelen)
932 struct btrfs_path *path;
933 struct btrfs_inode_ref *ref;
935 unsigned long ptr_end;
936 unsigned long name_ptr;
942 path = btrfs_alloc_path();
946 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
950 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
952 if (key->type == BTRFS_INODE_EXTREF_KEY) {
953 if (btrfs_find_name_in_ext_backref(path->nodes[0],
956 name, namelen, NULL))
962 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
963 ptr_end = ptr + item_size;
964 while (ptr < ptr_end) {
965 ref = (struct btrfs_inode_ref *)ptr;
966 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
967 if (found_name_len == namelen) {
968 name_ptr = (unsigned long)(ref + 1);
969 ret = memcmp_extent_buffer(path->nodes[0], name,
976 ptr = (unsigned long)(ref + 1) + found_name_len;
979 btrfs_free_path(path);
983 static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
984 struct btrfs_root *root,
985 struct btrfs_path *path,
986 struct btrfs_root *log_root,
987 struct btrfs_inode *dir,
988 struct btrfs_inode *inode,
989 u64 inode_objectid, u64 parent_objectid,
990 u64 ref_index, char *name, int namelen,
996 struct extent_buffer *leaf;
997 struct btrfs_dir_item *di;
998 struct btrfs_key search_key;
999 struct btrfs_inode_extref *extref;
1002 /* Search old style refs */
1003 search_key.objectid = inode_objectid;
1004 search_key.type = BTRFS_INODE_REF_KEY;
1005 search_key.offset = parent_objectid;
1006 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
1008 struct btrfs_inode_ref *victim_ref;
1010 unsigned long ptr_end;
1012 leaf = path->nodes[0];
1014 /* are we trying to overwrite a back ref for the root directory
1015 * if so, just jump out, we're done
1017 if (search_key.objectid == search_key.offset)
1020 /* check all the names in this back reference to see
1021 * if they are in the log. if so, we allow them to stay
1022 * otherwise they must be unlinked as a conflict
1024 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1025 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1026 while (ptr < ptr_end) {
1027 victim_ref = (struct btrfs_inode_ref *)ptr;
1028 victim_name_len = btrfs_inode_ref_name_len(leaf,
1030 victim_name = kmalloc(victim_name_len, GFP_NOFS);
1034 read_extent_buffer(leaf, victim_name,
1035 (unsigned long)(victim_ref + 1),
1038 if (!backref_in_log(log_root, &search_key,
1042 inc_nlink(&inode->vfs_inode);
1043 btrfs_release_path(path);
1045 ret = btrfs_unlink_inode(trans, root, dir, inode,
1046 victim_name, victim_name_len);
1050 ret = btrfs_run_delayed_items(trans);
1058 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1062 * NOTE: we have searched root tree and checked the
1063 * corresponding ref, it does not need to check again.
1067 btrfs_release_path(path);
1069 /* Same search but for extended refs */
1070 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1071 inode_objectid, parent_objectid, 0,
1073 if (!IS_ERR_OR_NULL(extref)) {
1077 struct inode *victim_parent;
1079 leaf = path->nodes[0];
1081 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1082 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1084 while (cur_offset < item_size) {
1085 extref = (struct btrfs_inode_extref *)(base + cur_offset);
1087 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1089 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1092 victim_name = kmalloc(victim_name_len, GFP_NOFS);
1095 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1098 search_key.objectid = inode_objectid;
1099 search_key.type = BTRFS_INODE_EXTREF_KEY;
1100 search_key.offset = btrfs_extref_hash(parent_objectid,
1104 if (!backref_in_log(log_root, &search_key,
1105 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 conflicing 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, name,
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);
1295 * replay one inode back reference item found in the log tree.
1296 * eb, slot and key refer to the buffer and key found in the log tree.
1297 * root is the destination we are replaying into, and path is for temp
1298 * use by this function. (it should be released on return).
1300 static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1301 struct btrfs_root *root,
1302 struct btrfs_root *log,
1303 struct btrfs_path *path,
1304 struct extent_buffer *eb, int slot,
1305 struct btrfs_key *key)
1307 struct inode *dir = NULL;
1308 struct inode *inode = NULL;
1309 unsigned long ref_ptr;
1310 unsigned long ref_end;
1314 int search_done = 0;
1315 int log_ref_ver = 0;
1316 u64 parent_objectid;
1319 int ref_struct_size;
1321 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1322 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1324 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1325 struct btrfs_inode_extref *r;
1327 ref_struct_size = sizeof(struct btrfs_inode_extref);
1329 r = (struct btrfs_inode_extref *)ref_ptr;
1330 parent_objectid = btrfs_inode_extref_parent(eb, r);
1332 ref_struct_size = sizeof(struct btrfs_inode_ref);
1333 parent_objectid = key->offset;
1335 inode_objectid = key->objectid;
1338 * it is possible that we didn't log all the parent directories
1339 * for a given inode. If we don't find the dir, just don't
1340 * copy the back ref in. The link count fixup code will take
1343 dir = read_one_inode(root, parent_objectid);
1349 inode = read_one_inode(root, inode_objectid);
1355 while (ref_ptr < ref_end) {
1357 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1358 &ref_index, &parent_objectid);
1360 * parent object can change from one array
1364 dir = read_one_inode(root, parent_objectid);
1370 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1376 /* if we already have a perfect match, we're done */
1377 if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1378 btrfs_ino(BTRFS_I(inode)), ref_index,
1381 * look for a conflicting back reference in the
1382 * metadata. if we find one we have to unlink that name
1383 * of the file before we add our new link. Later on, we
1384 * overwrite any existing back reference, and we don't
1385 * want to create dangling pointers in the directory.
1389 ret = __add_inode_ref(trans, root, path, log,
1394 ref_index, name, namelen,
1403 /* insert our name */
1404 ret = btrfs_add_link(trans, BTRFS_I(dir),
1406 name, namelen, 0, ref_index);
1410 btrfs_update_inode(trans, root, inode);
1413 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
1423 * Before we overwrite the inode reference item in the subvolume tree
1424 * with the item from the log tree, we must unlink all names from the
1425 * parent directory that are in the subvolume's tree inode reference
1426 * item, otherwise we end up with an inconsistent subvolume tree where
1427 * dir index entries exist for a name but there is no inode reference
1428 * item with the same name.
1430 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1435 /* finally write the back reference in the inode */
1436 ret = overwrite_item(trans, root, path, eb, slot, key);
1438 btrfs_release_path(path);
1445 static int insert_orphan_item(struct btrfs_trans_handle *trans,
1446 struct btrfs_root *root, u64 ino)
1450 ret = btrfs_insert_orphan_item(trans, root, ino);
1457 static int count_inode_extrefs(struct btrfs_root *root,
1458 struct btrfs_inode *inode, struct btrfs_path *path)
1462 unsigned int nlink = 0;
1465 u64 inode_objectid = btrfs_ino(inode);
1468 struct btrfs_inode_extref *extref;
1469 struct extent_buffer *leaf;
1472 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1477 leaf = path->nodes[0];
1478 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1479 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1482 while (cur_offset < item_size) {
1483 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1484 name_len = btrfs_inode_extref_name_len(leaf, extref);
1488 cur_offset += name_len + sizeof(*extref);
1492 btrfs_release_path(path);
1494 btrfs_release_path(path);
1496 if (ret < 0 && ret != -ENOENT)
1501 static int count_inode_refs(struct btrfs_root *root,
1502 struct btrfs_inode *inode, struct btrfs_path *path)
1505 struct btrfs_key key;
1506 unsigned int nlink = 0;
1508 unsigned long ptr_end;
1510 u64 ino = btrfs_ino(inode);
1513 key.type = BTRFS_INODE_REF_KEY;
1514 key.offset = (u64)-1;
1517 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1521 if (path->slots[0] == 0)
1526 btrfs_item_key_to_cpu(path->nodes[0], &key,
1528 if (key.objectid != ino ||
1529 key.type != BTRFS_INODE_REF_KEY)
1531 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1532 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1534 while (ptr < ptr_end) {
1535 struct btrfs_inode_ref *ref;
1537 ref = (struct btrfs_inode_ref *)ptr;
1538 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1540 ptr = (unsigned long)(ref + 1) + name_len;
1544 if (key.offset == 0)
1546 if (path->slots[0] > 0) {
1551 btrfs_release_path(path);
1553 btrfs_release_path(path);
1559 * There are a few corners where the link count of the file can't
1560 * be properly maintained during replay. So, instead of adding
1561 * lots of complexity to the log code, we just scan the backrefs
1562 * for any file that has been through replay.
1564 * The scan will update the link count on the inode to reflect the
1565 * number of back refs found. If it goes down to zero, the iput
1566 * will free the inode.
1568 static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1569 struct btrfs_root *root,
1570 struct inode *inode)
1572 struct btrfs_path *path;
1575 u64 ino = btrfs_ino(BTRFS_I(inode));
1577 path = btrfs_alloc_path();
1581 ret = count_inode_refs(root, BTRFS_I(inode), path);
1587 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
1595 if (nlink != inode->i_nlink) {
1596 set_nlink(inode, nlink);
1597 btrfs_update_inode(trans, root, inode);
1599 BTRFS_I(inode)->index_cnt = (u64)-1;
1601 if (inode->i_nlink == 0) {
1602 if (S_ISDIR(inode->i_mode)) {
1603 ret = replay_dir_deletes(trans, root, NULL, path,
1608 ret = insert_orphan_item(trans, root, ino);
1612 btrfs_free_path(path);
1616 static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1617 struct btrfs_root *root,
1618 struct btrfs_path *path)
1621 struct btrfs_key key;
1622 struct inode *inode;
1624 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1625 key.type = BTRFS_ORPHAN_ITEM_KEY;
1626 key.offset = (u64)-1;
1628 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1633 if (path->slots[0] == 0)
1638 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1639 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1640 key.type != BTRFS_ORPHAN_ITEM_KEY)
1643 ret = btrfs_del_item(trans, root, path);
1647 btrfs_release_path(path);
1648 inode = read_one_inode(root, key.offset);
1652 ret = fixup_inode_link_count(trans, root, inode);
1658 * fixup on a directory may create new entries,
1659 * make sure we always look for the highset possible
1662 key.offset = (u64)-1;
1666 btrfs_release_path(path);
1672 * record a given inode in the fixup dir so we can check its link
1673 * count when replay is done. The link count is incremented here
1674 * so the inode won't go away until we check it
1676 static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1677 struct btrfs_root *root,
1678 struct btrfs_path *path,
1681 struct btrfs_key key;
1683 struct inode *inode;
1685 inode = read_one_inode(root, objectid);
1689 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1690 key.type = BTRFS_ORPHAN_ITEM_KEY;
1691 key.offset = objectid;
1693 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1695 btrfs_release_path(path);
1697 if (!inode->i_nlink)
1698 set_nlink(inode, 1);
1701 ret = btrfs_update_inode(trans, root, inode);
1702 } else if (ret == -EEXIST) {
1705 BUG(); /* Logic Error */
1713 * when replaying the log for a directory, we only insert names
1714 * for inodes that actually exist. This means an fsync on a directory
1715 * does not implicitly fsync all the new files in it
1717 static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1718 struct btrfs_root *root,
1719 u64 dirid, u64 index,
1720 char *name, int name_len,
1721 struct btrfs_key *location)
1723 struct inode *inode;
1727 inode = read_one_inode(root, location->objectid);
1731 dir = read_one_inode(root, dirid);
1737 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1738 name_len, 1, index);
1740 /* FIXME, put inode into FIXUP list */
1748 * Return true if an inode reference exists in the log for the given name,
1749 * inode and parent inode.
1751 static bool name_in_log_ref(struct btrfs_root *log_root,
1752 const char *name, const int name_len,
1753 const u64 dirid, const u64 ino)
1755 struct btrfs_key search_key;
1757 search_key.objectid = ino;
1758 search_key.type = BTRFS_INODE_REF_KEY;
1759 search_key.offset = dirid;
1760 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1763 search_key.type = BTRFS_INODE_EXTREF_KEY;
1764 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1765 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1772 * take a single entry in a log directory item and replay it into
1775 * if a conflicting item exists in the subdirectory already,
1776 * the inode it points to is unlinked and put into the link count
1779 * If a name from the log points to a file or directory that does
1780 * not exist in the FS, it is skipped. fsyncs on directories
1781 * do not force down inodes inside that directory, just changes to the
1782 * names or unlinks in a directory.
1784 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1785 * non-existing inode) and 1 if the name was replayed.
1787 static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1788 struct btrfs_root *root,
1789 struct btrfs_path *path,
1790 struct extent_buffer *eb,
1791 struct btrfs_dir_item *di,
1792 struct btrfs_key *key)
1796 struct btrfs_dir_item *dst_di;
1797 struct btrfs_key found_key;
1798 struct btrfs_key log_key;
1803 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
1804 bool name_added = false;
1806 dir = read_one_inode(root, key->objectid);
1810 name_len = btrfs_dir_name_len(eb, di);
1811 name = kmalloc(name_len, GFP_NOFS);
1817 log_type = btrfs_dir_type(eb, di);
1818 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1821 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
1822 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1827 btrfs_release_path(path);
1829 if (key->type == BTRFS_DIR_ITEM_KEY) {
1830 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1832 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
1833 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1842 if (IS_ERR_OR_NULL(dst_di)) {
1843 /* we need a sequence number to insert, so we only
1844 * do inserts for the BTRFS_DIR_INDEX_KEY types
1846 if (key->type != BTRFS_DIR_INDEX_KEY)
1851 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1852 /* the existing item matches the logged item */
1853 if (found_key.objectid == log_key.objectid &&
1854 found_key.type == log_key.type &&
1855 found_key.offset == log_key.offset &&
1856 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1857 update_size = false;
1862 * don't drop the conflicting directory entry if the inode
1863 * for the new entry doesn't exist
1868 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
1872 if (key->type == BTRFS_DIR_INDEX_KEY)
1875 btrfs_release_path(path);
1876 if (!ret && update_size) {
1877 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
1878 ret = btrfs_update_inode(trans, root, dir);
1882 if (!ret && name_added)
1887 if (name_in_log_ref(root->log_root, name, name_len,
1888 key->objectid, log_key.objectid)) {
1889 /* The dentry will be added later. */
1891 update_size = false;
1894 btrfs_release_path(path);
1895 ret = insert_one_name(trans, root, key->objectid, key->offset,
1896 name, name_len, &log_key);
1897 if (ret && ret != -ENOENT && ret != -EEXIST)
1901 update_size = false;
1907 * find all the names in a directory item and reconcile them into
1908 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1909 * one name in a directory item, but the same code gets used for
1910 * both directory index types
1912 static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1913 struct btrfs_root *root,
1914 struct btrfs_path *path,
1915 struct extent_buffer *eb, int slot,
1916 struct btrfs_key *key)
1919 u32 item_size = btrfs_item_size_nr(eb, slot);
1920 struct btrfs_dir_item *di;
1923 unsigned long ptr_end;
1924 struct btrfs_path *fixup_path = NULL;
1926 ptr = btrfs_item_ptr_offset(eb, slot);
1927 ptr_end = ptr + item_size;
1928 while (ptr < ptr_end) {
1929 di = (struct btrfs_dir_item *)ptr;
1930 name_len = btrfs_dir_name_len(eb, di);
1931 ret = replay_one_name(trans, root, path, eb, di, key);
1934 ptr = (unsigned long)(di + 1);
1938 * If this entry refers to a non-directory (directories can not
1939 * have a link count > 1) and it was added in the transaction
1940 * that was not committed, make sure we fixup the link count of
1941 * the inode it the entry points to. Otherwise something like
1942 * the following would result in a directory pointing to an
1943 * inode with a wrong link that does not account for this dir
1951 * ln testdir/bar testdir/bar_link
1952 * ln testdir/foo testdir/foo_link
1953 * xfs_io -c "fsync" testdir/bar
1957 * mount fs, log replay happens
1959 * File foo would remain with a link count of 1 when it has two
1960 * entries pointing to it in the directory testdir. This would
1961 * make it impossible to ever delete the parent directory has
1962 * it would result in stale dentries that can never be deleted.
1964 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1965 struct btrfs_key di_key;
1968 fixup_path = btrfs_alloc_path();
1975 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1976 ret = link_to_fixup_dir(trans, root, fixup_path,
1983 btrfs_free_path(fixup_path);
1988 * directory replay has two parts. There are the standard directory
1989 * items in the log copied from the subvolume, and range items
1990 * created in the log while the subvolume was logged.
1992 * The range items tell us which parts of the key space the log
1993 * is authoritative for. During replay, if a key in the subvolume
1994 * directory is in a logged range item, but not actually in the log
1995 * that means it was deleted from the directory before the fsync
1996 * and should be removed.
1998 static noinline int find_dir_range(struct btrfs_root *root,
1999 struct btrfs_path *path,
2000 u64 dirid, int key_type,
2001 u64 *start_ret, u64 *end_ret)
2003 struct btrfs_key key;
2005 struct btrfs_dir_log_item *item;
2009 if (*start_ret == (u64)-1)
2012 key.objectid = dirid;
2013 key.type = key_type;
2014 key.offset = *start_ret;
2016 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2020 if (path->slots[0] == 0)
2025 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2027 if (key.type != key_type || key.objectid != dirid) {
2031 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2032 struct btrfs_dir_log_item);
2033 found_end = btrfs_dir_log_end(path->nodes[0], item);
2035 if (*start_ret >= key.offset && *start_ret <= found_end) {
2037 *start_ret = key.offset;
2038 *end_ret = found_end;
2043 /* check the next slot in the tree to see if it is a valid item */
2044 nritems = btrfs_header_nritems(path->nodes[0]);
2046 if (path->slots[0] >= nritems) {
2047 ret = btrfs_next_leaf(root, path);
2052 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2054 if (key.type != key_type || key.objectid != dirid) {
2058 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2059 struct btrfs_dir_log_item);
2060 found_end = btrfs_dir_log_end(path->nodes[0], item);
2061 *start_ret = key.offset;
2062 *end_ret = found_end;
2065 btrfs_release_path(path);
2070 * this looks for a given directory item in the log. If the directory
2071 * item is not in the log, the item is removed and the inode it points
2074 static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2075 struct btrfs_root *root,
2076 struct btrfs_root *log,
2077 struct btrfs_path *path,
2078 struct btrfs_path *log_path,
2080 struct btrfs_key *dir_key)
2083 struct extent_buffer *eb;
2086 struct btrfs_dir_item *di;
2087 struct btrfs_dir_item *log_di;
2090 unsigned long ptr_end;
2092 struct inode *inode;
2093 struct btrfs_key location;
2096 eb = path->nodes[0];
2097 slot = path->slots[0];
2098 item_size = btrfs_item_size_nr(eb, slot);
2099 ptr = btrfs_item_ptr_offset(eb, slot);
2100 ptr_end = ptr + item_size;
2101 while (ptr < ptr_end) {
2102 di = (struct btrfs_dir_item *)ptr;
2103 name_len = btrfs_dir_name_len(eb, di);
2104 name = kmalloc(name_len, GFP_NOFS);
2109 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2112 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
2113 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2116 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
2117 log_di = btrfs_lookup_dir_index_item(trans, log,
2123 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
2124 btrfs_dir_item_key_to_cpu(eb, di, &location);
2125 btrfs_release_path(path);
2126 btrfs_release_path(log_path);
2127 inode = read_one_inode(root, location.objectid);
2133 ret = link_to_fixup_dir(trans, root,
2134 path, location.objectid);
2142 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2143 BTRFS_I(inode), name, name_len);
2145 ret = btrfs_run_delayed_items(trans);
2151 /* there might still be more names under this key
2152 * check and repeat if required
2154 ret = btrfs_search_slot(NULL, root, dir_key, path,
2160 } else if (IS_ERR(log_di)) {
2162 return PTR_ERR(log_di);
2164 btrfs_release_path(log_path);
2167 ptr = (unsigned long)(di + 1);
2172 btrfs_release_path(path);
2173 btrfs_release_path(log_path);
2177 static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2178 struct btrfs_root *root,
2179 struct btrfs_root *log,
2180 struct btrfs_path *path,
2183 struct btrfs_key search_key;
2184 struct btrfs_path *log_path;
2189 log_path = btrfs_alloc_path();
2193 search_key.objectid = ino;
2194 search_key.type = BTRFS_XATTR_ITEM_KEY;
2195 search_key.offset = 0;
2197 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2201 nritems = btrfs_header_nritems(path->nodes[0]);
2202 for (i = path->slots[0]; i < nritems; i++) {
2203 struct btrfs_key key;
2204 struct btrfs_dir_item *di;
2205 struct btrfs_dir_item *log_di;
2209 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2210 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2215 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2216 total_size = btrfs_item_size_nr(path->nodes[0], i);
2218 while (cur < total_size) {
2219 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2220 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2221 u32 this_len = sizeof(*di) + name_len + data_len;
2224 name = kmalloc(name_len, GFP_NOFS);
2229 read_extent_buffer(path->nodes[0], name,
2230 (unsigned long)(di + 1), name_len);
2232 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2234 btrfs_release_path(log_path);
2236 /* Doesn't exist in log tree, so delete it. */
2237 btrfs_release_path(path);
2238 di = btrfs_lookup_xattr(trans, root, path, ino,
2239 name, name_len, -1);
2246 ret = btrfs_delete_one_dir_name(trans, root,
2250 btrfs_release_path(path);
2255 if (IS_ERR(log_di)) {
2256 ret = PTR_ERR(log_di);
2260 di = (struct btrfs_dir_item *)((char *)di + this_len);
2263 ret = btrfs_next_leaf(root, path);
2269 btrfs_free_path(log_path);
2270 btrfs_release_path(path);
2276 * deletion replay happens before we copy any new directory items
2277 * out of the log or out of backreferences from inodes. It
2278 * scans the log to find ranges of keys that log is authoritative for,
2279 * and then scans the directory to find items in those ranges that are
2280 * not present in the log.
2282 * Anything we don't find in the log is unlinked and removed from the
2285 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2286 struct btrfs_root *root,
2287 struct btrfs_root *log,
2288 struct btrfs_path *path,
2289 u64 dirid, int del_all)
2293 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2295 struct btrfs_key dir_key;
2296 struct btrfs_key found_key;
2297 struct btrfs_path *log_path;
2300 dir_key.objectid = dirid;
2301 dir_key.type = BTRFS_DIR_ITEM_KEY;
2302 log_path = btrfs_alloc_path();
2306 dir = read_one_inode(root, dirid);
2307 /* it isn't an error if the inode isn't there, that can happen
2308 * because we replay the deletes before we copy in the inode item
2312 btrfs_free_path(log_path);
2320 range_end = (u64)-1;
2322 ret = find_dir_range(log, path, dirid, key_type,
2323 &range_start, &range_end);
2328 dir_key.offset = range_start;
2331 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2336 nritems = btrfs_header_nritems(path->nodes[0]);
2337 if (path->slots[0] >= nritems) {
2338 ret = btrfs_next_leaf(root, path);
2344 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2346 if (found_key.objectid != dirid ||
2347 found_key.type != dir_key.type)
2350 if (found_key.offset > range_end)
2353 ret = check_item_in_log(trans, root, log, path,
2358 if (found_key.offset == (u64)-1)
2360 dir_key.offset = found_key.offset + 1;
2362 btrfs_release_path(path);
2363 if (range_end == (u64)-1)
2365 range_start = range_end + 1;
2370 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2371 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2372 dir_key.type = BTRFS_DIR_INDEX_KEY;
2373 btrfs_release_path(path);
2377 btrfs_release_path(path);
2378 btrfs_free_path(log_path);
2384 * the process_func used to replay items from the log tree. This
2385 * gets called in two different stages. The first stage just looks
2386 * for inodes and makes sure they are all copied into the subvolume.
2388 * The second stage copies all the other item types from the log into
2389 * the subvolume. The two stage approach is slower, but gets rid of
2390 * lots of complexity around inodes referencing other inodes that exist
2391 * only in the log (references come from either directory items or inode
2394 static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2395 struct walk_control *wc, u64 gen, int level)
2398 struct btrfs_path *path;
2399 struct btrfs_root *root = wc->replay_dest;
2400 struct btrfs_key key;
2404 ret = btrfs_read_buffer(eb, gen, level, NULL);
2408 level = btrfs_header_level(eb);
2413 path = btrfs_alloc_path();
2417 nritems = btrfs_header_nritems(eb);
2418 for (i = 0; i < nritems; i++) {
2419 btrfs_item_key_to_cpu(eb, &key, i);
2421 /* inode keys are done during the first stage */
2422 if (key.type == BTRFS_INODE_ITEM_KEY &&
2423 wc->stage == LOG_WALK_REPLAY_INODES) {
2424 struct btrfs_inode_item *inode_item;
2427 inode_item = btrfs_item_ptr(eb, i,
2428 struct btrfs_inode_item);
2429 ret = replay_xattr_deletes(wc->trans, root, log,
2430 path, key.objectid);
2433 mode = btrfs_inode_mode(eb, inode_item);
2434 if (S_ISDIR(mode)) {
2435 ret = replay_dir_deletes(wc->trans,
2436 root, log, path, key.objectid, 0);
2440 ret = overwrite_item(wc->trans, root, path,
2446 * Before replaying extents, truncate the inode to its
2447 * size. We need to do it now and not after log replay
2448 * because before an fsync we can have prealloc extents
2449 * added beyond the inode's i_size. If we did it after,
2450 * through orphan cleanup for example, we would drop
2451 * those prealloc extents just after replaying them.
2453 if (S_ISREG(mode)) {
2454 struct inode *inode;
2457 inode = read_one_inode(root, key.objectid);
2462 from = ALIGN(i_size_read(inode),
2463 root->fs_info->sectorsize);
2464 ret = btrfs_drop_extents(wc->trans, root, inode,
2467 * If the nlink count is zero here, the iput
2468 * will free the inode. We bump it to make
2469 * sure it doesn't get freed until the link
2470 * count fixup is done.
2473 if (inode->i_nlink == 0)
2475 /* Update link count and nbytes. */
2476 ret = btrfs_update_inode(wc->trans,
2484 ret = link_to_fixup_dir(wc->trans, root,
2485 path, key.objectid);
2490 if (key.type == BTRFS_DIR_INDEX_KEY &&
2491 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2492 ret = replay_one_dir_item(wc->trans, root, path,
2498 if (wc->stage < LOG_WALK_REPLAY_ALL)
2501 /* these keys are simply copied */
2502 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2503 ret = overwrite_item(wc->trans, root, path,
2507 } else if (key.type == BTRFS_INODE_REF_KEY ||
2508 key.type == BTRFS_INODE_EXTREF_KEY) {
2509 ret = add_inode_ref(wc->trans, root, log, path,
2511 if (ret && ret != -ENOENT)
2514 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2515 ret = replay_one_extent(wc->trans, root, path,
2519 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
2520 ret = replay_one_dir_item(wc->trans, root, path,
2526 btrfs_free_path(path);
2530 static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2531 struct btrfs_root *root,
2532 struct btrfs_path *path, int *level,
2533 struct walk_control *wc)
2535 struct btrfs_fs_info *fs_info = root->fs_info;
2539 struct extent_buffer *next;
2540 struct extent_buffer *cur;
2541 struct extent_buffer *parent;
2545 WARN_ON(*level < 0);
2546 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2548 while (*level > 0) {
2549 struct btrfs_key first_key;
2551 WARN_ON(*level < 0);
2552 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2553 cur = path->nodes[*level];
2555 WARN_ON(btrfs_header_level(cur) != *level);
2557 if (path->slots[*level] >=
2558 btrfs_header_nritems(cur))
2561 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2562 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2563 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
2564 blocksize = fs_info->nodesize;
2566 parent = path->nodes[*level];
2567 root_owner = btrfs_header_owner(parent);
2569 next = btrfs_find_create_tree_block(fs_info, bytenr);
2571 return PTR_ERR(next);
2574 ret = wc->process_func(root, next, wc, ptr_gen,
2577 free_extent_buffer(next);
2581 path->slots[*level]++;
2583 ret = btrfs_read_buffer(next, ptr_gen,
2584 *level - 1, &first_key);
2586 free_extent_buffer(next);
2591 btrfs_tree_lock(next);
2592 btrfs_set_lock_blocking(next);
2593 clean_tree_block(fs_info, next);
2594 btrfs_wait_tree_block_writeback(next);
2595 btrfs_tree_unlock(next);
2597 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2598 clear_extent_buffer_dirty(next);
2601 WARN_ON(root_owner !=
2602 BTRFS_TREE_LOG_OBJECTID);
2603 ret = btrfs_free_and_pin_reserved_extent(
2607 free_extent_buffer(next);
2611 free_extent_buffer(next);
2614 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
2616 free_extent_buffer(next);
2620 WARN_ON(*level <= 0);
2621 if (path->nodes[*level-1])
2622 free_extent_buffer(path->nodes[*level-1]);
2623 path->nodes[*level-1] = next;
2624 *level = btrfs_header_level(next);
2625 path->slots[*level] = 0;
2628 WARN_ON(*level < 0);
2629 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2631 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
2637 static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
2638 struct btrfs_root *root,
2639 struct btrfs_path *path, int *level,
2640 struct walk_control *wc)
2642 struct btrfs_fs_info *fs_info = root->fs_info;
2648 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2649 slot = path->slots[i];
2650 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
2653 WARN_ON(*level == 0);
2656 struct extent_buffer *parent;
2657 if (path->nodes[*level] == root->node)
2658 parent = path->nodes[*level];
2660 parent = path->nodes[*level + 1];
2662 root_owner = btrfs_header_owner(parent);
2663 ret = wc->process_func(root, path->nodes[*level], wc,
2664 btrfs_header_generation(path->nodes[*level]),
2670 struct extent_buffer *next;
2672 next = path->nodes[*level];
2675 btrfs_tree_lock(next);
2676 btrfs_set_lock_blocking(next);
2677 clean_tree_block(fs_info, next);
2678 btrfs_wait_tree_block_writeback(next);
2679 btrfs_tree_unlock(next);
2681 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2682 clear_extent_buffer_dirty(next);
2685 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
2686 ret = btrfs_free_and_pin_reserved_extent(
2688 path->nodes[*level]->start,
2689 path->nodes[*level]->len);
2693 free_extent_buffer(path->nodes[*level]);
2694 path->nodes[*level] = NULL;
2702 * drop the reference count on the tree rooted at 'snap'. This traverses
2703 * the tree freeing any blocks that have a ref count of zero after being
2706 static int walk_log_tree(struct btrfs_trans_handle *trans,
2707 struct btrfs_root *log, struct walk_control *wc)
2709 struct btrfs_fs_info *fs_info = log->fs_info;
2713 struct btrfs_path *path;
2716 path = btrfs_alloc_path();
2720 level = btrfs_header_level(log->node);
2722 path->nodes[level] = log->node;
2723 extent_buffer_get(log->node);
2724 path->slots[level] = 0;
2727 wret = walk_down_log_tree(trans, log, path, &level, wc);
2735 wret = walk_up_log_tree(trans, log, path, &level, wc);
2744 /* was the root node processed? if not, catch it here */
2745 if (path->nodes[orig_level]) {
2746 ret = wc->process_func(log, path->nodes[orig_level], wc,
2747 btrfs_header_generation(path->nodes[orig_level]),
2752 struct extent_buffer *next;
2754 next = path->nodes[orig_level];
2757 btrfs_tree_lock(next);
2758 btrfs_set_lock_blocking(next);
2759 clean_tree_block(fs_info, next);
2760 btrfs_wait_tree_block_writeback(next);
2761 btrfs_tree_unlock(next);
2763 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2764 clear_extent_buffer_dirty(next);
2767 WARN_ON(log->root_key.objectid !=
2768 BTRFS_TREE_LOG_OBJECTID);
2769 ret = btrfs_free_and_pin_reserved_extent(fs_info,
2770 next->start, next->len);
2777 btrfs_free_path(path);
2782 * helper function to update the item for a given subvolumes log root
2783 * in the tree of log roots
2785 static int update_log_root(struct btrfs_trans_handle *trans,
2786 struct btrfs_root *log)
2788 struct btrfs_fs_info *fs_info = log->fs_info;
2791 if (log->log_transid == 1) {
2792 /* insert root item on the first sync */
2793 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
2794 &log->root_key, &log->root_item);
2796 ret = btrfs_update_root(trans, fs_info->log_root_tree,
2797 &log->root_key, &log->root_item);
2802 static void wait_log_commit(struct btrfs_root *root, int transid)
2805 int index = transid % 2;
2808 * we only allow two pending log transactions at a time,
2809 * so we know that if ours is more than 2 older than the
2810 * current transaction, we're done
2813 prepare_to_wait(&root->log_commit_wait[index],
2814 &wait, TASK_UNINTERRUPTIBLE);
2816 if (!(root->log_transid_committed < transid &&
2817 atomic_read(&root->log_commit[index])))
2820 mutex_unlock(&root->log_mutex);
2822 mutex_lock(&root->log_mutex);
2824 finish_wait(&root->log_commit_wait[index], &wait);
2827 static void wait_for_writer(struct btrfs_root *root)
2832 prepare_to_wait(&root->log_writer_wait, &wait,
2833 TASK_UNINTERRUPTIBLE);
2834 if (!atomic_read(&root->log_writers))
2837 mutex_unlock(&root->log_mutex);
2839 mutex_lock(&root->log_mutex);
2841 finish_wait(&root->log_writer_wait, &wait);
2844 static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2845 struct btrfs_log_ctx *ctx)
2850 mutex_lock(&root->log_mutex);
2851 list_del_init(&ctx->list);
2852 mutex_unlock(&root->log_mutex);
2856 * Invoked in log mutex context, or be sure there is no other task which
2857 * can access the list.
2859 static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2860 int index, int error)
2862 struct btrfs_log_ctx *ctx;
2863 struct btrfs_log_ctx *safe;
2865 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2866 list_del_init(&ctx->list);
2867 ctx->log_ret = error;
2870 INIT_LIST_HEAD(&root->log_ctxs[index]);
2874 * btrfs_sync_log does sends a given tree log down to the disk and
2875 * updates the super blocks to record it. When this call is done,
2876 * you know that any inodes previously logged are safely on disk only
2879 * Any other return value means you need to call btrfs_commit_transaction.
2880 * Some of the edge cases for fsyncing directories that have had unlinks
2881 * or renames done in the past mean that sometimes the only safe
2882 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2883 * that has happened.
2885 int btrfs_sync_log(struct btrfs_trans_handle *trans,
2886 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
2892 struct btrfs_fs_info *fs_info = root->fs_info;
2893 struct btrfs_root *log = root->log_root;
2894 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
2895 int log_transid = 0;
2896 struct btrfs_log_ctx root_log_ctx;
2897 struct blk_plug plug;
2899 mutex_lock(&root->log_mutex);
2900 log_transid = ctx->log_transid;
2901 if (root->log_transid_committed >= log_transid) {
2902 mutex_unlock(&root->log_mutex);
2903 return ctx->log_ret;
2906 index1 = log_transid % 2;
2907 if (atomic_read(&root->log_commit[index1])) {
2908 wait_log_commit(root, log_transid);
2909 mutex_unlock(&root->log_mutex);
2910 return ctx->log_ret;
2912 ASSERT(log_transid == root->log_transid);
2913 atomic_set(&root->log_commit[index1], 1);
2915 /* wait for previous tree log sync to complete */
2916 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
2917 wait_log_commit(root, log_transid - 1);
2920 int batch = atomic_read(&root->log_batch);
2921 /* when we're on an ssd, just kick the log commit out */
2922 if (!btrfs_test_opt(fs_info, SSD) &&
2923 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
2924 mutex_unlock(&root->log_mutex);
2925 schedule_timeout_uninterruptible(1);
2926 mutex_lock(&root->log_mutex);
2928 wait_for_writer(root);
2929 if (batch == atomic_read(&root->log_batch))
2933 /* bail out if we need to do a full commit */
2934 if (btrfs_need_log_full_commit(fs_info, trans)) {
2936 btrfs_free_logged_extents(log, log_transid);
2937 mutex_unlock(&root->log_mutex);
2941 if (log_transid % 2 == 0)
2942 mark = EXTENT_DIRTY;
2946 /* we start IO on all the marked extents here, but we don't actually
2947 * wait for them until later.
2949 blk_start_plug(&plug);
2950 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
2952 blk_finish_plug(&plug);
2953 btrfs_abort_transaction(trans, ret);
2954 btrfs_free_logged_extents(log, log_transid);
2955 btrfs_set_log_full_commit(fs_info, trans);
2956 mutex_unlock(&root->log_mutex);
2960 btrfs_set_root_node(&log->root_item, log->node);
2962 root->log_transid++;
2963 log->log_transid = root->log_transid;
2964 root->log_start_pid = 0;
2966 * IO has been started, blocks of the log tree have WRITTEN flag set
2967 * in their headers. new modifications of the log will be written to
2968 * new positions. so it's safe to allow log writers to go in.
2970 mutex_unlock(&root->log_mutex);
2972 btrfs_init_log_ctx(&root_log_ctx, NULL);
2974 mutex_lock(&log_root_tree->log_mutex);
2975 atomic_inc(&log_root_tree->log_batch);
2976 atomic_inc(&log_root_tree->log_writers);
2978 index2 = log_root_tree->log_transid % 2;
2979 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2980 root_log_ctx.log_transid = log_root_tree->log_transid;
2982 mutex_unlock(&log_root_tree->log_mutex);
2984 ret = update_log_root(trans, log);
2986 mutex_lock(&log_root_tree->log_mutex);
2987 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2988 /* atomic_dec_and_test implies a barrier */
2989 cond_wake_up_nomb(&log_root_tree->log_writer_wait);
2993 if (!list_empty(&root_log_ctx.list))
2994 list_del_init(&root_log_ctx.list);
2996 blk_finish_plug(&plug);
2997 btrfs_set_log_full_commit(fs_info, trans);
2999 if (ret != -ENOSPC) {
3000 btrfs_abort_transaction(trans, ret);
3001 mutex_unlock(&log_root_tree->log_mutex);
3004 btrfs_wait_tree_log_extents(log, mark);
3005 btrfs_free_logged_extents(log, log_transid);
3006 mutex_unlock(&log_root_tree->log_mutex);
3011 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
3012 blk_finish_plug(&plug);
3013 list_del_init(&root_log_ctx.list);
3014 mutex_unlock(&log_root_tree->log_mutex);
3015 ret = root_log_ctx.log_ret;
3019 index2 = root_log_ctx.log_transid % 2;
3020 if (atomic_read(&log_root_tree->log_commit[index2])) {
3021 blk_finish_plug(&plug);
3022 ret = btrfs_wait_tree_log_extents(log, mark);
3023 btrfs_wait_logged_extents(trans, log, log_transid);
3024 wait_log_commit(log_root_tree,
3025 root_log_ctx.log_transid);
3026 mutex_unlock(&log_root_tree->log_mutex);
3028 ret = root_log_ctx.log_ret;
3031 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
3032 atomic_set(&log_root_tree->log_commit[index2], 1);
3034 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
3035 wait_log_commit(log_root_tree,
3036 root_log_ctx.log_transid - 1);
3039 wait_for_writer(log_root_tree);
3042 * now that we've moved on to the tree of log tree roots,
3043 * check the full commit flag again
3045 if (btrfs_need_log_full_commit(fs_info, trans)) {
3046 blk_finish_plug(&plug);
3047 btrfs_wait_tree_log_extents(log, mark);
3048 btrfs_free_logged_extents(log, log_transid);
3049 mutex_unlock(&log_root_tree->log_mutex);
3051 goto out_wake_log_root;
3054 ret = btrfs_write_marked_extents(fs_info,
3055 &log_root_tree->dirty_log_pages,
3056 EXTENT_DIRTY | EXTENT_NEW);
3057 blk_finish_plug(&plug);
3059 btrfs_set_log_full_commit(fs_info, trans);
3060 btrfs_abort_transaction(trans, ret);
3061 btrfs_free_logged_extents(log, log_transid);
3062 mutex_unlock(&log_root_tree->log_mutex);
3063 goto out_wake_log_root;
3065 ret = btrfs_wait_tree_log_extents(log, mark);
3067 ret = btrfs_wait_tree_log_extents(log_root_tree,
3068 EXTENT_NEW | EXTENT_DIRTY);
3070 btrfs_set_log_full_commit(fs_info, trans);
3071 btrfs_free_logged_extents(log, log_transid);
3072 mutex_unlock(&log_root_tree->log_mutex);
3073 goto out_wake_log_root;
3075 btrfs_wait_logged_extents(trans, log, log_transid);
3077 btrfs_set_super_log_root(fs_info->super_for_commit,
3078 log_root_tree->node->start);
3079 btrfs_set_super_log_root_level(fs_info->super_for_commit,
3080 btrfs_header_level(log_root_tree->node));
3082 log_root_tree->log_transid++;
3083 mutex_unlock(&log_root_tree->log_mutex);
3086 * nobody else is going to jump in and write the the ctree
3087 * super here because the log_commit atomic below is protecting
3088 * us. We must be called with a transaction handle pinning
3089 * the running transaction open, so a full commit can't hop
3090 * in and cause problems either.
3092 ret = write_all_supers(fs_info, 1);
3094 btrfs_set_log_full_commit(fs_info, trans);
3095 btrfs_abort_transaction(trans, ret);
3096 goto out_wake_log_root;
3099 mutex_lock(&root->log_mutex);
3100 if (root->last_log_commit < log_transid)
3101 root->last_log_commit = log_transid;
3102 mutex_unlock(&root->log_mutex);
3105 mutex_lock(&log_root_tree->log_mutex);
3106 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3108 log_root_tree->log_transid_committed++;
3109 atomic_set(&log_root_tree->log_commit[index2], 0);
3110 mutex_unlock(&log_root_tree->log_mutex);
3113 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3114 * all the updates above are seen by the woken threads. It might not be
3115 * necessary, but proving that seems to be hard.
3117 cond_wake_up(&log_root_tree->log_commit_wait[index2]);
3119 mutex_lock(&root->log_mutex);
3120 btrfs_remove_all_log_ctxs(root, index1, ret);
3121 root->log_transid_committed++;
3122 atomic_set(&root->log_commit[index1], 0);
3123 mutex_unlock(&root->log_mutex);
3126 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3127 * all the updates above are seen by the woken threads. It might not be
3128 * necessary, but proving that seems to be hard.
3130 cond_wake_up(&root->log_commit_wait[index1]);
3134 static void free_log_tree(struct btrfs_trans_handle *trans,
3135 struct btrfs_root *log)
3140 struct walk_control wc = {
3142 .process_func = process_one_buffer
3145 ret = walk_log_tree(trans, log, &wc);
3146 /* I don't think this can happen but just in case */
3148 btrfs_abort_transaction(trans, ret);
3151 ret = find_first_extent_bit(&log->dirty_log_pages,
3153 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
3158 clear_extent_bits(&log->dirty_log_pages, start, end,
3159 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
3163 * We may have short-circuited the log tree with the full commit logic
3164 * and left ordered extents on our list, so clear these out to keep us
3165 * from leaking inodes and memory.
3167 btrfs_free_logged_extents(log, 0);
3168 btrfs_free_logged_extents(log, 1);
3170 free_extent_buffer(log->node);
3175 * free all the extents used by the tree log. This should be called
3176 * at commit time of the full transaction
3178 int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3180 if (root->log_root) {
3181 free_log_tree(trans, root->log_root);
3182 root->log_root = NULL;
3187 int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3188 struct btrfs_fs_info *fs_info)
3190 if (fs_info->log_root_tree) {
3191 free_log_tree(trans, fs_info->log_root_tree);
3192 fs_info->log_root_tree = NULL;
3198 * If both a file and directory are logged, and unlinks or renames are
3199 * mixed in, we have a few interesting corners:
3201 * create file X in dir Y
3202 * link file X to X.link in dir Y
3204 * unlink file X but leave X.link
3207 * After a crash we would expect only X.link to exist. But file X
3208 * didn't get fsync'd again so the log has back refs for X and X.link.
3210 * We solve this by removing directory entries and inode backrefs from the
3211 * log when a file that was logged in the current transaction is
3212 * unlinked. Any later fsync will include the updated log entries, and
3213 * we'll be able to reconstruct the proper directory items from backrefs.
3215 * This optimizations allows us to avoid relogging the entire inode
3216 * or the entire directory.
3218 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3219 struct btrfs_root *root,
3220 const char *name, int name_len,
3221 struct btrfs_inode *dir, u64 index)
3223 struct btrfs_root *log;
3224 struct btrfs_dir_item *di;
3225 struct btrfs_path *path;
3229 u64 dir_ino = btrfs_ino(dir);
3231 if (dir->logged_trans < trans->transid)
3234 ret = join_running_log_trans(root);
3238 mutex_lock(&dir->log_mutex);
3240 log = root->log_root;
3241 path = btrfs_alloc_path();
3247 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
3248 name, name_len, -1);
3254 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3255 bytes_del += name_len;
3261 btrfs_release_path(path);
3262 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
3263 index, name, name_len, -1);
3269 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3270 bytes_del += name_len;
3277 /* update the directory size in the log to reflect the names
3281 struct btrfs_key key;
3283 key.objectid = dir_ino;
3285 key.type = BTRFS_INODE_ITEM_KEY;
3286 btrfs_release_path(path);
3288 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
3294 struct btrfs_inode_item *item;
3297 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3298 struct btrfs_inode_item);
3299 i_size = btrfs_inode_size(path->nodes[0], item);
3300 if (i_size > bytes_del)
3301 i_size -= bytes_del;
3304 btrfs_set_inode_size(path->nodes[0], item, i_size);
3305 btrfs_mark_buffer_dirty(path->nodes[0]);
3308 btrfs_release_path(path);
3311 btrfs_free_path(path);
3313 mutex_unlock(&dir->log_mutex);
3314 if (ret == -ENOSPC) {
3315 btrfs_set_log_full_commit(root->fs_info, trans);
3318 btrfs_abort_transaction(trans, ret);
3320 btrfs_end_log_trans(root);
3325 /* see comments for btrfs_del_dir_entries_in_log */
3326 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3327 struct btrfs_root *root,
3328 const char *name, int name_len,
3329 struct btrfs_inode *inode, u64 dirid)
3331 struct btrfs_fs_info *fs_info = root->fs_info;
3332 struct btrfs_root *log;
3336 if (inode->logged_trans < trans->transid)
3339 ret = join_running_log_trans(root);
3342 log = root->log_root;
3343 mutex_lock(&inode->log_mutex);
3345 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
3347 mutex_unlock(&inode->log_mutex);
3348 if (ret == -ENOSPC) {
3349 btrfs_set_log_full_commit(fs_info, trans);
3351 } else if (ret < 0 && ret != -ENOENT)
3352 btrfs_abort_transaction(trans, ret);
3353 btrfs_end_log_trans(root);
3359 * creates a range item in the log for 'dirid'. first_offset and
3360 * last_offset tell us which parts of the key space the log should
3361 * be considered authoritative for.
3363 static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3364 struct btrfs_root *log,
3365 struct btrfs_path *path,
3366 int key_type, u64 dirid,
3367 u64 first_offset, u64 last_offset)
3370 struct btrfs_key key;
3371 struct btrfs_dir_log_item *item;
3373 key.objectid = dirid;
3374 key.offset = first_offset;
3375 if (key_type == BTRFS_DIR_ITEM_KEY)
3376 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3378 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3379 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
3383 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3384 struct btrfs_dir_log_item);
3385 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3386 btrfs_mark_buffer_dirty(path->nodes[0]);
3387 btrfs_release_path(path);
3392 * log all the items included in the current transaction for a given
3393 * directory. This also creates the range items in the log tree required
3394 * to replay anything deleted before the fsync
3396 static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3397 struct btrfs_root *root, struct btrfs_inode *inode,
3398 struct btrfs_path *path,
3399 struct btrfs_path *dst_path, int key_type,
3400 struct btrfs_log_ctx *ctx,
3401 u64 min_offset, u64 *last_offset_ret)
3403 struct btrfs_key min_key;
3404 struct btrfs_root *log = root->log_root;
3405 struct extent_buffer *src;
3410 u64 first_offset = min_offset;
3411 u64 last_offset = (u64)-1;
3412 u64 ino = btrfs_ino(inode);
3414 log = root->log_root;
3416 min_key.objectid = ino;
3417 min_key.type = key_type;
3418 min_key.offset = min_offset;
3420 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
3423 * we didn't find anything from this transaction, see if there
3424 * is anything at all
3426 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3427 min_key.objectid = ino;
3428 min_key.type = key_type;
3429 min_key.offset = (u64)-1;
3430 btrfs_release_path(path);
3431 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3433 btrfs_release_path(path);
3436 ret = btrfs_previous_item(root, path, ino, key_type);
3438 /* if ret == 0 there are items for this type,
3439 * create a range to tell us the last key of this type.
3440 * otherwise, there are no items in this directory after
3441 * *min_offset, and we create a range to indicate that.
3444 struct btrfs_key tmp;
3445 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3447 if (key_type == tmp.type)
3448 first_offset = max(min_offset, tmp.offset) + 1;
3453 /* go backward to find any previous key */
3454 ret = btrfs_previous_item(root, path, ino, key_type);
3456 struct btrfs_key tmp;
3457 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3458 if (key_type == tmp.type) {
3459 first_offset = tmp.offset;
3460 ret = overwrite_item(trans, log, dst_path,
3461 path->nodes[0], path->slots[0],
3469 btrfs_release_path(path);
3471 /* find the first key from this transaction again */
3472 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3473 if (WARN_ON(ret != 0))
3477 * we have a block from this transaction, log every item in it
3478 * from our directory
3481 struct btrfs_key tmp;
3482 src = path->nodes[0];
3483 nritems = btrfs_header_nritems(src);
3484 for (i = path->slots[0]; i < nritems; i++) {
3485 struct btrfs_dir_item *di;
3487 btrfs_item_key_to_cpu(src, &min_key, i);
3489 if (min_key.objectid != ino || min_key.type != key_type)
3491 ret = overwrite_item(trans, log, dst_path, src, i,
3499 * We must make sure that when we log a directory entry,
3500 * the corresponding inode, after log replay, has a
3501 * matching link count. For example:
3507 * xfs_io -c "fsync" mydir
3509 * <mount fs and log replay>
3511 * Would result in a fsync log that when replayed, our
3512 * file inode would have a link count of 1, but we get
3513 * two directory entries pointing to the same inode.
3514 * After removing one of the names, it would not be
3515 * possible to remove the other name, which resulted
3516 * always in stale file handle errors, and would not
3517 * be possible to rmdir the parent directory, since
3518 * its i_size could never decrement to the value
3519 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3521 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3522 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3524 (btrfs_dir_transid(src, di) == trans->transid ||
3525 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3526 tmp.type != BTRFS_ROOT_ITEM_KEY)
3527 ctx->log_new_dentries = true;
3529 path->slots[0] = nritems;
3532 * look ahead to the next item and see if it is also
3533 * from this directory and from this transaction
3535 ret = btrfs_next_leaf(root, path);
3538 last_offset = (u64)-1;
3543 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3544 if (tmp.objectid != ino || tmp.type != key_type) {
3545 last_offset = (u64)-1;
3548 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3549 ret = overwrite_item(trans, log, dst_path,
3550 path->nodes[0], path->slots[0],
3555 last_offset = tmp.offset;
3560 btrfs_release_path(path);
3561 btrfs_release_path(dst_path);
3564 *last_offset_ret = last_offset;
3566 * insert the log range keys to indicate where the log
3569 ret = insert_dir_log_key(trans, log, path, key_type,
3570 ino, first_offset, last_offset);
3578 * logging directories is very similar to logging inodes, We find all the items
3579 * from the current transaction and write them to the log.
3581 * The recovery code scans the directory in the subvolume, and if it finds a
3582 * key in the range logged that is not present in the log tree, then it means
3583 * that dir entry was unlinked during the transaction.
3585 * In order for that scan to work, we must include one key smaller than
3586 * the smallest logged by this transaction and one key larger than the largest
3587 * key logged by this transaction.
3589 static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3590 struct btrfs_root *root, struct btrfs_inode *inode,
3591 struct btrfs_path *path,
3592 struct btrfs_path *dst_path,
3593 struct btrfs_log_ctx *ctx)
3598 int key_type = BTRFS_DIR_ITEM_KEY;
3604 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3605 ctx, min_key, &max_key);
3608 if (max_key == (u64)-1)
3610 min_key = max_key + 1;
3613 if (key_type == BTRFS_DIR_ITEM_KEY) {
3614 key_type = BTRFS_DIR_INDEX_KEY;
3621 * a helper function to drop items from the log before we relog an
3622 * inode. max_key_type indicates the highest item type to remove.
3623 * This cannot be run for file data extents because it does not
3624 * free the extents they point to.
3626 static int drop_objectid_items(struct btrfs_trans_handle *trans,
3627 struct btrfs_root *log,
3628 struct btrfs_path *path,
3629 u64 objectid, int max_key_type)
3632 struct btrfs_key key;
3633 struct btrfs_key found_key;
3636 key.objectid = objectid;
3637 key.type = max_key_type;
3638 key.offset = (u64)-1;
3641 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
3642 BUG_ON(ret == 0); /* Logic error */
3646 if (path->slots[0] == 0)
3650 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3653 if (found_key.objectid != objectid)
3656 found_key.offset = 0;
3658 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3661 ret = btrfs_del_items(trans, log, path, start_slot,
3662 path->slots[0] - start_slot + 1);
3664 * If start slot isn't 0 then we don't need to re-search, we've
3665 * found the last guy with the objectid in this tree.
3667 if (ret || start_slot != 0)
3669 btrfs_release_path(path);
3671 btrfs_release_path(path);
3677 static void fill_inode_item(struct btrfs_trans_handle *trans,
3678 struct extent_buffer *leaf,
3679 struct btrfs_inode_item *item,
3680 struct inode *inode, int log_inode_only,
3683 struct btrfs_map_token token;
3685 btrfs_init_map_token(&token);
3687 if (log_inode_only) {
3688 /* set the generation to zero so the recover code
3689 * can tell the difference between an logging
3690 * just to say 'this inode exists' and a logging
3691 * to say 'update this inode with these values'
3693 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3694 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
3696 btrfs_set_token_inode_generation(leaf, item,
3697 BTRFS_I(inode)->generation,
3699 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
3702 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3703 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3704 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3705 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3707 btrfs_set_token_timespec_sec(leaf, &item->atime,
3708 inode->i_atime.tv_sec, &token);
3709 btrfs_set_token_timespec_nsec(leaf, &item->atime,
3710 inode->i_atime.tv_nsec, &token);
3712 btrfs_set_token_timespec_sec(leaf, &item->mtime,
3713 inode->i_mtime.tv_sec, &token);
3714 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
3715 inode->i_mtime.tv_nsec, &token);
3717 btrfs_set_token_timespec_sec(leaf, &item->ctime,
3718 inode->i_ctime.tv_sec, &token);
3719 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
3720 inode->i_ctime.tv_nsec, &token);
3722 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3725 btrfs_set_token_inode_sequence(leaf, item,
3726 inode_peek_iversion(inode), &token);
3727 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3728 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3729 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3730 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
3733 static int log_inode_item(struct btrfs_trans_handle *trans,
3734 struct btrfs_root *log, struct btrfs_path *path,
3735 struct btrfs_inode *inode)
3737 struct btrfs_inode_item *inode_item;
3740 ret = btrfs_insert_empty_item(trans, log, path,
3741 &inode->location, sizeof(*inode_item));
3742 if (ret && ret != -EEXIST)
3744 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3745 struct btrfs_inode_item);
3746 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3748 btrfs_release_path(path);
3752 static noinline int copy_items(struct btrfs_trans_handle *trans,
3753 struct btrfs_inode *inode,
3754 struct btrfs_path *dst_path,
3755 struct btrfs_path *src_path, u64 *last_extent,
3756 int start_slot, int nr, int inode_only,
3759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3760 unsigned long src_offset;
3761 unsigned long dst_offset;
3762 struct btrfs_root *log = inode->root->log_root;
3763 struct btrfs_file_extent_item *extent;
3764 struct btrfs_inode_item *inode_item;
3765 struct extent_buffer *src = src_path->nodes[0];
3766 struct btrfs_key first_key, last_key, key;
3768 struct btrfs_key *ins_keys;
3772 struct list_head ordered_sums;
3773 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
3774 bool has_extents = false;
3775 bool need_find_last_extent = true;
3778 INIT_LIST_HEAD(&ordered_sums);
3780 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3781 nr * sizeof(u32), GFP_NOFS);
3785 first_key.objectid = (u64)-1;
3787 ins_sizes = (u32 *)ins_data;
3788 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3790 for (i = 0; i < nr; i++) {
3791 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3792 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3794 ret = btrfs_insert_empty_items(trans, log, dst_path,
3795 ins_keys, ins_sizes, nr);
3801 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
3802 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3803 dst_path->slots[0]);
3805 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3808 last_key = ins_keys[i];
3810 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
3811 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3813 struct btrfs_inode_item);
3814 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3816 inode_only == LOG_INODE_EXISTS,
3819 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3820 src_offset, ins_sizes[i]);
3824 * We set need_find_last_extent here in case we know we were
3825 * processing other items and then walk into the first extent in
3826 * the inode. If we don't hit an extent then nothing changes,
3827 * we'll do the last search the next time around.
3829 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3831 if (first_key.objectid == (u64)-1)
3832 first_key = ins_keys[i];
3834 need_find_last_extent = false;
3837 /* take a reference on file data extents so that truncates
3838 * or deletes of this inode don't have to relog the inode
3841 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
3844 extent = btrfs_item_ptr(src, start_slot + i,
3845 struct btrfs_file_extent_item);
3847 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3850 found_type = btrfs_file_extent_type(src, extent);
3851 if (found_type == BTRFS_FILE_EXTENT_REG) {
3853 ds = btrfs_file_extent_disk_bytenr(src,
3855 /* ds == 0 is a hole */
3859 dl = btrfs_file_extent_disk_num_bytes(src,
3861 cs = btrfs_file_extent_offset(src, extent);
3862 cl = btrfs_file_extent_num_bytes(src,
3864 if (btrfs_file_extent_compression(src,
3870 ret = btrfs_lookup_csums_range(
3872 ds + cs, ds + cs + cl - 1,
3875 btrfs_release_path(dst_path);
3883 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
3884 btrfs_release_path(dst_path);
3888 * we have to do this after the loop above to avoid changing the
3889 * log tree while trying to change the log tree.
3892 while (!list_empty(&ordered_sums)) {
3893 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3894 struct btrfs_ordered_sum,
3897 ret = btrfs_csum_file_blocks(trans, log, sums);
3898 list_del(&sums->list);
3905 if (need_find_last_extent && *last_extent == first_key.offset) {
3907 * We don't have any leafs between our current one and the one
3908 * we processed before that can have file extent items for our
3909 * inode (and have a generation number smaller than our current
3912 need_find_last_extent = false;
3916 * Because we use btrfs_search_forward we could skip leaves that were
3917 * not modified and then assume *last_extent is valid when it really
3918 * isn't. So back up to the previous leaf and read the end of the last
3919 * extent before we go and fill in holes.
3921 if (need_find_last_extent) {
3924 ret = btrfs_prev_leaf(inode->root, src_path);
3929 if (src_path->slots[0])
3930 src_path->slots[0]--;
3931 src = src_path->nodes[0];
3932 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3933 if (key.objectid != btrfs_ino(inode) ||
3934 key.type != BTRFS_EXTENT_DATA_KEY)
3936 extent = btrfs_item_ptr(src, src_path->slots[0],
3937 struct btrfs_file_extent_item);
3938 if (btrfs_file_extent_type(src, extent) ==
3939 BTRFS_FILE_EXTENT_INLINE) {
3940 len = btrfs_file_extent_inline_len(src,
3943 *last_extent = ALIGN(key.offset + len,
3944 fs_info->sectorsize);
3946 len = btrfs_file_extent_num_bytes(src, extent);
3947 *last_extent = key.offset + len;
3951 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3952 * things could have happened
3954 * 1) A merge could have happened, so we could currently be on a leaf
3955 * that holds what we were copying in the first place.
3956 * 2) A split could have happened, and now not all of the items we want
3957 * are on the same leaf.
3959 * So we need to adjust how we search for holes, we need to drop the
3960 * path and re-search for the first extent key we found, and then walk
3961 * forward until we hit the last one we copied.
3963 if (need_find_last_extent) {
3964 /* btrfs_prev_leaf could return 1 without releasing the path */
3965 btrfs_release_path(src_path);
3966 ret = btrfs_search_slot(NULL, inode->root, &first_key,
3971 src = src_path->nodes[0];
3972 i = src_path->slots[0];
3978 * Ok so here we need to go through and fill in any holes we may have
3979 * to make sure that holes are punched for those areas in case they had
3980 * extents previously.
3986 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3987 ret = btrfs_next_leaf(inode->root, src_path);
3991 src = src_path->nodes[0];
3993 need_find_last_extent = true;
3996 btrfs_item_key_to_cpu(src, &key, i);
3997 if (!btrfs_comp_cpu_keys(&key, &last_key))
3999 if (key.objectid != btrfs_ino(inode) ||
4000 key.type != BTRFS_EXTENT_DATA_KEY) {
4004 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
4005 if (btrfs_file_extent_type(src, extent) ==
4006 BTRFS_FILE_EXTENT_INLINE) {
4007 len = btrfs_file_extent_inline_len(src, i, extent);
4008 extent_end = ALIGN(key.offset + len,
4009 fs_info->sectorsize);
4011 len = btrfs_file_extent_num_bytes(src, extent);
4012 extent_end = key.offset + len;
4016 if (*last_extent == key.offset) {
4017 *last_extent = extent_end;
4020 offset = *last_extent;
4021 len = key.offset - *last_extent;
4022 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
4023 offset, 0, 0, len, 0, len, 0, 0, 0);
4026 *last_extent = extent_end;
4030 * Check if there is a hole between the last extent found in our leaf
4031 * and the first extent in the next leaf. If there is one, we need to
4032 * log an explicit hole so that at replay time we can punch the hole.
4035 key.objectid == btrfs_ino(inode) &&
4036 key.type == BTRFS_EXTENT_DATA_KEY &&
4037 i == btrfs_header_nritems(src_path->nodes[0])) {
4038 ret = btrfs_next_leaf(inode->root, src_path);
4039 need_find_last_extent = true;
4042 } else if (ret == 0) {
4043 btrfs_item_key_to_cpu(src_path->nodes[0], &key,
4044 src_path->slots[0]);
4045 if (key.objectid == btrfs_ino(inode) &&
4046 key.type == BTRFS_EXTENT_DATA_KEY &&
4047 *last_extent < key.offset) {
4048 const u64 len = key.offset - *last_extent;
4050 ret = btrfs_insert_file_extent(trans, log,
4059 * Need to let the callers know we dropped the path so they should
4062 if (!ret && need_find_last_extent)
4067 static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
4069 struct extent_map *em1, *em2;
4071 em1 = list_entry(a, struct extent_map, list);
4072 em2 = list_entry(b, struct extent_map, list);
4074 if (em1->start < em2->start)
4076 else if (em1->start > em2->start)
4081 static int wait_ordered_extents(struct btrfs_trans_handle *trans,
4082 struct inode *inode,
4083 struct btrfs_root *root,
4084 const struct extent_map *em,
4085 const struct list_head *logged_list,
4086 bool *ordered_io_error)
4088 struct btrfs_fs_info *fs_info = root->fs_info;
4089 struct btrfs_ordered_extent *ordered;
4090 struct btrfs_root *log = root->log_root;
4091 u64 mod_start = em->mod_start;
4092 u64 mod_len = em->mod_len;
4093 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
4096 LIST_HEAD(ordered_sums);
4099 *ordered_io_error = false;
4101 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
4102 em->block_start == EXTENT_MAP_HOLE)
4106 * Wait far any ordered extent that covers our extent map. If it
4107 * finishes without an error, first check and see if our csums are on
4108 * our outstanding ordered extents.
4110 list_for_each_entry(ordered, logged_list, log_list) {
4111 struct btrfs_ordered_sum *sum;
4116 if (ordered->file_offset + ordered->len <= mod_start ||
4117 mod_start + mod_len <= ordered->file_offset)
4120 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
4121 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
4122 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
4123 const u64 start = ordered->file_offset;
4124 const u64 end = ordered->file_offset + ordered->len - 1;
4126 WARN_ON(ordered->inode != inode);
4127 filemap_fdatawrite_range(inode->i_mapping, start, end);
4130 wait_event(ordered->wait,
4131 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
4132 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
4134 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
4136 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
4137 * i_mapping flags, so that the next fsync won't get
4138 * an outdated io error too.
4140 filemap_check_errors(inode->i_mapping);
4141 *ordered_io_error = true;
4145 * We are going to copy all the csums on this ordered extent, so
4146 * go ahead and adjust mod_start and mod_len in case this
4147 * ordered extent has already been logged.
4149 if (ordered->file_offset > mod_start) {
4150 if (ordered->file_offset + ordered->len >=
4151 mod_start + mod_len)
4152 mod_len = ordered->file_offset - mod_start;
4154 * If we have this case
4156 * |--------- logged extent ---------|
4157 * |----- ordered extent ----|
4159 * Just don't mess with mod_start and mod_len, we'll
4160 * just end up logging more csums than we need and it
4164 if (ordered->file_offset + ordered->len <
4165 mod_start + mod_len) {
4166 mod_len = (mod_start + mod_len) -
4167 (ordered->file_offset + ordered->len);
4168 mod_start = ordered->file_offset +
4179 * To keep us from looping for the above case of an ordered
4180 * extent that falls inside of the logged extent.
4182 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4186 list_for_each_entry(sum, &ordered->list, list) {
4187 ret = btrfs_csum_file_blocks(trans, log, sum);
4193 if (*ordered_io_error || !mod_len || ret || skip_csum)
4196 if (em->compress_type) {
4198 csum_len = max(em->block_len, em->orig_block_len);
4200 csum_offset = mod_start - em->start;
4204 /* block start is already adjusted for the file extent offset. */
4205 ret = btrfs_lookup_csums_range(fs_info->csum_root,
4206 em->block_start + csum_offset,
4207 em->block_start + csum_offset +
4208 csum_len - 1, &ordered_sums, 0);
4212 while (!list_empty(&ordered_sums)) {
4213 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4214 struct btrfs_ordered_sum,
4217 ret = btrfs_csum_file_blocks(trans, log, sums);
4218 list_del(&sums->list);
4225 static int log_one_extent(struct btrfs_trans_handle *trans,
4226 struct btrfs_inode *inode, struct btrfs_root *root,
4227 const struct extent_map *em,
4228 struct btrfs_path *path,
4229 const struct list_head *logged_list,
4230 struct btrfs_log_ctx *ctx)
4232 struct btrfs_root *log = root->log_root;
4233 struct btrfs_file_extent_item *fi;
4234 struct extent_buffer *leaf;
4235 struct btrfs_map_token token;
4236 struct btrfs_key key;
4237 u64 extent_offset = em->start - em->orig_start;
4240 int extent_inserted = 0;
4241 bool ordered_io_err = false;
4243 ret = wait_ordered_extents(trans, &inode->vfs_inode, root, em,
4244 logged_list, &ordered_io_err);
4248 if (ordered_io_err) {
4253 btrfs_init_map_token(&token);
4255 ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
4256 em->start + em->len, NULL, 0, 1,
4257 sizeof(*fi), &extent_inserted);
4261 if (!extent_inserted) {
4262 key.objectid = btrfs_ino(inode);
4263 key.type = BTRFS_EXTENT_DATA_KEY;
4264 key.offset = em->start;
4266 ret = btrfs_insert_empty_item(trans, log, path, &key,
4271 leaf = path->nodes[0];
4272 fi = btrfs_item_ptr(leaf, path->slots[0],
4273 struct btrfs_file_extent_item);
4275 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
4277 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4278 btrfs_set_token_file_extent_type(leaf, fi,
4279 BTRFS_FILE_EXTENT_PREALLOC,
4282 btrfs_set_token_file_extent_type(leaf, fi,
4283 BTRFS_FILE_EXTENT_REG,
4286 block_len = max(em->block_len, em->orig_block_len);
4287 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4288 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4291 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4293 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4294 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4296 extent_offset, &token);
4297 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4300 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4301 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4305 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4306 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4307 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4308 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4310 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4311 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4312 btrfs_mark_buffer_dirty(leaf);
4314 btrfs_release_path(path);
4320 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4321 * lose them after doing a fast fsync and replaying the log. We scan the
4322 * subvolume's root instead of iterating the inode's extent map tree because
4323 * otherwise we can log incorrect extent items based on extent map conversion.
4324 * That can happen due to the fact that extent maps are merged when they
4325 * are not in the extent map tree's list of modified extents.
4327 static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4328 struct btrfs_inode *inode,
4329 struct btrfs_path *path)
4331 struct btrfs_root *root = inode->root;
4332 struct btrfs_key key;
4333 const u64 i_size = i_size_read(&inode->vfs_inode);
4334 const u64 ino = btrfs_ino(inode);
4335 struct btrfs_path *dst_path = NULL;
4336 u64 last_extent = (u64)-1;
4341 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4345 key.type = BTRFS_EXTENT_DATA_KEY;
4346 key.offset = i_size;
4347 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4352 struct extent_buffer *leaf = path->nodes[0];
4353 int slot = path->slots[0];
4355 if (slot >= btrfs_header_nritems(leaf)) {
4357 ret = copy_items(trans, inode, dst_path, path,
4358 &last_extent, start_slot,
4364 ret = btrfs_next_leaf(root, path);
4374 btrfs_item_key_to_cpu(leaf, &key, slot);
4375 if (key.objectid > ino)
4377 if (WARN_ON_ONCE(key.objectid < ino) ||
4378 key.type < BTRFS_EXTENT_DATA_KEY ||
4379 key.offset < i_size) {
4383 if (last_extent == (u64)-1) {
4384 last_extent = key.offset;
4386 * Avoid logging extent items logged in past fsync calls
4387 * and leading to duplicate keys in the log tree.
4390 ret = btrfs_truncate_inode_items(trans,
4394 BTRFS_EXTENT_DATA_KEY);
4395 } while (ret == -EAGAIN);
4404 dst_path = btrfs_alloc_path();
4412 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4413 start_slot, ins_nr, 1, 0);
4418 btrfs_release_path(path);
4419 btrfs_free_path(dst_path);
4423 static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4424 struct btrfs_root *root,
4425 struct btrfs_inode *inode,
4426 struct btrfs_path *path,
4427 struct list_head *logged_list,
4428 struct btrfs_log_ctx *ctx,
4432 struct extent_map *em, *n;
4433 struct list_head extents;
4434 struct extent_map_tree *tree = &inode->extent_tree;
4435 u64 logged_start, logged_end;
4440 INIT_LIST_HEAD(&extents);
4442 down_write(&inode->dio_sem);
4443 write_lock(&tree->lock);
4444 test_gen = root->fs_info->last_trans_committed;
4445 logged_start = start;
4448 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4449 list_del_init(&em->list);
4451 * Just an arbitrary number, this can be really CPU intensive
4452 * once we start getting a lot of extents, and really once we
4453 * have a bunch of extents we just want to commit since it will
4456 if (++num > 32768) {
4457 list_del_init(&tree->modified_extents);
4462 if (em->generation <= test_gen)
4465 /* We log prealloc extents beyond eof later. */
4466 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4467 em->start >= i_size_read(&inode->vfs_inode))
4470 if (em->start < logged_start)
4471 logged_start = em->start;
4472 if ((em->start + em->len - 1) > logged_end)
4473 logged_end = em->start + em->len - 1;
4475 /* Need a ref to keep it from getting evicted from cache */
4476 refcount_inc(&em->refs);
4477 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
4478 list_add_tail(&em->list, &extents);
4482 list_sort(NULL, &extents, extent_cmp);
4483 btrfs_get_logged_extents(inode, logged_list, logged_start, logged_end);
4485 * Some ordered extents started by fsync might have completed
4486 * before we could collect them into the list logged_list, which
4487 * means they're gone, not in our logged_list nor in the inode's
4488 * ordered tree. We want the application/user space to know an
4489 * error happened while attempting to persist file data so that
4490 * it can take proper action. If such error happened, we leave
4491 * without writing to the log tree and the fsync must report the
4492 * file data write error and not commit the current transaction.
4494 ret = filemap_check_errors(inode->vfs_inode.i_mapping);
4498 while (!list_empty(&extents)) {
4499 em = list_entry(extents.next, struct extent_map, list);
4501 list_del_init(&em->list);
4504 * If we had an error we just need to delete everybody from our
4508 clear_em_logging(tree, em);
4509 free_extent_map(em);
4513 write_unlock(&tree->lock);
4515 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4517 write_lock(&tree->lock);
4518 clear_em_logging(tree, em);
4519 free_extent_map(em);
4521 WARN_ON(!list_empty(&extents));
4522 write_unlock(&tree->lock);
4523 up_write(&inode->dio_sem);
4525 btrfs_release_path(path);
4527 ret = btrfs_log_prealloc_extents(trans, inode, path);
4532 static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
4533 struct btrfs_path *path, u64 *size_ret)
4535 struct btrfs_key key;
4538 key.objectid = btrfs_ino(inode);
4539 key.type = BTRFS_INODE_ITEM_KEY;
4542 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4545 } else if (ret > 0) {
4548 struct btrfs_inode_item *item;
4550 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4551 struct btrfs_inode_item);
4552 *size_ret = btrfs_inode_size(path->nodes[0], item);
4555 btrfs_release_path(path);
4560 * At the moment we always log all xattrs. This is to figure out at log replay
4561 * time which xattrs must have their deletion replayed. If a xattr is missing
4562 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4563 * because if a xattr is deleted, the inode is fsynced and a power failure
4564 * happens, causing the log to be replayed the next time the fs is mounted,
4565 * we want the xattr to not exist anymore (same behaviour as other filesystems
4566 * with a journal, ext3/4, xfs, f2fs, etc).
4568 static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4569 struct btrfs_root *root,
4570 struct btrfs_inode *inode,
4571 struct btrfs_path *path,
4572 struct btrfs_path *dst_path)
4575 struct btrfs_key key;
4576 const u64 ino = btrfs_ino(inode);
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 u64 last_extent = 0;
4597 ret = copy_items(trans, inode, dst_path, path,
4598 &last_extent, start_slot,
4600 /* can't be 1, extent items aren't processed */
4606 ret = btrfs_next_leaf(root, path);
4614 btrfs_item_key_to_cpu(leaf, &key, slot);
4615 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4625 u64 last_extent = 0;
4627 ret = copy_items(trans, inode, dst_path, path,
4628 &last_extent, start_slot,
4630 /* can't be 1, extent items aren't processed */
4640 * If the no holes feature is enabled we need to make sure any hole between the
4641 * last extent and the i_size of our inode is explicitly marked in the log. This
4642 * is to make sure that doing something like:
4644 * 1) create file with 128Kb of data
4645 * 2) truncate file to 64Kb
4646 * 3) truncate file to 256Kb
4648 * 5) <crash/power failure>
4649 * 6) mount fs and trigger log replay
4651 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4652 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4653 * file correspond to a hole. The presence of explicit holes in a log tree is
4654 * what guarantees that log replay will remove/adjust file extent items in the
4657 * Here we do not need to care about holes between extents, that is already done
4658 * by copy_items(). We also only need to do this in the full sync path, where we
4659 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4660 * lookup the list of modified extent maps and if any represents a hole, we
4661 * insert a corresponding extent representing a hole in the log tree.
4663 static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4664 struct btrfs_root *root,
4665 struct btrfs_inode *inode,
4666 struct btrfs_path *path)
4668 struct btrfs_fs_info *fs_info = root->fs_info;
4670 struct btrfs_key key;
4673 struct extent_buffer *leaf;
4674 struct btrfs_root *log = root->log_root;
4675 const u64 ino = btrfs_ino(inode);
4676 const u64 i_size = i_size_read(&inode->vfs_inode);
4678 if (!btrfs_fs_incompat(fs_info, NO_HOLES))
4682 key.type = BTRFS_EXTENT_DATA_KEY;
4683 key.offset = (u64)-1;
4685 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4690 ASSERT(path->slots[0] > 0);
4692 leaf = path->nodes[0];
4693 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4695 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4696 /* inode does not have any extents */
4700 struct btrfs_file_extent_item *extent;
4704 * If there's an extent beyond i_size, an explicit hole was
4705 * already inserted by copy_items().
4707 if (key.offset >= i_size)
4710 extent = btrfs_item_ptr(leaf, path->slots[0],
4711 struct btrfs_file_extent_item);
4713 if (btrfs_file_extent_type(leaf, extent) ==
4714 BTRFS_FILE_EXTENT_INLINE) {
4715 len = btrfs_file_extent_inline_len(leaf,
4718 ASSERT(len == i_size ||
4719 (len == fs_info->sectorsize &&
4720 btrfs_file_extent_compression(leaf, extent) !=
4721 BTRFS_COMPRESS_NONE));
4725 len = btrfs_file_extent_num_bytes(leaf, extent);
4726 /* Last extent goes beyond i_size, no need to log a hole. */
4727 if (key.offset + len > i_size)
4729 hole_start = key.offset + len;
4730 hole_size = i_size - hole_start;
4732 btrfs_release_path(path);
4734 /* Last extent ends at i_size. */
4738 hole_size = ALIGN(hole_size, fs_info->sectorsize);
4739 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4740 hole_size, 0, hole_size, 0, 0, 0);
4745 * When we are logging a new inode X, check if it doesn't have a reference that
4746 * matches the reference from some other inode Y created in a past transaction
4747 * and that was renamed in the current transaction. If we don't do this, then at
4748 * log replay time we can lose inode Y (and all its files if it's a directory):
4751 * echo "hello world" > /mnt/x/foobar
4754 * mkdir /mnt/x # or touch /mnt/x
4755 * xfs_io -c fsync /mnt/x
4757 * mount fs, trigger log replay
4759 * After the log replay procedure, we would lose the first directory and all its
4760 * files (file foobar).
4761 * For the case where inode Y is not a directory we simply end up losing it:
4763 * echo "123" > /mnt/foo
4765 * mv /mnt/foo /mnt/bar
4766 * echo "abc" > /mnt/foo
4767 * xfs_io -c fsync /mnt/foo
4770 * We also need this for cases where a snapshot entry is replaced by some other
4771 * entry (file or directory) otherwise we end up with an unreplayable log due to
4772 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4773 * if it were a regular entry:
4776 * btrfs subvolume snapshot /mnt /mnt/x/snap
4777 * btrfs subvolume delete /mnt/x/snap
4780 * fsync /mnt/x or fsync some new file inside it
4783 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4784 * the same transaction.
4786 static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4788 const struct btrfs_key *key,
4789 struct btrfs_inode *inode,
4793 struct btrfs_path *search_path;
4796 u32 item_size = btrfs_item_size_nr(eb, slot);
4798 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4800 search_path = btrfs_alloc_path();
4803 search_path->search_commit_root = 1;
4804 search_path->skip_locking = 1;
4806 while (cur_offset < item_size) {
4810 unsigned long name_ptr;
4811 struct btrfs_dir_item *di;
4813 if (key->type == BTRFS_INODE_REF_KEY) {
4814 struct btrfs_inode_ref *iref;
4816 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4817 parent = key->offset;
4818 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4819 name_ptr = (unsigned long)(iref + 1);
4820 this_len = sizeof(*iref) + this_name_len;
4822 struct btrfs_inode_extref *extref;
4824 extref = (struct btrfs_inode_extref *)(ptr +
4826 parent = btrfs_inode_extref_parent(eb, extref);
4827 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4828 name_ptr = (unsigned long)&extref->name;
4829 this_len = sizeof(*extref) + this_name_len;
4832 if (this_name_len > name_len) {
4835 new_name = krealloc(name, this_name_len, GFP_NOFS);
4840 name_len = this_name_len;
4844 read_extent_buffer(eb, name, name_ptr, this_name_len);
4845 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4846 parent, name, this_name_len, 0);
4847 if (di && !IS_ERR(di)) {
4848 struct btrfs_key di_key;
4850 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4852 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4854 *other_ino = di_key.objectid;
4859 } else if (IS_ERR(di)) {
4863 btrfs_release_path(search_path);
4865 cur_offset += this_len;
4869 btrfs_free_path(search_path);
4874 /* log a single inode in the tree log.
4875 * At least one parent directory for this inode must exist in the tree
4876 * or be logged already.
4878 * Any items from this inode changed by the current transaction are copied
4879 * to the log tree. An extra reference is taken on any extents in this
4880 * file, allowing us to avoid a whole pile of corner cases around logging
4881 * blocks that have been removed from the tree.
4883 * See LOG_INODE_ALL and related defines for a description of what inode_only
4886 * This handles both files and directories.
4888 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
4889 struct btrfs_root *root, struct btrfs_inode *inode,
4893 struct btrfs_log_ctx *ctx)
4895 struct btrfs_fs_info *fs_info = root->fs_info;
4896 struct btrfs_path *path;
4897 struct btrfs_path *dst_path;
4898 struct btrfs_key min_key;
4899 struct btrfs_key max_key;
4900 struct btrfs_root *log = root->log_root;
4901 LIST_HEAD(logged_list);
4902 u64 last_extent = 0;
4906 int ins_start_slot = 0;
4908 bool fast_search = false;
4909 u64 ino = btrfs_ino(inode);
4910 struct extent_map_tree *em_tree = &inode->extent_tree;
4911 u64 logged_isize = 0;
4912 bool need_log_inode_item = true;
4913 bool xattrs_logged = false;
4915 path = btrfs_alloc_path();
4918 dst_path = btrfs_alloc_path();
4920 btrfs_free_path(path);
4924 min_key.objectid = ino;
4925 min_key.type = BTRFS_INODE_ITEM_KEY;
4928 max_key.objectid = ino;
4931 /* today the code can only do partial logging of directories */
4932 if (S_ISDIR(inode->vfs_inode.i_mode) ||
4933 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4934 &inode->runtime_flags) &&
4935 inode_only >= LOG_INODE_EXISTS))
4936 max_key.type = BTRFS_XATTR_ITEM_KEY;
4938 max_key.type = (u8)-1;
4939 max_key.offset = (u64)-1;
4942 * Only run delayed items if we are a dir or a new file.
4943 * Otherwise commit the delayed inode only, which is needed in
4944 * order for the log replay code to mark inodes for link count
4945 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4947 if (S_ISDIR(inode->vfs_inode.i_mode) ||
4948 inode->generation > fs_info->last_trans_committed)
4949 ret = btrfs_commit_inode_delayed_items(trans, inode);
4951 ret = btrfs_commit_inode_delayed_inode(inode);
4954 btrfs_free_path(path);
4955 btrfs_free_path(dst_path);
4959 if (inode_only == LOG_OTHER_INODE) {
4960 inode_only = LOG_INODE_EXISTS;
4961 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
4963 mutex_lock(&inode->log_mutex);
4967 * a brute force approach to making sure we get the most uptodate
4968 * copies of everything.
4970 if (S_ISDIR(inode->vfs_inode.i_mode)) {
4971 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4973 if (inode_only == LOG_INODE_EXISTS)
4974 max_key_type = BTRFS_XATTR_ITEM_KEY;
4975 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
4977 if (inode_only == LOG_INODE_EXISTS) {
4979 * Make sure the new inode item we write to the log has
4980 * the same isize as the current one (if it exists).
4981 * This is necessary to prevent data loss after log
4982 * replay, and also to prevent doing a wrong expanding
4983 * truncate - for e.g. create file, write 4K into offset
4984 * 0, fsync, write 4K into offset 4096, add hard link,
4985 * fsync some other file (to sync log), power fail - if
4986 * we use the inode's current i_size, after log replay
4987 * we get a 8Kb file, with the last 4Kb extent as a hole
4988 * (zeroes), as if an expanding truncate happened,
4989 * instead of getting a file of 4Kb only.
4991 err = logged_inode_size(log, inode, path, &logged_isize);
4995 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4996 &inode->runtime_flags)) {
4997 if (inode_only == LOG_INODE_EXISTS) {
4998 max_key.type = BTRFS_XATTR_ITEM_KEY;
4999 ret = drop_objectid_items(trans, log, path, ino,
5002 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5003 &inode->runtime_flags);
5004 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
5005 &inode->runtime_flags);
5007 ret = btrfs_truncate_inode_items(trans,
5008 log, &inode->vfs_inode, 0, 0);
5013 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
5014 &inode->runtime_flags) ||
5015 inode_only == LOG_INODE_EXISTS) {
5016 if (inode_only == LOG_INODE_ALL)
5018 max_key.type = BTRFS_XATTR_ITEM_KEY;
5019 ret = drop_objectid_items(trans, log, path, ino,
5022 if (inode_only == LOG_INODE_ALL)
5035 ret = btrfs_search_forward(root, &min_key,
5036 path, trans->transid);
5044 /* note, ins_nr might be > 0 here, cleanup outside the loop */
5045 if (min_key.objectid != ino)
5047 if (min_key.type > max_key.type)
5050 if (min_key.type == BTRFS_INODE_ITEM_KEY)
5051 need_log_inode_item = false;
5053 if ((min_key.type == BTRFS_INODE_REF_KEY ||
5054 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
5055 inode->generation == trans->transid) {
5058 ret = btrfs_check_ref_name_override(path->nodes[0],
5059 path->slots[0], &min_key, inode,
5064 } else if (ret > 0 && ctx &&
5065 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
5066 struct btrfs_key inode_key;
5067 struct inode *other_inode;
5073 ins_start_slot = path->slots[0];
5075 ret = copy_items(trans, inode, dst_path, path,
5076 &last_extent, ins_start_slot,
5084 btrfs_release_path(path);
5085 inode_key.objectid = other_ino;
5086 inode_key.type = BTRFS_INODE_ITEM_KEY;
5087 inode_key.offset = 0;
5088 other_inode = btrfs_iget(fs_info->sb,
5092 * If the other inode that had a conflicting dir
5093 * entry was deleted in the current transaction,
5094 * we don't need to do more work nor fallback to
5095 * a transaction commit.
5097 if (IS_ERR(other_inode) &&
5098 PTR_ERR(other_inode) == -ENOENT) {
5100 } else if (IS_ERR(other_inode)) {
5101 err = PTR_ERR(other_inode);
5105 * We are safe logging the other inode without
5106 * acquiring its i_mutex as long as we log with
5107 * the LOG_INODE_EXISTS mode. We're safe against
5108 * concurrent renames of the other inode as well
5109 * because during a rename we pin the log and
5110 * update the log with the new name before we
5113 err = btrfs_log_inode(trans, root,
5114 BTRFS_I(other_inode),
5115 LOG_OTHER_INODE, 0, LLONG_MAX,
5125 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5126 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
5129 ret = copy_items(trans, inode, dst_path, path,
5130 &last_extent, ins_start_slot,
5131 ins_nr, inode_only, logged_isize);
5138 btrfs_release_path(path);
5144 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5147 } else if (!ins_nr) {
5148 ins_start_slot = path->slots[0];
5153 ret = copy_items(trans, inode, dst_path, path, &last_extent,
5154 ins_start_slot, ins_nr, inode_only,
5162 btrfs_release_path(path);
5166 ins_start_slot = path->slots[0];
5169 nritems = btrfs_header_nritems(path->nodes[0]);
5171 if (path->slots[0] < nritems) {
5172 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
5177 ret = copy_items(trans, inode, dst_path, path,
5178 &last_extent, ins_start_slot,
5179 ins_nr, inode_only, logged_isize);
5187 btrfs_release_path(path);
5189 if (min_key.offset < (u64)-1) {
5191 } else if (min_key.type < max_key.type) {
5199 ret = copy_items(trans, inode, dst_path, path, &last_extent,
5200 ins_start_slot, ins_nr, inode_only,
5210 btrfs_release_path(path);
5211 btrfs_release_path(dst_path);
5212 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
5215 xattrs_logged = true;
5216 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5217 btrfs_release_path(path);
5218 btrfs_release_path(dst_path);
5219 err = btrfs_log_trailing_hole(trans, root, inode, path);
5224 btrfs_release_path(path);
5225 btrfs_release_path(dst_path);
5226 if (need_log_inode_item) {
5227 err = log_inode_item(trans, log, dst_path, inode);
5228 if (!err && !xattrs_logged) {
5229 err = btrfs_log_all_xattrs(trans, root, inode, path,
5231 btrfs_release_path(path);
5237 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
5238 &logged_list, ctx, start, end);
5243 } else if (inode_only == LOG_INODE_ALL) {
5244 struct extent_map *em, *n;
5246 write_lock(&em_tree->lock);
5248 * We can't just remove every em if we're called for a ranged
5249 * fsync - that is, one that doesn't cover the whole possible
5250 * file range (0 to LLONG_MAX). This is because we can have
5251 * em's that fall outside the range we're logging and therefore
5252 * their ordered operations haven't completed yet
5253 * (btrfs_finish_ordered_io() not invoked yet). This means we
5254 * didn't get their respective file extent item in the fs/subvol
5255 * tree yet, and need to let the next fast fsync (one which
5256 * consults the list of modified extent maps) find the em so
5257 * that it logs a matching file extent item and waits for the
5258 * respective ordered operation to complete (if it's still
5261 * Removing every em outside the range we're logging would make
5262 * the next fast fsync not log their matching file extent items,
5263 * therefore making us lose data after a log replay.
5265 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
5267 const u64 mod_end = em->mod_start + em->mod_len - 1;
5269 if (em->mod_start >= start && mod_end <= end)
5270 list_del_init(&em->list);
5272 write_unlock(&em_tree->lock);
5275 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5276 ret = log_directory_changes(trans, root, inode, path, dst_path,
5284 spin_lock(&inode->lock);
5285 inode->logged_trans = trans->transid;
5286 inode->last_log_commit = inode->last_sub_trans;
5287 spin_unlock(&inode->lock);
5290 btrfs_put_logged_extents(&logged_list);
5292 btrfs_submit_logged_extents(&logged_list, log);
5293 mutex_unlock(&inode->log_mutex);
5295 btrfs_free_path(path);
5296 btrfs_free_path(dst_path);
5301 * Check if we must fallback to a transaction commit when logging an inode.
5302 * This must be called after logging the inode and is used only in the context
5303 * when fsyncing an inode requires the need to log some other inode - in which
5304 * case we can't lock the i_mutex of each other inode we need to log as that
5305 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5306 * log inodes up or down in the hierarchy) or rename operations for example. So
5307 * we take the log_mutex of the inode after we have logged it and then check for
5308 * its last_unlink_trans value - this is safe because any task setting
5309 * last_unlink_trans must take the log_mutex and it must do this before it does
5310 * the actual unlink operation, so if we do this check before a concurrent task
5311 * sets last_unlink_trans it means we've logged a consistent version/state of
5312 * all the inode items, otherwise we are not sure and must do a transaction
5313 * commit (the concurrent task might have only updated last_unlink_trans before
5314 * we logged the inode or it might have also done the unlink).
5316 static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5317 struct btrfs_inode *inode)
5319 struct btrfs_fs_info *fs_info = inode->root->fs_info;
5322 mutex_lock(&inode->log_mutex);
5323 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
5325 * Make sure any commits to the log are forced to be full
5328 btrfs_set_log_full_commit(fs_info, trans);
5331 mutex_unlock(&inode->log_mutex);
5337 * follow the dentry parent pointers up the chain and see if any
5338 * of the directories in it require a full commit before they can
5339 * be logged. Returns zero if nothing special needs to be done or 1 if
5340 * a full commit is required.
5342 static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5343 struct btrfs_inode *inode,
5344 struct dentry *parent,
5345 struct super_block *sb,
5349 struct dentry *old_parent = NULL;
5350 struct btrfs_inode *orig_inode = inode;
5353 * for regular files, if its inode is already on disk, we don't
5354 * have to worry about the parents at all. This is because
5355 * we can use the last_unlink_trans field to record renames
5356 * and other fun in this file.
5358 if (S_ISREG(inode->vfs_inode.i_mode) &&
5359 inode->generation <= last_committed &&
5360 inode->last_unlink_trans <= last_committed)
5363 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
5364 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5366 inode = BTRFS_I(d_inode(parent));
5371 * If we are logging a directory then we start with our inode,
5372 * not our parent's inode, so we need to skip setting the
5373 * logged_trans so that further down in the log code we don't
5374 * think this inode has already been logged.
5376 if (inode != orig_inode)
5377 inode->logged_trans = trans->transid;
5380 if (btrfs_must_commit_transaction(trans, inode)) {
5385 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5388 if (IS_ROOT(parent)) {
5389 inode = BTRFS_I(d_inode(parent));
5390 if (btrfs_must_commit_transaction(trans, inode))
5395 parent = dget_parent(parent);
5397 old_parent = parent;
5398 inode = BTRFS_I(d_inode(parent));
5406 struct btrfs_dir_list {
5408 struct list_head list;
5412 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5413 * details about the why it is needed.
5414 * This is a recursive operation - if an existing dentry corresponds to a
5415 * directory, that directory's new entries are logged too (same behaviour as
5416 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5417 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5418 * complains about the following circular lock dependency / possible deadlock:
5422 * lock(&type->i_mutex_dir_key#3/2);
5423 * lock(sb_internal#2);
5424 * lock(&type->i_mutex_dir_key#3/2);
5425 * lock(&sb->s_type->i_mutex_key#14);
5427 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5428 * sb_start_intwrite() in btrfs_start_transaction().
5429 * Not locking i_mutex of the inodes is still safe because:
5431 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5432 * that while logging the inode new references (names) are added or removed
5433 * from the inode, leaving the logged inode item with a link count that does
5434 * not match the number of logged inode reference items. This is fine because
5435 * at log replay time we compute the real number of links and correct the
5436 * link count in the inode item (see replay_one_buffer() and
5437 * link_to_fixup_dir());
5439 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5440 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5441 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5442 * has a size that doesn't match the sum of the lengths of all the logged
5443 * names. This does not result in a problem because if a dir_item key is
5444 * logged but its matching dir_index key is not logged, at log replay time we
5445 * don't use it to replay the respective name (see replay_one_name()). On the
5446 * other hand if only the dir_index key ends up being logged, the respective
5447 * name is added to the fs/subvol tree with both the dir_item and dir_index
5448 * keys created (see replay_one_name()).
5449 * The directory's inode item with a wrong i_size is not a problem as well,
5450 * since we don't use it at log replay time to set the i_size in the inode
5451 * item of the fs/subvol tree (see overwrite_item()).
5453 static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5454 struct btrfs_root *root,
5455 struct btrfs_inode *start_inode,
5456 struct btrfs_log_ctx *ctx)
5458 struct btrfs_fs_info *fs_info = root->fs_info;
5459 struct btrfs_root *log = root->log_root;
5460 struct btrfs_path *path;
5461 LIST_HEAD(dir_list);
5462 struct btrfs_dir_list *dir_elem;
5465 path = btrfs_alloc_path();
5469 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5471 btrfs_free_path(path);
5474 dir_elem->ino = btrfs_ino(start_inode);
5475 list_add_tail(&dir_elem->list, &dir_list);
5477 while (!list_empty(&dir_list)) {
5478 struct extent_buffer *leaf;
5479 struct btrfs_key min_key;
5483 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5486 goto next_dir_inode;
5488 min_key.objectid = dir_elem->ino;
5489 min_key.type = BTRFS_DIR_ITEM_KEY;
5492 btrfs_release_path(path);
5493 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5495 goto next_dir_inode;
5496 } else if (ret > 0) {
5498 goto next_dir_inode;
5502 leaf = path->nodes[0];
5503 nritems = btrfs_header_nritems(leaf);
5504 for (i = path->slots[0]; i < nritems; i++) {
5505 struct btrfs_dir_item *di;
5506 struct btrfs_key di_key;
5507 struct inode *di_inode;
5508 struct btrfs_dir_list *new_dir_elem;
5509 int log_mode = LOG_INODE_EXISTS;
5512 btrfs_item_key_to_cpu(leaf, &min_key, i);
5513 if (min_key.objectid != dir_elem->ino ||
5514 min_key.type != BTRFS_DIR_ITEM_KEY)
5515 goto next_dir_inode;
5517 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5518 type = btrfs_dir_type(leaf, di);
5519 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5520 type != BTRFS_FT_DIR)
5522 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5523 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5526 btrfs_release_path(path);
5527 di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
5528 if (IS_ERR(di_inode)) {
5529 ret = PTR_ERR(di_inode);
5530 goto next_dir_inode;
5533 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
5538 ctx->log_new_dentries = false;
5539 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
5540 log_mode = LOG_INODE_ALL;
5541 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
5542 log_mode, 0, LLONG_MAX, ctx);
5544 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
5548 goto next_dir_inode;
5549 if (ctx->log_new_dentries) {
5550 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5552 if (!new_dir_elem) {
5554 goto next_dir_inode;
5556 new_dir_elem->ino = di_key.objectid;
5557 list_add_tail(&new_dir_elem->list, &dir_list);
5562 ret = btrfs_next_leaf(log, path);
5564 goto next_dir_inode;
5565 } else if (ret > 0) {
5567 goto next_dir_inode;
5571 if (min_key.offset < (u64)-1) {
5576 list_del(&dir_elem->list);
5580 btrfs_free_path(path);
5584 static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5585 struct btrfs_inode *inode,
5586 struct btrfs_log_ctx *ctx)
5588 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
5590 struct btrfs_path *path;
5591 struct btrfs_key key;
5592 struct btrfs_root *root = inode->root;
5593 const u64 ino = btrfs_ino(inode);
5595 path = btrfs_alloc_path();
5598 path->skip_locking = 1;
5599 path->search_commit_root = 1;
5602 key.type = BTRFS_INODE_REF_KEY;
5604 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5609 struct extent_buffer *leaf = path->nodes[0];
5610 int slot = path->slots[0];
5615 if (slot >= btrfs_header_nritems(leaf)) {
5616 ret = btrfs_next_leaf(root, path);
5624 btrfs_item_key_to_cpu(leaf, &key, slot);
5625 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5626 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5629 item_size = btrfs_item_size_nr(leaf, slot);
5630 ptr = btrfs_item_ptr_offset(leaf, slot);
5631 while (cur_offset < item_size) {
5632 struct btrfs_key inode_key;
5633 struct inode *dir_inode;
5635 inode_key.type = BTRFS_INODE_ITEM_KEY;
5636 inode_key.offset = 0;
5638 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5639 struct btrfs_inode_extref *extref;
5641 extref = (struct btrfs_inode_extref *)
5643 inode_key.objectid = btrfs_inode_extref_parent(
5645 cur_offset += sizeof(*extref);
5646 cur_offset += btrfs_inode_extref_name_len(leaf,
5649 inode_key.objectid = key.offset;
5650 cur_offset = item_size;
5653 dir_inode = btrfs_iget(fs_info->sb, &inode_key,
5655 /* If parent inode was deleted, skip it. */
5656 if (IS_ERR(dir_inode))
5660 ctx->log_new_dentries = false;
5661 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
5662 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
5664 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
5666 if (!ret && ctx && ctx->log_new_dentries)
5667 ret = log_new_dir_dentries(trans, root,
5668 BTRFS_I(dir_inode), ctx);
5677 btrfs_free_path(path);
5682 * helper function around btrfs_log_inode to make sure newly created
5683 * parent directories also end up in the log. A minimal inode and backref
5684 * only logging is done of any parent directories that are older than
5685 * the last committed transaction
5687 static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5688 struct btrfs_inode *inode,
5689 struct dentry *parent,
5693 struct btrfs_log_ctx *ctx)
5695 struct btrfs_root *root = inode->root;
5696 struct btrfs_fs_info *fs_info = root->fs_info;
5697 struct super_block *sb;
5698 struct dentry *old_parent = NULL;
5700 u64 last_committed = fs_info->last_trans_committed;
5701 bool log_dentries = false;
5702 struct btrfs_inode *orig_inode = inode;
5704 sb = inode->vfs_inode.i_sb;
5706 if (btrfs_test_opt(fs_info, NOTREELOG)) {
5712 * The prev transaction commit doesn't complete, we need do
5713 * full commit by ourselves.
5715 if (fs_info->last_trans_log_full_commit >
5716 fs_info->last_trans_committed) {
5721 if (btrfs_root_refs(&root->root_item) == 0) {
5726 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
5731 if (btrfs_inode_in_log(inode, trans->transid)) {
5732 ret = BTRFS_NO_LOG_SYNC;
5736 ret = start_log_trans(trans, root, ctx);
5740 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
5745 * for regular files, if its inode is already on disk, we don't
5746 * have to worry about the parents at all. This is because
5747 * we can use the last_unlink_trans field to record renames
5748 * and other fun in this file.
5750 if (S_ISREG(inode->vfs_inode.i_mode) &&
5751 inode->generation <= last_committed &&
5752 inode->last_unlink_trans <= last_committed) {
5757 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
5758 log_dentries = true;
5761 * On unlink we must make sure all our current and old parent directory
5762 * inodes are fully logged. This is to prevent leaving dangling
5763 * directory index entries in directories that were our parents but are
5764 * not anymore. Not doing this results in old parent directory being
5765 * impossible to delete after log replay (rmdir will always fail with
5766 * error -ENOTEMPTY).
5772 * ln testdir/foo testdir/bar
5774 * unlink testdir/bar
5775 * xfs_io -c fsync testdir/foo
5777 * mount fs, triggers log replay
5779 * If we don't log the parent directory (testdir), after log replay the
5780 * directory still has an entry pointing to the file inode using the bar
5781 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5782 * the file inode has a link count of 1.
5788 * ln foo testdir/foo2
5789 * ln foo testdir/foo3
5791 * unlink testdir/foo3
5792 * xfs_io -c fsync foo
5794 * mount fs, triggers log replay
5796 * Similar as the first example, after log replay the parent directory
5797 * testdir still has an entry pointing to the inode file with name foo3
5798 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5799 * and has a link count of 2.
5801 if (inode->last_unlink_trans > last_committed) {
5802 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5808 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5811 inode = BTRFS_I(d_inode(parent));
5812 if (root != inode->root)
5815 if (inode->generation > last_committed) {
5816 ret = btrfs_log_inode(trans, root, inode,
5817 LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
5821 if (IS_ROOT(parent))
5824 parent = dget_parent(parent);
5826 old_parent = parent;
5829 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5835 btrfs_set_log_full_commit(fs_info, trans);
5840 btrfs_remove_log_ctx(root, ctx);
5841 btrfs_end_log_trans(root);
5847 * it is not safe to log dentry if the chunk root has added new
5848 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5849 * If this returns 1, you must commit the transaction to safely get your
5852 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
5853 struct dentry *dentry,
5856 struct btrfs_log_ctx *ctx)
5858 struct dentry *parent = dget_parent(dentry);
5861 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
5862 start, end, LOG_INODE_ALL, ctx);
5869 * should be called during mount to recover any replay any log trees
5872 int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5875 struct btrfs_path *path;
5876 struct btrfs_trans_handle *trans;
5877 struct btrfs_key key;
5878 struct btrfs_key found_key;
5879 struct btrfs_key tmp_key;
5880 struct btrfs_root *log;
5881 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5882 struct walk_control wc = {
5883 .process_func = process_one_buffer,
5887 path = btrfs_alloc_path();
5891 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
5893 trans = btrfs_start_transaction(fs_info->tree_root, 0);
5894 if (IS_ERR(trans)) {
5895 ret = PTR_ERR(trans);
5902 ret = walk_log_tree(trans, log_root_tree, &wc);
5904 btrfs_handle_fs_error(fs_info, ret,
5905 "Failed to pin buffers while recovering log root tree.");
5910 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5911 key.offset = (u64)-1;
5912 key.type = BTRFS_ROOT_ITEM_KEY;
5915 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
5918 btrfs_handle_fs_error(fs_info, ret,
5919 "Couldn't find tree log root.");
5923 if (path->slots[0] == 0)
5927 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5929 btrfs_release_path(path);
5930 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5933 log = btrfs_read_fs_root(log_root_tree, &found_key);
5936 btrfs_handle_fs_error(fs_info, ret,
5937 "Couldn't read tree log root.");
5941 tmp_key.objectid = found_key.offset;
5942 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5943 tmp_key.offset = (u64)-1;
5945 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
5946 if (IS_ERR(wc.replay_dest)) {
5947 ret = PTR_ERR(wc.replay_dest);
5948 free_extent_buffer(log->node);
5949 free_extent_buffer(log->commit_root);
5951 btrfs_handle_fs_error(fs_info, ret,
5952 "Couldn't read target root for tree log recovery.");
5956 wc.replay_dest->log_root = log;
5957 btrfs_record_root_in_trans(trans, wc.replay_dest);
5958 ret = walk_log_tree(trans, log, &wc);
5960 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5961 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5965 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5966 struct btrfs_root *root = wc.replay_dest;
5968 btrfs_release_path(path);
5971 * We have just replayed everything, and the highest
5972 * objectid of fs roots probably has changed in case
5973 * some inode_item's got replayed.
5975 * root->objectid_mutex is not acquired as log replay
5976 * could only happen during mount.
5978 ret = btrfs_find_highest_objectid(root,
5979 &root->highest_objectid);
5982 key.offset = found_key.offset - 1;
5983 wc.replay_dest->log_root = NULL;
5984 free_extent_buffer(log->node);
5985 free_extent_buffer(log->commit_root);
5991 if (found_key.offset == 0)
5994 btrfs_release_path(path);
5996 /* step one is to pin it all, step two is to replay just inodes */
5999 wc.process_func = replay_one_buffer;
6000 wc.stage = LOG_WALK_REPLAY_INODES;
6003 /* step three is to replay everything */
6004 if (wc.stage < LOG_WALK_REPLAY_ALL) {
6009 btrfs_free_path(path);
6011 /* step 4: commit the transaction, which also unpins the blocks */
6012 ret = btrfs_commit_transaction(trans);
6016 free_extent_buffer(log_root_tree->node);
6017 log_root_tree->log_root = NULL;
6018 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
6019 kfree(log_root_tree);
6024 btrfs_end_transaction(wc.trans);
6025 btrfs_free_path(path);
6030 * there are some corner cases where we want to force a full
6031 * commit instead of allowing a directory to be logged.
6033 * They revolve around files there were unlinked from the directory, and
6034 * this function updates the parent directory so that a full commit is
6035 * properly done if it is fsync'd later after the unlinks are done.
6037 * Must be called before the unlink operations (updates to the subvolume tree,
6038 * inodes, etc) are done.
6040 void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
6041 struct btrfs_inode *dir, struct btrfs_inode *inode,
6045 * when we're logging a file, if it hasn't been renamed
6046 * or unlinked, and its inode is fully committed on disk,
6047 * we don't have to worry about walking up the directory chain
6048 * to log its parents.
6050 * So, we use the last_unlink_trans field to put this transid
6051 * into the file. When the file is logged we check it and
6052 * don't log the parents if the file is fully on disk.
6054 mutex_lock(&inode->log_mutex);
6055 inode->last_unlink_trans = trans->transid;
6056 mutex_unlock(&inode->log_mutex);
6059 * if this directory was already logged any new
6060 * names for this file/dir will get recorded
6063 if (dir->logged_trans == trans->transid)
6067 * if the inode we're about to unlink was logged,
6068 * the log will be properly updated for any new names
6070 if (inode->logged_trans == trans->transid)
6074 * when renaming files across directories, if the directory
6075 * there we're unlinking from gets fsync'd later on, there's
6076 * no way to find the destination directory later and fsync it
6077 * properly. So, we have to be conservative and force commits
6078 * so the new name gets discovered.
6083 /* we can safely do the unlink without any special recording */
6087 mutex_lock(&dir->log_mutex);
6088 dir->last_unlink_trans = trans->transid;
6089 mutex_unlock(&dir->log_mutex);
6093 * Make sure that if someone attempts to fsync the parent directory of a deleted
6094 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6095 * that after replaying the log tree of the parent directory's root we will not
6096 * see the snapshot anymore and at log replay time we will not see any log tree
6097 * corresponding to the deleted snapshot's root, which could lead to replaying
6098 * it after replaying the log tree of the parent directory (which would replay
6099 * the snapshot delete operation).
6101 * Must be called before the actual snapshot destroy operation (updates to the
6102 * parent root and tree of tree roots trees, etc) are done.
6104 void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
6105 struct btrfs_inode *dir)
6107 mutex_lock(&dir->log_mutex);
6108 dir->last_unlink_trans = trans->transid;
6109 mutex_unlock(&dir->log_mutex);
6113 * Call this after adding a new name for a file and it will properly
6114 * update the log to reflect the new name.
6116 * It will return zero if all goes well, and it will return 1 if a
6117 * full transaction commit is required.
6119 int btrfs_log_new_name(struct btrfs_trans_handle *trans,
6120 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
6121 struct dentry *parent)
6123 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
6126 * this will force the logging code to walk the dentry chain
6129 if (!S_ISDIR(inode->vfs_inode.i_mode))
6130 inode->last_unlink_trans = trans->transid;
6133 * if this inode hasn't been logged and directory we're renaming it
6134 * from hasn't been logged, we don't need to log it
6136 if (inode->logged_trans <= fs_info->last_trans_committed &&
6137 (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
6140 return btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6141 LOG_INODE_EXISTS, NULL);