From nobody Sun May 19 23:34:07 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C45AFC74A5B for ; Sun, 26 Mar 2023 13:16:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230309AbjCZNQt (ORCPT ); Sun, 26 Mar 2023 09:16:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229483AbjCZNQr (ORCPT ); Sun, 26 Mar 2023 09:16:47 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 168FF7ECC for ; Sun, 26 Mar 2023 06:16:46 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 3569B60EB9 for ; Sun, 26 Mar 2023 13:16:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 75C5BC433EF; Sun, 26 Mar 2023 13:16:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679836604; bh=UhA5nZ0WIjNyoy6xU0ABSs2w1vXxmti8KsJAj8cZizo=; h=From:To:Cc:Subject:Date:From; b=KgIH63WdJwREPEyupws2qOdaqperwjpfolmUr9X65FBu3zb+6yvAS+IcJe4UQoeiY q20ZF+hOUO0hKPpUrH8Cgbt08egILF7dA25Pa71meQ/4k1rTv468rOLxCaEbSPHyWU IxOsr6W0pGtHJEr30TD3LIF3pCjZeG41wN7526saehXKttiZQZWE8pgMN1qq2FAqR3 jCf55iw/A1LrhgI1RZqUiT6mL6CPI+QBoGl87ik8XopRQ0frMXfxBuJC4GS015ga+N Wudsz0nM+XNoBwyve1Wlu5PFHwR1+QO4sJ/j0kGeTbjS11g43uWK0J+9IOd6H/jsws K4j/W6WdO1Jvw== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu , Yangtao Li Subject: [PATCH v7] f2fs: support errors=remount-ro|continue|panic mountoption Date: Sun, 26 Mar 2023 14:21:28 +0800 Message-Id: <20230326062128.1423340-1-chao@kernel.org> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This patch supports errors=3Dremount-ro|continue|panic mount option, by default it uses "continue" mode. Signed-off-by: Chao Yu Signed-off-by: Yangtao Li --- v7: - use errors=3Dreadonly as by default. - set CP_ERROR_FLAG for critical error. Documentation/filesystems/f2fs.rst | 5 ++ fs/f2fs/checkpoint.c | 7 +- fs/f2fs/f2fs.h | 20 ++++- fs/f2fs/file.c | 5 -- fs/f2fs/gc.c | 2 +- fs/f2fs/super.c | 134 ++++++++++++++++++++++++++--- 6 files changed, 149 insertions(+), 24 deletions(-) diff --git a/Documentation/filesystems/f2fs.rst b/Documentation/filesystems= /f2fs.rst index 2055e72871fe..6142396c015e 100644 --- a/Documentation/filesystems/f2fs.rst +++ b/Documentation/filesystems/f2fs.rst @@ -351,6 +351,11 @@ age_extent_cache Enable an age extent cache based on = rb-tree. It records data block update frequency of the extent per inode, in order to provide better temperature hints for data block allocation. +errors=3D%s Specify f2fs behavior on critical errors. This supports mode= s: + "panic", "continue" and "remount-ro", respectively, trigger + panic immediately, continue without doing anything, and remount + the partition in read-only mode. By default it uses "readonly" + mode. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 Debugfs Entries diff --git a/fs/f2fs/checkpoint.c b/fs/f2fs/checkpoint.c index 1e0164cde23d..d98f37d174d5 100644 --- a/fs/f2fs/checkpoint.c +++ b/fs/f2fs/checkpoint.c @@ -30,12 +30,9 @@ void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool= end_io, unsigned char reason) { f2fs_build_fault_attr(sbi, 0, 0); - set_ckpt_flags(sbi, CP_ERROR_FLAG); - if (!end_io) { + if (!end_io) f2fs_flush_merged_writes(sbi); - - f2fs_handle_stop(sbi, reason); - } + f2fs_handle_critical_error(sbi, reason, end_io); } =20 /* diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index 53a005b420cf..b8a3c7af839b 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -162,6 +162,7 @@ struct f2fs_mount_info { int fs_mode; /* fs mode: LFS or ADAPTIVE */ int bggc_mode; /* bggc mode: off, on or sync */ int memory_mode; /* memory mode */ + int errors; /* errors parameter */ int discard_unit; /* * discard command's offset/size should * be aligned to this unit: block, @@ -1369,6 +1370,12 @@ enum { MEMORY_MODE_LOW, /* memory mode for low memry devices */ }; =20 +enum errors_option { + MOUNT_ERRORS_READONLY, /* remount fs ro on errors */ + MOUNT_ERRORS_CONTINUE, /* continue on errors */ + MOUNT_ERRORS_PANIC, /* panic on errors */ +}; + static inline int f2fs_test_bit(unsigned int nr, char *addr); static inline void f2fs_set_bit(unsigned int nr, char *addr); static inline void f2fs_clear_bit(unsigned int nr, char *addr); @@ -1801,8 +1808,14 @@ struct f2fs_sb_info { =20 struct workqueue_struct *post_read_wq; /* post read workqueue */ =20 - unsigned char errors[MAX_F2FS_ERRORS]; /* error flags */ - spinlock_t error_lock; /* protect errors array */ + /* + * If we are in irq context, let's update error information into + * on-disk superblock in the work. + */ + struct work_struct s_error_work; + unsigned char errors[MAX_F2FS_ERRORS]; /* error flags */ + unsigned char stop_reason[MAX_STOP_REASON]; /* stop reason */ + spinlock_t error_lock; /* protect errors/stop_reason array */ bool error_dirty; /* errors of sb is dirty */ =20 struct kmem_cache *inline_xattr_slab; /* inline xattr entry */ @@ -3538,7 +3551,8 @@ int f2fs_enable_quota_files(struct f2fs_sb_info *sbi,= bool rdonly); int f2fs_quota_sync(struct super_block *sb, int type); loff_t max_file_blocks(struct inode *inode); void f2fs_quota_off_umount(struct super_block *sb); -void f2fs_handle_stop(struct f2fs_sb_info *sbi, unsigned char reason); +void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char re= ason, + bool irq_context); void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error); int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover); int f2fs_sync_fs(struct super_block *sb, int sync); diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index 836173c53eb9..c1b0b00098e4 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -2221,7 +2221,6 @@ static int f2fs_ioc_shutdown(struct file *filp, unsig= ned long arg) ret =3D 0; f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN); - set_sbi_flag(sbi, SBI_IS_SHUTDOWN); trace_f2fs_shutdown(sbi, in, ret); } return ret; @@ -2234,7 +2233,6 @@ static int f2fs_ioc_shutdown(struct file *filp, unsig= ned long arg) if (ret) goto out; f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN); - set_sbi_flag(sbi, SBI_IS_SHUTDOWN); thaw_bdev(sb->s_bdev); break; case F2FS_GOING_DOWN_METASYNC: @@ -2243,16 +2241,13 @@ static int f2fs_ioc_shutdown(struct file *filp, uns= igned long arg) if (ret) goto out; f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN); - set_sbi_flag(sbi, SBI_IS_SHUTDOWN); break; case F2FS_GOING_DOWN_NOSYNC: f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN); - set_sbi_flag(sbi, SBI_IS_SHUTDOWN); break; case F2FS_GOING_DOWN_METAFLUSH: f2fs_sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO); f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN); - set_sbi_flag(sbi, SBI_IS_SHUTDOWN); break; case F2FS_GOING_DOWN_NEED_FSCK: set_sbi_flag(sbi, SBI_NEED_FSCK); diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c index 2996d38aa89c..2a52d9c649cb 100644 --- a/fs/f2fs/gc.c +++ b/fs/f2fs/gc.c @@ -59,7 +59,7 @@ static int gc_thread_func(void *data) if (gc_th->gc_wake) gc_th->gc_wake =3D false; =20 - if (try_to_freeze()) { + if (try_to_freeze() || f2fs_readonly(sbi->sb)) { stat_other_skip_bggc_count(sbi); continue; } diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c index 9c87d91df61b..3d806bfd890e 100644 --- a/fs/f2fs/super.c +++ b/fs/f2fs/super.c @@ -164,6 +164,7 @@ enum { Opt_discard_unit, Opt_memory_mode, Opt_age_extent_cache, + Opt_errors, Opt_err, }; =20 @@ -243,6 +244,7 @@ static match_table_t f2fs_tokens =3D { {Opt_discard_unit, "discard_unit=3D%s"}, {Opt_memory_mode, "memory=3D%s"}, {Opt_age_extent_cache, "age_extent_cache"}, + {Opt_errors, "errors=3D%s"}, {Opt_err, NULL}, }; =20 @@ -1256,6 +1258,25 @@ static int parse_options(struct super_block *sb, cha= r *options, bool is_remount) case Opt_age_extent_cache: set_opt(sbi, AGE_EXTENT_CACHE); break; + case Opt_errors: + name =3D match_strdup(&args[0]); + if (!name) + return -ENOMEM; + if (!strcmp(name, "remount-ro")) { + F2FS_OPTION(sbi).errors =3D + MOUNT_ERRORS_READONLY; + } else if (!strcmp(name, "continue")) { + F2FS_OPTION(sbi).errors =3D + MOUNT_ERRORS_CONTINUE; + } else if (!strcmp(name, "panic")) { + F2FS_OPTION(sbi).errors =3D + MOUNT_ERRORS_PANIC; + } else { + kfree(name); + return -EINVAL; + } + kfree(name); + break; default: f2fs_err(sbi, "Unrecognized mount option \"%s\" or missing value", p); @@ -1610,6 +1631,9 @@ static void f2fs_put_super(struct super_block *sb) f2fs_destroy_node_manager(sbi); f2fs_destroy_segment_manager(sbi); =20 + /* flush s_error_work before sbi destroy */ + flush_work(&sbi->s_error_work); + f2fs_destroy_post_read_wq(sbi); =20 kvfree(sbi->ckpt); @@ -2040,6 +2064,13 @@ static int f2fs_show_options(struct seq_file *seq, s= truct dentry *root) else if (F2FS_OPTION(sbi).memory_mode =3D=3D MEMORY_MODE_LOW) seq_printf(seq, ",memory=3D%s", "low"); =20 + if (F2FS_OPTION(sbi).errors =3D=3D MOUNT_ERRORS_READONLY) + seq_printf(seq, ",errors=3D%s", "remount-ro"); + else if (F2FS_OPTION(sbi).errors =3D=3D MOUNT_ERRORS_CONTINUE) + seq_printf(seq, ",errors=3D%s", "continue"); + else if (F2FS_OPTION(sbi).errors =3D=3D MOUNT_ERRORS_PANIC) + seq_printf(seq, ",errors=3D%s", "panic"); + return 0; } =20 @@ -2066,6 +2097,7 @@ static void default_options(struct f2fs_sb_info *sbi) F2FS_OPTION(sbi).compress_mode =3D COMPR_MODE_FS; F2FS_OPTION(sbi).bggc_mode =3D BGGC_MODE_ON; F2FS_OPTION(sbi).memory_mode =3D MEMORY_MODE_NORMAL; + F2FS_OPTION(sbi).errors =3D MOUNT_ERRORS_READONLY; =20 sbi->sb->s_flags &=3D ~SB_INLINECRYPT; =20 @@ -2267,6 +2299,9 @@ static int f2fs_remount(struct super_block *sb, int *= flags, char *data) if (err) goto restore_opts; =20 + /* flush outstanding errors before changing fs state */ + flush_work(&sbi->s_error_work); + /* * Previous and new state of filesystem is RO, * so skip checking GC and FLUSH_MERGE conditions. @@ -3868,45 +3903,60 @@ int f2fs_commit_super(struct f2fs_sb_info *sbi, boo= l recover) return err; } =20 -void f2fs_handle_stop(struct f2fs_sb_info *sbi, unsigned char reason) +static void save_stop_reason(struct f2fs_sb_info *sbi, unsigned char reaso= n) +{ + unsigned long flags; + + spin_lock_irqsave(&sbi->error_lock, flags); + if (sbi->stop_reason[reason] < GENMASK(BITS_PER_BYTE - 1, 0)) + sbi->stop_reason[reason]++; + spin_unlock_irqrestore(&sbi->error_lock, flags); +} + +static void f2fs_record_stop_reason(struct f2fs_sb_info *sbi) { struct f2fs_super_block *raw_super =3D F2FS_RAW_SUPER(sbi); + unsigned long flags; int err; =20 f2fs_down_write(&sbi->sb_lock); =20 - if (raw_super->s_stop_reason[reason] < GENMASK(BITS_PER_BYTE - 1, 0)) - raw_super->s_stop_reason[reason]++; + spin_lock_irqsave(&sbi->error_lock, flags); + memcpy(raw_super->s_stop_reason, sbi->stop_reason, MAX_STOP_REASON); + spin_unlock_irqrestore(&sbi->error_lock, flags); =20 err =3D f2fs_commit_super(sbi, false); - if (err) - f2fs_err(sbi, "f2fs_commit_super fails to record reason:%u err:%d", - reason, err); + f2fs_up_write(&sbi->sb_lock); + if (err) + f2fs_err(sbi, "f2fs_commit_super fails to record err:%d", err); } =20 static void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag) { - spin_lock(&sbi->error_lock); + unsigned long flags; + + spin_lock_irqsave(&sbi->error_lock, flags); if (!test_bit(flag, (unsigned long *)sbi->errors)) { set_bit(flag, (unsigned long *)sbi->errors); sbi->error_dirty =3D true; } - spin_unlock(&sbi->error_lock); + spin_unlock_irqrestore(&sbi->error_lock, flags); } =20 static bool f2fs_update_errors(struct f2fs_sb_info *sbi) { + unsigned long flags; bool need_update =3D false; =20 - spin_lock(&sbi->error_lock); + spin_lock_irqsave(&sbi->error_lock, flags); if (sbi->error_dirty) { memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors, MAX_F2FS_ERRORS); sbi->error_dirty =3D false; need_update =3D true; } - spin_unlock(&sbi->error_lock); + spin_unlock_irqrestore(&sbi->error_lock, flags); =20 return need_update; } @@ -3930,6 +3980,66 @@ void f2fs_handle_error(struct f2fs_sb_info *sbi, uns= igned char error) f2fs_up_write(&sbi->sb_lock); } =20 +static bool system_going_down(void) +{ + return system_state =3D=3D SYSTEM_HALT || system_state =3D=3D SYSTEM_POWE= R_OFF + || system_state =3D=3D SYSTEM_RESTART; +} + +void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char re= ason, + bool irq_context) +{ + struct super_block *sb =3D sbi->sb; + bool shutdown =3D reason =3D=3D STOP_CP_REASON_SHUTDOWN; + bool continue_fs =3D !shutdown && + F2FS_OPTION(sbi).errors =3D=3D MOUNT_ERRORS_CONTINUE; + + set_ckpt_flags(sbi, CP_ERROR_FLAG); + + if (!bdev_read_only(sb->s_bdev)) { + save_stop_reason(sbi, reason); + + if (irq_context) + schedule_work(&sbi->s_error_work); + else + f2fs_record_stop_reason(sbi); + } + + /* + * We force ERRORS_RO behavior when system is rebooting. Otherwise we + * could panic during 'reboot -f' as the underlying device got already + * disabled. + */ + if (F2FS_OPTION(sbi).errors =3D=3D MOUNT_ERRORS_PANIC && + !shutdown && !system_going_down() && + !is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)) + panic("F2FS-fs (device %s): panic forced after error\n", + sb->s_id); + + if (shutdown) + set_sbi_flag(sbi, SBI_IS_SHUTDOWN); + + /* continue filesystem operators if errors=3Dcontinue */ + if (continue_fs || f2fs_readonly(sb)) + return; + + f2fs_warn(sbi, "Remounting filesystem read-only"); + /* + * Make sure updated value of ->s_mount_flags will be visible before + * ->s_flags update + */ + smp_wmb(); + sb->s_flags |=3D SB_RDONLY; +} + +static void f2fs_record_error_work(struct work_struct *work) +{ + struct f2fs_sb_info *sbi =3D container_of(work, + struct f2fs_sb_info, s_error_work); + + f2fs_record_stop_reason(sbi); +} + static int f2fs_scan_devices(struct f2fs_sb_info *sbi) { struct f2fs_super_block *raw_super =3D F2FS_RAW_SUPER(sbi); @@ -4157,7 +4267,9 @@ static int f2fs_fill_super(struct super_block *sb, vo= id *data, int silent) sb->s_fs_info =3D sbi; sbi->raw_super =3D raw_super; =20 + INIT_WORK(&sbi->s_error_work, f2fs_record_error_work); memcpy(sbi->errors, raw_super->s_errors, MAX_F2FS_ERRORS); + memcpy(sbi->stop_reason, raw_super->s_stop_reason, MAX_STOP_REASON); =20 /* precompute checksum seed for metadata */ if (f2fs_sb_has_inode_chksum(sbi)) @@ -4548,6 +4660,8 @@ static int f2fs_fill_super(struct super_block *sb, vo= id *data, int silent) f2fs_destroy_segment_manager(sbi); stop_ckpt_thread: f2fs_stop_ckpt_thread(sbi); + /* flush s_error_work before sbi destroy */ + flush_work(&sbi->s_error_work); f2fs_destroy_post_read_wq(sbi); free_devices: destroy_device_list(sbi); --=20 2.36.1