xref: /openbmc/linux/fs/ext4/fast_commit.c (revision 0bce577bf9cae13ae32d391432d0030e3f67fc1d)
16866d7b3SHarshad Shirwadkar // SPDX-License-Identifier: GPL-2.0
26866d7b3SHarshad Shirwadkar 
36866d7b3SHarshad Shirwadkar /*
46866d7b3SHarshad Shirwadkar  * fs/ext4/fast_commit.c
56866d7b3SHarshad Shirwadkar  *
66866d7b3SHarshad Shirwadkar  * Written by Harshad Shirwadkar <harshadshirwadkar@gmail.com>
76866d7b3SHarshad Shirwadkar  *
86866d7b3SHarshad Shirwadkar  * Ext4 fast commits routines.
96866d7b3SHarshad Shirwadkar  */
10aa75f4d3SHarshad Shirwadkar #include "ext4.h"
116866d7b3SHarshad Shirwadkar #include "ext4_jbd2.h"
12aa75f4d3SHarshad Shirwadkar #include "ext4_extents.h"
13aa75f4d3SHarshad Shirwadkar #include "mballoc.h"
14aa75f4d3SHarshad Shirwadkar 
15aa75f4d3SHarshad Shirwadkar /*
16aa75f4d3SHarshad Shirwadkar  * Ext4 Fast Commits
17aa75f4d3SHarshad Shirwadkar  * -----------------
18aa75f4d3SHarshad Shirwadkar  *
19aa75f4d3SHarshad Shirwadkar  * Ext4 fast commits implement fine grained journalling for Ext4.
20aa75f4d3SHarshad Shirwadkar  *
21aa75f4d3SHarshad Shirwadkar  * Fast commits are organized as a log of tag-length-value (TLV) structs. (See
22aa75f4d3SHarshad Shirwadkar  * struct ext4_fc_tl). Each TLV contains some delta that is replayed TLV by
23aa75f4d3SHarshad Shirwadkar  * TLV during the recovery phase. For the scenarios for which we currently
24aa75f4d3SHarshad Shirwadkar  * don't have replay code, fast commit falls back to full commits.
25aa75f4d3SHarshad Shirwadkar  * Fast commits record delta in one of the following three categories.
26aa75f4d3SHarshad Shirwadkar  *
27aa75f4d3SHarshad Shirwadkar  * (A) Directory entry updates:
28aa75f4d3SHarshad Shirwadkar  *
29aa75f4d3SHarshad Shirwadkar  * - EXT4_FC_TAG_UNLINK		- records directory entry unlink
30aa75f4d3SHarshad Shirwadkar  * - EXT4_FC_TAG_LINK		- records directory entry link
31aa75f4d3SHarshad Shirwadkar  * - EXT4_FC_TAG_CREAT		- records inode and directory entry creation
32aa75f4d3SHarshad Shirwadkar  *
33aa75f4d3SHarshad Shirwadkar  * (B) File specific data range updates:
34aa75f4d3SHarshad Shirwadkar  *
35aa75f4d3SHarshad Shirwadkar  * - EXT4_FC_TAG_ADD_RANGE	- records addition of new blocks to an inode
36aa75f4d3SHarshad Shirwadkar  * - EXT4_FC_TAG_DEL_RANGE	- records deletion of blocks from an inode
37aa75f4d3SHarshad Shirwadkar  *
38aa75f4d3SHarshad Shirwadkar  * (C) Inode metadata (mtime / ctime etc):
39aa75f4d3SHarshad Shirwadkar  *
40aa75f4d3SHarshad Shirwadkar  * - EXT4_FC_TAG_INODE		- record the inode that should be replayed
41aa75f4d3SHarshad Shirwadkar  *				  during recovery. Note that iblocks field is
42aa75f4d3SHarshad Shirwadkar  *				  not replayed and instead derived during
43aa75f4d3SHarshad Shirwadkar  *				  replay.
44aa75f4d3SHarshad Shirwadkar  * Commit Operation
45aa75f4d3SHarshad Shirwadkar  * ----------------
46aa75f4d3SHarshad Shirwadkar  * With fast commits, we maintain all the directory entry operations in the
47aa75f4d3SHarshad Shirwadkar  * order in which they are issued in an in-memory queue. This queue is flushed
48aa75f4d3SHarshad Shirwadkar  * to disk during the commit operation. We also maintain a list of inodes
49aa75f4d3SHarshad Shirwadkar  * that need to be committed during a fast commit in another in memory queue of
50aa75f4d3SHarshad Shirwadkar  * inodes. During the commit operation, we commit in the following order:
51aa75f4d3SHarshad Shirwadkar  *
52aa75f4d3SHarshad Shirwadkar  * [1] Lock inodes for any further data updates by setting COMMITTING state
53aa75f4d3SHarshad Shirwadkar  * [2] Submit data buffers of all the inodes
54aa75f4d3SHarshad Shirwadkar  * [3] Wait for [2] to complete
55aa75f4d3SHarshad Shirwadkar  * [4] Commit all the directory entry updates in the fast commit space
56aa75f4d3SHarshad Shirwadkar  * [5] Commit all the changed inode structures
57aa75f4d3SHarshad Shirwadkar  * [6] Write tail tag (this tag ensures the atomicity, please read the following
58aa75f4d3SHarshad Shirwadkar  *     section for more details).
59aa75f4d3SHarshad Shirwadkar  * [7] Wait for [4], [5] and [6] to complete.
60aa75f4d3SHarshad Shirwadkar  *
61aa75f4d3SHarshad Shirwadkar  * All the inode updates must call ext4_fc_start_update() before starting an
62aa75f4d3SHarshad Shirwadkar  * update. If such an ongoing update is present, fast commit waits for it to
63aa75f4d3SHarshad Shirwadkar  * complete. The completion of such an update is marked by
64aa75f4d3SHarshad Shirwadkar  * ext4_fc_stop_update().
65aa75f4d3SHarshad Shirwadkar  *
66aa75f4d3SHarshad Shirwadkar  * Fast Commit Ineligibility
67aa75f4d3SHarshad Shirwadkar  * -------------------------
68aa75f4d3SHarshad Shirwadkar  * Not all operations are supported by fast commits today (e.g extended
69aa75f4d3SHarshad Shirwadkar  * attributes). Fast commit ineligiblity is marked by calling one of the
70aa75f4d3SHarshad Shirwadkar  * two following functions:
71aa75f4d3SHarshad Shirwadkar  *
72aa75f4d3SHarshad Shirwadkar  * - ext4_fc_mark_ineligible(): This makes next fast commit operation to fall
73aa75f4d3SHarshad Shirwadkar  *   back to full commit. This is useful in case of transient errors.
74aa75f4d3SHarshad Shirwadkar  *
75aa75f4d3SHarshad Shirwadkar  * - ext4_fc_start_ineligible() and ext4_fc_stop_ineligible() - This makes all
76aa75f4d3SHarshad Shirwadkar  *   the fast commits happening between ext4_fc_start_ineligible() and
77aa75f4d3SHarshad Shirwadkar  *   ext4_fc_stop_ineligible() and one fast commit after the call to
78aa75f4d3SHarshad Shirwadkar  *   ext4_fc_stop_ineligible() to fall back to full commits. It is important to
79aa75f4d3SHarshad Shirwadkar  *   make one more fast commit to fall back to full commit after stop call so
80aa75f4d3SHarshad Shirwadkar  *   that it guaranteed that the fast commit ineligible operation contained
81aa75f4d3SHarshad Shirwadkar  *   within ext4_fc_start_ineligible() and ext4_fc_stop_ineligible() is
82aa75f4d3SHarshad Shirwadkar  *   followed by at least 1 full commit.
83aa75f4d3SHarshad Shirwadkar  *
84aa75f4d3SHarshad Shirwadkar  * Atomicity of commits
85aa75f4d3SHarshad Shirwadkar  * --------------------
86aa75f4d3SHarshad Shirwadkar  * In order to gaurantee atomicity during the commit operation, fast commit
87aa75f4d3SHarshad Shirwadkar  * uses "EXT4_FC_TAG_TAIL" tag that marks a fast commit as complete. Tail
88aa75f4d3SHarshad Shirwadkar  * tag contains CRC of the contents and TID of the transaction after which
89aa75f4d3SHarshad Shirwadkar  * this fast commit should be applied. Recovery code replays fast commit
90aa75f4d3SHarshad Shirwadkar  * logs only if there's at least 1 valid tail present. For every fast commit
91aa75f4d3SHarshad Shirwadkar  * operation, there is 1 tail. This means, we may end up with multiple tails
92aa75f4d3SHarshad Shirwadkar  * in the fast commit space. Here's an example:
93aa75f4d3SHarshad Shirwadkar  *
94aa75f4d3SHarshad Shirwadkar  * - Create a new file A and remove existing file B
95aa75f4d3SHarshad Shirwadkar  * - fsync()
96aa75f4d3SHarshad Shirwadkar  * - Append contents to file A
97aa75f4d3SHarshad Shirwadkar  * - Truncate file A
98aa75f4d3SHarshad Shirwadkar  * - fsync()
99aa75f4d3SHarshad Shirwadkar  *
100aa75f4d3SHarshad Shirwadkar  * The fast commit space at the end of above operations would look like this:
101aa75f4d3SHarshad Shirwadkar  *      [HEAD] [CREAT A] [UNLINK B] [TAIL] [ADD_RANGE A] [DEL_RANGE A] [TAIL]
102aa75f4d3SHarshad Shirwadkar  *             |<---  Fast Commit 1   --->|<---      Fast Commit 2     ---->|
103aa75f4d3SHarshad Shirwadkar  *
104aa75f4d3SHarshad Shirwadkar  * Replay code should thus check for all the valid tails in the FC area.
105aa75f4d3SHarshad Shirwadkar  *
106aa75f4d3SHarshad Shirwadkar  * TODOs
107aa75f4d3SHarshad Shirwadkar  * -----
108aa75f4d3SHarshad Shirwadkar  * 1) Make fast commit atomic updates more fine grained. Today, a fast commit
109aa75f4d3SHarshad Shirwadkar  *    eligible update must be protected within ext4_fc_start_update() and
110aa75f4d3SHarshad Shirwadkar  *    ext4_fc_stop_update(). These routines are called at much higher
111aa75f4d3SHarshad Shirwadkar  *    routines. This can be made more fine grained by combining with
112aa75f4d3SHarshad Shirwadkar  *    ext4_journal_start().
113aa75f4d3SHarshad Shirwadkar  *
114aa75f4d3SHarshad Shirwadkar  * 2) Same above for ext4_fc_start_ineligible() and ext4_fc_stop_ineligible()
115aa75f4d3SHarshad Shirwadkar  *
116aa75f4d3SHarshad Shirwadkar  * 3) Handle more ineligible cases.
117aa75f4d3SHarshad Shirwadkar  */
118aa75f4d3SHarshad Shirwadkar 
119aa75f4d3SHarshad Shirwadkar #include <trace/events/ext4.h>
120aa75f4d3SHarshad Shirwadkar static struct kmem_cache *ext4_fc_dentry_cachep;
121aa75f4d3SHarshad Shirwadkar 
122aa75f4d3SHarshad Shirwadkar static void ext4_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
123aa75f4d3SHarshad Shirwadkar {
124aa75f4d3SHarshad Shirwadkar 	BUFFER_TRACE(bh, "");
125aa75f4d3SHarshad Shirwadkar 	if (uptodate) {
126aa75f4d3SHarshad Shirwadkar 		ext4_debug("%s: Block %lld up-to-date",
127aa75f4d3SHarshad Shirwadkar 			   __func__, bh->b_blocknr);
128aa75f4d3SHarshad Shirwadkar 		set_buffer_uptodate(bh);
129aa75f4d3SHarshad Shirwadkar 	} else {
130aa75f4d3SHarshad Shirwadkar 		ext4_debug("%s: Block %lld not up-to-date",
131aa75f4d3SHarshad Shirwadkar 			   __func__, bh->b_blocknr);
132aa75f4d3SHarshad Shirwadkar 		clear_buffer_uptodate(bh);
133aa75f4d3SHarshad Shirwadkar 	}
134aa75f4d3SHarshad Shirwadkar 
135aa75f4d3SHarshad Shirwadkar 	unlock_buffer(bh);
136aa75f4d3SHarshad Shirwadkar }
137aa75f4d3SHarshad Shirwadkar 
138aa75f4d3SHarshad Shirwadkar static inline void ext4_fc_reset_inode(struct inode *inode)
139aa75f4d3SHarshad Shirwadkar {
140aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *ei = EXT4_I(inode);
141aa75f4d3SHarshad Shirwadkar 
142aa75f4d3SHarshad Shirwadkar 	ei->i_fc_lblk_start = 0;
143aa75f4d3SHarshad Shirwadkar 	ei->i_fc_lblk_len = 0;
144aa75f4d3SHarshad Shirwadkar }
145aa75f4d3SHarshad Shirwadkar 
146aa75f4d3SHarshad Shirwadkar void ext4_fc_init_inode(struct inode *inode)
147aa75f4d3SHarshad Shirwadkar {
148aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *ei = EXT4_I(inode);
149aa75f4d3SHarshad Shirwadkar 
150aa75f4d3SHarshad Shirwadkar 	ext4_fc_reset_inode(inode);
151aa75f4d3SHarshad Shirwadkar 	ext4_clear_inode_state(inode, EXT4_STATE_FC_COMMITTING);
152aa75f4d3SHarshad Shirwadkar 	INIT_LIST_HEAD(&ei->i_fc_list);
153aa75f4d3SHarshad Shirwadkar 	init_waitqueue_head(&ei->i_fc_wait);
154aa75f4d3SHarshad Shirwadkar 	atomic_set(&ei->i_fc_updates, 0);
155aa75f4d3SHarshad Shirwadkar 	ei->i_fc_committed_subtid = 0;
156aa75f4d3SHarshad Shirwadkar }
157aa75f4d3SHarshad Shirwadkar 
158aa75f4d3SHarshad Shirwadkar /*
159aa75f4d3SHarshad Shirwadkar  * Inform Ext4's fast about start of an inode update
160aa75f4d3SHarshad Shirwadkar  *
161aa75f4d3SHarshad Shirwadkar  * This function is called by the high level call VFS callbacks before
162aa75f4d3SHarshad Shirwadkar  * performing any inode update. This function blocks if there's an ongoing
163aa75f4d3SHarshad Shirwadkar  * fast commit on the inode in question.
164aa75f4d3SHarshad Shirwadkar  */
165aa75f4d3SHarshad Shirwadkar void ext4_fc_start_update(struct inode *inode)
166aa75f4d3SHarshad Shirwadkar {
167aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *ei = EXT4_I(inode);
168aa75f4d3SHarshad Shirwadkar 
1698016e29fSHarshad Shirwadkar 	if (!test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT) ||
1708016e29fSHarshad Shirwadkar 	    (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY))
171aa75f4d3SHarshad Shirwadkar 		return;
172aa75f4d3SHarshad Shirwadkar 
173aa75f4d3SHarshad Shirwadkar restart:
174aa75f4d3SHarshad Shirwadkar 	spin_lock(&EXT4_SB(inode->i_sb)->s_fc_lock);
175aa75f4d3SHarshad Shirwadkar 	if (list_empty(&ei->i_fc_list))
176aa75f4d3SHarshad Shirwadkar 		goto out;
177aa75f4d3SHarshad Shirwadkar 
178aa75f4d3SHarshad Shirwadkar 	if (ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING)) {
179aa75f4d3SHarshad Shirwadkar 		wait_queue_head_t *wq;
180aa75f4d3SHarshad Shirwadkar #if (BITS_PER_LONG < 64)
181aa75f4d3SHarshad Shirwadkar 		DEFINE_WAIT_BIT(wait, &ei->i_state_flags,
182aa75f4d3SHarshad Shirwadkar 				EXT4_STATE_FC_COMMITTING);
183aa75f4d3SHarshad Shirwadkar 		wq = bit_waitqueue(&ei->i_state_flags,
184aa75f4d3SHarshad Shirwadkar 				   EXT4_STATE_FC_COMMITTING);
185aa75f4d3SHarshad Shirwadkar #else
186aa75f4d3SHarshad Shirwadkar 		DEFINE_WAIT_BIT(wait, &ei->i_flags,
187aa75f4d3SHarshad Shirwadkar 				EXT4_STATE_FC_COMMITTING);
188aa75f4d3SHarshad Shirwadkar 		wq = bit_waitqueue(&ei->i_flags,
189aa75f4d3SHarshad Shirwadkar 				   EXT4_STATE_FC_COMMITTING);
190aa75f4d3SHarshad Shirwadkar #endif
191aa75f4d3SHarshad Shirwadkar 		prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
192aa75f4d3SHarshad Shirwadkar 		spin_unlock(&EXT4_SB(inode->i_sb)->s_fc_lock);
193aa75f4d3SHarshad Shirwadkar 		schedule();
194aa75f4d3SHarshad Shirwadkar 		finish_wait(wq, &wait.wq_entry);
195aa75f4d3SHarshad Shirwadkar 		goto restart;
196aa75f4d3SHarshad Shirwadkar 	}
197aa75f4d3SHarshad Shirwadkar out:
198aa75f4d3SHarshad Shirwadkar 	atomic_inc(&ei->i_fc_updates);
199aa75f4d3SHarshad Shirwadkar 	spin_unlock(&EXT4_SB(inode->i_sb)->s_fc_lock);
200aa75f4d3SHarshad Shirwadkar }
201aa75f4d3SHarshad Shirwadkar 
202aa75f4d3SHarshad Shirwadkar /*
203aa75f4d3SHarshad Shirwadkar  * Stop inode update and wake up waiting fast commits if any.
204aa75f4d3SHarshad Shirwadkar  */
205aa75f4d3SHarshad Shirwadkar void ext4_fc_stop_update(struct inode *inode)
206aa75f4d3SHarshad Shirwadkar {
207aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *ei = EXT4_I(inode);
208aa75f4d3SHarshad Shirwadkar 
2098016e29fSHarshad Shirwadkar 	if (!test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT) ||
2108016e29fSHarshad Shirwadkar 	    (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY))
211aa75f4d3SHarshad Shirwadkar 		return;
212aa75f4d3SHarshad Shirwadkar 
213aa75f4d3SHarshad Shirwadkar 	if (atomic_dec_and_test(&ei->i_fc_updates))
214aa75f4d3SHarshad Shirwadkar 		wake_up_all(&ei->i_fc_wait);
215aa75f4d3SHarshad Shirwadkar }
216aa75f4d3SHarshad Shirwadkar 
217aa75f4d3SHarshad Shirwadkar /*
218aa75f4d3SHarshad Shirwadkar  * Remove inode from fast commit list. If the inode is being committed
219aa75f4d3SHarshad Shirwadkar  * we wait until inode commit is done.
220aa75f4d3SHarshad Shirwadkar  */
221aa75f4d3SHarshad Shirwadkar void ext4_fc_del(struct inode *inode)
222aa75f4d3SHarshad Shirwadkar {
223aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *ei = EXT4_I(inode);
224aa75f4d3SHarshad Shirwadkar 
2258016e29fSHarshad Shirwadkar 	if (!test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT) ||
2268016e29fSHarshad Shirwadkar 	    (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY))
227aa75f4d3SHarshad Shirwadkar 		return;
228aa75f4d3SHarshad Shirwadkar 
229aa75f4d3SHarshad Shirwadkar restart:
230aa75f4d3SHarshad Shirwadkar 	spin_lock(&EXT4_SB(inode->i_sb)->s_fc_lock);
231aa75f4d3SHarshad Shirwadkar 	if (list_empty(&ei->i_fc_list)) {
232aa75f4d3SHarshad Shirwadkar 		spin_unlock(&EXT4_SB(inode->i_sb)->s_fc_lock);
233aa75f4d3SHarshad Shirwadkar 		return;
234aa75f4d3SHarshad Shirwadkar 	}
235aa75f4d3SHarshad Shirwadkar 
236aa75f4d3SHarshad Shirwadkar 	if (ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING)) {
237aa75f4d3SHarshad Shirwadkar 		wait_queue_head_t *wq;
238aa75f4d3SHarshad Shirwadkar #if (BITS_PER_LONG < 64)
239aa75f4d3SHarshad Shirwadkar 		DEFINE_WAIT_BIT(wait, &ei->i_state_flags,
240aa75f4d3SHarshad Shirwadkar 				EXT4_STATE_FC_COMMITTING);
241aa75f4d3SHarshad Shirwadkar 		wq = bit_waitqueue(&ei->i_state_flags,
242aa75f4d3SHarshad Shirwadkar 				   EXT4_STATE_FC_COMMITTING);
243aa75f4d3SHarshad Shirwadkar #else
244aa75f4d3SHarshad Shirwadkar 		DEFINE_WAIT_BIT(wait, &ei->i_flags,
245aa75f4d3SHarshad Shirwadkar 				EXT4_STATE_FC_COMMITTING);
246aa75f4d3SHarshad Shirwadkar 		wq = bit_waitqueue(&ei->i_flags,
247aa75f4d3SHarshad Shirwadkar 				   EXT4_STATE_FC_COMMITTING);
248aa75f4d3SHarshad Shirwadkar #endif
249aa75f4d3SHarshad Shirwadkar 		prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
250aa75f4d3SHarshad Shirwadkar 		spin_unlock(&EXT4_SB(inode->i_sb)->s_fc_lock);
251aa75f4d3SHarshad Shirwadkar 		schedule();
252aa75f4d3SHarshad Shirwadkar 		finish_wait(wq, &wait.wq_entry);
253aa75f4d3SHarshad Shirwadkar 		goto restart;
254aa75f4d3SHarshad Shirwadkar 	}
255aa75f4d3SHarshad Shirwadkar 	if (!list_empty(&ei->i_fc_list))
256aa75f4d3SHarshad Shirwadkar 		list_del_init(&ei->i_fc_list);
257aa75f4d3SHarshad Shirwadkar 	spin_unlock(&EXT4_SB(inode->i_sb)->s_fc_lock);
258aa75f4d3SHarshad Shirwadkar }
259aa75f4d3SHarshad Shirwadkar 
260aa75f4d3SHarshad Shirwadkar /*
261aa75f4d3SHarshad Shirwadkar  * Mark file system as fast commit ineligible. This means that next commit
262aa75f4d3SHarshad Shirwadkar  * operation would result in a full jbd2 commit.
263aa75f4d3SHarshad Shirwadkar  */
264aa75f4d3SHarshad Shirwadkar void ext4_fc_mark_ineligible(struct super_block *sb, int reason)
265aa75f4d3SHarshad Shirwadkar {
266aa75f4d3SHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(sb);
267aa75f4d3SHarshad Shirwadkar 
2688016e29fSHarshad Shirwadkar 	if (!test_opt2(sb, JOURNAL_FAST_COMMIT) ||
2698016e29fSHarshad Shirwadkar 	    (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))
2708016e29fSHarshad Shirwadkar 		return;
2718016e29fSHarshad Shirwadkar 
272ababea77SHarshad Shirwadkar 	sbi->s_mount_flags |= EXT4_MF_FC_INELIGIBLE;
273aa75f4d3SHarshad Shirwadkar 	WARN_ON(reason >= EXT4_FC_REASON_MAX);
274aa75f4d3SHarshad Shirwadkar 	sbi->s_fc_stats.fc_ineligible_reason_count[reason]++;
275aa75f4d3SHarshad Shirwadkar }
276aa75f4d3SHarshad Shirwadkar 
277aa75f4d3SHarshad Shirwadkar /*
278aa75f4d3SHarshad Shirwadkar  * Start a fast commit ineligible update. Any commits that happen while
279aa75f4d3SHarshad Shirwadkar  * such an operation is in progress fall back to full commits.
280aa75f4d3SHarshad Shirwadkar  */
281aa75f4d3SHarshad Shirwadkar void ext4_fc_start_ineligible(struct super_block *sb, int reason)
282aa75f4d3SHarshad Shirwadkar {
283aa75f4d3SHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(sb);
284aa75f4d3SHarshad Shirwadkar 
2858016e29fSHarshad Shirwadkar 	if (!test_opt2(sb, JOURNAL_FAST_COMMIT) ||
2868016e29fSHarshad Shirwadkar 	    (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))
2878016e29fSHarshad Shirwadkar 		return;
2888016e29fSHarshad Shirwadkar 
289aa75f4d3SHarshad Shirwadkar 	WARN_ON(reason >= EXT4_FC_REASON_MAX);
290aa75f4d3SHarshad Shirwadkar 	sbi->s_fc_stats.fc_ineligible_reason_count[reason]++;
291aa75f4d3SHarshad Shirwadkar 	atomic_inc(&sbi->s_fc_ineligible_updates);
292aa75f4d3SHarshad Shirwadkar }
293aa75f4d3SHarshad Shirwadkar 
294aa75f4d3SHarshad Shirwadkar /*
295ababea77SHarshad Shirwadkar  * Stop a fast commit ineligible update. We set EXT4_MF_FC_INELIGIBLE flag here
296aa75f4d3SHarshad Shirwadkar  * to ensure that after stopping the ineligible update, at least one full
297aa75f4d3SHarshad Shirwadkar  * commit takes place.
298aa75f4d3SHarshad Shirwadkar  */
299aa75f4d3SHarshad Shirwadkar void ext4_fc_stop_ineligible(struct super_block *sb)
300aa75f4d3SHarshad Shirwadkar {
3018016e29fSHarshad Shirwadkar 	if (!test_opt2(sb, JOURNAL_FAST_COMMIT) ||
3028016e29fSHarshad Shirwadkar 	    (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))
3038016e29fSHarshad Shirwadkar 		return;
3048016e29fSHarshad Shirwadkar 
305ababea77SHarshad Shirwadkar 	EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FC_INELIGIBLE;
306aa75f4d3SHarshad Shirwadkar 	atomic_dec(&EXT4_SB(sb)->s_fc_ineligible_updates);
307aa75f4d3SHarshad Shirwadkar }
308aa75f4d3SHarshad Shirwadkar 
309aa75f4d3SHarshad Shirwadkar static inline int ext4_fc_is_ineligible(struct super_block *sb)
310aa75f4d3SHarshad Shirwadkar {
311ababea77SHarshad Shirwadkar 	return (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FC_INELIGIBLE) ||
312aa75f4d3SHarshad Shirwadkar 		atomic_read(&EXT4_SB(sb)->s_fc_ineligible_updates);
313aa75f4d3SHarshad Shirwadkar }
314aa75f4d3SHarshad Shirwadkar 
315aa75f4d3SHarshad Shirwadkar /*
316aa75f4d3SHarshad Shirwadkar  * Generic fast commit tracking function. If this is the first time this we are
317aa75f4d3SHarshad Shirwadkar  * called after a full commit, we initialize fast commit fields and then call
318aa75f4d3SHarshad Shirwadkar  * __fc_track_fn() with update = 0. If we have already been called after a full
319aa75f4d3SHarshad Shirwadkar  * commit, we pass update = 1. Based on that, the track function can determine
320aa75f4d3SHarshad Shirwadkar  * if it needs to track a field for the first time or if it needs to just
321aa75f4d3SHarshad Shirwadkar  * update the previously tracked value.
322aa75f4d3SHarshad Shirwadkar  *
323aa75f4d3SHarshad Shirwadkar  * If enqueue is set, this function enqueues the inode in fast commit list.
324aa75f4d3SHarshad Shirwadkar  */
325aa75f4d3SHarshad Shirwadkar static int ext4_fc_track_template(
326a80f7fcfSHarshad Shirwadkar 	handle_t *handle, struct inode *inode,
327a80f7fcfSHarshad Shirwadkar 	int (*__fc_track_fn)(struct inode *, void *, bool),
328aa75f4d3SHarshad Shirwadkar 	void *args, int enqueue)
329aa75f4d3SHarshad Shirwadkar {
330aa75f4d3SHarshad Shirwadkar 	bool update = false;
331aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *ei = EXT4_I(inode);
332aa75f4d3SHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
333a80f7fcfSHarshad Shirwadkar 	tid_t tid = 0;
334aa75f4d3SHarshad Shirwadkar 	int ret;
335aa75f4d3SHarshad Shirwadkar 
3368016e29fSHarshad Shirwadkar 	if (!test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT) ||
3378016e29fSHarshad Shirwadkar 	    (sbi->s_mount_state & EXT4_FC_REPLAY))
338aa75f4d3SHarshad Shirwadkar 		return -EOPNOTSUPP;
339aa75f4d3SHarshad Shirwadkar 
340aa75f4d3SHarshad Shirwadkar 	if (ext4_fc_is_ineligible(inode->i_sb))
341aa75f4d3SHarshad Shirwadkar 		return -EINVAL;
342aa75f4d3SHarshad Shirwadkar 
343a80f7fcfSHarshad Shirwadkar 	tid = handle->h_transaction->t_tid;
344aa75f4d3SHarshad Shirwadkar 	mutex_lock(&ei->i_fc_lock);
345a80f7fcfSHarshad Shirwadkar 	if (tid == ei->i_sync_tid) {
346aa75f4d3SHarshad Shirwadkar 		update = true;
347aa75f4d3SHarshad Shirwadkar 	} else {
348aa75f4d3SHarshad Shirwadkar 		ext4_fc_reset_inode(inode);
349a80f7fcfSHarshad Shirwadkar 		ei->i_sync_tid = tid;
350aa75f4d3SHarshad Shirwadkar 	}
351aa75f4d3SHarshad Shirwadkar 	ret = __fc_track_fn(inode, args, update);
352aa75f4d3SHarshad Shirwadkar 	mutex_unlock(&ei->i_fc_lock);
353aa75f4d3SHarshad Shirwadkar 
354aa75f4d3SHarshad Shirwadkar 	if (!enqueue)
355aa75f4d3SHarshad Shirwadkar 		return ret;
356aa75f4d3SHarshad Shirwadkar 
357aa75f4d3SHarshad Shirwadkar 	spin_lock(&sbi->s_fc_lock);
358aa75f4d3SHarshad Shirwadkar 	if (list_empty(&EXT4_I(inode)->i_fc_list))
359aa75f4d3SHarshad Shirwadkar 		list_add_tail(&EXT4_I(inode)->i_fc_list,
360ababea77SHarshad Shirwadkar 				(sbi->s_mount_flags & EXT4_MF_FC_COMMITTING) ?
361aa75f4d3SHarshad Shirwadkar 				&sbi->s_fc_q[FC_Q_STAGING] :
362aa75f4d3SHarshad Shirwadkar 				&sbi->s_fc_q[FC_Q_MAIN]);
363aa75f4d3SHarshad Shirwadkar 	spin_unlock(&sbi->s_fc_lock);
364aa75f4d3SHarshad Shirwadkar 
365aa75f4d3SHarshad Shirwadkar 	return ret;
366aa75f4d3SHarshad Shirwadkar }
367aa75f4d3SHarshad Shirwadkar 
368aa75f4d3SHarshad Shirwadkar struct __track_dentry_update_args {
369aa75f4d3SHarshad Shirwadkar 	struct dentry *dentry;
370aa75f4d3SHarshad Shirwadkar 	int op;
371aa75f4d3SHarshad Shirwadkar };
372aa75f4d3SHarshad Shirwadkar 
373aa75f4d3SHarshad Shirwadkar /* __track_fn for directory entry updates. Called with ei->i_fc_lock. */
374aa75f4d3SHarshad Shirwadkar static int __track_dentry_update(struct inode *inode, void *arg, bool update)
375aa75f4d3SHarshad Shirwadkar {
376aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_dentry_update *node;
377aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *ei = EXT4_I(inode);
378aa75f4d3SHarshad Shirwadkar 	struct __track_dentry_update_args *dentry_update =
379aa75f4d3SHarshad Shirwadkar 		(struct __track_dentry_update_args *)arg;
380aa75f4d3SHarshad Shirwadkar 	struct dentry *dentry = dentry_update->dentry;
381aa75f4d3SHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
382aa75f4d3SHarshad Shirwadkar 
383aa75f4d3SHarshad Shirwadkar 	mutex_unlock(&ei->i_fc_lock);
384aa75f4d3SHarshad Shirwadkar 	node = kmem_cache_alloc(ext4_fc_dentry_cachep, GFP_NOFS);
385aa75f4d3SHarshad Shirwadkar 	if (!node) {
386b21ebf14SHarshad Shirwadkar 		ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_NOMEM);
387aa75f4d3SHarshad Shirwadkar 		mutex_lock(&ei->i_fc_lock);
388aa75f4d3SHarshad Shirwadkar 		return -ENOMEM;
389aa75f4d3SHarshad Shirwadkar 	}
390aa75f4d3SHarshad Shirwadkar 
391aa75f4d3SHarshad Shirwadkar 	node->fcd_op = dentry_update->op;
392aa75f4d3SHarshad Shirwadkar 	node->fcd_parent = dentry->d_parent->d_inode->i_ino;
393aa75f4d3SHarshad Shirwadkar 	node->fcd_ino = inode->i_ino;
394aa75f4d3SHarshad Shirwadkar 	if (dentry->d_name.len > DNAME_INLINE_LEN) {
395aa75f4d3SHarshad Shirwadkar 		node->fcd_name.name = kmalloc(dentry->d_name.len, GFP_NOFS);
396aa75f4d3SHarshad Shirwadkar 		if (!node->fcd_name.name) {
397aa75f4d3SHarshad Shirwadkar 			kmem_cache_free(ext4_fc_dentry_cachep, node);
398aa75f4d3SHarshad Shirwadkar 			ext4_fc_mark_ineligible(inode->i_sb,
399b21ebf14SHarshad Shirwadkar 				EXT4_FC_REASON_NOMEM);
400aa75f4d3SHarshad Shirwadkar 			mutex_lock(&ei->i_fc_lock);
401aa75f4d3SHarshad Shirwadkar 			return -ENOMEM;
402aa75f4d3SHarshad Shirwadkar 		}
403aa75f4d3SHarshad Shirwadkar 		memcpy((u8 *)node->fcd_name.name, dentry->d_name.name,
404aa75f4d3SHarshad Shirwadkar 			dentry->d_name.len);
405aa75f4d3SHarshad Shirwadkar 	} else {
406aa75f4d3SHarshad Shirwadkar 		memcpy(node->fcd_iname, dentry->d_name.name,
407aa75f4d3SHarshad Shirwadkar 			dentry->d_name.len);
408aa75f4d3SHarshad Shirwadkar 		node->fcd_name.name = node->fcd_iname;
409aa75f4d3SHarshad Shirwadkar 	}
410aa75f4d3SHarshad Shirwadkar 	node->fcd_name.len = dentry->d_name.len;
411aa75f4d3SHarshad Shirwadkar 
412aa75f4d3SHarshad Shirwadkar 	spin_lock(&sbi->s_fc_lock);
413ababea77SHarshad Shirwadkar 	if (sbi->s_mount_flags & EXT4_MF_FC_COMMITTING)
414aa75f4d3SHarshad Shirwadkar 		list_add_tail(&node->fcd_list,
415aa75f4d3SHarshad Shirwadkar 				&sbi->s_fc_dentry_q[FC_Q_STAGING]);
416aa75f4d3SHarshad Shirwadkar 	else
417aa75f4d3SHarshad Shirwadkar 		list_add_tail(&node->fcd_list, &sbi->s_fc_dentry_q[FC_Q_MAIN]);
418aa75f4d3SHarshad Shirwadkar 	spin_unlock(&sbi->s_fc_lock);
419aa75f4d3SHarshad Shirwadkar 	mutex_lock(&ei->i_fc_lock);
420aa75f4d3SHarshad Shirwadkar 
421aa75f4d3SHarshad Shirwadkar 	return 0;
422aa75f4d3SHarshad Shirwadkar }
423aa75f4d3SHarshad Shirwadkar 
424a80f7fcfSHarshad Shirwadkar void __ext4_fc_track_unlink(handle_t *handle,
425a80f7fcfSHarshad Shirwadkar 		struct inode *inode, struct dentry *dentry)
426aa75f4d3SHarshad Shirwadkar {
427aa75f4d3SHarshad Shirwadkar 	struct __track_dentry_update_args args;
428aa75f4d3SHarshad Shirwadkar 	int ret;
429aa75f4d3SHarshad Shirwadkar 
430aa75f4d3SHarshad Shirwadkar 	args.dentry = dentry;
431aa75f4d3SHarshad Shirwadkar 	args.op = EXT4_FC_TAG_UNLINK;
432aa75f4d3SHarshad Shirwadkar 
433a80f7fcfSHarshad Shirwadkar 	ret = ext4_fc_track_template(handle, inode, __track_dentry_update,
434aa75f4d3SHarshad Shirwadkar 					(void *)&args, 0);
435aa75f4d3SHarshad Shirwadkar 	trace_ext4_fc_track_unlink(inode, dentry, ret);
436aa75f4d3SHarshad Shirwadkar }
437aa75f4d3SHarshad Shirwadkar 
438a80f7fcfSHarshad Shirwadkar void ext4_fc_track_unlink(handle_t *handle, struct dentry *dentry)
439a80f7fcfSHarshad Shirwadkar {
440a80f7fcfSHarshad Shirwadkar 	__ext4_fc_track_unlink(handle, d_inode(dentry), dentry);
441a80f7fcfSHarshad Shirwadkar }
442a80f7fcfSHarshad Shirwadkar 
443a80f7fcfSHarshad Shirwadkar void __ext4_fc_track_link(handle_t *handle,
444a80f7fcfSHarshad Shirwadkar 	struct inode *inode, struct dentry *dentry)
445aa75f4d3SHarshad Shirwadkar {
446aa75f4d3SHarshad Shirwadkar 	struct __track_dentry_update_args args;
447aa75f4d3SHarshad Shirwadkar 	int ret;
448aa75f4d3SHarshad Shirwadkar 
449aa75f4d3SHarshad Shirwadkar 	args.dentry = dentry;
450aa75f4d3SHarshad Shirwadkar 	args.op = EXT4_FC_TAG_LINK;
451aa75f4d3SHarshad Shirwadkar 
452a80f7fcfSHarshad Shirwadkar 	ret = ext4_fc_track_template(handle, inode, __track_dentry_update,
453aa75f4d3SHarshad Shirwadkar 					(void *)&args, 0);
454aa75f4d3SHarshad Shirwadkar 	trace_ext4_fc_track_link(inode, dentry, ret);
455aa75f4d3SHarshad Shirwadkar }
456aa75f4d3SHarshad Shirwadkar 
457a80f7fcfSHarshad Shirwadkar void ext4_fc_track_link(handle_t *handle, struct dentry *dentry)
458a80f7fcfSHarshad Shirwadkar {
459a80f7fcfSHarshad Shirwadkar 	__ext4_fc_track_link(handle, d_inode(dentry), dentry);
460a80f7fcfSHarshad Shirwadkar }
461a80f7fcfSHarshad Shirwadkar 
462a80f7fcfSHarshad Shirwadkar void ext4_fc_track_create(handle_t *handle, struct dentry *dentry)
463aa75f4d3SHarshad Shirwadkar {
464aa75f4d3SHarshad Shirwadkar 	struct __track_dentry_update_args args;
465a80f7fcfSHarshad Shirwadkar 	struct inode *inode = d_inode(dentry);
466aa75f4d3SHarshad Shirwadkar 	int ret;
467aa75f4d3SHarshad Shirwadkar 
468aa75f4d3SHarshad Shirwadkar 	args.dentry = dentry;
469aa75f4d3SHarshad Shirwadkar 	args.op = EXT4_FC_TAG_CREAT;
470aa75f4d3SHarshad Shirwadkar 
471a80f7fcfSHarshad Shirwadkar 	ret = ext4_fc_track_template(handle, inode, __track_dentry_update,
472aa75f4d3SHarshad Shirwadkar 					(void *)&args, 0);
473aa75f4d3SHarshad Shirwadkar 	trace_ext4_fc_track_create(inode, dentry, ret);
474aa75f4d3SHarshad Shirwadkar }
475aa75f4d3SHarshad Shirwadkar 
476aa75f4d3SHarshad Shirwadkar /* __track_fn for inode tracking */
477aa75f4d3SHarshad Shirwadkar static int __track_inode(struct inode *inode, void *arg, bool update)
478aa75f4d3SHarshad Shirwadkar {
479aa75f4d3SHarshad Shirwadkar 	if (update)
480aa75f4d3SHarshad Shirwadkar 		return -EEXIST;
481aa75f4d3SHarshad Shirwadkar 
482aa75f4d3SHarshad Shirwadkar 	EXT4_I(inode)->i_fc_lblk_len = 0;
483aa75f4d3SHarshad Shirwadkar 
484aa75f4d3SHarshad Shirwadkar 	return 0;
485aa75f4d3SHarshad Shirwadkar }
486aa75f4d3SHarshad Shirwadkar 
487a80f7fcfSHarshad Shirwadkar void ext4_fc_track_inode(handle_t *handle, struct inode *inode)
488aa75f4d3SHarshad Shirwadkar {
489aa75f4d3SHarshad Shirwadkar 	int ret;
490aa75f4d3SHarshad Shirwadkar 
491aa75f4d3SHarshad Shirwadkar 	if (S_ISDIR(inode->i_mode))
492aa75f4d3SHarshad Shirwadkar 		return;
493aa75f4d3SHarshad Shirwadkar 
494a80f7fcfSHarshad Shirwadkar 	ret = ext4_fc_track_template(handle, inode, __track_inode, NULL, 1);
495aa75f4d3SHarshad Shirwadkar 	trace_ext4_fc_track_inode(inode, ret);
496aa75f4d3SHarshad Shirwadkar }
497aa75f4d3SHarshad Shirwadkar 
498aa75f4d3SHarshad Shirwadkar struct __track_range_args {
499aa75f4d3SHarshad Shirwadkar 	ext4_lblk_t start, end;
500aa75f4d3SHarshad Shirwadkar };
501aa75f4d3SHarshad Shirwadkar 
502aa75f4d3SHarshad Shirwadkar /* __track_fn for tracking data updates */
503aa75f4d3SHarshad Shirwadkar static int __track_range(struct inode *inode, void *arg, bool update)
504aa75f4d3SHarshad Shirwadkar {
505aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *ei = EXT4_I(inode);
506aa75f4d3SHarshad Shirwadkar 	ext4_lblk_t oldstart;
507aa75f4d3SHarshad Shirwadkar 	struct __track_range_args *__arg =
508aa75f4d3SHarshad Shirwadkar 		(struct __track_range_args *)arg;
509aa75f4d3SHarshad Shirwadkar 
510aa75f4d3SHarshad Shirwadkar 	if (inode->i_ino < EXT4_FIRST_INO(inode->i_sb)) {
511aa75f4d3SHarshad Shirwadkar 		ext4_debug("Special inode %ld being modified\n", inode->i_ino);
512aa75f4d3SHarshad Shirwadkar 		return -ECANCELED;
513aa75f4d3SHarshad Shirwadkar 	}
514aa75f4d3SHarshad Shirwadkar 
515aa75f4d3SHarshad Shirwadkar 	oldstart = ei->i_fc_lblk_start;
516aa75f4d3SHarshad Shirwadkar 
517aa75f4d3SHarshad Shirwadkar 	if (update && ei->i_fc_lblk_len > 0) {
518aa75f4d3SHarshad Shirwadkar 		ei->i_fc_lblk_start = min(ei->i_fc_lblk_start, __arg->start);
519aa75f4d3SHarshad Shirwadkar 		ei->i_fc_lblk_len =
520aa75f4d3SHarshad Shirwadkar 			max(oldstart + ei->i_fc_lblk_len - 1, __arg->end) -
521aa75f4d3SHarshad Shirwadkar 				ei->i_fc_lblk_start + 1;
522aa75f4d3SHarshad Shirwadkar 	} else {
523aa75f4d3SHarshad Shirwadkar 		ei->i_fc_lblk_start = __arg->start;
524aa75f4d3SHarshad Shirwadkar 		ei->i_fc_lblk_len = __arg->end - __arg->start + 1;
525aa75f4d3SHarshad Shirwadkar 	}
526aa75f4d3SHarshad Shirwadkar 
527aa75f4d3SHarshad Shirwadkar 	return 0;
528aa75f4d3SHarshad Shirwadkar }
529aa75f4d3SHarshad Shirwadkar 
530a80f7fcfSHarshad Shirwadkar void ext4_fc_track_range(handle_t *handle, struct inode *inode, ext4_lblk_t start,
531aa75f4d3SHarshad Shirwadkar 			 ext4_lblk_t end)
532aa75f4d3SHarshad Shirwadkar {
533aa75f4d3SHarshad Shirwadkar 	struct __track_range_args args;
534aa75f4d3SHarshad Shirwadkar 	int ret;
535aa75f4d3SHarshad Shirwadkar 
536aa75f4d3SHarshad Shirwadkar 	if (S_ISDIR(inode->i_mode))
537aa75f4d3SHarshad Shirwadkar 		return;
538aa75f4d3SHarshad Shirwadkar 
539aa75f4d3SHarshad Shirwadkar 	args.start = start;
540aa75f4d3SHarshad Shirwadkar 	args.end = end;
541aa75f4d3SHarshad Shirwadkar 
542a80f7fcfSHarshad Shirwadkar 	ret = ext4_fc_track_template(handle, inode,  __track_range, &args, 1);
543aa75f4d3SHarshad Shirwadkar 
544aa75f4d3SHarshad Shirwadkar 	trace_ext4_fc_track_range(inode, start, end, ret);
545aa75f4d3SHarshad Shirwadkar }
546aa75f4d3SHarshad Shirwadkar 
547aa75f4d3SHarshad Shirwadkar static void ext4_fc_submit_bh(struct super_block *sb)
548aa75f4d3SHarshad Shirwadkar {
549aa75f4d3SHarshad Shirwadkar 	int write_flags = REQ_SYNC;
550aa75f4d3SHarshad Shirwadkar 	struct buffer_head *bh = EXT4_SB(sb)->s_fc_bh;
551aa75f4d3SHarshad Shirwadkar 
552aa75f4d3SHarshad Shirwadkar 	if (test_opt(sb, BARRIER))
553aa75f4d3SHarshad Shirwadkar 		write_flags |= REQ_FUA | REQ_PREFLUSH;
554aa75f4d3SHarshad Shirwadkar 	lock_buffer(bh);
555aa75f4d3SHarshad Shirwadkar 	clear_buffer_dirty(bh);
556aa75f4d3SHarshad Shirwadkar 	set_buffer_uptodate(bh);
557aa75f4d3SHarshad Shirwadkar 	bh->b_end_io = ext4_end_buffer_io_sync;
558aa75f4d3SHarshad Shirwadkar 	submit_bh(REQ_OP_WRITE, write_flags, bh);
559aa75f4d3SHarshad Shirwadkar 	EXT4_SB(sb)->s_fc_bh = NULL;
560aa75f4d3SHarshad Shirwadkar }
561aa75f4d3SHarshad Shirwadkar 
562aa75f4d3SHarshad Shirwadkar /* Ext4 commit path routines */
563aa75f4d3SHarshad Shirwadkar 
564aa75f4d3SHarshad Shirwadkar /* memzero and update CRC */
565aa75f4d3SHarshad Shirwadkar static void *ext4_fc_memzero(struct super_block *sb, void *dst, int len,
566aa75f4d3SHarshad Shirwadkar 				u32 *crc)
567aa75f4d3SHarshad Shirwadkar {
568aa75f4d3SHarshad Shirwadkar 	void *ret;
569aa75f4d3SHarshad Shirwadkar 
570aa75f4d3SHarshad Shirwadkar 	ret = memset(dst, 0, len);
571aa75f4d3SHarshad Shirwadkar 	if (crc)
572aa75f4d3SHarshad Shirwadkar 		*crc = ext4_chksum(EXT4_SB(sb), *crc, dst, len);
573aa75f4d3SHarshad Shirwadkar 	return ret;
574aa75f4d3SHarshad Shirwadkar }
575aa75f4d3SHarshad Shirwadkar 
576aa75f4d3SHarshad Shirwadkar /*
577aa75f4d3SHarshad Shirwadkar  * Allocate len bytes on a fast commit buffer.
578aa75f4d3SHarshad Shirwadkar  *
579aa75f4d3SHarshad Shirwadkar  * During the commit time this function is used to manage fast commit
580aa75f4d3SHarshad Shirwadkar  * block space. We don't split a fast commit log onto different
581aa75f4d3SHarshad Shirwadkar  * blocks. So this function makes sure that if there's not enough space
582aa75f4d3SHarshad Shirwadkar  * on the current block, the remaining space in the current block is
583aa75f4d3SHarshad Shirwadkar  * marked as unused by adding EXT4_FC_TAG_PAD tag. In that case,
584aa75f4d3SHarshad Shirwadkar  * new block is from jbd2 and CRC is updated to reflect the padding
585aa75f4d3SHarshad Shirwadkar  * we added.
586aa75f4d3SHarshad Shirwadkar  */
587aa75f4d3SHarshad Shirwadkar static u8 *ext4_fc_reserve_space(struct super_block *sb, int len, u32 *crc)
588aa75f4d3SHarshad Shirwadkar {
589aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_tl *tl;
590aa75f4d3SHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(sb);
591aa75f4d3SHarshad Shirwadkar 	struct buffer_head *bh;
592aa75f4d3SHarshad Shirwadkar 	int bsize = sbi->s_journal->j_blocksize;
593aa75f4d3SHarshad Shirwadkar 	int ret, off = sbi->s_fc_bytes % bsize;
594aa75f4d3SHarshad Shirwadkar 	int pad_len;
595aa75f4d3SHarshad Shirwadkar 
596aa75f4d3SHarshad Shirwadkar 	/*
597aa75f4d3SHarshad Shirwadkar 	 * After allocating len, we should have space at least for a 0 byte
598aa75f4d3SHarshad Shirwadkar 	 * padding.
599aa75f4d3SHarshad Shirwadkar 	 */
600aa75f4d3SHarshad Shirwadkar 	if (len + sizeof(struct ext4_fc_tl) > bsize)
601aa75f4d3SHarshad Shirwadkar 		return NULL;
602aa75f4d3SHarshad Shirwadkar 
603aa75f4d3SHarshad Shirwadkar 	if (bsize - off - 1 > len + sizeof(struct ext4_fc_tl)) {
604aa75f4d3SHarshad Shirwadkar 		/*
605aa75f4d3SHarshad Shirwadkar 		 * Only allocate from current buffer if we have enough space for
606aa75f4d3SHarshad Shirwadkar 		 * this request AND we have space to add a zero byte padding.
607aa75f4d3SHarshad Shirwadkar 		 */
608aa75f4d3SHarshad Shirwadkar 		if (!sbi->s_fc_bh) {
609aa75f4d3SHarshad Shirwadkar 			ret = jbd2_fc_get_buf(EXT4_SB(sb)->s_journal, &bh);
610aa75f4d3SHarshad Shirwadkar 			if (ret)
611aa75f4d3SHarshad Shirwadkar 				return NULL;
612aa75f4d3SHarshad Shirwadkar 			sbi->s_fc_bh = bh;
613aa75f4d3SHarshad Shirwadkar 		}
614aa75f4d3SHarshad Shirwadkar 		sbi->s_fc_bytes += len;
615aa75f4d3SHarshad Shirwadkar 		return sbi->s_fc_bh->b_data + off;
616aa75f4d3SHarshad Shirwadkar 	}
617aa75f4d3SHarshad Shirwadkar 	/* Need to add PAD tag */
618aa75f4d3SHarshad Shirwadkar 	tl = (struct ext4_fc_tl *)(sbi->s_fc_bh->b_data + off);
619aa75f4d3SHarshad Shirwadkar 	tl->fc_tag = cpu_to_le16(EXT4_FC_TAG_PAD);
620aa75f4d3SHarshad Shirwadkar 	pad_len = bsize - off - 1 - sizeof(struct ext4_fc_tl);
621aa75f4d3SHarshad Shirwadkar 	tl->fc_len = cpu_to_le16(pad_len);
622aa75f4d3SHarshad Shirwadkar 	if (crc)
623aa75f4d3SHarshad Shirwadkar 		*crc = ext4_chksum(sbi, *crc, tl, sizeof(*tl));
624aa75f4d3SHarshad Shirwadkar 	if (pad_len > 0)
625aa75f4d3SHarshad Shirwadkar 		ext4_fc_memzero(sb, tl + 1, pad_len, crc);
626aa75f4d3SHarshad Shirwadkar 	ext4_fc_submit_bh(sb);
627aa75f4d3SHarshad Shirwadkar 
628aa75f4d3SHarshad Shirwadkar 	ret = jbd2_fc_get_buf(EXT4_SB(sb)->s_journal, &bh);
629aa75f4d3SHarshad Shirwadkar 	if (ret)
630aa75f4d3SHarshad Shirwadkar 		return NULL;
631aa75f4d3SHarshad Shirwadkar 	sbi->s_fc_bh = bh;
632aa75f4d3SHarshad Shirwadkar 	sbi->s_fc_bytes = (sbi->s_fc_bytes / bsize + 1) * bsize + len;
633aa75f4d3SHarshad Shirwadkar 	return sbi->s_fc_bh->b_data;
634aa75f4d3SHarshad Shirwadkar }
635aa75f4d3SHarshad Shirwadkar 
636aa75f4d3SHarshad Shirwadkar /* memcpy to fc reserved space and update CRC */
637aa75f4d3SHarshad Shirwadkar static void *ext4_fc_memcpy(struct super_block *sb, void *dst, const void *src,
638aa75f4d3SHarshad Shirwadkar 				int len, u32 *crc)
639aa75f4d3SHarshad Shirwadkar {
640aa75f4d3SHarshad Shirwadkar 	if (crc)
641aa75f4d3SHarshad Shirwadkar 		*crc = ext4_chksum(EXT4_SB(sb), *crc, src, len);
642aa75f4d3SHarshad Shirwadkar 	return memcpy(dst, src, len);
643aa75f4d3SHarshad Shirwadkar }
644aa75f4d3SHarshad Shirwadkar 
645aa75f4d3SHarshad Shirwadkar /*
646aa75f4d3SHarshad Shirwadkar  * Complete a fast commit by writing tail tag.
647aa75f4d3SHarshad Shirwadkar  *
648aa75f4d3SHarshad Shirwadkar  * Writing tail tag marks the end of a fast commit. In order to guarantee
649aa75f4d3SHarshad Shirwadkar  * atomicity, after writing tail tag, even if there's space remaining
650aa75f4d3SHarshad Shirwadkar  * in the block, next commit shouldn't use it. That's why tail tag
651aa75f4d3SHarshad Shirwadkar  * has the length as that of the remaining space on the block.
652aa75f4d3SHarshad Shirwadkar  */
653aa75f4d3SHarshad Shirwadkar static int ext4_fc_write_tail(struct super_block *sb, u32 crc)
654aa75f4d3SHarshad Shirwadkar {
655aa75f4d3SHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(sb);
656aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_tl tl;
657aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_tail tail;
658aa75f4d3SHarshad Shirwadkar 	int off, bsize = sbi->s_journal->j_blocksize;
659aa75f4d3SHarshad Shirwadkar 	u8 *dst;
660aa75f4d3SHarshad Shirwadkar 
661aa75f4d3SHarshad Shirwadkar 	/*
662aa75f4d3SHarshad Shirwadkar 	 * ext4_fc_reserve_space takes care of allocating an extra block if
663aa75f4d3SHarshad Shirwadkar 	 * there's no enough space on this block for accommodating this tail.
664aa75f4d3SHarshad Shirwadkar 	 */
665aa75f4d3SHarshad Shirwadkar 	dst = ext4_fc_reserve_space(sb, sizeof(tl) + sizeof(tail), &crc);
666aa75f4d3SHarshad Shirwadkar 	if (!dst)
667aa75f4d3SHarshad Shirwadkar 		return -ENOSPC;
668aa75f4d3SHarshad Shirwadkar 
669aa75f4d3SHarshad Shirwadkar 	off = sbi->s_fc_bytes % bsize;
670aa75f4d3SHarshad Shirwadkar 
671aa75f4d3SHarshad Shirwadkar 	tl.fc_tag = cpu_to_le16(EXT4_FC_TAG_TAIL);
672aa75f4d3SHarshad Shirwadkar 	tl.fc_len = cpu_to_le16(bsize - off - 1 + sizeof(struct ext4_fc_tail));
673aa75f4d3SHarshad Shirwadkar 	sbi->s_fc_bytes = round_up(sbi->s_fc_bytes, bsize);
674aa75f4d3SHarshad Shirwadkar 
675aa75f4d3SHarshad Shirwadkar 	ext4_fc_memcpy(sb, dst, &tl, sizeof(tl), &crc);
676aa75f4d3SHarshad Shirwadkar 	dst += sizeof(tl);
677aa75f4d3SHarshad Shirwadkar 	tail.fc_tid = cpu_to_le32(sbi->s_journal->j_running_transaction->t_tid);
678aa75f4d3SHarshad Shirwadkar 	ext4_fc_memcpy(sb, dst, &tail.fc_tid, sizeof(tail.fc_tid), &crc);
679aa75f4d3SHarshad Shirwadkar 	dst += sizeof(tail.fc_tid);
680aa75f4d3SHarshad Shirwadkar 	tail.fc_crc = cpu_to_le32(crc);
681aa75f4d3SHarshad Shirwadkar 	ext4_fc_memcpy(sb, dst, &tail.fc_crc, sizeof(tail.fc_crc), NULL);
682aa75f4d3SHarshad Shirwadkar 
683aa75f4d3SHarshad Shirwadkar 	ext4_fc_submit_bh(sb);
684aa75f4d3SHarshad Shirwadkar 
685aa75f4d3SHarshad Shirwadkar 	return 0;
686aa75f4d3SHarshad Shirwadkar }
687aa75f4d3SHarshad Shirwadkar 
688aa75f4d3SHarshad Shirwadkar /*
689aa75f4d3SHarshad Shirwadkar  * Adds tag, length, value and updates CRC. Returns true if tlv was added.
690aa75f4d3SHarshad Shirwadkar  * Returns false if there's not enough space.
691aa75f4d3SHarshad Shirwadkar  */
692aa75f4d3SHarshad Shirwadkar static bool ext4_fc_add_tlv(struct super_block *sb, u16 tag, u16 len, u8 *val,
693aa75f4d3SHarshad Shirwadkar 			   u32 *crc)
694aa75f4d3SHarshad Shirwadkar {
695aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_tl tl;
696aa75f4d3SHarshad Shirwadkar 	u8 *dst;
697aa75f4d3SHarshad Shirwadkar 
698aa75f4d3SHarshad Shirwadkar 	dst = ext4_fc_reserve_space(sb, sizeof(tl) + len, crc);
699aa75f4d3SHarshad Shirwadkar 	if (!dst)
700aa75f4d3SHarshad Shirwadkar 		return false;
701aa75f4d3SHarshad Shirwadkar 
702aa75f4d3SHarshad Shirwadkar 	tl.fc_tag = cpu_to_le16(tag);
703aa75f4d3SHarshad Shirwadkar 	tl.fc_len = cpu_to_le16(len);
704aa75f4d3SHarshad Shirwadkar 
705aa75f4d3SHarshad Shirwadkar 	ext4_fc_memcpy(sb, dst, &tl, sizeof(tl), crc);
706aa75f4d3SHarshad Shirwadkar 	ext4_fc_memcpy(sb, dst + sizeof(tl), val, len, crc);
707aa75f4d3SHarshad Shirwadkar 
708aa75f4d3SHarshad Shirwadkar 	return true;
709aa75f4d3SHarshad Shirwadkar }
710aa75f4d3SHarshad Shirwadkar 
711aa75f4d3SHarshad Shirwadkar /* Same as above, but adds dentry tlv. */
712aa75f4d3SHarshad Shirwadkar static  bool ext4_fc_add_dentry_tlv(struct super_block *sb, u16 tag,
713aa75f4d3SHarshad Shirwadkar 					int parent_ino, int ino, int dlen,
714aa75f4d3SHarshad Shirwadkar 					const unsigned char *dname,
715aa75f4d3SHarshad Shirwadkar 					u32 *crc)
716aa75f4d3SHarshad Shirwadkar {
717aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_dentry_info fcd;
718aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_tl tl;
719aa75f4d3SHarshad Shirwadkar 	u8 *dst = ext4_fc_reserve_space(sb, sizeof(tl) + sizeof(fcd) + dlen,
720aa75f4d3SHarshad Shirwadkar 					crc);
721aa75f4d3SHarshad Shirwadkar 
722aa75f4d3SHarshad Shirwadkar 	if (!dst)
723aa75f4d3SHarshad Shirwadkar 		return false;
724aa75f4d3SHarshad Shirwadkar 
725aa75f4d3SHarshad Shirwadkar 	fcd.fc_parent_ino = cpu_to_le32(parent_ino);
726aa75f4d3SHarshad Shirwadkar 	fcd.fc_ino = cpu_to_le32(ino);
727aa75f4d3SHarshad Shirwadkar 	tl.fc_tag = cpu_to_le16(tag);
728aa75f4d3SHarshad Shirwadkar 	tl.fc_len = cpu_to_le16(sizeof(fcd) + dlen);
729aa75f4d3SHarshad Shirwadkar 	ext4_fc_memcpy(sb, dst, &tl, sizeof(tl), crc);
730aa75f4d3SHarshad Shirwadkar 	dst += sizeof(tl);
731aa75f4d3SHarshad Shirwadkar 	ext4_fc_memcpy(sb, dst, &fcd, sizeof(fcd), crc);
732aa75f4d3SHarshad Shirwadkar 	dst += sizeof(fcd);
733aa75f4d3SHarshad Shirwadkar 	ext4_fc_memcpy(sb, dst, dname, dlen, crc);
734aa75f4d3SHarshad Shirwadkar 	dst += dlen;
735aa75f4d3SHarshad Shirwadkar 
736aa75f4d3SHarshad Shirwadkar 	return true;
737aa75f4d3SHarshad Shirwadkar }
738aa75f4d3SHarshad Shirwadkar 
739aa75f4d3SHarshad Shirwadkar /*
740aa75f4d3SHarshad Shirwadkar  * Writes inode in the fast commit space under TLV with tag @tag.
741aa75f4d3SHarshad Shirwadkar  * Returns 0 on success, error on failure.
742aa75f4d3SHarshad Shirwadkar  */
743aa75f4d3SHarshad Shirwadkar static int ext4_fc_write_inode(struct inode *inode, u32 *crc)
744aa75f4d3SHarshad Shirwadkar {
745aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *ei = EXT4_I(inode);
746aa75f4d3SHarshad Shirwadkar 	int inode_len = EXT4_GOOD_OLD_INODE_SIZE;
747aa75f4d3SHarshad Shirwadkar 	int ret;
748aa75f4d3SHarshad Shirwadkar 	struct ext4_iloc iloc;
749aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_inode fc_inode;
750aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_tl tl;
751aa75f4d3SHarshad Shirwadkar 	u8 *dst;
752aa75f4d3SHarshad Shirwadkar 
753aa75f4d3SHarshad Shirwadkar 	ret = ext4_get_inode_loc(inode, &iloc);
754aa75f4d3SHarshad Shirwadkar 	if (ret)
755aa75f4d3SHarshad Shirwadkar 		return ret;
756aa75f4d3SHarshad Shirwadkar 
757aa75f4d3SHarshad Shirwadkar 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE)
758aa75f4d3SHarshad Shirwadkar 		inode_len += ei->i_extra_isize;
759aa75f4d3SHarshad Shirwadkar 
760aa75f4d3SHarshad Shirwadkar 	fc_inode.fc_ino = cpu_to_le32(inode->i_ino);
761aa75f4d3SHarshad Shirwadkar 	tl.fc_tag = cpu_to_le16(EXT4_FC_TAG_INODE);
762aa75f4d3SHarshad Shirwadkar 	tl.fc_len = cpu_to_le16(inode_len + sizeof(fc_inode.fc_ino));
763aa75f4d3SHarshad Shirwadkar 
764aa75f4d3SHarshad Shirwadkar 	dst = ext4_fc_reserve_space(inode->i_sb,
765aa75f4d3SHarshad Shirwadkar 			sizeof(tl) + inode_len + sizeof(fc_inode.fc_ino), crc);
766aa75f4d3SHarshad Shirwadkar 	if (!dst)
767aa75f4d3SHarshad Shirwadkar 		return -ECANCELED;
768aa75f4d3SHarshad Shirwadkar 
769aa75f4d3SHarshad Shirwadkar 	if (!ext4_fc_memcpy(inode->i_sb, dst, &tl, sizeof(tl), crc))
770aa75f4d3SHarshad Shirwadkar 		return -ECANCELED;
771aa75f4d3SHarshad Shirwadkar 	dst += sizeof(tl);
772aa75f4d3SHarshad Shirwadkar 	if (!ext4_fc_memcpy(inode->i_sb, dst, &fc_inode, sizeof(fc_inode), crc))
773aa75f4d3SHarshad Shirwadkar 		return -ECANCELED;
774aa75f4d3SHarshad Shirwadkar 	dst += sizeof(fc_inode);
775aa75f4d3SHarshad Shirwadkar 	if (!ext4_fc_memcpy(inode->i_sb, dst, (u8 *)ext4_raw_inode(&iloc),
776aa75f4d3SHarshad Shirwadkar 					inode_len, crc))
777aa75f4d3SHarshad Shirwadkar 		return -ECANCELED;
778aa75f4d3SHarshad Shirwadkar 
779aa75f4d3SHarshad Shirwadkar 	return 0;
780aa75f4d3SHarshad Shirwadkar }
781aa75f4d3SHarshad Shirwadkar 
782aa75f4d3SHarshad Shirwadkar /*
783aa75f4d3SHarshad Shirwadkar  * Writes updated data ranges for the inode in question. Updates CRC.
784aa75f4d3SHarshad Shirwadkar  * Returns 0 on success, error otherwise.
785aa75f4d3SHarshad Shirwadkar  */
786aa75f4d3SHarshad Shirwadkar static int ext4_fc_write_inode_data(struct inode *inode, u32 *crc)
787aa75f4d3SHarshad Shirwadkar {
788aa75f4d3SHarshad Shirwadkar 	ext4_lblk_t old_blk_size, cur_lblk_off, new_blk_size;
789aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *ei = EXT4_I(inode);
790aa75f4d3SHarshad Shirwadkar 	struct ext4_map_blocks map;
791aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_add_range fc_ext;
792aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_del_range lrange;
793aa75f4d3SHarshad Shirwadkar 	struct ext4_extent *ex;
794aa75f4d3SHarshad Shirwadkar 	int ret;
795aa75f4d3SHarshad Shirwadkar 
796aa75f4d3SHarshad Shirwadkar 	mutex_lock(&ei->i_fc_lock);
797aa75f4d3SHarshad Shirwadkar 	if (ei->i_fc_lblk_len == 0) {
798aa75f4d3SHarshad Shirwadkar 		mutex_unlock(&ei->i_fc_lock);
799aa75f4d3SHarshad Shirwadkar 		return 0;
800aa75f4d3SHarshad Shirwadkar 	}
801aa75f4d3SHarshad Shirwadkar 	old_blk_size = ei->i_fc_lblk_start;
802aa75f4d3SHarshad Shirwadkar 	new_blk_size = ei->i_fc_lblk_start + ei->i_fc_lblk_len - 1;
803aa75f4d3SHarshad Shirwadkar 	ei->i_fc_lblk_len = 0;
804aa75f4d3SHarshad Shirwadkar 	mutex_unlock(&ei->i_fc_lock);
805aa75f4d3SHarshad Shirwadkar 
806aa75f4d3SHarshad Shirwadkar 	cur_lblk_off = old_blk_size;
807aa75f4d3SHarshad Shirwadkar 	jbd_debug(1, "%s: will try writing %d to %d for inode %ld\n",
808aa75f4d3SHarshad Shirwadkar 		  __func__, cur_lblk_off, new_blk_size, inode->i_ino);
809aa75f4d3SHarshad Shirwadkar 
810aa75f4d3SHarshad Shirwadkar 	while (cur_lblk_off <= new_blk_size) {
811aa75f4d3SHarshad Shirwadkar 		map.m_lblk = cur_lblk_off;
812aa75f4d3SHarshad Shirwadkar 		map.m_len = new_blk_size - cur_lblk_off + 1;
813aa75f4d3SHarshad Shirwadkar 		ret = ext4_map_blocks(NULL, inode, &map, 0);
814aa75f4d3SHarshad Shirwadkar 		if (ret < 0)
815aa75f4d3SHarshad Shirwadkar 			return -ECANCELED;
816aa75f4d3SHarshad Shirwadkar 
817aa75f4d3SHarshad Shirwadkar 		if (map.m_len == 0) {
818aa75f4d3SHarshad Shirwadkar 			cur_lblk_off++;
819aa75f4d3SHarshad Shirwadkar 			continue;
820aa75f4d3SHarshad Shirwadkar 		}
821aa75f4d3SHarshad Shirwadkar 
822aa75f4d3SHarshad Shirwadkar 		if (ret == 0) {
823aa75f4d3SHarshad Shirwadkar 			lrange.fc_ino = cpu_to_le32(inode->i_ino);
824aa75f4d3SHarshad Shirwadkar 			lrange.fc_lblk = cpu_to_le32(map.m_lblk);
825aa75f4d3SHarshad Shirwadkar 			lrange.fc_len = cpu_to_le32(map.m_len);
826aa75f4d3SHarshad Shirwadkar 			if (!ext4_fc_add_tlv(inode->i_sb, EXT4_FC_TAG_DEL_RANGE,
827aa75f4d3SHarshad Shirwadkar 					    sizeof(lrange), (u8 *)&lrange, crc))
828aa75f4d3SHarshad Shirwadkar 				return -ENOSPC;
829aa75f4d3SHarshad Shirwadkar 		} else {
830aa75f4d3SHarshad Shirwadkar 			fc_ext.fc_ino = cpu_to_le32(inode->i_ino);
831aa75f4d3SHarshad Shirwadkar 			ex = (struct ext4_extent *)&fc_ext.fc_ex;
832aa75f4d3SHarshad Shirwadkar 			ex->ee_block = cpu_to_le32(map.m_lblk);
833aa75f4d3SHarshad Shirwadkar 			ex->ee_len = cpu_to_le16(map.m_len);
834aa75f4d3SHarshad Shirwadkar 			ext4_ext_store_pblock(ex, map.m_pblk);
835aa75f4d3SHarshad Shirwadkar 			if (map.m_flags & EXT4_MAP_UNWRITTEN)
836aa75f4d3SHarshad Shirwadkar 				ext4_ext_mark_unwritten(ex);
837aa75f4d3SHarshad Shirwadkar 			else
838aa75f4d3SHarshad Shirwadkar 				ext4_ext_mark_initialized(ex);
839aa75f4d3SHarshad Shirwadkar 			if (!ext4_fc_add_tlv(inode->i_sb, EXT4_FC_TAG_ADD_RANGE,
840aa75f4d3SHarshad Shirwadkar 					    sizeof(fc_ext), (u8 *)&fc_ext, crc))
841aa75f4d3SHarshad Shirwadkar 				return -ENOSPC;
842aa75f4d3SHarshad Shirwadkar 		}
843aa75f4d3SHarshad Shirwadkar 
844aa75f4d3SHarshad Shirwadkar 		cur_lblk_off += map.m_len;
845aa75f4d3SHarshad Shirwadkar 	}
846aa75f4d3SHarshad Shirwadkar 
847aa75f4d3SHarshad Shirwadkar 	return 0;
848aa75f4d3SHarshad Shirwadkar }
849aa75f4d3SHarshad Shirwadkar 
850aa75f4d3SHarshad Shirwadkar 
851aa75f4d3SHarshad Shirwadkar /* Submit data for all the fast commit inodes */
852aa75f4d3SHarshad Shirwadkar static int ext4_fc_submit_inode_data_all(journal_t *journal)
853aa75f4d3SHarshad Shirwadkar {
854aa75f4d3SHarshad Shirwadkar 	struct super_block *sb = (struct super_block *)(journal->j_private);
855aa75f4d3SHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(sb);
856aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *ei;
857aa75f4d3SHarshad Shirwadkar 	struct list_head *pos;
858aa75f4d3SHarshad Shirwadkar 	int ret = 0;
859aa75f4d3SHarshad Shirwadkar 
860aa75f4d3SHarshad Shirwadkar 	spin_lock(&sbi->s_fc_lock);
861ababea77SHarshad Shirwadkar 	sbi->s_mount_flags |= EXT4_MF_FC_COMMITTING;
862aa75f4d3SHarshad Shirwadkar 	list_for_each(pos, &sbi->s_fc_q[FC_Q_MAIN]) {
863aa75f4d3SHarshad Shirwadkar 		ei = list_entry(pos, struct ext4_inode_info, i_fc_list);
864aa75f4d3SHarshad Shirwadkar 		ext4_set_inode_state(&ei->vfs_inode, EXT4_STATE_FC_COMMITTING);
865aa75f4d3SHarshad Shirwadkar 		while (atomic_read(&ei->i_fc_updates)) {
866aa75f4d3SHarshad Shirwadkar 			DEFINE_WAIT(wait);
867aa75f4d3SHarshad Shirwadkar 
868aa75f4d3SHarshad Shirwadkar 			prepare_to_wait(&ei->i_fc_wait, &wait,
869aa75f4d3SHarshad Shirwadkar 						TASK_UNINTERRUPTIBLE);
870aa75f4d3SHarshad Shirwadkar 			if (atomic_read(&ei->i_fc_updates)) {
871aa75f4d3SHarshad Shirwadkar 				spin_unlock(&sbi->s_fc_lock);
872aa75f4d3SHarshad Shirwadkar 				schedule();
873aa75f4d3SHarshad Shirwadkar 				spin_lock(&sbi->s_fc_lock);
874aa75f4d3SHarshad Shirwadkar 			}
875aa75f4d3SHarshad Shirwadkar 			finish_wait(&ei->i_fc_wait, &wait);
876aa75f4d3SHarshad Shirwadkar 		}
877aa75f4d3SHarshad Shirwadkar 		spin_unlock(&sbi->s_fc_lock);
878aa75f4d3SHarshad Shirwadkar 		ret = jbd2_submit_inode_data(ei->jinode);
879aa75f4d3SHarshad Shirwadkar 		if (ret)
880aa75f4d3SHarshad Shirwadkar 			return ret;
881aa75f4d3SHarshad Shirwadkar 		spin_lock(&sbi->s_fc_lock);
882aa75f4d3SHarshad Shirwadkar 	}
883aa75f4d3SHarshad Shirwadkar 	spin_unlock(&sbi->s_fc_lock);
884aa75f4d3SHarshad Shirwadkar 
885aa75f4d3SHarshad Shirwadkar 	return ret;
886aa75f4d3SHarshad Shirwadkar }
887aa75f4d3SHarshad Shirwadkar 
888aa75f4d3SHarshad Shirwadkar /* Wait for completion of data for all the fast commit inodes */
889aa75f4d3SHarshad Shirwadkar static int ext4_fc_wait_inode_data_all(journal_t *journal)
890aa75f4d3SHarshad Shirwadkar {
891aa75f4d3SHarshad Shirwadkar 	struct super_block *sb = (struct super_block *)(journal->j_private);
892aa75f4d3SHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(sb);
893aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *pos, *n;
894aa75f4d3SHarshad Shirwadkar 	int ret = 0;
895aa75f4d3SHarshad Shirwadkar 
896aa75f4d3SHarshad Shirwadkar 	spin_lock(&sbi->s_fc_lock);
897aa75f4d3SHarshad Shirwadkar 	list_for_each_entry_safe(pos, n, &sbi->s_fc_q[FC_Q_MAIN], i_fc_list) {
898aa75f4d3SHarshad Shirwadkar 		if (!ext4_test_inode_state(&pos->vfs_inode,
899aa75f4d3SHarshad Shirwadkar 					   EXT4_STATE_FC_COMMITTING))
900aa75f4d3SHarshad Shirwadkar 			continue;
901aa75f4d3SHarshad Shirwadkar 		spin_unlock(&sbi->s_fc_lock);
902aa75f4d3SHarshad Shirwadkar 
903aa75f4d3SHarshad Shirwadkar 		ret = jbd2_wait_inode_data(journal, pos->jinode);
904aa75f4d3SHarshad Shirwadkar 		if (ret)
905aa75f4d3SHarshad Shirwadkar 			return ret;
906aa75f4d3SHarshad Shirwadkar 		spin_lock(&sbi->s_fc_lock);
907aa75f4d3SHarshad Shirwadkar 	}
908aa75f4d3SHarshad Shirwadkar 	spin_unlock(&sbi->s_fc_lock);
909aa75f4d3SHarshad Shirwadkar 
910aa75f4d3SHarshad Shirwadkar 	return 0;
911aa75f4d3SHarshad Shirwadkar }
912aa75f4d3SHarshad Shirwadkar 
913aa75f4d3SHarshad Shirwadkar /* Commit all the directory entry updates */
914aa75f4d3SHarshad Shirwadkar static int ext4_fc_commit_dentry_updates(journal_t *journal, u32 *crc)
915aa75f4d3SHarshad Shirwadkar {
916aa75f4d3SHarshad Shirwadkar 	struct super_block *sb = (struct super_block *)(journal->j_private);
917aa75f4d3SHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(sb);
918aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_dentry_update *fc_dentry;
919aa75f4d3SHarshad Shirwadkar 	struct inode *inode;
920aa75f4d3SHarshad Shirwadkar 	struct list_head *pos, *n, *fcd_pos, *fcd_n;
921aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *ei;
922aa75f4d3SHarshad Shirwadkar 	int ret;
923aa75f4d3SHarshad Shirwadkar 
924aa75f4d3SHarshad Shirwadkar 	if (list_empty(&sbi->s_fc_dentry_q[FC_Q_MAIN]))
925aa75f4d3SHarshad Shirwadkar 		return 0;
926aa75f4d3SHarshad Shirwadkar 	list_for_each_safe(fcd_pos, fcd_n, &sbi->s_fc_dentry_q[FC_Q_MAIN]) {
927aa75f4d3SHarshad Shirwadkar 		fc_dentry = list_entry(fcd_pos, struct ext4_fc_dentry_update,
928aa75f4d3SHarshad Shirwadkar 					fcd_list);
929aa75f4d3SHarshad Shirwadkar 		if (fc_dentry->fcd_op != EXT4_FC_TAG_CREAT) {
930aa75f4d3SHarshad Shirwadkar 			spin_unlock(&sbi->s_fc_lock);
931aa75f4d3SHarshad Shirwadkar 			if (!ext4_fc_add_dentry_tlv(
932aa75f4d3SHarshad Shirwadkar 				sb, fc_dentry->fcd_op,
933aa75f4d3SHarshad Shirwadkar 				fc_dentry->fcd_parent, fc_dentry->fcd_ino,
934aa75f4d3SHarshad Shirwadkar 				fc_dentry->fcd_name.len,
935aa75f4d3SHarshad Shirwadkar 				fc_dentry->fcd_name.name, crc)) {
936aa75f4d3SHarshad Shirwadkar 				ret = -ENOSPC;
937aa75f4d3SHarshad Shirwadkar 				goto lock_and_exit;
938aa75f4d3SHarshad Shirwadkar 			}
939aa75f4d3SHarshad Shirwadkar 			spin_lock(&sbi->s_fc_lock);
940aa75f4d3SHarshad Shirwadkar 			continue;
941aa75f4d3SHarshad Shirwadkar 		}
942aa75f4d3SHarshad Shirwadkar 
943aa75f4d3SHarshad Shirwadkar 		inode = NULL;
944aa75f4d3SHarshad Shirwadkar 		list_for_each_safe(pos, n, &sbi->s_fc_q[FC_Q_MAIN]) {
945aa75f4d3SHarshad Shirwadkar 			ei = list_entry(pos, struct ext4_inode_info, i_fc_list);
946aa75f4d3SHarshad Shirwadkar 			if (ei->vfs_inode.i_ino == fc_dentry->fcd_ino) {
947aa75f4d3SHarshad Shirwadkar 				inode = &ei->vfs_inode;
948aa75f4d3SHarshad Shirwadkar 				break;
949aa75f4d3SHarshad Shirwadkar 			}
950aa75f4d3SHarshad Shirwadkar 		}
951aa75f4d3SHarshad Shirwadkar 		/*
952aa75f4d3SHarshad Shirwadkar 		 * If we don't find inode in our list, then it was deleted,
953aa75f4d3SHarshad Shirwadkar 		 * in which case, we don't need to record it's create tag.
954aa75f4d3SHarshad Shirwadkar 		 */
955aa75f4d3SHarshad Shirwadkar 		if (!inode)
956aa75f4d3SHarshad Shirwadkar 			continue;
957aa75f4d3SHarshad Shirwadkar 		spin_unlock(&sbi->s_fc_lock);
958aa75f4d3SHarshad Shirwadkar 
959aa75f4d3SHarshad Shirwadkar 		/*
960aa75f4d3SHarshad Shirwadkar 		 * We first write the inode and then the create dirent. This
961aa75f4d3SHarshad Shirwadkar 		 * allows the recovery code to create an unnamed inode first
962aa75f4d3SHarshad Shirwadkar 		 * and then link it to a directory entry. This allows us
963aa75f4d3SHarshad Shirwadkar 		 * to use namei.c routines almost as is and simplifies
964aa75f4d3SHarshad Shirwadkar 		 * the recovery code.
965aa75f4d3SHarshad Shirwadkar 		 */
966aa75f4d3SHarshad Shirwadkar 		ret = ext4_fc_write_inode(inode, crc);
967aa75f4d3SHarshad Shirwadkar 		if (ret)
968aa75f4d3SHarshad Shirwadkar 			goto lock_and_exit;
969aa75f4d3SHarshad Shirwadkar 
970aa75f4d3SHarshad Shirwadkar 		ret = ext4_fc_write_inode_data(inode, crc);
971aa75f4d3SHarshad Shirwadkar 		if (ret)
972aa75f4d3SHarshad Shirwadkar 			goto lock_and_exit;
973aa75f4d3SHarshad Shirwadkar 
974aa75f4d3SHarshad Shirwadkar 		if (!ext4_fc_add_dentry_tlv(
975aa75f4d3SHarshad Shirwadkar 			sb, fc_dentry->fcd_op,
976aa75f4d3SHarshad Shirwadkar 			fc_dentry->fcd_parent, fc_dentry->fcd_ino,
977aa75f4d3SHarshad Shirwadkar 			fc_dentry->fcd_name.len,
978aa75f4d3SHarshad Shirwadkar 			fc_dentry->fcd_name.name, crc)) {
979aa75f4d3SHarshad Shirwadkar 			ret = -ENOSPC;
980aa75f4d3SHarshad Shirwadkar 			goto lock_and_exit;
981aa75f4d3SHarshad Shirwadkar 		}
982aa75f4d3SHarshad Shirwadkar 
983aa75f4d3SHarshad Shirwadkar 		spin_lock(&sbi->s_fc_lock);
984aa75f4d3SHarshad Shirwadkar 	}
985aa75f4d3SHarshad Shirwadkar 	return 0;
986aa75f4d3SHarshad Shirwadkar lock_and_exit:
987aa75f4d3SHarshad Shirwadkar 	spin_lock(&sbi->s_fc_lock);
988aa75f4d3SHarshad Shirwadkar 	return ret;
989aa75f4d3SHarshad Shirwadkar }
990aa75f4d3SHarshad Shirwadkar 
991aa75f4d3SHarshad Shirwadkar static int ext4_fc_perform_commit(journal_t *journal)
992aa75f4d3SHarshad Shirwadkar {
993aa75f4d3SHarshad Shirwadkar 	struct super_block *sb = (struct super_block *)(journal->j_private);
994aa75f4d3SHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(sb);
995aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *iter;
996aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_head head;
997aa75f4d3SHarshad Shirwadkar 	struct list_head *pos;
998aa75f4d3SHarshad Shirwadkar 	struct inode *inode;
999aa75f4d3SHarshad Shirwadkar 	struct blk_plug plug;
1000aa75f4d3SHarshad Shirwadkar 	int ret = 0;
1001aa75f4d3SHarshad Shirwadkar 	u32 crc = 0;
1002aa75f4d3SHarshad Shirwadkar 
1003aa75f4d3SHarshad Shirwadkar 	ret = ext4_fc_submit_inode_data_all(journal);
1004aa75f4d3SHarshad Shirwadkar 	if (ret)
1005aa75f4d3SHarshad Shirwadkar 		return ret;
1006aa75f4d3SHarshad Shirwadkar 
1007aa75f4d3SHarshad Shirwadkar 	ret = ext4_fc_wait_inode_data_all(journal);
1008aa75f4d3SHarshad Shirwadkar 	if (ret)
1009aa75f4d3SHarshad Shirwadkar 		return ret;
1010aa75f4d3SHarshad Shirwadkar 
1011aa75f4d3SHarshad Shirwadkar 	blk_start_plug(&plug);
1012aa75f4d3SHarshad Shirwadkar 	if (sbi->s_fc_bytes == 0) {
1013aa75f4d3SHarshad Shirwadkar 		/*
1014aa75f4d3SHarshad Shirwadkar 		 * Add a head tag only if this is the first fast commit
1015aa75f4d3SHarshad Shirwadkar 		 * in this TID.
1016aa75f4d3SHarshad Shirwadkar 		 */
1017aa75f4d3SHarshad Shirwadkar 		head.fc_features = cpu_to_le32(EXT4_FC_SUPPORTED_FEATURES);
1018aa75f4d3SHarshad Shirwadkar 		head.fc_tid = cpu_to_le32(
1019aa75f4d3SHarshad Shirwadkar 			sbi->s_journal->j_running_transaction->t_tid);
1020aa75f4d3SHarshad Shirwadkar 		if (!ext4_fc_add_tlv(sb, EXT4_FC_TAG_HEAD, sizeof(head),
1021aa75f4d3SHarshad Shirwadkar 			(u8 *)&head, &crc))
1022aa75f4d3SHarshad Shirwadkar 			goto out;
1023aa75f4d3SHarshad Shirwadkar 	}
1024aa75f4d3SHarshad Shirwadkar 
1025aa75f4d3SHarshad Shirwadkar 	spin_lock(&sbi->s_fc_lock);
1026aa75f4d3SHarshad Shirwadkar 	ret = ext4_fc_commit_dentry_updates(journal, &crc);
1027aa75f4d3SHarshad Shirwadkar 	if (ret) {
1028aa75f4d3SHarshad Shirwadkar 		spin_unlock(&sbi->s_fc_lock);
1029aa75f4d3SHarshad Shirwadkar 		goto out;
1030aa75f4d3SHarshad Shirwadkar 	}
1031aa75f4d3SHarshad Shirwadkar 
1032aa75f4d3SHarshad Shirwadkar 	list_for_each(pos, &sbi->s_fc_q[FC_Q_MAIN]) {
1033aa75f4d3SHarshad Shirwadkar 		iter = list_entry(pos, struct ext4_inode_info, i_fc_list);
1034aa75f4d3SHarshad Shirwadkar 		inode = &iter->vfs_inode;
1035aa75f4d3SHarshad Shirwadkar 		if (!ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING))
1036aa75f4d3SHarshad Shirwadkar 			continue;
1037aa75f4d3SHarshad Shirwadkar 
1038aa75f4d3SHarshad Shirwadkar 		spin_unlock(&sbi->s_fc_lock);
1039aa75f4d3SHarshad Shirwadkar 		ret = ext4_fc_write_inode_data(inode, &crc);
1040aa75f4d3SHarshad Shirwadkar 		if (ret)
1041aa75f4d3SHarshad Shirwadkar 			goto out;
1042aa75f4d3SHarshad Shirwadkar 		ret = ext4_fc_write_inode(inode, &crc);
1043aa75f4d3SHarshad Shirwadkar 		if (ret)
1044aa75f4d3SHarshad Shirwadkar 			goto out;
1045aa75f4d3SHarshad Shirwadkar 		spin_lock(&sbi->s_fc_lock);
1046aa75f4d3SHarshad Shirwadkar 		EXT4_I(inode)->i_fc_committed_subtid =
1047aa75f4d3SHarshad Shirwadkar 			atomic_read(&sbi->s_fc_subtid);
1048aa75f4d3SHarshad Shirwadkar 	}
1049aa75f4d3SHarshad Shirwadkar 	spin_unlock(&sbi->s_fc_lock);
1050aa75f4d3SHarshad Shirwadkar 
1051aa75f4d3SHarshad Shirwadkar 	ret = ext4_fc_write_tail(sb, crc);
1052aa75f4d3SHarshad Shirwadkar 
1053aa75f4d3SHarshad Shirwadkar out:
1054aa75f4d3SHarshad Shirwadkar 	blk_finish_plug(&plug);
1055aa75f4d3SHarshad Shirwadkar 	return ret;
1056aa75f4d3SHarshad Shirwadkar }
1057aa75f4d3SHarshad Shirwadkar 
1058aa75f4d3SHarshad Shirwadkar /*
1059aa75f4d3SHarshad Shirwadkar  * The main commit entry point. Performs a fast commit for transaction
1060aa75f4d3SHarshad Shirwadkar  * commit_tid if needed. If it's not possible to perform a fast commit
1061aa75f4d3SHarshad Shirwadkar  * due to various reasons, we fall back to full commit. Returns 0
1062aa75f4d3SHarshad Shirwadkar  * on success, error otherwise.
1063aa75f4d3SHarshad Shirwadkar  */
1064aa75f4d3SHarshad Shirwadkar int ext4_fc_commit(journal_t *journal, tid_t commit_tid)
1065aa75f4d3SHarshad Shirwadkar {
1066aa75f4d3SHarshad Shirwadkar 	struct super_block *sb = (struct super_block *)(journal->j_private);
1067aa75f4d3SHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1068aa75f4d3SHarshad Shirwadkar 	int nblks = 0, ret, bsize = journal->j_blocksize;
1069aa75f4d3SHarshad Shirwadkar 	int subtid = atomic_read(&sbi->s_fc_subtid);
1070aa75f4d3SHarshad Shirwadkar 	int reason = EXT4_FC_REASON_OK, fc_bufs_before = 0;
1071aa75f4d3SHarshad Shirwadkar 	ktime_t start_time, commit_time;
1072aa75f4d3SHarshad Shirwadkar 
1073aa75f4d3SHarshad Shirwadkar 	trace_ext4_fc_commit_start(sb);
1074aa75f4d3SHarshad Shirwadkar 
1075aa75f4d3SHarshad Shirwadkar 	start_time = ktime_get();
1076aa75f4d3SHarshad Shirwadkar 
1077aa75f4d3SHarshad Shirwadkar 	if (!test_opt2(sb, JOURNAL_FAST_COMMIT) ||
1078aa75f4d3SHarshad Shirwadkar 		(ext4_fc_is_ineligible(sb))) {
1079aa75f4d3SHarshad Shirwadkar 		reason = EXT4_FC_REASON_INELIGIBLE;
1080aa75f4d3SHarshad Shirwadkar 		goto out;
1081aa75f4d3SHarshad Shirwadkar 	}
1082aa75f4d3SHarshad Shirwadkar 
1083aa75f4d3SHarshad Shirwadkar restart_fc:
1084aa75f4d3SHarshad Shirwadkar 	ret = jbd2_fc_begin_commit(journal, commit_tid);
1085aa75f4d3SHarshad Shirwadkar 	if (ret == -EALREADY) {
1086aa75f4d3SHarshad Shirwadkar 		/* There was an ongoing commit, check if we need to restart */
1087aa75f4d3SHarshad Shirwadkar 		if (atomic_read(&sbi->s_fc_subtid) <= subtid &&
1088aa75f4d3SHarshad Shirwadkar 			commit_tid > journal->j_commit_sequence)
1089aa75f4d3SHarshad Shirwadkar 			goto restart_fc;
1090aa75f4d3SHarshad Shirwadkar 		reason = EXT4_FC_REASON_ALREADY_COMMITTED;
1091aa75f4d3SHarshad Shirwadkar 		goto out;
1092aa75f4d3SHarshad Shirwadkar 	} else if (ret) {
1093aa75f4d3SHarshad Shirwadkar 		sbi->s_fc_stats.fc_ineligible_reason_count[EXT4_FC_COMMIT_FAILED]++;
1094aa75f4d3SHarshad Shirwadkar 		reason = EXT4_FC_REASON_FC_START_FAILED;
1095aa75f4d3SHarshad Shirwadkar 		goto out;
1096aa75f4d3SHarshad Shirwadkar 	}
1097aa75f4d3SHarshad Shirwadkar 
1098aa75f4d3SHarshad Shirwadkar 	fc_bufs_before = (sbi->s_fc_bytes + bsize - 1) / bsize;
1099aa75f4d3SHarshad Shirwadkar 	ret = ext4_fc_perform_commit(journal);
1100aa75f4d3SHarshad Shirwadkar 	if (ret < 0) {
1101aa75f4d3SHarshad Shirwadkar 		sbi->s_fc_stats.fc_ineligible_reason_count[EXT4_FC_COMMIT_FAILED]++;
1102aa75f4d3SHarshad Shirwadkar 		reason = EXT4_FC_REASON_FC_FAILED;
1103aa75f4d3SHarshad Shirwadkar 		goto out;
1104aa75f4d3SHarshad Shirwadkar 	}
1105aa75f4d3SHarshad Shirwadkar 	nblks = (sbi->s_fc_bytes + bsize - 1) / bsize - fc_bufs_before;
1106aa75f4d3SHarshad Shirwadkar 	ret = jbd2_fc_wait_bufs(journal, nblks);
1107aa75f4d3SHarshad Shirwadkar 	if (ret < 0) {
1108aa75f4d3SHarshad Shirwadkar 		sbi->s_fc_stats.fc_ineligible_reason_count[EXT4_FC_COMMIT_FAILED]++;
1109aa75f4d3SHarshad Shirwadkar 		reason = EXT4_FC_REASON_FC_FAILED;
1110aa75f4d3SHarshad Shirwadkar 		goto out;
1111aa75f4d3SHarshad Shirwadkar 	}
1112aa75f4d3SHarshad Shirwadkar 	atomic_inc(&sbi->s_fc_subtid);
1113aa75f4d3SHarshad Shirwadkar 	jbd2_fc_end_commit(journal);
1114aa75f4d3SHarshad Shirwadkar out:
1115aa75f4d3SHarshad Shirwadkar 	/* Has any ineligible update happened since we started? */
1116aa75f4d3SHarshad Shirwadkar 	if (reason == EXT4_FC_REASON_OK && ext4_fc_is_ineligible(sb)) {
1117aa75f4d3SHarshad Shirwadkar 		sbi->s_fc_stats.fc_ineligible_reason_count[EXT4_FC_COMMIT_FAILED]++;
1118aa75f4d3SHarshad Shirwadkar 		reason = EXT4_FC_REASON_INELIGIBLE;
1119aa75f4d3SHarshad Shirwadkar 	}
1120aa75f4d3SHarshad Shirwadkar 
1121aa75f4d3SHarshad Shirwadkar 	spin_lock(&sbi->s_fc_lock);
1122aa75f4d3SHarshad Shirwadkar 	if (reason != EXT4_FC_REASON_OK &&
1123aa75f4d3SHarshad Shirwadkar 		reason != EXT4_FC_REASON_ALREADY_COMMITTED) {
1124aa75f4d3SHarshad Shirwadkar 		sbi->s_fc_stats.fc_ineligible_commits++;
1125aa75f4d3SHarshad Shirwadkar 	} else {
1126aa75f4d3SHarshad Shirwadkar 		sbi->s_fc_stats.fc_num_commits++;
1127aa75f4d3SHarshad Shirwadkar 		sbi->s_fc_stats.fc_numblks += nblks;
1128aa75f4d3SHarshad Shirwadkar 	}
1129aa75f4d3SHarshad Shirwadkar 	spin_unlock(&sbi->s_fc_lock);
1130aa75f4d3SHarshad Shirwadkar 	nblks = (reason == EXT4_FC_REASON_OK) ? nblks : 0;
1131aa75f4d3SHarshad Shirwadkar 	trace_ext4_fc_commit_stop(sb, nblks, reason);
1132aa75f4d3SHarshad Shirwadkar 	commit_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1133aa75f4d3SHarshad Shirwadkar 	/*
1134aa75f4d3SHarshad Shirwadkar 	 * weight the commit time higher than the average time so we don't
1135aa75f4d3SHarshad Shirwadkar 	 * react too strongly to vast changes in the commit time
1136aa75f4d3SHarshad Shirwadkar 	 */
1137aa75f4d3SHarshad Shirwadkar 	if (likely(sbi->s_fc_avg_commit_time))
1138aa75f4d3SHarshad Shirwadkar 		sbi->s_fc_avg_commit_time = (commit_time +
1139aa75f4d3SHarshad Shirwadkar 				sbi->s_fc_avg_commit_time * 3) / 4;
1140aa75f4d3SHarshad Shirwadkar 	else
1141aa75f4d3SHarshad Shirwadkar 		sbi->s_fc_avg_commit_time = commit_time;
1142aa75f4d3SHarshad Shirwadkar 	jbd_debug(1,
1143aa75f4d3SHarshad Shirwadkar 		"Fast commit ended with blks = %d, reason = %d, subtid - %d",
1144aa75f4d3SHarshad Shirwadkar 		nblks, reason, subtid);
1145aa75f4d3SHarshad Shirwadkar 	if (reason == EXT4_FC_REASON_FC_FAILED)
1146*0bce577bSHarshad Shirwadkar 		return jbd2_fc_end_commit_fallback(journal);
1147aa75f4d3SHarshad Shirwadkar 	if (reason == EXT4_FC_REASON_FC_START_FAILED ||
1148aa75f4d3SHarshad Shirwadkar 		reason == EXT4_FC_REASON_INELIGIBLE)
1149aa75f4d3SHarshad Shirwadkar 		return jbd2_complete_transaction(journal, commit_tid);
1150aa75f4d3SHarshad Shirwadkar 	return 0;
1151aa75f4d3SHarshad Shirwadkar }
1152aa75f4d3SHarshad Shirwadkar 
1153ff780b91SHarshad Shirwadkar /*
1154ff780b91SHarshad Shirwadkar  * Fast commit cleanup routine. This is called after every fast commit and
1155ff780b91SHarshad Shirwadkar  * full commit. full is true if we are called after a full commit.
1156ff780b91SHarshad Shirwadkar  */
1157ff780b91SHarshad Shirwadkar static void ext4_fc_cleanup(journal_t *journal, int full)
1158ff780b91SHarshad Shirwadkar {
1159aa75f4d3SHarshad Shirwadkar 	struct super_block *sb = journal->j_private;
1160aa75f4d3SHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1161aa75f4d3SHarshad Shirwadkar 	struct ext4_inode_info *iter;
1162aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_dentry_update *fc_dentry;
1163aa75f4d3SHarshad Shirwadkar 	struct list_head *pos, *n;
1164aa75f4d3SHarshad Shirwadkar 
1165aa75f4d3SHarshad Shirwadkar 	if (full && sbi->s_fc_bh)
1166aa75f4d3SHarshad Shirwadkar 		sbi->s_fc_bh = NULL;
1167aa75f4d3SHarshad Shirwadkar 
1168aa75f4d3SHarshad Shirwadkar 	jbd2_fc_release_bufs(journal);
1169aa75f4d3SHarshad Shirwadkar 
1170aa75f4d3SHarshad Shirwadkar 	spin_lock(&sbi->s_fc_lock);
1171aa75f4d3SHarshad Shirwadkar 	list_for_each_safe(pos, n, &sbi->s_fc_q[FC_Q_MAIN]) {
1172aa75f4d3SHarshad Shirwadkar 		iter = list_entry(pos, struct ext4_inode_info, i_fc_list);
1173aa75f4d3SHarshad Shirwadkar 		list_del_init(&iter->i_fc_list);
1174aa75f4d3SHarshad Shirwadkar 		ext4_clear_inode_state(&iter->vfs_inode,
1175aa75f4d3SHarshad Shirwadkar 				       EXT4_STATE_FC_COMMITTING);
1176aa75f4d3SHarshad Shirwadkar 		ext4_fc_reset_inode(&iter->vfs_inode);
1177aa75f4d3SHarshad Shirwadkar 		/* Make sure EXT4_STATE_FC_COMMITTING bit is clear */
1178aa75f4d3SHarshad Shirwadkar 		smp_mb();
1179aa75f4d3SHarshad Shirwadkar #if (BITS_PER_LONG < 64)
1180aa75f4d3SHarshad Shirwadkar 		wake_up_bit(&iter->i_state_flags, EXT4_STATE_FC_COMMITTING);
1181aa75f4d3SHarshad Shirwadkar #else
1182aa75f4d3SHarshad Shirwadkar 		wake_up_bit(&iter->i_flags, EXT4_STATE_FC_COMMITTING);
1183aa75f4d3SHarshad Shirwadkar #endif
1184aa75f4d3SHarshad Shirwadkar 	}
1185aa75f4d3SHarshad Shirwadkar 
1186aa75f4d3SHarshad Shirwadkar 	while (!list_empty(&sbi->s_fc_dentry_q[FC_Q_MAIN])) {
1187aa75f4d3SHarshad Shirwadkar 		fc_dentry = list_first_entry(&sbi->s_fc_dentry_q[FC_Q_MAIN],
1188aa75f4d3SHarshad Shirwadkar 					     struct ext4_fc_dentry_update,
1189aa75f4d3SHarshad Shirwadkar 					     fcd_list);
1190aa75f4d3SHarshad Shirwadkar 		list_del_init(&fc_dentry->fcd_list);
1191aa75f4d3SHarshad Shirwadkar 		spin_unlock(&sbi->s_fc_lock);
1192aa75f4d3SHarshad Shirwadkar 
1193aa75f4d3SHarshad Shirwadkar 		if (fc_dentry->fcd_name.name &&
1194aa75f4d3SHarshad Shirwadkar 			fc_dentry->fcd_name.len > DNAME_INLINE_LEN)
1195aa75f4d3SHarshad Shirwadkar 			kfree(fc_dentry->fcd_name.name);
1196aa75f4d3SHarshad Shirwadkar 		kmem_cache_free(ext4_fc_dentry_cachep, fc_dentry);
1197aa75f4d3SHarshad Shirwadkar 		spin_lock(&sbi->s_fc_lock);
1198aa75f4d3SHarshad Shirwadkar 	}
1199aa75f4d3SHarshad Shirwadkar 
1200aa75f4d3SHarshad Shirwadkar 	list_splice_init(&sbi->s_fc_dentry_q[FC_Q_STAGING],
1201aa75f4d3SHarshad Shirwadkar 				&sbi->s_fc_dentry_q[FC_Q_MAIN]);
1202aa75f4d3SHarshad Shirwadkar 	list_splice_init(&sbi->s_fc_q[FC_Q_STAGING],
1203aa75f4d3SHarshad Shirwadkar 				&sbi->s_fc_q[FC_Q_STAGING]);
1204aa75f4d3SHarshad Shirwadkar 
1205ababea77SHarshad Shirwadkar 	sbi->s_mount_flags &= ~EXT4_MF_FC_COMMITTING;
1206ababea77SHarshad Shirwadkar 	sbi->s_mount_flags &= ~EXT4_MF_FC_INELIGIBLE;
1207aa75f4d3SHarshad Shirwadkar 
1208aa75f4d3SHarshad Shirwadkar 	if (full)
1209aa75f4d3SHarshad Shirwadkar 		sbi->s_fc_bytes = 0;
1210aa75f4d3SHarshad Shirwadkar 	spin_unlock(&sbi->s_fc_lock);
1211aa75f4d3SHarshad Shirwadkar 	trace_ext4_fc_stats(sb);
1212ff780b91SHarshad Shirwadkar }
12136866d7b3SHarshad Shirwadkar 
12148016e29fSHarshad Shirwadkar /* Ext4 Replay Path Routines */
12158016e29fSHarshad Shirwadkar 
12168016e29fSHarshad Shirwadkar /* Get length of a particular tlv */
12178016e29fSHarshad Shirwadkar static inline int ext4_fc_tag_len(struct ext4_fc_tl *tl)
12188016e29fSHarshad Shirwadkar {
12198016e29fSHarshad Shirwadkar 	return le16_to_cpu(tl->fc_len);
12208016e29fSHarshad Shirwadkar }
12218016e29fSHarshad Shirwadkar 
12228016e29fSHarshad Shirwadkar /* Get a pointer to "value" of a tlv */
12238016e29fSHarshad Shirwadkar static inline u8 *ext4_fc_tag_val(struct ext4_fc_tl *tl)
12248016e29fSHarshad Shirwadkar {
12258016e29fSHarshad Shirwadkar 	return (u8 *)tl + sizeof(*tl);
12268016e29fSHarshad Shirwadkar }
12278016e29fSHarshad Shirwadkar 
12288016e29fSHarshad Shirwadkar /* Helper struct for dentry replay routines */
12298016e29fSHarshad Shirwadkar struct dentry_info_args {
12308016e29fSHarshad Shirwadkar 	int parent_ino, dname_len, ino, inode_len;
12318016e29fSHarshad Shirwadkar 	char *dname;
12328016e29fSHarshad Shirwadkar };
12338016e29fSHarshad Shirwadkar 
12348016e29fSHarshad Shirwadkar static inline void tl_to_darg(struct dentry_info_args *darg,
12358016e29fSHarshad Shirwadkar 				struct  ext4_fc_tl *tl)
12368016e29fSHarshad Shirwadkar {
12378016e29fSHarshad Shirwadkar 	struct ext4_fc_dentry_info *fcd;
12388016e29fSHarshad Shirwadkar 
12398016e29fSHarshad Shirwadkar 	fcd = (struct ext4_fc_dentry_info *)ext4_fc_tag_val(tl);
12408016e29fSHarshad Shirwadkar 
12418016e29fSHarshad Shirwadkar 	darg->parent_ino = le32_to_cpu(fcd->fc_parent_ino);
12428016e29fSHarshad Shirwadkar 	darg->ino = le32_to_cpu(fcd->fc_ino);
12438016e29fSHarshad Shirwadkar 	darg->dname = fcd->fc_dname;
12448016e29fSHarshad Shirwadkar 	darg->dname_len = ext4_fc_tag_len(tl) -
12458016e29fSHarshad Shirwadkar 			sizeof(struct ext4_fc_dentry_info);
12468016e29fSHarshad Shirwadkar }
12478016e29fSHarshad Shirwadkar 
12488016e29fSHarshad Shirwadkar /* Unlink replay function */
12498016e29fSHarshad Shirwadkar static int ext4_fc_replay_unlink(struct super_block *sb, struct ext4_fc_tl *tl)
12508016e29fSHarshad Shirwadkar {
12518016e29fSHarshad Shirwadkar 	struct inode *inode, *old_parent;
12528016e29fSHarshad Shirwadkar 	struct qstr entry;
12538016e29fSHarshad Shirwadkar 	struct dentry_info_args darg;
12548016e29fSHarshad Shirwadkar 	int ret = 0;
12558016e29fSHarshad Shirwadkar 
12568016e29fSHarshad Shirwadkar 	tl_to_darg(&darg, tl);
12578016e29fSHarshad Shirwadkar 
12588016e29fSHarshad Shirwadkar 	trace_ext4_fc_replay(sb, EXT4_FC_TAG_UNLINK, darg.ino,
12598016e29fSHarshad Shirwadkar 			darg.parent_ino, darg.dname_len);
12608016e29fSHarshad Shirwadkar 
12618016e29fSHarshad Shirwadkar 	entry.name = darg.dname;
12628016e29fSHarshad Shirwadkar 	entry.len = darg.dname_len;
12638016e29fSHarshad Shirwadkar 	inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
12648016e29fSHarshad Shirwadkar 
12658016e29fSHarshad Shirwadkar 	if (IS_ERR_OR_NULL(inode)) {
12668016e29fSHarshad Shirwadkar 		jbd_debug(1, "Inode %d not found", darg.ino);
12678016e29fSHarshad Shirwadkar 		return 0;
12688016e29fSHarshad Shirwadkar 	}
12698016e29fSHarshad Shirwadkar 
12708016e29fSHarshad Shirwadkar 	old_parent = ext4_iget(sb, darg.parent_ino,
12718016e29fSHarshad Shirwadkar 				EXT4_IGET_NORMAL);
12728016e29fSHarshad Shirwadkar 	if (IS_ERR_OR_NULL(old_parent)) {
12738016e29fSHarshad Shirwadkar 		jbd_debug(1, "Dir with inode  %d not found", darg.parent_ino);
12748016e29fSHarshad Shirwadkar 		iput(inode);
12758016e29fSHarshad Shirwadkar 		return 0;
12768016e29fSHarshad Shirwadkar 	}
12778016e29fSHarshad Shirwadkar 
1278a80f7fcfSHarshad Shirwadkar 	ret = __ext4_unlink(NULL, old_parent, &entry, inode);
12798016e29fSHarshad Shirwadkar 	/* -ENOENT ok coz it might not exist anymore. */
12808016e29fSHarshad Shirwadkar 	if (ret == -ENOENT)
12818016e29fSHarshad Shirwadkar 		ret = 0;
12828016e29fSHarshad Shirwadkar 	iput(old_parent);
12838016e29fSHarshad Shirwadkar 	iput(inode);
12848016e29fSHarshad Shirwadkar 	return ret;
12858016e29fSHarshad Shirwadkar }
12868016e29fSHarshad Shirwadkar 
12878016e29fSHarshad Shirwadkar static int ext4_fc_replay_link_internal(struct super_block *sb,
12888016e29fSHarshad Shirwadkar 				struct dentry_info_args *darg,
12898016e29fSHarshad Shirwadkar 				struct inode *inode)
12908016e29fSHarshad Shirwadkar {
12918016e29fSHarshad Shirwadkar 	struct inode *dir = NULL;
12928016e29fSHarshad Shirwadkar 	struct dentry *dentry_dir = NULL, *dentry_inode = NULL;
12938016e29fSHarshad Shirwadkar 	struct qstr qstr_dname = QSTR_INIT(darg->dname, darg->dname_len);
12948016e29fSHarshad Shirwadkar 	int ret = 0;
12958016e29fSHarshad Shirwadkar 
12968016e29fSHarshad Shirwadkar 	dir = ext4_iget(sb, darg->parent_ino, EXT4_IGET_NORMAL);
12978016e29fSHarshad Shirwadkar 	if (IS_ERR(dir)) {
12988016e29fSHarshad Shirwadkar 		jbd_debug(1, "Dir with inode %d not found.", darg->parent_ino);
12998016e29fSHarshad Shirwadkar 		dir = NULL;
13008016e29fSHarshad Shirwadkar 		goto out;
13018016e29fSHarshad Shirwadkar 	}
13028016e29fSHarshad Shirwadkar 
13038016e29fSHarshad Shirwadkar 	dentry_dir = d_obtain_alias(dir);
13048016e29fSHarshad Shirwadkar 	if (IS_ERR(dentry_dir)) {
13058016e29fSHarshad Shirwadkar 		jbd_debug(1, "Failed to obtain dentry");
13068016e29fSHarshad Shirwadkar 		dentry_dir = NULL;
13078016e29fSHarshad Shirwadkar 		goto out;
13088016e29fSHarshad Shirwadkar 	}
13098016e29fSHarshad Shirwadkar 
13108016e29fSHarshad Shirwadkar 	dentry_inode = d_alloc(dentry_dir, &qstr_dname);
13118016e29fSHarshad Shirwadkar 	if (!dentry_inode) {
13128016e29fSHarshad Shirwadkar 		jbd_debug(1, "Inode dentry not created.");
13138016e29fSHarshad Shirwadkar 		ret = -ENOMEM;
13148016e29fSHarshad Shirwadkar 		goto out;
13158016e29fSHarshad Shirwadkar 	}
13168016e29fSHarshad Shirwadkar 
13178016e29fSHarshad Shirwadkar 	ret = __ext4_link(dir, inode, dentry_inode);
13188016e29fSHarshad Shirwadkar 	/*
13198016e29fSHarshad Shirwadkar 	 * It's possible that link already existed since data blocks
13208016e29fSHarshad Shirwadkar 	 * for the dir in question got persisted before we crashed OR
13218016e29fSHarshad Shirwadkar 	 * we replayed this tag and crashed before the entire replay
13228016e29fSHarshad Shirwadkar 	 * could complete.
13238016e29fSHarshad Shirwadkar 	 */
13248016e29fSHarshad Shirwadkar 	if (ret && ret != -EEXIST) {
13258016e29fSHarshad Shirwadkar 		jbd_debug(1, "Failed to link\n");
13268016e29fSHarshad Shirwadkar 		goto out;
13278016e29fSHarshad Shirwadkar 	}
13288016e29fSHarshad Shirwadkar 
13298016e29fSHarshad Shirwadkar 	ret = 0;
13308016e29fSHarshad Shirwadkar out:
13318016e29fSHarshad Shirwadkar 	if (dentry_dir) {
13328016e29fSHarshad Shirwadkar 		d_drop(dentry_dir);
13338016e29fSHarshad Shirwadkar 		dput(dentry_dir);
13348016e29fSHarshad Shirwadkar 	} else if (dir) {
13358016e29fSHarshad Shirwadkar 		iput(dir);
13368016e29fSHarshad Shirwadkar 	}
13378016e29fSHarshad Shirwadkar 	if (dentry_inode) {
13388016e29fSHarshad Shirwadkar 		d_drop(dentry_inode);
13398016e29fSHarshad Shirwadkar 		dput(dentry_inode);
13408016e29fSHarshad Shirwadkar 	}
13418016e29fSHarshad Shirwadkar 
13428016e29fSHarshad Shirwadkar 	return ret;
13438016e29fSHarshad Shirwadkar }
13448016e29fSHarshad Shirwadkar 
13458016e29fSHarshad Shirwadkar /* Link replay function */
13468016e29fSHarshad Shirwadkar static int ext4_fc_replay_link(struct super_block *sb, struct ext4_fc_tl *tl)
13478016e29fSHarshad Shirwadkar {
13488016e29fSHarshad Shirwadkar 	struct inode *inode;
13498016e29fSHarshad Shirwadkar 	struct dentry_info_args darg;
13508016e29fSHarshad Shirwadkar 	int ret = 0;
13518016e29fSHarshad Shirwadkar 
13528016e29fSHarshad Shirwadkar 	tl_to_darg(&darg, tl);
13538016e29fSHarshad Shirwadkar 	trace_ext4_fc_replay(sb, EXT4_FC_TAG_LINK, darg.ino,
13548016e29fSHarshad Shirwadkar 			darg.parent_ino, darg.dname_len);
13558016e29fSHarshad Shirwadkar 
13568016e29fSHarshad Shirwadkar 	inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
13578016e29fSHarshad Shirwadkar 	if (IS_ERR_OR_NULL(inode)) {
13588016e29fSHarshad Shirwadkar 		jbd_debug(1, "Inode not found.");
13598016e29fSHarshad Shirwadkar 		return 0;
13608016e29fSHarshad Shirwadkar 	}
13618016e29fSHarshad Shirwadkar 
13628016e29fSHarshad Shirwadkar 	ret = ext4_fc_replay_link_internal(sb, &darg, inode);
13638016e29fSHarshad Shirwadkar 	iput(inode);
13648016e29fSHarshad Shirwadkar 	return ret;
13658016e29fSHarshad Shirwadkar }
13668016e29fSHarshad Shirwadkar 
13678016e29fSHarshad Shirwadkar /*
13688016e29fSHarshad Shirwadkar  * Record all the modified inodes during replay. We use this later to setup
13698016e29fSHarshad Shirwadkar  * block bitmaps correctly.
13708016e29fSHarshad Shirwadkar  */
13718016e29fSHarshad Shirwadkar static int ext4_fc_record_modified_inode(struct super_block *sb, int ino)
13728016e29fSHarshad Shirwadkar {
13738016e29fSHarshad Shirwadkar 	struct ext4_fc_replay_state *state;
13748016e29fSHarshad Shirwadkar 	int i;
13758016e29fSHarshad Shirwadkar 
13768016e29fSHarshad Shirwadkar 	state = &EXT4_SB(sb)->s_fc_replay_state;
13778016e29fSHarshad Shirwadkar 	for (i = 0; i < state->fc_modified_inodes_used; i++)
13788016e29fSHarshad Shirwadkar 		if (state->fc_modified_inodes[i] == ino)
13798016e29fSHarshad Shirwadkar 			return 0;
13808016e29fSHarshad Shirwadkar 	if (state->fc_modified_inodes_used == state->fc_modified_inodes_size) {
13818016e29fSHarshad Shirwadkar 		state->fc_modified_inodes_size +=
13828016e29fSHarshad Shirwadkar 			EXT4_FC_REPLAY_REALLOC_INCREMENT;
13838016e29fSHarshad Shirwadkar 		state->fc_modified_inodes = krealloc(
13848016e29fSHarshad Shirwadkar 					state->fc_modified_inodes, sizeof(int) *
13858016e29fSHarshad Shirwadkar 					state->fc_modified_inodes_size,
13868016e29fSHarshad Shirwadkar 					GFP_KERNEL);
13878016e29fSHarshad Shirwadkar 		if (!state->fc_modified_inodes)
13888016e29fSHarshad Shirwadkar 			return -ENOMEM;
13898016e29fSHarshad Shirwadkar 	}
13908016e29fSHarshad Shirwadkar 	state->fc_modified_inodes[state->fc_modified_inodes_used++] = ino;
13918016e29fSHarshad Shirwadkar 	return 0;
13928016e29fSHarshad Shirwadkar }
13938016e29fSHarshad Shirwadkar 
13948016e29fSHarshad Shirwadkar /*
13958016e29fSHarshad Shirwadkar  * Inode replay function
13968016e29fSHarshad Shirwadkar  */
13978016e29fSHarshad Shirwadkar static int ext4_fc_replay_inode(struct super_block *sb, struct ext4_fc_tl *tl)
13988016e29fSHarshad Shirwadkar {
13998016e29fSHarshad Shirwadkar 	struct ext4_fc_inode *fc_inode;
14008016e29fSHarshad Shirwadkar 	struct ext4_inode *raw_inode;
14018016e29fSHarshad Shirwadkar 	struct ext4_inode *raw_fc_inode;
14028016e29fSHarshad Shirwadkar 	struct inode *inode = NULL;
14038016e29fSHarshad Shirwadkar 	struct ext4_iloc iloc;
14048016e29fSHarshad Shirwadkar 	int inode_len, ino, ret, tag = le16_to_cpu(tl->fc_tag);
14058016e29fSHarshad Shirwadkar 	struct ext4_extent_header *eh;
14068016e29fSHarshad Shirwadkar 
14078016e29fSHarshad Shirwadkar 	fc_inode = (struct ext4_fc_inode *)ext4_fc_tag_val(tl);
14088016e29fSHarshad Shirwadkar 
14098016e29fSHarshad Shirwadkar 	ino = le32_to_cpu(fc_inode->fc_ino);
14108016e29fSHarshad Shirwadkar 	trace_ext4_fc_replay(sb, tag, ino, 0, 0);
14118016e29fSHarshad Shirwadkar 
14128016e29fSHarshad Shirwadkar 	inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
14138016e29fSHarshad Shirwadkar 	if (!IS_ERR_OR_NULL(inode)) {
14148016e29fSHarshad Shirwadkar 		ext4_ext_clear_bb(inode);
14158016e29fSHarshad Shirwadkar 		iput(inode);
14168016e29fSHarshad Shirwadkar 	}
14178016e29fSHarshad Shirwadkar 
14188016e29fSHarshad Shirwadkar 	ext4_fc_record_modified_inode(sb, ino);
14198016e29fSHarshad Shirwadkar 
14208016e29fSHarshad Shirwadkar 	raw_fc_inode = (struct ext4_inode *)fc_inode->fc_raw_inode;
14218016e29fSHarshad Shirwadkar 	ret = ext4_get_fc_inode_loc(sb, ino, &iloc);
14228016e29fSHarshad Shirwadkar 	if (ret)
14238016e29fSHarshad Shirwadkar 		goto out;
14248016e29fSHarshad Shirwadkar 
14258016e29fSHarshad Shirwadkar 	inode_len = ext4_fc_tag_len(tl) - sizeof(struct ext4_fc_inode);
14268016e29fSHarshad Shirwadkar 	raw_inode = ext4_raw_inode(&iloc);
14278016e29fSHarshad Shirwadkar 
14288016e29fSHarshad Shirwadkar 	memcpy(raw_inode, raw_fc_inode, offsetof(struct ext4_inode, i_block));
14298016e29fSHarshad Shirwadkar 	memcpy(&raw_inode->i_generation, &raw_fc_inode->i_generation,
14308016e29fSHarshad Shirwadkar 		inode_len - offsetof(struct ext4_inode, i_generation));
14318016e29fSHarshad Shirwadkar 	if (le32_to_cpu(raw_inode->i_flags) & EXT4_EXTENTS_FL) {
14328016e29fSHarshad Shirwadkar 		eh = (struct ext4_extent_header *)(&raw_inode->i_block[0]);
14338016e29fSHarshad Shirwadkar 		if (eh->eh_magic != EXT4_EXT_MAGIC) {
14348016e29fSHarshad Shirwadkar 			memset(eh, 0, sizeof(*eh));
14358016e29fSHarshad Shirwadkar 			eh->eh_magic = EXT4_EXT_MAGIC;
14368016e29fSHarshad Shirwadkar 			eh->eh_max = cpu_to_le16(
14378016e29fSHarshad Shirwadkar 				(sizeof(raw_inode->i_block) -
14388016e29fSHarshad Shirwadkar 				 sizeof(struct ext4_extent_header))
14398016e29fSHarshad Shirwadkar 				 / sizeof(struct ext4_extent));
14408016e29fSHarshad Shirwadkar 		}
14418016e29fSHarshad Shirwadkar 	} else if (le32_to_cpu(raw_inode->i_flags) & EXT4_INLINE_DATA_FL) {
14428016e29fSHarshad Shirwadkar 		memcpy(raw_inode->i_block, raw_fc_inode->i_block,
14438016e29fSHarshad Shirwadkar 			sizeof(raw_inode->i_block));
14448016e29fSHarshad Shirwadkar 	}
14458016e29fSHarshad Shirwadkar 
14468016e29fSHarshad Shirwadkar 	/* Immediately update the inode on disk. */
14478016e29fSHarshad Shirwadkar 	ret = ext4_handle_dirty_metadata(NULL, NULL, iloc.bh);
14488016e29fSHarshad Shirwadkar 	if (ret)
14498016e29fSHarshad Shirwadkar 		goto out;
14508016e29fSHarshad Shirwadkar 	ret = sync_dirty_buffer(iloc.bh);
14518016e29fSHarshad Shirwadkar 	if (ret)
14528016e29fSHarshad Shirwadkar 		goto out;
14538016e29fSHarshad Shirwadkar 	ret = ext4_mark_inode_used(sb, ino);
14548016e29fSHarshad Shirwadkar 	if (ret)
14558016e29fSHarshad Shirwadkar 		goto out;
14568016e29fSHarshad Shirwadkar 
14578016e29fSHarshad Shirwadkar 	/* Given that we just wrote the inode on disk, this SHOULD succeed. */
14588016e29fSHarshad Shirwadkar 	inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
14598016e29fSHarshad Shirwadkar 	if (IS_ERR_OR_NULL(inode)) {
14608016e29fSHarshad Shirwadkar 		jbd_debug(1, "Inode not found.");
14618016e29fSHarshad Shirwadkar 		return -EFSCORRUPTED;
14628016e29fSHarshad Shirwadkar 	}
14638016e29fSHarshad Shirwadkar 
14648016e29fSHarshad Shirwadkar 	/*
14658016e29fSHarshad Shirwadkar 	 * Our allocator could have made different decisions than before
14668016e29fSHarshad Shirwadkar 	 * crashing. This should be fixed but until then, we calculate
14678016e29fSHarshad Shirwadkar 	 * the number of blocks the inode.
14688016e29fSHarshad Shirwadkar 	 */
14698016e29fSHarshad Shirwadkar 	ext4_ext_replay_set_iblocks(inode);
14708016e29fSHarshad Shirwadkar 
14718016e29fSHarshad Shirwadkar 	inode->i_generation = le32_to_cpu(ext4_raw_inode(&iloc)->i_generation);
14728016e29fSHarshad Shirwadkar 	ext4_reset_inode_seed(inode);
14738016e29fSHarshad Shirwadkar 
14748016e29fSHarshad Shirwadkar 	ext4_inode_csum_set(inode, ext4_raw_inode(&iloc), EXT4_I(inode));
14758016e29fSHarshad Shirwadkar 	ret = ext4_handle_dirty_metadata(NULL, NULL, iloc.bh);
14768016e29fSHarshad Shirwadkar 	sync_dirty_buffer(iloc.bh);
14778016e29fSHarshad Shirwadkar 	brelse(iloc.bh);
14788016e29fSHarshad Shirwadkar out:
14798016e29fSHarshad Shirwadkar 	iput(inode);
14808016e29fSHarshad Shirwadkar 	if (!ret)
14818016e29fSHarshad Shirwadkar 		blkdev_issue_flush(sb->s_bdev, GFP_KERNEL);
14828016e29fSHarshad Shirwadkar 
14838016e29fSHarshad Shirwadkar 	return 0;
14848016e29fSHarshad Shirwadkar }
14858016e29fSHarshad Shirwadkar 
14868016e29fSHarshad Shirwadkar /*
14878016e29fSHarshad Shirwadkar  * Dentry create replay function.
14888016e29fSHarshad Shirwadkar  *
14898016e29fSHarshad Shirwadkar  * EXT4_FC_TAG_CREAT is preceded by EXT4_FC_TAG_INODE_FULL. Which means, the
14908016e29fSHarshad Shirwadkar  * inode for which we are trying to create a dentry here, should already have
14918016e29fSHarshad Shirwadkar  * been replayed before we start here.
14928016e29fSHarshad Shirwadkar  */
14938016e29fSHarshad Shirwadkar static int ext4_fc_replay_create(struct super_block *sb, struct ext4_fc_tl *tl)
14948016e29fSHarshad Shirwadkar {
14958016e29fSHarshad Shirwadkar 	int ret = 0;
14968016e29fSHarshad Shirwadkar 	struct inode *inode = NULL;
14978016e29fSHarshad Shirwadkar 	struct inode *dir = NULL;
14988016e29fSHarshad Shirwadkar 	struct dentry_info_args darg;
14998016e29fSHarshad Shirwadkar 
15008016e29fSHarshad Shirwadkar 	tl_to_darg(&darg, tl);
15018016e29fSHarshad Shirwadkar 
15028016e29fSHarshad Shirwadkar 	trace_ext4_fc_replay(sb, EXT4_FC_TAG_CREAT, darg.ino,
15038016e29fSHarshad Shirwadkar 			darg.parent_ino, darg.dname_len);
15048016e29fSHarshad Shirwadkar 
15058016e29fSHarshad Shirwadkar 	/* This takes care of update group descriptor and other metadata */
15068016e29fSHarshad Shirwadkar 	ret = ext4_mark_inode_used(sb, darg.ino);
15078016e29fSHarshad Shirwadkar 	if (ret)
15088016e29fSHarshad Shirwadkar 		goto out;
15098016e29fSHarshad Shirwadkar 
15108016e29fSHarshad Shirwadkar 	inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
15118016e29fSHarshad Shirwadkar 	if (IS_ERR_OR_NULL(inode)) {
15128016e29fSHarshad Shirwadkar 		jbd_debug(1, "inode %d not found.", darg.ino);
15138016e29fSHarshad Shirwadkar 		inode = NULL;
15148016e29fSHarshad Shirwadkar 		ret = -EINVAL;
15158016e29fSHarshad Shirwadkar 		goto out;
15168016e29fSHarshad Shirwadkar 	}
15178016e29fSHarshad Shirwadkar 
15188016e29fSHarshad Shirwadkar 	if (S_ISDIR(inode->i_mode)) {
15198016e29fSHarshad Shirwadkar 		/*
15208016e29fSHarshad Shirwadkar 		 * If we are creating a directory, we need to make sure that the
15218016e29fSHarshad Shirwadkar 		 * dot and dot dot dirents are setup properly.
15228016e29fSHarshad Shirwadkar 		 */
15238016e29fSHarshad Shirwadkar 		dir = ext4_iget(sb, darg.parent_ino, EXT4_IGET_NORMAL);
15248016e29fSHarshad Shirwadkar 		if (IS_ERR_OR_NULL(dir)) {
15258016e29fSHarshad Shirwadkar 			jbd_debug(1, "Dir %d not found.", darg.ino);
15268016e29fSHarshad Shirwadkar 			goto out;
15278016e29fSHarshad Shirwadkar 		}
15288016e29fSHarshad Shirwadkar 		ret = ext4_init_new_dir(NULL, dir, inode);
15298016e29fSHarshad Shirwadkar 		iput(dir);
15308016e29fSHarshad Shirwadkar 		if (ret) {
15318016e29fSHarshad Shirwadkar 			ret = 0;
15328016e29fSHarshad Shirwadkar 			goto out;
15338016e29fSHarshad Shirwadkar 		}
15348016e29fSHarshad Shirwadkar 	}
15358016e29fSHarshad Shirwadkar 	ret = ext4_fc_replay_link_internal(sb, &darg, inode);
15368016e29fSHarshad Shirwadkar 	if (ret)
15378016e29fSHarshad Shirwadkar 		goto out;
15388016e29fSHarshad Shirwadkar 	set_nlink(inode, 1);
15398016e29fSHarshad Shirwadkar 	ext4_mark_inode_dirty(NULL, inode);
15408016e29fSHarshad Shirwadkar out:
15418016e29fSHarshad Shirwadkar 	if (inode)
15428016e29fSHarshad Shirwadkar 		iput(inode);
15438016e29fSHarshad Shirwadkar 	return ret;
15448016e29fSHarshad Shirwadkar }
15458016e29fSHarshad Shirwadkar 
15468016e29fSHarshad Shirwadkar /*
15478016e29fSHarshad Shirwadkar  * Record physical disk regions which are in use as per fast commit area. Our
15488016e29fSHarshad Shirwadkar  * simple replay phase allocator excludes these regions from allocation.
15498016e29fSHarshad Shirwadkar  */
15508016e29fSHarshad Shirwadkar static int ext4_fc_record_regions(struct super_block *sb, int ino,
15518016e29fSHarshad Shirwadkar 		ext4_lblk_t lblk, ext4_fsblk_t pblk, int len)
15528016e29fSHarshad Shirwadkar {
15538016e29fSHarshad Shirwadkar 	struct ext4_fc_replay_state *state;
15548016e29fSHarshad Shirwadkar 	struct ext4_fc_alloc_region *region;
15558016e29fSHarshad Shirwadkar 
15568016e29fSHarshad Shirwadkar 	state = &EXT4_SB(sb)->s_fc_replay_state;
15578016e29fSHarshad Shirwadkar 	if (state->fc_regions_used == state->fc_regions_size) {
15588016e29fSHarshad Shirwadkar 		state->fc_regions_size +=
15598016e29fSHarshad Shirwadkar 			EXT4_FC_REPLAY_REALLOC_INCREMENT;
15608016e29fSHarshad Shirwadkar 		state->fc_regions = krealloc(
15618016e29fSHarshad Shirwadkar 					state->fc_regions,
15628016e29fSHarshad Shirwadkar 					state->fc_regions_size *
15638016e29fSHarshad Shirwadkar 					sizeof(struct ext4_fc_alloc_region),
15648016e29fSHarshad Shirwadkar 					GFP_KERNEL);
15658016e29fSHarshad Shirwadkar 		if (!state->fc_regions)
15668016e29fSHarshad Shirwadkar 			return -ENOMEM;
15678016e29fSHarshad Shirwadkar 	}
15688016e29fSHarshad Shirwadkar 	region = &state->fc_regions[state->fc_regions_used++];
15698016e29fSHarshad Shirwadkar 	region->ino = ino;
15708016e29fSHarshad Shirwadkar 	region->lblk = lblk;
15718016e29fSHarshad Shirwadkar 	region->pblk = pblk;
15728016e29fSHarshad Shirwadkar 	region->len = len;
15738016e29fSHarshad Shirwadkar 
15748016e29fSHarshad Shirwadkar 	return 0;
15758016e29fSHarshad Shirwadkar }
15768016e29fSHarshad Shirwadkar 
15778016e29fSHarshad Shirwadkar /* Replay add range tag */
15788016e29fSHarshad Shirwadkar static int ext4_fc_replay_add_range(struct super_block *sb,
15798016e29fSHarshad Shirwadkar 				struct ext4_fc_tl *tl)
15808016e29fSHarshad Shirwadkar {
15818016e29fSHarshad Shirwadkar 	struct ext4_fc_add_range *fc_add_ex;
15828016e29fSHarshad Shirwadkar 	struct ext4_extent newex, *ex;
15838016e29fSHarshad Shirwadkar 	struct inode *inode;
15848016e29fSHarshad Shirwadkar 	ext4_lblk_t start, cur;
15858016e29fSHarshad Shirwadkar 	int remaining, len;
15868016e29fSHarshad Shirwadkar 	ext4_fsblk_t start_pblk;
15878016e29fSHarshad Shirwadkar 	struct ext4_map_blocks map;
15888016e29fSHarshad Shirwadkar 	struct ext4_ext_path *path = NULL;
15898016e29fSHarshad Shirwadkar 	int ret;
15908016e29fSHarshad Shirwadkar 
15918016e29fSHarshad Shirwadkar 	fc_add_ex = (struct ext4_fc_add_range *)ext4_fc_tag_val(tl);
15928016e29fSHarshad Shirwadkar 	ex = (struct ext4_extent *)&fc_add_ex->fc_ex;
15938016e29fSHarshad Shirwadkar 
15948016e29fSHarshad Shirwadkar 	trace_ext4_fc_replay(sb, EXT4_FC_TAG_ADD_RANGE,
15958016e29fSHarshad Shirwadkar 		le32_to_cpu(fc_add_ex->fc_ino), le32_to_cpu(ex->ee_block),
15968016e29fSHarshad Shirwadkar 		ext4_ext_get_actual_len(ex));
15978016e29fSHarshad Shirwadkar 
15988016e29fSHarshad Shirwadkar 	inode = ext4_iget(sb, le32_to_cpu(fc_add_ex->fc_ino),
15998016e29fSHarshad Shirwadkar 				EXT4_IGET_NORMAL);
16008016e29fSHarshad Shirwadkar 	if (IS_ERR_OR_NULL(inode)) {
16018016e29fSHarshad Shirwadkar 		jbd_debug(1, "Inode not found.");
16028016e29fSHarshad Shirwadkar 		return 0;
16038016e29fSHarshad Shirwadkar 	}
16048016e29fSHarshad Shirwadkar 
16058016e29fSHarshad Shirwadkar 	ret = ext4_fc_record_modified_inode(sb, inode->i_ino);
16068016e29fSHarshad Shirwadkar 
16078016e29fSHarshad Shirwadkar 	start = le32_to_cpu(ex->ee_block);
16088016e29fSHarshad Shirwadkar 	start_pblk = ext4_ext_pblock(ex);
16098016e29fSHarshad Shirwadkar 	len = ext4_ext_get_actual_len(ex);
16108016e29fSHarshad Shirwadkar 
16118016e29fSHarshad Shirwadkar 	cur = start;
16128016e29fSHarshad Shirwadkar 	remaining = len;
16138016e29fSHarshad Shirwadkar 	jbd_debug(1, "ADD_RANGE, lblk %d, pblk %lld, len %d, unwritten %d, inode %ld\n",
16148016e29fSHarshad Shirwadkar 		  start, start_pblk, len, ext4_ext_is_unwritten(ex),
16158016e29fSHarshad Shirwadkar 		  inode->i_ino);
16168016e29fSHarshad Shirwadkar 
16178016e29fSHarshad Shirwadkar 	while (remaining > 0) {
16188016e29fSHarshad Shirwadkar 		map.m_lblk = cur;
16198016e29fSHarshad Shirwadkar 		map.m_len = remaining;
16208016e29fSHarshad Shirwadkar 		map.m_pblk = 0;
16218016e29fSHarshad Shirwadkar 		ret = ext4_map_blocks(NULL, inode, &map, 0);
16228016e29fSHarshad Shirwadkar 
16238016e29fSHarshad Shirwadkar 		if (ret < 0) {
16248016e29fSHarshad Shirwadkar 			iput(inode);
16258016e29fSHarshad Shirwadkar 			return 0;
16268016e29fSHarshad Shirwadkar 		}
16278016e29fSHarshad Shirwadkar 
16288016e29fSHarshad Shirwadkar 		if (ret == 0) {
16298016e29fSHarshad Shirwadkar 			/* Range is not mapped */
16308016e29fSHarshad Shirwadkar 			path = ext4_find_extent(inode, cur, NULL, 0);
16318c9be1e5SHarshad Shirwadkar 			if (IS_ERR(path)) {
16328c9be1e5SHarshad Shirwadkar 				iput(inode);
16338c9be1e5SHarshad Shirwadkar 				return 0;
16348c9be1e5SHarshad Shirwadkar 			}
16358016e29fSHarshad Shirwadkar 			memset(&newex, 0, sizeof(newex));
16368016e29fSHarshad Shirwadkar 			newex.ee_block = cpu_to_le32(cur);
16378016e29fSHarshad Shirwadkar 			ext4_ext_store_pblock(
16388016e29fSHarshad Shirwadkar 				&newex, start_pblk + cur - start);
16398016e29fSHarshad Shirwadkar 			newex.ee_len = cpu_to_le16(map.m_len);
16408016e29fSHarshad Shirwadkar 			if (ext4_ext_is_unwritten(ex))
16418016e29fSHarshad Shirwadkar 				ext4_ext_mark_unwritten(&newex);
16428016e29fSHarshad Shirwadkar 			down_write(&EXT4_I(inode)->i_data_sem);
16438016e29fSHarshad Shirwadkar 			ret = ext4_ext_insert_extent(
16448016e29fSHarshad Shirwadkar 				NULL, inode, &path, &newex, 0);
16458016e29fSHarshad Shirwadkar 			up_write((&EXT4_I(inode)->i_data_sem));
16468016e29fSHarshad Shirwadkar 			ext4_ext_drop_refs(path);
16478016e29fSHarshad Shirwadkar 			kfree(path);
16488016e29fSHarshad Shirwadkar 			if (ret) {
16498016e29fSHarshad Shirwadkar 				iput(inode);
16508016e29fSHarshad Shirwadkar 				return 0;
16518016e29fSHarshad Shirwadkar 			}
16528016e29fSHarshad Shirwadkar 			goto next;
16538016e29fSHarshad Shirwadkar 		}
16548016e29fSHarshad Shirwadkar 
16558016e29fSHarshad Shirwadkar 		if (start_pblk + cur - start != map.m_pblk) {
16568016e29fSHarshad Shirwadkar 			/*
16578016e29fSHarshad Shirwadkar 			 * Logical to physical mapping changed. This can happen
16588016e29fSHarshad Shirwadkar 			 * if this range was removed and then reallocated to
16598016e29fSHarshad Shirwadkar 			 * map to new physical blocks during a fast commit.
16608016e29fSHarshad Shirwadkar 			 */
16618016e29fSHarshad Shirwadkar 			ret = ext4_ext_replay_update_ex(inode, cur, map.m_len,
16628016e29fSHarshad Shirwadkar 					ext4_ext_is_unwritten(ex),
16638016e29fSHarshad Shirwadkar 					start_pblk + cur - start);
16648016e29fSHarshad Shirwadkar 			if (ret) {
16658016e29fSHarshad Shirwadkar 				iput(inode);
16668016e29fSHarshad Shirwadkar 				return 0;
16678016e29fSHarshad Shirwadkar 			}
16688016e29fSHarshad Shirwadkar 			/*
16698016e29fSHarshad Shirwadkar 			 * Mark the old blocks as free since they aren't used
16708016e29fSHarshad Shirwadkar 			 * anymore. We maintain an array of all the modified
16718016e29fSHarshad Shirwadkar 			 * inodes. In case these blocks are still used at either
16728016e29fSHarshad Shirwadkar 			 * a different logical range in the same inode or in
16738016e29fSHarshad Shirwadkar 			 * some different inode, we will mark them as allocated
16748016e29fSHarshad Shirwadkar 			 * at the end of the FC replay using our array of
16758016e29fSHarshad Shirwadkar 			 * modified inodes.
16768016e29fSHarshad Shirwadkar 			 */
16778016e29fSHarshad Shirwadkar 			ext4_mb_mark_bb(inode->i_sb, map.m_pblk, map.m_len, 0);
16788016e29fSHarshad Shirwadkar 			goto next;
16798016e29fSHarshad Shirwadkar 		}
16808016e29fSHarshad Shirwadkar 
16818016e29fSHarshad Shirwadkar 		/* Range is mapped and needs a state change */
16828016e29fSHarshad Shirwadkar 		jbd_debug(1, "Converting from %d to %d %lld",
16838016e29fSHarshad Shirwadkar 				map.m_flags & EXT4_MAP_UNWRITTEN,
16848016e29fSHarshad Shirwadkar 			ext4_ext_is_unwritten(ex), map.m_pblk);
16858016e29fSHarshad Shirwadkar 		ret = ext4_ext_replay_update_ex(inode, cur, map.m_len,
16868016e29fSHarshad Shirwadkar 					ext4_ext_is_unwritten(ex), map.m_pblk);
16878016e29fSHarshad Shirwadkar 		if (ret) {
16888016e29fSHarshad Shirwadkar 			iput(inode);
16898016e29fSHarshad Shirwadkar 			return 0;
16908016e29fSHarshad Shirwadkar 		}
16918016e29fSHarshad Shirwadkar 		/*
16928016e29fSHarshad Shirwadkar 		 * We may have split the extent tree while toggling the state.
16938016e29fSHarshad Shirwadkar 		 * Try to shrink the extent tree now.
16948016e29fSHarshad Shirwadkar 		 */
16958016e29fSHarshad Shirwadkar 		ext4_ext_replay_shrink_inode(inode, start + len);
16968016e29fSHarshad Shirwadkar next:
16978016e29fSHarshad Shirwadkar 		cur += map.m_len;
16988016e29fSHarshad Shirwadkar 		remaining -= map.m_len;
16998016e29fSHarshad Shirwadkar 	}
17008016e29fSHarshad Shirwadkar 	ext4_ext_replay_shrink_inode(inode, i_size_read(inode) >>
17018016e29fSHarshad Shirwadkar 					sb->s_blocksize_bits);
17028016e29fSHarshad Shirwadkar 	iput(inode);
17038016e29fSHarshad Shirwadkar 	return 0;
17048016e29fSHarshad Shirwadkar }
17058016e29fSHarshad Shirwadkar 
17068016e29fSHarshad Shirwadkar /* Replay DEL_RANGE tag */
17078016e29fSHarshad Shirwadkar static int
17088016e29fSHarshad Shirwadkar ext4_fc_replay_del_range(struct super_block *sb, struct ext4_fc_tl *tl)
17098016e29fSHarshad Shirwadkar {
17108016e29fSHarshad Shirwadkar 	struct inode *inode;
17118016e29fSHarshad Shirwadkar 	struct ext4_fc_del_range *lrange;
17128016e29fSHarshad Shirwadkar 	struct ext4_map_blocks map;
17138016e29fSHarshad Shirwadkar 	ext4_lblk_t cur, remaining;
17148016e29fSHarshad Shirwadkar 	int ret;
17158016e29fSHarshad Shirwadkar 
17168016e29fSHarshad Shirwadkar 	lrange = (struct ext4_fc_del_range *)ext4_fc_tag_val(tl);
17178016e29fSHarshad Shirwadkar 	cur = le32_to_cpu(lrange->fc_lblk);
17188016e29fSHarshad Shirwadkar 	remaining = le32_to_cpu(lrange->fc_len);
17198016e29fSHarshad Shirwadkar 
17208016e29fSHarshad Shirwadkar 	trace_ext4_fc_replay(sb, EXT4_FC_TAG_DEL_RANGE,
17218016e29fSHarshad Shirwadkar 		le32_to_cpu(lrange->fc_ino), cur, remaining);
17228016e29fSHarshad Shirwadkar 
17238016e29fSHarshad Shirwadkar 	inode = ext4_iget(sb, le32_to_cpu(lrange->fc_ino), EXT4_IGET_NORMAL);
17248016e29fSHarshad Shirwadkar 	if (IS_ERR_OR_NULL(inode)) {
17258016e29fSHarshad Shirwadkar 		jbd_debug(1, "Inode %d not found", le32_to_cpu(lrange->fc_ino));
17268016e29fSHarshad Shirwadkar 		return 0;
17278016e29fSHarshad Shirwadkar 	}
17288016e29fSHarshad Shirwadkar 
17298016e29fSHarshad Shirwadkar 	ret = ext4_fc_record_modified_inode(sb, inode->i_ino);
17308016e29fSHarshad Shirwadkar 
17318016e29fSHarshad Shirwadkar 	jbd_debug(1, "DEL_RANGE, inode %ld, lblk %d, len %d\n",
17328016e29fSHarshad Shirwadkar 			inode->i_ino, le32_to_cpu(lrange->fc_lblk),
17338016e29fSHarshad Shirwadkar 			le32_to_cpu(lrange->fc_len));
17348016e29fSHarshad Shirwadkar 	while (remaining > 0) {
17358016e29fSHarshad Shirwadkar 		map.m_lblk = cur;
17368016e29fSHarshad Shirwadkar 		map.m_len = remaining;
17378016e29fSHarshad Shirwadkar 
17388016e29fSHarshad Shirwadkar 		ret = ext4_map_blocks(NULL, inode, &map, 0);
17398016e29fSHarshad Shirwadkar 		if (ret < 0) {
17408016e29fSHarshad Shirwadkar 			iput(inode);
17418016e29fSHarshad Shirwadkar 			return 0;
17428016e29fSHarshad Shirwadkar 		}
17438016e29fSHarshad Shirwadkar 		if (ret > 0) {
17448016e29fSHarshad Shirwadkar 			remaining -= ret;
17458016e29fSHarshad Shirwadkar 			cur += ret;
17468016e29fSHarshad Shirwadkar 			ext4_mb_mark_bb(inode->i_sb, map.m_pblk, map.m_len, 0);
17478016e29fSHarshad Shirwadkar 		} else {
17488016e29fSHarshad Shirwadkar 			remaining -= map.m_len;
17498016e29fSHarshad Shirwadkar 			cur += map.m_len;
17508016e29fSHarshad Shirwadkar 		}
17518016e29fSHarshad Shirwadkar 	}
17528016e29fSHarshad Shirwadkar 
17538016e29fSHarshad Shirwadkar 	ret = ext4_punch_hole(inode,
17548016e29fSHarshad Shirwadkar 		le32_to_cpu(lrange->fc_lblk) << sb->s_blocksize_bits,
17558016e29fSHarshad Shirwadkar 		le32_to_cpu(lrange->fc_len) <<  sb->s_blocksize_bits);
17568016e29fSHarshad Shirwadkar 	if (ret)
17578016e29fSHarshad Shirwadkar 		jbd_debug(1, "ext4_punch_hole returned %d", ret);
17588016e29fSHarshad Shirwadkar 	ext4_ext_replay_shrink_inode(inode,
17598016e29fSHarshad Shirwadkar 		i_size_read(inode) >> sb->s_blocksize_bits);
17608016e29fSHarshad Shirwadkar 	ext4_mark_inode_dirty(NULL, inode);
17618016e29fSHarshad Shirwadkar 	iput(inode);
17628016e29fSHarshad Shirwadkar 
17638016e29fSHarshad Shirwadkar 	return 0;
17648016e29fSHarshad Shirwadkar }
17658016e29fSHarshad Shirwadkar 
17668016e29fSHarshad Shirwadkar static inline const char *tag2str(u16 tag)
17678016e29fSHarshad Shirwadkar {
17688016e29fSHarshad Shirwadkar 	switch (tag) {
17698016e29fSHarshad Shirwadkar 	case EXT4_FC_TAG_LINK:
17708016e29fSHarshad Shirwadkar 		return "TAG_ADD_ENTRY";
17718016e29fSHarshad Shirwadkar 	case EXT4_FC_TAG_UNLINK:
17728016e29fSHarshad Shirwadkar 		return "TAG_DEL_ENTRY";
17738016e29fSHarshad Shirwadkar 	case EXT4_FC_TAG_ADD_RANGE:
17748016e29fSHarshad Shirwadkar 		return "TAG_ADD_RANGE";
17758016e29fSHarshad Shirwadkar 	case EXT4_FC_TAG_CREAT:
17768016e29fSHarshad Shirwadkar 		return "TAG_CREAT_DENTRY";
17778016e29fSHarshad Shirwadkar 	case EXT4_FC_TAG_DEL_RANGE:
17788016e29fSHarshad Shirwadkar 		return "TAG_DEL_RANGE";
17798016e29fSHarshad Shirwadkar 	case EXT4_FC_TAG_INODE:
17808016e29fSHarshad Shirwadkar 		return "TAG_INODE";
17818016e29fSHarshad Shirwadkar 	case EXT4_FC_TAG_PAD:
17828016e29fSHarshad Shirwadkar 		return "TAG_PAD";
17838016e29fSHarshad Shirwadkar 	case EXT4_FC_TAG_TAIL:
17848016e29fSHarshad Shirwadkar 		return "TAG_TAIL";
17858016e29fSHarshad Shirwadkar 	case EXT4_FC_TAG_HEAD:
17868016e29fSHarshad Shirwadkar 		return "TAG_HEAD";
17878016e29fSHarshad Shirwadkar 	default:
17888016e29fSHarshad Shirwadkar 		return "TAG_ERROR";
17898016e29fSHarshad Shirwadkar 	}
17908016e29fSHarshad Shirwadkar }
17918016e29fSHarshad Shirwadkar 
17928016e29fSHarshad Shirwadkar static void ext4_fc_set_bitmaps_and_counters(struct super_block *sb)
17938016e29fSHarshad Shirwadkar {
17948016e29fSHarshad Shirwadkar 	struct ext4_fc_replay_state *state;
17958016e29fSHarshad Shirwadkar 	struct inode *inode;
17968016e29fSHarshad Shirwadkar 	struct ext4_ext_path *path = NULL;
17978016e29fSHarshad Shirwadkar 	struct ext4_map_blocks map;
17988016e29fSHarshad Shirwadkar 	int i, ret, j;
17998016e29fSHarshad Shirwadkar 	ext4_lblk_t cur, end;
18008016e29fSHarshad Shirwadkar 
18018016e29fSHarshad Shirwadkar 	state = &EXT4_SB(sb)->s_fc_replay_state;
18028016e29fSHarshad Shirwadkar 	for (i = 0; i < state->fc_modified_inodes_used; i++) {
18038016e29fSHarshad Shirwadkar 		inode = ext4_iget(sb, state->fc_modified_inodes[i],
18048016e29fSHarshad Shirwadkar 			EXT4_IGET_NORMAL);
18058016e29fSHarshad Shirwadkar 		if (IS_ERR_OR_NULL(inode)) {
18068016e29fSHarshad Shirwadkar 			jbd_debug(1, "Inode %d not found.",
18078016e29fSHarshad Shirwadkar 				state->fc_modified_inodes[i]);
18088016e29fSHarshad Shirwadkar 			continue;
18098016e29fSHarshad Shirwadkar 		}
18108016e29fSHarshad Shirwadkar 		cur = 0;
18118016e29fSHarshad Shirwadkar 		end = EXT_MAX_BLOCKS;
18128016e29fSHarshad Shirwadkar 		while (cur < end) {
18138016e29fSHarshad Shirwadkar 			map.m_lblk = cur;
18148016e29fSHarshad Shirwadkar 			map.m_len = end - cur;
18158016e29fSHarshad Shirwadkar 
18168016e29fSHarshad Shirwadkar 			ret = ext4_map_blocks(NULL, inode, &map, 0);
18178016e29fSHarshad Shirwadkar 			if (ret < 0)
18188016e29fSHarshad Shirwadkar 				break;
18198016e29fSHarshad Shirwadkar 
18208016e29fSHarshad Shirwadkar 			if (ret > 0) {
18218016e29fSHarshad Shirwadkar 				path = ext4_find_extent(inode, map.m_lblk, NULL, 0);
18228016e29fSHarshad Shirwadkar 				if (!IS_ERR_OR_NULL(path)) {
18238016e29fSHarshad Shirwadkar 					for (j = 0; j < path->p_depth; j++)
18248016e29fSHarshad Shirwadkar 						ext4_mb_mark_bb(inode->i_sb,
18258016e29fSHarshad Shirwadkar 							path[j].p_block, 1, 1);
18268016e29fSHarshad Shirwadkar 					ext4_ext_drop_refs(path);
18278016e29fSHarshad Shirwadkar 					kfree(path);
18288016e29fSHarshad Shirwadkar 				}
18298016e29fSHarshad Shirwadkar 				cur += ret;
18308016e29fSHarshad Shirwadkar 				ext4_mb_mark_bb(inode->i_sb, map.m_pblk,
18318016e29fSHarshad Shirwadkar 							map.m_len, 1);
18328016e29fSHarshad Shirwadkar 			} else {
18338016e29fSHarshad Shirwadkar 				cur = cur + (map.m_len ? map.m_len : 1);
18348016e29fSHarshad Shirwadkar 			}
18358016e29fSHarshad Shirwadkar 		}
18368016e29fSHarshad Shirwadkar 		iput(inode);
18378016e29fSHarshad Shirwadkar 	}
18388016e29fSHarshad Shirwadkar }
18398016e29fSHarshad Shirwadkar 
18408016e29fSHarshad Shirwadkar /*
18418016e29fSHarshad Shirwadkar  * Check if block is in excluded regions for block allocation. The simple
18428016e29fSHarshad Shirwadkar  * allocator that runs during replay phase is calls this function to see
18438016e29fSHarshad Shirwadkar  * if it is okay to use a block.
18448016e29fSHarshad Shirwadkar  */
18458016e29fSHarshad Shirwadkar bool ext4_fc_replay_check_excluded(struct super_block *sb, ext4_fsblk_t blk)
18468016e29fSHarshad Shirwadkar {
18478016e29fSHarshad Shirwadkar 	int i;
18488016e29fSHarshad Shirwadkar 	struct ext4_fc_replay_state *state;
18498016e29fSHarshad Shirwadkar 
18508016e29fSHarshad Shirwadkar 	state = &EXT4_SB(sb)->s_fc_replay_state;
18518016e29fSHarshad Shirwadkar 	for (i = 0; i < state->fc_regions_valid; i++) {
18528016e29fSHarshad Shirwadkar 		if (state->fc_regions[i].ino == 0 ||
18538016e29fSHarshad Shirwadkar 			state->fc_regions[i].len == 0)
18548016e29fSHarshad Shirwadkar 			continue;
18558016e29fSHarshad Shirwadkar 		if (blk >= state->fc_regions[i].pblk &&
18568016e29fSHarshad Shirwadkar 		    blk < state->fc_regions[i].pblk + state->fc_regions[i].len)
18578016e29fSHarshad Shirwadkar 			return true;
18588016e29fSHarshad Shirwadkar 	}
18598016e29fSHarshad Shirwadkar 	return false;
18608016e29fSHarshad Shirwadkar }
18618016e29fSHarshad Shirwadkar 
18628016e29fSHarshad Shirwadkar /* Cleanup function called after replay */
18638016e29fSHarshad Shirwadkar void ext4_fc_replay_cleanup(struct super_block *sb)
18648016e29fSHarshad Shirwadkar {
18658016e29fSHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(sb);
18668016e29fSHarshad Shirwadkar 
18678016e29fSHarshad Shirwadkar 	sbi->s_mount_state &= ~EXT4_FC_REPLAY;
18688016e29fSHarshad Shirwadkar 	kfree(sbi->s_fc_replay_state.fc_regions);
18698016e29fSHarshad Shirwadkar 	kfree(sbi->s_fc_replay_state.fc_modified_inodes);
18708016e29fSHarshad Shirwadkar }
18718016e29fSHarshad Shirwadkar 
18728016e29fSHarshad Shirwadkar /*
18738016e29fSHarshad Shirwadkar  * Recovery Scan phase handler
18748016e29fSHarshad Shirwadkar  *
18758016e29fSHarshad Shirwadkar  * This function is called during the scan phase and is responsible
18768016e29fSHarshad Shirwadkar  * for doing following things:
18778016e29fSHarshad Shirwadkar  * - Make sure the fast commit area has valid tags for replay
18788016e29fSHarshad Shirwadkar  * - Count number of tags that need to be replayed by the replay handler
18798016e29fSHarshad Shirwadkar  * - Verify CRC
18808016e29fSHarshad Shirwadkar  * - Create a list of excluded blocks for allocation during replay phase
18818016e29fSHarshad Shirwadkar  *
18828016e29fSHarshad Shirwadkar  * This function returns JBD2_FC_REPLAY_CONTINUE to indicate that SCAN is
18838016e29fSHarshad Shirwadkar  * incomplete and JBD2 should send more blocks. It returns JBD2_FC_REPLAY_STOP
18848016e29fSHarshad Shirwadkar  * to indicate that scan has finished and JBD2 can now start replay phase.
18858016e29fSHarshad Shirwadkar  * It returns a negative error to indicate that there was an error. At the end
18868016e29fSHarshad Shirwadkar  * of a successful scan phase, sbi->s_fc_replay_state.fc_replay_num_tags is set
18878016e29fSHarshad Shirwadkar  * to indicate the number of tags that need to replayed during the replay phase.
18888016e29fSHarshad Shirwadkar  */
18898016e29fSHarshad Shirwadkar static int ext4_fc_replay_scan(journal_t *journal,
18908016e29fSHarshad Shirwadkar 				struct buffer_head *bh, int off,
18918016e29fSHarshad Shirwadkar 				tid_t expected_tid)
18928016e29fSHarshad Shirwadkar {
18938016e29fSHarshad Shirwadkar 	struct super_block *sb = journal->j_private;
18948016e29fSHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(sb);
18958016e29fSHarshad Shirwadkar 	struct ext4_fc_replay_state *state;
18968016e29fSHarshad Shirwadkar 	int ret = JBD2_FC_REPLAY_CONTINUE;
18978016e29fSHarshad Shirwadkar 	struct ext4_fc_add_range *ext;
18988016e29fSHarshad Shirwadkar 	struct ext4_fc_tl *tl;
18998016e29fSHarshad Shirwadkar 	struct ext4_fc_tail *tail;
19008016e29fSHarshad Shirwadkar 	__u8 *start, *end;
19018016e29fSHarshad Shirwadkar 	struct ext4_fc_head *head;
19028016e29fSHarshad Shirwadkar 	struct ext4_extent *ex;
19038016e29fSHarshad Shirwadkar 
19048016e29fSHarshad Shirwadkar 	state = &sbi->s_fc_replay_state;
19058016e29fSHarshad Shirwadkar 
19068016e29fSHarshad Shirwadkar 	start = (u8 *)bh->b_data;
19078016e29fSHarshad Shirwadkar 	end = (__u8 *)bh->b_data + journal->j_blocksize - 1;
19088016e29fSHarshad Shirwadkar 
19098016e29fSHarshad Shirwadkar 	if (state->fc_replay_expected_off == 0) {
19108016e29fSHarshad Shirwadkar 		state->fc_cur_tag = 0;
19118016e29fSHarshad Shirwadkar 		state->fc_replay_num_tags = 0;
19128016e29fSHarshad Shirwadkar 		state->fc_crc = 0;
19138016e29fSHarshad Shirwadkar 		state->fc_regions = NULL;
19148016e29fSHarshad Shirwadkar 		state->fc_regions_valid = state->fc_regions_used =
19158016e29fSHarshad Shirwadkar 			state->fc_regions_size = 0;
19168016e29fSHarshad Shirwadkar 		/* Check if we can stop early */
19178016e29fSHarshad Shirwadkar 		if (le16_to_cpu(((struct ext4_fc_tl *)start)->fc_tag)
19188016e29fSHarshad Shirwadkar 			!= EXT4_FC_TAG_HEAD)
19198016e29fSHarshad Shirwadkar 			return 0;
19208016e29fSHarshad Shirwadkar 	}
19218016e29fSHarshad Shirwadkar 
19228016e29fSHarshad Shirwadkar 	if (off != state->fc_replay_expected_off) {
19238016e29fSHarshad Shirwadkar 		ret = -EFSCORRUPTED;
19248016e29fSHarshad Shirwadkar 		goto out_err;
19258016e29fSHarshad Shirwadkar 	}
19268016e29fSHarshad Shirwadkar 
19278016e29fSHarshad Shirwadkar 	state->fc_replay_expected_off++;
19288016e29fSHarshad Shirwadkar 	fc_for_each_tl(start, end, tl) {
19298016e29fSHarshad Shirwadkar 		jbd_debug(3, "Scan phase, tag:%s, blk %lld\n",
19308016e29fSHarshad Shirwadkar 			  tag2str(le16_to_cpu(tl->fc_tag)), bh->b_blocknr);
19318016e29fSHarshad Shirwadkar 		switch (le16_to_cpu(tl->fc_tag)) {
19328016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_ADD_RANGE:
19338016e29fSHarshad Shirwadkar 			ext = (struct ext4_fc_add_range *)ext4_fc_tag_val(tl);
19348016e29fSHarshad Shirwadkar 			ex = (struct ext4_extent *)&ext->fc_ex;
19358016e29fSHarshad Shirwadkar 			ret = ext4_fc_record_regions(sb,
19368016e29fSHarshad Shirwadkar 				le32_to_cpu(ext->fc_ino),
19378016e29fSHarshad Shirwadkar 				le32_to_cpu(ex->ee_block), ext4_ext_pblock(ex),
19388016e29fSHarshad Shirwadkar 				ext4_ext_get_actual_len(ex));
19398016e29fSHarshad Shirwadkar 			if (ret < 0)
19408016e29fSHarshad Shirwadkar 				break;
19418016e29fSHarshad Shirwadkar 			ret = JBD2_FC_REPLAY_CONTINUE;
19428016e29fSHarshad Shirwadkar 			fallthrough;
19438016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_DEL_RANGE:
19448016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_LINK:
19458016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_UNLINK:
19468016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_CREAT:
19478016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_INODE:
19488016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_PAD:
19498016e29fSHarshad Shirwadkar 			state->fc_cur_tag++;
19508016e29fSHarshad Shirwadkar 			state->fc_crc = ext4_chksum(sbi, state->fc_crc, tl,
19518016e29fSHarshad Shirwadkar 					sizeof(*tl) + ext4_fc_tag_len(tl));
19528016e29fSHarshad Shirwadkar 			break;
19538016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_TAIL:
19548016e29fSHarshad Shirwadkar 			state->fc_cur_tag++;
19558016e29fSHarshad Shirwadkar 			tail = (struct ext4_fc_tail *)ext4_fc_tag_val(tl);
19568016e29fSHarshad Shirwadkar 			state->fc_crc = ext4_chksum(sbi, state->fc_crc, tl,
19578016e29fSHarshad Shirwadkar 						sizeof(*tl) +
19588016e29fSHarshad Shirwadkar 						offsetof(struct ext4_fc_tail,
19598016e29fSHarshad Shirwadkar 						fc_crc));
19608016e29fSHarshad Shirwadkar 			if (le32_to_cpu(tail->fc_tid) == expected_tid &&
19618016e29fSHarshad Shirwadkar 				le32_to_cpu(tail->fc_crc) == state->fc_crc) {
19628016e29fSHarshad Shirwadkar 				state->fc_replay_num_tags = state->fc_cur_tag;
19638016e29fSHarshad Shirwadkar 				state->fc_regions_valid =
19648016e29fSHarshad Shirwadkar 					state->fc_regions_used;
19658016e29fSHarshad Shirwadkar 			} else {
19668016e29fSHarshad Shirwadkar 				ret = state->fc_replay_num_tags ?
19678016e29fSHarshad Shirwadkar 					JBD2_FC_REPLAY_STOP : -EFSBADCRC;
19688016e29fSHarshad Shirwadkar 			}
19698016e29fSHarshad Shirwadkar 			state->fc_crc = 0;
19708016e29fSHarshad Shirwadkar 			break;
19718016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_HEAD:
19728016e29fSHarshad Shirwadkar 			head = (struct ext4_fc_head *)ext4_fc_tag_val(tl);
19738016e29fSHarshad Shirwadkar 			if (le32_to_cpu(head->fc_features) &
19748016e29fSHarshad Shirwadkar 				~EXT4_FC_SUPPORTED_FEATURES) {
19758016e29fSHarshad Shirwadkar 				ret = -EOPNOTSUPP;
19768016e29fSHarshad Shirwadkar 				break;
19778016e29fSHarshad Shirwadkar 			}
19788016e29fSHarshad Shirwadkar 			if (le32_to_cpu(head->fc_tid) != expected_tid) {
19798016e29fSHarshad Shirwadkar 				ret = JBD2_FC_REPLAY_STOP;
19808016e29fSHarshad Shirwadkar 				break;
19818016e29fSHarshad Shirwadkar 			}
19828016e29fSHarshad Shirwadkar 			state->fc_cur_tag++;
19838016e29fSHarshad Shirwadkar 			state->fc_crc = ext4_chksum(sbi, state->fc_crc, tl,
19848016e29fSHarshad Shirwadkar 					sizeof(*tl) + ext4_fc_tag_len(tl));
19858016e29fSHarshad Shirwadkar 			break;
19868016e29fSHarshad Shirwadkar 		default:
19878016e29fSHarshad Shirwadkar 			ret = state->fc_replay_num_tags ?
19888016e29fSHarshad Shirwadkar 				JBD2_FC_REPLAY_STOP : -ECANCELED;
19898016e29fSHarshad Shirwadkar 		}
19908016e29fSHarshad Shirwadkar 		if (ret < 0 || ret == JBD2_FC_REPLAY_STOP)
19918016e29fSHarshad Shirwadkar 			break;
19928016e29fSHarshad Shirwadkar 	}
19938016e29fSHarshad Shirwadkar 
19948016e29fSHarshad Shirwadkar out_err:
19958016e29fSHarshad Shirwadkar 	trace_ext4_fc_replay_scan(sb, ret, off);
19968016e29fSHarshad Shirwadkar 	return ret;
19978016e29fSHarshad Shirwadkar }
19988016e29fSHarshad Shirwadkar 
19995b849b5fSHarshad Shirwadkar /*
20005b849b5fSHarshad Shirwadkar  * Main recovery path entry point.
20018016e29fSHarshad Shirwadkar  * The meaning of return codes is similar as above.
20025b849b5fSHarshad Shirwadkar  */
20035b849b5fSHarshad Shirwadkar static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh,
20045b849b5fSHarshad Shirwadkar 				enum passtype pass, int off, tid_t expected_tid)
20055b849b5fSHarshad Shirwadkar {
20068016e29fSHarshad Shirwadkar 	struct super_block *sb = journal->j_private;
20078016e29fSHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB(sb);
20088016e29fSHarshad Shirwadkar 	struct ext4_fc_tl *tl;
20098016e29fSHarshad Shirwadkar 	__u8 *start, *end;
20108016e29fSHarshad Shirwadkar 	int ret = JBD2_FC_REPLAY_CONTINUE;
20118016e29fSHarshad Shirwadkar 	struct ext4_fc_replay_state *state = &sbi->s_fc_replay_state;
20128016e29fSHarshad Shirwadkar 	struct ext4_fc_tail *tail;
20138016e29fSHarshad Shirwadkar 
20148016e29fSHarshad Shirwadkar 	if (pass == PASS_SCAN) {
20158016e29fSHarshad Shirwadkar 		state->fc_current_pass = PASS_SCAN;
20168016e29fSHarshad Shirwadkar 		return ext4_fc_replay_scan(journal, bh, off, expected_tid);
20178016e29fSHarshad Shirwadkar 	}
20188016e29fSHarshad Shirwadkar 
20198016e29fSHarshad Shirwadkar 	if (state->fc_current_pass != pass) {
20208016e29fSHarshad Shirwadkar 		state->fc_current_pass = pass;
20218016e29fSHarshad Shirwadkar 		sbi->s_mount_state |= EXT4_FC_REPLAY;
20228016e29fSHarshad Shirwadkar 	}
20238016e29fSHarshad Shirwadkar 	if (!sbi->s_fc_replay_state.fc_replay_num_tags) {
20248016e29fSHarshad Shirwadkar 		jbd_debug(1, "Replay stops\n");
20258016e29fSHarshad Shirwadkar 		ext4_fc_set_bitmaps_and_counters(sb);
20265b849b5fSHarshad Shirwadkar 		return 0;
20275b849b5fSHarshad Shirwadkar 	}
20285b849b5fSHarshad Shirwadkar 
20298016e29fSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
20308016e29fSHarshad Shirwadkar 	if (sbi->s_fc_debug_max_replay && off >= sbi->s_fc_debug_max_replay) {
20318016e29fSHarshad Shirwadkar 		pr_warn("Dropping fc block %d because max_replay set\n", off);
20328016e29fSHarshad Shirwadkar 		return JBD2_FC_REPLAY_STOP;
20338016e29fSHarshad Shirwadkar 	}
20348016e29fSHarshad Shirwadkar #endif
20358016e29fSHarshad Shirwadkar 
20368016e29fSHarshad Shirwadkar 	start = (u8 *)bh->b_data;
20378016e29fSHarshad Shirwadkar 	end = (__u8 *)bh->b_data + journal->j_blocksize - 1;
20388016e29fSHarshad Shirwadkar 
20398016e29fSHarshad Shirwadkar 	fc_for_each_tl(start, end, tl) {
20408016e29fSHarshad Shirwadkar 		if (state->fc_replay_num_tags == 0) {
20418016e29fSHarshad Shirwadkar 			ret = JBD2_FC_REPLAY_STOP;
20428016e29fSHarshad Shirwadkar 			ext4_fc_set_bitmaps_and_counters(sb);
20438016e29fSHarshad Shirwadkar 			break;
20448016e29fSHarshad Shirwadkar 		}
20458016e29fSHarshad Shirwadkar 		jbd_debug(3, "Replay phase, tag:%s\n",
20468016e29fSHarshad Shirwadkar 				tag2str(le16_to_cpu(tl->fc_tag)));
20478016e29fSHarshad Shirwadkar 		state->fc_replay_num_tags--;
20488016e29fSHarshad Shirwadkar 		switch (le16_to_cpu(tl->fc_tag)) {
20498016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_LINK:
20508016e29fSHarshad Shirwadkar 			ret = ext4_fc_replay_link(sb, tl);
20518016e29fSHarshad Shirwadkar 			break;
20528016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_UNLINK:
20538016e29fSHarshad Shirwadkar 			ret = ext4_fc_replay_unlink(sb, tl);
20548016e29fSHarshad Shirwadkar 			break;
20558016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_ADD_RANGE:
20568016e29fSHarshad Shirwadkar 			ret = ext4_fc_replay_add_range(sb, tl);
20578016e29fSHarshad Shirwadkar 			break;
20588016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_CREAT:
20598016e29fSHarshad Shirwadkar 			ret = ext4_fc_replay_create(sb, tl);
20608016e29fSHarshad Shirwadkar 			break;
20618016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_DEL_RANGE:
20628016e29fSHarshad Shirwadkar 			ret = ext4_fc_replay_del_range(sb, tl);
20638016e29fSHarshad Shirwadkar 			break;
20648016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_INODE:
20658016e29fSHarshad Shirwadkar 			ret = ext4_fc_replay_inode(sb, tl);
20668016e29fSHarshad Shirwadkar 			break;
20678016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_PAD:
20688016e29fSHarshad Shirwadkar 			trace_ext4_fc_replay(sb, EXT4_FC_TAG_PAD, 0,
20698016e29fSHarshad Shirwadkar 				ext4_fc_tag_len(tl), 0);
20708016e29fSHarshad Shirwadkar 			break;
20718016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_TAIL:
20728016e29fSHarshad Shirwadkar 			trace_ext4_fc_replay(sb, EXT4_FC_TAG_TAIL, 0,
20738016e29fSHarshad Shirwadkar 				ext4_fc_tag_len(tl), 0);
20748016e29fSHarshad Shirwadkar 			tail = (struct ext4_fc_tail *)ext4_fc_tag_val(tl);
20758016e29fSHarshad Shirwadkar 			WARN_ON(le32_to_cpu(tail->fc_tid) != expected_tid);
20768016e29fSHarshad Shirwadkar 			break;
20778016e29fSHarshad Shirwadkar 		case EXT4_FC_TAG_HEAD:
20788016e29fSHarshad Shirwadkar 			break;
20798016e29fSHarshad Shirwadkar 		default:
20808016e29fSHarshad Shirwadkar 			trace_ext4_fc_replay(sb, le16_to_cpu(tl->fc_tag), 0,
20818016e29fSHarshad Shirwadkar 				ext4_fc_tag_len(tl), 0);
20828016e29fSHarshad Shirwadkar 			ret = -ECANCELED;
20838016e29fSHarshad Shirwadkar 			break;
20848016e29fSHarshad Shirwadkar 		}
20858016e29fSHarshad Shirwadkar 		if (ret < 0)
20868016e29fSHarshad Shirwadkar 			break;
20878016e29fSHarshad Shirwadkar 		ret = JBD2_FC_REPLAY_CONTINUE;
20888016e29fSHarshad Shirwadkar 	}
20898016e29fSHarshad Shirwadkar 	return ret;
20908016e29fSHarshad Shirwadkar }
20918016e29fSHarshad Shirwadkar 
20926866d7b3SHarshad Shirwadkar void ext4_fc_init(struct super_block *sb, journal_t *journal)
20936866d7b3SHarshad Shirwadkar {
20945b849b5fSHarshad Shirwadkar 	/*
20955b849b5fSHarshad Shirwadkar 	 * We set replay callback even if fast commit disabled because we may
20965b849b5fSHarshad Shirwadkar 	 * could still have fast commit blocks that need to be replayed even if
20975b849b5fSHarshad Shirwadkar 	 * fast commit has now been turned off.
20985b849b5fSHarshad Shirwadkar 	 */
20995b849b5fSHarshad Shirwadkar 	journal->j_fc_replay_callback = ext4_fc_replay;
21006866d7b3SHarshad Shirwadkar 	if (!test_opt2(sb, JOURNAL_FAST_COMMIT))
21016866d7b3SHarshad Shirwadkar 		return;
2102ff780b91SHarshad Shirwadkar 	journal->j_fc_cleanup_callback = ext4_fc_cleanup;
21036866d7b3SHarshad Shirwadkar }
2104aa75f4d3SHarshad Shirwadkar 
2105ce8c59d1SHarshad Shirwadkar const char *fc_ineligible_reasons[] = {
2106ce8c59d1SHarshad Shirwadkar 	"Extended attributes changed",
2107ce8c59d1SHarshad Shirwadkar 	"Cross rename",
2108ce8c59d1SHarshad Shirwadkar 	"Journal flag changed",
2109ce8c59d1SHarshad Shirwadkar 	"Insufficient memory",
2110ce8c59d1SHarshad Shirwadkar 	"Swap boot",
2111ce8c59d1SHarshad Shirwadkar 	"Resize",
2112ce8c59d1SHarshad Shirwadkar 	"Dir renamed",
2113ce8c59d1SHarshad Shirwadkar 	"Falloc range op",
2114ce8c59d1SHarshad Shirwadkar 	"FC Commit Failed"
2115ce8c59d1SHarshad Shirwadkar };
2116ce8c59d1SHarshad Shirwadkar 
2117ce8c59d1SHarshad Shirwadkar int ext4_fc_info_show(struct seq_file *seq, void *v)
2118ce8c59d1SHarshad Shirwadkar {
2119ce8c59d1SHarshad Shirwadkar 	struct ext4_sb_info *sbi = EXT4_SB((struct super_block *)seq->private);
2120ce8c59d1SHarshad Shirwadkar 	struct ext4_fc_stats *stats = &sbi->s_fc_stats;
2121ce8c59d1SHarshad Shirwadkar 	int i;
2122ce8c59d1SHarshad Shirwadkar 
2123ce8c59d1SHarshad Shirwadkar 	if (v != SEQ_START_TOKEN)
2124ce8c59d1SHarshad Shirwadkar 		return 0;
2125ce8c59d1SHarshad Shirwadkar 
2126ce8c59d1SHarshad Shirwadkar 	seq_printf(seq,
2127ce8c59d1SHarshad Shirwadkar 		"fc stats:\n%ld commits\n%ld ineligible\n%ld numblks\n%lluus avg_commit_time\n",
2128ce8c59d1SHarshad Shirwadkar 		   stats->fc_num_commits, stats->fc_ineligible_commits,
2129ce8c59d1SHarshad Shirwadkar 		   stats->fc_numblks,
2130ce8c59d1SHarshad Shirwadkar 		   div_u64(sbi->s_fc_avg_commit_time, 1000));
2131ce8c59d1SHarshad Shirwadkar 	seq_puts(seq, "Ineligible reasons:\n");
2132ce8c59d1SHarshad Shirwadkar 	for (i = 0; i < EXT4_FC_REASON_MAX; i++)
2133ce8c59d1SHarshad Shirwadkar 		seq_printf(seq, "\"%s\":\t%d\n", fc_ineligible_reasons[i],
2134ce8c59d1SHarshad Shirwadkar 			stats->fc_ineligible_reason_count[i]);
2135ce8c59d1SHarshad Shirwadkar 
2136ce8c59d1SHarshad Shirwadkar 	return 0;
2137ce8c59d1SHarshad Shirwadkar }
2138ce8c59d1SHarshad Shirwadkar 
2139aa75f4d3SHarshad Shirwadkar int __init ext4_fc_init_dentry_cache(void)
2140aa75f4d3SHarshad Shirwadkar {
2141aa75f4d3SHarshad Shirwadkar 	ext4_fc_dentry_cachep = KMEM_CACHE(ext4_fc_dentry_update,
2142aa75f4d3SHarshad Shirwadkar 					   SLAB_RECLAIM_ACCOUNT);
2143aa75f4d3SHarshad Shirwadkar 
2144aa75f4d3SHarshad Shirwadkar 	if (ext4_fc_dentry_cachep == NULL)
2145aa75f4d3SHarshad Shirwadkar 		return -ENOMEM;
2146aa75f4d3SHarshad Shirwadkar 
2147aa75f4d3SHarshad Shirwadkar 	return 0;
2148aa75f4d3SHarshad Shirwadkar }
2149