2020-03-09 07:06:28

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 01/20] ext4: update docs for fast commit feature

This patch series adds support for fast commits which is a simplified
version of the scheme proposed by Park and Shin, in their paper,
"iJournaling: Fine-Grained Journaling for Improving the Latency of
Fsync System Call"[1]. The basic idea of fast commits is to make JBD2
give the client file system an opportunity to perform a faster
commit. Only if the file system cannot perform such a commit
operation, then JBD2 should fall back to traditional commits.

Because JBD2 operates at block granularity, for every file system
metadata update it commits all the changed blocks are written to the
journal at commit time. This is inefficient because updates to some
blocks that JBD2 commits are derivable from some other blocks. For
example, if a new extent is added to an inode, then corresponding
updates to the inode table, the block bitmap, the group descriptor and
the superblock can be derived based on just the extent information and
the corresponding inode information. So, if we take this relationship
between blocks into account and replay the journalled blocks smartly,
we could increase performance of file system commits significantly.

Fast commits introduced in this patch have two main contributions:

(1) Making JBD2 fast commit aware, so that clients of JBD2 can
implement fast commits

(2) Add support in ext4 to use JBD2's new interfaces and implement
fast commits.

Ext4 supports two modes of fast commits: 1) fast commits with hard
consistency guarantees 2) fast commits with soft consistency guarantees

When hard consistency is enabled, fast commit guarantees that all the
updates will be committed. After a successful replay of fast commits
blocks in hard consistency mode, the entire file system would be in
the same state as that when fsync() returned before crash. This
guarantee is similar to what jbd2 gives with full commits.

With soft consistency, file system only guarantees consistency for the
inode in question. In this mode, file system will try to write as less
data to the backend as possible during the commit time. To be precise,
file system records all the data updates for the inode in question and
directory updates that are required for guaranteeing consistency of the
inode in question.

In our evaluations, fast commits with hard consistency performed
better than fast commits with soft consistency. That's because with
hard consistency, a fast commit often ends up committing other inodes
together, while with soft consistency commits get serialized. Future
work can look at creating hybrid approach between the two extremes
that are there in this patchset.

Testing
-------

e2fsprogs was updated to set fast commit feature flag and to ignore
fast commit blocks during e2fsck.

https://github.com/harshadjs/e2fsprogs.git

After applying all the patches in this series, following runs of
xfstests were performed:

- kvm-xfstest.sh -g log -c 4k
- kvm-xfstests.sh smoke

All the log tests were successful and smoke tests didn't introduce any
additional failures.

Performance Evaluation
----------------------

Ext4 file system performance was tested with full commits, with fast
commits with soft consistency and with fast commits with hard
consistency. fs_mark benchmark showed that depending on the file size,
performance improvement was seen up to 50%. Soft fast commits performed
slightly worse than hard fast commits. But soft fast commits ended up
writing slightly lesser number of blocks on disk.

Changes since V4:

- Rebased on top of v5.6-rc3
- Fixed inode handling due to changed ext4_add_nondir()

Harshad Shirwadkar(20):
ext4: add debug mount option to test fast commit replay
ext4: add fast commit replay path
ext4: disable certain features in replay path
ext4: add idempotent helpers to manipulate bitmaps
ext4: fast commit recovery path preparation
jbd2: add fast commit recovery path support
ext4: main commit routine for fast commits
jbd2: add new APIs for commit path of fast commits
ext4: add fast commit on-disk format structs
ext4: add fast commit track points
ext4: break ext4_unlink() and ext4_link()
ext4: add inode tracking and ineligible marking routines
ext4: add directory entry tracking routines
ext4: add generic diff tracking routines and range tracking
jbd2: fast commit main commit path changes
jbd2: disable fast commits if journal is empty
jbd2: add fast commit block tracker variables
ext4, jbd2: add fast commit initialization routines
ext4: add handling for extended mount options
ext4: update docs for fast commit feature

Documentation/filesystems/ext4/journal.rst | 127 ++-
Documentation/filesystems/journalling.rst | 18 +
fs/ext4/acl.c | 1 +
fs/ext4/balloc.c | 10 +-
fs/ext4/ext4.h | 126 +++
fs/ext4/ext4_jbd2.c | 1484 +++++++++++++++++++++++++++-
fs/ext4/ext4_jbd2.h | 71 ++
fs/ext4/extents.c | 5 +
fs/ext4/extents_status.c | 24 +
fs/ext4/fsync.c | 2 +-
fs/ext4/ialloc.c | 165 +++-
fs/ext4/inline.c | 3 +
fs/ext4/inode.c | 76 +-
fs/ext4/ioctl.c | 11 +-
fs/ext4/mballoc.c | 158 ++-
fs/ext4/mballoc.h | 2 +
fs/ext4/migrate.c | 1 +
fs/ext4/namei.c | 182 ++--
fs/ext4/super.c | 72 +-
fs/ext4/xattr.c | 6 +
fs/jbd2/commit.c | 61 ++
fs/jbd2/journal.c | 217 +++-
fs/jbd2/recovery.c | 67 +-
include/linux/jbd2.h | 83 +-
include/trace/events/ext4.h | 208 +++-
25 files changed, 3046 insertions(+), 134 deletions(-)ˆ

Signed-off-by: Harshad Shirwadkar <[email protected]>
---
Documentation/filesystems/ext4/journal.rst | 127 ++++++++++++++++++++-
Documentation/filesystems/journalling.rst | 18 +++
2 files changed, 139 insertions(+), 6 deletions(-)

diff --git a/Documentation/filesystems/ext4/journal.rst b/Documentation/filesystems/ext4/journal.rst
index ea613ee701f5..f94e66f2f8c4 100644
--- a/Documentation/filesystems/ext4/journal.rst
+++ b/Documentation/filesystems/ext4/journal.rst
@@ -29,10 +29,10 @@ safest. If ``data=writeback``, dirty data blocks are not flushed to the
disk before the metadata are written to disk through the journal.

The journal inode is typically inode 8. The first 68 bytes of the
-journal inode are replicated in the ext4 superblock. The journal itself
-is normal (but hidden) file within the filesystem. The file usually
-consumes an entire block group, though mke2fs tries to put it in the
-middle of the disk.
+journal inode are replicated in the ext4 superblock. The journal
+itself is normal (but hidden) file within the filesystem. The file
+usually consumes an entire block group, though mke2fs tries to put it
+in the middle of the disk.

All fields in jbd2 are written to disk in big-endian order. This is the
opposite of ext4.
@@ -42,22 +42,74 @@ NOTE: Both ext4 and ocfs2 use jbd2.
The maximum size of a journal embedded in an ext4 filesystem is 2^32
blocks. jbd2 itself does not seem to care.

+Fast Commits
+~~~~~~~~~~~~
+
+Ext4 also implements fast commits and integrates it with JBD2 journalling.
+Fast commits store metadata changes made to the file system as inode level
+diff. In other words, each fast commit block identifies updates made to
+a particular inode and collectively they represent total changes made to
+the file system.
+
+A fast commit is valid only if there is no full commit after that particular
+fast commit. Because of this feature, fast commit blocks can be reused by
+the following transactions.
+
+Each fast commit block stores updates to 1 particular inode. Updates in each
+fast commit block are one of the 2 types:
+- Data updates (add range / delete range)
+- Directory entry updates (Add / remove links)
+
+Fast commit blocks must be replayed in the order in which they appear on disk.
+That's because directory entry updates are written in fast commit blocks
+in the order in which they are applied on the file system before crash.
+Changing the order of replaying for directory entry updates may result
+in inconsistent file system. Note that only directory entry updates need
+ordering, data updates, since they apply to only one inode, do not require
+ordered replay. Also, fast commits guarantee that file system is in consistent
+state after replay of each fast commit block as long as order of replay has
+been followed.
+
+Note that directory inode updates are never directly recorded in fast commits.
+Just like other file system level metaata, updates to directories are always
+implied based on directory entry updates stored in fast commit blocks.
+
+Based on which directory entry updates are committed with an inode, fast
+commits have two modes of operation:
+
+- Hard Consistency (default)
+- Soft Consistency (can be enabled by setting mount flag "fc_soft_consistency")
+
+When hard consistency is enabled, fast commit guarantees that all the updates
+will be committed. After a successful replay of fast commits blocks
+in hard consistency mode, the entire file system would be in the same state as
+that when fsync() returned before crash. This guarantee is similar to what
+jbd2 gives.
+
+With soft consistency, file system only guarantees consistency for the
+inode in question. In this mode, file system will try to write as less data
+to the backed as possible during the commit time. To be precise, file system
+records all the data updates for the inode in question and directory updates
+that are required for guaranteeing consistency of the inode in question.
+
Layout
~~~~~~

Generally speaking, the journal has this format:

.. list-table::
- :widths: 16 48 16
+ :widths: 16 48 16 18
:header-rows: 1

* - Superblock
- descriptor\_block (data\_blocks or revocation\_block) [more data or
revocations] commmit\_block
- [more transactions...]
+ - [Fast commits...]
* -
- One transaction
-
+ -

Notice that a transaction begins with either a descriptor and some data,
or a block revocation list. A finished transaction always ends with a
@@ -76,7 +128,7 @@ The journal superblock will be in the next full block after the
superblock.

.. list-table::
- :widths: 12 12 12 32 12
+ :widths: 12 12 12 32 12 12
:header-rows: 1

* - 1024 bytes of padding
@@ -85,11 +137,13 @@ superblock.
- descriptor\_block (data\_blocks or revocation\_block) [more data or
revocations] commmit\_block
- [more transactions...]
+ - [Fast commits...]
* -
-
-
- One transaction
-
+ -

Block Header
~~~~~~~~~~~~
@@ -609,3 +663,64 @@ bytes long (but uses a full block):
- h\_commit\_nsec
- Nanoseconds component of the above timestamp.

+Fast Commit Block
+~~~~~~~~~~~~~~~~~
+
+The fast commit block indicates an append to the last commit block
+that was written to the journal. One fast commit block records updates
+to one inode. So, typically you would find as many fast commit blocks
+as the number of inodes that got changed since the last commit. A fast
+commit block is valid only if there is no commit block present with
+transaction ID greater than that of the fast commit block. If such a
+block a present, then there is no need to replay the fast commit
+block.
+
+.. list-table::
+ :widths: 8 8 24 40
+ :header-rows: 1
+
+ * - Offset
+ - Type
+ - Name
+ - Descriptor
+ * - 0x0
+ - journal\_header\_s
+ - (open coded)
+ - Common block header.
+ * - 0xC
+ - \_\_le32
+ - fc\_magic
+ - Magic value which should be set to 0xE2540090. This identifies
+ that this block is a fast commit block.
+ * - 0x10
+ - \_\_u8
+ - fc\_features
+ - Features used by this fast commit block.
+ * - 0x11
+ - \_\_le16
+ - fc_num_tlvs
+ - Number of TLVs contained in this fast commit block
+ * - 0x13
+ - \_\_le32
+ - \_\_fc\_len
+ - Length of the fast commit block in terms of number of blocks
+ * - 0x17
+ - \_\_le32
+ - fc\_ino
+ - Inode number of the inode that will be recovered using this fast commit
+ * - 0x2B
+ - struct ext4\_inode
+ - inode
+ - On-disk copy of the inode at the commit time
+ * - <Variable based on inode size>
+ - struct ext4\_fc\_tl
+ - Array of struct ext4\_fc\_tl
+ - The actual delta with the last commit. Starting at this offset,
+ there is an array of TLVs that indicates which all extents
+ should be present in the corresponding inode. Currently,
+ following tags are supported: EXT4\_FC\_TAG\_EXT (extent that
+ should be present in the inode), EXT4\_FC\_TAG\_HOLE (extent
+ that should be removed from the inode), EXT4\_FC\_TAG\_ADD\_DENTRY
+ (dentry that should be linked), EXT4\_FC\_TAG\_DEL\_DENTRY
+ (dentry that should be unlinked), EXT4\_FC\_TAG\_CREATE\_DENTRY
+ (dentry that for the file that should be created for the first time).
diff --git a/Documentation/filesystems/journalling.rst b/Documentation/filesystems/journalling.rst
index 58ce6b395206..1cb116ab27ab 100644
--- a/Documentation/filesystems/journalling.rst
+++ b/Documentation/filesystems/journalling.rst
@@ -115,6 +115,24 @@ called after each transaction commit. You can also use
``transaction->t_private_list`` for attaching entries to a transaction
that need processing when the transaction commits.

+JBD2 also allows client file systems to implement file system specific
+commits which are called as ``fast commits``. Fast commits are
+asynchronous in nature i.e. file systems can call their own commit
+functions at any time. In order to avoid the race with kjournald
+thread and other possible fast commits that may be happening in
+parallel, file systems should first call
+:c:func:`jbd2_start_async_fc()`. File system can call
+:c:func:`jbd2_map_fc_buf()` to get buffers reserved for fast
+commits. Once a fast commit is completed, file system should call
+:c:func:`jbd2_stop_async_fc()` to indicate and unblock other
+committers and the kjournald thread. After performing either a fast
+or a full commit, JBD2 calls ``journal->j_fc_cleanup_cb`` to allow
+file systems to perform cleanups for their internal fast commit
+related data structures. At the replay time, JBD2 passes each and
+every fast commit block to the file system via
+``journal->j_fc_replay_cb``. Ext4 effectively uses this fast commit
+mechanism to improve journal commit performance.
+
JBD2 also provides a way to block all transaction updates via
:c:func:`jbd2_journal_lock_updates()` /
:c:func:`jbd2_journal_unlock_updates()`. Ext4 uses this when it wants a
--
2.25.1.481.gfbce0eb801-goog


2020-03-09 07:06:29

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 02/20] ext4: add handling for extended mount options

We are running out of mount option bits. Add handling for using
s_mount_opt2. Add ability to turn on / off the fast commit
feature and to turn on / off fast commit soft consistency option.

Signed-off-by: Harshad Shirwadkar <[email protected]>
---
fs/ext4/ext4.h | 7 +++++++
fs/ext4/super.c | 23 +++++++++++++++++++----
2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 61b37a052052..f96a672232a1 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1173,6 +1173,13 @@ struct ext4_inode_info {
#define EXT4_MOUNT2_EXPLICIT_JOURNAL_CHECKSUM 0x00000008 /* User explicitly
specified journal checksum */

+#define EXT4_MOUNT2_JOURNAL_FAST_COMMIT 0x00000010 /* Journal fast commit */
+
+#define EXT4_MOUNT2_JOURNAL_FC_SOFT_CONSISTENCY 0x00000020 /* Soft consistency
+ * mode for fast
+ * commits
+ */
+
#define clear_opt(sb, opt) EXT4_SB(sb)->s_mount_opt &= \
~EXT4_MOUNT_##opt
#define set_opt(sb, opt) EXT4_SB(sb)->s_mount_opt |= \
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index ff1b764b0c0e..00571de3390b 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -1526,6 +1526,7 @@ enum {
Opt_dioread_nolock, Opt_dioread_lock,
Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
+ Opt_no_fc, Opt_fc_soft_consistency
};

static const match_table_t tokens = {
@@ -1609,6 +1610,8 @@ static const match_table_t tokens = {
{Opt_init_itable, "init_itable=%u"},
{Opt_init_itable, "init_itable"},
{Opt_noinit_itable, "noinit_itable"},
+ {Opt_no_fc, "no_fc"},
+ {Opt_fc_soft_consistency, "fc_soft_consistency"},
{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
{Opt_test_dummy_encryption, "test_dummy_encryption"},
{Opt_nombcache, "nombcache"},
@@ -1731,6 +1734,7 @@ static int clear_qf_name(struct super_block *sb, int qtype)
#define MOPT_NO_EXT3 0x0200
#define MOPT_EXT4_ONLY (MOPT_NO_EXT2 | MOPT_NO_EXT3)
#define MOPT_STRING 0x0400
+#define MOPT_2 0x0800

static const struct mount_opts {
int token;
@@ -1823,6 +1827,10 @@ static const struct mount_opts {
{Opt_max_dir_size_kb, 0, MOPT_GTE0},
{Opt_test_dummy_encryption, 0, MOPT_GTE0},
{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
+ {Opt_no_fc, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,
+ MOPT_CLEAR | MOPT_2 | MOPT_EXT4_ONLY},
+ {Opt_fc_soft_consistency, EXT4_MOUNT2_JOURNAL_FC_SOFT_CONSISTENCY,
+ MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
{Opt_err, 0, 0}
};

@@ -2113,10 +2121,17 @@ static int handle_mount_opt(struct super_block *sb, char *opt, int token,
WARN_ON(1);
return -1;
}
- if (arg != 0)
- sbi->s_mount_opt |= m->mount_opt;
- else
- sbi->s_mount_opt &= ~m->mount_opt;
+ if (m->flags & MOPT_2) {
+ if (arg != 0)
+ sbi->s_mount_opt2 |= m->mount_opt;
+ else
+ sbi->s_mount_opt2 &= ~m->mount_opt;
+ } else {
+ if (arg != 0)
+ sbi->s_mount_opt |= m->mount_opt;
+ else
+ sbi->s_mount_opt &= ~m->mount_opt;
+ }
}
return 1;
}
--
2.25.1.481.gfbce0eb801-goog

2020-03-09 07:06:29

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 06/20] jbd2: fast commit main commit path changes

Add 3 new APIs jbd2_start_async_fc_nowait(),
jbd2_start_async_fc_wait() and jbd2_stop_async_fc(). These APIs can be
used by file systems to indicate to jbd2 that they are starting or
stopping a fast commit.

Signed-off-by: Harshad Shirwadkar <[email protected]>
---
fs/jbd2/commit.c | 21 +++++++++++
fs/jbd2/journal.c | 85 +++++++++++++++++++++++++++++++++++++++++++-
include/linux/jbd2.h | 21 +++++++++++
3 files changed, 126 insertions(+), 1 deletion(-)

diff --git a/fs/jbd2/commit.c b/fs/jbd2/commit.c
index 27373f5792a4..869fe193fbe3 100644
--- a/fs/jbd2/commit.c
+++ b/fs/jbd2/commit.c
@@ -413,6 +413,23 @@ void jbd2_journal_commit_transaction(journal_t *journal)
J_ASSERT(journal->j_running_transaction != NULL);
J_ASSERT(journal->j_committing_transaction == NULL);

+ write_lock(&journal->j_state_lock);
+ journal->j_flags |= JBD2_FULL_COMMIT_ONGOING;
+ while (journal->j_flags & JBD2_FAST_COMMIT_ONGOING) {
+ DEFINE_WAIT(wait);
+
+ prepare_to_wait(&journal->j_wait_async_fc, &wait,
+ TASK_UNINTERRUPTIBLE);
+ write_unlock(&journal->j_state_lock);
+ schedule();
+ write_lock(&journal->j_state_lock);
+ finish_wait(&journal->j_wait_async_fc, &wait);
+ }
+ write_unlock(&journal->j_state_lock);
+
+ if (journal->j_fc_cleanup_callback)
+ journal->j_fc_cleanup_callback(journal);
+
commit_transaction = journal->j_running_transaction;

trace_jbd2_start_commit(journal, commit_transaction);
@@ -420,6 +437,7 @@ void jbd2_journal_commit_transaction(journal_t *journal)
commit_transaction->t_tid);

write_lock(&journal->j_state_lock);
+ journal->j_fc_off = 0;
J_ASSERT(commit_transaction->t_state == T_RUNNING);
commit_transaction->t_state = T_LOCKED;

@@ -1124,6 +1142,8 @@ void jbd2_journal_commit_transaction(journal_t *journal)
journal->j_commit_sequence, journal->j_tail_sequence);

write_lock(&journal->j_state_lock);
+ journal->j_flags &= ~JBD2_FULL_COMMIT_ONGOING;
+ journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
spin_lock(&journal->j_list_lock);
commit_transaction->t_state = T_FINISHED;
/* Check if the transaction can be dropped now that we are finished */
@@ -1135,6 +1155,7 @@ void jbd2_journal_commit_transaction(journal_t *journal)
spin_unlock(&journal->j_list_lock);
write_unlock(&journal->j_state_lock);
wake_up(&journal->j_wait_done_commit);
+ wake_up(&journal->j_wait_async_fc);

/*
* Calculate overall stats
diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
index f8f55d0814ea..d3897d155fb9 100644
--- a/fs/jbd2/journal.c
+++ b/fs/jbd2/journal.c
@@ -157,7 +157,9 @@ static void commit_timeout(struct timer_list *t)
*
* 1) COMMIT: Every so often we need to commit the current state of the
* filesystem to disk. The journal thread is responsible for writing
- * all of the metadata buffers to disk.
+ * all of the metadata buffers to disk. If a fast commit is ongoing
+ * journal thread waits until it's done and then copntinues from
+ * there on.
*
* 2) CHECKPOINT: We cannot reuse a used section of the log file until all
* of the data in that part of the log has been rewritten elsewhere on
@@ -714,6 +716,86 @@ int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
return err;
}

+/*
+ * Returns 0 if async fc could be started. Returns -EINVAL if no full
+ * commit has been done yet. Returns -EALREADY if another fast /
+ * full commit is ongoing.
+ */
+int jbd2_start_async_fc_nowait(journal_t *journal, tid_t tid)
+{
+ /*
+ * Fast commits only allowed if at least one full commit has
+ * been processed.
+ */
+ if (!journal->j_stats.ts_tid)
+ return -EINVAL;
+
+ if (tid <= journal->j_commit_sequence)
+ return -EALREADY;
+
+ write_lock(&journal->j_state_lock);
+ if (journal->j_flags &
+ (JBD2_FAST_COMMIT_ONGOING | JBD2_FULL_COMMIT_ONGOING)) {
+ write_unlock(&journal->j_state_lock);
+ return -EALREADY;
+ }
+
+ journal->j_flags |= JBD2_FAST_COMMIT_ONGOING;
+ write_unlock(&journal->j_state_lock);
+
+ return 0;
+}
+
+/*
+ * Same as above but waits for any ongoing fast commits to complete.
+ * If a full commit is ongoing, this function returns with
+ * -EALREADY.
+ */
+int jbd2_start_async_fc_wait(journal_t *journal, tid_t tid)
+{
+ int ret;
+
+ /*
+ * Fast commits only allowed if at least one full commit has
+ * been processed.
+ */
+ if (!journal->j_stats.ts_tid)
+ return -EINVAL;
+
+ if (tid <= journal->j_commit_sequence)
+ return -EALREADY;
+
+ write_lock(&journal->j_state_lock);
+restart:
+ if (journal->j_flags & JBD2_FULL_COMMIT_ONGOING) {
+ ret = -EALREADY;
+ } else if (journal->j_flags & JBD2_FAST_COMMIT_ONGOING) {
+ DEFINE_WAIT(wait);
+
+ prepare_to_wait(&journal->j_wait_async_fc, &wait,
+ TASK_UNINTERRUPTIBLE);
+ write_unlock(&journal->j_state_lock);
+ schedule();
+ write_lock(&journal->j_state_lock);
+ finish_wait(&journal->j_wait_async_fc, &wait);
+ goto restart;
+ } else {
+ journal->j_flags |= JBD2_FAST_COMMIT_ONGOING;
+ ret = 0;
+ }
+ write_unlock(&journal->j_state_lock);
+
+ return ret;
+}
+
+void jbd2_stop_async_fc(journal_t *journal, tid_t tid)
+{
+ write_lock(&journal->j_state_lock);
+ journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
+ write_unlock(&journal->j_state_lock);
+ wake_up(&journal->j_wait_async_fc);
+}
+
/* Return 1 when transaction with given tid has already committed. */
int jbd2_transaction_committed(journal_t *journal, tid_t tid)
{
@@ -1140,6 +1222,7 @@ static journal_t *journal_init_common(struct block_device *bdev,
init_waitqueue_head(&journal->j_wait_commit);
init_waitqueue_head(&journal->j_wait_updates);
init_waitqueue_head(&journal->j_wait_reserved);
+ init_waitqueue_head(&journal->j_wait_async_fc);
mutex_init(&journal->j_barrier);
mutex_init(&journal->j_checkpoint_mutex);
spin_lock_init(&journal->j_revoke_lock);
diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
index 1fc981cca479..0a4d9d484528 100644
--- a/include/linux/jbd2.h
+++ b/include/linux/jbd2.h
@@ -853,6 +853,13 @@ struct journal_s
*/
wait_queue_head_t j_wait_reserved;

+ /**
+ * @j_wait_async_fc:
+ *
+ * Wait queue to wait for completion of async fast commits.
+ */
+ wait_queue_head_t j_wait_async_fc;
+
/**
* @j_checkpoint_mutex:
*
@@ -1203,6 +1210,14 @@ struct journal_s
*/
struct lockdep_map j_trans_commit_map;
#endif
+
+ /**
+ * @j_fc_cleanup_callback:
+ *
+ * Clean-up after fast commit or full commit. JBD2 calls this function
+ * after every commit operation.
+ */
+ void (*j_fc_cleanup_callback)(struct journal_s *journal);
};

#define jbd2_might_wait_for_commit(j) \
@@ -1288,6 +1303,8 @@ JBD2_FEATURE_INCOMPAT_FUNCS(fast_commit, FAST_COMMIT)
* data write error in ordered
* mode */
#define JBD2_REC_ERR 0x080 /* The errno in the sb has been recorded */
+#define JBD2_FAST_COMMIT_ONGOING 0x100 /* Fast commit is ongoing */
+#define JBD2_FULL_COMMIT_ONGOING 0x200 /* Full commit is ongoing */

/*
* Function declarations for the journaling transaction and buffer
@@ -1540,6 +1557,10 @@ void __jbd2_log_wait_for_space(journal_t *journal);
extern void __jbd2_journal_drop_transaction(journal_t *, transaction_t *);
extern int jbd2_cleanup_journal_tail(journal_t *);

+/* Fast commit related APIs */
+int jbd2_start_async_fc_nowait(journal_t *journal, tid_t tid);
+int jbd2_start_async_fc_wait(journal_t *journal, tid_t tid);
+void jbd2_stop_async_fc(journal_t *journal, tid_t tid);
void jbd2_init_fast_commit(journal_t *journal, int num_fc_blks);
/*
* is_journal_abort
--
2.25.1.481.gfbce0eb801-goog

2020-03-09 07:06:29

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 09/20] ext4: add inode tracking and ineligible marking routines

Under certain situations, such as zeroing a range, there are only data
updates and no metadata updates. We need to track such inodes for fast
commits. Also, under some situations, we need to fall back to full
commits because remembering the delta is either not yet supported or
fast commits won't be "fast" enough. In such cases, we need to mark
inodes as ineligible for fast commits. Add routines that allow
tracking just the inodes and marking inodes as well as entire file
system as fast commit ineligible.

Signed-off-by: Harshad Shirwadkar <[email protected]>
---
fs/ext4/ext4.h | 2 ++
fs/ext4/ext4_jbd2.c | 57 +++++++++++++++++++++++++++++++++++++
fs/ext4/ext4_jbd2.h | 4 +++
fs/ext4/super.c | 1 +
include/trace/events/ext4.h | 22 ++++++++++++++
5 files changed, 86 insertions(+)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 1d4fb7b949a5..5cb7f923dbee 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1155,6 +1155,7 @@ struct ext4_inode_info {
#define EXT4_ERROR_FS 0x0002 /* Errors detected */
#define EXT4_ORPHAN_FS 0x0004 /* Orphans being recovered */
#define EXT4_FC_REPLAY 0x0008 /* Fast commit replay ongoing */
+#define EXT4_FC_INELIGIBLE 0x0010 /* Fast commit ineligible */

/*
* Misc. filesystem flags
@@ -1736,6 +1737,7 @@ enum {
EXT4_STATE_EXT_PRECACHED, /* extents have been precached */
EXT4_STATE_LUSTRE_EA_INODE, /* Lustre-style ea_inode */
EXT4_STATE_VERITY_IN_PROGRESS, /* building fs-verity Merkle tree */
+ EXT4_STATE_FC_ELIGIBLE, /* File is Fast commit eligible */
};

#define EXT4_INODE_BIT_FNS(name, field, offset) \
diff --git a/fs/ext4/ext4_jbd2.c b/fs/ext4/ext4_jbd2.c
index 85ba3dc7a3b6..793b6c0fc2fa 100644
--- a/fs/ext4/ext4_jbd2.c
+++ b/fs/ext4/ext4_jbd2.c
@@ -383,6 +383,7 @@ void ext4_reset_inode_fc_info(struct inode *inode)
ei->i_fc_lblk_start = 0;
ei->i_fc_lblk_end = 0;
ei->i_fc_mdata_update = NULL;
+ ext4_clear_inode_state(inode, EXT4_STATE_FC_ELIGIBLE);
}

void ext4_init_inode_fc_info(struct inode *inode)
@@ -414,6 +415,36 @@ static inline tid_t get_running_txn_tid(struct super_block *sb)
return 0;
}

