~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ext4/super.c

Version: ~ [ linux-5.14-rc3 ] ~ [ linux-5.13.5 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.53 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.135 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.198 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.240 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.276 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.276 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp