1 // SPDX-License-Identifier: GPL-2.0
4 * fs/ext4/fast_commit.c
6 * Written by Harshad Shirwadkar <harshadshirwadkar@gmail.com>
8 * Ext4 fast commits routines.
11 #include "ext4_jbd2.h"
12 #include "ext4_extents.h"
15 #include <linux/lockdep.h>
20 * Ext4 fast commits implement fine grained journalling for Ext4.
22 * Fast commits are organized as a log of tag-length-value (TLV) structs. (See
23 * struct ext4_fc_tl). Each TLV contains some delta that is replayed TLV by
24 * TLV during the recovery phase. For the scenarios for which we currently
25 * don't have replay code, fast commit falls back to full commits.
26 * Fast commits record delta in one of the following three categories.
28 * (A) Directory entry updates:
30 * - EXT4_FC_TAG_UNLINK - records directory entry unlink
31 * - EXT4_FC_TAG_LINK - records directory entry link
32 * - EXT4_FC_TAG_CREAT - records inode and directory entry creation
34 * (B) File specific data range updates:
36 * - EXT4_FC_TAG_ADD_RANGE - records addition of new blocks to an inode
37 * - EXT4_FC_TAG_DEL_RANGE - records deletion of blocks from an inode
39 * (C) Inode metadata (mtime / ctime etc):
41 * - EXT4_FC_TAG_INODE - record the inode that should be replayed
42 * during recovery. Note that iblocks field is
43 * not replayed and instead derived during
47 * With fast commits, we maintain all the directory entry operations in the
48 * order in which they are issued in an in-memory queue. This queue is flushed
49 * to disk during the commit operation. We also maintain a list of inodes
50 * that need to be committed during a fast commit in another in memory queue of
51 * inodes. During the commit operation, we commit in the following order:
53 * [1] Prepare all the inodes to write out their data by setting
54 * "EXT4_STATE_FC_FLUSHING_DATA". This ensures that inode cannot be
55 * deleted while it is being flushed.
56 * [2] Flush data buffers to disk and clear "EXT4_STATE_FC_FLUSHING_DATA"
58 * [3] Lock the journal by calling jbd2_journal_lock_updates. This ensures that
59 * all the exsiting handles finish and no new handles can start.
60 * [4] Mark all the fast commit eligible inodes as undergoing fast commit
61 * by setting "EXT4_STATE_FC_COMMITTING" state.
62 * [5] Unlock the journal by calling jbd2_journal_unlock_updates. This allows
63 * starting of new handles. If new handles try to start an update on
64 * any of the inodes that are being committed, ext4_fc_track_inode()
65 * will block until those inodes have finished the fast commit.
66 * [6] Commit all the directory entry updates in the fast commit space.
67 * [7] Commit all the changed inodes in the fast commit space and clear
68 * "EXT4_STATE_FC_COMMITTING" for these inodes.
69 * [8] Write tail tag (this tag ensures the atomicity, please read the following
70 * section for more details).
72 * All the inode updates must be enclosed within jbd2_jounrnal_start()
73 * and jbd2_journal_stop() similar to JBD2 journaling.
75 * Fast Commit Ineligibility
76 * -------------------------
78 * Not all operations are supported by fast commits today (e.g extended
79 * attributes). Fast commit ineligibility is marked by calling
80 * ext4_fc_mark_ineligible(): This makes next fast commit operation to fall back
83 * Atomicity of commits
84 * --------------------
85 * In order to guarantee atomicity during the commit operation, fast commit
86 * uses "EXT4_FC_TAG_TAIL" tag that marks a fast commit as complete. Tail
87 * tag contains CRC of the contents and TID of the transaction after which
88 * this fast commit should be applied. Recovery code replays fast commit
89 * logs only if there's at least 1 valid tail present. For every fast commit
90 * operation, there is 1 tail. This means, we may end up with multiple tails
91 * in the fast commit space. Here's an example:
93 * - Create a new file A and remove existing file B
95 * - Append contents to file A
99 * The fast commit space at the end of above operations would look like this:
100 * [HEAD] [CREAT A] [UNLINK B] [TAIL] [ADD_RANGE A] [DEL_RANGE A] [TAIL]
101 * |<--- Fast Commit 1 --->|<--- Fast Commit 2 ---->|
103 * Replay code should thus check for all the valid tails in the FC area.
105 * Fast Commit Replay Idempotence
106 * ------------------------------
108 * Fast commits tags are idempotent in nature provided the recovery code follows
109 * certain rules. The guiding principle that the commit path follows while
110 * committing is that it stores the result of a particular operation instead of
111 * storing the procedure.
113 * Let's consider this rename operation: 'mv /a /b'. Let's assume dirent '/a'
114 * was associated with inode 10. During fast commit, instead of storing this
115 * operation as a procedure "rename a to b", we store the resulting file system
116 * state as a "series" of outcomes:
118 * - Link dirent b to inode 10
120 * - Inode <10> with valid refcount
122 * Now when recovery code runs, it needs "enforce" this state on the file
123 * system. This is what guarantees idempotence of fast commit replay.
125 * Let's take an example of a procedure that is not idempotent and see how fast
126 * commits make it idempotent. Consider following sequence of operations:
128 * rm A; mv B A; read A
131 * (x), (y) and (z) are the points at which we can crash. If we store this
132 * sequence of operations as is then the replay is not idempotent. Let's say
133 * while in replay, we crash at (z). During the second replay, file A (which was
134 * actually created as a result of "mv B A" operation) would get deleted. Thus,
135 * file named A would be absent when we try to read A. So, this sequence of
136 * operations is not idempotent. However, as mentioned above, instead of storing
137 * the procedure fast commits store the outcome of each procedure. Thus the fast
138 * commit log for above procedure would be as follows:
140 * (Let's assume dirent A was linked to inode 10 and dirent B was linked to
141 * inode 11 before the replay)
143 * [Unlink A] [Link A to inode 11] [Unlink B] [Inode 11]
146 * If we crash at (z), we will have file A linked to inode 11. During the second
147 * replay, we will remove file A (inode 11). But we will create it back and make
148 * it point to inode 11. We won't find B, so we'll just skip that step. At this
149 * point, the refcount for inode 11 is not reliable, but that gets fixed by the
150 * replay of last inode 11 tag. Crashes at points (w), (x) and (y) get handled
151 * similarly. Thus, by converting a non-idempotent procedure into a series of
152 * idempotent outcomes, fast commits ensured idempotence during the replay.
156 * sbi->s_fc_lock protects the fast commit inodes queue and the fast commit
157 * dentry queue. ei->i_fc_lock protects the fast commit related info in a given
158 * inode. Most of the code avoids acquiring both the locks, but if one must do
159 * that then sbi->s_fc_lock must be acquired before ei->i_fc_lock.
164 * 0) Fast commit replay path hardening: Fast commit replay code should use
165 * journal handles to make sure all the updates it does during the replay
166 * path are atomic. With that if we crash during fast commit replay, after
167 * trying to do recovery again, we will find a file system where fast commit
168 * area is invalid (because new full commit would be found). In order to deal
169 * with that, fast commit replay code should ensure that the "FC_REPLAY"
170 * superblock state is persisted before starting the replay, so that after
171 * the crash, fast commit recovery code can look at that flag and perform
172 * fast commit recovery even if that area is invalidated by later full
175 * 1) Handle more ineligible cases.
177 * 2) Change ext4_fc_commit() to lookup logical to physical mapping using extent
178 * status tree. This would get rid of the need to call ext4_fc_track_inode()
179 * before acquiring i_data_sem. To do that we would need to ensure that
180 * modified extents from the extent status tree are not evicted from memory.
183 #include <trace/events/ext4.h>
184 static struct kmem_cache *ext4_fc_dentry_cachep;
186 static void ext4_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
188 BUFFER_TRACE(bh, "");
190 ext4_debug("%s: Block %lld up-to-date",
191 __func__, bh->b_blocknr);
192 set_buffer_uptodate(bh);
194 ext4_debug("%s: Block %lld not up-to-date",
195 __func__, bh->b_blocknr);
196 clear_buffer_uptodate(bh);
202 static inline void ext4_fc_reset_inode(struct inode *inode)
204 struct ext4_inode_info *ei = EXT4_I(inode);
206 ei->i_fc_lblk_start = 0;
207 ei->i_fc_lblk_len = 0;
210 void ext4_fc_init_inode(struct inode *inode)
212 struct ext4_inode_info *ei = EXT4_I(inode);
214 ext4_fc_reset_inode(inode);
215 ext4_clear_inode_state(inode, EXT4_STATE_FC_COMMITTING);
216 INIT_LIST_HEAD(&ei->i_fc_list);
217 INIT_LIST_HEAD(&ei->i_fc_dilist);
218 init_waitqueue_head(&ei->i_fc_wait);
221 static bool ext4_fc_disabled(struct super_block *sb)
223 return (!test_opt2(sb, JOURNAL_FAST_COMMIT) ||
224 (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY));
228 * Remove inode from fast commit list. If the inode is being committed
229 * we wait until inode commit is done.
231 void ext4_fc_del(struct inode *inode)
233 struct ext4_inode_info *ei = EXT4_I(inode);
234 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
235 struct ext4_fc_dentry_update *fc_dentry;
236 wait_queue_head_t *wq;
238 if (ext4_fc_disabled(inode->i_sb))
241 mutex_lock(&sbi->s_fc_lock);
242 if (list_empty(&ei->i_fc_list) && list_empty(&ei->i_fc_dilist)) {
243 mutex_unlock(&sbi->s_fc_lock);
248 * Since ext4_fc_del is called from ext4_evict_inode while having a
249 * handle open, there is no need for us to wait here even if a fast
250 * commit is going on. That is because, if this inode is being
251 * committed, ext4_mark_inode_dirty would have waited for inode commit
252 * operation to finish before we come here. So, by the time we come
253 * here, inode's EXT4_STATE_FC_COMMITTING would have been cleared. So,
254 * we shouldn't see EXT4_STATE_FC_COMMITTING to be set on this inode
257 * We may come here without any handles open in the "no_delete" case of
258 * ext4_evict_inode as well. However, if that happens, we first mark the
259 * file system as fast commit ineligible anyway. So, even in that case,
260 * it is okay to remove the inode from the fc list.
262 WARN_ON(ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING)
263 && !ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_INELIGIBLE));
264 while (ext4_test_inode_state(inode, EXT4_STATE_FC_FLUSHING_DATA)) {
265 #if (BITS_PER_LONG < 64)
266 DEFINE_WAIT_BIT(wait, &ei->i_state_flags,
267 EXT4_STATE_FC_FLUSHING_DATA);
268 wq = bit_waitqueue(&ei->i_state_flags,
269 EXT4_STATE_FC_FLUSHING_DATA);
271 DEFINE_WAIT_BIT(wait, &ei->i_flags,
272 EXT4_STATE_FC_FLUSHING_DATA);
273 wq = bit_waitqueue(&ei->i_flags,
274 EXT4_STATE_FC_FLUSHING_DATA);
276 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
277 if (ext4_test_inode_state(inode, EXT4_STATE_FC_FLUSHING_DATA)) {
278 mutex_unlock(&sbi->s_fc_lock);
280 mutex_lock(&sbi->s_fc_lock);
282 finish_wait(wq, &wait.wq_entry);
284 list_del_init(&ei->i_fc_list);
287 * Since this inode is getting removed, let's also remove all FC
288 * dentry create references, since it is not needed to log it anyways.
290 if (list_empty(&ei->i_fc_dilist)) {
291 mutex_unlock(&sbi->s_fc_lock);
295 fc_dentry = list_first_entry(&ei->i_fc_dilist, struct ext4_fc_dentry_update, fcd_dilist);
296 WARN_ON(fc_dentry->fcd_op != EXT4_FC_TAG_CREAT);
297 list_del_init(&fc_dentry->fcd_list);
298 list_del_init(&fc_dentry->fcd_dilist);
300 WARN_ON(!list_empty(&ei->i_fc_dilist));
301 mutex_unlock(&sbi->s_fc_lock);
303 release_dentry_name_snapshot(&fc_dentry->fcd_name);
304 kmem_cache_free(ext4_fc_dentry_cachep, fc_dentry);
308 * Mark file system as fast commit ineligible, and record latest
309 * ineligible transaction tid. This means until the recorded
310 * transaction, commit operation would result in a full jbd2 commit.
312 void ext4_fc_mark_ineligible(struct super_block *sb, int reason, handle_t *handle)
314 struct ext4_sb_info *sbi = EXT4_SB(sb);
316 bool has_transaction = true;
319 if (ext4_fc_disabled(sb))
322 if (handle && !IS_ERR(handle))
323 tid = handle->h_transaction->t_tid;
325 read_lock(&sbi->s_journal->j_state_lock);
326 if (sbi->s_journal->j_running_transaction)
327 tid = sbi->s_journal->j_running_transaction->t_tid;
329 has_transaction = false;
330 read_unlock(&sbi->s_journal->j_state_lock);
332 mutex_lock(&sbi->s_fc_lock);
333 is_ineligible = ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
334 if (has_transaction && (!is_ineligible || tid_gt(tid, sbi->s_fc_ineligible_tid)))
335 sbi->s_fc_ineligible_tid = tid;
336 ext4_set_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
337 mutex_unlock(&sbi->s_fc_lock);
338 WARN_ON(reason >= EXT4_FC_REASON_MAX);
339 sbi->s_fc_stats.fc_ineligible_reason_count[reason]++;
343 * Generic fast commit tracking function. If this is the first time this we are
344 * called after a full commit, we initialize fast commit fields and then call
345 * __fc_track_fn() with update = 0. If we have already been called after a full
346 * commit, we pass update = 1. Based on that, the track function can determine
347 * if it needs to track a field for the first time or if it needs to just
348 * update the previously tracked value.
350 * If enqueue is set, this function enqueues the inode in fast commit list.
352 static int ext4_fc_track_template(
353 handle_t *handle, struct inode *inode,
354 int (*__fc_track_fn)(handle_t *handle, struct inode *, void *, bool),
355 void *args, int enqueue)
358 struct ext4_inode_info *ei = EXT4_I(inode);
359 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
363 tid = handle->h_transaction->t_tid;
364 spin_lock(&ei->i_fc_lock);
365 if (tid == ei->i_sync_tid) {
368 ext4_fc_reset_inode(inode);
369 ei->i_sync_tid = tid;
371 ret = __fc_track_fn(handle, inode, args, update);
372 spin_unlock(&ei->i_fc_lock);
376 mutex_lock(&sbi->s_fc_lock);
377 if (list_empty(&EXT4_I(inode)->i_fc_list))
378 list_add_tail(&EXT4_I(inode)->i_fc_list,
379 (sbi->s_journal->j_flags & JBD2_FULL_COMMIT_ONGOING ||
380 sbi->s_journal->j_flags & JBD2_FAST_COMMIT_ONGOING) ?
381 &sbi->s_fc_q[FC_Q_STAGING] :
382 &sbi->s_fc_q[FC_Q_MAIN]);
383 mutex_unlock(&sbi->s_fc_lock);
388 struct __track_dentry_update_args {
389 struct dentry *dentry;
393 /* __track_fn for directory entry updates. Called with ei->i_fc_lock. */
394 static int __track_dentry_update(handle_t *handle, struct inode *inode,
395 void *arg, bool update)
397 struct ext4_fc_dentry_update *node;
398 struct ext4_inode_info *ei = EXT4_I(inode);
399 struct __track_dentry_update_args *dentry_update =
400 (struct __track_dentry_update_args *)arg;
401 struct dentry *dentry = dentry_update->dentry;
402 struct inode *dir = dentry->d_parent->d_inode;
403 struct super_block *sb = inode->i_sb;
404 struct ext4_sb_info *sbi = EXT4_SB(sb);
406 spin_unlock(&ei->i_fc_lock);
408 if (IS_ENCRYPTED(dir)) {
409 ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_ENCRYPTED_FILENAME,
411 spin_lock(&ei->i_fc_lock);
415 node = kmem_cache_alloc(ext4_fc_dentry_cachep, GFP_NOFS);
417 ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_NOMEM, handle);
418 spin_lock(&ei->i_fc_lock);
422 node->fcd_op = dentry_update->op;
423 node->fcd_parent = dir->i_ino;
424 node->fcd_ino = inode->i_ino;
425 take_dentry_name_snapshot(&node->fcd_name, dentry);
426 INIT_LIST_HEAD(&node->fcd_dilist);
427 INIT_LIST_HEAD(&node->fcd_list);
428 mutex_lock(&sbi->s_fc_lock);
429 if (sbi->s_journal->j_flags & JBD2_FULL_COMMIT_ONGOING ||
430 sbi->s_journal->j_flags & JBD2_FAST_COMMIT_ONGOING)
431 list_add_tail(&node->fcd_list,
432 &sbi->s_fc_dentry_q[FC_Q_STAGING]);
434 list_add_tail(&node->fcd_list, &sbi->s_fc_dentry_q[FC_Q_MAIN]);
437 * This helps us keep a track of all fc_dentry updates which is part of
438 * this ext4 inode. So in case the inode is getting unlinked, before
439 * even we get a chance to fsync, we could remove all fc_dentry
440 * references while evicting the inode in ext4_fc_del().
441 * Also with this, we don't need to loop over all the inodes in
442 * sbi->s_fc_q to get the corresponding inode in
443 * ext4_fc_commit_dentry_updates().
445 if (dentry_update->op == EXT4_FC_TAG_CREAT) {
446 WARN_ON(!list_empty(&ei->i_fc_dilist));
447 list_add_tail(&node->fcd_dilist, &ei->i_fc_dilist);
449 mutex_unlock(&sbi->s_fc_lock);
450 spin_lock(&ei->i_fc_lock);
455 void __ext4_fc_track_unlink(handle_t *handle,
456 struct inode *inode, struct dentry *dentry)
458 struct __track_dentry_update_args args;
461 args.dentry = dentry;
462 args.op = EXT4_FC_TAG_UNLINK;
464 ret = ext4_fc_track_template(handle, inode, __track_dentry_update,
466 trace_ext4_fc_track_unlink(handle, inode, dentry, ret);
469 void ext4_fc_track_unlink(handle_t *handle, struct dentry *dentry)
471 struct inode *inode = d_inode(dentry);
473 if (ext4_fc_disabled(inode->i_sb))
476 if (ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_INELIGIBLE))
479 __ext4_fc_track_unlink(handle, inode, dentry);
482 void __ext4_fc_track_link(handle_t *handle,
483 struct inode *inode, struct dentry *dentry)
485 struct __track_dentry_update_args args;
488 args.dentry = dentry;
489 args.op = EXT4_FC_TAG_LINK;
491 ret = ext4_fc_track_template(handle, inode, __track_dentry_update,
493 trace_ext4_fc_track_link(handle, inode, dentry, ret);
496 void ext4_fc_track_link(handle_t *handle, struct dentry *dentry)
498 struct inode *inode = d_inode(dentry);
500 if (ext4_fc_disabled(inode->i_sb))
503 if (ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_INELIGIBLE))
506 __ext4_fc_track_link(handle, inode, dentry);
509 void __ext4_fc_track_create(handle_t *handle, struct inode *inode,
510 struct dentry *dentry)
512 struct __track_dentry_update_args args;
515 args.dentry = dentry;
516 args.op = EXT4_FC_TAG_CREAT;
518 ret = ext4_fc_track_template(handle, inode, __track_dentry_update,
520 trace_ext4_fc_track_create(handle, inode, dentry, ret);
523 void ext4_fc_track_create(handle_t *handle, struct dentry *dentry)
525 struct inode *inode = d_inode(dentry);
527 if (ext4_fc_disabled(inode->i_sb))
530 if (ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_INELIGIBLE))
533 __ext4_fc_track_create(handle, inode, dentry);
536 /* __track_fn for inode tracking */
537 static int __track_inode(handle_t *handle, struct inode *inode, void *arg,
543 EXT4_I(inode)->i_fc_lblk_len = 0;
548 void ext4_fc_track_inode(handle_t *handle, struct inode *inode)
550 struct ext4_inode_info *ei = EXT4_I(inode);
551 wait_queue_head_t *wq;
554 if (S_ISDIR(inode->i_mode))
557 if (ext4_fc_disabled(inode->i_sb))
560 if (ext4_should_journal_data(inode)) {
561 ext4_fc_mark_ineligible(inode->i_sb,
562 EXT4_FC_REASON_INODE_JOURNAL_DATA, handle);
566 if (ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_INELIGIBLE))
570 * If we come here, we may sleep while waiting for the inode to
571 * commit. We shouldn't be holding i_data_sem when we go to sleep since
572 * the commit path needs to grab the lock while committing the inode.
574 lockdep_assert_not_held(&ei->i_data_sem);
576 while (ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING)) {
577 #if (BITS_PER_LONG < 64)
578 DEFINE_WAIT_BIT(wait, &ei->i_state_flags,
579 EXT4_STATE_FC_COMMITTING);
580 wq = bit_waitqueue(&ei->i_state_flags,
581 EXT4_STATE_FC_COMMITTING);
583 DEFINE_WAIT_BIT(wait, &ei->i_flags,
584 EXT4_STATE_FC_COMMITTING);
585 wq = bit_waitqueue(&ei->i_flags,
586 EXT4_STATE_FC_COMMITTING);
588 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
589 if (ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING))
591 finish_wait(wq, &wait.wq_entry);
595 * From this point on, this inode will not be committed either
596 * by fast or full commit as long as the handle is open.
598 ret = ext4_fc_track_template(handle, inode, __track_inode, NULL, 1);
599 trace_ext4_fc_track_inode(handle, inode, ret);
602 struct __track_range_args {
603 ext4_lblk_t start, end;
606 /* __track_fn for tracking data updates */
607 static int __track_range(handle_t *handle, struct inode *inode, void *arg,
610 struct ext4_inode_info *ei = EXT4_I(inode);
611 ext4_lblk_t oldstart;
612 struct __track_range_args *__arg =
613 (struct __track_range_args *)arg;
615 if (inode->i_ino < EXT4_FIRST_INO(inode->i_sb)) {
616 ext4_debug("Special inode %ld being modified\n", inode->i_ino);
620 oldstart = ei->i_fc_lblk_start;
622 if (update && ei->i_fc_lblk_len > 0) {
623 ei->i_fc_lblk_start = min(ei->i_fc_lblk_start, __arg->start);
625 max(oldstart + ei->i_fc_lblk_len - 1, __arg->end) -
626 ei->i_fc_lblk_start + 1;
628 ei->i_fc_lblk_start = __arg->start;
629 ei->i_fc_lblk_len = __arg->end - __arg->start + 1;
635 void ext4_fc_track_range(handle_t *handle, struct inode *inode, ext4_lblk_t start,
638 struct __track_range_args args;
641 if (S_ISDIR(inode->i_mode))
644 if (ext4_fc_disabled(inode->i_sb))
647 if (ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_INELIGIBLE))
650 if (ext4_has_inline_data(inode)) {
651 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR,
659 ret = ext4_fc_track_template(handle, inode, __track_range, &args, 1);
661 trace_ext4_fc_track_range(handle, inode, start, end, ret);
664 static void ext4_fc_submit_bh(struct super_block *sb, bool is_tail)
666 blk_opf_t write_flags = REQ_SYNC;
667 struct buffer_head *bh = EXT4_SB(sb)->s_fc_bh;
669 /* Add REQ_FUA | REQ_PREFLUSH only its tail */
670 if (test_opt(sb, BARRIER) && is_tail)
671 write_flags |= REQ_FUA | REQ_PREFLUSH;
673 set_buffer_dirty(bh);
674 set_buffer_uptodate(bh);
675 bh->b_end_io = ext4_end_buffer_io_sync;
676 submit_bh(REQ_OP_WRITE | write_flags, bh);
677 EXT4_SB(sb)->s_fc_bh = NULL;
680 /* Ext4 commit path routines */
683 * Allocate len bytes on a fast commit buffer.
685 * During the commit time this function is used to manage fast commit
686 * block space. We don't split a fast commit log onto different
687 * blocks. So this function makes sure that if there's not enough space
688 * on the current block, the remaining space in the current block is
689 * marked as unused by adding EXT4_FC_TAG_PAD tag. In that case,
690 * new block is from jbd2 and CRC is updated to reflect the padding
693 static u8 *ext4_fc_reserve_space(struct super_block *sb, int len, u32 *crc)
695 struct ext4_fc_tl tl;
696 struct ext4_sb_info *sbi = EXT4_SB(sb);
697 struct buffer_head *bh;
698 int bsize = sbi->s_journal->j_blocksize;
699 int ret, off = sbi->s_fc_bytes % bsize;
704 * If 'len' is too long to fit in any block alongside a PAD tlv, then we
705 * cannot fulfill the request.
707 if (len > bsize - EXT4_FC_TAG_BASE_LEN)
711 ret = jbd2_fc_get_buf(EXT4_SB(sb)->s_journal, &bh);
716 dst = sbi->s_fc_bh->b_data + off;
719 * Allocate the bytes in the current block if we can do so while still
720 * leaving enough space for a PAD tlv.
722 remaining = bsize - EXT4_FC_TAG_BASE_LEN - off;
723 if (len <= remaining) {
724 sbi->s_fc_bytes += len;
729 * Else, terminate the current block with a PAD tlv, then allocate a new
730 * block and allocate the bytes at the start of that new block.
733 tl.fc_tag = cpu_to_le16(EXT4_FC_TAG_PAD);
734 tl.fc_len = cpu_to_le16(remaining);
735 memcpy(dst, &tl, EXT4_FC_TAG_BASE_LEN);
736 memset(dst + EXT4_FC_TAG_BASE_LEN, 0, remaining);
737 *crc = ext4_chksum(*crc, sbi->s_fc_bh->b_data, bsize);
739 ext4_fc_submit_bh(sb, false);
741 ret = jbd2_fc_get_buf(EXT4_SB(sb)->s_journal, &bh);
745 sbi->s_fc_bytes += bsize - off + len;
746 return sbi->s_fc_bh->b_data;
750 * Complete a fast commit by writing tail tag.
752 * Writing tail tag marks the end of a fast commit. In order to guarantee
753 * atomicity, after writing tail tag, even if there's space remaining
754 * in the block, next commit shouldn't use it. That's why tail tag
755 * has the length as that of the remaining space on the block.
757 static int ext4_fc_write_tail(struct super_block *sb, u32 crc)
759 struct ext4_sb_info *sbi = EXT4_SB(sb);
760 struct ext4_fc_tl tl;
761 struct ext4_fc_tail tail;
762 int off, bsize = sbi->s_journal->j_blocksize;
766 * ext4_fc_reserve_space takes care of allocating an extra block if
767 * there's no enough space on this block for accommodating this tail.
769 dst = ext4_fc_reserve_space(sb, EXT4_FC_TAG_BASE_LEN + sizeof(tail), &crc);
773 off = sbi->s_fc_bytes % bsize;
775 tl.fc_tag = cpu_to_le16(EXT4_FC_TAG_TAIL);
776 tl.fc_len = cpu_to_le16(bsize - off + sizeof(struct ext4_fc_tail));
777 sbi->s_fc_bytes = round_up(sbi->s_fc_bytes, bsize);
779 memcpy(dst, &tl, EXT4_FC_TAG_BASE_LEN);
780 dst += EXT4_FC_TAG_BASE_LEN;
781 tail.fc_tid = cpu_to_le32(sbi->s_journal->j_running_transaction->t_tid);
782 memcpy(dst, &tail.fc_tid, sizeof(tail.fc_tid));
783 dst += sizeof(tail.fc_tid);
784 crc = ext4_chksum(crc, sbi->s_fc_bh->b_data,
785 dst - (u8 *)sbi->s_fc_bh->b_data);
786 tail.fc_crc = cpu_to_le32(crc);
787 memcpy(dst, &tail.fc_crc, sizeof(tail.fc_crc));
788 dst += sizeof(tail.fc_crc);
789 memset(dst, 0, bsize - off); /* Don't leak uninitialized memory. */
791 ext4_fc_submit_bh(sb, true);
797 * Adds tag, length, value and updates CRC. Returns true if tlv was added.
798 * Returns false if there's not enough space.
800 static bool ext4_fc_add_tlv(struct super_block *sb, u16 tag, u16 len, u8 *val,
803 struct ext4_fc_tl tl;
806 dst = ext4_fc_reserve_space(sb, EXT4_FC_TAG_BASE_LEN + len, crc);
810 tl.fc_tag = cpu_to_le16(tag);
811 tl.fc_len = cpu_to_le16(len);
813 memcpy(dst, &tl, EXT4_FC_TAG_BASE_LEN);
814 memcpy(dst + EXT4_FC_TAG_BASE_LEN, val, len);
819 /* Same as above, but adds dentry tlv. */
820 static bool ext4_fc_add_dentry_tlv(struct super_block *sb, u32 *crc,
821 struct ext4_fc_dentry_update *fc_dentry)
823 struct ext4_fc_dentry_info fcd;
824 struct ext4_fc_tl tl;
825 int dlen = fc_dentry->fcd_name.name.len;
826 u8 *dst = ext4_fc_reserve_space(sb,
827 EXT4_FC_TAG_BASE_LEN + sizeof(fcd) + dlen, crc);
832 fcd.fc_parent_ino = cpu_to_le32(fc_dentry->fcd_parent);
833 fcd.fc_ino = cpu_to_le32(fc_dentry->fcd_ino);
834 tl.fc_tag = cpu_to_le16(fc_dentry->fcd_op);
835 tl.fc_len = cpu_to_le16(sizeof(fcd) + dlen);
836 memcpy(dst, &tl, EXT4_FC_TAG_BASE_LEN);
837 dst += EXT4_FC_TAG_BASE_LEN;
838 memcpy(dst, &fcd, sizeof(fcd));
840 memcpy(dst, fc_dentry->fcd_name.name.name, dlen);
846 * Writes inode in the fast commit space under TLV with tag @tag.
847 * Returns 0 on success, error on failure.
849 static int ext4_fc_write_inode(struct inode *inode, u32 *crc)
851 struct ext4_inode_info *ei = EXT4_I(inode);
852 int inode_len = EXT4_GOOD_OLD_INODE_SIZE;
854 struct ext4_iloc iloc;
855 struct ext4_fc_inode fc_inode;
856 struct ext4_fc_tl tl;
859 ret = ext4_get_inode_loc(inode, &iloc);
863 if (ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA))
864 inode_len = EXT4_INODE_SIZE(inode->i_sb);
865 else if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE)
866 inode_len += ei->i_extra_isize;
868 fc_inode.fc_ino = cpu_to_le32(inode->i_ino);
869 tl.fc_tag = cpu_to_le16(EXT4_FC_TAG_INODE);
870 tl.fc_len = cpu_to_le16(inode_len + sizeof(fc_inode.fc_ino));
873 dst = ext4_fc_reserve_space(inode->i_sb,
874 EXT4_FC_TAG_BASE_LEN + inode_len + sizeof(fc_inode.fc_ino), crc);
878 memcpy(dst, &tl, EXT4_FC_TAG_BASE_LEN);
879 dst += EXT4_FC_TAG_BASE_LEN;
880 memcpy(dst, &fc_inode, sizeof(fc_inode));
881 dst += sizeof(fc_inode);
882 memcpy(dst, (u8 *)ext4_raw_inode(&iloc), inode_len);
890 * Writes updated data ranges for the inode in question. Updates CRC.
891 * Returns 0 on success, error otherwise.
893 static int ext4_fc_write_inode_data(struct inode *inode, u32 *crc)
895 ext4_lblk_t old_blk_size, cur_lblk_off, new_blk_size;
896 struct ext4_inode_info *ei = EXT4_I(inode);
897 struct ext4_map_blocks map;
898 struct ext4_fc_add_range fc_ext;
899 struct ext4_fc_del_range lrange;
900 struct ext4_extent *ex;
903 spin_lock(&ei->i_fc_lock);
904 if (ei->i_fc_lblk_len == 0) {
905 spin_unlock(&ei->i_fc_lock);
908 old_blk_size = ei->i_fc_lblk_start;
909 new_blk_size = ei->i_fc_lblk_start + ei->i_fc_lblk_len - 1;
910 ei->i_fc_lblk_len = 0;
911 spin_unlock(&ei->i_fc_lock);
913 cur_lblk_off = old_blk_size;
914 ext4_debug("will try writing %d to %d for inode %ld\n",
915 cur_lblk_off, new_blk_size, inode->i_ino);
917 while (cur_lblk_off <= new_blk_size) {
918 map.m_lblk = cur_lblk_off;
919 map.m_len = new_blk_size - cur_lblk_off + 1;
920 ret = ext4_map_blocks(NULL, inode, &map,
921 EXT4_GET_BLOCKS_IO_SUBMIT |
926 if (map.m_len == 0) {
932 lrange.fc_ino = cpu_to_le32(inode->i_ino);
933 lrange.fc_lblk = cpu_to_le32(map.m_lblk);
934 lrange.fc_len = cpu_to_le32(map.m_len);
935 if (!ext4_fc_add_tlv(inode->i_sb, EXT4_FC_TAG_DEL_RANGE,
936 sizeof(lrange), (u8 *)&lrange, crc))
939 unsigned int max = (map.m_flags & EXT4_MAP_UNWRITTEN) ?
940 EXT_UNWRITTEN_MAX_LEN : EXT_INIT_MAX_LEN;
942 /* Limit the number of blocks in one extent */
943 map.m_len = min(max, map.m_len);
945 fc_ext.fc_ino = cpu_to_le32(inode->i_ino);
946 ex = (struct ext4_extent *)&fc_ext.fc_ex;
947 ex->ee_block = cpu_to_le32(map.m_lblk);
948 ex->ee_len = cpu_to_le16(map.m_len);
949 ext4_ext_store_pblock(ex, map.m_pblk);
950 if (map.m_flags & EXT4_MAP_UNWRITTEN)
951 ext4_ext_mark_unwritten(ex);
953 ext4_ext_mark_initialized(ex);
954 if (!ext4_fc_add_tlv(inode->i_sb, EXT4_FC_TAG_ADD_RANGE,
955 sizeof(fc_ext), (u8 *)&fc_ext, crc))
959 cur_lblk_off += map.m_len;
966 /* Flushes data of all the inodes in the commit queue. */
967 static int ext4_fc_flush_data(journal_t *journal)
969 struct super_block *sb = journal->j_private;
970 struct ext4_sb_info *sbi = EXT4_SB(sb);
971 struct ext4_inode_info *ei;
974 list_for_each_entry(ei, &sbi->s_fc_q[FC_Q_MAIN], i_fc_list) {
975 ret = jbd2_submit_inode_data(journal, ei->jinode);
980 list_for_each_entry(ei, &sbi->s_fc_q[FC_Q_MAIN], i_fc_list) {
981 ret = jbd2_wait_inode_data(journal, ei->jinode);
989 /* Commit all the directory entry updates */
990 static int ext4_fc_commit_dentry_updates(journal_t *journal, u32 *crc)
992 struct super_block *sb = journal->j_private;
993 struct ext4_sb_info *sbi = EXT4_SB(sb);
994 struct ext4_fc_dentry_update *fc_dentry, *fc_dentry_n;
996 struct ext4_inode_info *ei;
999 if (list_empty(&sbi->s_fc_dentry_q[FC_Q_MAIN]))
1001 list_for_each_entry_safe(fc_dentry, fc_dentry_n,
1002 &sbi->s_fc_dentry_q[FC_Q_MAIN], fcd_list) {
1003 if (fc_dentry->fcd_op != EXT4_FC_TAG_CREAT) {
1004 if (!ext4_fc_add_dentry_tlv(sb, crc, fc_dentry))
1009 * With fcd_dilist we need not loop in sbi->s_fc_q to get the
1010 * corresponding inode. Also, the corresponding inode could have been
1011 * deleted, in which case, we don't need to do anything.
1013 if (list_empty(&fc_dentry->fcd_dilist))
1015 ei = list_first_entry(&fc_dentry->fcd_dilist,
1016 struct ext4_inode_info, i_fc_dilist);
1017 inode = &ei->vfs_inode;
1018 WARN_ON(inode->i_ino != fc_dentry->fcd_ino);
1021 * We first write the inode and then the create dirent. This
1022 * allows the recovery code to create an unnamed inode first
1023 * and then link it to a directory entry. This allows us
1024 * to use namei.c routines almost as is and simplifies
1025 * the recovery code.
1027 ret = ext4_fc_write_inode(inode, crc);
1030 ret = ext4_fc_write_inode_data(inode, crc);
1033 if (!ext4_fc_add_dentry_tlv(sb, crc, fc_dentry))
1039 static int ext4_fc_perform_commit(journal_t *journal)
1041 struct super_block *sb = journal->j_private;
1042 struct ext4_sb_info *sbi = EXT4_SB(sb);
1043 struct ext4_inode_info *iter;
1044 struct ext4_fc_head head;
1045 struct inode *inode;
1046 struct blk_plug plug;
1051 * Step 1: Mark all inodes on s_fc_q[MAIN] with
1052 * EXT4_STATE_FC_FLUSHING_DATA. This prevents these inodes from being
1053 * freed until the data flush is over.
1055 mutex_lock(&sbi->s_fc_lock);
1056 list_for_each_entry(iter, &sbi->s_fc_q[FC_Q_MAIN], i_fc_list) {
1057 ext4_set_inode_state(&iter->vfs_inode,
1058 EXT4_STATE_FC_FLUSHING_DATA);
1060 mutex_unlock(&sbi->s_fc_lock);
1062 /* Step 2: Flush data for all the eligible inodes. */
1063 ret = ext4_fc_flush_data(journal);
1066 * Step 3: Clear EXT4_STATE_FC_FLUSHING_DATA flag, before returning
1067 * any error from step 2. This ensures that waiters waiting on
1068 * EXT4_STATE_FC_FLUSHING_DATA can resume.
1070 mutex_lock(&sbi->s_fc_lock);
1071 list_for_each_entry(iter, &sbi->s_fc_q[FC_Q_MAIN], i_fc_list) {
1072 ext4_clear_inode_state(&iter->vfs_inode,
1073 EXT4_STATE_FC_FLUSHING_DATA);
1074 #if (BITS_PER_LONG < 64)
1075 wake_up_bit(&iter->i_state_flags, EXT4_STATE_FC_FLUSHING_DATA);
1077 wake_up_bit(&iter->i_flags, EXT4_STATE_FC_FLUSHING_DATA);
1082 * Make sure clearing of EXT4_STATE_FC_FLUSHING_DATA is visible before
1083 * the waiter checks the bit. Pairs with implicit barrier in
1084 * prepare_to_wait() in ext4_fc_del().
1087 mutex_unlock(&sbi->s_fc_lock);
1090 * If we encountered error in Step 2, return it now after clearing
1091 * EXT4_STATE_FC_FLUSHING_DATA bit.
1097 /* Step 4: Mark all inodes as being committed. */
1098 jbd2_journal_lock_updates(journal);
1100 * The journal is now locked. No more handles can start and all the
1101 * previous handles are now drained. We now mark the inodes on the
1102 * commit queue as being committed.
1104 mutex_lock(&sbi->s_fc_lock);
1105 list_for_each_entry(iter, &sbi->s_fc_q[FC_Q_MAIN], i_fc_list) {
1106 ext4_set_inode_state(&iter->vfs_inode,
1107 EXT4_STATE_FC_COMMITTING);
1109 mutex_unlock(&sbi->s_fc_lock);
1110 jbd2_journal_unlock_updates(journal);
1113 * Step 5: If file system device is different from journal device,
1114 * issue a cache flush before we start writing fast commit blocks.
1116 if (journal->j_fs_dev != journal->j_dev)
1117 blkdev_issue_flush(journal->j_fs_dev);
1119 blk_start_plug(&plug);
1120 /* Step 6: Write fast commit blocks to disk. */
1121 if (sbi->s_fc_bytes == 0) {
1123 * Step 6.1: Add a head tag only if this is the first fast
1124 * commit in this TID.
1126 head.fc_features = cpu_to_le32(EXT4_FC_SUPPORTED_FEATURES);
1127 head.fc_tid = cpu_to_le32(
1128 sbi->s_journal->j_running_transaction->t_tid);
1129 if (!ext4_fc_add_tlv(sb, EXT4_FC_TAG_HEAD, sizeof(head),
1130 (u8 *)&head, &crc)) {
1136 /* Step 6.2: Now write all the dentry updates. */
1137 mutex_lock(&sbi->s_fc_lock);
1138 ret = ext4_fc_commit_dentry_updates(journal, &crc);
1142 /* Step 6.3: Now write all the changed inodes to disk. */
1143 list_for_each_entry(iter, &sbi->s_fc_q[FC_Q_MAIN], i_fc_list) {
1144 inode = &iter->vfs_inode;
1145 if (!ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING))
1148 ret = ext4_fc_write_inode_data(inode, &crc);
1151 ret = ext4_fc_write_inode(inode, &crc);
1155 /* Step 6.4: Finally write tail tag to conclude this fast commit. */
1156 ret = ext4_fc_write_tail(sb, crc);
1159 mutex_unlock(&sbi->s_fc_lock);
1160 blk_finish_plug(&plug);
1164 static void ext4_fc_update_stats(struct super_block *sb, int status,
1165 u64 commit_time, int nblks, tid_t commit_tid)
1167 struct ext4_fc_stats *stats = &EXT4_SB(sb)->s_fc_stats;
1169 ext4_debug("Fast commit ended with status = %d for tid %u",
1170 status, commit_tid);
1171 if (status == EXT4_FC_STATUS_OK) {
1172 stats->fc_num_commits++;
1173 stats->fc_numblks += nblks;
1174 if (likely(stats->s_fc_avg_commit_time))
1175 stats->s_fc_avg_commit_time =
1177 stats->s_fc_avg_commit_time * 3) / 4;
1179 stats->s_fc_avg_commit_time = commit_time;
1180 } else if (status == EXT4_FC_STATUS_FAILED ||
1181 status == EXT4_FC_STATUS_INELIGIBLE) {
1182 if (status == EXT4_FC_STATUS_FAILED)
1183 stats->fc_failed_commits++;
1184 stats->fc_ineligible_commits++;
1186 stats->fc_skipped_commits++;
1188 trace_ext4_fc_commit_stop(sb, nblks, status, commit_tid);
1192 * The main commit entry point. Performs a fast commit for transaction
1193 * commit_tid if needed. If it's not possible to perform a fast commit
1194 * due to various reasons, we fall back to full commit. Returns 0
1195 * on success, error otherwise.
1197 int ext4_fc_commit(journal_t *journal, tid_t commit_tid)
1199 struct super_block *sb = journal->j_private;
1200 struct ext4_sb_info *sbi = EXT4_SB(sb);
1201 int nblks = 0, ret, bsize = journal->j_blocksize;
1202 int subtid = atomic_read(&sbi->s_fc_subtid);
1203 int status = EXT4_FC_STATUS_OK, fc_bufs_before = 0;
1204 ktime_t start_time, commit_time;
1205 int old_ioprio, journal_ioprio;
1207 if (!test_opt2(sb, JOURNAL_FAST_COMMIT))
1208 return jbd2_complete_transaction(journal, commit_tid);
1210 trace_ext4_fc_commit_start(sb, commit_tid);
1212 start_time = ktime_get();
1213 old_ioprio = get_current_ioprio();
1216 ret = jbd2_fc_begin_commit(journal, commit_tid);
1217 if (ret == -EALREADY) {
1218 /* There was an ongoing commit, check if we need to restart */
1219 if (atomic_read(&sbi->s_fc_subtid) <= subtid &&
1220 tid_gt(commit_tid, journal->j_commit_sequence))
1222 ext4_fc_update_stats(sb, EXT4_FC_STATUS_SKIPPED, 0, 0,
1227 * Commit couldn't start. Just update stats and perform a
1230 ext4_fc_update_stats(sb, EXT4_FC_STATUS_FAILED, 0, 0,
1232 return jbd2_complete_transaction(journal, commit_tid);
1236 * After establishing journal barrier via jbd2_fc_begin_commit(), check
1237 * if we are fast commit ineligible.
1239 if (ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE)) {
1240 status = EXT4_FC_STATUS_INELIGIBLE;
1245 * Now that we know that this thread is going to do a fast commit,
1246 * elevate the priority to match that of the journal thread.
1248 if (journal->j_task->io_context)
1249 journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
1251 journal_ioprio = EXT4_DEF_JOURNAL_IOPRIO;
1252 set_task_ioprio(current, journal_ioprio);
1253 fc_bufs_before = (sbi->s_fc_bytes + bsize - 1) / bsize;
1254 ret = ext4_fc_perform_commit(journal);
1256 status = EXT4_FC_STATUS_FAILED;
1259 nblks = (sbi->s_fc_bytes + bsize - 1) / bsize - fc_bufs_before;
1260 ret = jbd2_fc_wait_bufs(journal, nblks);
1262 status = EXT4_FC_STATUS_FAILED;
1265 atomic_inc(&sbi->s_fc_subtid);
1266 ret = jbd2_fc_end_commit(journal);
1267 set_task_ioprio(current, old_ioprio);
1269 * weight the commit time higher than the average time so we
1270 * don't react too strongly to vast changes in the commit time
1272 commit_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1273 ext4_fc_update_stats(sb, status, commit_time, nblks, commit_tid);
1277 set_task_ioprio(current, old_ioprio);
1278 ret = jbd2_fc_end_commit_fallback(journal);
1279 ext4_fc_update_stats(sb, status, 0, 0, commit_tid);
1284 * Fast commit cleanup routine. This is called after every fast commit and
1285 * full commit. full is true if we are called after a full commit.
1287 static void ext4_fc_cleanup(journal_t *journal, int full, tid_t tid)
1289 struct super_block *sb = journal->j_private;
1290 struct ext4_sb_info *sbi = EXT4_SB(sb);
1291 struct ext4_inode_info *ei;
1292 struct ext4_fc_dentry_update *fc_dentry;
1294 if (full && sbi->s_fc_bh)
1295 sbi->s_fc_bh = NULL;
1297 trace_ext4_fc_cleanup(journal, full, tid);
1298 jbd2_fc_release_bufs(journal);
1300 mutex_lock(&sbi->s_fc_lock);
1301 while (!list_empty(&sbi->s_fc_q[FC_Q_MAIN])) {
1302 ei = list_first_entry(&sbi->s_fc_q[FC_Q_MAIN],
1303 struct ext4_inode_info,
1305 list_del_init(&ei->i_fc_list);
1306 ext4_clear_inode_state(&ei->vfs_inode,
1307 EXT4_STATE_FC_COMMITTING);
1308 if (tid_geq(tid, ei->i_sync_tid)) {
1309 ext4_fc_reset_inode(&ei->vfs_inode);
1312 * We are called after a full commit, inode has been
1313 * modified while the commit was running. Re-enqueue
1314 * the inode into STAGING, which will then be splice
1315 * back into MAIN. This cannot happen during
1316 * fastcommit because the journal is locked all the
1317 * time in that case (and tid doesn't increase so
1318 * tid check above isn't reliable).
1320 list_add_tail(&ei->i_fc_list,
1321 &sbi->s_fc_q[FC_Q_STAGING]);
1324 * Make sure clearing of EXT4_STATE_FC_COMMITTING is
1325 * visible before we send the wakeup. Pairs with implicit
1326 * barrier in prepare_to_wait() in ext4_fc_track_inode().
1329 #if (BITS_PER_LONG < 64)
1330 wake_up_bit(&ei->i_state_flags, EXT4_STATE_FC_COMMITTING);
1332 wake_up_bit(&ei->i_flags, EXT4_STATE_FC_COMMITTING);
1336 while (!list_empty(&sbi->s_fc_dentry_q[FC_Q_MAIN])) {
1337 fc_dentry = list_first_entry(&sbi->s_fc_dentry_q[FC_Q_MAIN],
1338 struct ext4_fc_dentry_update,
1340 list_del_init(&fc_dentry->fcd_list);
1341 list_del_init(&fc_dentry->fcd_dilist);
1343 release_dentry_name_snapshot(&fc_dentry->fcd_name);
1344 kmem_cache_free(ext4_fc_dentry_cachep, fc_dentry);
1347 list_splice_init(&sbi->s_fc_dentry_q[FC_Q_STAGING],
1348 &sbi->s_fc_dentry_q[FC_Q_MAIN]);
1349 list_splice_init(&sbi->s_fc_q[FC_Q_STAGING],
1350 &sbi->s_fc_q[FC_Q_MAIN]);
1352 if (tid_geq(tid, sbi->s_fc_ineligible_tid)) {
1353 sbi->s_fc_ineligible_tid = 0;
1354 ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
1358 sbi->s_fc_bytes = 0;
1359 mutex_unlock(&sbi->s_fc_lock);
1360 trace_ext4_fc_stats(sb);
1363 /* Ext4 Replay Path Routines */
1365 /* Helper struct for dentry replay routines */
1366 struct dentry_info_args {
1367 int parent_ino, dname_len, ino, inode_len;
1371 /* Same as struct ext4_fc_tl, but uses native endianness fields */
1372 struct ext4_fc_tl_mem {
1377 static inline void tl_to_darg(struct dentry_info_args *darg,
1378 struct ext4_fc_tl_mem *tl, u8 *val)
1380 struct ext4_fc_dentry_info fcd;
1382 memcpy(&fcd, val, sizeof(fcd));
1384 darg->parent_ino = le32_to_cpu(fcd.fc_parent_ino);
1385 darg->ino = le32_to_cpu(fcd.fc_ino);
1386 darg->dname = val + offsetof(struct ext4_fc_dentry_info, fc_dname);
1387 darg->dname_len = tl->fc_len - sizeof(struct ext4_fc_dentry_info);
1390 static inline void ext4_fc_get_tl(struct ext4_fc_tl_mem *tl, u8 *val)
1392 struct ext4_fc_tl tl_disk;
1394 memcpy(&tl_disk, val, EXT4_FC_TAG_BASE_LEN);
1395 tl->fc_len = le16_to_cpu(tl_disk.fc_len);
1396 tl->fc_tag = le16_to_cpu(tl_disk.fc_tag);
1399 /* Unlink replay function */
1400 static int ext4_fc_replay_unlink(struct super_block *sb,
1401 struct ext4_fc_tl_mem *tl, u8 *val)
1403 struct inode *inode, *old_parent;
1405 struct dentry_info_args darg;
1408 tl_to_darg(&darg, tl, val);
1410 trace_ext4_fc_replay(sb, EXT4_FC_TAG_UNLINK, darg.ino,
1411 darg.parent_ino, darg.dname_len);
1413 entry.name = darg.dname;
1414 entry.len = darg.dname_len;
1415 inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
1417 if (IS_ERR(inode)) {
1418 ext4_debug("Inode %d not found", darg.ino);
1422 old_parent = ext4_iget(sb, darg.parent_ino,
1424 if (IS_ERR(old_parent)) {
1425 ext4_debug("Dir with inode %d not found", darg.parent_ino);
1430 ret = __ext4_unlink(old_parent, &entry, inode, NULL);
1431 /* -ENOENT ok coz it might not exist anymore. */
1439 static int ext4_fc_replay_link_internal(struct super_block *sb,
1440 struct dentry_info_args *darg,
1441 struct inode *inode)
1443 struct inode *dir = NULL;
1444 struct dentry *dentry_dir = NULL, *dentry_inode = NULL;
1445 struct qstr qstr_dname = QSTR_INIT(darg->dname, darg->dname_len);
1448 dir = ext4_iget(sb, darg->parent_ino, EXT4_IGET_NORMAL);
1450 ext4_debug("Dir with inode %d not found.", darg->parent_ino);
1455 dentry_dir = d_obtain_alias(dir);
1456 if (IS_ERR(dentry_dir)) {
1457 ext4_debug("Failed to obtain dentry");
1462 dentry_inode = d_alloc(dentry_dir, &qstr_dname);
1463 if (!dentry_inode) {
1464 ext4_debug("Inode dentry not created.");
1469 ret = __ext4_link(dir, inode, dentry_inode);
1471 * It's possible that link already existed since data blocks
1472 * for the dir in question got persisted before we crashed OR
1473 * we replayed this tag and crashed before the entire replay
1476 if (ret && ret != -EEXIST) {
1477 ext4_debug("Failed to link\n");
1490 d_drop(dentry_inode);
1497 /* Link replay function */
1498 static int ext4_fc_replay_link(struct super_block *sb,
1499 struct ext4_fc_tl_mem *tl, u8 *val)
1501 struct inode *inode;
1502 struct dentry_info_args darg;
1505 tl_to_darg(&darg, tl, val);
1506 trace_ext4_fc_replay(sb, EXT4_FC_TAG_LINK, darg.ino,
1507 darg.parent_ino, darg.dname_len);
1509 inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
1510 if (IS_ERR(inode)) {
1511 ext4_debug("Inode not found.");
1515 ret = ext4_fc_replay_link_internal(sb, &darg, inode);
1521 * Record all the modified inodes during replay. We use this later to setup
1522 * block bitmaps correctly.
1524 static int ext4_fc_record_modified_inode(struct super_block *sb, int ino)
1526 struct ext4_fc_replay_state *state;
1529 state = &EXT4_SB(sb)->s_fc_replay_state;
1530 for (i = 0; i < state->fc_modified_inodes_used; i++)
1531 if (state->fc_modified_inodes[i] == ino)
1533 if (state->fc_modified_inodes_used == state->fc_modified_inodes_size) {
1534 int *fc_modified_inodes;
1536 fc_modified_inodes = krealloc(state->fc_modified_inodes,
1537 sizeof(int) * (state->fc_modified_inodes_size +
1538 EXT4_FC_REPLAY_REALLOC_INCREMENT),
1540 if (!fc_modified_inodes)
1542 state->fc_modified_inodes = fc_modified_inodes;
1543 state->fc_modified_inodes_size +=
1544 EXT4_FC_REPLAY_REALLOC_INCREMENT;
1546 state->fc_modified_inodes[state->fc_modified_inodes_used++] = ino;
1551 * Inode replay function
1553 static int ext4_fc_replay_inode(struct super_block *sb,
1554 struct ext4_fc_tl_mem *tl, u8 *val)
1556 struct ext4_fc_inode fc_inode;
1557 struct ext4_inode *raw_inode;
1558 struct ext4_inode *raw_fc_inode;
1559 struct inode *inode = NULL;
1560 struct ext4_iloc iloc;
1561 int inode_len, ino, ret, tag = tl->fc_tag;
1562 struct ext4_extent_header *eh;
1563 size_t off_gen = offsetof(struct ext4_inode, i_generation);
1565 memcpy(&fc_inode, val, sizeof(fc_inode));
1567 ino = le32_to_cpu(fc_inode.fc_ino);
1568 trace_ext4_fc_replay(sb, tag, ino, 0, 0);
1570 inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
1571 if (!IS_ERR(inode)) {
1572 ext4_ext_clear_bb(inode);
1577 ret = ext4_fc_record_modified_inode(sb, ino);
1581 raw_fc_inode = (struct ext4_inode *)
1582 (val + offsetof(struct ext4_fc_inode, fc_raw_inode));
1583 ret = ext4_get_fc_inode_loc(sb, ino, &iloc);
1587 inode_len = tl->fc_len - sizeof(struct ext4_fc_inode);
1588 raw_inode = ext4_raw_inode(&iloc);
1590 memcpy(raw_inode, raw_fc_inode, offsetof(struct ext4_inode, i_block));
1591 memcpy((u8 *)raw_inode + off_gen, (u8 *)raw_fc_inode + off_gen,
1592 inode_len - off_gen);
1593 if (le32_to_cpu(raw_inode->i_flags) & EXT4_EXTENTS_FL) {
1594 eh = (struct ext4_extent_header *)(&raw_inode->i_block[0]);
1595 if (eh->eh_magic != EXT4_EXT_MAGIC) {
1596 memset(eh, 0, sizeof(*eh));
1597 eh->eh_magic = EXT4_EXT_MAGIC;
1598 eh->eh_max = cpu_to_le16(
1599 (sizeof(raw_inode->i_block) -
1600 sizeof(struct ext4_extent_header))
1601 / sizeof(struct ext4_extent));
1603 } else if (le32_to_cpu(raw_inode->i_flags) & EXT4_INLINE_DATA_FL) {
1604 memcpy(raw_inode->i_block, raw_fc_inode->i_block,
1605 sizeof(raw_inode->i_block));
1608 /* Immediately update the inode on disk. */
1609 ret = ext4_handle_dirty_metadata(NULL, NULL, iloc.bh);
1612 ret = sync_dirty_buffer(iloc.bh);
1615 ret = ext4_mark_inode_used(sb, ino);
1619 /* Given that we just wrote the inode on disk, this SHOULD succeed. */
1620 inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
1621 if (IS_ERR(inode)) {
1622 ext4_debug("Inode not found.");
1623 return -EFSCORRUPTED;
1627 * Our allocator could have made different decisions than before
1628 * crashing. This should be fixed but until then, we calculate
1629 * the number of blocks the inode.
1631 if (!ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA))
1632 ext4_ext_replay_set_iblocks(inode);
1634 inode->i_generation = le32_to_cpu(ext4_raw_inode(&iloc)->i_generation);
1635 ext4_reset_inode_seed(inode);
1637 ext4_inode_csum_set(inode, ext4_raw_inode(&iloc), EXT4_I(inode));
1638 ret = ext4_handle_dirty_metadata(NULL, NULL, iloc.bh);
1639 sync_dirty_buffer(iloc.bh);
1644 blkdev_issue_flush(sb->s_bdev);
1650 * Dentry create replay function.
1652 * EXT4_FC_TAG_CREAT is preceded by EXT4_FC_TAG_INODE_FULL. Which means, the
1653 * inode for which we are trying to create a dentry here, should already have
1654 * been replayed before we start here.
1656 static int ext4_fc_replay_create(struct super_block *sb,
1657 struct ext4_fc_tl_mem *tl, u8 *val)
1660 struct inode *inode = NULL;
1661 struct inode *dir = NULL;
1662 struct dentry_info_args darg;
1664 tl_to_darg(&darg, tl, val);
1666 trace_ext4_fc_replay(sb, EXT4_FC_TAG_CREAT, darg.ino,
1667 darg.parent_ino, darg.dname_len);
1669 /* This takes care of update group descriptor and other metadata */
1670 ret = ext4_mark_inode_used(sb, darg.ino);
1674 inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
1675 if (IS_ERR(inode)) {
1676 ext4_debug("inode %d not found.", darg.ino);
1682 if (S_ISDIR(inode->i_mode)) {
1684 * If we are creating a directory, we need to make sure that the
1685 * dot and dot dot dirents are setup properly.
1687 dir = ext4_iget(sb, darg.parent_ino, EXT4_IGET_NORMAL);
1689 ext4_debug("Dir %d not found.", darg.ino);
1692 ret = ext4_init_new_dir(NULL, dir, inode);
1699 ret = ext4_fc_replay_link_internal(sb, &darg, inode);
1702 set_nlink(inode, 1);
1703 ext4_mark_inode_dirty(NULL, inode);
1710 * Record physical disk regions which are in use as per fast commit area,
1711 * and used by inodes during replay phase. Our simple replay phase
1712 * allocator excludes these regions from allocation.
1714 int ext4_fc_record_regions(struct super_block *sb, int ino,
1715 ext4_lblk_t lblk, ext4_fsblk_t pblk, int len, int replay)
1717 struct ext4_fc_replay_state *state;
1718 struct ext4_fc_alloc_region *region;
1720 state = &EXT4_SB(sb)->s_fc_replay_state;
1722 * during replay phase, the fc_regions_valid may not same as
1723 * fc_regions_used, update it when do new additions.
1725 if (replay && state->fc_regions_used != state->fc_regions_valid)
1726 state->fc_regions_used = state->fc_regions_valid;
1727 if (state->fc_regions_used == state->fc_regions_size) {
1728 struct ext4_fc_alloc_region *fc_regions;
1730 fc_regions = krealloc(state->fc_regions,
1731 sizeof(struct ext4_fc_alloc_region) *
1732 (state->fc_regions_size +
1733 EXT4_FC_REPLAY_REALLOC_INCREMENT),
1737 state->fc_regions_size +=
1738 EXT4_FC_REPLAY_REALLOC_INCREMENT;
1739 state->fc_regions = fc_regions;
1741 region = &state->fc_regions[state->fc_regions_used++];
1743 region->lblk = lblk;
1744 region->pblk = pblk;
1748 state->fc_regions_valid++;
1753 /* Replay add range tag */
1754 static int ext4_fc_replay_add_range(struct super_block *sb,
1755 struct ext4_fc_tl_mem *tl, u8 *val)
1757 struct ext4_fc_add_range fc_add_ex;
1758 struct ext4_extent newex, *ex;
1759 struct inode *inode;
1760 ext4_lblk_t start, cur;
1762 ext4_fsblk_t start_pblk;
1763 struct ext4_map_blocks map;
1764 struct ext4_ext_path *path = NULL;
1767 memcpy(&fc_add_ex, val, sizeof(fc_add_ex));
1768 ex = (struct ext4_extent *)&fc_add_ex.fc_ex;
1770 trace_ext4_fc_replay(sb, EXT4_FC_TAG_ADD_RANGE,
1771 le32_to_cpu(fc_add_ex.fc_ino), le32_to_cpu(ex->ee_block),
1772 ext4_ext_get_actual_len(ex));
1774 inode = ext4_iget(sb, le32_to_cpu(fc_add_ex.fc_ino), EXT4_IGET_NORMAL);
1775 if (IS_ERR(inode)) {
1776 ext4_debug("Inode not found.");
1780 ret = ext4_fc_record_modified_inode(sb, inode->i_ino);
1784 start = le32_to_cpu(ex->ee_block);
1785 start_pblk = ext4_ext_pblock(ex);
1786 len = ext4_ext_get_actual_len(ex);
1790 ext4_debug("ADD_RANGE, lblk %d, pblk %lld, len %d, unwritten %d, inode %ld\n",
1791 start, start_pblk, len, ext4_ext_is_unwritten(ex),
1794 while (remaining > 0) {
1796 map.m_len = remaining;
1798 ret = ext4_map_blocks(NULL, inode, &map, 0);
1804 /* Range is not mapped */
1805 path = ext4_find_extent(inode, cur, path, 0);
1808 memset(&newex, 0, sizeof(newex));
1809 newex.ee_block = cpu_to_le32(cur);
1810 ext4_ext_store_pblock(
1811 &newex, start_pblk + cur - start);
1812 newex.ee_len = cpu_to_le16(map.m_len);
1813 if (ext4_ext_is_unwritten(ex))
1814 ext4_ext_mark_unwritten(&newex);
1815 down_write(&EXT4_I(inode)->i_data_sem);
1816 path = ext4_ext_insert_extent(NULL, inode,
1818 up_write((&EXT4_I(inode)->i_data_sem));
1824 if (start_pblk + cur - start != map.m_pblk) {
1826 * Logical to physical mapping changed. This can happen
1827 * if this range was removed and then reallocated to
1828 * map to new physical blocks during a fast commit.
1830 ret = ext4_ext_replay_update_ex(inode, cur, map.m_len,
1831 ext4_ext_is_unwritten(ex),
1832 start_pblk + cur - start);
1836 * Mark the old blocks as free since they aren't used
1837 * anymore. We maintain an array of all the modified
1838 * inodes. In case these blocks are still used at either
1839 * a different logical range in the same inode or in
1840 * some different inode, we will mark them as allocated
1841 * at the end of the FC replay using our array of
1844 ext4_mb_mark_bb(inode->i_sb, map.m_pblk, map.m_len, false);
1848 /* Range is mapped and needs a state change */
1849 ext4_debug("Converting from %ld to %d %lld",
1850 map.m_flags & EXT4_MAP_UNWRITTEN,
1851 ext4_ext_is_unwritten(ex), map.m_pblk);
1852 ret = ext4_ext_replay_update_ex(inode, cur, map.m_len,
1853 ext4_ext_is_unwritten(ex), map.m_pblk);
1857 * We may have split the extent tree while toggling the state.
1858 * Try to shrink the extent tree now.
1860 ext4_ext_replay_shrink_inode(inode, start + len);
1863 remaining -= map.m_len;
1865 ext4_ext_replay_shrink_inode(inode, i_size_read(inode) >>
1866 sb->s_blocksize_bits);
1868 ext4_free_ext_path(path);
1873 /* Replay DEL_RANGE tag */
1875 ext4_fc_replay_del_range(struct super_block *sb,
1876 struct ext4_fc_tl_mem *tl, u8 *val)
1878 struct inode *inode;
1879 struct ext4_fc_del_range lrange;
1880 struct ext4_map_blocks map;
1881 ext4_lblk_t cur, remaining;
1884 memcpy(&lrange, val, sizeof(lrange));
1885 cur = le32_to_cpu(lrange.fc_lblk);
1886 remaining = le32_to_cpu(lrange.fc_len);
1888 trace_ext4_fc_replay(sb, EXT4_FC_TAG_DEL_RANGE,
1889 le32_to_cpu(lrange.fc_ino), cur, remaining);
1891 inode = ext4_iget(sb, le32_to_cpu(lrange.fc_ino), EXT4_IGET_NORMAL);
1892 if (IS_ERR(inode)) {
1893 ext4_debug("Inode %d not found", le32_to_cpu(lrange.fc_ino));
1897 ret = ext4_fc_record_modified_inode(sb, inode->i_ino);
1901 ext4_debug("DEL_RANGE, inode %ld, lblk %d, len %d\n",
1902 inode->i_ino, le32_to_cpu(lrange.fc_lblk),
1903 le32_to_cpu(lrange.fc_len));
1904 while (remaining > 0) {
1906 map.m_len = remaining;
1908 ret = ext4_map_blocks(NULL, inode, &map, 0);
1914 ext4_mb_mark_bb(inode->i_sb, map.m_pblk, map.m_len, false);
1916 remaining -= map.m_len;
1921 down_write(&EXT4_I(inode)->i_data_sem);
1922 ret = ext4_ext_remove_space(inode, le32_to_cpu(lrange.fc_lblk),
1923 le32_to_cpu(lrange.fc_lblk) +
1924 le32_to_cpu(lrange.fc_len) - 1);
1925 up_write(&EXT4_I(inode)->i_data_sem);
1928 ext4_ext_replay_shrink_inode(inode,
1929 i_size_read(inode) >> sb->s_blocksize_bits);
1930 ext4_mark_inode_dirty(NULL, inode);
1936 static void ext4_fc_set_bitmaps_and_counters(struct super_block *sb)
1938 struct ext4_fc_replay_state *state;
1939 struct inode *inode;
1940 struct ext4_ext_path *path = NULL;
1941 struct ext4_map_blocks map;
1943 ext4_lblk_t cur, end;
1945 state = &EXT4_SB(sb)->s_fc_replay_state;
1946 for (i = 0; i < state->fc_modified_inodes_used; i++) {
1947 inode = ext4_iget(sb, state->fc_modified_inodes[i],
1949 if (IS_ERR(inode)) {
1950 ext4_debug("Inode %d not found.",
1951 state->fc_modified_inodes[i]);
1955 end = EXT_MAX_BLOCKS;
1956 if (ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA)) {
1962 map.m_len = end - cur;
1964 ret = ext4_map_blocks(NULL, inode, &map, 0);
1969 path = ext4_find_extent(inode, map.m_lblk, path, 0);
1970 if (!IS_ERR(path)) {
1971 for (j = 0; j < path->p_depth; j++)
1972 ext4_mb_mark_bb(inode->i_sb,
1973 path[j].p_block, 1, true);
1978 ext4_mb_mark_bb(inode->i_sb, map.m_pblk,
1981 cur = cur + (map.m_len ? map.m_len : 1);
1987 ext4_free_ext_path(path);
1991 * Check if block is in excluded regions for block allocation. The simple
1992 * allocator that runs during replay phase is calls this function to see
1993 * if it is okay to use a block.
1995 bool ext4_fc_replay_check_excluded(struct super_block *sb, ext4_fsblk_t blk)
1998 struct ext4_fc_replay_state *state;
2000 state = &EXT4_SB(sb)->s_fc_replay_state;
2001 for (i = 0; i < state->fc_regions_valid; i++) {
2002 if (state->fc_regions[i].ino == 0 ||
2003 state->fc_regions[i].len == 0)
2005 if (in_range(blk, state->fc_regions[i].pblk,
2006 state->fc_regions[i].len))
2012 /* Cleanup function called after replay */
2013 void ext4_fc_replay_cleanup(struct super_block *sb)
2015 struct ext4_sb_info *sbi = EXT4_SB(sb);
2017 sbi->s_mount_state &= ~EXT4_FC_REPLAY;
2018 kfree(sbi->s_fc_replay_state.fc_regions);
2019 kfree(sbi->s_fc_replay_state.fc_modified_inodes);
2022 static bool ext4_fc_value_len_isvalid(struct ext4_sb_info *sbi,
2026 case EXT4_FC_TAG_ADD_RANGE:
2027 return len == sizeof(struct ext4_fc_add_range);
2028 case EXT4_FC_TAG_DEL_RANGE:
2029 return len == sizeof(struct ext4_fc_del_range);
2030 case EXT4_FC_TAG_CREAT:
2031 case EXT4_FC_TAG_LINK:
2032 case EXT4_FC_TAG_UNLINK:
2033 len -= sizeof(struct ext4_fc_dentry_info);
2034 return len >= 1 && len <= EXT4_NAME_LEN;
2035 case EXT4_FC_TAG_INODE:
2036 len -= sizeof(struct ext4_fc_inode);
2037 return len >= EXT4_GOOD_OLD_INODE_SIZE &&
2038 len <= sbi->s_inode_size;
2039 case EXT4_FC_TAG_PAD:
2040 return true; /* padding can have any length */
2041 case EXT4_FC_TAG_TAIL:
2042 return len >= sizeof(struct ext4_fc_tail);
2043 case EXT4_FC_TAG_HEAD:
2044 return len == sizeof(struct ext4_fc_head);
2050 * Recovery Scan phase handler
2052 * This function is called during the scan phase and is responsible
2053 * for doing following things:
2054 * - Make sure the fast commit area has valid tags for replay
2055 * - Count number of tags that need to be replayed by the replay handler
2057 * - Create a list of excluded blocks for allocation during replay phase
2059 * This function returns JBD2_FC_REPLAY_CONTINUE to indicate that SCAN is
2060 * incomplete and JBD2 should send more blocks. It returns JBD2_FC_REPLAY_STOP
2061 * to indicate that scan has finished and JBD2 can now start replay phase.
2062 * It returns a negative error to indicate that there was an error. At the end
2063 * of a successful scan phase, sbi->s_fc_replay_state.fc_replay_num_tags is set
2064 * to indicate the number of tags that need to replayed during the replay phase.
2066 static int ext4_fc_replay_scan(journal_t *journal,
2067 struct buffer_head *bh, int off,
2070 struct super_block *sb = journal->j_private;
2071 struct ext4_sb_info *sbi = EXT4_SB(sb);
2072 struct ext4_fc_replay_state *state;
2073 int ret = JBD2_FC_REPLAY_CONTINUE;
2074 struct ext4_fc_add_range ext;
2075 struct ext4_fc_tl_mem tl;
2076 struct ext4_fc_tail tail;
2077 __u8 *start, *end, *cur, *val;
2078 struct ext4_fc_head head;
2079 struct ext4_extent *ex;
2081 state = &sbi->s_fc_replay_state;
2083 start = (u8 *)bh->b_data;
2084 end = start + journal->j_blocksize;
2086 if (state->fc_replay_expected_off == 0) {
2087 state->fc_cur_tag = 0;
2088 state->fc_replay_num_tags = 0;
2090 state->fc_regions = NULL;
2091 state->fc_regions_valid = state->fc_regions_used =
2092 state->fc_regions_size = 0;
2093 /* Check if we can stop early */
2094 if (le16_to_cpu(((struct ext4_fc_tl *)start)->fc_tag)
2095 != EXT4_FC_TAG_HEAD)
2099 if (off != state->fc_replay_expected_off) {
2100 ret = -EFSCORRUPTED;
2104 state->fc_replay_expected_off++;
2105 for (cur = start; cur <= end - EXT4_FC_TAG_BASE_LEN;
2106 cur = cur + EXT4_FC_TAG_BASE_LEN + tl.fc_len) {
2107 ext4_fc_get_tl(&tl, cur);
2108 val = cur + EXT4_FC_TAG_BASE_LEN;
2109 if (tl.fc_len > end - val ||
2110 !ext4_fc_value_len_isvalid(sbi, tl.fc_tag, tl.fc_len)) {
2111 ret = state->fc_replay_num_tags ?
2112 JBD2_FC_REPLAY_STOP : -ECANCELED;
2115 ext4_debug("Scan phase, tag:%s, blk %lld\n",
2116 tag2str(tl.fc_tag), bh->b_blocknr);
2117 switch (tl.fc_tag) {
2118 case EXT4_FC_TAG_ADD_RANGE:
2119 memcpy(&ext, val, sizeof(ext));
2120 ex = (struct ext4_extent *)&ext.fc_ex;
2121 ret = ext4_fc_record_regions(sb,
2122 le32_to_cpu(ext.fc_ino),
2123 le32_to_cpu(ex->ee_block), ext4_ext_pblock(ex),
2124 ext4_ext_get_actual_len(ex), 0);
2127 ret = JBD2_FC_REPLAY_CONTINUE;
2129 case EXT4_FC_TAG_DEL_RANGE:
2130 case EXT4_FC_TAG_LINK:
2131 case EXT4_FC_TAG_UNLINK:
2132 case EXT4_FC_TAG_CREAT:
2133 case EXT4_FC_TAG_INODE:
2134 case EXT4_FC_TAG_PAD:
2135 state->fc_cur_tag++;
2136 state->fc_crc = ext4_chksum(state->fc_crc, cur,
2137 EXT4_FC_TAG_BASE_LEN + tl.fc_len);
2139 case EXT4_FC_TAG_TAIL:
2140 state->fc_cur_tag++;
2141 memcpy(&tail, val, sizeof(tail));
2142 state->fc_crc = ext4_chksum(state->fc_crc, cur,
2143 EXT4_FC_TAG_BASE_LEN +
2144 offsetof(struct ext4_fc_tail,
2146 if (le32_to_cpu(tail.fc_tid) == expected_tid &&
2147 le32_to_cpu(tail.fc_crc) == state->fc_crc) {
2148 state->fc_replay_num_tags = state->fc_cur_tag;
2149 state->fc_regions_valid =
2150 state->fc_regions_used;
2152 ret = state->fc_replay_num_tags ?
2153 JBD2_FC_REPLAY_STOP : -EFSBADCRC;
2157 case EXT4_FC_TAG_HEAD:
2158 memcpy(&head, val, sizeof(head));
2159 if (le32_to_cpu(head.fc_features) &
2160 ~EXT4_FC_SUPPORTED_FEATURES) {
2164 if (le32_to_cpu(head.fc_tid) != expected_tid) {
2165 ret = JBD2_FC_REPLAY_STOP;
2168 state->fc_cur_tag++;
2169 state->fc_crc = ext4_chksum(state->fc_crc, cur,
2170 EXT4_FC_TAG_BASE_LEN + tl.fc_len);
2173 ret = state->fc_replay_num_tags ?
2174 JBD2_FC_REPLAY_STOP : -ECANCELED;
2176 if (ret < 0 || ret == JBD2_FC_REPLAY_STOP)
2181 trace_ext4_fc_replay_scan(sb, ret, off);
2186 * Main recovery path entry point.
2187 * The meaning of return codes is similar as above.
2189 static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh,
2190 enum passtype pass, int off, tid_t expected_tid)
2192 struct super_block *sb = journal->j_private;
2193 struct ext4_sb_info *sbi = EXT4_SB(sb);
2194 struct ext4_fc_tl_mem tl;
2195 __u8 *start, *end, *cur, *val;
2196 int ret = JBD2_FC_REPLAY_CONTINUE;
2197 struct ext4_fc_replay_state *state = &sbi->s_fc_replay_state;
2198 struct ext4_fc_tail tail;
2200 if (pass == PASS_SCAN) {
2201 state->fc_current_pass = PASS_SCAN;
2202 return ext4_fc_replay_scan(journal, bh, off, expected_tid);
2205 if (state->fc_current_pass != pass) {
2206 state->fc_current_pass = pass;
2207 sbi->s_mount_state |= EXT4_FC_REPLAY;
2209 if (!sbi->s_fc_replay_state.fc_replay_num_tags) {
2210 ext4_debug("Replay stops\n");
2211 ext4_fc_set_bitmaps_and_counters(sb);
2215 #ifdef CONFIG_EXT4_DEBUG
2216 if (sbi->s_fc_debug_max_replay && off >= sbi->s_fc_debug_max_replay) {
2217 pr_warn("Dropping fc block %d because max_replay set\n", off);
2218 return JBD2_FC_REPLAY_STOP;
2222 start = (u8 *)bh->b_data;
2223 end = start + journal->j_blocksize;
2225 for (cur = start; cur <= end - EXT4_FC_TAG_BASE_LEN;
2226 cur = cur + EXT4_FC_TAG_BASE_LEN + tl.fc_len) {
2227 ext4_fc_get_tl(&tl, cur);
2228 val = cur + EXT4_FC_TAG_BASE_LEN;
2230 if (state->fc_replay_num_tags == 0) {
2231 ret = JBD2_FC_REPLAY_STOP;
2232 ext4_fc_set_bitmaps_and_counters(sb);
2236 ext4_debug("Replay phase, tag:%s\n", tag2str(tl.fc_tag));
2237 state->fc_replay_num_tags--;
2238 switch (tl.fc_tag) {
2239 case EXT4_FC_TAG_LINK:
2240 ret = ext4_fc_replay_link(sb, &tl, val);
2242 case EXT4_FC_TAG_UNLINK:
2243 ret = ext4_fc_replay_unlink(sb, &tl, val);
2245 case EXT4_FC_TAG_ADD_RANGE:
2246 ret = ext4_fc_replay_add_range(sb, &tl, val);
2248 case EXT4_FC_TAG_CREAT:
2249 ret = ext4_fc_replay_create(sb, &tl, val);
2251 case EXT4_FC_TAG_DEL_RANGE:
2252 ret = ext4_fc_replay_del_range(sb, &tl, val);
2254 case EXT4_FC_TAG_INODE:
2255 ret = ext4_fc_replay_inode(sb, &tl, val);
2257 case EXT4_FC_TAG_PAD:
2258 trace_ext4_fc_replay(sb, EXT4_FC_TAG_PAD, 0,
2261 case EXT4_FC_TAG_TAIL:
2262 trace_ext4_fc_replay(sb, EXT4_FC_TAG_TAIL,
2264 memcpy(&tail, val, sizeof(tail));
2265 WARN_ON(le32_to_cpu(tail.fc_tid) != expected_tid);
2267 case EXT4_FC_TAG_HEAD:
2270 trace_ext4_fc_replay(sb, tl.fc_tag, 0, tl.fc_len, 0);
2276 ret = JBD2_FC_REPLAY_CONTINUE;
2281 void ext4_fc_init(struct super_block *sb, journal_t *journal)
2284 * We set replay callback even if fast commit disabled because we may
2285 * could still have fast commit blocks that need to be replayed even if
2286 * fast commit has now been turned off.
2288 journal->j_fc_replay_callback = ext4_fc_replay;
2289 if (!test_opt2(sb, JOURNAL_FAST_COMMIT))
2291 journal->j_fc_cleanup_callback = ext4_fc_cleanup;
2294 static const char * const fc_ineligible_reasons[] = {
2295 [EXT4_FC_REASON_XATTR] = "Extended attributes changed",
2296 [EXT4_FC_REASON_CROSS_RENAME] = "Cross rename",
2297 [EXT4_FC_REASON_JOURNAL_FLAG_CHANGE] = "Journal flag changed",
2298 [EXT4_FC_REASON_NOMEM] = "Insufficient memory",
2299 [EXT4_FC_REASON_SWAP_BOOT] = "Swap boot",
2300 [EXT4_FC_REASON_RESIZE] = "Resize",
2301 [EXT4_FC_REASON_RENAME_DIR] = "Dir renamed",
2302 [EXT4_FC_REASON_FALLOC_RANGE] = "Falloc range op",
2303 [EXT4_FC_REASON_INODE_JOURNAL_DATA] = "Data journalling",
2304 [EXT4_FC_REASON_ENCRYPTED_FILENAME] = "Encrypted filename",
2307 int ext4_fc_info_show(struct seq_file *seq, void *v)
2309 struct ext4_sb_info *sbi = EXT4_SB((struct super_block *)seq->private);
2310 struct ext4_fc_stats *stats = &sbi->s_fc_stats;
2313 if (v != SEQ_START_TOKEN)
2317 "fc stats:\n%ld commits\n%ld ineligible\n%ld numblks\n%lluus avg_commit_time\n",
2318 stats->fc_num_commits, stats->fc_ineligible_commits,
2320 div_u64(stats->s_fc_avg_commit_time, 1000));
2321 seq_puts(seq, "Ineligible reasons:\n");
2322 for (i = 0; i < EXT4_FC_REASON_MAX; i++)
2323 seq_printf(seq, "\"%s\":\t%d\n", fc_ineligible_reasons[i],
2324 stats->fc_ineligible_reason_count[i]);
2329 int __init ext4_fc_init_dentry_cache(void)
2331 ext4_fc_dentry_cachep = KMEM_CACHE(ext4_fc_dentry_update,
2332 SLAB_RECLAIM_ACCOUNT);
2334 if (ext4_fc_dentry_cachep == NULL)
2340 void ext4_fc_destroy_dentry_cache(void)
2342 kmem_cache_destroy(ext4_fc_dentry_cachep);