+bool ext4_is_inode_fc_ineligible(struct inode *inode)
+{
+ if (get_running_txn_tid(inode->i_sb) == EXT4_I(inode)->i_fc_tid)
+ return !ext4_test_inode_state(inode, EXT4_STATE_FC_ELIGIBLE);
+ return false;
+}
+
+void ext4_fc_mark_ineligible(struct inode *inode, int reason)
+{
+ struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
+ struct ext4_inode_info *ei = EXT4_I(inode);
+
+ if (!ext4_should_fast_commit(inode->i_sb) ||
+ (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY))
+ return;
+
+ if (sbi->s_journal)
+ ei->i_fc_tid = get_running_txn_tid(inode->i_sb);
+ ext4_clear_inode_state(inode, EXT4_STATE_FC_ELIGIBLE);
+
+ ext4_fc_enqueue_inode(inode);
+}
+
+void ext4_fc_disable(struct super_block *sb, int reason)
+{
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+
+ sbi->s_mount_state |= EXT4_FC_INELIGIBLE;
+}
+
/*
* Generic fast commit tracking function. If this is the first
* time this we are called after a full commit, we initialize
@@ -439,10 +470,15 @@ static int __ext4_fc_track_template(

write_lock(&ei->i_fc_lock);
if (running_txn_tid == ei->i_fc_tid) {
+ if (!ext4_test_inode_state(inode, EXT4_STATE_FC_ELIGIBLE)) {
+ write_unlock(&ei->i_fc_lock);
+ return -EINVAL;
+ }
update = true;
} else {
ext4_reset_inode_fc_info(inode);
ei->i_fc_tid = running_txn_tid;
+ ext4_set_inode_state(inode, EXT4_STATE_FC_ELIGIBLE);
}
ret = __fc_track_fn(inode, args, update);
write_unlock(&ei->i_fc_lock);
@@ -539,6 +575,27 @@ void ext4_fc_track_create(struct inode *inode, struct dentry *dentry)
trace_ext4_fc_track_create(inode, dentry, ret);
}

+static int __ext4_fc_add_inode(struct inode *inode, void *arg, bool update)
+{
+ struct ext4_inode_info *ei = EXT4_I(inode);
+
+ if (update)
+ return -EEXIST;
+
+ ei->i_fc_lblk_start = (i_size_read(inode) - 1) >> inode->i_blkbits;
+ ei->i_fc_lblk_end = (i_size_read(inode) - 1) >> inode->i_blkbits;
+
+ return 0;
+}
+
+void ext4_fc_track_inode(struct inode *inode)
+{
+ int ret;
+
+ ret = __ext4_fc_track_template(inode, __ext4_fc_add_inode, NULL);
+ trace_ext4_fc_track_inode(inode, ret);
+}
+
struct __ext4_fc_track_range_args {
ext4_lblk_t start, end;
};
diff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h
index 883f715df71d..401f142172e4 100644
--- a/fs/ext4/ext4_jbd2.h
+++ b/fs/ext4/ext4_jbd2.h
@@ -535,4 +535,8 @@ void ext4_fc_track_unlink(struct inode *inode, struct dentry *dentry);
void ext4_fc_track_link(struct inode *inode, struct dentry *dentry);
void ext4_fc_track_create(struct inode *inode, struct dentry *dentry);
int __init ext4_init_fc_dentry_cache(void);
+void ext4_fc_track_inode(struct inode *inode);
+void ext4_fc_mark_ineligible(struct inode *inode, int reason);
+void ext4_fc_disable(struct super_block *sb, int reason);
+
#endif /* _EXT4_JBD2_H */
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 24f975aa5967..7b30f90ec2b7 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -4413,6 +4413,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)

INIT_LIST_HEAD(&sbi->s_fc_q);
INIT_LIST_HEAD(&sbi->s_fc_dentry_q);
+ sbi->s_mount_state &= ~EXT4_FC_INELIGIBLE;
spin_lock_init(&sbi->s_fc_lock);
sb->s_root = NULL;

diff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h
index 577c6230b23a..5d278a8082a7 100644
--- a/include/trace/events/ext4.h
+++ b/include/trace/events/ext4.h
@@ -2751,6 +2751,28 @@ DEFINE_TRACE_DENTRY_EVENT(create);
DEFINE_TRACE_DENTRY_EVENT(link);
DEFINE_TRACE_DENTRY_EVENT(unlink);

+TRACE_EVENT(ext4_fc_track_inode,
+ TP_PROTO(struct inode *inode, int ret),
+
+ TP_ARGS(inode, ret),
+
+ TP_STRUCT__entry(
+ __field(dev_t, dev)
+ __field(int, ino)
+ __field(int, error)
+ ),
+
+ TP_fast_assign(
+ __entry->dev = inode->i_sb->s_dev;
+ __entry->ino = inode->i_ino;
+ __entry->error = ret;
+ ),
+
+ TP_printk("dev %d:%d, inode %d, error %d",
+ MAJOR(__entry->dev), MINOR(__entry->dev),
+ __entry->ino, __entry->error)
+ );
+
TRACE_EVENT(ext4_fc_track_range,
TP_PROTO(struct inode *inode, long start, long end, int ret),

--
2.25.1.481.gfbce0eb801-goog

2020-03-09 07:06:47

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 11/20] ext4: add fast commit track points

Previous patches in the series have added following tracking routines:

- ext4_fc_track_inode() -> tracks just the inode
- ext4_fc_track_create() -> tracks creation of an inode and remembers
its dirent
- ext4_fc_track_unlink() -> tracks inode unlink
- ext4_fc_track_link() -> tracks inode link
- ext4_fc_mark_ineligible() -> marks inode as ineligible for fast
commits
- ext4_fc_disable() -> marks entire file system as fast commit
ineligible

Add these different track points at various points in the file
system. This patch also adds high level stats to remember reasons why
inodes were marked ineligible.

Signed-off-by: Harshad Shirwadkar <[email protected]>
---
fs/ext4/acl.c | 1 +
fs/ext4/balloc.c | 3 +++
fs/ext4/ext4.h | 26 ++++++++++++++++++++++++++
fs/ext4/ext4_jbd2.c | 5 +++++
fs/ext4/extents.c | 5 +++++
fs/ext4/inline.c | 3 +++
fs/ext4/inode.c | 6 ++++++
fs/ext4/ioctl.c | 5 +++++
fs/ext4/namei.c | 41 +++++++++++++++++++++++++++++++++++++++--
fs/ext4/super.c | 9 +++++++++
fs/ext4/xattr.c | 6 ++++++
11 files changed, 108 insertions(+), 2 deletions(-)

diff --git a/fs/ext4/acl.c b/fs/ext4/acl.c
index 8c7bbf3e566d..28e9e04a8e96 100644
--- a/fs/ext4/acl.c
+++ b/fs/ext4/acl.c
@@ -257,6 +257,7 @@ ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type)
inode->i_mode = mode;
inode->i_ctime = current_time(inode);
ext4_mark_inode_dirty(handle, inode);
+ ext4_fc_track_inode(inode);
}
out_stop:
ext4_journal_stop(handle);
diff --git a/fs/ext4/balloc.c b/fs/ext4/balloc.c
index 8fd0b3cdab4c..c3c4e7fc5c6b 100644
--- a/fs/ext4/balloc.c
+++ b/fs/ext4/balloc.c
@@ -665,6 +665,9 @@ ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
ar.len = count ? *count : 1;
ar.flags = flags;

+ ext4_fc_mark_ineligible(inode,
+ EXT4_FC_REASON_META_ALLOC);
+
ret = ext4_mb_new_blocks(handle, &ar, errp);
if (count)
*count = ar.len;
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 6cc3a93b0ce0..57ccc26012f0 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1440,6 +1440,31 @@ struct ext4_super_block {
#define ext4_has_strict_mode(sbi) \
(sbi->s_encoding_flags & EXT4_ENC_STRICT_MODE_FL)

+/*
+ * Fast commit ineligible reasons.
+ */
+enum {
+ EXT4_FC_REASON_META_ALLOC,
+ EXT4_FC_REASON_QUOTA,
+ EXT4_FC_REASON_XATTR,
+ EXT4_FC_REASON_CROSS_RENAME,
+ EXT4_FC_REASON_FALLOC_RANGE_OP,
+ EXT4_FC_REASON_JOURNAL_FLAG_CHANGE,
+ EXT4_FC_REASON_DELETE,
+ EXT4_FC_REASON_MEM,
+ EXT4_FC_REASON_SWAP_BOOT,
+ EXT4_FC_REASON_RESIZE,
+ EXT4_FC_REASON_RENAME_DIR,
+ EXT4_FC_REASON_MAX
+};
+
+struct ext4_fc_stats {
+ int fc_ineligible_reason_count[EXT4_FC_REASON_MAX];
+ int fc_num_commits;
+ int fc_ineligible_commits;
+ int fc_numblks;
+};
+
/*
* fourth extended-fs super-block data in memory
*/
@@ -1627,6 +1652,7 @@ struct ext4_sb_info {
*/
struct list_head s_fc_dentry_q;
spinlock_t s_fc_lock;
+ struct ext4_fc_stats s_fc_stats;
};

static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
diff --git a/fs/ext4/ext4_jbd2.c b/fs/ext4/ext4_jbd2.c
index 793b6c0fc2fa..a29ae83df881 100644
--- a/fs/ext4/ext4_jbd2.c
+++ b/fs/ext4/ext4_jbd2.c
@@ -431,6 +431,8 @@ void ext4_fc_mark_ineligible(struct inode *inode, int reason)
(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY))
return;

+ WARN_ON(reason >= EXT4_FC_REASON_MAX);
+ sbi->s_fc_stats.fc_ineligible_reason_count[reason]++;
if (sbi->s_journal)
ei->i_fc_tid = get_running_txn_tid(inode->i_sb);
ext4_clear_inode_state(inode, EXT4_STATE_FC_ELIGIBLE);
@@ -443,6 +445,8 @@ void ext4_fc_disable(struct super_block *sb, int reason)
struct ext4_sb_info *sbi = EXT4_SB(sb);

sbi->s_mount_state |= EXT4_FC_INELIGIBLE;
+ WARN_ON(reason >= EXT4_FC_REASON_MAX);
+ sbi->s_fc_stats.fc_ineligible_reason_count[reason]++;
}

/*
@@ -504,6 +508,7 @@ static int __ext4_dentry_update(struct inode *inode, void *arg, bool update)
write_unlock(&ei->i_fc_lock);
node = kmem_cache_alloc(ext4_fc_dentry_cachep, GFP_NOFS);
if (!node) {
+ ext4_fc_disable(inode->i_sb, EXT4_FC_REASON_MEM);
write_lock(&ei->i_fc_lock);
return -ENOMEM;
}
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 954013d6076b..76538ca1d903 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -4813,6 +4813,7 @@ static long ext4_zero_range(struct file *file, loff_t offset,
ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
}
ext4_mark_inode_dirty(handle, inode);
+ ext4_fc_track_inode(inode);

/* Zero out partial block at the edges of the range */
ret = ext4_zero_partial_blocks(handle, inode, offset, len);
@@ -5551,6 +5552,8 @@ static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
if (IS_SYNC(inode))
ext4_handle_sync(handle);
inode->i_mtime = inode->i_ctime = current_time(inode);
+ ext4_fc_mark_ineligible(inode,
+ EXT4_FC_REASON_FALLOC_RANGE_OP);
ext4_mark_inode_dirty(handle, inode);
ext4_update_inode_fsync_trans(handle, inode, 1);

@@ -5661,6 +5664,8 @@ static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
inode->i_size += len;
EXT4_I(inode)->i_disksize += len;
inode->i_mtime = inode->i_ctime = current_time(inode);
+ ext4_fc_mark_ineligible(inode,
+ EXT4_FC_REASON_FALLOC_RANGE_OP);
ret = ext4_mark_inode_dirty(handle, inode);
if (ret)
goto out_stop;
diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index fad82d08fca5..42c8ab5e536d 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -760,6 +760,7 @@ int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,

ext4_write_unlock_xattr(inode, &no_expand);
brelse(iloc.bh);
+ ext4_fc_track_inode(inode);
mark_inode_dirty(inode);
out:
return copied;
@@ -975,6 +976,7 @@ int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
* ordering of page lock and transaction start for journaling
* filesystems.
*/
+ ext4_fc_track_inode(inode);
mark_inode_dirty(inode);

return copied;
@@ -1988,6 +1990,7 @@ int ext4_inline_data_truncate(struct inode *inode, int *has_inline)

if (err == 0) {
inode->i_mtime = inode->i_ctime = current_time(inode);
+ ext4_fc_track_inode(inode);
err = ext4_mark_inode_dirty(handle, inode);
if (IS_SYNC(inode))
ext4_handle_sync(handle);
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 531aac4ec540..5720a12d7371 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -2851,6 +2851,7 @@ static int ext4_writepages(struct address_space *mapping,
out_writepages:
trace_ext4_writepages_result(inode, wbc, ret,
nr_to_write - wbc->nr_to_write);
+ ext4_fc_track_inode(inode);
percpu_up_read(&sbi->s_writepages_rwsem);
return ret;
}
@@ -5282,6 +5283,7 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr)
if (attr->ia_valid & ATTR_GID)
inode->i_gid = attr->ia_gid;
error = ext4_mark_inode_dirty(handle, inode);
+ ext4_fc_track_inode(inode);
ext4_journal_stop(handle);
}

@@ -5400,6 +5402,7 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr)

if (!error) {
setattr_copy(inode, attr);
+ ext4_fc_track_inode(inode);
mark_inode_dirty(inode);
}

@@ -5830,6 +5833,7 @@ void ext4_dirty_inode(struct inode *inode, int flags)
goto out;

ext4_mark_inode_dirty(handle, inode);
+ ext4_fc_track_inode(inode);

