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"
31 #include "inode-map.h"
33 /* magic values for the inode_only field in btrfs_log_inode:
35 * LOG_INODE_ALL means to log everything
36 * LOG_INODE_EXISTS means to log just enough to recreate the inode
39 #define LOG_INODE_ALL 0
40 #define LOG_INODE_EXISTS 1
41 #define LOG_OTHER_INODE 2
44 * directory trouble cases
46 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
47 * log, we must force a full commit before doing an fsync of the directory
48 * where the unlink was done.
49 * ---> record transid of last unlink/rename per directory
53 * rename foo/some_dir foo2/some_dir
55 * fsync foo/some_dir/some_file
57 * The fsync above will unlink the original some_dir without recording
58 * it in its new location (foo2). After a crash, some_dir will be gone
59 * unless the fsync of some_file forces a full commit
61 * 2) we must log any new names for any file or dir that is in the fsync
62 * log. ---> check inode while renaming/linking.
64 * 2a) we must log any new names for any file or dir during rename
65 * when the directory they are being removed from was logged.
66 * ---> check inode and old parent dir during rename
68 * 2a is actually the more important variant. With the extra logging
69 * a crash might unlink the old name without recreating the new one
71 * 3) after a crash, we must go through any directories with a link count
72 * of zero and redo the rm -rf
79 * The directory f1 was fully removed from the FS, but fsync was never
80 * called on f1, only its parent dir. After a crash the rm -rf must
81 * be replayed. This must be able to recurse down the entire
82 * directory tree. The inode link count fixup code takes care of the
87 * stages for the tree walking. The first
88 * stage (0) is to only pin down the blocks we find
89 * the second stage (1) is to make sure that all the inodes
90 * we find in the log are created in the subvolume.
92 * The last stage is to deal with directories and links and extents
93 * and all the other fun semantics
95 #define LOG_WALK_PIN_ONLY 0
96 #define LOG_WALK_REPLAY_INODES 1
97 #define LOG_WALK_REPLAY_DIR_INDEX 2
98 #define LOG_WALK_REPLAY_ALL 3
100 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
101 struct btrfs_root *root, struct inode *inode,
105 struct btrfs_log_ctx *ctx);
106 static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
107 struct btrfs_root *root,
108 struct btrfs_path *path, u64 objectid);
109 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
110 struct btrfs_root *root,
111 struct btrfs_root *log,
112 struct btrfs_path *path,
113 u64 dirid, int del_all);
116 * tree logging is a special write ahead log used to make sure that
117 * fsyncs and O_SYNCs can happen without doing full tree commits.
119 * Full tree commits are expensive because they require commonly
120 * modified blocks to be recowed, creating many dirty pages in the
121 * extent tree an 4x-6x higher write load than ext3.
123 * Instead of doing a tree commit on every fsync, we use the
124 * key ranges and transaction ids to find items for a given file or directory
125 * that have changed in this transaction. Those items are copied into
126 * a special tree (one per subvolume root), that tree is written to disk
127 * and then the fsync is considered complete.
129 * After a crash, items are copied out of the log-tree back into the
130 * subvolume tree. Any file data extents found are recorded in the extent
131 * allocation tree, and the log-tree freed.
133 * The log tree is read three times, once to pin down all the extents it is
134 * using in ram and once, once to create all the inodes logged in the tree
135 * and once to do all the other items.
139 * start a sub transaction and setup the log tree
140 * this increments the log tree writer count to make the people
141 * syncing the tree wait for us to finish
143 static int start_log_trans(struct btrfs_trans_handle *trans,
144 struct btrfs_root *root,
145 struct btrfs_log_ctx *ctx)
149 mutex_lock(&root->log_mutex);
151 if (root->log_root) {
152 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
157 if (!root->log_start_pid) {
158 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
159 root->log_start_pid = current->pid;
160 } else if (root->log_start_pid != current->pid) {
161 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
164 mutex_lock(&root->fs_info->tree_log_mutex);
165 if (!root->fs_info->log_root_tree)
166 ret = btrfs_init_log_root_tree(trans, root->fs_info);
167 mutex_unlock(&root->fs_info->tree_log_mutex);
171 ret = btrfs_add_log_tree(trans, root);
175 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
176 root->log_start_pid = current->pid;
179 atomic_inc(&root->log_batch);
180 atomic_inc(&root->log_writers);
182 int index = root->log_transid % 2;
183 list_add_tail(&ctx->list, &root->log_ctxs[index]);
184 ctx->log_transid = root->log_transid;
188 mutex_unlock(&root->log_mutex);
193 * returns 0 if there was a log transaction running and we were able
194 * to join, or returns -ENOENT if there were not transactions
197 static int join_running_log_trans(struct btrfs_root *root)
205 mutex_lock(&root->log_mutex);
206 if (root->log_root) {
208 atomic_inc(&root->log_writers);
210 mutex_unlock(&root->log_mutex);
215 * This either makes the current running log transaction wait
216 * until you call btrfs_end_log_trans() or it makes any future
217 * log transactions wait until you call btrfs_end_log_trans()
219 int btrfs_pin_log_trans(struct btrfs_root *root)
223 mutex_lock(&root->log_mutex);
224 atomic_inc(&root->log_writers);
225 mutex_unlock(&root->log_mutex);
230 * indicate we're done making changes to the log tree
231 * and wake up anyone waiting to do a sync
233 void btrfs_end_log_trans(struct btrfs_root *root)
235 if (atomic_dec_and_test(&root->log_writers)) {
237 * Implicit memory barrier after atomic_dec_and_test
239 if (waitqueue_active(&root->log_writer_wait))
240 wake_up(&root->log_writer_wait);
246 * the walk control struct is used to pass state down the chain when
247 * processing the log tree. The stage field tells us which part
248 * of the log tree processing we are currently doing. The others
249 * are state fields used for that specific part
251 struct walk_control {
252 /* should we free the extent on disk when done? This is used
253 * at transaction commit time while freeing a log tree
257 /* should we write out the extent buffer? This is used
258 * while flushing the log tree to disk during a sync
262 /* should we wait for the extent buffer io to finish? Also used
263 * while flushing the log tree to disk for a sync
267 /* pin only walk, we record which extents on disk belong to the
272 /* what stage of the replay code we're currently in */
275 /* the root we are currently replaying */
276 struct btrfs_root *replay_dest;
278 /* the trans handle for the current replay */
279 struct btrfs_trans_handle *trans;
281 /* the function that gets used to process blocks we find in the
282 * tree. Note the extent_buffer might not be up to date when it is
283 * passed in, and it must be checked or read if you need the data
286 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
287 struct walk_control *wc, u64 gen);
291 * process_func used to pin down extents, write them or wait on them
293 static int process_one_buffer(struct btrfs_root *log,
294 struct extent_buffer *eb,
295 struct walk_control *wc, u64 gen)
300 * If this fs is mixed then we need to be able to process the leaves to
301 * pin down any logged extents, so we have to read the block.
303 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) {
304 ret = btrfs_read_buffer(eb, gen);
310 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root,
313 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
314 if (wc->pin && btrfs_header_level(eb) == 0)
315 ret = btrfs_exclude_logged_extents(log, eb);
317 btrfs_write_tree_block(eb);
319 btrfs_wait_tree_block_writeback(eb);
325 * Item overwrite used by replay and tree logging. eb, slot and key all refer
326 * to the src data we are copying out.
328 * root is the tree we are copying into, and path is a scratch
329 * path for use in this function (it should be released on entry and
330 * will be released on exit).
332 * If the key is already in the destination tree the existing item is
333 * overwritten. If the existing item isn't big enough, it is extended.
334 * If it is too large, it is truncated.
336 * If the key isn't in the destination yet, a new item is inserted.
338 static noinline int overwrite_item(struct btrfs_trans_handle *trans,
339 struct btrfs_root *root,
340 struct btrfs_path *path,
341 struct extent_buffer *eb, int slot,
342 struct btrfs_key *key)
346 u64 saved_i_size = 0;
347 int save_old_i_size = 0;
348 unsigned long src_ptr;
349 unsigned long dst_ptr;
350 int overwrite_root = 0;
351 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
353 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
356 item_size = btrfs_item_size_nr(eb, slot);
357 src_ptr = btrfs_item_ptr_offset(eb, slot);
359 /* look for the key in the destination tree */
360 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
367 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
369 if (dst_size != item_size)
372 if (item_size == 0) {
373 btrfs_release_path(path);
376 dst_copy = kmalloc(item_size, GFP_NOFS);
377 src_copy = kmalloc(item_size, GFP_NOFS);
378 if (!dst_copy || !src_copy) {
379 btrfs_release_path(path);
385 read_extent_buffer(eb, src_copy, src_ptr, item_size);
387 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
388 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
390 ret = memcmp(dst_copy, src_copy, item_size);
395 * they have the same contents, just return, this saves
396 * us from cowing blocks in the destination tree and doing
397 * extra writes that may not have been done by a previous
401 btrfs_release_path(path);
406 * We need to load the old nbytes into the inode so when we
407 * replay the extents we've logged we get the right nbytes.
410 struct btrfs_inode_item *item;
414 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
415 struct btrfs_inode_item);
416 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
417 item = btrfs_item_ptr(eb, slot,
418 struct btrfs_inode_item);
419 btrfs_set_inode_nbytes(eb, item, nbytes);
422 * If this is a directory we need to reset the i_size to
423 * 0 so that we can set it up properly when replaying
424 * the rest of the items in this log.
426 mode = btrfs_inode_mode(eb, item);
428 btrfs_set_inode_size(eb, item, 0);
430 } else if (inode_item) {
431 struct btrfs_inode_item *item;
435 * New inode, set nbytes to 0 so that the nbytes comes out
436 * properly when we replay the extents.
438 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
439 btrfs_set_inode_nbytes(eb, item, 0);
442 * If this is a directory we need to reset the i_size to 0 so
443 * that we can set it up properly when replaying the rest of
444 * the items in this log.
446 mode = btrfs_inode_mode(eb, item);
448 btrfs_set_inode_size(eb, item, 0);
451 btrfs_release_path(path);
452 /* try to insert the key into the destination tree */
453 path->skip_release_on_error = 1;
454 ret = btrfs_insert_empty_item(trans, root, path,
456 path->skip_release_on_error = 0;
458 /* make sure any existing item is the correct size */
459 if (ret == -EEXIST || ret == -EOVERFLOW) {
461 found_size = btrfs_item_size_nr(path->nodes[0],
463 if (found_size > item_size)
464 btrfs_truncate_item(root, path, item_size, 1);
465 else if (found_size < item_size)
466 btrfs_extend_item(root, path,
467 item_size - found_size);
471 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
474 /* don't overwrite an existing inode if the generation number
475 * was logged as zero. This is done when the tree logging code
476 * is just logging an inode to make sure it exists after recovery.
478 * Also, don't overwrite i_size on directories during replay.
479 * log replay inserts and removes directory items based on the
480 * state of the tree found in the subvolume, and i_size is modified
483 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
484 struct btrfs_inode_item *src_item;
485 struct btrfs_inode_item *dst_item;
487 src_item = (struct btrfs_inode_item *)src_ptr;
488 dst_item = (struct btrfs_inode_item *)dst_ptr;
490 if (btrfs_inode_generation(eb, src_item) == 0) {
491 struct extent_buffer *dst_eb = path->nodes[0];
492 const u64 ino_size = btrfs_inode_size(eb, src_item);
495 * For regular files an ino_size == 0 is used only when
496 * logging that an inode exists, as part of a directory
497 * fsync, and the inode wasn't fsynced before. In this
498 * case don't set the size of the inode in the fs/subvol
499 * tree, otherwise we would be throwing valid data away.
501 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
502 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
504 struct btrfs_map_token token;
506 btrfs_init_map_token(&token);
507 btrfs_set_token_inode_size(dst_eb, dst_item,
513 if (overwrite_root &&
514 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
515 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
517 saved_i_size = btrfs_inode_size(path->nodes[0],
522 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
525 if (save_old_i_size) {
526 struct btrfs_inode_item *dst_item;
527 dst_item = (struct btrfs_inode_item *)dst_ptr;
528 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
531 /* make sure the generation is filled in */
532 if (key->type == BTRFS_INODE_ITEM_KEY) {
533 struct btrfs_inode_item *dst_item;
534 dst_item = (struct btrfs_inode_item *)dst_ptr;
535 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
536 btrfs_set_inode_generation(path->nodes[0], dst_item,
541 btrfs_mark_buffer_dirty(path->nodes[0]);
542 btrfs_release_path(path);
547 * simple helper to read an inode off the disk from a given root
548 * This can only be called for subvolume roots and not for the log
550 static noinline struct inode *read_one_inode(struct btrfs_root *root,
553 struct btrfs_key key;
556 key.objectid = objectid;
557 key.type = BTRFS_INODE_ITEM_KEY;
559 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
562 } else if (is_bad_inode(inode)) {
569 /* replays a single extent in 'eb' at 'slot' with 'key' into the
570 * subvolume 'root'. path is released on entry and should be released
573 * extents in the log tree have not been allocated out of the extent
574 * tree yet. So, this completes the allocation, taking a reference
575 * as required if the extent already exists or creating a new extent
576 * if it isn't in the extent allocation tree yet.
578 * The extent is inserted into the file, dropping any existing extents
579 * from the file that overlap the new one.
581 static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
582 struct btrfs_root *root,
583 struct btrfs_path *path,
584 struct extent_buffer *eb, int slot,
585 struct btrfs_key *key)
589 u64 start = key->offset;
591 struct btrfs_file_extent_item *item;
592 struct inode *inode = NULL;
596 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
597 found_type = btrfs_file_extent_type(eb, item);
599 if (found_type == BTRFS_FILE_EXTENT_REG ||
600 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
601 nbytes = btrfs_file_extent_num_bytes(eb, item);
602 extent_end = start + nbytes;
605 * We don't add to the inodes nbytes if we are prealloc or a
608 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
610 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
611 size = btrfs_file_extent_inline_len(eb, slot, item);
612 nbytes = btrfs_file_extent_ram_bytes(eb, item);
613 extent_end = ALIGN(start + size, root->sectorsize);
619 inode = read_one_inode(root, key->objectid);
626 * first check to see if we already have this extent in the
627 * file. This must be done before the btrfs_drop_extents run
628 * so we don't try to drop this extent.
630 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
634 (found_type == BTRFS_FILE_EXTENT_REG ||
635 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
636 struct btrfs_file_extent_item cmp1;
637 struct btrfs_file_extent_item cmp2;
638 struct btrfs_file_extent_item *existing;
639 struct extent_buffer *leaf;
641 leaf = path->nodes[0];
642 existing = btrfs_item_ptr(leaf, path->slots[0],
643 struct btrfs_file_extent_item);
645 read_extent_buffer(eb, &cmp1, (unsigned long)item,
647 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
651 * we already have a pointer to this exact extent,
652 * we don't have to do anything
654 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
655 btrfs_release_path(path);
659 btrfs_release_path(path);
661 /* drop any overlapping extents */
662 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
666 if (found_type == BTRFS_FILE_EXTENT_REG ||
667 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
669 unsigned long dest_offset;
670 struct btrfs_key ins;
672 ret = btrfs_insert_empty_item(trans, root, path, key,
676 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
678 copy_extent_buffer(path->nodes[0], eb, dest_offset,
679 (unsigned long)item, sizeof(*item));
681 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
682 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
683 ins.type = BTRFS_EXTENT_ITEM_KEY;
684 offset = key->offset - btrfs_file_extent_offset(eb, item);
687 * Manually record dirty extent, as here we did a shallow
688 * file extent item copy and skip normal backref update,
689 * but modifying extent tree all by ourselves.
690 * So need to manually record dirty extent for qgroup,
691 * as the owner of the file extent changed from log tree
692 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
694 ret = btrfs_qgroup_insert_dirty_extent(trans, root->fs_info,
695 btrfs_file_extent_disk_bytenr(eb, item),
696 btrfs_file_extent_disk_num_bytes(eb, item),
701 if (ins.objectid > 0) {
704 LIST_HEAD(ordered_sums);
706 * is this extent already allocated in the extent
707 * allocation tree? If so, just add a reference
709 ret = btrfs_lookup_data_extent(root, ins.objectid,
712 ret = btrfs_inc_extent_ref(trans, root,
713 ins.objectid, ins.offset,
714 0, root->root_key.objectid,
715 key->objectid, offset);
720 * insert the extent pointer in the extent
723 ret = btrfs_alloc_logged_file_extent(trans,
724 root, root->root_key.objectid,
725 key->objectid, offset, &ins);
729 btrfs_release_path(path);
731 if (btrfs_file_extent_compression(eb, item)) {
732 csum_start = ins.objectid;
733 csum_end = csum_start + ins.offset;
735 csum_start = ins.objectid +
736 btrfs_file_extent_offset(eb, item);
737 csum_end = csum_start +
738 btrfs_file_extent_num_bytes(eb, item);
741 ret = btrfs_lookup_csums_range(root->log_root,
742 csum_start, csum_end - 1,
747 * Now delete all existing cums in the csum root that
748 * cover our range. We do this because we can have an
749 * extent that is completely referenced by one file
750 * extent item and partially referenced by another
751 * file extent item (like after using the clone or
752 * extent_same ioctls). In this case if we end up doing
753 * the replay of the one that partially references the
754 * extent first, and we do not do the csum deletion
755 * below, we can get 2 csum items in the csum tree that
756 * overlap each other. For example, imagine our log has
757 * the two following file extent items:
759 * key (257 EXTENT_DATA 409600)
760 * extent data disk byte 12845056 nr 102400
761 * extent data offset 20480 nr 20480 ram 102400
763 * key (257 EXTENT_DATA 819200)
764 * extent data disk byte 12845056 nr 102400
765 * extent data offset 0 nr 102400 ram 102400
767 * Where the second one fully references the 100K extent
768 * that starts at disk byte 12845056, and the log tree
769 * has a single csum item that covers the entire range
772 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
774 * After the first file extent item is replayed, the
775 * csum tree gets the following csum item:
777 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
779 * Which covers the 20K sub-range starting at offset 20K
780 * of our extent. Now when we replay the second file
781 * extent item, if we do not delete existing csum items
782 * that cover any of its blocks, we end up getting two
783 * csum items in our csum tree that overlap each other:
785 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
786 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
788 * Which is a problem, because after this anyone trying
789 * to lookup up for the checksum of any block of our
790 * extent starting at an offset of 40K or higher, will
791 * end up looking at the second csum item only, which
792 * does not contain the checksum for any block starting
793 * at offset 40K or higher of our extent.
795 while (!list_empty(&ordered_sums)) {
796 struct btrfs_ordered_sum *sums;
797 sums = list_entry(ordered_sums.next,
798 struct btrfs_ordered_sum,
801 ret = btrfs_del_csums(trans,
802 root->fs_info->csum_root,
806 ret = btrfs_csum_file_blocks(trans,
807 root->fs_info->csum_root,
809 list_del(&sums->list);
815 btrfs_release_path(path);
817 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
818 /* inline extents are easy, we just overwrite them */
819 ret = overwrite_item(trans, root, path, eb, slot, key);
824 inode_add_bytes(inode, nbytes);
825 ret = btrfs_update_inode(trans, root, inode);
833 * when cleaning up conflicts between the directory names in the
834 * subvolume, directory names in the log and directory names in the
835 * inode back references, we may have to unlink inodes from directories.
837 * This is a helper function to do the unlink of a specific directory
840 static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
841 struct btrfs_root *root,
842 struct btrfs_path *path,
844 struct btrfs_dir_item *di)
849 struct extent_buffer *leaf;
850 struct btrfs_key location;
853 leaf = path->nodes[0];
855 btrfs_dir_item_key_to_cpu(leaf, di, &location);
856 name_len = btrfs_dir_name_len(leaf, di);
857 name = kmalloc(name_len, GFP_NOFS);
861 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
862 btrfs_release_path(path);
864 inode = read_one_inode(root, location.objectid);
870 ret = link_to_fixup_dir(trans, root, path, location.objectid);
874 ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
878 ret = btrfs_run_delayed_items(trans, root);
886 * helper function to see if a given name and sequence number found
887 * in an inode back reference are already in a directory and correctly
888 * point to this inode
890 static noinline int inode_in_dir(struct btrfs_root *root,
891 struct btrfs_path *path,
892 u64 dirid, u64 objectid, u64 index,
893 const char *name, int name_len)
895 struct btrfs_dir_item *di;
896 struct btrfs_key location;
899 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
900 index, name, name_len, 0);
901 if (di && !IS_ERR(di)) {
902 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
903 if (location.objectid != objectid)
907 btrfs_release_path(path);
909 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
910 if (di && !IS_ERR(di)) {
911 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
912 if (location.objectid != objectid)
918 btrfs_release_path(path);
923 * helper function to check a log tree for a named back reference in
924 * an inode. This is used to decide if a back reference that is
925 * found in the subvolume conflicts with what we find in the log.
927 * inode backreferences may have multiple refs in a single item,
928 * during replay we process one reference at a time, and we don't
929 * want to delete valid links to a file from the subvolume if that
930 * link is also in the log.
932 static noinline int backref_in_log(struct btrfs_root *log,
933 struct btrfs_key *key,
935 const char *name, int namelen)
937 struct btrfs_path *path;
938 struct btrfs_inode_ref *ref;
940 unsigned long ptr_end;
941 unsigned long name_ptr;
947 path = btrfs_alloc_path();
951 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
955 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
957 if (key->type == BTRFS_INODE_EXTREF_KEY) {
958 if (btrfs_find_name_in_ext_backref(path, ref_objectid,
959 name, namelen, NULL))
965 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
966 ptr_end = ptr + item_size;
967 while (ptr < ptr_end) {
968 ref = (struct btrfs_inode_ref *)ptr;
969 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
970 if (found_name_len == namelen) {
971 name_ptr = (unsigned long)(ref + 1);
972 ret = memcmp_extent_buffer(path->nodes[0], name,
979 ptr = (unsigned long)(ref + 1) + found_name_len;
982 btrfs_free_path(path);
986 static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
987 struct btrfs_root *root,
988 struct btrfs_path *path,
989 struct btrfs_root *log_root,
990 struct inode *dir, struct inode *inode,
991 struct extent_buffer *eb,
992 u64 inode_objectid, u64 parent_objectid,
993 u64 ref_index, char *name, int namelen,
999 struct extent_buffer *leaf;
1000 struct btrfs_dir_item *di;
1001 struct btrfs_key search_key;
1002 struct btrfs_inode_extref *extref;
1005 /* Search old style refs */
1006 search_key.objectid = inode_objectid;
1007 search_key.type = BTRFS_INODE_REF_KEY;
1008 search_key.offset = parent_objectid;
1009 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
1011 struct btrfs_inode_ref *victim_ref;
1013 unsigned long ptr_end;
1015 leaf = path->nodes[0];
1017 /* are we trying to overwrite a back ref for the root directory
1018 * if so, just jump out, we're done
1020 if (search_key.objectid == search_key.offset)
1023 /* check all the names in this back reference to see
1024 * if they are in the log. if so, we allow them to stay
1025 * otherwise they must be unlinked as a conflict
1027 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1028 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1029 while (ptr < ptr_end) {
1030 victim_ref = (struct btrfs_inode_ref *)ptr;
1031 victim_name_len = btrfs_inode_ref_name_len(leaf,
1033 victim_name = kmalloc(victim_name_len, GFP_NOFS);
1037 read_extent_buffer(leaf, victim_name,
1038 (unsigned long)(victim_ref + 1),
1041 if (!backref_in_log(log_root, &search_key,
1046 btrfs_release_path(path);
1048 ret = btrfs_unlink_inode(trans, root, dir,
1054 ret = btrfs_run_delayed_items(trans, root);
1062 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1066 * NOTE: we have searched root tree and checked the
1067 * corresponding ref, it does not need to check again.
1071 btrfs_release_path(path);
1073 /* Same search but for extended refs */
1074 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1075 inode_objectid, parent_objectid, 0,
1077 if (!IS_ERR_OR_NULL(extref)) {
1081 struct inode *victim_parent;
1083 leaf = path->nodes[0];
1085 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1086 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1088 while (cur_offset < item_size) {
1089 extref = (struct btrfs_inode_extref *)(base + cur_offset);
1091 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1093 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1096 victim_name = kmalloc(victim_name_len, GFP_NOFS);
1099 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1102 search_key.objectid = inode_objectid;
1103 search_key.type = BTRFS_INODE_EXTREF_KEY;
1104 search_key.offset = btrfs_extref_hash(parent_objectid,
1108 if (!backref_in_log(log_root, &search_key,
1109 parent_objectid, victim_name,
1112 victim_parent = read_one_inode(root,
1114 if (victim_parent) {
1116 btrfs_release_path(path);
1118 ret = btrfs_unlink_inode(trans, root,
1124 ret = btrfs_run_delayed_items(
1127 iput(victim_parent);
1138 cur_offset += victim_name_len + sizeof(*extref);
1142 btrfs_release_path(path);
1144 /* look for a conflicting sequence number */
1145 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
1146 ref_index, name, namelen, 0);
1147 if (di && !IS_ERR(di)) {
1148 ret = drop_one_dir_item(trans, root, path, dir, di);
1152 btrfs_release_path(path);
1154 /* look for a conflicing name */
1155 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1157 if (di && !IS_ERR(di)) {
1158 ret = drop_one_dir_item(trans, root, path, dir, di);
1162 btrfs_release_path(path);
1167 static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1168 u32 *namelen, char **name, u64 *index,
1169 u64 *parent_objectid)
1171 struct btrfs_inode_extref *extref;
1173 extref = (struct btrfs_inode_extref *)ref_ptr;
1175 *namelen = btrfs_inode_extref_name_len(eb, extref);
1176 *name = kmalloc(*namelen, GFP_NOFS);
1180 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1183 *index = btrfs_inode_extref_index(eb, extref);
1184 if (parent_objectid)
1185 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1190 static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1191 u32 *namelen, char **name, u64 *index)
1193 struct btrfs_inode_ref *ref;
1195 ref = (struct btrfs_inode_ref *)ref_ptr;
1197 *namelen = btrfs_inode_ref_name_len(eb, ref);
1198 *name = kmalloc(*namelen, GFP_NOFS);
1202 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1204 *index = btrfs_inode_ref_index(eb, ref);
1210 * replay one inode back reference item found in the log tree.
1211 * eb, slot and key refer to the buffer and key found in the log tree.
1212 * root is the destination we are replaying into, and path is for temp
1213 * use by this function. (it should be released on return).
1215 static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1216 struct btrfs_root *root,
1217 struct btrfs_root *log,
1218 struct btrfs_path *path,
1219 struct extent_buffer *eb, int slot,
1220 struct btrfs_key *key)
1222 struct inode *dir = NULL;
1223 struct inode *inode = NULL;
1224 unsigned long ref_ptr;
1225 unsigned long ref_end;
1229 int search_done = 0;
1230 int log_ref_ver = 0;
1231 u64 parent_objectid;
1234 int ref_struct_size;
1236 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1237 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1239 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1240 struct btrfs_inode_extref *r;
1242 ref_struct_size = sizeof(struct btrfs_inode_extref);
1244 r = (struct btrfs_inode_extref *)ref_ptr;
1245 parent_objectid = btrfs_inode_extref_parent(eb, r);
1247 ref_struct_size = sizeof(struct btrfs_inode_ref);
1248 parent_objectid = key->offset;
1250 inode_objectid = key->objectid;
1253 * it is possible that we didn't log all the parent directories
1254 * for a given inode. If we don't find the dir, just don't
1255 * copy the back ref in. The link count fixup code will take
1258 dir = read_one_inode(root, parent_objectid);
1264 inode = read_one_inode(root, inode_objectid);
1270 while (ref_ptr < ref_end) {
1272 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1273 &ref_index, &parent_objectid);
1275 * parent object can change from one array
1279 dir = read_one_inode(root, parent_objectid);
1285 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1291 /* if we already have a perfect match, we're done */
1292 if (!inode_in_dir(root, path, btrfs_ino(dir), btrfs_ino(inode),
1293 ref_index, name, namelen)) {
1295 * look for a conflicting back reference in the
1296 * metadata. if we find one we have to unlink that name
1297 * of the file before we add our new link. Later on, we
1298 * overwrite any existing back reference, and we don't
1299 * want to create dangling pointers in the directory.
1303 ret = __add_inode_ref(trans, root, path, log,
1307 ref_index, name, namelen,
1316 /* insert our name */
1317 ret = btrfs_add_link(trans, dir, inode, name, namelen,
1322 btrfs_update_inode(trans, root, inode);
1325 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
1334 /* finally write the back reference in the inode */
1335 ret = overwrite_item(trans, root, path, eb, slot, key);
1337 btrfs_release_path(path);
1344 static int insert_orphan_item(struct btrfs_trans_handle *trans,
1345 struct btrfs_root *root, u64 ino)
1349 ret = btrfs_insert_orphan_item(trans, root, ino);
1356 static int count_inode_extrefs(struct btrfs_root *root,
1357 struct inode *inode, struct btrfs_path *path)
1361 unsigned int nlink = 0;
1364 u64 inode_objectid = btrfs_ino(inode);
1367 struct btrfs_inode_extref *extref;
1368 struct extent_buffer *leaf;
1371 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1376 leaf = path->nodes[0];
1377 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1378 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1381 while (cur_offset < item_size) {
1382 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1383 name_len = btrfs_inode_extref_name_len(leaf, extref);
1387 cur_offset += name_len + sizeof(*extref);
1391 btrfs_release_path(path);
1393 btrfs_release_path(path);
1395 if (ret < 0 && ret != -ENOENT)
1400 static int count_inode_refs(struct btrfs_root *root,
1401 struct inode *inode, struct btrfs_path *path)
1404 struct btrfs_key key;
1405 unsigned int nlink = 0;
1407 unsigned long ptr_end;
1409 u64 ino = btrfs_ino(inode);
1412 key.type = BTRFS_INODE_REF_KEY;
1413 key.offset = (u64)-1;
1416 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1420 if (path->slots[0] == 0)
1425 btrfs_item_key_to_cpu(path->nodes[0], &key,
1427 if (key.objectid != ino ||
1428 key.type != BTRFS_INODE_REF_KEY)
1430 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1431 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1433 while (ptr < ptr_end) {
1434 struct btrfs_inode_ref *ref;
1436 ref = (struct btrfs_inode_ref *)ptr;
1437 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1439 ptr = (unsigned long)(ref + 1) + name_len;
1443 if (key.offset == 0)
1445 if (path->slots[0] > 0) {
1450 btrfs_release_path(path);
1452 btrfs_release_path(path);
1458 * There are a few corners where the link count of the file can't
1459 * be properly maintained during replay. So, instead of adding
1460 * lots of complexity to the log code, we just scan the backrefs
1461 * for any file that has been through replay.
1463 * The scan will update the link count on the inode to reflect the
1464 * number of back refs found. If it goes down to zero, the iput
1465 * will free the inode.
1467 static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1468 struct btrfs_root *root,
1469 struct inode *inode)
1471 struct btrfs_path *path;
1474 u64 ino = btrfs_ino(inode);
1476 path = btrfs_alloc_path();
1480 ret = count_inode_refs(root, inode, path);
1486 ret = count_inode_extrefs(root, inode, path);
1494 if (nlink != inode->i_nlink) {
1495 set_nlink(inode, nlink);
1496 btrfs_update_inode(trans, root, inode);
1498 BTRFS_I(inode)->index_cnt = (u64)-1;
1500 if (inode->i_nlink == 0) {
1501 if (S_ISDIR(inode->i_mode)) {
1502 ret = replay_dir_deletes(trans, root, NULL, path,
1507 ret = insert_orphan_item(trans, root, ino);
1511 btrfs_free_path(path);
1515 static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1516 struct btrfs_root *root,
1517 struct btrfs_path *path)
1520 struct btrfs_key key;
1521 struct inode *inode;
1523 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1524 key.type = BTRFS_ORPHAN_ITEM_KEY;
1525 key.offset = (u64)-1;
1527 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1533 if (path->slots[0] == 0)
1538 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1539 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1540 key.type != BTRFS_ORPHAN_ITEM_KEY)
1543 ret = btrfs_del_item(trans, root, path);
1547 btrfs_release_path(path);
1548 inode = read_one_inode(root, key.offset);
1554 ret = fixup_inode_link_count(trans, root, inode);
1560 * fixup on a directory may create new entries,
1561 * make sure we always look for the highset possible
1564 key.offset = (u64)-1;
1566 btrfs_release_path(path);
1572 * record a given inode in the fixup dir so we can check its link
1573 * count when replay is done. The link count is incremented here
1574 * so the inode won't go away until we check it
1576 static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1577 struct btrfs_root *root,
1578 struct btrfs_path *path,
1581 struct btrfs_key key;
1583 struct inode *inode;
1585 inode = read_one_inode(root, objectid);
1589 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1590 key.type = BTRFS_ORPHAN_ITEM_KEY;
1591 key.offset = objectid;
1593 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1595 btrfs_release_path(path);
1597 if (!inode->i_nlink)
1598 set_nlink(inode, 1);
1601 ret = btrfs_update_inode(trans, root, inode);
1602 } else if (ret == -EEXIST) {
1611 * when replaying the log for a directory, we only insert names
1612 * for inodes that actually exist. This means an fsync on a directory
1613 * does not implicitly fsync all the new files in it
1615 static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1616 struct btrfs_root *root,
1617 u64 dirid, u64 index,
1618 char *name, int name_len,
1619 struct btrfs_key *location)
1621 struct inode *inode;
1625 inode = read_one_inode(root, location->objectid);
1629 dir = read_one_inode(root, dirid);
1635 ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
1637 /* FIXME, put inode into FIXUP list */
1645 * Return true if an inode reference exists in the log for the given name,
1646 * inode and parent inode.
1648 static bool name_in_log_ref(struct btrfs_root *log_root,
1649 const char *name, const int name_len,
1650 const u64 dirid, const u64 ino)
1652 struct btrfs_key search_key;
1654 search_key.objectid = ino;
1655 search_key.type = BTRFS_INODE_REF_KEY;
1656 search_key.offset = dirid;
1657 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1660 search_key.type = BTRFS_INODE_EXTREF_KEY;
1661 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1662 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1669 * take a single entry in a log directory item and replay it into
1672 * if a conflicting item exists in the subdirectory already,
1673 * the inode it points to is unlinked and put into the link count
1676 * If a name from the log points to a file or directory that does
1677 * not exist in the FS, it is skipped. fsyncs on directories
1678 * do not force down inodes inside that directory, just changes to the
1679 * names or unlinks in a directory.
1681 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1682 * non-existing inode) and 1 if the name was replayed.
1684 static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1685 struct btrfs_root *root,
1686 struct btrfs_path *path,
1687 struct extent_buffer *eb,
1688 struct btrfs_dir_item *di,
1689 struct btrfs_key *key)
1693 struct btrfs_dir_item *dst_di;
1694 struct btrfs_key found_key;
1695 struct btrfs_key log_key;
1700 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
1701 bool name_added = false;
1703 dir = read_one_inode(root, key->objectid);
1707 name_len = btrfs_dir_name_len(eb, di);
1708 name = kmalloc(name_len, GFP_NOFS);
1714 log_type = btrfs_dir_type(eb, di);
1715 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1718 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
1719 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1724 btrfs_release_path(path);
1726 if (key->type == BTRFS_DIR_ITEM_KEY) {
1727 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1729 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
1730 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1739 if (IS_ERR_OR_NULL(dst_di)) {
1740 /* we need a sequence number to insert, so we only
1741 * do inserts for the BTRFS_DIR_INDEX_KEY types
1743 if (key->type != BTRFS_DIR_INDEX_KEY)
1748 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1749 /* the existing item matches the logged item */
1750 if (found_key.objectid == log_key.objectid &&
1751 found_key.type == log_key.type &&
1752 found_key.offset == log_key.offset &&
1753 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1754 update_size = false;
1759 * don't drop the conflicting directory entry if the inode
1760 * for the new entry doesn't exist
1765 ret = drop_one_dir_item(trans, root, path, dir, dst_di);
1769 if (key->type == BTRFS_DIR_INDEX_KEY)
1772 btrfs_release_path(path);
1773 if (!ret && update_size) {
1774 btrfs_i_size_write(dir, dir->i_size + name_len * 2);
1775 ret = btrfs_update_inode(trans, root, dir);
1779 if (!ret && name_added)
1784 if (name_in_log_ref(root->log_root, name, name_len,
1785 key->objectid, log_key.objectid)) {
1786 /* The dentry will be added later. */
1788 update_size = false;
1791 btrfs_release_path(path);
1792 ret = insert_one_name(trans, root, key->objectid, key->offset,
1793 name, name_len, &log_key);
1794 if (ret && ret != -ENOENT && ret != -EEXIST)
1798 update_size = false;
1804 * find all the names in a directory item and reconcile them into
1805 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1806 * one name in a directory item, but the same code gets used for
1807 * both directory index types
1809 static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1810 struct btrfs_root *root,
1811 struct btrfs_path *path,
1812 struct extent_buffer *eb, int slot,
1813 struct btrfs_key *key)
1816 u32 item_size = btrfs_item_size_nr(eb, slot);
1817 struct btrfs_dir_item *di;
1820 unsigned long ptr_end;
1821 struct btrfs_path *fixup_path = NULL;
1823 ptr = btrfs_item_ptr_offset(eb, slot);
1824 ptr_end = ptr + item_size;
1825 while (ptr < ptr_end) {
1826 di = (struct btrfs_dir_item *)ptr;
1827 if (verify_dir_item(root, eb, di))
1829 name_len = btrfs_dir_name_len(eb, di);
1830 ret = replay_one_name(trans, root, path, eb, di, key);
1833 ptr = (unsigned long)(di + 1);
1837 * If this entry refers to a non-directory (directories can not
1838 * have a link count > 1) and it was added in the transaction
1839 * that was not committed, make sure we fixup the link count of
1840 * the inode it the entry points to. Otherwise something like
1841 * the following would result in a directory pointing to an
1842 * inode with a wrong link that does not account for this dir
1850 * ln testdir/bar testdir/bar_link
1851 * ln testdir/foo testdir/foo_link
1852 * xfs_io -c "fsync" testdir/bar
1856 * mount fs, log replay happens
1858 * File foo would remain with a link count of 1 when it has two
1859 * entries pointing to it in the directory testdir. This would
1860 * make it impossible to ever delete the parent directory has
1861 * it would result in stale dentries that can never be deleted.
1863 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
1864 struct btrfs_key di_key;
1867 fixup_path = btrfs_alloc_path();
1874 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1875 ret = link_to_fixup_dir(trans, root, fixup_path,
1882 btrfs_free_path(fixup_path);
1887 * directory replay has two parts. There are the standard directory
1888 * items in the log copied from the subvolume, and range items
1889 * created in the log while the subvolume was logged.
1891 * The range items tell us which parts of the key space the log
1892 * is authoritative for. During replay, if a key in the subvolume
1893 * directory is in a logged range item, but not actually in the log
1894 * that means it was deleted from the directory before the fsync
1895 * and should be removed.
1897 static noinline int find_dir_range(struct btrfs_root *root,
1898 struct btrfs_path *path,
1899 u64 dirid, int key_type,
1900 u64 *start_ret, u64 *end_ret)
1902 struct btrfs_key key;
1904 struct btrfs_dir_log_item *item;
1908 if (*start_ret == (u64)-1)
1911 key.objectid = dirid;
1912 key.type = key_type;
1913 key.offset = *start_ret;
1915 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1919 if (path->slots[0] == 0)
1924 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1926 if (key.type != key_type || key.objectid != dirid) {
1930 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1931 struct btrfs_dir_log_item);
1932 found_end = btrfs_dir_log_end(path->nodes[0], item);
1934 if (*start_ret >= key.offset && *start_ret <= found_end) {
1936 *start_ret = key.offset;
1937 *end_ret = found_end;
1942 /* check the next slot in the tree to see if it is a valid item */
1943 nritems = btrfs_header_nritems(path->nodes[0]);
1945 if (path->slots[0] >= nritems) {
1946 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);
2234 dir_key.offset = range_start;
2237 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2242 nritems = btrfs_header_nritems(path->nodes[0]);
2243 if (path->slots[0] >= nritems) {
2244 ret = btrfs_next_leaf(root, path);
2250 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2252 if (found_key.objectid != dirid ||
2253 found_key.type != dir_key.type)
2256 if (found_key.offset > range_end)
2259 ret = check_item_in_log(trans, root, log, path,
2264 if (found_key.offset == (u64)-1)
2266 dir_key.offset = found_key.offset + 1;
2268 btrfs_release_path(path);
2269 if (range_end == (u64)-1)
2271 range_start = range_end + 1;
2276 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2277 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2278 dir_key.type = BTRFS_DIR_INDEX_KEY;
2279 btrfs_release_path(path);
2283 btrfs_release_path(path);
2284 btrfs_free_path(log_path);
2290 * the process_func used to replay items from the log tree. This
2291 * gets called in two different stages. The first stage just looks
2292 * for inodes and makes sure they are all copied into the subvolume.
2294 * The second stage copies all the other item types from the log into
2295 * the subvolume. The two stage approach is slower, but gets rid of
2296 * lots of complexity around inodes referencing other inodes that exist
2297 * only in the log (references come from either directory items or inode
2300 static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2301 struct walk_control *wc, u64 gen)
2304 struct btrfs_path *path;
2305 struct btrfs_root *root = wc->replay_dest;
2306 struct btrfs_key key;
2311 ret = btrfs_read_buffer(eb, gen);
2315 level = btrfs_header_level(eb);
2320 path = btrfs_alloc_path();
2324 nritems = btrfs_header_nritems(eb);
2325 for (i = 0; i < nritems; i++) {
2326 btrfs_item_key_to_cpu(eb, &key, i);
2328 /* inode keys are done during the first stage */
2329 if (key.type == BTRFS_INODE_ITEM_KEY &&
2330 wc->stage == LOG_WALK_REPLAY_INODES) {
2331 struct btrfs_inode_item *inode_item;
2334 inode_item = btrfs_item_ptr(eb, i,
2335 struct btrfs_inode_item);
2336 ret = replay_xattr_deletes(wc->trans, root, log,
2337 path, key.objectid);
2340 mode = btrfs_inode_mode(eb, inode_item);
2341 if (S_ISDIR(mode)) {
2342 ret = replay_dir_deletes(wc->trans,
2343 root, log, path, key.objectid, 0);
2347 ret = overwrite_item(wc->trans, root, path,
2352 /* for regular files, make sure corresponding
2353 * orphan item exist. extents past the new EOF
2354 * will be truncated later by orphan cleanup.
2356 if (S_ISREG(mode)) {
2357 ret = insert_orphan_item(wc->trans, root,
2363 ret = link_to_fixup_dir(wc->trans, root,
2364 path, key.objectid);
2369 if (key.type == BTRFS_DIR_INDEX_KEY &&
2370 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2371 ret = replay_one_dir_item(wc->trans, root, path,
2377 if (wc->stage < LOG_WALK_REPLAY_ALL)
2380 /* these keys are simply copied */
2381 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2382 ret = overwrite_item(wc->trans, root, path,
2386 } else if (key.type == BTRFS_INODE_REF_KEY ||
2387 key.type == BTRFS_INODE_EXTREF_KEY) {
2388 ret = add_inode_ref(wc->trans, root, log, path,
2390 if (ret && ret != -ENOENT)
2393 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2394 ret = replay_one_extent(wc->trans, root, path,
2398 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
2399 ret = replay_one_dir_item(wc->trans, root, path,
2405 btrfs_free_path(path);
2409 static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2410 struct btrfs_root *root,
2411 struct btrfs_path *path, int *level,
2412 struct walk_control *wc)
2417 struct extent_buffer *next;
2418 struct extent_buffer *cur;
2419 struct extent_buffer *parent;
2423 WARN_ON(*level < 0);
2424 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2426 while (*level > 0) {
2427 WARN_ON(*level < 0);
2428 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2429 cur = path->nodes[*level];
2431 WARN_ON(btrfs_header_level(cur) != *level);
2433 if (path->slots[*level] >=
2434 btrfs_header_nritems(cur))
2437 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2438 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2439 blocksize = root->nodesize;
2441 parent = path->nodes[*level];
2442 root_owner = btrfs_header_owner(parent);
2444 next = btrfs_find_create_tree_block(root, bytenr);
2446 return PTR_ERR(next);
2449 ret = wc->process_func(root, next, wc, ptr_gen);
2451 free_extent_buffer(next);
2455 path->slots[*level]++;
2457 ret = btrfs_read_buffer(next, ptr_gen);
2459 free_extent_buffer(next);
2464 btrfs_tree_lock(next);
2465 btrfs_set_lock_blocking(next);
2466 clean_tree_block(trans, root->fs_info,
2468 btrfs_wait_tree_block_writeback(next);
2469 btrfs_tree_unlock(next);
2471 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2472 clear_extent_buffer_dirty(next);
2475 WARN_ON(root_owner !=
2476 BTRFS_TREE_LOG_OBJECTID);
2477 ret = btrfs_free_and_pin_reserved_extent(root,
2480 free_extent_buffer(next);
2484 free_extent_buffer(next);
2487 ret = btrfs_read_buffer(next, ptr_gen);
2489 free_extent_buffer(next);
2493 WARN_ON(*level <= 0);
2494 if (path->nodes[*level-1])
2495 free_extent_buffer(path->nodes[*level-1]);
2496 path->nodes[*level-1] = next;
2497 *level = btrfs_header_level(next);
2498 path->slots[*level] = 0;
2501 WARN_ON(*level < 0);
2502 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2504 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
2510 static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
2511 struct btrfs_root *root,
2512 struct btrfs_path *path, int *level,
2513 struct walk_control *wc)
2520 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2521 slot = path->slots[i];
2522 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
2525 WARN_ON(*level == 0);
2528 struct extent_buffer *parent;
2529 if (path->nodes[*level] == root->node)
2530 parent = path->nodes[*level];
2532 parent = path->nodes[*level + 1];
2534 root_owner = btrfs_header_owner(parent);
2535 ret = wc->process_func(root, path->nodes[*level], wc,
2536 btrfs_header_generation(path->nodes[*level]));
2541 struct extent_buffer *next;
2543 next = path->nodes[*level];
2546 btrfs_tree_lock(next);
2547 btrfs_set_lock_blocking(next);
2548 clean_tree_block(trans, root->fs_info,
2550 btrfs_wait_tree_block_writeback(next);
2551 btrfs_tree_unlock(next);
2553 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2554 clear_extent_buffer_dirty(next);
2557 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
2558 ret = btrfs_free_and_pin_reserved_extent(root,
2559 path->nodes[*level]->start,
2560 path->nodes[*level]->len);
2564 free_extent_buffer(path->nodes[*level]);
2565 path->nodes[*level] = NULL;
2573 * drop the reference count on the tree rooted at 'snap'. This traverses
2574 * the tree freeing any blocks that have a ref count of zero after being
2577 static int walk_log_tree(struct btrfs_trans_handle *trans,
2578 struct btrfs_root *log, struct walk_control *wc)
2583 struct btrfs_path *path;
2586 path = btrfs_alloc_path();
2590 level = btrfs_header_level(log->node);
2592 path->nodes[level] = log->node;
2593 extent_buffer_get(log->node);
2594 path->slots[level] = 0;
2597 wret = walk_down_log_tree(trans, log, path, &level, wc);
2605 wret = walk_up_log_tree(trans, log, path, &level, wc);
2614 /* was the root node processed? if not, catch it here */
2615 if (path->nodes[orig_level]) {
2616 ret = wc->process_func(log, path->nodes[orig_level], wc,
2617 btrfs_header_generation(path->nodes[orig_level]));
2621 struct extent_buffer *next;
2623 next = path->nodes[orig_level];
2626 btrfs_tree_lock(next);
2627 btrfs_set_lock_blocking(next);
2628 clean_tree_block(trans, log->fs_info, next);
2629 btrfs_wait_tree_block_writeback(next);
2630 btrfs_tree_unlock(next);
2632 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2633 clear_extent_buffer_dirty(next);
2636 WARN_ON(log->root_key.objectid !=
2637 BTRFS_TREE_LOG_OBJECTID);
2638 ret = btrfs_free_and_pin_reserved_extent(log, next->start,
2646 btrfs_free_path(path);
2651 * helper function to update the item for a given subvolumes log root
2652 * in the tree of log roots
2654 static int update_log_root(struct btrfs_trans_handle *trans,
2655 struct btrfs_root *log)
2659 if (log->log_transid == 1) {
2660 /* insert root item on the first sync */
2661 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
2662 &log->root_key, &log->root_item);
2664 ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
2665 &log->root_key, &log->root_item);
2670 static void wait_log_commit(struct btrfs_root *root, int transid)
2673 int index = transid % 2;
2676 * we only allow two pending log transactions at a time,
2677 * so we know that if ours is more than 2 older than the
2678 * current transaction, we're done
2681 prepare_to_wait(&root->log_commit_wait[index],
2682 &wait, TASK_UNINTERRUPTIBLE);
2683 mutex_unlock(&root->log_mutex);
2685 if (root->log_transid_committed < transid &&
2686 atomic_read(&root->log_commit[index]))
2689 finish_wait(&root->log_commit_wait[index], &wait);
2690 mutex_lock(&root->log_mutex);
2691 } while (root->log_transid_committed < transid &&
2692 atomic_read(&root->log_commit[index]));
2695 static void wait_for_writer(struct btrfs_root *root)
2699 while (atomic_read(&root->log_writers)) {
2700 prepare_to_wait(&root->log_writer_wait,
2701 &wait, TASK_UNINTERRUPTIBLE);
2702 mutex_unlock(&root->log_mutex);
2703 if (atomic_read(&root->log_writers))
2705 finish_wait(&root->log_writer_wait, &wait);
2706 mutex_lock(&root->log_mutex);
2710 static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2711 struct btrfs_log_ctx *ctx)
2716 mutex_lock(&root->log_mutex);
2717 list_del_init(&ctx->list);
2718 mutex_unlock(&root->log_mutex);
2722 * Invoked in log mutex context, or be sure there is no other task which
2723 * can access the list.
2725 static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2726 int index, int error)
2728 struct btrfs_log_ctx *ctx;
2729 struct btrfs_log_ctx *safe;
2731 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2732 list_del_init(&ctx->list);
2733 ctx->log_ret = error;
2736 INIT_LIST_HEAD(&root->log_ctxs[index]);
2740 * btrfs_sync_log does sends a given tree log down to the disk and
2741 * updates the super blocks to record it. When this call is done,
2742 * you know that any inodes previously logged are safely on disk only
2745 * Any other return value means you need to call btrfs_commit_transaction.
2746 * Some of the edge cases for fsyncing directories that have had unlinks
2747 * or renames done in the past mean that sometimes the only safe
2748 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2749 * that has happened.
2751 int btrfs_sync_log(struct btrfs_trans_handle *trans,
2752 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
2758 struct btrfs_root *log = root->log_root;
2759 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
2760 int log_transid = 0;
2761 struct btrfs_log_ctx root_log_ctx;
2762 struct blk_plug plug;
2764 mutex_lock(&root->log_mutex);
2765 log_transid = ctx->log_transid;
2766 if (root->log_transid_committed >= log_transid) {
2767 mutex_unlock(&root->log_mutex);
2768 return ctx->log_ret;
2771 index1 = log_transid % 2;
2772 if (atomic_read(&root->log_commit[index1])) {
2773 wait_log_commit(root, log_transid);
2774 mutex_unlock(&root->log_mutex);
2775 return ctx->log_ret;
2777 ASSERT(log_transid == root->log_transid);
2778 atomic_set(&root->log_commit[index1], 1);
2780 /* wait for previous tree log sync to complete */
2781 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
2782 wait_log_commit(root, log_transid - 1);
2785 int batch = atomic_read(&root->log_batch);
2786 /* when we're on an ssd, just kick the log commit out */
2787 if (!btrfs_test_opt(root->fs_info, SSD) &&
2788 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
2789 mutex_unlock(&root->log_mutex);
2790 schedule_timeout_uninterruptible(1);
2791 mutex_lock(&root->log_mutex);
2793 wait_for_writer(root);
2794 if (batch == atomic_read(&root->log_batch))
2798 /* bail out if we need to do a full commit */
2799 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
2801 btrfs_free_logged_extents(log, log_transid);
2802 mutex_unlock(&root->log_mutex);
2806 if (log_transid % 2 == 0)
2807 mark = EXTENT_DIRTY;
2811 /* we start IO on all the marked extents here, but we don't actually
2812 * wait for them until later.
2814 blk_start_plug(&plug);
2815 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
2817 blk_finish_plug(&plug);
2818 btrfs_abort_transaction(trans, ret);
2819 btrfs_free_logged_extents(log, log_transid);
2820 btrfs_set_log_full_commit(root->fs_info, trans);
2821 mutex_unlock(&root->log_mutex);
2825 btrfs_set_root_node(&log->root_item, log->node);
2827 root->log_transid++;
2828 log->log_transid = root->log_transid;
2829 root->log_start_pid = 0;
2831 * Update or create log root item under the root's log_mutex to prevent
2832 * races with concurrent log syncs that can lead to failure to update
2833 * log root item because it was not created yet.
2835 ret = update_log_root(trans, log);
2837 * IO has been started, blocks of the log tree have WRITTEN flag set
2838 * in their headers. new modifications of the log will be written to
2839 * new positions. so it's safe to allow log writers to go in.
2841 mutex_unlock(&root->log_mutex);
2843 btrfs_init_log_ctx(&root_log_ctx, NULL);
2845 mutex_lock(&log_root_tree->log_mutex);
2846 atomic_inc(&log_root_tree->log_batch);
2847 atomic_inc(&log_root_tree->log_writers);
2849 index2 = log_root_tree->log_transid % 2;
2850 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
2851 root_log_ctx.log_transid = log_root_tree->log_transid;
2853 mutex_unlock(&log_root_tree->log_mutex);
2855 mutex_lock(&log_root_tree->log_mutex);
2856 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
2858 * Implicit memory barrier after atomic_dec_and_test
2860 if (waitqueue_active(&log_root_tree->log_writer_wait))
2861 wake_up(&log_root_tree->log_writer_wait);
2865 if (!list_empty(&root_log_ctx.list))
2866 list_del_init(&root_log_ctx.list);
2868 blk_finish_plug(&plug);
2869 btrfs_set_log_full_commit(root->fs_info, trans);
2871 if (ret != -ENOSPC) {
2872 btrfs_abort_transaction(trans, ret);
2873 mutex_unlock(&log_root_tree->log_mutex);
2876 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2877 btrfs_free_logged_extents(log, log_transid);
2878 mutex_unlock(&log_root_tree->log_mutex);
2883 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
2884 blk_finish_plug(&plug);
2885 list_del_init(&root_log_ctx.list);
2886 mutex_unlock(&log_root_tree->log_mutex);
2887 ret = root_log_ctx.log_ret;
2891 index2 = root_log_ctx.log_transid % 2;
2892 if (atomic_read(&log_root_tree->log_commit[index2])) {
2893 blk_finish_plug(&plug);
2894 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages,
2896 btrfs_wait_logged_extents(trans, log, log_transid);
2897 wait_log_commit(log_root_tree,
2898 root_log_ctx.log_transid);
2899 mutex_unlock(&log_root_tree->log_mutex);
2901 ret = root_log_ctx.log_ret;
2904 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
2905 atomic_set(&log_root_tree->log_commit[index2], 1);
2907 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
2908 wait_log_commit(log_root_tree,
2909 root_log_ctx.log_transid - 1);
2912 wait_for_writer(log_root_tree);
2915 * now that we've moved on to the tree of log tree roots,
2916 * check the full commit flag again
2918 if (btrfs_need_log_full_commit(root->fs_info, trans)) {
2919 blk_finish_plug(&plug);
2920 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2921 btrfs_free_logged_extents(log, log_transid);
2922 mutex_unlock(&log_root_tree->log_mutex);
2924 goto out_wake_log_root;
2927 ret = btrfs_write_marked_extents(log_root_tree,
2928 &log_root_tree->dirty_log_pages,
2929 EXTENT_DIRTY | EXTENT_NEW);
2930 blk_finish_plug(&plug);
2932 btrfs_set_log_full_commit(root->fs_info, trans);
2933 btrfs_abort_transaction(trans, ret);
2934 btrfs_free_logged_extents(log, log_transid);
2935 mutex_unlock(&log_root_tree->log_mutex);
2936 goto out_wake_log_root;
2938 ret = btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2940 ret = btrfs_wait_marked_extents(log_root_tree,
2941 &log_root_tree->dirty_log_pages,
2942 EXTENT_NEW | EXTENT_DIRTY);
2944 btrfs_set_log_full_commit(root->fs_info, trans);
2945 btrfs_free_logged_extents(log, log_transid);
2946 mutex_unlock(&log_root_tree->log_mutex);
2947 goto out_wake_log_root;
2949 btrfs_wait_logged_extents(trans, log, log_transid);
2951 btrfs_set_super_log_root(root->fs_info->super_for_commit,
2952 log_root_tree->node->start);
2953 btrfs_set_super_log_root_level(root->fs_info->super_for_commit,
2954 btrfs_header_level(log_root_tree->node));
2956 log_root_tree->log_transid++;
2957 mutex_unlock(&log_root_tree->log_mutex);
2960 * nobody else is going to jump in and write the the ctree
2961 * super here because the log_commit atomic below is protecting
2962 * us. We must be called with a transaction handle pinning
2963 * the running transaction open, so a full commit can't hop
2964 * in and cause problems either.
2966 ret = write_ctree_super(trans, root->fs_info->tree_root, 1);
2968 btrfs_set_log_full_commit(root->fs_info, trans);
2969 btrfs_abort_transaction(trans, ret);
2970 goto out_wake_log_root;
2973 mutex_lock(&root->log_mutex);
2974 if (root->last_log_commit < log_transid)
2975 root->last_log_commit = log_transid;
2976 mutex_unlock(&root->log_mutex);
2979 mutex_lock(&log_root_tree->log_mutex);
2980 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
2982 log_root_tree->log_transid_committed++;
2983 atomic_set(&log_root_tree->log_commit[index2], 0);
2984 mutex_unlock(&log_root_tree->log_mutex);
2987 * The barrier before waitqueue_active is needed so all the updates
2988 * above are seen by the woken threads. It might not be necessary, but
2989 * proving that seems to be hard.
2992 if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
2993 wake_up(&log_root_tree->log_commit_wait[index2]);
2995 mutex_lock(&root->log_mutex);
2996 btrfs_remove_all_log_ctxs(root, index1, ret);
2997 root->log_transid_committed++;
2998 atomic_set(&root->log_commit[index1], 0);
2999 mutex_unlock(&root->log_mutex);
3002 * The barrier before waitqueue_active is needed so all the updates
3003 * above are seen by the woken threads. It might not be necessary, but
3004 * proving that seems to be hard.
3007 if (waitqueue_active(&root->log_commit_wait[index1]))
3008 wake_up(&root->log_commit_wait[index1]);
3012 static void free_log_tree(struct btrfs_trans_handle *trans,
3013 struct btrfs_root *log)
3018 struct walk_control wc = {
3020 .process_func = process_one_buffer
3023 ret = walk_log_tree(trans, log, &wc);
3026 btrfs_abort_transaction(trans, ret);
3028 btrfs_handle_fs_error(log->fs_info, ret, NULL);
3032 ret = find_first_extent_bit(&log->dirty_log_pages,
3034 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
3039 clear_extent_bits(&log->dirty_log_pages, start, end,
3040 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
3044 * We may have short-circuited the log tree with the full commit logic
3045 * and left ordered extents on our list, so clear these out to keep us
3046 * from leaking inodes and memory.
3048 btrfs_free_logged_extents(log, 0);
3049 btrfs_free_logged_extents(log, 1);
3051 free_extent_buffer(log->node);
3056 * free all the extents used by the tree log. This should be called
3057 * at commit time of the full transaction
3059 int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3061 if (root->log_root) {
3062 free_log_tree(trans, root->log_root);
3063 root->log_root = NULL;
3068 int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3069 struct btrfs_fs_info *fs_info)
3071 if (fs_info->log_root_tree) {
3072 free_log_tree(trans, fs_info->log_root_tree);
3073 fs_info->log_root_tree = NULL;
3079 * If both a file and directory are logged, and unlinks or renames are
3080 * mixed in, we have a few interesting corners:
3082 * create file X in dir Y
3083 * link file X to X.link in dir Y
3085 * unlink file X but leave X.link
3088 * After a crash we would expect only X.link to exist. But file X
3089 * didn't get fsync'd again so the log has back refs for X and X.link.
3091 * We solve this by removing directory entries and inode backrefs from the
3092 * log when a file that was logged in the current transaction is
3093 * unlinked. Any later fsync will include the updated log entries, and
3094 * we'll be able to reconstruct the proper directory items from backrefs.
3096 * This optimizations allows us to avoid relogging the entire inode
3097 * or the entire directory.
3099 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3100 struct btrfs_root *root,
3101 const char *name, int name_len,
3102 struct inode *dir, u64 index)
3104 struct btrfs_root *log;
3105 struct btrfs_dir_item *di;
3106 struct btrfs_path *path;
3110 u64 dir_ino = btrfs_ino(dir);
3112 if (BTRFS_I(dir)->logged_trans < trans->transid)
3115 ret = join_running_log_trans(root);
3119 mutex_lock(&BTRFS_I(dir)->log_mutex);
3121 log = root->log_root;
3122 path = btrfs_alloc_path();
3128 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
3129 name, name_len, -1);
3135 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3136 bytes_del += name_len;
3142 btrfs_release_path(path);
3143 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
3144 index, name, name_len, -1);
3150 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3151 bytes_del += name_len;
3158 /* update the directory size in the log to reflect the names
3162 struct btrfs_key key;
3164 key.objectid = dir_ino;
3166 key.type = BTRFS_INODE_ITEM_KEY;
3167 btrfs_release_path(path);
3169 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
3175 struct btrfs_inode_item *item;
3178 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3179 struct btrfs_inode_item);
3180 i_size = btrfs_inode_size(path->nodes[0], item);
3181 if (i_size > bytes_del)
3182 i_size -= bytes_del;
3185 btrfs_set_inode_size(path->nodes[0], item, i_size);
3186 btrfs_mark_buffer_dirty(path->nodes[0]);
3189 btrfs_release_path(path);
3192 btrfs_free_path(path);
3194 mutex_unlock(&BTRFS_I(dir)->log_mutex);
3195 if (err == -ENOSPC) {
3196 btrfs_set_log_full_commit(root->fs_info, trans);
3198 } else if (err < 0 && err != -ENOENT) {
3199 /* ENOENT can be returned if the entry hasn't been fsynced yet */
3200 btrfs_abort_transaction(trans, err);
3203 btrfs_end_log_trans(root);
3208 /* see comments for btrfs_del_dir_entries_in_log */
3209 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3210 struct btrfs_root *root,
3211 const char *name, int name_len,
3212 struct inode *inode, u64 dirid)
3214 struct btrfs_root *log;
3218 if (BTRFS_I(inode)->logged_trans < trans->transid)
3221 ret = join_running_log_trans(root);
3224 log = root->log_root;
3225 mutex_lock(&BTRFS_I(inode)->log_mutex);
3227 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
3229 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3230 if (ret == -ENOSPC) {
3231 btrfs_set_log_full_commit(root->fs_info, trans);
3233 } else if (ret < 0 && ret != -ENOENT)
3234 btrfs_abort_transaction(trans, ret);
3235 btrfs_end_log_trans(root);
3241 * creates a range item in the log for 'dirid'. first_offset and
3242 * last_offset tell us which parts of the key space the log should
3243 * be considered authoritative for.
3245 static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3246 struct btrfs_root *log,
3247 struct btrfs_path *path,
3248 int key_type, u64 dirid,
3249 u64 first_offset, u64 last_offset)
3252 struct btrfs_key key;
3253 struct btrfs_dir_log_item *item;
3255 key.objectid = dirid;
3256 key.offset = first_offset;
3257 if (key_type == BTRFS_DIR_ITEM_KEY)
3258 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3260 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3261 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
3265 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3266 struct btrfs_dir_log_item);
3267 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3268 btrfs_mark_buffer_dirty(path->nodes[0]);
3269 btrfs_release_path(path);
3274 * log all the items included in the current transaction for a given
3275 * directory. This also creates the range items in the log tree required
3276 * to replay anything deleted before the fsync
3278 static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3279 struct btrfs_root *root, struct inode *inode,
3280 struct btrfs_path *path,
3281 struct btrfs_path *dst_path, int key_type,
3282 struct btrfs_log_ctx *ctx,
3283 u64 min_offset, u64 *last_offset_ret)
3285 struct btrfs_key min_key;
3286 struct btrfs_root *log = root->log_root;
3287 struct extent_buffer *src;
3292 u64 first_offset = min_offset;
3293 u64 last_offset = (u64)-1;
3294 u64 ino = btrfs_ino(inode);
3296 log = root->log_root;
3298 min_key.objectid = ino;
3299 min_key.type = key_type;
3300 min_key.offset = min_offset;
3302 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
3305 * we didn't find anything from this transaction, see if there
3306 * is anything at all
3308 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3309 min_key.objectid = ino;
3310 min_key.type = key_type;
3311 min_key.offset = (u64)-1;
3312 btrfs_release_path(path);
3313 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3315 btrfs_release_path(path);
3318 ret = btrfs_previous_item(root, path, ino, key_type);
3320 /* if ret == 0 there are items for this type,
3321 * create a range to tell us the last key of this type.
3322 * otherwise, there are no items in this directory after
3323 * *min_offset, and we create a range to indicate that.
3326 struct btrfs_key tmp;
3327 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3329 if (key_type == tmp.type)
3330 first_offset = max(min_offset, tmp.offset) + 1;
3335 /* go backward to find any previous key */
3336 ret = btrfs_previous_item(root, path, ino, key_type);
3338 struct btrfs_key tmp;
3339 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3340 if (key_type == tmp.type) {
3341 first_offset = tmp.offset;
3342 ret = overwrite_item(trans, log, dst_path,
3343 path->nodes[0], path->slots[0],
3351 btrfs_release_path(path);
3354 * Find the first key from this transaction again. See the note for
3355 * log_new_dir_dentries, if we're logging a directory recursively we
3356 * won't be holding its i_mutex, which means we can modify the directory
3357 * while we're logging it. If we remove an entry between our first
3358 * search and this search we'll not find the key again and can just
3362 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3367 * we have a block from this transaction, log every item in it
3368 * from our directory
3371 struct btrfs_key tmp;
3372 src = path->nodes[0];
3373 nritems = btrfs_header_nritems(src);
3374 for (i = path->slots[0]; i < nritems; i++) {
3375 struct btrfs_dir_item *di;
3377 btrfs_item_key_to_cpu(src, &min_key, i);
3379 if (min_key.objectid != ino || min_key.type != key_type)
3382 if (need_resched()) {
3383 btrfs_release_path(path);
3388 ret = overwrite_item(trans, log, dst_path, src, i,
3396 * We must make sure that when we log a directory entry,
3397 * the corresponding inode, after log replay, has a
3398 * matching link count. For example:
3404 * xfs_io -c "fsync" mydir
3406 * <mount fs and log replay>
3408 * Would result in a fsync log that when replayed, our
3409 * file inode would have a link count of 1, but we get
3410 * two directory entries pointing to the same inode.
3411 * After removing one of the names, it would not be
3412 * possible to remove the other name, which resulted
3413 * always in stale file handle errors, and would not
3414 * be possible to rmdir the parent directory, since
3415 * its i_size could never decrement to the value
3416 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3418 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3419 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3421 (btrfs_dir_transid(src, di) == trans->transid ||
3422 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3423 tmp.type != BTRFS_ROOT_ITEM_KEY)
3424 ctx->log_new_dentries = true;
3426 path->slots[0] = nritems;
3429 * look ahead to the next item and see if it is also
3430 * from this directory and from this transaction
3432 ret = btrfs_next_leaf(root, path);
3435 last_offset = (u64)-1;
3440 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3441 if (tmp.objectid != ino || tmp.type != key_type) {
3442 last_offset = (u64)-1;
3445 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3446 ret = overwrite_item(trans, log, dst_path,
3447 path->nodes[0], path->slots[0],
3452 last_offset = tmp.offset;
3457 btrfs_release_path(path);
3458 btrfs_release_path(dst_path);
3461 *last_offset_ret = last_offset;
3463 * insert the log range keys to indicate where the log
3466 ret = insert_dir_log_key(trans, log, path, key_type,
3467 ino, first_offset, last_offset);
3475 * logging directories is very similar to logging inodes, We find all the items
3476 * from the current transaction and write them to the log.
3478 * The recovery code scans the directory in the subvolume, and if it finds a
3479 * key in the range logged that is not present in the log tree, then it means
3480 * that dir entry was unlinked during the transaction.
3482 * In order for that scan to work, we must include one key smaller than
3483 * the smallest logged by this transaction and one key larger than the largest
3484 * key logged by this transaction.
3486 static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3487 struct btrfs_root *root, struct inode *inode,
3488 struct btrfs_path *path,
3489 struct btrfs_path *dst_path,
3490 struct btrfs_log_ctx *ctx)
3495 int key_type = BTRFS_DIR_ITEM_KEY;
3501 ret = log_dir_items(trans, root, inode, path,
3502 dst_path, key_type, ctx, min_key,
3506 if (max_key == (u64)-1)
3508 min_key = max_key + 1;
3511 if (key_type == BTRFS_DIR_ITEM_KEY) {
3512 key_type = BTRFS_DIR_INDEX_KEY;
3519 * a helper function to drop items from the log before we relog an
3520 * inode. max_key_type indicates the highest item type to remove.
3521 * This cannot be run for file data extents because it does not
3522 * free the extents they point to.
3524 static int drop_objectid_items(struct btrfs_trans_handle *trans,
3525 struct btrfs_root *log,
3526 struct btrfs_path *path,
3527 u64 objectid, int max_key_type)
3530 struct btrfs_key key;
3531 struct btrfs_key found_key;
3534 key.objectid = objectid;
3535 key.type = max_key_type;
3536 key.offset = (u64)-1;
3539 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
3540 BUG_ON(ret == 0); /* Logic error */
3544 if (path->slots[0] == 0)
3548 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3551 if (found_key.objectid != objectid)
3554 found_key.offset = 0;
3556 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3559 ret = btrfs_del_items(trans, log, path, start_slot,
3560 path->slots[0] - start_slot + 1);
3562 * If start slot isn't 0 then we don't need to re-search, we've
3563 * found the last guy with the objectid in this tree.
3565 if (ret || start_slot != 0)
3567 btrfs_release_path(path);
3569 btrfs_release_path(path);
3575 static void fill_inode_item(struct btrfs_trans_handle *trans,
3576 struct extent_buffer *leaf,
3577 struct btrfs_inode_item *item,
3578 struct inode *inode, int log_inode_only,
3581 struct btrfs_map_token token;
3583 btrfs_init_map_token(&token);
3585 if (log_inode_only) {
3586 /* set the generation to zero so the recover code
3587 * can tell the difference between an logging
3588 * just to say 'this inode exists' and a logging
3589 * to say 'update this inode with these values'
3591 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3592 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
3594 btrfs_set_token_inode_generation(leaf, item,
3595 BTRFS_I(inode)->generation,
3597 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
3600 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3601 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3602 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3603 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3605 btrfs_set_token_timespec_sec(leaf, &item->atime,
3606 inode->i_atime.tv_sec, &token);
3607 btrfs_set_token_timespec_nsec(leaf, &item->atime,
3608 inode->i_atime.tv_nsec, &token);
3610 btrfs_set_token_timespec_sec(leaf, &item->mtime,
3611 inode->i_mtime.tv_sec, &token);
3612 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
3613 inode->i_mtime.tv_nsec, &token);
3615 btrfs_set_token_timespec_sec(leaf, &item->ctime,
3616 inode->i_ctime.tv_sec, &token);
3617 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
3618 inode->i_ctime.tv_nsec, &token);
3620 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3623 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3624 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3625 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3626 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3627 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
3630 static int log_inode_item(struct btrfs_trans_handle *trans,
3631 struct btrfs_root *log, struct btrfs_path *path,
3632 struct inode *inode)
3634 struct btrfs_inode_item *inode_item;
3637 ret = btrfs_insert_empty_item(trans, log, path,
3638 &BTRFS_I(inode)->location,
3639 sizeof(*inode_item));
3640 if (ret && ret != -EEXIST)
3642 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3643 struct btrfs_inode_item);
3644 fill_inode_item(trans, path->nodes[0], inode_item, inode, 0, 0);
3645 btrfs_release_path(path);
3649 static noinline int copy_items(struct btrfs_trans_handle *trans,
3650 struct inode *inode,
3651 struct btrfs_path *dst_path,
3652 struct btrfs_path *src_path, u64 *last_extent,
3653 int start_slot, int nr, int inode_only,
3656 unsigned long src_offset;
3657 unsigned long dst_offset;
3658 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
3659 struct btrfs_file_extent_item *extent;
3660 struct btrfs_inode_item *inode_item;
3661 struct extent_buffer *src = src_path->nodes[0];
3662 struct btrfs_key first_key, last_key, key;
3664 struct btrfs_key *ins_keys;
3668 struct list_head ordered_sums;
3669 int skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
3670 bool has_extents = false;
3671 bool need_find_last_extent = true;
3674 INIT_LIST_HEAD(&ordered_sums);
3676 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3677 nr * sizeof(u32), GFP_NOFS);
3681 first_key.objectid = (u64)-1;
3683 ins_sizes = (u32 *)ins_data;
3684 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3686 for (i = 0; i < nr; i++) {
3687 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3688 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3690 ret = btrfs_insert_empty_items(trans, log, dst_path,
3691 ins_keys, ins_sizes, nr);
3697 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
3698 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3699 dst_path->slots[0]);
3701 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3704 last_key = ins_keys[i];
3706 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
3707 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3709 struct btrfs_inode_item);
3710 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3711 inode, inode_only == LOG_INODE_EXISTS,
3714 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3715 src_offset, ins_sizes[i]);
3719 * We set need_find_last_extent here in case we know we were
3720 * processing other items and then walk into the first extent in
3721 * the inode. If we don't hit an extent then nothing changes,
3722 * we'll do the last search the next time around.
3724 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3726 if (first_key.objectid == (u64)-1)
3727 first_key = ins_keys[i];
3729 need_find_last_extent = false;
3732 /* take a reference on file data extents so that truncates
3733 * or deletes of this inode don't have to relog the inode
3736 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
3739 extent = btrfs_item_ptr(src, start_slot + i,
3740 struct btrfs_file_extent_item);
3742 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3745 found_type = btrfs_file_extent_type(src, extent);
3746 if (found_type == BTRFS_FILE_EXTENT_REG) {
3748 ds = btrfs_file_extent_disk_bytenr(src,
3750 /* ds == 0 is a hole */
3754 dl = btrfs_file_extent_disk_num_bytes(src,
3756 cs = btrfs_file_extent_offset(src, extent);
3757 cl = btrfs_file_extent_num_bytes(src,
3759 if (btrfs_file_extent_compression(src,
3765 ret = btrfs_lookup_csums_range(
3766 log->fs_info->csum_root,
3767 ds + cs, ds + cs + cl - 1,
3775 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
3776 btrfs_release_path(dst_path);
3780 * we have to do this after the loop above to avoid changing the
3781 * log tree while trying to change the log tree.
3783 while (!list_empty(&ordered_sums)) {
3784 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3785 struct btrfs_ordered_sum,
3788 ret = btrfs_csum_file_blocks(trans, log, sums);
3789 list_del(&sums->list);
3796 if (need_find_last_extent && *last_extent == first_key.offset) {
3798 * We don't have any leafs between our current one and the one
3799 * we processed before that can have file extent items for our
3800 * inode (and have a generation number smaller than our current
3803 need_find_last_extent = false;
3807 * Because we use btrfs_search_forward we could skip leaves that were
3808 * not modified and then assume *last_extent is valid when it really
3809 * isn't. So back up to the previous leaf and read the end of the last
3810 * extent before we go and fill in holes.
3812 if (need_find_last_extent) {
3815 ret = btrfs_prev_leaf(BTRFS_I(inode)->root, src_path);
3820 if (src_path->slots[0])
3821 src_path->slots[0]--;
3822 src = src_path->nodes[0];
3823 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3824 if (key.objectid != btrfs_ino(inode) ||
3825 key.type != BTRFS_EXTENT_DATA_KEY)
3827 extent = btrfs_item_ptr(src, src_path->slots[0],
3828 struct btrfs_file_extent_item);
3829 if (btrfs_file_extent_type(src, extent) ==
3830 BTRFS_FILE_EXTENT_INLINE) {
3831 len = btrfs_file_extent_inline_len(src,
3834 *last_extent = ALIGN(key.offset + len,
3837 len = btrfs_file_extent_num_bytes(src, extent);
3838 *last_extent = key.offset + len;
3842 /* So we did prev_leaf, now we need to move to the next leaf, but a few
3843 * things could have happened
3845 * 1) A merge could have happened, so we could currently be on a leaf
3846 * that holds what we were copying in the first place.
3847 * 2) A split could have happened, and now not all of the items we want
3848 * are on the same leaf.
3850 * So we need to adjust how we search for holes, we need to drop the
3851 * path and re-search for the first extent key we found, and then walk
3852 * forward until we hit the last one we copied.
3854 if (need_find_last_extent) {
3855 /* btrfs_prev_leaf could return 1 without releasing the path */
3856 btrfs_release_path(src_path);
3857 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &first_key,
3862 src = src_path->nodes[0];
3863 i = src_path->slots[0];
3869 * Ok so here we need to go through and fill in any holes we may have
3870 * to make sure that holes are punched for those areas in case they had
3871 * extents previously.
3877 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
3878 ret = btrfs_next_leaf(BTRFS_I(inode)->root, src_path);
3882 src = src_path->nodes[0];
3884 need_find_last_extent = true;
3887 btrfs_item_key_to_cpu(src, &key, i);
3888 if (!btrfs_comp_cpu_keys(&key, &last_key))
3890 if (key.objectid != btrfs_ino(inode) ||
3891 key.type != BTRFS_EXTENT_DATA_KEY) {
3895 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
3896 if (btrfs_file_extent_type(src, extent) ==
3897 BTRFS_FILE_EXTENT_INLINE) {
3898 len = btrfs_file_extent_inline_len(src, i, extent);
3899 extent_end = ALIGN(key.offset + len, log->sectorsize);
3901 len = btrfs_file_extent_num_bytes(src, extent);
3902 extent_end = key.offset + len;
3906 if (*last_extent == key.offset) {
3907 *last_extent = extent_end;
3910 offset = *last_extent;
3911 len = key.offset - *last_extent;
3912 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
3913 offset, 0, 0, len, 0, len, 0,
3917 *last_extent = extent_end;
3920 * Need to let the callers know we dropped the path so they should
3923 if (!ret && need_find_last_extent)
3928 static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
3930 struct extent_map *em1, *em2;
3932 em1 = list_entry(a, struct extent_map, list);
3933 em2 = list_entry(b, struct extent_map, list);
3935 if (em1->start < em2->start)
3937 else if (em1->start > em2->start)
3942 static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3943 struct inode *inode,
3944 struct btrfs_root *root,
3945 const struct extent_map *em,
3946 const struct list_head *logged_list,
3947 bool *ordered_io_error)
3949 struct btrfs_ordered_extent *ordered;
3950 struct btrfs_root *log = root->log_root;
3951 u64 mod_start = em->mod_start;
3952 u64 mod_len = em->mod_len;
3953 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
3956 LIST_HEAD(ordered_sums);
3959 *ordered_io_error = false;
3961 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
3962 em->block_start == EXTENT_MAP_HOLE)
3966 * Wait far any ordered extent that covers our extent map. If it
3967 * finishes without an error, first check and see if our csums are on
3968 * our outstanding ordered extents.
3970 list_for_each_entry(ordered, logged_list, log_list) {
3971 struct btrfs_ordered_sum *sum;
3976 if (ordered->file_offset + ordered->len <= mod_start ||
3977 mod_start + mod_len <= ordered->file_offset)
3980 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
3981 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
3982 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
3983 const u64 start = ordered->file_offset;
3984 const u64 end = ordered->file_offset + ordered->len - 1;
3986 WARN_ON(ordered->inode != inode);
3987 filemap_fdatawrite_range(inode->i_mapping, start, end);
3990 wait_event(ordered->wait,
3991 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
3992 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
3994 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
3996 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
3997 * i_mapping flags, so that the next fsync won't get
3998 * an outdated io error too.
4000 filemap_check_errors(inode->i_mapping);
4001 *ordered_io_error = true;
4005 * We are going to copy all the csums on this ordered extent, so
4006 * go ahead and adjust mod_start and mod_len in case this
4007 * ordered extent has already been logged.
4009 if (ordered->file_offset > mod_start) {
4010 if (ordered->file_offset + ordered->len >=
4011 mod_start + mod_len)
4012 mod_len = ordered->file_offset - mod_start;
4014 * If we have this case
4016 * |--------- logged extent ---------|
4017 * |----- ordered extent ----|
4019 * Just don't mess with mod_start and mod_len, we'll
4020 * just end up logging more csums than we need and it
4024 if (ordered->file_offset + ordered->len <
4025 mod_start + mod_len) {
4026 mod_len = (mod_start + mod_len) -
4027 (ordered->file_offset + ordered->len);
4028 mod_start = ordered->file_offset +
4039 * To keep us from looping for the above case of an ordered
4040 * extent that falls inside of the logged extent.
4042 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4046 list_for_each_entry(sum, &ordered->list, list) {
4047 ret = btrfs_csum_file_blocks(trans, log, sum);
4053 if (*ordered_io_error || !mod_len || ret || skip_csum)
4056 if (em->compress_type) {
4058 csum_len = max(em->block_len, em->orig_block_len);
4060 csum_offset = mod_start - em->start;
4064 /* block start is already adjusted for the file extent offset. */
4065 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
4066 em->block_start + csum_offset,
4067 em->block_start + csum_offset +
4068 csum_len - 1, &ordered_sums, 0);
4072 while (!list_empty(&ordered_sums)) {
4073 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4074 struct btrfs_ordered_sum,
4077 ret = btrfs_csum_file_blocks(trans, log, sums);
4078 list_del(&sums->list);
4085 static int log_one_extent(struct btrfs_trans_handle *trans,
4086 struct inode *inode, struct btrfs_root *root,
4087 const struct extent_map *em,
4088 struct btrfs_path *path,
4089 const struct list_head *logged_list,
4090 struct btrfs_log_ctx *ctx)
4092 struct btrfs_root *log = root->log_root;
4093 struct btrfs_file_extent_item *fi;
4094 struct extent_buffer *leaf;
4095 struct btrfs_map_token token;
4096 struct btrfs_key key;
4097 u64 extent_offset = em->start - em->orig_start;
4100 int extent_inserted = 0;
4101 bool ordered_io_err = false;
4103 ret = wait_ordered_extents(trans, inode, root, em, logged_list,
4108 if (ordered_io_err) {
4113 btrfs_init_map_token(&token);
4115 ret = __btrfs_drop_extents(trans, log, inode, path, em->start,
4116 em->start + em->len, NULL, 0, 1,
4117 sizeof(*fi), &extent_inserted);
4121 if (!extent_inserted) {
4122 key.objectid = btrfs_ino(inode);
4123 key.type = BTRFS_EXTENT_DATA_KEY;
4124 key.offset = em->start;
4126 ret = btrfs_insert_empty_item(trans, log, path, &key,
4131 leaf = path->nodes[0];
4132 fi = btrfs_item_ptr(leaf, path->slots[0],
4133 struct btrfs_file_extent_item);
4135 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
4137 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4138 btrfs_set_token_file_extent_type(leaf, fi,
4139 BTRFS_FILE_EXTENT_PREALLOC,
4142 btrfs_set_token_file_extent_type(leaf, fi,
4143 BTRFS_FILE_EXTENT_REG,
4146 block_len = max(em->block_len, em->orig_block_len);
4147 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4148 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4151 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4153 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4154 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4156 extent_offset, &token);
4157 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4160 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4161 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4165 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4166 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4167 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4168 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4170 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4171 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4172 btrfs_mark_buffer_dirty(leaf);
4174 btrfs_release_path(path);
4179 static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4180 struct btrfs_root *root,
4181 struct inode *inode,
4182 struct btrfs_path *path,
4183 struct list_head *logged_list,
4184 struct btrfs_log_ctx *ctx,
4188 struct extent_map *em, *n;
4189 struct list_head extents;
4190 struct extent_map_tree *tree = &BTRFS_I(inode)->extent_tree;
4195 INIT_LIST_HEAD(&extents);
4197 down_write(&BTRFS_I(inode)->dio_sem);
4198 write_lock(&tree->lock);
4199 test_gen = root->fs_info->last_trans_committed;
4201 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4202 list_del_init(&em->list);
4205 * Just an arbitrary number, this can be really CPU intensive
4206 * once we start getting a lot of extents, and really once we
4207 * have a bunch of extents we just want to commit since it will
4210 if (++num > 32768) {
4211 list_del_init(&tree->modified_extents);
4216 if (em->generation <= test_gen)
4218 /* Need a ref to keep it from getting evicted from cache */
4219 atomic_inc(&em->refs);
4220 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
4221 list_add_tail(&em->list, &extents);
4225 list_sort(NULL, &extents, extent_cmp);
4226 btrfs_get_logged_extents(inode, logged_list, start, end);
4228 * Some ordered extents started by fsync might have completed
4229 * before we could collect them into the list logged_list, which
4230 * means they're gone, not in our logged_list nor in the inode's
4231 * ordered tree. We want the application/user space to know an
4232 * error happened while attempting to persist file data so that
4233 * it can take proper action. If such error happened, we leave
4234 * without writing to the log tree and the fsync must report the
4235 * file data write error and not commit the current transaction.
4237 ret = filemap_check_errors(inode->i_mapping);
4241 while (!list_empty(&extents)) {
4242 em = list_entry(extents.next, struct extent_map, list);
4244 list_del_init(&em->list);
4247 * If we had an error we just need to delete everybody from our
4251 clear_em_logging(tree, em);
4252 free_extent_map(em);
4256 write_unlock(&tree->lock);
4258 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4260 write_lock(&tree->lock);
4261 clear_em_logging(tree, em);
4262 free_extent_map(em);
4264 WARN_ON(!list_empty(&extents));
4265 write_unlock(&tree->lock);
4266 up_write(&BTRFS_I(inode)->dio_sem);
4268 btrfs_release_path(path);
4272 static int logged_inode_size(struct btrfs_root *log, struct inode *inode,
4273 struct btrfs_path *path, u64 *size_ret)
4275 struct btrfs_key key;
4278 key.objectid = btrfs_ino(inode);
4279 key.type = BTRFS_INODE_ITEM_KEY;
4282 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4285 } else if (ret > 0) {
4288 struct btrfs_inode_item *item;
4290 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4291 struct btrfs_inode_item);
4292 *size_ret = btrfs_inode_size(path->nodes[0], item);
4295 btrfs_release_path(path);
4300 * At the moment we always log all xattrs. This is to figure out at log replay
4301 * time which xattrs must have their deletion replayed. If a xattr is missing
4302 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4303 * because if a xattr is deleted, the inode is fsynced and a power failure
4304 * happens, causing the log to be replayed the next time the fs is mounted,
4305 * we want the xattr to not exist anymore (same behaviour as other filesystems
4306 * with a journal, ext3/4, xfs, f2fs, etc).
4308 static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4309 struct btrfs_root *root,
4310 struct inode *inode,
4311 struct btrfs_path *path,
4312 struct btrfs_path *dst_path)
4315 struct btrfs_key key;
4316 const u64 ino = btrfs_ino(inode);
4321 key.type = BTRFS_XATTR_ITEM_KEY;
4324 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4329 int slot = path->slots[0];
4330 struct extent_buffer *leaf = path->nodes[0];
4331 int nritems = btrfs_header_nritems(leaf);
4333 if (slot >= nritems) {
4335 u64 last_extent = 0;
4337 ret = copy_items(trans, inode, dst_path, path,
4338 &last_extent, start_slot,
4340 /* can't be 1, extent items aren't processed */
4346 ret = btrfs_next_leaf(root, path);
4354 btrfs_item_key_to_cpu(leaf, &key, slot);
4355 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4365 u64 last_extent = 0;
4367 ret = copy_items(trans, inode, dst_path, path,
4368 &last_extent, start_slot,
4370 /* can't be 1, extent items aren't processed */
4380 * If the no holes feature is enabled we need to make sure any hole between the
4381 * last extent and the i_size of our inode is explicitly marked in the log. This
4382 * is to make sure that doing something like:
4384 * 1) create file with 128Kb of data
4385 * 2) truncate file to 64Kb
4386 * 3) truncate file to 256Kb
4388 * 5) <crash/power failure>
4389 * 6) mount fs and trigger log replay
4391 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4392 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4393 * file correspond to a hole. The presence of explicit holes in a log tree is
4394 * what guarantees that log replay will remove/adjust file extent items in the
4397 * Here we do not need to care about holes between extents, that is already done
4398 * by copy_items(). We also only need to do this in the full sync path, where we
4399 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4400 * lookup the list of modified extent maps and if any represents a hole, we
4401 * insert a corresponding extent representing a hole in the log tree.
4403 static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4404 struct btrfs_root *root,
4405 struct inode *inode,
4406 struct btrfs_path *path)
4409 struct btrfs_key key;
4412 struct extent_buffer *leaf;
4413 struct btrfs_root *log = root->log_root;
4414 const u64 ino = btrfs_ino(inode);
4415 const u64 i_size = i_size_read(inode);
4417 if (!btrfs_fs_incompat(root->fs_info, NO_HOLES))
4421 key.type = BTRFS_EXTENT_DATA_KEY;
4422 key.offset = (u64)-1;
4424 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4429 ASSERT(path->slots[0] > 0);
4431 leaf = path->nodes[0];
4432 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4434 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4435 /* inode does not have any extents */
4439 struct btrfs_file_extent_item *extent;
4443 * If there's an extent beyond i_size, an explicit hole was
4444 * already inserted by copy_items().
4446 if (key.offset >= i_size)
4449 extent = btrfs_item_ptr(leaf, path->slots[0],
4450 struct btrfs_file_extent_item);
4452 if (btrfs_file_extent_type(leaf, extent) ==
4453 BTRFS_FILE_EXTENT_INLINE)
4456 len = btrfs_file_extent_num_bytes(leaf, extent);
4457 /* Last extent goes beyond i_size, no need to log a hole. */
4458 if (key.offset + len > i_size)
4460 hole_start = key.offset + len;
4461 hole_size = i_size - hole_start;
4463 btrfs_release_path(path);
4465 /* Last extent ends at i_size. */
4469 hole_size = ALIGN(hole_size, root->sectorsize);
4470 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4471 hole_size, 0, hole_size, 0, 0, 0);
4476 * When we are logging a new inode X, check if it doesn't have a reference that
4477 * matches the reference from some other inode Y created in a past transaction
4478 * and that was renamed in the current transaction. If we don't do this, then at
4479 * log replay time we can lose inode Y (and all its files if it's a directory):
4482 * echo "hello world" > /mnt/x/foobar
4485 * mkdir /mnt/x # or touch /mnt/x
4486 * xfs_io -c fsync /mnt/x
4488 * mount fs, trigger log replay
4490 * After the log replay procedure, we would lose the first directory and all its
4491 * files (file foobar).
4492 * For the case where inode Y is not a directory we simply end up losing it:
4494 * echo "123" > /mnt/foo
4496 * mv /mnt/foo /mnt/bar
4497 * echo "abc" > /mnt/foo
4498 * xfs_io -c fsync /mnt/foo
4501 * We also need this for cases where a snapshot entry is replaced by some other
4502 * entry (file or directory) otherwise we end up with an unreplayable log due to
4503 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4504 * if it were a regular entry:
4507 * btrfs subvolume snapshot /mnt /mnt/x/snap
4508 * btrfs subvolume delete /mnt/x/snap
4511 * fsync /mnt/x or fsync some new file inside it
4514 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4515 * the same transaction.
4517 static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4519 const struct btrfs_key *key,
4520 struct inode *inode,
4524 struct btrfs_path *search_path;
4527 u32 item_size = btrfs_item_size_nr(eb, slot);
4529 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4531 search_path = btrfs_alloc_path();
4534 search_path->search_commit_root = 1;
4535 search_path->skip_locking = 1;
4537 while (cur_offset < item_size) {
4541 unsigned long name_ptr;
4542 struct btrfs_dir_item *di;
4544 if (key->type == BTRFS_INODE_REF_KEY) {
4545 struct btrfs_inode_ref *iref;
4547 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4548 parent = key->offset;
4549 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4550 name_ptr = (unsigned long)(iref + 1);
4551 this_len = sizeof(*iref) + this_name_len;
4553 struct btrfs_inode_extref *extref;
4555 extref = (struct btrfs_inode_extref *)(ptr +
4557 parent = btrfs_inode_extref_parent(eb, extref);
4558 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4559 name_ptr = (unsigned long)&extref->name;
4560 this_len = sizeof(*extref) + this_name_len;
4563 if (this_name_len > name_len) {
4566 new_name = krealloc(name, this_name_len, GFP_NOFS);
4571 name_len = this_name_len;
4575 read_extent_buffer(eb, name, name_ptr, this_name_len);
4576 di = btrfs_lookup_dir_item(NULL, BTRFS_I(inode)->root,
4577 search_path, parent,
4578 name, this_name_len, 0);
4579 if (di && !IS_ERR(di)) {
4580 struct btrfs_key di_key;
4582 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4584 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4586 *other_ino = di_key.objectid;
4591 } else if (IS_ERR(di)) {
4595 btrfs_release_path(search_path);
4597 cur_offset += this_len;
4601 btrfs_free_path(search_path);
4606 /* log a single inode in the tree log.
4607 * At least one parent directory for this inode must exist in the tree
4608 * or be logged already.
4610 * Any items from this inode changed by the current transaction are copied
4611 * to the log tree. An extra reference is taken on any extents in this
4612 * file, allowing us to avoid a whole pile of corner cases around logging
4613 * blocks that have been removed from the tree.
4615 * See LOG_INODE_ALL and related defines for a description of what inode_only
4618 * This handles both files and directories.
4620 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
4621 struct btrfs_root *root, struct inode *inode,
4625 struct btrfs_log_ctx *ctx)
4627 struct btrfs_path *path;
4628 struct btrfs_path *dst_path;
4629 struct btrfs_key min_key;
4630 struct btrfs_key max_key;
4631 struct btrfs_root *log = root->log_root;
4632 struct extent_buffer *src = NULL;
4633 LIST_HEAD(logged_list);
4634 u64 last_extent = 0;
4638 int ins_start_slot = 0;
4640 bool fast_search = false;
4641 u64 ino = btrfs_ino(inode);
4642 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
4643 u64 logged_isize = 0;
4644 bool need_log_inode_item = true;
4645 bool xattrs_logged = false;
4647 path = btrfs_alloc_path();
4650 dst_path = btrfs_alloc_path();
4652 btrfs_free_path(path);
4656 min_key.objectid = ino;
4657 min_key.type = BTRFS_INODE_ITEM_KEY;
4660 max_key.objectid = ino;
4663 /* today the code can only do partial logging of directories */
4664 if (S_ISDIR(inode->i_mode) ||
4665 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4666 &BTRFS_I(inode)->runtime_flags) &&
4667 inode_only >= LOG_INODE_EXISTS))
4668 max_key.type = BTRFS_XATTR_ITEM_KEY;
4670 max_key.type = (u8)-1;
4671 max_key.offset = (u64)-1;
4674 * Only run delayed items if we are a dir or a new file.
4675 * Otherwise commit the delayed inode only, which is needed in
4676 * order for the log replay code to mark inodes for link count
4677 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4679 if (S_ISDIR(inode->i_mode) ||
4680 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed)
4681 ret = btrfs_commit_inode_delayed_items(trans, inode);
4683 ret = btrfs_commit_inode_delayed_inode(inode);
4686 btrfs_free_path(path);
4687 btrfs_free_path(dst_path);
4691 if (inode_only == LOG_OTHER_INODE) {
4692 inode_only = LOG_INODE_EXISTS;
4693 mutex_lock_nested(&BTRFS_I(inode)->log_mutex,
4694 SINGLE_DEPTH_NESTING);
4696 mutex_lock(&BTRFS_I(inode)->log_mutex);
4700 * For symlinks, we must always log their content, which is stored in an
4701 * inline extent, otherwise we could end up with an empty symlink after
4702 * log replay, which is invalid on linux (symlink(2) returns -ENOENT if
4703 * one attempts to create an empty symlink).
4704 * We don't need to worry about flushing delalloc, because when we create
4705 * the inline extent when the symlink is created (we never have delalloc
4708 if (S_ISLNK(inode->i_mode))
4709 inode_only = LOG_INODE_ALL;
4712 * a brute force approach to making sure we get the most uptodate
4713 * copies of everything.
4715 if (S_ISDIR(inode->i_mode)) {
4716 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4718 if (inode_only == LOG_INODE_EXISTS)
4719 max_key_type = BTRFS_XATTR_ITEM_KEY;
4720 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
4722 if (inode_only == LOG_INODE_EXISTS) {
4724 * Make sure the new inode item we write to the log has
4725 * the same isize as the current one (if it exists).
4726 * This is necessary to prevent data loss after log
4727 * replay, and also to prevent doing a wrong expanding
4728 * truncate - for e.g. create file, write 4K into offset
4729 * 0, fsync, write 4K into offset 4096, add hard link,
4730 * fsync some other file (to sync log), power fail - if
4731 * we use the inode's current i_size, after log replay
4732 * we get a 8Kb file, with the last 4Kb extent as a hole
4733 * (zeroes), as if an expanding truncate happened,
4734 * instead of getting a file of 4Kb only.
4736 err = logged_inode_size(log, inode, path,
4741 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4742 &BTRFS_I(inode)->runtime_flags)) {
4743 if (inode_only == LOG_INODE_EXISTS) {
4744 max_key.type = BTRFS_XATTR_ITEM_KEY;
4745 ret = drop_objectid_items(trans, log, path, ino,
4748 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4749 &BTRFS_I(inode)->runtime_flags);
4750 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4751 &BTRFS_I(inode)->runtime_flags);
4753 ret = btrfs_truncate_inode_items(trans,
4759 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4760 &BTRFS_I(inode)->runtime_flags) ||
4761 inode_only == LOG_INODE_EXISTS) {
4762 if (inode_only == LOG_INODE_ALL)
4764 max_key.type = BTRFS_XATTR_ITEM_KEY;
4765 ret = drop_objectid_items(trans, log, path, ino,
4768 if (inode_only == LOG_INODE_ALL)
4781 ret = btrfs_search_forward(root, &min_key,
4782 path, trans->transid);
4790 /* note, ins_nr might be > 0 here, cleanup outside the loop */
4791 if (min_key.objectid != ino)
4793 if (min_key.type > max_key.type)
4796 if (min_key.type == BTRFS_INODE_ITEM_KEY)
4797 need_log_inode_item = false;
4799 if ((min_key.type == BTRFS_INODE_REF_KEY ||
4800 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
4801 BTRFS_I(inode)->generation == trans->transid) {
4804 ret = btrfs_check_ref_name_override(path->nodes[0],
4811 } else if (ret > 0 && ctx &&
4812 other_ino != btrfs_ino(ctx->inode)) {
4813 struct btrfs_key inode_key;
4814 struct inode *other_inode;
4820 ins_start_slot = path->slots[0];
4822 ret = copy_items(trans, inode, dst_path, path,
4823 &last_extent, ins_start_slot,
4831 btrfs_release_path(path);
4832 inode_key.objectid = other_ino;
4833 inode_key.type = BTRFS_INODE_ITEM_KEY;
4834 inode_key.offset = 0;
4835 other_inode = btrfs_iget(root->fs_info->sb,
4839 * If the other inode that had a conflicting dir
4840 * entry was deleted in the current transaction,
4841 * we don't need to do more work nor fallback to
4842 * a transaction commit.
4844 if (IS_ERR(other_inode) &&
4845 PTR_ERR(other_inode) == -ENOENT) {
4847 } else if (IS_ERR(other_inode)) {
4848 err = PTR_ERR(other_inode);
4852 * We are safe logging the other inode without
4853 * acquiring its i_mutex as long as we log with
4854 * the LOG_INODE_EXISTS mode. We're safe against
4855 * concurrent renames of the other inode as well
4856 * because during a rename we pin the log and
4857 * update the log with the new name before we
4860 err = btrfs_log_inode(trans, root, other_inode,
4863 btrfs_add_delayed_iput(other_inode);
4871 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
4872 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
4875 ret = copy_items(trans, inode, dst_path, path,
4876 &last_extent, ins_start_slot,
4877 ins_nr, inode_only, logged_isize);
4884 btrfs_release_path(path);
4890 src = path->nodes[0];
4891 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
4894 } else if (!ins_nr) {
4895 ins_start_slot = path->slots[0];
4900 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4901 ins_start_slot, ins_nr, inode_only,
4909 btrfs_release_path(path);
4913 ins_start_slot = path->slots[0];
4916 nritems = btrfs_header_nritems(path->nodes[0]);
4918 if (path->slots[0] < nritems) {
4919 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
4924 ret = copy_items(trans, inode, dst_path, path,
4925 &last_extent, ins_start_slot,
4926 ins_nr, inode_only, logged_isize);
4934 btrfs_release_path(path);
4936 if (min_key.offset < (u64)-1) {
4938 } else if (min_key.type < max_key.type) {
4946 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4947 ins_start_slot, ins_nr, inode_only,
4957 btrfs_release_path(path);
4958 btrfs_release_path(dst_path);
4959 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
4962 xattrs_logged = true;
4963 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
4964 btrfs_release_path(path);
4965 btrfs_release_path(dst_path);
4966 err = btrfs_log_trailing_hole(trans, root, inode, path);
4971 btrfs_release_path(path);
4972 btrfs_release_path(dst_path);
4973 if (need_log_inode_item) {
4974 err = log_inode_item(trans, log, dst_path, inode);
4975 if (!err && !xattrs_logged) {
4976 err = btrfs_log_all_xattrs(trans, root, inode, path,
4978 btrfs_release_path(path);
4984 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
4985 &logged_list, ctx, start, end);
4990 } else if (inode_only == LOG_INODE_ALL) {
4991 struct extent_map *em, *n;
4993 write_lock(&em_tree->lock);
4995 * We can't just remove every em if we're called for a ranged
4996 * fsync - that is, one that doesn't cover the whole possible
4997 * file range (0 to LLONG_MAX). This is because we can have
4998 * em's that fall outside the range we're logging and therefore
4999 * their ordered operations haven't completed yet
5000 * (btrfs_finish_ordered_io() not invoked yet). This means we
5001 * didn't get their respective file extent item in the fs/subvol
5002 * tree yet, and need to let the next fast fsync (one which
5003 * consults the list of modified extent maps) find the em so
5004 * that it logs a matching file extent item and waits for the
5005 * respective ordered operation to complete (if it's still
5008 * Removing every em outside the range we're logging would make
5009 * the next fast fsync not log their matching file extent items,
5010 * therefore making us lose data after a log replay.
5012 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
5014 const u64 mod_end = em->mod_start + em->mod_len - 1;
5016 if (em->mod_start >= start && mod_end <= end)
5017 list_del_init(&em->list);
5019 write_unlock(&em_tree->lock);
5022 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
5023 ret = log_directory_changes(trans, root, inode, path, dst_path,
5031 spin_lock(&BTRFS_I(inode)->lock);
5032 BTRFS_I(inode)->logged_trans = trans->transid;
5033 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->last_sub_trans;
5034 spin_unlock(&BTRFS_I(inode)->lock);
5037 btrfs_put_logged_extents(&logged_list);
5039 btrfs_submit_logged_extents(&logged_list, log);
5040 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5042 btrfs_free_path(path);
5043 btrfs_free_path(dst_path);
5048 * Check if we must fallback to a transaction commit when logging an inode.
5049 * This must be called after logging the inode and is used only in the context
5050 * when fsyncing an inode requires the need to log some other inode - in which
5051 * case we can't lock the i_mutex of each other inode we need to log as that
5052 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5053 * log inodes up or down in the hierarchy) or rename operations for example. So
5054 * we take the log_mutex of the inode after we have logged it and then check for
5055 * its last_unlink_trans value - this is safe because any task setting
5056 * last_unlink_trans must take the log_mutex and it must do this before it does
5057 * the actual unlink operation, so if we do this check before a concurrent task
5058 * sets last_unlink_trans it means we've logged a consistent version/state of
5059 * all the inode items, otherwise we are not sure and must do a transaction
5060 * commit (the concurrent task might have only updated last_unlink_trans before
5061 * we logged the inode or it might have also done the unlink).
5063 static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5064 struct inode *inode)
5066 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
5069 mutex_lock(&BTRFS_I(inode)->log_mutex);
5070 if (BTRFS_I(inode)->last_unlink_trans > fs_info->last_trans_committed) {
5072 * Make sure any commits to the log are forced to be full
5075 btrfs_set_log_full_commit(fs_info, trans);
5078 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5084 * follow the dentry parent pointers up the chain and see if any
5085 * of the directories in it require a full commit before they can
5086 * be logged. Returns zero if nothing special needs to be done or 1 if
5087 * a full commit is required.
5089 static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5090 struct inode *inode,
5091 struct dentry *parent,
5092 struct super_block *sb,
5096 struct dentry *old_parent = NULL;
5097 struct inode *orig_inode = inode;
5100 * for regular files, if its inode is already on disk, we don't
5101 * have to worry about the parents at all. This is because
5102 * we can use the last_unlink_trans field to record renames
5103 * and other fun in this file.
5105 if (S_ISREG(inode->i_mode) &&
5106 BTRFS_I(inode)->generation <= last_committed &&
5107 BTRFS_I(inode)->last_unlink_trans <= last_committed)
5110 if (!S_ISDIR(inode->i_mode)) {
5111 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5113 inode = d_inode(parent);
5118 * If we are logging a directory then we start with our inode,
5119 * not our parent's inode, so we need to skip setting the
5120 * logged_trans so that further down in the log code we don't
5121 * think this inode has already been logged.
5123 if (inode != orig_inode)
5124 BTRFS_I(inode)->logged_trans = trans->transid;
5127 if (btrfs_must_commit_transaction(trans, inode)) {
5132 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5135 if (IS_ROOT(parent)) {
5136 inode = d_inode(parent);
5137 if (btrfs_must_commit_transaction(trans, inode))
5142 parent = dget_parent(parent);
5144 old_parent = parent;
5145 inode = d_inode(parent);
5153 struct btrfs_dir_list {
5155 struct list_head list;
5159 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5160 * details about the why it is needed.
5161 * This is a recursive operation - if an existing dentry corresponds to a
5162 * directory, that directory's new entries are logged too (same behaviour as
5163 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5164 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5165 * complains about the following circular lock dependency / possible deadlock:
5169 * lock(&type->i_mutex_dir_key#3/2);
5170 * lock(sb_internal#2);
5171 * lock(&type->i_mutex_dir_key#3/2);
5172 * lock(&sb->s_type->i_mutex_key#14);
5174 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5175 * sb_start_intwrite() in btrfs_start_transaction().
5176 * Not locking i_mutex of the inodes is still safe because:
5178 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5179 * that while logging the inode new references (names) are added or removed
5180 * from the inode, leaving the logged inode item with a link count that does
5181 * not match the number of logged inode reference items. This is fine because
5182 * at log replay time we compute the real number of links and correct the
5183 * link count in the inode item (see replay_one_buffer() and
5184 * link_to_fixup_dir());
5186 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5187 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5188 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5189 * has a size that doesn't match the sum of the lengths of all the logged
5190 * names. This does not result in a problem because if a dir_item key is
5191 * logged but its matching dir_index key is not logged, at log replay time we
5192 * don't use it to replay the respective name (see replay_one_name()). On the
5193 * other hand if only the dir_index key ends up being logged, the respective
5194 * name is added to the fs/subvol tree with both the dir_item and dir_index
5195 * keys created (see replay_one_name()).
5196 * The directory's inode item with a wrong i_size is not a problem as well,
5197 * since we don't use it at log replay time to set the i_size in the inode
5198 * item of the fs/subvol tree (see overwrite_item()).
5200 static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5201 struct btrfs_root *root,
5202 struct inode *start_inode,
5203 struct btrfs_log_ctx *ctx)
5205 struct btrfs_root *log = root->log_root;
5206 struct btrfs_path *path;
5207 LIST_HEAD(dir_list);
5208 struct btrfs_dir_list *dir_elem;
5211 path = btrfs_alloc_path();
5215 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5217 btrfs_free_path(path);
5220 dir_elem->ino = btrfs_ino(start_inode);
5221 list_add_tail(&dir_elem->list, &dir_list);
5223 while (!list_empty(&dir_list)) {
5224 struct extent_buffer *leaf;
5225 struct btrfs_key min_key;
5229 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5232 goto next_dir_inode;
5234 min_key.objectid = dir_elem->ino;
5235 min_key.type = BTRFS_DIR_ITEM_KEY;
5238 btrfs_release_path(path);
5239 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5241 goto next_dir_inode;
5242 } else if (ret > 0) {
5244 goto next_dir_inode;
5248 leaf = path->nodes[0];
5249 nritems = btrfs_header_nritems(leaf);
5250 for (i = path->slots[0]; i < nritems; i++) {
5251 struct btrfs_dir_item *di;
5252 struct btrfs_key di_key;
5253 struct inode *di_inode;
5254 struct btrfs_dir_list *new_dir_elem;
5255 int log_mode = LOG_INODE_EXISTS;
5258 btrfs_item_key_to_cpu(leaf, &min_key, i);
5259 if (min_key.objectid != dir_elem->ino ||
5260 min_key.type != BTRFS_DIR_ITEM_KEY)
5261 goto next_dir_inode;
5263 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5264 type = btrfs_dir_type(leaf, di);
5265 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5266 type != BTRFS_FT_DIR)
5268 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5269 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5272 btrfs_release_path(path);
5273 di_inode = btrfs_iget(root->fs_info->sb, &di_key,
5275 if (IS_ERR(di_inode)) {
5276 ret = PTR_ERR(di_inode);
5277 goto next_dir_inode;
5280 if (btrfs_inode_in_log(di_inode, trans->transid)) {
5281 btrfs_add_delayed_iput(di_inode);
5285 ctx->log_new_dentries = false;
5286 if (type == BTRFS_FT_DIR)
5287 log_mode = LOG_INODE_ALL;
5288 ret = btrfs_log_inode(trans, root, di_inode,
5289 log_mode, 0, LLONG_MAX, ctx);
5291 btrfs_must_commit_transaction(trans, di_inode))
5293 btrfs_add_delayed_iput(di_inode);
5295 goto next_dir_inode;
5296 if (ctx->log_new_dentries) {
5297 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5299 if (!new_dir_elem) {
5301 goto next_dir_inode;
5303 new_dir_elem->ino = di_key.objectid;
5304 list_add_tail(&new_dir_elem->list, &dir_list);
5309 ret = btrfs_next_leaf(log, path);
5311 goto next_dir_inode;
5312 } else if (ret > 0) {
5314 goto next_dir_inode;
5318 if (min_key.offset < (u64)-1) {
5323 list_del(&dir_elem->list);
5327 btrfs_free_path(path);
5331 static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5332 struct inode *inode,
5333 struct btrfs_log_ctx *ctx)
5336 struct btrfs_path *path;
5337 struct btrfs_key key;
5338 struct btrfs_root *root = BTRFS_I(inode)->root;
5339 const u64 ino = btrfs_ino(inode);
5341 path = btrfs_alloc_path();
5344 path->skip_locking = 1;
5345 path->search_commit_root = 1;
5348 key.type = BTRFS_INODE_REF_KEY;
5350 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5355 struct extent_buffer *leaf = path->nodes[0];
5356 int slot = path->slots[0];
5361 if (slot >= btrfs_header_nritems(leaf)) {
5362 ret = btrfs_next_leaf(root, path);
5370 btrfs_item_key_to_cpu(leaf, &key, slot);
5371 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5372 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5375 item_size = btrfs_item_size_nr(leaf, slot);
5376 ptr = btrfs_item_ptr_offset(leaf, slot);
5377 while (cur_offset < item_size) {
5378 struct btrfs_key inode_key;
5379 struct inode *dir_inode;
5381 inode_key.type = BTRFS_INODE_ITEM_KEY;
5382 inode_key.offset = 0;
5384 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5385 struct btrfs_inode_extref *extref;
5387 extref = (struct btrfs_inode_extref *)
5389 inode_key.objectid = btrfs_inode_extref_parent(
5391 cur_offset += sizeof(*extref);
5392 cur_offset += btrfs_inode_extref_name_len(leaf,
5395 inode_key.objectid = key.offset;
5396 cur_offset = item_size;
5399 dir_inode = btrfs_iget(root->fs_info->sb, &inode_key,
5402 * If the parent inode was deleted, return an error to
5403 * fallback to a transaction commit. This is to prevent
5404 * getting an inode that was moved from one parent A to
5405 * a parent B, got its former parent A deleted and then
5406 * it got fsync'ed, from existing at both parents after
5407 * a log replay (and the old parent still existing).
5414 * mv /mnt/B/bar /mnt/A/bar
5415 * mv -T /mnt/A /mnt/B
5419 * If we ignore the old parent B which got deleted,
5420 * after a log replay we would have file bar linked
5421 * at both parents and the old parent B would still
5424 if (IS_ERR(dir_inode)) {
5425 ret = PTR_ERR(dir_inode);
5430 ctx->log_new_dentries = false;
5431 ret = btrfs_log_inode(trans, root, dir_inode,
5432 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
5434 btrfs_must_commit_transaction(trans, dir_inode))
5436 if (!ret && ctx && ctx->log_new_dentries)
5437 ret = log_new_dir_dentries(trans, root,
5439 btrfs_add_delayed_iput(dir_inode);
5447 btrfs_free_path(path);
5452 * helper function around btrfs_log_inode to make sure newly created
5453 * parent directories also end up in the log. A minimal inode and backref
5454 * only logging is done of any parent directories that are older than
5455 * the last committed transaction
5457 static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5458 struct btrfs_root *root, struct inode *inode,
5459 struct dentry *parent,
5463 struct btrfs_log_ctx *ctx)
5465 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
5466 struct super_block *sb;
5467 struct dentry *old_parent = NULL;
5469 u64 last_committed = root->fs_info->last_trans_committed;
5470 bool log_dentries = false;
5471 struct inode *orig_inode = inode;
5475 if (btrfs_test_opt(root->fs_info, NOTREELOG)) {
5481 * The prev transaction commit doesn't complete, we need do
5482 * full commit by ourselves.
5484 if (root->fs_info->last_trans_log_full_commit >
5485 root->fs_info->last_trans_committed) {
5490 if (root != BTRFS_I(inode)->root ||
5491 btrfs_root_refs(&root->root_item) == 0) {
5496 ret = check_parent_dirs_for_sync(trans, inode, parent,
5497 sb, last_committed);
5501 if (btrfs_inode_in_log(inode, trans->transid)) {
5502 ret = BTRFS_NO_LOG_SYNC;
5506 ret = start_log_trans(trans, root, ctx);
5510 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
5515 * for regular files, if its inode is already on disk, we don't
5516 * have to worry about the parents at all. This is because
5517 * we can use the last_unlink_trans field to record renames
5518 * and other fun in this file.
5520 if (S_ISREG(inode->i_mode) &&
5521 BTRFS_I(inode)->generation <= last_committed &&
5522 BTRFS_I(inode)->last_unlink_trans <= last_committed) {
5527 if (S_ISDIR(inode->i_mode) && ctx && ctx->log_new_dentries)
5528 log_dentries = true;
5531 * On unlink we must make sure all our current and old parent directory
5532 * inodes are fully logged. This is to prevent leaving dangling
5533 * directory index entries in directories that were our parents but are
5534 * not anymore. Not doing this results in old parent directory being
5535 * impossible to delete after log replay (rmdir will always fail with
5536 * error -ENOTEMPTY).
5542 * ln testdir/foo testdir/bar
5544 * unlink testdir/bar
5545 * xfs_io -c fsync testdir/foo
5547 * mount fs, triggers log replay
5549 * If we don't log the parent directory (testdir), after log replay the
5550 * directory still has an entry pointing to the file inode using the bar
5551 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5552 * the file inode has a link count of 1.
5558 * ln foo testdir/foo2
5559 * ln foo testdir/foo3
5561 * unlink testdir/foo3
5562 * xfs_io -c fsync foo
5564 * mount fs, triggers log replay
5566 * Similar as the first example, after log replay the parent directory
5567 * testdir still has an entry pointing to the inode file with name foo3
5568 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5569 * and has a link count of 2.
5571 if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
5572 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5578 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5581 inode = d_inode(parent);
5582 if (root != BTRFS_I(inode)->root)
5585 if (BTRFS_I(inode)->generation > last_committed) {
5586 ret = btrfs_log_inode(trans, root, inode,
5592 if (IS_ROOT(parent))
5595 parent = dget_parent(parent);
5597 old_parent = parent;
5600 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5606 btrfs_set_log_full_commit(root->fs_info, trans);
5611 btrfs_remove_log_ctx(root, ctx);
5612 btrfs_end_log_trans(root);
5618 * it is not safe to log dentry if the chunk root has added new
5619 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5620 * If this returns 1, you must commit the transaction to safely get your
5623 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
5624 struct btrfs_root *root, struct dentry *dentry,
5627 struct btrfs_log_ctx *ctx)
5629 struct dentry *parent = dget_parent(dentry);
5632 ret = btrfs_log_inode_parent(trans, root, d_inode(dentry), parent,
5633 start, end, 0, ctx);
5640 * should be called during mount to recover any replay any log trees
5643 int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5646 struct btrfs_path *path;
5647 struct btrfs_trans_handle *trans;
5648 struct btrfs_key key;
5649 struct btrfs_key found_key;
5650 struct btrfs_key tmp_key;
5651 struct btrfs_root *log;
5652 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5653 struct walk_control wc = {
5654 .process_func = process_one_buffer,
5658 path = btrfs_alloc_path();
5662 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
5664 trans = btrfs_start_transaction(fs_info->tree_root, 0);
5665 if (IS_ERR(trans)) {
5666 ret = PTR_ERR(trans);
5673 ret = walk_log_tree(trans, log_root_tree, &wc);
5675 btrfs_handle_fs_error(fs_info, ret,
5676 "Failed to pin buffers while recovering log root tree.");
5681 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5682 key.offset = (u64)-1;
5683 key.type = BTRFS_ROOT_ITEM_KEY;
5686 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
5689 btrfs_handle_fs_error(fs_info, ret,
5690 "Couldn't find tree log root.");
5694 if (path->slots[0] == 0)
5698 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5700 btrfs_release_path(path);
5701 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5704 log = btrfs_read_fs_root(log_root_tree, &found_key);
5707 btrfs_handle_fs_error(fs_info, ret,
5708 "Couldn't read tree log root.");
5712 tmp_key.objectid = found_key.offset;
5713 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
5714 tmp_key.offset = (u64)-1;
5716 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
5717 if (IS_ERR(wc.replay_dest)) {
5718 ret = PTR_ERR(wc.replay_dest);
5721 * We didn't find the subvol, likely because it was
5722 * deleted. This is ok, simply skip this log and go to
5725 * We need to exclude the root because we can't have
5726 * other log replays overwriting this log as we'll read
5727 * it back in a few more times. This will keep our
5728 * block from being modified, and we'll just bail for
5729 * each subsequent pass.
5732 ret = btrfs_pin_extent_for_log_replay(fs_info->extent_root,
5735 free_extent_buffer(log->node);
5736 free_extent_buffer(log->commit_root);
5741 btrfs_handle_fs_error(fs_info, ret,
5742 "Couldn't read target root for tree log recovery.");
5746 wc.replay_dest->log_root = log;
5747 btrfs_record_root_in_trans(trans, wc.replay_dest);
5748 ret = walk_log_tree(trans, log, &wc);
5750 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5751 ret = fixup_inode_link_counts(trans, wc.replay_dest,
5755 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
5756 struct btrfs_root *root = wc.replay_dest;
5758 btrfs_release_path(path);
5761 * We have just replayed everything, and the highest
5762 * objectid of fs roots probably has changed in case
5763 * some inode_item's got replayed.
5765 * root->objectid_mutex is not acquired as log replay
5766 * could only happen during mount.
5768 ret = btrfs_find_highest_objectid(root,
5769 &root->highest_objectid);
5772 wc.replay_dest->log_root = NULL;
5773 free_extent_buffer(log->node);
5774 free_extent_buffer(log->commit_root);
5780 if (found_key.offset == 0)
5782 key.offset = found_key.offset - 1;
5784 btrfs_release_path(path);
5786 /* step one is to pin it all, step two is to replay just inodes */
5789 wc.process_func = replay_one_buffer;
5790 wc.stage = LOG_WALK_REPLAY_INODES;
5793 /* step three is to replay everything */
5794 if (wc.stage < LOG_WALK_REPLAY_ALL) {
5799 btrfs_free_path(path);
5801 /* step 4: commit the transaction, which also unpins the blocks */
5802 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
5806 free_extent_buffer(log_root_tree->node);
5807 log_root_tree->log_root = NULL;
5808 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
5809 kfree(log_root_tree);
5814 btrfs_end_transaction(wc.trans, fs_info->tree_root);
5815 btrfs_free_path(path);
5820 * there are some corner cases where we want to force a full
5821 * commit instead of allowing a directory to be logged.
5823 * They revolve around files there were unlinked from the directory, and
5824 * this function updates the parent directory so that a full commit is
5825 * properly done if it is fsync'd later after the unlinks are done.
5827 * Must be called before the unlink operations (updates to the subvolume tree,
5828 * inodes, etc) are done.
5830 void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
5831 struct inode *dir, struct inode *inode,
5835 * when we're logging a file, if it hasn't been renamed
5836 * or unlinked, and its inode is fully committed on disk,
5837 * we don't have to worry about walking up the directory chain
5838 * to log its parents.
5840 * So, we use the last_unlink_trans field to put this transid
5841 * into the file. When the file is logged we check it and
5842 * don't log the parents if the file is fully on disk.
5844 mutex_lock(&BTRFS_I(inode)->log_mutex);
5845 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5846 mutex_unlock(&BTRFS_I(inode)->log_mutex);
5849 * if this directory was already logged any new
5850 * names for this file/dir will get recorded
5853 if (BTRFS_I(dir)->logged_trans == trans->transid)
5857 * if the inode we're about to unlink was logged,
5858 * the log will be properly updated for any new names
5860 if (BTRFS_I(inode)->logged_trans == trans->transid)
5864 * when renaming files across directories, if the directory
5865 * there we're unlinking from gets fsync'd later on, there's
5866 * no way to find the destination directory later and fsync it
5867 * properly. So, we have to be conservative and force commits
5868 * so the new name gets discovered.
5873 /* we can safely do the unlink without any special recording */
5877 mutex_lock(&BTRFS_I(dir)->log_mutex);
5878 BTRFS_I(dir)->last_unlink_trans = trans->transid;
5879 mutex_unlock(&BTRFS_I(dir)->log_mutex);
5883 * Make sure that if someone attempts to fsync the parent directory of a deleted
5884 * snapshot, it ends up triggering a transaction commit. This is to guarantee
5885 * that after replaying the log tree of the parent directory's root we will not
5886 * see the snapshot anymore and at log replay time we will not see any log tree
5887 * corresponding to the deleted snapshot's root, which could lead to replaying
5888 * it after replaying the log tree of the parent directory (which would replay
5889 * the snapshot delete operation).
5891 * Must be called before the actual snapshot destroy operation (updates to the
5892 * parent root and tree of tree roots trees, etc) are done.
5894 void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
5897 mutex_lock(&BTRFS_I(dir)->log_mutex);
5898 BTRFS_I(dir)->last_unlink_trans = trans->transid;
5899 mutex_unlock(&BTRFS_I(dir)->log_mutex);
5903 * Call this after adding a new name for a file and it will properly
5904 * update the log to reflect the new name.
5906 * It will return zero if all goes well, and it will return 1 if a
5907 * full transaction commit is required.
5909 int btrfs_log_new_name(struct btrfs_trans_handle *trans,
5910 struct inode *inode, struct inode *old_dir,
5911 struct dentry *parent)
5913 struct btrfs_root * root = BTRFS_I(inode)->root;
5916 * this will force the logging code to walk the dentry chain
5919 if (S_ISREG(inode->i_mode))
5920 BTRFS_I(inode)->last_unlink_trans = trans->transid;
5923 * if this inode hasn't been logged and directory we're renaming it
5924 * from hasn't been logged, we don't need to log it
5926 if (BTRFS_I(inode)->logged_trans <=
5927 root->fs_info->last_trans_committed &&
5928 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
5929 root->fs_info->last_trans_committed))
5932 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
5933 LLONG_MAX, 1, NULL);