@@ -242,27 +242,44 @@ static void ext4_put_nojournal(handle_t *handle)
242242 * journal_end calls result in the superblock being marked dirty, so
243243 * that sync() will call the filesystem's write_super callback if
244244 * appropriate.
245+ *
246+ * To avoid j_barrier hold in userspace when a user calls freeze(),
247+ * ext4 prevents a new handle from being started by s_frozen, which
248+ * is in an upper layer.
245249 */
246250handle_t * ext4_journal_start_sb (struct super_block * sb , int nblocks )
247251{
248252 journal_t * journal ;
253+ handle_t * handle ;
249254
250255 if (sb -> s_flags & MS_RDONLY )
251256 return ERR_PTR (- EROFS );
252257
253- vfs_check_frozen (sb , SB_FREEZE_TRANS );
254- /* Special case here: if the journal has aborted behind our
255- * backs (eg. EIO in the commit thread), then we still need to
256- * take the FS itself readonly cleanly. */
257258 journal = EXT4_SB (sb )-> s_journal ;
258- if (journal ) {
259- if (is_journal_aborted (journal )) {
260- ext4_abort (sb , "Detected aborted journal" );
261- return ERR_PTR (- EROFS );
262- }
263- return jbd2_journal_start (journal , nblocks );
259+ handle = ext4_journal_current_handle ();
260+
261+ /*
262+ * If a handle has been started, it should be allowed to
263+ * finish, otherwise deadlock could happen between freeze
264+ * and others(e.g. truncate) due to the restart of the
265+ * journal handle if the filesystem is forzen and active
266+ * handles are not stopped.
267+ */
268+ if (!handle )
269+ vfs_check_frozen (sb , SB_FREEZE_TRANS );
270+
271+ if (!journal )
272+ return ext4_get_nojournal ();
273+ /*
274+ * Special case here: if the journal has aborted behind our
275+ * backs (eg. EIO in the commit thread), then we still need to
276+ * take the FS itself readonly cleanly.
277+ */
278+ if (is_journal_aborted (journal )) {
279+ ext4_abort (sb , "Detected aborted journal" );
280+ return ERR_PTR (- EROFS );
264281 }
265- return ext4_get_nojournal ( );
282+ return jbd2_journal_start ( journal , nblocks );
266283}
267284
268285/*
@@ -2975,6 +2992,12 @@ static int ext4_register_li_request(struct super_block *sb,
29752992 mutex_unlock (& ext4_li_info -> li_list_mtx );
29762993
29772994 sbi -> s_li_request = elr ;
2995+ /*
2996+ * set elr to NULL here since it has been inserted to
2997+ * the request_list and the removal and free of it is
2998+ * handled by ext4_clear_request_list from now on.
2999+ */
3000+ elr = NULL ;
29783001
29793002 if (!(ext4_li_info -> li_state & EXT4_LAZYINIT_RUNNING )) {
29803003 ret = ext4_run_lazyinit_thread ();
@@ -3385,6 +3408,10 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
33853408 get_random_bytes (& sbi -> s_next_generation , sizeof (u32 ));
33863409 spin_lock_init (& sbi -> s_next_gen_lock );
33873410
3411+ init_timer (& sbi -> s_err_report );
3412+ sbi -> s_err_report .function = print_daily_error_info ;
3413+ sbi -> s_err_report .data = (unsigned long ) sb ;
3414+
33883415 err = percpu_counter_init (& sbi -> s_freeblocks_counter ,
33893416 ext4_count_free_blocks (sb ));
33903417 if (!err ) {
@@ -3646,9 +3673,6 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
36463673 "Opts: %s%s%s" , descr , sbi -> s_es -> s_mount_opts ,
36473674 * sbi -> s_es -> s_mount_opts ? "; " : "" , orig_data );
36483675
3649- init_timer (& sbi -> s_err_report );
3650- sbi -> s_err_report .function = print_daily_error_info ;
3651- sbi -> s_err_report .data = (unsigned long ) sb ;
36523676 if (es -> s_error_count )
36533677 mod_timer (& sbi -> s_err_report , jiffies + 300 * HZ ); /* 5 minutes */
36543678
@@ -3672,6 +3696,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
36723696 sbi -> s_journal = NULL ;
36733697 }
36743698failed_mount3 :
3699+ del_timer (& sbi -> s_err_report );
36753700 if (sbi -> s_flex_groups ) {
36763701 if (is_vmalloc_addr (sbi -> s_flex_groups ))
36773702 vfree (sbi -> s_flex_groups );
@@ -4138,6 +4163,11 @@ static int ext4_sync_fs(struct super_block *sb, int wait)
41384163/*
41394164 * LVM calls this function before a (read-only) snapshot is created. This
41404165 * gives us a chance to flush the journal completely and mark the fs clean.
4166+ *
4167+ * Note that only this function cannot bring a filesystem to be in a clean
4168+ * state independently, because ext4 prevents a new handle from being started
4169+ * by @sb->s_frozen, which stays in an upper layer. It thus needs help from
4170+ * the upper layer.
41414171 */
41424172static int ext4_freeze (struct super_block * sb )
41434173{
@@ -4614,11 +4644,24 @@ static int ext4_quota_on(struct super_block *sb, int type, int format_id,
46144644
46154645static int ext4_quota_off (struct super_block * sb , int type )
46164646{
4647+ struct inode * inode = sb_dqopt (sb )-> files [type ];
4648+ handle_t * handle ;
4649+
46174650 /* Force all delayed allocation blocks to be allocated.
46184651 * Caller already holds s_umount sem */
46194652 if (test_opt (sb , DELALLOC ))
46204653 sync_filesystem (sb );
46214654
4655+ /* Update modification times of quota files when userspace can
4656+ * start looking at them */
4657+ handle = ext4_journal_start (inode , 1 );
4658+ if (IS_ERR (handle ))
4659+ goto out ;
4660+ inode -> i_mtime = inode -> i_ctime = CURRENT_TIME ;
4661+ ext4_mark_inode_dirty (handle , inode );
4662+ ext4_journal_stop (handle );
4663+
4664+ out :
46224665 return dquot_quota_off (sb , type );
46234666}
46244667
@@ -4714,9 +4757,8 @@ static ssize_t ext4_quota_write(struct super_block *sb, int type,
47144757 if (inode -> i_size < off + len ) {
47154758 i_size_write (inode , off + len );
47164759 EXT4_I (inode )-> i_disksize = inode -> i_size ;
4760+ ext4_mark_inode_dirty (handle , inode );
47174761 }
4718- inode -> i_mtime = inode -> i_ctime = CURRENT_TIME ;
4719- ext4_mark_inode_dirty (handle , inode );
47204762 mutex_unlock (& inode -> i_mutex );
47214763 return len ;
47224764}
0 commit comments