ext4_journal_stop(handle);
out:
@@ -5915,6 +5919,8 @@ int ext4_change_inode_journal_flag(struct inode *inode, int val)
if (IS_ERR(handle))
return PTR_ERR(handle);

+ ext4_fc_mark_ineligible(inode,
+ EXT4_FC_REASON_JOURNAL_FLAG_CHANGE);
err = ext4_mark_inode_dirty(handle, inode);
ext4_handle_sync(handle);
ext4_journal_stop(handle);
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index a0ec750018dd..3ea66e929afe 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -204,6 +204,8 @@ static long swap_inode_boot_loader(struct super_block *sb,

ext4_discard_preallocations(inode);

+ if (EXT4_SB(sb)->s_journal)
+ ext4_fc_disable(sb, EXT4_FC_REASON_SWAP_BOOT);
err = ext4_mark_inode_dirty(handle, inode);
if (err < 0) {
/* No need to update quota information. */
@@ -397,6 +399,8 @@ static int ext4_ioctl_setflags(struct inode *inode,
inode->i_ctime = current_time(inode);

err = ext4_mark_iloc_dirty(handle, inode, &iloc);
+ ext4_fc_track_inode(inode);
+
flags_err:
ext4_journal_stop(handle);
if (err)
@@ -1080,6 +1084,7 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)

err = ext4_resize_fs(sb, n_blocks_count);
if (EXT4_SB(sb)->s_journal) {
+ ext4_fc_disable(sb, EXT4_FC_REASON_RESIZE);
jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index 5ee002cc0acd..ae0e112c65d5 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -2601,7 +2601,7 @@ static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
bool excl)
{
handle_t *handle;
- struct inode *inode;
+ struct inode *inode, *inode_save;
int err, credits, retries = 0;

err = dquot_initialize(dir);
@@ -2619,7 +2619,11 @@ static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
inode->i_op = &ext4_file_inode_operations;
inode->i_fop = &ext4_file_operations;
ext4_set_aops(inode);
+ inode_save = inode;
+ ihold(inode_save);
err = ext4_add_nondir(handle, dentry, &inode);
+ ext4_fc_track_create(inode_save, dentry);
+ iput(inode_save);
}
if (handle)
ext4_journal_stop(handle);
@@ -2634,7 +2638,7 @@ static int ext4_mknod(struct inode *dir, struct dentry *dentry,
umode_t mode, dev_t rdev)
{
handle_t *handle;
- struct inode *inode;
+ struct inode *inode, *inode_save;
int err, credits, retries = 0;

err = dquot_initialize(dir);
@@ -2651,12 +2655,18 @@ static int ext4_mknod(struct inode *dir, struct dentry *dentry,
if (!IS_ERR(inode)) {
init_special_inode(inode, inode->i_mode, rdev);
inode->i_op = &ext4_special_inode_operations;
+ inode_save = inode;
+ ihold(inode_save);
err = ext4_add_nondir(handle, dentry, &inode);
+ if (!err)
+ ext4_fc_track_create(inode_save, dentry);
+ iput(inode_save);
}
if (handle)
ext4_journal_stop(handle);
if (!IS_ERR_OR_NULL(inode))
iput(inode);
+
if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
goto retry;
return err;
@@ -2688,6 +2698,8 @@ static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
err = ext4_orphan_add(handle, inode);
if (err)
goto err_unlock_inode;
+
+ ext4_fc_track_inode(inode);
mark_inode_dirty(inode);
unlock_new_inode(inode);
}
@@ -2813,6 +2825,7 @@ static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
iput(inode);
goto out_retry;
}
+ ext4_fc_track_create(inode, dentry);
ext4_inc_count(handle, dir);
ext4_update_dx_flag(dir);
err = ext4_mark_inode_dirty(handle, dir);
@@ -3261,6 +3274,8 @@ static int ext4_unlink(struct inode *dir, struct dentry *dentry)
return retval;

retval = __ext4_unlink(dir, &dentry->d_name, d_inode(dentry));
+ if (!retval)
+ ext4_fc_track_unlink(d_inode(dentry), dentry);
#ifdef CONFIG_UNICODE
/* VFS negative dentries are incompatible with Encoding and
* Case-insensitiveness. Eventually we'll want avoid
@@ -3424,6 +3439,7 @@ int __ext4_link(struct inode *dir, struct inode *inode, struct dentry *dentry)

err = ext4_add_entry(handle, dentry, inode);
if (!err) {
+ ext4_fc_track_link(inode, dentry);
ext4_mark_inode_dirty(handle, inode);
/* this can happen only for tmpfile being
* linked the first time
@@ -3872,6 +3888,23 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
ext4_mark_inode_dirty(handle, new.dir);
}
}
+
+ if (S_ISDIR(old.inode->i_mode)) {
+ /*
+ * We disable fast commits here that's because the
+ * replay code is not yet capable of changing dot dot
+ * dirents in directories. Since this is a metadata
+ * update that's ineligible, we need to mark entire fs
+ * as ineligbile.
+ */
+ ext4_fc_disable(old.inode->i_sb, EXT4_FC_REASON_RENAME_DIR);
+ } else {
+ if (new.inode)
+ ext4_fc_track_unlink(new.inode, new.dentry);
+ ext4_fc_track_link(old.inode, new.dentry);
+ ext4_fc_track_unlink(old.inode, old.dentry);
+ }
+
ext4_mark_inode_dirty(handle, old.dir);
if (new.inode) {
ext4_mark_inode_dirty(handle, new.inode);
@@ -4008,7 +4041,11 @@ static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
ctime = current_time(old.inode);
old.inode->i_ctime = ctime;
new.inode->i_ctime = ctime;
+ ext4_fc_mark_ineligible(old.inode,
+ EXT4_FC_REASON_CROSS_RENAME);
ext4_mark_inode_dirty(handle, old.inode);
+ ext4_fc_mark_ineligible(new.inode,
+ EXT4_FC_REASON_CROSS_RENAME);
ext4_mark_inode_dirty(handle, new.inode);

if (old.dir_bh) {
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 7b30f90ec2b7..a484520e83d5 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -1384,6 +1384,7 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
* S_DAX may be disabled
*/
ext4_set_inode_flags(inode);
+ ext4_fc_track_inode(inode);
res = ext4_mark_inode_dirty(handle, inode);
if (res)
EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
@@ -4415,6 +4416,8 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
INIT_LIST_HEAD(&sbi->s_fc_dentry_q);
sbi->s_mount_state &= ~EXT4_FC_INELIGIBLE;
spin_lock_init(&sbi->s_fc_lock);
+ memset(&sbi->s_fc_stats, 0, sizeof(sbi->s_fc_stats));
+
sb->s_root = NULL;

needs_recovery = (es->s_last_orphan != 0 ||
@@ -5910,6 +5913,8 @@ static int ext4_quota_on(struct super_block *sb, int type, int format_id,
EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
S_NOATIME | S_IMMUTABLE);
+ ext4_fc_mark_ineligible(inode,
+ EXT4_FC_REASON_QUOTA);
ext4_mark_inode_dirty(handle, inode);
ext4_journal_stop(handle);
unlock_inode:
@@ -6017,6 +6022,8 @@ static int ext4_quota_off(struct super_block *sb, int type)
EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
inode->i_mtime = inode->i_ctime = current_time(inode);
+ ext4_fc_mark_ineligible(inode,
+ EXT4_FC_REASON_QUOTA);
ext4_mark_inode_dirty(handle, inode);
ext4_journal_stop(handle);
out_unlock:
@@ -6123,6 +6130,8 @@ static ssize_t ext4_quota_write(struct super_block *sb, int type,
if (inode->i_size < off + len) {
i_size_write(inode, off + len);
EXT4_I(inode)->i_disksize = inode->i_size;
+ ext4_fc_mark_ineligible(inode,
+ EXT4_FC_REASON_QUOTA);
ext4_mark_inode_dirty(handle, inode);
}
return len;
diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
index 8cac7d95c3ad..c090ac12aa93 100644
--- a/fs/ext4/xattr.c
+++ b/fs/ext4/xattr.c
@@ -2413,6 +2413,8 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
if (IS_SYNC(inode))
ext4_handle_sync(handle);
}
+ ext4_fc_mark_ineligible(inode,
+ EXT4_FC_REASON_XATTR);

cleanup:
brelse(is.iloc.bh);
@@ -2490,6 +2492,8 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
if (error == 0)
error = error2;
}
+ ext4_fc_mark_ineligible(inode,
+ EXT4_FC_REASON_XATTR);

return error;
}
@@ -2922,6 +2926,8 @@ int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
error);
goto cleanup;
}
+ ext4_fc_mark_ineligible(inode,
+ EXT4_FC_REASON_XATTR);
}
error = 0;
cleanup:
--
2.25.1.481.gfbce0eb801-goog

2020-03-09 07:06:56

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 13/20] jbd2: add new APIs for commit path of fast commits

Add following helpers for commit path:
- jbd2_map_fc_buf() - allocates fast commit buffers for caller
- jbd2_wait_on_fc_bufs() - waits on fast commit buffers allocated
using jbd2_map_fc_buf()
- jbd2_submit_inode_data() - submit data buffers for one inode
- jbd2_wait_inode_data() - wait for inode data

Signed-off-by: Harshad Shirwadkar <[email protected]>
---
fs/jbd2/commit.c | 40 +++++++++++++++++++++++
fs/jbd2/journal.c | 76 ++++++++++++++++++++++++++++++++++++++++++++
include/linux/jbd2.h | 6 ++++
3 files changed, 122 insertions(+)

diff --git a/fs/jbd2/commit.c b/fs/jbd2/commit.c
index 869fe193fbe3..a4c4c6717674 100644
--- a/fs/jbd2/commit.c
+++ b/fs/jbd2/commit.c
@@ -202,6 +202,46 @@ static int journal_submit_inode_data_buffers(struct address_space *mapping,
return ret;
}

+/* Send all the data buffers related to an inode */
+int jbd2_submit_inode_data(journal_t *journal, struct jbd2_inode *jinode)
+{
+ struct address_space *mapping;
+ loff_t dirty_start;
+ loff_t dirty_end;
+ int ret;
+
+ if (!jinode)
+ return 0;
+
+ dirty_start = jinode->i_dirty_start;
+ dirty_end = jinode->i_dirty_end;
+
+ if (!(jinode->i_flags & JI_WRITE_DATA))
+ return 0;
+
+ dirty_start = jinode->i_dirty_start;
+ dirty_end = jinode->i_dirty_end;
+
+ mapping = jinode->i_vfs_inode->i_mapping;
+
+ trace_jbd2_submit_inode_data(jinode->i_vfs_inode);
+ ret = journal_submit_inode_data_buffers(mapping, dirty_start,
+ dirty_end);
+
+ return ret;
+}
+EXPORT_SYMBOL(jbd2_submit_inode_data);
+
+int jbd2_wait_inode_data(journal_t *journal, struct jbd2_inode *jinode)
+{
+ if (!jinode || !(jinode->i_flags & JI_WAIT_DATA))
+ return 0;
+ return filemap_fdatawait_range_keep_errors(
+ jinode->i_vfs_inode->i_mapping, jinode->i_dirty_start,
+ jinode->i_dirty_end);
+}
+EXPORT_SYMBOL(jbd2_wait_inode_data);
+
/*
* Submit all the data buffers of inode associated with the transaction to
* disk.
diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
index d3897d155fb9..e4e0b55dd077 100644
--- a/fs/jbd2/journal.c
+++ b/fs/jbd2/journal.c
@@ -864,6 +864,82 @@ int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
return jbd2_journal_bmap(journal, blocknr, retp);
}

+/* Map one fast commit buffer for use by the file system */
+int jbd2_map_fc_buf(journal_t *journal, struct buffer_head **bh_out)
+{
+ unsigned long long pblock;
+ unsigned long blocknr;
+ int ret = 0;
+ struct buffer_head *bh;
+ int fc_off;
+ journal_header_t *jhdr;
+
+ write_lock(&journal->j_state_lock);
+
+ if (journal->j_fc_off + journal->j_first_fc < journal->j_last_fc) {
+ fc_off = journal->j_fc_off;
+ blocknr = journal->j_first_fc + fc_off;
+ journal->j_fc_off++;
+ } else {
+ ret = -EINVAL;
+ }
+ write_unlock(&journal->j_state_lock);
+
+ if (ret)
+ return ret;
+
+ ret = jbd2_journal_bmap(journal, blocknr, &pblock);
+ if (ret)
+ return ret;
+
+ bh = __getblk(journal->j_dev, pblock, journal->j_blocksize);
+ if (!bh)
+ return -ENOMEM;
+
+ lock_buffer(bh);
+ jhdr = (journal_header_t *)bh->b_data;
+ jhdr->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
+ jhdr->h_blocktype = cpu_to_be32(JBD2_FC_BLOCK);
+ jhdr->h_sequence = cpu_to_be32(journal->j_running_transaction->t_tid);
+
+ set_buffer_uptodate(bh);
+ unlock_buffer(bh);
+ journal->j_fc_wbuf[fc_off] = bh;
+
+ *bh_out = bh;
+
+ return 0;
+}
+EXPORT_SYMBOL(jbd2_map_fc_buf);
+
+/*
+ * Wait on fast commit buffers that were allocated by jbd2_map_fc_buf
+ * for completion.
+ */
+int jbd2_wait_on_fc_bufs(journal_t *journal, int num_blks)
+{
+ struct buffer_head *bh;
+ int i, j_fc_off;
+
+ read_lock(&journal->j_state_lock);
+ j_fc_off = journal->j_fc_off;
+ read_unlock(&journal->j_state_lock);
+
+ /*
+ * Wait in reverse order to minimize chances of us being woken up before
+ * all IOs have completed
+ */
+ for (i = j_fc_off - 1; i >= j_fc_off - num_blks; i--) {
+ bh = journal->j_fc_wbuf[i];
+ wait_on_buffer(bh);
+ if (unlikely(!buffer_uptodate(bh)))
+ return -EIO;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL(jbd2_wait_on_fc_bufs);
+
/*
* Conversion of logical to physical block numbers for the journal
*
diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
index 0a4d9d484528..599113bef67f 100644
--- a/include/linux/jbd2.h
+++ b/include/linux/jbd2.h
@@ -123,6 +123,7 @@ typedef struct journal_s journal_t; /* Journal control structure */
#define JBD2_SUPERBLOCK_V1 3
#define JBD2_SUPERBLOCK_V2 4
#define JBD2_REVOKE_BLOCK 5
+#define JBD2_FC_BLOCK 6

/*
* Standard header for all descriptor blocks:
@@ -1562,6 +1563,11 @@ int jbd2_start_async_fc_nowait(journal_t *journal, tid_t tid);
int jbd2_start_async_fc_wait(journal_t *journal, tid_t tid);
void jbd2_stop_async_fc(journal_t *journal, tid_t tid);
void jbd2_init_fast_commit(journal_t *journal, int num_fc_blks);
+int jbd2_map_fc_buf(journal_t *journal, struct buffer_head **bh_out);
+int jbd2_wait_on_fc_bufs(journal_t *journal, int num_blks);
+int jbd2_submit_inode_data(journal_t *journal, struct jbd2_inode *jinode);
+int jbd2_wait_inode_data(journal_t *journal, struct jbd2_inode *jinode);
+
/*
* is_journal_abort
*
--
2.25.1.481.gfbce0eb801-goog

2020-03-09 07:06:57

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 14/20] ext4: main commit routine for fast commits

Add main commit routine for fast commits to perform a fast commit.
Also, handle race between inode deletion which results in inode
being deleted from fast commit list and the commit routine. Add
commit routines for fast commit with hard consistency as well
as for soft consistency.

Signed-off-by: Harshad Shirwadkar <[email protected]>
---
fs/ext4/ext4.h | 7 +
fs/ext4/ext4_jbd2.c | 714 ++++++++++++++++++++++++++++++++++++
fs/ext4/ext4_jbd2.h | 2 +
fs/ext4/fsync.c | 2 +-
fs/ext4/inode.c | 6 +-
fs/ext4/migrate.c | 1 +
fs/ext4/super.c | 12 +
include/trace/events/ext4.h | 101 +++++
8 files changed, 842 insertions(+), 3 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 57ccc26012f0..401d28b57d81 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1764,6 +1764,10 @@ enum {
EXT4_STATE_LUSTRE_EA_INODE, /* Lustre-style ea_inode */
EXT4_STATE_VERITY_IN_PROGRESS, /* building fs-verity Merkle tree */
EXT4_STATE_FC_ELIGIBLE, /* File is Fast commit eligible */
+ EXT4_STATE_FC_DATA_SUBMIT, /* File is going through fast commit */
+ EXT4_STATE_FC_MDATA_SUBMIT, /* Fast commit block is
+ * being submitted
+ */
};

#define EXT4_INODE_BIT_FNS(name, field, offset) \
@@ -2854,6 +2858,9 @@ extern int ext4_group_extend(struct super_block *sb,
extern int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count);

/* super.c */
+
+int ext4_fc_async_commit_inode(journal_t *journal, tid_t commit_tid,
+ struct inode *inode);
extern struct buffer_head *ext4_sb_bread(struct super_block *sb,
sector_t block, int op_flags);
extern int ext4_seq_options_show(struct seq_file *seq, void *offset);
diff --git a/fs/ext4/ext4_jbd2.c b/fs/ext4/ext4_jbd2.c
index 78c5431c7aad..f291c186eb34 100644
--- a/fs/ext4/ext4_jbd2.c
+++ b/fs/ext4/ext4_jbd2.c
@@ -392,6 +392,8 @@ void ext4_init_inode_fc_info(struct inode *inode)
struct ext4_inode_info *ei = EXT4_I(inode);

ext4_reset_inode_fc_info(inode);
+ ext4_clear_inode_state(inode, EXT4_STATE_FC_DATA_SUBMIT);
+ ext4_clear_inode_state(inode, EXT4_STATE_FC_MDATA_SUBMIT);
INIT_LIST_HEAD(&ei->i_fc_list);
}

@@ -416,6 +418,43 @@ static inline tid_t get_running_txn_tid(struct super_block *sb)
return 0;
}

