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,
        MEMORY_MODE_LOW,        /* memory mode for low memry devices */
 };
 
+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);
 
        struct workqueue_struct *post_read_wq;  /* post read workqueue */
 
-       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 */
 
        struct kmem_cache *inline_xattr_slab;   /* inline xattr entry */
 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_save_errors(struct f2fs_sb_info *sbi, unsigned char flag);
+void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason,
+                                                       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);
 
        Opt_discard_unit,
        Opt_memory_mode,
        Opt_age_extent_cache,
+       Opt_errors,
        Opt_err,
 };
 
        {Opt_discard_unit, "discard_unit=%s"},
        {Opt_memory_mode, "memory=%s"},
        {Opt_age_extent_cache, "age_extent_cache"},
+       {Opt_errors, "errors=%s"},
        {Opt_err, NULL},
 };
 
                case Opt_age_extent_cache:
                        set_opt(sbi, AGE_EXTENT_CACHE);
                        break;
+               case Opt_errors:
+                       name = match_strdup(&args[0]);
+                       if (!name)
+                               return -ENOMEM;
+                       if (!strcmp(name, "remount-ro")) {
+                               F2FS_OPTION(sbi).errors =
+                                               MOUNT_ERRORS_READONLY;
+                       } else if (!strcmp(name, "continue")) {
+                               F2FS_OPTION(sbi).errors =
+                                               MOUNT_ERRORS_CONTINUE;
+                       } else if (!strcmp(name, "panic")) {
+                               F2FS_OPTION(sbi).errors =
+                                               MOUNT_ERRORS_PANIC;
+                       } else {
+                               kfree(name);
+                               return -EINVAL;
+                       }
+                       kfree(name);
+                       break;
                default:
                        f2fs_err(sbi, "Unrecognized mount option \"%s\" or missing value",
                                 p);
        f2fs_destroy_node_manager(sbi);
        f2fs_destroy_segment_manager(sbi);
 
+       /* flush s_error_work before sbi destroy */
+       flush_work(&sbi->s_error_work);
+
        f2fs_destroy_post_read_wq(sbi);
 
        kvfree(sbi->ckpt);
        else if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW)
                seq_printf(seq, ",memory=%s", "low");
 
+       if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
+               seq_printf(seq, ",errors=%s", "remount-ro");
+       else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE)
+               seq_printf(seq, ",errors=%s", "continue");
+       else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC)
+               seq_printf(seq, ",errors=%s", "panic");
+
        return 0;
 }
 
        }
        F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
        F2FS_OPTION(sbi).memory_mode = MEMORY_MODE_NORMAL;
+       F2FS_OPTION(sbi).errors = MOUNT_ERRORS_CONTINUE;
 
        sbi->sb->s_flags &= ~SB_INLINECRYPT;
 
        if (err)
                goto restore_opts;
 
+       /* 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.
        return err;
 }
 
-void f2fs_handle_stop(struct f2fs_sb_info *sbi, unsigned char reason)
+static void save_stop_reason(struct f2fs_sb_info *sbi, unsigned char reason)
+{
+       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 = F2FS_RAW_SUPER(sbi);
+       unsigned long flags;
        int err;
 
        f2fs_down_write(&sbi->sb_lock);
 
-       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);
 
        err = 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);
 }
 
 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 = true;
        }
-       spin_unlock(&sbi->error_lock);
+       spin_unlock_irqrestore(&sbi->error_lock, flags);
 }
 
 static bool f2fs_update_errors(struct f2fs_sb_info *sbi)
 {
+       unsigned long flags;
        bool need_update = false;
 
-       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 = false;
                need_update = true;
        }
-       spin_unlock(&sbi->error_lock);
+       spin_unlock_irqrestore(&sbi->error_lock, flags);
 
        return need_update;
 }
        f2fs_up_write(&sbi->sb_lock);
 }
 
+static bool system_going_down(void)
+{
+       return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
+               || system_state == SYSTEM_RESTART;
+}
+
+void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason,
+                                                       bool irq_context)
+{
+       struct super_block *sb = sbi->sb;
+       bool shutdown = reason == STOP_CP_REASON_SHUTDOWN;
+       bool continue_fs = !shutdown &&
+                       F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE;
+
+       set_ckpt_flags(sbi, CP_ERROR_FLAG);
+
+       if (!f2fs_hw_is_readonly(sbi)) {
+               save_stop_reason(sbi, reason);
+
+               if (irq_context && !shutdown)
+                       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 == 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=continue */
+       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 |= SB_RDONLY;
+}
+
+static void f2fs_record_error_work(struct work_struct *work)
+{
+       struct f2fs_sb_info *sbi = 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 = F2FS_RAW_SUPER(sbi);
        sb->s_fs_info = sbi;
        sbi->raw_super = raw_super;
 
+       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);
 
        /* precompute checksum seed for metadata */
        if (f2fs_sb_has_inode_chksum(sbi))
        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);