Linux 4.18.10
[linux/fpc-iii.git] / fs / btrfs / tree-log.c
blob84b00a29d53102292c8ddf63d1da9fdd6330b9ed
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2008 Oracle. All rights reserved.
4 */
6 #include <linux/sched.h>
7 #include <linux/slab.h>
8 #include <linux/blkdev.h>
9 #include <linux/list_sort.h>
10 #include <linux/iversion.h>
11 #include "ctree.h"
12 #include "tree-log.h"
13 #include "disk-io.h"
14 #include "locking.h"
15 #include "print-tree.h"
16 #include "backref.h"
17 #include "compression.h"
18 #include "qgroup.h"
19 #include "inode-map.h"
21 /* magic values for the inode_only field in btrfs_log_inode:
23 * LOG_INODE_ALL means to log everything
24 * LOG_INODE_EXISTS means to log just enough to recreate the inode
25 * during log replay
27 #define LOG_INODE_ALL 0
28 #define LOG_INODE_EXISTS 1
29 #define LOG_OTHER_INODE 2
32 * directory trouble cases
34 * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
35 * log, we must force a full commit before doing an fsync of the directory
36 * where the unlink was done.
37 * ---> record transid of last unlink/rename per directory
39 * mkdir foo/some_dir
40 * normal commit
41 * rename foo/some_dir foo2/some_dir
42 * mkdir foo/some_dir
43 * fsync foo/some_dir/some_file
45 * The fsync above will unlink the original some_dir without recording
46 * it in its new location (foo2). After a crash, some_dir will be gone
47 * unless the fsync of some_file forces a full commit
49 * 2) we must log any new names for any file or dir that is in the fsync
50 * log. ---> check inode while renaming/linking.
52 * 2a) we must log any new names for any file or dir during rename
53 * when the directory they are being removed from was logged.
54 * ---> check inode and old parent dir during rename
56 * 2a is actually the more important variant. With the extra logging
57 * a crash might unlink the old name without recreating the new one
59 * 3) after a crash, we must go through any directories with a link count
60 * of zero and redo the rm -rf
62 * mkdir f1/foo
63 * normal commit
64 * rm -rf f1/foo
65 * fsync(f1)
67 * The directory f1 was fully removed from the FS, but fsync was never
68 * called on f1, only its parent dir. After a crash the rm -rf must
69 * be replayed. This must be able to recurse down the entire
70 * directory tree. The inode link count fixup code takes care of the
71 * ugly details.
75 * stages for the tree walking. The first
76 * stage (0) is to only pin down the blocks we find
77 * the second stage (1) is to make sure that all the inodes
78 * we find in the log are created in the subvolume.
80 * The last stage is to deal with directories and links and extents
81 * and all the other fun semantics
83 #define LOG_WALK_PIN_ONLY 0
84 #define LOG_WALK_REPLAY_INODES 1
85 #define LOG_WALK_REPLAY_DIR_INDEX 2
86 #define LOG_WALK_REPLAY_ALL 3
88 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
89 struct btrfs_root *root, struct btrfs_inode *inode,
90 int inode_only,
91 const loff_t start,
92 const loff_t end,
93 struct btrfs_log_ctx *ctx);
94 static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
95 struct btrfs_root *root,
96 struct btrfs_path *path, u64 objectid);
97 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root,
99 struct btrfs_root *log,
100 struct btrfs_path *path,
101 u64 dirid, int del_all);
104 * tree logging is a special write ahead log used to make sure that
105 * fsyncs and O_SYNCs can happen without doing full tree commits.
107 * Full tree commits are expensive because they require commonly
108 * modified blocks to be recowed, creating many dirty pages in the
109 * extent tree an 4x-6x higher write load than ext3.
111 * Instead of doing a tree commit on every fsync, we use the
112 * key ranges and transaction ids to find items for a given file or directory
113 * that have changed in this transaction. Those items are copied into
114 * a special tree (one per subvolume root), that tree is written to disk
115 * and then the fsync is considered complete.
117 * After a crash, items are copied out of the log-tree back into the
118 * subvolume tree. Any file data extents found are recorded in the extent
119 * allocation tree, and the log-tree freed.
121 * The log tree is read three times, once to pin down all the extents it is
122 * using in ram and once, once to create all the inodes logged in the tree
123 * and once to do all the other items.
127 * start a sub transaction and setup the log tree
128 * this increments the log tree writer count to make the people
129 * syncing the tree wait for us to finish
131 static int start_log_trans(struct btrfs_trans_handle *trans,
132 struct btrfs_root *root,
133 struct btrfs_log_ctx *ctx)
135 struct btrfs_fs_info *fs_info = root->fs_info;
136 int ret = 0;
138 mutex_lock(&root->log_mutex);
140 if (root->log_root) {
141 if (btrfs_need_log_full_commit(fs_info, trans)) {
142 ret = -EAGAIN;
143 goto out;
146 if (!root->log_start_pid) {
147 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
148 root->log_start_pid = current->pid;
149 } else if (root->log_start_pid != current->pid) {
150 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
152 } else {
153 mutex_lock(&fs_info->tree_log_mutex);
154 if (!fs_info->log_root_tree)
155 ret = btrfs_init_log_root_tree(trans, fs_info);
156 mutex_unlock(&fs_info->tree_log_mutex);
157 if (ret)
158 goto out;
160 ret = btrfs_add_log_tree(trans, root);
161 if (ret)
162 goto out;
164 clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
165 root->log_start_pid = current->pid;
168 atomic_inc(&root->log_batch);
169 atomic_inc(&root->log_writers);
170 if (ctx) {
171 int index = root->log_transid % 2;
172 list_add_tail(&ctx->list, &root->log_ctxs[index]);
173 ctx->log_transid = root->log_transid;
176 out:
177 mutex_unlock(&root->log_mutex);
178 return ret;
182 * returns 0 if there was a log transaction running and we were able
183 * to join, or returns -ENOENT if there were not transactions
184 * in progress
186 static int join_running_log_trans(struct btrfs_root *root)
188 int ret = -ENOENT;
190 smp_mb();
191 if (!root->log_root)
192 return -ENOENT;
194 mutex_lock(&root->log_mutex);
195 if (root->log_root) {
196 ret = 0;
197 atomic_inc(&root->log_writers);
199 mutex_unlock(&root->log_mutex);
200 return ret;
204 * This either makes the current running log transaction wait
205 * until you call btrfs_end_log_trans() or it makes any future
206 * log transactions wait until you call btrfs_end_log_trans()
208 int btrfs_pin_log_trans(struct btrfs_root *root)
210 int ret = -ENOENT;
212 mutex_lock(&root->log_mutex);
213 atomic_inc(&root->log_writers);
214 mutex_unlock(&root->log_mutex);
215 return ret;
219 * indicate we're done making changes to the log tree
220 * and wake up anyone waiting to do a sync
222 void btrfs_end_log_trans(struct btrfs_root *root)
224 if (atomic_dec_and_test(&root->log_writers)) {
225 /* atomic_dec_and_test implies a barrier */
226 cond_wake_up_nomb(&root->log_writer_wait);
232 * the walk control struct is used to pass state down the chain when
233 * processing the log tree. The stage field tells us which part
234 * of the log tree processing we are currently doing. The others
235 * are state fields used for that specific part
237 struct walk_control {
238 /* should we free the extent on disk when done? This is used
239 * at transaction commit time while freeing a log tree
241 int free;
243 /* should we write out the extent buffer? This is used
244 * while flushing the log tree to disk during a sync
246 int write;
248 /* should we wait for the extent buffer io to finish? Also used
249 * while flushing the log tree to disk for a sync
251 int wait;
253 /* pin only walk, we record which extents on disk belong to the
254 * log trees
256 int pin;
258 /* what stage of the replay code we're currently in */
259 int stage;
261 /* the root we are currently replaying */
262 struct btrfs_root *replay_dest;
264 /* the trans handle for the current replay */
265 struct btrfs_trans_handle *trans;
267 /* the function that gets used to process blocks we find in the
268 * tree. Note the extent_buffer might not be up to date when it is
269 * passed in, and it must be checked or read if you need the data
270 * inside it
272 int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
273 struct walk_control *wc, u64 gen, int level);
277 * process_func used to pin down extents, write them or wait on them
279 static int process_one_buffer(struct btrfs_root *log,
280 struct extent_buffer *eb,
281 struct walk_control *wc, u64 gen, int level)
283 struct btrfs_fs_info *fs_info = log->fs_info;
284 int ret = 0;
287 * If this fs is mixed then we need to be able to process the leaves to
288 * pin down any logged extents, so we have to read the block.
290 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
291 ret = btrfs_read_buffer(eb, gen, level, NULL);
292 if (ret)
293 return ret;
296 if (wc->pin)
297 ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
298 eb->len);
300 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
301 if (wc->pin && btrfs_header_level(eb) == 0)
302 ret = btrfs_exclude_logged_extents(fs_info, eb);
303 if (wc->write)
304 btrfs_write_tree_block(eb);
305 if (wc->wait)
306 btrfs_wait_tree_block_writeback(eb);
308 return ret;
312 * Item overwrite used by replay and tree logging. eb, slot and key all refer
313 * to the src data we are copying out.
315 * root is the tree we are copying into, and path is a scratch
316 * path for use in this function (it should be released on entry and
317 * will be released on exit).
319 * If the key is already in the destination tree the existing item is
320 * overwritten. If the existing item isn't big enough, it is extended.
321 * If it is too large, it is truncated.
323 * If the key isn't in the destination yet, a new item is inserted.
325 static noinline int overwrite_item(struct btrfs_trans_handle *trans,
326 struct btrfs_root *root,
327 struct btrfs_path *path,
328 struct extent_buffer *eb, int slot,
329 struct btrfs_key *key)
331 struct btrfs_fs_info *fs_info = root->fs_info;
332 int ret;
333 u32 item_size;
334 u64 saved_i_size = 0;
335 int save_old_i_size = 0;
336 unsigned long src_ptr;
337 unsigned long dst_ptr;
338 int overwrite_root = 0;
339 bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
341 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
342 overwrite_root = 1;
344 item_size = btrfs_item_size_nr(eb, slot);
345 src_ptr = btrfs_item_ptr_offset(eb, slot);
347 /* look for the key in the destination tree */
348 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
349 if (ret < 0)
350 return ret;
352 if (ret == 0) {
353 char *src_copy;
354 char *dst_copy;
355 u32 dst_size = btrfs_item_size_nr(path->nodes[0],
356 path->slots[0]);
357 if (dst_size != item_size)
358 goto insert;
360 if (item_size == 0) {
361 btrfs_release_path(path);
362 return 0;
364 dst_copy = kmalloc(item_size, GFP_NOFS);
365 src_copy = kmalloc(item_size, GFP_NOFS);
366 if (!dst_copy || !src_copy) {
367 btrfs_release_path(path);
368 kfree(dst_copy);
369 kfree(src_copy);
370 return -ENOMEM;
373 read_extent_buffer(eb, src_copy, src_ptr, item_size);
375 dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
376 read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
377 item_size);
378 ret = memcmp(dst_copy, src_copy, item_size);
380 kfree(dst_copy);
381 kfree(src_copy);
383 * they have the same contents, just return, this saves
384 * us from cowing blocks in the destination tree and doing
385 * extra writes that may not have been done by a previous
386 * sync
388 if (ret == 0) {
389 btrfs_release_path(path);
390 return 0;
394 * We need to load the old nbytes into the inode so when we
395 * replay the extents we've logged we get the right nbytes.
397 if (inode_item) {
398 struct btrfs_inode_item *item;
399 u64 nbytes;
400 u32 mode;
402 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
403 struct btrfs_inode_item);
404 nbytes = btrfs_inode_nbytes(path->nodes[0], item);
405 item = btrfs_item_ptr(eb, slot,
406 struct btrfs_inode_item);
407 btrfs_set_inode_nbytes(eb, item, nbytes);
410 * If this is a directory we need to reset the i_size to
411 * 0 so that we can set it up properly when replaying
412 * the rest of the items in this log.
414 mode = btrfs_inode_mode(eb, item);
415 if (S_ISDIR(mode))
416 btrfs_set_inode_size(eb, item, 0);
418 } else if (inode_item) {
419 struct btrfs_inode_item *item;
420 u32 mode;
423 * New inode, set nbytes to 0 so that the nbytes comes out
424 * properly when we replay the extents.
426 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
427 btrfs_set_inode_nbytes(eb, item, 0);
430 * If this is a directory we need to reset the i_size to 0 so
431 * that we can set it up properly when replaying the rest of
432 * the items in this log.
434 mode = btrfs_inode_mode(eb, item);
435 if (S_ISDIR(mode))
436 btrfs_set_inode_size(eb, item, 0);
438 insert:
439 btrfs_release_path(path);
440 /* try to insert the key into the destination tree */
441 path->skip_release_on_error = 1;
442 ret = btrfs_insert_empty_item(trans, root, path,
443 key, item_size);
444 path->skip_release_on_error = 0;
446 /* make sure any existing item is the correct size */
447 if (ret == -EEXIST || ret == -EOVERFLOW) {
448 u32 found_size;
449 found_size = btrfs_item_size_nr(path->nodes[0],
450 path->slots[0]);
451 if (found_size > item_size)
452 btrfs_truncate_item(fs_info, path, item_size, 1);
453 else if (found_size < item_size)
454 btrfs_extend_item(fs_info, path,
455 item_size - found_size);
456 } else if (ret) {
457 return ret;
459 dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
460 path->slots[0]);
462 /* don't overwrite an existing inode if the generation number
463 * was logged as zero. This is done when the tree logging code
464 * is just logging an inode to make sure it exists after recovery.
466 * Also, don't overwrite i_size on directories during replay.
467 * log replay inserts and removes directory items based on the
468 * state of the tree found in the subvolume, and i_size is modified
469 * as it goes
471 if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
472 struct btrfs_inode_item *src_item;
473 struct btrfs_inode_item *dst_item;
475 src_item = (struct btrfs_inode_item *)src_ptr;
476 dst_item = (struct btrfs_inode_item *)dst_ptr;
478 if (btrfs_inode_generation(eb, src_item) == 0) {
479 struct extent_buffer *dst_eb = path->nodes[0];
480 const u64 ino_size = btrfs_inode_size(eb, src_item);
483 * For regular files an ino_size == 0 is used only when
484 * logging that an inode exists, as part of a directory
485 * fsync, and the inode wasn't fsynced before. In this
486 * case don't set the size of the inode in the fs/subvol
487 * tree, otherwise we would be throwing valid data away.
489 if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
490 S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
491 ino_size != 0) {
492 struct btrfs_map_token token;
494 btrfs_init_map_token(&token);
495 btrfs_set_token_inode_size(dst_eb, dst_item,
496 ino_size, &token);
498 goto no_copy;
501 if (overwrite_root &&
502 S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
503 S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
504 save_old_i_size = 1;
505 saved_i_size = btrfs_inode_size(path->nodes[0],
506 dst_item);
510 copy_extent_buffer(path->nodes[0], eb, dst_ptr,
511 src_ptr, item_size);
513 if (save_old_i_size) {
514 struct btrfs_inode_item *dst_item;
515 dst_item = (struct btrfs_inode_item *)dst_ptr;
516 btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
519 /* make sure the generation is filled in */
520 if (key->type == BTRFS_INODE_ITEM_KEY) {
521 struct btrfs_inode_item *dst_item;
522 dst_item = (struct btrfs_inode_item *)dst_ptr;
523 if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
524 btrfs_set_inode_generation(path->nodes[0], dst_item,
525 trans->transid);
528 no_copy:
529 btrfs_mark_buffer_dirty(path->nodes[0]);
530 btrfs_release_path(path);
531 return 0;
535 * simple helper to read an inode off the disk from a given root
536 * This can only be called for subvolume roots and not for the log
538 static noinline struct inode *read_one_inode(struct btrfs_root *root,
539 u64 objectid)
541 struct btrfs_key key;
542 struct inode *inode;
544 key.objectid = objectid;
545 key.type = BTRFS_INODE_ITEM_KEY;
546 key.offset = 0;
547 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
548 if (IS_ERR(inode)) {
549 inode = NULL;
550 } else if (is_bad_inode(inode)) {
551 iput(inode);
552 inode = NULL;
554 return inode;
557 /* replays a single extent in 'eb' at 'slot' with 'key' into the
558 * subvolume 'root'. path is released on entry and should be released
559 * on exit.
561 * extents in the log tree have not been allocated out of the extent
562 * tree yet. So, this completes the allocation, taking a reference
563 * as required if the extent already exists or creating a new extent
564 * if it isn't in the extent allocation tree yet.
566 * The extent is inserted into the file, dropping any existing extents
567 * from the file that overlap the new one.
569 static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
570 struct btrfs_root *root,
571 struct btrfs_path *path,
572 struct extent_buffer *eb, int slot,
573 struct btrfs_key *key)
575 struct btrfs_fs_info *fs_info = root->fs_info;
576 int found_type;
577 u64 extent_end;
578 u64 start = key->offset;
579 u64 nbytes = 0;
580 struct btrfs_file_extent_item *item;
581 struct inode *inode = NULL;
582 unsigned long size;
583 int ret = 0;
585 item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
586 found_type = btrfs_file_extent_type(eb, item);
588 if (found_type == BTRFS_FILE_EXTENT_REG ||
589 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
590 nbytes = btrfs_file_extent_num_bytes(eb, item);
591 extent_end = start + nbytes;
594 * We don't add to the inodes nbytes if we are prealloc or a
595 * hole.
597 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
598 nbytes = 0;
599 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
600 size = btrfs_file_extent_inline_len(eb, slot, item);
601 nbytes = btrfs_file_extent_ram_bytes(eb, item);
602 extent_end = ALIGN(start + size,
603 fs_info->sectorsize);
604 } else {
605 ret = 0;
606 goto out;
609 inode = read_one_inode(root, key->objectid);
610 if (!inode) {
611 ret = -EIO;
612 goto out;
616 * first check to see if we already have this extent in the
617 * file. This must be done before the btrfs_drop_extents run
618 * so we don't try to drop this extent.
620 ret = btrfs_lookup_file_extent(trans, root, path,
621 btrfs_ino(BTRFS_I(inode)), start, 0);
623 if (ret == 0 &&
624 (found_type == BTRFS_FILE_EXTENT_REG ||
625 found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
626 struct btrfs_file_extent_item cmp1;
627 struct btrfs_file_extent_item cmp2;
628 struct btrfs_file_extent_item *existing;
629 struct extent_buffer *leaf;
631 leaf = path->nodes[0];
632 existing = btrfs_item_ptr(leaf, path->slots[0],
633 struct btrfs_file_extent_item);
635 read_extent_buffer(eb, &cmp1, (unsigned long)item,
636 sizeof(cmp1));
637 read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
638 sizeof(cmp2));
641 * we already have a pointer to this exact extent,
642 * we don't have to do anything
644 if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
645 btrfs_release_path(path);
646 goto out;
649 btrfs_release_path(path);
651 /* drop any overlapping extents */
652 ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
653 if (ret)
654 goto out;
656 if (found_type == BTRFS_FILE_EXTENT_REG ||
657 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
658 u64 offset;
659 unsigned long dest_offset;
660 struct btrfs_key ins;
662 if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
663 btrfs_fs_incompat(fs_info, NO_HOLES))
664 goto update_inode;
666 ret = btrfs_insert_empty_item(trans, root, path, key,
667 sizeof(*item));
668 if (ret)
669 goto out;
670 dest_offset = btrfs_item_ptr_offset(path->nodes[0],
671 path->slots[0]);
672 copy_extent_buffer(path->nodes[0], eb, dest_offset,
673 (unsigned long)item, sizeof(*item));
675 ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
676 ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
677 ins.type = BTRFS_EXTENT_ITEM_KEY;
678 offset = key->offset - btrfs_file_extent_offset(eb, item);
681 * Manually record dirty extent, as here we did a shallow
682 * file extent item copy and skip normal backref update,
683 * but modifying extent tree all by ourselves.
684 * So need to manually record dirty extent for qgroup,
685 * as the owner of the file extent changed from log tree
686 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
688 ret = btrfs_qgroup_trace_extent(trans, fs_info,
689 btrfs_file_extent_disk_bytenr(eb, item),
690 btrfs_file_extent_disk_num_bytes(eb, item),
691 GFP_NOFS);
692 if (ret < 0)
693 goto out;
695 if (ins.objectid > 0) {
696 u64 csum_start;
697 u64 csum_end;
698 LIST_HEAD(ordered_sums);
700 * is this extent already allocated in the extent
701 * allocation tree? If so, just add a reference
703 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
704 ins.offset);
705 if (ret == 0) {
706 ret = btrfs_inc_extent_ref(trans, root,
707 ins.objectid, ins.offset,
708 0, root->root_key.objectid,
709 key->objectid, offset);
710 if (ret)
711 goto out;
712 } else {
714 * insert the extent pointer in the extent
715 * allocation tree
717 ret = btrfs_alloc_logged_file_extent(trans,
718 fs_info,
719 root->root_key.objectid,
720 key->objectid, offset, &ins);
721 if (ret)
722 goto out;
724 btrfs_release_path(path);
726 if (btrfs_file_extent_compression(eb, item)) {
727 csum_start = ins.objectid;
728 csum_end = csum_start + ins.offset;
729 } else {
730 csum_start = ins.objectid +
731 btrfs_file_extent_offset(eb, item);
732 csum_end = csum_start +
733 btrfs_file_extent_num_bytes(eb, item);
736 ret = btrfs_lookup_csums_range(root->log_root,
737 csum_start, csum_end - 1,
738 &ordered_sums, 0);
739 if (ret)
740 goto out;
742 * Now delete all existing cums in the csum root that
743 * cover our range. We do this because we can have an
744 * extent that is completely referenced by one file
745 * extent item and partially referenced by another
746 * file extent item (like after using the clone or
747 * extent_same ioctls). In this case if we end up doing
748 * the replay of the one that partially references the
749 * extent first, and we do not do the csum deletion
750 * below, we can get 2 csum items in the csum tree that
751 * overlap each other. For example, imagine our log has
752 * the two following file extent items:
754 * key (257 EXTENT_DATA 409600)
755 * extent data disk byte 12845056 nr 102400
756 * extent data offset 20480 nr 20480 ram 102400
758 * key (257 EXTENT_DATA 819200)
759 * extent data disk byte 12845056 nr 102400
760 * extent data offset 0 nr 102400 ram 102400
762 * Where the second one fully references the 100K extent
763 * that starts at disk byte 12845056, and the log tree
764 * has a single csum item that covers the entire range
765 * of the extent:
767 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
769 * After the first file extent item is replayed, the
770 * csum tree gets the following csum item:
772 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
774 * Which covers the 20K sub-range starting at offset 20K
775 * of our extent. Now when we replay the second file
776 * extent item, if we do not delete existing csum items
777 * that cover any of its blocks, we end up getting two
778 * csum items in our csum tree that overlap each other:
780 * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
781 * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
783 * Which is a problem, because after this anyone trying
784 * to lookup up for the checksum of any block of our
785 * extent starting at an offset of 40K or higher, will
786 * end up looking at the second csum item only, which
787 * does not contain the checksum for any block starting
788 * at offset 40K or higher of our extent.
790 while (!list_empty(&ordered_sums)) {
791 struct btrfs_ordered_sum *sums;
792 sums = list_entry(ordered_sums.next,
793 struct btrfs_ordered_sum,
794 list);
795 if (!ret)
796 ret = btrfs_del_csums(trans, fs_info,
797 sums->bytenr,
798 sums->len);
799 if (!ret)
800 ret = btrfs_csum_file_blocks(trans,
801 fs_info->csum_root, sums);
802 list_del(&sums->list);
803 kfree(sums);
805 if (ret)
806 goto out;
807 } else {
808 btrfs_release_path(path);
810 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
811 /* inline extents are easy, we just overwrite them */
812 ret = overwrite_item(trans, root, path, eb, slot, key);
813 if (ret)
814 goto out;
817 inode_add_bytes(inode, nbytes);
818 update_inode:
819 ret = btrfs_update_inode(trans, root, inode);
820 out:
821 if (inode)
822 iput(inode);
823 return ret;
827 * when cleaning up conflicts between the directory names in the
828 * subvolume, directory names in the log and directory names in the
829 * inode back references, we may have to unlink inodes from directories.
831 * This is a helper function to do the unlink of a specific directory
832 * item
834 static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
835 struct btrfs_root *root,
836 struct btrfs_path *path,
837 struct btrfs_inode *dir,
838 struct btrfs_dir_item *di)
840 struct inode *inode;
841 char *name;
842 int name_len;
843 struct extent_buffer *leaf;
844 struct btrfs_key location;
845 int ret;
847 leaf = path->nodes[0];
849 btrfs_dir_item_key_to_cpu(leaf, di, &location);
850 name_len = btrfs_dir_name_len(leaf, di);
851 name = kmalloc(name_len, GFP_NOFS);
852 if (!name)
853 return -ENOMEM;
855 read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
856 btrfs_release_path(path);
858 inode = read_one_inode(root, location.objectid);
859 if (!inode) {
860 ret = -EIO;
861 goto out;
864 ret = link_to_fixup_dir(trans, root, path, location.objectid);
865 if (ret)
866 goto out;
868 ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
869 name_len);
870 if (ret)
871 goto out;
872 else
873 ret = btrfs_run_delayed_items(trans);
874 out:
875 kfree(name);
876 iput(inode);
877 return ret;
881 * helper function to see if a given name and sequence number found
882 * in an inode back reference are already in a directory and correctly
883 * point to this inode
885 static noinline int inode_in_dir(struct btrfs_root *root,
886 struct btrfs_path *path,
887 u64 dirid, u64 objectid, u64 index,
888 const char *name, int name_len)
890 struct btrfs_dir_item *di;
891 struct btrfs_key location;
892 int match = 0;
894 di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
895 index, name, name_len, 0);
896 if (di && !IS_ERR(di)) {
897 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
898 if (location.objectid != objectid)
899 goto out;
900 } else
901 goto out;
902 btrfs_release_path(path);
904 di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
905 if (di && !IS_ERR(di)) {
906 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
907 if (location.objectid != objectid)
908 goto out;
909 } else
910 goto out;
911 match = 1;
912 out:
913 btrfs_release_path(path);
914 return match;
918 * helper function to check a log tree for a named back reference in
919 * an inode. This is used to decide if a back reference that is
920 * found in the subvolume conflicts with what we find in the log.
922 * inode backreferences may have multiple refs in a single item,
923 * during replay we process one reference at a time, and we don't
924 * want to delete valid links to a file from the subvolume if that
925 * link is also in the log.
927 static noinline int backref_in_log(struct btrfs_root *log,
928 struct btrfs_key *key,
929 u64 ref_objectid,
930 const char *name, int namelen)
932 struct btrfs_path *path;
933 struct btrfs_inode_ref *ref;
934 unsigned long ptr;
935 unsigned long ptr_end;
936 unsigned long name_ptr;
937 int found_name_len;
938 int item_size;
939 int ret;
940 int match = 0;
942 path = btrfs_alloc_path();
943 if (!path)
944 return -ENOMEM;
946 ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
947 if (ret != 0)
948 goto out;
950 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
952 if (key->type == BTRFS_INODE_EXTREF_KEY) {
953 if (btrfs_find_name_in_ext_backref(path->nodes[0],
954 path->slots[0],
955 ref_objectid,
956 name, namelen, NULL))
957 match = 1;
959 goto out;
962 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
963 ptr_end = ptr + item_size;
964 while (ptr < ptr_end) {
965 ref = (struct btrfs_inode_ref *)ptr;
966 found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
967 if (found_name_len == namelen) {
968 name_ptr = (unsigned long)(ref + 1);
969 ret = memcmp_extent_buffer(path->nodes[0], name,
970 name_ptr, namelen);
971 if (ret == 0) {
972 match = 1;
973 goto out;
976 ptr = (unsigned long)(ref + 1) + found_name_len;
978 out:
979 btrfs_free_path(path);
980 return match;
983 static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
984 struct btrfs_root *root,
985 struct btrfs_path *path,
986 struct btrfs_root *log_root,
987 struct btrfs_inode *dir,
988 struct btrfs_inode *inode,
989 u64 inode_objectid, u64 parent_objectid,
990 u64 ref_index, char *name, int namelen,
991 int *search_done)
993 int ret;
994 char *victim_name;
995 int victim_name_len;
996 struct extent_buffer *leaf;
997 struct btrfs_dir_item *di;
998 struct btrfs_key search_key;
999 struct btrfs_inode_extref *extref;
1001 again:
1002 /* Search old style refs */
1003 search_key.objectid = inode_objectid;
1004 search_key.type = BTRFS_INODE_REF_KEY;
1005 search_key.offset = parent_objectid;
1006 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
1007 if (ret == 0) {
1008 struct btrfs_inode_ref *victim_ref;
1009 unsigned long ptr;
1010 unsigned long ptr_end;
1012 leaf = path->nodes[0];
1014 /* are we trying to overwrite a back ref for the root directory
1015 * if so, just jump out, we're done
1017 if (search_key.objectid == search_key.offset)
1018 return 1;
1020 /* check all the names in this back reference to see
1021 * if they are in the log. if so, we allow them to stay
1022 * otherwise they must be unlinked as a conflict
1024 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1025 ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
1026 while (ptr < ptr_end) {
1027 victim_ref = (struct btrfs_inode_ref *)ptr;
1028 victim_name_len = btrfs_inode_ref_name_len(leaf,
1029 victim_ref);
1030 victim_name = kmalloc(victim_name_len, GFP_NOFS);
1031 if (!victim_name)
1032 return -ENOMEM;
1034 read_extent_buffer(leaf, victim_name,
1035 (unsigned long)(victim_ref + 1),
1036 victim_name_len);
1038 if (!backref_in_log(log_root, &search_key,
1039 parent_objectid,
1040 victim_name,
1041 victim_name_len)) {
1042 inc_nlink(&inode->vfs_inode);
1043 btrfs_release_path(path);
1045 ret = btrfs_unlink_inode(trans, root, dir, inode,
1046 victim_name, victim_name_len);
1047 kfree(victim_name);
1048 if (ret)
1049 return ret;
1050 ret = btrfs_run_delayed_items(trans);
1051 if (ret)
1052 return ret;
1053 *search_done = 1;
1054 goto again;
1056 kfree(victim_name);
1058 ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
1062 * NOTE: we have searched root tree and checked the
1063 * corresponding ref, it does not need to check again.
1065 *search_done = 1;
1067 btrfs_release_path(path);
1069 /* Same search but for extended refs */
1070 extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
1071 inode_objectid, parent_objectid, 0,
1073 if (!IS_ERR_OR_NULL(extref)) {
1074 u32 item_size;
1075 u32 cur_offset = 0;
1076 unsigned long base;
1077 struct inode *victim_parent;
1079 leaf = path->nodes[0];
1081 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1082 base = btrfs_item_ptr_offset(leaf, path->slots[0]);
1084 while (cur_offset < item_size) {
1085 extref = (struct btrfs_inode_extref *)(base + cur_offset);
1087 victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
1089 if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
1090 goto next;
1092 victim_name = kmalloc(victim_name_len, GFP_NOFS);
1093 if (!victim_name)
1094 return -ENOMEM;
1095 read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
1096 victim_name_len);
1098 search_key.objectid = inode_objectid;
1099 search_key.type = BTRFS_INODE_EXTREF_KEY;
1100 search_key.offset = btrfs_extref_hash(parent_objectid,
1101 victim_name,
1102 victim_name_len);
1103 ret = 0;
1104 if (!backref_in_log(log_root, &search_key,
1105 parent_objectid, victim_name,
1106 victim_name_len)) {
1107 ret = -ENOENT;
1108 victim_parent = read_one_inode(root,
1109 parent_objectid);
1110 if (victim_parent) {
1111 inc_nlink(&inode->vfs_inode);
1112 btrfs_release_path(path);
1114 ret = btrfs_unlink_inode(trans, root,
1115 BTRFS_I(victim_parent),
1116 inode,
1117 victim_name,
1118 victim_name_len);
1119 if (!ret)
1120 ret = btrfs_run_delayed_items(
1121 trans);
1123 iput(victim_parent);
1124 kfree(victim_name);
1125 if (ret)
1126 return ret;
1127 *search_done = 1;
1128 goto again;
1130 kfree(victim_name);
1131 next:
1132 cur_offset += victim_name_len + sizeof(*extref);
1134 *search_done = 1;
1136 btrfs_release_path(path);
1138 /* look for a conflicting sequence number */
1139 di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
1140 ref_index, name, namelen, 0);
1141 if (di && !IS_ERR(di)) {
1142 ret = drop_one_dir_item(trans, root, path, dir, di);
1143 if (ret)
1144 return ret;
1146 btrfs_release_path(path);
1148 /* look for a conflicing name */
1149 di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
1150 name, namelen, 0);
1151 if (di && !IS_ERR(di)) {
1152 ret = drop_one_dir_item(trans, root, path, dir, di);
1153 if (ret)
1154 return ret;
1156 btrfs_release_path(path);
1158 return 0;
1161 static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1162 u32 *namelen, char **name, u64 *index,
1163 u64 *parent_objectid)
1165 struct btrfs_inode_extref *extref;
1167 extref = (struct btrfs_inode_extref *)ref_ptr;
1169 *namelen = btrfs_inode_extref_name_len(eb, extref);
1170 *name = kmalloc(*namelen, GFP_NOFS);
1171 if (*name == NULL)
1172 return -ENOMEM;
1174 read_extent_buffer(eb, *name, (unsigned long)&extref->name,
1175 *namelen);
1177 if (index)
1178 *index = btrfs_inode_extref_index(eb, extref);
1179 if (parent_objectid)
1180 *parent_objectid = btrfs_inode_extref_parent(eb, extref);
1182 return 0;
1185 static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
1186 u32 *namelen, char **name, u64 *index)
1188 struct btrfs_inode_ref *ref;
1190 ref = (struct btrfs_inode_ref *)ref_ptr;
1192 *namelen = btrfs_inode_ref_name_len(eb, ref);
1193 *name = kmalloc(*namelen, GFP_NOFS);
1194 if (*name == NULL)
1195 return -ENOMEM;
1197 read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
1199 if (index)
1200 *index = btrfs_inode_ref_index(eb, ref);
1202 return 0;
1206 * Take an inode reference item from the log tree and iterate all names from the
1207 * inode reference item in the subvolume tree with the same key (if it exists).
1208 * For any name that is not in the inode reference item from the log tree, do a
1209 * proper unlink of that name (that is, remove its entry from the inode
1210 * reference item and both dir index keys).
1212 static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
1213 struct btrfs_root *root,
1214 struct btrfs_path *path,
1215 struct btrfs_inode *inode,
1216 struct extent_buffer *log_eb,
1217 int log_slot,
1218 struct btrfs_key *key)
1220 int ret;
1221 unsigned long ref_ptr;
1222 unsigned long ref_end;
1223 struct extent_buffer *eb;
1225 again:
1226 btrfs_release_path(path);
1227 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
1228 if (ret > 0) {
1229 ret = 0;
1230 goto out;
1232 if (ret < 0)
1233 goto out;
1235 eb = path->nodes[0];
1236 ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
1237 ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
1238 while (ref_ptr < ref_end) {
1239 char *name = NULL;
1240 int namelen;
1241 u64 parent_id;
1243 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1244 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1245 NULL, &parent_id);
1246 } else {
1247 parent_id = key->offset;
1248 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1249 NULL);
1251 if (ret)
1252 goto out;
1254 if (key->type == BTRFS_INODE_EXTREF_KEY)
1255 ret = btrfs_find_name_in_ext_backref(log_eb, log_slot,
1256 parent_id, name,
1257 namelen, NULL);
1258 else
1259 ret = btrfs_find_name_in_backref(log_eb, log_slot, name,
1260 namelen, NULL);
1262 if (!ret) {
1263 struct inode *dir;
1265 btrfs_release_path(path);
1266 dir = read_one_inode(root, parent_id);
1267 if (!dir) {
1268 ret = -ENOENT;
1269 kfree(name);
1270 goto out;
1272 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
1273 inode, name, namelen);
1274 kfree(name);
1275 iput(dir);
1276 if (ret)
1277 goto out;
1278 goto again;
1281 kfree(name);
1282 ref_ptr += namelen;
1283 if (key->type == BTRFS_INODE_EXTREF_KEY)
1284 ref_ptr += sizeof(struct btrfs_inode_extref);
1285 else
1286 ref_ptr += sizeof(struct btrfs_inode_ref);
1288 ret = 0;
1289 out:
1290 btrfs_release_path(path);
1291 return ret;
1294 static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
1295 const u8 ref_type, const char *name,
1296 const int namelen)
1298 struct btrfs_key key;
1299 struct btrfs_path *path;
1300 const u64 parent_id = btrfs_ino(BTRFS_I(dir));
1301 int ret;
1303 path = btrfs_alloc_path();
1304 if (!path)
1305 return -ENOMEM;
1307 key.objectid = btrfs_ino(BTRFS_I(inode));
1308 key.type = ref_type;
1309 if (key.type == BTRFS_INODE_REF_KEY)
1310 key.offset = parent_id;
1311 else
1312 key.offset = btrfs_extref_hash(parent_id, name, namelen);
1314 ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
1315 if (ret < 0)
1316 goto out;
1317 if (ret > 0) {
1318 ret = 0;
1319 goto out;
1321 if (key.type == BTRFS_INODE_EXTREF_KEY)
1322 ret = btrfs_find_name_in_ext_backref(path->nodes[0],
1323 path->slots[0], parent_id,
1324 name, namelen, NULL);
1325 else
1326 ret = btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
1327 name, namelen, NULL);
1329 out:
1330 btrfs_free_path(path);
1331 return ret;
1335 * replay one inode back reference item found in the log tree.
1336 * eb, slot and key refer to the buffer and key found in the log tree.
1337 * root is the destination we are replaying into, and path is for temp
1338 * use by this function. (it should be released on return).
1340 static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
1341 struct btrfs_root *root,
1342 struct btrfs_root *log,
1343 struct btrfs_path *path,
1344 struct extent_buffer *eb, int slot,
1345 struct btrfs_key *key)
1347 struct inode *dir = NULL;
1348 struct inode *inode = NULL;
1349 unsigned long ref_ptr;
1350 unsigned long ref_end;
1351 char *name = NULL;
1352 int namelen;
1353 int ret;
1354 int search_done = 0;
1355 int log_ref_ver = 0;
1356 u64 parent_objectid;
1357 u64 inode_objectid;
1358 u64 ref_index = 0;
1359 int ref_struct_size;
1361 ref_ptr = btrfs_item_ptr_offset(eb, slot);
1362 ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
1364 if (key->type == BTRFS_INODE_EXTREF_KEY) {
1365 struct btrfs_inode_extref *r;
1367 ref_struct_size = sizeof(struct btrfs_inode_extref);
1368 log_ref_ver = 1;
1369 r = (struct btrfs_inode_extref *)ref_ptr;
1370 parent_objectid = btrfs_inode_extref_parent(eb, r);
1371 } else {
1372 ref_struct_size = sizeof(struct btrfs_inode_ref);
1373 parent_objectid = key->offset;
1375 inode_objectid = key->objectid;
1378 * it is possible that we didn't log all the parent directories
1379 * for a given inode. If we don't find the dir, just don't
1380 * copy the back ref in. The link count fixup code will take
1381 * care of the rest
1383 dir = read_one_inode(root, parent_objectid);
1384 if (!dir) {
1385 ret = -ENOENT;
1386 goto out;
1389 inode = read_one_inode(root, inode_objectid);
1390 if (!inode) {
1391 ret = -EIO;
1392 goto out;
1395 while (ref_ptr < ref_end) {
1396 if (log_ref_ver) {
1397 ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
1398 &ref_index, &parent_objectid);
1400 * parent object can change from one array
1401 * item to another.
1403 if (!dir)
1404 dir = read_one_inode(root, parent_objectid);
1405 if (!dir) {
1406 ret = -ENOENT;
1407 goto out;
1409 } else {
1410 ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
1411 &ref_index);
1413 if (ret)
1414 goto out;
1416 /* if we already have a perfect match, we're done */
1417 if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
1418 btrfs_ino(BTRFS_I(inode)), ref_index,
1419 name, namelen)) {
1421 * look for a conflicting back reference in the
1422 * metadata. if we find one we have to unlink that name
1423 * of the file before we add our new link. Later on, we
1424 * overwrite any existing back reference, and we don't
1425 * want to create dangling pointers in the directory.
1428 if (!search_done) {
1429 ret = __add_inode_ref(trans, root, path, log,
1430 BTRFS_I(dir),
1431 BTRFS_I(inode),
1432 inode_objectid,
1433 parent_objectid,
1434 ref_index, name, namelen,
1435 &search_done);
1436 if (ret) {
1437 if (ret == 1)
1438 ret = 0;
1439 goto out;
1444 * If a reference item already exists for this inode
1445 * with the same parent and name, but different index,
1446 * drop it and the corresponding directory index entries
1447 * from the parent before adding the new reference item
1448 * and dir index entries, otherwise we would fail with
1449 * -EEXIST returned from btrfs_add_link() below.
1451 ret = btrfs_inode_ref_exists(inode, dir, key->type,
1452 name, namelen);
1453 if (ret > 0) {
1454 ret = btrfs_unlink_inode(trans, root,
1455 BTRFS_I(dir),
1456 BTRFS_I(inode),
1457 name, namelen);
1459 * If we dropped the link count to 0, bump it so
1460 * that later the iput() on the inode will not
1461 * free it. We will fixup the link count later.
1463 if (!ret && inode->i_nlink == 0)
1464 inc_nlink(inode);
1466 if (ret < 0)
1467 goto out;
1469 /* insert our name */
1470 ret = btrfs_add_link(trans, BTRFS_I(dir),
1471 BTRFS_I(inode),
1472 name, namelen, 0, ref_index);
1473 if (ret)
1474 goto out;
1476 btrfs_update_inode(trans, root, inode);
1479 ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
1480 kfree(name);
1481 name = NULL;
1482 if (log_ref_ver) {
1483 iput(dir);
1484 dir = NULL;
1489 * Before we overwrite the inode reference item in the subvolume tree
1490 * with the item from the log tree, we must unlink all names from the
1491 * parent directory that are in the subvolume's tree inode reference
1492 * item, otherwise we end up with an inconsistent subvolume tree where
1493 * dir index entries exist for a name but there is no inode reference
1494 * item with the same name.
1496 ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
1497 key);
1498 if (ret)
1499 goto out;
1501 /* finally write the back reference in the inode */
1502 ret = overwrite_item(trans, root, path, eb, slot, key);
1503 out:
1504 btrfs_release_path(path);
1505 kfree(name);
1506 iput(dir);
1507 iput(inode);
1508 return ret;
1511 static int insert_orphan_item(struct btrfs_trans_handle *trans,
1512 struct btrfs_root *root, u64 ino)
1514 int ret;
1516 ret = btrfs_insert_orphan_item(trans, root, ino);
1517 if (ret == -EEXIST)
1518 ret = 0;
1520 return ret;
1523 static int count_inode_extrefs(struct btrfs_root *root,
1524 struct btrfs_inode *inode, struct btrfs_path *path)
1526 int ret = 0;
1527 int name_len;
1528 unsigned int nlink = 0;
1529 u32 item_size;
1530 u32 cur_offset = 0;
1531 u64 inode_objectid = btrfs_ino(inode);
1532 u64 offset = 0;
1533 unsigned long ptr;
1534 struct btrfs_inode_extref *extref;
1535 struct extent_buffer *leaf;
1537 while (1) {
1538 ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
1539 &extref, &offset);
1540 if (ret)
1541 break;
1543 leaf = path->nodes[0];
1544 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1545 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
1546 cur_offset = 0;
1548 while (cur_offset < item_size) {
1549 extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
1550 name_len = btrfs_inode_extref_name_len(leaf, extref);
1552 nlink++;
1554 cur_offset += name_len + sizeof(*extref);
1557 offset++;
1558 btrfs_release_path(path);
1560 btrfs_release_path(path);
1562 if (ret < 0 && ret != -ENOENT)
1563 return ret;
1564 return nlink;
1567 static int count_inode_refs(struct btrfs_root *root,
1568 struct btrfs_inode *inode, struct btrfs_path *path)
1570 int ret;
1571 struct btrfs_key key;
1572 unsigned int nlink = 0;
1573 unsigned long ptr;
1574 unsigned long ptr_end;
1575 int name_len;
1576 u64 ino = btrfs_ino(inode);
1578 key.objectid = ino;
1579 key.type = BTRFS_INODE_REF_KEY;
1580 key.offset = (u64)-1;
1582 while (1) {
1583 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1584 if (ret < 0)
1585 break;
1586 if (ret > 0) {
1587 if (path->slots[0] == 0)
1588 break;
1589 path->slots[0]--;
1591 process_slot:
1592 btrfs_item_key_to_cpu(path->nodes[0], &key,
1593 path->slots[0]);
1594 if (key.objectid != ino ||
1595 key.type != BTRFS_INODE_REF_KEY)
1596 break;
1597 ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
1598 ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
1599 path->slots[0]);
1600 while (ptr < ptr_end) {
1601 struct btrfs_inode_ref *ref;
1603 ref = (struct btrfs_inode_ref *)ptr;
1604 name_len = btrfs_inode_ref_name_len(path->nodes[0],
1605 ref);
1606 ptr = (unsigned long)(ref + 1) + name_len;
1607 nlink++;
1610 if (key.offset == 0)
1611 break;
1612 if (path->slots[0] > 0) {
1613 path->slots[0]--;
1614 goto process_slot;
1616 key.offset--;
1617 btrfs_release_path(path);
1619 btrfs_release_path(path);
1621 return nlink;
1625 * There are a few corners where the link count of the file can't
1626 * be properly maintained during replay. So, instead of adding
1627 * lots of complexity to the log code, we just scan the backrefs
1628 * for any file that has been through replay.
1630 * The scan will update the link count on the inode to reflect the
1631 * number of back refs found. If it goes down to zero, the iput
1632 * will free the inode.
1634 static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
1635 struct btrfs_root *root,
1636 struct inode *inode)
1638 struct btrfs_path *path;
1639 int ret;
1640 u64 nlink = 0;
1641 u64 ino = btrfs_ino(BTRFS_I(inode));
1643 path = btrfs_alloc_path();
1644 if (!path)
1645 return -ENOMEM;
1647 ret = count_inode_refs(root, BTRFS_I(inode), path);
1648 if (ret < 0)
1649 goto out;
1651 nlink = ret;
1653 ret = count_inode_extrefs(root, BTRFS_I(inode), path);
1654 if (ret < 0)
1655 goto out;
1657 nlink += ret;
1659 ret = 0;
1661 if (nlink != inode->i_nlink) {
1662 set_nlink(inode, nlink);
1663 btrfs_update_inode(trans, root, inode);
1665 BTRFS_I(inode)->index_cnt = (u64)-1;
1667 if (inode->i_nlink == 0) {
1668 if (S_ISDIR(inode->i_mode)) {
1669 ret = replay_dir_deletes(trans, root, NULL, path,
1670 ino, 1);
1671 if (ret)
1672 goto out;
1674 ret = insert_orphan_item(trans, root, ino);
1677 out:
1678 btrfs_free_path(path);
1679 return ret;
1682 static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
1683 struct btrfs_root *root,
1684 struct btrfs_path *path)
1686 int ret;
1687 struct btrfs_key key;
1688 struct inode *inode;
1690 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1691 key.type = BTRFS_ORPHAN_ITEM_KEY;
1692 key.offset = (u64)-1;
1693 while (1) {
1694 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1695 if (ret < 0)
1696 break;
1698 if (ret == 1) {
1699 if (path->slots[0] == 0)
1700 break;
1701 path->slots[0]--;
1704 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1705 if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
1706 key.type != BTRFS_ORPHAN_ITEM_KEY)
1707 break;
1709 ret = btrfs_del_item(trans, root, path);
1710 if (ret)
1711 goto out;
1713 btrfs_release_path(path);
1714 inode = read_one_inode(root, key.offset);
1715 if (!inode)
1716 return -EIO;
1718 ret = fixup_inode_link_count(trans, root, inode);
1719 iput(inode);
1720 if (ret)
1721 goto out;
1724 * fixup on a directory may create new entries,
1725 * make sure we always look for the highset possible
1726 * offset
1728 key.offset = (u64)-1;
1730 ret = 0;
1731 out:
1732 btrfs_release_path(path);
1733 return ret;
1738 * record a given inode in the fixup dir so we can check its link
1739 * count when replay is done. The link count is incremented here
1740 * so the inode won't go away until we check it
1742 static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
1743 struct btrfs_root *root,
1744 struct btrfs_path *path,
1745 u64 objectid)
1747 struct btrfs_key key;
1748 int ret = 0;
1749 struct inode *inode;
1751 inode = read_one_inode(root, objectid);
1752 if (!inode)
1753 return -EIO;
1755 key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
1756 key.type = BTRFS_ORPHAN_ITEM_KEY;
1757 key.offset = objectid;
1759 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1761 btrfs_release_path(path);
1762 if (ret == 0) {
1763 if (!inode->i_nlink)
1764 set_nlink(inode, 1);
1765 else
1766 inc_nlink(inode);
1767 ret = btrfs_update_inode(trans, root, inode);
1768 } else if (ret == -EEXIST) {
1769 ret = 0;
1770 } else {
1771 BUG(); /* Logic Error */
1773 iput(inode);
1775 return ret;
1779 * when replaying the log for a directory, we only insert names
1780 * for inodes that actually exist. This means an fsync on a directory
1781 * does not implicitly fsync all the new files in it
1783 static noinline int insert_one_name(struct btrfs_trans_handle *trans,
1784 struct btrfs_root *root,
1785 u64 dirid, u64 index,
1786 char *name, int name_len,
1787 struct btrfs_key *location)
1789 struct inode *inode;
1790 struct inode *dir;
1791 int ret;
1793 inode = read_one_inode(root, location->objectid);
1794 if (!inode)
1795 return -ENOENT;
1797 dir = read_one_inode(root, dirid);
1798 if (!dir) {
1799 iput(inode);
1800 return -EIO;
1803 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
1804 name_len, 1, index);
1806 /* FIXME, put inode into FIXUP list */
1808 iput(inode);
1809 iput(dir);
1810 return ret;
1814 * Return true if an inode reference exists in the log for the given name,
1815 * inode and parent inode.
1817 static bool name_in_log_ref(struct btrfs_root *log_root,
1818 const char *name, const int name_len,
1819 const u64 dirid, const u64 ino)
1821 struct btrfs_key search_key;
1823 search_key.objectid = ino;
1824 search_key.type = BTRFS_INODE_REF_KEY;
1825 search_key.offset = dirid;
1826 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1827 return true;
1829 search_key.type = BTRFS_INODE_EXTREF_KEY;
1830 search_key.offset = btrfs_extref_hash(dirid, name, name_len);
1831 if (backref_in_log(log_root, &search_key, dirid, name, name_len))
1832 return true;
1834 return false;
1838 * take a single entry in a log directory item and replay it into
1839 * the subvolume.
1841 * if a conflicting item exists in the subdirectory already,
1842 * the inode it points to is unlinked and put into the link count
1843 * fix up tree.
1845 * If a name from the log points to a file or directory that does
1846 * not exist in the FS, it is skipped. fsyncs on directories
1847 * do not force down inodes inside that directory, just changes to the
1848 * names or unlinks in a directory.
1850 * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
1851 * non-existing inode) and 1 if the name was replayed.
1853 static noinline int replay_one_name(struct btrfs_trans_handle *trans,
1854 struct btrfs_root *root,
1855 struct btrfs_path *path,
1856 struct extent_buffer *eb,
1857 struct btrfs_dir_item *di,
1858 struct btrfs_key *key)
1860 char *name;
1861 int name_len;
1862 struct btrfs_dir_item *dst_di;
1863 struct btrfs_key found_key;
1864 struct btrfs_key log_key;
1865 struct inode *dir;
1866 u8 log_type;
1867 int exists;
1868 int ret = 0;
1869 bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
1870 bool name_added = false;
1872 dir = read_one_inode(root, key->objectid);
1873 if (!dir)
1874 return -EIO;
1876 name_len = btrfs_dir_name_len(eb, di);
1877 name = kmalloc(name_len, GFP_NOFS);
1878 if (!name) {
1879 ret = -ENOMEM;
1880 goto out;
1883 log_type = btrfs_dir_type(eb, di);
1884 read_extent_buffer(eb, name, (unsigned long)(di + 1),
1885 name_len);
1887 btrfs_dir_item_key_to_cpu(eb, di, &log_key);
1888 exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
1889 if (exists == 0)
1890 exists = 1;
1891 else
1892 exists = 0;
1893 btrfs_release_path(path);
1895 if (key->type == BTRFS_DIR_ITEM_KEY) {
1896 dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
1897 name, name_len, 1);
1898 } else if (key->type == BTRFS_DIR_INDEX_KEY) {
1899 dst_di = btrfs_lookup_dir_index_item(trans, root, path,
1900 key->objectid,
1901 key->offset, name,
1902 name_len, 1);
1903 } else {
1904 /* Corruption */
1905 ret = -EINVAL;
1906 goto out;
1908 if (IS_ERR_OR_NULL(dst_di)) {
1909 /* we need a sequence number to insert, so we only
1910 * do inserts for the BTRFS_DIR_INDEX_KEY types
1912 if (key->type != BTRFS_DIR_INDEX_KEY)
1913 goto out;
1914 goto insert;
1917 btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
1918 /* the existing item matches the logged item */
1919 if (found_key.objectid == log_key.objectid &&
1920 found_key.type == log_key.type &&
1921 found_key.offset == log_key.offset &&
1922 btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
1923 update_size = false;
1924 goto out;
1928 * don't drop the conflicting directory entry if the inode
1929 * for the new entry doesn't exist
1931 if (!exists)
1932 goto out;
1934 ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
1935 if (ret)
1936 goto out;
1938 if (key->type == BTRFS_DIR_INDEX_KEY)
1939 goto insert;
1940 out:
1941 btrfs_release_path(path);
1942 if (!ret && update_size) {
1943 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
1944 ret = btrfs_update_inode(trans, root, dir);
1946 kfree(name);
1947 iput(dir);
1948 if (!ret && name_added)
1949 ret = 1;
1950 return ret;
1952 insert:
1953 if (name_in_log_ref(root->log_root, name, name_len,
1954 key->objectid, log_key.objectid)) {
1955 /* The dentry will be added later. */
1956 ret = 0;
1957 update_size = false;
1958 goto out;
1960 btrfs_release_path(path);
1961 ret = insert_one_name(trans, root, key->objectid, key->offset,
1962 name, name_len, &log_key);
1963 if (ret && ret != -ENOENT && ret != -EEXIST)
1964 goto out;
1965 if (!ret)
1966 name_added = true;
1967 update_size = false;
1968 ret = 0;
1969 goto out;
1973 * find all the names in a directory item and reconcile them into
1974 * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
1975 * one name in a directory item, but the same code gets used for
1976 * both directory index types
1978 static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1979 struct btrfs_root *root,
1980 struct btrfs_path *path,
1981 struct extent_buffer *eb, int slot,
1982 struct btrfs_key *key)
1984 int ret = 0;
1985 u32 item_size = btrfs_item_size_nr(eb, slot);
1986 struct btrfs_dir_item *di;
1987 int name_len;
1988 unsigned long ptr;
1989 unsigned long ptr_end;
1990 struct btrfs_path *fixup_path = NULL;
1992 ptr = btrfs_item_ptr_offset(eb, slot);
1993 ptr_end = ptr + item_size;
1994 while (ptr < ptr_end) {
1995 di = (struct btrfs_dir_item *)ptr;
1996 name_len = btrfs_dir_name_len(eb, di);
1997 ret = replay_one_name(trans, root, path, eb, di, key);
1998 if (ret < 0)
1999 break;
2000 ptr = (unsigned long)(di + 1);
2001 ptr += name_len;
2004 * If this entry refers to a non-directory (directories can not
2005 * have a link count > 1) and it was added in the transaction
2006 * that was not committed, make sure we fixup the link count of
2007 * the inode it the entry points to. Otherwise something like
2008 * the following would result in a directory pointing to an
2009 * inode with a wrong link that does not account for this dir
2010 * entry:
2012 * mkdir testdir
2013 * touch testdir/foo
2014 * touch testdir/bar
2015 * sync
2017 * ln testdir/bar testdir/bar_link
2018 * ln testdir/foo testdir/foo_link
2019 * xfs_io -c "fsync" testdir/bar
2021 * <power failure>
2023 * mount fs, log replay happens
2025 * File foo would remain with a link count of 1 when it has two
2026 * entries pointing to it in the directory testdir. This would
2027 * make it impossible to ever delete the parent directory has
2028 * it would result in stale dentries that can never be deleted.
2030 if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
2031 struct btrfs_key di_key;
2033 if (!fixup_path) {
2034 fixup_path = btrfs_alloc_path();
2035 if (!fixup_path) {
2036 ret = -ENOMEM;
2037 break;
2041 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2042 ret = link_to_fixup_dir(trans, root, fixup_path,
2043 di_key.objectid);
2044 if (ret)
2045 break;
2047 ret = 0;
2049 btrfs_free_path(fixup_path);
2050 return ret;
2054 * directory replay has two parts. There are the standard directory
2055 * items in the log copied from the subvolume, and range items
2056 * created in the log while the subvolume was logged.
2058 * The range items tell us which parts of the key space the log
2059 * is authoritative for. During replay, if a key in the subvolume
2060 * directory is in a logged range item, but not actually in the log
2061 * that means it was deleted from the directory before the fsync
2062 * and should be removed.
2064 static noinline int find_dir_range(struct btrfs_root *root,
2065 struct btrfs_path *path,
2066 u64 dirid, int key_type,
2067 u64 *start_ret, u64 *end_ret)
2069 struct btrfs_key key;
2070 u64 found_end;
2071 struct btrfs_dir_log_item *item;
2072 int ret;
2073 int nritems;
2075 if (*start_ret == (u64)-1)
2076 return 1;
2078 key.objectid = dirid;
2079 key.type = key_type;
2080 key.offset = *start_ret;
2082 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2083 if (ret < 0)
2084 goto out;
2085 if (ret > 0) {
2086 if (path->slots[0] == 0)
2087 goto out;
2088 path->slots[0]--;
2090 if (ret != 0)
2091 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2093 if (key.type != key_type || key.objectid != dirid) {
2094 ret = 1;
2095 goto next;
2097 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2098 struct btrfs_dir_log_item);
2099 found_end = btrfs_dir_log_end(path->nodes[0], item);
2101 if (*start_ret >= key.offset && *start_ret <= found_end) {
2102 ret = 0;
2103 *start_ret = key.offset;
2104 *end_ret = found_end;
2105 goto out;
2107 ret = 1;
2108 next:
2109 /* check the next slot in the tree to see if it is a valid item */
2110 nritems = btrfs_header_nritems(path->nodes[0]);
2111 path->slots[0]++;
2112 if (path->slots[0] >= nritems) {
2113 ret = btrfs_next_leaf(root, path);
2114 if (ret)
2115 goto out;
2118 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2120 if (key.type != key_type || key.objectid != dirid) {
2121 ret = 1;
2122 goto out;
2124 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2125 struct btrfs_dir_log_item);
2126 found_end = btrfs_dir_log_end(path->nodes[0], item);
2127 *start_ret = key.offset;
2128 *end_ret = found_end;
2129 ret = 0;
2130 out:
2131 btrfs_release_path(path);
2132 return ret;
2136 * this looks for a given directory item in the log. If the directory
2137 * item is not in the log, the item is removed and the inode it points
2138 * to is unlinked
2140 static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
2141 struct btrfs_root *root,
2142 struct btrfs_root *log,
2143 struct btrfs_path *path,
2144 struct btrfs_path *log_path,
2145 struct inode *dir,
2146 struct btrfs_key *dir_key)
2148 int ret;
2149 struct extent_buffer *eb;
2150 int slot;
2151 u32 item_size;
2152 struct btrfs_dir_item *di;
2153 struct btrfs_dir_item *log_di;
2154 int name_len;
2155 unsigned long ptr;
2156 unsigned long ptr_end;
2157 char *name;
2158 struct inode *inode;
2159 struct btrfs_key location;
2161 again:
2162 eb = path->nodes[0];
2163 slot = path->slots[0];
2164 item_size = btrfs_item_size_nr(eb, slot);
2165 ptr = btrfs_item_ptr_offset(eb, slot);
2166 ptr_end = ptr + item_size;
2167 while (ptr < ptr_end) {
2168 di = (struct btrfs_dir_item *)ptr;
2169 name_len = btrfs_dir_name_len(eb, di);
2170 name = kmalloc(name_len, GFP_NOFS);
2171 if (!name) {
2172 ret = -ENOMEM;
2173 goto out;
2175 read_extent_buffer(eb, name, (unsigned long)(di + 1),
2176 name_len);
2177 log_di = NULL;
2178 if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
2179 log_di = btrfs_lookup_dir_item(trans, log, log_path,
2180 dir_key->objectid,
2181 name, name_len, 0);
2182 } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
2183 log_di = btrfs_lookup_dir_index_item(trans, log,
2184 log_path,
2185 dir_key->objectid,
2186 dir_key->offset,
2187 name, name_len, 0);
2189 if (!log_di || (IS_ERR(log_di) && PTR_ERR(log_di) == -ENOENT)) {
2190 btrfs_dir_item_key_to_cpu(eb, di, &location);
2191 btrfs_release_path(path);
2192 btrfs_release_path(log_path);
2193 inode = read_one_inode(root, location.objectid);
2194 if (!inode) {
2195 kfree(name);
2196 return -EIO;
2199 ret = link_to_fixup_dir(trans, root,
2200 path, location.objectid);
2201 if (ret) {
2202 kfree(name);
2203 iput(inode);
2204 goto out;
2207 inc_nlink(inode);
2208 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
2209 BTRFS_I(inode), name, name_len);
2210 if (!ret)
2211 ret = btrfs_run_delayed_items(trans);
2212 kfree(name);
2213 iput(inode);
2214 if (ret)
2215 goto out;
2217 /* there might still be more names under this key
2218 * check and repeat if required
2220 ret = btrfs_search_slot(NULL, root, dir_key, path,
2221 0, 0);
2222 if (ret == 0)
2223 goto again;
2224 ret = 0;
2225 goto out;
2226 } else if (IS_ERR(log_di)) {
2227 kfree(name);
2228 return PTR_ERR(log_di);
2230 btrfs_release_path(log_path);
2231 kfree(name);
2233 ptr = (unsigned long)(di + 1);
2234 ptr += name_len;
2236 ret = 0;
2237 out:
2238 btrfs_release_path(path);
2239 btrfs_release_path(log_path);
2240 return ret;
2243 static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
2244 struct btrfs_root *root,
2245 struct btrfs_root *log,
2246 struct btrfs_path *path,
2247 const u64 ino)
2249 struct btrfs_key search_key;
2250 struct btrfs_path *log_path;
2251 int i;
2252 int nritems;
2253 int ret;
2255 log_path = btrfs_alloc_path();
2256 if (!log_path)
2257 return -ENOMEM;
2259 search_key.objectid = ino;
2260 search_key.type = BTRFS_XATTR_ITEM_KEY;
2261 search_key.offset = 0;
2262 again:
2263 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
2264 if (ret < 0)
2265 goto out;
2266 process_leaf:
2267 nritems = btrfs_header_nritems(path->nodes[0]);
2268 for (i = path->slots[0]; i < nritems; i++) {
2269 struct btrfs_key key;
2270 struct btrfs_dir_item *di;
2271 struct btrfs_dir_item *log_di;
2272 u32 total_size;
2273 u32 cur;
2275 btrfs_item_key_to_cpu(path->nodes[0], &key, i);
2276 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
2277 ret = 0;
2278 goto out;
2281 di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
2282 total_size = btrfs_item_size_nr(path->nodes[0], i);
2283 cur = 0;
2284 while (cur < total_size) {
2285 u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
2286 u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
2287 u32 this_len = sizeof(*di) + name_len + data_len;
2288 char *name;
2290 name = kmalloc(name_len, GFP_NOFS);
2291 if (!name) {
2292 ret = -ENOMEM;
2293 goto out;
2295 read_extent_buffer(path->nodes[0], name,
2296 (unsigned long)(di + 1), name_len);
2298 log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
2299 name, name_len, 0);
2300 btrfs_release_path(log_path);
2301 if (!log_di) {
2302 /* Doesn't exist in log tree, so delete it. */
2303 btrfs_release_path(path);
2304 di = btrfs_lookup_xattr(trans, root, path, ino,
2305 name, name_len, -1);
2306 kfree(name);
2307 if (IS_ERR(di)) {
2308 ret = PTR_ERR(di);
2309 goto out;
2311 ASSERT(di);
2312 ret = btrfs_delete_one_dir_name(trans, root,
2313 path, di);
2314 if (ret)
2315 goto out;
2316 btrfs_release_path(path);
2317 search_key = key;
2318 goto again;
2320 kfree(name);
2321 if (IS_ERR(log_di)) {
2322 ret = PTR_ERR(log_di);
2323 goto out;
2325 cur += this_len;
2326 di = (struct btrfs_dir_item *)((char *)di + this_len);
2329 ret = btrfs_next_leaf(root, path);
2330 if (ret > 0)
2331 ret = 0;
2332 else if (ret == 0)
2333 goto process_leaf;
2334 out:
2335 btrfs_free_path(log_path);
2336 btrfs_release_path(path);
2337 return ret;
2342 * deletion replay happens before we copy any new directory items
2343 * out of the log or out of backreferences from inodes. It
2344 * scans the log to find ranges of keys that log is authoritative for,
2345 * and then scans the directory to find items in those ranges that are
2346 * not present in the log.
2348 * Anything we don't find in the log is unlinked and removed from the
2349 * directory.
2351 static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
2352 struct btrfs_root *root,
2353 struct btrfs_root *log,
2354 struct btrfs_path *path,
2355 u64 dirid, int del_all)
2357 u64 range_start;
2358 u64 range_end;
2359 int key_type = BTRFS_DIR_LOG_ITEM_KEY;
2360 int ret = 0;
2361 struct btrfs_key dir_key;
2362 struct btrfs_key found_key;
2363 struct btrfs_path *log_path;
2364 struct inode *dir;
2366 dir_key.objectid = dirid;
2367 dir_key.type = BTRFS_DIR_ITEM_KEY;
2368 log_path = btrfs_alloc_path();
2369 if (!log_path)
2370 return -ENOMEM;
2372 dir = read_one_inode(root, dirid);
2373 /* it isn't an error if the inode isn't there, that can happen
2374 * because we replay the deletes before we copy in the inode item
2375 * from the log
2377 if (!dir) {
2378 btrfs_free_path(log_path);
2379 return 0;
2381 again:
2382 range_start = 0;
2383 range_end = 0;
2384 while (1) {
2385 if (del_all)
2386 range_end = (u64)-1;
2387 else {
2388 ret = find_dir_range(log, path, dirid, key_type,
2389 &range_start, &range_end);
2390 if (ret != 0)
2391 break;
2394 dir_key.offset = range_start;
2395 while (1) {
2396 int nritems;
2397 ret = btrfs_search_slot(NULL, root, &dir_key, path,
2398 0, 0);
2399 if (ret < 0)
2400 goto out;
2402 nritems = btrfs_header_nritems(path->nodes[0]);
2403 if (path->slots[0] >= nritems) {
2404 ret = btrfs_next_leaf(root, path);
2405 if (ret == 1)
2406 break;
2407 else if (ret < 0)
2408 goto out;
2410 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2411 path->slots[0]);
2412 if (found_key.objectid != dirid ||
2413 found_key.type != dir_key.type)
2414 goto next_type;
2416 if (found_key.offset > range_end)
2417 break;
2419 ret = check_item_in_log(trans, root, log, path,
2420 log_path, dir,
2421 &found_key);
2422 if (ret)
2423 goto out;
2424 if (found_key.offset == (u64)-1)
2425 break;
2426 dir_key.offset = found_key.offset + 1;
2428 btrfs_release_path(path);
2429 if (range_end == (u64)-1)
2430 break;
2431 range_start = range_end + 1;
2434 next_type:
2435 ret = 0;
2436 if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
2437 key_type = BTRFS_DIR_LOG_INDEX_KEY;
2438 dir_key.type = BTRFS_DIR_INDEX_KEY;
2439 btrfs_release_path(path);
2440 goto again;
2442 out:
2443 btrfs_release_path(path);
2444 btrfs_free_path(log_path);
2445 iput(dir);
2446 return ret;
2450 * the process_func used to replay items from the log tree. This
2451 * gets called in two different stages. The first stage just looks
2452 * for inodes and makes sure they are all copied into the subvolume.
2454 * The second stage copies all the other item types from the log into
2455 * the subvolume. The two stage approach is slower, but gets rid of
2456 * lots of complexity around inodes referencing other inodes that exist
2457 * only in the log (references come from either directory items or inode
2458 * back refs).
2460 static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
2461 struct walk_control *wc, u64 gen, int level)
2463 int nritems;
2464 struct btrfs_path *path;
2465 struct btrfs_root *root = wc->replay_dest;
2466 struct btrfs_key key;
2467 int i;
2468 int ret;
2470 ret = btrfs_read_buffer(eb, gen, level, NULL);
2471 if (ret)
2472 return ret;
2474 level = btrfs_header_level(eb);
2476 if (level != 0)
2477 return 0;
2479 path = btrfs_alloc_path();
2480 if (!path)
2481 return -ENOMEM;
2483 nritems = btrfs_header_nritems(eb);
2484 for (i = 0; i < nritems; i++) {
2485 btrfs_item_key_to_cpu(eb, &key, i);
2487 /* inode keys are done during the first stage */
2488 if (key.type == BTRFS_INODE_ITEM_KEY &&
2489 wc->stage == LOG_WALK_REPLAY_INODES) {
2490 struct btrfs_inode_item *inode_item;
2491 u32 mode;
2493 inode_item = btrfs_item_ptr(eb, i,
2494 struct btrfs_inode_item);
2495 ret = replay_xattr_deletes(wc->trans, root, log,
2496 path, key.objectid);
2497 if (ret)
2498 break;
2499 mode = btrfs_inode_mode(eb, inode_item);
2500 if (S_ISDIR(mode)) {
2501 ret = replay_dir_deletes(wc->trans,
2502 root, log, path, key.objectid, 0);
2503 if (ret)
2504 break;
2506 ret = overwrite_item(wc->trans, root, path,
2507 eb, i, &key);
2508 if (ret)
2509 break;
2512 * Before replaying extents, truncate the inode to its
2513 * size. We need to do it now and not after log replay
2514 * because before an fsync we can have prealloc extents
2515 * added beyond the inode's i_size. If we did it after,
2516 * through orphan cleanup for example, we would drop
2517 * those prealloc extents just after replaying them.
2519 if (S_ISREG(mode)) {
2520 struct inode *inode;
2521 u64 from;
2523 inode = read_one_inode(root, key.objectid);
2524 if (!inode) {
2525 ret = -EIO;
2526 break;
2528 from = ALIGN(i_size_read(inode),
2529 root->fs_info->sectorsize);
2530 ret = btrfs_drop_extents(wc->trans, root, inode,
2531 from, (u64)-1, 1);
2533 * If the nlink count is zero here, the iput
2534 * will free the inode. We bump it to make
2535 * sure it doesn't get freed until the link
2536 * count fixup is done.
2538 if (!ret) {
2539 if (inode->i_nlink == 0)
2540 inc_nlink(inode);
2541 /* Update link count and nbytes. */
2542 ret = btrfs_update_inode(wc->trans,
2543 root, inode);
2545 iput(inode);
2546 if (ret)
2547 break;
2550 ret = link_to_fixup_dir(wc->trans, root,
2551 path, key.objectid);
2552 if (ret)
2553 break;
2556 if (key.type == BTRFS_DIR_INDEX_KEY &&
2557 wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
2558 ret = replay_one_dir_item(wc->trans, root, path,
2559 eb, i, &key);
2560 if (ret)
2561 break;
2564 if (wc->stage < LOG_WALK_REPLAY_ALL)
2565 continue;
2567 /* these keys are simply copied */
2568 if (key.type == BTRFS_XATTR_ITEM_KEY) {
2569 ret = overwrite_item(wc->trans, root, path,
2570 eb, i, &key);
2571 if (ret)
2572 break;
2573 } else if (key.type == BTRFS_INODE_REF_KEY ||
2574 key.type == BTRFS_INODE_EXTREF_KEY) {
2575 ret = add_inode_ref(wc->trans, root, log, path,
2576 eb, i, &key);
2577 if (ret && ret != -ENOENT)
2578 break;
2579 ret = 0;
2580 } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
2581 ret = replay_one_extent(wc->trans, root, path,
2582 eb, i, &key);
2583 if (ret)
2584 break;
2585 } else if (key.type == BTRFS_DIR_ITEM_KEY) {
2586 ret = replay_one_dir_item(wc->trans, root, path,
2587 eb, i, &key);
2588 if (ret)
2589 break;
2592 btrfs_free_path(path);
2593 return ret;
2596 static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2597 struct btrfs_root *root,
2598 struct btrfs_path *path, int *level,
2599 struct walk_control *wc)
2601 struct btrfs_fs_info *fs_info = root->fs_info;
2602 u64 root_owner;
2603 u64 bytenr;
2604 u64 ptr_gen;
2605 struct extent_buffer *next;
2606 struct extent_buffer *cur;
2607 struct extent_buffer *parent;
2608 u32 blocksize;
2609 int ret = 0;
2611 WARN_ON(*level < 0);
2612 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2614 while (*level > 0) {
2615 struct btrfs_key first_key;
2617 WARN_ON(*level < 0);
2618 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2619 cur = path->nodes[*level];
2621 WARN_ON(btrfs_header_level(cur) != *level);
2623 if (path->slots[*level] >=
2624 btrfs_header_nritems(cur))
2625 break;
2627 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2628 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2629 btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
2630 blocksize = fs_info->nodesize;
2632 parent = path->nodes[*level];
2633 root_owner = btrfs_header_owner(parent);
2635 next = btrfs_find_create_tree_block(fs_info, bytenr);
2636 if (IS_ERR(next))
2637 return PTR_ERR(next);
2639 if (*level == 1) {
2640 ret = wc->process_func(root, next, wc, ptr_gen,
2641 *level - 1);
2642 if (ret) {
2643 free_extent_buffer(next);
2644 return ret;
2647 path->slots[*level]++;
2648 if (wc->free) {
2649 ret = btrfs_read_buffer(next, ptr_gen,
2650 *level - 1, &first_key);
2651 if (ret) {
2652 free_extent_buffer(next);
2653 return ret;
2656 if (trans) {
2657 btrfs_tree_lock(next);
2658 btrfs_set_lock_blocking(next);
2659 clean_tree_block(fs_info, next);
2660 btrfs_wait_tree_block_writeback(next);
2661 btrfs_tree_unlock(next);
2662 } else {
2663 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2664 clear_extent_buffer_dirty(next);
2667 WARN_ON(root_owner !=
2668 BTRFS_TREE_LOG_OBJECTID);
2669 ret = btrfs_free_and_pin_reserved_extent(
2670 fs_info, bytenr,
2671 blocksize);
2672 if (ret) {
2673 free_extent_buffer(next);
2674 return ret;
2677 free_extent_buffer(next);
2678 continue;
2680 ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
2681 if (ret) {
2682 free_extent_buffer(next);
2683 return ret;
2686 WARN_ON(*level <= 0);
2687 if (path->nodes[*level-1])
2688 free_extent_buffer(path->nodes[*level-1]);
2689 path->nodes[*level-1] = next;
2690 *level = btrfs_header_level(next);
2691 path->slots[*level] = 0;
2692 cond_resched();
2694 WARN_ON(*level < 0);
2695 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2697 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
2699 cond_resched();
2700 return 0;
2703 static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
2704 struct btrfs_root *root,
2705 struct btrfs_path *path, int *level,
2706 struct walk_control *wc)
2708 struct btrfs_fs_info *fs_info = root->fs_info;
2709 u64 root_owner;
2710 int i;
2711 int slot;
2712 int ret;
2714 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2715 slot = path->slots[i];
2716 if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
2717 path->slots[i]++;
2718 *level = i;
2719 WARN_ON(*level == 0);
2720 return 0;
2721 } else {
2722 struct extent_buffer *parent;
2723 if (path->nodes[*level] == root->node)
2724 parent = path->nodes[*level];
2725 else
2726 parent = path->nodes[*level + 1];
2728 root_owner = btrfs_header_owner(parent);
2729 ret = wc->process_func(root, path->nodes[*level], wc,
2730 btrfs_header_generation(path->nodes[*level]),
2731 *level);
2732 if (ret)
2733 return ret;
2735 if (wc->free) {
2736 struct extent_buffer *next;
2738 next = path->nodes[*level];
2740 if (trans) {
2741 btrfs_tree_lock(next);
2742 btrfs_set_lock_blocking(next);
2743 clean_tree_block(fs_info, next);
2744 btrfs_wait_tree_block_writeback(next);
2745 btrfs_tree_unlock(next);
2746 } else {
2747 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2748 clear_extent_buffer_dirty(next);
2751 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
2752 ret = btrfs_free_and_pin_reserved_extent(
2753 fs_info,
2754 path->nodes[*level]->start,
2755 path->nodes[*level]->len);
2756 if (ret)
2757 return ret;
2759 free_extent_buffer(path->nodes[*level]);
2760 path->nodes[*level] = NULL;
2761 *level = i + 1;
2764 return 1;
2768 * drop the reference count on the tree rooted at 'snap'. This traverses
2769 * the tree freeing any blocks that have a ref count of zero after being
2770 * decremented.
2772 static int walk_log_tree(struct btrfs_trans_handle *trans,
2773 struct btrfs_root *log, struct walk_control *wc)
2775 struct btrfs_fs_info *fs_info = log->fs_info;
2776 int ret = 0;
2777 int wret;
2778 int level;
2779 struct btrfs_path *path;
2780 int orig_level;
2782 path = btrfs_alloc_path();
2783 if (!path)
2784 return -ENOMEM;
2786 level = btrfs_header_level(log->node);
2787 orig_level = level;
2788 path->nodes[level] = log->node;
2789 extent_buffer_get(log->node);
2790 path->slots[level] = 0;
2792 while (1) {
2793 wret = walk_down_log_tree(trans, log, path, &level, wc);
2794 if (wret > 0)
2795 break;
2796 if (wret < 0) {
2797 ret = wret;
2798 goto out;
2801 wret = walk_up_log_tree(trans, log, path, &level, wc);
2802 if (wret > 0)
2803 break;
2804 if (wret < 0) {
2805 ret = wret;
2806 goto out;
2810 /* was the root node processed? if not, catch it here */
2811 if (path->nodes[orig_level]) {
2812 ret = wc->process_func(log, path->nodes[orig_level], wc,
2813 btrfs_header_generation(path->nodes[orig_level]),
2814 orig_level);
2815 if (ret)
2816 goto out;
2817 if (wc->free) {
2818 struct extent_buffer *next;
2820 next = path->nodes[orig_level];
2822 if (trans) {
2823 btrfs_tree_lock(next);
2824 btrfs_set_lock_blocking(next);
2825 clean_tree_block(fs_info, next);
2826 btrfs_wait_tree_block_writeback(next);
2827 btrfs_tree_unlock(next);
2828 } else {
2829 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
2830 clear_extent_buffer_dirty(next);
2833 WARN_ON(log->root_key.objectid !=
2834 BTRFS_TREE_LOG_OBJECTID);
2835 ret = btrfs_free_and_pin_reserved_extent(fs_info,
2836 next->start, next->len);
2837 if (ret)
2838 goto out;
2842 out:
2843 btrfs_free_path(path);
2844 return ret;
2848 * helper function to update the item for a given subvolumes log root
2849 * in the tree of log roots
2851 static int update_log_root(struct btrfs_trans_handle *trans,
2852 struct btrfs_root *log)
2854 struct btrfs_fs_info *fs_info = log->fs_info;
2855 int ret;
2857 if (log->log_transid == 1) {
2858 /* insert root item on the first sync */
2859 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
2860 &log->root_key, &log->root_item);
2861 } else {
2862 ret = btrfs_update_root(trans, fs_info->log_root_tree,
2863 &log->root_key, &log->root_item);
2865 return ret;
2868 static void wait_log_commit(struct btrfs_root *root, int transid)
2870 DEFINE_WAIT(wait);
2871 int index = transid % 2;
2874 * we only allow two pending log transactions at a time,
2875 * so we know that if ours is more than 2 older than the
2876 * current transaction, we're done
2878 for (;;) {
2879 prepare_to_wait(&root->log_commit_wait[index],
2880 &wait, TASK_UNINTERRUPTIBLE);
2882 if (!(root->log_transid_committed < transid &&
2883 atomic_read(&root->log_commit[index])))
2884 break;
2886 mutex_unlock(&root->log_mutex);
2887 schedule();
2888 mutex_lock(&root->log_mutex);
2890 finish_wait(&root->log_commit_wait[index], &wait);
2893 static void wait_for_writer(struct btrfs_root *root)
2895 DEFINE_WAIT(wait);
2897 for (;;) {
2898 prepare_to_wait(&root->log_writer_wait, &wait,
2899 TASK_UNINTERRUPTIBLE);
2900 if (!atomic_read(&root->log_writers))
2901 break;
2903 mutex_unlock(&root->log_mutex);
2904 schedule();
2905 mutex_lock(&root->log_mutex);
2907 finish_wait(&root->log_writer_wait, &wait);
2910 static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
2911 struct btrfs_log_ctx *ctx)
2913 if (!ctx)
2914 return;
2916 mutex_lock(&root->log_mutex);
2917 list_del_init(&ctx->list);
2918 mutex_unlock(&root->log_mutex);
2922 * Invoked in log mutex context, or be sure there is no other task which
2923 * can access the list.
2925 static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
2926 int index, int error)
2928 struct btrfs_log_ctx *ctx;
2929 struct btrfs_log_ctx *safe;
2931 list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
2932 list_del_init(&ctx->list);
2933 ctx->log_ret = error;
2936 INIT_LIST_HEAD(&root->log_ctxs[index]);
2940 * btrfs_sync_log does sends a given tree log down to the disk and
2941 * updates the super blocks to record it. When this call is done,
2942 * you know that any inodes previously logged are safely on disk only
2943 * if it returns 0.
2945 * Any other return value means you need to call btrfs_commit_transaction.
2946 * Some of the edge cases for fsyncing directories that have had unlinks
2947 * or renames done in the past mean that sometimes the only safe
2948 * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
2949 * that has happened.
2951 int btrfs_sync_log(struct btrfs_trans_handle *trans,
2952 struct btrfs_root *root, struct btrfs_log_ctx *ctx)
2954 int index1;
2955 int index2;
2956 int mark;
2957 int ret;
2958 struct btrfs_fs_info *fs_info = root->fs_info;
2959 struct btrfs_root *log = root->log_root;
2960 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
2961 int log_transid = 0;
2962 struct btrfs_log_ctx root_log_ctx;
2963 struct blk_plug plug;
2965 mutex_lock(&root->log_mutex);
2966 log_transid = ctx->log_transid;
2967 if (root->log_transid_committed >= log_transid) {
2968 mutex_unlock(&root->log_mutex);
2969 return ctx->log_ret;
2972 index1 = log_transid % 2;
2973 if (atomic_read(&root->log_commit[index1])) {
2974 wait_log_commit(root, log_transid);
2975 mutex_unlock(&root->log_mutex);
2976 return ctx->log_ret;
2978 ASSERT(log_transid == root->log_transid);
2979 atomic_set(&root->log_commit[index1], 1);
2981 /* wait for previous tree log sync to complete */
2982 if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
2983 wait_log_commit(root, log_transid - 1);
2985 while (1) {
2986 int batch = atomic_read(&root->log_batch);
2987 /* when we're on an ssd, just kick the log commit out */
2988 if (!btrfs_test_opt(fs_info, SSD) &&
2989 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
2990 mutex_unlock(&root->log_mutex);
2991 schedule_timeout_uninterruptible(1);
2992 mutex_lock(&root->log_mutex);
2994 wait_for_writer(root);
2995 if (batch == atomic_read(&root->log_batch))
2996 break;
2999 /* bail out if we need to do a full commit */
3000 if (btrfs_need_log_full_commit(fs_info, trans)) {
3001 ret = -EAGAIN;
3002 btrfs_free_logged_extents(log, log_transid);
3003 mutex_unlock(&root->log_mutex);
3004 goto out;
3007 if (log_transid % 2 == 0)
3008 mark = EXTENT_DIRTY;
3009 else
3010 mark = EXTENT_NEW;
3012 /* we start IO on all the marked extents here, but we don't actually
3013 * wait for them until later.
3015 blk_start_plug(&plug);
3016 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
3017 if (ret) {
3018 blk_finish_plug(&plug);
3019 btrfs_abort_transaction(trans, ret);
3020 btrfs_free_logged_extents(log, log_transid);
3021 btrfs_set_log_full_commit(fs_info, trans);
3022 mutex_unlock(&root->log_mutex);
3023 goto out;
3026 btrfs_set_root_node(&log->root_item, log->node);
3028 root->log_transid++;
3029 log->log_transid = root->log_transid;
3030 root->log_start_pid = 0;
3032 * IO has been started, blocks of the log tree have WRITTEN flag set
3033 * in their headers. new modifications of the log will be written to
3034 * new positions. so it's safe to allow log writers to go in.
3036 mutex_unlock(&root->log_mutex);
3038 btrfs_init_log_ctx(&root_log_ctx, NULL);
3040 mutex_lock(&log_root_tree->log_mutex);
3041 atomic_inc(&log_root_tree->log_batch);
3042 atomic_inc(&log_root_tree->log_writers);
3044 index2 = log_root_tree->log_transid % 2;
3045 list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
3046 root_log_ctx.log_transid = log_root_tree->log_transid;
3048 mutex_unlock(&log_root_tree->log_mutex);
3050 ret = update_log_root(trans, log);
3052 mutex_lock(&log_root_tree->log_mutex);
3053 if (atomic_dec_and_test(&log_root_tree->log_writers)) {
3054 /* atomic_dec_and_test implies a barrier */
3055 cond_wake_up_nomb(&log_root_tree->log_writer_wait);
3058 if (ret) {
3059 if (!list_empty(&root_log_ctx.list))
3060 list_del_init(&root_log_ctx.list);
3062 blk_finish_plug(&plug);
3063 btrfs_set_log_full_commit(fs_info, trans);
3065 if (ret != -ENOSPC) {
3066 btrfs_abort_transaction(trans, ret);
3067 mutex_unlock(&log_root_tree->log_mutex);
3068 goto out;
3070 btrfs_wait_tree_log_extents(log, mark);
3071 btrfs_free_logged_extents(log, log_transid);
3072 mutex_unlock(&log_root_tree->log_mutex);
3073 ret = -EAGAIN;
3074 goto out;
3077 if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
3078 blk_finish_plug(&plug);
3079 list_del_init(&root_log_ctx.list);
3080 mutex_unlock(&log_root_tree->log_mutex);
3081 ret = root_log_ctx.log_ret;
3082 goto out;
3085 index2 = root_log_ctx.log_transid % 2;
3086 if (atomic_read(&log_root_tree->log_commit[index2])) {
3087 blk_finish_plug(&plug);
3088 ret = btrfs_wait_tree_log_extents(log, mark);
3089 btrfs_wait_logged_extents(trans, log, log_transid);
3090 wait_log_commit(log_root_tree,
3091 root_log_ctx.log_transid);
3092 mutex_unlock(&log_root_tree->log_mutex);
3093 if (!ret)
3094 ret = root_log_ctx.log_ret;
3095 goto out;
3097 ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
3098 atomic_set(&log_root_tree->log_commit[index2], 1);
3100 if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
3101 wait_log_commit(log_root_tree,
3102 root_log_ctx.log_transid - 1);
3105 wait_for_writer(log_root_tree);
3108 * now that we've moved on to the tree of log tree roots,
3109 * check the full commit flag again
3111 if (btrfs_need_log_full_commit(fs_info, trans)) {
3112 blk_finish_plug(&plug);
3113 btrfs_wait_tree_log_extents(log, mark);
3114 btrfs_free_logged_extents(log, log_transid);
3115 mutex_unlock(&log_root_tree->log_mutex);
3116 ret = -EAGAIN;
3117 goto out_wake_log_root;
3120 ret = btrfs_write_marked_extents(fs_info,
3121 &log_root_tree->dirty_log_pages,
3122 EXTENT_DIRTY | EXTENT_NEW);
3123 blk_finish_plug(&plug);
3124 if (ret) {
3125 btrfs_set_log_full_commit(fs_info, trans);
3126 btrfs_abort_transaction(trans, ret);
3127 btrfs_free_logged_extents(log, log_transid);
3128 mutex_unlock(&log_root_tree->log_mutex);
3129 goto out_wake_log_root;
3131 ret = btrfs_wait_tree_log_extents(log, mark);
3132 if (!ret)
3133 ret = btrfs_wait_tree_log_extents(log_root_tree,
3134 EXTENT_NEW | EXTENT_DIRTY);
3135 if (ret) {
3136 btrfs_set_log_full_commit(fs_info, trans);
3137 btrfs_free_logged_extents(log, log_transid);
3138 mutex_unlock(&log_root_tree->log_mutex);
3139 goto out_wake_log_root;
3141 btrfs_wait_logged_extents(trans, log, log_transid);
3143 btrfs_set_super_log_root(fs_info->super_for_commit,
3144 log_root_tree->node->start);
3145 btrfs_set_super_log_root_level(fs_info->super_for_commit,
3146 btrfs_header_level(log_root_tree->node));
3148 log_root_tree->log_transid++;
3149 mutex_unlock(&log_root_tree->log_mutex);
3152 * nobody else is going to jump in and write the the ctree
3153 * super here because the log_commit atomic below is protecting
3154 * us. We must be called with a transaction handle pinning
3155 * the running transaction open, so a full commit can't hop
3156 * in and cause problems either.
3158 ret = write_all_supers(fs_info, 1);
3159 if (ret) {
3160 btrfs_set_log_full_commit(fs_info, trans);
3161 btrfs_abort_transaction(trans, ret);
3162 goto out_wake_log_root;
3165 mutex_lock(&root->log_mutex);
3166 if (root->last_log_commit < log_transid)
3167 root->last_log_commit = log_transid;
3168 mutex_unlock(&root->log_mutex);
3170 out_wake_log_root:
3171 mutex_lock(&log_root_tree->log_mutex);
3172 btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
3174 log_root_tree->log_transid_committed++;
3175 atomic_set(&log_root_tree->log_commit[index2], 0);
3176 mutex_unlock(&log_root_tree->log_mutex);
3179 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3180 * all the updates above are seen by the woken threads. It might not be
3181 * necessary, but proving that seems to be hard.
3183 cond_wake_up(&log_root_tree->log_commit_wait[index2]);
3184 out:
3185 mutex_lock(&root->log_mutex);
3186 btrfs_remove_all_log_ctxs(root, index1, ret);
3187 root->log_transid_committed++;
3188 atomic_set(&root->log_commit[index1], 0);
3189 mutex_unlock(&root->log_mutex);
3192 * The barrier before waitqueue_active (in cond_wake_up) is needed so
3193 * all the updates above are seen by the woken threads. It might not be
3194 * necessary, but proving that seems to be hard.
3196 cond_wake_up(&root->log_commit_wait[index1]);
3197 return ret;
3200 static void free_log_tree(struct btrfs_trans_handle *trans,
3201 struct btrfs_root *log)
3203 int ret;
3204 u64 start;
3205 u64 end;
3206 struct walk_control wc = {
3207 .free = 1,
3208 .process_func = process_one_buffer
3211 ret = walk_log_tree(trans, log, &wc);
3212 /* I don't think this can happen but just in case */
3213 if (ret)
3214 btrfs_abort_transaction(trans, ret);
3216 while (1) {
3217 ret = find_first_extent_bit(&log->dirty_log_pages,
3218 0, &start, &end,
3219 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
3220 NULL);
3221 if (ret)
3222 break;
3224 clear_extent_bits(&log->dirty_log_pages, start, end,
3225 EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
3229 * We may have short-circuited the log tree with the full commit logic
3230 * and left ordered extents on our list, so clear these out to keep us
3231 * from leaking inodes and memory.
3233 btrfs_free_logged_extents(log, 0);
3234 btrfs_free_logged_extents(log, 1);
3236 free_extent_buffer(log->node);
3237 kfree(log);
3241 * free all the extents used by the tree log. This should be called
3242 * at commit time of the full transaction
3244 int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
3246 if (root->log_root) {
3247 free_log_tree(trans, root->log_root);
3248 root->log_root = NULL;
3250 return 0;
3253 int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
3254 struct btrfs_fs_info *fs_info)
3256 if (fs_info->log_root_tree) {
3257 free_log_tree(trans, fs_info->log_root_tree);
3258 fs_info->log_root_tree = NULL;
3260 return 0;
3264 * If both a file and directory are logged, and unlinks or renames are
3265 * mixed in, we have a few interesting corners:
3267 * create file X in dir Y
3268 * link file X to X.link in dir Y
3269 * fsync file X
3270 * unlink file X but leave X.link
3271 * fsync dir Y
3273 * After a crash we would expect only X.link to exist. But file X
3274 * didn't get fsync'd again so the log has back refs for X and X.link.
3276 * We solve this by removing directory entries and inode backrefs from the
3277 * log when a file that was logged in the current transaction is
3278 * unlinked. Any later fsync will include the updated log entries, and
3279 * we'll be able to reconstruct the proper directory items from backrefs.
3281 * This optimizations allows us to avoid relogging the entire inode
3282 * or the entire directory.
3284 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
3285 struct btrfs_root *root,
3286 const char *name, int name_len,
3287 struct btrfs_inode *dir, u64 index)
3289 struct btrfs_root *log;
3290 struct btrfs_dir_item *di;
3291 struct btrfs_path *path;
3292 int ret;
3293 int err = 0;
3294 int bytes_del = 0;
3295 u64 dir_ino = btrfs_ino(dir);
3297 if (dir->logged_trans < trans->transid)
3298 return 0;
3300 ret = join_running_log_trans(root);
3301 if (ret)
3302 return 0;
3304 mutex_lock(&dir->log_mutex);
3306 log = root->log_root;
3307 path = btrfs_alloc_path();
3308 if (!path) {
3309 err = -ENOMEM;
3310 goto out_unlock;
3313 di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
3314 name, name_len, -1);
3315 if (IS_ERR(di)) {
3316 err = PTR_ERR(di);
3317 goto fail;
3319 if (di) {
3320 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3321 bytes_del += name_len;
3322 if (ret) {
3323 err = ret;
3324 goto fail;
3327 btrfs_release_path(path);
3328 di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
3329 index, name, name_len, -1);
3330 if (IS_ERR(di)) {
3331 err = PTR_ERR(di);
3332 goto fail;
3334 if (di) {
3335 ret = btrfs_delete_one_dir_name(trans, log, path, di);
3336 bytes_del += name_len;
3337 if (ret) {
3338 err = ret;
3339 goto fail;
3343 /* update the directory size in the log to reflect the names
3344 * we have removed
3346 if (bytes_del) {
3347 struct btrfs_key key;
3349 key.objectid = dir_ino;
3350 key.offset = 0;
3351 key.type = BTRFS_INODE_ITEM_KEY;
3352 btrfs_release_path(path);
3354 ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
3355 if (ret < 0) {
3356 err = ret;
3357 goto fail;
3359 if (ret == 0) {
3360 struct btrfs_inode_item *item;
3361 u64 i_size;
3363 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3364 struct btrfs_inode_item);
3365 i_size = btrfs_inode_size(path->nodes[0], item);
3366 if (i_size > bytes_del)
3367 i_size -= bytes_del;
3368 else
3369 i_size = 0;
3370 btrfs_set_inode_size(path->nodes[0], item, i_size);
3371 btrfs_mark_buffer_dirty(path->nodes[0]);
3372 } else
3373 ret = 0;
3374 btrfs_release_path(path);
3376 fail:
3377 btrfs_free_path(path);
3378 out_unlock:
3379 mutex_unlock(&dir->log_mutex);
3380 if (ret == -ENOSPC) {
3381 btrfs_set_log_full_commit(root->fs_info, trans);
3382 ret = 0;
3383 } else if (ret < 0)
3384 btrfs_abort_transaction(trans, ret);
3386 btrfs_end_log_trans(root);
3388 return err;
3391 /* see comments for btrfs_del_dir_entries_in_log */
3392 int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3393 struct btrfs_root *root,
3394 const char *name, int name_len,
3395 struct btrfs_inode *inode, u64 dirid)
3397 struct btrfs_fs_info *fs_info = root->fs_info;
3398 struct btrfs_root *log;
3399 u64 index;
3400 int ret;
3402 if (inode->logged_trans < trans->transid)
3403 return 0;
3405 ret = join_running_log_trans(root);
3406 if (ret)
3407 return 0;
3408 log = root->log_root;
3409 mutex_lock(&inode->log_mutex);
3411 ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
3412 dirid, &index);
3413 mutex_unlock(&inode->log_mutex);
3414 if (ret == -ENOSPC) {
3415 btrfs_set_log_full_commit(fs_info, trans);
3416 ret = 0;
3417 } else if (ret < 0 && ret != -ENOENT)
3418 btrfs_abort_transaction(trans, ret);
3419 btrfs_end_log_trans(root);
3421 return ret;
3425 * creates a range item in the log for 'dirid'. first_offset and
3426 * last_offset tell us which parts of the key space the log should
3427 * be considered authoritative for.
3429 static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
3430 struct btrfs_root *log,
3431 struct btrfs_path *path,
3432 int key_type, u64 dirid,
3433 u64 first_offset, u64 last_offset)
3435 int ret;
3436 struct btrfs_key key;
3437 struct btrfs_dir_log_item *item;
3439 key.objectid = dirid;
3440 key.offset = first_offset;
3441 if (key_type == BTRFS_DIR_ITEM_KEY)
3442 key.type = BTRFS_DIR_LOG_ITEM_KEY;
3443 else
3444 key.type = BTRFS_DIR_LOG_INDEX_KEY;
3445 ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
3446 if (ret)
3447 return ret;
3449 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3450 struct btrfs_dir_log_item);
3451 btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
3452 btrfs_mark_buffer_dirty(path->nodes[0]);
3453 btrfs_release_path(path);
3454 return 0;
3458 * log all the items included in the current transaction for a given
3459 * directory. This also creates the range items in the log tree required
3460 * to replay anything deleted before the fsync
3462 static noinline int log_dir_items(struct btrfs_trans_handle *trans,
3463 struct btrfs_root *root, struct btrfs_inode *inode,
3464 struct btrfs_path *path,
3465 struct btrfs_path *dst_path, int key_type,
3466 struct btrfs_log_ctx *ctx,
3467 u64 min_offset, u64 *last_offset_ret)
3469 struct btrfs_key min_key;
3470 struct btrfs_root *log = root->log_root;
3471 struct extent_buffer *src;
3472 int err = 0;
3473 int ret;
3474 int i;
3475 int nritems;
3476 u64 first_offset = min_offset;
3477 u64 last_offset = (u64)-1;
3478 u64 ino = btrfs_ino(inode);
3480 log = root->log_root;
3482 min_key.objectid = ino;
3483 min_key.type = key_type;
3484 min_key.offset = min_offset;
3486 ret = btrfs_search_forward(root, &min_key, path, trans->transid);
3489 * we didn't find anything from this transaction, see if there
3490 * is anything at all
3492 if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
3493 min_key.objectid = ino;
3494 min_key.type = key_type;
3495 min_key.offset = (u64)-1;
3496 btrfs_release_path(path);
3497 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3498 if (ret < 0) {
3499 btrfs_release_path(path);
3500 return ret;
3502 ret = btrfs_previous_item(root, path, ino, key_type);
3504 /* if ret == 0 there are items for this type,
3505 * create a range to tell us the last key of this type.
3506 * otherwise, there are no items in this directory after
3507 * *min_offset, and we create a range to indicate that.
3509 if (ret == 0) {
3510 struct btrfs_key tmp;
3511 btrfs_item_key_to_cpu(path->nodes[0], &tmp,
3512 path->slots[0]);
3513 if (key_type == tmp.type)
3514 first_offset = max(min_offset, tmp.offset) + 1;
3516 goto done;
3519 /* go backward to find any previous key */
3520 ret = btrfs_previous_item(root, path, ino, key_type);
3521 if (ret == 0) {
3522 struct btrfs_key tmp;
3523 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3524 if (key_type == tmp.type) {
3525 first_offset = tmp.offset;
3526 ret = overwrite_item(trans, log, dst_path,
3527 path->nodes[0], path->slots[0],
3528 &tmp);
3529 if (ret) {
3530 err = ret;
3531 goto done;
3535 btrfs_release_path(path);
3537 /* find the first key from this transaction again */
3538 ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
3539 if (WARN_ON(ret != 0))
3540 goto done;
3543 * we have a block from this transaction, log every item in it
3544 * from our directory
3546 while (1) {
3547 struct btrfs_key tmp;
3548 src = path->nodes[0];
3549 nritems = btrfs_header_nritems(src);
3550 for (i = path->slots[0]; i < nritems; i++) {
3551 struct btrfs_dir_item *di;
3553 btrfs_item_key_to_cpu(src, &min_key, i);
3555 if (min_key.objectid != ino || min_key.type != key_type)
3556 goto done;
3557 ret = overwrite_item(trans, log, dst_path, src, i,
3558 &min_key);
3559 if (ret) {
3560 err = ret;
3561 goto done;
3565 * We must make sure that when we log a directory entry,
3566 * the corresponding inode, after log replay, has a
3567 * matching link count. For example:
3569 * touch foo
3570 * mkdir mydir
3571 * sync
3572 * ln foo mydir/bar
3573 * xfs_io -c "fsync" mydir
3574 * <crash>
3575 * <mount fs and log replay>
3577 * Would result in a fsync log that when replayed, our
3578 * file inode would have a link count of 1, but we get
3579 * two directory entries pointing to the same inode.
3580 * After removing one of the names, it would not be
3581 * possible to remove the other name, which resulted
3582 * always in stale file handle errors, and would not
3583 * be possible to rmdir the parent directory, since
3584 * its i_size could never decrement to the value
3585 * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
3587 di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
3588 btrfs_dir_item_key_to_cpu(src, di, &tmp);
3589 if (ctx &&
3590 (btrfs_dir_transid(src, di) == trans->transid ||
3591 btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
3592 tmp.type != BTRFS_ROOT_ITEM_KEY)
3593 ctx->log_new_dentries = true;
3595 path->slots[0] = nritems;
3598 * look ahead to the next item and see if it is also
3599 * from this directory and from this transaction
3601 ret = btrfs_next_leaf(root, path);
3602 if (ret) {
3603 if (ret == 1)
3604 last_offset = (u64)-1;
3605 else
3606 err = ret;
3607 goto done;
3609 btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
3610 if (tmp.objectid != ino || tmp.type != key_type) {
3611 last_offset = (u64)-1;
3612 goto done;
3614 if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
3615 ret = overwrite_item(trans, log, dst_path,
3616 path->nodes[0], path->slots[0],
3617 &tmp);
3618 if (ret)
3619 err = ret;
3620 else
3621 last_offset = tmp.offset;
3622 goto done;
3625 done:
3626 btrfs_release_path(path);
3627 btrfs_release_path(dst_path);
3629 if (err == 0) {
3630 *last_offset_ret = last_offset;
3632 * insert the log range keys to indicate where the log
3633 * is valid
3635 ret = insert_dir_log_key(trans, log, path, key_type,
3636 ino, first_offset, last_offset);
3637 if (ret)
3638 err = ret;
3640 return err;
3644 * logging directories is very similar to logging inodes, We find all the items
3645 * from the current transaction and write them to the log.
3647 * The recovery code scans the directory in the subvolume, and if it finds a
3648 * key in the range logged that is not present in the log tree, then it means
3649 * that dir entry was unlinked during the transaction.
3651 * In order for that scan to work, we must include one key smaller than
3652 * the smallest logged by this transaction and one key larger than the largest
3653 * key logged by this transaction.
3655 static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
3656 struct btrfs_root *root, struct btrfs_inode *inode,
3657 struct btrfs_path *path,
3658 struct btrfs_path *dst_path,
3659 struct btrfs_log_ctx *ctx)
3661 u64 min_key;
3662 u64 max_key;
3663 int ret;
3664 int key_type = BTRFS_DIR_ITEM_KEY;
3666 again:
3667 min_key = 0;
3668 max_key = 0;
3669 while (1) {
3670 ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
3671 ctx, min_key, &max_key);
3672 if (ret)
3673 return ret;
3674 if (max_key == (u64)-1)
3675 break;
3676 min_key = max_key + 1;
3679 if (key_type == BTRFS_DIR_ITEM_KEY) {
3680 key_type = BTRFS_DIR_INDEX_KEY;
3681 goto again;
3683 return 0;
3687 * a helper function to drop items from the log before we relog an
3688 * inode. max_key_type indicates the highest item type to remove.
3689 * This cannot be run for file data extents because it does not
3690 * free the extents they point to.
3692 static int drop_objectid_items(struct btrfs_trans_handle *trans,
3693 struct btrfs_root *log,
3694 struct btrfs_path *path,
3695 u64 objectid, int max_key_type)
3697 int ret;
3698 struct btrfs_key key;
3699 struct btrfs_key found_key;
3700 int start_slot;
3702 key.objectid = objectid;
3703 key.type = max_key_type;
3704 key.offset = (u64)-1;
3706 while (1) {
3707 ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
3708 BUG_ON(ret == 0); /* Logic error */
3709 if (ret < 0)
3710 break;
3712 if (path->slots[0] == 0)
3713 break;
3715 path->slots[0]--;
3716 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3717 path->slots[0]);
3719 if (found_key.objectid != objectid)
3720 break;
3722 found_key.offset = 0;
3723 found_key.type = 0;
3724 ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
3725 &start_slot);
3727 ret = btrfs_del_items(trans, log, path, start_slot,
3728 path->slots[0] - start_slot + 1);
3730 * If start slot isn't 0 then we don't need to re-search, we've
3731 * found the last guy with the objectid in this tree.
3733 if (ret || start_slot != 0)
3734 break;
3735 btrfs_release_path(path);
3737 btrfs_release_path(path);
3738 if (ret > 0)
3739 ret = 0;
3740 return ret;
3743 static void fill_inode_item(struct btrfs_trans_handle *trans,
3744 struct extent_buffer *leaf,
3745 struct btrfs_inode_item *item,
3746 struct inode *inode, int log_inode_only,
3747 u64 logged_isize)
3749 struct btrfs_map_token token;
3751 btrfs_init_map_token(&token);
3753 if (log_inode_only) {
3754 /* set the generation to zero so the recover code
3755 * can tell the difference between an logging
3756 * just to say 'this inode exists' and a logging
3757 * to say 'update this inode with these values'
3759 btrfs_set_token_inode_generation(leaf, item, 0, &token);
3760 btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
3761 } else {
3762 btrfs_set_token_inode_generation(leaf, item,
3763 BTRFS_I(inode)->generation,
3764 &token);
3765 btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
3768 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3769 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3770 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3771 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3773 btrfs_set_token_timespec_sec(leaf, &item->atime,
3774 inode->i_atime.tv_sec, &token);
3775 btrfs_set_token_timespec_nsec(leaf, &item->atime,
3776 inode->i_atime.tv_nsec, &token);
3778 btrfs_set_token_timespec_sec(leaf, &item->mtime,
3779 inode->i_mtime.tv_sec, &token);
3780 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
3781 inode->i_mtime.tv_nsec, &token);
3783 btrfs_set_token_timespec_sec(leaf, &item->ctime,
3784 inode->i_ctime.tv_sec, &token);
3785 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
3786 inode->i_ctime.tv_nsec, &token);
3788 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3789 &token);
3791 btrfs_set_token_inode_sequence(leaf, item,
3792 inode_peek_iversion(inode), &token);
3793 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3794 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3795 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3796 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
3799 static int log_inode_item(struct btrfs_trans_handle *trans,
3800 struct btrfs_root *log, struct btrfs_path *path,
3801 struct btrfs_inode *inode)
3803 struct btrfs_inode_item *inode_item;
3804 int ret;
3806 ret = btrfs_insert_empty_item(trans, log, path,
3807 &inode->location, sizeof(*inode_item));
3808 if (ret && ret != -EEXIST)
3809 return ret;
3810 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3811 struct btrfs_inode_item);
3812 fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
3813 0, 0);
3814 btrfs_release_path(path);
3815 return 0;
3818 static noinline int copy_items(struct btrfs_trans_handle *trans,
3819 struct btrfs_inode *inode,
3820 struct btrfs_path *dst_path,
3821 struct btrfs_path *src_path, u64 *last_extent,
3822 int start_slot, int nr, int inode_only,
3823 u64 logged_isize)
3825 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3826 unsigned long src_offset;
3827 unsigned long dst_offset;
3828 struct btrfs_root *log = inode->root->log_root;
3829 struct btrfs_file_extent_item *extent;
3830 struct btrfs_inode_item *inode_item;
3831 struct extent_buffer *src = src_path->nodes[0];
3832 struct btrfs_key first_key, last_key, key;
3833 int ret;
3834 struct btrfs_key *ins_keys;
3835 u32 *ins_sizes;
3836 char *ins_data;
3837 int i;
3838 struct list_head ordered_sums;
3839 int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
3840 bool has_extents = false;
3841 bool need_find_last_extent = true;
3842 bool done = false;
3844 INIT_LIST_HEAD(&ordered_sums);
3846 ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
3847 nr * sizeof(u32), GFP_NOFS);
3848 if (!ins_data)
3849 return -ENOMEM;
3851 first_key.objectid = (u64)-1;
3853 ins_sizes = (u32 *)ins_data;
3854 ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
3856 for (i = 0; i < nr; i++) {
3857 ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
3858 btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
3860 ret = btrfs_insert_empty_items(trans, log, dst_path,
3861 ins_keys, ins_sizes, nr);
3862 if (ret) {
3863 kfree(ins_data);
3864 return ret;
3867 for (i = 0; i < nr; i++, dst_path->slots[0]++) {
3868 dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
3869 dst_path->slots[0]);
3871 src_offset = btrfs_item_ptr_offset(src, start_slot + i);
3873 if (i == nr - 1)
3874 last_key = ins_keys[i];
3876 if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
3877 inode_item = btrfs_item_ptr(dst_path->nodes[0],
3878 dst_path->slots[0],
3879 struct btrfs_inode_item);
3880 fill_inode_item(trans, dst_path->nodes[0], inode_item,
3881 &inode->vfs_inode,
3882 inode_only == LOG_INODE_EXISTS,
3883 logged_isize);
3884 } else {
3885 copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
3886 src_offset, ins_sizes[i]);
3890 * We set need_find_last_extent here in case we know we were
3891 * processing other items and then walk into the first extent in
3892 * the inode. If we don't hit an extent then nothing changes,
3893 * we'll do the last search the next time around.
3895 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
3896 has_extents = true;
3897 if (first_key.objectid == (u64)-1)
3898 first_key = ins_keys[i];
3899 } else {
3900 need_find_last_extent = false;
3903 /* take a reference on file data extents so that truncates
3904 * or deletes of this inode don't have to relog the inode
3905 * again
3907 if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
3908 !skip_csum) {
3909 int found_type;
3910 extent = btrfs_item_ptr(src, start_slot + i,
3911 struct btrfs_file_extent_item);
3913 if (btrfs_file_extent_generation(src, extent) < trans->transid)
3914 continue;
3916 found_type = btrfs_file_extent_type(src, extent);
3917 if (found_type == BTRFS_FILE_EXTENT_REG) {
3918 u64 ds, dl, cs, cl;
3919 ds = btrfs_file_extent_disk_bytenr(src,
3920 extent);
3921 /* ds == 0 is a hole */
3922 if (ds == 0)
3923 continue;
3925 dl = btrfs_file_extent_disk_num_bytes(src,
3926 extent);
3927 cs = btrfs_file_extent_offset(src, extent);
3928 cl = btrfs_file_extent_num_bytes(src,
3929 extent);
3930 if (btrfs_file_extent_compression(src,
3931 extent)) {
3932 cs = 0;
3933 cl = dl;
3936 ret = btrfs_lookup_csums_range(
3937 fs_info->csum_root,
3938 ds + cs, ds + cs + cl - 1,
3939 &ordered_sums, 0);
3940 if (ret) {
3941 btrfs_release_path(dst_path);
3942 kfree(ins_data);
3943 return ret;
3949 btrfs_mark_buffer_dirty(dst_path->nodes[0]);
3950 btrfs_release_path(dst_path);
3951 kfree(ins_data);
3954 * we have to do this after the loop above to avoid changing the
3955 * log tree while trying to change the log tree.
3957 ret = 0;
3958 while (!list_empty(&ordered_sums)) {
3959 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
3960 struct btrfs_ordered_sum,
3961 list);
3962 if (!ret)
3963 ret = btrfs_csum_file_blocks(trans, log, sums);
3964 list_del(&sums->list);
3965 kfree(sums);
3968 if (!has_extents)
3969 return ret;
3971 if (need_find_last_extent && *last_extent == first_key.offset) {
3973 * We don't have any leafs between our current one and the one
3974 * we processed before that can have file extent items for our
3975 * inode (and have a generation number smaller than our current
3976 * transaction id).
3978 need_find_last_extent = false;
3982 * Because we use btrfs_search_forward we could skip leaves that were
3983 * not modified and then assume *last_extent is valid when it really
3984 * isn't. So back up to the previous leaf and read the end of the last
3985 * extent before we go and fill in holes.
3987 if (need_find_last_extent) {
3988 u64 len;
3990 ret = btrfs_prev_leaf(inode->root, src_path);
3991 if (ret < 0)
3992 return ret;
3993 if (ret)
3994 goto fill_holes;
3995 if (src_path->slots[0])
3996 src_path->slots[0]--;
3997 src = src_path->nodes[0];
3998 btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
3999 if (key.objectid != btrfs_ino(inode) ||
4000 key.type != BTRFS_EXTENT_DATA_KEY)
4001 goto fill_holes;
4002 extent = btrfs_item_ptr(src, src_path->slots[0],
4003 struct btrfs_file_extent_item);
4004 if (btrfs_file_extent_type(src, extent) ==
4005 BTRFS_FILE_EXTENT_INLINE) {
4006 len = btrfs_file_extent_inline_len(src,
4007 src_path->slots[0],
4008 extent);
4009 *last_extent = ALIGN(key.offset + len,
4010 fs_info->sectorsize);
4011 } else {
4012 len = btrfs_file_extent_num_bytes(src, extent);
4013 *last_extent = key.offset + len;
4016 fill_holes:
4017 /* So we did prev_leaf, now we need to move to the next leaf, but a few
4018 * things could have happened
4020 * 1) A merge could have happened, so we could currently be on a leaf
4021 * that holds what we were copying in the first place.
4022 * 2) A split could have happened, and now not all of the items we want
4023 * are on the same leaf.
4025 * So we need to adjust how we search for holes, we need to drop the
4026 * path and re-search for the first extent key we found, and then walk
4027 * forward until we hit the last one we copied.
4029 if (need_find_last_extent) {
4030 /* btrfs_prev_leaf could return 1 without releasing the path */
4031 btrfs_release_path(src_path);
4032 ret = btrfs_search_slot(NULL, inode->root, &first_key,
4033 src_path, 0, 0);
4034 if (ret < 0)
4035 return ret;
4036 ASSERT(ret == 0);
4037 src = src_path->nodes[0];
4038 i = src_path->slots[0];
4039 } else {
4040 i = start_slot;
4044 * Ok so here we need to go through and fill in any holes we may have
4045 * to make sure that holes are punched for those areas in case they had
4046 * extents previously.
4048 while (!done) {
4049 u64 offset, len;
4050 u64 extent_end;
4052 if (i >= btrfs_header_nritems(src_path->nodes[0])) {
4053 ret = btrfs_next_leaf(inode->root, src_path);
4054 if (ret < 0)
4055 return ret;
4056 ASSERT(ret == 0);
4057 src = src_path->nodes[0];
4058 i = 0;
4059 need_find_last_extent = true;
4062 btrfs_item_key_to_cpu(src, &key, i);
4063 if (!btrfs_comp_cpu_keys(&key, &last_key))
4064 done = true;
4065 if (key.objectid != btrfs_ino(inode) ||
4066 key.type != BTRFS_EXTENT_DATA_KEY) {
4067 i++;
4068 continue;
4070 extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
4071 if (btrfs_file_extent_type(src, extent) ==
4072 BTRFS_FILE_EXTENT_INLINE) {
4073 len = btrfs_file_extent_inline_len(src, i, extent);
4074 extent_end = ALIGN(key.offset + len,
4075 fs_info->sectorsize);
4076 } else {
4077 len = btrfs_file_extent_num_bytes(src, extent);
4078 extent_end = key.offset + len;
4080 i++;
4082 if (*last_extent == key.offset) {
4083 *last_extent = extent_end;
4084 continue;
4086 offset = *last_extent;
4087 len = key.offset - *last_extent;
4088 ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
4089 offset, 0, 0, len, 0, len, 0, 0, 0);
4090 if (ret)
4091 break;
4092 *last_extent = extent_end;
4096 * Check if there is a hole between the last extent found in our leaf
4097 * and the first extent in the next leaf. If there is one, we need to
4098 * log an explicit hole so that at replay time we can punch the hole.
4100 if (ret == 0 &&
4101 key.objectid == btrfs_ino(inode) &&
4102 key.type == BTRFS_EXTENT_DATA_KEY &&
4103 i == btrfs_header_nritems(src_path->nodes[0])) {
4104 ret = btrfs_next_leaf(inode->root, src_path);
4105 need_find_last_extent = true;
4106 if (ret > 0) {
4107 ret = 0;
4108 } else if (ret == 0) {
4109 btrfs_item_key_to_cpu(src_path->nodes[0], &key,
4110 src_path->slots[0]);
4111 if (key.objectid == btrfs_ino(inode) &&
4112 key.type == BTRFS_EXTENT_DATA_KEY &&
4113 *last_extent < key.offset) {
4114 const u64 len = key.offset - *last_extent;
4116 ret = btrfs_insert_file_extent(trans, log,
4117 btrfs_ino(inode),
4118 *last_extent, 0,
4119 0, len, 0, len,
4120 0, 0, 0);
4125 * Need to let the callers know we dropped the path so they should
4126 * re-search.
4128 if (!ret && need_find_last_extent)
4129 ret = 1;
4130 return ret;
4133 static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
4135 struct extent_map *em1, *em2;
4137 em1 = list_entry(a, struct extent_map, list);
4138 em2 = list_entry(b, struct extent_map, list);
4140 if (em1->start < em2->start)
4141 return -1;
4142 else if (em1->start > em2->start)
4143 return 1;
4144 return 0;
4147 static int wait_ordered_extents(struct btrfs_trans_handle *trans,
4148 struct inode *inode,
4149 struct btrfs_root *root,
4150 const struct extent_map *em,
4151 const struct list_head *logged_list,
4152 bool *ordered_io_error)
4154 struct btrfs_fs_info *fs_info = root->fs_info;
4155 struct btrfs_ordered_extent *ordered;
4156 struct btrfs_root *log = root->log_root;
4157 u64 mod_start = em->mod_start;
4158 u64 mod_len = em->mod_len;
4159 const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
4160 u64 csum_offset;
4161 u64 csum_len;
4162 LIST_HEAD(ordered_sums);
4163 int ret = 0;
4165 *ordered_io_error = false;
4167 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
4168 em->block_start == EXTENT_MAP_HOLE)
4169 return 0;
4172 * Wait far any ordered extent that covers our extent map. If it
4173 * finishes without an error, first check and see if our csums are on
4174 * our outstanding ordered extents.
4176 list_for_each_entry(ordered, logged_list, log_list) {
4177 struct btrfs_ordered_sum *sum;
4179 if (!mod_len)
4180 break;
4182 if (ordered->file_offset + ordered->len <= mod_start ||
4183 mod_start + mod_len <= ordered->file_offset)
4184 continue;
4186 if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
4187 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
4188 !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
4189 const u64 start = ordered->file_offset;
4190 const u64 end = ordered->file_offset + ordered->len - 1;
4192 WARN_ON(ordered->inode != inode);
4193 filemap_fdatawrite_range(inode->i_mapping, start, end);
4196 wait_event(ordered->wait,
4197 (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
4198 test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
4200 if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
4202 * Clear the AS_EIO/AS_ENOSPC flags from the inode's
4203 * i_mapping flags, so that the next fsync won't get
4204 * an outdated io error too.
4206 filemap_check_errors(inode->i_mapping);
4207 *ordered_io_error = true;
4208 break;
4211 * We are going to copy all the csums on this ordered extent, so
4212 * go ahead and adjust mod_start and mod_len in case this
4213 * ordered extent has already been logged.
4215 if (ordered->file_offset > mod_start) {
4216 if (ordered->file_offset + ordered->len >=
4217 mod_start + mod_len)
4218 mod_len = ordered->file_offset - mod_start;
4220 * If we have this case
4222 * |--------- logged extent ---------|
4223 * |----- ordered extent ----|
4225 * Just don't mess with mod_start and mod_len, we'll
4226 * just end up logging more csums than we need and it
4227 * will be ok.
4229 } else {
4230 if (ordered->file_offset + ordered->len <
4231 mod_start + mod_len) {
4232 mod_len = (mod_start + mod_len) -
4233 (ordered->file_offset + ordered->len);
4234 mod_start = ordered->file_offset +
4235 ordered->len;
4236 } else {
4237 mod_len = 0;
4241 if (skip_csum)
4242 continue;
4245 * To keep us from looping for the above case of an ordered
4246 * extent that falls inside of the logged extent.
4248 if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
4249 &ordered->flags))
4250 continue;
4252 list_for_each_entry(sum, &ordered->list, list) {
4253 ret = btrfs_csum_file_blocks(trans, log, sum);
4254 if (ret)
4255 break;
4259 if (*ordered_io_error || !mod_len || ret || skip_csum)
4260 return ret;
4262 if (em->compress_type) {
4263 csum_offset = 0;
4264 csum_len = max(em->block_len, em->orig_block_len);
4265 } else {
4266 csum_offset = mod_start - em->start;
4267 csum_len = mod_len;
4270 /* block start is already adjusted for the file extent offset. */
4271 ret = btrfs_lookup_csums_range(fs_info->csum_root,
4272 em->block_start + csum_offset,
4273 em->block_start + csum_offset +
4274 csum_len - 1, &ordered_sums, 0);
4275 if (ret)
4276 return ret;
4278 while (!list_empty(&ordered_sums)) {
4279 struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
4280 struct btrfs_ordered_sum,
4281 list);
4282 if (!ret)
4283 ret = btrfs_csum_file_blocks(trans, log, sums);
4284 list_del(&sums->list);
4285 kfree(sums);
4288 return ret;
4291 static int log_one_extent(struct btrfs_trans_handle *trans,
4292 struct btrfs_inode *inode, struct btrfs_root *root,
4293 const struct extent_map *em,
4294 struct btrfs_path *path,
4295 const struct list_head *logged_list,
4296 struct btrfs_log_ctx *ctx)
4298 struct btrfs_root *log = root->log_root;
4299 struct btrfs_file_extent_item *fi;
4300 struct extent_buffer *leaf;
4301 struct btrfs_map_token token;
4302 struct btrfs_key key;
4303 u64 extent_offset = em->start - em->orig_start;
4304 u64 block_len;
4305 int ret;
4306 int extent_inserted = 0;
4307 bool ordered_io_err = false;
4309 ret = wait_ordered_extents(trans, &inode->vfs_inode, root, em,
4310 logged_list, &ordered_io_err);
4311 if (ret)
4312 return ret;
4314 if (ordered_io_err) {
4315 ctx->io_err = -EIO;
4316 return ctx->io_err;
4319 btrfs_init_map_token(&token);
4321 ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
4322 em->start + em->len, NULL, 0, 1,
4323 sizeof(*fi), &extent_inserted);
4324 if (ret)
4325 return ret;
4327 if (!extent_inserted) {
4328 key.objectid = btrfs_ino(inode);
4329 key.type = BTRFS_EXTENT_DATA_KEY;
4330 key.offset = em->start;
4332 ret = btrfs_insert_empty_item(trans, log, path, &key,
4333 sizeof(*fi));
4334 if (ret)
4335 return ret;
4337 leaf = path->nodes[0];
4338 fi = btrfs_item_ptr(leaf, path->slots[0],
4339 struct btrfs_file_extent_item);
4341 btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
4342 &token);
4343 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
4344 btrfs_set_token_file_extent_type(leaf, fi,
4345 BTRFS_FILE_EXTENT_PREALLOC,
4346 &token);
4347 else
4348 btrfs_set_token_file_extent_type(leaf, fi,
4349 BTRFS_FILE_EXTENT_REG,
4350 &token);
4352 block_len = max(em->block_len, em->orig_block_len);
4353 if (em->compress_type != BTRFS_COMPRESS_NONE) {
4354 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4355 em->block_start,
4356 &token);
4357 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4358 &token);
4359 } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
4360 btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
4361 em->block_start -
4362 extent_offset, &token);
4363 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
4364 &token);
4365 } else {
4366 btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
4367 btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
4368 &token);
4371 btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
4372 btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
4373 btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
4374 btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
4375 &token);
4376 btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
4377 btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
4378 btrfs_mark_buffer_dirty(leaf);
4380 btrfs_release_path(path);
4382 return ret;
4386 * Log all prealloc extents beyond the inode's i_size to make sure we do not
4387 * lose them after doing a fast fsync and replaying the log. We scan the
4388 * subvolume's root instead of iterating the inode's extent map tree because
4389 * otherwise we can log incorrect extent items based on extent map conversion.
4390 * That can happen due to the fact that extent maps are merged when they
4391 * are not in the extent map tree's list of modified extents.
4393 static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
4394 struct btrfs_inode *inode,
4395 struct btrfs_path *path)
4397 struct btrfs_root *root = inode->root;
4398 struct btrfs_key key;
4399 const u64 i_size = i_size_read(&inode->vfs_inode);
4400 const u64 ino = btrfs_ino(inode);
4401 struct btrfs_path *dst_path = NULL;
4402 u64 last_extent = (u64)-1;
4403 int ins_nr = 0;
4404 int start_slot;
4405 int ret;
4407 if (!(inode->flags & BTRFS_INODE_PREALLOC))
4408 return 0;
4410 key.objectid = ino;
4411 key.type = BTRFS_EXTENT_DATA_KEY;
4412 key.offset = i_size;
4413 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4414 if (ret < 0)
4415 goto out;
4417 while (true) {
4418 struct extent_buffer *leaf = path->nodes[0];
4419 int slot = path->slots[0];
4421 if (slot >= btrfs_header_nritems(leaf)) {
4422 if (ins_nr > 0) {
4423 ret = copy_items(trans, inode, dst_path, path,
4424 &last_extent, start_slot,
4425 ins_nr, 1, 0);
4426 if (ret < 0)
4427 goto out;
4428 ins_nr = 0;
4430 ret = btrfs_next_leaf(root, path);
4431 if (ret < 0)
4432 goto out;
4433 if (ret > 0) {
4434 ret = 0;
4435 break;
4437 continue;
4440 btrfs_item_key_to_cpu(leaf, &key, slot);
4441 if (key.objectid > ino)
4442 break;
4443 if (WARN_ON_ONCE(key.objectid < ino) ||
4444 key.type < BTRFS_EXTENT_DATA_KEY ||
4445 key.offset < i_size) {
4446 path->slots[0]++;
4447 continue;
4449 if (last_extent == (u64)-1) {
4450 last_extent = key.offset;
4452 * Avoid logging extent items logged in past fsync calls
4453 * and leading to duplicate keys in the log tree.
4455 do {
4456 ret = btrfs_truncate_inode_items(trans,
4457 root->log_root,
4458 &inode->vfs_inode,
4459 i_size,
4460 BTRFS_EXTENT_DATA_KEY);
4461 } while (ret == -EAGAIN);
4462 if (ret)
4463 goto out;
4465 if (ins_nr == 0)
4466 start_slot = slot;
4467 ins_nr++;
4468 path->slots[0]++;
4469 if (!dst_path) {
4470 dst_path = btrfs_alloc_path();
4471 if (!dst_path) {
4472 ret = -ENOMEM;
4473 goto out;
4477 if (ins_nr > 0) {
4478 ret = copy_items(trans, inode, dst_path, path, &last_extent,
4479 start_slot, ins_nr, 1, 0);
4480 if (ret > 0)
4481 ret = 0;
4483 out:
4484 btrfs_release_path(path);
4485 btrfs_free_path(dst_path);
4486 return ret;
4489 static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
4490 struct btrfs_root *root,
4491 struct btrfs_inode *inode,
4492 struct btrfs_path *path,
4493 struct list_head *logged_list,
4494 struct btrfs_log_ctx *ctx,
4495 const u64 start,
4496 const u64 end)
4498 struct extent_map *em, *n;
4499 struct list_head extents;
4500 struct extent_map_tree *tree = &inode->extent_tree;
4501 u64 logged_start, logged_end;
4502 u64 test_gen;
4503 int ret = 0;
4504 int num = 0;
4506 INIT_LIST_HEAD(&extents);
4508 down_write(&inode->dio_sem);
4509 write_lock(&tree->lock);
4510 test_gen = root->fs_info->last_trans_committed;
4511 logged_start = start;
4512 logged_end = end;
4514 list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
4515 list_del_init(&em->list);
4517 * Just an arbitrary number, this can be really CPU intensive
4518 * once we start getting a lot of extents, and really once we
4519 * have a bunch of extents we just want to commit since it will
4520 * be faster.
4522 if (++num > 32768) {
4523 list_del_init(&tree->modified_extents);
4524 ret = -EFBIG;
4525 goto process;
4528 if (em->generation <= test_gen)
4529 continue;
4531 /* We log prealloc extents beyond eof later. */
4532 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
4533 em->start >= i_size_read(&inode->vfs_inode))
4534 continue;
4536 if (em->start < logged_start)
4537 logged_start = em->start;
4538 if ((em->start + em->len - 1) > logged_end)
4539 logged_end = em->start + em->len - 1;
4541 /* Need a ref to keep it from getting evicted from cache */
4542 refcount_inc(&em->refs);
4543 set_bit(EXTENT_FLAG_LOGGING, &em->flags);
4544 list_add_tail(&em->list, &extents);
4545 num++;
4548 list_sort(NULL, &extents, extent_cmp);
4549 btrfs_get_logged_extents(inode, logged_list, logged_start, logged_end);
4551 * Some ordered extents started by fsync might have completed
4552 * before we could collect them into the list logged_list, which
4553 * means they're gone, not in our logged_list nor in the inode's
4554 * ordered tree. We want the application/user space to know an
4555 * error happened while attempting to persist file data so that
4556 * it can take proper action. If such error happened, we leave
4557 * without writing to the log tree and the fsync must report the
4558 * file data write error and not commit the current transaction.
4560 ret = filemap_check_errors(inode->vfs_inode.i_mapping);
4561 if (ret)
4562 ctx->io_err = ret;
4563 process:
4564 while (!list_empty(&extents)) {
4565 em = list_entry(extents.next, struct extent_map, list);
4567 list_del_init(&em->list);
4570 * If we had an error we just need to delete everybody from our
4571 * private list.
4573 if (ret) {
4574 clear_em_logging(tree, em);
4575 free_extent_map(em);
4576 continue;
4579 write_unlock(&tree->lock);
4581 ret = log_one_extent(trans, inode, root, em, path, logged_list,
4582 ctx);
4583 write_lock(&tree->lock);
4584 clear_em_logging(tree, em);
4585 free_extent_map(em);
4587 WARN_ON(!list_empty(&extents));
4588 write_unlock(&tree->lock);
4589 up_write(&inode->dio_sem);
4591 btrfs_release_path(path);
4592 if (!ret)
4593 ret = btrfs_log_prealloc_extents(trans, inode, path);
4595 return ret;
4598 static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
4599 struct btrfs_path *path, u64 *size_ret)
4601 struct btrfs_key key;
4602 int ret;
4604 key.objectid = btrfs_ino(inode);
4605 key.type = BTRFS_INODE_ITEM_KEY;
4606 key.offset = 0;
4608 ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
4609 if (ret < 0) {
4610 return ret;
4611 } else if (ret > 0) {
4612 *size_ret = 0;
4613 } else {
4614 struct btrfs_inode_item *item;
4616 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4617 struct btrfs_inode_item);
4618 *size_ret = btrfs_inode_size(path->nodes[0], item);
4621 btrfs_release_path(path);
4622 return 0;
4626 * At the moment we always log all xattrs. This is to figure out at log replay
4627 * time which xattrs must have their deletion replayed. If a xattr is missing
4628 * in the log tree and exists in the fs/subvol tree, we delete it. This is
4629 * because if a xattr is deleted, the inode is fsynced and a power failure
4630 * happens, causing the log to be replayed the next time the fs is mounted,
4631 * we want the xattr to not exist anymore (same behaviour as other filesystems
4632 * with a journal, ext3/4, xfs, f2fs, etc).
4634 static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
4635 struct btrfs_root *root,
4636 struct btrfs_inode *inode,
4637 struct btrfs_path *path,
4638 struct btrfs_path *dst_path)
4640 int ret;
4641 struct btrfs_key key;
4642 const u64 ino = btrfs_ino(inode);
4643 int ins_nr = 0;
4644 int start_slot = 0;
4646 key.objectid = ino;
4647 key.type = BTRFS_XATTR_ITEM_KEY;
4648 key.offset = 0;
4650 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4651 if (ret < 0)
4652 return ret;
4654 while (true) {
4655 int slot = path->slots[0];
4656 struct extent_buffer *leaf = path->nodes[0];
4657 int nritems = btrfs_header_nritems(leaf);
4659 if (slot >= nritems) {
4660 if (ins_nr > 0) {
4661 u64 last_extent = 0;
4663 ret = copy_items(trans, inode, dst_path, path,
4664 &last_extent, start_slot,
4665 ins_nr, 1, 0);
4666 /* can't be 1, extent items aren't processed */
4667 ASSERT(ret <= 0);
4668 if (ret < 0)
4669 return ret;
4670 ins_nr = 0;
4672 ret = btrfs_next_leaf(root, path);
4673 if (ret < 0)
4674 return ret;
4675 else if (ret > 0)
4676 break;
4677 continue;
4680 btrfs_item_key_to_cpu(leaf, &key, slot);
4681 if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
4682 break;
4684 if (ins_nr == 0)
4685 start_slot = slot;
4686 ins_nr++;
4687 path->slots[0]++;
4688 cond_resched();
4690 if (ins_nr > 0) {
4691 u64 last_extent = 0;
4693 ret = copy_items(trans, inode, dst_path, path,
4694 &last_extent, start_slot,
4695 ins_nr, 1, 0);
4696 /* can't be 1, extent items aren't processed */
4697 ASSERT(ret <= 0);
4698 if (ret < 0)
4699 return ret;
4702 return 0;
4706 * If the no holes feature is enabled we need to make sure any hole between the
4707 * last extent and the i_size of our inode is explicitly marked in the log. This
4708 * is to make sure that doing something like:
4710 * 1) create file with 128Kb of data
4711 * 2) truncate file to 64Kb
4712 * 3) truncate file to 256Kb
4713 * 4) fsync file
4714 * 5) <crash/power failure>
4715 * 6) mount fs and trigger log replay
4717 * Will give us a file with a size of 256Kb, the first 64Kb of data match what
4718 * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
4719 * file correspond to a hole. The presence of explicit holes in a log tree is
4720 * what guarantees that log replay will remove/adjust file extent items in the
4721 * fs/subvol tree.
4723 * Here we do not need to care about holes between extents, that is already done
4724 * by copy_items(). We also only need to do this in the full sync path, where we
4725 * lookup for extents from the fs/subvol tree only. In the fast path case, we
4726 * lookup the list of modified extent maps and if any represents a hole, we
4727 * insert a corresponding extent representing a hole in the log tree.
4729 static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4730 struct btrfs_root *root,
4731 struct btrfs_inode *inode,
4732 struct btrfs_path *path)
4734 struct btrfs_fs_info *fs_info = root->fs_info;
4735 int ret;
4736 struct btrfs_key key;
4737 u64 hole_start;
4738 u64 hole_size;
4739 struct extent_buffer *leaf;
4740 struct btrfs_root *log = root->log_root;
4741 const u64 ino = btrfs_ino(inode);
4742 const u64 i_size = i_size_read(&inode->vfs_inode);
4744 if (!btrfs_fs_incompat(fs_info, NO_HOLES))
4745 return 0;
4747 key.objectid = ino;
4748 key.type = BTRFS_EXTENT_DATA_KEY;
4749 key.offset = (u64)-1;
4751 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4752 ASSERT(ret != 0);
4753 if (ret < 0)
4754 return ret;
4756 ASSERT(path->slots[0] > 0);
4757 path->slots[0]--;
4758 leaf = path->nodes[0];
4759 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4761 if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
4762 /* inode does not have any extents */
4763 hole_start = 0;
4764 hole_size = i_size;
4765 } else {
4766 struct btrfs_file_extent_item *extent;
4767 u64 len;
4770 * If there's an extent beyond i_size, an explicit hole was
4771 * already inserted by copy_items().
4773 if (key.offset >= i_size)
4774 return 0;
4776 extent = btrfs_item_ptr(leaf, path->slots[0],
4777 struct btrfs_file_extent_item);
4779 if (btrfs_file_extent_type(leaf, extent) ==
4780 BTRFS_FILE_EXTENT_INLINE) {
4781 len = btrfs_file_extent_inline_len(leaf,
4782 path->slots[0],
4783 extent);
4784 ASSERT(len == i_size ||
4785 (len == fs_info->sectorsize &&
4786 btrfs_file_extent_compression(leaf, extent) !=
4787 BTRFS_COMPRESS_NONE));
4788 return 0;
4791 len = btrfs_file_extent_num_bytes(leaf, extent);
4792 /* Last extent goes beyond i_size, no need to log a hole. */
4793 if (key.offset + len > i_size)
4794 return 0;
4795 hole_start = key.offset + len;
4796 hole_size = i_size - hole_start;
4798 btrfs_release_path(path);
4800 /* Last extent ends at i_size. */
4801 if (hole_size == 0)
4802 return 0;
4804 hole_size = ALIGN(hole_size, fs_info->sectorsize);
4805 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4806 hole_size, 0, hole_size, 0, 0, 0);
4807 return ret;
4811 * When we are logging a new inode X, check if it doesn't have a reference that
4812 * matches the reference from some other inode Y created in a past transaction
4813 * and that was renamed in the current transaction. If we don't do this, then at
4814 * log replay time we can lose inode Y (and all its files if it's a directory):
4816 * mkdir /mnt/x
4817 * echo "hello world" > /mnt/x/foobar
4818 * sync
4819 * mv /mnt/x /mnt/y
4820 * mkdir /mnt/x # or touch /mnt/x
4821 * xfs_io -c fsync /mnt/x
4822 * <power fail>
4823 * mount fs, trigger log replay
4825 * After the log replay procedure, we would lose the first directory and all its
4826 * files (file foobar).
4827 * For the case where inode Y is not a directory we simply end up losing it:
4829 * echo "123" > /mnt/foo
4830 * sync
4831 * mv /mnt/foo /mnt/bar
4832 * echo "abc" > /mnt/foo
4833 * xfs_io -c fsync /mnt/foo
4834 * <power fail>
4836 * We also need this for cases where a snapshot entry is replaced by some other
4837 * entry (file or directory) otherwise we end up with an unreplayable log due to
4838 * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
4839 * if it were a regular entry:
4841 * mkdir /mnt/x
4842 * btrfs subvolume snapshot /mnt /mnt/x/snap
4843 * btrfs subvolume delete /mnt/x/snap
4844 * rmdir /mnt/x
4845 * mkdir /mnt/x
4846 * fsync /mnt/x or fsync some new file inside it
4847 * <power fail>
4849 * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
4850 * the same transaction.
4852 static int btrfs_check_ref_name_override(struct extent_buffer *eb,
4853 const int slot,
4854 const struct btrfs_key *key,
4855 struct btrfs_inode *inode,
4856 u64 *other_ino)
4858 int ret;
4859 struct btrfs_path *search_path;
4860 char *name = NULL;
4861 u32 name_len = 0;
4862 u32 item_size = btrfs_item_size_nr(eb, slot);
4863 u32 cur_offset = 0;
4864 unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
4866 search_path = btrfs_alloc_path();
4867 if (!search_path)
4868 return -ENOMEM;
4869 search_path->search_commit_root = 1;
4870 search_path->skip_locking = 1;
4872 while (cur_offset < item_size) {
4873 u64 parent;
4874 u32 this_name_len;
4875 u32 this_len;
4876 unsigned long name_ptr;
4877 struct btrfs_dir_item *di;
4879 if (key->type == BTRFS_INODE_REF_KEY) {
4880 struct btrfs_inode_ref *iref;
4882 iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
4883 parent = key->offset;
4884 this_name_len = btrfs_inode_ref_name_len(eb, iref);
4885 name_ptr = (unsigned long)(iref + 1);
4886 this_len = sizeof(*iref) + this_name_len;
4887 } else {
4888 struct btrfs_inode_extref *extref;
4890 extref = (struct btrfs_inode_extref *)(ptr +
4891 cur_offset);
4892 parent = btrfs_inode_extref_parent(eb, extref);
4893 this_name_len = btrfs_inode_extref_name_len(eb, extref);
4894 name_ptr = (unsigned long)&extref->name;
4895 this_len = sizeof(*extref) + this_name_len;
4898 if (this_name_len > name_len) {
4899 char *new_name;
4901 new_name = krealloc(name, this_name_len, GFP_NOFS);
4902 if (!new_name) {
4903 ret = -ENOMEM;
4904 goto out;
4906 name_len = this_name_len;
4907 name = new_name;
4910 read_extent_buffer(eb, name, name_ptr, this_name_len);
4911 di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
4912 parent, name, this_name_len, 0);
4913 if (di && !IS_ERR(di)) {
4914 struct btrfs_key di_key;
4916 btrfs_dir_item_key_to_cpu(search_path->nodes[0],
4917 di, &di_key);
4918 if (di_key.type == BTRFS_INODE_ITEM_KEY) {
4919 ret = 1;
4920 *other_ino = di_key.objectid;
4921 } else {
4922 ret = -EAGAIN;
4924 goto out;
4925 } else if (IS_ERR(di)) {
4926 ret = PTR_ERR(di);
4927 goto out;
4929 btrfs_release_path(search_path);
4931 cur_offset += this_len;
4933 ret = 0;
4934 out:
4935 btrfs_free_path(search_path);
4936 kfree(name);
4937 return ret;
4940 /* log a single inode in the tree log.
4941 * At least one parent directory for this inode must exist in the tree
4942 * or be logged already.
4944 * Any items from this inode changed by the current transaction are copied
4945 * to the log tree. An extra reference is taken on any extents in this
4946 * file, allowing us to avoid a whole pile of corner cases around logging
4947 * blocks that have been removed from the tree.
4949 * See LOG_INODE_ALL and related defines for a description of what inode_only
4950 * does.
4952 * This handles both files and directories.
4954 static int btrfs_log_inode(struct btrfs_trans_handle *trans,
4955 struct btrfs_root *root, struct btrfs_inode *inode,
4956 int inode_only,
4957 const loff_t start,
4958 const loff_t end,
4959 struct btrfs_log_ctx *ctx)
4961 struct btrfs_fs_info *fs_info = root->fs_info;
4962 struct btrfs_path *path;
4963 struct btrfs_path *dst_path;
4964 struct btrfs_key min_key;
4965 struct btrfs_key max_key;
4966 struct btrfs_root *log = root->log_root;
4967 LIST_HEAD(logged_list);
4968 u64 last_extent = 0;
4969 int err = 0;
4970 int ret;
4971 int nritems;
4972 int ins_start_slot = 0;
4973 int ins_nr;
4974 bool fast_search = false;
4975 u64 ino = btrfs_ino(inode);
4976 struct extent_map_tree *em_tree = &inode->extent_tree;
4977 u64 logged_isize = 0;
4978 bool need_log_inode_item = true;
4979 bool xattrs_logged = false;
4981 path = btrfs_alloc_path();
4982 if (!path)
4983 return -ENOMEM;
4984 dst_path = btrfs_alloc_path();
4985 if (!dst_path) {
4986 btrfs_free_path(path);
4987 return -ENOMEM;
4990 min_key.objectid = ino;
4991 min_key.type = BTRFS_INODE_ITEM_KEY;
4992 min_key.offset = 0;
4994 max_key.objectid = ino;
4997 /* today the code can only do partial logging of directories */
4998 if (S_ISDIR(inode->vfs_inode.i_mode) ||
4999 (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5000 &inode->runtime_flags) &&
5001 inode_only >= LOG_INODE_EXISTS))
5002 max_key.type = BTRFS_XATTR_ITEM_KEY;
5003 else
5004 max_key.type = (u8)-1;
5005 max_key.offset = (u64)-1;
5008 * Only run delayed items if we are a dir or a new file.
5009 * Otherwise commit the delayed inode only, which is needed in
5010 * order for the log replay code to mark inodes for link count
5011 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
5013 if (S_ISDIR(inode->vfs_inode.i_mode) ||
5014 inode->generation > fs_info->last_trans_committed)
5015 ret = btrfs_commit_inode_delayed_items(trans, inode);
5016 else
5017 ret = btrfs_commit_inode_delayed_inode(inode);
5019 if (ret) {
5020 btrfs_free_path(path);
5021 btrfs_free_path(dst_path);
5022 return ret;
5025 if (inode_only == LOG_OTHER_INODE) {
5026 inode_only = LOG_INODE_EXISTS;
5027 mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
5028 } else {
5029 mutex_lock(&inode->log_mutex);
5033 * a brute force approach to making sure we get the most uptodate
5034 * copies of everything.
5036 if (S_ISDIR(inode->vfs_inode.i_mode)) {
5037 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
5039 if (inode_only == LOG_INODE_EXISTS)
5040 max_key_type = BTRFS_XATTR_ITEM_KEY;
5041 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
5042 } else {
5043 if (inode_only == LOG_INODE_EXISTS) {
5045 * Make sure the new inode item we write to the log has
5046 * the same isize as the current one (if it exists).
5047 * This is necessary to prevent data loss after log
5048 * replay, and also to prevent doing a wrong expanding
5049 * truncate - for e.g. create file, write 4K into offset
5050 * 0, fsync, write 4K into offset 4096, add hard link,
5051 * fsync some other file (to sync log), power fail - if
5052 * we use the inode's current i_size, after log replay
5053 * we get a 8Kb file, with the last 4Kb extent as a hole
5054 * (zeroes), as if an expanding truncate happened,
5055 * instead of getting a file of 4Kb only.
5057 err = logged_inode_size(log, inode, path, &logged_isize);
5058 if (err)
5059 goto out_unlock;
5061 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5062 &inode->runtime_flags)) {
5063 if (inode_only == LOG_INODE_EXISTS) {
5064 max_key.type = BTRFS_XATTR_ITEM_KEY;
5065 ret = drop_objectid_items(trans, log, path, ino,
5066 max_key.type);
5067 } else {
5068 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5069 &inode->runtime_flags);
5070 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
5071 &inode->runtime_flags);
5072 while(1) {
5073 ret = btrfs_truncate_inode_items(trans,
5074 log, &inode->vfs_inode, 0, 0);
5075 if (ret != -EAGAIN)
5076 break;
5079 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
5080 &inode->runtime_flags) ||
5081 inode_only == LOG_INODE_EXISTS) {
5082 if (inode_only == LOG_INODE_ALL)
5083 fast_search = true;
5084 max_key.type = BTRFS_XATTR_ITEM_KEY;
5085 ret = drop_objectid_items(trans, log, path, ino,
5086 max_key.type);
5087 } else {
5088 if (inode_only == LOG_INODE_ALL)
5089 fast_search = true;
5090 goto log_extents;
5094 if (ret) {
5095 err = ret;
5096 goto out_unlock;
5099 while (1) {
5100 ins_nr = 0;
5101 ret = btrfs_search_forward(root, &min_key,
5102 path, trans->transid);
5103 if (ret < 0) {
5104 err = ret;
5105 goto out_unlock;
5107 if (ret != 0)
5108 break;
5109 again:
5110 /* note, ins_nr might be > 0 here, cleanup outside the loop */
5111 if (min_key.objectid != ino)
5112 break;
5113 if (min_key.type > max_key.type)
5114 break;
5116 if (min_key.type == BTRFS_INODE_ITEM_KEY)
5117 need_log_inode_item = false;
5119 if ((min_key.type == BTRFS_INODE_REF_KEY ||
5120 min_key.type == BTRFS_INODE_EXTREF_KEY) &&
5121 inode->generation == trans->transid) {
5122 u64 other_ino = 0;
5124 ret = btrfs_check_ref_name_override(path->nodes[0],
5125 path->slots[0], &min_key, inode,
5126 &other_ino);
5127 if (ret < 0) {
5128 err = ret;
5129 goto out_unlock;
5130 } else if (ret > 0 && ctx &&
5131 other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
5132 struct btrfs_key inode_key;
5133 struct inode *other_inode;
5135 if (ins_nr > 0) {
5136 ins_nr++;
5137 } else {
5138 ins_nr = 1;
5139 ins_start_slot = path->slots[0];
5141 ret = copy_items(trans, inode, dst_path, path,
5142 &last_extent, ins_start_slot,
5143 ins_nr, inode_only,
5144 logged_isize);
5145 if (ret < 0) {
5146 err = ret;
5147 goto out_unlock;
5149 ins_nr = 0;
5150 btrfs_release_path(path);
5151 inode_key.objectid = other_ino;
5152 inode_key.type = BTRFS_INODE_ITEM_KEY;
5153 inode_key.offset = 0;
5154 other_inode = btrfs_iget(fs_info->sb,
5155 &inode_key, root,
5156 NULL);
5158 * If the other inode that had a conflicting dir
5159 * entry was deleted in the current transaction,
5160 * we don't need to do more work nor fallback to
5161 * a transaction commit.
5163 if (IS_ERR(other_inode) &&
5164 PTR_ERR(other_inode) == -ENOENT) {
5165 goto next_key;
5166 } else if (IS_ERR(other_inode)) {
5167 err = PTR_ERR(other_inode);
5168 goto out_unlock;
5171 * We are safe logging the other inode without
5172 * acquiring its i_mutex as long as we log with
5173 * the LOG_INODE_EXISTS mode. We're safe against
5174 * concurrent renames of the other inode as well
5175 * because during a rename we pin the log and
5176 * update the log with the new name before we
5177 * unpin it.
5179 err = btrfs_log_inode(trans, root,
5180 BTRFS_I(other_inode),
5181 LOG_OTHER_INODE, 0, LLONG_MAX,
5182 ctx);
5183 iput(other_inode);
5184 if (err)
5185 goto out_unlock;
5186 else
5187 goto next_key;
5191 /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
5192 if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
5193 if (ins_nr == 0)
5194 goto next_slot;
5195 ret = copy_items(trans, inode, dst_path, path,
5196 &last_extent, ins_start_slot,
5197 ins_nr, inode_only, logged_isize);
5198 if (ret < 0) {
5199 err = ret;
5200 goto out_unlock;
5202 ins_nr = 0;
5203 if (ret) {
5204 btrfs_release_path(path);
5205 continue;
5207 goto next_slot;
5210 if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
5211 ins_nr++;
5212 goto next_slot;
5213 } else if (!ins_nr) {
5214 ins_start_slot = path->slots[0];
5215 ins_nr = 1;
5216 goto next_slot;
5219 ret = copy_items(trans, inode, dst_path, path, &last_extent,
5220 ins_start_slot, ins_nr, inode_only,
5221 logged_isize);
5222 if (ret < 0) {
5223 err = ret;
5224 goto out_unlock;
5226 if (ret) {
5227 ins_nr = 0;
5228 btrfs_release_path(path);
5229 continue;
5231 ins_nr = 1;
5232 ins_start_slot = path->slots[0];
5233 next_slot:
5235 nritems = btrfs_header_nritems(path->nodes[0]);
5236 path->slots[0]++;
5237 if (path->slots[0] < nritems) {
5238 btrfs_item_key_to_cpu(path->nodes[0], &min_key,
5239 path->slots[0]);
5240 goto again;
5242 if (ins_nr) {
5243 ret = copy_items(trans, inode, dst_path, path,
5244 &last_extent, ins_start_slot,
5245 ins_nr, inode_only, logged_isize);
5246 if (ret < 0) {
5247 err = ret;
5248 goto out_unlock;
5250 ret = 0;
5251 ins_nr = 0;
5253 btrfs_release_path(path);
5254 next_key:
5255 if (min_key.offset < (u64)-1) {
5256 min_key.offset++;
5257 } else if (min_key.type < max_key.type) {
5258 min_key.type++;
5259 min_key.offset = 0;
5260 } else {
5261 break;
5264 if (ins_nr) {
5265 ret = copy_items(trans, inode, dst_path, path, &last_extent,
5266 ins_start_slot, ins_nr, inode_only,
5267 logged_isize);
5268 if (ret < 0) {
5269 err = ret;
5270 goto out_unlock;
5272 ret = 0;
5273 ins_nr = 0;
5276 btrfs_release_path(path);
5277 btrfs_release_path(dst_path);
5278 err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
5279 if (err)
5280 goto out_unlock;
5281 xattrs_logged = true;
5282 if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
5283 btrfs_release_path(path);
5284 btrfs_release_path(dst_path);
5285 err = btrfs_log_trailing_hole(trans, root, inode, path);
5286 if (err)
5287 goto out_unlock;
5289 log_extents:
5290 btrfs_release_path(path);
5291 btrfs_release_path(dst_path);
5292 if (need_log_inode_item) {
5293 err = log_inode_item(trans, log, dst_path, inode);
5294 if (!err && !xattrs_logged) {
5295 err = btrfs_log_all_xattrs(trans, root, inode, path,
5296 dst_path);
5297 btrfs_release_path(path);
5299 if (err)
5300 goto out_unlock;
5302 if (fast_search) {
5303 ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
5304 &logged_list, ctx, start, end);
5305 if (ret) {
5306 err = ret;
5307 goto out_unlock;
5309 } else if (inode_only == LOG_INODE_ALL) {
5310 struct extent_map *em, *n;
5312 write_lock(&em_tree->lock);
5314 * We can't just remove every em if we're called for a ranged
5315 * fsync - that is, one that doesn't cover the whole possible
5316 * file range (0 to LLONG_MAX). This is because we can have
5317 * em's that fall outside the range we're logging and therefore
5318 * their ordered operations haven't completed yet
5319 * (btrfs_finish_ordered_io() not invoked yet). This means we
5320 * didn't get their respective file extent item in the fs/subvol
5321 * tree yet, and need to let the next fast fsync (one which
5322 * consults the list of modified extent maps) find the em so
5323 * that it logs a matching file extent item and waits for the
5324 * respective ordered operation to complete (if it's still
5325 * running).
5327 * Removing every em outside the range we're logging would make
5328 * the next fast fsync not log their matching file extent items,
5329 * therefore making us lose data after a log replay.
5331 list_for_each_entry_safe(em, n, &em_tree->modified_extents,
5332 list) {
5333 const u64 mod_end = em->mod_start + em->mod_len - 1;
5335 if (em->mod_start >= start && mod_end <= end)
5336 list_del_init(&em->list);
5338 write_unlock(&em_tree->lock);
5341 if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
5342 ret = log_directory_changes(trans, root, inode, path, dst_path,
5343 ctx);
5344 if (ret) {
5345 err = ret;
5346 goto out_unlock;
5350 spin_lock(&inode->lock);
5351 inode->logged_trans = trans->transid;
5352 inode->last_log_commit = inode->last_sub_trans;
5353 spin_unlock(&inode->lock);
5354 out_unlock:
5355 if (unlikely(err))
5356 btrfs_put_logged_extents(&logged_list);
5357 else
5358 btrfs_submit_logged_extents(&logged_list, log);
5359 mutex_unlock(&inode->log_mutex);
5361 btrfs_free_path(path);
5362 btrfs_free_path(dst_path);
5363 return err;
5367 * Check if we must fallback to a transaction commit when logging an inode.
5368 * This must be called after logging the inode and is used only in the context
5369 * when fsyncing an inode requires the need to log some other inode - in which
5370 * case we can't lock the i_mutex of each other inode we need to log as that
5371 * can lead to deadlocks with concurrent fsync against other inodes (as we can
5372 * log inodes up or down in the hierarchy) or rename operations for example. So
5373 * we take the log_mutex of the inode after we have logged it and then check for
5374 * its last_unlink_trans value - this is safe because any task setting
5375 * last_unlink_trans must take the log_mutex and it must do this before it does
5376 * the actual unlink operation, so if we do this check before a concurrent task
5377 * sets last_unlink_trans it means we've logged a consistent version/state of
5378 * all the inode items, otherwise we are not sure and must do a transaction
5379 * commit (the concurrent task might have only updated last_unlink_trans before
5380 * we logged the inode or it might have also done the unlink).
5382 static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
5383 struct btrfs_inode *inode)
5385 struct btrfs_fs_info *fs_info = inode->root->fs_info;
5386 bool ret = false;
5388 mutex_lock(&inode->log_mutex);
5389 if (inode->last_unlink_trans > fs_info->last_trans_committed) {
5391 * Make sure any commits to the log are forced to be full
5392 * commits.
5394 btrfs_set_log_full_commit(fs_info, trans);
5395 ret = true;
5397 mutex_unlock(&inode->log_mutex);
5399 return ret;
5403 * follow the dentry parent pointers up the chain and see if any
5404 * of the directories in it require a full commit before they can
5405 * be logged. Returns zero if nothing special needs to be done or 1 if
5406 * a full commit is required.
5408 static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
5409 struct btrfs_inode *inode,
5410 struct dentry *parent,
5411 struct super_block *sb,
5412 u64 last_committed)
5414 int ret = 0;
5415 struct dentry *old_parent = NULL;
5416 struct btrfs_inode *orig_inode = inode;
5419 * for regular files, if its inode is already on disk, we don't
5420 * have to worry about the parents at all. This is because
5421 * we can use the last_unlink_trans field to record renames
5422 * and other fun in this file.
5424 if (S_ISREG(inode->vfs_inode.i_mode) &&
5425 inode->generation <= last_committed &&
5426 inode->last_unlink_trans <= last_committed)
5427 goto out;
5429 if (!S_ISDIR(inode->vfs_inode.i_mode)) {
5430 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5431 goto out;
5432 inode = BTRFS_I(d_inode(parent));
5435 while (1) {
5437 * If we are logging a directory then we start with our inode,
5438 * not our parent's inode, so we need to skip setting the
5439 * logged_trans so that further down in the log code we don't
5440 * think this inode has already been logged.
5442 if (inode != orig_inode)
5443 inode->logged_trans = trans->transid;
5444 smp_mb();
5446 if (btrfs_must_commit_transaction(trans, inode)) {
5447 ret = 1;
5448 break;
5451 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5452 break;
5454 if (IS_ROOT(parent)) {
5455 inode = BTRFS_I(d_inode(parent));
5456 if (btrfs_must_commit_transaction(trans, inode))
5457 ret = 1;
5458 break;
5461 parent = dget_parent(parent);
5462 dput(old_parent);
5463 old_parent = parent;
5464 inode = BTRFS_I(d_inode(parent));
5467 dput(old_parent);
5468 out:
5469 return ret;
5472 struct btrfs_dir_list {
5473 u64 ino;
5474 struct list_head list;
5478 * Log the inodes of the new dentries of a directory. See log_dir_items() for
5479 * details about the why it is needed.
5480 * This is a recursive operation - if an existing dentry corresponds to a
5481 * directory, that directory's new entries are logged too (same behaviour as
5482 * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
5483 * the dentries point to we do not lock their i_mutex, otherwise lockdep
5484 * complains about the following circular lock dependency / possible deadlock:
5486 * CPU0 CPU1
5487 * ---- ----
5488 * lock(&type->i_mutex_dir_key#3/2);
5489 * lock(sb_internal#2);
5490 * lock(&type->i_mutex_dir_key#3/2);
5491 * lock(&sb->s_type->i_mutex_key#14);
5493 * Where sb_internal is the lock (a counter that works as a lock) acquired by
5494 * sb_start_intwrite() in btrfs_start_transaction().
5495 * Not locking i_mutex of the inodes is still safe because:
5497 * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
5498 * that while logging the inode new references (names) are added or removed
5499 * from the inode, leaving the logged inode item with a link count that does
5500 * not match the number of logged inode reference items. This is fine because
5501 * at log replay time we compute the real number of links and correct the
5502 * link count in the inode item (see replay_one_buffer() and
5503 * link_to_fixup_dir());
5505 * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
5506 * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
5507 * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
5508 * has a size that doesn't match the sum of the lengths of all the logged
5509 * names. This does not result in a problem because if a dir_item key is
5510 * logged but its matching dir_index key is not logged, at log replay time we
5511 * don't use it to replay the respective name (see replay_one_name()). On the
5512 * other hand if only the dir_index key ends up being logged, the respective
5513 * name is added to the fs/subvol tree with both the dir_item and dir_index
5514 * keys created (see replay_one_name()).
5515 * The directory's inode item with a wrong i_size is not a problem as well,
5516 * since we don't use it at log replay time to set the i_size in the inode
5517 * item of the fs/subvol tree (see overwrite_item()).
5519 static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5520 struct btrfs_root *root,
5521 struct btrfs_inode *start_inode,
5522 struct btrfs_log_ctx *ctx)
5524 struct btrfs_fs_info *fs_info = root->fs_info;
5525 struct btrfs_root *log = root->log_root;
5526 struct btrfs_path *path;
5527 LIST_HEAD(dir_list);
5528 struct btrfs_dir_list *dir_elem;
5529 int ret = 0;
5531 path = btrfs_alloc_path();
5532 if (!path)
5533 return -ENOMEM;
5535 dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
5536 if (!dir_elem) {
5537 btrfs_free_path(path);
5538 return -ENOMEM;
5540 dir_elem->ino = btrfs_ino(start_inode);
5541 list_add_tail(&dir_elem->list, &dir_list);
5543 while (!list_empty(&dir_list)) {
5544 struct extent_buffer *leaf;
5545 struct btrfs_key min_key;
5546 int nritems;
5547 int i;
5549 dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
5550 list);
5551 if (ret)
5552 goto next_dir_inode;
5554 min_key.objectid = dir_elem->ino;
5555 min_key.type = BTRFS_DIR_ITEM_KEY;
5556 min_key.offset = 0;
5557 again:
5558 btrfs_release_path(path);
5559 ret = btrfs_search_forward(log, &min_key, path, trans->transid);
5560 if (ret < 0) {
5561 goto next_dir_inode;
5562 } else if (ret > 0) {
5563 ret = 0;
5564 goto next_dir_inode;
5567 process_leaf:
5568 leaf = path->nodes[0];
5569 nritems = btrfs_header_nritems(leaf);
5570 for (i = path->slots[0]; i < nritems; i++) {
5571 struct btrfs_dir_item *di;
5572 struct btrfs_key di_key;
5573 struct inode *di_inode;
5574 struct btrfs_dir_list *new_dir_elem;
5575 int log_mode = LOG_INODE_EXISTS;
5576 int type;
5578 btrfs_item_key_to_cpu(leaf, &min_key, i);
5579 if (min_key.objectid != dir_elem->ino ||
5580 min_key.type != BTRFS_DIR_ITEM_KEY)
5581 goto next_dir_inode;
5583 di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
5584 type = btrfs_dir_type(leaf, di);
5585 if (btrfs_dir_transid(leaf, di) < trans->transid &&
5586 type != BTRFS_FT_DIR)
5587 continue;
5588 btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
5589 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5590 continue;
5592 btrfs_release_path(path);
5593 di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
5594 if (IS_ERR(di_inode)) {
5595 ret = PTR_ERR(di_inode);
5596 goto next_dir_inode;
5599 if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
5600 iput(di_inode);
5601 break;
5604 ctx->log_new_dentries = false;
5605 if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
5606 log_mode = LOG_INODE_ALL;
5607 ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
5608 log_mode, 0, LLONG_MAX, ctx);
5609 if (!ret &&
5610 btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
5611 ret = 1;
5612 iput(di_inode);
5613 if (ret)
5614 goto next_dir_inode;
5615 if (ctx->log_new_dentries) {
5616 new_dir_elem = kmalloc(sizeof(*new_dir_elem),
5617 GFP_NOFS);
5618 if (!new_dir_elem) {
5619 ret = -ENOMEM;
5620 goto next_dir_inode;
5622 new_dir_elem->ino = di_key.objectid;
5623 list_add_tail(&new_dir_elem->list, &dir_list);
5625 break;
5627 if (i == nritems) {
5628 ret = btrfs_next_leaf(log, path);
5629 if (ret < 0) {
5630 goto next_dir_inode;
5631 } else if (ret > 0) {
5632 ret = 0;
5633 goto next_dir_inode;
5635 goto process_leaf;
5637 if (min_key.offset < (u64)-1) {
5638 min_key.offset++;
5639 goto again;
5641 next_dir_inode:
5642 list_del(&dir_elem->list);
5643 kfree(dir_elem);
5646 btrfs_free_path(path);
5647 return ret;
5650 static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5651 struct btrfs_inode *inode,
5652 struct btrfs_log_ctx *ctx)
5654 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
5655 int ret;
5656 struct btrfs_path *path;
5657 struct btrfs_key key;
5658 struct btrfs_root *root = inode->root;
5659 const u64 ino = btrfs_ino(inode);
5661 path = btrfs_alloc_path();
5662 if (!path)
5663 return -ENOMEM;
5664 path->skip_locking = 1;
5665 path->search_commit_root = 1;
5667 key.objectid = ino;
5668 key.type = BTRFS_INODE_REF_KEY;
5669 key.offset = 0;
5670 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5671 if (ret < 0)
5672 goto out;
5674 while (true) {
5675 struct extent_buffer *leaf = path->nodes[0];
5676 int slot = path->slots[0];
5677 u32 cur_offset = 0;
5678 u32 item_size;
5679 unsigned long ptr;
5681 if (slot >= btrfs_header_nritems(leaf)) {
5682 ret = btrfs_next_leaf(root, path);
5683 if (ret < 0)
5684 goto out;
5685 else if (ret > 0)
5686 break;
5687 continue;
5690 btrfs_item_key_to_cpu(leaf, &key, slot);
5691 /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
5692 if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
5693 break;
5695 item_size = btrfs_item_size_nr(leaf, slot);
5696 ptr = btrfs_item_ptr_offset(leaf, slot);
5697 while (cur_offset < item_size) {
5698 struct btrfs_key inode_key;
5699 struct inode *dir_inode;
5701 inode_key.type = BTRFS_INODE_ITEM_KEY;
5702 inode_key.offset = 0;
5704 if (key.type == BTRFS_INODE_EXTREF_KEY) {
5705 struct btrfs_inode_extref *extref;
5707 extref = (struct btrfs_inode_extref *)
5708 (ptr + cur_offset);
5709 inode_key.objectid = btrfs_inode_extref_parent(
5710 leaf, extref);
5711 cur_offset += sizeof(*extref);
5712 cur_offset += btrfs_inode_extref_name_len(leaf,
5713 extref);
5714 } else {
5715 inode_key.objectid = key.offset;
5716 cur_offset = item_size;
5719 dir_inode = btrfs_iget(fs_info->sb, &inode_key,
5720 root, NULL);
5721 /* If parent inode was deleted, skip it. */
5722 if (IS_ERR(dir_inode))
5723 continue;
5725 if (ctx)
5726 ctx->log_new_dentries = false;
5727 ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
5728 LOG_INODE_ALL, 0, LLONG_MAX, ctx);
5729 if (!ret &&
5730 btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
5731 ret = 1;
5732 if (!ret && ctx && ctx->log_new_dentries)
5733 ret = log_new_dir_dentries(trans, root,
5734 BTRFS_I(dir_inode), ctx);
5735 iput(dir_inode);
5736 if (ret)
5737 goto out;
5739 path->slots[0]++;
5741 ret = 0;
5742 out:
5743 btrfs_free_path(path);
5744 return ret;
5748 * helper function around btrfs_log_inode to make sure newly created
5749 * parent directories also end up in the log. A minimal inode and backref
5750 * only logging is done of any parent directories that are older than
5751 * the last committed transaction
5753 static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5754 struct btrfs_inode *inode,
5755 struct dentry *parent,
5756 const loff_t start,
5757 const loff_t end,
5758 int inode_only,
5759 struct btrfs_log_ctx *ctx)
5761 struct btrfs_root *root = inode->root;
5762 struct btrfs_fs_info *fs_info = root->fs_info;
5763 struct super_block *sb;
5764 struct dentry *old_parent = NULL;
5765 int ret = 0;
5766 u64 last_committed = fs_info->last_trans_committed;
5767 bool log_dentries = false;
5768 struct btrfs_inode *orig_inode = inode;
5770 sb = inode->vfs_inode.i_sb;
5772 if (btrfs_test_opt(fs_info, NOTREELOG)) {
5773 ret = 1;
5774 goto end_no_trans;
5778 * The prev transaction commit doesn't complete, we need do
5779 * full commit by ourselves.
5781 if (fs_info->last_trans_log_full_commit >
5782 fs_info->last_trans_committed) {
5783 ret = 1;
5784 goto end_no_trans;
5787 if (btrfs_root_refs(&root->root_item) == 0) {
5788 ret = 1;
5789 goto end_no_trans;
5792 ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
5793 last_committed);
5794 if (ret)
5795 goto end_no_trans;
5797 if (btrfs_inode_in_log(inode, trans->transid)) {
5798 ret = BTRFS_NO_LOG_SYNC;
5799 goto end_no_trans;
5802 ret = start_log_trans(trans, root, ctx);
5803 if (ret)
5804 goto end_no_trans;
5806 ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
5807 if (ret)
5808 goto end_trans;
5811 * for regular files, if its inode is already on disk, we don't
5812 * have to worry about the parents at all. This is because
5813 * we can use the last_unlink_trans field to record renames
5814 * and other fun in this file.
5816 if (S_ISREG(inode->vfs_inode.i_mode) &&
5817 inode->generation <= last_committed &&
5818 inode->last_unlink_trans <= last_committed) {
5819 ret = 0;
5820 goto end_trans;
5823 if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
5824 log_dentries = true;
5827 * On unlink we must make sure all our current and old parent directory
5828 * inodes are fully logged. This is to prevent leaving dangling
5829 * directory index entries in directories that were our parents but are
5830 * not anymore. Not doing this results in old parent directory being
5831 * impossible to delete after log replay (rmdir will always fail with
5832 * error -ENOTEMPTY).
5834 * Example 1:
5836 * mkdir testdir
5837 * touch testdir/foo
5838 * ln testdir/foo testdir/bar
5839 * sync
5840 * unlink testdir/bar
5841 * xfs_io -c fsync testdir/foo
5842 * <power failure>
5843 * mount fs, triggers log replay
5845 * If we don't log the parent directory (testdir), after log replay the
5846 * directory still has an entry pointing to the file inode using the bar
5847 * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
5848 * the file inode has a link count of 1.
5850 * Example 2:
5852 * mkdir testdir
5853 * touch foo
5854 * ln foo testdir/foo2
5855 * ln foo testdir/foo3
5856 * sync
5857 * unlink testdir/foo3
5858 * xfs_io -c fsync foo
5859 * <power failure>
5860 * mount fs, triggers log replay
5862 * Similar as the first example, after log replay the parent directory
5863 * testdir still has an entry pointing to the inode file with name foo3
5864 * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
5865 * and has a link count of 2.
5867 if (inode->last_unlink_trans > last_committed) {
5868 ret = btrfs_log_all_parents(trans, orig_inode, ctx);
5869 if (ret)
5870 goto end_trans;
5873 while (1) {
5874 if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
5875 break;
5877 inode = BTRFS_I(d_inode(parent));
5878 if (root != inode->root)
5879 break;
5881 if (inode->generation > last_committed) {
5882 ret = btrfs_log_inode(trans, root, inode,
5883 LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
5884 if (ret)
5885 goto end_trans;
5887 if (IS_ROOT(parent))
5888 break;
5890 parent = dget_parent(parent);
5891 dput(old_parent);
5892 old_parent = parent;
5894 if (log_dentries)
5895 ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
5896 else
5897 ret = 0;
5898 end_trans:
5899 dput(old_parent);
5900 if (ret < 0) {
5901 btrfs_set_log_full_commit(fs_info, trans);
5902 ret = 1;
5905 if (ret)
5906 btrfs_remove_log_ctx(root, ctx);
5907 btrfs_end_log_trans(root);
5908 end_no_trans:
5909 return ret;
5913 * it is not safe to log dentry if the chunk root has added new
5914 * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
5915 * If this returns 1, you must commit the transaction to safely get your
5916 * data on disk.
5918 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
5919 struct dentry *dentry,
5920 const loff_t start,
5921 const loff_t end,
5922 struct btrfs_log_ctx *ctx)
5924 struct dentry *parent = dget_parent(dentry);
5925 int ret;
5927 ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
5928 start, end, LOG_INODE_ALL, ctx);
5929 dput(parent);
5931 return ret;
5935 * should be called during mount to recover any replay any log trees
5936 * from the FS
5938 int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5940 int ret;
5941 struct btrfs_path *path;
5942 struct btrfs_trans_handle *trans;
5943 struct btrfs_key key;
5944 struct btrfs_key found_key;
5945 struct btrfs_key tmp_key;
5946 struct btrfs_root *log;
5947 struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
5948 struct walk_control wc = {
5949 .process_func = process_one_buffer,
5950 .stage = 0,
5953 path = btrfs_alloc_path();
5954 if (!path)
5955 return -ENOMEM;
5957 set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
5959 trans = btrfs_start_transaction(fs_info->tree_root, 0);
5960 if (IS_ERR(trans)) {
5961 ret = PTR_ERR(trans);
5962 goto error;
5965 wc.trans = trans;
5966 wc.pin = 1;
5968 ret = walk_log_tree(trans, log_root_tree, &wc);
5969 if (ret) {
5970 btrfs_handle_fs_error(fs_info, ret,
5971 "Failed to pin buffers while recovering log root tree.");
5972 goto error;
5975 again:
5976 key.objectid = BTRFS_TREE_LOG_OBJECTID;
5977 key.offset = (u64)-1;
5978 key.type = BTRFS_ROOT_ITEM_KEY;
5980 while (1) {
5981 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
5983 if (ret < 0) {
5984 btrfs_handle_fs_error(fs_info, ret,
5985 "Couldn't find tree log root.");
5986 goto error;
5988 if (ret > 0) {
5989 if (path->slots[0] == 0)
5990 break;
5991 path->slots[0]--;
5993 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
5994 path->slots[0]);
5995 btrfs_release_path(path);
5996 if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
5997 break;
5999 log = btrfs_read_fs_root(log_root_tree, &found_key);
6000 if (IS_ERR(log)) {
6001 ret = PTR_ERR(log);
6002 btrfs_handle_fs_error(fs_info, ret,
6003 "Couldn't read tree log root.");
6004 goto error;
6007 tmp_key.objectid = found_key.offset;
6008 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
6009 tmp_key.offset = (u64)-1;
6011 wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
6012 if (IS_ERR(wc.replay_dest)) {
6013 ret = PTR_ERR(wc.replay_dest);
6014 free_extent_buffer(log->node);
6015 free_extent_buffer(log->commit_root);
6016 kfree(log);
6017 btrfs_handle_fs_error(fs_info, ret,
6018 "Couldn't read target root for tree log recovery.");
6019 goto error;
6022 wc.replay_dest->log_root = log;
6023 btrfs_record_root_in_trans(trans, wc.replay_dest);
6024 ret = walk_log_tree(trans, log, &wc);
6026 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
6027 ret = fixup_inode_link_counts(trans, wc.replay_dest,
6028 path);
6031 if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
6032 struct btrfs_root *root = wc.replay_dest;
6034 btrfs_release_path(path);
6037 * We have just replayed everything, and the highest
6038 * objectid of fs roots probably has changed in case
6039 * some inode_item's got replayed.
6041 * root->objectid_mutex is not acquired as log replay
6042 * could only happen during mount.
6044 ret = btrfs_find_highest_objectid(root,
6045 &root->highest_objectid);
6048 key.offset = found_key.offset - 1;
6049 wc.replay_dest->log_root = NULL;
6050 free_extent_buffer(log->node);
6051 free_extent_buffer(log->commit_root);
6052 kfree(log);
6054 if (ret)
6055 goto error;
6057 if (found_key.offset == 0)
6058 break;
6060 btrfs_release_path(path);
6062 /* step one is to pin it all, step two is to replay just inodes */
6063 if (wc.pin) {
6064 wc.pin = 0;
6065 wc.process_func = replay_one_buffer;
6066 wc.stage = LOG_WALK_REPLAY_INODES;
6067 goto again;
6069 /* step three is to replay everything */
6070 if (wc.stage < LOG_WALK_REPLAY_ALL) {
6071 wc.stage++;
6072 goto again;
6075 btrfs_free_path(path);
6077 /* step 4: commit the transaction, which also unpins the blocks */
6078 ret = btrfs_commit_transaction(trans);
6079 if (ret)
6080 return ret;
6082 free_extent_buffer(log_root_tree->node);
6083 log_root_tree->log_root = NULL;
6084 clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
6085 kfree(log_root_tree);
6087 return 0;
6088 error:
6089 if (wc.trans)
6090 btrfs_end_transaction(wc.trans);
6091 btrfs_free_path(path);
6092 return ret;
6096 * there are some corner cases where we want to force a full
6097 * commit instead of allowing a directory to be logged.
6099 * They revolve around files there were unlinked from the directory, and
6100 * this function updates the parent directory so that a full commit is
6101 * properly done if it is fsync'd later after the unlinks are done.
6103 * Must be called before the unlink operations (updates to the subvolume tree,
6104 * inodes, etc) are done.
6106 void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
6107 struct btrfs_inode *dir, struct btrfs_inode *inode,
6108 int for_rename)
6111 * when we're logging a file, if it hasn't been renamed
6112 * or unlinked, and its inode is fully committed on disk,
6113 * we don't have to worry about walking up the directory chain
6114 * to log its parents.
6116 * So, we use the last_unlink_trans field to put this transid
6117 * into the file. When the file is logged we check it and
6118 * don't log the parents if the file is fully on disk.
6120 mutex_lock(&inode->log_mutex);
6121 inode->last_unlink_trans = trans->transid;
6122 mutex_unlock(&inode->log_mutex);
6125 * if this directory was already logged any new
6126 * names for this file/dir will get recorded
6128 smp_mb();
6129 if (dir->logged_trans == trans->transid)
6130 return;
6133 * if the inode we're about to unlink was logged,
6134 * the log will be properly updated for any new names
6136 if (inode->logged_trans == trans->transid)
6137 return;
6140 * when renaming files across directories, if the directory
6141 * there we're unlinking from gets fsync'd later on, there's
6142 * no way to find the destination directory later and fsync it
6143 * properly. So, we have to be conservative and force commits
6144 * so the new name gets discovered.
6146 if (for_rename)
6147 goto record;
6149 /* we can safely do the unlink without any special recording */
6150 return;
6152 record:
6153 mutex_lock(&dir->log_mutex);
6154 dir->last_unlink_trans = trans->transid;
6155 mutex_unlock(&dir->log_mutex);
6159 * Make sure that if someone attempts to fsync the parent directory of a deleted
6160 * snapshot, it ends up triggering a transaction commit. This is to guarantee
6161 * that after replaying the log tree of the parent directory's root we will not
6162 * see the snapshot anymore and at log replay time we will not see any log tree
6163 * corresponding to the deleted snapshot's root, which could lead to replaying
6164 * it after replaying the log tree of the parent directory (which would replay
6165 * the snapshot delete operation).
6167 * Must be called before the actual snapshot destroy operation (updates to the
6168 * parent root and tree of tree roots trees, etc) are done.
6170 void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
6171 struct btrfs_inode *dir)
6173 mutex_lock(&dir->log_mutex);
6174 dir->last_unlink_trans = trans->transid;
6175 mutex_unlock(&dir->log_mutex);
6179 * Call this after adding a new name for a file and it will properly
6180 * update the log to reflect the new name.
6182 * It will return zero if all goes well, and it will return 1 if a
6183 * full transaction commit is required.
6185 int btrfs_log_new_name(struct btrfs_trans_handle *trans,
6186 struct btrfs_inode *inode, struct btrfs_inode *old_dir,
6187 struct dentry *parent)
6189 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
6192 * this will force the logging code to walk the dentry chain
6193 * up for the file
6195 if (!S_ISDIR(inode->vfs_inode.i_mode))
6196 inode->last_unlink_trans = trans->transid;
6199 * if this inode hasn't been logged and directory we're renaming it
6200 * from hasn't been logged, we don't need to log it
6202 if (inode->logged_trans <= fs_info->last_trans_committed &&
6203 (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
6204 return 0;
6206 return btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
6207 LOG_INODE_EXISTS, NULL);