+void ext4_fc_del(struct inode *inode)
+{
+ if (!ext4_should_fast_commit(inode->i_sb) ||
+ (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY))
+ return;
+
+ if (list_empty(&EXT4_I(inode)->i_fc_list))
+ return;
+
+ ext4_fc_disable(inode->i_sb, EXT4_FC_REASON_DELETE);
+
+restart:
+ spin_lock(&EXT4_SB(inode->i_sb)->s_fc_lock);
+ if (ext4_test_inode_state(inode, EXT4_STATE_FC_DATA_SUBMIT)) {
+ struct ext4_inode_info *ei = EXT4_I(inode);
+ wait_queue_head_t *wq;
+#if (BITS_PER_LONG < 64)
+ DEFINE_WAIT_BIT(wait, &ei->i_state_flags,
+ EXT4_STATE_FC_DATA_SUBMIT);
+ wq = bit_waitqueue(&ei->i_state_flags,
+ EXT4_STATE_FC_DATA_SUBMIT);
+#else
+ DEFINE_WAIT_BIT(wait, &ei->i_flags,
+ EXT4_STATE_FC_DATA_SUBMIT);
+ wq = bit_waitqueue(&ei->i_flags,
+ EXT4_STATE_FC_DATA_SUBMIT);
+#endif
+ prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
+ spin_unlock(&EXT4_SB(inode->i_sb)->s_fc_lock);
+ schedule();
+ finish_wait(wq, &wait.wq_entry);
+ goto restart;
+ }
+ list_del_init(&EXT4_I(inode)->i_fc_list);
+ spin_unlock(&EXT4_SB(inode->i_sb)->s_fc_lock);
+}
+
bool ext4_is_inode_fc_ineligible(struct inode *inode)
{
if (get_running_txn_tid(inode->i_sb) == EXT4_I(inode)->i_fc_tid)
@@ -474,6 +513,7 @@ static int __ext4_fc_track_template(
return -EOPNOTSUPP;

write_lock(&ei->i_fc_lock);
+ ext4_clear_inode_state(inode, EXT4_STATE_FC_MDATA_SUBMIT);
if (running_txn_tid == ei->i_fc_tid) {
if (!ext4_test_inode_state(inode, EXT4_STATE_FC_ELIGIBLE)) {
write_unlock(&ei->i_fc_lock);
@@ -646,10 +686,684 @@ void ext4_fc_track_range(struct inode *inode, ext4_lblk_t start,
trace_ext4_fc_track_range(inode, start, end, ret);
}

+static void ext4_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
+{
+ BUFFER_TRACE(bh, "");
+ if (uptodate) {
+ ext4_debug("%s: Block %lld up-to-date",
+ __func__, bh->b_blocknr);
+ set_buffer_uptodate(bh);
+ } else {
+ ext4_debug("%s: Block %lld not up-to-date",
+ __func__, bh->b_blocknr);
+ clear_buffer_uptodate(bh);
+ }
+
+ unlock_buffer(bh);
+}
+
+void submit_fc_bh(struct buffer_head *bh)
+{
+ lock_buffer(bh);
+ clear_buffer_dirty(bh);
+ set_buffer_uptodate(bh);
+ bh->b_end_io = ext4_end_buffer_io_sync;
+ submit_bh(REQ_OP_WRITE, REQ_SYNC, bh);
+}
+
+/*
+ * Writes fast commit header and inode structure at memory
+ * pointed to by start. Returns 0 on success, error on failure.
+ * If successful, *last is upadated to point to the end of
+ * inode that was copied.
+ */
+static int fc_write_hdr(struct inode *inode, u8 *start, u8 *end,
+ u8 **last)
+{
+ struct ext4_fc_commit_hdr *fc_hdr = (struct ext4_fc_commit_hdr *)start;
+ struct ext4_inode_info *ei = EXT4_I(inode);
+ int inode_len = EXT4_GOOD_OLD_INODE_SIZE;
+ struct ext4_iloc iloc;
+ u8 *cur = start;
+ int ret;
+
+ if (ext4_is_inode_fc_ineligible(inode))
+ return -ECANCELED;
+
+ ret = ext4_get_inode_loc(inode, &iloc);
+ if (ret)
+ return ret;
+
+ fc_hdr->fc_magic = cpu_to_le32(EXT4_FC_MAGIC);
+ fc_hdr->fc_ino = cpu_to_le32(inode->i_ino);
+ fc_hdr->fc_features = 0;
+ fc_hdr->fc_csum = 0;
+
+ cur = (u8 *)(fc_hdr + 1);
+ if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE)
+ inode_len += ei->i_extra_isize;
+ if (cur + inode_len >= end)
+ return -ECANCELED;
+
+ memcpy(cur, ext4_raw_inode(&iloc), inode_len);
+ cur += inode_len;
+ *last = cur;
+
+ return 0;
+}
+
+/*
+ * Adds tag, length and value at memory pointed to by dst. Returns
+ * true if tlv was added. Returns false if there's not enough space.
+ * If successful also updates *dst to point to the end of this tlv.
+ */
+static bool fc_try_add_tlv(u8 **dst, u8 *end, u16 tag, u16 len, u8 *val)
+{
+ struct ext4_fc_tl tl;
+
+ if (*dst + sizeof(tl) + len >= end)
+ return false;
+
+ tl.fc_tag = cpu_to_le16(tag);
+ tl.fc_len = cpu_to_le16(len);
+ memcpy(*dst, &tl, sizeof(tl));
+ memcpy(*dst + sizeof(tl), val, len);
+
+ *dst = *dst + sizeof(tl) + len;
+ return true;
+}
+
+/* Same as above, but tries to add dentry tlv. */
+static bool fc_try_add_dentry_info_tlv(u8 **dst, u8 *end, u16 tag,
+ int parent_ino, int ino, int dlen,
+ const unsigned char *dname)
+{
+ struct ext4_fc_dentry_info fcd;
+ struct ext4_fc_tl tl;
+
+
+ if (*dst + sizeof(tl) + sizeof(fcd) + dlen >= end)
+ return false;
+
+ fcd.fc_parent_ino = cpu_to_le32(parent_ino);
+ fcd.fc_ino = cpu_to_le32(ino);
+ tl.fc_tag = cpu_to_le16(tag);
+ tl.fc_len = cpu_to_le16(sizeof(fcd) + dlen);
+ memcpy(*dst, &tl, sizeof(tl));
+ *dst += sizeof(tl);
+ memcpy(*dst, &fcd, sizeof(fcd));
+ *dst += sizeof(fcd);
+ memcpy(*dst, dname, dlen);
+ *dst += dlen;
+
+ return true;
+}
+
+/*
+ * Writes data tags (EXT4_FC_TAG_ADD_RANGE / EXT4_FC_TAG_DEL_RANGE)
+ * at memory pointed to by start. Returns number of TLVs that were
+ * added if successfully. Returns errors otherwise.
+ */
+static int fc_write_data(struct inode *inode, u8 *start, u8 *end,
+ u8 **last)
+{
+ ext4_lblk_t old_blk_size, cur_lblk_off, new_blk_size;
+ struct ext4_inode_info *ei = EXT4_I(inode);
+ struct ext4_map_blocks map;
+ struct ext4_extent extent;
+ struct ext4_fc_lrange lrange;
+ u8 *cur = start;
+ int num_tlvs = 0;
+ int ret;
+
+ write_lock(&ei->i_fc_lock);
+ old_blk_size = ei->i_fc_lblk_start;
+ new_blk_size = ei->i_fc_lblk_end;
+ ei->i_fc_lblk_start = ei->i_fc_lblk_end;
+ write_unlock(&ei->i_fc_lock);
+
+ cur_lblk_off = old_blk_size;
+ jbd_debug(1, "%s: will try writing %ld to %ld for inode %ld\n",
+ __func__, cur_lblk_off, new_blk_size, inode->i_ino);
+ while (cur_lblk_off <= new_blk_size) {
+ map.m_lblk = cur_lblk_off;
+ map.m_len = new_blk_size - cur_lblk_off + 1;
+ ret = ext4_map_blocks(NULL, inode, &map, 0);
+ if (ret < 0)
+ return ret;
+ if (map.m_len == 0)
+ return -ECANCELED;
+ if (map.m_flags & EXT4_MAP_UNWRITTEN)
+ return -ECANCELED;
+
+ cur_lblk_off += map.m_len;
+ if (ret == 0) {
+ lrange.fc_lblk = cpu_to_le32(map.m_lblk);
+ lrange.fc_len = cpu_to_le32(map.m_len);
+ if (!fc_try_add_tlv(&cur, end, EXT4_FC_TAG_DEL_RANGE,
+ sizeof(lrange), (u8 *)&lrange))
+ return -ENOSPC;
+
+ } else {
+ extent.ee_block = cpu_to_le32(map.m_lblk);
+ extent.ee_len = cpu_to_le16(map.m_len);
+ ext4_ext_store_pblock(&extent, map.m_pblk);
+ ext4_ext_mark_initialized(&extent);
+ if (!fc_try_add_tlv(&cur, end, EXT4_FC_TAG_ADD_RANGE,
+ sizeof(struct ext4_extent), (u8 *)&extent))
+ return -ENOSPC;
+ }
+ num_tlvs++;
+ }
+ *last = cur;
+
+ return num_tlvs;
+}
+
+static int fc_commit_data_inode(journal_t *journal, struct inode *inode)
+{
+ struct ext4_fc_commit_hdr *hdr;
+ struct buffer_head *bh;
+ u8 *start, *cur, *end;
+ int ret;
+ int num_tlvs = 0;
+
+ ret = jbd2_map_fc_buf(journal, &bh);
+ if (ret)
+ return -ECANCELED;
+
+ start = cur = ((__u8 *)bh->b_data + sizeof(journal_header_t));
+ end = (__u8 *)bh->b_data + journal->j_blocksize;
+ hdr = (struct ext4_fc_commit_hdr *)start;
+
+ ret = fc_write_hdr(inode, start, end, &cur);
+ if (ret < 0)
+ return ret;
+
+ ret = fc_write_data(inode, cur, end, &cur);
+ if (ret < 0)
+ return ret;
+ memset(cur, 0, end - cur);
+
+ hdr->fc_num_tlvs = cpu_to_le16(num_tlvs + ret);
+ hdr->fc_csum = 0;
+ hdr->fc_csum = cpu_to_le32(ext4_chksum(EXT4_SB(inode->i_sb),
+ 0, start, end - start));
+ submit_fc_bh(bh);
+ ext4_set_inode_state(inode, EXT4_STATE_FC_MDATA_SUBMIT);
+
+ return 1;
+}
+
+static int submit_all_inode_data(journal_t *journal)
+{
+ struct super_block *sb = (struct super_block *)(journal->j_private);
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+ struct ext4_inode_info *iter;
+ struct list_head *pos;
+ int ret = 0;
+
+ spin_lock(&sbi->s_fc_lock);
+ list_for_each(pos, &sbi->s_fc_q) {
+ iter = list_entry(pos, struct ext4_inode_info, i_fc_list);
+ ext4_set_inode_state(&iter->vfs_inode,
+ EXT4_STATE_FC_DATA_SUBMIT);
+ spin_unlock(&sbi->s_fc_lock);
+ ret = jbd2_submit_inode_data(journal, iter->jinode);
+ if (ret)
+ return ret;
+ spin_lock(&sbi->s_fc_lock);
+ }
+ spin_unlock(&sbi->s_fc_lock);
+
+ return ret;
+}
+
+static int wait_all_inode_data(journal_t *journal)
+{
+ struct super_block *sb = (struct super_block *)(journal->j_private);
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+ struct ext4_inode_info *pos, *n;
+ int ret = 0;
+
+ spin_lock(&sbi->s_fc_lock);
+ list_for_each_entry_safe(pos, n, &sbi->s_fc_q, i_fc_list) {
+ if (!ext4_test_inode_state(&pos->vfs_inode,
+ EXT4_STATE_FC_DATA_SUBMIT))
+ continue;
+ spin_unlock(&sbi->s_fc_lock);
+ ret = jbd2_wait_inode_data(journal, pos->jinode);
+ if (ret)
+ break;
+ spin_lock(&sbi->s_fc_lock);
+ list_safe_reset_next(pos, n, i_fc_list);
+ list_del_init(&pos->i_fc_list);
+
+ ext4_clear_inode_state(&pos->vfs_inode,
+ EXT4_STATE_FC_DATA_SUBMIT);
+ /* Make sure DATA_SUBMIT bit is set before waking up */
+ smp_mb();
+#if (BITS_PER_LONG < 64)
+ wake_up_bit(&pos->i_state_flags, EXT4_STATE_FC_DATA_SUBMIT);
+#else
+ wake_up_bit(&pos->i_flags, EXT4_STATE_FC_DATA_SUBMIT);
+#endif
+ }
+ spin_unlock(&sbi->s_fc_lock);
+
+ return 0;
+}
+
+static int fc_inode_match(struct inode *inode, void *data)
+{
+ if (inode->i_ino != (long)data)
+ return 0;
+
+ if (inode->i_nlink)
+ return 1;
+
+ /*
+ * Avoid returning a nearly dead inode (withi_nlink == 0).
+ */
+ if (ext4_test_inode_state(inode,
+ EXT4_STATE_FC_DATA_SUBMIT)) {
+ /*
+ * This is a tricky situation, after we
+ * submitted data for this inode, someone
+ * tried to free this. ext4_fc_del() is
+ * waiting on FC_DATA_SUBMIT bit to clear.
+ * Since we are never going to wait for data
+ * just wake the sleeper.
+ * TODO: Even in this case don't fallback to full commits
+ * and indicate the caller that this is a deleted inode.
+ */
+ ext4_clear_inode_state(
+ inode, EXT4_STATE_FC_DATA_SUBMIT);
+ /* Make sure that data_submit bit is set */
+ smp_mb();
+#if (BITS_PER_LONG < 64)
+ wake_up_bit(&EXT4_I(inode)->i_state_flags,
+ EXT4_STATE_FC_DATA_SUBMIT);
+#else
+ wake_up_bit(&EXT4_I(inode)->i_flags,
+ EXT4_STATE_FC_DATA_SUBMIT);
+#endif
+ }
+ return 0;
+}
+
+/*
+ * Commits all the dentry updates and respective inodes till and
+ * including "last".
+ */
+static int fc_commit_dentry_updates(journal_t *journal,
+ struct ext4_fc_dentry_update *last)
+{
+ struct super_block *sb = (struct super_block *)(journal->j_private);
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+ struct ext4_fc_commit_hdr *hdr;
+ struct ext4_fc_dentry_update *fc_dentry;
+ struct inode *inode;
+ struct buffer_head *bh;
+ u8 *start, *cur, *end;
+ int len, ret;
+ int nblks = 0;
+ int num_tlvs = 0;
+ bool is_last;
+
+ ret = jbd2_map_fc_buf(journal, &bh);
+ if (ret)
+ return -ECANCELED;
+
+ start = cur = ((__u8 *)bh->b_data + sizeof(journal_header_t));
+ end = (__u8 *)bh->b_data + journal->j_blocksize;
+ hdr = (struct ext4_fc_commit_hdr *)start;
+
+ spin_lock(&sbi->s_fc_lock);
+ while (!list_empty(&sbi->s_fc_dentry_q)) {
+ fc_dentry = list_first_entry(
+ &sbi->s_fc_dentry_q, struct ext4_fc_dentry_update,
+ fcd_list);
+ list_del_init(&fc_dentry->fcd_list);
+ spin_unlock(&sbi->s_fc_lock);
+ if (!fc_try_add_dentry_info_tlv(
+ &cur, end, fc_dentry->fcd_op,
+ fc_dentry->fcd_parent, fc_dentry->fcd_ino,
+ fc_dentry->fcd_name.len,
+ fc_dentry->fcd_name.name)) {
+ kmem_cache_free(ext4_fc_dentry_cachep, fc_dentry);
+ return -ENOSPC;
+ }
+ num_tlvs++;
+ inode = ilookup5_nowait(sb, fc_dentry->fcd_ino, fc_inode_match,
+ (void *)(long)fc_dentry->fcd_ino);
+ /*
+ * If this was the last metadata update for this inode, clear
+ * since we are going to handle it now.
+ */
+ if (inode && EXT4_I(inode)->i_fc_mdata_update == fc_dentry)
+ EXT4_I(inode)->i_fc_mdata_update = NULL;
+ if (fc_dentry != last &&
+ fc_dentry->fcd_op != EXT4_FC_TAG_CREAT_DENTRY) {
+ if (inode)
+ iput(inode);
+ spin_lock(&sbi->s_fc_lock);
+ kmem_cache_free(ext4_fc_dentry_cachep, fc_dentry);
+ continue;
+ }
+ is_last = (fc_dentry == last);
+ kmem_cache_free(ext4_fc_dentry_cachep, fc_dentry);
+ if (IS_ERR_OR_NULL(inode))
+ /*
+ * Inode got evicted from memory for some
+ * reason. it's possible that someone deleted
+ * the inode after we started fast commit.
+ * We just abort fast commits in this case.
+ */
+ return -ECANCELED;
+
+ /*
+ * It's either the last dentry update or it's inode
+ * creation. Until now, we have written all the
+ * directory entry updates since the beginning or
+ * the last creation in current fast commit buf.
+ * Move the contents towards the end of the block and
+ * then write header first. We move it to the end
+ * because header size is variable.
+ */
+ len = cur - start;
+ memmove(end - len, start, len);
+ ret = fc_write_hdr(inode, start, end - len, &cur);
+ if (ret < 0) {
+ iput(inode);
+ return ret;
+ }
+ /*
+ * Place directory entry updates right after the
+ * header and the inode and write remaining
+ * tags if any.
+ */
+ memmove(cur, end - len, len);
+ cur = cur + len;
+ if (inode->i_nlink) {
+ ret = fc_write_data(inode, cur, end, &cur);
+ if (ret < 0) {
+ iput(inode);
+ return ret;
+ }
+ }
+ memset(cur, 0, end - cur);
+ hdr->fc_num_tlvs = cpu_to_le16(num_tlvs + ret);
+ hdr->fc_csum = cpu_to_le32(
+ ext4_chksum(sbi, 0, start, end - start));
+ submit_fc_bh(bh);
+ nblks++;
+ if (!inode->i_nlink) {
+ ext4_clear_inode_state(inode,
+ EXT4_STATE_FC_DATA_SUBMIT);
+ smp_mb(); /* Make sure data submit bit is set */
+#if (BITS_PER_LONG < 64)
+ wake_up_bit(&EXT4_I(inode)->i_state_flags,
+ EXT4_STATE_FC_DATA_SUBMIT);
+#else
+ wake_up_bit(&EXT4_I(inode)->i_flags,
+ EXT4_STATE_FC_DATA_SUBMIT);
+#endif
+ } else if (!ext4_test_inode_state(inode,
+ EXT4_STATE_FC_DATA_SUBMIT)) {
+ ret = jbd2_submit_inode_data(
+ journal, EXT4_I(inode)->jinode);
+ if (ret < 0)
+ return ret;
+ ext4_set_inode_state(inode,
+ EXT4_STATE_FC_DATA_SUBMIT);
+ }
+ ext4_set_inode_state(inode, EXT4_STATE_FC_MDATA_SUBMIT);
+ iput(inode);
+ if (is_last) {
+ bh = NULL;
+ goto skip_unlock;
+ }
+ ret = jbd2_map_fc_buf(journal, &bh);
+ if (ret < 0)
+ return ret;
+ start = cur = ((__u8 *)bh->b_data + sizeof(journal_header_t));
+ hdr = (struct ext4_fc_commit_hdr *)start;
+ end = (__u8 *)bh->b_data + journal->j_blocksize;
+ memset(start, 0, end - start);
+ spin_lock(&sbi->s_fc_lock);
+ }
+
+ spin_unlock(&sbi->s_fc_lock);
+skip_unlock:
+ WARN_ON(bh != NULL);
+ return nblks;
+}
+
+static void ext4_journal_fc_cleanup_cb(journal_t *journal)
+{
+ struct super_block *sb = journal->j_private;
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+ struct ext4_inode_info *iter;
+ struct ext4_fc_dentry_update *fc_dentry;
+
+ spin_lock(&sbi->s_fc_lock);
+ while (!list_empty(&sbi->s_fc_q)) {
+ iter = list_first_entry(&sbi->s_fc_q,
+ struct ext4_inode_info, i_fc_list);
+ iter->i_fc_mdata_update = NULL;
+
+ list_del_init(&iter->i_fc_list);
+ ext4_clear_inode_state(&iter->vfs_inode,
+ EXT4_STATE_FC_DATA_SUBMIT);
+ ext4_clear_inode_state(&iter->vfs_inode,
+ EXT4_STATE_FC_MDATA_SUBMIT);
+ /* Make sure DATA_SUBMIT bit is set */
+ smp_mb();
+ wake_up_bit(&iter->i_flags, EXT4_STATE_FC_DATA_SUBMIT);
+ }
+ INIT_LIST_HEAD(&sbi->s_fc_q);
+ while (!list_empty(&sbi->s_fc_dentry_q)) {
+ fc_dentry = list_first_entry(&sbi->s_fc_dentry_q,
+ struct ext4_fc_dentry_update,
+ fcd_list);
+ list_del_init(&fc_dentry->fcd_list);
+ spin_unlock(&sbi->s_fc_lock);
+
+ if (fc_dentry->fcd_name.name &&
+ fc_dentry->fcd_name.len > DNAME_INLINE_LEN)
+ kfree(fc_dentry->fcd_name.name);
+ kmem_cache_free(ext4_fc_dentry_cachep, fc_dentry);
+ spin_lock(&sbi->s_fc_lock);
+ }
+ INIT_LIST_HEAD(&sbi->s_fc_dentry_q);
+ sbi->s_mount_state &= ~EXT4_FC_INELIGIBLE;
+ spin_unlock(&sbi->s_fc_lock);
+ trace_ext4_journal_fc_stats(sb);
+}
+
+int ext4_fc_perform_hard_commit(journal_t *journal)
+{
+ struct super_block *sb = (struct super_block *)(journal->j_private);
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+ struct ext4_inode_info *iter;
+ struct list_head *pos;
+ struct inode *inode;
+ int ret = 0, nblks = 0;
+
+ ret = submit_all_inode_data(journal);
+ if (ret < 0)
+ return ret;
+
+ if (!list_empty(&EXT4_SB(sb)->s_fc_dentry_q)) {
+ ret = fc_commit_dentry_updates(
+ journal, list_last_entry(
+ &EXT4_SB(sb)->s_fc_dentry_q,
+ struct ext4_fc_dentry_update,
+ fcd_list));
+ if (ret < 0)
+ return ret;
+ nblks = ret;
+ }
+
+ spin_lock(&sbi->s_fc_lock);
+ list_for_each(pos, &sbi->s_fc_q) {
+ iter = list_entry(pos, struct ext4_inode_info, i_fc_list);
+ inode = &iter->vfs_inode;
+ if (ext4_test_inode_state(
+ inode, EXT4_STATE_FC_MDATA_SUBMIT) ||
+ !ext4_test_inode_state(
+ inode, EXT4_STATE_FC_DATA_SUBMIT))
+ continue;
+
+ spin_unlock(&sbi->s_fc_lock);
+ ret = fc_commit_data_inode(journal, inode);
+ if (ret < 0)
+ return ret;
+ nblks += ret;
+ spin_lock(&sbi->s_fc_lock);
+ }
+ spin_unlock(&sbi->s_fc_lock);
+
+ ret = wait_all_inode_data(journal);
+ if (ret < 0)
+ return ret;
+
+ return nblks;
+}
+
+int ext4_fc_async_commit_inode(journal_t *journal, tid_t commit_tid,
+ struct inode *inode)
+{
+ struct ext4_inode_info *ei = EXT4_I(inode);
+ struct super_block *sb = inode->i_sb;
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+ int nblks = 0, ret;
+ int start_jiffies;
+
+ trace_ext4_journal_fc_commit_cb_start(sb);
+ start_jiffies = jiffies;
+
+ if (!ext4_should_fast_commit(sb) ||
+ (sbi->s_mount_state & EXT4_FC_INELIGIBLE)) {
+ sbi->s_fc_stats.fc_ineligible_commits++;
+ trace_ext4_journal_fc_commit_cb_stop(sb, 0, "disabled");
+ trace_ext4_journal_fc_stats(sb);
+ return jbd2_complete_transaction(journal, commit_tid);
+ }
+
+ if (ext4_is_inode_fc_ineligible(inode)) {
+ sbi->s_fc_stats.fc_ineligible_commits++;
+ trace_ext4_journal_fc_commit_cb_stop(sb, 0, "ineligible");
+ trace_ext4_journal_fc_stats(sb);
+ return jbd2_complete_transaction(journal, commit_tid);
+ }
+
+ /*
+ * In case of soft consistency mode, we wait for any parallel
+ * fast commits to complete. In case of hard consistency, if a
+ * parallel fast commit is ongoing, it is going to take care
+ * of us as well, so we don't wait.
+ */
+ if (!test_opt2(sb, JOURNAL_FC_SOFT_CONSISTENCY))
+ ret = jbd2_start_async_fc_nowait(journal, commit_tid);
+ else
+ ret = jbd2_start_async_fc_wait(journal, commit_tid);
+ if (ret == -EALREADY) {
+ trace_ext4_journal_fc_commit_cb_stop(sb, 0, "already");
+ trace_ext4_journal_fc_stats(sb);
+ return 0;
+ }
+
+ if (ret) {
+ sbi->s_fc_stats.fc_ineligible_commits++;
+ trace_ext4_journal_fc_commit_cb_stop(sb, 0, "start");
+ trace_ext4_journal_fc_stats(sb);
+ return jbd2_complete_transaction(journal, commit_tid);
+ }
+
+ if (ext4_test_inode_state(inode, EXT4_STATE_FC_MDATA_SUBMIT)) {
+ jbd2_stop_async_fc(journal, commit_tid);
+ trace_ext4_journal_fc_commit_cb_stop(sb, 0, "committed");
+ trace_ext4_journal_fc_stats(sb);
+ return 0;
+ }
+
+ if (ei->i_fc_tid != commit_tid) {
+ jbd2_stop_async_fc(journal, commit_tid);
+ trace_ext4_journal_fc_commit_cb_stop(sb, 0, "stale");
+ trace_ext4_journal_fc_stats(sb);
+ return 0;
+ }
+
+ if (!test_opt2(sb, JOURNAL_FC_SOFT_CONSISTENCY)) {
+ ret = ext4_fc_perform_hard_commit(journal);
+ nblks = ret;
+ } else if (ei->i_fc_mdata_update) {
+ ret = submit_all_inode_data(journal);
+ if (ret < 0)
+ goto out;
+ nblks = fc_commit_dentry_updates(journal,
+ ei->i_fc_mdata_update);
+ if (nblks < 0) {
+ ret = nblks;
+ goto out;
+ }
+ ret = wait_all_inode_data(journal);
+ } else if (!list_empty(&EXT4_I(inode)->i_fc_list)) {
+ ext4_set_inode_state(inode, EXT4_STATE_FC_DATA_SUBMIT);
+ ret = jbd2_submit_inode_data(journal, EXT4_I(inode)->jinode);
+ if (ret < 0)
+ goto out;
+ nblks = fc_commit_data_inode(journal, inode);
+ if (nblks < 0) {
+ ret = nblks;
+ goto out;
+ }
+ ext4_set_inode_state(inode, EXT4_STATE_FC_MDATA_SUBMIT);
+ ret = jbd2_wait_inode_data(journal, EXT4_I(inode)->jinode);
+ spin_lock(&sbi->s_fc_lock);
+
+ list_del_init(&EXT4_I(inode)->i_fc_list);
+ ext4_clear_inode_state(inode, EXT4_STATE_FC_DATA_SUBMIT);
+ smp_mb(); /* Make sure data submit bit is set */
+#if (BITS_PER_LONG < 64)
+ wake_up_bit(&EXT4_I(inode)->i_state_flags,
+ EXT4_STATE_FC_DATA_SUBMIT);
+#else
+ wake_up_bit(&EXT4_I(inode)->i_flags,
+ EXT4_STATE_FC_DATA_SUBMIT);
+#endif
+ spin_unlock(&sbi->s_fc_lock);
+ }
+
+out:
+ if (ret < 0) {
+ sbi->s_fc_stats.fc_ineligible_commits++;
+ trace_ext4_journal_fc_commit_cb_stop(sb, 0, "fail1");
+ jbd2_stop_async_fc(journal, commit_tid);
+ trace_ext4_journal_fc_stats(sb);
+ sbi->s_mount_state &= ~EXT4_FC_REPLAY;
+ return jbd2_complete_transaction(journal, commit_tid);
+ }
+ jbd2_wait_on_fc_bufs(journal, nblks);
+ jbd2_stop_async_fc(journal, commit_tid);
+
+ EXT4_SB(sb)->s_fc_stats.fc_num_commits++;
+ EXT4_SB(sb)->s_fc_stats.fc_numblks += nblks;
+ trace_ext4_journal_fc_commit_cb_stop(sb,
+ nblks < 0 ? 0 : nblks,
+ nblks >= 0 ? "success" : "fail2");
+ trace_ext4_journal_fc_stats(sb);
+ sbi->s_mount_state &= ~EXT4_FC_REPLAY;
+ return 0;
+}
+
void ext4_init_fast_commit(struct super_block *sb, journal_t *journal)
{
if (!ext4_should_fast_commit(sb))
return;
+ journal->j_fc_cleanup_callback = ext4_journal_fc_cleanup_cb;
jbd2_init_fast_commit(journal, EXT4_NUM_FC_BLKS);
}

diff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h
index 761148b99b35..185561b38793 100644
--- a/fs/ext4/ext4_jbd2.h
+++ b/fs/ext4/ext4_jbd2.h
@@ -583,5 +583,7 @@ int __init ext4_init_fc_dentry_cache(void);
void ext4_fc_track_inode(struct inode *inode);
void ext4_fc_mark_ineligible(struct inode *inode, int reason);
void ext4_fc_disable(struct super_block *sb, int reason);
+void ext4_fc_del(struct inode *inode);
+

#endif /* _EXT4_JBD2_H */
diff --git a/fs/ext4/fsync.c b/fs/ext4/fsync.c
index e10206e7f4bb..f3bab652f98c 100644
--- a/fs/ext4/fsync.c
+++ b/fs/ext4/fsync.c
@@ -114,7 +114,7 @@ static int ext4_fsync_journal(struct inode *inode, bool datasync,
!jbd2_trans_will_send_data_barrier(journal, commit_tid))
*needs_barrier = true;

- return jbd2_complete_transaction(journal, commit_tid);
+ return ext4_fc_async_commit_inode(journal, commit_tid, inode);
}

/*
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 5720a12d7371..b209f81a01b6 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -5121,8 +5121,10 @@ int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync)
return 0;

- err = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
- EXT4_I(inode)->i_sync_tid);
+ err = ext4_fc_async_commit_inode(EXT4_SB(inode->i_sb)
+ ->s_journal,
+ EXT4_I(inode)->i_sync_tid,
+ inode);
} else {
struct ext4_iloc iloc;

diff --git a/fs/ext4/migrate.c b/fs/ext4/migrate.c
index fb6520f37135..5ddb23c036a1 100644
--- a/fs/ext4/migrate.c
+++ b/fs/ext4/migrate.c
@@ -496,6 +496,7 @@ int ext4_ext_migrate(struct inode *inode)
* work to orphan_list_cleanup()
*/
ext4_orphan_del(NULL, tmp_inode);
+ ext4_fc_del(inode);
retval = PTR_ERR(handle);
goto out_tmp_inode;
}
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index a484520e83d5..ffa91815dacf 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -1165,6 +1165,11 @@ static int ext4_drop_inode(struct inode *inode)

if (!drop)
drop = fscrypt_drop_inode(inode);
+ if (drop) {
+ spin_unlock(&inode->i_lock);
+ ext4_fc_del(inode);
+ spin_lock(&inode->i_lock);
+ }

trace_ext4_drop_inode(inode, drop);
return drop;
@@ -1173,6 +1178,11 @@ static int ext4_drop_inode(struct inode *inode)
static void ext4_free_in_core_inode(struct inode *inode)
{
fscrypt_free_inode(inode);
+ if (!list_empty(&(EXT4_I(inode)->i_fc_list))) {
+ pr_warn("%s: inode %ld still in fc list",
+ __func__, inode->i_ino);
+ ext4_fc_del(inode);
+ }
kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
}

@@ -1187,6 +1197,8 @@ static void ext4_destroy_inode(struct inode *inode)
true);
dump_stack();
}
+ if (!list_empty(&(EXT4_I(inode)->i_fc_list)))
+ ext4_fc_del(inode);
}

