2 * Copyright (C) 2008 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/blkdev.h>
22 #include <linux/list_sort.h>
26 #include "print-tree.h"
29 #include "compression.h"
32 /* magic values for the inode_only field in btrfs_log_inode:
34 * LOG_INODE_ALL means to log everything
35 * LOG_INODE_EXISTS means to log just enough to recreate the inode
38 #define LOG_INODE_ALL 0
39 #define LOG_INODE_EXISTS 1
42 * directory trouble cases
44 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
45 * log, we must force a full commit before doing an fsync of the directory
46 * where the unlink was done.
47 * ---> record transid of last unlink/rename per directory
51 * rename foo/some_dir foo2/some_dir
53 * fsync foo/some_dir/some_file
55 * The fsync above will unlink the original some_dir without recording
56 * it in its new location (foo2). After a crash, some_dir will be gone
57 * unless the fsync of some_file forces a full commit
59 * 2) we must log any new names for any file or dir that is in the fsync
60 * log. ---> check inode while renaming/linking.
62 * 2a) we must log any new names for any file or dir during rename
63 * when the directory they are being removed from was logged.
64 * ---> check inode and old parent dir during rename
66 * 2a is actually the more important variant. With the extra logging
67 * a crash might unlink the old name without recreating the new one
69 * 3) after a crash, we must go through any directories with a link count
70 * of zero and redo the rm -rf
77 * The directory f1 was fully removed from the FS, but fsync was never
78 * called on f1, only its parent dir. After a crash the rm -rf must
79 * be replayed. This must be able to recurse down the entire
80 * directory tree. The inode link count fixup code takes care of the
85 * stages for the tree walking. The first
86 * stage (0) is to only pin down the blocks we find
87 * the second stage (1) is to make sure that all the inodes
88 * we find in the log are created in the subvolume.
90 * The last stage is to deal with directories and links and extents
91 * and all the other fun semantics
93 #define LOG_WALK_PIN_ONLY 0
94 #define LOG_WALK_REPLAY_INODES 1
95 #define LOG_WALK_REPLAY_DIR_INDEX 2
96 #define LOG_WALK_REPLAY_ALL 3
98 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
99 struct btrfs_root *root, struct inode *inode,
103 struct btrfs_log_ctx *ctx);
104 static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
105 struct btrfs_root *root,
106 struct btrfs_path *path, u64 objectid);
107 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
108 struct btrfs_root *root,
109 struct btrfs_root *log,
110 struct btrfs_path *path,
111 u64 dirid, int del_all);
114 * tree logging is a special write ahead log used to make sure that
115 * fsyncs and O_SYNCs can happen without doing full tree commits.
117 * Full tree commits are expensive because they require commonly
118 * modified blocks to be recowed, creating many dirty pages in the
119 * extent tree an 4x-6x higher write load than ext3.
121 * Instead of doing a tree commit on every fsync, we use the
122 * key ranges and transaction ids to find items for a given file or directory
123 * that have changed in this transaction. Those items are copied into
124 * a special tree (one per subvolume root), that tree is written to disk
125 * and then the fsync is considered complete.
127 * After a crash, items are copied out of the log-tree back into the
128 * subvolume tree. Any file data extents found are recorded in the extent
129 * allocation tree, and the log-tree freed.
131 * The log tree is read three times, once to pin down all the extents it is
132 * using in ram and once, once to create all the inodes logged in the tree
133 * and once to do all the other items.
137 * start a sub transaction and setup the log tree
138 * this increments the log tree writer count to make the people
139 * syncing the tree wait for us to finish
141 static int start_log_trans(struct btrfs_trans_handle *trans,
142 struct btrfs_root *root,
143 struct btrfs_log_ctx *ctx)
147 mutex_lock(&root->log_mutex);
149 if (root->log_root) {
150 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
155 if (!root->log_start_pid) {
156 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
157 root->log_start_pid = current->pid;
158 } else if (root->log_start_pid != current->pid) {
159 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
162 mutex_lock(&root->fs_info->tree_log_mutex);
163 if (!root->fs_info->log_root_tree)
164 ret = btrfs_init_log_root_tree(trans, root->fs_info);
165 mutex_unlock(&root->fs_info->tree_log_mutex);
169 ret = btrfs_add_log_tree(trans, root);
173 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
174 root->log_start_pid = current->pid;
177 atomic_inc(&root->log_batch);
178 atomic_inc(&root->log_writers);
180 int index = root->log_transid % 2;
181 list_add_tail(&ctx->list, &root->log_ctxs[index]);
182 ctx->log_transid = root->log_transid;
186 mutex_unlock(&root->log_mutex);
191 * returns 0 if there was a log transaction running and we were able
192 * to join, or returns -ENOENT if there were not transactions
195 static int join_running_log_trans(struct btrfs_root *root)
203 mutex_lock(&root->log_mutex);
204 if (root->log_root) {
206 atomic_inc(&root->log_writers);
208 mutex_unlock(&root->log_mutex);
213 * This either makes the current running log transaction wait
214 * until you call btrfs_end_log_trans() or it makes any future
215 * log transactions wait until you call btrfs_end_log_trans()
217 int btrfs_pin_log_trans(struct btrfs_root *root)
221 mutex_lock(&root->log_mutex);
222 atomic_inc(&root->log_writers);
223 mutex_unlock(&root->log_mutex);
228 * indicate we're done making changes to the log tree
229 * and wake up anyone waiting to do a sync
231 void btrfs_end_log_trans(struct btrfs_root *root)
233 if (atomic_dec_and_test(&root->log_writers)) {
235 * Implicit memory barrier after atomic_dec_and_test
237 if (waitqueue_active(&root->log_writer_wait))
238 wake_up(&root->log_writer_wait);
244 * the walk control struct is used to pass state down the chain when
245 * processing the log tree. The stage field tells us which part
246 * of the log tree processing we are currently doing. The others
247 * are state fields used for that specific part
249 struct walk_control {
250 /* should we free the extent on disk when done? This is used
251 * at transaction commit time while freeing a log tree
255 /* should we write out the extent buffer? This is used
256 * while flushing the log tree to disk during a sync
260 /* should we wait for the extent buffer io to finish? Also used
261 * while flushing the log tree to disk for a sync
265 /* pin only walk, we record which extents on disk belong to the
270 /* what stage of the replay code we're currently in */
273 /* the root we are currently replaying */
274 struct btrfs_root *replay_dest;
276 /* the trans handle for the current replay */
277 struct btrfs_trans_handle *trans;
279 /* the function that gets used to process blocks we find in the
280 * tree. Note the extent_buffer might not be up to date when it is
281 * passed in, and it must be checked or read if you need the data
284 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
285 struct walk_control *wc, u64 gen);
289 * process_func used to pin down extents, write them or wait on them
291 static int process_one_buffer(struct btrfs_root *log,
292 struct extent_buffer *eb,
293 struct walk_control *wc, u64 gen)
298 * If this fs is mixed then we need to be able to process the leaves to
299 * pin down any logged extents, so we have to read the block.
301 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
302 ret = btrfs_read_buffer(eb, gen);
308 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
311 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
312 if (wc->pin && btrfs_header_level(eb) == 0)
313 ret = btrfs_exclude_logged_extents(log, eb);
315 btrfs_write_tree_block(eb);
317 btrfs_wait_tree_block_writeback(eb);
323 * Item overwrite used by replay and tree logging. eb, slot and key all refer
324 * to the src data we are copying out.
326 * root is the tree we are copying into, and path is a scratch
327 * path for use in this function (it should be released on entry and
328 * will be released on exit).
330 * If the key is already in the destination tree the existing item is
331 * overwritten. If the existing item isn't big enough, it is extended.
332 * If it is too large, it is truncated.
334 * If the key isn't in the destination yet, a new item is inserted.
336 static noinline int overwrite_item(struct btrfs_trans_handle *trans,
337 struct btrfs_root *root,
338 struct btrfs_path *path,
339 struct extent_buffer *eb, int slot,
340 struct btrfs_key *key)
344 u64 saved_i_size = 0;
345 int save_old_i_size = 0;
346 unsigned long src_ptr;
347 unsigned long dst_ptr;
348 int overwrite_root = 0;
349 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
351 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
354 item_size = btrfs_item_size_nr(eb, slot);
355 src_ptr = btrfs_item_ptr_offset(eb, slot);
357 /* look for the key in the destination tree */
358 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
365 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
367 if (dst_size != item_size)
370 if (item_size == 0) {
371 btrfs_release_path(path);
374 dst_copy = kmalloc(item_size, GFP_NOFS);
375 src_copy = kmalloc(item_size, GFP_NOFS);
376 if (!dst_copy || !src_copy) {
377 btrfs_release_path(path);
383 read_extent_buffer(eb, src_copy, src_ptr, item_size);
385 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
386 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
388 ret = memcmp(dst_copy, src_copy, item_size);
393 * they have the same contents, just return, this saves
394 * us from cowing blocks in the destination tree and doing
395 * extra writes that may not have been done by a previous
399 btrfs_release_path(path);
404 * We need to load the old nbytes into the inode so when we
405 * replay the extents we've logged we get the right nbytes.
408 struct btrfs_inode_item *item;
412 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
413 struct btrfs_inode_item);
414 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
415 item = btrfs_item_ptr(eb, slot,
416 struct btrfs_inode_item);
417 btrfs_set_inode_nbytes(eb, item, nbytes);
420 * If this is a directory we need to reset the i_size to
421 * 0 so that we can set it up properly when replaying
422 * the rest of the items in this log.
424 mode = btrfs_inode_mode(eb, item);
426 btrfs_set_inode_size(eb, item, 0);
428 } else if (inode_item) {
429 struct btrfs_inode_item *item;
433 * New inode, set nbytes to 0 so that the nbytes comes out
434 * properly when we replay the extents.
436 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
437 btrfs_set_inode_nbytes(eb, item, 0);
440 * If this is a directory we need to reset the i_size to 0 so
441 * that we can set it up properly when replaying the rest of
442 * the items in this log.
444 mode = btrfs_inode_mode(eb, item);
446 btrfs_set_inode_size(eb, item, 0);
449 btrfs_release_path(path);
450 /* try to insert the key into the destination tree */
451 path->skip_release_on_error = 1;
452 ret = btrfs_insert_empty_item(trans, root, path,
454 path->skip_release_on_error = 0;
456 /* make sure any existing item is the correct size */
457 if (ret == -EEXIST || ret == -EOVERFLOW) {
459 found_size = btrfs_item_size_nr(path->nodes[0],
461 if (found_size > item_size)
462 btrfs_truncate_item(root, path, item_size, 1);
463 else if (found_size < item_size)
464 btrfs_extend_item(root, path,
465 item_size - found_size);
469 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
472 /* don't overwrite an existing inode if the generation number
473 * was logged as zero. This is done when the tree logging code
474 * is just logging an inode to make sure it exists after recovery.
476 * Also, don't overwrite i_size on directories during replay.
477 * log replay inserts and removes directory items based on the
478 * state of the tree found in the subvolume, and i_size is modified
481 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
482 struct btrfs_inode_item *src_item;
483 struct btrfs_inode_item *dst_item;
485 src_item = (struct btrfs_inode_item *)src_ptr;
486 dst_item = (struct btrfs_inode_item *)dst_ptr;
488 if (btrfs_inode_generation(eb, src_item) == 0) {
489 struct extent_buffer *dst_eb = path->nodes[0];
490 const u64 ino_size = btrfs_inode_size(eb, src_item);
493 * For regular files an ino_size == 0 is used only when
494 * logging that an inode exists, as part of a directory
495 * fsync, and the inode wasn't fsynced before. In this
496 * case don't set the size of the inode in the fs/subvol
497 * tree, otherwise we would be throwing valid data away.
499 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
500 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
502 struct btrfs_map_token token;
504 btrfs_init_map_token(&token);
505 btrfs_set_token_inode_size(dst_eb, dst_item,
511 if (overwrite_root &&
512 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
513 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
515 saved_i_size = btrfs_inode_size(path->nodes[0],
520 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
523 if (save_old_i_size) {
524 struct btrfs_inode_item *dst_item;
525 dst_item = (struct btrfs_inode_item *)dst_ptr;
526 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
529 /* make sure the generation is filled in */
530 if (key->type == BTRFS_INODE_ITEM_KEY) {
531 struct btrfs_inode_item *dst_item;
532 dst_item = (struct btrfs_inode_item *)dst_ptr;
533 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
534 btrfs_set_inode_generation(path->nodes[0], dst_item,
539 btrfs_mark_buffer_dirty(path->nodes[0]);
540 btrfs_release_path(path);
545 * simple helper to read an inode off the disk from a given root
546 * This can only be called for subvolume roots and not for the log
548 static noinline struct inode *read_one_inode(struct btrfs_root *root,
551 struct btrfs_key key;
554 key.objectid = objectid;
555 key.type = BTRFS_INODE_ITEM_KEY;
557 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
560 } else if (is_bad_inode(inode)) {
567 /* replays a single extent in 'eb' at 'slot' with 'key' into the
568 * subvolume 'root'. path is released on entry and should be released
571 * extents in the log tree have not been allocated out of the extent
572 * tree yet. So, this completes the allocation, taking a reference
573 * as required if the extent already exists or creating a new extent
574 * if it isn't in the extent allocation tree yet.
576 * The extent is inserted into the file, dropping any existing extents
577 * from the file that overlap the new one.
579 static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
580 struct btrfs_root *root,
581 struct btrfs_path *path,
582 struct extent_buffer *eb, int slot,
583 struct btrfs_key *key)
587 u64 start = key->offset;
589 struct btrfs_file_extent_item *item;
590 struct inode *inode = NULL;
594 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
595 found_type = btrfs_file_extent_type(eb, item);
597 if (found_type == BTRFS_FILE_EXTENT_REG ||
598 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
599 nbytes = btrfs_file_extent_num_bytes(eb, item);
600 extent_end = start + nbytes;
603 * We don't add to the inodes nbytes if we are prealloc or a
606 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
608 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
609 size = btrfs_file_extent_inline_len(eb, slot, item);
610 nbytes = btrfs_file_extent_ram_bytes(eb, item);
611 extent_end = ALIGN(start + size, root->sectorsize);
617 inode = read_one_inode(root, key->objectid);
624 * first check to see if we already have this extent in the
625 * file. This must be done before the btrfs_drop_extents run
626 * so we don't try to drop this extent.
628 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
632 (found_type == BTRFS_FILE_EXTENT_REG ||
633 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
634 struct btrfs_file_extent_item cmp1;
635 struct btrfs_file_extent_item cmp2;
636 struct btrfs_file_extent_item *existing;
637 struct extent_buffer *leaf;
639 leaf = path->nodes[0];
640 existing = btrfs_item_ptr(leaf, path->slots[0],
641 struct btrfs_file_extent_item);
643 read_extent_buffer(eb, &cmp1, (unsigned long)item,
645 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
649 * we already have a pointer to this exact extent,
650 * we don't have to do anything
652 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
653 btrfs_release_path(path);
657 btrfs_release_path(path);
659 /* drop any overlapping extents */
660 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
664 if (found_type == BTRFS_FILE_EXTENT_REG ||
665 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
667 unsigned long dest_offset;
668 struct btrfs_key ins;
670 ret = btrfs_insert_empty_item(trans, root, path, key,
674 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
676 copy_extent_buffer(path->nodes[0], eb, dest_offset,
677 (unsigned long)item, sizeof(*item));
679 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
680 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
681 ins.type = BTRFS_EXTENT_ITEM_KEY;
682 offset = key->offset - btrfs_file_extent_offset(eb, item);
685 * Manually record dirty extent, as here we did a shallow
686 * file extent item copy and skip normal backref update,
687 * but modifying extent tree all by ourselves.
688 * So need to manually record dirty extent for qgroup,
689 * as the owner of the file extent changed from log tree
690 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
692 ret = btrfs_qgroup_insert_dirty_extent(trans, root->fs_info,
693 btrfs_file_extent_disk_bytenr(eb, item),
694 btrfs_file_extent_disk_num_bytes(eb, item),
699 if (ins.objectid > 0) {
702 LIST_HEAD(ordered_sums);
704 * is this extent already allocated in the extent
705 * allocation tree? If so, just add a reference
707 ret = btrfs_lookup_data_extent(root, ins.objectid,
710 ret = btrfs_inc_extent_ref(trans, root,
711 ins.objectid, ins.offset,
712 0, root->root_key.objectid,
713 key->objectid, offset);
718 * insert the extent pointer in the extent
721 ret = btrfs_alloc_logged_file_extent(trans,
722 root, root->root_key.objectid,
723 key->objectid, offset, &ins);
727 btrfs_release_path(path);
729 if (btrfs_file_extent_compression(eb, item)) {
730 csum_start = ins.objectid;
731 csum_end = csum_start + ins.offset;
733 csum_start = ins.objectid +
734 btrfs_file_extent_offset(eb, item);
735 csum_end = csum_start +
736 btrfs_file_extent_num_bytes(eb, item);
739 ret = btrfs_lookup_csums_range(root->log_root,
740 csum_start, csum_end - 1,
745 * Now delete all existing cums in the csum root that
746 * cover our range. We do this because we can have an
747 * extent that is completely referenced by one file
748 * extent item and partially referenced by another
749 * file extent item (like after using the clone or
750 * extent_same ioctls). In this case if we end up doing
751 * the replay of the one that partially references the
752 * extent first, and we do not do the csum deletion
753 * below, we can get 2 csum items in the csum tree that
754 * overlap each other. For example, imagine our log has
755 * the two following file extent items:
757 * key (257 EXTENT_DATA 409600)
758 * extent data disk byte 12845056 nr 102400
759 * extent data offset 20480 nr 20480 ram 102400
761 * key (257 EXTENT_DATA 819200)
762 * extent data disk byte 12845056 nr 102400
763 * extent data offset 0 nr 102400 ram 102400
765 * Where the second one fully references the 100K extent
766 * that starts at disk byte 12845056, and the log tree
767 * has a single csum item that covers the entire range
770 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
772 * After the first file extent item is replayed, the
773 * csum tree gets the following csum item:
775 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
777 * Which covers the 20K sub-range starting at offset 20K
778 * of our extent. Now when we replay the second file
779 * extent item, if we do not delete existing csum items
780 * that cover any of its blocks, we end up getting two
781 * csum items in our csum tree that overlap each other:
783 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
784 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
786 * Which is a problem, because after this anyone trying
787 * to lookup up for the checksum of any block of our
788 * extent starting at an offset of 40K or higher, will
789 * end up looking at the second csum item only, which
790 * does not contain the checksum for any block starting
791 * at offset 40K or higher of our extent.
793 while (!list_empty(&ordered_sums)) {
794 struct btrfs_ordered_sum *sums;
795 sums = list_entry(ordered_sums.next,
796 struct btrfs_ordered_sum,
799 ret = btrfs_del_csums(trans,
800 root->fs_info->csum_root,
804 ret = btrfs_csum_file_blocks(trans,
805 root->fs_info->csum_root,
807 list_del(&sums->list);
813 btrfs_release_path(path);
815 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
816 /* inline extents are easy, we just overwrite them */
817 ret = overwrite_item(trans, root, path, eb, slot, key);
822 inode_add_bytes(inode, nbytes);
823 ret = btrfs_update_inode(trans, root, inode);
831 * when cleaning up conflicts between the directory names in the
832 * subvolume, directory names in the log and directory names in the
833 * inode back references, we may have to unlink inodes from directories.
835 * This is a helper function to do the unlink of a specific directory
838 static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
839 struct btrfs_root *root,
840 struct btrfs_path *path,
842 struct btrfs_dir_item *di)
847 struct extent_buffer *leaf;
848 struct btrfs_key location;
851 leaf = path->nodes[0];
853 btrfs_dir_item_key_to_cpu(leaf, di, &location);
854 name_len = btrfs_dir_name_len(leaf, di);
855 name = kmalloc(name_len, GFP_NOFS);
859 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
860 btrfs_release_path(path);
862 inode = read_one_inode(root, location.objectid);
868 ret = link_to_fixup_dir(trans, root, path, location.objectid);
872 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
876 ret = btrfs_run_delayed_items(trans, root);
884 * helper function to see if a given name and sequence number found
885 * in an inode back reference are already in a directory and correctly
886 * point to this inode
888 static noinline int inode_in_dir(struct btrfs_root *root,
889 struct btrfs_path *path,
890 u64 dirid, u64 objectid, u64 index,
891 const char *name, int name_len)
893 struct btrfs_dir_item *di;
894 struct btrfs_key location;
897 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
898 index, name, name_len, 0);
899 if (di && !IS_ERR(di)) {
900 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
901 if (location.objectid != objectid)
905 btrfs_release_path(path);
907 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
908 if (di && !IS_ERR(di)) {
909 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
910 if (location.objectid != objectid)
916 btrfs_release_path(path);
921 * helper function to check a log tree for a named back reference in
922 * an inode. This is used to decide if a back reference that is
923 * found in the subvolume conflicts with what we find in the log.
925 * inode backreferences may have multiple refs in a single item,
926 * during replay we process one reference at a time, and we don't
927 * want to delete valid links to a file from the subvolume if that
928 * link is also in the log.
930 static noinline int backref_in_log(struct btrfs_root *log,
931 struct btrfs_key *key,
933 const char *name, int namelen)
935 struct btrfs_path *path;
936 struct btrfs_inode_ref *ref;
938 unsigned long ptr_end;
939 unsigned long name_ptr;
945 path = btrfs_alloc_path();
949 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
953 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
955 if (key->type == BTRFS_INODE_EXTREF_KEY) {
956 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
957 name, namelen, NULL))
963 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
964 ptr_end = ptr + item_size;
965 while (ptr < ptr_end) {
966 ref = (struct btrfs_inode_ref *)ptr;
967 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
968 if (found_name_len == namelen) {
969 name_ptr = (unsigned long)(ref + 1);
970 ret = memcmp_extent_buffer(path->nodes[0], name,
977 ptr = (unsigned long)(ref + 1) + found_name_len;
980 btrfs_free_path(path);
984 static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
985 struct btrfs_root *root,
986 struct btrfs_path *path,
987 struct btrfs_root *log_root,
988 struct inode *dir, struct inode *inode,
989 struct extent_buffer *eb,
990 u64 inode_objectid, u64 parent_objectid,
991 u64 ref_index, char *name, int namelen,
997 struct extent_buffer *leaf;
998 struct btrfs_dir_item *di;
999 struct btrfs_key search_key;
1000 struct btrfs_inode_extref *extref;
1003 /* Search old style refs */
1004 search_key.objectid = inode_objectid;
1005 search_key.type = BTRFS_INODE_REF_KEY;
1006 search_key.offset = parent_objectid;
1007 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
1009 struct btrfs_inode_ref *victim_ref;
1011 unsigned long ptr_end;
1013 leaf = path->nodes[0];
1015 /* are we trying to overwrite a back ref for the root directory
1016 * if so, just jump out, we're done
1018 if (search_key.objectid == search_key.offset)
1021 /* check all the names in this back reference to see
1022 * if they are in the log. if so, we allow them to stay
1023 * otherwise they must be unlinked as a conflict
1025 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1026 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1027 while (ptr < ptr_end) {
1028 victim_ref = (struct btrfs_inode_ref *)ptr;
1029 victim_name_len = btrfs_inode_ref_name_len(leaf,
1031 victim_name = kmalloc(victim_name_len, GFP_NOFS);
1035 read_extent_buffer(leaf, victim_name,
1036 (unsigned long)(victim_ref + 1),
1039 if (!backref_in_log(log_root, &search_key,
1044 btrfs_release_path(path);
1046 ret = btrfs_unlink_inode(trans, root, dir,
1052 ret = btrfs_run_delayed_items(trans, root);
1060 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1064 * NOTE: we have searched root tree and checked the
1065 * corresponding ref, it does not need to check again.
1069 btrfs_release_path(path);
1071 /* Same search but for extended refs */
1072 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1073 inode_objectid, parent_objectid, 0,
1075 if (!IS_ERR_OR_NULL(extref)) {
1079 struct inode *victim_parent;
1081 leaf = path->nodes[0];
1083 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1084 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1086 while (cur_offset < item_size) {
1087 extref = (struct btrfs_inode_extref *)(base + cur_offset);
1089 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1091 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1094 victim_name = kmalloc(victim_name_len, GFP_NOFS);
1097 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1100 search_key.objectid = inode_objectid;
1101 search_key.type = BTRFS_INODE_EXTREF_KEY;
1102 search_key.offset = btrfs_extref_hash(parent_objectid,
1106 if (!backref_in_log(log_root, &search_key,
1107 parent_objectid, victim_name,
1110 victim_parent = read_one_inode(root,
1112 if (victim_parent) {
1114 btrfs_release_path(path);
1116 ret = btrfs_unlink_inode(trans, root,
1122 ret = btrfs_run_delayed_items(
1125 iput(victim_parent);
1136 cur_offset += victim_name_len + sizeof(*extref);
1140 btrfs_release_path(path);
1142 /* look for a conflicting sequence number */
1143 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
1144 ref_index, name, namelen, 0);
1145 if (di && !IS_ERR(di)) {
1146 ret = drop_one_dir_item(trans, root, path, dir, di);
1150 btrfs_release_path(path);
1152 /* look for a conflicing name */
1153 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1155 if (di && !IS_ERR(di)) {
1156 ret = drop_one_dir_item(trans, root, path, dir, di);
1160 btrfs_release_path(path);
1165 static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1166 u32 *namelen, char **name, u64 *index,
1167 u64 *parent_objectid)
1169 struct btrfs_inode_extref *extref;
1171 extref = (struct btrfs_inode_extref *)ref_ptr;
1173 *namelen = btrfs_inode_extref_name_len(eb, extref);
1174 *name = kmalloc(*namelen, GFP_NOFS);
1178 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1181 *index = btrfs_inode_extref_index(eb, extref);
1182 if (parent_objectid)
1183 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1188 static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1189 u32 *namelen, char **name, u64 *index)
1191 struct btrfs_inode_ref *ref;
1193 ref = (struct btrfs_inode_ref *)ref_ptr;
1195 *namelen = btrfs_inode_ref_name_len(eb, ref);
1196 *name = kmalloc(*namelen, GFP_NOFS);
1200 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1202 *index = btrfs_inode_ref_index(eb, ref);
1208 * replay one inode back reference item found in the log tree.
1209 * eb, slot and key refer to the buffer and key found in the log tree.
1210 * root is the destination we are replaying into, and path is for temp
1211 * use by this function. (it should be released on return).
1213 static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1214 struct btrfs_root *root,
1215 struct btrfs_root *log,
1216 struct btrfs_path *path,
1217 struct extent_buffer *eb, int slot,
1218 struct btrfs_key *key)
1220 struct inode *dir = NULL;
1221 struct inode *inode = NULL;
1222 unsigned long ref_ptr;
1223 unsigned long ref_end;
1227 int search_done = 0;
1228 int log_ref_ver = 0;
1229 u64 parent_objectid;
1232 int ref_struct_size;
1234 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1235 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1237 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1238 struct btrfs_inode_extref *r;
1240 ref_struct_size = sizeof(struct btrfs_inode_extref);
1242 r = (struct btrfs_inode_extref *)ref_ptr;
1243 parent_objectid = btrfs_inode_extref_parent(eb, r);
1245 ref_struct_size = sizeof(struct btrfs_inode_ref);
1246 parent_objectid = key->offset;
1248 inode_objectid = key->objectid;
1251 * it is possible that we didn't log all the parent directories
1252 * for a given inode. If we don't find the dir, just don't
1253 * copy the back ref in. The link count fixup code will take
1256 dir = read_one_inode(root, parent_objectid);
1262 inode = read_one_inode(root, inode_objectid);
1268 while (ref_ptr < ref_end) {
1270 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1271 &ref_index, &parent_objectid);
1273 * parent object can change from one array
1277 dir = read_one_inode(root, parent_objectid);
1283 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1289 /* if we already have a perfect match, we're done */
1290 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
1291 ref_index, name, namelen)) {
1293 * look for a conflicting back reference in the
1294 * metadata. if we find one we have to unlink that name
1295 * of the file before we add our new link. Later on, we
1296 * overwrite any existing back reference, and we don't
1297 * want to create dangling pointers in the directory.
1301 ret = __add_inode_ref(trans, root, path, log,
1305 ref_index, name, namelen,
1314 /* insert our name */
1315 ret = btrfs_add_link(trans, dir, inode, name, namelen,
1320 btrfs_update_inode(trans, root, inode);
1323 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
1332 /* finally write the back reference in the inode */
1333 ret = overwrite_item(trans, root, path, eb, slot, key);
1335 btrfs_release_path(path);
1342 static int insert_orphan_item(struct btrfs_trans_handle *trans,
1343 struct btrfs_root *root, u64 ino)
1347 ret = btrfs_insert_orphan_item(trans, root, ino);
1354 static int count_inode_extrefs(struct btrfs_root *root,
1355 struct inode *inode, struct btrfs_path *path)
1359 unsigned int nlink = 0;
1362 u64 inode_objectid = btrfs_ino(inode);
1365 struct btrfs_inode_extref *extref;
1366 struct extent_buffer *leaf;
1369 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1374 leaf = path->nodes[0];
1375 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1376 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1379 while (cur_offset < item_size) {
1380 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1381 name_len = btrfs_inode_extref_name_len(leaf, extref);
1385 cur_offset += name_len + sizeof(*extref);
1389 btrfs_release_path(path);
1391 btrfs_release_path(path);
1393 if (ret < 0 && ret != -ENOENT)
1398 static int count_inode_refs(struct btrfs_root *root,
1399 struct inode *inode, struct btrfs_path *path)
1402 struct btrfs_key key;
1403 unsigned int nlink = 0;
1405 unsigned long ptr_end;
1407 u64 ino = btrfs_ino(inode);
1410 key.type = BTRFS_INODE_REF_KEY;
1411 key.offset = (u64)-1;
1414 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1418 if (path->slots[0] == 0)
1423 btrfs_item_key_to_cpu(path->nodes[0], &key,
1425 if (key.objectid != ino ||
1426 key.type != BTRFS_INODE_REF_KEY)
1428 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1429 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1431 while (ptr < ptr_end) {
1432 struct btrfs_inode_ref *ref;
1434 ref = (struct btrfs_inode_ref *)ptr;
1435 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1437 ptr = (unsigned long)(ref + 1) + name_len;
1441 if (key.offset == 0)
1443 if (path->slots[0] > 0) {
1448 btrfs_release_path(path);
1450 btrfs_release_path(path);
1456 * There are a few corners where the link count of the file can't
1457 * be properly maintained during replay. So, instead of adding
1458 * lots of complexity to the log code, we just scan the backrefs
1459 * for any file that has been through replay.
1461 * The scan will update the link count on the inode to reflect the
1462 * number of back refs found. If it goes down to zero, the iput
1463 * will free the inode.
1465 static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1466 struct btrfs_root *root,
1467 struct inode *inode)
1469 struct btrfs_path *path;
1472 u64 ino = btrfs_ino(inode);
1474 path = btrfs_alloc_path();
1478 ret = count_inode_refs(root, inode, path);
1484 ret = count_inode_extrefs(root, inode, path);
1492 if (nlink != inode->i_nlink) {
1493 set_nlink(inode, nlink);
1494 btrfs_update_inode(trans, root, inode);
1496 BTRFS_I(inode)->index_cnt = (u64)-1;
1498 if (inode->i_nlink == 0) {
1499 if (S_ISDIR(inode->i_mode)) {
1500 ret = replay_dir_deletes(trans, root, NULL, path,
1505 ret = insert_orphan_item(trans, root, ino);
1509 btrfs_free_path(path);
1513 static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1514 struct btrfs_root *root,
1515 struct btrfs_path *path)
1518 struct btrfs_key key;
1519 struct inode *inode;
1521 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1522 key.type = BTRFS_ORPHAN_ITEM_KEY;
1523 key.offset = (u64)-1;
1525 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1530 if (path->slots[0] == 0)
1535 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1536 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1537 key.type != BTRFS_ORPHAN_ITEM_KEY)
1540 ret = btrfs_del_item(trans, root, path);
1544 btrfs_release_path(path);
1545 inode = read_one_inode(root, key.offset);
1549 ret = fixup_inode_link_count(trans, root, inode);
1555 * fixup on a directory may create new entries,
1556 * make sure we always look for the highset possible
1559 key.offset = (u64)-1;
1563 btrfs_release_path(path);
1569 * record a given inode in the fixup dir so we can check its link
1570 * count when replay is done. The link count is incremented here
1571 * so the inode won't go away until we check it
1573 static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1574 struct btrfs_root *root,
1575 struct btrfs_path *path,
1578 struct btrfs_key key;
1580 struct inode *inode;
1582 inode = read_one_inode(root, objectid);
1586 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1587 key.type = BTRFS_ORPHAN_ITEM_KEY;
1588 key.offset = objectid;
1590 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1592 btrfs_release_path(path);
1594 if (!inode->i_nlink)
1595 set_nlink(inode, 1);
1598 ret = btrfs_update_inode(trans, root, inode);
1599 } else if (ret == -EEXIST) {
1602 BUG(); /* Logic Error */
1610 * when replaying the log for a directory, we only insert names
1611 * for inodes that actually exist. This means an fsync on a directory
1612 * does not implicitly fsync all the new files in it
1614 static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1615 struct btrfs_root *root,
1616 u64 dirid, u64 index,
1617 char *name, int name_len,
1618 struct btrfs_key *location)
1620 struct inode *inode;
1624 inode = read_one_inode(root, location->objectid);
1628 dir = read_one_inode(root, dirid);
1634 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1636 /* FIXME, put inode into FIXUP list */
1644 * Return true if an inode reference exists in the log for the given name,
1645 * inode and parent inode.
1647 static bool name_in_log_ref(struct btrfs_root *log_root,
1648 const char *name, const int name_len,
1649 const u64 dirid, const u64 ino)
1651 struct btrfs_key search_key;
1653 search_key.objectid = ino;
1654 search_key.type = BTRFS_INODE_REF_KEY;
1655 search_key.offset = dirid;
1656 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1659 search_key.type = BTRFS_INODE_EXTREF_KEY;
1660 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1661 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1668 * take a single entry in a log directory item and replay it into
1671 * if a conflicting item exists in the subdirectory already,
1672 * the inode it points to is unlinked and put into the link count
1675 * If a name from the log points to a file or directory that does
1676 * not exist in the FS, it is skipped. fsyncs on directories
1677 * do not force down inodes inside that directory, just changes to the
1678 * names or unlinks in a directory.
1680 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1681 * non-existing inode) and 1 if the name was replayed.
1683 static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1684 struct btrfs_root *root,
1685 struct btrfs_path *path,
1686 struct extent_buffer *eb,
1687 struct btrfs_dir_item *di,
1688 struct btrfs_key *key)
1692 struct btrfs_dir_item *dst_di;
1693 struct btrfs_key found_key;
1694 struct btrfs_key log_key;
1699 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
1700 bool name_added = false;
1702 dir = read_one_inode(root, key->objectid);
1706 name_len = btrfs_dir_name_len(eb, di);
1707 name = kmalloc(name_len, GFP_NOFS);
1713 log_type = btrfs_dir_type(eb, di);
1714 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1717 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
1718 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1723 btrfs_release_path(path);
1725 if (key->type == BTRFS_DIR_ITEM_KEY) {
1726 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1728 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
1729 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1738 if (IS_ERR_OR_NULL(dst_di)) {
1739 /* we need a sequence number to insert, so we only
1740 * do inserts for the BTRFS_DIR_INDEX_KEY types
1742 if (key->type != BTRFS_DIR_INDEX_KEY)
1747 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1748 /* the existing item matches the logged item */
1749 if (found_key.objectid == log_key.objectid &&
1750 found_key.type == log_key.type &&
1751 found_key.offset == log_key.offset &&
1752 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1753 update_size = false;
1758 * don't drop the conflicting directory entry if the inode
1759 * for the new entry doesn't exist
1764 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1768 if (key->type == BTRFS_DIR_INDEX_KEY)
1771 btrfs_release_path(path);
1772 if (!ret && update_size) {
1773 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1774 ret = btrfs_update_inode(trans, root, dir);
1778 if (!ret && name_added)
1783 if (name_in_log_ref(root->log_root, name, name_len,
1784 key->objectid, log_key.objectid)) {
1785 /* The dentry will be added later. */
1787 update_size = false;
1790 btrfs_release_path(path);
1791 ret = insert_one_name(trans, root, key->objectid, key->offset,
1792 name, name_len, &log_key);
1793 if (ret && ret != -ENOENT && ret != -EEXIST)
1797 update_size = false;
1803 * find all the names in a directory item and reconcile them into
1804 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1805 * one name in a directory item, but the same code gets used for
1806 * both directory index types
1808 static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1809 struct btrfs_root *root,
1810 struct btrfs_path *path,
1811 struct extent_buffer *eb, int slot,
1812 struct btrfs_key *key)
1815 u32 item_size = btrfs_item_size_nr(eb, slot);
1816 struct btrfs_dir_item *di;
1819 unsigned long ptr_end;
1820 struct btrfs_path *fixup_path = NULL;
1822 ptr = btrfs_item_ptr_offset(eb, slot);
1823 ptr_end = ptr + item_size;
1824 while (ptr < ptr_end) {
1825 di = (struct btrfs_dir_item *)ptr;
1826 if (verify_dir_item(root, eb, di))
1828 name_len = btrfs_dir_name_len(eb, di);
1829 ret = replay_one_name(trans, root, path, eb, di, key);
1832 ptr = (unsigned long)(di + 1);
1836 * If this entry refers to a non-directory (directories can not
1837 * have a link count > 1) and it was added in the transaction
1838 * that was not committed, make sure we fixup the link count of
1839 * the inode it the entry points to. Otherwise something like
1840 * the following would result in a directory pointing to an
1841 * inode with a wrong link that does not account for this dir
1849 * ln testdir/bar testdir/bar_link
1850 * ln testdir/foo testdir/foo_link
1851 * xfs_io -c "fsync" testdir/bar
1855 * mount fs, log replay happens
1857 * File foo would remain with a link count of 1 when it has two
1858 * entries pointing to it in the directory testdir. This would
1859 * make it impossible to ever delete the parent directory has
1860 * it would result in stale dentries that can never be deleted.
1862 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1863 struct btrfs_key di_key;
1866 fixup_path = btrfs_alloc_path();
1873 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1874 ret = link_to_fixup_dir(trans, root, fixup_path,
1881 btrfs_free_path(fixup_path);
1886 * directory replay has two parts. There are the standard directory
1887 * items in the log copied from the subvolume, and range items
1888 * created in the log while the subvolume was logged.
1890 * The range items tell us which parts of the key space the log
1891 * is authoritative for. During replay, if a key in the subvolume
1892 * directory is in a logged range item, but not actually in the log
1893 * that means it was deleted from the directory before the fsync
1894 * and should be removed.
1896 static noinline int find_dir_range(struct btrfs_root *root,
1897 struct btrfs_path *path,
1898 u64 dirid, int key_type,
1899 u64 *start_ret, u64 *end_ret)
1901 struct btrfs_key key;
1903 struct btrfs_dir_log_item *item;
1907 if (*start_ret == (u64)-1)
1910 key.objectid = dirid;
1911 key.type = key_type;
1912 key.offset = *start_ret;
1914 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1918 if (path->slots[0] == 0)
1923 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1925 if (key.type != key_type || key.objectid != dirid) {
1929 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1930 struct btrfs_dir_log_item);
1931 found_end = btrfs_dir_log_end(path->nodes[0], item);
1933 if (*start_ret >= key.offset && *start_ret <= found_end) {
1935 *start_ret = key.offset;
1936 *end_ret = found_end;
1941 /* check the next slot in the tree to see if it is a valid item */
1942 nritems = btrfs_header_nritems(path->nodes[0]);
1943 if (path->slots[0] >= nritems) {
1944 ret = btrfs_next_leaf(root, path);
1951 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1953 if (key.type != key_type || key.objectid != dirid) {
1957 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1958 struct btrfs_dir_log_item);
1959 found_end = btrfs_dir_log_end(path->nodes[0], item);
1960 *start_ret = key.offset;
1961 *end_ret = found_end;
1964 btrfs_release_path(path);
1969 * this looks for a given directory item in the log. If the directory
1970 * item is not in the log, the item is removed and the inode it points
1973 static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1974 struct btrfs_root *root,
1975 struct btrfs_root *log,
1976 struct btrfs_path *path,
1977 struct btrfs_path *log_path,
1979 struct btrfs_key *dir_key)
1982 struct extent_buffer *eb;
1985 struct btrfs_dir_item *di;
1986 struct btrfs_dir_item *log_di;
1989 unsigned long ptr_end;
1991 struct inode *inode;
1992 struct btrfs_key location;
1995 eb = path->nodes[0];
1996 slot = path->slots[0];
1997 item_size = btrfs_item_size_nr(eb, slot);
1998 ptr = btrfs_item_ptr_offset(eb, slot);
1999 ptr_end = ptr + item_size;
2000 while (ptr < ptr_end) {
2001 di = (struct btrfs_dir_item *)ptr;
2002 if (verify_dir_item(root, eb, di)) {
2007 name_len = btrfs_dir_name_len(eb, di);
2008 name = kmalloc(name_len, GFP_NOFS);
2013 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2016 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
2017 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2020 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
2021 log_di = btrfs_lookup_dir_index_item(trans, log,
2027 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
2028 btrfs_dir_item_key_to_cpu(eb, di, &location);
2029 btrfs_release_path(path);
2030 btrfs_release_path(log_path);
2031 inode = read_one_inode(root, location.objectid);
2037 ret = link_to_fixup_dir(trans, root,
2038 path, location.objectid);
2046 ret = btrfs_unlink_inode(trans, root, dir, inode,
2049 ret = btrfs_run_delayed_items(trans, root);
2055 /* there might still be more names under this key
2056 * check and repeat if required
2058 ret = btrfs_search_slot(NULL, root, dir_key, path,
2064 } else if (IS_ERR(log_di)) {
2066 return PTR_ERR(log_di);
2068 btrfs_release_path(log_path);
2071 ptr = (unsigned long)(di + 1);
2076 btrfs_release_path(path);
2077 btrfs_release_path(log_path);
2081 static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2082 struct btrfs_root *root,
2083 struct btrfs_root *log,
2084 struct btrfs_path *path,
2087 struct btrfs_key search_key;
2088 struct btrfs_path *log_path;
2093 log_path = btrfs_alloc_path();
2097 search_key.objectid = ino;
2098 search_key.type = BTRFS_XATTR_ITEM_KEY;
2099 search_key.offset = 0;
2101 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2105 nritems = btrfs_header_nritems(path->nodes[0]);
2106 for (i = path->slots[0]; i < nritems; i++) {
2107 struct btrfs_key key;
2108 struct btrfs_dir_item *di;
2109 struct btrfs_dir_item *log_di;
2113 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2114 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2119 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2120 total_size = btrfs_item_size_nr(path->nodes[0], i);
2122 while (cur < total_size) {
2123 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2124 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2125 u32 this_len = sizeof(*di) + name_len + data_len;
2128 name = kmalloc(name_len, GFP_NOFS);
2133 read_extent_buffer(path->nodes[0], name,
2134 (unsigned long)(di + 1), name_len);
2136 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2138 btrfs_release_path(log_path);
2140 /* Doesn't exist in log tree, so delete it. */
2141 btrfs_release_path(path);
2142 di = btrfs_lookup_xattr(trans, root, path, ino,
2143 name, name_len, -1);
2150 ret = btrfs_delete_one_dir_name(trans, root,
2154 btrfs_release_path(path);
2159 if (IS_ERR(log_di)) {
2160 ret = PTR_ERR(log_di);
2164 di = (struct btrfs_dir_item *)((char *)di + this_len);
2167 ret = btrfs_next_leaf(root, path);
2173 btrfs_free_path(log_path);
2174 btrfs_release_path(path);
2180 * deletion replay happens before we copy any new directory items
2181 * out of the log or out of backreferences from inodes. It
2182 * scans the log to find ranges of keys that log is authoritative for,
2183 * and then scans the directory to find items in those ranges that are
2184 * not present in the log.
2186 * Anything we don't find in the log is unlinked and removed from the
2189 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2190 struct btrfs_root *root,
2191 struct btrfs_root *log,
2192 struct btrfs_path *path,
2193 u64 dirid, int del_all)
2197 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2199 struct btrfs_key dir_key;
2200 struct btrfs_key found_key;
2201 struct btrfs_path *log_path;
2204 dir_key.objectid = dirid;
2205 dir_key.type = BTRFS_DIR_ITEM_KEY;
2206 log_path = btrfs_alloc_path();
2210 dir = read_one_inode(root, dirid);
2211 /* it isn't an error if the inode isn't there, that can happen
2212 * because we replay the deletes before we copy in the inode item
2216 btrfs_free_path(log_path);
2224 range_end = (u64)-1;
2226 ret = find_dir_range(log, path, dirid, key_type,
2227 &range_start, &range_end);
2232 dir_key.offset = range_start;
2235 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2240 nritems = btrfs_header_nritems(path->nodes[0]);
2241 if (path->slots[0] >= nritems) {
2242 ret = btrfs_next_leaf(root, path);
2246 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2248 if (found_key.objectid != dirid ||
2249 found_key.type != dir_key.type)
2252 if (found_key.offset > range_end)
2255 ret = check_item_in_log(trans, root, log, path,
2260 if (found_key.offset == (u64)-1)
2262 dir_key.offset = found_key.offset + 1;
2264 btrfs_release_path(path);
2265 if (range_end == (u64)-1)
2267 range_start = range_end + 1;
2272 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2273 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2274 dir_key.type = BTRFS_DIR_INDEX_KEY;
2275 btrfs_release_path(path);
2279 btrfs_release_path(path);
2280 btrfs_free_path(log_path);
2286 * the process_func used to replay items from the log tree. This
2287 * gets called in two different stages. The first stage just looks
2288 * for inodes and makes sure they are all copied into the subvolume.
2290 * The second stage copies all the other item types from the log into
2291 * the subvolume. The two stage approach is slower, but gets rid of
2292 * lots of complexity around inodes referencing other inodes that exist
2293 * only in the log (references come from either directory items or inode
2296 static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2297 struct walk_control *wc, u64 gen)
2300 struct btrfs_path *path;
2301 struct btrfs_root *root = wc->replay_dest;
2302 struct btrfs_key key;
2307 ret = btrfs_read_buffer(eb, gen);
2311 level = btrfs_header_level(eb);
2316 path = btrfs_alloc_path();
2320 nritems = btrfs_header_nritems(eb);
2321 for (i = 0; i < nritems; i++) {
2322 btrfs_item_key_to_cpu(eb, &key, i);
2324 /* inode keys are done during the first stage */
2325 if (key.type == BTRFS_INODE_ITEM_KEY &&
2326 wc->stage == LOG_WALK_REPLAY_INODES) {
2327 struct btrfs_inode_item *inode_item;
2330 inode_item = btrfs_item_ptr(eb, i,
2331 struct btrfs_inode_item);
2332 ret = replay_xattr_deletes(wc->trans, root, log,
2333 path, key.objectid);
2336 mode = btrfs_inode_mode(eb, inode_item);
2337 if (S_ISDIR(mode)) {
2338 ret = replay_dir_deletes(wc->trans,
2339 root, log, path, key.objectid, 0);
2343 ret = overwrite_item(wc->trans, root, path,
2348 /* for regular files, make sure corresponding
2349 * orphan item exist. extents past the new EOF
2350 * will be truncated later by orphan cleanup.
2352 if (S_ISREG(mode)) {
2353 ret = insert_orphan_item(wc->trans, root,
2359 ret = link_to_fixup_dir(wc->trans, root,
2360 path, key.objectid);
2365 if (key.type == BTRFS_DIR_INDEX_KEY &&
2366 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2367 ret = replay_one_dir_item(wc->trans, root, path,
2373 if (wc->stage < LOG_WALK_REPLAY_ALL)
2376 /* these keys are simply copied */
2377 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2378 ret = overwrite_item(wc->trans, root, path,
2382 } else if (key.type == BTRFS_INODE_REF_KEY ||
2383 key.type == BTRFS_INODE_EXTREF_KEY) {
2384 ret = add_inode_ref(wc->trans, root, log, path,
2386 if (ret && ret != -ENOENT)
2389 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2390 ret = replay_one_extent(wc->trans, root, path,
2394 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
2395 ret = replay_one_dir_item(wc->trans, root, path,
2401 btrfs_free_path(path);
2405 static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2406 struct btrfs_root *root,
2407 struct btrfs_path *path, int *level,
2408 struct walk_control *wc)
2413 struct extent_buffer *next;
2414 struct extent_buffer *cur;
2415 struct extent_buffer *parent;
2419 WARN_ON(*level < 0);
2420 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2422 while (*level > 0) {
2423 WARN_ON(*level < 0);
2424 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2425 cur = path->nodes[*level];
2427 WARN_ON(btrfs_header_level(cur) != *level);
2429 if (path->slots[*level] >=
2430 btrfs_header_nritems(cur))
2433 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2434 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2435 blocksize = root->nodesize;
2437 parent = path->nodes[*level];
2438 root_owner = btrfs_header_owner(parent);
2440 next = btrfs_find_create_tree_block(root, bytenr);
2442 return PTR_ERR(next);
2445 ret = wc->process_func(root, next, wc, ptr_gen);
2447 free_extent_buffer(next);
2451 path->slots[*level]++;
2453 ret = btrfs_read_buffer(next, ptr_gen);
2455 free_extent_buffer(next);
2460 btrfs_tree_lock(next);
2461 btrfs_set_lock_blocking(next);
2462 clean_tree_block(trans, root->fs_info,
2464 btrfs_wait_tree_block_writeback(next);
2465 btrfs_tree_unlock(next);
2468 WARN_ON(root_owner !=
2469 BTRFS_TREE_LOG_OBJECTID);
2470 ret = btrfs_free_and_pin_reserved_extent(root,
2473 free_extent_buffer(next);
2477 free_extent_buffer(next);
2480 ret = btrfs_read_buffer(next, ptr_gen);
2482 free_extent_buffer(next);
2486 WARN_ON(*level <= 0);
2487 if (path->nodes[*level-1])
2488 free_extent_buffer(path->nodes[*level-1]);
2489 path->nodes[*level-1] = next;
2490 *level = btrfs_header_level(next);
2491 path->slots[*level] = 0;
2494 WARN_ON(*level < 0);
2495 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2497 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
2503 static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
2504 struct btrfs_root *root,
2505 struct btrfs_path *path, int *level,
2506 struct walk_control *wc)
2513 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2514 slot = path->slots[i];
2515 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
2518 WARN_ON(*level == 0);
2521 struct extent_buffer *parent;
2522 if (path->nodes[*level] == root->node)
2523 parent = path->nodes[*level];
2525 parent = path->nodes[*level + 1];
2527 root_owner = btrfs_header_owner(parent);
2528 ret = wc->process_func(root, path->nodes[*level], wc,
2529 btrfs_header_generation(path->nodes[*level]));
2534 struct extent_buffer *next;
2536 next = path->nodes[*level];
2539 btrfs_tree_lock(next);
2540 btrfs_set_lock_blocking(next);
2541 clean_tree_block(trans, root->fs_info,
2543 btrfs_wait_tree_block_writeback(next);
2544 btrfs_tree_unlock(next);
2547 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
2548 ret = btrfs_free_and_pin_reserved_extent(root,
2549 path->nodes[*level]->start,
2550 path->nodes[*level]->len);
2554 free_extent_buffer(path->nodes[*level]);
2555 path->nodes[*level] = NULL;
2563 * drop the reference count on the tree rooted at 'snap'. This traverses
2564 * the tree freeing any blocks that have a ref count of zero after being
2567 static int walk_log_tree(struct btrfs_trans_handle *trans,
2568 struct btrfs_root *log, struct walk_control *wc)
2573 struct btrfs_path *path;
2576 path = btrfs_alloc_path();
2580 level = btrfs_header_level(log->node);
2582 path->nodes[level] = log->node;
2583 extent_buffer_get(log->node);
2584 path->slots[level] = 0;
2587 wret = walk_down_log_tree(trans, log, path, &level, wc);
2595 wret = walk_up_log_tree(trans, log, path, &level, wc);
2604 /* was the root node processed? if not, catch it here */
2605 if (path->nodes[orig_level]) {
2606 ret = wc->process_func(log, path->nodes[orig_level], wc,
2607 btrfs_header_generation(path->nodes[orig_level]));
2611 struct extent_buffer *next;
2613 next = path->nodes[orig_level];
2616 btrfs_tree_lock(next);
2617 btrfs_set_lock_blocking(next);
2618 clean_tree_block(trans, log->fs_info, next);
2619 btrfs_wait_tree_block_writeback(next);
2620 btrfs_tree_unlock(next);
2623 WARN_ON(log->root_key.objectid !=
2624 BTRFS_TREE_LOG_OBJECTID);
2625 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
2633 btrfs_free_path(path);
2638 * helper function to update the item for a given subvolumes log root
2639 * in the tree of log roots
2641 static int update_log_root(struct btrfs_trans_handle *trans,
2642 struct btrfs_root *log)
2646 if (log->log_transid == 1) {
2647 /* insert root item on the first sync */
2648 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2649 &log->root_key, &log->root_item);
2651 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2652 &log->root_key, &log->root_item);
2657 static void wait_log_commit(struct btrfs_root *root, int transid)
2660 int index = transid % 2;
2663 * we only allow two pending log transactions at a time,
2664 * so we know that if ours is more than 2 older than the
2665 * current transaction, we're done
2668 prepare_to_wait(&root->log_commit_wait[index],
2669 &wait, TASK_UNINTERRUPTIBLE);
2670 mutex_unlock(&root->log_mutex);
2672 if (root->log_transid_committed < transid &&
2673 atomic_read(&root->log_commit[index]))
2676 finish_wait(&root->log_commit_wait[index], &wait);
2677 mutex_lock(&root->log_mutex);
2678 } while (root->log_transid_committed < transid &&
2679 atomic_read(&root->log_commit[index]));
2682 static void wait_for_writer(struct btrfs_root *root)
2686 while (atomic_read(&root->log_writers)) {
2687 prepare_to_wait(&root->log_writer_wait,
2688 &wait, TASK_UNINTERRUPTIBLE);
2689 mutex_unlock(&root->log_mutex);
2690 if (atomic_read(&root->log_writers))
2692 finish_wait(&root->log_writer_wait, &wait);
2693 mutex_lock(&root->log_mutex);
2697 static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2698 struct btrfs_log_ctx *ctx)
2703 mutex_lock(&root->log_mutex);
2704 list_del_init(&ctx->list);
2705 mutex_unlock(&root->log_mutex);
2709 * Invoked in log mutex context, or be sure there is no other task which
2710 * can access the list.
2712 static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2713 int index, int error)
2715 struct btrfs_log_ctx *ctx;
2718 INIT_LIST_HEAD(&root->log_ctxs[index]);
2722 list_for_each_entry(ctx, &root->log_ctxs[index], list)
2723 ctx->log_ret = error;
2725 INIT_LIST_HEAD(&root->log_ctxs[index]);
2729 * btrfs_sync_log does sends a given tree log down to the disk and
2730 * updates the super blocks to record it. When this call is done,
2731 * you know that any inodes previously logged are safely on disk only
2734 * Any other return value means you need to call btrfs_commit_transaction.
2735 * Some of the edge cases for fsyncing directories that have had unlinks
2736 * or renames done in the past mean that sometimes the only safe
2737 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2738 * that has happened.
2740 int btrfs_sync_log(struct btrfs_trans_handle *trans,
2741 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
2747 struct btrfs_root *log = root->log_root;
2748 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
2749 int log_transid = 0;
2750 struct btrfs_log_ctx root_log_ctx;
2751 struct blk_plug plug;
2753 mutex_lock(&root->log_mutex);
2754 log_transid = ctx->log_transid;
2755 if (root->log_transid_committed >= log_transid) {
2756 mutex_unlock(&root->log_mutex);
2757 return ctx->log_ret;
2760 index1 = log_transid % 2;
2761 if (atomic_read(&root->log_commit[index1])) {
2762 wait_log_commit(root, log_transid);
2763 mutex_unlock(&root->log_mutex);
2764 return ctx->log_ret;
2766 ASSERT(log_transid == root->log_transid);
2767 atomic_set(&root->log_commit[index1], 1);
2769 /* wait for previous tree log sync to complete */
2770 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
2771 wait_log_commit(root, log_transid - 1);
2774 int batch = atomic_read(&root->log_batch);
2775 /* when we're on an ssd, just kick the log commit out */
2776 if (!btrfs_test_opt(root->fs_info, SSD) &&
2777 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
2778 mutex_unlock(&root->log_mutex);
2779 schedule_timeout_uninterruptible(1);
2780 mutex_lock(&root->log_mutex);
2782 wait_for_writer(root);
2783 if (batch == atomic_read(&root->log_batch))
2787 /* bail out if we need to do a full commit */
2788 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
2790 btrfs_free_logged_extents(log, log_transid);
2791 mutex_unlock(&root->log_mutex);
2795 if (log_transid % 2 == 0)
2796 mark = EXTENT_DIRTY;
2800 /* we start IO on all the marked extents here, but we don't actually
2801 * wait for them until later.
2803 blk_start_plug(&plug);
2804 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
2806 blk_finish_plug(&plug);
2807 btrfs_abort_transaction(trans, ret);
2808 btrfs_free_logged_extents(log, log_transid);
2809 btrfs_set_log_full_commit(root->fs_info, trans);
2810 mutex_unlock(&root->log_mutex);
2814 btrfs_set_root_node(&log->root_item, log->node);
2816 root->log_transid++;
2817 log->log_transid = root->log_transid;
2818 root->log_start_pid = 0;
2820 * IO has been started, blocks of the log tree have WRITTEN flag set
2821 * in their headers. new modifications of the log will be written to
2822 * new positions. so it's safe to allow log writers to go in.
2824 mutex_unlock(&root->log_mutex);
2826 btrfs_init_log_ctx(&root_log_ctx, NULL);
2828 mutex_lock(&log_root_tree->log_mutex);
2829 atomic_inc(&log_root_tree->log_batch);
2830 atomic_inc(&log_root_tree->log_writers);
2832 index2 = log_root_tree->log_transid % 2;
2833 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2834 root_log_ctx.log_transid = log_root_tree->log_transid;
2836 mutex_unlock(&log_root_tree->log_mutex);
2838 ret = update_log_root(trans, log);
2840 mutex_lock(&log_root_tree->log_mutex);
2841 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2843 * Implicit memory barrier after atomic_dec_and_test
2845 if (waitqueue_active(&log_root_tree->log_writer_wait))
2846 wake_up(&log_root_tree->log_writer_wait);
2850 if (!list_empty(&root_log_ctx.list))
2851 list_del_init(&root_log_ctx.list);
2853 blk_finish_plug(&plug);
2854 btrfs_set_log_full_commit(root->fs_info, trans);
2856 if (ret != -ENOSPC) {
2857 btrfs_abort_transaction(trans, ret);
2858 mutex_unlock(&log_root_tree->log_mutex);
2861 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2862 btrfs_free_logged_extents(log, log_transid);
2863 mutex_unlock(&log_root_tree->log_mutex);
2868 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
2869 blk_finish_plug(&plug);
2870 mutex_unlock(&log_root_tree->log_mutex);
2871 ret = root_log_ctx.log_ret;
2875 index2 = root_log_ctx.log_transid % 2;
2876 if (atomic_read(&log_root_tree->log_commit[index2])) {
2877 blk_finish_plug(&plug);
2878 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages,
2880 btrfs_wait_logged_extents(trans, log, log_transid);
2881 wait_log_commit(log_root_tree,
2882 root_log_ctx.log_transid);
2883 mutex_unlock(&log_root_tree->log_mutex);
2885 ret = root_log_ctx.log_ret;
2888 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
2889 atomic_set(&log_root_tree->log_commit[index2], 1);
2891 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2892 wait_log_commit(log_root_tree,
2893 root_log_ctx.log_transid - 1);
2896 wait_for_writer(log_root_tree);
2899 * now that we've moved on to the tree of log tree roots,
2900 * check the full commit flag again
2902 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
2903 blk_finish_plug(&plug);
2904 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2905 btrfs_free_logged_extents(log, log_transid);
2906 mutex_unlock(&log_root_tree->log_mutex);
2908 goto out_wake_log_root;
2911 ret = btrfs_write_marked_extents(log_root_tree,
2912 &log_root_tree->dirty_log_pages,
2913 EXTENT_DIRTY | EXTENT_NEW);
2914 blk_finish_plug(&plug);
2916 btrfs_set_log_full_commit(root->fs_info, trans);
2917 btrfs_abort_transaction(trans, ret);
2918 btrfs_free_logged_extents(log, log_transid);
2919 mutex_unlock(&log_root_tree->log_mutex);
2920 goto out_wake_log_root;
2922 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2924 ret = btrfs_wait_marked_extents(log_root_tree,
2925 &log_root_tree->dirty_log_pages,
2926 EXTENT_NEW | EXTENT_DIRTY);
2928 btrfs_set_log_full_commit(root->fs_info, trans);
2929 btrfs_free_logged_extents(log, log_transid);
2930 mutex_unlock(&log_root_tree->log_mutex);
2931 goto out_wake_log_root;
2933 btrfs_wait_logged_extents(trans, log, log_transid);
2935 btrfs_set_super_log_root(root->fs_info->super_for_commit,
2936 log_root_tree->node->start);
2937 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
2938 btrfs_header_level(log_root_tree->node));
2940 log_root_tree->log_transid++;
2941 mutex_unlock(&log_root_tree->log_mutex);
2944 * nobody else is going to jump in and write the the ctree
2945 * super here because the log_commit atomic below is protecting
2946 * us. We must be called with a transaction handle pinning
2947 * the running transaction open, so a full commit can't hop
2948 * in and cause problems either.
2950 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
2952 btrfs_set_log_full_commit(root->fs_info, trans);
2953 btrfs_abort_transaction(trans, ret);
2954 goto out_wake_log_root;
2957 mutex_lock(&root->log_mutex);
2958 if (root->last_log_commit < log_transid)
2959 root->last_log_commit = log_transid;
2960 mutex_unlock(&root->log_mutex);
2964 * We needn't get log_mutex here because we are sure all
2965 * the other tasks are blocked.
2967 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2969 mutex_lock(&log_root_tree->log_mutex);
2970 log_root_tree->log_transid_committed++;
2971 atomic_set(&log_root_tree->log_commit[index2], 0);
2972 mutex_unlock(&log_root_tree->log_mutex);
2975 * The barrier before waitqueue_active is implied by mutex_unlock
2977 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2978 wake_up(&log_root_tree->log_commit_wait[index2]);
2981 btrfs_remove_all_log_ctxs(root, index1, ret);
2983 mutex_lock(&root->log_mutex);
2984 root->log_transid_committed++;
2985 atomic_set(&root->log_commit[index1], 0);
2986 mutex_unlock(&root->log_mutex);
2989 * The barrier before waitqueue_active is implied by mutex_unlock
2991 if (waitqueue_active(&root->log_commit_wait[index1]))
2992 wake_up(&root->log_commit_wait[index1]);
2996 static void free_log_tree(struct btrfs_trans_handle *trans,
2997 struct btrfs_root *log)
3002 struct walk_control wc = {
3004 .process_func = process_one_buffer
3007 ret = walk_log_tree(trans, log, &wc);
3008 /* I don't think this can happen but just in case */
3010 btrfs_abort_transaction(trans, ret);
3013 ret = find_first_extent_bit(&log->dirty_log_pages,
3014 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
3019 clear_extent_bits(&log->dirty_log_pages, start, end,
3020 EXTENT_DIRTY | EXTENT_NEW);
3024 * We may have short-circuited the log tree with the full commit logic
3025 * and left ordered extents on our list, so clear these out to keep us
3026 * from leaking inodes and memory.
3028 btrfs_free_logged_extents(log, 0);
3029 btrfs_free_logged_extents(log, 1);
3031 free_extent_buffer(log->node);
3036 * free all the extents used by the tree log. This should be called
3037 * at commit time of the full transaction
3039 int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3041 if (root->log_root) {
3042 free_log_tree(trans, root->log_root);
3043 root->log_root = NULL;
3048 int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3049 struct btrfs_fs_info *fs_info)
3051 if (fs_info->log_root_tree) {
3052 free_log_tree(trans, fs_info->log_root_tree);
3053 fs_info->log_root_tree = NULL;
3059 * If both a file and directory are logged, and unlinks or renames are
3060 * mixed in, we have a few interesting corners:
3062 * create file X in dir Y
3063 * link file X to X.link in dir Y
3065 * unlink file X but leave X.link
3068 * After a crash we would expect only X.link to exist. But file X
3069 * didn't get fsync'd again so the log has back refs for X and X.link.
3071 * We solve this by removing directory entries and inode backrefs from the
3072 * log when a file that was logged in the current transaction is
3073 * unlinked. Any later fsync will include the updated log entries, and
3074 * we'll be able to reconstruct the proper directory items from backrefs.
3076 * This optimizations allows us to avoid relogging the entire inode
3077 * or the entire directory.
3079 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3080 struct btrfs_root *root,
3081 const char *name, int name_len,
3082 struct inode *dir, u64 index)
3084 struct btrfs_root *log;
3085 struct btrfs_dir_item *di;
3086 struct btrfs_path *path;
3090 u64 dir_ino = btrfs_ino(dir);
3092 if (BTRFS_I(dir)->logged_trans < trans->transid)
3095 ret = join_running_log_trans(root);
3099 mutex_lock(&BTRFS_I(dir)->log_mutex);
3101 log = root->log_root;
3102 path = btrfs_alloc_path();
3108 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
3109 name, name_len, -1);
3115 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3116 bytes_del += name_len;
3122 btrfs_release_path(path);
3123 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
3124 index, name, name_len, -1);
3130 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3131 bytes_del += name_len;
3138 /* update the directory size in the log to reflect the names
3142 struct btrfs_key key;
3144 key.objectid = dir_ino;
3146 key.type = BTRFS_INODE_ITEM_KEY;
3147 btrfs_release_path(path);
3149 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
3155 struct btrfs_inode_item *item;
3158 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3159 struct btrfs_inode_item);
3160 i_size = btrfs_inode_size(path->nodes[0], item);
3161 if (i_size > bytes_del)
3162 i_size -= bytes_del;
3165 btrfs_set_inode_size(path->nodes[0], item, i_size);
3166 btrfs_mark_buffer_dirty(path->nodes[0]);
3169 btrfs_release_path(path);
3172 btrfs_free_path(path);
3174 mutex_unlock(&BTRFS_I(dir)->log_mutex);
3175 if (ret == -ENOSPC) {
3176 btrfs_set_log_full_commit(root->fs_info, trans);
3179 btrfs_abort_transaction(trans, ret);
3181 btrfs_end_log_trans(root);
3186 /* see comments for btrfs_del_dir_entries_in_log */
3187 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3188 struct btrfs_root *root,
3189 const char *name, int name_len,
3190 struct inode *inode, u64 dirid)
3192 struct btrfs_root *log;
3196 if (BTRFS_I(inode)->logged_trans < trans->transid)
3199 ret = join_running_log_trans(root);
3202 log = root->log_root;
3203 mutex_lock(&BTRFS_I(inode)->log_mutex);
3205 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
3207 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3208 if (ret == -ENOSPC) {
3209 btrfs_set_log_full_commit(root->fs_info, trans);
3211 } else if (ret < 0 && ret != -ENOENT)
3212 btrfs_abort_transaction(trans, ret);
3213 btrfs_end_log_trans(root);
3219 * creates a range item in the log for 'dirid'. first_offset and
3220 * last_offset tell us which parts of the key space the log should
3221 * be considered authoritative for.
3223 static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3224 struct btrfs_root *log,
3225 struct btrfs_path *path,
3226 int key_type, u64 dirid,
3227 u64 first_offset, u64 last_offset)
3230 struct btrfs_key key;
3231 struct btrfs_dir_log_item *item;
3233 key.objectid = dirid;
3234 key.offset = first_offset;
3235 if (key_type == BTRFS_DIR_ITEM_KEY)
3236 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3238 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3239 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
3243 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3244 struct btrfs_dir_log_item);
3245 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3246 btrfs_mark_buffer_dirty(path->nodes[0]);
3247 btrfs_release_path(path);
3252 * log all the items included in the current transaction for a given
3253 * directory. This also creates the range items in the log tree required
3254 * to replay anything deleted before the fsync
3256 static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3257 struct btrfs_root *root, struct inode *inode,
3258 struct btrfs_path *path,
3259 struct btrfs_path *dst_path, int key_type,
3260 struct btrfs_log_ctx *ctx,
3261 u64 min_offset, u64 *last_offset_ret)
3263 struct btrfs_key min_key;
3264 struct btrfs_root *log = root->log_root;
3265 struct extent_buffer *src;
3270 u64 first_offset = min_offset;
3271 u64 last_offset = (u64)-1;
3272 u64 ino = btrfs_ino(inode);
3274 log = root->log_root;
3276 min_key.objectid = ino;
3277 min_key.type = key_type;
3278 min_key.offset = min_offset;
3280 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
3283 * we didn't find anything from this transaction, see if there
3284 * is anything at all
3286 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3287 min_key.objectid = ino;
3288 min_key.type = key_type;
3289 min_key.offset = (u64)-1;
3290 btrfs_release_path(path);
3291 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3293 btrfs_release_path(path);
3296 ret = btrfs_previous_item(root, path, ino, key_type);
3298 /* if ret == 0 there are items for this type,
3299 * create a range to tell us the last key of this type.
3300 * otherwise, there are no items in this directory after
3301 * *min_offset, and we create a range to indicate that.
3304 struct btrfs_key tmp;
3305 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3307 if (key_type == tmp.type)
3308 first_offset = max(min_offset, tmp.offset) + 1;
3313 /* go backward to find any previous key */
3314 ret = btrfs_previous_item(root, path, ino, key_type);
3316 struct btrfs_key tmp;
3317 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3318 if (key_type == tmp.type) {
3319 first_offset = tmp.offset;
3320 ret = overwrite_item(trans, log, dst_path,
3321 path->nodes[0], path->slots[0],
3329 btrfs_release_path(path);
3331 /* find the first key from this transaction again */
3332 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3333 if (WARN_ON(ret != 0))
3337 * we have a block from this transaction, log every item in it
3338 * from our directory
3341 struct btrfs_key tmp;
3342 src = path->nodes[0];
3343 nritems = btrfs_header_nritems(src);
3344 for (i = path->slots[0]; i < nritems; i++) {
3345 struct btrfs_dir_item *di;
3347 btrfs_item_key_to_cpu(src, &min_key, i);
3349 if (min_key.objectid != ino || min_key.type != key_type)
3351 ret = overwrite_item(trans, log, dst_path, src, i,
3359 * We must make sure that when we log a directory entry,
3360 * the corresponding inode, after log replay, has a
3361 * matching link count. For example:
3367 * xfs_io -c "fsync" mydir
3369 * <mount fs and log replay>
3371 * Would result in a fsync log that when replayed, our
3372 * file inode would have a link count of 1, but we get
3373 * two directory entries pointing to the same inode.
3374 * After removing one of the names, it would not be
3375 * possible to remove the other name, which resulted
3376 * always in stale file handle errors, and would not
3377 * be possible to rmdir the parent directory, since
3378 * its i_size could never decrement to the value
3379 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3381 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3382 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3384 (btrfs_dir_transid(src, di) == trans->transid ||
3385 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3386 tmp.type != BTRFS_ROOT_ITEM_KEY)
3387 ctx->log_new_dentries = true;
3389 path->slots[0] = nritems;
3392 * look ahead to the next item and see if it is also
3393 * from this directory and from this transaction
3395 ret = btrfs_next_leaf(root, path);
3397 last_offset = (u64)-1;
3400 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3401 if (tmp.objectid != ino || tmp.type != key_type) {
3402 last_offset = (u64)-1;
3405 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3406 ret = overwrite_item(trans, log, dst_path,
3407 path->nodes[0], path->slots[0],
3412 last_offset = tmp.offset;
3417 btrfs_release_path(path);
3418 btrfs_release_path(dst_path);
3421 *last_offset_ret = last_offset;
3423 * insert the log range keys to indicate where the log
3426 ret = insert_dir_log_key(trans, log, path, key_type,
3427 ino, first_offset, last_offset);
3435 * logging directories is very similar to logging inodes, We find all the items
3436 * from the current transaction and write them to the log.
3438 * The recovery code scans the directory in the subvolume, and if it finds a
3439 * key in the range logged that is not present in the log tree, then it means
3440 * that dir entry was unlinked during the transaction.
3442 * In order for that scan to work, we must include one key smaller than
3443 * the smallest logged by this transaction and one key larger than the largest
3444 * key logged by this transaction.
3446 static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3447 struct btrfs_root *root, struct inode *inode,
3448 struct btrfs_path *path,
3449 struct btrfs_path *dst_path,
3450 struct btrfs_log_ctx *ctx)
3455 int key_type = BTRFS_DIR_ITEM_KEY;
3461 ret = log_dir_items(trans, root, inode, path,
3462 dst_path, key_type, ctx, min_key,
3466 if (max_key == (u64)-1)
3468 min_key = max_key + 1;
3471 if (key_type == BTRFS_DIR_ITEM_KEY) {
3472 key_type = BTRFS_DIR_INDEX_KEY;
3479 * a helper function to drop items from the log before we relog an
3480 * inode. max_key_type indicates the highest item type to remove.
3481 * This cannot be run for file data extents because it does not
3482 * free the extents they point to.
3484 static int drop_objectid_items(struct btrfs_trans_handle *trans,
3485 struct btrfs_root *log,
3486 struct btrfs_path *path,
3487 u64 objectid, int max_key_type)
3490 struct btrfs_key key;
3491 struct btrfs_key found_key;
3494 key.objectid = objectid;
3495 key.type = max_key_type;
3496 key.offset = (u64)-1;
3499 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
3500 BUG_ON(ret == 0); /* Logic error */
3504 if (path->slots[0] == 0)
3508 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3511 if (found_key.objectid != objectid)
3514 found_key.offset = 0;
3516 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3519 ret = btrfs_del_items(trans, log, path, start_slot,
3520 path->slots[0] - start_slot + 1);
3522 * If start slot isn't 0 then we don't need to re-search, we've
3523 * found the last guy with the objectid in this tree.
3525 if (ret || start_slot != 0)
3527 btrfs_release_path(path);
3529 btrfs_release_path(path);
3535 static void fill_inode_item(struct btrfs_trans_handle *trans,
3536 struct extent_buffer *leaf,
3537 struct btrfs_inode_item *item,
3538 struct inode *inode, int log_inode_only,
3541 struct btrfs_map_token token;
3543 btrfs_init_map_token(&token);
3545 if (log_inode_only) {
3546 /* set the generation to zero so the recover code
3547 * can tell the difference between an logging
3548 * just to say 'this inode exists' and a logging
3549 * to say 'update this inode with these values'
3551 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3552 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
3554 btrfs_set_token_inode_generation(leaf, item,
3555 BTRFS_I(inode)->generation,
3557 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
3560 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3561 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3562 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3563 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3565 btrfs_set_token_timespec_sec(leaf, &item->atime,
3566 inode->i_atime.tv_sec, &token);
3567 btrfs_set_token_timespec_nsec(leaf, &item->atime,
3568 inode->i_atime.tv_nsec, &token);
3570 btrfs_set_token_timespec_sec(leaf, &item->mtime,
3571 inode->i_mtime.tv_sec, &token);
3572 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
3573 inode->i_mtime.tv_nsec, &token);
3575 btrfs_set_token_timespec_sec(leaf, &item->ctime,
3576 inode->i_ctime.tv_sec, &token);
3577 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
3578 inode->i_ctime.tv_nsec, &token);
3580 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3583 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3584 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3585 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3586 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3587 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
3590 static int log_inode_item(struct btrfs_trans_handle *trans,
3591 struct btrfs_root *log, struct btrfs_path *path,
3592 struct inode *inode)
3594 struct btrfs_inode_item *inode_item;
3597 ret = btrfs_insert_empty_item(trans, log, path,
3598 &BTRFS_I(inode)->location,
3599 sizeof(*inode_item));
3600 if (ret && ret != -EEXIST)
3602 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3603 struct btrfs_inode_item);
3604 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
3605 btrfs_release_path(path);
3609 static noinline int copy_items(struct btrfs_trans_handle *trans,
3610 struct inode *inode,
3611 struct btrfs_path *dst_path,
3612 struct btrfs_path *src_path, u64 *last_extent,
3613 int start_slot, int nr, int inode_only,
3616 unsigned long src_offset;
3617 unsigned long dst_offset;
3618 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
3619 struct btrfs_file_extent_item *extent;
3620 struct btrfs_inode_item *inode_item;
3621 struct extent_buffer *src = src_path->nodes[0];
3622 struct btrfs_key first_key, last_key, key;
3624 struct btrfs_key *ins_keys;
3628 struct list_head ordered_sums;
3629 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
3630 bool has_extents = false;
3631 bool need_find_last_extent = true;
3634 INIT_LIST_HEAD(&ordered_sums);
3636 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3637 nr * sizeof(u32), GFP_NOFS);
3641 first_key.objectid = (u64)-1;
3643 ins_sizes = (u32 *)ins_data;
3644 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3646 for (i = 0; i < nr; i++) {
3647 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3648 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3650 ret = btrfs_insert_empty_items(trans, log, dst_path,
3651 ins_keys, ins_sizes, nr);
3657 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
3658 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3659 dst_path->slots[0]);
3661 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3663 if ((i == (nr - 1)))
3664 last_key = ins_keys[i];
3666 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
3667 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3669 struct btrfs_inode_item);
3670 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3671 inode, inode_only == LOG_INODE_EXISTS,
3674 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3675 src_offset, ins_sizes[i]);
3679 * We set need_find_last_extent here in case we know we were
3680 * processing other items and then walk into the first extent in
3681 * the inode. If we don't hit an extent then nothing changes,
3682 * we'll do the last search the next time around.
3684 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3686 if (first_key.objectid == (u64)-1)
3687 first_key = ins_keys[i];
3689 need_find_last_extent = false;
3692 /* take a reference on file data extents so that truncates
3693 * or deletes of this inode don't have to relog the inode
3696 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
3699 extent = btrfs_item_ptr(src, start_slot + i,
3700 struct btrfs_file_extent_item);
3702 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3705 found_type = btrfs_file_extent_type(src, extent);
3706 if (found_type == BTRFS_FILE_EXTENT_REG) {
3708 ds = btrfs_file_extent_disk_bytenr(src,
3710 /* ds == 0 is a hole */
3714 dl = btrfs_file_extent_disk_num_bytes(src,
3716 cs = btrfs_file_extent_offset(src, extent);
3717 cl = btrfs_file_extent_num_bytes(src,
3719 if (btrfs_file_extent_compression(src,
3725 ret = btrfs_lookup_csums_range(
3726 log->fs_info->csum_root,
3727 ds + cs, ds + cs + cl - 1,
3730 btrfs_release_path(dst_path);
3738 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
3739 btrfs_release_path(dst_path);
3743 * we have to do this after the loop above to avoid changing the
3744 * log tree while trying to change the log tree.
3747 while (!list_empty(&ordered_sums)) {
3748 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3749 struct btrfs_ordered_sum,
3752 ret = btrfs_csum_file_blocks(trans, log, sums);
3753 list_del(&sums->list);
3760 if (need_find_last_extent && *last_extent == first_key.offset) {
3762 * We don't have any leafs between our current one and the one
3763 * we processed before that can have file extent items for our
3764 * inode (and have a generation number smaller than our current
3767 need_find_last_extent = false;
3771 * Because we use btrfs_search_forward we could skip leaves that were
3772 * not modified and then assume *last_extent is valid when it really
3773 * isn't. So back up to the previous leaf and read the end of the last
3774 * extent before we go and fill in holes.
3776 if (need_find_last_extent) {
3779 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3784 if (src_path->slots[0])
3785 src_path->slots[0]--;
3786 src = src_path->nodes[0];
3787 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3788 if (key.objectid != btrfs_ino(inode) ||
3789 key.type != BTRFS_EXTENT_DATA_KEY)
3791 extent = btrfs_item_ptr(src, src_path->slots[0],
3792 struct btrfs_file_extent_item);
3793 if (btrfs_file_extent_type(src, extent) ==
3794 BTRFS_FILE_EXTENT_INLINE) {
3795 len = btrfs_file_extent_inline_len(src,
3798 *last_extent = ALIGN(key.offset + len,
3801 len = btrfs_file_extent_num_bytes(src, extent);
3802 *last_extent = key.offset + len;
3806 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3807 * things could have happened
3809 * 1) A merge could have happened, so we could currently be on a leaf
3810 * that holds what we were copying in the first place.
3811 * 2) A split could have happened, and now not all of the items we want
3812 * are on the same leaf.
3814 * So we need to adjust how we search for holes, we need to drop the
3815 * path and re-search for the first extent key we found, and then walk
3816 * forward until we hit the last one we copied.
3818 if (need_find_last_extent) {
3819 /* btrfs_prev_leaf could return 1 without releasing the path */
3820 btrfs_release_path(src_path);
3821 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3826 src = src_path->nodes[0];
3827 i = src_path->slots[0];
3833 * Ok so here we need to go through and fill in any holes we may have
3834 * to make sure that holes are punched for those areas in case they had
3835 * extents previously.
3841 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3842 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3846 src = src_path->nodes[0];
3850 btrfs_item_key_to_cpu(src, &key, i);
3851 if (!btrfs_comp_cpu_keys(&key, &last_key))
3853 if (key.objectid != btrfs_ino(inode) ||
3854 key.type != BTRFS_EXTENT_DATA_KEY) {
3858 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3859 if (btrfs_file_extent_type(src, extent) ==
3860 BTRFS_FILE_EXTENT_INLINE) {
3861 len = btrfs_file_extent_inline_len(src, i, extent);
3862 extent_end = ALIGN(key.offset + len, log->sectorsize);
3864 len = btrfs_file_extent_num_bytes(src, extent);
3865 extent_end = key.offset + len;
3869 if (*last_extent == key.offset) {
3870 *last_extent = extent_end;
3873 offset = *last_extent;
3874 len = key.offset - *last_extent;
3875 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3876 offset, 0, 0, len, 0, len, 0,
3880 *last_extent = extent_end;
3883 * Need to let the callers know we dropped the path so they should
3886 if (!ret && need_find_last_extent)
3891 static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3893 struct extent_map *em1, *em2;
3895 em1 = list_entry(a, struct extent_map, list);
3896 em2 = list_entry(b, struct extent_map, list);
3898 if (em1->start < em2->start)
3900 else if (em1->start > em2->start)
3905 static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3906 struct inode *inode,
3907 struct btrfs_root *root,
3908 const struct extent_map *em,
3909 const struct list_head *logged_list,
3910 bool *ordered_io_error)
3912 struct btrfs_ordered_extent *ordered;
3913 struct btrfs_root *log = root->log_root;
3914 u64 mod_start = em->mod_start;
3915 u64 mod_len = em->mod_len;
3916 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
3919 LIST_HEAD(ordered_sums);
3922 *ordered_io_error = false;
3924 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3925 em->block_start == EXTENT_MAP_HOLE)
3929 * Wait far any ordered extent that covers our extent map. If it
3930 * finishes without an error, first check and see if our csums are on
3931 * our outstanding ordered extents.
3933 list_for_each_entry(ordered, logged_list, log_list) {
3934 struct btrfs_ordered_sum *sum;
3939 if (ordered->file_offset + ordered->len <= mod_start ||
3940 mod_start + mod_len <= ordered->file_offset)
3943 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3944 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3945 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3946 const u64 start = ordered->file_offset;
3947 const u64 end = ordered->file_offset + ordered->len - 1;
3949 WARN_ON(ordered->inode != inode);
3950 filemap_fdatawrite_range(inode->i_mapping, start, end);
3953 wait_event(ordered->wait,
3954 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3955 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3957 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
3959 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3960 * i_mapping flags, so that the next fsync won't get
3961 * an outdated io error too.
3963 btrfs_inode_check_errors(inode);
3964 *ordered_io_error = true;
3968 * We are going to copy all the csums on this ordered extent, so
3969 * go ahead and adjust mod_start and mod_len in case this
3970 * ordered extent has already been logged.
3972 if (ordered->file_offset > mod_start) {
3973 if (ordered->file_offset + ordered->len >=
3974 mod_start + mod_len)
3975 mod_len = ordered->file_offset - mod_start;
3977 * If we have this case
3979 * |--------- logged extent ---------|
3980 * |----- ordered extent ----|
3982 * Just don't mess with mod_start and mod_len, we'll
3983 * just end up logging more csums than we need and it
3987 if (ordered->file_offset + ordered->len <
3988 mod_start + mod_len) {
3989 mod_len = (mod_start + mod_len) -
3990 (ordered->file_offset + ordered->len);
3991 mod_start = ordered->file_offset +
4002 * To keep us from looping for the above case of an ordered
4003 * extent that falls inside of the logged extent.
4005 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4009 list_for_each_entry(sum, &ordered->list, list) {
4010 ret = btrfs_csum_file_blocks(trans, log, sum);
4016 if (*ordered_io_error || !mod_len || ret || skip_csum)
4019 if (em->compress_type) {
4021 csum_len = max(em->block_len, em->orig_block_len);
4023 csum_offset = mod_start - em->start;
4027 /* block start is already adjusted for the file extent offset. */
4028 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
4029 em->block_start + csum_offset,
4030 em->block_start + csum_offset +
4031 csum_len - 1, &ordered_sums, 0);
4035 while (!list_empty(&ordered_sums)) {
4036 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4037 struct btrfs_ordered_sum,
4040 ret = btrfs_csum_file_blocks(trans, log, sums);
4041 list_del(&sums->list);
4048 static int log_one_extent(struct btrfs_trans_handle *trans,
4049 struct inode *inode, struct btrfs_root *root,
4050 const struct extent_map *em,
4051 struct btrfs_path *path,
4052 const struct list_head *logged_list,
4053 struct btrfs_log_ctx *ctx)
4055 struct btrfs_root *log = root->log_root;
4056 struct btrfs_file_extent_item *fi;
4057 struct extent_buffer *leaf;
4058 struct btrfs_map_token token;
4059 struct btrfs_key key;
4060 u64 extent_offset = em->start - em->orig_start;
4063 int extent_inserted = 0;
4064 bool ordered_io_err = false;
4066 ret = wait_ordered_extents(trans, inode, root, em, logged_list,
4071 if (ordered_io_err) {
4076 btrfs_init_map_token(&token);
4078 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
4079 em->start + em->len, NULL, 0, 1,
4080 sizeof(*fi), &extent_inserted);
4084 if (!extent_inserted) {
4085 key.objectid = btrfs_ino(inode);
4086 key.type = BTRFS_EXTENT_DATA_KEY;
4087 key.offset = em->start;
4089 ret = btrfs_insert_empty_item(trans, log, path, &key,
4094 leaf = path->nodes[0];
4095 fi = btrfs_item_ptr(leaf, path->slots[0],
4096 struct btrfs_file_extent_item);
4098 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
4100 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4101 btrfs_set_token_file_extent_type(leaf, fi,
4102 BTRFS_FILE_EXTENT_PREALLOC,
4105 btrfs_set_token_file_extent_type(leaf, fi,
4106 BTRFS_FILE_EXTENT_REG,
4109 block_len = max(em->block_len, em->orig_block_len);
4110 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4111 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4114 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4116 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4117 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4119 extent_offset, &token);
4120 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4123 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4124 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4128 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4129 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4130 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4131 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4133 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4134 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4135 btrfs_mark_buffer_dirty(leaf);
4137 btrfs_release_path(path);
4142 static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4143 struct btrfs_root *root,
4144 struct inode *inode,
4145 struct btrfs_path *path,
4146 struct list_head *logged_list,
4147 struct btrfs_log_ctx *ctx,
4151 struct extent_map *em, *n;
4152 struct list_head extents;
4153 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4158 INIT_LIST_HEAD(&extents);
4160 down_write(&BTRFS_I(inode)->dio_sem);
4161 write_lock(&tree->lock);
4162 test_gen = root->fs_info->last_trans_committed;
4164 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4165 list_del_init(&em->list);
4168 * Just an arbitrary number, this can be really CPU intensive
4169 * once we start getting a lot of extents, and really once we
4170 * have a bunch of extents we just want to commit since it will
4173 if (++num > 32768) {
4174 list_del_init(&tree->modified_extents);
4179 if (em->generation <= test_gen)
4181 /* Need a ref to keep it from getting evicted from cache */
4182 atomic_inc(&em->refs);
4183 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
4184 list_add_tail(&em->list, &extents);
4188 list_sort(NULL, &extents, extent_cmp);
4189 btrfs_get_logged_extents(inode, logged_list, start, end);
4191 * Some ordered extents started by fsync might have completed
4192 * before we could collect them into the list logged_list, which
4193 * means they're gone, not in our logged_list nor in the inode's
4194 * ordered tree. We want the application/user space to know an
4195 * error happened while attempting to persist file data so that
4196 * it can take proper action. If such error happened, we leave
4197 * without writing to the log tree and the fsync must report the
4198 * file data write error and not commit the current transaction.
4200 ret = btrfs_inode_check_errors(inode);
4204 while (!list_empty(&extents)) {
4205 em = list_entry(extents.next, struct extent_map, list);
4207 list_del_init(&em->list);
4210 * If we had an error we just need to delete everybody from our
4214 clear_em_logging(tree, em);
4215 free_extent_map(em);
4219 write_unlock(&tree->lock);
4221 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4223 write_lock(&tree->lock);
4224 clear_em_logging(tree, em);
4225 free_extent_map(em);
4227 WARN_ON(!list_empty(&extents));
4228 write_unlock(&tree->lock);
4229 up_write(&BTRFS_I(inode)->dio_sem);
4231 btrfs_release_path(path);
4235 static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
4236 struct btrfs_path *path, u64 *size_ret)
4238 struct btrfs_key key;
4241 key.objectid = btrfs_ino(inode);
4242 key.type = BTRFS_INODE_ITEM_KEY;
4245 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4248 } else if (ret > 0) {
4251 struct btrfs_inode_item *item;
4253 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4254 struct btrfs_inode_item);
4255 *size_ret = btrfs_inode_size(path->nodes[0], item);
4258 btrfs_release_path(path);
4263 * At the moment we always log all xattrs. This is to figure out at log replay
4264 * time which xattrs must have their deletion replayed. If a xattr is missing
4265 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4266 * because if a xattr is deleted, the inode is fsynced and a power failure
4267 * happens, causing the log to be replayed the next time the fs is mounted,
4268 * we want the xattr to not exist anymore (same behaviour as other filesystems
4269 * with a journal, ext3/4, xfs, f2fs, etc).
4271 static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4272 struct btrfs_root *root,
4273 struct inode *inode,
4274 struct btrfs_path *path,
4275 struct btrfs_path *dst_path)
4278 struct btrfs_key key;
4279 const u64 ino = btrfs_ino(inode);
4284 key.type = BTRFS_XATTR_ITEM_KEY;
4287 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4292 int slot = path->slots[0];
4293 struct extent_buffer *leaf = path->nodes[0];
4294 int nritems = btrfs_header_nritems(leaf);
4296 if (slot >= nritems) {
4298 u64 last_extent = 0;
4300 ret = copy_items(trans, inode, dst_path, path,
4301 &last_extent, start_slot,
4303 /* can't be 1, extent items aren't processed */
4309 ret = btrfs_next_leaf(root, path);
4317 btrfs_item_key_to_cpu(leaf, &key, slot);
4318 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4328 u64 last_extent = 0;
4330 ret = copy_items(trans, inode, dst_path, path,
4331 &last_extent, start_slot,
4333 /* can't be 1, extent items aren't processed */
4343 * If the no holes feature is enabled we need to make sure any hole between the
4344 * last extent and the i_size of our inode is explicitly marked in the log. This
4345 * is to make sure that doing something like:
4347 * 1) create file with 128Kb of data
4348 * 2) truncate file to 64Kb
4349 * 3) truncate file to 256Kb
4351 * 5) <crash/power failure>
4352 * 6) mount fs and trigger log replay
4354 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4355 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4356 * file correspond to a hole. The presence of explicit holes in a log tree is
4357 * what guarantees that log replay will remove/adjust file extent items in the
4360 * Here we do not need to care about holes between extents, that is already done
4361 * by copy_items(). We also only need to do this in the full sync path, where we
4362 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4363 * lookup the list of modified extent maps and if any represents a hole, we
4364 * insert a corresponding extent representing a hole in the log tree.
4366 static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4367 struct btrfs_root *root,
4368 struct inode *inode,
4369 struct btrfs_path *path)
4372 struct btrfs_key key;
4375 struct extent_buffer *leaf;
4376 struct btrfs_root *log = root->log_root;
4377 const u64 ino = btrfs_ino(inode);
4378 const u64 i_size = i_size_read(inode);
4380 if (!btrfs_fs_incompat(root->fs_info, NO_HOLES))
4384 key.type = BTRFS_EXTENT_DATA_KEY;
4385 key.offset = (u64)-1;
4387 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4392 ASSERT(path->slots[0] > 0);
4394 leaf = path->nodes[0];
4395 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4397 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4398 /* inode does not have any extents */
4402 struct btrfs_file_extent_item *extent;
4406 * If there's an extent beyond i_size, an explicit hole was
4407 * already inserted by copy_items().
4409 if (key.offset >= i_size)
4412 extent = btrfs_item_ptr(leaf, path->slots[0],
4413 struct btrfs_file_extent_item);
4415 if (btrfs_file_extent_type(leaf, extent) ==
4416 BTRFS_FILE_EXTENT_INLINE) {
4417 len = btrfs_file_extent_inline_len(leaf,
4420 ASSERT(len == i_size);
4424 len = btrfs_file_extent_num_bytes(leaf, extent);
4425 /* Last extent goes beyond i_size, no need to log a hole. */
4426 if (key.offset + len > i_size)
4428 hole_start = key.offset + len;
4429 hole_size = i_size - hole_start;
4431 btrfs_release_path(path);
4433 /* Last extent ends at i_size. */
4437 hole_size = ALIGN(hole_size, root->sectorsize);
4438 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4439 hole_size, 0, hole_size, 0, 0, 0);
4444 * When we are logging a new inode X, check if it doesn't have a reference that
4445 * matches the reference from some other inode Y created in a past transaction
4446 * and that was renamed in the current transaction. If we don't do this, then at
4447 * log replay time we can lose inode Y (and all its files if it's a directory):
4450 * echo "hello world" > /mnt/x/foobar
4453 * mkdir /mnt/x # or touch /mnt/x
4454 * xfs_io -c fsync /mnt/x
4456 * mount fs, trigger log replay
4458 * After the log replay procedure, we would lose the first directory and all its
4459 * files (file foobar).
4460 * For the case where inode Y is not a directory we simply end up losing it:
4462 * echo "123" > /mnt/foo
4464 * mv /mnt/foo /mnt/bar
4465 * echo "abc" > /mnt/foo
4466 * xfs_io -c fsync /mnt/foo
4469 * We also need this for cases where a snapshot entry is replaced by some other
4470 * entry (file or directory) otherwise we end up with an unreplayable log due to
4471 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4472 * if it were a regular entry:
4475 * btrfs subvolume snapshot /mnt /mnt/x/snap
4476 * btrfs subvolume delete /mnt/x/snap
4479 * fsync /mnt/x or fsync some new file inside it
4482 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4483 * the same transaction.
4485 static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4487 const struct btrfs_key *key,
4488 struct inode *inode,
4492 struct btrfs_path *search_path;
4495 u32 item_size = btrfs_item_size_nr(eb, slot);
4497 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4499 search_path = btrfs_alloc_path();
4502 search_path->search_commit_root = 1;
4503 search_path->skip_locking = 1;
4505 while (cur_offset < item_size) {
4509 unsigned long name_ptr;
4510 struct btrfs_dir_item *di;
4512 if (key->type == BTRFS_INODE_REF_KEY) {
4513 struct btrfs_inode_ref *iref;
4515 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4516 parent = key->offset;
4517 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4518 name_ptr = (unsigned long)(iref + 1);
4519 this_len = sizeof(*iref) + this_name_len;
4521 struct btrfs_inode_extref *extref;
4523 extref = (struct btrfs_inode_extref *)(ptr +
4525 parent = btrfs_inode_extref_parent(eb, extref);
4526 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4527 name_ptr = (unsigned long)&extref->name;
4528 this_len = sizeof(*extref) + this_name_len;
4531 if (this_name_len > name_len) {
4534 new_name = krealloc(name, this_name_len, GFP_NOFS);
4539 name_len = this_name_len;
4543 read_extent_buffer(eb, name, name_ptr, this_name_len);
4544 di = btrfs_lookup_dir_item(NULL, BTRFS_I(inode)->root,
4545 search_path, parent,
4546 name, this_name_len, 0);
4547 if (di && !IS_ERR(di)) {
4548 struct btrfs_key di_key;
4550 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4552 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4554 *other_ino = di_key.objectid;
4559 } else if (IS_ERR(di)) {
4563 btrfs_release_path(search_path);
4565 cur_offset += this_len;
4569 btrfs_free_path(search_path);
4574 /* log a single inode in the tree log.
4575 * At least one parent directory for this inode must exist in the tree
4576 * or be logged already.
4578 * Any items from this inode changed by the current transaction are copied
4579 * to the log tree. An extra reference is taken on any extents in this
4580 * file, allowing us to avoid a whole pile of corner cases around logging
4581 * blocks that have been removed from the tree.
4583 * See LOG_INODE_ALL and related defines for a description of what inode_only
4586 * This handles both files and directories.
4588 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
4589 struct btrfs_root *root, struct inode *inode,
4593 struct btrfs_log_ctx *ctx)
4595 struct btrfs_path *path;
4596 struct btrfs_path *dst_path;
4597 struct btrfs_key min_key;
4598 struct btrfs_key max_key;
4599 struct btrfs_root *log = root->log_root;
4600 struct extent_buffer *src = NULL;
4601 LIST_HEAD(logged_list);
4602 u64 last_extent = 0;
4606 int ins_start_slot = 0;
4608 bool fast_search = false;
4609 u64 ino = btrfs_ino(inode);
4610 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
4611 u64 logged_isize = 0;
4612 bool need_log_inode_item = true;
4614 path = btrfs_alloc_path();
4617 dst_path = btrfs_alloc_path();
4619 btrfs_free_path(path);
4623 min_key.objectid = ino;
4624 min_key.type = BTRFS_INODE_ITEM_KEY;
4627 max_key.objectid = ino;
4630 /* today the code can only do partial logging of directories */
4631 if (S_ISDIR(inode->i_mode) ||
4632 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4633 &BTRFS_I(inode)->runtime_flags) &&
4634 inode_only == LOG_INODE_EXISTS))
4635 max_key.type = BTRFS_XATTR_ITEM_KEY;
4637 max_key.type = (u8)-1;
4638 max_key.offset = (u64)-1;
4641 * Only run delayed items if we are a dir or a new file.
4642 * Otherwise commit the delayed inode only, which is needed in
4643 * order for the log replay code to mark inodes for link count
4644 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4646 if (S_ISDIR(inode->i_mode) ||
4647 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
4648 ret = btrfs_commit_inode_delayed_items(trans, inode);
4650 ret = btrfs_commit_inode_delayed_inode(inode);
4653 btrfs_free_path(path);
4654 btrfs_free_path(dst_path);
4658 mutex_lock(&BTRFS_I(inode)->log_mutex);
4661 * a brute force approach to making sure we get the most uptodate
4662 * copies of everything.
4664 if (S_ISDIR(inode->i_mode)) {
4665 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4667 if (inode_only == LOG_INODE_EXISTS)
4668 max_key_type = BTRFS_XATTR_ITEM_KEY;
4669 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
4671 if (inode_only == LOG_INODE_EXISTS) {
4673 * Make sure the new inode item we write to the log has
4674 * the same isize as the current one (if it exists).
4675 * This is necessary to prevent data loss after log
4676 * replay, and also to prevent doing a wrong expanding
4677 * truncate - for e.g. create file, write 4K into offset
4678 * 0, fsync, write 4K into offset 4096, add hard link,
4679 * fsync some other file (to sync log), power fail - if
4680 * we use the inode's current i_size, after log replay
4681 * we get a 8Kb file, with the last 4Kb extent as a hole
4682 * (zeroes), as if an expanding truncate happened,
4683 * instead of getting a file of 4Kb only.
4685 err = logged_inode_size(log, inode, path,
4690 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4691 &BTRFS_I(inode)->runtime_flags)) {
4692 if (inode_only == LOG_INODE_EXISTS) {
4693 max_key.type = BTRFS_XATTR_ITEM_KEY;
4694 ret = drop_objectid_items(trans, log, path, ino,
4697 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4698 &BTRFS_I(inode)->runtime_flags);
4699 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4700 &BTRFS_I(inode)->runtime_flags);
4702 ret = btrfs_truncate_inode_items(trans,
4708 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4709 &BTRFS_I(inode)->runtime_flags) ||
4710 inode_only == LOG_INODE_EXISTS) {
4711 if (inode_only == LOG_INODE_ALL)
4713 max_key.type = BTRFS_XATTR_ITEM_KEY;
4714 ret = drop_objectid_items(trans, log, path, ino,
4717 if (inode_only == LOG_INODE_ALL)
4730 ret = btrfs_search_forward(root, &min_key,
4731 path, trans->transid);
4739 /* note, ins_nr might be > 0 here, cleanup outside the loop */
4740 if (min_key.objectid != ino)
4742 if (min_key.type > max_key.type)
4745 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4746 need_log_inode_item = false;
4748 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4749 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
4750 BTRFS_I(inode)->generation == trans->transid) {
4753 ret = btrfs_check_ref_name_override(path->nodes[0],
4760 } else if (ret > 0 && ctx &&
4761 other_ino != btrfs_ino(ctx->inode)) {
4762 struct btrfs_key inode_key;
4763 struct inode *other_inode;
4769 ins_start_slot = path->slots[0];
4771 ret = copy_items(trans, inode, dst_path, path,
4772 &last_extent, ins_start_slot,
4780 btrfs_release_path(path);
4781 inode_key.objectid = other_ino;
4782 inode_key.type = BTRFS_INODE_ITEM_KEY;
4783 inode_key.offset = 0;
4784 other_inode = btrfs_iget(root->fs_info->sb,
4788 * If the other inode that had a conflicting dir
4789 * entry was deleted in the current transaction,
4790 * we don't need to do more work nor fallback to
4791 * a transaction commit.
4793 if (IS_ERR(other_inode) &&
4794 PTR_ERR(other_inode) == -ENOENT) {
4796 } else if (IS_ERR(other_inode)) {
4797 err = PTR_ERR(other_inode);
4801 * We are safe logging the other inode without
4802 * acquiring its i_mutex as long as we log with
4803 * the LOG_INODE_EXISTS mode. We're safe against
4804 * concurrent renames of the other inode as well
4805 * because during a rename we pin the log and
4806 * update the log with the new name before we
4809 err = btrfs_log_inode(trans, root, other_inode,
4820 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
4821 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
4824 ret = copy_items(trans, inode, dst_path, path,
4825 &last_extent, ins_start_slot,
4826 ins_nr, inode_only, logged_isize);
4833 btrfs_release_path(path);
4839 src = path->nodes[0];
4840 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4843 } else if (!ins_nr) {
4844 ins_start_slot = path->slots[0];
4849 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4850 ins_start_slot, ins_nr, inode_only,
4858 btrfs_release_path(path);
4862 ins_start_slot = path->slots[0];
4865 nritems = btrfs_header_nritems(path->nodes[0]);
4867 if (path->slots[0] < nritems) {
4868 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4873 ret = copy_items(trans, inode, dst_path, path,
4874 &last_extent, ins_start_slot,
4875 ins_nr, inode_only, logged_isize);
4883 btrfs_release_path(path);
4885 if (min_key.offset < (u64)-1) {
4887 } else if (min_key.type < max_key.type) {
4895 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4896 ins_start_slot, ins_nr, inode_only,
4906 btrfs_release_path(path);
4907 btrfs_release_path(dst_path);
4908 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
4911 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
4912 btrfs_release_path(path);
4913 btrfs_release_path(dst_path);
4914 err = btrfs_log_trailing_hole(trans, root, inode, path);
4919 btrfs_release_path(path);
4920 btrfs_release_path(dst_path);
4921 if (need_log_inode_item) {
4922 err = log_inode_item(trans, log, dst_path, inode);
4927 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
4928 &logged_list, ctx, start, end);
4933 } else if (inode_only == LOG_INODE_ALL) {
4934 struct extent_map *em, *n;
4936 write_lock(&em_tree->lock);
4938 * We can't just remove every em if we're called for a ranged
4939 * fsync - that is, one that doesn't cover the whole possible
4940 * file range (0 to LLONG_MAX). This is because we can have
4941 * em's that fall outside the range we're logging and therefore
4942 * their ordered operations haven't completed yet
4943 * (btrfs_finish_ordered_io() not invoked yet). This means we
4944 * didn't get their respective file extent item in the fs/subvol
4945 * tree yet, and need to let the next fast fsync (one which
4946 * consults the list of modified extent maps) find the em so
4947 * that it logs a matching file extent item and waits for the
4948 * respective ordered operation to complete (if it's still
4951 * Removing every em outside the range we're logging would make
4952 * the next fast fsync not log their matching file extent items,
4953 * therefore making us lose data after a log replay.
4955 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
4957 const u64 mod_end = em->mod_start + em->mod_len - 1;
4959 if (em->mod_start >= start && mod_end <= end)
4960 list_del_init(&em->list);
4962 write_unlock(&em_tree->lock);
4965 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
4966 ret = log_directory_changes(trans, root, inode, path, dst_path,
4974 spin_lock(&BTRFS_I(inode)->lock);
4975 BTRFS_I(inode)->logged_trans = trans->transid;
4976 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
4977 spin_unlock(&BTRFS_I(inode)->lock);
4980 btrfs_put_logged_extents(&logged_list);
4982 btrfs_submit_logged_extents(&logged_list, log);
4983 mutex_unlock(&BTRFS_I(inode)->log_mutex);
4985 btrfs_free_path(path);
4986 btrfs_free_path(dst_path);
4991 * Check if we must fallback to a transaction commit when logging an inode.
4992 * This must be called after logging the inode and is used only in the context
4993 * when fsyncing an inode requires the need to log some other inode - in which
4994 * case we can't lock the i_mutex of each other inode we need to log as that
4995 * can lead to deadlocks with concurrent fsync against other inodes (as we can
4996 * log inodes up or down in the hierarchy) or rename operations for example. So
4997 * we take the log_mutex of the inode after we have logged it and then check for
4998 * its last_unlink_trans value - this is safe because any task setting
4999 * last_unlink_trans must take the log_mutex and it must do this before it does
5000 * the actual unlink operation, so if we do this check before a concurrent task
5001 * sets last_unlink_trans it means we've logged a consistent version/state of
5002 * all the inode items, otherwise we are not sure and must do a transaction
5003 * commit (the concurrent task might have only updated last_unlink_trans before
5004 * we logged the inode or it might have also done the unlink).
5006 static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5007 struct inode *inode)
5009 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
5012 mutex_lock(&BTRFS_I(inode)->log_mutex);
5013 if (BTRFS_I(inode)->last_unlink_trans > fs_info->last_trans_committed) {
5015 * Make sure any commits to the log are forced to be full
5018 btrfs_set_log_full_commit(fs_info, trans);
5021 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5027 * follow the dentry parent pointers up the chain and see if any
5028 * of the directories in it require a full commit before they can
5029 * be logged. Returns zero if nothing special needs to be done or 1 if
5030 * a full commit is required.
5032 static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5033 struct inode *inode,
5034 struct dentry *parent,
5035 struct super_block *sb,
5039 struct dentry *old_parent = NULL;
5040 struct inode *orig_inode = inode;
5043 * for regular files, if its inode is already on disk, we don't
5044 * have to worry about the parents at all. This is because
5045 * we can use the last_unlink_trans field to record renames
5046 * and other fun in this file.
5048 if (S_ISREG(inode->i_mode) &&
5049 BTRFS_I(inode)->generation <= last_committed &&
5050 BTRFS_I(inode)->last_unlink_trans <= last_committed)
5053 if (!S_ISDIR(inode->i_mode)) {
5054 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5056 inode = d_inode(parent);
5061 * If we are logging a directory then we start with our inode,
5062 * not our parent's inode, so we need to skip setting the
5063 * logged_trans so that further down in the log code we don't
5064 * think this inode has already been logged.
5066 if (inode != orig_inode)
5067 BTRFS_I(inode)->logged_trans = trans->transid;
5070 if (btrfs_must_commit_transaction(trans, inode)) {
5075 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5078 if (IS_ROOT(parent)) {
5079 inode = d_inode(parent);
5080 if (btrfs_must_commit_transaction(trans, inode))
5085 parent = dget_parent(parent);
5087 old_parent = parent;
5088 inode = d_inode(parent);
5096 struct btrfs_dir_list {
5098 struct list_head list;
5102 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5103 * details about the why it is needed.
5104 * This is a recursive operation - if an existing dentry corresponds to a
5105 * directory, that directory's new entries are logged too (same behaviour as
5106 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5107 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5108 * complains about the following circular lock dependency / possible deadlock:
5112 * lock(&type->i_mutex_dir_key#3/2);
5113 * lock(sb_internal#2);
5114 * lock(&type->i_mutex_dir_key#3/2);
5115 * lock(&sb->s_type->i_mutex_key#14);
5117 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5118 * sb_start_intwrite() in btrfs_start_transaction().
5119 * Not locking i_mutex of the inodes is still safe because:
5121 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5122 * that while logging the inode new references (names) are added or removed
5123 * from the inode, leaving the logged inode item with a link count that does
5124 * not match the number of logged inode reference items. This is fine because
5125 * at log replay time we compute the real number of links and correct the
5126 * link count in the inode item (see replay_one_buffer() and
5127 * link_to_fixup_dir());
5129 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5130 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5131 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5132 * has a size that doesn't match the sum of the lengths of all the logged
5133 * names. This does not result in a problem because if a dir_item key is
5134 * logged but its matching dir_index key is not logged, at log replay time we
5135 * don't use it to replay the respective name (see replay_one_name()). On the
5136 * other hand if only the dir_index key ends up being logged, the respective
5137 * name is added to the fs/subvol tree with both the dir_item and dir_index
5138 * keys created (see replay_one_name()).
5139 * The directory's inode item with a wrong i_size is not a problem as well,
5140 * since we don't use it at log replay time to set the i_size in the inode
5141 * item of the fs/subvol tree (see overwrite_item()).
5143 static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5144 struct btrfs_root *root,
5145 struct inode *start_inode,
5146 struct btrfs_log_ctx *ctx)
5148 struct btrfs_root *log = root->log_root;
5149 struct btrfs_path *path;
5150 LIST_HEAD(dir_list);
5151 struct btrfs_dir_list *dir_elem;
5154 path = btrfs_alloc_path();
5158 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5160 btrfs_free_path(path);
5163 dir_elem->ino = btrfs_ino(start_inode);
5164 list_add_tail(&dir_elem->list, &dir_list);
5166 while (!list_empty(&dir_list)) {
5167 struct extent_buffer *leaf;
5168 struct btrfs_key min_key;
5172 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5175 goto next_dir_inode;
5177 min_key.objectid = dir_elem->ino;
5178 min_key.type = BTRFS_DIR_ITEM_KEY;
5181 btrfs_release_path(path);
5182 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5184 goto next_dir_inode;
5185 } else if (ret > 0) {
5187 goto next_dir_inode;
5191 leaf = path->nodes[0];
5192 nritems = btrfs_header_nritems(leaf);
5193 for (i = path->slots[0]; i < nritems; i++) {
5194 struct btrfs_dir_item *di;
5195 struct btrfs_key di_key;
5196 struct inode *di_inode;
5197 struct btrfs_dir_list *new_dir_elem;
5198 int log_mode = LOG_INODE_EXISTS;
5201 btrfs_item_key_to_cpu(leaf, &min_key, i);
5202 if (min_key.objectid != dir_elem->ino ||
5203 min_key.type != BTRFS_DIR_ITEM_KEY)
5204 goto next_dir_inode;
5206 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5207 type = btrfs_dir_type(leaf, di);
5208 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5209 type != BTRFS_FT_DIR)
5211 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5212 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5215 di_inode = btrfs_iget(root->fs_info->sb, &di_key,
5217 if (IS_ERR(di_inode)) {
5218 ret = PTR_ERR(di_inode);
5219 goto next_dir_inode;
5222 if (btrfs_inode_in_log(di_inode, trans->transid)) {
5227 ctx->log_new_dentries = false;
5228 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
5229 log_mode = LOG_INODE_ALL;
5230 btrfs_release_path(path);
5231 ret = btrfs_log_inode(trans, root, di_inode,
5232 log_mode, 0, LLONG_MAX, ctx);
5234 btrfs_must_commit_transaction(trans, di_inode))
5238 goto next_dir_inode;
5239 if (ctx->log_new_dentries) {
5240 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5242 if (!new_dir_elem) {
5244 goto next_dir_inode;
5246 new_dir_elem->ino = di_key.objectid;
5247 list_add_tail(&new_dir_elem->list, &dir_list);
5252 ret = btrfs_next_leaf(log, path);
5254 goto next_dir_inode;
5255 } else if (ret > 0) {
5257 goto next_dir_inode;
5261 if (min_key.offset < (u64)-1) {
5266 list_del(&dir_elem->list);
5270 btrfs_free_path(path);
5274 static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5275 struct inode *inode,
5276 struct btrfs_log_ctx *ctx)
5279 struct btrfs_path *path;
5280 struct btrfs_key key;
5281 struct btrfs_root *root = BTRFS_I(inode)->root;
5282 const u64 ino = btrfs_ino(inode);
5284 path = btrfs_alloc_path();
5287 path->skip_locking = 1;
5288 path->search_commit_root = 1;
5291 key.type = BTRFS_INODE_REF_KEY;
5293 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5298 struct extent_buffer *leaf = path->nodes[0];
5299 int slot = path->slots[0];
5304 if (slot >= btrfs_header_nritems(leaf)) {
5305 ret = btrfs_next_leaf(root, path);
5313 btrfs_item_key_to_cpu(leaf, &key, slot);
5314 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5315 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5318 item_size = btrfs_item_size_nr(leaf, slot);
5319 ptr = btrfs_item_ptr_offset(leaf, slot);
5320 while (cur_offset < item_size) {
5321 struct btrfs_key inode_key;
5322 struct inode *dir_inode;
5324 inode_key.type = BTRFS_INODE_ITEM_KEY;
5325 inode_key.offset = 0;
5327 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5328 struct btrfs_inode_extref *extref;
5330 extref = (struct btrfs_inode_extref *)
5332 inode_key.objectid = btrfs_inode_extref_parent(
5334 cur_offset += sizeof(*extref);
5335 cur_offset += btrfs_inode_extref_name_len(leaf,
5338 inode_key.objectid = key.offset;
5339 cur_offset = item_size;
5342 dir_inode = btrfs_iget(root->fs_info->sb, &inode_key,
5344 /* If parent inode was deleted, skip it. */
5345 if (IS_ERR(dir_inode))
5349 ctx->log_new_dentries = false;
5350 ret = btrfs_log_inode(trans, root, dir_inode,
5351 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
5353 btrfs_must_commit_transaction(trans, dir_inode))
5355 if (!ret && ctx && ctx->log_new_dentries)
5356 ret = log_new_dir_dentries(trans, root,
5366 btrfs_free_path(path);
5371 * helper function around btrfs_log_inode to make sure newly created
5372 * parent directories also end up in the log. A minimal inode and backref
5373 * only logging is done of any parent directories that are older than
5374 * the last committed transaction
5376 static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5377 struct btrfs_root *root, struct inode *inode,
5378 struct dentry *parent,
5382 struct btrfs_log_ctx *ctx)
5384 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
5385 struct super_block *sb;
5386 struct dentry *old_parent = NULL;
5388 u64 last_committed = root->fs_info->last_trans_committed;
5389 bool log_dentries = false;
5390 struct inode *orig_inode = inode;
5394 if (btrfs_test_opt(root->fs_info, NOTREELOG)) {
5400 * The prev transaction commit doesn't complete, we need do
5401 * full commit by ourselves.
5403 if (root->fs_info->last_trans_log_full_commit >
5404 root->fs_info->last_trans_committed) {
5409 if (root != BTRFS_I(inode)->root ||
5410 btrfs_root_refs(&root->root_item) == 0) {
5415 ret = check_parent_dirs_for_sync(trans, inode, parent,
5416 sb, last_committed);
5420 if (btrfs_inode_in_log(inode, trans->transid)) {
5421 ret = BTRFS_NO_LOG_SYNC;
5425 ret = start_log_trans(trans, root, ctx);
5429 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
5434 * for regular files, if its inode is already on disk, we don't
5435 * have to worry about the parents at all. This is because
5436 * we can use the last_unlink_trans field to record renames
5437 * and other fun in this file.
5439 if (S_ISREG(inode->i_mode) &&
5440 BTRFS_I(inode)->generation <= last_committed &&
5441 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
5446 if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
5447 log_dentries = true;
5450 * On unlink we must make sure all our current and old parent directory
5451 * inodes are fully logged. This is to prevent leaving dangling
5452 * directory index entries in directories that were our parents but are
5453 * not anymore. Not doing this results in old parent directory being
5454 * impossible to delete after log replay (rmdir will always fail with
5455 * error -ENOTEMPTY).
5461 * ln testdir/foo testdir/bar
5463 * unlink testdir/bar
5464 * xfs_io -c fsync testdir/foo
5466 * mount fs, triggers log replay
5468 * If we don't log the parent directory (testdir), after log replay the
5469 * directory still has an entry pointing to the file inode using the bar
5470 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5471 * the file inode has a link count of 1.
5477 * ln foo testdir/foo2
5478 * ln foo testdir/foo3
5480 * unlink testdir/foo3
5481 * xfs_io -c fsync foo
5483 * mount fs, triggers log replay
5485 * Similar as the first example, after log replay the parent directory
5486 * testdir still has an entry pointing to the inode file with name foo3
5487 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5488 * and has a link count of 2.
5490 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
5491 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5497 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5500 inode = d_inode(parent);
5501 if (root != BTRFS_I(inode)->root)
5504 if (BTRFS_I(inode)->generation > last_committed) {
5505 ret = btrfs_log_inode(trans, root, inode,
5511 if (IS_ROOT(parent))
5514 parent = dget_parent(parent);
5516 old_parent = parent;
5519 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5525 btrfs_set_log_full_commit(root->fs_info, trans);
5530 btrfs_remove_log_ctx(root, ctx);
5531 btrfs_end_log_trans(root);
5537 * it is not safe to log dentry if the chunk root has added new
5538 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5539 * If this returns 1, you must commit the transaction to safely get your
5542 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
5543 struct btrfs_root *root, struct dentry *dentry,
5546 struct btrfs_log_ctx *ctx)
5548 struct dentry *parent = dget_parent(dentry);
5551 ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
5552 start, end, 0, ctx);
5559 * should be called during mount to recover any replay any log trees
5562 int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5565 struct btrfs_path *path;
5566 struct btrfs_trans_handle *trans;
5567 struct btrfs_key key;
5568 struct btrfs_key found_key;
5569 struct btrfs_key tmp_key;
5570 struct btrfs_root *log;
5571 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5572 struct walk_control wc = {
5573 .process_func = process_one_buffer,
5577 path = btrfs_alloc_path();
5581 fs_info->log_root_recovering = 1;
5583 trans = btrfs_start_transaction(fs_info->tree_root, 0);
5584 if (IS_ERR(trans)) {
5585 ret = PTR_ERR(trans);
5592 ret = walk_log_tree(trans, log_root_tree, &wc);
5594 btrfs_handle_fs_error(fs_info, ret, "Failed to pin buffers while "
5595 "recovering log root tree.");
5600 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5601 key.offset = (u64)-1;
5602 key.type = BTRFS_ROOT_ITEM_KEY;
5605 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
5608 btrfs_handle_fs_error(fs_info, ret,
5609 "Couldn't find tree log root.");
5613 if (path->slots[0] == 0)
5617 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5619 btrfs_release_path(path);
5620 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5623 log = btrfs_read_fs_root(log_root_tree, &found_key);
5626 btrfs_handle_fs_error(fs_info, ret,
5627 "Couldn't read tree log root.");
5631 tmp_key.objectid = found_key.offset;
5632 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5633 tmp_key.offset = (u64)-1;
5635 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
5636 if (IS_ERR(wc.replay_dest)) {
5637 ret = PTR_ERR(wc.replay_dest);
5638 free_extent_buffer(log->node);
5639 free_extent_buffer(log->commit_root);
5641 btrfs_handle_fs_error(fs_info, ret, "Couldn't read target root "
5642 "for tree log recovery.");
5646 wc.replay_dest->log_root = log;
5647 btrfs_record_root_in_trans(trans, wc.replay_dest);
5648 ret = walk_log_tree(trans, log, &wc);
5650 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5651 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5655 key.offset = found_key.offset - 1;
5656 wc.replay_dest->log_root = NULL;
5657 free_extent_buffer(log->node);
5658 free_extent_buffer(log->commit_root);
5664 if (found_key.offset == 0)
5667 btrfs_release_path(path);
5669 /* step one is to pin it all, step two is to replay just inodes */
5672 wc.process_func = replay_one_buffer;
5673 wc.stage = LOG_WALK_REPLAY_INODES;
5676 /* step three is to replay everything */
5677 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5682 btrfs_free_path(path);
5684 /* step 4: commit the transaction, which also unpins the blocks */
5685 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
5689 free_extent_buffer(log_root_tree->node);
5690 log_root_tree->log_root = NULL;
5691 fs_info->log_root_recovering = 0;
5692 kfree(log_root_tree);
5697 btrfs_end_transaction(wc.trans, fs_info->tree_root);
5698 btrfs_free_path(path);
5703 * there are some corner cases where we want to force a full
5704 * commit instead of allowing a directory to be logged.
5706 * They revolve around files there were unlinked from the directory, and
5707 * this function updates the parent directory so that a full commit is
5708 * properly done if it is fsync'd later after the unlinks are done.
5710 * Must be called before the unlink operations (updates to the subvolume tree,
5711 * inodes, etc) are done.
5713 void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
5714 struct inode *dir, struct inode *inode,
5718 * when we're logging a file, if it hasn't been renamed
5719 * or unlinked, and its inode is fully committed on disk,
5720 * we don't have to worry about walking up the directory chain
5721 * to log its parents.
5723 * So, we use the last_unlink_trans field to put this transid
5724 * into the file. When the file is logged we check it and
5725 * don't log the parents if the file is fully on disk.
5727 mutex_lock(&BTRFS_I(inode)->log_mutex);
5728 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5729 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5732 * if this directory was already logged any new
5733 * names for this file/dir will get recorded
5736 if (BTRFS_I(dir)->logged_trans == trans->transid)
5740 * if the inode we're about to unlink was logged,
5741 * the log will be properly updated for any new names
5743 if (BTRFS_I(inode)->logged_trans == trans->transid)
5747 * when renaming files across directories, if the directory
5748 * there we're unlinking from gets fsync'd later on, there's
5749 * no way to find the destination directory later and fsync it
5750 * properly. So, we have to be conservative and force commits
5751 * so the new name gets discovered.
5756 /* we can safely do the unlink without any special recording */
5760 mutex_lock(&BTRFS_I(dir)->log_mutex);
5761 BTRFS_I(dir)->last_unlink_trans = trans->transid;
5762 mutex_unlock(&BTRFS_I(dir)->log_mutex);
5766 * Make sure that if someone attempts to fsync the parent directory of a deleted
5767 * snapshot, it ends up triggering a transaction commit. This is to guarantee
5768 * that after replaying the log tree of the parent directory's root we will not
5769 * see the snapshot anymore and at log replay time we will not see any log tree
5770 * corresponding to the deleted snapshot's root, which could lead to replaying
5771 * it after replaying the log tree of the parent directory (which would replay
5772 * the snapshot delete operation).
5774 * Must be called before the actual snapshot destroy operation (updates to the
5775 * parent root and tree of tree roots trees, etc) are done.
5777 void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
5780 mutex_lock(&BTRFS_I(dir)->log_mutex);
5781 BTRFS_I(dir)->last_unlink_trans = trans->transid;
5782 mutex_unlock(&BTRFS_I(dir)->log_mutex);
5786 * Call this after adding a new name for a file and it will properly
5787 * update the log to reflect the new name.
5789 * It will return zero if all goes well, and it will return 1 if a
5790 * full transaction commit is required.
5792 int btrfs_log_new_name(struct btrfs_trans_handle *trans,
5793 struct inode *inode, struct inode *old_dir,
5794 struct dentry *parent)
5796 struct btrfs_root * root = BTRFS_I(inode)->root;
5799 * this will force the logging code to walk the dentry chain
5802 if (S_ISREG(inode->i_mode))
5803 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5806 * if this inode hasn't been logged and directory we're renaming it
5807 * from hasn't been logged, we don't need to log it
5809 if (BTRFS_I(inode)->logged_trans <=
5810 root->fs_info->last_trans_committed &&
5811 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
5812 root->fs_info->last_trans_committed))
5815 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
5816 LLONG_MAX, 1, NULL);