root/fs/erofs/super.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. _erofs_err
  2. _erofs_info
  3. erofs_inode_init_once
  4. erofs_alloc_inode
  5. erofs_free_inode
  6. check_layout_compatibility
  7. erofs_read_superblock
  8. erofs_build_cache_strategy
  9. erofs_build_cache_strategy
  10. erofs_default_options
  11. erofs_parse_options
  12. erofs_managed_cache_releasepage
  13. erofs_managed_cache_invalidatepage
  14. erofs_init_managed_cache
  15. erofs_init_managed_cache
  16. erofs_fill_super
  17. erofs_mount
  18. erofs_kill_sb
  19. erofs_put_super
  20. erofs_module_init
  21. erofs_module_exit
  22. erofs_statfs
  23. erofs_show_options
  24. erofs_remount

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * Copyright (C) 2017-2018 HUAWEI, Inc.
   4  *             http://www.huawei.com/
   5  * Created by Gao Xiang <gaoxiang25@huawei.com>
   6  */
   7 #include <linux/module.h>
   8 #include <linux/buffer_head.h>
   9 #include <linux/statfs.h>
  10 #include <linux/parser.h>
  11 #include <linux/seq_file.h>
  12 #include "xattr.h"
  13 
  14 #define CREATE_TRACE_POINTS
  15 #include <trace/events/erofs.h>
  16 
  17 static struct kmem_cache *erofs_inode_cachep __read_mostly;
  18 
  19 void _erofs_err(struct super_block *sb, const char *function,
  20                 const char *fmt, ...)
  21 {
  22         struct va_format vaf;
  23         va_list args;
  24 
  25         va_start(args, fmt);
  26 
  27         vaf.fmt = fmt;
  28         vaf.va = &args;
  29 
  30         pr_err("(device %s): %s: %pV", sb->s_id, function, &vaf);
  31         va_end(args);
  32 }
  33 
  34 void _erofs_info(struct super_block *sb, const char *function,
  35                  const char *fmt, ...)
  36 {
  37         struct va_format vaf;
  38         va_list args;
  39 
  40         va_start(args, fmt);
  41 
  42         vaf.fmt = fmt;
  43         vaf.va = &args;
  44 
  45         pr_info("(device %s): %pV", sb->s_id, &vaf);
  46         va_end(args);
  47 }
  48 
  49 static void erofs_inode_init_once(void *ptr)
  50 {
  51         struct erofs_inode *vi = ptr;
  52 
  53         inode_init_once(&vi->vfs_inode);
  54 }
  55 
  56 static struct inode *erofs_alloc_inode(struct super_block *sb)
  57 {
  58         struct erofs_inode *vi =
  59                 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
  60 
  61         if (!vi)
  62                 return NULL;
  63 
  64         /* zero out everything except vfs_inode */
  65         memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
  66         return &vi->vfs_inode;
  67 }
  68 
  69 static void erofs_free_inode(struct inode *inode)
  70 {
  71         struct erofs_inode *vi = EROFS_I(inode);
  72 
  73         /* be careful of RCU symlink path */
  74         if (inode->i_op == &erofs_fast_symlink_iops)
  75                 kfree(inode->i_link);
  76         kfree(vi->xattr_shared_xattrs);
  77 
  78         kmem_cache_free(erofs_inode_cachep, vi);
  79 }
  80 
  81 static bool check_layout_compatibility(struct super_block *sb,
  82                                        struct erofs_super_block *dsb)
  83 {
  84         const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
  85 
  86         EROFS_SB(sb)->feature_incompat = feature;
  87 
  88         /* check if current kernel meets all mandatory requirements */
  89         if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
  90                 erofs_err(sb,
  91                           "unidentified incompatible feature %x, please upgrade kernel version",
  92                            feature & ~EROFS_ALL_FEATURE_INCOMPAT);
  93                 return false;
  94         }
  95         return true;
  96 }
  97 
  98 static int erofs_read_superblock(struct super_block *sb)
  99 {
 100         struct erofs_sb_info *sbi;
 101         struct page *page;
 102         struct erofs_super_block *dsb;
 103         unsigned int blkszbits;
 104         void *data;
 105         int ret;
 106 
 107         page = read_mapping_page(sb->s_bdev->bd_inode->i_mapping, 0, NULL);
 108         if (IS_ERR(page)) {
 109                 erofs_err(sb, "cannot read erofs superblock");
 110                 return PTR_ERR(page);
 111         }
 112 
 113         sbi = EROFS_SB(sb);
 114 
 115         data = kmap_atomic(page);
 116         dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
 117 
 118         ret = -EINVAL;
 119         if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
 120                 erofs_err(sb, "cannot find valid erofs superblock");
 121                 goto out;
 122         }
 123 
 124         blkszbits = dsb->blkszbits;
 125         /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
 126         if (blkszbits != LOG_BLOCK_SIZE) {
 127                 erofs_err(sb, "blksize %u isn't supported on this platform",
 128                           1 << blkszbits);
 129                 goto out;
 130         }
 131 
 132         if (!check_layout_compatibility(sb, dsb))
 133                 goto out;
 134 
 135         sbi->blocks = le32_to_cpu(dsb->blocks);
 136         sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
 137 #ifdef CONFIG_EROFS_FS_XATTR
 138         sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
 139 #endif
 140         sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
 141         sbi->root_nid = le16_to_cpu(dsb->root_nid);
 142         sbi->inos = le64_to_cpu(dsb->inos);
 143 
 144         sbi->build_time = le64_to_cpu(dsb->build_time);
 145         sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
 146 
 147         memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
 148 
 149         ret = strscpy(sbi->volume_name, dsb->volume_name,
 150                       sizeof(dsb->volume_name));
 151         if (ret < 0) {  /* -E2BIG */
 152                 erofs_err(sb, "bad volume name without NIL terminator");
 153                 ret = -EFSCORRUPTED;
 154                 goto out;
 155         }
 156         ret = 0;
 157 out:
 158         kunmap_atomic(data);
 159         put_page(page);
 160         return ret;
 161 }
 162 
 163 #ifdef CONFIG_EROFS_FS_ZIP
 164 static int erofs_build_cache_strategy(struct super_block *sb,
 165                                       substring_t *args)
 166 {
 167         struct erofs_sb_info *sbi = EROFS_SB(sb);
 168         const char *cs = match_strdup(args);
 169         int err = 0;
 170 
 171         if (!cs) {
 172                 erofs_err(sb, "Not enough memory to store cache strategy");
 173                 return -ENOMEM;
 174         }
 175 
 176         if (!strcmp(cs, "disabled")) {
 177                 sbi->cache_strategy = EROFS_ZIP_CACHE_DISABLED;
 178         } else if (!strcmp(cs, "readahead")) {
 179                 sbi->cache_strategy = EROFS_ZIP_CACHE_READAHEAD;
 180         } else if (!strcmp(cs, "readaround")) {
 181                 sbi->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
 182         } else {
 183                 erofs_err(sb, "Unrecognized cache strategy \"%s\"", cs);
 184                 err = -EINVAL;
 185         }
 186         kfree(cs);
 187         return err;
 188 }
 189 #else
 190 static int erofs_build_cache_strategy(struct super_block *sb,
 191                                       substring_t *args)
 192 {
 193         erofs_info(sb, "EROFS compression is disabled, so cache strategy is ignored");
 194         return 0;
 195 }
 196 #endif
 197 
 198 /* set up default EROFS parameters */
 199 static void erofs_default_options(struct erofs_sb_info *sbi)
 200 {
 201 #ifdef CONFIG_EROFS_FS_ZIP
 202         sbi->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
 203         sbi->max_sync_decompress_pages = 3;
 204 #endif
 205 #ifdef CONFIG_EROFS_FS_XATTR
 206         set_opt(sbi, XATTR_USER);
 207 #endif
 208 #ifdef CONFIG_EROFS_FS_POSIX_ACL
 209         set_opt(sbi, POSIX_ACL);
 210 #endif
 211 }
 212 
 213 enum {
 214         Opt_user_xattr,
 215         Opt_nouser_xattr,
 216         Opt_acl,
 217         Opt_noacl,
 218         Opt_cache_strategy,
 219         Opt_err
 220 };
 221 
 222 static match_table_t erofs_tokens = {
 223         {Opt_user_xattr, "user_xattr"},
 224         {Opt_nouser_xattr, "nouser_xattr"},
 225         {Opt_acl, "acl"},
 226         {Opt_noacl, "noacl"},
 227         {Opt_cache_strategy, "cache_strategy=%s"},
 228         {Opt_err, NULL}
 229 };
 230 
 231 static int erofs_parse_options(struct super_block *sb, char *options)
 232 {
 233         substring_t args[MAX_OPT_ARGS];
 234         char *p;
 235         int err;
 236 
 237         if (!options)
 238                 return 0;
 239 
 240         while ((p = strsep(&options, ","))) {
 241                 int token;
 242 
 243                 if (!*p)
 244                         continue;
 245 
 246                 args[0].to = args[0].from = NULL;
 247                 token = match_token(p, erofs_tokens, args);
 248 
 249                 switch (token) {
 250 #ifdef CONFIG_EROFS_FS_XATTR
 251                 case Opt_user_xattr:
 252                         set_opt(EROFS_SB(sb), XATTR_USER);
 253                         break;
 254                 case Opt_nouser_xattr:
 255                         clear_opt(EROFS_SB(sb), XATTR_USER);
 256                         break;
 257 #else
 258                 case Opt_user_xattr:
 259                         erofs_info(sb, "user_xattr options not supported");
 260                         break;
 261                 case Opt_nouser_xattr:
 262                         erofs_info(sb, "nouser_xattr options not supported");
 263                         break;
 264 #endif
 265 #ifdef CONFIG_EROFS_FS_POSIX_ACL
 266                 case Opt_acl:
 267                         set_opt(EROFS_SB(sb), POSIX_ACL);
 268                         break;
 269                 case Opt_noacl:
 270                         clear_opt(EROFS_SB(sb), POSIX_ACL);
 271                         break;
 272 #else
 273                 case Opt_acl:
 274                         erofs_info(sb, "acl options not supported");
 275                         break;
 276                 case Opt_noacl:
 277                         erofs_info(sb, "noacl options not supported");
 278                         break;
 279 #endif
 280                 case Opt_cache_strategy:
 281                         err = erofs_build_cache_strategy(sb, args);
 282                         if (err)
 283                                 return err;
 284                         break;
 285                 default:
 286                         erofs_err(sb, "Unrecognized mount option \"%s\" or missing value", p);
 287                         return -EINVAL;
 288                 }
 289         }
 290         return 0;
 291 }
 292 
 293 #ifdef CONFIG_EROFS_FS_ZIP
 294 static const struct address_space_operations managed_cache_aops;
 295 
 296 static int erofs_managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
 297 {
 298         int ret = 1;    /* 0 - busy */
 299         struct address_space *const mapping = page->mapping;
 300 
 301         DBG_BUGON(!PageLocked(page));
 302         DBG_BUGON(mapping->a_ops != &managed_cache_aops);
 303 
 304         if (PagePrivate(page))
 305                 ret = erofs_try_to_free_cached_page(mapping, page);
 306 
 307         return ret;
 308 }
 309 
 310 static void erofs_managed_cache_invalidatepage(struct page *page,
 311                                                unsigned int offset,
 312                                                unsigned int length)
 313 {
 314         const unsigned int stop = length + offset;
 315 
 316         DBG_BUGON(!PageLocked(page));
 317 
 318         /* Check for potential overflow in debug mode */
 319         DBG_BUGON(stop > PAGE_SIZE || stop < length);
 320 
 321         if (offset == 0 && stop == PAGE_SIZE)
 322                 while (!erofs_managed_cache_releasepage(page, GFP_NOFS))
 323                         cond_resched();
 324 }
 325 
 326 static const struct address_space_operations managed_cache_aops = {
 327         .releasepage = erofs_managed_cache_releasepage,
 328         .invalidatepage = erofs_managed_cache_invalidatepage,
 329 };
 330 
 331 static int erofs_init_managed_cache(struct super_block *sb)
 332 {
 333         struct erofs_sb_info *const sbi = EROFS_SB(sb);
 334         struct inode *const inode = new_inode(sb);
 335 
 336         if (!inode)
 337                 return -ENOMEM;
 338 
 339         set_nlink(inode, 1);
 340         inode->i_size = OFFSET_MAX;
 341 
 342         inode->i_mapping->a_ops = &managed_cache_aops;
 343         mapping_set_gfp_mask(inode->i_mapping,
 344                              GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
 345         sbi->managed_cache = inode;
 346         return 0;
 347 }
 348 #else
 349 static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
 350 #endif
 351 
 352 static int erofs_fill_super(struct super_block *sb, void *data, int silent)
 353 {
 354         struct inode *inode;
 355         struct erofs_sb_info *sbi;
 356         int err;
 357 
 358         sb->s_magic = EROFS_SUPER_MAGIC;
 359 
 360         if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
 361                 erofs_err(sb, "failed to set erofs blksize");
 362                 return -EINVAL;
 363         }
 364 
 365         sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
 366         if (!sbi)
 367                 return -ENOMEM;
 368 
 369         sb->s_fs_info = sbi;
 370         err = erofs_read_superblock(sb);
 371         if (err)
 372                 return err;
 373 
 374         sb->s_flags |= SB_RDONLY | SB_NOATIME;
 375         sb->s_maxbytes = MAX_LFS_FILESIZE;
 376         sb->s_time_gran = 1;
 377 
 378         sb->s_op = &erofs_sops;
 379 
 380 #ifdef CONFIG_EROFS_FS_XATTR
 381         sb->s_xattr = erofs_xattr_handlers;
 382 #endif
 383         /* set erofs default mount options */
 384         erofs_default_options(sbi);
 385 
 386         err = erofs_parse_options(sb, data);
 387         if (err)
 388                 return err;
 389 
 390         if (test_opt(sbi, POSIX_ACL))
 391                 sb->s_flags |= SB_POSIXACL;
 392         else
 393                 sb->s_flags &= ~SB_POSIXACL;
 394 
 395 #ifdef CONFIG_EROFS_FS_ZIP
 396         INIT_RADIX_TREE(&sbi->workstn_tree, GFP_ATOMIC);
 397 #endif
 398 
 399         /* get the root inode */
 400         inode = erofs_iget(sb, ROOT_NID(sbi), true);
 401         if (IS_ERR(inode))
 402                 return PTR_ERR(inode);
 403 
 404         if (!S_ISDIR(inode->i_mode)) {
 405                 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
 406                           ROOT_NID(sbi), inode->i_mode);
 407                 iput(inode);
 408                 return -EINVAL;
 409         }
 410 
 411         sb->s_root = d_make_root(inode);
 412         if (!sb->s_root)
 413                 return -ENOMEM;
 414 
 415         erofs_shrinker_register(sb);
 416         /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
 417         err = erofs_init_managed_cache(sb);
 418         if (err)
 419                 return err;
 420 
 421         erofs_info(sb, "mounted with opts: %s, root inode @ nid %llu.",
 422                    (char *)data, ROOT_NID(sbi));
 423         return 0;
 424 }
 425 
 426 static struct dentry *erofs_mount(struct file_system_type *fs_type, int flags,
 427                                   const char *dev_name, void *data)
 428 {
 429         return mount_bdev(fs_type, flags, dev_name, data, erofs_fill_super);
 430 }
 431 
 432 /*
 433  * could be triggered after deactivate_locked_super()
 434  * is called, thus including umount and failed to initialize.
 435  */
 436 static void erofs_kill_sb(struct super_block *sb)
 437 {
 438         struct erofs_sb_info *sbi;
 439 
 440         WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
 441 
 442         kill_block_super(sb);
 443 
 444         sbi = EROFS_SB(sb);
 445         if (!sbi)
 446                 return;
 447         kfree(sbi);
 448         sb->s_fs_info = NULL;
 449 }
 450 
 451 /* called when ->s_root is non-NULL */
 452 static void erofs_put_super(struct super_block *sb)
 453 {
 454         struct erofs_sb_info *const sbi = EROFS_SB(sb);
 455 
 456         DBG_BUGON(!sbi);
 457 
 458         erofs_shrinker_unregister(sb);
 459 #ifdef CONFIG_EROFS_FS_ZIP
 460         iput(sbi->managed_cache);
 461         sbi->managed_cache = NULL;
 462 #endif
 463 }
 464 
 465 static struct file_system_type erofs_fs_type = {
 466         .owner          = THIS_MODULE,
 467         .name           = "erofs",
 468         .mount          = erofs_mount,
 469         .kill_sb        = erofs_kill_sb,
 470         .fs_flags       = FS_REQUIRES_DEV,
 471 };
 472 MODULE_ALIAS_FS("erofs");
 473 
 474 static int __init erofs_module_init(void)
 475 {
 476         int err;
 477 
 478         erofs_check_ondisk_layout_definitions();
 479 
 480         erofs_inode_cachep = kmem_cache_create("erofs_inode",
 481                                                sizeof(struct erofs_inode), 0,
 482                                                SLAB_RECLAIM_ACCOUNT,
 483                                                erofs_inode_init_once);
 484         if (!erofs_inode_cachep) {
 485                 err = -ENOMEM;
 486                 goto icache_err;
 487         }
 488 
 489         err = erofs_init_shrinker();
 490         if (err)
 491                 goto shrinker_err;
 492 
 493         err = z_erofs_init_zip_subsystem();
 494         if (err)
 495                 goto zip_err;
 496 
 497         err = register_filesystem(&erofs_fs_type);
 498         if (err)
 499                 goto fs_err;
 500 
 501         return 0;
 502 
 503 fs_err:
 504         z_erofs_exit_zip_subsystem();
 505 zip_err:
 506         erofs_exit_shrinker();
 507 shrinker_err:
 508         kmem_cache_destroy(erofs_inode_cachep);
 509 icache_err:
 510         return err;
 511 }
 512 
 513 static void __exit erofs_module_exit(void)
 514 {
 515         unregister_filesystem(&erofs_fs_type);
 516         z_erofs_exit_zip_subsystem();
 517         erofs_exit_shrinker();
 518 
 519         /* Ensure all RCU free inodes are safe before cache is destroyed. */
 520         rcu_barrier();
 521         kmem_cache_destroy(erofs_inode_cachep);
 522 }
 523 
 524 /* get filesystem statistics */
 525 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
 526 {
 527         struct super_block *sb = dentry->d_sb;
 528         struct erofs_sb_info *sbi = EROFS_SB(sb);
 529         u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
 530 
 531         buf->f_type = sb->s_magic;
 532         buf->f_bsize = EROFS_BLKSIZ;
 533         buf->f_blocks = sbi->blocks;
 534         buf->f_bfree = buf->f_bavail = 0;
 535 
 536         buf->f_files = ULLONG_MAX;
 537         buf->f_ffree = ULLONG_MAX - sbi->inos;
 538 
 539         buf->f_namelen = EROFS_NAME_LEN;
 540 
 541         buf->f_fsid.val[0] = (u32)id;
 542         buf->f_fsid.val[1] = (u32)(id >> 32);
 543         return 0;
 544 }
 545 
 546 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
 547 {
 548         struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
 549 
 550 #ifdef CONFIG_EROFS_FS_XATTR
 551         if (test_opt(sbi, XATTR_USER))
 552                 seq_puts(seq, ",user_xattr");
 553         else
 554                 seq_puts(seq, ",nouser_xattr");
 555 #endif
 556 #ifdef CONFIG_EROFS_FS_POSIX_ACL
 557         if (test_opt(sbi, POSIX_ACL))
 558                 seq_puts(seq, ",acl");
 559         else
 560                 seq_puts(seq, ",noacl");
 561 #endif
 562 #ifdef CONFIG_EROFS_FS_ZIP
 563         if (sbi->cache_strategy == EROFS_ZIP_CACHE_DISABLED) {
 564                 seq_puts(seq, ",cache_strategy=disabled");
 565         } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAHEAD) {
 566                 seq_puts(seq, ",cache_strategy=readahead");
 567         } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAROUND) {
 568                 seq_puts(seq, ",cache_strategy=readaround");
 569         } else {
 570                 seq_puts(seq, ",cache_strategy=(unknown)");
 571                 DBG_BUGON(1);
 572         }
 573 #endif
 574         return 0;
 575 }
 576 
 577 static int erofs_remount(struct super_block *sb, int *flags, char *data)
 578 {
 579         struct erofs_sb_info *sbi = EROFS_SB(sb);
 580         unsigned int org_mnt_opt = sbi->mount_opt;
 581         int err;
 582 
 583         DBG_BUGON(!sb_rdonly(sb));
 584         err = erofs_parse_options(sb, data);
 585         if (err)
 586                 goto out;
 587 
 588         if (test_opt(sbi, POSIX_ACL))
 589                 sb->s_flags |= SB_POSIXACL;
 590         else
 591                 sb->s_flags &= ~SB_POSIXACL;
 592 
 593         *flags |= SB_RDONLY;
 594         return 0;
 595 out:
 596         sbi->mount_opt = org_mnt_opt;
 597         return err;
 598 }
 599 
 600 const struct super_operations erofs_sops = {
 601         .put_super = erofs_put_super,
 602         .alloc_inode = erofs_alloc_inode,
 603         .free_inode = erofs_free_inode,
 604         .statfs = erofs_statfs,
 605         .show_options = erofs_show_options,
 606         .remount_fs = erofs_remount,
 607 };
 608 
 609 module_init(erofs_module_init);
 610 module_exit(erofs_module_exit);
 611 
 612 MODULE_DESCRIPTION("Enhanced ROM File System");
 613 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
 614 MODULE_LICENSE("GPL");
 615 

/* [<][>][^][v][top][bottom][index][help] */