static void init_once(void *foo)
diff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h
index 5d278a8082a7..c8a05453f166 100644
--- a/include/trace/events/ext4.h
+++ b/include/trace/events/ext4.h
@@ -92,6 +92,19 @@ TRACE_DEFINE_ENUM(ES_REFERENCED_B);
{ FALLOC_FL_COLLAPSE_RANGE, "COLLAPSE_RANGE"}, \
{ FALLOC_FL_ZERO_RANGE, "ZERO_RANGE"})

+#define show_fc_reason(reason) \
+ __print_symbolic(reason, \
+ { EXT4_FC_REASON_META_ALLOC, "META_ALLOC"}, \
+ { EXT4_FC_REASON_QUOTA, "QUOTA"}, \
+ { EXT4_FC_REASON_XATTR, "XATTR"}, \
+ { EXT4_FC_REASON_CROSS_RENAME, "CROSS_RENAME"}, \
+ { EXT4_FC_REASON_FALLOC_RANGE_OP, "FALLOC_RANGE_OP"}, \
+ { EXT4_FC_REASON_JOURNAL_FLAG_CHANGE, "JOURNAL_FLAG_CHANGE"}, \
+ { EXT4_FC_REASON_MEM, "NO_MEM"}, \
+ { EXT4_FC_REASON_SWAP_BOOT, "SWAP_BOOT"}, \
+ { EXT4_FC_REASON_RESIZE, "RESIZE"}, \
+ { EXT4_FC_REASON_RENAME_DIR, "FALLOC_RANGE_OP"})
+

TRACE_EVENT(ext4_other_inode_update_time,
TP_PROTO(struct inode *inode, ino_t orig_ino),
@@ -2723,6 +2736,94 @@ TRACE_EVENT(ext4_error,
__entry->function, __entry->line)
);

+TRACE_EVENT(ext4_journal_fc_commit_cb_start,
+ TP_PROTO(struct super_block *sb),
+
+ TP_ARGS(sb),
+
+ TP_STRUCT__entry(
+ __field(dev_t, dev)
+ ),
+
+ TP_fast_assign(
+ __entry->dev = sb->s_dev;
+ ),
+
+ TP_printk("fast_commit started on dev %d,%d",
+ MAJOR(__entry->dev), MINOR(__entry->dev))
+);
+
+TRACE_EVENT(ext4_journal_fc_commit_cb_stop,
+ TP_PROTO(struct super_block *sb, int nblks, const char *reason),
+
+ TP_ARGS(sb, nblks, reason),
+
+ TP_STRUCT__entry(
+ __field(dev_t, dev)
+ __field(int, nblks)
+ __field(const char *, reason)
+ __field(int, num_fc)
+ __field(int, num_fc_ineligible)
+ __field(int, nblks_agg)
+ ),
+
+ TP_fast_assign(
+ __entry->dev = sb->s_dev;
+ __entry->nblks = nblks;
+ __entry->reason = reason;
+ __entry->num_fc = EXT4_SB(sb)->s_fc_stats.fc_num_commits;
+ __entry->num_fc_ineligible =
+ EXT4_SB(sb)->s_fc_stats.fc_ineligible_commits;
+ __entry->nblks_agg = EXT4_SB(sb)->s_fc_stats.fc_numblks;
+ ),
+
+ TP_printk("fc on [%d,%d] nblks %d, reason %s, fc = %d, ineligible = %d, agg_nblks %d",
+ MAJOR(__entry->dev), MINOR(__entry->dev),
+ __entry->nblks, __entry->reason, __entry->num_fc,
+ __entry->num_fc_ineligible, __entry->nblks_agg)
+);
+
+#define FC_REASON_NAME_STAT(reason) \
+ show_fc_reason(reason), \
+ __entry->sbi->s_fc_stats.fc_ineligible_reason_count[reason]
+
+TRACE_EVENT(ext4_journal_fc_stats,
+ TP_PROTO(struct super_block *sb),
+
+ TP_ARGS(sb),
+
+ TP_STRUCT__entry(
+ __field(dev_t, dev)
+ __field(struct ext4_sb_info *, sbi)
+ __field(int, count)
+ ),
+
+ TP_fast_assign(
+ __entry->dev = sb->s_dev;
+ __entry->sbi = EXT4_SB(sb);
+ ),
+
+ TP_printk("dev %d:%d fc ineligible reasons:\n"
+ "%s:%d, %s:%d, %s:%d, %s:%d, %s:%d, %s:%d, "
+ "%s:%d, %s:%d, %s:%d, %s:%d; "
+ "num_commits:%d, ineligible: %d, numblks: %d",
+ MAJOR(__entry->dev), MINOR(__entry->dev),
+ FC_REASON_NAME_STAT(EXT4_FC_REASON_META_ALLOC),
+ FC_REASON_NAME_STAT(EXT4_FC_REASON_QUOTA),
+ FC_REASON_NAME_STAT(EXT4_FC_REASON_XATTR),
+ FC_REASON_NAME_STAT(EXT4_FC_REASON_CROSS_RENAME),
+ FC_REASON_NAME_STAT(EXT4_FC_REASON_FALLOC_RANGE_OP),
+ FC_REASON_NAME_STAT(EXT4_FC_REASON_JOURNAL_FLAG_CHANGE),
+ FC_REASON_NAME_STAT(EXT4_FC_REASON_MEM),
+ FC_REASON_NAME_STAT(EXT4_FC_REASON_SWAP_BOOT),
+ FC_REASON_NAME_STAT(EXT4_FC_REASON_RESIZE),
+ FC_REASON_NAME_STAT(EXT4_FC_REASON_RENAME_DIR),
+ __entry->sbi->s_fc_stats.fc_num_commits,
+ __entry->sbi->s_fc_stats.fc_ineligible_commits,
+ __entry->sbi->s_fc_stats.fc_numblks)
+
+);
+
#define DEFINE_TRACE_DENTRY_EVENT(__type) \
TRACE_EVENT(ext4_fc_track_##__type, \
TP_PROTO(struct inode *inode, struct dentry *dentry, int ret), \
--
2.25.1.481.gfbce0eb801-goog

2020-03-09 07:06:58

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 17/20] ext4: add idempotent helpers to manipulate bitmaps

For fast commit replay path, we need idempotent helpers that mark
inodes used, data blocks as used or free. It's important these are
idempotent and that's because we can crash while we are replaying.

Signed-off-by: Harshad Shirwadkar <[email protected]>
Reported-by: kbuild test robot <[email protected]>
---
fs/ext4/ext4.h | 1 +
fs/ext4/ialloc.c | 113 ++++++++++++++++++++++++++++++++++++++
fs/ext4/mballoc.c | 136 +++++++++++++++++++++++++++++++++++++++++++++-
fs/ext4/mballoc.h | 2 +
4 files changed, 251 insertions(+), 1 deletion(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 6dacbb95cc52..ca1e7f100bc3 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -2691,6 +2691,7 @@ extern int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
struct dx_hash_info *hinfo);

/* ialloc.c */
+extern int ext4_mark_inode_used(struct super_block *sb, int ino);
extern struct inode *__ext4_new_inode(handle_t *, struct inode *, umode_t,
const struct qstr *qstr, __u32 goal,
uid_t *owner, __u32 i_flags,
diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index f95ee99091e4..f3c5b86c6a06 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -730,6 +730,119 @@ static int find_inode_bit(struct super_block *sb, ext4_group_t group,
return 1;
}

+int ext4_mark_inode_used(struct super_block *sb, int ino)
+{
+ unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
+ struct buffer_head *inode_bitmap_bh = NULL, *group_desc_bh = NULL;
+ struct ext4_group_desc *gdp;
+ ext4_group_t group;
+ int bit;
+ int err = -EFSCORRUPTED;
+
+ if (ino < EXT4_FIRST_INO(sb) || ino > max_ino)
+ goto out;
+
+ group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
+ bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
+ inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
+ if (IS_ERR(inode_bitmap_bh))
+ return PTR_ERR(inode_bitmap_bh);
+
+ if (ext4_test_bit(bit, inode_bitmap_bh->b_data)) {
+ err = -EEXIST;
+ goto out;
+ }
+
+ gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
+ if (!gdp || !group_desc_bh) {
+ err = -EINVAL;
+ goto out;
+ }
+
+ ext4_set_bit(bit, inode_bitmap_bh->b_data);
+
+ BUFFER_TRACE(inode_bitmap_bh, "call ext4_handle_dirty_metadata");
+ err = ext4_handle_dirty_metadata(NULL, NULL, inode_bitmap_bh);
+ if (err) {
+ ext4_std_error(sb, err);
+ goto out;
+ }
+ sync_dirty_buffer(inode_bitmap_bh);
+ BUFFER_TRACE(group_desc_bh, "get_write_access");
+
+ /* We may have to initialize the block bitmap if it isn't already */
+ if (ext4_has_group_desc_csum(sb) &&
+ gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
+ struct buffer_head *block_bitmap_bh;
+
+ block_bitmap_bh = ext4_read_block_bitmap(sb, group);
+ if (IS_ERR(block_bitmap_bh)) {
+ err = PTR_ERR(block_bitmap_bh);
+ goto out;
+ }
+
+ BUFFER_TRACE(block_bitmap_bh, "dirty block bitmap");
+ err = ext4_handle_dirty_metadata(NULL, NULL, block_bitmap_bh);
+ sync_dirty_buffer(block_bitmap_bh);
+
+ /* recheck and clear flag under lock if we still need to */
+ ext4_lock_group(sb, group);
+ if (ext4_has_group_desc_csum(sb) &&
+ (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
+ gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
+ ext4_free_group_clusters_set(sb, gdp,
+ ext4_free_clusters_after_init(sb, group, gdp));
+ ext4_block_bitmap_csum_set(sb, group, gdp,
+ block_bitmap_bh);
+ ext4_group_desc_csum_set(sb, group, gdp);
+ }
+ ext4_unlock_group(sb, group);
+ brelse(block_bitmap_bh);
+
+ if (err) {
+ ext4_std_error(sb, err);
+ goto out;
+ }
+ }
+
+ /* Update the relevant bg descriptor fields */
+ if (ext4_has_group_desc_csum(sb)) {
+ int free;
+
+ ext4_lock_group(sb, group); /* while we modify the bg desc */
+ free = EXT4_INODES_PER_GROUP(sb) -
+ ext4_itable_unused_count(sb, gdp);
+ if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
+ gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
+ free = 0;
+ }
+
+ /*
+ * Check the relative inode number against the last used
+ * relative inode number in this group. if it is greater
+ * we need to update the bg_itable_unused count
+ */
+ if (bit >= free)
+ ext4_itable_unused_set(sb, gdp,
+ (EXT4_INODES_PER_GROUP(sb) - bit - 1));
+ } else {
+ ext4_lock_group(sb, group);
+ }
+
+ ext4_free_inodes_set(sb, gdp, ext4_free_inodes_count(sb, gdp) - 1);
+ if (ext4_has_group_desc_csum(sb)) {
+ ext4_inode_bitmap_csum_set(sb, group, gdp, inode_bitmap_bh,
+ EXT4_INODES_PER_GROUP(sb) / 8);
+ ext4_group_desc_csum_set(sb, group, gdp);
+ }
+
+ ext4_unlock_group(sb, group);
+ err = ext4_handle_dirty_metadata(NULL, NULL, group_desc_bh);
+ sync_dirty_buffer(group_desc_bh);
+out:
+ return err;
+}
+
/*
* There are two policies for allocating an inode. If the new inode is
* a directory, then a forward search is made for a block group with both
diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 51a78eb65f3c..96be991718f1 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -3052,6 +3052,93 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
return err;
}

+void ext4_mb_mark_used(struct super_block *sb, ext4_fsblk_t block,
+ int len)
+{
+ struct buffer_head *bitmap_bh = NULL;
+ struct ext4_group_desc *gdp;
+ struct buffer_head *gdp_bh;
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+ ext4_group_t group;
+ ext4_fsblk_t cluster;
+ ext4_grpblk_t blkoff;
+ int i, clen, err;
+ int already_allocated_count;
+
+ cluster = EXT4_B2C(sbi, block);
+ clen = EXT4_B2C(sbi, len);
+
+ ext4_get_group_no_and_offset(sb, block, &group, &blkoff);
+ bitmap_bh = ext4_read_block_bitmap(sb, group);
+ if (IS_ERR(bitmap_bh)) {
+ err = PTR_ERR(bitmap_bh);
+ bitmap_bh = NULL;
+ goto out_err;
+ }
+
+ err = -EIO;
+ gdp = ext4_get_group_desc(sb, group, &gdp_bh);
+ if (!gdp)
+ goto out_err;
+
+ if (!ext4_data_block_valid(sbi, block, len)) {
+ ext4_error(sb, "Allocating blks %llu-%llu which overlap mdata",
+ cluster, cluster+clen);
+ /* File system mounted not to panic on error
+ * Fix the bitmap and return EFSCORRUPTED
+ * We leak some of the blocks here.
+ */
+ ext4_lock_group(sb, group);
+ ext4_set_bits(bitmap_bh->b_data, blkoff, clen);
+ ext4_unlock_group(sb, group);
+ err = ext4_handle_dirty_metadata(NULL, NULL, bitmap_bh);
+ if (!err)
+ err = -EFSCORRUPTED;
+ sync_dirty_buffer(bitmap_bh);
+ goto out_err;
+ }
+
+ ext4_lock_group(sb, group);
+ already_allocated_count = 0;
+ for (i = 0; i < clen; i++)
+ if (mb_test_bit(blkoff + i, bitmap_bh->b_data))
+ already_allocated_count++;
+
+ ext4_set_bits(bitmap_bh->b_data, blkoff, clen);
+ if (ext4_has_group_desc_csum(sb) &&
+ (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
+ gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
+ ext4_free_group_clusters_set(sb, gdp,
+ ext4_free_clusters_after_init(sb,
+ group, gdp));
+ }
+ clen = ext4_free_group_clusters(sb, gdp) - clen +
+ already_allocated_count;
+ ext4_free_group_clusters_set(sb, gdp, clen);
+ ext4_block_bitmap_csum_set(sb, group, gdp, bitmap_bh);
+ ext4_group_desc_csum_set(sb, group, gdp);
+
+ ext4_unlock_group(sb, group);
+
+ if (sbi->s_log_groups_per_flex) {
+ ext4_group_t flex_group = ext4_flex_group(sbi, group);
+
+ atomic64_sub(len,
+ &sbi_array_rcu_deref(sbi, s_flex_groups,
+ flex_group)->free_clusters);
+ }
+
+ err = ext4_handle_dirty_metadata(NULL, NULL, bitmap_bh);
+ if (err)
+ goto out_err;
+ sync_dirty_buffer(bitmap_bh);
+ err = ext4_handle_dirty_metadata(NULL, NULL, gdp_bh);
+ sync_dirty_buffer(gdp_bh);
+
+out_err:
+ brelse(bitmap_bh);
+}
+
/*
* here we normalize request for locality group
* Group request are normalized to s_mb_group_prealloc, which goes to
@@ -4715,6 +4802,47 @@ ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
return 0;
}

+void ext4_free_blocks_simple(struct inode *inode, ext4_fsblk_t block,
+ unsigned long count)
+{
+ struct buffer_head *bitmap_bh;
+ struct super_block *sb = inode->i_sb;
+ struct ext4_group_desc *gdp;
+ struct buffer_head *gdp_bh;
+ ext4_group_t group;
+ ext4_grpblk_t blkoff;
+ int already_freed = 0, err, i;
+
+ ext4_get_group_no_and_offset(sb, block, &group, &blkoff);
+ bitmap_bh = ext4_read_block_bitmap(sb, group);
+ if (IS_ERR(bitmap_bh)) {
+ err = PTR_ERR(bitmap_bh);
+ pr_warn("Failed to read block bitmap\n");
+ return;
+ }
+ gdp = ext4_get_group_desc(sb, group, &gdp_bh);
+ if (!gdp)
+ return;
+
+ for (i = 0; i < count; i++) {
+ if (!mb_test_bit(blkoff + i, bitmap_bh->b_data))
+ already_freed++;
+ }
+ mb_clear_bits(bitmap_bh->b_data, blkoff, count);
+ err = ext4_handle_dirty_metadata(NULL, NULL, bitmap_bh);
+ if (err)
+ return;
+ ext4_free_group_clusters_set(
+ sb, gdp, ext4_free_group_clusters(sb, gdp) +
+ count - already_freed);
+ ext4_block_bitmap_csum_set(sb, group, gdp, bitmap_bh);
+ ext4_group_desc_csum_set(sb, group, gdp);
+ ext4_handle_dirty_metadata(NULL, NULL, gdp_bh);
+ sync_dirty_buffer(bitmap_bh);
+ sync_dirty_buffer(gdp_bh);
+ brelse(bitmap_bh);
+}
+
/**
* ext4_free_blocks() -- Free given blocks and update quota
* @handle: handle for this transaction
@@ -4741,6 +4869,13 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode,
int err = 0;
int ret;

+ sbi = EXT4_SB(sb);
+
+ if (sbi->s_mount_state & EXT4_FC_REPLAY) {
+ ext4_free_blocks_simple(inode, block, count);
+ return;
+ }
+
might_sleep();
if (bh) {
if (block)
@@ -4749,7 +4884,6 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode,
block = bh->b_blocknr;
}

- sbi = EXT4_SB(sb);
if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
!ext4_data_block_valid(sbi, block, count)) {
ext4_error(sb, "Freeing blocks not in datazone - "
diff --git a/fs/ext4/mballoc.h b/fs/ext4/mballoc.h
index 88c98f17e3d9..1881710041b6 100644
--- a/fs/ext4/mballoc.h
+++ b/fs/ext4/mballoc.h
@@ -215,4 +215,6 @@ ext4_mballoc_query_range(
ext4_mballoc_query_range_fn formatter,
void *priv);

+void ext4_mb_mark_used(struct super_block *sb, ext4_fsblk_t block,
+ int len);
#endif
--
2.25.1.481.gfbce0eb801-goog

2020-03-09 07:07:04

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 19/20] ext4: add fast commit replay path

Add main routine for replaying fast commit blocks. Fast commit replay
routine should be idempotent; so that if we crash while replaying,
we can restart from the beginning and won't result in a corrupted
file system.

Signed-off-by: Harshad Shirwadkar <[email protected]>
---
fs/ext4/ext4.h | 9 +
fs/ext4/ext4_jbd2.c | 470 ++++++++++++++++++++++++++++++++++++
include/trace/events/ext4.h | 22 ++
3 files changed, 501 insertions(+)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index ca1e7f100bc3..cc92a333bc68 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1440,6 +1440,14 @@ struct ext4_super_block {
#define ext4_has_strict_mode(sbi) \
(sbi->s_encoding_flags & EXT4_ENC_STRICT_MODE_FL)

+/*
+ * Fast commit replay state.
+ */
+struct ext4_fc_replay_state {
+ int fc_replay_error;
+ int fc_replay_expected_off;
+};
+
/*
* Fast commit ineligible reasons.
*/
@@ -1651,6 +1659,7 @@ struct ext4_sb_info {
* that have data changes in them.
*/
struct list_head s_fc_dentry_q;
+ struct ext4_fc_replay_state s_fc_replay_state;
spinlock_t s_fc_lock;
struct ext4_fc_stats s_fc_stats;
};
diff --git a/fs/ext4/ext4_jbd2.c b/fs/ext4/ext4_jbd2.c
index 8b49d508efbd..9e4b6bcbd76c 100644
--- a/fs/ext4/ext4_jbd2.c
+++ b/fs/ext4/ext4_jbd2.c
@@ -5,6 +5,7 @@

