]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
ext4: for committing inode, make ext4_fc_track_inode wait
authorHarshad Shirwadkar <harshadshirwadkar@gmail.com>
Thu, 8 May 2025 17:59:01 +0000 (17:59 +0000)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 9 May 2025 01:56:17 +0000 (21:56 -0400)
If the inode that's being requested to track using ext4_fc_track_inode
is being committed, then wait until the inode finishes the
commit. Also, add calls to ext4_fc_track_inode at the right places.
With this patch, now calling ext4_reserve_inode_write() results in
inode being tracked for next fast commit. This ensures that by the
time ext4_reserve_inode_write() returns, it is ready to be modified
and won't be committed until the corresponding handle is open.

A subtle lock ordering requirement with i_data_sem (which is
documented in the code) requires that ext4_fc_track_inode() be called
before grabbing i_data_sem. So, this patch also adds explicit
ext4_fc_track_inode() calls in places where i_data_sem grabbed.

Signed-off-by: Harshad Shirwadkar <harshadshirwadkar@gmail.com>
Link: https://patch.msgid.link/20250508175908.1004880-3-harshadshirwadkar@gmail.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/fast_commit.c
fs/ext4/inline.c
fs/ext4/inode.c

index 63859ec6d91dde56bbef80ad7a5e82e965f4fc45..c4d3c71d5e6c616835d15b383c0b918d472bedee 100644 (file)
@@ -12,6 +12,7 @@
 #include "ext4_extents.h"
 #include "mballoc.h"
 
+#include <linux/lockdep.h>
 /*
  * Ext4 Fast Commits
  * -----------------
@@ -570,6 +571,8 @@ static int __track_inode(handle_t *handle, struct inode *inode, void *arg,
 
 void ext4_fc_track_inode(handle_t *handle, struct inode *inode)
 {
+       struct ext4_inode_info *ei = EXT4_I(inode);
+       wait_queue_head_t *wq;
        int ret;
 
        if (S_ISDIR(inode->i_mode))
@@ -587,6 +590,38 @@ void ext4_fc_track_inode(handle_t *handle, struct inode *inode)
        if (ext4_test_mount_flag(inode->i_sb, EXT4_MF_FC_INELIGIBLE))
                return;
 
+       if (!list_empty(&ei->i_fc_list))
+               return;
+
+       /*
+        * If we come here, we may sleep while waiting for the inode to
+        * commit. We shouldn't be holding i_data_sem when we go to sleep since
+        * the commit path needs to grab the lock while committing the inode.
+        */
+       lockdep_assert_not_held(&ei->i_data_sem);
+
+       while (ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING)) {
+#if (BITS_PER_LONG < 64)
+               DEFINE_WAIT_BIT(wait, &ei->i_state_flags,
+                               EXT4_STATE_FC_COMMITTING);
+               wq = bit_waitqueue(&ei->i_state_flags,
+                                  EXT4_STATE_FC_COMMITTING);
+#else
+               DEFINE_WAIT_BIT(wait, &ei->i_flags,
+                               EXT4_STATE_FC_COMMITTING);
+               wq = bit_waitqueue(&ei->i_flags,
+                                  EXT4_STATE_FC_COMMITTING);
+#endif
+               prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
+               if (ext4_test_inode_state(inode, EXT4_STATE_FC_COMMITTING))
+                       schedule();
+               finish_wait(wq, &wait.wq_entry);
+       }
+
+       /*
+        * From this point on, this inode will not be committed either
+        * by fast or full commit as long as the handle is open.
+        */
        ret = ext4_fc_track_template(handle, inode, __track_inode, NULL, 1);
        trace_ext4_fc_track_inode(handle, inode, ret);
 }
index 2c9b762925c72f2ff5a402b02500370bc1eb0eb1..fbc1c84b555c124d47f8a1b8da18938c738ce801 100644 (file)
@@ -601,6 +601,7 @@ retry:
                        goto out;
        }
 
+       ext4_fc_track_inode(handle, inode);
        ret = ext4_destroy_inline_data_nolock(handle, inode);
        if (ret)
                goto out;
index 94c7d2d828a64e42ded09c82497ed7617071aa19..d58b99407390febcf640cd506c8985be7f1a1696 100644 (file)
@@ -696,6 +696,8 @@ found:
                if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
                        return retval;
 
+
+       ext4_fc_track_inode(handle, inode);
        /*
         * New blocks allocate and/or writing to unwritten extent
         * will possibly result in updating i_data, so we take
@@ -4072,6 +4074,7 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
        if (end_lblk > start_lblk) {
                ext4_lblk_t hole_len = end_lblk - start_lblk;
 
+               ext4_fc_track_inode(handle, inode);
                down_write(&EXT4_I(inode)->i_data_sem);
                ext4_discard_preallocations(inode);
 
@@ -4224,6 +4227,7 @@ int ext4_truncate(struct inode *inode)
        if (err)
                goto out_stop;
 
+       ext4_fc_track_inode(handle, inode);
        down_write(&EXT4_I(inode)->i_data_sem);
 
        ext4_discard_preallocations(inode);
@@ -5895,6 +5899,7 @@ ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
                        brelse(iloc->bh);
                        iloc->bh = NULL;
                }
+               ext4_fc_track_inode(handle, inode);
        }
        ext4_std_error(inode->i_sb, err);
        return err;