LCOV - code coverage report
Current view: top level - fs/ext4 - super.c (source / functions) Hit Total Coverage
Test: Real Lines: 872 2510 34.7 %
Date: 2020-10-17 15:46:16 Functions: 3 126 2.4 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0
       2                 :            : /*
       3                 :            :  *  linux/fs/ext4/super.c
       4                 :            :  *
       5                 :            :  * Copyright (C) 1992, 1993, 1994, 1995
       6                 :            :  * Remy Card (card@masi.ibp.fr)
       7                 :            :  * Laboratoire MASI - Institut Blaise Pascal
       8                 :            :  * Universite Pierre et Marie Curie (Paris VI)
       9                 :            :  *
      10                 :            :  *  from
      11                 :            :  *
      12                 :            :  *  linux/fs/minix/inode.c
      13                 :            :  *
      14                 :            :  *  Copyright (C) 1991, 1992  Linus Torvalds
      15                 :            :  *
      16                 :            :  *  Big-endian to little-endian byte-swapping/bitmaps by
      17                 :            :  *        David S. Miller (davem@caip.rutgers.edu), 1995
      18                 :            :  */
      19                 :            : 
      20                 :            : #include <linux/module.h>
      21                 :            : #include <linux/string.h>
      22                 :            : #include <linux/fs.h>
      23                 :            : #include <linux/time.h>
      24                 :            : #include <linux/vmalloc.h>
      25                 :            : #include <linux/slab.h>
      26                 :            : #include <linux/init.h>
      27                 :            : #include <linux/blkdev.h>
      28                 :            : #include <linux/backing-dev.h>
      29                 :            : #include <linux/parser.h>
      30                 :            : #include <linux/buffer_head.h>
      31                 :            : #include <linux/exportfs.h>
      32                 :            : #include <linux/vfs.h>
      33                 :            : #include <linux/random.h>
      34                 :            : #include <linux/mount.h>
      35                 :            : #include <linux/namei.h>
      36                 :            : #include <linux/quotaops.h>
      37                 :            : #include <linux/seq_file.h>
      38                 :            : #include <linux/ctype.h>
      39                 :            : #include <linux/log2.h>
      40                 :            : #include <linux/crc16.h>
      41                 :            : #include <linux/dax.h>
      42                 :            : #include <linux/cleancache.h>
      43                 :            : #include <linux/uaccess.h>
      44                 :            : #include <linux/iversion.h>
      45                 :            : #include <linux/unicode.h>
      46                 :            : 
      47                 :            : #include <linux/kthread.h>
      48                 :            : #include <linux/freezer.h>
      49                 :            : 
      50                 :            : #include "ext4.h"
      51                 :            : #include "ext4_extents.h"     /* Needed for trace points definition */
      52                 :            : #include "ext4_jbd2.h"
      53                 :            : #include "xattr.h"
      54                 :            : #include "acl.h"
      55                 :            : #include "mballoc.h"
      56                 :            : #include "fsmap.h"
      57                 :            : 
      58                 :            : #define CREATE_TRACE_POINTS
      59                 :            : #include <trace/events/ext4.h>
      60                 :            : 
      61                 :            : static struct ext4_lazy_init *ext4_li_info;
      62                 :            : static struct mutex ext4_li_mtx;
      63                 :            : static struct ratelimit_state ext4_mount_msg_ratelimit;
      64                 :            : 
      65                 :            : static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
      66                 :            :                              unsigned long journal_devnum);
      67                 :            : static int ext4_show_options(struct seq_file *seq, struct dentry *root);
      68                 :            : static int ext4_commit_super(struct super_block *sb, int sync);
      69                 :            : static void ext4_mark_recovery_complete(struct super_block *sb,
      70                 :            :                                         struct ext4_super_block *es);
      71                 :            : static void ext4_clear_journal_err(struct super_block *sb,
      72                 :            :                                    struct ext4_super_block *es);
      73                 :            : static int ext4_sync_fs(struct super_block *sb, int wait);
      74                 :            : static int ext4_remount(struct super_block *sb, int *flags, char *data);
      75                 :            : static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
      76                 :            : static int ext4_unfreeze(struct super_block *sb);
      77                 :            : static int ext4_freeze(struct super_block *sb);
      78                 :            : static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
      79                 :            :                        const char *dev_name, void *data);
      80                 :            : static inline int ext2_feature_set_ok(struct super_block *sb);
      81                 :            : static inline int ext3_feature_set_ok(struct super_block *sb);
      82                 :            : static int ext4_feature_set_ok(struct super_block *sb, int readonly);
      83                 :            : static void ext4_destroy_lazyinit_thread(void);
      84                 :            : static void ext4_unregister_li_request(struct super_block *sb);
      85                 :            : static void ext4_clear_request_list(void);
      86                 :            : static struct inode *ext4_get_journal_inode(struct super_block *sb,
      87                 :            :                                             unsigned int journal_inum);
      88                 :            : 
      89                 :            : /*
      90                 :            :  * Lock ordering
      91                 :            :  *
      92                 :            :  * Note the difference between i_mmap_sem (EXT4_I(inode)->i_mmap_sem) and
      93                 :            :  * i_mmap_rwsem (inode->i_mmap_rwsem)!
      94                 :            :  *
      95                 :            :  * page fault path:
      96                 :            :  * mmap_sem -> sb_start_pagefault -> i_mmap_sem (r) -> transaction start ->
      97                 :            :  *   page lock -> i_data_sem (rw)
      98                 :            :  *
      99                 :            :  * buffered write path:
     100                 :            :  * sb_start_write -> i_mutex -> mmap_sem
     101                 :            :  * sb_start_write -> i_mutex -> transaction start -> page lock ->
     102                 :            :  *   i_data_sem (rw)
     103                 :            :  *
     104                 :            :  * truncate:
     105                 :            :  * sb_start_write -> i_mutex -> i_mmap_sem (w) -> i_mmap_rwsem (w) -> page lock
     106                 :            :  * sb_start_write -> i_mutex -> i_mmap_sem (w) -> transaction start ->
     107                 :            :  *   i_data_sem (rw)
     108                 :            :  *
     109                 :            :  * direct IO:
     110                 :            :  * sb_start_write -> i_mutex -> mmap_sem
     111                 :            :  * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
     112                 :            :  *
     113                 :            :  * writepages:
     114                 :            :  * transaction start -> page lock(s) -> i_data_sem (rw)
     115                 :            :  */
     116                 :            : 
     117                 :            : #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
     118                 :            : static struct file_system_type ext2_fs_type = {
     119                 :            :         .owner          = THIS_MODULE,
     120                 :            :         .name           = "ext2",
     121                 :            :         .mount          = ext4_mount,
     122                 :            :         .kill_sb        = kill_block_super,
     123                 :            :         .fs_flags       = FS_REQUIRES_DEV,
     124                 :            : };
     125                 :            : MODULE_ALIAS_FS("ext2");
     126                 :            : MODULE_ALIAS("ext2");
     127                 :            : #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
     128                 :            : #else
     129                 :            : #define IS_EXT2_SB(sb) (0)
     130                 :            : #endif
     131                 :            : 
     132                 :            : 
     133                 :            : static struct file_system_type ext3_fs_type = {
     134                 :            :         .owner          = THIS_MODULE,
     135                 :            :         .name           = "ext3",
     136                 :            :         .mount          = ext4_mount,
     137                 :            :         .kill_sb        = kill_block_super,
     138                 :            :         .fs_flags       = FS_REQUIRES_DEV,
     139                 :            : };
     140                 :            : MODULE_ALIAS_FS("ext3");
     141                 :            : MODULE_ALIAS("ext3");
     142                 :            : #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
     143                 :            : 
     144                 :            : /*
     145                 :            :  * This works like sb_bread() except it uses ERR_PTR for error
     146                 :            :  * returns.  Currently with sb_bread it's impossible to distinguish
     147                 :            :  * between ENOMEM and EIO situations (since both result in a NULL
     148                 :            :  * return.
     149                 :            :  */
     150                 :            : struct buffer_head *
     151                 :          0 : ext4_sb_bread(struct super_block *sb, sector_t block, int op_flags)
     152                 :            : {
     153                 :          0 :         struct buffer_head *bh = sb_getblk(sb, block);
     154                 :            : 
     155                 :          0 :         if (bh == NULL)
     156                 :            :                 return ERR_PTR(-ENOMEM);
     157                 :          0 :         if (buffer_uptodate(bh))
     158                 :          0 :                 return bh;
     159                 :          0 :         ll_rw_block(REQ_OP_READ, REQ_META | op_flags, 1, &bh);
     160                 :          0 :         wait_on_buffer(bh);
     161                 :          0 :         if (buffer_uptodate(bh))
     162                 :          0 :                 return bh;
     163                 :          0 :         put_bh(bh);
     164                 :          0 :         return ERR_PTR(-EIO);
     165                 :            : }
     166                 :            : 
     167                 :            : static int ext4_verify_csum_type(struct super_block *sb,
     168                 :            :                                  struct ext4_super_block *es)
     169                 :            : {
     170                 :          3 :         if (!ext4_has_feature_metadata_csum(sb))
     171                 :            :                 return 1;
     172                 :            : 
     173                 :          0 :         return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
     174                 :            : }
     175                 :            : 
     176                 :            : static __le32 ext4_superblock_csum(struct super_block *sb,
     177                 :            :                                    struct ext4_super_block *es)
     178                 :            : {
     179                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
     180                 :            :         int offset = offsetof(struct ext4_super_block, s_checksum);
     181                 :            :         __u32 csum;
     182                 :            : 
     183                 :          0 :         csum = ext4_chksum(sbi, ~0, (char *)es, offset);
     184                 :            : 
     185                 :            :         return cpu_to_le32(csum);
     186                 :            : }
     187                 :            : 
     188                 :          3 : static int ext4_superblock_csum_verify(struct super_block *sb,
     189                 :            :                                        struct ext4_super_block *es)
     190                 :            : {
     191                 :          3 :         if (!ext4_has_metadata_csum(sb))
     192                 :            :                 return 1;
     193                 :            : 
     194                 :          0 :         return es->s_checksum == ext4_superblock_csum(sb, es);
     195                 :            : }
     196                 :            : 
     197                 :          3 : void ext4_superblock_csum_set(struct super_block *sb)
     198                 :            : {
     199                 :          3 :         struct ext4_super_block *es = EXT4_SB(sb)->s_es;
     200                 :            : 
     201                 :          3 :         if (!ext4_has_metadata_csum(sb))
     202                 :          3 :                 return;
     203                 :            : 
     204                 :          0 :         es->s_checksum = ext4_superblock_csum(sb, es);
     205                 :            : }
     206                 :            : 
     207                 :          0 : void *ext4_kvmalloc(size_t size, gfp_t flags)
     208                 :            : {
     209                 :            :         void *ret;
     210                 :            : 
     211                 :          0 :         ret = kmalloc(size, flags | __GFP_NOWARN);
     212                 :          0 :         if (!ret)
     213                 :          0 :                 ret = __vmalloc(size, flags, PAGE_KERNEL);
     214                 :          0 :         return ret;
     215                 :            : }
     216                 :            : 
     217                 :          0 : void *ext4_kvzalloc(size_t size, gfp_t flags)
     218                 :            : {
     219                 :            :         void *ret;
     220                 :            : 
     221                 :          0 :         ret = kzalloc(size, flags | __GFP_NOWARN);
     222                 :          0 :         if (!ret)
     223                 :          0 :                 ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
     224                 :          0 :         return ret;
     225                 :            : }
     226                 :            : 
     227                 :          3 : ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
     228                 :            :                                struct ext4_group_desc *bg)
     229                 :            : {
     230                 :          3 :         return le32_to_cpu(bg->bg_block_bitmap_lo) |
     231                 :          3 :                 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
     232                 :          3 :                  (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
     233                 :            : }
     234                 :            : 
     235                 :          3 : ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
     236                 :            :                                struct ext4_group_desc *bg)
     237                 :            : {
     238                 :          3 :         return le32_to_cpu(bg->bg_inode_bitmap_lo) |
     239                 :          3 :                 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
     240                 :          3 :                  (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
     241                 :            : }
     242                 :            : 
     243                 :          3 : ext4_fsblk_t ext4_inode_table(struct super_block *sb,
     244                 :            :                               struct ext4_group_desc *bg)
     245                 :            : {
     246                 :          3 :         return le32_to_cpu(bg->bg_inode_table_lo) |
     247                 :          3 :                 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
     248                 :          3 :                  (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
     249                 :            : }
     250                 :            : 
     251                 :          3 : __u32 ext4_free_group_clusters(struct super_block *sb,
     252                 :            :                                struct ext4_group_desc *bg)
     253                 :            : {
     254                 :          3 :         return le16_to_cpu(bg->bg_free_blocks_count_lo) |
     255                 :          3 :                 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
     256                 :          3 :                  (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
     257                 :            : }
     258                 :            : 
     259                 :          3 : __u32 ext4_free_inodes_count(struct super_block *sb,
     260                 :            :                               struct ext4_group_desc *bg)
     261                 :            : {
     262                 :          3 :         return le16_to_cpu(bg->bg_free_inodes_count_lo) |
     263                 :          3 :                 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
     264                 :          3 :                  (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
     265                 :            : }
     266                 :            : 
     267                 :          3 : __u32 ext4_used_dirs_count(struct super_block *sb,
     268                 :            :                               struct ext4_group_desc *bg)
     269                 :            : {
     270                 :          3 :         return le16_to_cpu(bg->bg_used_dirs_count_lo) |
     271                 :          3 :                 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
     272                 :          3 :                  (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
     273                 :            : }
     274                 :            : 
     275                 :          0 : __u32 ext4_itable_unused_count(struct super_block *sb,
     276                 :            :                               struct ext4_group_desc *bg)
     277                 :            : {
     278                 :          0 :         return le16_to_cpu(bg->bg_itable_unused_lo) |
     279                 :          0 :                 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
     280                 :          0 :                  (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
     281                 :            : }
     282                 :            : 
     283                 :          0 : void ext4_block_bitmap_set(struct super_block *sb,
     284                 :            :                            struct ext4_group_desc *bg, ext4_fsblk_t blk)
     285                 :            : {
     286                 :          0 :         bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
     287                 :          0 :         if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
     288                 :          0 :                 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
     289                 :          0 : }
     290                 :            : 
     291                 :          0 : void ext4_inode_bitmap_set(struct super_block *sb,
     292                 :            :                            struct ext4_group_desc *bg, ext4_fsblk_t blk)
     293                 :            : {
     294                 :          0 :         bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
     295                 :          0 :         if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
     296                 :          0 :                 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
     297                 :          0 : }
     298                 :            : 
     299                 :          0 : void ext4_inode_table_set(struct super_block *sb,
     300                 :            :                           struct ext4_group_desc *bg, ext4_fsblk_t blk)
     301                 :            : {
     302                 :          0 :         bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
     303                 :          0 :         if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
     304                 :          0 :                 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
     305                 :          0 : }
     306                 :            : 
     307                 :          3 : void ext4_free_group_clusters_set(struct super_block *sb,
     308                 :            :                                   struct ext4_group_desc *bg, __u32 count)
     309                 :            : {
     310                 :          3 :         bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
     311                 :          3 :         if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
     312                 :          0 :                 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
     313                 :          3 : }
     314                 :            : 
     315                 :          3 : void ext4_free_inodes_set(struct super_block *sb,
     316                 :            :                           struct ext4_group_desc *bg, __u32 count)
     317                 :            : {
     318                 :          3 :         bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
     319                 :          3 :         if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
     320                 :          0 :                 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
     321                 :          3 : }
     322                 :            : 
     323                 :          3 : void ext4_used_dirs_set(struct super_block *sb,
     324                 :            :                           struct ext4_group_desc *bg, __u32 count)
     325                 :            : {
     326                 :          3 :         bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
     327                 :          3 :         if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
     328                 :          0 :                 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
     329                 :          3 : }
     330                 :            : 
     331                 :          0 : void ext4_itable_unused_set(struct super_block *sb,
     332                 :            :                           struct ext4_group_desc *bg, __u32 count)
     333                 :            : {
     334                 :          0 :         bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
     335                 :          0 :         if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
     336                 :          0 :                 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
     337                 :          0 : }
     338                 :            : 
     339                 :            : static void __ext4_update_tstamp(__le32 *lo, __u8 *hi)
     340                 :            : {
     341                 :          3 :         time64_t now = ktime_get_real_seconds();
     342                 :            : 
     343                 :          3 :         now = clamp_val(now, 0, (1ull << 40) - 1);
     344                 :            : 
     345                 :          3 :         *lo = cpu_to_le32(lower_32_bits(now));
     346                 :          3 :         *hi = upper_32_bits(now);
     347                 :            : }
     348                 :            : 
     349                 :            : static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
     350                 :            : {
     351                 :          0 :         return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo);
     352                 :            : }
     353                 :            : #define ext4_update_tstamp(es, tstamp) \
     354                 :            :         __ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
     355                 :            : #define ext4_get_tstamp(es, tstamp) \
     356                 :            :         __ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
     357                 :            : 
     358                 :          0 : static void __save_error_info(struct super_block *sb, const char *func,
     359                 :            :                             unsigned int line)
     360                 :            : {
     361                 :          0 :         struct ext4_super_block *es = EXT4_SB(sb)->s_es;
     362                 :            : 
     363                 :          0 :         EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
     364                 :          0 :         if (bdev_read_only(sb->s_bdev))
     365                 :          0 :                 return;
     366                 :          0 :         es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
     367                 :            :         ext4_update_tstamp(es, s_last_error_time);
     368                 :          0 :         strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
     369                 :          0 :         es->s_last_error_line = cpu_to_le32(line);
     370                 :          0 :         if (!es->s_first_error_time) {
     371                 :          0 :                 es->s_first_error_time = es->s_last_error_time;
     372                 :          0 :                 es->s_first_error_time_hi = es->s_last_error_time_hi;
     373                 :          0 :                 strncpy(es->s_first_error_func, func,
     374                 :            :                         sizeof(es->s_first_error_func));
     375                 :          0 :                 es->s_first_error_line = cpu_to_le32(line);
     376                 :          0 :                 es->s_first_error_ino = es->s_last_error_ino;
     377                 :          0 :                 es->s_first_error_block = es->s_last_error_block;
     378                 :            :         }
     379                 :            :         /*
     380                 :            :          * Start the daily error reporting function if it hasn't been
     381                 :            :          * started already
     382                 :            :          */
     383                 :          0 :         if (!es->s_error_count)
     384                 :          0 :                 mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
     385                 :            :         le32_add_cpu(&es->s_error_count, 1);
     386                 :            : }
     387                 :            : 
     388                 :          0 : static void save_error_info(struct super_block *sb, const char *func,
     389                 :            :                             unsigned int line)
     390                 :            : {
     391                 :          0 :         __save_error_info(sb, func, line);
     392                 :          0 :         if (!bdev_read_only(sb->s_bdev))
     393                 :          0 :                 ext4_commit_super(sb, 1);
     394                 :          0 : }
     395                 :            : 
     396                 :            : /*
     397                 :            :  * The del_gendisk() function uninitializes the disk-specific data
     398                 :            :  * structures, including the bdi structure, without telling anyone
     399                 :            :  * else.  Once this happens, any attempt to call mark_buffer_dirty()
     400                 :            :  * (for example, by ext4_commit_super), will cause a kernel OOPS.
     401                 :            :  * This is a kludge to prevent these oops until we can put in a proper
     402                 :            :  * hook in del_gendisk() to inform the VFS and file system layers.
     403                 :            :  */
     404                 :            : static int block_device_ejected(struct super_block *sb)
     405                 :            : {
     406                 :          3 :         struct inode *bd_inode = sb->s_bdev->bd_inode;
     407                 :          3 :         struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
     408                 :            : 
     409                 :          3 :         return bdi->dev == NULL;
     410                 :            : }
     411                 :            : 
     412                 :          3 : static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
     413                 :            : {
     414                 :          3 :         struct super_block              *sb = journal->j_private;
     415                 :            :         struct ext4_sb_info             *sbi = EXT4_SB(sb);
     416                 :            :         int                             error = is_journal_aborted(journal);
     417                 :            :         struct ext4_journal_cb_entry    *jce;
     418                 :            : 
     419                 :          3 :         BUG_ON(txn->t_state == T_FINISHED);
     420                 :            : 
     421                 :          3 :         ext4_process_freed_data(sb, txn->t_tid);
     422                 :            : 
     423                 :            :         spin_lock(&sbi->s_md_lock);
     424                 :          3 :         while (!list_empty(&txn->t_private_list)) {
     425                 :          0 :                 jce = list_entry(txn->t_private_list.next,
     426                 :            :                                  struct ext4_journal_cb_entry, jce_list);
     427                 :          0 :                 list_del_init(&jce->jce_list);
     428                 :            :                 spin_unlock(&sbi->s_md_lock);
     429                 :          0 :                 jce->jce_func(sb, jce, error);
     430                 :            :                 spin_lock(&sbi->s_md_lock);
     431                 :            :         }
     432                 :            :         spin_unlock(&sbi->s_md_lock);
     433                 :          3 : }
     434                 :            : 
     435                 :            : static bool system_going_down(void)
     436                 :            : {
     437                 :            :         return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
     438                 :          0 :                 || system_state == SYSTEM_RESTART;
     439                 :            : }
     440                 :            : 
     441                 :            : /* Deal with the reporting of failure conditions on a filesystem such as
     442                 :            :  * inconsistencies detected or read IO failures.
     443                 :            :  *
     444                 :            :  * On ext2, we can store the error state of the filesystem in the
     445                 :            :  * superblock.  That is not possible on ext4, because we may have other
     446                 :            :  * write ordering constraints on the superblock which prevent us from
     447                 :            :  * writing it out straight away; and given that the journal is about to
     448                 :            :  * be aborted, we can't rely on the current, or future, transactions to
     449                 :            :  * write out the superblock safely.
     450                 :            :  *
     451                 :            :  * We'll just use the jbd2_journal_abort() error code to record an error in
     452                 :            :  * the journal instead.  On recovery, the journal will complain about
     453                 :            :  * that error until we've noted it down and cleared it.
     454                 :            :  */
     455                 :            : 
     456                 :          0 : static void ext4_handle_error(struct super_block *sb)
     457                 :            : {
     458                 :          0 :         if (test_opt(sb, WARN_ON_ERROR))
     459                 :          0 :                 WARN_ON_ONCE(1);
     460                 :            : 
     461                 :          0 :         if (sb_rdonly(sb))
     462                 :            :                 return;
     463                 :            : 
     464                 :          0 :         if (!test_opt(sb, ERRORS_CONT)) {
     465                 :          0 :                 journal_t *journal = EXT4_SB(sb)->s_journal;
     466                 :            : 
     467                 :          0 :                 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
     468                 :          0 :                 if (journal)
     469                 :          0 :                         jbd2_journal_abort(journal, -EIO);
     470                 :            :         }
     471                 :            :         /*
     472                 :            :          * We force ERRORS_RO behavior when system is rebooting. Otherwise we
     473                 :            :          * could panic during 'reboot -f' as the underlying device got already
     474                 :            :          * disabled.
     475                 :            :          */
     476                 :          0 :         if (test_opt(sb, ERRORS_RO) || system_going_down()) {
     477                 :          0 :                 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
     478                 :            :                 /*
     479                 :            :                  * Make sure updated value of ->s_mount_flags will be visible
     480                 :            :                  * before ->s_flags update
     481                 :            :                  */
     482                 :          0 :                 smp_wmb();
     483                 :          0 :                 sb->s_flags |= SB_RDONLY;
     484                 :          0 :         } else if (test_opt(sb, ERRORS_PANIC)) {
     485                 :          0 :                 if (EXT4_SB(sb)->s_journal &&
     486                 :          0 :                   !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
     487                 :            :                         return;
     488                 :          0 :                 panic("EXT4-fs (device %s): panic forced after error\n",
     489                 :          0 :                         sb->s_id);
     490                 :            :         }
     491                 :            : }
     492                 :            : 
     493                 :            : #define ext4_error_ratelimit(sb)                                        \
     494                 :            :                 ___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),      \
     495                 :            :                              "EXT4-fs error")
     496                 :            : 
     497                 :          0 : void __ext4_error(struct super_block *sb, const char *function,
     498                 :            :                   unsigned int line, const char *fmt, ...)
     499                 :            : {
     500                 :            :         struct va_format vaf;
     501                 :            :         va_list args;
     502                 :            : 
     503                 :          0 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
     504                 :          0 :                 return;
     505                 :            : 
     506                 :          0 :         trace_ext4_error(sb, function, line);
     507                 :          0 :         if (ext4_error_ratelimit(sb)) {
     508                 :          0 :                 va_start(args, fmt);
     509                 :          0 :                 vaf.fmt = fmt;
     510                 :          0 :                 vaf.va = &args;
     511                 :          0 :                 printk(KERN_CRIT
     512                 :            :                        "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
     513                 :          0 :                        sb->s_id, function, line, current->comm, &vaf);
     514                 :          0 :                 va_end(args);
     515                 :            :         }
     516                 :          0 :         save_error_info(sb, function, line);
     517                 :          0 :         ext4_handle_error(sb);
     518                 :            : }
     519                 :            : 
     520                 :          0 : void __ext4_error_inode(struct inode *inode, const char *function,
     521                 :            :                         unsigned int line, ext4_fsblk_t block,
     522                 :            :                         const char *fmt, ...)
     523                 :            : {
     524                 :            :         va_list args;
     525                 :            :         struct va_format vaf;
     526                 :          0 :         struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
     527                 :            : 
     528                 :          0 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
     529                 :          0 :                 return;
     530                 :            : 
     531                 :          0 :         trace_ext4_error(inode->i_sb, function, line);
     532                 :          0 :         es->s_last_error_ino = cpu_to_le32(inode->i_ino);
     533                 :          0 :         es->s_last_error_block = cpu_to_le64(block);
     534                 :          0 :         if (ext4_error_ratelimit(inode->i_sb)) {
     535                 :          0 :                 va_start(args, fmt);
     536                 :          0 :                 vaf.fmt = fmt;
     537                 :          0 :                 vaf.va = &args;
     538                 :          0 :                 if (block)
     539                 :          0 :                         printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
     540                 :            :                                "inode #%lu: block %llu: comm %s: %pV\n",
     541                 :          0 :                                inode->i_sb->s_id, function, line, inode->i_ino,
     542                 :          0 :                                block, current->comm, &vaf);
     543                 :            :                 else
     544                 :          0 :                         printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
     545                 :            :                                "inode #%lu: comm %s: %pV\n",
     546                 :          0 :                                inode->i_sb->s_id, function, line, inode->i_ino,
     547                 :          0 :                                current->comm, &vaf);
     548                 :          0 :                 va_end(args);
     549                 :            :         }
     550                 :          0 :         save_error_info(inode->i_sb, function, line);
     551                 :          0 :         ext4_handle_error(inode->i_sb);
     552                 :            : }
     553                 :            : 
     554                 :          0 : void __ext4_error_file(struct file *file, const char *function,
     555                 :            :                        unsigned int line, ext4_fsblk_t block,
     556                 :            :                        const char *fmt, ...)
     557                 :            : {
     558                 :            :         va_list args;
     559                 :            :         struct va_format vaf;
     560                 :            :         struct ext4_super_block *es;
     561                 :            :         struct inode *inode = file_inode(file);
     562                 :            :         char pathname[80], *path;
     563                 :            : 
     564                 :          0 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
     565                 :          0 :                 return;
     566                 :            : 
     567                 :          0 :         trace_ext4_error(inode->i_sb, function, line);
     568                 :          0 :         es = EXT4_SB(inode->i_sb)->s_es;
     569                 :          0 :         es->s_last_error_ino = cpu_to_le32(inode->i_ino);
     570                 :          0 :         if (ext4_error_ratelimit(inode->i_sb)) {
     571                 :          0 :                 path = file_path(file, pathname, sizeof(pathname));
     572                 :          0 :                 if (IS_ERR(path))
     573                 :            :                         path = "(unknown)";
     574                 :          0 :                 va_start(args, fmt);
     575                 :          0 :                 vaf.fmt = fmt;
     576                 :          0 :                 vaf.va = &args;
     577                 :          0 :                 if (block)
     578                 :          0 :                         printk(KERN_CRIT
     579                 :            :                                "EXT4-fs error (device %s): %s:%d: inode #%lu: "
     580                 :            :                                "block %llu: comm %s: path %s: %pV\n",
     581                 :          0 :                                inode->i_sb->s_id, function, line, inode->i_ino,
     582                 :          0 :                                block, current->comm, path, &vaf);
     583                 :            :                 else
     584                 :          0 :                         printk(KERN_CRIT
     585                 :            :                                "EXT4-fs error (device %s): %s:%d: inode #%lu: "
     586                 :            :                                "comm %s: path %s: %pV\n",
     587                 :          0 :                                inode->i_sb->s_id, function, line, inode->i_ino,
     588                 :          0 :                                current->comm, path, &vaf);
     589                 :          0 :                 va_end(args);
     590                 :            :         }
     591                 :          0 :         save_error_info(inode->i_sb, function, line);
     592                 :          0 :         ext4_handle_error(inode->i_sb);
     593                 :            : }
     594                 :            : 
     595                 :          0 : const char *ext4_decode_error(struct super_block *sb, int errno,
     596                 :            :                               char nbuf[16])
     597                 :            : {
     598                 :            :         char *errstr = NULL;
     599                 :            : 
     600                 :          0 :         switch (errno) {
     601                 :            :         case -EFSCORRUPTED:
     602                 :            :                 errstr = "Corrupt filesystem";
     603                 :            :                 break;
     604                 :            :         case -EFSBADCRC:
     605                 :            :                 errstr = "Filesystem failed CRC";
     606                 :          0 :                 break;
     607                 :            :         case -EIO:
     608                 :            :                 errstr = "IO failure";
     609                 :          0 :                 break;
     610                 :            :         case -ENOMEM:
     611                 :            :                 errstr = "Out of memory";
     612                 :          0 :                 break;
     613                 :            :         case -EROFS:
     614                 :          0 :                 if (!sb || (EXT4_SB(sb)->s_journal &&
     615                 :          0 :                             EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
     616                 :            :                         errstr = "Journal has aborted";
     617                 :            :                 else
     618                 :            :                         errstr = "Readonly filesystem";
     619                 :            :                 break;
     620                 :            :         default:
     621                 :            :                 /* If the caller passed in an extra buffer for unknown
     622                 :            :                  * errors, textualise them now.  Else we just return
     623                 :            :                  * NULL. */
     624                 :          0 :                 if (nbuf) {
     625                 :            :                         /* Check for truncated error codes... */
     626                 :          0 :                         if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
     627                 :            :                                 errstr = nbuf;
     628                 :            :                 }
     629                 :            :                 break;
     630                 :            :         }
     631                 :            : 
     632                 :          0 :         return errstr;
     633                 :            : }
     634                 :            : 
     635                 :            : /* __ext4_std_error decodes expected errors from journaling functions
     636                 :            :  * automatically and invokes the appropriate error response.  */
     637                 :            : 
     638                 :          0 : void __ext4_std_error(struct super_block *sb, const char *function,
     639                 :            :                       unsigned int line, int errno)
     640                 :            : {
     641                 :            :         char nbuf[16];
     642                 :            :         const char *errstr;
     643                 :            : 
     644                 :          0 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
     645                 :          0 :                 return;
     646                 :            : 
     647                 :            :         /* Special case: if the error is EROFS, and we're not already
     648                 :            :          * inside a transaction, then there's really no point in logging
     649                 :            :          * an error. */
     650                 :          0 :         if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
     651                 :            :                 return;
     652                 :            : 
     653                 :          0 :         if (ext4_error_ratelimit(sb)) {
     654                 :          0 :                 errstr = ext4_decode_error(sb, errno, nbuf);
     655                 :          0 :                 printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
     656                 :          0 :                        sb->s_id, function, line, errstr);
     657                 :            :         }
     658                 :            : 
     659                 :          0 :         save_error_info(sb, function, line);
     660                 :          0 :         ext4_handle_error(sb);
     661                 :            : }
     662                 :            : 
     663                 :            : /*
     664                 :            :  * ext4_abort is a much stronger failure handler than ext4_error.  The
     665                 :            :  * abort function may be used to deal with unrecoverable failures such
     666                 :            :  * as journal IO errors or ENOMEM at a critical moment in log management.
     667                 :            :  *
     668                 :            :  * We unconditionally force the filesystem into an ABORT|READONLY state,
     669                 :            :  * unless the error response on the fs has been set to panic in which
     670                 :            :  * case we take the easy way out and panic immediately.
     671                 :            :  */
     672                 :            : 
     673                 :          0 : void __ext4_abort(struct super_block *sb, const char *function,
     674                 :            :                 unsigned int line, const char *fmt, ...)
     675                 :            : {
     676                 :            :         struct va_format vaf;
     677                 :            :         va_list args;
     678                 :            : 
     679                 :          0 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
     680                 :          0 :                 return;
     681                 :            : 
     682                 :          0 :         save_error_info(sb, function, line);
     683                 :          0 :         va_start(args, fmt);
     684                 :          0 :         vaf.fmt = fmt;
     685                 :          0 :         vaf.va = &args;
     686                 :          0 :         printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n",
     687                 :          0 :                sb->s_id, function, line, &vaf);
     688                 :          0 :         va_end(args);
     689                 :            : 
     690                 :          0 :         if (sb_rdonly(sb) == 0) {
     691                 :          0 :                 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
     692                 :          0 :                 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
     693                 :            :                 /*
     694                 :            :                  * Make sure updated value of ->s_mount_flags will be visible
     695                 :            :                  * before ->s_flags update
     696                 :            :                  */
     697                 :          0 :                 smp_wmb();
     698                 :          0 :                 sb->s_flags |= SB_RDONLY;
     699                 :          0 :                 if (EXT4_SB(sb)->s_journal)
     700                 :          0 :                         jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
     701                 :          0 :                 save_error_info(sb, function, line);
     702                 :            :         }
     703                 :          0 :         if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) {
     704                 :          0 :                 if (EXT4_SB(sb)->s_journal &&
     705                 :          0 :                   !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
     706                 :            :                         return;
     707                 :          0 :                 panic("EXT4-fs panic from previous error\n");
     708                 :            :         }
     709                 :            : }
     710                 :            : 
     711                 :          3 : void __ext4_msg(struct super_block *sb,
     712                 :            :                 const char *prefix, const char *fmt, ...)
     713                 :            : {
     714                 :            :         struct va_format vaf;
     715                 :            :         va_list args;
     716                 :            : 
     717                 :          3 :         if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
     718                 :          0 :                 return;
     719                 :            : 
     720                 :          3 :         va_start(args, fmt);
     721                 :          3 :         vaf.fmt = fmt;
     722                 :          3 :         vaf.va = &args;
     723                 :          3 :         printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
     724                 :          3 :         va_end(args);
     725                 :            : }
     726                 :            : 
     727                 :            : #define ext4_warning_ratelimit(sb)                                      \
     728                 :            :                 ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),  \
     729                 :            :                              "EXT4-fs warning")
     730                 :            : 
     731                 :          0 : void __ext4_warning(struct super_block *sb, const char *function,
     732                 :            :                     unsigned int line, const char *fmt, ...)
     733                 :            : {
     734                 :            :         struct va_format vaf;
     735                 :            :         va_list args;
     736                 :            : 
     737                 :          0 :         if (!ext4_warning_ratelimit(sb))
     738                 :          0 :                 return;
     739                 :            : 
     740                 :          0 :         va_start(args, fmt);
     741                 :          0 :         vaf.fmt = fmt;
     742                 :          0 :         vaf.va = &args;
     743                 :          0 :         printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
     744                 :          0 :                sb->s_id, function, line, &vaf);
     745                 :          0 :         va_end(args);
     746                 :            : }
     747                 :            : 
     748                 :          0 : void __ext4_warning_inode(const struct inode *inode, const char *function,
     749                 :            :                           unsigned int line, const char *fmt, ...)
     750                 :            : {
     751                 :            :         struct va_format vaf;
     752                 :            :         va_list args;
     753                 :            : 
     754                 :          0 :         if (!ext4_warning_ratelimit(inode->i_sb))
     755                 :          0 :                 return;
     756                 :            : 
     757                 :          0 :         va_start(args, fmt);
     758                 :          0 :         vaf.fmt = fmt;
     759                 :          0 :         vaf.va = &args;
     760                 :          0 :         printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
     761                 :          0 :                "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
     762                 :          0 :                function, line, inode->i_ino, current->comm, &vaf);
     763                 :          0 :         va_end(args);
     764                 :            : }
     765                 :            : 
     766                 :          0 : void __ext4_grp_locked_error(const char *function, unsigned int line,
     767                 :            :                              struct super_block *sb, ext4_group_t grp,
     768                 :            :                              unsigned long ino, ext4_fsblk_t block,
     769                 :            :                              const char *fmt, ...)
     770                 :            : __releases(bitlock)
     771                 :            : __acquires(bitlock)
     772                 :            : {
     773                 :            :         struct va_format vaf;
     774                 :            :         va_list args;
     775                 :          0 :         struct ext4_super_block *es = EXT4_SB(sb)->s_es;
     776                 :            : 
     777                 :          0 :         if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
     778                 :            :                 return;
     779                 :            : 
     780                 :          0 :         trace_ext4_error(sb, function, line);
     781                 :          0 :         es->s_last_error_ino = cpu_to_le32(ino);
     782                 :          0 :         es->s_last_error_block = cpu_to_le64(block);
     783                 :          0 :         __save_error_info(sb, function, line);
     784                 :            : 
     785                 :          0 :         if (ext4_error_ratelimit(sb)) {
     786                 :          0 :                 va_start(args, fmt);
     787                 :          0 :                 vaf.fmt = fmt;
     788                 :          0 :                 vaf.va = &args;
     789                 :          0 :                 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
     790                 :          0 :                        sb->s_id, function, line, grp);
     791                 :          0 :                 if (ino)
     792                 :          0 :                         printk(KERN_CONT "inode %lu: ", ino);
     793                 :          0 :                 if (block)
     794                 :          0 :                         printk(KERN_CONT "block %llu:",
     795                 :            :                                (unsigned long long) block);
     796                 :          0 :                 printk(KERN_CONT "%pV\n", &vaf);
     797                 :          0 :                 va_end(args);
     798                 :            :         }
     799                 :            : 
     800                 :          0 :         if (test_opt(sb, WARN_ON_ERROR))
     801                 :          0 :                 WARN_ON_ONCE(1);
     802                 :            : 
     803                 :          0 :         if (test_opt(sb, ERRORS_CONT)) {
     804                 :          0 :                 ext4_commit_super(sb, 0);
     805                 :          0 :                 return;
     806                 :            :         }
     807                 :            : 
     808                 :            :         ext4_unlock_group(sb, grp);
     809                 :          0 :         ext4_commit_super(sb, 1);
     810                 :          0 :         ext4_handle_error(sb);
     811                 :            :         /*
     812                 :            :          * We only get here in the ERRORS_RO case; relocking the group
     813                 :            :          * may be dangerous, but nothing bad will happen since the
     814                 :            :          * filesystem will have already been marked read/only and the
     815                 :            :          * journal has been aborted.  We return 1 as a hint to callers
     816                 :            :          * who might what to use the return value from
     817                 :            :          * ext4_grp_locked_error() to distinguish between the
     818                 :            :          * ERRORS_CONT and ERRORS_RO case, and perhaps return more
     819                 :            :          * aggressively from the ext4 function in question, with a
     820                 :            :          * more appropriate error code.
     821                 :            :          */
     822                 :          0 :         ext4_lock_group(sb, grp);
     823                 :          0 :         return;
     824                 :            : }
     825                 :            : 
     826                 :          0 : void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
     827                 :            :                                      ext4_group_t group,
     828                 :            :                                      unsigned int flags)
     829                 :            : {
     830                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
     831                 :          0 :         struct ext4_group_info *grp = ext4_get_group_info(sb, group);
     832                 :          0 :         struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
     833                 :            :         int ret;
     834                 :            : 
     835                 :          0 :         if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
     836                 :            :                 ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
     837                 :            :                                             &grp->bb_state);
     838                 :          0 :                 if (!ret)
     839                 :          0 :                         percpu_counter_sub(&sbi->s_freeclusters_counter,
     840                 :          0 :                                            grp->bb_free);
     841                 :            :         }
     842                 :            : 
     843                 :          0 :         if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
     844                 :            :                 ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
     845                 :            :                                             &grp->bb_state);
     846                 :          0 :                 if (!ret && gdp) {
     847                 :            :                         int count;
     848                 :            : 
     849                 :          0 :                         count = ext4_free_inodes_count(sb, gdp);
     850                 :          0 :                         percpu_counter_sub(&sbi->s_freeinodes_counter,
     851                 :            :                                            count);
     852                 :            :                 }
     853                 :            :         }
     854                 :          0 : }
     855                 :            : 
     856                 :          3 : void ext4_update_dynamic_rev(struct super_block *sb)
     857                 :            : {
     858                 :          3 :         struct ext4_super_block *es = EXT4_SB(sb)->s_es;
     859                 :            : 
     860                 :          3 :         if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
     861                 :          3 :                 return;
     862                 :            : 
     863                 :          0 :         ext4_warning(sb,
     864                 :            :                      "updating to rev %d because of new feature flag, "
     865                 :            :                      "running e2fsck is recommended",
     866                 :            :                      EXT4_DYNAMIC_REV);
     867                 :            : 
     868                 :          0 :         es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
     869                 :          0 :         es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
     870                 :          0 :         es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
     871                 :            :         /* leave es->s_feature_*compat flags alone */
     872                 :            :         /* es->s_uuid will be set by e2fsck if empty */
     873                 :            : 
     874                 :            :         /*
     875                 :            :          * The rest of the superblock fields should be zero, and if not it
     876                 :            :          * means they are likely already in use, so leave them alone.  We
     877                 :            :          * can leave it up to e2fsck to clean up any inconsistencies there.
     878                 :            :          */
     879                 :            : }
     880                 :            : 
     881                 :            : /*
     882                 :            :  * Open the external journal device
     883                 :            :  */
     884                 :          0 : static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
     885                 :            : {
     886                 :            :         struct block_device *bdev;
     887                 :            :         char b[BDEVNAME_SIZE];
     888                 :            : 
     889                 :          0 :         bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
     890                 :          0 :         if (IS_ERR(bdev))
     891                 :            :                 goto fail;
     892                 :            :         return bdev;
     893                 :            : 
     894                 :            : fail:
     895                 :          0 :         ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
     896                 :            :                         __bdevname(dev, b), PTR_ERR(bdev));
     897                 :          0 :         return NULL;
     898                 :            : }
     899                 :            : 
     900                 :            : /*
     901                 :            :  * Release the journal device
     902                 :            :  */
     903                 :            : static void ext4_blkdev_put(struct block_device *bdev)
     904                 :            : {
     905                 :          0 :         blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
     906                 :            : }
     907                 :            : 
     908                 :            : static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
     909                 :            : {
     910                 :            :         struct block_device *bdev;
     911                 :          1 :         bdev = sbi->journal_bdev;
     912                 :          1 :         if (bdev) {
     913                 :            :                 ext4_blkdev_put(bdev);
     914                 :          0 :                 sbi->journal_bdev = NULL;
     915                 :            :         }
     916                 :            : }
     917                 :            : 
     918                 :            : static inline struct inode *orphan_list_entry(struct list_head *l)
     919                 :            : {
     920                 :          0 :         return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
     921                 :            : }
     922                 :            : 
     923                 :          0 : static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
     924                 :            : {
     925                 :            :         struct list_head *l;
     926                 :            : 
     927                 :          0 :         ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
     928                 :            :                  le32_to_cpu(sbi->s_es->s_last_orphan));
     929                 :            : 
     930                 :          0 :         printk(KERN_ERR "sb_info orphan list:\n");
     931                 :          0 :         list_for_each(l, &sbi->s_orphan) {
     932                 :            :                 struct inode *inode = orphan_list_entry(l);
     933                 :          0 :                 printk(KERN_ERR "  "
     934                 :            :                        "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
     935                 :          0 :                        inode->i_sb->s_id, inode->i_ino, inode,
     936                 :          0 :                        inode->i_mode, inode->i_nlink,
     937                 :            :                        NEXT_ORPHAN(inode));
     938                 :            :         }
     939                 :          0 : }
     940                 :            : 
     941                 :            : #ifdef CONFIG_QUOTA
     942                 :            : static int ext4_quota_off(struct super_block *sb, int type);
     943                 :            : 
     944                 :            : static inline void ext4_quota_off_umount(struct super_block *sb)
     945                 :            : {
     946                 :            :         int type;
     947                 :            : 
     948                 :            :         /* Use our quota_off function to clear inode flags etc. */
     949                 :          0 :         for (type = 0; type < EXT4_MAXQUOTAS; type++)
     950                 :          0 :                 ext4_quota_off(sb, type);
     951                 :            : }
     952                 :            : 
     953                 :            : /*
     954                 :            :  * This is a helper function which is used in the mount/remount
     955                 :            :  * codepaths (which holds s_umount) to fetch the quota file name.
     956                 :            :  */
     957                 :            : static inline char *get_qf_name(struct super_block *sb,
     958                 :            :                                 struct ext4_sb_info *sbi,
     959                 :            :                                 int type)
     960                 :            : {
     961                 :          1 :         return rcu_dereference_protected(sbi->s_qf_names[type],
     962                 :            :                                          lockdep_is_held(&sb->s_umount));
     963                 :            : }
     964                 :            : #else
     965                 :            : static inline void ext4_quota_off_umount(struct super_block *sb)
     966                 :            : {
     967                 :            : }
     968                 :            : #endif
     969                 :            : 
     970                 :          0 : static void ext4_put_super(struct super_block *sb)
     971                 :            : {
     972                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
     973                 :          0 :         struct ext4_super_block *es = sbi->s_es;
     974                 :            :         struct buffer_head **group_desc;
     975                 :            :         struct flex_groups **flex_groups;
     976                 :            :         int aborted = 0;
     977                 :            :         int i, err;
     978                 :            : 
     979                 :          0 :         ext4_unregister_li_request(sb);
     980                 :            :         ext4_quota_off_umount(sb);
     981                 :            : 
     982                 :          0 :         destroy_workqueue(sbi->rsv_conversion_wq);
     983                 :            : 
     984                 :          0 :         if (sbi->s_journal) {
     985                 :            :                 aborted = is_journal_aborted(sbi->s_journal);
     986                 :          0 :                 err = jbd2_journal_destroy(sbi->s_journal);
     987                 :          0 :                 sbi->s_journal = NULL;
     988                 :          0 :                 if ((err < 0) && !aborted)
     989                 :          0 :                         ext4_abort(sb, "Couldn't clean up the journal");
     990                 :            :         }
     991                 :            : 
     992                 :          0 :         ext4_unregister_sysfs(sb);
     993                 :          0 :         ext4_es_unregister_shrinker(sbi);
     994                 :          0 :         del_timer_sync(&sbi->s_err_report);
     995                 :          0 :         ext4_release_system_zone(sb);
     996                 :          0 :         ext4_mb_release(sb);
     997                 :          0 :         ext4_ext_release(sb);
     998                 :            : 
     999                 :          0 :         if (!sb_rdonly(sb) && !aborted) {
    1000                 :            :                 ext4_clear_feature_journal_needs_recovery(sb);
    1001                 :          0 :                 es->s_state = cpu_to_le16(sbi->s_mount_state);
    1002                 :            :         }
    1003                 :          0 :         if (!sb_rdonly(sb))
    1004                 :          0 :                 ext4_commit_super(sb, 1);
    1005                 :            : 
    1006                 :            :         rcu_read_lock();
    1007                 :          0 :         group_desc = rcu_dereference(sbi->s_group_desc);
    1008                 :          0 :         for (i = 0; i < sbi->s_gdb_count; i++)
    1009                 :          0 :                 brelse(group_desc[i]);
    1010                 :          0 :         kvfree(group_desc);
    1011                 :          0 :         flex_groups = rcu_dereference(sbi->s_flex_groups);
    1012                 :          0 :         if (flex_groups) {
    1013                 :          0 :                 for (i = 0; i < sbi->s_flex_groups_allocated; i++)
    1014                 :          0 :                         kvfree(flex_groups[i]);
    1015                 :          0 :                 kvfree(flex_groups);
    1016                 :            :         }
    1017                 :            :         rcu_read_unlock();
    1018                 :          0 :         percpu_counter_destroy(&sbi->s_freeclusters_counter);
    1019                 :          0 :         percpu_counter_destroy(&sbi->s_freeinodes_counter);
    1020                 :          0 :         percpu_counter_destroy(&sbi->s_dirs_counter);
    1021                 :          0 :         percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
    1022                 :          0 :         percpu_free_rwsem(&sbi->s_writepages_rwsem);
    1023                 :            : #ifdef CONFIG_QUOTA
    1024                 :          0 :         for (i = 0; i < EXT4_MAXQUOTAS; i++)
    1025                 :          0 :                 kfree(get_qf_name(sb, sbi, i));
    1026                 :            : #endif
    1027                 :            : 
    1028                 :            :         /* Debugging code just in case the in-memory inode orphan list
    1029                 :            :          * isn't empty.  The on-disk one can be non-empty if we've
    1030                 :            :          * detected an error and taken the fs readonly, but the
    1031                 :            :          * in-memory list had better be clean by this point. */
    1032                 :          0 :         if (!list_empty(&sbi->s_orphan))
    1033                 :          0 :                 dump_orphan_list(sb, sbi);
    1034                 :          0 :         J_ASSERT(list_empty(&sbi->s_orphan));
    1035                 :            : 
    1036                 :          0 :         sync_blockdev(sb->s_bdev);
    1037                 :          0 :         invalidate_bdev(sb->s_bdev);
    1038                 :          0 :         if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
    1039                 :            :                 /*
    1040                 :            :                  * Invalidate the journal device's buffers.  We don't want them
    1041                 :            :                  * floating about in memory - the physical journal device may
    1042                 :            :                  * hotswapped, and it breaks the `ro-after' testing code.
    1043                 :            :                  */
    1044                 :          0 :                 sync_blockdev(sbi->journal_bdev);
    1045                 :          0 :                 invalidate_bdev(sbi->journal_bdev);
    1046                 :            :                 ext4_blkdev_remove(sbi);
    1047                 :            :         }
    1048                 :            : 
    1049                 :          0 :         ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
    1050                 :          0 :         sbi->s_ea_inode_cache = NULL;
    1051                 :            : 
    1052                 :          0 :         ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
    1053                 :          0 :         sbi->s_ea_block_cache = NULL;
    1054                 :            : 
    1055                 :          0 :         if (sbi->s_mmp_tsk)
    1056                 :          0 :                 kthread_stop(sbi->s_mmp_tsk);
    1057                 :          0 :         brelse(sbi->s_sbh);
    1058                 :          0 :         sb->s_fs_info = NULL;
    1059                 :            :         /*
    1060                 :            :          * Now that we are completely done shutting down the
    1061                 :            :          * superblock, we need to actually destroy the kobject.
    1062                 :            :          */
    1063                 :          0 :         kobject_put(&sbi->s_kobj);
    1064                 :          0 :         wait_for_completion(&sbi->s_kobj_unregister);
    1065                 :          0 :         if (sbi->s_chksum_driver)
    1066                 :            :                 crypto_free_shash(sbi->s_chksum_driver);
    1067                 :          0 :         kfree(sbi->s_blockgroup_lock);
    1068                 :            :         fs_put_dax(sbi->s_daxdev);
    1069                 :            : #ifdef CONFIG_UNICODE
    1070                 :            :         utf8_unload(sbi->s_encoding);
    1071                 :            : #endif
    1072                 :          0 :         kfree(sbi);
    1073                 :          0 : }
    1074                 :            : 
    1075                 :            : static struct kmem_cache *ext4_inode_cachep;
    1076                 :            : 
    1077                 :            : /*
    1078                 :            :  * Called inside transaction, so use GFP_NOFS
    1079                 :            :  */
    1080                 :          3 : static struct inode *ext4_alloc_inode(struct super_block *sb)
    1081                 :            : {
    1082                 :            :         struct ext4_inode_info *ei;
    1083                 :            : 
    1084                 :          3 :         ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
    1085                 :          3 :         if (!ei)
    1086                 :            :                 return NULL;
    1087                 :            : 
    1088                 :            :         inode_set_iversion(&ei->vfs_inode, 1);
    1089                 :          3 :         spin_lock_init(&ei->i_raw_lock);
    1090                 :          3 :         INIT_LIST_HEAD(&ei->i_prealloc_list);
    1091                 :          3 :         spin_lock_init(&ei->i_prealloc_lock);
    1092                 :          3 :         ext4_es_init_tree(&ei->i_es_tree);
    1093                 :          3 :         rwlock_init(&ei->i_es_lock);
    1094                 :          3 :         INIT_LIST_HEAD(&ei->i_es_list);
    1095                 :          3 :         ei->i_es_all_nr = 0;
    1096                 :          3 :         ei->i_es_shk_nr = 0;
    1097                 :          3 :         ei->i_es_shrink_lblk = 0;
    1098                 :          3 :         ei->i_reserved_data_blocks = 0;
    1099                 :          3 :         ei->i_da_metadata_calc_len = 0;
    1100                 :          3 :         ei->i_da_metadata_calc_last_lblock = 0;
    1101                 :          3 :         spin_lock_init(&(ei->i_block_reservation_lock));
    1102                 :          3 :         ext4_init_pending_tree(&ei->i_pending_tree);
    1103                 :            : #ifdef CONFIG_QUOTA
    1104                 :          3 :         ei->i_reserved_quota = 0;
    1105                 :          3 :         memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
    1106                 :            : #endif
    1107                 :          3 :         ei->jinode = NULL;
    1108                 :          3 :         INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
    1109                 :          3 :         spin_lock_init(&ei->i_completed_io_lock);
    1110                 :          3 :         ei->i_sync_tid = 0;
    1111                 :          3 :         ei->i_datasync_tid = 0;
    1112                 :            :         atomic_set(&ei->i_unwritten, 0);
    1113                 :          3 :         INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
    1114                 :          3 :         return &ei->vfs_inode;
    1115                 :            : }
    1116                 :            : 
    1117                 :          3 : static int ext4_drop_inode(struct inode *inode)
    1118                 :            : {
    1119                 :            :         int drop = generic_drop_inode(inode);
    1120                 :            : 
    1121                 :          3 :         if (!drop)
    1122                 :          3 :                 drop = fscrypt_drop_inode(inode);
    1123                 :            : 
    1124                 :          3 :         trace_ext4_drop_inode(inode, drop);
    1125                 :          3 :         return drop;
    1126                 :            : }
    1127                 :            : 
    1128                 :          3 : static void ext4_free_in_core_inode(struct inode *inode)
    1129                 :            : {
    1130                 :          3 :         fscrypt_free_inode(inode);
    1131                 :          3 :         kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
    1132                 :          3 : }
    1133                 :            : 
    1134                 :          3 : static void ext4_destroy_inode(struct inode *inode)
    1135                 :            : {
    1136                 :          3 :         if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
    1137                 :          0 :                 ext4_msg(inode->i_sb, KERN_ERR,
    1138                 :            :                          "Inode %lu (%p): orphan list check failed!",
    1139                 :            :                          inode->i_ino, EXT4_I(inode));
    1140                 :          0 :                 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
    1141                 :            :                                 EXT4_I(inode), sizeof(struct ext4_inode_info),
    1142                 :            :                                 true);
    1143                 :          0 :                 dump_stack();
    1144                 :            :         }
    1145                 :          3 : }
    1146                 :            : 
    1147                 :          3 : static void init_once(void *foo)
    1148                 :            : {
    1149                 :            :         struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
    1150                 :            : 
    1151                 :          3 :         INIT_LIST_HEAD(&ei->i_orphan);
    1152                 :          3 :         init_rwsem(&ei->xattr_sem);
    1153                 :          3 :         init_rwsem(&ei->i_data_sem);
    1154                 :          3 :         init_rwsem(&ei->i_mmap_sem);
    1155                 :          3 :         inode_init_once(&ei->vfs_inode);
    1156                 :          3 : }
    1157                 :            : 
    1158                 :          3 : static int __init init_inodecache(void)
    1159                 :            : {
    1160                 :          3 :         ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
    1161                 :            :                                 sizeof(struct ext4_inode_info), 0,
    1162                 :            :                                 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
    1163                 :            :                                         SLAB_ACCOUNT),
    1164                 :            :                                 offsetof(struct ext4_inode_info, i_data),
    1165                 :            :                                 sizeof_field(struct ext4_inode_info, i_data),
    1166                 :            :                                 init_once);
    1167                 :          3 :         if (ext4_inode_cachep == NULL)
    1168                 :            :                 return -ENOMEM;
    1169                 :          3 :         return 0;
    1170                 :            : }
    1171                 :            : 
    1172                 :            : static void destroy_inodecache(void)
    1173                 :            : {
    1174                 :            :         /*
    1175                 :            :          * Make sure all delayed rcu free inodes are flushed before we
    1176                 :            :          * destroy cache.
    1177                 :            :          */
    1178                 :          0 :         rcu_barrier();
    1179                 :          0 :         kmem_cache_destroy(ext4_inode_cachep);
    1180                 :            : }
    1181                 :            : 
    1182                 :          3 : void ext4_clear_inode(struct inode *inode)
    1183                 :            : {
    1184                 :          3 :         invalidate_inode_buffers(inode);
    1185                 :          3 :         clear_inode(inode);
    1186                 :          3 :         ext4_discard_preallocations(inode);
    1187                 :          3 :         ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
    1188                 :          3 :         dquot_drop(inode);
    1189                 :          3 :         if (EXT4_I(inode)->jinode) {
    1190                 :          3 :                 jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
    1191                 :            :                                                EXT4_I(inode)->jinode);
    1192                 :          3 :                 jbd2_free_inode(EXT4_I(inode)->jinode);
    1193                 :          3 :                 EXT4_I(inode)->jinode = NULL;
    1194                 :            :         }
    1195                 :          3 :         fscrypt_put_encryption_info(inode);
    1196                 :            :         fsverity_cleanup_inode(inode);
    1197                 :          3 : }
    1198                 :            : 
    1199                 :          0 : static struct inode *ext4_nfs_get_inode(struct super_block *sb,
    1200                 :            :                                         u64 ino, u32 generation)
    1201                 :            : {
    1202                 :            :         struct inode *inode;
    1203                 :            : 
    1204                 :            :         /*
    1205                 :            :          * Currently we don't know the generation for parent directory, so
    1206                 :            :          * a generation of 0 means "accept any"
    1207                 :            :          */
    1208                 :          0 :         inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
    1209                 :          0 :         if (IS_ERR(inode))
    1210                 :            :                 return ERR_CAST(inode);
    1211                 :          0 :         if (generation && inode->i_generation != generation) {
    1212                 :          0 :                 iput(inode);
    1213                 :          0 :                 return ERR_PTR(-ESTALE);
    1214                 :            :         }
    1215                 :            : 
    1216                 :            :         return inode;
    1217                 :            : }
    1218                 :            : 
    1219                 :          0 : static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
    1220                 :            :                                         int fh_len, int fh_type)
    1221                 :            : {
    1222                 :          0 :         return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
    1223                 :            :                                     ext4_nfs_get_inode);
    1224                 :            : }
    1225                 :            : 
    1226                 :          0 : static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
    1227                 :            :                                         int fh_len, int fh_type)
    1228                 :            : {
    1229                 :          0 :         return generic_fh_to_parent(sb, fid, fh_len, fh_type,
    1230                 :            :                                     ext4_nfs_get_inode);
    1231                 :            : }
    1232                 :            : 
    1233                 :          0 : static int ext4_nfs_commit_metadata(struct inode *inode)
    1234                 :            : {
    1235                 :          0 :         struct writeback_control wbc = {
    1236                 :            :                 .sync_mode = WB_SYNC_ALL
    1237                 :            :         };
    1238                 :            : 
    1239                 :          0 :         trace_ext4_nfs_commit_metadata(inode);
    1240                 :          0 :         return ext4_write_inode(inode, &wbc);
    1241                 :            : }
    1242                 :            : 
    1243                 :            : /*
    1244                 :            :  * Try to release metadata pages (indirect blocks, directories) which are
    1245                 :            :  * mapped via the block device.  Since these pages could have journal heads
    1246                 :            :  * which would prevent try_to_free_buffers() from freeing them, we must use
    1247                 :            :  * jbd2 layer's try_to_free_buffers() function to release them.
    1248                 :            :  */
    1249                 :          0 : static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
    1250                 :            :                                  gfp_t wait)
    1251                 :            : {
    1252                 :          0 :         journal_t *journal = EXT4_SB(sb)->s_journal;
    1253                 :            : 
    1254                 :          0 :         WARN_ON(PageChecked(page));
    1255                 :          0 :         if (!page_has_buffers(page))
    1256                 :            :                 return 0;
    1257                 :          0 :         if (journal)
    1258                 :          0 :                 return jbd2_journal_try_to_free_buffers(journal, page,
    1259                 :            :                                                 wait & ~__GFP_DIRECT_RECLAIM);
    1260                 :          0 :         return try_to_free_buffers(page);
    1261                 :            : }
    1262                 :            : 
    1263                 :            : #ifdef CONFIG_FS_ENCRYPTION
    1264                 :          0 : static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
    1265                 :            : {
    1266                 :          0 :         return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
    1267                 :            :                                  EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
    1268                 :            : }
    1269                 :            : 
    1270                 :          0 : static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
    1271                 :            :                                                         void *fs_data)
    1272                 :            : {
    1273                 :            :         handle_t *handle = fs_data;
    1274                 :          0 :         int res, res2, credits, retries = 0;
    1275                 :            : 
    1276                 :            :         /*
    1277                 :            :          * Encrypting the root directory is not allowed because e2fsck expects
    1278                 :            :          * lost+found to exist and be unencrypted, and encrypting the root
    1279                 :            :          * directory would imply encrypting the lost+found directory as well as
    1280                 :            :          * the filename "lost+found" itself.
    1281                 :            :          */
    1282                 :          0 :         if (inode->i_ino == EXT4_ROOT_INO)
    1283                 :            :                 return -EPERM;
    1284                 :            : 
    1285                 :            :         if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
    1286                 :            :                 return -EINVAL;
    1287                 :            : 
    1288                 :          0 :         res = ext4_convert_inline_data(inode);
    1289                 :          0 :         if (res)
    1290                 :            :                 return res;
    1291                 :            : 
    1292                 :            :         /*
    1293                 :            :          * If a journal handle was specified, then the encryption context is
    1294                 :            :          * being set on a new inode via inheritance and is part of a larger
    1295                 :            :          * transaction to create the inode.  Otherwise the encryption context is
    1296                 :            :          * being set on an existing inode in its own transaction.  Only in the
    1297                 :            :          * latter case should the "retry on ENOSPC" logic be used.
    1298                 :            :          */
    1299                 :            : 
    1300                 :          0 :         if (handle) {
    1301                 :          0 :                 res = ext4_xattr_set_handle(handle, inode,
    1302                 :            :                                             EXT4_XATTR_INDEX_ENCRYPTION,
    1303                 :            :                                             EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
    1304                 :            :                                             ctx, len, 0);
    1305                 :          0 :                 if (!res) {
    1306                 :            :                         ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
    1307                 :            :                         ext4_clear_inode_state(inode,
    1308                 :            :                                         EXT4_STATE_MAY_INLINE_DATA);
    1309                 :            :                         /*
    1310                 :            :                          * Update inode->i_flags - S_ENCRYPTED will be enabled,
    1311                 :            :                          * S_DAX may be disabled
    1312                 :            :                          */
    1313                 :          0 :                         ext4_set_inode_flags(inode);
    1314                 :            :                 }
    1315                 :          0 :                 return res;
    1316                 :            :         }
    1317                 :            : 
    1318                 :          0 :         res = dquot_initialize(inode);
    1319                 :          0 :         if (res)
    1320                 :            :                 return res;
    1321                 :            : retry:
    1322                 :          0 :         res = ext4_xattr_set_credits(inode, len, false /* is_create */,
    1323                 :            :                                      &credits);
    1324                 :          0 :         if (res)
    1325                 :          0 :                 return res;
    1326                 :            : 
    1327                 :          0 :         handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
    1328                 :          0 :         if (IS_ERR(handle))
    1329                 :          0 :                 return PTR_ERR(handle);
    1330                 :            : 
    1331                 :          0 :         res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
    1332                 :            :                                     EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
    1333                 :            :                                     ctx, len, 0);
    1334                 :          0 :         if (!res) {
    1335                 :            :                 ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
    1336                 :            :                 /*
    1337                 :            :                  * Update inode->i_flags - S_ENCRYPTED will be enabled,
    1338                 :            :                  * S_DAX may be disabled
    1339                 :            :                  */
    1340                 :          0 :                 ext4_set_inode_flags(inode);
    1341                 :          0 :                 res = ext4_mark_inode_dirty(handle, inode);
    1342                 :          0 :                 if (res)
    1343                 :          0 :                         EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
    1344                 :            :         }
    1345                 :          0 :         res2 = ext4_journal_stop(handle);
    1346                 :            : 
    1347                 :          0 :         if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
    1348                 :            :                 goto retry;
    1349                 :          0 :         if (!res)
    1350                 :          0 :                 res = res2;
    1351                 :          0 :         return res;
    1352                 :            : }
    1353                 :            : 
    1354                 :          1 : static bool ext4_dummy_context(struct inode *inode)
    1355                 :            : {
    1356                 :          1 :         return DUMMY_ENCRYPTION_ENABLED(EXT4_SB(inode->i_sb));
    1357                 :            : }
    1358                 :            : 
    1359                 :            : static const struct fscrypt_operations ext4_cryptops = {
    1360                 :            :         .key_prefix             = "ext4:",
    1361                 :            :         .get_context            = ext4_get_context,
    1362                 :            :         .set_context            = ext4_set_context,
    1363                 :            :         .dummy_context          = ext4_dummy_context,
    1364                 :            :         .empty_dir              = ext4_empty_dir,
    1365                 :            :         .max_namelen            = EXT4_NAME_LEN,
    1366                 :            : };
    1367                 :            : #endif
    1368                 :            : 
    1369                 :            : #ifdef CONFIG_QUOTA
    1370                 :            : static const char * const quotatypes[] = INITQFNAMES;
    1371                 :            : #define QTYPE2NAME(t) (quotatypes[t])
    1372                 :            : 
    1373                 :            : static int ext4_write_dquot(struct dquot *dquot);
    1374                 :            : static int ext4_acquire_dquot(struct dquot *dquot);
    1375                 :            : static int ext4_release_dquot(struct dquot *dquot);
    1376                 :            : static int ext4_mark_dquot_dirty(struct dquot *dquot);
    1377                 :            : static int ext4_write_info(struct super_block *sb, int type);
    1378                 :            : static int ext4_quota_on(struct super_block *sb, int type, int format_id,
    1379                 :            :                          const struct path *path);
    1380                 :            : static int ext4_quota_on_mount(struct super_block *sb, int type);
    1381                 :            : static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
    1382                 :            :                                size_t len, loff_t off);
    1383                 :            : static ssize_t ext4_quota_write(struct super_block *sb, int type,
    1384                 :            :                                 const char *data, size_t len, loff_t off);
    1385                 :            : static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
    1386                 :            :                              unsigned int flags);
    1387                 :            : static int ext4_enable_quotas(struct super_block *sb);
    1388                 :            : static int ext4_get_next_id(struct super_block *sb, struct kqid *qid);
    1389                 :            : 
    1390                 :          3 : static struct dquot **ext4_get_dquots(struct inode *inode)
    1391                 :            : {
    1392                 :          3 :         return EXT4_I(inode)->i_dquot;
    1393                 :            : }
    1394                 :            : 
    1395                 :            : static const struct dquot_operations ext4_quota_operations = {
    1396                 :            :         .get_reserved_space     = ext4_get_reserved_space,
    1397                 :            :         .write_dquot            = ext4_write_dquot,
    1398                 :            :         .acquire_dquot          = ext4_acquire_dquot,
    1399                 :            :         .release_dquot          = ext4_release_dquot,
    1400                 :            :         .mark_dirty             = ext4_mark_dquot_dirty,
    1401                 :            :         .write_info             = ext4_write_info,
    1402                 :            :         .alloc_dquot            = dquot_alloc,
    1403                 :            :         .destroy_dquot          = dquot_destroy,
    1404                 :            :         .get_projid             = ext4_get_projid,
    1405                 :            :         .get_inode_usage        = ext4_get_inode_usage,
    1406                 :            :         .get_next_id            = ext4_get_next_id,
    1407                 :            : };
    1408                 :            : 
    1409                 :            : static const struct quotactl_ops ext4_qctl_operations = {
    1410                 :            :         .quota_on       = ext4_quota_on,
    1411                 :            :         .quota_off      = ext4_quota_off,
    1412                 :            :         .quota_sync     = dquot_quota_sync,
    1413                 :            :         .get_state      = dquot_get_state,
    1414                 :            :         .set_info       = dquot_set_dqinfo,
    1415                 :            :         .get_dqblk      = dquot_get_dqblk,
    1416                 :            :         .set_dqblk      = dquot_set_dqblk,
    1417                 :            :         .get_nextdqblk  = dquot_get_next_dqblk,
    1418                 :            : };
    1419                 :            : #endif
    1420                 :            : 
    1421                 :            : static const struct super_operations ext4_sops = {
    1422                 :            :         .alloc_inode    = ext4_alloc_inode,
    1423                 :            :         .free_inode     = ext4_free_in_core_inode,
    1424                 :            :         .destroy_inode  = ext4_destroy_inode,
    1425                 :            :         .write_inode    = ext4_write_inode,
    1426                 :            :         .dirty_inode    = ext4_dirty_inode,
    1427                 :            :         .drop_inode     = ext4_drop_inode,
    1428                 :            :         .evict_inode    = ext4_evict_inode,
    1429                 :            :         .put_super      = ext4_put_super,
    1430                 :            :         .sync_fs        = ext4_sync_fs,
    1431                 :            :         .freeze_fs      = ext4_freeze,
    1432                 :            :         .unfreeze_fs    = ext4_unfreeze,
    1433                 :            :         .statfs         = ext4_statfs,
    1434                 :            :         .remount_fs     = ext4_remount,
    1435                 :            :         .show_options   = ext4_show_options,
    1436                 :            : #ifdef CONFIG_QUOTA
    1437                 :            :         .quota_read     = ext4_quota_read,
    1438                 :            :         .quota_write    = ext4_quota_write,
    1439                 :            :         .get_dquots     = ext4_get_dquots,
    1440                 :            : #endif
    1441                 :            :         .bdev_try_to_free_page = bdev_try_to_free_page,
    1442                 :            : };
    1443                 :            : 
    1444                 :            : static const struct export_operations ext4_export_ops = {
    1445                 :            :         .fh_to_dentry = ext4_fh_to_dentry,
    1446                 :            :         .fh_to_parent = ext4_fh_to_parent,
    1447                 :            :         .get_parent = ext4_get_parent,
    1448                 :            :         .commit_metadata = ext4_nfs_commit_metadata,
    1449                 :            : };
    1450                 :            : 
    1451                 :            : enum {
    1452                 :            :         Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
    1453                 :            :         Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
    1454                 :            :         Opt_nouid32, Opt_debug, Opt_removed,
    1455                 :            :         Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
    1456                 :            :         Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
    1457                 :            :         Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
    1458                 :            :         Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
    1459                 :            :         Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
    1460                 :            :         Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
    1461                 :            :         Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
    1462                 :            :         Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
    1463                 :            :         Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
    1464                 :            :         Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version, Opt_dax,
    1465                 :            :         Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
    1466                 :            :         Opt_nowarn_on_error, Opt_mblk_io_submit,
    1467                 :            :         Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
    1468                 :            :         Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
    1469                 :            :         Opt_inode_readahead_blks, Opt_journal_ioprio,
    1470                 :            :         Opt_dioread_nolock, Opt_dioread_lock,
    1471                 :            :         Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
    1472                 :            :         Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
    1473                 :            : };
    1474                 :            : 
    1475                 :            : static const match_table_t tokens = {
    1476                 :            :         {Opt_bsd_df, "bsddf"},
    1477                 :            :         {Opt_minix_df, "minixdf"},
    1478                 :            :         {Opt_grpid, "grpid"},
    1479                 :            :         {Opt_grpid, "bsdgroups"},
    1480                 :            :         {Opt_nogrpid, "nogrpid"},
    1481                 :            :         {Opt_nogrpid, "sysvgroups"},
    1482                 :            :         {Opt_resgid, "resgid=%u"},
    1483                 :            :         {Opt_resuid, "resuid=%u"},
    1484                 :            :         {Opt_sb, "sb=%u"},
    1485                 :            :         {Opt_err_cont, "errors=continue"},
    1486                 :            :         {Opt_err_panic, "errors=panic"},
    1487                 :            :         {Opt_err_ro, "errors=remount-ro"},
    1488                 :            :         {Opt_nouid32, "nouid32"},
    1489                 :            :         {Opt_debug, "debug"},
    1490                 :            :         {Opt_removed, "oldalloc"},
    1491                 :            :         {Opt_removed, "orlov"},
    1492                 :            :         {Opt_user_xattr, "user_xattr"},
    1493                 :            :         {Opt_nouser_xattr, "nouser_xattr"},
    1494                 :            :         {Opt_acl, "acl"},
    1495                 :            :         {Opt_noacl, "noacl"},
    1496                 :            :         {Opt_noload, "norecovery"},
    1497                 :            :         {Opt_noload, "noload"},
    1498                 :            :         {Opt_removed, "nobh"},
    1499                 :            :         {Opt_removed, "bh"},
    1500                 :            :         {Opt_commit, "commit=%u"},
    1501                 :            :         {Opt_min_batch_time, "min_batch_time=%u"},
    1502                 :            :         {Opt_max_batch_time, "max_batch_time=%u"},
    1503                 :            :         {Opt_journal_dev, "journal_dev=%u"},
    1504                 :            :         {Opt_journal_path, "journal_path=%s"},
    1505                 :            :         {Opt_journal_checksum, "journal_checksum"},
    1506                 :            :         {Opt_nojournal_checksum, "nojournal_checksum"},
    1507                 :            :         {Opt_journal_async_commit, "journal_async_commit"},
    1508                 :            :         {Opt_abort, "abort"},
    1509                 :            :         {Opt_data_journal, "data=journal"},
    1510                 :            :         {Opt_data_ordered, "data=ordered"},
    1511                 :            :         {Opt_data_writeback, "data=writeback"},
    1512                 :            :         {Opt_data_err_abort, "data_err=abort"},
    1513                 :            :         {Opt_data_err_ignore, "data_err=ignore"},
    1514                 :            :         {Opt_offusrjquota, "usrjquota="},
    1515                 :            :         {Opt_usrjquota, "usrjquota=%s"},
    1516                 :            :         {Opt_offgrpjquota, "grpjquota="},
    1517                 :            :         {Opt_grpjquota, "grpjquota=%s"},
    1518                 :            :         {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
    1519                 :            :         {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
    1520                 :            :         {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
    1521                 :            :         {Opt_grpquota, "grpquota"},
    1522                 :            :         {Opt_noquota, "noquota"},
    1523                 :            :         {Opt_quota, "quota"},
    1524                 :            :         {Opt_usrquota, "usrquota"},
    1525                 :            :         {Opt_prjquota, "prjquota"},
    1526                 :            :         {Opt_barrier, "barrier=%u"},
    1527                 :            :         {Opt_barrier, "barrier"},
    1528                 :            :         {Opt_nobarrier, "nobarrier"},
    1529                 :            :         {Opt_i_version, "i_version"},
    1530                 :            :         {Opt_dax, "dax"},
    1531                 :            :         {Opt_stripe, "stripe=%u"},
    1532                 :            :         {Opt_delalloc, "delalloc"},
    1533                 :            :         {Opt_warn_on_error, "warn_on_error"},
    1534                 :            :         {Opt_nowarn_on_error, "nowarn_on_error"},
    1535                 :            :         {Opt_lazytime, "lazytime"},
    1536                 :            :         {Opt_nolazytime, "nolazytime"},
    1537                 :            :         {Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
    1538                 :            :         {Opt_nodelalloc, "nodelalloc"},
    1539                 :            :         {Opt_removed, "mblk_io_submit"},
    1540                 :            :         {Opt_removed, "nomblk_io_submit"},
    1541                 :            :         {Opt_block_validity, "block_validity"},
    1542                 :            :         {Opt_noblock_validity, "noblock_validity"},
    1543                 :            :         {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
    1544                 :            :         {Opt_journal_ioprio, "journal_ioprio=%u"},
    1545                 :            :         {Opt_auto_da_alloc, "auto_da_alloc=%u"},
    1546                 :            :         {Opt_auto_da_alloc, "auto_da_alloc"},
    1547                 :            :         {Opt_noauto_da_alloc, "noauto_da_alloc"},
    1548                 :            :         {Opt_dioread_nolock, "dioread_nolock"},
    1549                 :            :         {Opt_dioread_lock, "dioread_lock"},
    1550                 :            :         {Opt_discard, "discard"},
    1551                 :            :         {Opt_nodiscard, "nodiscard"},
    1552                 :            :         {Opt_init_itable, "init_itable=%u"},
    1553                 :            :         {Opt_init_itable, "init_itable"},
    1554                 :            :         {Opt_noinit_itable, "noinit_itable"},
    1555                 :            :         {Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
    1556                 :            :         {Opt_test_dummy_encryption, "test_dummy_encryption"},
    1557                 :            :         {Opt_nombcache, "nombcache"},
    1558                 :            :         {Opt_nombcache, "no_mbcache"},        /* for backward compatibility */
    1559                 :            :         {Opt_removed, "check=none"},  /* mount option from ext2/3 */
    1560                 :            :         {Opt_removed, "nocheck"},     /* mount option from ext2/3 */
    1561                 :            :         {Opt_removed, "reservation"}, /* mount option from ext2/3 */
    1562                 :            :         {Opt_removed, "noreservation"}, /* mount option from ext2/3 */
    1563                 :            :         {Opt_removed, "journal=%u"},  /* mount option from ext2/3 */
    1564                 :            :         {Opt_err, NULL},
    1565                 :            : };
    1566                 :            : 
    1567                 :          3 : static ext4_fsblk_t get_sb_block(void **data)
    1568                 :            : {
    1569                 :            :         ext4_fsblk_t    sb_block;
    1570                 :          3 :         char            *options = (char *) *data;
    1571                 :            : 
    1572                 :          3 :         if (!options || strncmp(options, "sb=", 3) != 0)
    1573                 :            :                 return 1;       /* Default location */
    1574                 :            : 
    1575                 :          0 :         options += 3;
    1576                 :            :         /* TODO: use simple_strtoll with >32bit ext4 */
    1577                 :          0 :         sb_block = simple_strtoul(options, &options, 0);
    1578                 :          0 :         if (*options && *options != ',') {
    1579                 :          0 :                 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
    1580                 :            :                        (char *) *data);
    1581                 :          0 :                 return 1;
    1582                 :            :         }
    1583                 :          0 :         if (*options == ',')
    1584                 :          0 :                 options++;
    1585                 :          0 :         *data = (void *) options;
    1586                 :            : 
    1587                 :          0 :         return sb_block;
    1588                 :            : }
    1589                 :            : 
    1590                 :            : #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
    1591                 :            : static const char deprecated_msg[] =
    1592                 :            :         "Mount option \"%s\" will be removed by %s\n"
    1593                 :            :         "Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
    1594                 :            : 
    1595                 :            : #ifdef CONFIG_QUOTA
    1596                 :          0 : static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
    1597                 :            : {
    1598                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    1599                 :            :         char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
    1600                 :            :         int ret = -1;
    1601                 :            : 
    1602                 :          0 :         if (sb_any_quota_loaded(sb) && !old_qname) {
    1603                 :          0 :                 ext4_msg(sb, KERN_ERR,
    1604                 :            :                         "Cannot change journaled "
    1605                 :            :                         "quota options when quota turned on");
    1606                 :          0 :                 return -1;
    1607                 :            :         }
    1608                 :          0 :         if (ext4_has_feature_quota(sb)) {
    1609                 :          0 :                 ext4_msg(sb, KERN_INFO, "Journaled quota options "
    1610                 :            :                          "ignored when QUOTA feature is enabled");
    1611                 :          0 :                 return 1;
    1612                 :            :         }
    1613                 :          0 :         qname = match_strdup(args);
    1614                 :          0 :         if (!qname) {
    1615                 :          0 :                 ext4_msg(sb, KERN_ERR,
    1616                 :            :                         "Not enough memory for storing quotafile name");
    1617                 :          0 :                 return -1;
    1618                 :            :         }
    1619                 :          0 :         if (old_qname) {
    1620                 :          0 :                 if (strcmp(old_qname, qname) == 0)
    1621                 :            :                         ret = 1;
    1622                 :            :                 else
    1623                 :          0 :                         ext4_msg(sb, KERN_ERR,
    1624                 :            :                                  "%s quota file already specified",
    1625                 :            :                                  QTYPE2NAME(qtype));
    1626                 :            :                 goto errout;
    1627                 :            :         }
    1628                 :          0 :         if (strchr(qname, '/')) {
    1629                 :          0 :                 ext4_msg(sb, KERN_ERR,
    1630                 :            :                         "quotafile must be on filesystem root");
    1631                 :          0 :                 goto errout;
    1632                 :            :         }
    1633                 :          0 :         rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
    1634                 :          0 :         set_opt(sb, QUOTA);
    1635                 :          0 :         return 1;
    1636                 :            : errout:
    1637                 :          0 :         kfree(qname);
    1638                 :          0 :         return ret;
    1639                 :            : }
    1640                 :            : 
    1641                 :          0 : static int clear_qf_name(struct super_block *sb, int qtype)
    1642                 :            : {
    1643                 :            : 
    1644                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    1645                 :            :         char *old_qname = get_qf_name(sb, sbi, qtype);
    1646                 :            : 
    1647                 :          0 :         if (sb_any_quota_loaded(sb) && old_qname) {
    1648                 :          0 :                 ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
    1649                 :            :                         " when quota turned on");
    1650                 :          0 :                 return -1;
    1651                 :            :         }
    1652                 :          0 :         rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
    1653                 :          0 :         synchronize_rcu();
    1654                 :          0 :         kfree(old_qname);
    1655                 :          0 :         return 1;
    1656                 :            : }
    1657                 :            : #endif
    1658                 :            : 
    1659                 :            : #define MOPT_SET        0x0001
    1660                 :            : #define MOPT_CLEAR      0x0002
    1661                 :            : #define MOPT_NOSUPPORT  0x0004
    1662                 :            : #define MOPT_EXPLICIT   0x0008
    1663                 :            : #define MOPT_CLEAR_ERR  0x0010
    1664                 :            : #define MOPT_GTE0       0x0020
    1665                 :            : #ifdef CONFIG_QUOTA
    1666                 :            : #define MOPT_Q          0
    1667                 :            : #define MOPT_QFMT       0x0040
    1668                 :            : #else
    1669                 :            : #define MOPT_Q          MOPT_NOSUPPORT
    1670                 :            : #define MOPT_QFMT       MOPT_NOSUPPORT
    1671                 :            : #endif
    1672                 :            : #define MOPT_DATAJ      0x0080
    1673                 :            : #define MOPT_NO_EXT2    0x0100
    1674                 :            : #define MOPT_NO_EXT3    0x0200
    1675                 :            : #define MOPT_EXT4_ONLY  (MOPT_NO_EXT2 | MOPT_NO_EXT3)
    1676                 :            : #define MOPT_STRING     0x0400
    1677                 :            : 
    1678                 :            : static const struct mount_opts {
    1679                 :            :         int     token;
    1680                 :            :         int     mount_opt;
    1681                 :            :         int     flags;
    1682                 :            : } ext4_mount_opts[] = {
    1683                 :            :         {Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
    1684                 :            :         {Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
    1685                 :            :         {Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
    1686                 :            :         {Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
    1687                 :            :         {Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
    1688                 :            :         {Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
    1689                 :            :         {Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
    1690                 :            :          MOPT_EXT4_ONLY | MOPT_SET},
    1691                 :            :         {Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
    1692                 :            :          MOPT_EXT4_ONLY | MOPT_CLEAR},
    1693                 :            :         {Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
    1694                 :            :         {Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
    1695                 :            :         {Opt_delalloc, EXT4_MOUNT_DELALLOC,
    1696                 :            :          MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
    1697                 :            :         {Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
    1698                 :            :          MOPT_EXT4_ONLY | MOPT_CLEAR},
    1699                 :            :         {Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
    1700                 :            :         {Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
    1701                 :            :         {Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
    1702                 :            :          MOPT_EXT4_ONLY | MOPT_CLEAR},
    1703                 :            :         {Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
    1704                 :            :          MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
    1705                 :            :         {Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
    1706                 :            :                                     EXT4_MOUNT_JOURNAL_CHECKSUM),
    1707                 :            :          MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
    1708                 :            :         {Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
    1709                 :            :         {Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
    1710                 :            :         {Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
    1711                 :            :         {Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
    1712                 :            :         {Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
    1713                 :            :          MOPT_NO_EXT2},
    1714                 :            :         {Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
    1715                 :            :          MOPT_NO_EXT2},
    1716                 :            :         {Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
    1717                 :            :         {Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
    1718                 :            :         {Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
    1719                 :            :         {Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
    1720                 :            :         {Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
    1721                 :            :         {Opt_commit, 0, MOPT_GTE0},
    1722                 :            :         {Opt_max_batch_time, 0, MOPT_GTE0},
    1723                 :            :         {Opt_min_batch_time, 0, MOPT_GTE0},
    1724                 :            :         {Opt_inode_readahead_blks, 0, MOPT_GTE0},
    1725                 :            :         {Opt_init_itable, 0, MOPT_GTE0},
    1726                 :            :         {Opt_dax, EXT4_MOUNT_DAX, MOPT_SET},
    1727                 :            :         {Opt_stripe, 0, MOPT_GTE0},
    1728                 :            :         {Opt_resuid, 0, MOPT_GTE0},
    1729                 :            :         {Opt_resgid, 0, MOPT_GTE0},
    1730                 :            :         {Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
    1731                 :            :         {Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
    1732                 :            :         {Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
    1733                 :            :         {Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
    1734                 :            :         {Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
    1735                 :            :         {Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
    1736                 :            :          MOPT_NO_EXT2 | MOPT_DATAJ},
    1737                 :            :         {Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
    1738                 :            :         {Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
    1739                 :            : #ifdef CONFIG_EXT4_FS_POSIX_ACL
    1740                 :            :         {Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
    1741                 :            :         {Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
    1742                 :            : #else
    1743                 :            :         {Opt_acl, 0, MOPT_NOSUPPORT},
    1744                 :            :         {Opt_noacl, 0, MOPT_NOSUPPORT},
    1745                 :            : #endif
    1746                 :            :         {Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
    1747                 :            :         {Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
    1748                 :            :         {Opt_debug_want_extra_isize, 0, MOPT_GTE0},
    1749                 :            :         {Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
    1750                 :            :         {Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
    1751                 :            :                                                         MOPT_SET | MOPT_Q},
    1752                 :            :         {Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
    1753                 :            :                                                         MOPT_SET | MOPT_Q},
    1754                 :            :         {Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
    1755                 :            :                                                         MOPT_SET | MOPT_Q},
    1756                 :            :         {Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
    1757                 :            :                        EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
    1758                 :            :                                                         MOPT_CLEAR | MOPT_Q},
    1759                 :            :         {Opt_usrjquota, 0, MOPT_Q},
    1760                 :            :         {Opt_grpjquota, 0, MOPT_Q},
    1761                 :            :         {Opt_offusrjquota, 0, MOPT_Q},
    1762                 :            :         {Opt_offgrpjquota, 0, MOPT_Q},
    1763                 :            :         {Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
    1764                 :            :         {Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
    1765                 :            :         {Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
    1766                 :            :         {Opt_max_dir_size_kb, 0, MOPT_GTE0},
    1767                 :            :         {Opt_test_dummy_encryption, 0, MOPT_GTE0},
    1768                 :            :         {Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
    1769                 :            :         {Opt_err, 0, 0}
    1770                 :            : };
    1771                 :            : 
    1772                 :            : #ifdef CONFIG_UNICODE
    1773                 :            : static const struct ext4_sb_encodings {
    1774                 :            :         __u16 magic;
    1775                 :            :         char *name;
    1776                 :            :         char *version;
    1777                 :            : } ext4_sb_encoding_map[] = {
    1778                 :            :         {EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
    1779                 :            : };
    1780                 :            : 
    1781                 :            : static int ext4_sb_read_encoding(const struct ext4_super_block *es,
    1782                 :            :                                  const struct ext4_sb_encodings **encoding,
    1783                 :            :                                  __u16 *flags)
    1784                 :            : {
    1785                 :            :         __u16 magic = le16_to_cpu(es->s_encoding);
    1786                 :            :         int i;
    1787                 :            : 
    1788                 :            :         for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
    1789                 :            :                 if (magic == ext4_sb_encoding_map[i].magic)
    1790                 :            :                         break;
    1791                 :            : 
    1792                 :            :         if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
    1793                 :            :                 return -EINVAL;
    1794                 :            : 
    1795                 :            :         *encoding = &ext4_sb_encoding_map[i];
    1796                 :            :         *flags = le16_to_cpu(es->s_encoding_flags);
    1797                 :            : 
    1798                 :            :         return 0;
    1799                 :            : }
    1800                 :            : #endif
    1801                 :            : 
    1802                 :          0 : static int handle_mount_opt(struct super_block *sb, char *opt, int token,
    1803                 :            :                             substring_t *args, unsigned long *journal_devnum,
    1804                 :            :                             unsigned int *journal_ioprio, int is_remount)
    1805                 :            : {
    1806                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    1807                 :            :         const struct mount_opts *m;
    1808                 :            :         kuid_t uid;
    1809                 :            :         kgid_t gid;
    1810                 :          0 :         int arg = 0;
    1811                 :            : 
    1812                 :            : #ifdef CONFIG_QUOTA
    1813                 :          0 :         if (token == Opt_usrjquota)
    1814                 :          0 :                 return set_qf_name(sb, USRQUOTA, &args[0]);
    1815                 :          0 :         else if (token == Opt_grpjquota)
    1816                 :          0 :                 return set_qf_name(sb, GRPQUOTA, &args[0]);
    1817                 :          0 :         else if (token == Opt_offusrjquota)
    1818                 :          0 :                 return clear_qf_name(sb, USRQUOTA);
    1819                 :          0 :         else if (token == Opt_offgrpjquota)
    1820                 :          0 :                 return clear_qf_name(sb, GRPQUOTA);
    1821                 :            : #endif
    1822                 :          0 :         switch (token) {
    1823                 :            :         case Opt_noacl:
    1824                 :            :         case Opt_nouser_xattr:
    1825                 :          0 :                 ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
    1826                 :          0 :                 break;
    1827                 :            :         case Opt_sb:
    1828                 :            :                 return 1;       /* handled by get_sb_block() */
    1829                 :            :         case Opt_removed:
    1830                 :          0 :                 ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
    1831                 :          0 :                 return 1;
    1832                 :            :         case Opt_abort:
    1833                 :          0 :                 sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
    1834                 :          0 :                 return 1;
    1835                 :            :         case Opt_i_version:
    1836                 :          0 :                 sb->s_flags |= SB_I_VERSION;
    1837                 :          0 :                 return 1;
    1838                 :            :         case Opt_lazytime:
    1839                 :          0 :                 sb->s_flags |= SB_LAZYTIME;
    1840                 :          0 :                 return 1;
    1841                 :            :         case Opt_nolazytime:
    1842                 :          0 :                 sb->s_flags &= ~SB_LAZYTIME;
    1843                 :          0 :                 return 1;
    1844                 :            :         }
    1845                 :            : 
    1846                 :          0 :         for (m = ext4_mount_opts; m->token != Opt_err; m++)
    1847                 :          0 :                 if (token == m->token)
    1848                 :            :                         break;
    1849                 :            : 
    1850                 :          0 :         if (m->token == Opt_err) {
    1851                 :          0 :                 ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
    1852                 :            :                          "or missing value", opt);
    1853                 :          0 :                 return -1;
    1854                 :            :         }
    1855                 :            : 
    1856                 :          0 :         if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
    1857                 :          0 :                 ext4_msg(sb, KERN_ERR,
    1858                 :            :                          "Mount option \"%s\" incompatible with ext2", opt);
    1859                 :          0 :                 return -1;
    1860                 :            :         }
    1861                 :          0 :         if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
    1862                 :          0 :                 ext4_msg(sb, KERN_ERR,
    1863                 :            :                          "Mount option \"%s\" incompatible with ext3", opt);
    1864                 :          0 :                 return -1;
    1865                 :            :         }
    1866                 :            : 
    1867                 :          0 :         if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
    1868                 :            :                 return -1;
    1869                 :          0 :         if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
    1870                 :            :                 return -1;
    1871                 :          0 :         if (m->flags & MOPT_EXPLICIT) {
    1872                 :          0 :                 if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
    1873                 :          0 :                         set_opt2(sb, EXPLICIT_DELALLOC);
    1874                 :          0 :                 } else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
    1875                 :          0 :                         set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
    1876                 :            :                 } else
    1877                 :            :                         return -1;
    1878                 :            :         }
    1879                 :          0 :         if (m->flags & MOPT_CLEAR_ERR)
    1880                 :          0 :                 clear_opt(sb, ERRORS_MASK);
    1881                 :          0 :         if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
    1882                 :          0 :                 ext4_msg(sb, KERN_ERR, "Cannot change quota "
    1883                 :            :                          "options when quota turned on");
    1884                 :          0 :                 return -1;
    1885                 :            :         }
    1886                 :            : 
    1887                 :          0 :         if (m->flags & MOPT_NOSUPPORT) {
    1888                 :          0 :                 ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
    1889                 :          0 :         } else if (token == Opt_commit) {
    1890                 :          0 :                 if (arg == 0)
    1891                 :          0 :                         arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
    1892                 :          0 :                 else if (arg > INT_MAX / HZ) {
    1893                 :          0 :                         ext4_msg(sb, KERN_ERR,
    1894                 :            :                                  "Invalid commit interval %d, "
    1895                 :            :                                  "must be smaller than %d",
    1896                 :            :                                  arg, INT_MAX / HZ);
    1897                 :          0 :                         return -1;
    1898                 :            :                 }
    1899                 :          0 :                 sbi->s_commit_interval = HZ * arg;
    1900                 :          0 :         } else if (token == Opt_debug_want_extra_isize) {
    1901                 :          0 :                 if ((arg & 1) ||
    1902                 :          0 :                     (arg < 4) ||
    1903                 :          0 :                     (arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) {
    1904                 :          0 :                         ext4_msg(sb, KERN_ERR,
    1905                 :            :                                  "Invalid want_extra_isize %d", arg);
    1906                 :          0 :                         return -1;
    1907                 :            :                 }
    1908                 :          0 :                 sbi->s_want_extra_isize = arg;
    1909                 :          0 :         } else if (token == Opt_max_batch_time) {
    1910                 :          0 :                 sbi->s_max_batch_time = arg;
    1911                 :          0 :         } else if (token == Opt_min_batch_time) {
    1912                 :          0 :                 sbi->s_min_batch_time = arg;
    1913                 :          0 :         } else if (token == Opt_inode_readahead_blks) {
    1914                 :          0 :                 if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
    1915                 :          0 :                         ext4_msg(sb, KERN_ERR,
    1916                 :            :                                  "EXT4-fs: inode_readahead_blks must be "
    1917                 :            :                                  "0 or a power of 2 smaller than 2^31");
    1918                 :          0 :                         return -1;
    1919                 :            :                 }
    1920                 :          0 :                 sbi->s_inode_readahead_blks = arg;
    1921                 :          0 :         } else if (token == Opt_init_itable) {
    1922                 :          0 :                 set_opt(sb, INIT_INODE_TABLE);
    1923                 :          0 :                 if (!args->from)
    1924                 :          0 :                         arg = EXT4_DEF_LI_WAIT_MULT;
    1925                 :          0 :                 sbi->s_li_wait_mult = arg;
    1926                 :          0 :         } else if (token == Opt_max_dir_size_kb) {
    1927                 :          0 :                 sbi->s_max_dir_size_kb = arg;
    1928                 :          0 :         } else if (token == Opt_stripe) {
    1929                 :          0 :                 sbi->s_stripe = arg;
    1930                 :          0 :         } else if (token == Opt_resuid) {
    1931                 :          0 :                 uid = make_kuid(current_user_ns(), arg);
    1932                 :          0 :                 if (!uid_valid(uid)) {
    1933                 :          0 :                         ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
    1934                 :          0 :                         return -1;
    1935                 :            :                 }
    1936                 :          0 :                 sbi->s_resuid = uid;
    1937                 :          0 :         } else if (token == Opt_resgid) {
    1938                 :          0 :                 gid = make_kgid(current_user_ns(), arg);
    1939                 :          0 :                 if (!gid_valid(gid)) {
    1940                 :          0 :                         ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
    1941                 :          0 :                         return -1;
    1942                 :            :                 }
    1943                 :          0 :                 sbi->s_resgid = gid;
    1944                 :          0 :         } else if (token == Opt_journal_dev) {
    1945                 :          0 :                 if (is_remount) {
    1946                 :          0 :                         ext4_msg(sb, KERN_ERR,
    1947                 :            :                                  "Cannot specify journal on remount");
    1948                 :          0 :                         return -1;
    1949                 :            :                 }
    1950                 :          0 :                 *journal_devnum = arg;
    1951                 :          0 :         } else if (token == Opt_journal_path) {
    1952                 :            :                 char *journal_path;
    1953                 :            :                 struct inode *journal_inode;
    1954                 :            :                 struct path path;
    1955                 :            :                 int error;
    1956                 :            : 
    1957                 :          0 :                 if (is_remount) {
    1958                 :          0 :                         ext4_msg(sb, KERN_ERR,
    1959                 :            :                                  "Cannot specify journal on remount");
    1960                 :          0 :                         return -1;
    1961                 :            :                 }
    1962                 :          0 :                 journal_path = match_strdup(&args[0]);
    1963                 :          0 :                 if (!journal_path) {
    1964                 :          0 :                         ext4_msg(sb, KERN_ERR, "error: could not dup "
    1965                 :            :                                 "journal device string");
    1966                 :          0 :                         return -1;
    1967                 :            :                 }
    1968                 :            : 
    1969                 :          0 :                 error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
    1970                 :          0 :                 if (error) {
    1971                 :          0 :                         ext4_msg(sb, KERN_ERR, "error: could not find "
    1972                 :            :                                 "journal device path: error %d", error);
    1973                 :          0 :                         kfree(journal_path);
    1974                 :          0 :                         return -1;
    1975                 :            :                 }
    1976                 :            : 
    1977                 :          0 :                 journal_inode = d_inode(path.dentry);
    1978                 :          0 :                 if (!S_ISBLK(journal_inode->i_mode)) {
    1979                 :          0 :                         ext4_msg(sb, KERN_ERR, "error: journal path %s "
    1980                 :            :                                 "is not a block device", journal_path);
    1981                 :          0 :                         path_put(&path);
    1982                 :          0 :                         kfree(journal_path);
    1983                 :          0 :                         return -1;
    1984                 :            :                 }
    1985                 :            : 
    1986                 :          0 :                 *journal_devnum = new_encode_dev(journal_inode->i_rdev);
    1987                 :          0 :                 path_put(&path);
    1988                 :          0 :                 kfree(journal_path);
    1989                 :          0 :         } else if (token == Opt_journal_ioprio) {
    1990                 :          0 :                 if (arg > 7) {
    1991                 :          0 :                         ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
    1992                 :            :                                  " (must be 0-7)");
    1993                 :          0 :                         return -1;
    1994                 :            :                 }
    1995                 :          0 :                 *journal_ioprio =
    1996                 :          0 :                         IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
    1997                 :          0 :         } else if (token == Opt_test_dummy_encryption) {
    1998                 :            : #ifdef CONFIG_FS_ENCRYPTION
    1999                 :          0 :                 sbi->s_mount_flags |= EXT4_MF_TEST_DUMMY_ENCRYPTION;
    2000                 :          0 :                 ext4_msg(sb, KERN_WARNING,
    2001                 :            :                          "Test dummy encryption mode enabled");
    2002                 :            : #else
    2003                 :            :                 ext4_msg(sb, KERN_WARNING,
    2004                 :            :                          "Test dummy encryption mount option ignored");
    2005                 :            : #endif
    2006                 :          0 :         } else if (m->flags & MOPT_DATAJ) {
    2007                 :          0 :                 if (is_remount) {
    2008                 :          0 :                         if (!sbi->s_journal)
    2009                 :          0 :                                 ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
    2010                 :          0 :                         else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
    2011                 :          0 :                                 ext4_msg(sb, KERN_ERR,
    2012                 :            :                                          "Cannot change data mode on remount");
    2013                 :          0 :                                 return -1;
    2014                 :            :                         }
    2015                 :            :                 } else {
    2016                 :          0 :                         clear_opt(sb, DATA_FLAGS);
    2017                 :          0 :                         sbi->s_mount_opt |= m->mount_opt;
    2018                 :            :                 }
    2019                 :            : #ifdef CONFIG_QUOTA
    2020                 :          0 :         } else if (m->flags & MOPT_QFMT) {
    2021                 :          0 :                 if (sb_any_quota_loaded(sb) &&
    2022                 :          0 :                     sbi->s_jquota_fmt != m->mount_opt) {
    2023                 :          0 :                         ext4_msg(sb, KERN_ERR, "Cannot change journaled "
    2024                 :            :                                  "quota options when quota turned on");
    2025                 :          0 :                         return -1;
    2026                 :            :                 }
    2027                 :          0 :                 if (ext4_has_feature_quota(sb)) {
    2028                 :          0 :                         ext4_msg(sb, KERN_INFO,
    2029                 :            :                                  "Quota format mount options ignored "
    2030                 :            :                                  "when QUOTA feature is enabled");
    2031                 :          0 :                         return 1;
    2032                 :            :                 }
    2033                 :          0 :                 sbi->s_jquota_fmt = m->mount_opt;
    2034                 :            : #endif
    2035                 :          0 :         } else if (token == Opt_dax) {
    2036                 :            : #ifdef CONFIG_FS_DAX
    2037                 :            :                 if (is_remount && test_opt(sb, DAX)) {
    2038                 :            :                         ext4_msg(sb, KERN_ERR, "can't mount with "
    2039                 :            :                                 "both data=journal and dax");
    2040                 :            :                         return -1;
    2041                 :            :                 }
    2042                 :            :                 if (is_remount && !(sbi->s_mount_opt & EXT4_MOUNT_DAX)) {
    2043                 :            :                         ext4_msg(sb, KERN_ERR, "can't change "
    2044                 :            :                                         "dax mount option while remounting");
    2045                 :            :                         return -1;
    2046                 :            :                 }
    2047                 :            :                 ext4_msg(sb, KERN_WARNING,
    2048                 :            :                 "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
    2049                 :            :                 sbi->s_mount_opt |= m->mount_opt;
    2050                 :            : #else
    2051                 :          0 :                 ext4_msg(sb, KERN_INFO, "dax option not supported");
    2052                 :          0 :                 return -1;
    2053                 :            : #endif
    2054                 :          0 :         } else if (token == Opt_data_err_abort) {
    2055                 :          0 :                 sbi->s_mount_opt |= m->mount_opt;
    2056                 :          0 :         } else if (token == Opt_data_err_ignore) {
    2057                 :          0 :                 sbi->s_mount_opt &= ~m->mount_opt;
    2058                 :            :         } else {
    2059                 :          0 :                 if (!args->from)
    2060                 :          0 :                         arg = 1;
    2061                 :          0 :                 if (m->flags & MOPT_CLEAR)
    2062                 :          0 :                         arg = !arg;
    2063                 :          0 :                 else if (unlikely(!(m->flags & MOPT_SET))) {
    2064                 :          0 :                         ext4_msg(sb, KERN_WARNING,
    2065                 :            :                                  "buggy handling of option %s", opt);
    2066                 :          0 :                         WARN_ON(1);
    2067                 :          0 :                         return -1;
    2068                 :            :                 }
    2069                 :          0 :                 if (arg != 0)
    2070                 :          0 :                         sbi->s_mount_opt |= m->mount_opt;
    2071                 :            :                 else
    2072                 :          0 :                         sbi->s_mount_opt &= ~m->mount_opt;
    2073                 :            :         }
    2074                 :            :         return 1;
    2075                 :            : }
    2076                 :            : 
    2077                 :          3 : static int parse_options(char *options, struct super_block *sb,
    2078                 :            :                          unsigned long *journal_devnum,
    2079                 :            :                          unsigned int *journal_ioprio,
    2080                 :            :                          int is_remount)
    2081                 :            : {
    2082                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    2083                 :            :         char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
    2084                 :            :         substring_t args[MAX_OPT_ARGS];
    2085                 :            :         int token;
    2086                 :            : 
    2087                 :          3 :         if (!options)
    2088                 :            :                 return 1;
    2089                 :            : 
    2090                 :          0 :         while ((p = strsep(&options, ",")) != NULL) {
    2091                 :          0 :                 if (!*p)
    2092                 :          0 :                         continue;
    2093                 :            :                 /*
    2094                 :            :                  * Initialize args struct so we know whether arg was
    2095                 :            :                  * found; some options take optional arguments.
    2096                 :            :                  */
    2097                 :          0 :                 args[0].to = args[0].from = NULL;
    2098                 :          0 :                 token = match_token(p, tokens, args);
    2099                 :          0 :                 if (handle_mount_opt(sb, p, token, args, journal_devnum,
    2100                 :            :                                      journal_ioprio, is_remount) < 0)
    2101                 :            :                         return 0;
    2102                 :            :         }
    2103                 :            : #ifdef CONFIG_QUOTA
    2104                 :            :         /*
    2105                 :            :          * We do the test below only for project quotas. 'usrquota' and
    2106                 :            :          * 'grpquota' mount options are allowed even without quota feature
    2107                 :            :          * to support legacy quotas in quota files.
    2108                 :            :          */
    2109                 :          0 :         if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
    2110                 :          0 :                 ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
    2111                 :            :                          "Cannot enable project quota enforcement.");
    2112                 :          0 :                 return 0;
    2113                 :            :         }
    2114                 :            :         usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
    2115                 :            :         grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
    2116                 :          0 :         if (usr_qf_name || grp_qf_name) {
    2117                 :          0 :                 if (test_opt(sb, USRQUOTA) && usr_qf_name)
    2118                 :          0 :                         clear_opt(sb, USRQUOTA);
    2119                 :            : 
    2120                 :          0 :                 if (test_opt(sb, GRPQUOTA) && grp_qf_name)
    2121                 :          0 :                         clear_opt(sb, GRPQUOTA);
    2122                 :            : 
    2123                 :          0 :                 if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
    2124                 :          0 :                         ext4_msg(sb, KERN_ERR, "old and new quota "
    2125                 :            :                                         "format mixing");
    2126                 :          0 :                         return 0;
    2127                 :            :                 }
    2128                 :            : 
    2129                 :          0 :                 if (!sbi->s_jquota_fmt) {
    2130                 :          0 :                         ext4_msg(sb, KERN_ERR, "journaled quota format "
    2131                 :            :                                         "not specified");
    2132                 :          0 :                         return 0;
    2133                 :            :                 }
    2134                 :            :         }
    2135                 :            : #endif
    2136                 :          0 :         if (test_opt(sb, DIOREAD_NOLOCK)) {
    2137                 :          0 :                 int blocksize =
    2138                 :          0 :                         BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
    2139                 :            : 
    2140                 :          0 :                 if (blocksize < PAGE_SIZE) {
    2141                 :          0 :                         ext4_msg(sb, KERN_ERR, "can't mount with "
    2142                 :            :                                  "dioread_nolock if block size != PAGE_SIZE");
    2143                 :          0 :                         return 0;
    2144                 :            :                 }
    2145                 :            :         }
    2146                 :            :         return 1;
    2147                 :            : }
    2148                 :            : 
    2149                 :          3 : static inline void ext4_show_quota_options(struct seq_file *seq,
    2150                 :            :                                            struct super_block *sb)
    2151                 :            : {
    2152                 :            : #if defined(CONFIG_QUOTA)
    2153                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    2154                 :            :         char *usr_qf_name, *grp_qf_name;
    2155                 :            : 
    2156                 :          3 :         if (sbi->s_jquota_fmt) {
    2157                 :            :                 char *fmtname = "";
    2158                 :            : 
    2159                 :          0 :                 switch (sbi->s_jquota_fmt) {
    2160                 :            :                 case QFMT_VFS_OLD:
    2161                 :            :                         fmtname = "vfsold";
    2162                 :            :                         break;
    2163                 :            :                 case QFMT_VFS_V0:
    2164                 :            :                         fmtname = "vfsv0";
    2165                 :            :                         break;
    2166                 :            :                 case QFMT_VFS_V1:
    2167                 :            :                         fmtname = "vfsv1";
    2168                 :            :                         break;
    2169                 :            :                 }
    2170                 :          0 :                 seq_printf(seq, ",jqfmt=%s", fmtname);
    2171                 :            :         }
    2172                 :            : 
    2173                 :            :         rcu_read_lock();
    2174                 :            :         usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
    2175                 :            :         grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
    2176                 :          3 :         if (usr_qf_name)
    2177                 :          0 :                 seq_show_option(seq, "usrjquota", usr_qf_name);
    2178                 :          3 :         if (grp_qf_name)
    2179                 :          0 :                 seq_show_option(seq, "grpjquota", grp_qf_name);
    2180                 :            :         rcu_read_unlock();
    2181                 :            : #endif
    2182                 :          3 : }
    2183                 :            : 
    2184                 :          0 : static const char *token2str(int token)
    2185                 :            : {
    2186                 :            :         const struct match_token *t;
    2187                 :            : 
    2188                 :          0 :         for (t = tokens; t->token != Opt_err; t++)
    2189                 :          0 :                 if (t->token == token && !strchr(t->pattern, '='))
    2190                 :            :                         break;
    2191                 :          0 :         return t->pattern;
    2192                 :            : }
    2193                 :            : 
    2194                 :            : /*
    2195                 :            :  * Show an option if
    2196                 :            :  *  - it's set to a non-default value OR
    2197                 :            :  *  - if the per-sb default is different from the global default
    2198                 :            :  */
    2199                 :          3 : static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
    2200                 :            :                               int nodefs)
    2201                 :            : {
    2202                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    2203                 :          3 :         struct ext4_super_block *es = sbi->s_es;
    2204                 :          3 :         int def_errors, def_mount_opt = sbi->s_def_mount_opt;
    2205                 :            :         const struct mount_opts *m;
    2206                 :          3 :         char sep = nodefs ? '\n' : ',';
    2207                 :            : 
    2208                 :            : #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
    2209                 :            : #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
    2210                 :            : 
    2211                 :          3 :         if (sbi->s_sb_block != 1)
    2212                 :          0 :                 SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
    2213                 :            : 
    2214                 :          3 :         for (m = ext4_mount_opts; m->token != Opt_err; m++) {
    2215                 :          3 :                 int want_set = m->flags & MOPT_SET;
    2216                 :          3 :                 if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
    2217                 :          3 :                     (m->flags & MOPT_CLEAR_ERR))
    2218                 :          3 :                         continue;
    2219                 :          3 :                 if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
    2220                 :          3 :                         continue; /* skip if same as the default */
    2221                 :          1 :                 if ((want_set &&
    2222                 :          1 :                      (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
    2223                 :          0 :                     (!want_set && (sbi->s_mount_opt & m->mount_opt)))
    2224                 :          0 :                         continue; /* select Opt_noFoo vs Opt_Foo */
    2225                 :          1 :                 SEQ_OPTS_PRINT("%s", token2str(m->token));
    2226                 :            :         }
    2227                 :            : 
    2228                 :          3 :         if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
    2229                 :          3 :             le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
    2230                 :          3 :                 SEQ_OPTS_PRINT("resuid=%u",
    2231                 :            :                                 from_kuid_munged(&init_user_ns, sbi->s_resuid));
    2232                 :          3 :         if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
    2233                 :          3 :             le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
    2234                 :          3 :                 SEQ_OPTS_PRINT("resgid=%u",
    2235                 :            :                                 from_kgid_munged(&init_user_ns, sbi->s_resgid));
    2236                 :          3 :         def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
    2237                 :          3 :         if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
    2238                 :          0 :                 SEQ_OPTS_PUTS("errors=remount-ro");
    2239                 :          3 :         if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
    2240                 :          0 :                 SEQ_OPTS_PUTS("errors=continue");
    2241                 :          3 :         if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
    2242                 :          0 :                 SEQ_OPTS_PUTS("errors=panic");
    2243                 :          3 :         if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
    2244                 :          3 :                 SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
    2245                 :          3 :         if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
    2246                 :          3 :                 SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
    2247                 :          3 :         if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
    2248                 :          3 :                 SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
    2249                 :          3 :         if (sb->s_flags & SB_I_VERSION)
    2250                 :          0 :                 SEQ_OPTS_PUTS("i_version");
    2251                 :          3 :         if (nodefs || sbi->s_stripe)
    2252                 :          3 :                 SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
    2253                 :          3 :         if (nodefs || EXT4_MOUNT_DATA_FLAGS &
    2254                 :          3 :                         (sbi->s_mount_opt ^ def_mount_opt)) {
    2255                 :          0 :                 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
    2256                 :          0 :                         SEQ_OPTS_PUTS("data=journal");
    2257                 :          0 :                 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
    2258                 :          0 :                         SEQ_OPTS_PUTS("data=ordered");
    2259                 :          0 :                 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
    2260                 :          0 :                         SEQ_OPTS_PUTS("data=writeback");
    2261                 :            :         }
    2262                 :          3 :         if (nodefs ||
    2263                 :          3 :             sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
    2264                 :          3 :                 SEQ_OPTS_PRINT("inode_readahead_blks=%u",
    2265                 :            :                                sbi->s_inode_readahead_blks);
    2266                 :            : 
    2267                 :          3 :         if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
    2268                 :          3 :                        (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
    2269                 :          3 :                 SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
    2270                 :          3 :         if (nodefs || sbi->s_max_dir_size_kb)
    2271                 :          1 :                 SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
    2272                 :          3 :         if (test_opt(sb, DATA_ERR_ABORT))
    2273                 :          0 :                 SEQ_OPTS_PUTS("data_err=abort");
    2274                 :          3 :         if (DUMMY_ENCRYPTION_ENABLED(sbi))
    2275                 :          0 :                 SEQ_OPTS_PUTS("test_dummy_encryption");
    2276                 :            : 
    2277                 :          3 :         ext4_show_quota_options(seq, sb);
    2278                 :          3 :         return 0;
    2279                 :            : }
    2280                 :            : 
    2281                 :          3 : static int ext4_show_options(struct seq_file *seq, struct dentry *root)
    2282                 :            : {
    2283                 :          3 :         return _ext4_show_options(seq, root->d_sb, 0);
    2284                 :            : }
    2285                 :            : 
    2286                 :          0 : int ext4_seq_options_show(struct seq_file *seq, void *offset)
    2287                 :            : {
    2288                 :          0 :         struct super_block *sb = seq->private;
    2289                 :            :         int rc;
    2290                 :            : 
    2291                 :          0 :         seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
    2292                 :          0 :         rc = _ext4_show_options(seq, sb, 1);
    2293                 :          0 :         seq_puts(seq, "\n");
    2294                 :          0 :         return rc;
    2295                 :            : }
    2296                 :            : 
    2297                 :          3 : static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
    2298                 :            :                             int read_only)
    2299                 :            : {
    2300                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    2301                 :            :         int err = 0;
    2302                 :            : 
    2303                 :          3 :         if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
    2304                 :          0 :                 ext4_msg(sb, KERN_ERR, "revision level too high, "
    2305                 :            :                          "forcing read-only mode");
    2306                 :            :                 err = -EROFS;
    2307                 :          0 :                 goto done;
    2308                 :            :         }
    2309                 :          3 :         if (read_only)
    2310                 :            :                 goto done;
    2311                 :          3 :         if (!(sbi->s_mount_state & EXT4_VALID_FS))
    2312                 :          0 :                 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
    2313                 :            :                          "running e2fsck is recommended");
    2314                 :          3 :         else if (sbi->s_mount_state & EXT4_ERROR_FS)
    2315                 :          0 :                 ext4_msg(sb, KERN_WARNING,
    2316                 :            :                          "warning: mounting fs with errors, "
    2317                 :            :                          "running e2fsck is recommended");
    2318                 :          3 :         else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
    2319                 :          0 :                  le16_to_cpu(es->s_mnt_count) >=
    2320                 :            :                  (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
    2321                 :          0 :                 ext4_msg(sb, KERN_WARNING,
    2322                 :            :                          "warning: maximal mount count reached, "
    2323                 :            :                          "running e2fsck is recommended");
    2324                 :          3 :         else if (le32_to_cpu(es->s_checkinterval) &&
    2325                 :          0 :                  (ext4_get_tstamp(es, s_lastcheck) +
    2326                 :          0 :                   le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
    2327                 :          0 :                 ext4_msg(sb, KERN_WARNING,
    2328                 :            :                          "warning: checktime reached, "
    2329                 :            :                          "running e2fsck is recommended");
    2330                 :          3 :         if (!sbi->s_journal)
    2331                 :          0 :                 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
    2332                 :          3 :         if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
    2333                 :          0 :                 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
    2334                 :            :         le16_add_cpu(&es->s_mnt_count, 1);
    2335                 :            :         ext4_update_tstamp(es, s_mtime);
    2336                 :          3 :         if (sbi->s_journal)
    2337                 :            :                 ext4_set_feature_journal_needs_recovery(sb);
    2338                 :            : 
    2339                 :          3 :         err = ext4_commit_super(sb, 1);
    2340                 :            : done:
    2341                 :          3 :         if (test_opt(sb, DEBUG))
    2342                 :          0 :                 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
    2343                 :            :                                 "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
    2344                 :            :                         sb->s_blocksize,
    2345                 :            :                         sbi->s_groups_count,
    2346                 :            :                         EXT4_BLOCKS_PER_GROUP(sb),
    2347                 :            :                         EXT4_INODES_PER_GROUP(sb),
    2348                 :            :                         sbi->s_mount_opt, sbi->s_mount_opt2);
    2349                 :            : 
    2350                 :            :         cleancache_init_fs(sb);
    2351                 :          3 :         return err;
    2352                 :            : }
    2353                 :            : 
    2354                 :          3 : int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
    2355                 :            : {
    2356                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    2357                 :            :         struct flex_groups **old_groups, **new_groups;
    2358                 :            :         int size, i, j;
    2359                 :            : 
    2360                 :          3 :         if (!sbi->s_log_groups_per_flex)
    2361                 :            :                 return 0;
    2362                 :            : 
    2363                 :          3 :         size = ext4_flex_group(sbi, ngroup - 1) + 1;
    2364                 :          3 :         if (size <= sbi->s_flex_groups_allocated)
    2365                 :            :                 return 0;
    2366                 :            : 
    2367                 :          3 :         new_groups = kvzalloc(roundup_pow_of_two(size *
    2368                 :            :                               sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
    2369                 :          3 :         if (!new_groups) {
    2370                 :          0 :                 ext4_msg(sb, KERN_ERR,
    2371                 :            :                          "not enough memory for %d flex group pointers", size);
    2372                 :          0 :                 return -ENOMEM;
    2373                 :            :         }
    2374                 :          3 :         for (i = sbi->s_flex_groups_allocated; i < size; i++) {
    2375                 :          3 :                 new_groups[i] = kvzalloc(roundup_pow_of_two(
    2376                 :            :                                          sizeof(struct flex_groups)),
    2377                 :            :                                          GFP_KERNEL);
    2378                 :          3 :                 if (!new_groups[i]) {
    2379                 :          0 :                         for (j = sbi->s_flex_groups_allocated; j < i; j++)
    2380                 :          0 :                                 kvfree(new_groups[j]);
    2381                 :          0 :                         kvfree(new_groups);
    2382                 :          0 :                         ext4_msg(sb, KERN_ERR,
    2383                 :            :                                  "not enough memory for %d flex groups", size);
    2384                 :          0 :                         return -ENOMEM;
    2385                 :            :                 }
    2386                 :            :         }
    2387                 :            :         rcu_read_lock();
    2388                 :          3 :         old_groups = rcu_dereference(sbi->s_flex_groups);
    2389                 :          3 :         if (old_groups)
    2390                 :          0 :                 memcpy(new_groups, old_groups,
    2391                 :          0 :                        (sbi->s_flex_groups_allocated *
    2392                 :            :                         sizeof(struct flex_groups *)));
    2393                 :            :         rcu_read_unlock();
    2394                 :          3 :         rcu_assign_pointer(sbi->s_flex_groups, new_groups);
    2395                 :          3 :         sbi->s_flex_groups_allocated = size;
    2396                 :          3 :         if (old_groups)
    2397                 :          0 :                 ext4_kvfree_array_rcu(old_groups);
    2398                 :            :         return 0;
    2399                 :            : }
    2400                 :            : 
    2401                 :          3 : static int ext4_fill_flex_info(struct super_block *sb)
    2402                 :            : {
    2403                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    2404                 :            :         struct ext4_group_desc *gdp = NULL;
    2405                 :            :         struct flex_groups *fg;
    2406                 :            :         ext4_group_t flex_group;
    2407                 :            :         int i, err;
    2408                 :            : 
    2409                 :          3 :         sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
    2410                 :          3 :         if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
    2411                 :          0 :                 sbi->s_log_groups_per_flex = 0;
    2412                 :          0 :                 return 1;
    2413                 :            :         }
    2414                 :            : 
    2415                 :          3 :         err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
    2416                 :          3 :         if (err)
    2417                 :            :                 goto failed;
    2418                 :            : 
    2419                 :          3 :         for (i = 0; i < sbi->s_groups_count; i++) {
    2420                 :          3 :                 gdp = ext4_get_group_desc(sb, i, NULL);
    2421                 :            : 
    2422                 :            :                 flex_group = ext4_flex_group(sbi, i);
    2423                 :          3 :                 fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
    2424                 :          3 :                 atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
    2425                 :          3 :                 atomic64_add(ext4_free_group_clusters(sb, gdp),
    2426                 :            :                              &fg->free_clusters);
    2427                 :          3 :                 atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
    2428                 :            :         }
    2429                 :            : 
    2430                 :            :         return 1;
    2431                 :            : failed:
    2432                 :            :         return 0;
    2433                 :            : }
    2434                 :            : 
    2435                 :          0 : static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
    2436                 :            :                                    struct ext4_group_desc *gdp)
    2437                 :            : {
    2438                 :            :         int offset = offsetof(struct ext4_group_desc, bg_checksum);
    2439                 :            :         __u16 crc = 0;
    2440                 :          0 :         __le32 le_group = cpu_to_le32(block_group);
    2441                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    2442                 :            : 
    2443                 :          0 :         if (ext4_has_metadata_csum(sbi->s_sb)) {
    2444                 :            :                 /* Use new metadata_csum algorithm */
    2445                 :            :                 __u32 csum32;
    2446                 :          0 :                 __u16 dummy_csum = 0;
    2447                 :            : 
    2448                 :          0 :                 csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
    2449                 :            :                                      sizeof(le_group));
    2450                 :          0 :                 csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
    2451                 :          0 :                 csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
    2452                 :            :                                      sizeof(dummy_csum));
    2453                 :            :                 offset += sizeof(dummy_csum);
    2454                 :          0 :                 if (offset < sbi->s_desc_size)
    2455                 :          0 :                         csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
    2456                 :          0 :                                              sbi->s_desc_size - offset);
    2457                 :            : 
    2458                 :          0 :                 crc = csum32 & 0xFFFF;
    2459                 :            :                 goto out;
    2460                 :            :         }
    2461                 :            : 
    2462                 :            :         /* old crc16 code */
    2463                 :          0 :         if (!ext4_has_feature_gdt_csum(sb))
    2464                 :            :                 return 0;
    2465                 :            : 
    2466                 :          0 :         crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
    2467                 :          0 :         crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
    2468                 :          0 :         crc = crc16(crc, (__u8 *)gdp, offset);
    2469                 :            :         offset += sizeof(gdp->bg_checksum); /* skip checksum */
    2470                 :            :         /* for checksum of struct ext4_group_desc do the rest...*/
    2471                 :          0 :         if (ext4_has_feature_64bit(sb) &&
    2472                 :          0 :             offset < le16_to_cpu(sbi->s_es->s_desc_size))
    2473                 :          0 :                 crc = crc16(crc, (__u8 *)gdp + offset,
    2474                 :          0 :                             le16_to_cpu(sbi->s_es->s_desc_size) -
    2475                 :            :                                 offset);
    2476                 :            : 
    2477                 :            : out:
    2478                 :          0 :         return cpu_to_le16(crc);
    2479                 :            : }
    2480                 :            : 
    2481                 :          3 : int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
    2482                 :            :                                 struct ext4_group_desc *gdp)
    2483                 :            : {
    2484                 :          3 :         if (ext4_has_group_desc_csum(sb) &&
    2485                 :          0 :             (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
    2486                 :            :                 return 0;
    2487                 :            : 
    2488                 :            :         return 1;
    2489                 :            : }
    2490                 :            : 
    2491                 :          3 : void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
    2492                 :            :                               struct ext4_group_desc *gdp)
    2493                 :            : {
    2494                 :          3 :         if (!ext4_has_group_desc_csum(sb))
    2495                 :          3 :                 return;
    2496                 :          0 :         gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
    2497                 :            : }
    2498                 :            : 
    2499                 :            : /* Called at mount-time, super-block is locked */
    2500                 :          3 : static int ext4_check_descriptors(struct super_block *sb,
    2501                 :            :                                   ext4_fsblk_t sb_block,
    2502                 :            :                                   ext4_group_t *first_not_zeroed)
    2503                 :            : {
    2504                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    2505                 :          3 :         ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
    2506                 :            :         ext4_fsblk_t last_block;
    2507                 :          3 :         ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
    2508                 :            :         ext4_fsblk_t block_bitmap;
    2509                 :            :         ext4_fsblk_t inode_bitmap;
    2510                 :            :         ext4_fsblk_t inode_table;
    2511                 :            :         int flexbg_flag = 0;
    2512                 :          3 :         ext4_group_t i, grp = sbi->s_groups_count;
    2513                 :            : 
    2514                 :          3 :         if (ext4_has_feature_flex_bg(sb))
    2515                 :            :                 flexbg_flag = 1;
    2516                 :            : 
    2517                 :            :         ext4_debug("Checking group descriptors");
    2518                 :            : 
    2519                 :          3 :         for (i = 0; i < sbi->s_groups_count; i++) {
    2520                 :          3 :                 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
    2521                 :            : 
    2522                 :          3 :                 if (i == sbi->s_groups_count - 1 || flexbg_flag)
    2523                 :          3 :                         last_block = ext4_blocks_count(sbi->s_es) - 1;
    2524                 :            :                 else
    2525                 :          0 :                         last_block = first_block +
    2526                 :          0 :                                 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
    2527                 :            : 
    2528                 :          3 :                 if ((grp == sbi->s_groups_count) &&
    2529                 :          3 :                    !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
    2530                 :            :                         grp = i;
    2531                 :            : 
    2532                 :            :                 block_bitmap = ext4_block_bitmap(sb, gdp);
    2533                 :          3 :                 if (block_bitmap == sb_block) {
    2534                 :          0 :                         ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
    2535                 :            :                                  "Block bitmap for group %u overlaps "
    2536                 :            :                                  "superblock", i);
    2537                 :          0 :                         if (!sb_rdonly(sb))
    2538                 :            :                                 return 0;
    2539                 :            :                 }
    2540                 :          3 :                 if (block_bitmap >= sb_block + 1 &&
    2541                 :            :                     block_bitmap <= last_bg_block) {
    2542                 :          0 :                         ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
    2543                 :            :                                  "Block bitmap for group %u overlaps "
    2544                 :            :                                  "block group descriptors", i);
    2545                 :          0 :                         if (!sb_rdonly(sb))
    2546                 :            :                                 return 0;
    2547                 :            :                 }
    2548                 :          3 :                 if (block_bitmap < first_block || block_bitmap > last_block) {
    2549                 :          0 :                         ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
    2550                 :            :                                "Block bitmap for group %u not in group "
    2551                 :            :                                "(block %llu)!", i, block_bitmap);
    2552                 :          0 :                         return 0;
    2553                 :            :                 }
    2554                 :            :                 inode_bitmap = ext4_inode_bitmap(sb, gdp);
    2555                 :          3 :                 if (inode_bitmap == sb_block) {
    2556                 :          0 :                         ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
    2557                 :            :                                  "Inode bitmap for group %u overlaps "
    2558                 :            :                                  "superblock", i);
    2559                 :          0 :                         if (!sb_rdonly(sb))
    2560                 :            :                                 return 0;
    2561                 :            :                 }
    2562                 :          3 :                 if (inode_bitmap >= sb_block + 1 &&
    2563                 :            :                     inode_bitmap <= last_bg_block) {
    2564                 :          0 :                         ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
    2565                 :            :                                  "Inode bitmap for group %u overlaps "
    2566                 :            :                                  "block group descriptors", i);
    2567                 :          0 :                         if (!sb_rdonly(sb))
    2568                 :            :                                 return 0;
    2569                 :            :                 }
    2570                 :          3 :                 if (inode_bitmap < first_block || inode_bitmap > last_block) {
    2571                 :          0 :                         ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
    2572                 :            :                                "Inode bitmap for group %u not in group "
    2573                 :            :                                "(block %llu)!", i, inode_bitmap);
    2574                 :          0 :                         return 0;
    2575                 :            :                 }
    2576                 :            :                 inode_table = ext4_inode_table(sb, gdp);
    2577                 :          3 :                 if (inode_table == sb_block) {
    2578                 :          0 :                         ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
    2579                 :            :                                  "Inode table for group %u overlaps "
    2580                 :            :                                  "superblock", i);
    2581                 :          0 :                         if (!sb_rdonly(sb))
    2582                 :            :                                 return 0;
    2583                 :            :                 }
    2584                 :          3 :                 if (inode_table >= sb_block + 1 &&
    2585                 :            :                     inode_table <= last_bg_block) {
    2586                 :          0 :                         ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
    2587                 :            :                                  "Inode table for group %u overlaps "
    2588                 :            :                                  "block group descriptors", i);
    2589                 :          0 :                         if (!sb_rdonly(sb))
    2590                 :            :                                 return 0;
    2591                 :            :                 }
    2592                 :          3 :                 if (inode_table < first_block ||
    2593                 :          3 :                     inode_table + sbi->s_itb_per_group - 1 > last_block) {
    2594                 :          0 :                         ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
    2595                 :            :                                "Inode table for group %u not in group "
    2596                 :            :                                "(block %llu)!", i, inode_table);
    2597                 :          0 :                         return 0;
    2598                 :            :                 }
    2599                 :          3 :                 ext4_lock_group(sb, i);
    2600                 :          3 :                 if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
    2601                 :          0 :                         ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
    2602                 :            :                                  "Checksum for group %u failed (%u!=%u)",
    2603                 :            :                                  i, le16_to_cpu(ext4_group_desc_csum(sb, i,
    2604                 :            :                                      gdp)), le16_to_cpu(gdp->bg_checksum));
    2605                 :          0 :                         if (!sb_rdonly(sb)) {
    2606                 :            :                                 ext4_unlock_group(sb, i);
    2607                 :          0 :                                 return 0;
    2608                 :            :                         }
    2609                 :            :                 }
    2610                 :            :                 ext4_unlock_group(sb, i);
    2611                 :          3 :                 if (!flexbg_flag)
    2612                 :          0 :                         first_block += EXT4_BLOCKS_PER_GROUP(sb);
    2613                 :            :         }
    2614                 :          3 :         if (NULL != first_not_zeroed)
    2615                 :          3 :                 *first_not_zeroed = grp;
    2616                 :            :         return 1;
    2617                 :            : }
    2618                 :            : 
    2619                 :            : /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
    2620                 :            :  * the superblock) which were deleted from all directories, but held open by
    2621                 :            :  * a process at the time of a crash.  We walk the list and try to delete these
    2622                 :            :  * inodes at recovery time (only with a read-write filesystem).
    2623                 :            :  *
    2624                 :            :  * In order to keep the orphan inode chain consistent during traversal (in
    2625                 :            :  * case of crash during recovery), we link each inode into the superblock
    2626                 :            :  * orphan list_head and handle it the same way as an inode deletion during
    2627                 :            :  * normal operation (which journals the operations for us).
    2628                 :            :  *
    2629                 :            :  * We only do an iget() and an iput() on each inode, which is very safe if we
    2630                 :            :  * accidentally point at an in-use or already deleted inode.  The worst that
    2631                 :            :  * can happen in this case is that we get a "bit already cleared" message from
    2632                 :            :  * ext4_free_inode().  The only reason we would point at a wrong inode is if
    2633                 :            :  * e2fsck was run on this filesystem, and it must have already done the orphan
    2634                 :            :  * inode cleanup for us, so we can safely abort without any further action.
    2635                 :            :  */
    2636                 :          3 : static void ext4_orphan_cleanup(struct super_block *sb,
    2637                 :            :                                 struct ext4_super_block *es)
    2638                 :            : {
    2639                 :          3 :         unsigned int s_flags = sb->s_flags;
    2640                 :            :         int ret, nr_orphans = 0, nr_truncates = 0;
    2641                 :            : #ifdef CONFIG_QUOTA
    2642                 :            :         int quota_update = 0;
    2643                 :            :         int i;
    2644                 :            : #endif
    2645                 :          3 :         if (!es->s_last_orphan) {
    2646                 :            :                 jbd_debug(4, "no orphan inodes to clean up\n");
    2647                 :            :                 return;
    2648                 :            :         }
    2649                 :            : 
    2650                 :          0 :         if (bdev_read_only(sb->s_bdev)) {
    2651                 :          0 :                 ext4_msg(sb, KERN_ERR, "write access "
    2652                 :            :                         "unavailable, skipping orphan cleanup");
    2653                 :          0 :                 return;
    2654                 :            :         }
    2655                 :            : 
    2656                 :            :         /* Check if feature set would not allow a r/w mount */
    2657                 :          0 :         if (!ext4_feature_set_ok(sb, 0)) {
    2658                 :          0 :                 ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
    2659                 :            :                          "unknown ROCOMPAT features");
    2660                 :          0 :                 return;
    2661                 :            :         }
    2662                 :            : 
    2663                 :          0 :         if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
    2664                 :            :                 /* don't clear list on RO mount w/ errors */
    2665                 :          0 :                 if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
    2666                 :          0 :                         ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
    2667                 :            :                                   "clearing orphan list.\n");
    2668                 :          0 :                         es->s_last_orphan = 0;
    2669                 :            :                 }
    2670                 :            :                 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
    2671                 :            :                 return;
    2672                 :            :         }
    2673                 :            : 
    2674                 :          0 :         if (s_flags & SB_RDONLY) {
    2675                 :          0 :                 ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
    2676                 :          0 :                 sb->s_flags &= ~SB_RDONLY;
    2677                 :            :         }
    2678                 :            : #ifdef CONFIG_QUOTA
    2679                 :            :         /* Needed for iput() to work correctly and not trash data */
    2680                 :          0 :         sb->s_flags |= SB_ACTIVE;
    2681                 :            : 
    2682                 :            :         /*
    2683                 :            :          * Turn on quotas which were not enabled for read-only mounts if
    2684                 :            :          * filesystem has quota feature, so that they are updated correctly.
    2685                 :            :          */
    2686                 :          0 :         if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
    2687                 :          0 :                 int ret = ext4_enable_quotas(sb);
    2688                 :            : 
    2689                 :          0 :                 if (!ret)
    2690                 :            :                         quota_update = 1;
    2691                 :            :                 else
    2692                 :          0 :                         ext4_msg(sb, KERN_ERR,
    2693                 :            :                                 "Cannot turn on quotas: error %d", ret);
    2694                 :            :         }
    2695                 :            : 
    2696                 :            :         /* Turn on journaled quotas used for old sytle */
    2697                 :          0 :         for (i = 0; i < EXT4_MAXQUOTAS; i++) {
    2698                 :          0 :                 if (EXT4_SB(sb)->s_qf_names[i]) {
    2699                 :            :                         int ret = ext4_quota_on_mount(sb, i);
    2700                 :            : 
    2701                 :          0 :                         if (!ret)
    2702                 :            :                                 quota_update = 1;
    2703                 :            :                         else
    2704                 :          0 :                                 ext4_msg(sb, KERN_ERR,
    2705                 :            :                                         "Cannot turn on journaled "
    2706                 :            :                                         "quota: type %d: error %d", i, ret);
    2707                 :            :                 }
    2708                 :            :         }
    2709                 :            : #endif
    2710                 :            : 
    2711                 :          0 :         while (es->s_last_orphan) {
    2712                 :            :                 struct inode *inode;
    2713                 :            : 
    2714                 :            :                 /*
    2715                 :            :                  * We may have encountered an error during cleanup; if
    2716                 :            :                  * so, skip the rest.
    2717                 :            :                  */
    2718                 :          0 :                 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
    2719                 :            :                         jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
    2720                 :          0 :                         es->s_last_orphan = 0;
    2721                 :          0 :                         break;
    2722                 :            :                 }
    2723                 :            : 
    2724                 :          0 :                 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
    2725                 :          0 :                 if (IS_ERR(inode)) {
    2726                 :          0 :                         es->s_last_orphan = 0;
    2727                 :          0 :                         break;
    2728                 :            :                 }
    2729                 :            : 
    2730                 :          0 :                 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
    2731                 :          0 :                 dquot_initialize(inode);
    2732                 :          0 :                 if (inode->i_nlink) {
    2733                 :          0 :                         if (test_opt(sb, DEBUG))
    2734                 :          0 :                                 ext4_msg(sb, KERN_DEBUG,
    2735                 :            :                                         "%s: truncating inode %lu to %lld bytes",
    2736                 :            :                                         __func__, inode->i_ino, inode->i_size);
    2737                 :            :                         jbd_debug(2, "truncating inode %lu to %lld bytes\n",
    2738                 :            :                                   inode->i_ino, inode->i_size);
    2739                 :            :                         inode_lock(inode);
    2740                 :          0 :                         truncate_inode_pages(inode->i_mapping, inode->i_size);
    2741                 :          0 :                         ret = ext4_truncate(inode);
    2742                 :          0 :                         if (ret)
    2743                 :          0 :                                 ext4_std_error(inode->i_sb, ret);
    2744                 :            :                         inode_unlock(inode);
    2745                 :          0 :                         nr_truncates++;
    2746                 :            :                 } else {
    2747                 :          0 :                         if (test_opt(sb, DEBUG))
    2748                 :          0 :                                 ext4_msg(sb, KERN_DEBUG,
    2749                 :            :                                         "%s: deleting unreferenced inode %lu",
    2750                 :            :                                         __func__, inode->i_ino);
    2751                 :            :                         jbd_debug(2, "deleting unreferenced inode %lu\n",
    2752                 :            :                                   inode->i_ino);
    2753                 :          0 :                         nr_orphans++;
    2754                 :            :                 }
    2755                 :          0 :                 iput(inode);  /* The delete magic happens here! */
    2756                 :            :         }
    2757                 :            : 
    2758                 :            : #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
    2759                 :            : 
    2760                 :          0 :         if (nr_orphans)
    2761                 :          0 :                 ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
    2762                 :            :                        PLURAL(nr_orphans));
    2763                 :          0 :         if (nr_truncates)
    2764                 :          0 :                 ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
    2765                 :            :                        PLURAL(nr_truncates));
    2766                 :            : #ifdef CONFIG_QUOTA
    2767                 :            :         /* Turn off quotas if they were enabled for orphan cleanup */
    2768                 :          0 :         if (quota_update) {
    2769                 :          0 :                 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
    2770                 :          0 :                         if (sb_dqopt(sb)->files[i])
    2771                 :          0 :                                 dquot_quota_off(sb, i);
    2772                 :            :                 }
    2773                 :            :         }
    2774                 :            : #endif
    2775                 :          0 :         sb->s_flags = s_flags; /* Restore SB_RDONLY status */
    2776                 :            : }
    2777                 :            : 
    2778                 :            : /*
    2779                 :            :  * Maximal extent format file size.
    2780                 :            :  * Resulting logical blkno at s_maxbytes must fit in our on-disk
    2781                 :            :  * extent format containers, within a sector_t, and within i_blocks
    2782                 :            :  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
    2783                 :            :  * so that won't be a limiting factor.
    2784                 :            :  *
    2785                 :            :  * However there is other limiting factor. We do store extents in the form
    2786                 :            :  * of starting block and length, hence the resulting length of the extent
    2787                 :            :  * covering maximum file size must fit into on-disk format containers as
    2788                 :            :  * well. Given that length is always by 1 unit bigger than max unit (because
    2789                 :            :  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
    2790                 :            :  *
    2791                 :            :  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
    2792                 :            :  */
    2793                 :            : static loff_t ext4_max_size(int blkbits, int has_huge_files)
    2794                 :            : {
    2795                 :            :         loff_t res;
    2796                 :            :         loff_t upper_limit = MAX_LFS_FILESIZE;
    2797                 :            : 
    2798                 :            :         BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
    2799                 :            : 
    2800                 :          3 :         if (!has_huge_files) {
    2801                 :            :                 upper_limit = (1LL << 32) - 1;
    2802                 :            : 
    2803                 :            :                 /* total blocks in file system block size */
    2804                 :          3 :                 upper_limit >>= (blkbits - 9);
    2805                 :          3 :                 upper_limit <<= blkbits;
    2806                 :            :         }
    2807                 :            : 
    2808                 :            :         /*
    2809                 :            :          * 32-bit extent-start container, ee_block. We lower the maxbytes
    2810                 :            :          * by one fs block, so ee_len can cover the extent of maximum file
    2811                 :            :          * size
    2812                 :            :          */
    2813                 :            :         res = (1LL << 32) - 1;
    2814                 :          3 :         res <<= blkbits;
    2815                 :            : 
    2816                 :            :         /* Sanity check against vm- & vfs- imposed limits */
    2817                 :          3 :         if (res > upper_limit)
    2818                 :            :                 res = upper_limit;
    2819                 :            : 
    2820                 :            :         return res;
    2821                 :            : }
    2822                 :            : 
    2823                 :            : /*
    2824                 :            :  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
    2825                 :            :  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
    2826                 :            :  * We need to be 1 filesystem block less than the 2^48 sector limit.
    2827                 :            :  */
    2828                 :          3 : static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
    2829                 :            : {
    2830                 :            :         loff_t res = EXT4_NDIR_BLOCKS;
    2831                 :            :         int meta_blocks;
    2832                 :            :         loff_t upper_limit;
    2833                 :            :         /* This is calculated to be the largest file size for a dense, block
    2834                 :            :          * mapped file such that the file's total number of 512-byte sectors,
    2835                 :            :          * including data and all indirect blocks, does not exceed (2^48 - 1).
    2836                 :            :          *
    2837                 :            :          * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
    2838                 :            :          * number of 512-byte sectors of the file.
    2839                 :            :          */
    2840                 :            : 
    2841                 :          3 :         if (!has_huge_files) {
    2842                 :            :                 /*
    2843                 :            :                  * !has_huge_files or implies that the inode i_block field
    2844                 :            :                  * represents total file blocks in 2^32 512-byte sectors ==
    2845                 :            :                  * size of vfs inode i_blocks * 8
    2846                 :            :                  */
    2847                 :            :                 upper_limit = (1LL << 32) - 1;
    2848                 :            : 
    2849                 :            :                 /* total blocks in file system block size */
    2850                 :          3 :                 upper_limit >>= (bits - 9);
    2851                 :            : 
    2852                 :            :         } else {
    2853                 :            :                 /*
    2854                 :            :                  * We use 48 bit ext4_inode i_blocks
    2855                 :            :                  * With EXT4_HUGE_FILE_FL set the i_blocks
    2856                 :            :                  * represent total number of blocks in
    2857                 :            :                  * file system block size
    2858                 :            :                  */
    2859                 :            :                 upper_limit = (1LL << 48) - 1;
    2860                 :            : 
    2861                 :            :         }
    2862                 :            : 
    2863                 :            :         /* indirect blocks */
    2864                 :            :         meta_blocks = 1;
    2865                 :            :         /* double indirect blocks */
    2866                 :          3 :         meta_blocks += 1 + (1LL << (bits-2));
    2867                 :            :         /* tripple indirect blocks */
    2868                 :          3 :         meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
    2869                 :            : 
    2870                 :          3 :         upper_limit -= meta_blocks;
    2871                 :          3 :         upper_limit <<= bits;
    2872                 :            : 
    2873                 :          3 :         res += 1LL << (bits-2);
    2874                 :          3 :         res += 1LL << (2*(bits-2));
    2875                 :          3 :         res += 1LL << (3*(bits-2));
    2876                 :          3 :         res <<= bits;
    2877                 :          3 :         if (res > upper_limit)
    2878                 :            :                 res = upper_limit;
    2879                 :            : 
    2880                 :          3 :         if (res > MAX_LFS_FILESIZE)
    2881                 :            :                 res = MAX_LFS_FILESIZE;
    2882                 :            : 
    2883                 :          3 :         return res;
    2884                 :            : }
    2885                 :            : 
    2886                 :          3 : static ext4_fsblk_t descriptor_loc(struct super_block *sb,
    2887                 :            :                                    ext4_fsblk_t logical_sb_block, int nr)
    2888                 :            : {
    2889                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    2890                 :            :         ext4_group_t bg, first_meta_bg;
    2891                 :            :         int has_super = 0;
    2892                 :            : 
    2893                 :          3 :         first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
    2894                 :            : 
    2895                 :          3 :         if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
    2896                 :          3 :                 return logical_sb_block + nr + 1;
    2897                 :          0 :         bg = sbi->s_desc_per_block * nr;
    2898                 :          0 :         if (ext4_bg_has_super(sb, bg))
    2899                 :            :                 has_super = 1;
    2900                 :            : 
    2901                 :            :         /*
    2902                 :            :          * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
    2903                 :            :          * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
    2904                 :            :          * on modern mke2fs or blksize > 1k on older mke2fs) then we must
    2905                 :            :          * compensate.
    2906                 :            :          */
    2907                 :          0 :         if (sb->s_blocksize == 1024 && nr == 0 &&
    2908                 :          0 :             le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
    2909                 :          0 :                 has_super++;
    2910                 :            : 
    2911                 :          0 :         return (has_super + ext4_group_first_block_no(sb, bg));
    2912                 :            : }
    2913                 :            : 
    2914                 :            : /**
    2915                 :            :  * ext4_get_stripe_size: Get the stripe size.
    2916                 :            :  * @sbi: In memory super block info
    2917                 :            :  *
    2918                 :            :  * If we have specified it via mount option, then
    2919                 :            :  * use the mount option value. If the value specified at mount time is
    2920                 :            :  * greater than the blocks per group use the super block value.
    2921                 :            :  * If the super block value is greater than blocks per group return 0.
    2922                 :            :  * Allocator needs it be less than blocks per group.
    2923                 :            :  *
    2924                 :            :  */
    2925                 :          3 : static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
    2926                 :            : {
    2927                 :          3 :         unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
    2928                 :          3 :         unsigned long stripe_width =
    2929                 :            :                         le32_to_cpu(sbi->s_es->s_raid_stripe_width);
    2930                 :            :         int ret;
    2931                 :            : 
    2932                 :          3 :         if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
    2933                 :          0 :                 ret = sbi->s_stripe;
    2934                 :          3 :         else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
    2935                 :          0 :                 ret = stripe_width;
    2936                 :          3 :         else if (stride && stride <= sbi->s_blocks_per_group)
    2937                 :          0 :                 ret = stride;
    2938                 :            :         else
    2939                 :            :                 ret = 0;
    2940                 :            : 
    2941                 :            :         /*
    2942                 :            :          * If the stripe width is 1, this makes no sense and
    2943                 :            :          * we set it to 0 to turn off stripe handling code.
    2944                 :            :          */
    2945                 :          3 :         if (ret <= 1)
    2946                 :            :                 ret = 0;
    2947                 :            : 
    2948                 :          3 :         return ret;
    2949                 :            : }
    2950                 :            : 
    2951                 :            : /*
    2952                 :            :  * Check whether this filesystem can be mounted based on
    2953                 :            :  * the features present and the RDONLY/RDWR mount requested.
    2954                 :            :  * Returns 1 if this filesystem can be mounted as requested,
    2955                 :            :  * 0 if it cannot be.
    2956                 :            :  */
    2957                 :          3 : static int ext4_feature_set_ok(struct super_block *sb, int readonly)
    2958                 :            : {
    2959                 :          3 :         if (ext4_has_unknown_ext4_incompat_features(sb)) {
    2960                 :          0 :                 ext4_msg(sb, KERN_ERR,
    2961                 :            :                         "Couldn't mount because of "
    2962                 :            :                         "unsupported optional features (%x)",
    2963                 :            :                         (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
    2964                 :            :                         ~EXT4_FEATURE_INCOMPAT_SUPP));
    2965                 :          0 :                 return 0;
    2966                 :            :         }
    2967                 :            : 
    2968                 :            : #ifndef CONFIG_UNICODE
    2969                 :          3 :         if (ext4_has_feature_casefold(sb)) {
    2970                 :          0 :                 ext4_msg(sb, KERN_ERR,
    2971                 :            :                          "Filesystem with casefold feature cannot be "
    2972                 :            :                          "mounted without CONFIG_UNICODE");
    2973                 :          0 :                 return 0;
    2974                 :            :         }
    2975                 :            : #endif
    2976                 :            : 
    2977                 :          3 :         if (readonly)
    2978                 :            :                 return 1;
    2979                 :            : 
    2980                 :          3 :         if (ext4_has_feature_readonly(sb)) {
    2981                 :          0 :                 ext4_msg(sb, KERN_INFO, "filesystem is read-only");
    2982                 :          0 :                 sb->s_flags |= SB_RDONLY;
    2983                 :          0 :                 return 1;
    2984                 :            :         }
    2985                 :            : 
    2986                 :            :         /* Check that feature set is OK for a read-write mount */
    2987                 :          3 :         if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
    2988                 :          0 :                 ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
    2989                 :            :                          "unsupported optional features (%x)",
    2990                 :            :                          (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
    2991                 :            :                                 ~EXT4_FEATURE_RO_COMPAT_SUPP));
    2992                 :          0 :                 return 0;
    2993                 :            :         }
    2994                 :          3 :         if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
    2995                 :          0 :                 ext4_msg(sb, KERN_ERR,
    2996                 :            :                          "Can't support bigalloc feature without "
    2997                 :            :                          "extents feature\n");
    2998                 :          0 :                 return 0;
    2999                 :            :         }
    3000                 :            : 
    3001                 :            : #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
    3002                 :            :         if (!readonly && (ext4_has_feature_quota(sb) ||
    3003                 :            :                           ext4_has_feature_project(sb))) {
    3004                 :            :                 ext4_msg(sb, KERN_ERR,
    3005                 :            :                          "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
    3006                 :            :                 return 0;
    3007                 :            :         }
    3008                 :            : #endif  /* CONFIG_QUOTA */
    3009                 :            :         return 1;
    3010                 :            : }
    3011                 :            : 
    3012                 :            : /*
    3013                 :            :  * This function is called once a day if we have errors logged
    3014                 :            :  * on the file system
    3015                 :            :  */
    3016                 :          0 : static void print_daily_error_info(struct timer_list *t)
    3017                 :            : {
    3018                 :            :         struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
    3019                 :          0 :         struct super_block *sb = sbi->s_sb;
    3020                 :          0 :         struct ext4_super_block *es = sbi->s_es;
    3021                 :            : 
    3022                 :          0 :         if (es->s_error_count)
    3023                 :            :                 /* fsck newer than v1.41.13 is needed to clean this condition. */
    3024                 :          0 :                 ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
    3025                 :            :                          le32_to_cpu(es->s_error_count));
    3026                 :          0 :         if (es->s_first_error_time) {
    3027                 :          0 :                 printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
    3028                 :          0 :                        sb->s_id,
    3029                 :            :                        ext4_get_tstamp(es, s_first_error_time),
    3030                 :            :                        (int) sizeof(es->s_first_error_func),
    3031                 :          0 :                        es->s_first_error_func,
    3032                 :            :                        le32_to_cpu(es->s_first_error_line));
    3033                 :          0 :                 if (es->s_first_error_ino)
    3034                 :          0 :                         printk(KERN_CONT ": inode %u",
    3035                 :            :                                le32_to_cpu(es->s_first_error_ino));
    3036                 :          0 :                 if (es->s_first_error_block)
    3037                 :          0 :                         printk(KERN_CONT ": block %llu", (unsigned long long)
    3038                 :            :                                le64_to_cpu(es->s_first_error_block));
    3039                 :          0 :                 printk(KERN_CONT "\n");
    3040                 :            :         }
    3041                 :          0 :         if (es->s_last_error_time) {
    3042                 :          0 :                 printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
    3043                 :          0 :                        sb->s_id,
    3044                 :            :                        ext4_get_tstamp(es, s_last_error_time),
    3045                 :            :                        (int) sizeof(es->s_last_error_func),
    3046                 :          0 :                        es->s_last_error_func,
    3047                 :            :                        le32_to_cpu(es->s_last_error_line));
    3048                 :          0 :                 if (es->s_last_error_ino)
    3049                 :          0 :                         printk(KERN_CONT ": inode %u",
    3050                 :            :                                le32_to_cpu(es->s_last_error_ino));
    3051                 :          0 :                 if (es->s_last_error_block)
    3052                 :          0 :                         printk(KERN_CONT ": block %llu", (unsigned long long)
    3053                 :            :                                le64_to_cpu(es->s_last_error_block));
    3054                 :          0 :                 printk(KERN_CONT "\n");
    3055                 :            :         }
    3056                 :          0 :         mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
    3057                 :          0 : }
    3058                 :            : 
    3059                 :            : /* Find next suitable group and run ext4_init_inode_table */
    3060                 :          0 : static int ext4_run_li_request(struct ext4_li_request *elr)
    3061                 :            : {
    3062                 :            :         struct ext4_group_desc *gdp = NULL;
    3063                 :            :         ext4_group_t group, ngroups;
    3064                 :            :         struct super_block *sb;
    3065                 :            :         unsigned long timeout = 0;
    3066                 :            :         int ret = 0;
    3067                 :            : 
    3068                 :          0 :         sb = elr->lr_super;
    3069                 :          0 :         ngroups = EXT4_SB(sb)->s_groups_count;
    3070                 :            : 
    3071                 :          0 :         for (group = elr->lr_next_group; group < ngroups; group++) {
    3072                 :          0 :                 gdp = ext4_get_group_desc(sb, group, NULL);
    3073                 :          0 :                 if (!gdp) {
    3074                 :            :                         ret = 1;
    3075                 :            :                         break;
    3076                 :            :                 }
    3077                 :            : 
    3078                 :          0 :                 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
    3079                 :            :                         break;
    3080                 :            :         }
    3081                 :            : 
    3082                 :          0 :         if (group >= ngroups)
    3083                 :            :                 ret = 1;
    3084                 :            : 
    3085                 :          0 :         if (!ret) {
    3086                 :          0 :                 timeout = jiffies;
    3087                 :          0 :                 ret = ext4_init_inode_table(sb, group,
    3088                 :          0 :                                             elr->lr_timeout ? 0 : 1);
    3089                 :          0 :                 if (elr->lr_timeout == 0) {
    3090                 :          0 :                         timeout = (jiffies - timeout) *
    3091                 :          0 :                                   elr->lr_sbi->s_li_wait_mult;
    3092                 :          0 :                         elr->lr_timeout = timeout;
    3093                 :            :                 }
    3094                 :          0 :                 elr->lr_next_sched = jiffies + elr->lr_timeout;
    3095                 :          0 :                 elr->lr_next_group = group + 1;
    3096                 :            :         }
    3097                 :          0 :         return ret;
    3098                 :            : }
    3099                 :            : 
    3100                 :            : /*
    3101                 :            :  * Remove lr_request from the list_request and free the
    3102                 :            :  * request structure. Should be called with li_list_mtx held
    3103                 :            :  */
    3104                 :          0 : static void ext4_remove_li_request(struct ext4_li_request *elr)
    3105                 :            : {
    3106                 :            :         struct ext4_sb_info *sbi;
    3107                 :            : 
    3108                 :          0 :         if (!elr)
    3109                 :          0 :                 return;
    3110                 :            : 
    3111                 :          0 :         sbi = elr->lr_sbi;
    3112                 :            : 
    3113                 :            :         list_del(&elr->lr_request);
    3114                 :          0 :         sbi->s_li_request = NULL;
    3115                 :          0 :         kfree(elr);
    3116                 :            : }
    3117                 :            : 
    3118                 :          0 : static void ext4_unregister_li_request(struct super_block *sb)
    3119                 :            : {
    3120                 :          0 :         mutex_lock(&ext4_li_mtx);
    3121                 :          0 :         if (!ext4_li_info) {
    3122                 :          0 :                 mutex_unlock(&ext4_li_mtx);
    3123                 :          0 :                 return;
    3124                 :            :         }
    3125                 :            : 
    3126                 :          0 :         mutex_lock(&ext4_li_info->li_list_mtx);
    3127                 :          0 :         ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
    3128                 :          0 :         mutex_unlock(&ext4_li_info->li_list_mtx);
    3129                 :          0 :         mutex_unlock(&ext4_li_mtx);
    3130                 :            : }
    3131                 :            : 
    3132                 :            : static struct task_struct *ext4_lazyinit_task;
    3133                 :            : 
    3134                 :            : /*
    3135                 :            :  * This is the function where ext4lazyinit thread lives. It walks
    3136                 :            :  * through the request list searching for next scheduled filesystem.
    3137                 :            :  * When such a fs is found, run the lazy initialization request
    3138                 :            :  * (ext4_rn_li_request) and keep track of the time spend in this
    3139                 :            :  * function. Based on that time we compute next schedule time of
    3140                 :            :  * the request. When walking through the list is complete, compute
    3141                 :            :  * next waking time and put itself into sleep.
    3142                 :            :  */
    3143                 :          0 : static int ext4_lazyinit_thread(void *arg)
    3144                 :            : {
    3145                 :            :         struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
    3146                 :            :         struct list_head *pos, *n;
    3147                 :            :         struct ext4_li_request *elr;
    3148                 :            :         unsigned long next_wakeup, cur;
    3149                 :            : 
    3150                 :          0 :         BUG_ON(NULL == eli);
    3151                 :            : 
    3152                 :            : cont_thread:
    3153                 :            :         while (true) {
    3154                 :            :                 next_wakeup = MAX_JIFFY_OFFSET;
    3155                 :            : 
    3156                 :          0 :                 mutex_lock(&eli->li_list_mtx);
    3157                 :          0 :                 if (list_empty(&eli->li_request_list)) {
    3158                 :          0 :                         mutex_unlock(&eli->li_list_mtx);
    3159                 :          0 :                         goto exit_thread;
    3160                 :            :                 }
    3161                 :          0 :                 list_for_each_safe(pos, n, &eli->li_request_list) {
    3162                 :            :                         int err = 0;
    3163                 :            :                         int progress = 0;
    3164                 :          0 :                         elr = list_entry(pos, struct ext4_li_request,
    3165                 :            :                                          lr_request);
    3166                 :            : 
    3167                 :          0 :                         if (time_before(jiffies, elr->lr_next_sched)) {
    3168                 :          0 :                                 if (time_before(elr->lr_next_sched, next_wakeup))
    3169                 :            :                                         next_wakeup = elr->lr_next_sched;
    3170                 :          0 :                                 continue;
    3171                 :            :                         }
    3172                 :          0 :                         if (down_read_trylock(&elr->lr_super->s_umount)) {
    3173                 :          0 :                                 if (sb_start_write_trylock(elr->lr_super)) {
    3174                 :            :                                         progress = 1;
    3175                 :            :                                         /*
    3176                 :            :                                          * We hold sb->s_umount, sb can not
    3177                 :            :                                          * be removed from the list, it is
    3178                 :            :                                          * now safe to drop li_list_mtx
    3179                 :            :                                          */
    3180                 :          0 :                                         mutex_unlock(&eli->li_list_mtx);
    3181                 :          0 :                                         err = ext4_run_li_request(elr);
    3182                 :          0 :                                         sb_end_write(elr->lr_super);
    3183                 :          0 :                                         mutex_lock(&eli->li_list_mtx);
    3184                 :          0 :                                         n = pos->next;
    3185                 :            :                                 }
    3186                 :          0 :                                 up_read((&elr->lr_super->s_umount));
    3187                 :            :                         }
    3188                 :            :                         /* error, remove the lazy_init job */
    3189                 :          0 :                         if (err) {
    3190                 :          0 :                                 ext4_remove_li_request(elr);
    3191                 :          0 :                                 continue;
    3192                 :            :                         }
    3193                 :          0 :                         if (!progress) {
    3194                 :          0 :                                 elr->lr_next_sched = jiffies +
    3195                 :          0 :                                         (prandom_u32()
    3196                 :          0 :                                          % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
    3197                 :            :                         }
    3198                 :          0 :                         if (time_before(elr->lr_next_sched, next_wakeup))
    3199                 :            :                                 next_wakeup = elr->lr_next_sched;
    3200                 :            :                 }
    3201                 :          0 :                 mutex_unlock(&eli->li_list_mtx);
    3202                 :            : 
    3203                 :            :                 try_to_freeze();
    3204                 :            : 
    3205                 :          0 :                 cur = jiffies;
    3206                 :          0 :                 if ((time_after_eq(cur, next_wakeup)) ||
    3207                 :            :                     (MAX_JIFFY_OFFSET == next_wakeup)) {
    3208                 :          0 :                         cond_resched();
    3209                 :          0 :                         continue;
    3210                 :            :                 }
    3211                 :            : 
    3212                 :          0 :                 schedule_timeout_interruptible(next_wakeup - cur);
    3213                 :            : 
    3214                 :          0 :                 if (kthread_should_stop()) {
    3215                 :          0 :                         ext4_clear_request_list();
    3216                 :          0 :                         goto exit_thread;
    3217                 :            :                 }
    3218                 :            :         }
    3219                 :            : 
    3220                 :            : exit_thread:
    3221                 :            :         /*
    3222                 :            :          * It looks like the request list is empty, but we need
    3223                 :            :          * to check it under the li_list_mtx lock, to prevent any
    3224                 :            :          * additions into it, and of course we should lock ext4_li_mtx
    3225                 :            :          * to atomically free the list and ext4_li_info, because at
    3226                 :            :          * this point another ext4 filesystem could be registering
    3227                 :            :          * new one.
    3228                 :            :          */
    3229                 :          0 :         mutex_lock(&ext4_li_mtx);
    3230                 :          0 :         mutex_lock(&eli->li_list_mtx);
    3231                 :          0 :         if (!list_empty(&eli->li_request_list)) {
    3232                 :          0 :                 mutex_unlock(&eli->li_list_mtx);
    3233                 :          0 :                 mutex_unlock(&ext4_li_mtx);
    3234                 :          0 :                 goto cont_thread;
    3235                 :            :         }
    3236                 :          0 :         mutex_unlock(&eli->li_list_mtx);
    3237                 :          0 :         kfree(ext4_li_info);
    3238                 :          0 :         ext4_li_info = NULL;
    3239                 :          0 :         mutex_unlock(&ext4_li_mtx);
    3240                 :            : 
    3241                 :          0 :         return 0;
    3242                 :            : }
    3243                 :            : 
    3244                 :          0 : static void ext4_clear_request_list(void)
    3245                 :            : {
    3246                 :            :         struct list_head *pos, *n;
    3247                 :            :         struct ext4_li_request *elr;
    3248                 :            : 
    3249                 :          0 :         mutex_lock(&ext4_li_info->li_list_mtx);
    3250                 :          0 :         list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
    3251                 :          0 :                 elr = list_entry(pos, struct ext4_li_request,
    3252                 :            :                                  lr_request);
    3253                 :          0 :                 ext4_remove_li_request(elr);
    3254                 :            :         }
    3255                 :          0 :         mutex_unlock(&ext4_li_info->li_list_mtx);
    3256                 :          0 : }
    3257                 :            : 
    3258                 :          0 : static int ext4_run_lazyinit_thread(void)
    3259                 :            : {
    3260                 :          0 :         ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
    3261                 :            :                                          ext4_li_info, "ext4lazyinit");
    3262                 :          0 :         if (IS_ERR(ext4_lazyinit_task)) {
    3263                 :            :                 int err = PTR_ERR(ext4_lazyinit_task);
    3264                 :          0 :                 ext4_clear_request_list();
    3265                 :          0 :                 kfree(ext4_li_info);
    3266                 :          0 :                 ext4_li_info = NULL;
    3267                 :          0 :                 printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
    3268                 :            :                                  "initialization thread\n",
    3269                 :            :                                  err);
    3270                 :          0 :                 return err;
    3271                 :            :         }
    3272                 :          0 :         ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
    3273                 :          0 :         return 0;
    3274                 :            : }
    3275                 :            : 
    3276                 :            : /*
    3277                 :            :  * Check whether it make sense to run itable init. thread or not.
    3278                 :            :  * If there is at least one uninitialized inode table, return
    3279                 :            :  * corresponding group number, else the loop goes through all
    3280                 :            :  * groups and return total number of groups.
    3281                 :            :  */
    3282                 :          3 : static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
    3283                 :            : {
    3284                 :          3 :         ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
    3285                 :            :         struct ext4_group_desc *gdp = NULL;
    3286                 :            : 
    3287                 :          3 :         if (!ext4_has_group_desc_csum(sb))
    3288                 :            :                 return ngroups;
    3289                 :            : 
    3290                 :          0 :         for (group = 0; group < ngroups; group++) {
    3291                 :          0 :                 gdp = ext4_get_group_desc(sb, group, NULL);
    3292                 :          0 :                 if (!gdp)
    3293                 :          0 :                         continue;
    3294                 :            : 
    3295                 :          0 :                 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
    3296                 :            :                         break;
    3297                 :            :         }
    3298                 :            : 
    3299                 :          0 :         return group;
    3300                 :            : }
    3301                 :            : 
    3302                 :          0 : static int ext4_li_info_new(void)
    3303                 :            : {
    3304                 :            :         struct ext4_lazy_init *eli = NULL;
    3305                 :            : 
    3306                 :          0 :         eli = kzalloc(sizeof(*eli), GFP_KERNEL);
    3307                 :          0 :         if (!eli)
    3308                 :            :                 return -ENOMEM;
    3309                 :            : 
    3310                 :          0 :         INIT_LIST_HEAD(&eli->li_request_list);
    3311                 :          0 :         mutex_init(&eli->li_list_mtx);
    3312                 :            : 
    3313                 :          0 :         eli->li_state |= EXT4_LAZYINIT_QUIT;
    3314                 :            : 
    3315                 :          0 :         ext4_li_info = eli;
    3316                 :            : 
    3317                 :          0 :         return 0;
    3318                 :            : }
    3319                 :            : 
    3320                 :          0 : static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
    3321                 :            :                                             ext4_group_t start)
    3322                 :            : {
    3323                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    3324                 :            :         struct ext4_li_request *elr;
    3325                 :            : 
    3326                 :          0 :         elr = kzalloc(sizeof(*elr), GFP_KERNEL);
    3327                 :          0 :         if (!elr)
    3328                 :            :                 return NULL;
    3329                 :            : 
    3330                 :          0 :         elr->lr_super = sb;
    3331                 :          0 :         elr->lr_sbi = sbi;
    3332                 :          0 :         elr->lr_next_group = start;
    3333                 :            : 
    3334                 :            :         /*
    3335                 :            :          * Randomize first schedule time of the request to
    3336                 :            :          * spread the inode table initialization requests
    3337                 :            :          * better.
    3338                 :            :          */
    3339                 :          0 :         elr->lr_next_sched = jiffies + (prandom_u32() %
    3340                 :            :                                 (EXT4_DEF_LI_MAX_START_DELAY * HZ));
    3341                 :          0 :         return elr;
    3342                 :            : }
    3343                 :            : 
    3344                 :          3 : int ext4_register_li_request(struct super_block *sb,
    3345                 :            :                              ext4_group_t first_not_zeroed)
    3346                 :            : {
    3347                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    3348                 :            :         struct ext4_li_request *elr = NULL;
    3349                 :          3 :         ext4_group_t ngroups = sbi->s_groups_count;
    3350                 :            :         int ret = 0;
    3351                 :            : 
    3352                 :          3 :         mutex_lock(&ext4_li_mtx);
    3353                 :          3 :         if (sbi->s_li_request != NULL) {
    3354                 :            :                 /*
    3355                 :            :                  * Reset timeout so it can be computed again, because
    3356                 :            :                  * s_li_wait_mult might have changed.
    3357                 :            :                  */
    3358                 :          0 :                 sbi->s_li_request->lr_timeout = 0;
    3359                 :          0 :                 goto out;
    3360                 :            :         }
    3361                 :            : 
    3362                 :          3 :         if (first_not_zeroed == ngroups || sb_rdonly(sb) ||
    3363                 :          0 :             !test_opt(sb, INIT_INODE_TABLE))
    3364                 :            :                 goto out;
    3365                 :            : 
    3366                 :          0 :         elr = ext4_li_request_new(sb, first_not_zeroed);
    3367                 :          0 :         if (!elr) {
    3368                 :            :                 ret = -ENOMEM;
    3369                 :            :                 goto out;
    3370                 :            :         }
    3371                 :            : 
    3372                 :          0 :         if (NULL == ext4_li_info) {
    3373                 :          0 :                 ret = ext4_li_info_new();
    3374                 :          0 :                 if (ret)
    3375                 :            :                         goto out;
    3376                 :            :         }
    3377                 :            : 
    3378                 :          0 :         mutex_lock(&ext4_li_info->li_list_mtx);
    3379                 :          0 :         list_add(&elr->lr_request, &ext4_li_info->li_request_list);
    3380                 :          0 :         mutex_unlock(&ext4_li_info->li_list_mtx);
    3381                 :            : 
    3382                 :          0 :         sbi->s_li_request = elr;
    3383                 :            :         /*
    3384                 :            :          * set elr to NULL here since it has been inserted to
    3385                 :            :          * the request_list and the removal and free of it is
    3386                 :            :          * handled by ext4_clear_request_list from now on.
    3387                 :            :          */
    3388                 :            :         elr = NULL;
    3389                 :            : 
    3390                 :          0 :         if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
    3391                 :          0 :                 ret = ext4_run_lazyinit_thread();
    3392                 :            :                 if (ret)
    3393                 :            :                         goto out;
    3394                 :            :         }
    3395                 :            : out:
    3396                 :          3 :         mutex_unlock(&ext4_li_mtx);
    3397                 :          3 :         if (ret)
    3398                 :          0 :                 kfree(elr);
    3399                 :          3 :         return ret;
    3400                 :            : }
    3401                 :            : 
    3402                 :            : /*
    3403                 :            :  * We do not need to lock anything since this is called on
    3404                 :            :  * module unload.
    3405                 :            :  */
    3406                 :          0 : static void ext4_destroy_lazyinit_thread(void)
    3407                 :            : {
    3408                 :            :         /*
    3409                 :            :          * If thread exited earlier
    3410                 :            :          * there's nothing to be done.
    3411                 :            :          */
    3412                 :          0 :         if (!ext4_li_info || !ext4_lazyinit_task)
    3413                 :          0 :                 return;
    3414                 :            : 
    3415                 :          0 :         kthread_stop(ext4_lazyinit_task);
    3416                 :            : }
    3417                 :            : 
    3418                 :          3 : static int set_journal_csum_feature_set(struct super_block *sb)
    3419                 :            : {
    3420                 :            :         int ret = 1;
    3421                 :            :         int compat, incompat;
    3422                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    3423                 :            : 
    3424                 :          3 :         if (ext4_has_metadata_csum(sb)) {
    3425                 :            :                 /* journal checksum v3 */
    3426                 :            :                 compat = 0;
    3427                 :            :                 incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
    3428                 :            :         } else {
    3429                 :            :                 /* journal checksum v1 */
    3430                 :            :                 compat = JBD2_FEATURE_COMPAT_CHECKSUM;
    3431                 :            :                 incompat = 0;
    3432                 :            :         }
    3433                 :            : 
    3434                 :          3 :         jbd2_journal_clear_features(sbi->s_journal,
    3435                 :            :                         JBD2_FEATURE_COMPAT_CHECKSUM, 0,
    3436                 :            :                         JBD2_FEATURE_INCOMPAT_CSUM_V3 |
    3437                 :            :                         JBD2_FEATURE_INCOMPAT_CSUM_V2);
    3438                 :          3 :         if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
    3439                 :          0 :                 ret = jbd2_journal_set_features(sbi->s_journal,
    3440                 :            :                                 compat, 0,
    3441                 :          0 :                                 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
    3442                 :            :                                 incompat);
    3443                 :          3 :         } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
    3444                 :          0 :                 ret = jbd2_journal_set_features(sbi->s_journal,
    3445                 :            :                                 compat, 0,
    3446                 :            :                                 incompat);
    3447                 :          0 :                 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
    3448                 :            :                                 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
    3449                 :            :         } else {
    3450                 :          3 :                 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
    3451                 :            :                                 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
    3452                 :            :         }
    3453                 :            : 
    3454                 :          3 :         return ret;
    3455                 :            : }
    3456                 :            : 
    3457                 :            : /*
    3458                 :            :  * Note: calculating the overhead so we can be compatible with
    3459                 :            :  * historical BSD practice is quite difficult in the face of
    3460                 :            :  * clusters/bigalloc.  This is because multiple metadata blocks from
    3461                 :            :  * different block group can end up in the same allocation cluster.
    3462                 :            :  * Calculating the exact overhead in the face of clustered allocation
    3463                 :            :  * requires either O(all block bitmaps) in memory or O(number of block
    3464                 :            :  * groups**2) in time.  We will still calculate the superblock for
    3465                 :            :  * older file systems --- and if we come across with a bigalloc file
    3466                 :            :  * system with zero in s_overhead_clusters the estimate will be close to
    3467                 :            :  * correct especially for very large cluster sizes --- but for newer
    3468                 :            :  * file systems, it's better to calculate this figure once at mkfs
    3469                 :            :  * time, and store it in the superblock.  If the superblock value is
    3470                 :            :  * present (even for non-bigalloc file systems), we will use it.
    3471                 :            :  */
    3472                 :          3 : static int count_overhead(struct super_block *sb, ext4_group_t grp,
    3473                 :            :                           char *buf)
    3474                 :            : {
    3475                 :            :         struct ext4_sb_info     *sbi = EXT4_SB(sb);
    3476                 :            :         struct ext4_group_desc  *gdp;
    3477                 :            :         ext4_fsblk_t            first_block, last_block, b;
    3478                 :            :         ext4_group_t            i, ngroups = ext4_get_groups_count(sb);
    3479                 :            :         int                     s, j, count = 0;
    3480                 :            : 
    3481                 :          3 :         if (!ext4_has_feature_bigalloc(sb))
    3482                 :          3 :                 return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
    3483                 :          3 :                         sbi->s_itb_per_group + 2);
    3484                 :            : 
    3485                 :          0 :         first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
    3486                 :          0 :                 (grp * EXT4_BLOCKS_PER_GROUP(sb));
    3487                 :          0 :         last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
    3488                 :          0 :         for (i = 0; i < ngroups; i++) {
    3489                 :          0 :                 gdp = ext4_get_group_desc(sb, i, NULL);
    3490                 :            :                 b = ext4_block_bitmap(sb, gdp);
    3491                 :          0 :                 if (b >= first_block && b <= last_block) {
    3492                 :          0 :                         ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
    3493                 :          0 :                         count++;
    3494                 :            :                 }
    3495                 :            :                 b = ext4_inode_bitmap(sb, gdp);
    3496                 :          0 :                 if (b >= first_block && b <= last_block) {
    3497                 :          0 :                         ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
    3498                 :          0 :                         count++;
    3499                 :            :                 }
    3500                 :            :                 b = ext4_inode_table(sb, gdp);
    3501                 :          0 :                 if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
    3502                 :          0 :                         for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
    3503                 :          0 :                                 int c = EXT4_B2C(sbi, b - first_block);
    3504                 :            :                                 ext4_set_bit(c, buf);
    3505                 :          0 :                                 count++;
    3506                 :            :                         }
    3507                 :          0 :                 if (i != grp)
    3508                 :          0 :                         continue;
    3509                 :            :                 s = 0;
    3510                 :          0 :                 if (ext4_bg_has_super(sb, grp)) {
    3511                 :            :                         ext4_set_bit(s++, buf);
    3512                 :          0 :                         count++;
    3513                 :            :                 }
    3514                 :          0 :                 j = ext4_bg_num_gdb(sb, grp);
    3515                 :          0 :                 if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
    3516                 :          0 :                         ext4_error(sb, "Invalid number of block group "
    3517                 :            :                                    "descriptor blocks: %d", j);
    3518                 :          0 :                         j = EXT4_BLOCKS_PER_GROUP(sb) - s;
    3519                 :            :                 }
    3520                 :          0 :                 count += j;
    3521                 :          0 :                 for (; j > 0; j--)
    3522                 :          0 :                         ext4_set_bit(EXT4_B2C(sbi, s++), buf);
    3523                 :            :         }
    3524                 :          0 :         if (!count)
    3525                 :            :                 return 0;
    3526                 :          0 :         return EXT4_CLUSTERS_PER_GROUP(sb) -
    3527                 :          0 :                 ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
    3528                 :            : }
    3529                 :            : 
    3530                 :            : /*
    3531                 :            :  * Compute the overhead and stash it in sbi->s_overhead
    3532                 :            :  */
    3533                 :          3 : int ext4_calculate_overhead(struct super_block *sb)
    3534                 :            : {
    3535                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    3536                 :          3 :         struct ext4_super_block *es = sbi->s_es;
    3537                 :            :         struct inode *j_inode;
    3538                 :          3 :         unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
    3539                 :            :         ext4_group_t i, ngroups = ext4_get_groups_count(sb);
    3540                 :            :         ext4_fsblk_t overhead = 0;
    3541                 :          3 :         char *buf = (char *) get_zeroed_page(GFP_NOFS);
    3542                 :            : 
    3543                 :          3 :         if (!buf)
    3544                 :            :                 return -ENOMEM;
    3545                 :            : 
    3546                 :            :         /*
    3547                 :            :          * Compute the overhead (FS structures).  This is constant
    3548                 :            :          * for a given filesystem unless the number of block groups
    3549                 :            :          * changes so we cache the previous value until it does.
    3550                 :            :          */
    3551                 :            : 
    3552                 :            :         /*
    3553                 :            :          * All of the blocks before first_data_block are overhead
    3554                 :            :          */
    3555                 :          3 :         overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
    3556                 :            : 
    3557                 :            :         /*
    3558                 :            :          * Add the overhead found in each block group
    3559                 :            :          */
    3560                 :          3 :         for (i = 0; i < ngroups; i++) {
    3561                 :            :                 int blks;
    3562                 :            : 
    3563                 :          3 :                 blks = count_overhead(sb, i, buf);
    3564                 :          3 :                 overhead += blks;
    3565                 :          3 :                 if (blks)
    3566                 :          3 :                         memset(buf, 0, PAGE_SIZE);
    3567                 :          3 :                 cond_resched();
    3568                 :            :         }
    3569                 :            : 
    3570                 :            :         /*
    3571                 :            :          * Add the internal journal blocks whether the journal has been
    3572                 :            :          * loaded or not
    3573                 :            :          */
    3574                 :          3 :         if (sbi->s_journal && !sbi->journal_bdev)
    3575                 :          3 :                 overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
    3576                 :          0 :         else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
    3577                 :            :                 /* j_inum for internal journal is non-zero */
    3578                 :          0 :                 j_inode = ext4_get_journal_inode(sb, j_inum);
    3579                 :          0 :                 if (j_inode) {
    3580                 :          0 :                         j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
    3581                 :          0 :                         overhead += EXT4_NUM_B2C(sbi, j_blocks);
    3582                 :          0 :                         iput(j_inode);
    3583                 :            :                 } else {
    3584                 :          0 :                         ext4_msg(sb, KERN_ERR, "can't get journal size");
    3585                 :            :                 }
    3586                 :            :         }
    3587                 :          3 :         sbi->s_overhead = overhead;
    3588                 :          3 :         smp_wmb();
    3589                 :          3 :         free_page((unsigned long) buf);
    3590                 :          3 :         return 0;
    3591                 :            : }
    3592                 :            : 
    3593                 :          3 : static void ext4_set_resv_clusters(struct super_block *sb)
    3594                 :            : {
    3595                 :            :         ext4_fsblk_t resv_clusters;
    3596                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    3597                 :            : 
    3598                 :            :         /*
    3599                 :            :          * There's no need to reserve anything when we aren't using extents.
    3600                 :            :          * The space estimates are exact, there are no unwritten extents,
    3601                 :            :          * hole punching doesn't need new metadata... This is needed especially
    3602                 :            :          * to keep ext2/3 backward compatibility.
    3603                 :            :          */
    3604                 :          3 :         if (!ext4_has_feature_extents(sb))
    3605                 :          3 :                 return;
    3606                 :            :         /*
    3607                 :            :          * By default we reserve 2% or 4096 clusters, whichever is smaller.
    3608                 :            :          * This should cover the situations where we can not afford to run
    3609                 :            :          * out of space like for example punch hole, or converting
    3610                 :            :          * unwritten extents in delalloc path. In most cases such
    3611                 :            :          * allocation would require 1, or 2 blocks, higher numbers are
    3612                 :            :          * very rare.
    3613                 :            :          */
    3614                 :          3 :         resv_clusters = (ext4_blocks_count(sbi->s_es) >>
    3615                 :          3 :                          sbi->s_cluster_bits);
    3616                 :            : 
    3617                 :          3 :         do_div(resv_clusters, 50);
    3618                 :          3 :         resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
    3619                 :            : 
    3620                 :          3 :         atomic64_set(&sbi->s_resv_clusters, resv_clusters);
    3621                 :            : }
    3622                 :            : 
    3623                 :          3 : static int ext4_fill_super(struct super_block *sb, void *data, int silent)
    3624                 :            : {
    3625                 :            :         struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
    3626                 :          3 :         char *orig_data = kstrdup(data, GFP_KERNEL);
    3627                 :            :         struct buffer_head *bh, **group_desc;
    3628                 :            :         struct ext4_super_block *es = NULL;
    3629                 :          3 :         struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
    3630                 :            :         struct flex_groups **flex_groups;
    3631                 :            :         ext4_fsblk_t block;
    3632                 :          3 :         ext4_fsblk_t sb_block = get_sb_block(&data);
    3633                 :            :         ext4_fsblk_t logical_sb_block;
    3634                 :            :         unsigned long offset = 0;
    3635                 :          3 :         unsigned long journal_devnum = 0;
    3636                 :            :         unsigned long def_mount_opts;
    3637                 :            :         struct inode *root;
    3638                 :            :         const char *descr;
    3639                 :            :         int ret = -ENOMEM;
    3640                 :            :         int blocksize, clustersize;
    3641                 :            :         unsigned int db_count;
    3642                 :            :         unsigned int i;
    3643                 :            :         int needs_recovery, has_huge_files, has_bigalloc;
    3644                 :            :         __u64 blocks_count;
    3645                 :            :         int err = 0;
    3646                 :          3 :         unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
    3647                 :            :         ext4_group_t first_not_zeroed;
    3648                 :            : 
    3649                 :          3 :         if ((data && !orig_data) || !sbi)
    3650                 :            :                 goto out_free_base;
    3651                 :            : 
    3652                 :          3 :         sbi->s_daxdev = dax_dev;
    3653                 :          3 :         sbi->s_blockgroup_lock =
    3654                 :          3 :                 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
    3655                 :          3 :         if (!sbi->s_blockgroup_lock)
    3656                 :            :                 goto out_free_base;
    3657                 :            : 
    3658                 :          3 :         sb->s_fs_info = sbi;
    3659                 :          3 :         sbi->s_sb = sb;
    3660                 :          3 :         sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
    3661                 :          3 :         sbi->s_sb_block = sb_block;
    3662                 :          3 :         if (sb->s_bdev->bd_part)
    3663                 :          3 :                 sbi->s_sectors_written_start =
    3664                 :          3 :                         part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]);
    3665                 :            : 
    3666                 :            :         /* Cleanup superblock name */
    3667                 :          3 :         strreplace(sb->s_id, '/', '!');
    3668                 :            : 
    3669                 :            :         /* -EINVAL is default */
    3670                 :            :         ret = -EINVAL;
    3671                 :          3 :         blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
    3672                 :          3 :         if (!blocksize) {
    3673                 :          0 :                 ext4_msg(sb, KERN_ERR, "unable to set blocksize");
    3674                 :          0 :                 goto out_fail;
    3675                 :            :         }
    3676                 :            : 
    3677                 :            :         /*
    3678                 :            :          * The ext4 superblock will not be buffer aligned for other than 1kB
    3679                 :            :          * block sizes.  We need to calculate the offset from buffer start.
    3680                 :            :          */
    3681                 :          3 :         if (blocksize != EXT4_MIN_BLOCK_SIZE) {
    3682                 :          0 :                 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
    3683                 :          0 :                 offset = do_div(logical_sb_block, blocksize);
    3684                 :            :         } else {
    3685                 :          3 :                 logical_sb_block = sb_block;
    3686                 :            :         }
    3687                 :            : 
    3688                 :          3 :         if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
    3689                 :          0 :                 ext4_msg(sb, KERN_ERR, "unable to read superblock");
    3690                 :          0 :                 goto out_fail;
    3691                 :            :         }
    3692                 :            :         /*
    3693                 :            :          * Note: s_es must be initialized as soon as possible because
    3694                 :            :          *       some ext4 macro-instructions depend on its value
    3695                 :            :          */
    3696                 :          3 :         es = (struct ext4_super_block *) (bh->b_data + offset);
    3697                 :          3 :         sbi->s_es = es;
    3698                 :          3 :         sb->s_magic = le16_to_cpu(es->s_magic);
    3699                 :          3 :         if (sb->s_magic != EXT4_SUPER_MAGIC)
    3700                 :            :                 goto cantfind_ext4;
    3701                 :          3 :         sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
    3702                 :            : 
    3703                 :            :         /* Warn if metadata_csum and gdt_csum are both set. */
    3704                 :          3 :         if (ext4_has_feature_metadata_csum(sb) &&
    3705                 :            :             ext4_has_feature_gdt_csum(sb))
    3706                 :          0 :                 ext4_warning(sb, "metadata_csum and uninit_bg are "
    3707                 :            :                              "redundant flags; please run fsck.");
    3708                 :            : 
    3709                 :            :         /* Check for a known checksum algorithm */
    3710                 :          3 :         if (!ext4_verify_csum_type(sb, es)) {
    3711                 :          0 :                 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
    3712                 :            :                          "unknown checksum algorithm.");
    3713                 :            :                 silent = 1;
    3714                 :          0 :                 goto cantfind_ext4;
    3715                 :            :         }
    3716                 :            : 
    3717                 :            :         /* Load the checksum driver */
    3718                 :          3 :         sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
    3719                 :          3 :         if (IS_ERR(sbi->s_chksum_driver)) {
    3720                 :          0 :                 ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
    3721                 :          0 :                 ret = PTR_ERR(sbi->s_chksum_driver);
    3722                 :          0 :                 sbi->s_chksum_driver = NULL;
    3723                 :          0 :                 goto failed_mount;
    3724                 :            :         }
    3725                 :            : 
    3726                 :            :         /* Check superblock checksum */
    3727                 :          3 :         if (!ext4_superblock_csum_verify(sb, es)) {
    3728                 :          0 :                 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
    3729                 :            :                          "invalid superblock checksum.  Run e2fsck?");
    3730                 :            :                 silent = 1;
    3731                 :            :                 ret = -EFSBADCRC;
    3732                 :          0 :                 goto cantfind_ext4;
    3733                 :            :         }
    3734                 :            : 
    3735                 :            :         /* Precompute checksum seed for all metadata */
    3736                 :          3 :         if (ext4_has_feature_csum_seed(sb))
    3737                 :          0 :                 sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
    3738                 :          3 :         else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
    3739                 :          0 :                 sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
    3740                 :            :                                                sizeof(es->s_uuid));
    3741                 :            : 
    3742                 :            :         /* Set defaults before we parse the mount options */
    3743                 :          3 :         def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
    3744                 :          3 :         set_opt(sb, INIT_INODE_TABLE);
    3745                 :          3 :         if (def_mount_opts & EXT4_DEFM_DEBUG)
    3746                 :          0 :                 set_opt(sb, DEBUG);
    3747                 :          3 :         if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
    3748                 :          0 :                 set_opt(sb, GRPID);
    3749                 :          3 :         if (def_mount_opts & EXT4_DEFM_UID16)
    3750                 :          0 :                 set_opt(sb, NO_UID32);
    3751                 :            :         /* xattr user namespace & acls are now defaulted on */
    3752                 :          3 :         set_opt(sb, XATTR_USER);
    3753                 :            : #ifdef CONFIG_EXT4_FS_POSIX_ACL
    3754                 :          3 :         set_opt(sb, POSIX_ACL);
    3755                 :            : #endif
    3756                 :            :         /* don't forget to enable journal_csum when metadata_csum is enabled. */
    3757                 :          3 :         if (ext4_has_metadata_csum(sb))
    3758                 :          0 :                 set_opt(sb, JOURNAL_CHECKSUM);
    3759                 :            : 
    3760                 :          3 :         if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
    3761                 :          0 :                 set_opt(sb, JOURNAL_DATA);
    3762                 :          3 :         else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
    3763                 :          0 :                 set_opt(sb, ORDERED_DATA);
    3764                 :          3 :         else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
    3765                 :          0 :                 set_opt(sb, WRITEBACK_DATA);
    3766                 :            : 
    3767                 :          3 :         if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
    3768                 :          0 :                 set_opt(sb, ERRORS_PANIC);
    3769                 :          3 :         else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
    3770                 :          3 :                 set_opt(sb, ERRORS_CONT);
    3771                 :            :         else
    3772                 :          0 :                 set_opt(sb, ERRORS_RO);
    3773                 :            :         /* block_validity enabled by default; disable with noblock_validity */
    3774                 :          3 :         set_opt(sb, BLOCK_VALIDITY);
    3775                 :          3 :         if (def_mount_opts & EXT4_DEFM_DISCARD)
    3776                 :          0 :                 set_opt(sb, DISCARD);
    3777                 :            : 
    3778                 :          3 :         sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
    3779                 :          3 :         sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
    3780                 :          3 :         sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
    3781                 :          3 :         sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
    3782                 :          3 :         sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
    3783                 :            : 
    3784                 :          3 :         if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
    3785                 :          3 :                 set_opt(sb, BARRIER);
    3786                 :            : 
    3787                 :            :         /*
    3788                 :            :          * enable delayed allocation by default
    3789                 :            :          * Use -o nodelalloc to turn it off
    3790                 :            :          */
    3791                 :          3 :         if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
    3792                 :          3 :             ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
    3793                 :          3 :                 set_opt(sb, DELALLOC);
    3794                 :            : 
    3795                 :            :         /*
    3796                 :            :          * set default s_li_wait_mult for lazyinit, for the case there is
    3797                 :            :          * no mount option specified.
    3798                 :            :          */
    3799                 :          3 :         sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
    3800                 :            : 
    3801                 :          3 :         blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
    3802                 :          3 :         if (blocksize < EXT4_MIN_BLOCK_SIZE ||
    3803                 :            :             blocksize > EXT4_MAX_BLOCK_SIZE) {
    3804                 :          0 :                 ext4_msg(sb, KERN_ERR,
    3805                 :            :                        "Unsupported filesystem blocksize %d (%d log_block_size)",
    3806                 :            :                          blocksize, le32_to_cpu(es->s_log_block_size));
    3807                 :          0 :                 goto failed_mount;
    3808                 :            :         }
    3809                 :            : 
    3810                 :          3 :         if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
    3811                 :          0 :                 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
    3812                 :          0 :                 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
    3813                 :            :         } else {
    3814                 :          3 :                 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
    3815                 :          3 :                 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
    3816                 :          3 :                 if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
    3817                 :          0 :                         ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
    3818                 :            :                                  sbi->s_first_ino);
    3819                 :          0 :                         goto failed_mount;
    3820                 :            :                 }
    3821                 :          3 :                 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
    3822                 :          3 :                     (!is_power_of_2(sbi->s_inode_size)) ||
    3823                 :            :                     (sbi->s_inode_size > blocksize)) {
    3824                 :          0 :                         ext4_msg(sb, KERN_ERR,
    3825                 :            :                                "unsupported inode size: %d",
    3826                 :            :                                sbi->s_inode_size);
    3827                 :          0 :                         ext4_msg(sb, KERN_ERR, "blocksize: %d", blocksize);
    3828                 :          0 :                         goto failed_mount;
    3829                 :            :                 }
    3830                 :            :                 /*
    3831                 :            :                  * i_atime_extra is the last extra field available for
    3832                 :            :                  * [acm]times in struct ext4_inode. Checking for that
    3833                 :            :                  * field should suffice to ensure we have extra space
    3834                 :            :                  * for all three.
    3835                 :            :                  */
    3836                 :          3 :                 if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
    3837                 :            :                         sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
    3838                 :          3 :                         sb->s_time_gran = 1;
    3839                 :          3 :                         sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
    3840                 :            :                 } else {
    3841                 :          0 :                         sb->s_time_gran = NSEC_PER_SEC;
    3842                 :          0 :                         sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
    3843                 :            :                 }
    3844                 :          3 :                 sb->s_time_min = EXT4_TIMESTAMP_MIN;
    3845                 :            :         }
    3846                 :          3 :         if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
    3847                 :          3 :                 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
    3848                 :            :                         EXT4_GOOD_OLD_INODE_SIZE;
    3849                 :          3 :                 if (ext4_has_feature_extra_isize(sb)) {
    3850                 :          3 :                         unsigned v, max = (sbi->s_inode_size -
    3851                 :            :                                            EXT4_GOOD_OLD_INODE_SIZE);
    3852                 :            : 
    3853                 :          3 :                         v = le16_to_cpu(es->s_want_extra_isize);
    3854                 :          3 :                         if (v > max) {
    3855                 :          0 :                                 ext4_msg(sb, KERN_ERR,
    3856                 :            :                                          "bad s_want_extra_isize: %d", v);
    3857                 :          0 :                                 goto failed_mount;
    3858                 :            :                         }
    3859                 :          3 :                         if (sbi->s_want_extra_isize < v)
    3860                 :          0 :                                 sbi->s_want_extra_isize = v;
    3861                 :            : 
    3862                 :          3 :                         v = le16_to_cpu(es->s_min_extra_isize);
    3863                 :          3 :                         if (v > max) {
    3864                 :          0 :                                 ext4_msg(sb, KERN_ERR,
    3865                 :            :                                          "bad s_min_extra_isize: %d", v);
    3866                 :          0 :                                 goto failed_mount;
    3867                 :            :                         }
    3868                 :          3 :                         if (sbi->s_want_extra_isize < v)
    3869                 :          0 :                                 sbi->s_want_extra_isize = v;
    3870                 :            :                 }
    3871                 :            :         }
    3872                 :            : 
    3873                 :          3 :         if (sbi->s_es->s_mount_opts[0]) {
    3874                 :          0 :                 char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
    3875                 :            :                                               sizeof(sbi->s_es->s_mount_opts),
    3876                 :            :                                               GFP_KERNEL);
    3877                 :          0 :                 if (!s_mount_opts)
    3878                 :            :                         goto failed_mount;
    3879                 :          0 :                 if (!parse_options(s_mount_opts, sb, &journal_devnum,
    3880                 :            :                                    &journal_ioprio, 0)) {
    3881                 :          0 :                         ext4_msg(sb, KERN_WARNING,
    3882                 :            :                                  "failed to parse options in superblock: %s",
    3883                 :            :                                  s_mount_opts);
    3884                 :            :                 }
    3885                 :          0 :                 kfree(s_mount_opts);
    3886                 :            :         }
    3887                 :          3 :         sbi->s_def_mount_opt = sbi->s_mount_opt;
    3888                 :          3 :         if (!parse_options((char *) data, sb, &journal_devnum,
    3889                 :            :                            &journal_ioprio, 0))
    3890                 :            :                 goto failed_mount;
    3891                 :            : 
    3892                 :            : #ifdef CONFIG_UNICODE
    3893                 :            :         if (ext4_has_feature_casefold(sb) && !sbi->s_encoding) {
    3894                 :            :                 const struct ext4_sb_encodings *encoding_info;
    3895                 :            :                 struct unicode_map *encoding;
    3896                 :            :                 __u16 encoding_flags;
    3897                 :            : 
    3898                 :            :                 if (ext4_has_feature_encrypt(sb)) {
    3899                 :            :                         ext4_msg(sb, KERN_ERR,
    3900                 :            :                                  "Can't mount with encoding and encryption");
    3901                 :            :                         goto failed_mount;
    3902                 :            :                 }
    3903                 :            : 
    3904                 :            :                 if (ext4_sb_read_encoding(es, &encoding_info,
    3905                 :            :                                           &encoding_flags)) {
    3906                 :            :                         ext4_msg(sb, KERN_ERR,
    3907                 :            :                                  "Encoding requested by superblock is unknown");
    3908                 :            :                         goto failed_mount;
    3909                 :            :                 }
    3910                 :            : 
    3911                 :            :                 encoding = utf8_load(encoding_info->version);
    3912                 :            :                 if (IS_ERR(encoding)) {
    3913                 :            :                         ext4_msg(sb, KERN_ERR,
    3914                 :            :                                  "can't mount with superblock charset: %s-%s "
    3915                 :            :                                  "not supported by the kernel. flags: 0x%x.",
    3916                 :            :                                  encoding_info->name, encoding_info->version,
    3917                 :            :                                  encoding_flags);
    3918                 :            :                         goto failed_mount;
    3919                 :            :                 }
    3920                 :            :                 ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
    3921                 :            :                          "%s-%s with flags 0x%hx", encoding_info->name,
    3922                 :            :                          encoding_info->version?:"\b", encoding_flags);
    3923                 :            : 
    3924                 :            :                 sbi->s_encoding = encoding;
    3925                 :            :                 sbi->s_encoding_flags = encoding_flags;
    3926                 :            :         }
    3927                 :            : #endif
    3928                 :            : 
    3929                 :          3 :         if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
    3930                 :          0 :                 printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
    3931                 :            :                             "with data=journal disables delayed "
    3932                 :            :                             "allocation and O_DIRECT support!\n");
    3933                 :          0 :                 if (test_opt2(sb, EXPLICIT_DELALLOC)) {
    3934                 :          0 :                         ext4_msg(sb, KERN_ERR, "can't mount with "
    3935                 :            :                                  "both data=journal and delalloc");
    3936                 :          0 :                         goto failed_mount;
    3937                 :            :                 }
    3938                 :          0 :                 if (test_opt(sb, DIOREAD_NOLOCK)) {
    3939                 :          0 :                         ext4_msg(sb, KERN_ERR, "can't mount with "
    3940                 :            :                                  "both data=journal and dioread_nolock");
    3941                 :          0 :                         goto failed_mount;
    3942                 :            :                 }
    3943                 :            :                 if (test_opt(sb, DAX)) {
    3944                 :            :                         ext4_msg(sb, KERN_ERR, "can't mount with "
    3945                 :            :                                  "both data=journal and dax");
    3946                 :            :                         goto failed_mount;
    3947                 :            :                 }
    3948                 :          0 :                 if (ext4_has_feature_encrypt(sb)) {
    3949                 :          0 :                         ext4_msg(sb, KERN_WARNING,
    3950                 :            :                                  "encrypted files will use data=ordered "
    3951                 :            :                                  "instead of data journaling mode");
    3952                 :            :                 }
    3953                 :          0 :                 if (test_opt(sb, DELALLOC))
    3954                 :          0 :                         clear_opt(sb, DELALLOC);
    3955                 :            :         } else {
    3956                 :          3 :                 sb->s_iflags |= SB_I_CGROUPWB;
    3957                 :            :         }
    3958                 :            : 
    3959                 :          3 :         sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
    3960                 :          3 :                 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
    3961                 :            : 
    3962                 :          3 :         if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
    3963                 :          0 :             (ext4_has_compat_features(sb) ||
    3964                 :          0 :              ext4_has_ro_compat_features(sb) ||
    3965                 :            :              ext4_has_incompat_features(sb)))
    3966                 :          0 :                 ext4_msg(sb, KERN_WARNING,
    3967                 :            :                        "feature flags set on rev 0 fs, "
    3968                 :            :                        "running e2fsck is recommended");
    3969                 :            : 
    3970                 :          3 :         if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
    3971                 :          0 :                 set_opt2(sb, HURD_COMPAT);
    3972                 :          0 :                 if (ext4_has_feature_64bit(sb)) {
    3973                 :          0 :                         ext4_msg(sb, KERN_ERR,
    3974                 :            :                                  "The Hurd can't support 64-bit file systems");
    3975                 :          0 :                         goto failed_mount;
    3976                 :            :                 }
    3977                 :            : 
    3978                 :            :                 /*
    3979                 :            :                  * ea_inode feature uses l_i_version field which is not
    3980                 :            :                  * available in HURD_COMPAT mode.
    3981                 :            :                  */
    3982                 :          0 :                 if (ext4_has_feature_ea_inode(sb)) {
    3983                 :          0 :                         ext4_msg(sb, KERN_ERR,
    3984                 :            :                                  "ea_inode feature is not supported for Hurd");
    3985                 :          0 :                         goto failed_mount;
    3986                 :            :                 }
    3987                 :            :         }
    3988                 :            : 
    3989                 :          3 :         if (IS_EXT2_SB(sb)) {
    3990                 :          1 :                 if (ext2_feature_set_ok(sb))
    3991                 :          0 :                         ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
    3992                 :            :                                  "using the ext4 subsystem");
    3993                 :            :                 else {
    3994                 :            :                         /*
    3995                 :            :                          * If we're probing be silent, if this looks like
    3996                 :            :                          * it's actually an ext[34] filesystem.
    3997                 :            :                          */
    3998                 :          1 :                         if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
    3999                 :            :                                 goto failed_mount;
    4000                 :          0 :                         ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
    4001                 :            :                                  "to feature incompatibilities");
    4002                 :          0 :                         goto failed_mount;
    4003                 :            :                 }
    4004                 :            :         }
    4005                 :            : 
    4006                 :          3 :         if (IS_EXT3_SB(sb)) {
    4007                 :          1 :                 if (ext3_feature_set_ok(sb))
    4008                 :          0 :                         ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
    4009                 :            :                                  "using the ext4 subsystem");
    4010                 :            :                 else {
    4011                 :            :                         /*
    4012                 :            :                          * If we're probing be silent, if this looks like
    4013                 :            :                          * it's actually an ext4 filesystem.
    4014                 :            :                          */
    4015                 :          1 :                         if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
    4016                 :            :                                 goto failed_mount;
    4017                 :          0 :                         ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
    4018                 :            :                                  "to feature incompatibilities");
    4019                 :          0 :                         goto failed_mount;
    4020                 :            :                 }
    4021                 :            :         }
    4022                 :            : 
    4023                 :            :         /*
    4024                 :            :          * Check feature flags regardless of the revision level, since we
    4025                 :            :          * previously didn't change the revision level when setting the flags,
    4026                 :            :          * so there is a chance incompat flags are set on a rev 0 filesystem.
    4027                 :            :          */
    4028                 :          3 :         if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
    4029                 :            :                 goto failed_mount;
    4030                 :            : 
    4031                 :          3 :         if (le32_to_cpu(es->s_log_block_size) >
    4032                 :            :             (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
    4033                 :          0 :                 ext4_msg(sb, KERN_ERR,
    4034                 :            :                          "Invalid log block size: %u",
    4035                 :            :                          le32_to_cpu(es->s_log_block_size));
    4036                 :          0 :                 goto failed_mount;
    4037                 :            :         }
    4038                 :          3 :         if (le32_to_cpu(es->s_log_cluster_size) >
    4039                 :            :             (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
    4040                 :          0 :                 ext4_msg(sb, KERN_ERR,
    4041                 :            :                          "Invalid log cluster size: %u",
    4042                 :            :                          le32_to_cpu(es->s_log_cluster_size));
    4043                 :          0 :                 goto failed_mount;
    4044                 :            :         }
    4045                 :            : 
    4046                 :          3 :         if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
    4047                 :          0 :                 ext4_msg(sb, KERN_ERR,
    4048                 :            :                          "Number of reserved GDT blocks insanely large: %d",
    4049                 :            :                          le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
    4050                 :          0 :                 goto failed_mount;
    4051                 :            :         }
    4052                 :            : 
    4053                 :            :         if (sbi->s_mount_opt & EXT4_MOUNT_DAX) {
    4054                 :            :                 if (ext4_has_feature_inline_data(sb)) {
    4055                 :            :                         ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
    4056                 :            :                                         " that may contain inline data");
    4057                 :            :                         goto failed_mount;
    4058                 :            :                 }
    4059                 :            :                 if (!bdev_dax_supported(sb->s_bdev, blocksize)) {
    4060                 :            :                         ext4_msg(sb, KERN_ERR,
    4061                 :            :                                 "DAX unsupported by block device.");
    4062                 :            :                         goto failed_mount;
    4063                 :            :                 }
    4064                 :            :         }
    4065                 :            : 
    4066                 :          3 :         if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
    4067                 :          0 :                 ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
    4068                 :            :                          es->s_encryption_level);
    4069                 :          0 :                 goto failed_mount;
    4070                 :            :         }
    4071                 :            : 
    4072                 :          3 :         if (sb->s_blocksize != blocksize) {
    4073                 :            :                 /* Validate the filesystem blocksize */
    4074                 :          3 :                 if (!sb_set_blocksize(sb, blocksize)) {
    4075                 :          0 :                         ext4_msg(sb, KERN_ERR, "bad block size %d",
    4076                 :            :                                         blocksize);
    4077                 :          0 :                         goto failed_mount;
    4078                 :            :                 }
    4079                 :            : 
    4080                 :            :                 brelse(bh);
    4081                 :          3 :                 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
    4082                 :          3 :                 offset = do_div(logical_sb_block, blocksize);
    4083                 :          3 :                 bh = sb_bread_unmovable(sb, logical_sb_block);
    4084                 :          3 :                 if (!bh) {
    4085                 :          0 :                         ext4_msg(sb, KERN_ERR,
    4086                 :            :                                "Can't read superblock on 2nd try");
    4087                 :          0 :                         goto failed_mount;
    4088                 :            :                 }
    4089                 :          3 :                 es = (struct ext4_super_block *)(bh->b_data + offset);
    4090                 :          3 :                 sbi->s_es = es;
    4091                 :          3 :                 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
    4092                 :          0 :                         ext4_msg(sb, KERN_ERR,
    4093                 :            :                                "Magic mismatch, very weird!");
    4094                 :          0 :                         goto failed_mount;
    4095                 :            :                 }
    4096                 :            :         }
    4097                 :            : 
    4098                 :          3 :         has_huge_files = ext4_has_feature_huge_file(sb);
    4099                 :          3 :         sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
    4100                 :            :                                                       has_huge_files);
    4101                 :          3 :         sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
    4102                 :            : 
    4103                 :          3 :         sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
    4104                 :          3 :         if (ext4_has_feature_64bit(sb)) {
    4105                 :          0 :                 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
    4106                 :          0 :                     sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
    4107                 :            :                     !is_power_of_2(sbi->s_desc_size)) {
    4108                 :          0 :                         ext4_msg(sb, KERN_ERR,
    4109                 :            :                                "unsupported descriptor size %lu",
    4110                 :            :                                sbi->s_desc_size);
    4111                 :          0 :                         goto failed_mount;
    4112                 :            :                 }
    4113                 :            :         } else
    4114                 :          3 :                 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
    4115                 :            : 
    4116                 :          3 :         sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
    4117                 :          3 :         sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
    4118                 :            : 
    4119                 :          3 :         sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
    4120                 :          3 :         if (sbi->s_inodes_per_block == 0)
    4121                 :            :                 goto cantfind_ext4;
    4122                 :          3 :         if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
    4123                 :          3 :             sbi->s_inodes_per_group > blocksize * 8) {
    4124                 :          0 :                 ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
    4125                 :            :                          sbi->s_inodes_per_group);
    4126                 :          0 :                 goto failed_mount;
    4127                 :            :         }
    4128                 :          3 :         sbi->s_itb_per_group = sbi->s_inodes_per_group /
    4129                 :            :                                         sbi->s_inodes_per_block;
    4130                 :          3 :         sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
    4131                 :          3 :         sbi->s_sbh = bh;
    4132                 :          3 :         sbi->s_mount_state = le16_to_cpu(es->s_state);
    4133                 :          3 :         sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
    4134                 :          3 :         sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
    4135                 :            : 
    4136                 :          3 :         for (i = 0; i < 4; i++)
    4137                 :          3 :                 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
    4138                 :          3 :         sbi->s_def_hash_version = es->s_def_hash_version;
    4139                 :          3 :         if (ext4_has_feature_dir_index(sb)) {
    4140                 :          3 :                 i = le32_to_cpu(es->s_flags);
    4141                 :          3 :                 if (i & EXT2_FLAGS_UNSIGNED_HASH)
    4142                 :          3 :                         sbi->s_hash_unsigned = 3;
    4143                 :          0 :                 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
    4144                 :            : #ifdef __CHAR_UNSIGNED__
    4145                 :          0 :                         if (!sb_rdonly(sb))
    4146                 :          0 :                                 es->s_flags |=
    4147                 :            :                                         cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
    4148                 :          0 :                         sbi->s_hash_unsigned = 3;
    4149                 :            : #else
    4150                 :            :                         if (!sb_rdonly(sb))
    4151                 :            :                                 es->s_flags |=
    4152                 :            :                                         cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
    4153                 :            : #endif
    4154                 :            :                 }
    4155                 :            :         }
    4156                 :            : 
    4157                 :            :         /* Handle clustersize */
    4158                 :          3 :         clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
    4159                 :            :         has_bigalloc = ext4_has_feature_bigalloc(sb);
    4160                 :          3 :         if (has_bigalloc) {
    4161                 :          0 :                 if (clustersize < blocksize) {
    4162                 :          0 :                         ext4_msg(sb, KERN_ERR,
    4163                 :            :                                  "cluster size (%d) smaller than "
    4164                 :            :                                  "block size (%d)", clustersize, blocksize);
    4165                 :          0 :                         goto failed_mount;
    4166                 :            :                 }
    4167                 :          0 :                 sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
    4168                 :          0 :                         le32_to_cpu(es->s_log_block_size);
    4169                 :          0 :                 sbi->s_clusters_per_group =
    4170                 :          0 :                         le32_to_cpu(es->s_clusters_per_group);
    4171                 :          0 :                 if (sbi->s_clusters_per_group > blocksize * 8) {
    4172                 :          0 :                         ext4_msg(sb, KERN_ERR,
    4173                 :            :                                  "#clusters per group too big: %lu",
    4174                 :            :                                  sbi->s_clusters_per_group);
    4175                 :          0 :                         goto failed_mount;
    4176                 :            :                 }
    4177                 :          0 :                 if (sbi->s_blocks_per_group !=
    4178                 :          0 :                     (sbi->s_clusters_per_group * (clustersize / blocksize))) {
    4179                 :          0 :                         ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
    4180                 :            :                                  "clusters per group (%lu) inconsistent",
    4181                 :            :                                  sbi->s_blocks_per_group,
    4182                 :            :                                  sbi->s_clusters_per_group);
    4183                 :          0 :                         goto failed_mount;
    4184                 :            :                 }
    4185                 :            :         } else {
    4186                 :          3 :                 if (clustersize != blocksize) {
    4187                 :          0 :                         ext4_msg(sb, KERN_ERR,
    4188                 :            :                                  "fragment/cluster size (%d) != "
    4189                 :            :                                  "block size (%d)", clustersize, blocksize);
    4190                 :          0 :                         goto failed_mount;
    4191                 :            :                 }
    4192                 :          3 :                 if (sbi->s_blocks_per_group > blocksize * 8) {
    4193                 :          0 :                         ext4_msg(sb, KERN_ERR,
    4194                 :            :                                  "#blocks per group too big: %lu",
    4195                 :            :                                  sbi->s_blocks_per_group);
    4196                 :          0 :                         goto failed_mount;
    4197                 :            :                 }
    4198                 :          3 :                 sbi->s_clusters_per_group = sbi->s_blocks_per_group;
    4199                 :          3 :                 sbi->s_cluster_bits = 0;
    4200                 :            :         }
    4201                 :          3 :         sbi->s_cluster_ratio = clustersize / blocksize;
    4202                 :            : 
    4203                 :            :         /* Do we have standard group size of clustersize * 8 blocks ? */
    4204                 :          3 :         if (sbi->s_blocks_per_group == clustersize << 3)
    4205                 :          3 :                 set_opt2(sb, STD_GROUP_SIZE);
    4206                 :            : 
    4207                 :            :         /*
    4208                 :            :          * Test whether we have more sectors than will fit in sector_t,
    4209                 :            :          * and whether the max offset is addressable by the page cache.
    4210                 :            :          */
    4211                 :          3 :         err = generic_check_addressable(sb->s_blocksize_bits,
    4212                 :            :                                         ext4_blocks_count(es));
    4213                 :          3 :         if (err) {
    4214                 :          0 :                 ext4_msg(sb, KERN_ERR, "filesystem"
    4215                 :            :                          " too large to mount safely on this system");
    4216                 :          0 :                 goto failed_mount;
    4217                 :            :         }
    4218                 :            : 
    4219                 :          3 :         if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
    4220                 :            :                 goto cantfind_ext4;
    4221                 :            : 
    4222                 :            :         /* check blocks count against device size */
    4223                 :          3 :         blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
    4224                 :          3 :         if (blocks_count && ext4_blocks_count(es) > blocks_count) {
    4225                 :          0 :                 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
    4226                 :            :                        "exceeds size of device (%llu blocks)",
    4227                 :            :                        ext4_blocks_count(es), blocks_count);
    4228                 :          0 :                 goto failed_mount;
    4229                 :            :         }
    4230                 :            : 
    4231                 :            :         /*
    4232                 :            :          * It makes no sense for the first data block to be beyond the end
    4233                 :            :          * of the filesystem.
    4234                 :            :          */
    4235                 :          3 :         if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
    4236                 :          0 :                 ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
    4237                 :            :                          "block %u is beyond end of filesystem (%llu)",
    4238                 :            :                          le32_to_cpu(es->s_first_data_block),
    4239                 :            :                          ext4_blocks_count(es));
    4240                 :          0 :                 goto failed_mount;
    4241                 :            :         }
    4242                 :          3 :         if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
    4243                 :          0 :             (sbi->s_cluster_ratio == 1)) {
    4244                 :          0 :                 ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
    4245                 :            :                          "block is 0 with a 1k block and cluster size");
    4246                 :          0 :                 goto failed_mount;
    4247                 :            :         }
    4248                 :            : 
    4249                 :          3 :         blocks_count = (ext4_blocks_count(es) -
    4250                 :          3 :                         le32_to_cpu(es->s_first_data_block) +
    4251                 :          3 :                         EXT4_BLOCKS_PER_GROUP(sb) - 1);
    4252                 :          3 :         do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
    4253                 :          3 :         if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
    4254                 :          0 :                 ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
    4255                 :            :                        "(block count %llu, first data block %u, "
    4256                 :            :                        "blocks per group %lu)", blocks_count,
    4257                 :            :                        ext4_blocks_count(es),
    4258                 :            :                        le32_to_cpu(es->s_first_data_block),
    4259                 :            :                        EXT4_BLOCKS_PER_GROUP(sb));
    4260                 :          0 :                 goto failed_mount;
    4261                 :            :         }
    4262                 :          3 :         sbi->s_groups_count = blocks_count;
    4263                 :          3 :         sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
    4264                 :            :                         (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
    4265                 :          3 :         if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
    4266                 :          3 :             le32_to_cpu(es->s_inodes_count)) {
    4267                 :          0 :                 ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
    4268                 :            :                          le32_to_cpu(es->s_inodes_count),
    4269                 :            :                          ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
    4270                 :            :                 ret = -EINVAL;
    4271                 :          0 :                 goto failed_mount;
    4272                 :            :         }
    4273                 :          3 :         db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
    4274                 :            :                    EXT4_DESC_PER_BLOCK(sb);
    4275                 :          3 :         if (ext4_has_feature_meta_bg(sb)) {
    4276                 :          0 :                 if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
    4277                 :          0 :                         ext4_msg(sb, KERN_WARNING,
    4278                 :            :                                  "first meta block group too large: %u "
    4279                 :            :                                  "(group descriptor block count %u)",
    4280                 :            :                                  le32_to_cpu(es->s_first_meta_bg), db_count);
    4281                 :          0 :                         goto failed_mount;
    4282                 :            :                 }
    4283                 :            :         }
    4284                 :          3 :         rcu_assign_pointer(sbi->s_group_desc,
    4285                 :            :                            kvmalloc_array(db_count,
    4286                 :            :                                           sizeof(struct buffer_head *),
    4287                 :            :                                           GFP_KERNEL));
    4288                 :          3 :         if (sbi->s_group_desc == NULL) {
    4289                 :          0 :                 ext4_msg(sb, KERN_ERR, "not enough memory");
    4290                 :            :                 ret = -ENOMEM;
    4291                 :          0 :                 goto failed_mount;
    4292                 :            :         }
    4293                 :            : 
    4294                 :          3 :         bgl_lock_init(sbi->s_blockgroup_lock);
    4295                 :            : 
    4296                 :            :         /* Pre-read the descriptors into the buffer cache */
    4297                 :          3 :         for (i = 0; i < db_count; i++) {
    4298                 :          3 :                 block = descriptor_loc(sb, logical_sb_block, i);
    4299                 :            :                 sb_breadahead_unmovable(sb, block);
    4300                 :            :         }
    4301                 :            : 
    4302                 :          3 :         for (i = 0; i < db_count; i++) {
    4303                 :            :                 struct buffer_head *bh;
    4304                 :            : 
    4305                 :          3 :                 block = descriptor_loc(sb, logical_sb_block, i);
    4306                 :            :                 bh = sb_bread_unmovable(sb, block);
    4307                 :          3 :                 if (!bh) {
    4308                 :          0 :                         ext4_msg(sb, KERN_ERR,
    4309                 :            :                                "can't read group descriptor %d", i);
    4310                 :          0 :                         db_count = i;
    4311                 :          0 :                         goto failed_mount2;
    4312                 :            :                 }
    4313                 :            :                 rcu_read_lock();
    4314                 :          3 :                 rcu_dereference(sbi->s_group_desc)[i] = bh;
    4315                 :            :                 rcu_read_unlock();
    4316                 :            :         }
    4317                 :          3 :         sbi->s_gdb_count = db_count;
    4318                 :          3 :         if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
    4319                 :          0 :                 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
    4320                 :            :                 ret = -EFSCORRUPTED;
    4321                 :          0 :                 goto failed_mount2;
    4322                 :            :         }
    4323                 :            : 
    4324                 :          3 :         timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
    4325                 :            : 
    4326                 :            :         /* Register extent status tree shrinker */
    4327                 :          3 :         if (ext4_es_register_shrinker(sbi))
    4328                 :            :                 goto failed_mount3;
    4329                 :            : 
    4330                 :          3 :         sbi->s_stripe = ext4_get_stripe_size(sbi);
    4331                 :          3 :         sbi->s_extent_max_zeroout_kb = 32;
    4332                 :            : 
    4333                 :            :         /*
    4334                 :            :          * set up enough so that it can read an inode
    4335                 :            :          */
    4336                 :          3 :         sb->s_op = &ext4_sops;
    4337                 :          3 :         sb->s_export_op = &ext4_export_ops;
    4338                 :          3 :         sb->s_xattr = ext4_xattr_handlers;
    4339                 :            : #ifdef CONFIG_FS_ENCRYPTION
    4340                 :          3 :         sb->s_cop = &ext4_cryptops;
    4341                 :            : #endif
    4342                 :            : #ifdef CONFIG_FS_VERITY
    4343                 :            :         sb->s_vop = &ext4_verityops;
    4344                 :            : #endif
    4345                 :            : #ifdef CONFIG_QUOTA
    4346                 :          3 :         sb->dq_op = &ext4_quota_operations;
    4347                 :          3 :         if (ext4_has_feature_quota(sb))
    4348                 :          0 :                 sb->s_qcop = &dquot_quotactl_sysfile_ops;
    4349                 :            :         else
    4350                 :          3 :                 sb->s_qcop = &ext4_qctl_operations;
    4351                 :          3 :         sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
    4352                 :            : #endif
    4353                 :          3 :         memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
    4354                 :            : 
    4355                 :          3 :         INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
    4356                 :          3 :         mutex_init(&sbi->s_orphan_lock);
    4357                 :            : 
    4358                 :          3 :         sb->s_root = NULL;
    4359                 :            : 
    4360                 :          3 :         needs_recovery = (es->s_last_orphan != 0 ||
    4361                 :            :                           ext4_has_feature_journal_needs_recovery(sb));
    4362                 :            : 
    4363                 :          3 :         if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
    4364                 :          0 :                 if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
    4365                 :            :                         goto failed_mount3a;
    4366                 :            : 
    4367                 :            :         /*
    4368                 :            :          * The first inode we look at is the journal inode.  Don't try
    4369                 :            :          * root first: it may be modified in the journal!
    4370                 :            :          */
    4371                 :          3 :         if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
    4372                 :          3 :                 err = ext4_load_journal(sb, es, journal_devnum);
    4373                 :          3 :                 if (err)
    4374                 :            :                         goto failed_mount3a;
    4375                 :          0 :         } else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
    4376                 :            :                    ext4_has_feature_journal_needs_recovery(sb)) {
    4377                 :          0 :                 ext4_msg(sb, KERN_ERR, "required journal recovery "
    4378                 :            :                        "suppressed and not mounted read-only");
    4379                 :          0 :                 goto failed_mount_wq;
    4380                 :            :         } else {
    4381                 :            :                 /* Nojournal mode, all journal mount options are illegal */
    4382                 :          0 :                 if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
    4383                 :          0 :                         ext4_msg(sb, KERN_ERR, "can't mount with "
    4384                 :            :                                  "journal_checksum, fs mounted w/o journal");
    4385                 :          0 :                         goto failed_mount_wq;
    4386                 :            :                 }
    4387                 :          0 :                 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
    4388                 :          0 :                         ext4_msg(sb, KERN_ERR, "can't mount with "
    4389                 :            :                                  "journal_async_commit, fs mounted w/o journal");
    4390                 :          0 :                         goto failed_mount_wq;
    4391                 :            :                 }
    4392                 :          0 :                 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
    4393                 :          0 :                         ext4_msg(sb, KERN_ERR, "can't mount with "
    4394                 :            :                                  "commit=%lu, fs mounted w/o journal",
    4395                 :            :                                  sbi->s_commit_interval / HZ);
    4396                 :          0 :                         goto failed_mount_wq;
    4397                 :            :                 }
    4398                 :          0 :                 if (EXT4_MOUNT_DATA_FLAGS &
    4399                 :          0 :                     (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
    4400                 :          0 :                         ext4_msg(sb, KERN_ERR, "can't mount with "
    4401                 :            :                                  "data=, fs mounted w/o journal");
    4402                 :          0 :                         goto failed_mount_wq;
    4403                 :            :                 }
    4404                 :          0 :                 sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
    4405                 :          0 :                 clear_opt(sb, JOURNAL_CHECKSUM);
    4406                 :          0 :                 clear_opt(sb, DATA_FLAGS);
    4407                 :          0 :                 sbi->s_journal = NULL;
    4408                 :            :                 needs_recovery = 0;
    4409                 :          0 :                 goto no_journal;
    4410                 :            :         }
    4411                 :            : 
    4412                 :          3 :         if (ext4_has_feature_64bit(sb) &&
    4413                 :          0 :             !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
    4414                 :            :                                        JBD2_FEATURE_INCOMPAT_64BIT)) {
    4415                 :          0 :                 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
    4416                 :          0 :                 goto failed_mount_wq;
    4417                 :            :         }
    4418                 :            : 
    4419                 :          3 :         if (!set_journal_csum_feature_set(sb)) {
    4420                 :          0 :                 ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
    4421                 :            :                          "feature set");
    4422                 :          0 :                 goto failed_mount_wq;
    4423                 :            :         }
    4424                 :            : 
    4425                 :            :         /* We have now updated the journal if required, so we can
    4426                 :            :          * validate the data journaling mode. */
    4427                 :          3 :         switch (test_opt(sb, DATA_FLAGS)) {
    4428                 :            :         case 0:
    4429                 :            :                 /* No mode set, assume a default based on the journal
    4430                 :            :                  * capabilities: ORDERED_DATA if the journal can
    4431                 :            :                  * cope, else JOURNAL_DATA
    4432                 :            :                  */
    4433                 :          3 :                 if (jbd2_journal_check_available_features
    4434                 :          3 :                     (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
    4435                 :          3 :                         set_opt(sb, ORDERED_DATA);
    4436                 :          3 :                         sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
    4437                 :            :                 } else {
    4438                 :          0 :                         set_opt(sb, JOURNAL_DATA);
    4439                 :          0 :                         sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
    4440                 :            :                 }
    4441                 :            :                 break;
    4442                 :            : 
    4443                 :            :         case EXT4_MOUNT_ORDERED_DATA:
    4444                 :            :         case EXT4_MOUNT_WRITEBACK_DATA:
    4445                 :          0 :                 if (!jbd2_journal_check_available_features
    4446                 :          0 :                     (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
    4447                 :          0 :                         ext4_msg(sb, KERN_ERR, "Journal does not support "
    4448                 :            :                                "requested data journaling mode");
    4449                 :          0 :                         goto failed_mount_wq;
    4450                 :            :                 }
    4451                 :            :         default:
    4452                 :            :                 break;
    4453                 :            :         }
    4454                 :            : 
    4455                 :          3 :         if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
    4456                 :          3 :             test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
    4457                 :          0 :                 ext4_msg(sb, KERN_ERR, "can't mount with "
    4458                 :            :                         "journal_async_commit in data=ordered mode");
    4459                 :          0 :                 goto failed_mount_wq;
    4460                 :            :         }
    4461                 :            : 
    4462                 :          3 :         set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
    4463                 :            : 
    4464                 :          3 :         sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
    4465                 :            : 
    4466                 :            : no_journal:
    4467                 :          3 :         if (!test_opt(sb, NO_MBCACHE)) {
    4468                 :          3 :                 sbi->s_ea_block_cache = ext4_xattr_create_cache();
    4469                 :          3 :                 if (!sbi->s_ea_block_cache) {
    4470                 :          0 :                         ext4_msg(sb, KERN_ERR,
    4471                 :            :                                  "Failed to create ea_block_cache");
    4472                 :          0 :                         goto failed_mount_wq;
    4473                 :            :                 }
    4474                 :            : 
    4475                 :          3 :                 if (ext4_has_feature_ea_inode(sb)) {
    4476                 :          0 :                         sbi->s_ea_inode_cache = ext4_xattr_create_cache();
    4477                 :          0 :                         if (!sbi->s_ea_inode_cache) {
    4478                 :          0 :                                 ext4_msg(sb, KERN_ERR,
    4479                 :            :                                          "Failed to create ea_inode_cache");
    4480                 :          0 :                                 goto failed_mount_wq;
    4481                 :            :                         }
    4482                 :            :                 }
    4483                 :            :         }
    4484                 :            : 
    4485                 :          3 :         if ((DUMMY_ENCRYPTION_ENABLED(sbi) || ext4_has_feature_encrypt(sb)) &&
    4486                 :            :             (blocksize != PAGE_SIZE)) {
    4487                 :          0 :                 ext4_msg(sb, KERN_ERR,
    4488                 :            :                          "Unsupported blocksize for fs encryption");
    4489                 :          0 :                 goto failed_mount_wq;
    4490                 :            :         }
    4491                 :            : 
    4492                 :          3 :         if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
    4493                 :          0 :                 ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
    4494                 :          0 :                 goto failed_mount_wq;
    4495                 :            :         }
    4496                 :            : 
    4497                 :          3 :         if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
    4498                 :            :             !ext4_has_feature_encrypt(sb)) {
    4499                 :            :                 ext4_set_feature_encrypt(sb);
    4500                 :          0 :                 ext4_commit_super(sb, 1);
    4501                 :            :         }
    4502                 :            : 
    4503                 :            :         /*
    4504                 :            :          * Get the # of file system overhead blocks from the
    4505                 :            :          * superblock if present.
    4506                 :            :          */
    4507                 :          3 :         if (es->s_overhead_clusters)
    4508                 :          0 :                 sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
    4509                 :            :         else {
    4510                 :          3 :                 err = ext4_calculate_overhead(sb);
    4511                 :          3 :                 if (err)
    4512                 :            :                         goto failed_mount_wq;
    4513                 :            :         }
    4514                 :            : 
    4515                 :            :         /*
    4516                 :            :          * The maximum number of concurrent works can be high and
    4517                 :            :          * concurrency isn't really necessary.  Limit it to 1.
    4518                 :            :          */
    4519                 :          3 :         EXT4_SB(sb)->rsv_conversion_wq =
    4520                 :          3 :                 alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
    4521                 :          3 :         if (!EXT4_SB(sb)->rsv_conversion_wq) {
    4522                 :          0 :                 printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
    4523                 :            :                 ret = -ENOMEM;
    4524                 :          0 :                 goto failed_mount4;
    4525                 :            :         }
    4526                 :            : 
    4527                 :            :         /*
    4528                 :            :          * The jbd2_journal_load will have done any necessary log recovery,
    4529                 :            :          * so we can safely mount the rest of the filesystem now.
    4530                 :            :          */
    4531                 :            : 
    4532                 :          3 :         root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
    4533                 :          3 :         if (IS_ERR(root)) {
    4534                 :          0 :                 ext4_msg(sb, KERN_ERR, "get root inode failed");
    4535                 :            :                 ret = PTR_ERR(root);
    4536                 :            :                 root = NULL;
    4537                 :          0 :                 goto failed_mount4;
    4538                 :            :         }
    4539                 :          3 :         if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
    4540                 :          0 :                 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
    4541                 :          0 :                 iput(root);
    4542                 :          0 :                 goto failed_mount4;
    4543                 :            :         }
    4544                 :            : 
    4545                 :            : #ifdef CONFIG_UNICODE
    4546                 :            :         if (sbi->s_encoding)
    4547                 :            :                 sb->s_d_op = &ext4_dentry_ops;
    4548                 :            : #endif
    4549                 :            : 
    4550                 :          3 :         sb->s_root = d_make_root(root);
    4551                 :          3 :         if (!sb->s_root) {
    4552                 :          0 :                 ext4_msg(sb, KERN_ERR, "get root dentry failed");
    4553                 :            :                 ret = -ENOMEM;
    4554                 :          0 :                 goto failed_mount4;
    4555                 :            :         }
    4556                 :            : 
    4557                 :          3 :         ret = ext4_setup_super(sb, es, sb_rdonly(sb));
    4558                 :          3 :         if (ret == -EROFS) {
    4559                 :          0 :                 sb->s_flags |= SB_RDONLY;
    4560                 :            :                 ret = 0;
    4561                 :          3 :         } else if (ret)
    4562                 :            :                 goto failed_mount4a;
    4563                 :            : 
    4564                 :          3 :         ext4_set_resv_clusters(sb);
    4565                 :            : 
    4566                 :          3 :         err = ext4_setup_system_zone(sb);
    4567                 :          3 :         if (err) {
    4568                 :          0 :                 ext4_msg(sb, KERN_ERR, "failed to initialize system "
    4569                 :            :                          "zone (%d)", err);
    4570                 :          0 :                 goto failed_mount4a;
    4571                 :            :         }
    4572                 :            : 
    4573                 :          3 :         ext4_ext_init(sb);
    4574                 :          3 :         err = ext4_mb_init(sb);
    4575                 :          3 :         if (err) {
    4576                 :          0 :                 ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
    4577                 :            :                          err);
    4578                 :          0 :                 goto failed_mount5;
    4579                 :            :         }
    4580                 :            : 
    4581                 :          3 :         block = ext4_count_free_clusters(sb);
    4582                 :          3 :         ext4_free_blocks_count_set(sbi->s_es, 
    4583                 :          3 :                                    EXT4_C2B(sbi, block));
    4584                 :          3 :         ext4_superblock_csum_set(sb);
    4585                 :          3 :         err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
    4586                 :            :                                   GFP_KERNEL);
    4587                 :          3 :         if (!err) {
    4588                 :          3 :                 unsigned long freei = ext4_count_free_inodes(sb);
    4589                 :          3 :                 sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
    4590                 :          3 :                 ext4_superblock_csum_set(sb);
    4591                 :          3 :                 err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
    4592                 :            :                                           GFP_KERNEL);
    4593                 :            :         }
    4594                 :          3 :         if (!err)
    4595                 :          3 :                 err = percpu_counter_init(&sbi->s_dirs_counter,
    4596                 :            :                                           ext4_count_dirs(sb), GFP_KERNEL);
    4597                 :          3 :         if (!err)
    4598                 :          3 :                 err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
    4599                 :            :                                           GFP_KERNEL);
    4600                 :          3 :         if (!err)
    4601                 :          3 :                 err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
    4602                 :            : 
    4603                 :          3 :         if (err) {
    4604                 :          0 :                 ext4_msg(sb, KERN_ERR, "insufficient memory");
    4605                 :          0 :                 goto failed_mount6;
    4606                 :            :         }
    4607                 :            : 
    4608                 :          3 :         if (ext4_has_feature_flex_bg(sb))
    4609                 :          3 :                 if (!ext4_fill_flex_info(sb)) {
    4610                 :          0 :                         ext4_msg(sb, KERN_ERR,
    4611                 :            :                                "unable to initialize "
    4612                 :            :                                "flex_bg meta info!");
    4613                 :          0 :                         goto failed_mount6;
    4614                 :            :                 }
    4615                 :            : 
    4616                 :          3 :         err = ext4_register_li_request(sb, first_not_zeroed);
    4617                 :          3 :         if (err)
    4618                 :            :                 goto failed_mount6;
    4619                 :            : 
    4620                 :          3 :         err = ext4_register_sysfs(sb);
    4621                 :          3 :         if (err)
    4622                 :            :                 goto failed_mount7;
    4623                 :            : 
    4624                 :            : #ifdef CONFIG_QUOTA
    4625                 :            :         /* Enable quota usage during mount. */
    4626                 :          3 :         if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
    4627                 :          0 :                 err = ext4_enable_quotas(sb);
    4628                 :          0 :                 if (err)
    4629                 :            :                         goto failed_mount8;
    4630                 :            :         }
    4631                 :            : #endif  /* CONFIG_QUOTA */
    4632                 :            : 
    4633                 :          3 :         EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
    4634                 :          3 :         ext4_orphan_cleanup(sb, es);
    4635                 :          3 :         EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
    4636                 :          3 :         if (needs_recovery) {
    4637                 :          1 :                 ext4_msg(sb, KERN_INFO, "recovery complete");
    4638                 :          1 :                 ext4_mark_recovery_complete(sb, es);
    4639                 :            :         }
    4640                 :          3 :         if (EXT4_SB(sb)->s_journal) {
    4641                 :          3 :                 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
    4642                 :            :                         descr = " journalled data mode";
    4643                 :          3 :                 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
    4644                 :            :                         descr = " ordered data mode";
    4645                 :            :                 else
    4646                 :            :                         descr = " writeback data mode";
    4647                 :            :         } else
    4648                 :            :                 descr = "out journal";
    4649                 :            : 
    4650                 :          3 :         if (test_opt(sb, DISCARD)) {
    4651                 :          0 :                 struct request_queue *q = bdev_get_queue(sb->s_bdev);
    4652                 :          0 :                 if (!blk_queue_discard(q))
    4653                 :          0 :                         ext4_msg(sb, KERN_WARNING,
    4654                 :            :                                  "mounting with \"discard\" option, but "
    4655                 :            :                                  "the device does not support discard");
    4656                 :            :         }
    4657                 :            : 
    4658                 :          3 :         if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
    4659                 :          3 :                 ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
    4660                 :            :                          "Opts: %.*s%s%s", descr,
    4661                 :            :                          (int) sizeof(sbi->s_es->s_mount_opts),
    4662                 :            :                          sbi->s_es->s_mount_opts,
    4663                 :            :                          *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
    4664                 :            : 
    4665                 :          3 :         if (es->s_error_count)
    4666                 :          0 :                 mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
    4667                 :            : 
    4668                 :            :         /* Enable message ratelimiting. Default is 10 messages per 5 secs. */
    4669                 :          3 :         ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
    4670                 :          3 :         ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
    4671                 :          3 :         ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
    4672                 :            : 
    4673                 :          3 :         kfree(orig_data);
    4674                 :          3 :         return 0;
    4675                 :            : 
    4676                 :            : cantfind_ext4:
    4677                 :          0 :         if (!silent)
    4678                 :          0 :                 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
    4679                 :            :         goto failed_mount;
    4680                 :            : 
    4681                 :            : #ifdef CONFIG_QUOTA
    4682                 :            : failed_mount8:
    4683                 :          0 :         ext4_unregister_sysfs(sb);
    4684                 :            : #endif
    4685                 :            : failed_mount7:
    4686                 :          0 :         ext4_unregister_li_request(sb);
    4687                 :            : failed_mount6:
    4688                 :          0 :         ext4_mb_release(sb);
    4689                 :            :         rcu_read_lock();
    4690                 :          0 :         flex_groups = rcu_dereference(sbi->s_flex_groups);
    4691                 :          0 :         if (flex_groups) {
    4692                 :          0 :                 for (i = 0; i < sbi->s_flex_groups_allocated; i++)
    4693                 :          0 :                         kvfree(flex_groups[i]);
    4694                 :          0 :                 kvfree(flex_groups);
    4695                 :            :         }
    4696                 :            :         rcu_read_unlock();
    4697                 :          0 :         percpu_counter_destroy(&sbi->s_freeclusters_counter);
    4698                 :          0 :         percpu_counter_destroy(&sbi->s_freeinodes_counter);
    4699                 :          0 :         percpu_counter_destroy(&sbi->s_dirs_counter);
    4700                 :          0 :         percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
    4701                 :          0 :         percpu_free_rwsem(&sbi->s_writepages_rwsem);
    4702                 :            : failed_mount5:
    4703                 :          0 :         ext4_ext_release(sb);
    4704                 :          0 :         ext4_release_system_zone(sb);
    4705                 :            : failed_mount4a:
    4706                 :          0 :         dput(sb->s_root);
    4707                 :          0 :         sb->s_root = NULL;
    4708                 :            : failed_mount4:
    4709                 :          0 :         ext4_msg(sb, KERN_ERR, "mount failed");
    4710                 :          0 :         if (EXT4_SB(sb)->rsv_conversion_wq)
    4711                 :          0 :                 destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
    4712                 :            : failed_mount_wq:
    4713                 :          0 :         ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
    4714                 :          0 :         sbi->s_ea_inode_cache = NULL;
    4715                 :            : 
    4716                 :          0 :         ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
    4717                 :          0 :         sbi->s_ea_block_cache = NULL;
    4718                 :            : 
    4719                 :          0 :         if (sbi->s_journal) {
    4720                 :          0 :                 jbd2_journal_destroy(sbi->s_journal);
    4721                 :          0 :                 sbi->s_journal = NULL;
    4722                 :            :         }
    4723                 :            : failed_mount3a:
    4724                 :          0 :         ext4_es_unregister_shrinker(sbi);
    4725                 :            : failed_mount3:
    4726                 :          0 :         del_timer_sync(&sbi->s_err_report);
    4727                 :          0 :         if (sbi->s_mmp_tsk)
    4728                 :          0 :                 kthread_stop(sbi->s_mmp_tsk);
    4729                 :            : failed_mount2:
    4730                 :            :         rcu_read_lock();
    4731                 :          0 :         group_desc = rcu_dereference(sbi->s_group_desc);
    4732                 :          0 :         for (i = 0; i < db_count; i++)
    4733                 :          0 :                 brelse(group_desc[i]);
    4734                 :          0 :         kvfree(group_desc);
    4735                 :            :         rcu_read_unlock();
    4736                 :            : failed_mount:
    4737                 :          1 :         if (sbi->s_chksum_driver)
    4738                 :            :                 crypto_free_shash(sbi->s_chksum_driver);
    4739                 :            : 
    4740                 :            : #ifdef CONFIG_UNICODE
    4741                 :            :         utf8_unload(sbi->s_encoding);
    4742                 :            : #endif
    4743                 :            : 
    4744                 :            : #ifdef CONFIG_QUOTA
    4745                 :          1 :         for (i = 0; i < EXT4_MAXQUOTAS; i++)
    4746                 :          1 :                 kfree(get_qf_name(sb, sbi, i));
    4747                 :            : #endif
    4748                 :            :         ext4_blkdev_remove(sbi);
    4749                 :            :         brelse(bh);
    4750                 :            : out_fail:
    4751                 :          1 :         sb->s_fs_info = NULL;
    4752                 :          1 :         kfree(sbi->s_blockgroup_lock);
    4753                 :            : out_free_base:
    4754                 :          1 :         kfree(sbi);
    4755                 :          1 :         kfree(orig_data);
    4756                 :            :         fs_put_dax(dax_dev);
    4757                 :          1 :         return err ? err : ret;
    4758                 :            : }
    4759                 :            : 
    4760                 :            : /*
    4761                 :            :  * Setup any per-fs journal parameters now.  We'll do this both on
    4762                 :            :  * initial mount, once the journal has been initialised but before we've
    4763                 :            :  * done any recovery; and again on any subsequent remount.
    4764                 :            :  */
    4765                 :          3 : static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
    4766                 :            : {
    4767                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    4768                 :            : 
    4769                 :          3 :         journal->j_commit_interval = sbi->s_commit_interval;
    4770                 :          3 :         journal->j_min_batch_time = sbi->s_min_batch_time;
    4771                 :          3 :         journal->j_max_batch_time = sbi->s_max_batch_time;
    4772                 :            : 
    4773                 :          3 :         write_lock(&journal->j_state_lock);
    4774                 :          3 :         if (test_opt(sb, BARRIER))
    4775                 :          3 :                 journal->j_flags |= JBD2_BARRIER;
    4776                 :            :         else
    4777                 :          0 :                 journal->j_flags &= ~JBD2_BARRIER;
    4778                 :          3 :         if (test_opt(sb, DATA_ERR_ABORT))
    4779                 :          0 :                 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
    4780                 :            :         else
    4781                 :          3 :                 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
    4782                 :            :         write_unlock(&journal->j_state_lock);
    4783                 :          3 : }
    4784                 :            : 
    4785                 :          3 : static struct inode *ext4_get_journal_inode(struct super_block *sb,
    4786                 :            :                                              unsigned int journal_inum)
    4787                 :            : {
    4788                 :            :         struct inode *journal_inode;
    4789                 :            : 
    4790                 :            :         /*
    4791                 :            :          * Test for the existence of a valid inode on disk.  Bad things
    4792                 :            :          * happen if we iget() an unused inode, as the subsequent iput()
    4793                 :            :          * will try to delete it.
    4794                 :            :          */
    4795                 :          3 :         journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
    4796                 :          3 :         if (IS_ERR(journal_inode)) {
    4797                 :          0 :                 ext4_msg(sb, KERN_ERR, "no journal found");
    4798                 :          0 :                 return NULL;
    4799                 :            :         }
    4800                 :          3 :         if (!journal_inode->i_nlink) {
    4801                 :          0 :                 make_bad_inode(journal_inode);
    4802                 :          0 :                 iput(journal_inode);
    4803                 :          0 :                 ext4_msg(sb, KERN_ERR, "journal inode is deleted");
    4804                 :          0 :                 return NULL;
    4805                 :            :         }
    4806                 :            : 
    4807                 :            :         jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
    4808                 :            :                   journal_inode, journal_inode->i_size);
    4809                 :          3 :         if (!S_ISREG(journal_inode->i_mode)) {
    4810                 :          0 :                 ext4_msg(sb, KERN_ERR, "invalid journal inode");
    4811                 :          0 :                 iput(journal_inode);
    4812                 :          0 :                 return NULL;
    4813                 :            :         }
    4814                 :            :         return journal_inode;
    4815                 :            : }
    4816                 :            : 
    4817                 :          3 : static journal_t *ext4_get_journal(struct super_block *sb,
    4818                 :            :                                    unsigned int journal_inum)
    4819                 :            : {
    4820                 :            :         struct inode *journal_inode;
    4821                 :            :         journal_t *journal;
    4822                 :            : 
    4823                 :          3 :         BUG_ON(!ext4_has_feature_journal(sb));
    4824                 :            : 
    4825                 :          3 :         journal_inode = ext4_get_journal_inode(sb, journal_inum);
    4826                 :          3 :         if (!journal_inode)
    4827                 :            :                 return NULL;
    4828                 :            : 
    4829                 :          3 :         journal = jbd2_journal_init_inode(journal_inode);
    4830                 :          3 :         if (!journal) {
    4831                 :          0 :                 ext4_msg(sb, KERN_ERR, "Could not load journal inode");
    4832                 :          0 :                 iput(journal_inode);
    4833                 :          0 :                 return NULL;
    4834                 :            :         }
    4835                 :          3 :         journal->j_private = sb;
    4836                 :          3 :         ext4_init_journal_params(sb, journal);
    4837                 :          3 :         return journal;
    4838                 :            : }
    4839                 :            : 
    4840                 :          0 : static journal_t *ext4_get_dev_journal(struct super_block *sb,
    4841                 :            :                                        dev_t j_dev)
    4842                 :            : {
    4843                 :            :         struct buffer_head *bh;
    4844                 :            :         journal_t *journal;
    4845                 :            :         ext4_fsblk_t start;
    4846                 :            :         ext4_fsblk_t len;
    4847                 :            :         int hblock, blocksize;
    4848                 :            :         ext4_fsblk_t sb_block;
    4849                 :            :         unsigned long offset;
    4850                 :            :         struct ext4_super_block *es;
    4851                 :            :         struct block_device *bdev;
    4852                 :            : 
    4853                 :          0 :         BUG_ON(!ext4_has_feature_journal(sb));
    4854                 :            : 
    4855                 :          0 :         bdev = ext4_blkdev_get(j_dev, sb);
    4856                 :          0 :         if (bdev == NULL)
    4857                 :            :                 return NULL;
    4858                 :            : 
    4859                 :          0 :         blocksize = sb->s_blocksize;
    4860                 :            :         hblock = bdev_logical_block_size(bdev);
    4861                 :          0 :         if (blocksize < hblock) {
    4862                 :          0 :                 ext4_msg(sb, KERN_ERR,
    4863                 :            :                         "blocksize too small for journal device");
    4864                 :          0 :                 goto out_bdev;
    4865                 :            :         }
    4866                 :            : 
    4867                 :          0 :         sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
    4868                 :          0 :         offset = EXT4_MIN_BLOCK_SIZE % blocksize;
    4869                 :          0 :         set_blocksize(bdev, blocksize);
    4870                 :          0 :         if (!(bh = __bread(bdev, sb_block, blocksize))) {
    4871                 :          0 :                 ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
    4872                 :            :                        "external journal");
    4873                 :          0 :                 goto out_bdev;
    4874                 :            :         }
    4875                 :            : 
    4876                 :          0 :         es = (struct ext4_super_block *) (bh->b_data + offset);
    4877                 :          0 :         if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
    4878                 :          0 :             !(le32_to_cpu(es->s_feature_incompat) &
    4879                 :            :               EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
    4880                 :          0 :                 ext4_msg(sb, KERN_ERR, "external journal has "
    4881                 :            :                                         "bad superblock");
    4882                 :            :                 brelse(bh);
    4883                 :            :                 goto out_bdev;
    4884                 :            :         }
    4885                 :            : 
    4886                 :          0 :         if ((le32_to_cpu(es->s_feature_ro_compat) &
    4887                 :          0 :              EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
    4888                 :          0 :             es->s_checksum != ext4_superblock_csum(sb, es)) {
    4889                 :          0 :                 ext4_msg(sb, KERN_ERR, "external journal has "
    4890                 :            :                                        "corrupt superblock");
    4891                 :            :                 brelse(bh);
    4892                 :            :                 goto out_bdev;
    4893                 :            :         }
    4894                 :            : 
    4895                 :          0 :         if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
    4896                 :          0 :                 ext4_msg(sb, KERN_ERR, "journal UUID does not match");
    4897                 :            :                 brelse(bh);
    4898                 :            :                 goto out_bdev;
    4899                 :            :         }
    4900                 :            : 
    4901                 :            :         len = ext4_blocks_count(es);
    4902                 :          0 :         start = sb_block + 1;
    4903                 :            :         brelse(bh);     /* we're done with the superblock */
    4904                 :            : 
    4905                 :          0 :         journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
    4906                 :            :                                         start, len, blocksize);
    4907                 :          0 :         if (!journal) {
    4908                 :          0 :                 ext4_msg(sb, KERN_ERR, "failed to create device journal");
    4909                 :          0 :                 goto out_bdev;
    4910                 :            :         }
    4911                 :          0 :         journal->j_private = sb;
    4912                 :          0 :         ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
    4913                 :          0 :         wait_on_buffer(journal->j_sb_buffer);
    4914                 :          0 :         if (!buffer_uptodate(journal->j_sb_buffer)) {
    4915                 :          0 :                 ext4_msg(sb, KERN_ERR, "I/O error on journal device");
    4916                 :          0 :                 goto out_journal;
    4917                 :            :         }
    4918                 :          0 :         if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
    4919                 :          0 :                 ext4_msg(sb, KERN_ERR, "External journal has more than one "
    4920                 :            :                                         "user (unsupported) - %d",
    4921                 :            :                         be32_to_cpu(journal->j_superblock->s_nr_users));
    4922                 :          0 :                 goto out_journal;
    4923                 :            :         }
    4924                 :          0 :         EXT4_SB(sb)->journal_bdev = bdev;
    4925                 :          0 :         ext4_init_journal_params(sb, journal);
    4926                 :          0 :         return journal;
    4927                 :            : 
    4928                 :            : out_journal:
    4929                 :          0 :         jbd2_journal_destroy(journal);
    4930                 :            : out_bdev:
    4931                 :            :         ext4_blkdev_put(bdev);
    4932                 :          0 :         return NULL;
    4933                 :            : }
    4934                 :            : 
    4935                 :          3 : static int ext4_load_journal(struct super_block *sb,
    4936                 :            :                              struct ext4_super_block *es,
    4937                 :            :                              unsigned long journal_devnum)
    4938                 :            : {
    4939                 :            :         journal_t *journal;
    4940                 :          3 :         unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
    4941                 :            :         dev_t journal_dev;
    4942                 :            :         int err = 0;
    4943                 :            :         int really_read_only;
    4944                 :            : 
    4945                 :          3 :         BUG_ON(!ext4_has_feature_journal(sb));
    4946                 :            : 
    4947                 :          3 :         if (journal_devnum &&
    4948                 :          0 :             journal_devnum != le32_to_cpu(es->s_journal_dev)) {
    4949                 :          0 :                 ext4_msg(sb, KERN_INFO, "external journal device major/minor "
    4950                 :            :                         "numbers have changed");
    4951                 :          0 :                 journal_dev = new_decode_dev(journal_devnum);
    4952                 :            :         } else
    4953                 :          3 :                 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
    4954                 :            : 
    4955                 :          3 :         really_read_only = bdev_read_only(sb->s_bdev);
    4956                 :            : 
    4957                 :            :         /*
    4958                 :            :          * Are we loading a blank journal or performing recovery after a
    4959                 :            :          * crash?  For recovery, we need to check in advance whether we
    4960                 :            :          * can get read-write access to the device.
    4961                 :            :          */
    4962                 :          3 :         if (ext4_has_feature_journal_needs_recovery(sb)) {
    4963                 :          1 :                 if (sb_rdonly(sb)) {
    4964                 :          0 :                         ext4_msg(sb, KERN_INFO, "INFO: recovery "
    4965                 :            :                                         "required on readonly filesystem");
    4966                 :          0 :                         if (really_read_only) {
    4967                 :          0 :                                 ext4_msg(sb, KERN_ERR, "write access "
    4968                 :            :                                         "unavailable, cannot proceed "
    4969                 :            :                                         "(try mounting with noload)");
    4970                 :          0 :                                 return -EROFS;
    4971                 :            :                         }
    4972                 :          0 :                         ext4_msg(sb, KERN_INFO, "write access will "
    4973                 :            :                                "be enabled during recovery");
    4974                 :            :                 }
    4975                 :            :         }
    4976                 :            : 
    4977                 :          3 :         if (journal_inum && journal_dev) {
    4978                 :          0 :                 ext4_msg(sb, KERN_ERR, "filesystem has both journal "
    4979                 :            :                        "and inode journals!");
    4980                 :          0 :                 return -EINVAL;
    4981                 :            :         }
    4982                 :            : 
    4983                 :          3 :         if (journal_inum) {
    4984                 :          3 :                 if (!(journal = ext4_get_journal(sb, journal_inum)))
    4985                 :            :                         return -EINVAL;
    4986                 :            :         } else {
    4987                 :          0 :                 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
    4988                 :            :                         return -EINVAL;
    4989                 :            :         }
    4990                 :            : 
    4991                 :          3 :         if (!(journal->j_flags & JBD2_BARRIER))
    4992                 :          0 :                 ext4_msg(sb, KERN_INFO, "barriers disabled");
    4993                 :            : 
    4994                 :          3 :         if (!ext4_has_feature_journal_needs_recovery(sb))
    4995                 :          3 :                 err = jbd2_journal_wipe(journal, !really_read_only);
    4996                 :          3 :         if (!err) {
    4997                 :            :                 char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
    4998                 :          3 :                 if (save)
    4999                 :          3 :                         memcpy(save, ((char *) es) +
    5000                 :            :                                EXT4_S_ERR_START, EXT4_S_ERR_LEN);
    5001                 :          3 :                 err = jbd2_journal_load(journal);
    5002                 :          3 :                 if (save)
    5003                 :          3 :                         memcpy(((char *) es) + EXT4_S_ERR_START,
    5004                 :            :                                save, EXT4_S_ERR_LEN);
    5005                 :          3 :                 kfree(save);
    5006                 :            :         }
    5007                 :            : 
    5008                 :          3 :         if (err) {
    5009                 :          0 :                 ext4_msg(sb, KERN_ERR, "error loading journal");
    5010                 :          0 :                 jbd2_journal_destroy(journal);
    5011                 :          0 :                 return err;
    5012                 :            :         }
    5013                 :            : 
    5014                 :          3 :         EXT4_SB(sb)->s_journal = journal;
    5015                 :          3 :         ext4_clear_journal_err(sb, es);
    5016                 :            : 
    5017                 :          3 :         if (!really_read_only && journal_devnum &&
    5018                 :          0 :             journal_devnum != le32_to_cpu(es->s_journal_dev)) {
    5019                 :          0 :                 es->s_journal_dev = cpu_to_le32(journal_devnum);
    5020                 :            : 
    5021                 :            :                 /* Make sure we flush the recovery flag to disk. */
    5022                 :          0 :                 ext4_commit_super(sb, 1);
    5023                 :            :         }
    5024                 :            : 
    5025                 :            :         return 0;
    5026                 :            : }
    5027                 :            : 
    5028                 :          3 : static int ext4_commit_super(struct super_block *sb, int sync)
    5029                 :            : {
    5030                 :          3 :         struct ext4_super_block *es = EXT4_SB(sb)->s_es;
    5031                 :          3 :         struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
    5032                 :            :         int error = 0;
    5033                 :            : 
    5034                 :          3 :         if (!sbh || block_device_ejected(sb))
    5035                 :            :                 return error;
    5036                 :            : 
    5037                 :            :         /*
    5038                 :            :          * The superblock bh should be mapped, but it might not be if the
    5039                 :            :          * device was hot-removed. Not much we can do but fail the I/O.
    5040                 :            :          */
    5041                 :          3 :         if (!buffer_mapped(sbh))
    5042                 :            :                 return error;
    5043                 :            : 
    5044                 :            :         /*
    5045                 :            :          * If the file system is mounted read-only, don't update the
    5046                 :            :          * superblock write time.  This avoids updating the superblock
    5047                 :            :          * write time when we are mounting the root file system
    5048                 :            :          * read/only but we need to replay the journal; at that point,
    5049                 :            :          * for people who are east of GMT and who make their clock
    5050                 :            :          * tick in localtime for Windows bug-for-bug compatibility,
    5051                 :            :          * the clock is set in the future, and this will cause e2fsck
    5052                 :            :          * to complain and force a full file system check.
    5053                 :            :          */
    5054                 :          3 :         if (!(sb->s_flags & SB_RDONLY))
    5055                 :            :                 ext4_update_tstamp(es, s_wtime);
    5056                 :          3 :         if (sb->s_bdev->bd_part)
    5057                 :          3 :                 es->s_kbytes_written =
    5058                 :          3 :                         cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
    5059                 :            :                             ((part_stat_read(sb->s_bdev->bd_part,
    5060                 :            :                                              sectors[STAT_WRITE]) -
    5061                 :            :                               EXT4_SB(sb)->s_sectors_written_start) >> 1));
    5062                 :            :         else
    5063                 :          0 :                 es->s_kbytes_written =
    5064                 :          0 :                         cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
    5065                 :          3 :         if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
    5066                 :          2 :                 ext4_free_blocks_count_set(es,
    5067                 :          2 :                         EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
    5068                 :            :                                 &EXT4_SB(sb)->s_freeclusters_counter)));
    5069                 :          3 :         if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
    5070                 :          2 :                 es->s_free_inodes_count =
    5071                 :          2 :                         cpu_to_le32(percpu_counter_sum_positive(
    5072                 :            :                                 &EXT4_SB(sb)->s_freeinodes_counter));
    5073                 :            :         BUFFER_TRACE(sbh, "marking dirty");
    5074                 :          3 :         ext4_superblock_csum_set(sb);
    5075                 :          3 :         if (sync)
    5076                 :          3 :                 lock_buffer(sbh);
    5077                 :          3 :         if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
    5078                 :            :                 /*
    5079                 :            :                  * Oh, dear.  A previous attempt to write the
    5080                 :            :                  * superblock failed.  This could happen because the
    5081                 :            :                  * USB device was yanked out.  Or it could happen to
    5082                 :            :                  * be a transient write error and maybe the block will
    5083                 :            :                  * be remapped.  Nothing we can do but to retry the
    5084                 :            :                  * write and hope for the best.
    5085                 :            :                  */
    5086                 :          0 :                 ext4_msg(sb, KERN_ERR, "previous I/O error to "
    5087                 :            :                        "superblock detected");
    5088                 :            :                 clear_buffer_write_io_error(sbh);
    5089                 :            :                 set_buffer_uptodate(sbh);
    5090                 :            :         }
    5091                 :          3 :         mark_buffer_dirty(sbh);
    5092                 :          3 :         if (sync) {
    5093                 :          3 :                 unlock_buffer(sbh);
    5094                 :          3 :                 error = __sync_dirty_buffer(sbh,
    5095                 :          3 :                         REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
    5096                 :          3 :                 if (buffer_write_io_error(sbh)) {
    5097                 :          0 :                         ext4_msg(sb, KERN_ERR, "I/O error while writing "
    5098                 :            :                                "superblock");
    5099                 :            :                         clear_buffer_write_io_error(sbh);
    5100                 :            :                         set_buffer_uptodate(sbh);
    5101                 :            :                 }
    5102                 :            :         }
    5103                 :          3 :         return error;
    5104                 :            : }
    5105                 :            : 
    5106                 :            : /*
    5107                 :            :  * Have we just finished recovery?  If so, and if we are mounting (or
    5108                 :            :  * remounting) the filesystem readonly, then we will end up with a
    5109                 :            :  * consistent fs on disk.  Record that fact.
    5110                 :            :  */
    5111                 :          1 : static void ext4_mark_recovery_complete(struct super_block *sb,
    5112                 :            :                                         struct ext4_super_block *es)
    5113                 :            : {
    5114                 :          1 :         journal_t *journal = EXT4_SB(sb)->s_journal;
    5115                 :            : 
    5116                 :          1 :         if (!ext4_has_feature_journal(sb)) {
    5117                 :          0 :                 BUG_ON(journal != NULL);
    5118                 :          1 :                 return;
    5119                 :            :         }
    5120                 :          1 :         jbd2_journal_lock_updates(journal);
    5121                 :          1 :         if (jbd2_journal_flush(journal) < 0)
    5122                 :            :                 goto out;
    5123                 :            : 
    5124                 :          1 :         if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
    5125                 :            :                 ext4_clear_feature_journal_needs_recovery(sb);
    5126                 :          0 :                 ext4_commit_super(sb, 1);
    5127                 :            :         }
    5128                 :            : 
    5129                 :            : out:
    5130                 :          1 :         jbd2_journal_unlock_updates(journal);
    5131                 :            : }
    5132                 :            : 
    5133                 :            : /*
    5134                 :            :  * If we are mounting (or read-write remounting) a filesystem whose journal
    5135                 :            :  * has recorded an error from a previous lifetime, move that error to the
    5136                 :            :  * main filesystem now.
    5137                 :            :  */
    5138                 :          3 : static void ext4_clear_journal_err(struct super_block *sb,
    5139                 :            :                                    struct ext4_super_block *es)
    5140                 :            : {
    5141                 :            :         journal_t *journal;
    5142                 :            :         int j_errno;
    5143                 :            :         const char *errstr;
    5144                 :            : 
    5145                 :          3 :         BUG_ON(!ext4_has_feature_journal(sb));
    5146                 :            : 
    5147                 :          3 :         journal = EXT4_SB(sb)->s_journal;
    5148                 :            : 
    5149                 :            :         /*
    5150                 :            :          * Now check for any error status which may have been recorded in the
    5151                 :            :          * journal by a prior ext4_error() or ext4_abort()
    5152                 :            :          */
    5153                 :            : 
    5154                 :          3 :         j_errno = jbd2_journal_errno(journal);
    5155                 :          3 :         if (j_errno) {
    5156                 :            :                 char nbuf[16];
    5157                 :            : 
    5158                 :          0 :                 errstr = ext4_decode_error(sb, j_errno, nbuf);
    5159                 :          0 :                 ext4_warning(sb, "Filesystem error recorded "
    5160                 :            :                              "from previous mount: %s", errstr);
    5161                 :          0 :                 ext4_warning(sb, "Marking fs in need of filesystem check.");
    5162                 :            : 
    5163                 :          0 :                 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
    5164                 :          0 :                 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
    5165                 :          0 :                 ext4_commit_super(sb, 1);
    5166                 :            : 
    5167                 :          0 :                 jbd2_journal_clear_err(journal);
    5168                 :          0 :                 jbd2_journal_update_sb_errno(journal);
    5169                 :            :         }
    5170                 :          3 : }
    5171                 :            : 
    5172                 :            : /*
    5173                 :            :  * Force the running and committing transactions to commit,
    5174                 :            :  * and wait on the commit.
    5175                 :            :  */
    5176                 :          1 : int ext4_force_commit(struct super_block *sb)
    5177                 :            : {
    5178                 :            :         journal_t *journal;
    5179                 :            : 
    5180                 :          1 :         if (sb_rdonly(sb))
    5181                 :            :                 return 0;
    5182                 :            : 
    5183                 :          1 :         journal = EXT4_SB(sb)->s_journal;
    5184                 :          1 :         return ext4_journal_force_commit(journal);
    5185                 :            : }
    5186                 :            : 
    5187                 :          3 : static int ext4_sync_fs(struct super_block *sb, int wait)
    5188                 :            : {
    5189                 :            :         int ret = 0;
    5190                 :            :         tid_t target;
    5191                 :            :         bool needs_barrier = false;
    5192                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    5193                 :            : 
    5194                 :          3 :         if (unlikely(ext4_forced_shutdown(sbi)))
    5195                 :            :                 return 0;
    5196                 :            : 
    5197                 :          3 :         trace_ext4_sync_fs(sb, wait);
    5198                 :          3 :         flush_workqueue(sbi->rsv_conversion_wq);
    5199                 :            :         /*
    5200                 :            :          * Writeback quota in non-journalled quota case - journalled quota has
    5201                 :            :          * no dirty dquots
    5202                 :            :          */
    5203                 :          3 :         dquot_writeback_dquots(sb, -1);
    5204                 :            :         /*
    5205                 :            :          * Data writeback is possible w/o journal transaction, so barrier must
    5206                 :            :          * being sent at the end of the function. But we can skip it if
    5207                 :            :          * transaction_commit will do it for us.
    5208                 :            :          */
    5209                 :          3 :         if (sbi->s_journal) {
    5210                 :          3 :                 target = jbd2_get_latest_transaction(sbi->s_journal);
    5211                 :          3 :                 if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
    5212                 :          3 :                     !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
    5213                 :            :                         needs_barrier = true;
    5214                 :            : 
    5215                 :          3 :                 if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
    5216                 :          3 :                         if (wait)
    5217                 :          3 :                                 ret = jbd2_log_wait_commit(sbi->s_journal,
    5218                 :            :                                                            target);
    5219                 :            :                 }
    5220                 :          0 :         } else if (wait && test_opt(sb, BARRIER))
    5221                 :            :                 needs_barrier = true;
    5222                 :          3 :         if (needs_barrier) {
    5223                 :            :                 int err;
    5224                 :          1 :                 err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
    5225                 :          1 :                 if (!ret)
    5226                 :            :                         ret = err;
    5227                 :            :         }
    5228                 :            : 
    5229                 :          3 :         return ret;
    5230                 :            : }
    5231                 :            : 
    5232                 :            : /*
    5233                 :            :  * LVM calls this function before a (read-only) snapshot is created.  This
    5234                 :            :  * gives us a chance to flush the journal completely and mark the fs clean.
    5235                 :            :  *
    5236                 :            :  * Note that only this function cannot bring a filesystem to be in a clean
    5237                 :            :  * state independently. It relies on upper layer to stop all data & metadata
    5238                 :            :  * modifications.
    5239                 :            :  */
    5240                 :          0 : static int ext4_freeze(struct super_block *sb)
    5241                 :            : {
    5242                 :            :         int error = 0;
    5243                 :            :         journal_t *journal;
    5244                 :            : 
    5245                 :          0 :         if (sb_rdonly(sb))
    5246                 :            :                 return 0;
    5247                 :            : 
    5248                 :          0 :         journal = EXT4_SB(sb)->s_journal;
    5249                 :            : 
    5250                 :          0 :         if (journal) {
    5251                 :            :                 /* Now we set up the journal barrier. */
    5252                 :          0 :                 jbd2_journal_lock_updates(journal);
    5253                 :            : 
    5254                 :            :                 /*
    5255                 :            :                  * Don't clear the needs_recovery flag if we failed to
    5256                 :            :                  * flush the journal.
    5257                 :            :                  */
    5258                 :          0 :                 error = jbd2_journal_flush(journal);
    5259                 :          0 :                 if (error < 0)
    5260                 :            :                         goto out;
    5261                 :            : 
    5262                 :            :                 /* Journal blocked and flushed, clear needs_recovery flag. */
    5263                 :            :                 ext4_clear_feature_journal_needs_recovery(sb);
    5264                 :            :         }
    5265                 :            : 
    5266                 :          0 :         error = ext4_commit_super(sb, 1);
    5267                 :            : out:
    5268                 :          0 :         if (journal)
    5269                 :            :                 /* we rely on upper layer to stop further updates */
    5270                 :          0 :                 jbd2_journal_unlock_updates(journal);
    5271                 :          0 :         return error;
    5272                 :            : }
    5273                 :            : 
    5274                 :            : /*
    5275                 :            :  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
    5276                 :            :  * flag here, even though the filesystem is not technically dirty yet.
    5277                 :            :  */
    5278                 :          0 : static int ext4_unfreeze(struct super_block *sb)
    5279                 :            : {
    5280                 :          0 :         if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
    5281                 :            :                 return 0;
    5282                 :            : 
    5283                 :          0 :         if (EXT4_SB(sb)->s_journal) {
    5284                 :            :                 /* Reset the needs_recovery flag before the fs is unlocked. */
    5285                 :            :                 ext4_set_feature_journal_needs_recovery(sb);
    5286                 :            :         }
    5287                 :            : 
    5288                 :          0 :         ext4_commit_super(sb, 1);
    5289                 :          0 :         return 0;
    5290                 :            : }
    5291                 :            : 
    5292                 :            : /*
    5293                 :            :  * Structure to save mount options for ext4_remount's benefit
    5294                 :            :  */
    5295                 :            : struct ext4_mount_options {
    5296                 :            :         unsigned long s_mount_opt;
    5297                 :            :         unsigned long s_mount_opt2;
    5298                 :            :         kuid_t s_resuid;
    5299                 :            :         kgid_t s_resgid;
    5300                 :            :         unsigned long s_commit_interval;
    5301                 :            :         u32 s_min_batch_time, s_max_batch_time;
    5302                 :            : #ifdef CONFIG_QUOTA
    5303                 :            :         int s_jquota_fmt;
    5304                 :            :         char *s_qf_names[EXT4_MAXQUOTAS];
    5305                 :            : #endif
    5306                 :            : };
    5307                 :            : 
    5308                 :          3 : static int ext4_remount(struct super_block *sb, int *flags, char *data)
    5309                 :            : {
    5310                 :            :         struct ext4_super_block *es;
    5311                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    5312                 :            :         unsigned long old_sb_flags;
    5313                 :            :         struct ext4_mount_options old_opts;
    5314                 :            :         int enable_quota = 0;
    5315                 :            :         ext4_group_t g;
    5316                 :          3 :         unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
    5317                 :            :         int err = 0;
    5318                 :            : #ifdef CONFIG_QUOTA
    5319                 :            :         int i, j;
    5320                 :            :         char *to_free[EXT4_MAXQUOTAS];
    5321                 :            : #endif
    5322                 :          3 :         char *orig_data = kstrdup(data, GFP_KERNEL);
    5323                 :            : 
    5324                 :          3 :         if (data && !orig_data)
    5325                 :            :                 return -ENOMEM;
    5326                 :            : 
    5327                 :            :         /* Store the original options */
    5328                 :          3 :         old_sb_flags = sb->s_flags;
    5329                 :          3 :         old_opts.s_mount_opt = sbi->s_mount_opt;
    5330                 :          3 :         old_opts.s_mount_opt2 = sbi->s_mount_opt2;
    5331                 :          3 :         old_opts.s_resuid = sbi->s_resuid;
    5332                 :          3 :         old_opts.s_resgid = sbi->s_resgid;
    5333                 :          3 :         old_opts.s_commit_interval = sbi->s_commit_interval;
    5334                 :          3 :         old_opts.s_min_batch_time = sbi->s_min_batch_time;
    5335                 :          3 :         old_opts.s_max_batch_time = sbi->s_max_batch_time;
    5336                 :            : #ifdef CONFIG_QUOTA
    5337                 :          3 :         old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
    5338                 :          3 :         for (i = 0; i < EXT4_MAXQUOTAS; i++)
    5339                 :          3 :                 if (sbi->s_qf_names[i]) {
    5340                 :            :                         char *qf_name = get_qf_name(sb, sbi, i);
    5341                 :            : 
    5342                 :          0 :                         old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
    5343                 :          0 :                         if (!old_opts.s_qf_names[i]) {
    5344                 :          0 :                                 for (j = 0; j < i; j++)
    5345                 :          0 :                                         kfree(old_opts.s_qf_names[j]);
    5346                 :          0 :                                 kfree(orig_data);
    5347                 :          0 :                                 return -ENOMEM;
    5348                 :            :                         }
    5349                 :            :                 } else
    5350                 :          3 :                         old_opts.s_qf_names[i] = NULL;
    5351                 :            : #endif
    5352                 :          3 :         if (sbi->s_journal && sbi->s_journal->j_task->io_context)
    5353                 :          3 :                 journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
    5354                 :            : 
    5355                 :          3 :         if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
    5356                 :            :                 err = -EINVAL;
    5357                 :            :                 goto restore_opts;
    5358                 :            :         }
    5359                 :            : 
    5360                 :          3 :         if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
    5361                 :          3 :             test_opt(sb, JOURNAL_CHECKSUM)) {
    5362                 :          0 :                 ext4_msg(sb, KERN_ERR, "changing journal_checksum "
    5363                 :            :                          "during remount not supported; ignoring");
    5364                 :          0 :                 sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
    5365                 :            :         }
    5366                 :            : 
    5367                 :          3 :         if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
    5368                 :          0 :                 if (test_opt2(sb, EXPLICIT_DELALLOC)) {
    5369                 :          0 :                         ext4_msg(sb, KERN_ERR, "can't mount with "
    5370                 :            :                                  "both data=journal and delalloc");
    5371                 :            :                         err = -EINVAL;
    5372                 :          0 :                         goto restore_opts;
    5373                 :            :                 }
    5374                 :          0 :                 if (test_opt(sb, DIOREAD_NOLOCK)) {
    5375                 :          0 :                         ext4_msg(sb, KERN_ERR, "can't mount with "
    5376                 :            :                                  "both data=journal and dioread_nolock");
    5377                 :            :                         err = -EINVAL;
    5378                 :          0 :                         goto restore_opts;
    5379                 :            :                 }
    5380                 :          3 :         } else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
    5381                 :          3 :                 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
    5382                 :          0 :                         ext4_msg(sb, KERN_ERR, "can't mount with "
    5383                 :            :                                 "journal_async_commit in data=ordered mode");
    5384                 :            :                         err = -EINVAL;
    5385                 :          0 :                         goto restore_opts;
    5386                 :            :                 }
    5387                 :            :         }
    5388                 :            : 
    5389                 :          3 :         if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
    5390                 :          0 :                 ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
    5391                 :            :                 err = -EINVAL;
    5392                 :          0 :                 goto restore_opts;
    5393                 :            :         }
    5394                 :            : 
    5395                 :          3 :         if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
    5396                 :          0 :                 ext4_abort(sb, "Abort forced by user");
    5397                 :            : 
    5398                 :          3 :         sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
    5399                 :          3 :                 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
    5400                 :            : 
    5401                 :          3 :         es = sbi->s_es;
    5402                 :            : 
    5403                 :          3 :         if (sbi->s_journal) {
    5404                 :          3 :                 ext4_init_journal_params(sb, sbi->s_journal);
    5405                 :          3 :                 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
    5406                 :            :         }
    5407                 :            : 
    5408                 :          3 :         if (*flags & SB_LAZYTIME)
    5409                 :          0 :                 sb->s_flags |= SB_LAZYTIME;
    5410                 :            : 
    5411                 :          3 :         if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
    5412                 :          2 :                 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
    5413                 :            :                         err = -EROFS;
    5414                 :            :                         goto restore_opts;
    5415                 :            :                 }
    5416                 :            : 
    5417                 :          2 :                 if (*flags & SB_RDONLY) {
    5418                 :          0 :                         err = sync_filesystem(sb);
    5419                 :          0 :                         if (err < 0)
    5420                 :            :                                 goto restore_opts;
    5421                 :            :                         err = dquot_suspend(sb, -1);
    5422                 :          0 :                         if (err < 0)
    5423                 :            :                                 goto restore_opts;
    5424                 :            : 
    5425                 :            :                         /*
    5426                 :            :                          * First of all, the unconditional stuff we have to do
    5427                 :            :                          * to disable replay of the journal when we next remount
    5428                 :            :                          */
    5429                 :          0 :                         sb->s_flags |= SB_RDONLY;
    5430                 :            : 
    5431                 :            :                         /*
    5432                 :            :                          * OK, test if we are remounting a valid rw partition
    5433                 :            :                          * readonly, and if so set the rdonly flag and then
    5434                 :            :                          * mark the partition as valid again.
    5435                 :            :                          */
    5436                 :          0 :                         if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
    5437                 :          0 :                             (sbi->s_mount_state & EXT4_VALID_FS))
    5438                 :          0 :                                 es->s_state = cpu_to_le16(sbi->s_mount_state);
    5439                 :            : 
    5440                 :          0 :                         if (sbi->s_journal)
    5441                 :          0 :                                 ext4_mark_recovery_complete(sb, es);
    5442                 :          0 :                         if (sbi->s_mmp_tsk)
    5443                 :          0 :                                 kthread_stop(sbi->s_mmp_tsk);
    5444                 :            :                 } else {
    5445                 :            :                         /* Make sure we can mount this feature set readwrite */
    5446                 :          2 :                         if (ext4_has_feature_readonly(sb) ||
    5447                 :          2 :                             !ext4_feature_set_ok(sb, 0)) {
    5448                 :            :                                 err = -EROFS;
    5449                 :            :                                 goto restore_opts;
    5450                 :            :                         }
    5451                 :            :                         /*
    5452                 :            :                          * Make sure the group descriptor checksums
    5453                 :            :                          * are sane.  If they aren't, refuse to remount r/w.
    5454                 :            :                          */
    5455                 :          2 :                         for (g = 0; g < sbi->s_groups_count; g++) {
    5456                 :          2 :                                 struct ext4_group_desc *gdp =
    5457                 :            :                                         ext4_get_group_desc(sb, g, NULL);
    5458                 :            : 
    5459                 :          2 :                                 if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
    5460                 :          0 :                                         ext4_msg(sb, KERN_ERR,
    5461                 :            :                "ext4_remount: Checksum for group %u failed (%u!=%u)",
    5462                 :            :                 g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
    5463                 :            :                                                le16_to_cpu(gdp->bg_checksum));
    5464                 :            :                                         err = -EFSBADCRC;
    5465                 :          0 :                                         goto restore_opts;
    5466                 :            :                                 }
    5467                 :            :                         }
    5468                 :            : 
    5469                 :            :                         /*
    5470                 :            :                          * If we have an unprocessed orphan list hanging
    5471                 :            :                          * around from a previously readonly bdev mount,
    5472                 :            :                          * require a full umount/remount for now.
    5473                 :            :                          */
    5474                 :          2 :                         if (es->s_last_orphan) {
    5475                 :          0 :                                 ext4_msg(sb, KERN_WARNING, "Couldn't "
    5476                 :            :                                        "remount RDWR because of unprocessed "
    5477                 :            :                                        "orphan inode list.  Please "
    5478                 :            :                                        "umount/remount instead");
    5479                 :            :                                 err = -EINVAL;
    5480                 :          0 :                                 goto restore_opts;
    5481                 :            :                         }
    5482                 :            : 
    5483                 :            :                         /*
    5484                 :            :                          * Mounting a RDONLY partition read-write, so reread
    5485                 :            :                          * and store the current valid flag.  (It may have
    5486                 :            :                          * been changed by e2fsck since we originally mounted
    5487                 :            :                          * the partition.)
    5488                 :            :                          */
    5489                 :          2 :                         if (sbi->s_journal)
    5490                 :          2 :                                 ext4_clear_journal_err(sb, es);
    5491                 :          2 :                         sbi->s_mount_state = le16_to_cpu(es->s_state);
    5492                 :            : 
    5493                 :          2 :                         err = ext4_setup_super(sb, es, 0);
    5494                 :          2 :                         if (err)
    5495                 :            :                                 goto restore_opts;
    5496                 :            : 
    5497                 :          2 :                         sb->s_flags &= ~SB_RDONLY;
    5498                 :          2 :                         if (ext4_has_feature_mmp(sb))
    5499                 :          0 :                                 if (ext4_multi_mount_protect(sb,
    5500                 :            :                                                 le64_to_cpu(es->s_mmp_block))) {
    5501                 :            :                                         err = -EROFS;
    5502                 :            :                                         goto restore_opts;
    5503                 :            :                                 }
    5504                 :            :                         enable_quota = 1;
    5505                 :            :                 }
    5506                 :            :         }
    5507                 :            : 
    5508                 :            :         /*
    5509                 :            :          * Reinitialize lazy itable initialization thread based on
    5510                 :            :          * current settings
    5511                 :            :          */
    5512                 :          3 :         if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
    5513                 :          0 :                 ext4_unregister_li_request(sb);
    5514                 :            :         else {
    5515                 :            :                 ext4_group_t first_not_zeroed;
    5516                 :          3 :                 first_not_zeroed = ext4_has_uninit_itable(sb);
    5517                 :          3 :                 ext4_register_li_request(sb, first_not_zeroed);
    5518                 :            :         }
    5519                 :            : 
    5520                 :          3 :         ext4_setup_system_zone(sb);
    5521                 :          3 :         if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
    5522                 :          0 :                 err = ext4_commit_super(sb, 1);
    5523                 :          0 :                 if (err)
    5524                 :            :                         goto restore_opts;
    5525                 :            :         }
    5526                 :            : 
    5527                 :            : #ifdef CONFIG_QUOTA
    5528                 :            :         /* Release old quota file names */
    5529                 :          3 :         for (i = 0; i < EXT4_MAXQUOTAS; i++)
    5530                 :          3 :                 kfree(old_opts.s_qf_names[i]);
    5531                 :          3 :         if (enable_quota) {
    5532                 :          2 :                 if (sb_any_quota_suspended(sb))
    5533                 :          0 :                         dquot_resume(sb, -1);
    5534                 :          2 :                 else if (ext4_has_feature_quota(sb)) {
    5535                 :          0 :                         err = ext4_enable_quotas(sb);
    5536                 :          0 :                         if (err)
    5537                 :            :                                 goto restore_opts;
    5538                 :            :                 }
    5539                 :            :         }
    5540                 :            : #endif
    5541                 :            : 
    5542                 :          3 :         *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
    5543                 :          3 :         ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
    5544                 :          3 :         kfree(orig_data);
    5545                 :          3 :         return 0;
    5546                 :            : 
    5547                 :            : restore_opts:
    5548                 :          0 :         sb->s_flags = old_sb_flags;
    5549                 :          0 :         sbi->s_mount_opt = old_opts.s_mount_opt;
    5550                 :          0 :         sbi->s_mount_opt2 = old_opts.s_mount_opt2;
    5551                 :          0 :         sbi->s_resuid = old_opts.s_resuid;
    5552                 :          0 :         sbi->s_resgid = old_opts.s_resgid;
    5553                 :          0 :         sbi->s_commit_interval = old_opts.s_commit_interval;
    5554                 :          0 :         sbi->s_min_batch_time = old_opts.s_min_batch_time;
    5555                 :          0 :         sbi->s_max_batch_time = old_opts.s_max_batch_time;
    5556                 :            : #ifdef CONFIG_QUOTA
    5557                 :          0 :         sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
    5558                 :          0 :         for (i = 0; i < EXT4_MAXQUOTAS; i++) {
    5559                 :          0 :                 to_free[i] = get_qf_name(sb, sbi, i);
    5560                 :          0 :                 rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
    5561                 :            :         }
    5562                 :          0 :         synchronize_rcu();
    5563                 :          0 :         for (i = 0; i < EXT4_MAXQUOTAS; i++)
    5564                 :          0 :                 kfree(to_free[i]);
    5565                 :            : #endif
    5566                 :          0 :         kfree(orig_data);
    5567                 :          0 :         return err;
    5568                 :            : }
    5569                 :            : 
    5570                 :            : #ifdef CONFIG_QUOTA
    5571                 :          0 : static int ext4_statfs_project(struct super_block *sb,
    5572                 :            :                                kprojid_t projid, struct kstatfs *buf)
    5573                 :            : {
    5574                 :            :         struct kqid qid;
    5575                 :            :         struct dquot *dquot;
    5576                 :            :         u64 limit;
    5577                 :            :         u64 curblock;
    5578                 :            : 
    5579                 :            :         qid = make_kqid_projid(projid);
    5580                 :          0 :         dquot = dqget(sb, qid);
    5581                 :          0 :         if (IS_ERR(dquot))
    5582                 :          0 :                 return PTR_ERR(dquot);
    5583                 :            :         spin_lock(&dquot->dq_dqb_lock);
    5584                 :            : 
    5585                 :            :         limit = 0;
    5586                 :          0 :         if (dquot->dq_dqb.dqb_bsoftlimit &&
    5587                 :            :             (!limit || dquot->dq_dqb.dqb_bsoftlimit < limit))
    5588                 :          0 :                 limit = dquot->dq_dqb.dqb_bsoftlimit;
    5589                 :          0 :         if (dquot->dq_dqb.dqb_bhardlimit &&
    5590                 :          0 :             (!limit || dquot->dq_dqb.dqb_bhardlimit < limit))
    5591                 :          0 :                 limit = dquot->dq_dqb.dqb_bhardlimit;
    5592                 :          0 :         limit >>= sb->s_blocksize_bits;
    5593                 :            : 
    5594                 :          0 :         if (limit && buf->f_blocks > limit) {
    5595                 :          0 :                 curblock = (dquot->dq_dqb.dqb_curspace +
    5596                 :          0 :                             dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
    5597                 :          0 :                 buf->f_blocks = limit;
    5598                 :          0 :                 buf->f_bfree = buf->f_bavail =
    5599                 :            :                         (buf->f_blocks > curblock) ?
    5600                 :          0 :                          (buf->f_blocks - curblock) : 0;
    5601                 :            :         }
    5602                 :            : 
    5603                 :            :         limit = 0;
    5604                 :          0 :         if (dquot->dq_dqb.dqb_isoftlimit &&
    5605                 :            :             (!limit || dquot->dq_dqb.dqb_isoftlimit < limit))
    5606                 :          0 :                 limit = dquot->dq_dqb.dqb_isoftlimit;
    5607                 :          0 :         if (dquot->dq_dqb.dqb_ihardlimit &&
    5608                 :          0 :             (!limit || dquot->dq_dqb.dqb_ihardlimit < limit))
    5609                 :          0 :                 limit = dquot->dq_dqb.dqb_ihardlimit;
    5610                 :            : 
    5611                 :          0 :         if (limit && buf->f_files > limit) {
    5612                 :          0 :                 buf->f_files = limit;
    5613                 :          0 :                 buf->f_ffree =
    5614                 :          0 :                         (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
    5615                 :          0 :                          (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
    5616                 :            :         }
    5617                 :            : 
    5618                 :            :         spin_unlock(&dquot->dq_dqb_lock);
    5619                 :          0 :         dqput(dquot);
    5620                 :          0 :         return 0;
    5621                 :            : }
    5622                 :            : #endif
    5623                 :            : 
    5624                 :          3 : static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
    5625                 :            : {
    5626                 :          3 :         struct super_block *sb = dentry->d_sb;
    5627                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    5628                 :          3 :         struct ext4_super_block *es = sbi->s_es;
    5629                 :            :         ext4_fsblk_t overhead = 0, resv_blocks;
    5630                 :            :         u64 fsid;
    5631                 :            :         s64 bfree;
    5632                 :          3 :         resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
    5633                 :            : 
    5634                 :          3 :         if (!test_opt(sb, MINIX_DF))
    5635                 :          3 :                 overhead = sbi->s_overhead;
    5636                 :            : 
    5637                 :          3 :         buf->f_type = EXT4_SUPER_MAGIC;
    5638                 :          3 :         buf->f_bsize = sb->s_blocksize;
    5639                 :          3 :         buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
    5640                 :          3 :         bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
    5641                 :          3 :                 percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
    5642                 :            :         /* prevent underflow in case that few free space is available */
    5643                 :          3 :         buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
    5644                 :          3 :         buf->f_bavail = buf->f_bfree -
    5645                 :          3 :                         (ext4_r_blocks_count(es) + resv_blocks);
    5646                 :          3 :         if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
    5647                 :          0 :                 buf->f_bavail = 0;
    5648                 :          3 :         buf->f_files = le32_to_cpu(es->s_inodes_count);
    5649                 :          3 :         buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
    5650                 :          3 :         buf->f_namelen = EXT4_NAME_LEN;
    5651                 :          3 :         fsid = le64_to_cpup((void *)es->s_uuid) ^
    5652                 :            :                le64_to_cpup((void *)es->s_uuid + sizeof(u64));
    5653                 :          3 :         buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
    5654                 :          3 :         buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
    5655                 :            : 
    5656                 :            : #ifdef CONFIG_QUOTA
    5657                 :          3 :         if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
    5658                 :            :             sb_has_quota_limits_enabled(sb, PRJQUOTA))
    5659                 :          0 :                 ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
    5660                 :            : #endif
    5661                 :          3 :         return 0;
    5662                 :            : }
    5663                 :            : 
    5664                 :            : 
    5665                 :            : #ifdef CONFIG_QUOTA
    5666                 :            : 
    5667                 :            : /*
    5668                 :            :  * Helper functions so that transaction is started before we acquire dqio_sem
    5669                 :            :  * to keep correct lock ordering of transaction > dqio_sem
    5670                 :            :  */
    5671                 :            : static inline struct inode *dquot_to_inode(struct dquot *dquot)
    5672                 :            : {
    5673                 :          0 :         return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
    5674                 :            : }
    5675                 :            : 
    5676                 :          0 : static int ext4_write_dquot(struct dquot *dquot)
    5677                 :            : {
    5678                 :            :         int ret, err;
    5679                 :            :         handle_t *handle;
    5680                 :            :         struct inode *inode;
    5681                 :            : 
    5682                 :            :         inode = dquot_to_inode(dquot);
    5683                 :          0 :         handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
    5684                 :            :                                     EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
    5685                 :          0 :         if (IS_ERR(handle))
    5686                 :          0 :                 return PTR_ERR(handle);
    5687                 :          0 :         ret = dquot_commit(dquot);
    5688                 :          0 :         err = ext4_journal_stop(handle);
    5689                 :          0 :         if (!ret)
    5690                 :            :                 ret = err;
    5691                 :          0 :         return ret;
    5692                 :            : }
    5693                 :            : 
    5694                 :          0 : static int ext4_acquire_dquot(struct dquot *dquot)
    5695                 :            : {
    5696                 :            :         int ret, err;
    5697                 :            :         handle_t *handle;
    5698                 :            : 
    5699                 :          0 :         handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
    5700                 :            :                                     EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
    5701                 :          0 :         if (IS_ERR(handle))
    5702                 :          0 :                 return PTR_ERR(handle);
    5703                 :          0 :         ret = dquot_acquire(dquot);
    5704                 :          0 :         err = ext4_journal_stop(handle);
    5705                 :          0 :         if (!ret)
    5706                 :            :                 ret = err;
    5707                 :          0 :         return ret;
    5708                 :            : }
    5709                 :            : 
    5710                 :          0 : static int ext4_release_dquot(struct dquot *dquot)
    5711                 :            : {
    5712                 :            :         int ret, err;
    5713                 :            :         handle_t *handle;
    5714                 :            : 
    5715                 :          0 :         handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
    5716                 :            :                                     EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
    5717                 :          0 :         if (IS_ERR(handle)) {
    5718                 :            :                 /* Release dquot anyway to avoid endless cycle in dqput() */
    5719                 :          0 :                 dquot_release(dquot);
    5720                 :          0 :                 return PTR_ERR(handle);
    5721                 :            :         }
    5722                 :          0 :         ret = dquot_release(dquot);
    5723                 :          0 :         err = ext4_journal_stop(handle);
    5724                 :          0 :         if (!ret)
    5725                 :            :                 ret = err;
    5726                 :          0 :         return ret;
    5727                 :            : }
    5728                 :            : 
    5729                 :          0 : static int ext4_mark_dquot_dirty(struct dquot *dquot)
    5730                 :            : {
    5731                 :          0 :         struct super_block *sb = dquot->dq_sb;
    5732                 :            :         struct ext4_sb_info *sbi = EXT4_SB(sb);
    5733                 :            : 
    5734                 :            :         /* Are we journaling quotas? */
    5735                 :          0 :         if (ext4_has_feature_quota(sb) ||
    5736                 :          0 :             sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
    5737                 :          0 :                 dquot_mark_dquot_dirty(dquot);
    5738                 :          0 :                 return ext4_write_dquot(dquot);
    5739                 :            :         } else {
    5740                 :          0 :                 return dquot_mark_dquot_dirty(dquot);
    5741                 :            :         }
    5742                 :            : }
    5743                 :            : 
    5744                 :          0 : static int ext4_write_info(struct super_block *sb, int type)
    5745                 :            : {
    5746                 :            :         int ret, err;
    5747                 :            :         handle_t *handle;
    5748                 :            : 
    5749                 :            :         /* Data block + inode block */
    5750                 :          0 :         handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
    5751                 :          0 :         if (IS_ERR(handle))
    5752                 :          0 :                 return PTR_ERR(handle);
    5753                 :          0 :         ret = dquot_commit_info(sb, type);
    5754                 :          0 :         err = ext4_journal_stop(handle);
    5755                 :          0 :         if (!ret)
    5756                 :            :                 ret = err;
    5757                 :          0 :         return ret;
    5758                 :            : }
    5759                 :            : 
    5760                 :            : /*
    5761                 :            :  * Turn on quotas during mount time - we need to find
    5762                 :            :  * the quota file and such...
    5763                 :            :  */
    5764                 :            : static int ext4_quota_on_mount(struct super_block *sb, int type)
    5765                 :            : {
    5766                 :          0 :         return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
    5767                 :            :                                         EXT4_SB(sb)->s_jquota_fmt, type);
    5768                 :            : }
    5769                 :            : 
    5770                 :            : static void lockdep_set_quota_inode(struct inode *inode, int subclass)
    5771                 :            : {
    5772                 :            :         struct ext4_inode_info *ei = EXT4_I(inode);
    5773                 :            : 
    5774                 :            :         /* The first argument of lockdep_set_subclass has to be
    5775                 :            :          * *exactly* the same as the argument to init_rwsem() --- in
    5776                 :            :          * this case, in init_once() --- or lockdep gets unhappy
    5777                 :            :          * because the name of the lock is set using the
    5778                 :            :          * stringification of the argument to init_rwsem().
    5779                 :            :          */
    5780                 :            :         (void) ei;      /* shut up clang warning if !CONFIG_LOCKDEP */
    5781                 :            :         lockdep_set_subclass(&ei->i_data_sem, subclass);
    5782                 :            : }
    5783                 :            : 
    5784                 :            : /*
    5785                 :            :  * Standard function to be called on quota_on
    5786                 :            :  */
    5787                 :          0 : static int ext4_quota_on(struct super_block *sb, int type, int format_id,
    5788                 :            :                          const struct path *path)
    5789                 :            : {
    5790                 :            :         int err;
    5791                 :            : 
    5792                 :          0 :         if (!test_opt(sb, QUOTA))
    5793                 :            :                 return -EINVAL;
    5794                 :            : 
    5795                 :            :         /* Quotafile not on the same filesystem? */
    5796                 :          0 :         if (path->dentry->d_sb != sb)
    5797                 :            :                 return -EXDEV;
    5798                 :            :         /* Journaling quota? */
    5799                 :          0 :         if (EXT4_SB(sb)->s_qf_names[type]) {
    5800                 :            :                 /* Quotafile not in fs root? */
    5801                 :          0 :                 if (path->dentry->d_parent != sb->s_root)
    5802                 :          0 :                         ext4_msg(sb, KERN_WARNING,
    5803                 :            :                                 "Quota file not on filesystem root. "
    5804                 :            :                                 "Journaled quota will not work");
    5805                 :          0 :                 sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
    5806                 :            :         } else {
    5807                 :            :                 /*
    5808                 :            :                  * Clear the flag just in case mount options changed since
    5809                 :            :                  * last time.
    5810                 :            :                  */
    5811                 :          0 :                 sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
    5812                 :            :         }
    5813                 :            : 
    5814                 :            :         /*
    5815                 :            :          * When we journal data on quota file, we have to flush journal to see
    5816                 :            :          * all updates to the file when we bypass pagecache...
    5817                 :            :          */
    5818                 :          0 :         if (EXT4_SB(sb)->s_journal &&
    5819                 :          0 :             ext4_should_journal_data(d_inode(path->dentry))) {
    5820                 :            :                 /*
    5821                 :            :                  * We don't need to lock updates but journal_flush() could
    5822                 :            :                  * otherwise be livelocked...
    5823                 :            :                  */
    5824                 :          0 :                 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
    5825                 :          0 :                 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
    5826                 :          0 :                 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
    5827                 :          0 :                 if (err)
    5828                 :            :                         return err;
    5829                 :            :         }
    5830                 :            : 
    5831                 :            :         lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
    5832                 :          0 :         err = dquot_quota_on(sb, type, format_id, path);
    5833                 :          0 :         if (err) {
    5834                 :            :                 lockdep_set_quota_inode(path->dentry->d_inode,
    5835                 :            :                                              I_DATA_SEM_NORMAL);
    5836                 :            :         } else {
    5837                 :          0 :                 struct inode *inode = d_inode(path->dentry);
    5838                 :            :                 handle_t *handle;
    5839                 :            : 
    5840                 :            :                 /*
    5841                 :            :                  * Set inode flags to prevent userspace from messing with quota
    5842                 :            :                  * files. If this fails, we return success anyway since quotas
    5843                 :            :                  * are already enabled and this is not a hard failure.
    5844                 :            :                  */
    5845                 :            :                 inode_lock(inode);
    5846                 :            :                 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
    5847                 :          0 :                 if (IS_ERR(handle))
    5848                 :            :                         goto unlock_inode;
    5849                 :          0 :                 EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
    5850                 :          0 :                 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
    5851                 :            :                                 S_NOATIME | S_IMMUTABLE);
    5852                 :          0 :                 ext4_mark_inode_dirty(handle, inode);
    5853                 :          0 :                 ext4_journal_stop(handle);
    5854                 :            :         unlock_inode:
    5855                 :            :                 inode_unlock(inode);
    5856                 :            :         }
    5857                 :          0 :         return err;
    5858                 :            : }
    5859                 :            : 
    5860                 :          0 : static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
    5861                 :            :                              unsigned int flags)
    5862                 :            : {
    5863                 :            :         int err;
    5864                 :            :         struct inode *qf_inode;
    5865                 :          0 :         unsigned long qf_inums[EXT4_MAXQUOTAS] = {
    5866                 :          0 :                 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
    5867                 :          0 :                 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
    5868                 :          0 :                 le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
    5869                 :            :         };
    5870                 :            : 
    5871                 :          0 :         BUG_ON(!ext4_has_feature_quota(sb));
    5872                 :            : 
    5873                 :          0 :         if (!qf_inums[type])
    5874                 :            :                 return -EPERM;
    5875                 :            : 
    5876                 :          0 :         qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
    5877                 :          0 :         if (IS_ERR(qf_inode)) {
    5878                 :          0 :                 ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
    5879                 :          0 :                 return PTR_ERR(qf_inode);
    5880                 :            :         }
    5881                 :            : 
    5882                 :            :         /* Don't account quota for quota files to avoid recursion */
    5883                 :          0 :         qf_inode->i_flags |= S_NOQUOTA;
    5884                 :            :         lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
    5885                 :          0 :         err = dquot_enable(qf_inode, type, format_id, flags);
    5886                 :            :         if (err)
    5887                 :            :                 lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
    5888                 :          0 :         iput(qf_inode);
    5889                 :            : 
    5890                 :          0 :         return err;
    5891                 :            : }
    5892                 :            : 
    5893                 :            : /* Enable usage tracking for all quota types. */
    5894                 :          0 : static int ext4_enable_quotas(struct super_block *sb)
    5895                 :            : {
    5896                 :            :         int type, err = 0;
    5897                 :          0 :         unsigned long qf_inums[EXT4_MAXQUOTAS] = {
    5898                 :          0 :                 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
    5899                 :          0 :                 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
    5900                 :          0 :                 le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
    5901                 :            :         };
    5902                 :          0 :         bool quota_mopt[EXT4_MAXQUOTAS] = {
    5903                 :          0 :                 test_opt(sb, USRQUOTA),
    5904                 :          0 :                 test_opt(sb, GRPQUOTA),
    5905                 :          0 :                 test_opt(sb, PRJQUOTA),
    5906                 :            :         };
    5907                 :            : 
    5908                 :          0 :         sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
    5909                 :          0 :         for (type = 0; type < EXT4_MAXQUOTAS; type++) {
    5910                 :          0 :                 if (qf_inums[type]) {
    5911                 :          0 :                         err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
    5912                 :            :                                 DQUOT_USAGE_ENABLED |
    5913                 :          0 :                                 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
    5914                 :          0 :                         if (err) {
    5915                 :          0 :                                 ext4_warning(sb,
    5916                 :            :                                         "Failed to enable quota tracking "
    5917                 :            :                                         "(type=%d, err=%d). Please run "
    5918                 :            :                                         "e2fsck to fix.", type, err);
    5919                 :          0 :                                 for (type--; type >= 0; type--)
    5920                 :          0 :                                         dquot_quota_off(sb, type);
    5921                 :            : 
    5922                 :          0 :                                 return err;
    5923                 :            :                         }
    5924                 :            :                 }
    5925                 :            :         }
    5926                 :            :         return 0;
    5927                 :            : }
    5928                 :            : 
    5929                 :          0 : static int ext4_quota_off(struct super_block *sb, int type)
    5930                 :            : {
    5931                 :          0 :         struct inode *inode = sb_dqopt(sb)->files[type];
    5932                 :            :         handle_t *handle;
    5933                 :            :         int err;
    5934                 :            : 
    5935                 :            :         /* Force all delayed allocation blocks to be allocated.
    5936                 :            :          * Caller already holds s_umount sem */
    5937                 :          0 :         if (test_opt(sb, DELALLOC))
    5938                 :          0 :                 sync_filesystem(sb);
    5939                 :            : 
    5940                 :          0 :         if (!inode || !igrab(inode))
    5941                 :            :                 goto out;
    5942                 :            : 
    5943                 :          0 :         err = dquot_quota_off(sb, type);
    5944                 :          0 :         if (err || ext4_has_feature_quota(sb))
    5945                 :            :                 goto out_put;
    5946                 :            : 
    5947                 :            :         inode_lock(inode);
    5948                 :            :         /*
    5949                 :            :          * Update modification times of quota files when userspace can
    5950                 :            :          * start looking at them. If we fail, we return success anyway since
    5951                 :            :          * this is not a hard failure and quotas are already disabled.
    5952                 :            :          */
    5953                 :            :         handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
    5954                 :          0 :         if (IS_ERR(handle))
    5955                 :            :                 goto out_unlock;
    5956                 :          0 :         EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
    5957                 :          0 :         inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
    5958                 :          0 :         inode->i_mtime = inode->i_ctime = current_time(inode);
    5959                 :          0 :         ext4_mark_inode_dirty(handle, inode);
    5960                 :          0 :         ext4_journal_stop(handle);
    5961                 :            : out_unlock:
    5962                 :            :         inode_unlock(inode);
    5963                 :            : out_put:
    5964                 :            :         lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
    5965                 :          0 :         iput(inode);
    5966                 :          0 :         return err;
    5967                 :            : out:
    5968                 :          0 :         return dquot_quota_off(sb, type);
    5969                 :            : }
    5970                 :            : 
    5971                 :            : /* Read data from quotafile - avoid pagecache and such because we cannot afford
    5972                 :            :  * acquiring the locks... As quota files are never truncated and quota code
    5973                 :            :  * itself serializes the operations (and no one else should touch the files)
    5974                 :            :  * we don't have to be afraid of races */
    5975                 :          0 : static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
    5976                 :            :                                size_t len, loff_t off)
    5977                 :            : {
    5978                 :          0 :         struct inode *inode = sb_dqopt(sb)->files[type];
    5979                 :          0 :         ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
    5980                 :          0 :         int offset = off & (sb->s_blocksize - 1);
    5981                 :            :         int tocopy;
    5982                 :            :         size_t toread;
    5983                 :            :         struct buffer_head *bh;
    5984                 :            :         loff_t i_size = i_size_read(inode);
    5985                 :            : 
    5986                 :          0 :         if (off > i_size)
    5987                 :            :                 return 0;
    5988                 :          0 :         if (off+len > i_size)
    5989                 :          0 :                 len = i_size-off;
    5990                 :            :         toread = len;
    5991                 :          0 :         while (toread > 0) {
    5992                 :            :                 tocopy = sb->s_blocksize - offset < toread ?
    5993                 :          0 :                                 sb->s_blocksize - offset : toread;
    5994                 :          0 :                 bh = ext4_bread(NULL, inode, blk, 0);
    5995                 :          0 :                 if (IS_ERR(bh))
    5996                 :          0 :                         return PTR_ERR(bh);
    5997                 :          0 :                 if (!bh)        /* A hole? */
    5998                 :          0 :                         memset(data, 0, tocopy);
    5999                 :            :                 else
    6000                 :          0 :                         memcpy(data, bh->b_data+offset, tocopy);
    6001                 :            :                 brelse(bh);
    6002                 :            :                 offset = 0;
    6003                 :          0 :                 toread -= tocopy;
    6004                 :          0 :                 data += tocopy;
    6005                 :          0 :                 blk++;
    6006                 :            :         }
    6007                 :          0 :         return len;
    6008                 :            : }
    6009                 :            : 
    6010                 :            : /* Write to quotafile (we know the transaction is already started and has
    6011                 :            :  * enough credits) */
    6012                 :          0 : static ssize_t ext4_quota_write(struct super_block *sb, int type,
    6013                 :            :                                 const char *data, size_t len, loff_t off)
    6014                 :            : {
    6015                 :          0 :         struct inode *inode = sb_dqopt(sb)->files[type];
    6016                 :          0 :         ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
    6017                 :          0 :         int err, offset = off & (sb->s_blocksize - 1);
    6018                 :          0 :         int retries = 0;
    6019                 :            :         struct buffer_head *bh;
    6020                 :            :         handle_t *handle = journal_current_handle();
    6021                 :            : 
    6022                 :          0 :         if (EXT4_SB(sb)->s_journal && !handle) {
    6023                 :          0 :                 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
    6024                 :            :                         " cancelled because transaction is not started",
    6025                 :            :                         (unsigned long long)off, (unsigned long long)len);
    6026                 :          0 :                 return -EIO;
    6027                 :            :         }
    6028                 :            :         /*
    6029                 :            :          * Since we account only one data block in transaction credits,
    6030                 :            :          * then it is impossible to cross a block boundary.
    6031                 :            :          */
    6032                 :          0 :         if (sb->s_blocksize - offset < len) {
    6033                 :          0 :                 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
    6034                 :            :                         " cancelled because not block aligned",
    6035                 :            :                         (unsigned long long)off, (unsigned long long)len);
    6036                 :          0 :                 return -EIO;
    6037                 :            :         }
    6038                 :            : 
    6039                 :            :         do {
    6040                 :          0 :                 bh = ext4_bread(handle, inode, blk,
    6041                 :            :                                 EXT4_GET_BLOCKS_CREATE |
    6042                 :            :                                 EXT4_GET_BLOCKS_METADATA_NOFAIL);
    6043                 :          0 :         } while (IS_ERR(bh) && (PTR_ERR(bh) == -ENOSPC) &&
    6044                 :          0 :                  ext4_should_retry_alloc(inode->i_sb, &retries));
    6045                 :          0 :         if (IS_ERR(bh))
    6046                 :          0 :                 return PTR_ERR(bh);
    6047                 :          0 :         if (!bh)
    6048                 :            :                 goto out;
    6049                 :            :         BUFFER_TRACE(bh, "get write access");
    6050                 :          0 :         err = ext4_journal_get_write_access(handle, bh);
    6051                 :          0 :         if (err) {
    6052                 :            :                 brelse(bh);
    6053                 :          0 :                 return err;
    6054                 :            :         }
    6055                 :          0 :         lock_buffer(bh);
    6056                 :          0 :         memcpy(bh->b_data+offset, data, len);
    6057                 :          0 :         flush_dcache_page(bh->b_page);
    6058                 :          0 :         unlock_buffer(bh);
    6059                 :          0 :         err = ext4_handle_dirty_metadata(handle, NULL, bh);
    6060                 :            :         brelse(bh);
    6061                 :            : out:
    6062                 :          0 :         if (inode->i_size < off + len) {
    6063                 :            :                 i_size_write(inode, off + len);
    6064                 :          0 :                 EXT4_I(inode)->i_disksize = inode->i_size;
    6065                 :          0 :                 ext4_mark_inode_dirty(handle, inode);
    6066                 :            :         }
    6067                 :          0 :         return len;
    6068                 :            : }
    6069                 :            : 
    6070                 :          0 : static int ext4_get_next_id(struct super_block *sb, struct kqid *qid)
    6071                 :            : {
    6072                 :            :         const struct quota_format_ops   *ops;
    6073                 :            : 
    6074                 :          0 :         if (!sb_has_quota_loaded(sb, qid->type))
    6075                 :            :                 return -ESRCH;
    6076                 :          0 :         ops = sb_dqopt(sb)->ops[qid->type];
    6077                 :          0 :         if (!ops || !ops->get_next_id)
    6078                 :            :                 return -ENOSYS;
    6079                 :          0 :         return dquot_get_next_id(sb, qid);
    6080                 :            : }
    6081                 :            : #endif
    6082                 :            : 
    6083                 :          3 : static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
    6084                 :            :                        const char *dev_name, void *data)
    6085                 :            : {
    6086                 :          3 :         return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
    6087                 :            : }
    6088                 :            : 
    6089                 :            : #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
    6090                 :          3 : static inline void register_as_ext2(void)
    6091                 :            : {
    6092                 :          3 :         int err = register_filesystem(&ext2_fs_type);
    6093                 :          3 :         if (err)
    6094                 :          0 :                 printk(KERN_WARNING
    6095                 :            :                        "EXT4-fs: Unable to register as ext2 (%d)\n", err);
    6096                 :          3 : }
    6097                 :            : 
    6098                 :            : static inline void unregister_as_ext2(void)
    6099                 :            : {
    6100                 :          0 :         unregister_filesystem(&ext2_fs_type);
    6101                 :            : }
    6102                 :            : 
    6103                 :            : static inline int ext2_feature_set_ok(struct super_block *sb)
    6104                 :            : {
    6105                 :          1 :         if (ext4_has_unknown_ext2_incompat_features(sb))
    6106                 :            :                 return 0;
    6107                 :          0 :         if (sb_rdonly(sb))
    6108                 :            :                 return 1;
    6109                 :          0 :         if (ext4_has_unknown_ext2_ro_compat_features(sb))
    6110                 :            :                 return 0;
    6111                 :            :         return 1;
    6112                 :            : }
    6113                 :            : #else
    6114                 :            : static inline void register_as_ext2(void) { }
    6115                 :            : static inline void unregister_as_ext2(void) { }
    6116                 :            : static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
    6117                 :            : #endif
    6118                 :            : 
    6119                 :          3 : static inline void register_as_ext3(void)
    6120                 :            : {
    6121                 :          3 :         int err = register_filesystem(&ext3_fs_type);
    6122                 :          3 :         if (err)
    6123                 :          0 :                 printk(KERN_WARNING
    6124                 :            :                        "EXT4-fs: Unable to register as ext3 (%d)\n", err);
    6125                 :          3 : }
    6126                 :            : 
    6127                 :            : static inline void unregister_as_ext3(void)
    6128                 :            : {
    6129                 :          0 :         unregister_filesystem(&ext3_fs_type);
    6130                 :            : }
    6131                 :            : 
    6132                 :          1 : static inline int ext3_feature_set_ok(struct super_block *sb)
    6133                 :            : {
    6134                 :          1 :         if (ext4_has_unknown_ext3_incompat_features(sb))
    6135                 :            :                 return 0;
    6136                 :          0 :         if (!ext4_has_feature_journal(sb))
    6137                 :            :                 return 0;
    6138                 :          0 :         if (sb_rdonly(sb))
    6139                 :            :                 return 1;
    6140                 :          0 :         if (ext4_has_unknown_ext3_ro_compat_features(sb))
    6141                 :            :                 return 0;
    6142                 :          0 :         return 1;
    6143                 :            : }
    6144                 :            : 
    6145                 :            : static struct file_system_type ext4_fs_type = {
    6146                 :            :         .owner          = THIS_MODULE,
    6147                 :            :         .name           = "ext4",
    6148                 :            :         .mount          = ext4_mount,
    6149                 :            :         .kill_sb        = kill_block_super,
    6150                 :            :         .fs_flags       = FS_REQUIRES_DEV,
    6151                 :            : };
    6152                 :            : MODULE_ALIAS_FS("ext4");
    6153                 :            : 
    6154                 :            : /* Shared across all ext4 file systems */
    6155                 :            : wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
    6156                 :            : 
    6157                 :          3 : static int __init ext4_init_fs(void)
    6158                 :            : {
    6159                 :            :         int i, err;
    6160                 :            : 
    6161                 :            :         ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
    6162                 :          3 :         ext4_li_info = NULL;
    6163                 :          3 :         mutex_init(&ext4_li_mtx);
    6164                 :            : 
    6165                 :            :         /* Build-time check for flags consistency */
    6166                 :            :         ext4_check_flag_values();
    6167                 :            : 
    6168                 :          3 :         for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
    6169                 :          3 :                 init_waitqueue_head(&ext4__ioend_wq[i]);
    6170                 :            : 
    6171                 :          3 :         err = ext4_init_es();
    6172                 :          3 :         if (err)
    6173                 :            :                 return err;
    6174                 :            : 
    6175                 :          3 :         err = ext4_init_pending();
    6176                 :          3 :         if (err)
    6177                 :            :                 goto out7;
    6178                 :            : 
    6179                 :          3 :         err = ext4_init_post_read_processing();
    6180                 :          3 :         if (err)
    6181                 :            :                 goto out6;
    6182                 :            : 
    6183                 :          3 :         err = ext4_init_pageio();
    6184                 :          3 :         if (err)
    6185                 :            :                 goto out5;
    6186                 :            : 
    6187                 :          3 :         err = ext4_init_system_zone();
    6188                 :          3 :         if (err)
    6189                 :            :                 goto out4;
    6190                 :            : 
    6191                 :          3 :         err = ext4_init_sysfs();
    6192                 :          3 :         if (err)
    6193                 :            :                 goto out3;
    6194                 :            : 
    6195                 :          3 :         err = ext4_init_mballoc();
    6196                 :          3 :         if (err)
    6197                 :            :                 goto out2;
    6198                 :          3 :         err = init_inodecache();
    6199                 :          3 :         if (err)
    6200                 :            :                 goto out1;
    6201                 :          3 :         register_as_ext3();
    6202                 :          3 :         register_as_ext2();
    6203                 :          3 :         err = register_filesystem(&ext4_fs_type);
    6204                 :          3 :         if (err)
    6205                 :            :                 goto out;
    6206                 :            : 
    6207                 :            :         return 0;
    6208                 :            : out:
    6209                 :            :         unregister_as_ext2();
    6210                 :            :         unregister_as_ext3();
    6211                 :            :         destroy_inodecache();
    6212                 :            : out1:
    6213                 :          0 :         ext4_exit_mballoc();
    6214                 :            : out2:
    6215                 :          0 :         ext4_exit_sysfs();
    6216                 :            : out3:
    6217                 :          0 :         ext4_exit_system_zone();
    6218                 :            : out4:
    6219                 :          0 :         ext4_exit_pageio();
    6220                 :            : out5:
    6221                 :          0 :         ext4_exit_post_read_processing();
    6222                 :            : out6:
    6223                 :          0 :         ext4_exit_pending();
    6224                 :            : out7:
    6225                 :          0 :         ext4_exit_es();
    6226                 :            : 
    6227                 :          0 :         return err;
    6228                 :            : }
    6229                 :            : 
    6230                 :          0 : static void __exit ext4_exit_fs(void)
    6231                 :            : {
    6232                 :          0 :         ext4_destroy_lazyinit_thread();
    6233                 :            :         unregister_as_ext2();
    6234                 :            :         unregister_as_ext3();
    6235                 :          0 :         unregister_filesystem(&ext4_fs_type);
    6236                 :            :         destroy_inodecache();
    6237                 :          0 :         ext4_exit_mballoc();
    6238                 :          0 :         ext4_exit_sysfs();
    6239                 :          0 :         ext4_exit_system_zone();
    6240                 :          0 :         ext4_exit_pageio();
    6241                 :          0 :         ext4_exit_post_read_processing();
    6242                 :          0 :         ext4_exit_es();
    6243                 :          0 :         ext4_exit_pending();
    6244                 :          0 : }
    6245                 :            : 
    6246                 :            : MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
    6247                 :            : MODULE_DESCRIPTION("Fourth Extended Filesystem");
    6248                 :            : MODULE_LICENSE("GPL");
    6249                 :            : MODULE_SOFTDEP("pre: crc32c");
    6250                 :            : module_init(ext4_init_fs)
    6251                 :            : module_exit(ext4_exit_fs)
    

Generated by: LCOV version 1.14