#include "ext4_jbd2.h"
#include "ext4_extents.h"
+#include "mballoc.h"

#include <trace/events/ext4.h>

@@ -860,6 +861,18 @@ static int fc_write_data(struct inode *inode, u8 *start, u8 *end,
return num_tlvs;
}

+/* Get length of a particular tlv */
+static int fc_tag_len(struct ext4_fc_tl *tl)
+{
+ return le16_to_cpu(tl->fc_len);
+}
+
+/* Get a pointer to "value" of a tlv */
+static u8 *fc_tag_val(struct ext4_fc_tl *tl)
+{
+ return (u8 *)tl + sizeof(*tl);
+}
+
static int fc_commit_data_inode(journal_t *journal, struct inode *inode)
{
struct ext4_fc_commit_hdr *hdr;
@@ -1182,6 +1195,457 @@ static void ext4_journal_fc_cleanup_cb(journal_t *journal)
trace_ext4_journal_fc_stats(sb);
}

+struct dentry_info_args {
+ int parent_ino, dname_len, ino, inode_len;
+ char *dname;
+};
+
+static int fc_replay_add_link(struct super_block *sb, struct inode *inode,
+ struct dentry_info_args *darg)
+{
+ struct inode *dir = NULL;
+ struct dentry *dentry_dir = NULL, *dentry_inode = NULL;
+ struct qstr qstr_dname = QSTR_INIT(darg->dname, darg->dname_len);
+ int ret = 0;
+
+ dir = ext4_iget(sb, darg->parent_ino, EXT4_IGET_NORMAL);
+ if (IS_ERR(dir)) {
+ jbd_debug(1, "Dir with inode %d not found.", darg->parent_ino);
+ ret = PTR_ERR(dir);
+ dir = NULL;
+ goto out;
+ }
+
+ dentry_dir = d_obtain_alias(dir);
+ if (IS_ERR(dentry_dir)) {
+ jbd_debug(1, "Failed to obtain dentry");
+ ret = PTR_ERR(dentry_dir);
+ dentry_dir = NULL;
+ goto out;
+ }
+
+ dentry_inode = d_alloc(dentry_dir, &qstr_dname);
+ if (!dentry_inode) {
+ jbd_debug(1, "Inode dentry not created.");
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ ret = __ext4_link(dir, inode, dentry_inode);
+ if (ret && ret != -EEXIST) {
+ jbd_debug(1, "Failed to link\n");
+ goto out;
+ }
+
+ /*
+ * It's possible that link already existed since data blocks
+ * for the dir in question got persisted before we crashed.
+ */
+ if (ret == -EEXIST)
+ ret = 0;
+out:
+ if (dentry_dir) {
+ d_drop(dentry_dir);
+ dput(dentry_dir);
+ } else if (dir) {
+ iput(dir);
+ }
+ if (dentry_inode) {
+ d_drop(dentry_inode);
+ dput(dentry_inode);
+ }
+
+ return ret;
+}
+
+static int fc_replay_create_inode(struct super_block *sb,
+ struct ext4_inode *raw_inode,
+ struct dentry_info_args *darg)
+{
+ int ret = 0;
+ struct ext4_iloc iloc;
+ int orig_nlink = 0;
+ struct inode *inode = NULL;
+ struct inode *dir = NULL;
+
+ /*
+ * First let's setup the on-disk inode using the one found in
+ * the journal
+ */
+ ret = ext4_get_fc_inode_loc(sb, darg->ino, &iloc);
+ if (ret)
+ goto out;
+
+ orig_nlink = le16_to_cpu(ext4_raw_inode(&iloc)->i_links_count);
+ memcpy(ext4_raw_inode(&iloc), raw_inode, darg->inode_len);
+ ret = ext4_handle_dirty_metadata(NULL, NULL, iloc.bh);
+ if (ret)
+ goto out;
+ sync_dirty_buffer(iloc.bh);
+ brelse(iloc.bh);
+ iloc.bh = NULL;
+
+ /* This takes care of update group descriptor and other metadata */
+ ret = ext4_mark_inode_used(sb, darg->ino);
+ if (ret)
+ goto out;
+
+ inode = ext4_iget(sb, darg->ino, EXT4_IGET_NORMAL);
+ if (IS_ERR(inode)) {
+ jbd_debug(1, "inode %d not found.", darg->ino);
+ ret = PTR_ERR(inode);
+ goto out;
+ }
+
+ if (S_ISDIR(inode->i_mode)) {
+ dir = ext4_iget(sb, darg->parent_ino, EXT4_IGET_NORMAL);
+ if (IS_ERR_OR_NULL(dir)) {
+ iput(inode);
+ ret = PTR_ERR(dir);
+ goto out;
+ }
+ ret = ext4_init_new_dir(NULL, dir, inode);
+ iput(dir);
+ if (ret)
+ goto out;
+ }
+ ret = fc_replay_add_link(sb, inode, darg);
+ if (ret)
+ goto out;
+ set_nlink(inode, orig_nlink + 1);
+out:
+ if (inode)
+ iput(inode);
+ if (iloc.bh)
+ brelse(iloc.bh);
+ return ret;
+}
+
+static int fc_replay_dentries(journal_t *journal,
+ struct ext4_fc_commit_hdr *fc_hdr)
+{
+ struct dentry_info_args darg = {0};
+ struct super_block *sb = journal->j_private;
+ struct ext4_fc_tl *tl;
+ __u8 *start;
+ int inode_len = EXT4_GOOD_OLD_INODE_SIZE;
+ struct inode *old_parent;
+ struct inode *inode;
+ int ret;
+ int i;
+ struct ext4_fc_dentry_info *fcd;
+
+ if (EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE)
+ inode_len +=
+ le16_to_cpu(((struct ext4_inode *)
+ (fc_hdr + 1))->i_extra_isize);
+ tl = (struct ext4_fc_tl *)((u8 *)fc_hdr +
+ sizeof(struct ext4_fc_commit_hdr) +
+ inode_len);
+ start = (__u8 *)tl;
+ for (i = 0; i < le16_to_cpu(fc_hdr->fc_num_tlvs); i++) {
+ fcd = (struct ext4_fc_dentry_info *)fc_tag_val(tl);
+
+ darg.parent_ino = le32_to_cpu(fcd->fc_parent_ino);
+ darg.ino = le32_to_cpu(fcd->fc_ino);
+ darg.dname = fcd->fc_dname;
+ darg.dname_len = fc_tag_len(tl) -
+ sizeof(struct ext4_fc_dentry_info);
+ if (le16_to_cpu(tl->fc_tag) == EXT4_FC_TAG_ADD_DENTRY) {
+ inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
+ if (IS_ERR_OR_NULL(inode)) {
+ jbd_debug(1, "Inode not found.");
+ return PTR_ERR(inode);
+ }
+ ret = fc_replay_add_link(sb, inode, &darg);
+ iput(inode);
+ if (ret)
+ return ret;
+ } else if (le16_to_cpu(tl->fc_tag) == EXT4_FC_TAG_DEL_DENTRY) {
+ const struct qstr entry = {
+ .name = darg.dname,
+ .len = darg.dname_len
+ };
+ inode = ext4_iget(sb, darg.ino, EXT4_IGET_NORMAL);
+
+ if (IS_ERR_OR_NULL(inode))
+ return -ECANCELED;
+
+ old_parent = ext4_iget(sb, darg.parent_ino,
+ EXT4_IGET_NORMAL);
+ if (IS_ERR_OR_NULL(old_parent)) {
+ iput(inode);
+ return -ECANCELED;
+ }
+
+ ret = __ext4_unlink(old_parent, &entry, inode);
+ /* -ENOENT ok coz it might not exist anymore. */
+ if (ret == -ENOENT)
+ ret = 0;
+ iput(old_parent);
+ iput(inode);
+ if (ret)
+ return ret;
+ } else if (le16_to_cpu(tl->fc_tag) ==
+ EXT4_FC_TAG_CREAT_DENTRY) {
+ darg.inode_len = inode_len;
+ ret = fc_replay_create_inode(
+ sb, (struct ext4_inode *)(fc_hdr + 1), &darg);
+ if (ret) {
+ jbd_debug(1, "Failed to create ext4 inode.");
+ return ret;
+ }
+ }
+ tl = (struct ext4_fc_tl *)((__u8 *)tl +
+ le16_to_cpu(tl->fc_len) +
+ sizeof(*tl));
+ }
+ return 0;
+}
+
+static int ext4_journal_fc_replay_scan(journal_t *journal,
+ struct buffer_head *bh, int off)
+{
+ struct super_block *sb = journal->j_private;
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+ struct ext4_fc_replay_state *state;
+ struct ext4_fc_commit_hdr *fc_hdr;
+ __u32 csum, old_csum;
+ __u8 *start, *end;
+
+ state = &sbi->s_fc_replay_state;
+ fc_hdr = (struct ext4_fc_commit_hdr *)
+ ((__u8 *)bh->b_data + sizeof(journal_header_t));
+
+ start = (u8 *)fc_hdr;
+ end = (__u8 *)bh->b_data + journal->j_blocksize;
+
+ /* Check if we already concluded that this fast commit is not useful */
+ if (state->fc_replay_expected_off && state->fc_replay_error)
+ goto out_err;
+
+ if (le32_to_cpu(fc_hdr->fc_magic) != EXT4_FC_MAGIC) {
+ state->fc_replay_error = -ENOENT;
+ goto out_err;
+ }
+
+ if (off != state->fc_replay_expected_off) {
+ state->fc_replay_error = -EFSCORRUPTED;
+ goto out_err;
+ }
+
+ state->fc_replay_expected_off++;
+
+ if (le16_to_cpu(fc_hdr->fc_features)) {
+ state->fc_replay_error = -EOPNOTSUPP;
+ goto out_err;
+ }
+
+ old_csum = fc_hdr->fc_csum;
+ fc_hdr->fc_csum = 0;
+ csum = ext4_chksum(sbi, 0, start, end - start);
+ fc_hdr->fc_csum = old_csum;
+
+ if (csum != le32_to_cpu(fc_hdr->fc_csum)) {
+ state->fc_replay_error = -EFSBADCRC;
+ goto out_err;
+ }
+
+ trace_ext4_journal_fc_replay_scan(sb, state->fc_replay_error, off);
+ return 0;
+
+out_err:
+ trace_ext4_journal_fc_replay_scan(sb, state->fc_replay_error, off);
+ return state->fc_replay_error;
+}
+
+static int fc_add_range(struct inode *inode, struct ext4_extent *ex)
+{
+ struct ext4_extent newex;
+ ext4_lblk_t start, cur;
+ int remaining, len;
+ ext4_fsblk_t start_pblk;
+ struct ext4_map_blocks map;
+ struct ext4_ext_path *path = NULL;
+ int ret;
+
+ start = le32_to_cpu(ex->ee_block);
+ start_pblk = ext4_ext_pblock(ex);
+ len = ext4_ext_get_actual_len(ex);
+
+ cur = start;
+ remaining = len;
+
+ jbd_debug(1, "Adding extent %ld:%ld to inode %ld\n",
+ start, len, inode->i_ino);
+
+ while (remaining > 0) {
+ map.m_lblk = cur;
+ map.m_len = remaining;
+
+ ret = ext4_map_blocks(NULL, inode, &map, 0);
+ if (ret < 0)
+ return -ECANCELED;
+ if (ret > 0) {
+ if (!!(ext4_ext_is_unwritten(ex)) ==
+ !!(map.m_flags & EXT4_MAP_UNWRITTEN)) {
+ remaining -= ret;
+ cur += ret;
+ ext4_mb_mark_used(inode->i_sb,
+ ext4_ext_pblock(ex),
+ map.m_len);
+ continue;
+ }
+
+ /* handle change of state */
+ map.m_lblk = cur;
+ map.m_len = ret;
+ map.m_flags = 0;
+ ret = ext4_map_blocks(
+ NULL, inode, &map,
+ EXT4_GET_BLOCKS_IO_CONVERT_EXT);
+ if (ret <= 0)
+ return -ECANCELED;
+ remaining -= ret;
+ cur += ret;
+ } else if (ret == 0) {
+ path = ext4_find_extent(inode, cur, NULL, 0);
+ if (!path)
+ continue;
+ memset(&newex, 0, sizeof(newex));
+ newex.ee_block = cpu_to_le32(cur);
+ ext4_ext_store_pblock(
+ &newex, start_pblk + cur - start);
+ newex.ee_len = cpu_to_le16(map.m_len);
+ if (ext4_ext_is_unwritten(ex))
+ ext4_ext_mark_unwritten(&newex);
+ down_write(&EXT4_I(inode)->i_data_sem);
+
+ ret = ext4_ext_insert_extent(
+ NULL, inode, &path, &newex, 0);
+ ext4_mb_mark_used(
+ inode->i_sb, ext4_ext_pblock(&newex),
+ map.m_len);
+ up_write((&EXT4_I(inode)->i_data_sem));
+ kfree(path);
+ if (ret)
+ return -ECANCELED;
+ cur += map.m_len;
+ remaining -= map.m_len;
+ }
+ }
+ return 0;
+}
+
+static int ext4_journal_fc_replay_cb(journal_t *journal, struct buffer_head *bh,
+ enum passtype pass, int off)
+{
+ struct super_block *sb = journal->j_private;
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
+ struct ext4_fc_commit_hdr *fc_hdr;
+ struct ext4_fc_tl *tl;
+ struct ext4_iloc iloc;
+ struct ext4_extent *ex;
+ struct ext4_fc_lrange *lrange;
+ struct inode *inode;
+
+ int i, ret;
+ int inode_len = EXT4_GOOD_OLD_INODE_SIZE;
+
+ if (pass == PASS_SCAN)
+ return ext4_journal_fc_replay_scan(journal, bh, off);
+
+ if (sbi->s_fc_replay_state.fc_replay_error) {
+ jbd_debug(1, "FC replay error set = %d\n",
+ sbi->s_fc_replay_state.fc_replay_error);
+ return sbi->s_fc_replay_state.fc_replay_error;
+ }
+
+ sbi->s_mount_state |= EXT4_FC_REPLAY;
+ fc_hdr = (struct ext4_fc_commit_hdr *)
+ ((__u8 *)bh->b_data + sizeof(journal_header_t));
+
+ jbd_debug(3, "%s: Got FC block for inode %d at [%d,%d]", __func__,
+ le32_to_cpu(fc_hdr->fc_ino),
+ be32_to_cpu(((journal_header_t *)bh->b_data)->h_sequence));
+
+ if (EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE)
+ inode_len += le16_to_cpu(((struct ext4_inode *)
+ (fc_hdr + 1))->i_extra_isize);
+
+ ret = fc_replay_dentries(journal, fc_hdr);
+
+ inode = ext4_iget(sb, le32_to_cpu(fc_hdr->fc_ino), EXT4_IGET_NORMAL);
+ if (IS_ERR(inode))
+ return 0;
+
+ ret = ext4_get_inode_loc(inode, &iloc);
+ if (ret)
+ return ret;
+
+ inode_lock(inode);
+ tl = (struct ext4_fc_tl *)((u8 *)fc_hdr +
+ sizeof(struct ext4_fc_commit_hdr) +
+ inode_len);
+ for (i = 0; i < le16_to_cpu(fc_hdr->fc_num_tlvs); i++) {
+ switch (le16_to_cpu(tl->fc_tag)) {
+ case EXT4_FC_TAG_ADD_RANGE:
+ ex = (struct ext4_extent *)(tl + 1);
+ ret = fc_add_range(inode, ex);
+ break;
+ case EXT4_FC_TAG_DEL_RANGE:
+ lrange = (struct ext4_fc_lrange *)(tl + 1);
+ inode_unlock(inode);
+ ret = ext4_punch_hole(inode,
+ le32_to_cpu(lrange->fc_lblk) <<
+ sb->s_blocksize_bits,
+ le32_to_cpu(lrange->fc_len) <<
+ sb->s_blocksize_bits);
+ inode_lock(inode);
+ break;
+ case EXT4_FC_TAG_ADD_DENTRY:
+ break;
+ default:
+ jbd_debug(1, "Unknown tag found.\n");
+ }
+ tl = (struct ext4_fc_tl *)((__u8 *)tl +
+ le16_to_cpu(tl->fc_len) +
+ sizeof(*tl));
+ }
+ ext4_reserve_inode_write(NULL, inode, &iloc);
+ inode_unlock(inode);
+
+ /*
+ * Unless inode contains inline data, copy everything except
+ * i_blocks. i_blocks would have been set alright by ext4_fc_add_block
+ * call above.
+ */
+ if (ext4_has_inline_data(inode)) {
+ memcpy(ext4_raw_inode(&iloc), fc_hdr + 1, inode_len);
+ } else {
+ memcpy(ext4_raw_inode(&iloc), fc_hdr + 1,
+ offsetof(struct ext4_inode, i_block));
+ memcpy(&ext4_raw_inode(&iloc)->i_generation,
+ &((struct ext4_inode *)(fc_hdr + 1))->i_generation,
+ inode_len -
+ offsetof(struct ext4_inode, i_generation));
+ }
+ inode->i_generation = le32_to_cpu(ext4_raw_inode(&iloc)->i_generation);
+ ext4_reset_inode_seed(inode);
+
+ ext4_inode_csum_set(inode, ext4_raw_inode(&iloc), EXT4_I(inode));
+ ret = ext4_handle_dirty_metadata(NULL, NULL, iloc.bh);
+ sync_dirty_buffer(iloc.bh);
+ brelse(iloc.bh);
+ iput(inode);
+ if (!ret)
+ ret = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
+
+ sbi->s_mount_state &= ~EXT4_FC_REPLAY;
+
+ return ret;
+}
+
int ext4_fc_perform_hard_commit(journal_t *journal)
{
struct super_block *sb = (struct super_block *)(journal->j_private);
@@ -1361,6 +1825,12 @@ int ext4_fc_async_commit_inode(journal_t *journal, tid_t commit_tid,

void ext4_init_fast_commit(struct super_block *sb, journal_t *journal)
{
+ /*
+ * We set replay callback even if fast commit disabled because we may
+ * could still have fast commit blocks that need to be replayed even if
+ * fast commit has now been turned off.
+ */
+ journal->j_fc_replay_callback = ext4_journal_fc_replay_cb;
if (!ext4_should_fast_commit(sb))
return;
journal->j_fc_cleanup_callback = ext4_journal_fc_cleanup_cb;
diff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h
index c1c2f193a604..a7a30a55d514 100644
--- a/include/trace/events/ext4.h
+++ b/include/trace/events/ext4.h
@@ -2736,6 +2736,28 @@ TRACE_EVENT(ext4_error,
__entry->function, __entry->line)
);

+TRACE_EVENT(ext4_journal_fc_replay_scan,
+ TP_PROTO(struct super_block *sb, int error, int off),
+
+ TP_ARGS(sb, error, off),
+
+ TP_STRUCT__entry(
+ __field(dev_t, dev)
+ __field(int, error)
+ __field(int, off)
+ ),
+
+ TP_fast_assign(
+ __entry->dev = sb->s_dev;
+ __entry->error = error;
+ __entry->off = off;
+ ),
+
+ TP_printk("FC scan pass on dev %d,%d: error %d, off %d",
+ MAJOR(__entry->dev), MINOR(__entry->dev),
+ __entry->error, __entry->off)
+);
+
TRACE_EVENT(ext4_journal_fc_commit_cb_start,
TP_PROTO(struct super_block *sb),

--
2.25.1.481.gfbce0eb801-goog

2020-03-09 07:07:53

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 16/20] ext4: fast commit recovery path preparation

Prepare for making ext4 fast commit recovery path changes. Make a few
existing functions visible. Break and add a wrapper around
ext4_get_inode_loc to allow reading inode from disk without having
a corresponding VFS inode.

Signed-off-by: Harshad Shirwadkar <[email protected]>
---
fs/ext4/ext4.h | 7 +++++++
fs/ext4/inode.c | 34 +++++++++++++++++++---------------
fs/ext4/ioctl.c | 6 +++---
fs/ext4/namei.c | 2 +-
include/trace/events/ext4.h | 8 ++++----
5 files changed, 34 insertions(+), 23 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 401d28b57d81..6dacbb95cc52 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -2741,6 +2741,8 @@ extern int ext4_trim_fs(struct super_block *, struct fstrim_range *);
extern void ext4_process_freed_data(struct super_block *sb, tid_t commit_tid);

/* inode.c */
+void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
+ struct ext4_inode_info *ei);
int ext4_inode_is_fast_symlink(struct inode *inode);
struct buffer_head *ext4_getblk(handle_t *, struct inode *, ext4_lblk_t, int);
struct buffer_head *ext4_bread(handle_t *, struct inode *, ext4_lblk_t, int);
@@ -2787,6 +2789,8 @@ extern int ext4_sync_inode(handle_t *, struct inode *);
extern void ext4_dirty_inode(struct inode *, int);
extern int ext4_change_inode_journal_flag(struct inode *, int);
extern int ext4_get_inode_loc(struct inode *, struct ext4_iloc *);
+extern int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino,
+ struct ext4_iloc *iloc);
extern int ext4_inode_attach_jinode(struct inode *inode);
extern int ext4_can_truncate(struct inode *inode);
extern int ext4_truncate(struct inode *);
@@ -2820,12 +2824,15 @@ extern int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
/* ioctl.c */
extern long ext4_ioctl(struct file *, unsigned int, unsigned long);
extern long ext4_compat_ioctl(struct file *, unsigned int, unsigned long);
+extern void ext4_reset_inode_seed(struct inode *inode);

/* migrate.c */
extern int ext4_ext_migrate(struct inode *);
extern int ext4_ind_migrate(struct inode *inode);

/* namei.c */
+extern int ext4_init_new_dir(handle_t *handle, struct inode *dir,
+ struct inode *inode);
extern int ext4_dirblock_csum_verify(struct inode *inode,
struct buffer_head *bh);
extern int ext4_orphan_add(handle_t *, struct inode *);
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index b209f81a01b6..66e56ac6d028 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -101,8 +101,8 @@ static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
return provided == calculated;
}

