1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun /*
4*4882a593Smuzhiyun * fs/ext4/fast_commit.c
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Written by Harshad Shirwadkar <harshadshirwadkar@gmail.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Ext4 fast commits routines.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun #include "ext4.h"
11*4882a593Smuzhiyun #include "ext4_jbd2.h"
12*4882a593Smuzhiyun #include "ext4_extents.h"
13*4882a593Smuzhiyun #include "mballoc.h"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun * Ext4 Fast Commits
17*4882a593Smuzhiyun * -----------------
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * Ext4 fast commits implement fine grained journalling for Ext4.
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * Fast commits are organized as a log of tag-length-value (TLV) structs. (See
22*4882a593Smuzhiyun * struct ext4_fc_tl). Each TLV contains some delta that is replayed TLV by
23*4882a593Smuzhiyun * TLV during the recovery phase. For the scenarios for which we currently
24*4882a593Smuzhiyun * don't have replay code, fast commit falls back to full commits.
25*4882a593Smuzhiyun * Fast commits record delta in one of the following three categories.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * (A) Directory entry updates:
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * - EXT4_FC_TAG_UNLINK - records directory entry unlink
30*4882a593Smuzhiyun * - EXT4_FC_TAG_LINK - records directory entry link
31*4882a593Smuzhiyun * - EXT4_FC_TAG_CREAT - records inode and directory entry creation
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun * (B) File specific data range updates:
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * - EXT4_FC_TAG_ADD_RANGE - records addition of new blocks to an inode
36*4882a593Smuzhiyun * - EXT4_FC_TAG_DEL_RANGE - records deletion of blocks from an inode
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * (C) Inode metadata (mtime / ctime etc):
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * - EXT4_FC_TAG_INODE - record the inode that should be replayed
41*4882a593Smuzhiyun * during recovery. Note that iblocks field is
42*4882a593Smuzhiyun * not replayed and instead derived during
43*4882a593Smuzhiyun * replay.
44*4882a593Smuzhiyun * Commit Operation
45*4882a593Smuzhiyun * ----------------
46*4882a593Smuzhiyun * With fast commits, we maintain all the directory entry operations in the
47*4882a593Smuzhiyun * order in which they are issued in an in-memory queue. This queue is flushed
48*4882a593Smuzhiyun * to disk during the commit operation. We also maintain a list of inodes
49*4882a593Smuzhiyun * that need to be committed during a fast commit in another in memory queue of
50*4882a593Smuzhiyun * inodes. During the commit operation, we commit in the following order:
51*4882a593Smuzhiyun *
52*4882a593Smuzhiyun * [1] Lock inodes for any further data updates by setting COMMITTING state
53*4882a593Smuzhiyun * [2] Submit data buffers of all the inodes
54*4882a593Smuzhiyun * [3] Wait for [2] to complete
55*4882a593Smuzhiyun * [4] Commit all the directory entry updates in the fast commit space
56*4882a593Smuzhiyun * [5] Commit all the changed inode structures
57*4882a593Smuzhiyun * [6] Write tail tag (this tag ensures the atomicity, please read the following
58*4882a593Smuzhiyun * section for more details).
59*4882a593Smuzhiyun * [7] Wait for [4], [5] and [6] to complete.
60*4882a593Smuzhiyun *
61*4882a593Smuzhiyun * All the inode updates must call ext4_fc_start_update() before starting an
62*4882a593Smuzhiyun * update. If such an ongoing update is present, fast commit waits for it to
63*4882a593Smuzhiyun * complete. The completion of such an update is marked by
64*4882a593Smuzhiyun * ext4_fc_stop_update().
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * Fast Commit Ineligibility
67*4882a593Smuzhiyun * -------------------------
68*4882a593Smuzhiyun * Not all operations are supported by fast commits today (e.g extended
69*4882a593Smuzhiyun * attributes). Fast commit ineligiblity is marked by calling one of the
70*4882a593Smuzhiyun * two following functions:
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun * - ext4_fc_mark_ineligible(): This makes next fast commit operation to fall
73*4882a593Smuzhiyun * back to full commit. This is useful in case of transient errors.
74*4882a593Smuzhiyun *
75*4882a593Smuzhiyun * - ext4_fc_start_ineligible() and ext4_fc_stop_ineligible() - This makes all
76*4882a593Smuzhiyun * the fast commits happening between ext4_fc_start_ineligible() and
77*4882a593Smuzhiyun * ext4_fc_stop_ineligible() and one fast commit after the call to
78*4882a593Smuzhiyun * ext4_fc_stop_ineligible() to fall back to full commits. It is important to
79*4882a593Smuzhiyun * make one more fast commit to fall back to full commit after stop call so
80*4882a593Smuzhiyun * that it guaranteed that the fast commit ineligible operation contained
81*4882a593Smuzhiyun * within ext4_fc_start_ineligible() and ext4_fc_stop_ineligible() is
82*4882a593Smuzhiyun * followed by at least 1 full commit.
83*4882a593Smuzhiyun *
84*4882a593Smuzhiyun * Atomicity of commits
85*4882a593Smuzhiyun * --------------------
86*4882a593Smuzhiyun * In order to guarantee atomicity during the commit operation, fast commit
87*4882a593Smuzhiyun * uses "EXT4_FC_TAG_TAIL" tag that marks a fast commit as complete. Tail
88*4882a593Smuzhiyun * tag contains CRC of the contents and TID of the transaction after which
89*4882a593Smuzhiyun * this fast commit should be applied. Recovery code replays fast commit
90*4882a593Smuzhiyun * logs only if there's at least 1 valid tail present. For every fast commit
91*4882a593Smuzhiyun * operation, there is 1 tail. This means, we may end up with multiple tails
92*4882a593Smuzhiyun * in the fast commit space. Here's an example:
93*4882a593Smuzhiyun *
94*4882a593Smuzhiyun * - Create a new file A and remove existing file B
95*4882a593Smuzhiyun * - fsync()
96*4882a593Smuzhiyun * - Append contents to file A
97*4882a593Smuzhiyun * - Truncate file A
98*4882a593Smuzhiyun * - fsync()
99*4882a593Smuzhiyun *
100*4882a593Smuzhiyun * The fast commit space at the end of above operations would look like this:
101*4882a593Smuzhiyun * [HEAD] [CREAT A] [UNLINK B] [TAIL] [ADD_RANGE A] [DEL_RANGE A] [TAIL]
102*4882a593Smuzhiyun * |<--- Fast Commit 1 --->|<--- Fast Commit 2 ---->|
103*4882a593Smuzhiyun *
104*4882a593Smuzhiyun * Replay code should thus check for all the valid tails in the FC area.
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * TODOs
107*4882a593Smuzhiyun * -----
108*4882a593Smuzhiyun * 1) Make fast commit atomic updates more fine grained. Today, a fast commit
109*4882a593Smuzhiyun * eligible update must be protected within ext4_fc_start_update() and
110*4882a593Smuzhiyun * ext4_fc_stop_update(). These routines are called at much higher
111*4882a593Smuzhiyun * routines. This can be made more fine grained by combining with
112*4882a593Smuzhiyun * ext4_journal_start().
113*4882a593Smuzhiyun *
114*4882a593Smuzhiyun * 2) Same above for ext4_fc_start_ineligible() and ext4_fc_stop_ineligible()
115*4882a593Smuzhiyun *
116*4882a593Smuzhiyun * 3) Handle more ineligible cases.
117*4882a593Smuzhiyun */
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun #include <trace/events/ext4.h>
120*4882a593Smuzhiyun static struct kmem_cache *ext4_fc_dentry_cachep;
121*4882a593Smuzhiyun
ext4_end_buffer_io_sync(struct buffer_head * bh,int uptodate)122*4882a593Smuzhiyun static void ext4_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun BUFFER_TRACE(bh, "");
125*4882a593Smuzhiyun if (uptodate) {
126*4882a593Smuzhiyun ext4_debug("%s: Block %lld up-to-date",
127*4882a593Smuzhiyun __func__, bh->b_blocknr);
128*4882a593Smuzhiyun set_buffer_uptodate(bh);
129*4882a593Smuzhiyun } else {
130*4882a593Smuzhiyun ext4_debug("%s: Block %lld not up-to-date",
131*4882a593Smuzhiyun __func__, bh->b_blocknr);
132*4882a593Smuzhiyun clear_buffer_uptodate(bh);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun unlock_buffer(bh);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
ext4_fc_reset_inode(struct inode * inode)138*4882a593Smuzhiyun static inline void ext4_fc_reset_inode(struct inode *inode)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun struct ext4_inode_info *ei = EXT4_I(inode);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun ei->i_fc_lblk_start = 0;
143*4882a593Smuzhiyun ei->i_fc_lblk_len = 0;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
ext4_fc_init_inode(struct inode * inode)146*4882a593Smuzhiyun void ext4_fc_init_inode(struct inode *inode)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun struct ext4_inode_info *ei = EXT4_I(inode);
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun ext4_fc_reset_inode(inode);
151*4882a593Smuzhiyun ext4_clear_inode_state(inode, EXT4_STATE_FC_COMMITTING);
152*4882a593Smuzhiyun INIT_LIST_HEAD(&ei->i_fc_list);
153*4882a593Smuzhiyun init_waitqueue_head(&ei->i_fc_wait);
154*4882a593Smuzhiyun atomic_set(&ei->i_fc_updates, 0);
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /* This function must be called with sbi->s_fc_lock held. */
ext4_fc_wait_committing_inode(struct inode * inode)158*4882a593Smuzhiyun static void ext4_fc_wait_committing_inode(struct inode *inode)
159*4882a593Smuzhiyun __releases(&EXT4_SB(inode->i_sb)->s_fc_lock)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun wait_queue_head_t *wq;
162*4882a593Smuzhiyun struct ext4_inode_info *ei = EXT4_I(inode);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun #if (BITS_PER_LONG < 64)
165*4882a593Smuzhiyun DEFINE_WAIT_BIT(wait, &ei->i_state_flags,
166*4882a593Smuzhiyun EXT4_STATE_FC_COMMITTING);
167*4882a593Smuzhiyun wq = bit_waitqueue(&ei->i_state_flags,
168*4882a593Smuzhiyun EXT4_STATE_FC_COMMITTING);
169*4882a593Smuzhiyun #else
170*4882a593Smuzhiyun DEFINE_WAIT_BIT(wait, &ei->i_flags,
171*4882a593Smuzhiyun EXT4_STATE_FC_COMMITTING);
172*4882a593Smuzhiyun wq = bit_waitqueue(&ei->i_flags,
173*4882a593Smuzhiyun EXT4_STATE_FC_COMMITTING);
174*4882a593Smuzhiyun #endif
175*4882a593Smuzhiyun lockdep_assert_held(&EXT4_SB(inode->i_sb)->s_fc_lock);
176*4882a593Smuzhiyun prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
177*4882a593Smuzhiyun spin_unlock(&EXT4_SB(inode->i_sb)->s_fc_lock);
178*4882a593Smuzhiyun schedule();
179*4882a593Smuzhiyun finish_wait(wq, &wait.wq_entry);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /*
183*4882a593Smuzhiyun * Inform Ext4's fast about start of an inode update
184*4882a593Smuzhiyun *
185*4882a593Smuzhiyun * This function is called by the high level call VFS callbacks before
186*4882a593Smuzhiyun * performing any inode update. This function blocks if there's an ongoing
187*4882a593Smuzhiyun * fast commit on the inode in question.
188*4882a593Smuzhiyun */
ext4_fc_start_update(struct inode * inode)189*4882a593Smuzhiyun void ext4_fc_start_update(struct inode *inode)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun struct ext4_inode_info *ei = EXT4_I(inode);
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun if (!test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT) ||
194*4882a593Smuzhiyun (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY))
195*4882a593Smuzhiyun return;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun restart:
198*4882a593Smuzhiyun spin_lock(&EXT4_SB(inode->i_sb)->s_fc_lock);
199*4882a593Smuzhiyun if (list_empty(&ei->i_fc_list))
200*4882a593Smuzhiyun goto out;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun if (ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING)) {
203*4882a593Smuzhiyun ext4_fc_wait_committing_inode(inode);
204*4882a593Smuzhiyun goto restart;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun out:
207*4882a593Smuzhiyun atomic_inc(&ei->i_fc_updates);
208*4882a593Smuzhiyun spin_unlock(&EXT4_SB(inode->i_sb)->s_fc_lock);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /*
212*4882a593Smuzhiyun * Stop inode update and wake up waiting fast commits if any.
213*4882a593Smuzhiyun */
ext4_fc_stop_update(struct inode * inode)214*4882a593Smuzhiyun void ext4_fc_stop_update(struct inode *inode)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun struct ext4_inode_info *ei = EXT4_I(inode);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun if (!test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT) ||
219*4882a593Smuzhiyun (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY))
220*4882a593Smuzhiyun return;
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun if (atomic_dec_and_test(&ei->i_fc_updates))
223*4882a593Smuzhiyun wake_up_all(&ei->i_fc_wait);
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun * Remove inode from fast commit list. If the inode is being committed
228*4882a593Smuzhiyun * we wait until inode commit is done.
229*4882a593Smuzhiyun */
ext4_fc_del(struct inode * inode)230*4882a593Smuzhiyun void ext4_fc_del(struct inode *inode)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun struct ext4_inode_info *ei = EXT4_I(inode);
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun if (!test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT) ||
235*4882a593Smuzhiyun (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY))
236*4882a593Smuzhiyun return;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun restart:
239*4882a593Smuzhiyun spin_lock(&EXT4_SB(inode->i_sb)->s_fc_lock);
240*4882a593Smuzhiyun if (list_empty(&ei->i_fc_list)) {
241*4882a593Smuzhiyun spin_unlock(&EXT4_SB(inode->i_sb)->s_fc_lock);
242*4882a593Smuzhiyun return;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun if (ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING)) {
246*4882a593Smuzhiyun ext4_fc_wait_committing_inode(inode);
247*4882a593Smuzhiyun goto restart;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun list_del_init(&ei->i_fc_list);
250*4882a593Smuzhiyun spin_unlock(&EXT4_SB(inode->i_sb)->s_fc_lock);
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun /*
254*4882a593Smuzhiyun * Mark file system as fast commit ineligible. This means that next commit
255*4882a593Smuzhiyun * operation would result in a full jbd2 commit.
256*4882a593Smuzhiyun */
ext4_fc_mark_ineligible(struct super_block * sb,int reason)257*4882a593Smuzhiyun void ext4_fc_mark_ineligible(struct super_block *sb, int reason)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(sb);
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun if (!test_opt2(sb, JOURNAL_FAST_COMMIT) ||
262*4882a593Smuzhiyun (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))
263*4882a593Smuzhiyun return;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun ext4_set_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
266*4882a593Smuzhiyun WARN_ON(reason >= EXT4_FC_REASON_MAX);
267*4882a593Smuzhiyun sbi->s_fc_stats.fc_ineligible_reason_count[reason]++;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun /*
271*4882a593Smuzhiyun * Start a fast commit ineligible update. Any commits that happen while
272*4882a593Smuzhiyun * such an operation is in progress fall back to full commits.
273*4882a593Smuzhiyun */
ext4_fc_start_ineligible(struct super_block * sb,int reason)274*4882a593Smuzhiyun void ext4_fc_start_ineligible(struct super_block *sb, int reason)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(sb);
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun if (!test_opt2(sb, JOURNAL_FAST_COMMIT) ||
279*4882a593Smuzhiyun (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))
280*4882a593Smuzhiyun return;
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun WARN_ON(reason >= EXT4_FC_REASON_MAX);
283*4882a593Smuzhiyun sbi->s_fc_stats.fc_ineligible_reason_count[reason]++;
284*4882a593Smuzhiyun atomic_inc(&sbi->s_fc_ineligible_updates);
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /*
288*4882a593Smuzhiyun * Stop a fast commit ineligible update. We set EXT4_MF_FC_INELIGIBLE flag here
289*4882a593Smuzhiyun * to ensure that after stopping the ineligible update, at least one full
290*4882a593Smuzhiyun * commit takes place.
291*4882a593Smuzhiyun */
ext4_fc_stop_ineligible(struct super_block * sb)292*4882a593Smuzhiyun void ext4_fc_stop_ineligible(struct super_block *sb)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun if (!test_opt2(sb, JOURNAL_FAST_COMMIT) ||
295*4882a593Smuzhiyun (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))
296*4882a593Smuzhiyun return;
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun ext4_set_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
299*4882a593Smuzhiyun atomic_dec(&EXT4_SB(sb)->s_fc_ineligible_updates);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
ext4_fc_is_ineligible(struct super_block * sb)302*4882a593Smuzhiyun static inline int ext4_fc_is_ineligible(struct super_block *sb)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun return (ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE) ||
305*4882a593Smuzhiyun atomic_read(&EXT4_SB(sb)->s_fc_ineligible_updates));
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun /*
309*4882a593Smuzhiyun * Generic fast commit tracking function. If this is the first time this we are
310*4882a593Smuzhiyun * called after a full commit, we initialize fast commit fields and then call
311*4882a593Smuzhiyun * __fc_track_fn() with update = 0. If we have already been called after a full
312*4882a593Smuzhiyun * commit, we pass update = 1. Based on that, the track function can determine
313*4882a593Smuzhiyun * if it needs to track a field for the first time or if it needs to just
314*4882a593Smuzhiyun * update the previously tracked value.
315*4882a593Smuzhiyun *
316*4882a593Smuzhiyun * If enqueue is set, this function enqueues the inode in fast commit list.
317*4882a593Smuzhiyun */
ext4_fc_track_template(handle_t * handle,struct inode * inode,int (* __fc_track_fn)(struct inode *,void *,bool),void * args,int enqueue)318*4882a593Smuzhiyun static int ext4_fc_track_template(
319*4882a593Smuzhiyun handle_t *handle, struct inode *inode,
320*4882a593Smuzhiyun int (*__fc_track_fn)(struct inode *, void *, bool),
321*4882a593Smuzhiyun void *args, int enqueue)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun bool update = false;
324*4882a593Smuzhiyun struct ext4_inode_info *ei = EXT4_I(inode);
325*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
326*4882a593Smuzhiyun tid_t tid = 0;
327*4882a593Smuzhiyun int ret;
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun if (!test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT) ||
330*4882a593Smuzhiyun (sbi->s_mount_state & EXT4_FC_REPLAY))
331*4882a593Smuzhiyun return -EOPNOTSUPP;
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun if (ext4_fc_is_ineligible(inode->i_sb))
334*4882a593Smuzhiyun return -EINVAL;
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun tid = handle->h_transaction->t_tid;
337*4882a593Smuzhiyun mutex_lock(&ei->i_fc_lock);
338*4882a593Smuzhiyun if (tid == ei->i_sync_tid) {
339*4882a593Smuzhiyun update = true;
340*4882a593Smuzhiyun } else {
341*4882a593Smuzhiyun ext4_fc_reset_inode(inode);
342*4882a593Smuzhiyun ei->i_sync_tid = tid;
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun ret = __fc_track_fn(inode, args, update);
345*4882a593Smuzhiyun mutex_unlock(&ei->i_fc_lock);
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun if (!enqueue)
348*4882a593Smuzhiyun return ret;
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
351*4882a593Smuzhiyun if (list_empty(&EXT4_I(inode)->i_fc_list))
352*4882a593Smuzhiyun list_add_tail(&EXT4_I(inode)->i_fc_list,
353*4882a593Smuzhiyun (ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_COMMITTING)) ?
354*4882a593Smuzhiyun &sbi->s_fc_q[FC_Q_STAGING] :
355*4882a593Smuzhiyun &sbi->s_fc_q[FC_Q_MAIN]);
356*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun return ret;
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun struct __track_dentry_update_args {
362*4882a593Smuzhiyun struct dentry *dentry;
363*4882a593Smuzhiyun int op;
364*4882a593Smuzhiyun };
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun /* __track_fn for directory entry updates. Called with ei->i_fc_lock. */
__track_dentry_update(struct inode * inode,void * arg,bool update)367*4882a593Smuzhiyun static int __track_dentry_update(struct inode *inode, void *arg, bool update)
368*4882a593Smuzhiyun {
369*4882a593Smuzhiyun struct ext4_fc_dentry_update *node;
370*4882a593Smuzhiyun struct ext4_inode_info *ei = EXT4_I(inode);
371*4882a593Smuzhiyun struct __track_dentry_update_args *dentry_update =
372*4882a593Smuzhiyun (struct __track_dentry_update_args *)arg;
373*4882a593Smuzhiyun struct dentry *dentry = dentry_update->dentry;
374*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun mutex_unlock(&ei->i_fc_lock);
377*4882a593Smuzhiyun node = kmem_cache_alloc(ext4_fc_dentry_cachep, GFP_NOFS);
378*4882a593Smuzhiyun if (!node) {
379*4882a593Smuzhiyun ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_NOMEM);
380*4882a593Smuzhiyun mutex_lock(&ei->i_fc_lock);
381*4882a593Smuzhiyun return -ENOMEM;
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun node->fcd_op = dentry_update->op;
385*4882a593Smuzhiyun node->fcd_parent = dentry->d_parent->d_inode->i_ino;
386*4882a593Smuzhiyun node->fcd_ino = inode->i_ino;
387*4882a593Smuzhiyun if (dentry->d_name.len > DNAME_INLINE_LEN) {
388*4882a593Smuzhiyun node->fcd_name.name = kmalloc(dentry->d_name.len, GFP_NOFS);
389*4882a593Smuzhiyun if (!node->fcd_name.name) {
390*4882a593Smuzhiyun kmem_cache_free(ext4_fc_dentry_cachep, node);
391*4882a593Smuzhiyun ext4_fc_mark_ineligible(inode->i_sb,
392*4882a593Smuzhiyun EXT4_FC_REASON_NOMEM);
393*4882a593Smuzhiyun mutex_lock(&ei->i_fc_lock);
394*4882a593Smuzhiyun return -ENOMEM;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun memcpy((u8 *)node->fcd_name.name, dentry->d_name.name,
397*4882a593Smuzhiyun dentry->d_name.len);
398*4882a593Smuzhiyun } else {
399*4882a593Smuzhiyun memcpy(node->fcd_iname, dentry->d_name.name,
400*4882a593Smuzhiyun dentry->d_name.len);
401*4882a593Smuzhiyun node->fcd_name.name = node->fcd_iname;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun node->fcd_name.len = dentry->d_name.len;
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
406*4882a593Smuzhiyun if (ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_COMMITTING))
407*4882a593Smuzhiyun list_add_tail(&node->fcd_list,
408*4882a593Smuzhiyun &sbi->s_fc_dentry_q[FC_Q_STAGING]);
409*4882a593Smuzhiyun else
410*4882a593Smuzhiyun list_add_tail(&node->fcd_list, &sbi->s_fc_dentry_q[FC_Q_MAIN]);
411*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
412*4882a593Smuzhiyun mutex_lock(&ei->i_fc_lock);
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun return 0;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
__ext4_fc_track_unlink(handle_t * handle,struct inode * inode,struct dentry * dentry)417*4882a593Smuzhiyun void __ext4_fc_track_unlink(handle_t *handle,
418*4882a593Smuzhiyun struct inode *inode, struct dentry *dentry)
419*4882a593Smuzhiyun {
420*4882a593Smuzhiyun struct __track_dentry_update_args args;
421*4882a593Smuzhiyun int ret;
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun args.dentry = dentry;
424*4882a593Smuzhiyun args.op = EXT4_FC_TAG_UNLINK;
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun ret = ext4_fc_track_template(handle, inode, __track_dentry_update,
427*4882a593Smuzhiyun (void *)&args, 0);
428*4882a593Smuzhiyun trace_ext4_fc_track_unlink(inode, dentry, ret);
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun
ext4_fc_track_unlink(handle_t * handle,struct dentry * dentry)431*4882a593Smuzhiyun void ext4_fc_track_unlink(handle_t *handle, struct dentry *dentry)
432*4882a593Smuzhiyun {
433*4882a593Smuzhiyun __ext4_fc_track_unlink(handle, d_inode(dentry), dentry);
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun
__ext4_fc_track_link(handle_t * handle,struct inode * inode,struct dentry * dentry)436*4882a593Smuzhiyun void __ext4_fc_track_link(handle_t *handle,
437*4882a593Smuzhiyun struct inode *inode, struct dentry *dentry)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun struct __track_dentry_update_args args;
440*4882a593Smuzhiyun int ret;
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun args.dentry = dentry;
443*4882a593Smuzhiyun args.op = EXT4_FC_TAG_LINK;
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun ret = ext4_fc_track_template(handle, inode, __track_dentry_update,
446*4882a593Smuzhiyun (void *)&args, 0);
447*4882a593Smuzhiyun trace_ext4_fc_track_link(inode, dentry, ret);
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun
ext4_fc_track_link(handle_t * handle,struct dentry * dentry)450*4882a593Smuzhiyun void ext4_fc_track_link(handle_t *handle, struct dentry *dentry)
451*4882a593Smuzhiyun {
452*4882a593Smuzhiyun __ext4_fc_track_link(handle, d_inode(dentry), dentry);
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun
__ext4_fc_track_create(handle_t * handle,struct inode * inode,struct dentry * dentry)455*4882a593Smuzhiyun void __ext4_fc_track_create(handle_t *handle, struct inode *inode,
456*4882a593Smuzhiyun struct dentry *dentry)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun struct __track_dentry_update_args args;
459*4882a593Smuzhiyun int ret;
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun args.dentry = dentry;
462*4882a593Smuzhiyun args.op = EXT4_FC_TAG_CREAT;
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun ret = ext4_fc_track_template(handle, inode, __track_dentry_update,
465*4882a593Smuzhiyun (void *)&args, 0);
466*4882a593Smuzhiyun trace_ext4_fc_track_create(inode, dentry, ret);
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun
ext4_fc_track_create(handle_t * handle,struct dentry * dentry)469*4882a593Smuzhiyun void ext4_fc_track_create(handle_t *handle, struct dentry *dentry)
470*4882a593Smuzhiyun {
471*4882a593Smuzhiyun __ext4_fc_track_create(handle, d_inode(dentry), dentry);
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun /* __track_fn for inode tracking */
__track_inode(struct inode * inode,void * arg,bool update)475*4882a593Smuzhiyun static int __track_inode(struct inode *inode, void *arg, bool update)
476*4882a593Smuzhiyun {
477*4882a593Smuzhiyun if (update)
478*4882a593Smuzhiyun return -EEXIST;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun EXT4_I(inode)->i_fc_lblk_len = 0;
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun return 0;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun
ext4_fc_track_inode(handle_t * handle,struct inode * inode)485*4882a593Smuzhiyun void ext4_fc_track_inode(handle_t *handle, struct inode *inode)
486*4882a593Smuzhiyun {
487*4882a593Smuzhiyun int ret;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun if (S_ISDIR(inode->i_mode))
490*4882a593Smuzhiyun return;
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun if (ext4_should_journal_data(inode)) {
493*4882a593Smuzhiyun ext4_fc_mark_ineligible(inode->i_sb,
494*4882a593Smuzhiyun EXT4_FC_REASON_INODE_JOURNAL_DATA);
495*4882a593Smuzhiyun return;
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun ret = ext4_fc_track_template(handle, inode, __track_inode, NULL, 1);
499*4882a593Smuzhiyun trace_ext4_fc_track_inode(inode, ret);
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun struct __track_range_args {
503*4882a593Smuzhiyun ext4_lblk_t start, end;
504*4882a593Smuzhiyun };
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun /* __track_fn for tracking data updates */
__track_range(struct inode * inode,void * arg,bool update)507*4882a593Smuzhiyun static int __track_range(struct inode *inode, void *arg, bool update)
508*4882a593Smuzhiyun {
509*4882a593Smuzhiyun struct ext4_inode_info *ei = EXT4_I(inode);
510*4882a593Smuzhiyun ext4_lblk_t oldstart;
511*4882a593Smuzhiyun struct __track_range_args *__arg =
512*4882a593Smuzhiyun (struct __track_range_args *)arg;
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun if (inode->i_ino < EXT4_FIRST_INO(inode->i_sb)) {
515*4882a593Smuzhiyun ext4_debug("Special inode %ld being modified\n", inode->i_ino);
516*4882a593Smuzhiyun return -ECANCELED;
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun oldstart = ei->i_fc_lblk_start;
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun if (update && ei->i_fc_lblk_len > 0) {
522*4882a593Smuzhiyun ei->i_fc_lblk_start = min(ei->i_fc_lblk_start, __arg->start);
523*4882a593Smuzhiyun ei->i_fc_lblk_len =
524*4882a593Smuzhiyun max(oldstart + ei->i_fc_lblk_len - 1, __arg->end) -
525*4882a593Smuzhiyun ei->i_fc_lblk_start + 1;
526*4882a593Smuzhiyun } else {
527*4882a593Smuzhiyun ei->i_fc_lblk_start = __arg->start;
528*4882a593Smuzhiyun ei->i_fc_lblk_len = __arg->end - __arg->start + 1;
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun return 0;
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun
ext4_fc_track_range(handle_t * handle,struct inode * inode,ext4_lblk_t start,ext4_lblk_t end)534*4882a593Smuzhiyun void ext4_fc_track_range(handle_t *handle, struct inode *inode, ext4_lblk_t start,
535*4882a593Smuzhiyun ext4_lblk_t end)
536*4882a593Smuzhiyun {
537*4882a593Smuzhiyun struct __track_range_args args;
538*4882a593Smuzhiyun int ret;
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun if (S_ISDIR(inode->i_mode))
541*4882a593Smuzhiyun return;
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun args.start = start;
544*4882a593Smuzhiyun args.end = end;
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun ret = ext4_fc_track_template(handle, inode, __track_range, &args, 1);
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun trace_ext4_fc_track_range(inode, start, end, ret);
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun
ext4_fc_submit_bh(struct super_block * sb)551*4882a593Smuzhiyun static void ext4_fc_submit_bh(struct super_block *sb)
552*4882a593Smuzhiyun {
553*4882a593Smuzhiyun int write_flags = REQ_SYNC;
554*4882a593Smuzhiyun struct buffer_head *bh = EXT4_SB(sb)->s_fc_bh;
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun /* TODO: REQ_FUA | REQ_PREFLUSH is unnecessarily expensive. */
557*4882a593Smuzhiyun if (test_opt(sb, BARRIER))
558*4882a593Smuzhiyun write_flags |= REQ_FUA | REQ_PREFLUSH;
559*4882a593Smuzhiyun lock_buffer(bh);
560*4882a593Smuzhiyun set_buffer_dirty(bh);
561*4882a593Smuzhiyun set_buffer_uptodate(bh);
562*4882a593Smuzhiyun bh->b_end_io = ext4_end_buffer_io_sync;
563*4882a593Smuzhiyun submit_bh(REQ_OP_WRITE, write_flags, bh);
564*4882a593Smuzhiyun EXT4_SB(sb)->s_fc_bh = NULL;
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun /* Ext4 commit path routines */
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun /* memzero and update CRC */
ext4_fc_memzero(struct super_block * sb,void * dst,int len,u32 * crc)570*4882a593Smuzhiyun static void *ext4_fc_memzero(struct super_block *sb, void *dst, int len,
571*4882a593Smuzhiyun u32 *crc)
572*4882a593Smuzhiyun {
573*4882a593Smuzhiyun void *ret;
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun ret = memset(dst, 0, len);
576*4882a593Smuzhiyun if (crc)
577*4882a593Smuzhiyun *crc = ext4_chksum(EXT4_SB(sb), *crc, dst, len);
578*4882a593Smuzhiyun return ret;
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun /*
582*4882a593Smuzhiyun * Allocate len bytes on a fast commit buffer.
583*4882a593Smuzhiyun *
584*4882a593Smuzhiyun * During the commit time this function is used to manage fast commit
585*4882a593Smuzhiyun * block space. We don't split a fast commit log onto different
586*4882a593Smuzhiyun * blocks. So this function makes sure that if there's not enough space
587*4882a593Smuzhiyun * on the current block, the remaining space in the current block is
588*4882a593Smuzhiyun * marked as unused by adding EXT4_FC_TAG_PAD tag. In that case,
589*4882a593Smuzhiyun * new block is from jbd2 and CRC is updated to reflect the padding
590*4882a593Smuzhiyun * we added.
591*4882a593Smuzhiyun */
ext4_fc_reserve_space(struct super_block * sb,int len,u32 * crc)592*4882a593Smuzhiyun static u8 *ext4_fc_reserve_space(struct super_block *sb, int len, u32 *crc)
593*4882a593Smuzhiyun {
594*4882a593Smuzhiyun struct ext4_fc_tl *tl;
595*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(sb);
596*4882a593Smuzhiyun struct buffer_head *bh;
597*4882a593Smuzhiyun int bsize = sbi->s_journal->j_blocksize;
598*4882a593Smuzhiyun int ret, off = sbi->s_fc_bytes % bsize;
599*4882a593Smuzhiyun int pad_len;
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun /*
602*4882a593Smuzhiyun * After allocating len, we should have space at least for a 0 byte
603*4882a593Smuzhiyun * padding.
604*4882a593Smuzhiyun */
605*4882a593Smuzhiyun if (len + sizeof(struct ext4_fc_tl) > bsize)
606*4882a593Smuzhiyun return NULL;
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun if (bsize - off - 1 > len + sizeof(struct ext4_fc_tl)) {
609*4882a593Smuzhiyun /*
610*4882a593Smuzhiyun * Only allocate from current buffer if we have enough space for
611*4882a593Smuzhiyun * this request AND we have space to add a zero byte padding.
612*4882a593Smuzhiyun */
613*4882a593Smuzhiyun if (!sbi->s_fc_bh) {
614*4882a593Smuzhiyun ret = jbd2_fc_get_buf(EXT4_SB(sb)->s_journal, &bh);
615*4882a593Smuzhiyun if (ret)
616*4882a593Smuzhiyun return NULL;
617*4882a593Smuzhiyun sbi->s_fc_bh = bh;
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun sbi->s_fc_bytes += len;
620*4882a593Smuzhiyun return sbi->s_fc_bh->b_data + off;
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun /* Need to add PAD tag */
623*4882a593Smuzhiyun tl = (struct ext4_fc_tl *)(sbi->s_fc_bh->b_data + off);
624*4882a593Smuzhiyun tl->fc_tag = cpu_to_le16(EXT4_FC_TAG_PAD);
625*4882a593Smuzhiyun pad_len = bsize - off - 1 - sizeof(struct ext4_fc_tl);
626*4882a593Smuzhiyun tl->fc_len = cpu_to_le16(pad_len);
627*4882a593Smuzhiyun if (crc)
628*4882a593Smuzhiyun *crc = ext4_chksum(sbi, *crc, tl, sizeof(*tl));
629*4882a593Smuzhiyun if (pad_len > 0)
630*4882a593Smuzhiyun ext4_fc_memzero(sb, tl + 1, pad_len, crc);
631*4882a593Smuzhiyun ext4_fc_submit_bh(sb);
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun ret = jbd2_fc_get_buf(EXT4_SB(sb)->s_journal, &bh);
634*4882a593Smuzhiyun if (ret)
635*4882a593Smuzhiyun return NULL;
636*4882a593Smuzhiyun sbi->s_fc_bh = bh;
637*4882a593Smuzhiyun sbi->s_fc_bytes = (sbi->s_fc_bytes / bsize + 1) * bsize + len;
638*4882a593Smuzhiyun return sbi->s_fc_bh->b_data;
639*4882a593Smuzhiyun }
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun /* memcpy to fc reserved space and update CRC */
ext4_fc_memcpy(struct super_block * sb,void * dst,const void * src,int len,u32 * crc)642*4882a593Smuzhiyun static void *ext4_fc_memcpy(struct super_block *sb, void *dst, const void *src,
643*4882a593Smuzhiyun int len, u32 *crc)
644*4882a593Smuzhiyun {
645*4882a593Smuzhiyun if (crc)
646*4882a593Smuzhiyun *crc = ext4_chksum(EXT4_SB(sb), *crc, src, len);
647*4882a593Smuzhiyun return memcpy(dst, src, len);
648*4882a593Smuzhiyun }
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun /*
651*4882a593Smuzhiyun * Complete a fast commit by writing tail tag.
652*4882a593Smuzhiyun *
653*4882a593Smuzhiyun * Writing tail tag marks the end of a fast commit. In order to guarantee
654*4882a593Smuzhiyun * atomicity, after writing tail tag, even if there's space remaining
655*4882a593Smuzhiyun * in the block, next commit shouldn't use it. That's why tail tag
656*4882a593Smuzhiyun * has the length as that of the remaining space on the block.
657*4882a593Smuzhiyun */
ext4_fc_write_tail(struct super_block * sb,u32 crc)658*4882a593Smuzhiyun static int ext4_fc_write_tail(struct super_block *sb, u32 crc)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(sb);
661*4882a593Smuzhiyun struct ext4_fc_tl tl;
662*4882a593Smuzhiyun struct ext4_fc_tail tail;
663*4882a593Smuzhiyun int off, bsize = sbi->s_journal->j_blocksize;
664*4882a593Smuzhiyun u8 *dst;
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun /*
667*4882a593Smuzhiyun * ext4_fc_reserve_space takes care of allocating an extra block if
668*4882a593Smuzhiyun * there's no enough space on this block for accommodating this tail.
669*4882a593Smuzhiyun */
670*4882a593Smuzhiyun dst = ext4_fc_reserve_space(sb, sizeof(tl) + sizeof(tail), &crc);
671*4882a593Smuzhiyun if (!dst)
672*4882a593Smuzhiyun return -ENOSPC;
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun off = sbi->s_fc_bytes % bsize;
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun tl.fc_tag = cpu_to_le16(EXT4_FC_TAG_TAIL);
677*4882a593Smuzhiyun tl.fc_len = cpu_to_le16(bsize - off - 1 + sizeof(struct ext4_fc_tail));
678*4882a593Smuzhiyun sbi->s_fc_bytes = round_up(sbi->s_fc_bytes, bsize);
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun ext4_fc_memcpy(sb, dst, &tl, sizeof(tl), &crc);
681*4882a593Smuzhiyun dst += sizeof(tl);
682*4882a593Smuzhiyun tail.fc_tid = cpu_to_le32(sbi->s_journal->j_running_transaction->t_tid);
683*4882a593Smuzhiyun ext4_fc_memcpy(sb, dst, &tail.fc_tid, sizeof(tail.fc_tid), &crc);
684*4882a593Smuzhiyun dst += sizeof(tail.fc_tid);
685*4882a593Smuzhiyun tail.fc_crc = cpu_to_le32(crc);
686*4882a593Smuzhiyun ext4_fc_memcpy(sb, dst, &tail.fc_crc, sizeof(tail.fc_crc), NULL);
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun ext4_fc_submit_bh(sb);
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun return 0;
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun /*
694*4882a593Smuzhiyun * Adds tag, length, value and updates CRC. Returns true if tlv was added.
695*4882a593Smuzhiyun * Returns false if there's not enough space.
696*4882a593Smuzhiyun */
ext4_fc_add_tlv(struct super_block * sb,u16 tag,u16 len,u8 * val,u32 * crc)697*4882a593Smuzhiyun static bool ext4_fc_add_tlv(struct super_block *sb, u16 tag, u16 len, u8 *val,
698*4882a593Smuzhiyun u32 *crc)
699*4882a593Smuzhiyun {
700*4882a593Smuzhiyun struct ext4_fc_tl tl;
701*4882a593Smuzhiyun u8 *dst;
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun dst = ext4_fc_reserve_space(sb, sizeof(tl) + len, crc);
704*4882a593Smuzhiyun if (!dst)
705*4882a593Smuzhiyun return false;
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun tl.fc_tag = cpu_to_le16(tag);
708*4882a593Smuzhiyun tl.fc_len = cpu_to_le16(len);
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun ext4_fc_memcpy(sb, dst, &tl, sizeof(tl), crc);
711*4882a593Smuzhiyun ext4_fc_memcpy(sb, dst + sizeof(tl), val, len, crc);
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun return true;
714*4882a593Smuzhiyun }
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun /* Same as above, but adds dentry tlv. */
ext4_fc_add_dentry_tlv(struct super_block * sb,u16 tag,int parent_ino,int ino,int dlen,const unsigned char * dname,u32 * crc)717*4882a593Smuzhiyun static bool ext4_fc_add_dentry_tlv(struct super_block *sb, u16 tag,
718*4882a593Smuzhiyun int parent_ino, int ino, int dlen,
719*4882a593Smuzhiyun const unsigned char *dname,
720*4882a593Smuzhiyun u32 *crc)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun struct ext4_fc_dentry_info fcd;
723*4882a593Smuzhiyun struct ext4_fc_tl tl;
724*4882a593Smuzhiyun u8 *dst = ext4_fc_reserve_space(sb, sizeof(tl) + sizeof(fcd) + dlen,
725*4882a593Smuzhiyun crc);
726*4882a593Smuzhiyun
727*4882a593Smuzhiyun if (!dst)
728*4882a593Smuzhiyun return false;
729*4882a593Smuzhiyun
730*4882a593Smuzhiyun fcd.fc_parent_ino = cpu_to_le32(parent_ino);
731*4882a593Smuzhiyun fcd.fc_ino = cpu_to_le32(ino);
732*4882a593Smuzhiyun tl.fc_tag = cpu_to_le16(tag);
733*4882a593Smuzhiyun tl.fc_len = cpu_to_le16(sizeof(fcd) + dlen);
734*4882a593Smuzhiyun ext4_fc_memcpy(sb, dst, &tl, sizeof(tl), crc);
735*4882a593Smuzhiyun dst += sizeof(tl);
736*4882a593Smuzhiyun ext4_fc_memcpy(sb, dst, &fcd, sizeof(fcd), crc);
737*4882a593Smuzhiyun dst += sizeof(fcd);
738*4882a593Smuzhiyun ext4_fc_memcpy(sb, dst, dname, dlen, crc);
739*4882a593Smuzhiyun dst += dlen;
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun return true;
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun /*
745*4882a593Smuzhiyun * Writes inode in the fast commit space under TLV with tag @tag.
746*4882a593Smuzhiyun * Returns 0 on success, error on failure.
747*4882a593Smuzhiyun */
ext4_fc_write_inode(struct inode * inode,u32 * crc)748*4882a593Smuzhiyun static int ext4_fc_write_inode(struct inode *inode, u32 *crc)
749*4882a593Smuzhiyun {
750*4882a593Smuzhiyun struct ext4_inode_info *ei = EXT4_I(inode);
751*4882a593Smuzhiyun int inode_len = EXT4_GOOD_OLD_INODE_SIZE;
752*4882a593Smuzhiyun int ret;
753*4882a593Smuzhiyun struct ext4_iloc iloc;
754*4882a593Smuzhiyun struct ext4_fc_inode fc_inode;
755*4882a593Smuzhiyun struct ext4_fc_tl tl;
756*4882a593Smuzhiyun u8 *dst;
757*4882a593Smuzhiyun
758*4882a593Smuzhiyun ret = ext4_get_inode_loc(inode, &iloc);
759*4882a593Smuzhiyun if (ret)
760*4882a593Smuzhiyun return ret;
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE)
763*4882a593Smuzhiyun inode_len += ei->i_extra_isize;
764*4882a593Smuzhiyun
765*4882a593Smuzhiyun fc_inode.fc_ino = cpu_to_le32(inode->i_ino);
766*4882a593Smuzhiyun tl.fc_tag = cpu_to_le16(EXT4_FC_TAG_INODE);
767*4882a593Smuzhiyun tl.fc_len = cpu_to_le16(inode_len + sizeof(fc_inode.fc_ino));
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun ret = -ECANCELED;
770*4882a593Smuzhiyun dst = ext4_fc_reserve_space(inode->i_sb,
771*4882a593Smuzhiyun sizeof(tl) + inode_len + sizeof(fc_inode.fc_ino), crc);
772*4882a593Smuzhiyun if (!dst)
773*4882a593Smuzhiyun goto err;
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun if (!ext4_fc_memcpy(inode->i_sb, dst, &tl, sizeof(tl), crc))
776*4882a593Smuzhiyun goto err;
777*4882a593Smuzhiyun dst += sizeof(tl);
778*4882a593Smuzhiyun if (!ext4_fc_memcpy(inode->i_sb, dst, &fc_inode, sizeof(fc_inode), crc))
779*4882a593Smuzhiyun goto err;
780*4882a593Smuzhiyun dst += sizeof(fc_inode);
781*4882a593Smuzhiyun if (!ext4_fc_memcpy(inode->i_sb, dst, (u8 *)ext4_raw_inode(&iloc),
782*4882a593Smuzhiyun inode_len, crc))
783*4882a593Smuzhiyun goto err;
784*4882a593Smuzhiyun ret = 0;
785*4882a593Smuzhiyun err:
786*4882a593Smuzhiyun brelse(iloc.bh);
787*4882a593Smuzhiyun return ret;
788*4882a593Smuzhiyun }
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun /*
791*4882a593Smuzhiyun * Writes updated data ranges for the inode in question. Updates CRC.
792*4882a593Smuzhiyun * Returns 0 on success, error otherwise.
793*4882a593Smuzhiyun */
ext4_fc_write_inode_data(struct inode * inode,u32 * crc)794*4882a593Smuzhiyun static int ext4_fc_write_inode_data(struct inode *inode, u32 *crc)
795*4882a593Smuzhiyun {
796*4882a593Smuzhiyun ext4_lblk_t old_blk_size, cur_lblk_off, new_blk_size;
797*4882a593Smuzhiyun struct ext4_inode_info *ei = EXT4_I(inode);
798*4882a593Smuzhiyun struct ext4_map_blocks map;
799*4882a593Smuzhiyun struct ext4_fc_add_range fc_ext;
800*4882a593Smuzhiyun struct ext4_fc_del_range lrange;
801*4882a593Smuzhiyun struct ext4_extent *ex;
802*4882a593Smuzhiyun int ret;
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun mutex_lock(&ei->i_fc_lock);
805*4882a593Smuzhiyun if (ei->i_fc_lblk_len == 0) {
806*4882a593Smuzhiyun mutex_unlock(&ei->i_fc_lock);
807*4882a593Smuzhiyun return 0;
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun old_blk_size = ei->i_fc_lblk_start;
810*4882a593Smuzhiyun new_blk_size = ei->i_fc_lblk_start + ei->i_fc_lblk_len - 1;
811*4882a593Smuzhiyun ei->i_fc_lblk_len = 0;
812*4882a593Smuzhiyun mutex_unlock(&ei->i_fc_lock);
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun cur_lblk_off = old_blk_size;
815*4882a593Smuzhiyun jbd_debug(1, "%s: will try writing %d to %d for inode %ld\n",
816*4882a593Smuzhiyun __func__, cur_lblk_off, new_blk_size, inode->i_ino);
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun while (cur_lblk_off <= new_blk_size) {
819*4882a593Smuzhiyun map.m_lblk = cur_lblk_off;
820*4882a593Smuzhiyun map.m_len = new_blk_size - cur_lblk_off + 1;
821*4882a593Smuzhiyun ret = ext4_map_blocks(NULL, inode, &map, 0);
822*4882a593Smuzhiyun if (ret < 0)
823*4882a593Smuzhiyun return -ECANCELED;
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun if (map.m_len == 0) {
826*4882a593Smuzhiyun cur_lblk_off++;
827*4882a593Smuzhiyun continue;
828*4882a593Smuzhiyun }
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun if (ret == 0) {
831*4882a593Smuzhiyun lrange.fc_ino = cpu_to_le32(inode->i_ino);
832*4882a593Smuzhiyun lrange.fc_lblk = cpu_to_le32(map.m_lblk);
833*4882a593Smuzhiyun lrange.fc_len = cpu_to_le32(map.m_len);
834*4882a593Smuzhiyun if (!ext4_fc_add_tlv(inode->i_sb, EXT4_FC_TAG_DEL_RANGE,
835*4882a593Smuzhiyun sizeof(lrange), (u8 *)&lrange, crc))
836*4882a593Smuzhiyun return -ENOSPC;
837*4882a593Smuzhiyun } else {
838*4882a593Smuzhiyun unsigned int max = (map.m_flags & EXT4_MAP_UNWRITTEN) ?
839*4882a593Smuzhiyun EXT_UNWRITTEN_MAX_LEN : EXT_INIT_MAX_LEN;
840*4882a593Smuzhiyun
841*4882a593Smuzhiyun /* Limit the number of blocks in one extent */
842*4882a593Smuzhiyun map.m_len = min(max, map.m_len);
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun fc_ext.fc_ino = cpu_to_le32(inode->i_ino);
845*4882a593Smuzhiyun ex = (struct ext4_extent *)&fc_ext.fc_ex;
846*4882a593Smuzhiyun ex->ee_block = cpu_to_le32(map.m_lblk);
847*4882a593Smuzhiyun ex->ee_len = cpu_to_le16(map.m_len);
848*4882a593Smuzhiyun ext4_ext_store_pblock(ex, map.m_pblk);
849*4882a593Smuzhiyun if (map.m_flags & EXT4_MAP_UNWRITTEN)
850*4882a593Smuzhiyun ext4_ext_mark_unwritten(ex);
851*4882a593Smuzhiyun else
852*4882a593Smuzhiyun ext4_ext_mark_initialized(ex);
853*4882a593Smuzhiyun if (!ext4_fc_add_tlv(inode->i_sb, EXT4_FC_TAG_ADD_RANGE,
854*4882a593Smuzhiyun sizeof(fc_ext), (u8 *)&fc_ext, crc))
855*4882a593Smuzhiyun return -ENOSPC;
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun cur_lblk_off += map.m_len;
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun return 0;
862*4882a593Smuzhiyun }
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun /* Submit data for all the fast commit inodes */
ext4_fc_submit_inode_data_all(journal_t * journal)866*4882a593Smuzhiyun static int ext4_fc_submit_inode_data_all(journal_t *journal)
867*4882a593Smuzhiyun {
868*4882a593Smuzhiyun struct super_block *sb = (struct super_block *)(journal->j_private);
869*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(sb);
870*4882a593Smuzhiyun struct ext4_inode_info *ei;
871*4882a593Smuzhiyun struct list_head *pos;
872*4882a593Smuzhiyun int ret = 0;
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
875*4882a593Smuzhiyun ext4_set_mount_flag(sb, EXT4_MF_FC_COMMITTING);
876*4882a593Smuzhiyun list_for_each(pos, &sbi->s_fc_q[FC_Q_MAIN]) {
877*4882a593Smuzhiyun ei = list_entry(pos, struct ext4_inode_info, i_fc_list);
878*4882a593Smuzhiyun ext4_set_inode_state(&ei->vfs_inode, EXT4_STATE_FC_COMMITTING);
879*4882a593Smuzhiyun while (atomic_read(&ei->i_fc_updates)) {
880*4882a593Smuzhiyun DEFINE_WAIT(wait);
881*4882a593Smuzhiyun
882*4882a593Smuzhiyun prepare_to_wait(&ei->i_fc_wait, &wait,
883*4882a593Smuzhiyun TASK_UNINTERRUPTIBLE);
884*4882a593Smuzhiyun if (atomic_read(&ei->i_fc_updates)) {
885*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
886*4882a593Smuzhiyun schedule();
887*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun finish_wait(&ei->i_fc_wait, &wait);
890*4882a593Smuzhiyun }
891*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
892*4882a593Smuzhiyun ret = jbd2_submit_inode_data(ei->jinode);
893*4882a593Smuzhiyun if (ret)
894*4882a593Smuzhiyun return ret;
895*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
896*4882a593Smuzhiyun }
897*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun return ret;
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun /* Wait for completion of data for all the fast commit inodes */
ext4_fc_wait_inode_data_all(journal_t * journal)903*4882a593Smuzhiyun static int ext4_fc_wait_inode_data_all(journal_t *journal)
904*4882a593Smuzhiyun {
905*4882a593Smuzhiyun struct super_block *sb = (struct super_block *)(journal->j_private);
906*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(sb);
907*4882a593Smuzhiyun struct ext4_inode_info *pos, *n;
908*4882a593Smuzhiyun int ret = 0;
909*4882a593Smuzhiyun
910*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
911*4882a593Smuzhiyun list_for_each_entry_safe(pos, n, &sbi->s_fc_q[FC_Q_MAIN], i_fc_list) {
912*4882a593Smuzhiyun if (!ext4_test_inode_state(&pos->vfs_inode,
913*4882a593Smuzhiyun EXT4_STATE_FC_COMMITTING))
914*4882a593Smuzhiyun continue;
915*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun ret = jbd2_wait_inode_data(journal, pos->jinode);
918*4882a593Smuzhiyun if (ret)
919*4882a593Smuzhiyun return ret;
920*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
921*4882a593Smuzhiyun }
922*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
923*4882a593Smuzhiyun
924*4882a593Smuzhiyun return 0;
925*4882a593Smuzhiyun }
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun /* Commit all the directory entry updates */
ext4_fc_commit_dentry_updates(journal_t * journal,u32 * crc)928*4882a593Smuzhiyun static int ext4_fc_commit_dentry_updates(journal_t *journal, u32 *crc)
929*4882a593Smuzhiyun __acquires(&sbi->s_fc_lock)
930*4882a593Smuzhiyun __releases(&sbi->s_fc_lock)
931*4882a593Smuzhiyun {
932*4882a593Smuzhiyun struct super_block *sb = (struct super_block *)(journal->j_private);
933*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(sb);
934*4882a593Smuzhiyun struct ext4_fc_dentry_update *fc_dentry;
935*4882a593Smuzhiyun struct inode *inode;
936*4882a593Smuzhiyun struct list_head *pos, *n, *fcd_pos, *fcd_n;
937*4882a593Smuzhiyun struct ext4_inode_info *ei;
938*4882a593Smuzhiyun int ret;
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun if (list_empty(&sbi->s_fc_dentry_q[FC_Q_MAIN]))
941*4882a593Smuzhiyun return 0;
942*4882a593Smuzhiyun list_for_each_safe(fcd_pos, fcd_n, &sbi->s_fc_dentry_q[FC_Q_MAIN]) {
943*4882a593Smuzhiyun fc_dentry = list_entry(fcd_pos, struct ext4_fc_dentry_update,
944*4882a593Smuzhiyun fcd_list);
945*4882a593Smuzhiyun if (fc_dentry->fcd_op != EXT4_FC_TAG_CREAT) {
946*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
947*4882a593Smuzhiyun if (!ext4_fc_add_dentry_tlv(
948*4882a593Smuzhiyun sb, fc_dentry->fcd_op,
949*4882a593Smuzhiyun fc_dentry->fcd_parent, fc_dentry->fcd_ino,
950*4882a593Smuzhiyun fc_dentry->fcd_name.len,
951*4882a593Smuzhiyun fc_dentry->fcd_name.name, crc)) {
952*4882a593Smuzhiyun ret = -ENOSPC;
953*4882a593Smuzhiyun goto lock_and_exit;
954*4882a593Smuzhiyun }
955*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
956*4882a593Smuzhiyun continue;
957*4882a593Smuzhiyun }
958*4882a593Smuzhiyun
959*4882a593Smuzhiyun inode = NULL;
960*4882a593Smuzhiyun list_for_each_safe(pos, n, &sbi->s_fc_q[FC_Q_MAIN]) {
961*4882a593Smuzhiyun ei = list_entry(pos, struct ext4_inode_info, i_fc_list);
962*4882a593Smuzhiyun if (ei->vfs_inode.i_ino == fc_dentry->fcd_ino) {
963*4882a593Smuzhiyun inode = &ei->vfs_inode;
964*4882a593Smuzhiyun break;
965*4882a593Smuzhiyun }
966*4882a593Smuzhiyun }
967*4882a593Smuzhiyun /*
968*4882a593Smuzhiyun * If we don't find inode in our list, then it was deleted,
969*4882a593Smuzhiyun * in which case, we don't need to record it's create tag.
970*4882a593Smuzhiyun */
971*4882a593Smuzhiyun if (!inode)
972*4882a593Smuzhiyun continue;
973*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun /*
976*4882a593Smuzhiyun * We first write the inode and then the create dirent. This
977*4882a593Smuzhiyun * allows the recovery code to create an unnamed inode first
978*4882a593Smuzhiyun * and then link it to a directory entry. This allows us
979*4882a593Smuzhiyun * to use namei.c routines almost as is and simplifies
980*4882a593Smuzhiyun * the recovery code.
981*4882a593Smuzhiyun */
982*4882a593Smuzhiyun ret = ext4_fc_write_inode(inode, crc);
983*4882a593Smuzhiyun if (ret)
984*4882a593Smuzhiyun goto lock_and_exit;
985*4882a593Smuzhiyun
986*4882a593Smuzhiyun ret = ext4_fc_write_inode_data(inode, crc);
987*4882a593Smuzhiyun if (ret)
988*4882a593Smuzhiyun goto lock_and_exit;
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun if (!ext4_fc_add_dentry_tlv(
991*4882a593Smuzhiyun sb, fc_dentry->fcd_op,
992*4882a593Smuzhiyun fc_dentry->fcd_parent, fc_dentry->fcd_ino,
993*4882a593Smuzhiyun fc_dentry->fcd_name.len,
994*4882a593Smuzhiyun fc_dentry->fcd_name.name, crc)) {
995*4882a593Smuzhiyun ret = -ENOSPC;
996*4882a593Smuzhiyun goto lock_and_exit;
997*4882a593Smuzhiyun }
998*4882a593Smuzhiyun
999*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
1000*4882a593Smuzhiyun }
1001*4882a593Smuzhiyun return 0;
1002*4882a593Smuzhiyun lock_and_exit:
1003*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
1004*4882a593Smuzhiyun return ret;
1005*4882a593Smuzhiyun }
1006*4882a593Smuzhiyun
ext4_fc_perform_commit(journal_t * journal)1007*4882a593Smuzhiyun static int ext4_fc_perform_commit(journal_t *journal)
1008*4882a593Smuzhiyun {
1009*4882a593Smuzhiyun struct super_block *sb = (struct super_block *)(journal->j_private);
1010*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(sb);
1011*4882a593Smuzhiyun struct ext4_inode_info *iter;
1012*4882a593Smuzhiyun struct ext4_fc_head head;
1013*4882a593Smuzhiyun struct list_head *pos;
1014*4882a593Smuzhiyun struct inode *inode;
1015*4882a593Smuzhiyun struct blk_plug plug;
1016*4882a593Smuzhiyun int ret = 0;
1017*4882a593Smuzhiyun u32 crc = 0;
1018*4882a593Smuzhiyun
1019*4882a593Smuzhiyun ret = ext4_fc_submit_inode_data_all(journal);
1020*4882a593Smuzhiyun if (ret)
1021*4882a593Smuzhiyun return ret;
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun ret = ext4_fc_wait_inode_data_all(journal);
1024*4882a593Smuzhiyun if (ret)
1025*4882a593Smuzhiyun return ret;
1026*4882a593Smuzhiyun
1027*4882a593Smuzhiyun /*
1028*4882a593Smuzhiyun * If file system device is different from journal device, issue a cache
1029*4882a593Smuzhiyun * flush before we start writing fast commit blocks.
1030*4882a593Smuzhiyun */
1031*4882a593Smuzhiyun if (journal->j_fs_dev != journal->j_dev)
1032*4882a593Smuzhiyun blkdev_issue_flush(journal->j_fs_dev, GFP_NOFS);
1033*4882a593Smuzhiyun
1034*4882a593Smuzhiyun blk_start_plug(&plug);
1035*4882a593Smuzhiyun if (sbi->s_fc_bytes == 0) {
1036*4882a593Smuzhiyun /*
1037*4882a593Smuzhiyun * Add a head tag only if this is the first fast commit
1038*4882a593Smuzhiyun * in this TID.
1039*4882a593Smuzhiyun */
1040*4882a593Smuzhiyun head.fc_features = cpu_to_le32(EXT4_FC_SUPPORTED_FEATURES);
1041*4882a593Smuzhiyun head.fc_tid = cpu_to_le32(
1042*4882a593Smuzhiyun sbi->s_journal->j_running_transaction->t_tid);
1043*4882a593Smuzhiyun if (!ext4_fc_add_tlv(sb, EXT4_FC_TAG_HEAD, sizeof(head),
1044*4882a593Smuzhiyun (u8 *)&head, &crc)) {
1045*4882a593Smuzhiyun ret = -ENOSPC;
1046*4882a593Smuzhiyun goto out;
1047*4882a593Smuzhiyun }
1048*4882a593Smuzhiyun }
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
1051*4882a593Smuzhiyun ret = ext4_fc_commit_dentry_updates(journal, &crc);
1052*4882a593Smuzhiyun if (ret) {
1053*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
1054*4882a593Smuzhiyun goto out;
1055*4882a593Smuzhiyun }
1056*4882a593Smuzhiyun
1057*4882a593Smuzhiyun list_for_each(pos, &sbi->s_fc_q[FC_Q_MAIN]) {
1058*4882a593Smuzhiyun iter = list_entry(pos, struct ext4_inode_info, i_fc_list);
1059*4882a593Smuzhiyun inode = &iter->vfs_inode;
1060*4882a593Smuzhiyun if (!ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING))
1061*4882a593Smuzhiyun continue;
1062*4882a593Smuzhiyun
1063*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
1064*4882a593Smuzhiyun ret = ext4_fc_write_inode_data(inode, &crc);
1065*4882a593Smuzhiyun if (ret)
1066*4882a593Smuzhiyun goto out;
1067*4882a593Smuzhiyun ret = ext4_fc_write_inode(inode, &crc);
1068*4882a593Smuzhiyun if (ret)
1069*4882a593Smuzhiyun goto out;
1070*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
1071*4882a593Smuzhiyun }
1072*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
1073*4882a593Smuzhiyun
1074*4882a593Smuzhiyun ret = ext4_fc_write_tail(sb, crc);
1075*4882a593Smuzhiyun
1076*4882a593Smuzhiyun out:
1077*4882a593Smuzhiyun blk_finish_plug(&plug);
1078*4882a593Smuzhiyun return ret;
1079*4882a593Smuzhiyun }
1080*4882a593Smuzhiyun
1081*4882a593Smuzhiyun /*
1082*4882a593Smuzhiyun * The main commit entry point. Performs a fast commit for transaction
1083*4882a593Smuzhiyun * commit_tid if needed. If it's not possible to perform a fast commit
1084*4882a593Smuzhiyun * due to various reasons, we fall back to full commit. Returns 0
1085*4882a593Smuzhiyun * on success, error otherwise.
1086*4882a593Smuzhiyun */
ext4_fc_commit(journal_t * journal,tid_t commit_tid)1087*4882a593Smuzhiyun int ext4_fc_commit(journal_t *journal, tid_t commit_tid)
1088*4882a593Smuzhiyun {
1089*4882a593Smuzhiyun struct super_block *sb = (struct super_block *)(journal->j_private);
1090*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(sb);
1091*4882a593Smuzhiyun int nblks = 0, ret, bsize = journal->j_blocksize;
1092*4882a593Smuzhiyun int subtid = atomic_read(&sbi->s_fc_subtid);
1093*4882a593Smuzhiyun int reason = EXT4_FC_REASON_OK, fc_bufs_before = 0;
1094*4882a593Smuzhiyun ktime_t start_time, commit_time;
1095*4882a593Smuzhiyun
1096*4882a593Smuzhiyun trace_ext4_fc_commit_start(sb);
1097*4882a593Smuzhiyun
1098*4882a593Smuzhiyun start_time = ktime_get();
1099*4882a593Smuzhiyun
1100*4882a593Smuzhiyun if (!test_opt2(sb, JOURNAL_FAST_COMMIT) ||
1101*4882a593Smuzhiyun (ext4_fc_is_ineligible(sb))) {
1102*4882a593Smuzhiyun reason = EXT4_FC_REASON_INELIGIBLE;
1103*4882a593Smuzhiyun goto out;
1104*4882a593Smuzhiyun }
1105*4882a593Smuzhiyun
1106*4882a593Smuzhiyun restart_fc:
1107*4882a593Smuzhiyun ret = jbd2_fc_begin_commit(journal, commit_tid);
1108*4882a593Smuzhiyun if (ret == -EALREADY) {
1109*4882a593Smuzhiyun /* There was an ongoing commit, check if we need to restart */
1110*4882a593Smuzhiyun if (atomic_read(&sbi->s_fc_subtid) <= subtid &&
1111*4882a593Smuzhiyun commit_tid > journal->j_commit_sequence)
1112*4882a593Smuzhiyun goto restart_fc;
1113*4882a593Smuzhiyun reason = EXT4_FC_REASON_ALREADY_COMMITTED;
1114*4882a593Smuzhiyun goto out;
1115*4882a593Smuzhiyun } else if (ret) {
1116*4882a593Smuzhiyun sbi->s_fc_stats.fc_ineligible_reason_count[EXT4_FC_COMMIT_FAILED]++;
1117*4882a593Smuzhiyun reason = EXT4_FC_REASON_FC_START_FAILED;
1118*4882a593Smuzhiyun goto out;
1119*4882a593Smuzhiyun }
1120*4882a593Smuzhiyun
1121*4882a593Smuzhiyun fc_bufs_before = (sbi->s_fc_bytes + bsize - 1) / bsize;
1122*4882a593Smuzhiyun ret = ext4_fc_perform_commit(journal);
1123*4882a593Smuzhiyun if (ret < 0) {
1124*4882a593Smuzhiyun sbi->s_fc_stats.fc_ineligible_reason_count[EXT4_FC_COMMIT_FAILED]++;
1125*4882a593Smuzhiyun reason = EXT4_FC_REASON_FC_FAILED;
1126*4882a593Smuzhiyun goto out;
1127*4882a593Smuzhiyun }
1128*4882a593Smuzhiyun nblks = (sbi->s_fc_bytes + bsize - 1) / bsize - fc_bufs_before;
1129*4882a593Smuzhiyun ret = jbd2_fc_wait_bufs(journal, nblks);
1130*4882a593Smuzhiyun if (ret < 0) {
1131*4882a593Smuzhiyun sbi->s_fc_stats.fc_ineligible_reason_count[EXT4_FC_COMMIT_FAILED]++;
1132*4882a593Smuzhiyun reason = EXT4_FC_REASON_FC_FAILED;
1133*4882a593Smuzhiyun goto out;
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun atomic_inc(&sbi->s_fc_subtid);
1136*4882a593Smuzhiyun jbd2_fc_end_commit(journal);
1137*4882a593Smuzhiyun out:
1138*4882a593Smuzhiyun /* Has any ineligible update happened since we started? */
1139*4882a593Smuzhiyun if (reason == EXT4_FC_REASON_OK && ext4_fc_is_ineligible(sb)) {
1140*4882a593Smuzhiyun sbi->s_fc_stats.fc_ineligible_reason_count[EXT4_FC_COMMIT_FAILED]++;
1141*4882a593Smuzhiyun reason = EXT4_FC_REASON_INELIGIBLE;
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
1145*4882a593Smuzhiyun if (reason != EXT4_FC_REASON_OK &&
1146*4882a593Smuzhiyun reason != EXT4_FC_REASON_ALREADY_COMMITTED) {
1147*4882a593Smuzhiyun sbi->s_fc_stats.fc_ineligible_commits++;
1148*4882a593Smuzhiyun } else {
1149*4882a593Smuzhiyun sbi->s_fc_stats.fc_num_commits++;
1150*4882a593Smuzhiyun sbi->s_fc_stats.fc_numblks += nblks;
1151*4882a593Smuzhiyun }
1152*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
1153*4882a593Smuzhiyun nblks = (reason == EXT4_FC_REASON_OK) ? nblks : 0;
1154*4882a593Smuzhiyun trace_ext4_fc_commit_stop(sb, nblks, reason);
1155*4882a593Smuzhiyun commit_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1156*4882a593Smuzhiyun /*
1157*4882a593Smuzhiyun * weight the commit time higher than the average time so we don't
1158*4882a593Smuzhiyun * react too strongly to vast changes in the commit time
1159*4882a593Smuzhiyun */
1160*4882a593Smuzhiyun if (likely(sbi->s_fc_avg_commit_time))
1161*4882a593Smuzhiyun sbi->s_fc_avg_commit_time = (commit_time +
1162*4882a593Smuzhiyun sbi->s_fc_avg_commit_time * 3) / 4;
1163*4882a593Smuzhiyun else
1164*4882a593Smuzhiyun sbi->s_fc_avg_commit_time = commit_time;
1165*4882a593Smuzhiyun jbd_debug(1,
1166*4882a593Smuzhiyun "Fast commit ended with blks = %d, reason = %d, subtid - %d",
1167*4882a593Smuzhiyun nblks, reason, subtid);
1168*4882a593Smuzhiyun if (reason == EXT4_FC_REASON_FC_FAILED)
1169*4882a593Smuzhiyun return jbd2_fc_end_commit_fallback(journal);
1170*4882a593Smuzhiyun if (reason == EXT4_FC_REASON_FC_START_FAILED ||
1171*4882a593Smuzhiyun reason == EXT4_FC_REASON_INELIGIBLE)
1172*4882a593Smuzhiyun return jbd2_complete_transaction(journal, commit_tid);
1173*4882a593Smuzhiyun return 0;
1174*4882a593Smuzhiyun }
1175*4882a593Smuzhiyun
1176*4882a593Smuzhiyun /*
1177*4882a593Smuzhiyun * Fast commit cleanup routine. This is called after every fast commit and
1178*4882a593Smuzhiyun * full commit. full is true if we are called after a full commit.
1179*4882a593Smuzhiyun */
ext4_fc_cleanup(journal_t * journal,int full)1180*4882a593Smuzhiyun static void ext4_fc_cleanup(journal_t *journal, int full)
1181*4882a593Smuzhiyun {
1182*4882a593Smuzhiyun struct super_block *sb = journal->j_private;
1183*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(sb);
1184*4882a593Smuzhiyun struct ext4_inode_info *iter;
1185*4882a593Smuzhiyun struct ext4_fc_dentry_update *fc_dentry;
1186*4882a593Smuzhiyun struct list_head *pos, *n;
1187*4882a593Smuzhiyun
1188*4882a593Smuzhiyun if (full && sbi->s_fc_bh)
1189*4882a593Smuzhiyun sbi->s_fc_bh = NULL;
1190*4882a593Smuzhiyun
1191*4882a593Smuzhiyun jbd2_fc_release_bufs(journal);
1192*4882a593Smuzhiyun
1193*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
1194*4882a593Smuzhiyun list_for_each_safe(pos, n, &sbi->s_fc_q[FC_Q_MAIN]) {
1195*4882a593Smuzhiyun iter = list_entry(pos, struct ext4_inode_info, i_fc_list);
1196*4882a593Smuzhiyun list_del_init(&iter->i_fc_list);
1197*4882a593Smuzhiyun ext4_clear_inode_state(&iter->vfs_inode,
1198*4882a593Smuzhiyun EXT4_STATE_FC_COMMITTING);
1199*4882a593Smuzhiyun ext4_fc_reset_inode(&iter->vfs_inode);
1200*4882a593Smuzhiyun /* Make sure EXT4_STATE_FC_COMMITTING bit is clear */
1201*4882a593Smuzhiyun smp_mb();
1202*4882a593Smuzhiyun #if (BITS_PER_LONG < 64)
1203*4882a593Smuzhiyun wake_up_bit(&iter->i_state_flags, EXT4_STATE_FC_COMMITTING);
1204*4882a593Smuzhiyun #else
1205*4882a593Smuzhiyun wake_up_bit(&iter->i_flags, EXT4_STATE_FC_COMMITTING);
1206*4882a593Smuzhiyun #endif
1207*4882a593Smuzhiyun }
1208*4882a593Smuzhiyun
1209*4882a593Smuzhiyun while (!list_empty(&sbi->s_fc_dentry_q[FC_Q_MAIN])) {
1210*4882a593Smuzhiyun fc_dentry = list_first_entry(&sbi->s_fc_dentry_q[FC_Q_MAIN],
1211*4882a593Smuzhiyun struct ext4_fc_dentry_update,
1212*4882a593Smuzhiyun fcd_list);
1213*4882a593Smuzhiyun list_del_init(&fc_dentry->fcd_list);
1214*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
1215*4882a593Smuzhiyun
1216*4882a593Smuzhiyun if (fc_dentry->fcd_name.name &&
1217*4882a593Smuzhiyun fc_dentry->fcd_name.len > DNAME_INLINE_LEN)
1218*4882a593Smuzhiyun kfree(fc_dentry->fcd_name.name);
1219*4882a593Smuzhiyun kmem_cache_free(ext4_fc_dentry_cachep, fc_dentry);
1220*4882a593Smuzhiyun spin_lock(&sbi->s_fc_lock);
1221*4882a593Smuzhiyun }
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun list_splice_init(&sbi->s_fc_dentry_q[FC_Q_STAGING],
1224*4882a593Smuzhiyun &sbi->s_fc_dentry_q[FC_Q_MAIN]);
1225*4882a593Smuzhiyun list_splice_init(&sbi->s_fc_q[FC_Q_STAGING],
1226*4882a593Smuzhiyun &sbi->s_fc_q[FC_Q_MAIN]);
1227*4882a593Smuzhiyun
1228*4882a593Smuzhiyun ext4_clear_mount_flag(sb, EXT4_MF_FC_COMMITTING);
1229*4882a593Smuzhiyun ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
1230*4882a593Smuzhiyun
1231*4882a593Smuzhiyun if (full)
1232*4882a593Smuzhiyun sbi->s_fc_bytes = 0;
1233*4882a593Smuzhiyun spin_unlock(&sbi->s_fc_lock);
1234*4882a593Smuzhiyun trace_ext4_fc_stats(sb);
1235*4882a593Smuzhiyun }
1236*4882a593Smuzhiyun
1237*4882a593Smuzhiyun /* Ext4 Replay Path Routines */
1238*4882a593Smuzhiyun
1239*4882a593Smuzhiyun /* Helper struct for dentry replay routines */
1240*4882a593Smuzhiyun struct dentry_info_args {
1241*4882a593Smuzhiyun int parent_ino, dname_len, ino, inode_len;
1242*4882a593Smuzhiyun char *dname;
1243*4882a593Smuzhiyun };
1244*4882a593Smuzhiyun
tl_to_darg(struct dentry_info_args * darg,struct ext4_fc_tl * tl,u8 * val)1245*4882a593Smuzhiyun static inline void tl_to_darg(struct dentry_info_args *darg,
1246*4882a593Smuzhiyun struct ext4_fc_tl *tl, u8 *val)
1247*4882a593Smuzhiyun {
1248*4882a593Smuzhiyun struct ext4_fc_dentry_info fcd;
1249*4882a593Smuzhiyun
1250*4882a593Smuzhiyun memcpy(&fcd, val, sizeof(fcd));
1251*4882a593Smuzhiyun
1252*4882a593Smuzhiyun darg->parent_ino = le32_to_cpu(fcd.fc_parent_ino);
1253*4882a593Smuzhiyun darg->ino = le32_to_cpu(fcd.fc_ino);
1254*4882a593Smuzhiyun darg->dname = val + offsetof(struct ext4_fc_dentry_info, fc_dname);
1255*4882a593Smuzhiyun darg->dname_len = le16_to_cpu(tl->fc_len) -
1256*4882a593Smuzhiyun sizeof(struct ext4_fc_dentry_info);
1257*4882a593Smuzhiyun }
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun /* Unlink replay function */
ext4_fc_replay_unlink(struct super_block * sb,struct ext4_fc_tl * tl,u8 * val)1260*4882a593Smuzhiyun static int ext4_fc_replay_unlink(struct super_block *sb, struct ext4_fc_tl *tl,
1261*4882a593Smuzhiyun u8 *val)
1262*4882a593Smuzhiyun {
1263*4882a593Smuzhiyun struct inode *inode, *old_parent;
1264*4882a593Smuzhiyun struct qstr entry;
1265*4882a593Smuzhiyun struct dentry_info_args darg;
1266*4882a593Smuzhiyun int ret = 0;
1267*4882a593Smuzhiyun
1268*4882a593Smuzhiyun tl_to_darg(&darg, tl, val);
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun trace_ext4_fc_replay(sb, EXT4_FC_TAG_UNLINK, darg.ino,
1271*4882a593Smuzhiyun darg.parent_ino, darg.dname_len);
1272*4882a593Smuzhiyun
1273*4882a593Smuzhiyun entry.name = darg.dname;
1274*4882a593Smuzhiyun entry.len = darg.dname_len;
1275*4882a593Smuzhiyun inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
1276*4882a593Smuzhiyun
1277*4882a593Smuzhiyun if (IS_ERR(inode)) {
1278*4882a593Smuzhiyun jbd_debug(1, "Inode %d not found", darg.ino);
1279*4882a593Smuzhiyun return 0;
1280*4882a593Smuzhiyun }
1281*4882a593Smuzhiyun
1282*4882a593Smuzhiyun old_parent = ext4_iget(sb, darg.parent_ino,
1283*4882a593Smuzhiyun EXT4_IGET_NORMAL);
1284*4882a593Smuzhiyun if (IS_ERR(old_parent)) {
1285*4882a593Smuzhiyun jbd_debug(1, "Dir with inode %d not found", darg.parent_ino);
1286*4882a593Smuzhiyun iput(inode);
1287*4882a593Smuzhiyun return 0;
1288*4882a593Smuzhiyun }
1289*4882a593Smuzhiyun
1290*4882a593Smuzhiyun ret = __ext4_unlink(NULL, old_parent, &entry, inode);
1291*4882a593Smuzhiyun /* -ENOENT ok coz it might not exist anymore. */
1292*4882a593Smuzhiyun if (ret == -ENOENT)
1293*4882a593Smuzhiyun ret = 0;
1294*4882a593Smuzhiyun iput(old_parent);
1295*4882a593Smuzhiyun iput(inode);
1296*4882a593Smuzhiyun return ret;
1297*4882a593Smuzhiyun }
1298*4882a593Smuzhiyun
ext4_fc_replay_link_internal(struct super_block * sb,struct dentry_info_args * darg,struct inode * inode)1299*4882a593Smuzhiyun static int ext4_fc_replay_link_internal(struct super_block *sb,
1300*4882a593Smuzhiyun struct dentry_info_args *darg,
1301*4882a593Smuzhiyun struct inode *inode)
1302*4882a593Smuzhiyun {
1303*4882a593Smuzhiyun struct inode *dir = NULL;
1304*4882a593Smuzhiyun struct dentry *dentry_dir = NULL, *dentry_inode = NULL;
1305*4882a593Smuzhiyun struct qstr qstr_dname = QSTR_INIT(darg->dname, darg->dname_len);
1306*4882a593Smuzhiyun int ret = 0;
1307*4882a593Smuzhiyun
1308*4882a593Smuzhiyun dir = ext4_iget(sb, darg->parent_ino, EXT4_IGET_NORMAL);
1309*4882a593Smuzhiyun if (IS_ERR(dir)) {
1310*4882a593Smuzhiyun jbd_debug(1, "Dir with inode %d not found.", darg->parent_ino);
1311*4882a593Smuzhiyun dir = NULL;
1312*4882a593Smuzhiyun goto out;
1313*4882a593Smuzhiyun }
1314*4882a593Smuzhiyun
1315*4882a593Smuzhiyun dentry_dir = d_obtain_alias(dir);
1316*4882a593Smuzhiyun if (IS_ERR(dentry_dir)) {
1317*4882a593Smuzhiyun jbd_debug(1, "Failed to obtain dentry");
1318*4882a593Smuzhiyun dentry_dir = NULL;
1319*4882a593Smuzhiyun goto out;
1320*4882a593Smuzhiyun }
1321*4882a593Smuzhiyun
1322*4882a593Smuzhiyun dentry_inode = d_alloc(dentry_dir, &qstr_dname);
1323*4882a593Smuzhiyun if (!dentry_inode) {
1324*4882a593Smuzhiyun jbd_debug(1, "Inode dentry not created.");
1325*4882a593Smuzhiyun ret = -ENOMEM;
1326*4882a593Smuzhiyun goto out;
1327*4882a593Smuzhiyun }
1328*4882a593Smuzhiyun
1329*4882a593Smuzhiyun ret = __ext4_link(dir, inode, dentry_inode);
1330*4882a593Smuzhiyun /*
1331*4882a593Smuzhiyun * It's possible that link already existed since data blocks
1332*4882a593Smuzhiyun * for the dir in question got persisted before we crashed OR
1333*4882a593Smuzhiyun * we replayed this tag and crashed before the entire replay
1334*4882a593Smuzhiyun * could complete.
1335*4882a593Smuzhiyun */
1336*4882a593Smuzhiyun if (ret && ret != -EEXIST) {
1337*4882a593Smuzhiyun jbd_debug(1, "Failed to link\n");
1338*4882a593Smuzhiyun goto out;
1339*4882a593Smuzhiyun }
1340*4882a593Smuzhiyun
1341*4882a593Smuzhiyun ret = 0;
1342*4882a593Smuzhiyun out:
1343*4882a593Smuzhiyun if (dentry_dir) {
1344*4882a593Smuzhiyun d_drop(dentry_dir);
1345*4882a593Smuzhiyun dput(dentry_dir);
1346*4882a593Smuzhiyun } else if (dir) {
1347*4882a593Smuzhiyun iput(dir);
1348*4882a593Smuzhiyun }
1349*4882a593Smuzhiyun if (dentry_inode) {
1350*4882a593Smuzhiyun d_drop(dentry_inode);
1351*4882a593Smuzhiyun dput(dentry_inode);
1352*4882a593Smuzhiyun }
1353*4882a593Smuzhiyun
1354*4882a593Smuzhiyun return ret;
1355*4882a593Smuzhiyun }
1356*4882a593Smuzhiyun
1357*4882a593Smuzhiyun /* Link replay function */
ext4_fc_replay_link(struct super_block * sb,struct ext4_fc_tl * tl,u8 * val)1358*4882a593Smuzhiyun static int ext4_fc_replay_link(struct super_block *sb, struct ext4_fc_tl *tl,
1359*4882a593Smuzhiyun u8 *val)
1360*4882a593Smuzhiyun {
1361*4882a593Smuzhiyun struct inode *inode;
1362*4882a593Smuzhiyun struct dentry_info_args darg;
1363*4882a593Smuzhiyun int ret = 0;
1364*4882a593Smuzhiyun
1365*4882a593Smuzhiyun tl_to_darg(&darg, tl, val);
1366*4882a593Smuzhiyun trace_ext4_fc_replay(sb, EXT4_FC_TAG_LINK, darg.ino,
1367*4882a593Smuzhiyun darg.parent_ino, darg.dname_len);
1368*4882a593Smuzhiyun
1369*4882a593Smuzhiyun inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
1370*4882a593Smuzhiyun if (IS_ERR(inode)) {
1371*4882a593Smuzhiyun jbd_debug(1, "Inode not found.");
1372*4882a593Smuzhiyun return 0;
1373*4882a593Smuzhiyun }
1374*4882a593Smuzhiyun
1375*4882a593Smuzhiyun ret = ext4_fc_replay_link_internal(sb, &darg, inode);
1376*4882a593Smuzhiyun iput(inode);
1377*4882a593Smuzhiyun return ret;
1378*4882a593Smuzhiyun }
1379*4882a593Smuzhiyun
1380*4882a593Smuzhiyun /*
1381*4882a593Smuzhiyun * Record all the modified inodes during replay. We use this later to setup
1382*4882a593Smuzhiyun * block bitmaps correctly.
1383*4882a593Smuzhiyun */
ext4_fc_record_modified_inode(struct super_block * sb,int ino)1384*4882a593Smuzhiyun static int ext4_fc_record_modified_inode(struct super_block *sb, int ino)
1385*4882a593Smuzhiyun {
1386*4882a593Smuzhiyun struct ext4_fc_replay_state *state;
1387*4882a593Smuzhiyun int i;
1388*4882a593Smuzhiyun
1389*4882a593Smuzhiyun state = &EXT4_SB(sb)->s_fc_replay_state;
1390*4882a593Smuzhiyun for (i = 0; i < state->fc_modified_inodes_used; i++)
1391*4882a593Smuzhiyun if (state->fc_modified_inodes[i] == ino)
1392*4882a593Smuzhiyun return 0;
1393*4882a593Smuzhiyun if (state->fc_modified_inodes_used == state->fc_modified_inodes_size) {
1394*4882a593Smuzhiyun int *fc_modified_inodes;
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyun fc_modified_inodes = krealloc(state->fc_modified_inodes,
1397*4882a593Smuzhiyun sizeof(int) * (state->fc_modified_inodes_size +
1398*4882a593Smuzhiyun EXT4_FC_REPLAY_REALLOC_INCREMENT),
1399*4882a593Smuzhiyun GFP_KERNEL);
1400*4882a593Smuzhiyun if (!fc_modified_inodes)
1401*4882a593Smuzhiyun return -ENOMEM;
1402*4882a593Smuzhiyun state->fc_modified_inodes = fc_modified_inodes;
1403*4882a593Smuzhiyun state->fc_modified_inodes_size +=
1404*4882a593Smuzhiyun EXT4_FC_REPLAY_REALLOC_INCREMENT;
1405*4882a593Smuzhiyun }
1406*4882a593Smuzhiyun state->fc_modified_inodes[state->fc_modified_inodes_used++] = ino;
1407*4882a593Smuzhiyun return 0;
1408*4882a593Smuzhiyun }
1409*4882a593Smuzhiyun
1410*4882a593Smuzhiyun /*
1411*4882a593Smuzhiyun * Inode replay function
1412*4882a593Smuzhiyun */
ext4_fc_replay_inode(struct super_block * sb,struct ext4_fc_tl * tl,u8 * val)1413*4882a593Smuzhiyun static int ext4_fc_replay_inode(struct super_block *sb, struct ext4_fc_tl *tl,
1414*4882a593Smuzhiyun u8 *val)
1415*4882a593Smuzhiyun {
1416*4882a593Smuzhiyun struct ext4_fc_inode fc_inode;
1417*4882a593Smuzhiyun struct ext4_inode *raw_inode;
1418*4882a593Smuzhiyun struct ext4_inode *raw_fc_inode;
1419*4882a593Smuzhiyun struct inode *inode = NULL;
1420*4882a593Smuzhiyun struct ext4_iloc iloc;
1421*4882a593Smuzhiyun int inode_len, ino, ret, tag = le16_to_cpu(tl->fc_tag);
1422*4882a593Smuzhiyun struct ext4_extent_header *eh;
1423*4882a593Smuzhiyun
1424*4882a593Smuzhiyun memcpy(&fc_inode, val, sizeof(fc_inode));
1425*4882a593Smuzhiyun
1426*4882a593Smuzhiyun ino = le32_to_cpu(fc_inode.fc_ino);
1427*4882a593Smuzhiyun trace_ext4_fc_replay(sb, tag, ino, 0, 0);
1428*4882a593Smuzhiyun
1429*4882a593Smuzhiyun inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
1430*4882a593Smuzhiyun if (!IS_ERR(inode)) {
1431*4882a593Smuzhiyun ext4_ext_clear_bb(inode);
1432*4882a593Smuzhiyun iput(inode);
1433*4882a593Smuzhiyun }
1434*4882a593Smuzhiyun inode = NULL;
1435*4882a593Smuzhiyun
1436*4882a593Smuzhiyun ret = ext4_fc_record_modified_inode(sb, ino);
1437*4882a593Smuzhiyun if (ret)
1438*4882a593Smuzhiyun goto out;
1439*4882a593Smuzhiyun
1440*4882a593Smuzhiyun raw_fc_inode = (struct ext4_inode *)
1441*4882a593Smuzhiyun (val + offsetof(struct ext4_fc_inode, fc_raw_inode));
1442*4882a593Smuzhiyun ret = ext4_get_fc_inode_loc(sb, ino, &iloc);
1443*4882a593Smuzhiyun if (ret)
1444*4882a593Smuzhiyun goto out;
1445*4882a593Smuzhiyun
1446*4882a593Smuzhiyun inode_len = le16_to_cpu(tl->fc_len) - sizeof(struct ext4_fc_inode);
1447*4882a593Smuzhiyun raw_inode = ext4_raw_inode(&iloc);
1448*4882a593Smuzhiyun
1449*4882a593Smuzhiyun memcpy(raw_inode, raw_fc_inode, offsetof(struct ext4_inode, i_block));
1450*4882a593Smuzhiyun memcpy(&raw_inode->i_generation, &raw_fc_inode->i_generation,
1451*4882a593Smuzhiyun inode_len - offsetof(struct ext4_inode, i_generation));
1452*4882a593Smuzhiyun if (le32_to_cpu(raw_inode->i_flags) & EXT4_EXTENTS_FL) {
1453*4882a593Smuzhiyun eh = (struct ext4_extent_header *)(&raw_inode->i_block[0]);
1454*4882a593Smuzhiyun if (eh->eh_magic != EXT4_EXT_MAGIC) {
1455*4882a593Smuzhiyun memset(eh, 0, sizeof(*eh));
1456*4882a593Smuzhiyun eh->eh_magic = EXT4_EXT_MAGIC;
1457*4882a593Smuzhiyun eh->eh_max = cpu_to_le16(
1458*4882a593Smuzhiyun (sizeof(raw_inode->i_block) -
1459*4882a593Smuzhiyun sizeof(struct ext4_extent_header))
1460*4882a593Smuzhiyun / sizeof(struct ext4_extent));
1461*4882a593Smuzhiyun }
1462*4882a593Smuzhiyun } else if (le32_to_cpu(raw_inode->i_flags) & EXT4_INLINE_DATA_FL) {
1463*4882a593Smuzhiyun memcpy(raw_inode->i_block, raw_fc_inode->i_block,
1464*4882a593Smuzhiyun sizeof(raw_inode->i_block));
1465*4882a593Smuzhiyun }
1466*4882a593Smuzhiyun
1467*4882a593Smuzhiyun /* Immediately update the inode on disk. */
1468*4882a593Smuzhiyun ret = ext4_handle_dirty_metadata(NULL, NULL, iloc.bh);
1469*4882a593Smuzhiyun if (ret)
1470*4882a593Smuzhiyun goto out;
1471*4882a593Smuzhiyun ret = sync_dirty_buffer(iloc.bh);
1472*4882a593Smuzhiyun if (ret)
1473*4882a593Smuzhiyun goto out;
1474*4882a593Smuzhiyun ret = ext4_mark_inode_used(sb, ino);
1475*4882a593Smuzhiyun if (ret)
1476*4882a593Smuzhiyun goto out;
1477*4882a593Smuzhiyun
1478*4882a593Smuzhiyun /* Given that we just wrote the inode on disk, this SHOULD succeed. */
1479*4882a593Smuzhiyun inode = ext4_iget(sb, ino, EXT4_IGET_NORMAL);
1480*4882a593Smuzhiyun if (IS_ERR(inode)) {
1481*4882a593Smuzhiyun jbd_debug(1, "Inode not found.");
1482*4882a593Smuzhiyun return -EFSCORRUPTED;
1483*4882a593Smuzhiyun }
1484*4882a593Smuzhiyun
1485*4882a593Smuzhiyun /*
1486*4882a593Smuzhiyun * Our allocator could have made different decisions than before
1487*4882a593Smuzhiyun * crashing. This should be fixed but until then, we calculate
1488*4882a593Smuzhiyun * the number of blocks the inode.
1489*4882a593Smuzhiyun */
1490*4882a593Smuzhiyun ext4_ext_replay_set_iblocks(inode);
1491*4882a593Smuzhiyun
1492*4882a593Smuzhiyun inode->i_generation = le32_to_cpu(ext4_raw_inode(&iloc)->i_generation);
1493*4882a593Smuzhiyun ext4_reset_inode_seed(inode);
1494*4882a593Smuzhiyun
1495*4882a593Smuzhiyun ext4_inode_csum_set(inode, ext4_raw_inode(&iloc), EXT4_I(inode));
1496*4882a593Smuzhiyun ret = ext4_handle_dirty_metadata(NULL, NULL, iloc.bh);
1497*4882a593Smuzhiyun sync_dirty_buffer(iloc.bh);
1498*4882a593Smuzhiyun brelse(iloc.bh);
1499*4882a593Smuzhiyun out:
1500*4882a593Smuzhiyun iput(inode);
1501*4882a593Smuzhiyun if (!ret)
1502*4882a593Smuzhiyun blkdev_issue_flush(sb->s_bdev, GFP_KERNEL);
1503*4882a593Smuzhiyun
1504*4882a593Smuzhiyun return 0;
1505*4882a593Smuzhiyun }
1506*4882a593Smuzhiyun
1507*4882a593Smuzhiyun /*
1508*4882a593Smuzhiyun * Dentry create replay function.
1509*4882a593Smuzhiyun *
1510*4882a593Smuzhiyun * EXT4_FC_TAG_CREAT is preceded by EXT4_FC_TAG_INODE_FULL. Which means, the
1511*4882a593Smuzhiyun * inode for which we are trying to create a dentry here, should already have
1512*4882a593Smuzhiyun * been replayed before we start here.
1513*4882a593Smuzhiyun */
ext4_fc_replay_create(struct super_block * sb,struct ext4_fc_tl * tl,u8 * val)1514*4882a593Smuzhiyun static int ext4_fc_replay_create(struct super_block *sb, struct ext4_fc_tl *tl,
1515*4882a593Smuzhiyun u8 *val)
1516*4882a593Smuzhiyun {
1517*4882a593Smuzhiyun int ret = 0;
1518*4882a593Smuzhiyun struct inode *inode = NULL;
1519*4882a593Smuzhiyun struct inode *dir = NULL;
1520*4882a593Smuzhiyun struct dentry_info_args darg;
1521*4882a593Smuzhiyun
1522*4882a593Smuzhiyun tl_to_darg(&darg, tl, val);
1523*4882a593Smuzhiyun
1524*4882a593Smuzhiyun trace_ext4_fc_replay(sb, EXT4_FC_TAG_CREAT, darg.ino,
1525*4882a593Smuzhiyun darg.parent_ino, darg.dname_len);
1526*4882a593Smuzhiyun
1527*4882a593Smuzhiyun /* This takes care of update group descriptor and other metadata */
1528*4882a593Smuzhiyun ret = ext4_mark_inode_used(sb, darg.ino);
1529*4882a593Smuzhiyun if (ret)
1530*4882a593Smuzhiyun goto out;
1531*4882a593Smuzhiyun
1532*4882a593Smuzhiyun inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
1533*4882a593Smuzhiyun if (IS_ERR(inode)) {
1534*4882a593Smuzhiyun jbd_debug(1, "inode %d not found.", darg.ino);
1535*4882a593Smuzhiyun inode = NULL;
1536*4882a593Smuzhiyun ret = -EINVAL;
1537*4882a593Smuzhiyun goto out;
1538*4882a593Smuzhiyun }
1539*4882a593Smuzhiyun
1540*4882a593Smuzhiyun if (S_ISDIR(inode->i_mode)) {
1541*4882a593Smuzhiyun /*
1542*4882a593Smuzhiyun * If we are creating a directory, we need to make sure that the
1543*4882a593Smuzhiyun * dot and dot dot dirents are setup properly.
1544*4882a593Smuzhiyun */
1545*4882a593Smuzhiyun dir = ext4_iget(sb, darg.parent_ino, EXT4_IGET_NORMAL);
1546*4882a593Smuzhiyun if (IS_ERR(dir)) {
1547*4882a593Smuzhiyun jbd_debug(1, "Dir %d not found.", darg.ino);
1548*4882a593Smuzhiyun goto out;
1549*4882a593Smuzhiyun }
1550*4882a593Smuzhiyun ret = ext4_init_new_dir(NULL, dir, inode);
1551*4882a593Smuzhiyun iput(dir);
1552*4882a593Smuzhiyun if (ret) {
1553*4882a593Smuzhiyun ret = 0;
1554*4882a593Smuzhiyun goto out;
1555*4882a593Smuzhiyun }
1556*4882a593Smuzhiyun }
1557*4882a593Smuzhiyun ret = ext4_fc_replay_link_internal(sb, &darg, inode);
1558*4882a593Smuzhiyun if (ret)
1559*4882a593Smuzhiyun goto out;
1560*4882a593Smuzhiyun set_nlink(inode, 1);
1561*4882a593Smuzhiyun ext4_mark_inode_dirty(NULL, inode);
1562*4882a593Smuzhiyun out:
1563*4882a593Smuzhiyun if (inode)
1564*4882a593Smuzhiyun iput(inode);
1565*4882a593Smuzhiyun return ret;
1566*4882a593Smuzhiyun }
1567*4882a593Smuzhiyun
1568*4882a593Smuzhiyun /*
1569*4882a593Smuzhiyun * Record physical disk regions which are in use as per fast commit area,
1570*4882a593Smuzhiyun * and used by inodes during replay phase. Our simple replay phase
1571*4882a593Smuzhiyun * allocator excludes these regions from allocation.
1572*4882a593Smuzhiyun */
ext4_fc_record_regions(struct super_block * sb,int ino,ext4_lblk_t lblk,ext4_fsblk_t pblk,int len,int replay)1573*4882a593Smuzhiyun int ext4_fc_record_regions(struct super_block *sb, int ino,
1574*4882a593Smuzhiyun ext4_lblk_t lblk, ext4_fsblk_t pblk, int len, int replay)
1575*4882a593Smuzhiyun {
1576*4882a593Smuzhiyun struct ext4_fc_replay_state *state;
1577*4882a593Smuzhiyun struct ext4_fc_alloc_region *region;
1578*4882a593Smuzhiyun
1579*4882a593Smuzhiyun state = &EXT4_SB(sb)->s_fc_replay_state;
1580*4882a593Smuzhiyun /*
1581*4882a593Smuzhiyun * during replay phase, the fc_regions_valid may not same as
1582*4882a593Smuzhiyun * fc_regions_used, update it when do new additions.
1583*4882a593Smuzhiyun */
1584*4882a593Smuzhiyun if (replay && state->fc_regions_used != state->fc_regions_valid)
1585*4882a593Smuzhiyun state->fc_regions_used = state->fc_regions_valid;
1586*4882a593Smuzhiyun if (state->fc_regions_used == state->fc_regions_size) {
1587*4882a593Smuzhiyun struct ext4_fc_alloc_region *fc_regions;
1588*4882a593Smuzhiyun
1589*4882a593Smuzhiyun fc_regions = krealloc(state->fc_regions,
1590*4882a593Smuzhiyun sizeof(struct ext4_fc_alloc_region) *
1591*4882a593Smuzhiyun (state->fc_regions_size +
1592*4882a593Smuzhiyun EXT4_FC_REPLAY_REALLOC_INCREMENT),
1593*4882a593Smuzhiyun GFP_KERNEL);
1594*4882a593Smuzhiyun if (!fc_regions)
1595*4882a593Smuzhiyun return -ENOMEM;
1596*4882a593Smuzhiyun state->fc_regions_size +=
1597*4882a593Smuzhiyun EXT4_FC_REPLAY_REALLOC_INCREMENT;
1598*4882a593Smuzhiyun state->fc_regions = fc_regions;
1599*4882a593Smuzhiyun }
1600*4882a593Smuzhiyun region = &state->fc_regions[state->fc_regions_used++];
1601*4882a593Smuzhiyun region->ino = ino;
1602*4882a593Smuzhiyun region->lblk = lblk;
1603*4882a593Smuzhiyun region->pblk = pblk;
1604*4882a593Smuzhiyun region->len = len;
1605*4882a593Smuzhiyun
1606*4882a593Smuzhiyun if (replay)
1607*4882a593Smuzhiyun state->fc_regions_valid++;
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun return 0;
1610*4882a593Smuzhiyun }
1611*4882a593Smuzhiyun
1612*4882a593Smuzhiyun /* Replay add range tag */
ext4_fc_replay_add_range(struct super_block * sb,struct ext4_fc_tl * tl,u8 * val)1613*4882a593Smuzhiyun static int ext4_fc_replay_add_range(struct super_block *sb,
1614*4882a593Smuzhiyun struct ext4_fc_tl *tl, u8 *val)
1615*4882a593Smuzhiyun {
1616*4882a593Smuzhiyun struct ext4_fc_add_range fc_add_ex;
1617*4882a593Smuzhiyun struct ext4_extent newex, *ex;
1618*4882a593Smuzhiyun struct inode *inode;
1619*4882a593Smuzhiyun ext4_lblk_t start, cur;
1620*4882a593Smuzhiyun int remaining, len;
1621*4882a593Smuzhiyun ext4_fsblk_t start_pblk;
1622*4882a593Smuzhiyun struct ext4_map_blocks map;
1623*4882a593Smuzhiyun struct ext4_ext_path *path = NULL;
1624*4882a593Smuzhiyun int ret;
1625*4882a593Smuzhiyun
1626*4882a593Smuzhiyun memcpy(&fc_add_ex, val, sizeof(fc_add_ex));
1627*4882a593Smuzhiyun ex = (struct ext4_extent *)&fc_add_ex.fc_ex;
1628*4882a593Smuzhiyun
1629*4882a593Smuzhiyun trace_ext4_fc_replay(sb, EXT4_FC_TAG_ADD_RANGE,
1630*4882a593Smuzhiyun le32_to_cpu(fc_add_ex.fc_ino), le32_to_cpu(ex->ee_block),
1631*4882a593Smuzhiyun ext4_ext_get_actual_len(ex));
1632*4882a593Smuzhiyun
1633*4882a593Smuzhiyun inode = ext4_iget(sb, le32_to_cpu(fc_add_ex.fc_ino), EXT4_IGET_NORMAL);
1634*4882a593Smuzhiyun if (IS_ERR(inode)) {
1635*4882a593Smuzhiyun jbd_debug(1, "Inode not found.");
1636*4882a593Smuzhiyun return 0;
1637*4882a593Smuzhiyun }
1638*4882a593Smuzhiyun
1639*4882a593Smuzhiyun ret = ext4_fc_record_modified_inode(sb, inode->i_ino);
1640*4882a593Smuzhiyun if (ret)
1641*4882a593Smuzhiyun goto out;
1642*4882a593Smuzhiyun
1643*4882a593Smuzhiyun start = le32_to_cpu(ex->ee_block);
1644*4882a593Smuzhiyun start_pblk = ext4_ext_pblock(ex);
1645*4882a593Smuzhiyun len = ext4_ext_get_actual_len(ex);
1646*4882a593Smuzhiyun
1647*4882a593Smuzhiyun cur = start;
1648*4882a593Smuzhiyun remaining = len;
1649*4882a593Smuzhiyun jbd_debug(1, "ADD_RANGE, lblk %d, pblk %lld, len %d, unwritten %d, inode %ld\n",
1650*4882a593Smuzhiyun start, start_pblk, len, ext4_ext_is_unwritten(ex),
1651*4882a593Smuzhiyun inode->i_ino);
1652*4882a593Smuzhiyun
1653*4882a593Smuzhiyun while (remaining > 0) {
1654*4882a593Smuzhiyun map.m_lblk = cur;
1655*4882a593Smuzhiyun map.m_len = remaining;
1656*4882a593Smuzhiyun map.m_pblk = 0;
1657*4882a593Smuzhiyun ret = ext4_map_blocks(NULL, inode, &map, 0);
1658*4882a593Smuzhiyun
1659*4882a593Smuzhiyun if (ret < 0)
1660*4882a593Smuzhiyun goto out;
1661*4882a593Smuzhiyun
1662*4882a593Smuzhiyun if (ret == 0) {
1663*4882a593Smuzhiyun /* Range is not mapped */
1664*4882a593Smuzhiyun path = ext4_find_extent(inode, cur, NULL, 0);
1665*4882a593Smuzhiyun if (IS_ERR(path))
1666*4882a593Smuzhiyun goto out;
1667*4882a593Smuzhiyun memset(&newex, 0, sizeof(newex));
1668*4882a593Smuzhiyun newex.ee_block = cpu_to_le32(cur);
1669*4882a593Smuzhiyun ext4_ext_store_pblock(
1670*4882a593Smuzhiyun &newex, start_pblk + cur - start);
1671*4882a593Smuzhiyun newex.ee_len = cpu_to_le16(map.m_len);
1672*4882a593Smuzhiyun if (ext4_ext_is_unwritten(ex))
1673*4882a593Smuzhiyun ext4_ext_mark_unwritten(&newex);
1674*4882a593Smuzhiyun down_write(&EXT4_I(inode)->i_data_sem);
1675*4882a593Smuzhiyun ret = ext4_ext_insert_extent(
1676*4882a593Smuzhiyun NULL, inode, &path, &newex, 0);
1677*4882a593Smuzhiyun up_write((&EXT4_I(inode)->i_data_sem));
1678*4882a593Smuzhiyun ext4_ext_drop_refs(path);
1679*4882a593Smuzhiyun kfree(path);
1680*4882a593Smuzhiyun if (ret)
1681*4882a593Smuzhiyun goto out;
1682*4882a593Smuzhiyun goto next;
1683*4882a593Smuzhiyun }
1684*4882a593Smuzhiyun
1685*4882a593Smuzhiyun if (start_pblk + cur - start != map.m_pblk) {
1686*4882a593Smuzhiyun /*
1687*4882a593Smuzhiyun * Logical to physical mapping changed. This can happen
1688*4882a593Smuzhiyun * if this range was removed and then reallocated to
1689*4882a593Smuzhiyun * map to new physical blocks during a fast commit.
1690*4882a593Smuzhiyun */
1691*4882a593Smuzhiyun ret = ext4_ext_replay_update_ex(inode, cur, map.m_len,
1692*4882a593Smuzhiyun ext4_ext_is_unwritten(ex),
1693*4882a593Smuzhiyun start_pblk + cur - start);
1694*4882a593Smuzhiyun if (ret)
1695*4882a593Smuzhiyun goto out;
1696*4882a593Smuzhiyun /*
1697*4882a593Smuzhiyun * Mark the old blocks as free since they aren't used
1698*4882a593Smuzhiyun * anymore. We maintain an array of all the modified
1699*4882a593Smuzhiyun * inodes. In case these blocks are still used at either
1700*4882a593Smuzhiyun * a different logical range in the same inode or in
1701*4882a593Smuzhiyun * some different inode, we will mark them as allocated
1702*4882a593Smuzhiyun * at the end of the FC replay using our array of
1703*4882a593Smuzhiyun * modified inodes.
1704*4882a593Smuzhiyun */
1705*4882a593Smuzhiyun ext4_mb_mark_bb(inode->i_sb, map.m_pblk, map.m_len, 0);
1706*4882a593Smuzhiyun goto next;
1707*4882a593Smuzhiyun }
1708*4882a593Smuzhiyun
1709*4882a593Smuzhiyun /* Range is mapped and needs a state change */
1710*4882a593Smuzhiyun jbd_debug(1, "Converting from %ld to %d %lld",
1711*4882a593Smuzhiyun map.m_flags & EXT4_MAP_UNWRITTEN,
1712*4882a593Smuzhiyun ext4_ext_is_unwritten(ex), map.m_pblk);
1713*4882a593Smuzhiyun ret = ext4_ext_replay_update_ex(inode, cur, map.m_len,
1714*4882a593Smuzhiyun ext4_ext_is_unwritten(ex), map.m_pblk);
1715*4882a593Smuzhiyun if (ret)
1716*4882a593Smuzhiyun goto out;
1717*4882a593Smuzhiyun /*
1718*4882a593Smuzhiyun * We may have split the extent tree while toggling the state.
1719*4882a593Smuzhiyun * Try to shrink the extent tree now.
1720*4882a593Smuzhiyun */
1721*4882a593Smuzhiyun ext4_ext_replay_shrink_inode(inode, start + len);
1722*4882a593Smuzhiyun next:
1723*4882a593Smuzhiyun cur += map.m_len;
1724*4882a593Smuzhiyun remaining -= map.m_len;
1725*4882a593Smuzhiyun }
1726*4882a593Smuzhiyun ext4_ext_replay_shrink_inode(inode, i_size_read(inode) >>
1727*4882a593Smuzhiyun sb->s_blocksize_bits);
1728*4882a593Smuzhiyun out:
1729*4882a593Smuzhiyun iput(inode);
1730*4882a593Smuzhiyun return 0;
1731*4882a593Smuzhiyun }
1732*4882a593Smuzhiyun
1733*4882a593Smuzhiyun /* Replay DEL_RANGE tag */
1734*4882a593Smuzhiyun static int
ext4_fc_replay_del_range(struct super_block * sb,struct ext4_fc_tl * tl,u8 * val)1735*4882a593Smuzhiyun ext4_fc_replay_del_range(struct super_block *sb, struct ext4_fc_tl *tl,
1736*4882a593Smuzhiyun u8 *val)
1737*4882a593Smuzhiyun {
1738*4882a593Smuzhiyun struct inode *inode;
1739*4882a593Smuzhiyun struct ext4_fc_del_range lrange;
1740*4882a593Smuzhiyun struct ext4_map_blocks map;
1741*4882a593Smuzhiyun ext4_lblk_t cur, remaining;
1742*4882a593Smuzhiyun int ret;
1743*4882a593Smuzhiyun
1744*4882a593Smuzhiyun memcpy(&lrange, val, sizeof(lrange));
1745*4882a593Smuzhiyun cur = le32_to_cpu(lrange.fc_lblk);
1746*4882a593Smuzhiyun remaining = le32_to_cpu(lrange.fc_len);
1747*4882a593Smuzhiyun
1748*4882a593Smuzhiyun trace_ext4_fc_replay(sb, EXT4_FC_TAG_DEL_RANGE,
1749*4882a593Smuzhiyun le32_to_cpu(lrange.fc_ino), cur, remaining);
1750*4882a593Smuzhiyun
1751*4882a593Smuzhiyun inode = ext4_iget(sb, le32_to_cpu(lrange.fc_ino), EXT4_IGET_NORMAL);
1752*4882a593Smuzhiyun if (IS_ERR(inode)) {
1753*4882a593Smuzhiyun jbd_debug(1, "Inode %d not found", le32_to_cpu(lrange.fc_ino));
1754*4882a593Smuzhiyun return 0;
1755*4882a593Smuzhiyun }
1756*4882a593Smuzhiyun
1757*4882a593Smuzhiyun ret = ext4_fc_record_modified_inode(sb, inode->i_ino);
1758*4882a593Smuzhiyun if (ret)
1759*4882a593Smuzhiyun goto out;
1760*4882a593Smuzhiyun
1761*4882a593Smuzhiyun jbd_debug(1, "DEL_RANGE, inode %ld, lblk %d, len %d\n",
1762*4882a593Smuzhiyun inode->i_ino, le32_to_cpu(lrange.fc_lblk),
1763*4882a593Smuzhiyun le32_to_cpu(lrange.fc_len));
1764*4882a593Smuzhiyun while (remaining > 0) {
1765*4882a593Smuzhiyun map.m_lblk = cur;
1766*4882a593Smuzhiyun map.m_len = remaining;
1767*4882a593Smuzhiyun
1768*4882a593Smuzhiyun ret = ext4_map_blocks(NULL, inode, &map, 0);
1769*4882a593Smuzhiyun if (ret < 0)
1770*4882a593Smuzhiyun goto out;
1771*4882a593Smuzhiyun if (ret > 0) {
1772*4882a593Smuzhiyun remaining -= ret;
1773*4882a593Smuzhiyun cur += ret;
1774*4882a593Smuzhiyun ext4_mb_mark_bb(inode->i_sb, map.m_pblk, map.m_len, 0);
1775*4882a593Smuzhiyun } else {
1776*4882a593Smuzhiyun remaining -= map.m_len;
1777*4882a593Smuzhiyun cur += map.m_len;
1778*4882a593Smuzhiyun }
1779*4882a593Smuzhiyun }
1780*4882a593Smuzhiyun
1781*4882a593Smuzhiyun down_write(&EXT4_I(inode)->i_data_sem);
1782*4882a593Smuzhiyun ret = ext4_ext_remove_space(inode, le32_to_cpu(lrange.fc_lblk),
1783*4882a593Smuzhiyun le32_to_cpu(lrange.fc_lblk) +
1784*4882a593Smuzhiyun le32_to_cpu(lrange.fc_len) - 1);
1785*4882a593Smuzhiyun up_write(&EXT4_I(inode)->i_data_sem);
1786*4882a593Smuzhiyun if (ret)
1787*4882a593Smuzhiyun goto out;
1788*4882a593Smuzhiyun ext4_ext_replay_shrink_inode(inode,
1789*4882a593Smuzhiyun i_size_read(inode) >> sb->s_blocksize_bits);
1790*4882a593Smuzhiyun ext4_mark_inode_dirty(NULL, inode);
1791*4882a593Smuzhiyun out:
1792*4882a593Smuzhiyun iput(inode);
1793*4882a593Smuzhiyun return 0;
1794*4882a593Smuzhiyun }
1795*4882a593Smuzhiyun
tag2str(u16 tag)1796*4882a593Smuzhiyun static inline const char *tag2str(u16 tag)
1797*4882a593Smuzhiyun {
1798*4882a593Smuzhiyun switch (tag) {
1799*4882a593Smuzhiyun case EXT4_FC_TAG_LINK:
1800*4882a593Smuzhiyun return "TAG_ADD_ENTRY";
1801*4882a593Smuzhiyun case EXT4_FC_TAG_UNLINK:
1802*4882a593Smuzhiyun return "TAG_DEL_ENTRY";
1803*4882a593Smuzhiyun case EXT4_FC_TAG_ADD_RANGE:
1804*4882a593Smuzhiyun return "TAG_ADD_RANGE";
1805*4882a593Smuzhiyun case EXT4_FC_TAG_CREAT:
1806*4882a593Smuzhiyun return "TAG_CREAT_DENTRY";
1807*4882a593Smuzhiyun case EXT4_FC_TAG_DEL_RANGE:
1808*4882a593Smuzhiyun return "TAG_DEL_RANGE";
1809*4882a593Smuzhiyun case EXT4_FC_TAG_INODE:
1810*4882a593Smuzhiyun return "TAG_INODE";
1811*4882a593Smuzhiyun case EXT4_FC_TAG_PAD:
1812*4882a593Smuzhiyun return "TAG_PAD";
1813*4882a593Smuzhiyun case EXT4_FC_TAG_TAIL:
1814*4882a593Smuzhiyun return "TAG_TAIL";
1815*4882a593Smuzhiyun case EXT4_FC_TAG_HEAD:
1816*4882a593Smuzhiyun return "TAG_HEAD";
1817*4882a593Smuzhiyun default:
1818*4882a593Smuzhiyun return "TAG_ERROR";
1819*4882a593Smuzhiyun }
1820*4882a593Smuzhiyun }
1821*4882a593Smuzhiyun
ext4_fc_set_bitmaps_and_counters(struct super_block * sb)1822*4882a593Smuzhiyun static void ext4_fc_set_bitmaps_and_counters(struct super_block *sb)
1823*4882a593Smuzhiyun {
1824*4882a593Smuzhiyun struct ext4_fc_replay_state *state;
1825*4882a593Smuzhiyun struct inode *inode;
1826*4882a593Smuzhiyun struct ext4_ext_path *path = NULL;
1827*4882a593Smuzhiyun struct ext4_map_blocks map;
1828*4882a593Smuzhiyun int i, ret, j;
1829*4882a593Smuzhiyun ext4_lblk_t cur, end;
1830*4882a593Smuzhiyun
1831*4882a593Smuzhiyun state = &EXT4_SB(sb)->s_fc_replay_state;
1832*4882a593Smuzhiyun for (i = 0; i < state->fc_modified_inodes_used; i++) {
1833*4882a593Smuzhiyun inode = ext4_iget(sb, state->fc_modified_inodes[i],
1834*4882a593Smuzhiyun EXT4_IGET_NORMAL);
1835*4882a593Smuzhiyun if (IS_ERR(inode)) {
1836*4882a593Smuzhiyun jbd_debug(1, "Inode %d not found.",
1837*4882a593Smuzhiyun state->fc_modified_inodes[i]);
1838*4882a593Smuzhiyun continue;
1839*4882a593Smuzhiyun }
1840*4882a593Smuzhiyun cur = 0;
1841*4882a593Smuzhiyun end = EXT_MAX_BLOCKS;
1842*4882a593Smuzhiyun while (cur < end) {
1843*4882a593Smuzhiyun map.m_lblk = cur;
1844*4882a593Smuzhiyun map.m_len = end - cur;
1845*4882a593Smuzhiyun
1846*4882a593Smuzhiyun ret = ext4_map_blocks(NULL, inode, &map, 0);
1847*4882a593Smuzhiyun if (ret < 0)
1848*4882a593Smuzhiyun break;
1849*4882a593Smuzhiyun
1850*4882a593Smuzhiyun if (ret > 0) {
1851*4882a593Smuzhiyun path = ext4_find_extent(inode, map.m_lblk, NULL, 0);
1852*4882a593Smuzhiyun if (!IS_ERR(path)) {
1853*4882a593Smuzhiyun for (j = 0; j < path->p_depth; j++)
1854*4882a593Smuzhiyun ext4_mb_mark_bb(inode->i_sb,
1855*4882a593Smuzhiyun path[j].p_block, 1, 1);
1856*4882a593Smuzhiyun ext4_ext_drop_refs(path);
1857*4882a593Smuzhiyun kfree(path);
1858*4882a593Smuzhiyun }
1859*4882a593Smuzhiyun cur += ret;
1860*4882a593Smuzhiyun ext4_mb_mark_bb(inode->i_sb, map.m_pblk,
1861*4882a593Smuzhiyun map.m_len, 1);
1862*4882a593Smuzhiyun } else {
1863*4882a593Smuzhiyun cur = cur + (map.m_len ? map.m_len : 1);
1864*4882a593Smuzhiyun }
1865*4882a593Smuzhiyun }
1866*4882a593Smuzhiyun iput(inode);
1867*4882a593Smuzhiyun }
1868*4882a593Smuzhiyun }
1869*4882a593Smuzhiyun
1870*4882a593Smuzhiyun /*
1871*4882a593Smuzhiyun * Check if block is in excluded regions for block allocation. The simple
1872*4882a593Smuzhiyun * allocator that runs during replay phase is calls this function to see
1873*4882a593Smuzhiyun * if it is okay to use a block.
1874*4882a593Smuzhiyun */
ext4_fc_replay_check_excluded(struct super_block * sb,ext4_fsblk_t blk)1875*4882a593Smuzhiyun bool ext4_fc_replay_check_excluded(struct super_block *sb, ext4_fsblk_t blk)
1876*4882a593Smuzhiyun {
1877*4882a593Smuzhiyun int i;
1878*4882a593Smuzhiyun struct ext4_fc_replay_state *state;
1879*4882a593Smuzhiyun
1880*4882a593Smuzhiyun state = &EXT4_SB(sb)->s_fc_replay_state;
1881*4882a593Smuzhiyun for (i = 0; i < state->fc_regions_valid; i++) {
1882*4882a593Smuzhiyun if (state->fc_regions[i].ino == 0 ||
1883*4882a593Smuzhiyun state->fc_regions[i].len == 0)
1884*4882a593Smuzhiyun continue;
1885*4882a593Smuzhiyun if (blk >= state->fc_regions[i].pblk &&
1886*4882a593Smuzhiyun blk < state->fc_regions[i].pblk + state->fc_regions[i].len)
1887*4882a593Smuzhiyun return true;
1888*4882a593Smuzhiyun }
1889*4882a593Smuzhiyun return false;
1890*4882a593Smuzhiyun }
1891*4882a593Smuzhiyun
1892*4882a593Smuzhiyun /* Cleanup function called after replay */
ext4_fc_replay_cleanup(struct super_block * sb)1893*4882a593Smuzhiyun void ext4_fc_replay_cleanup(struct super_block *sb)
1894*4882a593Smuzhiyun {
1895*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(sb);
1896*4882a593Smuzhiyun
1897*4882a593Smuzhiyun sbi->s_mount_state &= ~EXT4_FC_REPLAY;
1898*4882a593Smuzhiyun kfree(sbi->s_fc_replay_state.fc_regions);
1899*4882a593Smuzhiyun kfree(sbi->s_fc_replay_state.fc_modified_inodes);
1900*4882a593Smuzhiyun }
1901*4882a593Smuzhiyun
1902*4882a593Smuzhiyun /*
1903*4882a593Smuzhiyun * Recovery Scan phase handler
1904*4882a593Smuzhiyun *
1905*4882a593Smuzhiyun * This function is called during the scan phase and is responsible
1906*4882a593Smuzhiyun * for doing following things:
1907*4882a593Smuzhiyun * - Make sure the fast commit area has valid tags for replay
1908*4882a593Smuzhiyun * - Count number of tags that need to be replayed by the replay handler
1909*4882a593Smuzhiyun * - Verify CRC
1910*4882a593Smuzhiyun * - Create a list of excluded blocks for allocation during replay phase
1911*4882a593Smuzhiyun *
1912*4882a593Smuzhiyun * This function returns JBD2_FC_REPLAY_CONTINUE to indicate that SCAN is
1913*4882a593Smuzhiyun * incomplete and JBD2 should send more blocks. It returns JBD2_FC_REPLAY_STOP
1914*4882a593Smuzhiyun * to indicate that scan has finished and JBD2 can now start replay phase.
1915*4882a593Smuzhiyun * It returns a negative error to indicate that there was an error. At the end
1916*4882a593Smuzhiyun * of a successful scan phase, sbi->s_fc_replay_state.fc_replay_num_tags is set
1917*4882a593Smuzhiyun * to indicate the number of tags that need to replayed during the replay phase.
1918*4882a593Smuzhiyun */
ext4_fc_replay_scan(journal_t * journal,struct buffer_head * bh,int off,tid_t expected_tid)1919*4882a593Smuzhiyun static int ext4_fc_replay_scan(journal_t *journal,
1920*4882a593Smuzhiyun struct buffer_head *bh, int off,
1921*4882a593Smuzhiyun tid_t expected_tid)
1922*4882a593Smuzhiyun {
1923*4882a593Smuzhiyun struct super_block *sb = journal->j_private;
1924*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(sb);
1925*4882a593Smuzhiyun struct ext4_fc_replay_state *state;
1926*4882a593Smuzhiyun int ret = JBD2_FC_REPLAY_CONTINUE;
1927*4882a593Smuzhiyun struct ext4_fc_add_range ext;
1928*4882a593Smuzhiyun struct ext4_fc_tl tl;
1929*4882a593Smuzhiyun struct ext4_fc_tail tail;
1930*4882a593Smuzhiyun __u8 *start, *end, *cur, *val;
1931*4882a593Smuzhiyun struct ext4_fc_head head;
1932*4882a593Smuzhiyun struct ext4_extent *ex;
1933*4882a593Smuzhiyun
1934*4882a593Smuzhiyun state = &sbi->s_fc_replay_state;
1935*4882a593Smuzhiyun
1936*4882a593Smuzhiyun start = (u8 *)bh->b_data;
1937*4882a593Smuzhiyun end = (__u8 *)bh->b_data + journal->j_blocksize - 1;
1938*4882a593Smuzhiyun
1939*4882a593Smuzhiyun if (state->fc_replay_expected_off == 0) {
1940*4882a593Smuzhiyun state->fc_cur_tag = 0;
1941*4882a593Smuzhiyun state->fc_replay_num_tags = 0;
1942*4882a593Smuzhiyun state->fc_crc = 0;
1943*4882a593Smuzhiyun state->fc_regions = NULL;
1944*4882a593Smuzhiyun state->fc_regions_valid = state->fc_regions_used =
1945*4882a593Smuzhiyun state->fc_regions_size = 0;
1946*4882a593Smuzhiyun /* Check if we can stop early */
1947*4882a593Smuzhiyun if (le16_to_cpu(((struct ext4_fc_tl *)start)->fc_tag)
1948*4882a593Smuzhiyun != EXT4_FC_TAG_HEAD)
1949*4882a593Smuzhiyun return 0;
1950*4882a593Smuzhiyun }
1951*4882a593Smuzhiyun
1952*4882a593Smuzhiyun if (off != state->fc_replay_expected_off) {
1953*4882a593Smuzhiyun ret = -EFSCORRUPTED;
1954*4882a593Smuzhiyun goto out_err;
1955*4882a593Smuzhiyun }
1956*4882a593Smuzhiyun
1957*4882a593Smuzhiyun state->fc_replay_expected_off++;
1958*4882a593Smuzhiyun for (cur = start; cur < end; cur = cur + sizeof(tl) + le16_to_cpu(tl.fc_len)) {
1959*4882a593Smuzhiyun memcpy(&tl, cur, sizeof(tl));
1960*4882a593Smuzhiyun val = cur + sizeof(tl);
1961*4882a593Smuzhiyun jbd_debug(3, "Scan phase, tag:%s, blk %lld\n",
1962*4882a593Smuzhiyun tag2str(le16_to_cpu(tl.fc_tag)), bh->b_blocknr);
1963*4882a593Smuzhiyun switch (le16_to_cpu(tl.fc_tag)) {
1964*4882a593Smuzhiyun case EXT4_FC_TAG_ADD_RANGE:
1965*4882a593Smuzhiyun memcpy(&ext, val, sizeof(ext));
1966*4882a593Smuzhiyun ex = (struct ext4_extent *)&ext.fc_ex;
1967*4882a593Smuzhiyun ret = ext4_fc_record_regions(sb,
1968*4882a593Smuzhiyun le32_to_cpu(ext.fc_ino),
1969*4882a593Smuzhiyun le32_to_cpu(ex->ee_block), ext4_ext_pblock(ex),
1970*4882a593Smuzhiyun ext4_ext_get_actual_len(ex), 0);
1971*4882a593Smuzhiyun if (ret < 0)
1972*4882a593Smuzhiyun break;
1973*4882a593Smuzhiyun ret = JBD2_FC_REPLAY_CONTINUE;
1974*4882a593Smuzhiyun fallthrough;
1975*4882a593Smuzhiyun case EXT4_FC_TAG_DEL_RANGE:
1976*4882a593Smuzhiyun case EXT4_FC_TAG_LINK:
1977*4882a593Smuzhiyun case EXT4_FC_TAG_UNLINK:
1978*4882a593Smuzhiyun case EXT4_FC_TAG_CREAT:
1979*4882a593Smuzhiyun case EXT4_FC_TAG_INODE:
1980*4882a593Smuzhiyun case EXT4_FC_TAG_PAD:
1981*4882a593Smuzhiyun state->fc_cur_tag++;
1982*4882a593Smuzhiyun state->fc_crc = ext4_chksum(sbi, state->fc_crc, cur,
1983*4882a593Smuzhiyun sizeof(tl) + le16_to_cpu(tl.fc_len));
1984*4882a593Smuzhiyun break;
1985*4882a593Smuzhiyun case EXT4_FC_TAG_TAIL:
1986*4882a593Smuzhiyun state->fc_cur_tag++;
1987*4882a593Smuzhiyun memcpy(&tail, val, sizeof(tail));
1988*4882a593Smuzhiyun state->fc_crc = ext4_chksum(sbi, state->fc_crc, cur,
1989*4882a593Smuzhiyun sizeof(tl) +
1990*4882a593Smuzhiyun offsetof(struct ext4_fc_tail,
1991*4882a593Smuzhiyun fc_crc));
1992*4882a593Smuzhiyun if (le32_to_cpu(tail.fc_tid) == expected_tid &&
1993*4882a593Smuzhiyun le32_to_cpu(tail.fc_crc) == state->fc_crc) {
1994*4882a593Smuzhiyun state->fc_replay_num_tags = state->fc_cur_tag;
1995*4882a593Smuzhiyun state->fc_regions_valid =
1996*4882a593Smuzhiyun state->fc_regions_used;
1997*4882a593Smuzhiyun } else {
1998*4882a593Smuzhiyun ret = state->fc_replay_num_tags ?
1999*4882a593Smuzhiyun JBD2_FC_REPLAY_STOP : -EFSBADCRC;
2000*4882a593Smuzhiyun }
2001*4882a593Smuzhiyun state->fc_crc = 0;
2002*4882a593Smuzhiyun break;
2003*4882a593Smuzhiyun case EXT4_FC_TAG_HEAD:
2004*4882a593Smuzhiyun memcpy(&head, val, sizeof(head));
2005*4882a593Smuzhiyun if (le32_to_cpu(head.fc_features) &
2006*4882a593Smuzhiyun ~EXT4_FC_SUPPORTED_FEATURES) {
2007*4882a593Smuzhiyun ret = -EOPNOTSUPP;
2008*4882a593Smuzhiyun break;
2009*4882a593Smuzhiyun }
2010*4882a593Smuzhiyun if (le32_to_cpu(head.fc_tid) != expected_tid) {
2011*4882a593Smuzhiyun ret = JBD2_FC_REPLAY_STOP;
2012*4882a593Smuzhiyun break;
2013*4882a593Smuzhiyun }
2014*4882a593Smuzhiyun state->fc_cur_tag++;
2015*4882a593Smuzhiyun state->fc_crc = ext4_chksum(sbi, state->fc_crc, cur,
2016*4882a593Smuzhiyun sizeof(tl) + le16_to_cpu(tl.fc_len));
2017*4882a593Smuzhiyun break;
2018*4882a593Smuzhiyun default:
2019*4882a593Smuzhiyun ret = state->fc_replay_num_tags ?
2020*4882a593Smuzhiyun JBD2_FC_REPLAY_STOP : -ECANCELED;
2021*4882a593Smuzhiyun }
2022*4882a593Smuzhiyun if (ret < 0 || ret == JBD2_FC_REPLAY_STOP)
2023*4882a593Smuzhiyun break;
2024*4882a593Smuzhiyun }
2025*4882a593Smuzhiyun
2026*4882a593Smuzhiyun out_err:
2027*4882a593Smuzhiyun trace_ext4_fc_replay_scan(sb, ret, off);
2028*4882a593Smuzhiyun return ret;
2029*4882a593Smuzhiyun }
2030*4882a593Smuzhiyun
2031*4882a593Smuzhiyun /*
2032*4882a593Smuzhiyun * Main recovery path entry point.
2033*4882a593Smuzhiyun * The meaning of return codes is similar as above.
2034*4882a593Smuzhiyun */
ext4_fc_replay(journal_t * journal,struct buffer_head * bh,enum passtype pass,int off,tid_t expected_tid)2035*4882a593Smuzhiyun static int ext4_fc_replay(journal_t *journal, struct buffer_head *bh,
2036*4882a593Smuzhiyun enum passtype pass, int off, tid_t expected_tid)
2037*4882a593Smuzhiyun {
2038*4882a593Smuzhiyun struct super_block *sb = journal->j_private;
2039*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB(sb);
2040*4882a593Smuzhiyun struct ext4_fc_tl tl;
2041*4882a593Smuzhiyun __u8 *start, *end, *cur, *val;
2042*4882a593Smuzhiyun int ret = JBD2_FC_REPLAY_CONTINUE;
2043*4882a593Smuzhiyun struct ext4_fc_replay_state *state = &sbi->s_fc_replay_state;
2044*4882a593Smuzhiyun struct ext4_fc_tail tail;
2045*4882a593Smuzhiyun
2046*4882a593Smuzhiyun if (pass == PASS_SCAN) {
2047*4882a593Smuzhiyun state->fc_current_pass = PASS_SCAN;
2048*4882a593Smuzhiyun return ext4_fc_replay_scan(journal, bh, off, expected_tid);
2049*4882a593Smuzhiyun }
2050*4882a593Smuzhiyun
2051*4882a593Smuzhiyun if (state->fc_current_pass != pass) {
2052*4882a593Smuzhiyun state->fc_current_pass = pass;
2053*4882a593Smuzhiyun sbi->s_mount_state |= EXT4_FC_REPLAY;
2054*4882a593Smuzhiyun }
2055*4882a593Smuzhiyun if (!sbi->s_fc_replay_state.fc_replay_num_tags) {
2056*4882a593Smuzhiyun jbd_debug(1, "Replay stops\n");
2057*4882a593Smuzhiyun ext4_fc_set_bitmaps_and_counters(sb);
2058*4882a593Smuzhiyun return 0;
2059*4882a593Smuzhiyun }
2060*4882a593Smuzhiyun
2061*4882a593Smuzhiyun #ifdef CONFIG_EXT4_DEBUG
2062*4882a593Smuzhiyun if (sbi->s_fc_debug_max_replay && off >= sbi->s_fc_debug_max_replay) {
2063*4882a593Smuzhiyun pr_warn("Dropping fc block %d because max_replay set\n", off);
2064*4882a593Smuzhiyun return JBD2_FC_REPLAY_STOP;
2065*4882a593Smuzhiyun }
2066*4882a593Smuzhiyun #endif
2067*4882a593Smuzhiyun
2068*4882a593Smuzhiyun start = (u8 *)bh->b_data;
2069*4882a593Smuzhiyun end = (__u8 *)bh->b_data + journal->j_blocksize - 1;
2070*4882a593Smuzhiyun
2071*4882a593Smuzhiyun for (cur = start; cur < end; cur = cur + sizeof(tl) + le16_to_cpu(tl.fc_len)) {
2072*4882a593Smuzhiyun memcpy(&tl, cur, sizeof(tl));
2073*4882a593Smuzhiyun val = cur + sizeof(tl);
2074*4882a593Smuzhiyun
2075*4882a593Smuzhiyun if (state->fc_replay_num_tags == 0) {
2076*4882a593Smuzhiyun ret = JBD2_FC_REPLAY_STOP;
2077*4882a593Smuzhiyun ext4_fc_set_bitmaps_and_counters(sb);
2078*4882a593Smuzhiyun break;
2079*4882a593Smuzhiyun }
2080*4882a593Smuzhiyun jbd_debug(3, "Replay phase, tag:%s\n",
2081*4882a593Smuzhiyun tag2str(le16_to_cpu(tl.fc_tag)));
2082*4882a593Smuzhiyun state->fc_replay_num_tags--;
2083*4882a593Smuzhiyun switch (le16_to_cpu(tl.fc_tag)) {
2084*4882a593Smuzhiyun case EXT4_FC_TAG_LINK:
2085*4882a593Smuzhiyun ret = ext4_fc_replay_link(sb, &tl, val);
2086*4882a593Smuzhiyun break;
2087*4882a593Smuzhiyun case EXT4_FC_TAG_UNLINK:
2088*4882a593Smuzhiyun ret = ext4_fc_replay_unlink(sb, &tl, val);
2089*4882a593Smuzhiyun break;
2090*4882a593Smuzhiyun case EXT4_FC_TAG_ADD_RANGE:
2091*4882a593Smuzhiyun ret = ext4_fc_replay_add_range(sb, &tl, val);
2092*4882a593Smuzhiyun break;
2093*4882a593Smuzhiyun case EXT4_FC_TAG_CREAT:
2094*4882a593Smuzhiyun ret = ext4_fc_replay_create(sb, &tl, val);
2095*4882a593Smuzhiyun break;
2096*4882a593Smuzhiyun case EXT4_FC_TAG_DEL_RANGE:
2097*4882a593Smuzhiyun ret = ext4_fc_replay_del_range(sb, &tl, val);
2098*4882a593Smuzhiyun break;
2099*4882a593Smuzhiyun case EXT4_FC_TAG_INODE:
2100*4882a593Smuzhiyun ret = ext4_fc_replay_inode(sb, &tl, val);
2101*4882a593Smuzhiyun break;
2102*4882a593Smuzhiyun case EXT4_FC_TAG_PAD:
2103*4882a593Smuzhiyun trace_ext4_fc_replay(sb, EXT4_FC_TAG_PAD, 0,
2104*4882a593Smuzhiyun le16_to_cpu(tl.fc_len), 0);
2105*4882a593Smuzhiyun break;
2106*4882a593Smuzhiyun case EXT4_FC_TAG_TAIL:
2107*4882a593Smuzhiyun trace_ext4_fc_replay(sb, EXT4_FC_TAG_TAIL, 0,
2108*4882a593Smuzhiyun le16_to_cpu(tl.fc_len), 0);
2109*4882a593Smuzhiyun memcpy(&tail, val, sizeof(tail));
2110*4882a593Smuzhiyun WARN_ON(le32_to_cpu(tail.fc_tid) != expected_tid);
2111*4882a593Smuzhiyun break;
2112*4882a593Smuzhiyun case EXT4_FC_TAG_HEAD:
2113*4882a593Smuzhiyun break;
2114*4882a593Smuzhiyun default:
2115*4882a593Smuzhiyun trace_ext4_fc_replay(sb, le16_to_cpu(tl.fc_tag), 0,
2116*4882a593Smuzhiyun le16_to_cpu(tl.fc_len), 0);
2117*4882a593Smuzhiyun ret = -ECANCELED;
2118*4882a593Smuzhiyun break;
2119*4882a593Smuzhiyun }
2120*4882a593Smuzhiyun if (ret < 0)
2121*4882a593Smuzhiyun break;
2122*4882a593Smuzhiyun ret = JBD2_FC_REPLAY_CONTINUE;
2123*4882a593Smuzhiyun }
2124*4882a593Smuzhiyun return ret;
2125*4882a593Smuzhiyun }
2126*4882a593Smuzhiyun
ext4_fc_init(struct super_block * sb,journal_t * journal)2127*4882a593Smuzhiyun void ext4_fc_init(struct super_block *sb, journal_t *journal)
2128*4882a593Smuzhiyun {
2129*4882a593Smuzhiyun /*
2130*4882a593Smuzhiyun * We set replay callback even if fast commit disabled because we may
2131*4882a593Smuzhiyun * could still have fast commit blocks that need to be replayed even if
2132*4882a593Smuzhiyun * fast commit has now been turned off.
2133*4882a593Smuzhiyun */
2134*4882a593Smuzhiyun journal->j_fc_replay_callback = ext4_fc_replay;
2135*4882a593Smuzhiyun if (!test_opt2(sb, JOURNAL_FAST_COMMIT))
2136*4882a593Smuzhiyun return;
2137*4882a593Smuzhiyun journal->j_fc_cleanup_callback = ext4_fc_cleanup;
2138*4882a593Smuzhiyun }
2139*4882a593Smuzhiyun
2140*4882a593Smuzhiyun static const char *fc_ineligible_reasons[] = {
2141*4882a593Smuzhiyun "Extended attributes changed",
2142*4882a593Smuzhiyun "Cross rename",
2143*4882a593Smuzhiyun "Journal flag changed",
2144*4882a593Smuzhiyun "Insufficient memory",
2145*4882a593Smuzhiyun "Swap boot",
2146*4882a593Smuzhiyun "Resize",
2147*4882a593Smuzhiyun "Dir renamed",
2148*4882a593Smuzhiyun "Falloc range op",
2149*4882a593Smuzhiyun "Data journalling",
2150*4882a593Smuzhiyun "FC Commit Failed"
2151*4882a593Smuzhiyun };
2152*4882a593Smuzhiyun
ext4_fc_info_show(struct seq_file * seq,void * v)2153*4882a593Smuzhiyun int ext4_fc_info_show(struct seq_file *seq, void *v)
2154*4882a593Smuzhiyun {
2155*4882a593Smuzhiyun struct ext4_sb_info *sbi = EXT4_SB((struct super_block *)seq->private);
2156*4882a593Smuzhiyun struct ext4_fc_stats *stats = &sbi->s_fc_stats;
2157*4882a593Smuzhiyun int i;
2158*4882a593Smuzhiyun
2159*4882a593Smuzhiyun if (v != SEQ_START_TOKEN)
2160*4882a593Smuzhiyun return 0;
2161*4882a593Smuzhiyun
2162*4882a593Smuzhiyun seq_printf(seq,
2163*4882a593Smuzhiyun "fc stats:\n%ld commits\n%ld ineligible\n%ld numblks\n%lluus avg_commit_time\n",
2164*4882a593Smuzhiyun stats->fc_num_commits, stats->fc_ineligible_commits,
2165*4882a593Smuzhiyun stats->fc_numblks,
2166*4882a593Smuzhiyun div_u64(sbi->s_fc_avg_commit_time, 1000));
2167*4882a593Smuzhiyun seq_puts(seq, "Ineligible reasons:\n");
2168*4882a593Smuzhiyun for (i = 0; i < EXT4_FC_REASON_MAX; i++)
2169*4882a593Smuzhiyun seq_printf(seq, "\"%s\":\t%d\n", fc_ineligible_reasons[i],
2170*4882a593Smuzhiyun stats->fc_ineligible_reason_count[i]);
2171*4882a593Smuzhiyun
2172*4882a593Smuzhiyun return 0;
2173*4882a593Smuzhiyun }
2174*4882a593Smuzhiyun
ext4_fc_init_dentry_cache(void)2175*4882a593Smuzhiyun int __init ext4_fc_init_dentry_cache(void)
2176*4882a593Smuzhiyun {
2177*4882a593Smuzhiyun ext4_fc_dentry_cachep = KMEM_CACHE(ext4_fc_dentry_update,
2178*4882a593Smuzhiyun SLAB_RECLAIM_ACCOUNT);
2179*4882a593Smuzhiyun
2180*4882a593Smuzhiyun if (ext4_fc_dentry_cachep == NULL)
2181*4882a593Smuzhiyun return -ENOMEM;
2182*4882a593Smuzhiyun
2183*4882a593Smuzhiyun return 0;
2184*4882a593Smuzhiyun }
2185*4882a593Smuzhiyun
ext4_fc_destroy_dentry_cache(void)2186*4882a593Smuzhiyun void ext4_fc_destroy_dentry_cache(void)
2187*4882a593Smuzhiyun {
2188*4882a593Smuzhiyun kmem_cache_destroy(ext4_fc_dentry_cachep);
2189*4882a593Smuzhiyun }
2190