-static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
- struct ext4_inode_info *ei)
+void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
+ struct ext4_inode_info *ei)
{
__u32 csum;

@@ -4235,22 +4235,21 @@ int ext4_truncate(struct inode *inode)
* data in memory that is needed to recreate the on-disk version of this
* inode.
*/
-static int __ext4_get_inode_loc(struct inode *inode,
+static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino,
struct ext4_iloc *iloc, int in_mem)
{
struct ext4_group_desc *gdp;
struct buffer_head *bh;
- struct super_block *sb = inode->i_sb;
ext4_fsblk_t block;
struct blk_plug plug;
int inodes_per_block, inode_offset;

iloc->bh = NULL;
- if (inode->i_ino < EXT4_ROOT_INO ||
- inode->i_ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
+ if (ino < EXT4_ROOT_INO ||
+ ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
return -EFSCORRUPTED;

- iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
+ iloc->block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
if (!gdp)
return -EIO;
@@ -4259,7 +4258,7 @@ static int __ext4_get_inode_loc(struct inode *inode,
* Figure out the offset within the block group inode table
*/
inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
- inode_offset = ((inode->i_ino - 1) %
+ inode_offset = ((ino - 1) %
EXT4_INODES_PER_GROUP(sb));
block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
@@ -4360,7 +4359,7 @@ static int __ext4_get_inode_loc(struct inode *inode,
* has in-inode xattrs, or we don't have this inode in memory.
* Read the block from disk.
*/
- trace_ext4_load_inode(inode);
+ trace_ext4_load_inode(sb, ino);
get_bh(bh);
bh->b_end_io = end_buffer_read_sync;
submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
@@ -4368,9 +4367,8 @@ static int __ext4_get_inode_loc(struct inode *inode,
wait_on_buffer(bh);
if (!buffer_uptodate(bh)) {
simulate_eio:
- ext4_set_errno(inode->i_sb, EIO);
- EXT4_ERROR_INODE_BLOCK(inode, block,
- "unable to read itable block");
+ ext4_set_errno(sb, EIO);
+ ext4_error(sb, "unable to read itable block");
brelse(bh);
return -EIO;
}
@@ -4383,10 +4381,16 @@ static int __ext4_get_inode_loc(struct inode *inode,
int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
{
/* We have all inode data except xattrs in memory here. */
- return __ext4_get_inode_loc(inode, iloc,
+ return __ext4_get_inode_loc(inode->i_sb, inode->i_ino, iloc,
!ext4_test_inode_state(inode, EXT4_STATE_XATTR));
}

+int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino,
+ struct ext4_iloc *iloc)
+{
+ return __ext4_get_inode_loc(sb, ino, iloc, 0);
+}
+
static bool ext4_should_use_dax(struct inode *inode)
{
if (!test_opt(inode->i_sb, DAX))
@@ -4536,7 +4540,7 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
ei = EXT4_I(inode);
iloc.bh = NULL;

- ret = __ext4_get_inode_loc(inode, &iloc, 0);
+ ret = __ext4_get_inode_loc(sb, inode->i_ino, &iloc, 0);
if (ret < 0)
goto bad_inode;
raw_inode = ext4_raw_inode(&iloc);
@@ -5128,7 +5132,7 @@ int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
} else {
struct ext4_iloc iloc;

- err = __ext4_get_inode_loc(inode, &iloc, 0);
+ err = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, &iloc, 0);
if (err)
return err;
/*
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index 3ea66e929afe..e0f274fc5874 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -86,7 +86,7 @@ static void swap_inode_data(struct inode *inode1, struct inode *inode2)
i_size_write(inode2, isize);
}

-static void reset_inode_seed(struct inode *inode)
+void ext4_reset_inode_seed(struct inode *inode)
{
struct ext4_inode_info *ei = EXT4_I(inode);
struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
@@ -199,8 +199,8 @@ static long swap_inode_boot_loader(struct super_block *sb,

inode->i_generation = prandom_u32();
inode_bl->i_generation = prandom_u32();
- reset_inode_seed(inode);
- reset_inode_seed(inode_bl);
+ ext4_reset_inode_seed(inode);
+ ext4_reset_inode_seed(inode_bl);

ext4_discard_preallocations(inode);

diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index ae0e112c65d5..5b21fedd2348 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -2742,7 +2742,7 @@ struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
return ext4_next_entry(de, blocksize);
}

-static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
+int ext4_init_new_dir(handle_t *handle, struct inode *dir,
struct inode *inode)
{
struct buffer_head *dir_block = NULL;
diff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h
index c8a05453f166..c1c2f193a604 100644
--- a/include/trace/events/ext4.h
+++ b/include/trace/events/ext4.h
@@ -1755,9 +1755,9 @@ TRACE_EVENT(ext4_ext_load_extent,
);

TRACE_EVENT(ext4_load_inode,
- TP_PROTO(struct inode *inode),
+ TP_PROTO(struct super_block *sb, unsigned long ino),

- TP_ARGS(inode),
+ TP_ARGS(sb, ino),

TP_STRUCT__entry(
__field( dev_t, dev )
@@ -1765,8 +1765,8 @@ TRACE_EVENT(ext4_load_inode,
),

TP_fast_assign(
- __entry->dev = inode->i_sb->s_dev;
- __entry->ino = inode->i_ino;
+ __entry->dev = sb->s_dev;
+ __entry->ino = ino;
),

TP_printk("dev %d,%d ino %ld",
--
2.25.1.481.gfbce0eb801-goog

2020-03-09 07:07:53

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 20/20] ext4: add debug mount option to test fast commit replay

Add a debug mount option to simulate errors while replaying. If
fc_debug_max_replay is set, ext4 will replay only as many fast
commit blocks as passed as an argument.

Signed-off-by: Harshad Shirwadkar <[email protected]>
---
fs/ext4/ext4.h | 3 +++
fs/ext4/ext4_jbd2.c | 6 ++++++
fs/ext4/super.c | 12 +++++++++++-
3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index cc92a333bc68..5fea64ca8b5b 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1661,6 +1661,9 @@ struct ext4_sb_info {
struct list_head s_fc_dentry_q;
struct ext4_fc_replay_state s_fc_replay_state;
spinlock_t s_fc_lock;
+#ifdef EXT4_FC_DEBUG
+ int s_fc_debug_max_replay;
+#endif
struct ext4_fc_stats s_fc_stats;
};

diff --git a/fs/ext4/ext4_jbd2.c b/fs/ext4/ext4_jbd2.c
index 9e4b6bcbd76c..602eb3392a5d 100644
--- a/fs/ext4/ext4_jbd2.c
+++ b/fs/ext4/ext4_jbd2.c
@@ -1561,6 +1561,12 @@ static int ext4_journal_fc_replay_cb(journal_t *journal, struct buffer_head *bh,
return sbi->s_fc_replay_state.fc_replay_error;
}

+#ifdef EXT4_FC_DEBUG
+ if (sbi->s_fc_debug_max_replay && off >= sbi->s_fc_debug_max_replay) {
+ pr_warn("Dropping fc block %d because max_replay set\n", off);
+ return -EINVAL;
+ }
+#endif
sbi->s_mount_state |= EXT4_FC_REPLAY;
fc_hdr = (struct ext4_fc_commit_hdr *)
((__u8 *)bh->b_data + sizeof(journal_header_t));
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index ffa91815dacf..a7bc3a4e75d0 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -1542,7 +1542,7 @@ enum {
Opt_dioread_nolock, Opt_dioread_lock,
Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
- Opt_no_fc, Opt_fc_soft_consistency
+ Opt_no_fc, Opt_fc_soft_consistency, Opt_fc_debug_max_replay
};

static const match_table_t tokens = {
@@ -1628,6 +1628,9 @@ static const match_table_t tokens = {
{Opt_noinit_itable, "noinit_itable"},
{Opt_no_fc, "no_fc"},
{Opt_fc_soft_consistency, "fc_soft_consistency"},
+#ifdef EXT4_FC_DEBUG
+ {Opt_fc_debug_max_replay, "fc_debug_max_replay=%u"},
+#endif
{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
{Opt_test_dummy_encryption, "test_dummy_encryption"},
{Opt_nombcache, "nombcache"},
@@ -1847,6 +1850,9 @@ static const struct mount_opts {
MOPT_CLEAR | MOPT_2 | MOPT_EXT4_ONLY},
{Opt_fc_soft_consistency, EXT4_MOUNT2_JOURNAL_FC_SOFT_CONSISTENCY,
MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
+#ifdef EXT4_FC_DEBUG
+ {Opt_fc_debug_max_replay, 0, MOPT_GTE0},
+#endif
{Opt_err, 0, 0}
};

@@ -2006,6 +2012,10 @@ static int handle_mount_opt(struct super_block *sb, char *opt, int token,
sbi->s_li_wait_mult = arg;
} else if (token == Opt_max_dir_size_kb) {
sbi->s_max_dir_size_kb = arg;
+#ifdef EXT4_FC_DEBUG
+ } else if (token == Opt_fc_debug_max_replay) {
+ sbi->s_fc_debug_max_replay = arg;
+#endif
} else if (token == Opt_stripe) {
sbi->s_stripe = arg;
} else if (token == Opt_resuid) {
--
2.25.1.481.gfbce0eb801-goog

2020-03-09 07:07:53

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 18/20] ext4: disable certain features in replay path

Replay path uses similar code paths for replaying committed changes.
But since it runs before full initialization of the file system and
also since we don't have to be super careful about performance, we
can and need to disable certain file system features during the replay
path. More specifically, we disable most of the extent status tree
stuff, mballoc and some places where we mark file system with errors.

Signed-off-by: Harshad Shirwadkar <[email protected]>
---
fs/ext4/balloc.c | 7 +++++-
fs/ext4/ext4_jbd2.c | 2 +-
fs/ext4/extents_status.c | 24 +++++++++++++++++++
fs/ext4/ialloc.c | 52 +++++++++++++++++++++++++++-------------
fs/ext4/inode.c | 12 ++++++----
fs/ext4/mballoc.c | 22 +++++++++++------
6 files changed, 90 insertions(+), 29 deletions(-)

diff --git a/fs/ext4/balloc.c b/fs/ext4/balloc.c
index c3c4e7fc5c6b..8dbc993c1c90 100644
--- a/fs/ext4/balloc.c
+++ b/fs/ext4/balloc.c
@@ -368,7 +368,12 @@ static int ext4_validate_block_bitmap(struct super_block *sb,
struct buffer_head *bh)
{
ext4_fsblk_t blk;
- struct ext4_group_info *grp = ext4_get_group_info(sb, block_group);
+ struct ext4_group_info *grp;
+
+ if (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY)
+ return 0;
+
+ grp = ext4_get_group_info(sb, block_group);

if (buffer_verified(bh))
return 0;
diff --git a/fs/ext4/ext4_jbd2.c b/fs/ext4/ext4_jbd2.c
index f291c186eb34..8b49d508efbd 100644
--- a/fs/ext4/ext4_jbd2.c
+++ b/fs/ext4/ext4_jbd2.c
@@ -102,7 +102,7 @@ handle_t *__ext4_journal_start_sb(struct super_block *sb, unsigned int line,
return ERR_PTR(err);

journal = EXT4_SB(sb)->s_journal;
- if (!journal)
+ if (!journal || (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))
return ext4_get_nojournal();
return jbd2__journal_start(journal, blocks, rsv_blocks, revoke_creds,
GFP_NOFS, type, line);
diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c
index d996b44d2265..69c16ac7416e 100644
--- a/fs/ext4/extents_status.c
+++ b/fs/ext4/extents_status.c
@@ -311,6 +311,9 @@ void ext4_es_find_extent_range(struct inode *inode,
ext4_lblk_t lblk, ext4_lblk_t end,
struct extent_status *es)
{
+ if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
+ return;
+
trace_ext4_es_find_extent_range_enter(inode, lblk);

read_lock(&EXT4_I(inode)->i_es_lock);
@@ -361,6 +364,9 @@ bool ext4_es_scan_range(struct inode *inode,
{
bool ret;

+ if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
+ return false;
+
read_lock(&EXT4_I(inode)->i_es_lock);
ret = __es_scan_range(inode, matching_fn, lblk, end);
read_unlock(&EXT4_I(inode)->i_es_lock);
@@ -404,6 +410,9 @@ bool ext4_es_scan_clu(struct inode *inode,
{
bool ret;

+ if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
+ return false;
+
read_lock(&EXT4_I(inode)->i_es_lock);
ret = __es_scan_clu(inode, matching_fn, lblk);
read_unlock(&EXT4_I(inode)->i_es_lock);
@@ -812,6 +821,9 @@ int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
int err = 0;
struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);

+ if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
+ return 0;
+
es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
lblk, len, pblk, status, inode->i_ino);

@@ -873,6 +885,9 @@ void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
struct extent_status newes;
ext4_lblk_t end = lblk + len - 1;

+ if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
+ return;
+
newes.es_lblk = lblk;
newes.es_len = len;
ext4_es_store_pblock_status(&newes, pblk, status);
@@ -908,6 +923,9 @@ int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
struct rb_node *node;
int found = 0;

+ if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
+ return 0;
+
trace_ext4_es_lookup_extent_enter(inode, lblk);
es_debug("lookup extent in block %u\n", lblk);

@@ -1419,6 +1437,9 @@ int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
int err = 0;
int reserved = 0;

+ if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
+ return 0;
+
trace_ext4_es_remove_extent(inode, lblk, len);
es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
lblk, len, inode->i_ino);
@@ -1969,6 +1990,9 @@ int ext4_es_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk,
struct extent_status newes;
int err = 0;

+ if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
+ return 0;
+
es_debug("add [%u/1) delayed to extent status tree of inode %lu\n",
lblk, inode->i_ino);

diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index f3c5b86c6a06..2d3ebc2f6221 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -82,7 +82,12 @@ static int ext4_validate_inode_bitmap(struct super_block *sb,
struct buffer_head *bh)
{
ext4_fsblk_t blk;
- struct ext4_group_info *grp = ext4_get_group_info(sb, block_group);
+ struct ext4_group_info *grp;
+
+ if (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY)
+ return 0;
+
+ grp = ext4_get_group_info(sb, block_group);

if (buffer_verified(bh))
return 0;
@@ -285,15 +290,17 @@ void ext4_free_inode(handle_t *handle, struct inode *inode)
bit = (ino - 1) % EXT4_INODES_PER_GROUP(sb);
bitmap_bh = ext4_read_inode_bitmap(sb, block_group);
/* Don't bother if the inode bitmap is corrupt. */
- grp = ext4_get_group_info(sb, block_group);
if (IS_ERR(bitmap_bh)) {
fatal = PTR_ERR(bitmap_bh);
bitmap_bh = NULL;
goto error_return;
}
- if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) {
- fatal = -EFSCORRUPTED;
- goto error_return;
+ if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
+ grp = ext4_get_group_info(sb, block_group);
+ if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) {
+ fatal = -EFSCORRUPTED;
+ goto error_return;
+ }
}

BUFFER_TRACE(bitmap_bh, "get_write_access");
@@ -872,7 +879,7 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
struct inode *ret;
ext4_group_t i;
ext4_group_t flex_group;
- struct ext4_group_info *grp;
+ struct ext4_group_info *grp = NULL;
int encrypt = 0;

/* Cannot create files in a deleted directory */
@@ -1010,15 +1017,21 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
if (ext4_free_inodes_count(sb, gdp) == 0)
goto next_group;

- grp = ext4_get_group_info(sb, group);
- /* Skip groups with already-known suspicious inode tables */
- if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
- goto next_group;
+ if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
+ grp = ext4_get_group_info(sb, group);
+ /*
+ * Skip groups with already-known suspicious inode
+ * tables
+ */
+ if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
+ goto next_group;
+ }

brelse(inode_bitmap_bh);
inode_bitmap_bh = ext4_read_inode_bitmap(sb, group);
/* Skip groups with suspicious inode tables */
- if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp) ||
+ if (((!(sbi->s_mount_state & EXT4_FC_REPLAY))
+ && EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) ||
IS_ERR(inode_bitmap_bh)) {
inode_bitmap_bh = NULL;
goto next_group;
@@ -1037,7 +1050,7 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
goto next_group;
}

- if (!handle) {
+ if ((!(sbi->s_mount_state & EXT4_FC_REPLAY)) && !handle) {
BUG_ON(nblocks <= 0);
handle = __ext4_journal_start_sb(dir->i_sb, line_no,
handle_type, nblocks, 0,
@@ -1141,9 +1154,15 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
/* Update the relevant bg descriptor fields */
if (ext4_has_group_desc_csum(sb)) {
int free;
- struct ext4_group_info *grp = ext4_get_group_info(sb, group);
-
- down_read(&grp->alloc_sem); /* protect vs itable lazyinit */
+ struct ext4_group_info *grp = NULL;
+
+ if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
+ grp = ext4_get_group_info(sb, group);
+ down_read(&grp->alloc_sem); /*
+ * protect vs itable
+ * lazyinit
+ */
+ }
ext4_lock_group(sb, group); /* while we modify the bg desc */
free = EXT4_INODES_PER_GROUP(sb) -
ext4_itable_unused_count(sb, gdp);
@@ -1159,7 +1178,8 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
if (ino > free)
ext4_itable_unused_set(sb, gdp,
(EXT4_INODES_PER_GROUP(sb) - ino));
- up_read(&grp->alloc_sem);
+ if (!(sbi->s_mount_state & EXT4_FC_REPLAY))
+ up_read(&grp->alloc_sem);
} else {
ext4_lock_group(sb, group);
}
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 66e56ac6d028..ba839213c2c9 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -509,7 +509,8 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
return -EFSCORRUPTED;

/* Lookup extent status tree firstly */
- if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
+ if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) &&
+ ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
map->m_pblk = ext4_es_pblock(&es) +
map->m_lblk - es.es_lblk;
@@ -821,7 +822,8 @@ struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
int create = map_flags & EXT4_GET_BLOCKS_CREATE;
int err;

- J_ASSERT(handle != NULL || create == 0);
+ J_ASSERT((EXT4_SB(inode->i_sb)->s_mount_state | EXT4_FC_REPLAY)
+ || handle != NULL || create == 0);

map.m_lblk = block;
map.m_len = 1;
@@ -837,7 +839,8 @@ struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
return ERR_PTR(-ENOMEM);
if (map.m_flags & EXT4_MAP_NEW) {
J_ASSERT(create != 0);
- J_ASSERT(handle != NULL);
+ J_ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
+ || (handle != NULL));

/*
* Now that we do not always journal data, we should
@@ -4589,7 +4592,8 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
if (!ext4_inode_csum_verify(inode, raw_inode, ei) ||
ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) {
ext4_set_errno(inode->i_sb, EFSBADCRC);
- ext4_error_inode(inode, function, line, 0,
+ if (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))
+ ext4_error_inode(inode, function, line, 0,
"iget: checksum invalid");
ret = -EFSBADCRC;
goto bad_inode;
diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 96be991718f1..951ce1250dc7 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -1449,14 +1449,17 @@ static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,

blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
blocknr += EXT4_C2B(sbi, block);
- ext4_grp_locked_error(sb, e4b->bd_group,
- inode ? inode->i_ino : 0,
- blocknr,
- "freeing already freed block "
- "(bit %u); block bitmap corrupt.",
- block);
- ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
+ if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
+ ext4_grp_locked_error(sb, e4b->bd_group,
+ inode ? inode->i_ino : 0,
+ blocknr,
+ "freeing already freed block "
+ "(bit %u); block bitmap corrupt.",
+ block);
+ ext4_mark_group_bitmap_corrupted(
+ sb, e4b->bd_group,
EXT4_GROUP_INFO_BBITMAP_CORRUPT);
+ }
mb_regenerate_buddy(e4b);
goto done;
}
@@ -4109,6 +4112,9 @@ void ext4_discard_preallocations(struct inode *inode)
return;
}

+ if (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY)
+ return;
+
mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
trace_ext4_discard_preallocations(inode);

@@ -4585,6 +4591,8 @@ ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
sb = ar->inode->i_sb;
sbi = EXT4_SB(sb);

+ WARN_ON(sbi->s_mount_state & EXT4_FC_REPLAY);
+
trace_ext4_request_blocks(ar);

/* Allow to use superuser reservation for quota file */
--
2.25.1.481.gfbce0eb801-goog

2020-03-09 07:07:54

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 15/20] jbd2: add fast commit recovery path support

Add fc_do_one_pass to invoke file system specific replay
callback and pass discovered fast commit blocks to let
file system handle those.

Signed-off-by: Harshad Shirwadkar <[email protected]>
---
fs/jbd2/recovery.c | 67 +++++++++++++++++++++++++++++++++++++++++---
include/linux/jbd2.h | 13 +++++++++
2 files changed, 76 insertions(+), 4 deletions(-)

diff --git a/fs/jbd2/recovery.c b/fs/jbd2/recovery.c
index a4967b27ffb6..09f069e59c36 100644
--- a/fs/jbd2/recovery.c
+++ b/fs/jbd2/recovery.c
@@ -35,7 +35,6 @@ struct recovery_info
int nr_revoke_hits;
};

-enum passtype {PASS_SCAN, PASS_REVOKE, PASS_REPLAY};
static int do_one_pass(journal_t *journal,
struct recovery_info *info, enum passtype pass);
static int scan_revoke_records(journal_t *, struct buffer_head *,
@@ -225,10 +224,63 @@ static int count_tags(journal_t *journal, struct buffer_head *bh)
/* Make sure we wrap around the log correctly! */
#define wrap(journal, var) \
do { \
- if (var >= (journal)->j_last) \
- var -= ((journal)->j_last - (journal)->j_first); \
+ unsigned long _wrap_last = \
+ jbd2_has_feature_fast_commit(journal) ? \
+ (journal)->j_last_fc : (journal)->j_last; \
+ \
+ if (var >= _wrap_last) \
+ var -= (_wrap_last - (journal)->j_first); \
} while (0)

+static int fc_do_one_pass(journal_t *journal,
+ struct recovery_info *info, enum passtype pass)
+{
+ unsigned int expected_commit_id = info->end_transaction;
+ unsigned long next_fc_block;
+ struct buffer_head *bh;
+ unsigned int seq;
+ journal_header_t *jhdr;
+ int err = 0;
+
+ next_fc_block = journal->j_first_fc;
+
+ while (next_fc_block <= journal->j_last_fc) {
+ jbd_debug(3, "Fast commit replay: next block %ld",
+ next_fc_block);
+ err = jread(&bh, journal, next_fc_block);
+ if (err) {
+ jbd_debug(3, "Fast commit replay: read error");
+ break;
+ }
+
+ jhdr = (journal_header_t *)bh->b_data;
+ seq = be32_to_cpu(jhdr->h_sequence);
+ if (be32_to_cpu(jhdr->h_magic) != JBD2_MAGIC_NUMBER ||
+ seq != expected_commit_id) {
+ jbd_debug(3, "Fast commit replay: magic / commitid error [%d / %d / %d]\n",
+ be32_to_cpu(jhdr->h_magic), seq,
+ expected_commit_id);
+ break;
+ }
+ jbd_debug(3, "Processing fast commit blk with seq %d",
+ seq);
+ if (journal->j_fc_replay_callback) {
+ err = journal->j_fc_replay_callback(
+ journal, bh, pass,
+ next_fc_block -
+ journal->j_first_fc);
+ if (err)
+ break;
+ }
+ next_fc_block++;
+ }
+
+ if (err)
+ jbd_debug(3, "Fast commit replay failed, err = %d\n", err);
+
+ return err;
+}
+
/**
* jbd2_journal_recover - recovers a on-disk journal
* @journal: the journal to recover
@@ -470,7 +522,7 @@ static int do_one_pass(journal_t *journal,
break;

jbd_debug(2, "Scanning for sequence ID %u at %lu/%lu\n",
- next_commit_ID, next_log_block, journal->j_last);
+ next_commit_ID, next_log_block, journal->j_last_fc);

/* Skip over each chunk of the transaction looking
* either the next descriptor block or the final commit
@@ -768,6 +820,9 @@ static int do_one_pass(journal_t *journal,
if (err)
goto failed;
continue;
+ case JBD2_FC_BLOCK:
+ pr_warn("Unexpectedly found fast commit block.\n");
+ continue;

default:
jbd_debug(3, "Unrecognised magic %d, end of scan.\n",
@@ -799,6 +854,10 @@ static int do_one_pass(journal_t *journal,
success = -EIO;
}
}
+
+ if (jbd2_has_feature_fast_commit(journal) && pass != PASS_REVOKE)
+ success = fc_do_one_pass(journal, info, pass);
+
if (block_error && success == 0)
success = -EIO;
return success;
diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
index 599113bef67f..01f6de8f6731 100644
--- a/include/linux/jbd2.h
+++ b/include/linux/jbd2.h
@@ -749,6 +749,8 @@ jbd2_time_diff(unsigned long start, unsigned long end)

#define JBD2_NR_BATCH 64

+enum passtype {PASS_SCAN, PASS_REVOKE, PASS_REPLAY};
+
/**
* struct journal_s - The journal_s type is the concrete type associated with
* journal_t.
@@ -1219,6 +1221,17 @@ struct journal_s
* after every commit operation.
*/
void (*j_fc_cleanup_callback)(struct journal_s *journal);
+
+ /*
+ * @j_fc_replay_callback:
+ *
+ * File-system specific function that performs replay of a fast
+ * commit. JBD2 calls this function for each fast commit block found in
+ * the journal.
+ */
+ int (*j_fc_replay_callback)(struct journal_s *journal,
+ struct buffer_head *bh,
+ enum passtype pass, int off);
};

#define jbd2_might_wait_for_commit(j) \
--
2.25.1.481.gfbce0eb801-goog

2020-03-09 07:07:55

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 10/20] ext4: break ext4_unlink() and ext4_link()

Break ext4_link() and ext4_unlink() each into 2 parts in order to make
them usable in recovery path as well.

Signed-off-by: Harshad Shirwadkar <[email protected]>
---
fs/ext4/ext4.h | 4 ++
fs/ext4/namei.c | 139 +++++++++++++++++++++++++++++-------------------
2 files changed, 88 insertions(+), 55 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 5cb7f923dbee..6cc3a93b0ce0 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -3348,6 +3348,10 @@ extern int ext4_handle_dirty_dirblock(handle_t *handle, struct inode *inode,
extern int ext4_ci_compare(const struct inode *parent,
const struct qstr *fname,
const struct qstr *entry, bool quick);
+extern int __ext4_unlink(struct inode *dir, const struct qstr *d_name,
+ struct inode *inode);
+extern int __ext4_link(struct inode *dir, struct inode *inode,
+ struct dentry *dentry);

#define S_SHIFT 12
static const unsigned char ext4_type_by_mode[(S_IFMT >> S_SHIFT) + 1] = {
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index b05ea72f38fd..5ee002cc0acd 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -3171,39 +3171,36 @@ static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
return retval;
}

-static int ext4_unlink(struct inode *dir, struct dentry *dentry)
+int __ext4_unlink(struct inode *dir, const struct qstr *d_name,
+ struct inode *inode)
{
- int retval;
- struct inode *inode;
struct buffer_head *bh;
struct ext4_dir_entry_2 *de;
handle_t *handle = NULL;
+ int retval = -ENOENT;
+ int skip_remove_dentry = 0;

- if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
- return -EIO;
-
- trace_ext4_unlink_enter(dir, dentry);
- /* Initialize quotas before so that eventual writes go
- * in separate transaction */
- retval = dquot_initialize(dir);
- if (retval)
- return retval;
- retval = dquot_initialize(d_inode(dentry));
- if (retval)
- return retval;
-
- retval = -ENOENT;
- bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
+ bh = ext4_find_entry(dir, d_name, &de, NULL);
if (IS_ERR(bh))
return PTR_ERR(bh);
- if (!bh)
- goto end_unlink;

- inode = d_inode(dentry);
+ if (!bh) {
+ retval = -ENOENT;
+ goto end_unlink;
+ }

retval = -EFSCORRUPTED;
- if (le32_to_cpu(de->inode) != inode->i_ino)
- goto end_unlink;
+ if (le32_to_cpu(de->inode) != inode->i_ino) {
+ /*
+ * It's okay if we find dont find dentry which matches
+ * the inode. That's because it might have gotten
+ * renamed to a different inode number
+ */
+ if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
+ skip_remove_dentry = 1;
+ else
+ goto end_unlink;
+ }

handle = ext4_journal_start(dir, EXT4_HT_DIR,
EXT4_DATA_TRANS_BLOCKS(dir->i_sb));
@@ -3216,15 +3213,20 @@ static int ext4_unlink(struct inode *dir, struct dentry *dentry)
if (IS_DIRSYNC(dir))
ext4_handle_sync(handle);

- retval = ext4_delete_entry(handle, dir, de, bh);
- if (retval)
- goto end_unlink;
- dir->i_ctime = dir->i_mtime = current_time(dir);
- ext4_update_dx_flag(dir);
- ext4_mark_inode_dirty(handle, dir);
+ if (!skip_remove_dentry) {
+ retval = ext4_delete_entry(handle, dir, de, bh);
+ if (retval)
+ goto end_unlink;
+ dir->i_ctime = dir->i_mtime = current_time(dir);
+ ext4_update_dx_flag(dir);
+ ext4_mark_inode_dirty(handle, dir);
+ } else {
+ retval = 0;
+ }
+
if (inode->i_nlink == 0)
ext4_warning_inode(inode, "Deleting file '%.*s' with no links",
- dentry->d_name.len, dentry->d_name.name);
+ d_name->len, d_name->name);
else
drop_nlink(inode);
if (!inode->i_nlink)
@@ -3232,6 +3234,33 @@ static int ext4_unlink(struct inode *dir, struct dentry *dentry)
inode->i_ctime = current_time(inode);
ext4_mark_inode_dirty(handle, inode);

+end_unlink:
+ brelse(bh);
+ if (handle)
+ ext4_journal_stop(handle);
+ return retval;
+}
+
+static int ext4_unlink(struct inode *dir, struct dentry *dentry)
+{
+ int retval;
+
+ if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb))))
+ return -EIO;
+
+ trace_ext4_unlink_enter(dir, dentry);
+ /*
+ * Initialize quotas before so that eventual writes go
+ * in separate transaction
+ */
+ retval = dquot_initialize(dir);
+ if (retval)
+ return retval;
+ retval = dquot_initialize(d_inode(dentry));
+ if (retval)
+ return retval;
+
+ retval = __ext4_unlink(dir, &dentry->d_name, d_inode(dentry));
#ifdef CONFIG_UNICODE
/* VFS negative dentries are incompatible with Encoding and
* Case-insensitiveness. Eventually we'll want avoid
@@ -3242,11 +3271,6 @@ static int ext4_unlink(struct inode *dir, struct dentry *dentry)
if (IS_CASEFOLDED(dir))
d_invalidate(dentry);
#endif
-
-end_unlink:
- brelse(bh);
- if (handle)
- ext4_journal_stop(handle);
trace_ext4_unlink_exit(dentry, retval);
return retval;
}
@@ -3380,29 +3404,10 @@ static int ext4_symlink(struct inode *dir,
return err;
}

-static int ext4_link(struct dentry *old_dentry,
- struct inode *dir, struct dentry *dentry)
+int __ext4_link(struct inode *dir, struct inode *inode, struct dentry *dentry)
{
handle_t *handle;
- struct inode *inode = d_inode(old_dentry);
int err, retries = 0;
-
- if (inode->i_nlink >= EXT4_LINK_MAX)
- return -EMLINK;
-
- err = fscrypt_prepare_link(old_dentry, dir, dentry);
- if (err)
- return err;
-
- if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
- (!projid_eq(EXT4_I(dir)->i_projid,
- EXT4_I(old_dentry->d_inode)->i_projid)))
- return -EXDEV;
-
- err = dquot_initialize(dir);
- if (err)
- return err;
-
retry:
handle = ext4_journal_start(dir, EXT4_HT_DIR,
(EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
@@ -3436,6 +3441,30 @@ static int ext4_link(struct dentry *old_dentry,
return err;
}

+static int ext4_link(struct dentry *old_dentry,
+ struct inode *dir, struct dentry *dentry)
+{
+ struct inode *inode = d_inode(old_dentry);
+ int err;
+
+ if (inode->i_nlink >= EXT4_LINK_MAX)
+ return -EMLINK;
+
+ err = fscrypt_prepare_link(old_dentry, dir, dentry);
+ if (err)
+ return err;
+
+ if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
+ (!projid_eq(EXT4_I(dir)->i_projid,
+ EXT4_I(old_dentry->d_inode)->i_projid)))
+ return -EXDEV;
+
+ err = dquot_initialize(dir);
+ if (err)
+ return err;
+ return __ext4_link(dir, inode, dentry);
+}
+

/*
* Try to find buffer head where contains the parent block.
--
2.25.1.481.gfbce0eb801-goog

2020-03-09 07:07:54

by harshad shirwadkar

[permalink] [raw]
Subject: [PATCH v5 12/20] ext4: add fast commit on-disk format structs

Add structs representing on-disk format of the commit header and
tlvs in the commit header.

Signed-off-by: Harshad Shirwadkar <[email protected]>
---
fs/ext4/ext4_jbd2.c | 1 +
fs/ext4/ext4_jbd2.h | 45 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+)

diff --git a/fs/ext4/ext4_jbd2.c b/fs/ext4/ext4_jbd2.c
index a29ae83df881..78c5431c7aad 100644
--- a/fs/ext4/ext4_jbd2.c
+++ b/fs/ext4/ext4_jbd2.c
@@ -4,6 +4,7 @@
*/

#include "ext4_jbd2.h"
+#include "ext4_extents.h"

#include <trace/events/ext4.h>

diff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h
index 401f142172e4..761148b99b35 100644
--- a/fs/ext4/ext4_jbd2.h
+++ b/fs/ext4/ext4_jbd2.h
@@ -526,7 +526,52 @@ static inline int ext4_should_dioread_nolock(struct inode *inode)
return 1;
}

+/* Ext4 fast commit related info */
+
+/* Magic of fast commit header */
+#define EXT4_FC_MAGIC 0xE2540090
+
#define EXT4_NUM_FC_BLKS 128
+
+struct ext4_fc_commit_hdr {
+ /* Fast commit magic, should be EXT4_FC_MAGIC */
+ __le32 fc_magic;
+ /* Features used by this fast commit block */
+ __u8 fc_features;
+ /* Number of TLVs in this fast commmit block */
+ __le16 fc_num_tlvs;
+ /* Inode number */
+ __le32 fc_ino;
+ /* Csum(hdr+contents) */
+ __le32 fc_csum;
+};
+
+/* Fast commit on disk tag length structure */
+struct ext4_fc_tl {
+ __le16 fc_tag;
+ __le16 fc_len;
+};
+
+/* On disk fast commit tlv value structure for dirent tags:
+ * - EXT4_FC_TAG_CREATE_DENTRY
+ * - EXT4_FC_TAG_ADD_DENTRY
+ * - EXT4_FC_TAG_DEL_DENTRY
+ */
+struct ext4_fc_dentry_info {
+ __le32 fc_parent_ino;
+ __le32 fc_ino;
+ u8 fc_dname[0];
+};
+
+/*
+ * On disk fast commit tlv value structure for tag
+ * EXT4_FC_TAG_HOLE.
+ */
+struct ext4_fc_lrange {
+ __le32 fc_lblk;
+ __le32 fc_len;
+};
+
void ext4_init_fast_commit(struct super_block *sb, journal_t *journal);
void ext4_init_inode_fc_info(struct inode *inode);
void ext4_fc_track_range(struct inode *inode, ext4_lblk_t start,
--
2.25.1.481.gfbce0eb801-goog