1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2017-2018 HUAWEI, Inc. 4 * https://www.huawei.com/ 5 * Copyright (C) 2021, Alibaba Cloud 6 */ 7 #ifndef __EROFS_INTERNAL_H 8 #define __EROFS_INTERNAL_H 9 10 #include <linux/fs.h> 11 #include <linux/dax.h> 12 #include <linux/dcache.h> 13 #include <linux/mm.h> 14 #include <linux/module.h> 15 #include <linux/pagemap.h> 16 #include <linux/bio.h> 17 #include <linux/magic.h> 18 #include <linux/slab.h> 19 #include <linux/vmalloc.h> 20 #include <linux/iomap.h> 21 #include "erofs_fs.h" 22 23 __printf(2, 3) void _erofs_printk(struct super_block *sb, const char *fmt, ...); 24 #define erofs_err(sb, fmt, ...) \ 25 _erofs_printk(sb, KERN_ERR fmt "\n", ##__VA_ARGS__) 26 #define erofs_info(sb, fmt, ...) \ 27 _erofs_printk(sb, KERN_INFO fmt "\n", ##__VA_ARGS__) 28 29 #ifdef CONFIG_EROFS_FS_DEBUG 30 #define DBG_BUGON BUG_ON 31 #else 32 #define DBG_BUGON(x) ((void)(x)) 33 #endif /* !CONFIG_EROFS_FS_DEBUG */ 34 35 /* EROFS_SUPER_MAGIC_V1 to represent the whole file system */ 36 #define EROFS_SUPER_MAGIC EROFS_SUPER_MAGIC_V1 37 38 typedef u64 erofs_nid_t; 39 typedef u64 erofs_off_t; 40 typedef u64 erofs_blk_t; 41 42 struct erofs_device_info { 43 char *path; 44 struct erofs_fscache *fscache; 45 struct file *file; 46 struct dax_device *dax_dev; 47 u64 fsoff, dax_part_off; 48 49 erofs_blk_t blocks; 50 erofs_blk_t uniaddr; 51 }; 52 53 enum { 54 EROFS_SYNC_DECOMPRESS_AUTO, 55 EROFS_SYNC_DECOMPRESS_FORCE_ON, 56 EROFS_SYNC_DECOMPRESS_FORCE_OFF 57 }; 58 59 struct erofs_mount_opts { 60 /* current strategy of how to use managed cache */ 61 unsigned char cache_strategy; 62 /* strategy of sync decompression (0 - auto, 1 - force on, 2 - force off) */ 63 unsigned int sync_decompress; 64 /* threshold for decompression synchronously */ 65 unsigned int max_sync_decompress_pages; 66 unsigned int mount_opt; 67 }; 68 69 struct erofs_dev_context { 70 struct idr tree; 71 struct rw_semaphore rwsem; 72 73 unsigned int extra_devices; 74 bool flatdev; 75 }; 76 77 /* all filesystem-wide lz4 configurations */ 78 struct erofs_sb_lz4_info { 79 /* # of pages needed for EROFS lz4 rolling decompression */ 80 u16 max_distance_pages; 81 /* maximum possible blocks for pclusters in the filesystem */ 82 u16 max_pclusterblks; 83 }; 84 85 struct erofs_domain { 86 refcount_t ref; 87 struct list_head list; 88 struct fscache_volume *volume; 89 char *domain_id; 90 }; 91 92 struct erofs_fscache { 93 struct fscache_cookie *cookie; 94 struct inode *inode; /* anonymous inode for the blob */ 95 96 /* used for share domain mode */ 97 struct erofs_domain *domain; 98 struct list_head node; 99 refcount_t ref; 100 char *name; 101 }; 102 103 struct erofs_xattr_prefix_item { 104 struct erofs_xattr_long_prefix *prefix; 105 u8 infix_len; 106 }; 107 108 struct erofs_sb_info { 109 struct erofs_device_info dif0; 110 struct erofs_mount_opts opt; /* options */ 111 #ifdef CONFIG_EROFS_FS_ZIP 112 /* list for all registered superblocks, mainly for shrinker */ 113 struct list_head list; 114 struct mutex umount_mutex; 115 116 /* managed XArray arranged in physical block number */ 117 struct xarray managed_pslots; 118 119 unsigned int shrinker_run_no; 120 u16 available_compr_algs; 121 122 /* pseudo inode to manage cached pages */ 123 struct inode *managed_cache; 124 125 struct erofs_sb_lz4_info lz4; 126 #endif /* CONFIG_EROFS_FS_ZIP */ 127 struct inode *packed_inode; 128 struct inode *metabox_inode; 129 struct erofs_dev_context *devs; 130 u64 total_blocks; 131 132 u32 meta_blkaddr; 133 #ifdef CONFIG_EROFS_FS_XATTR 134 u32 xattr_blkaddr; 135 u32 xattr_prefix_start; 136 u8 xattr_prefix_count; 137 struct erofs_xattr_prefix_item *xattr_prefixes; 138 unsigned int xattr_filter_reserved; 139 #endif 140 u16 device_id_mask; /* valid bits of device id to be used */ 141 142 unsigned char islotbits; /* inode slot unit size in bit shift */ 143 unsigned char blkszbits; /* filesystem block size in bit shift */ 144 145 u32 sb_size; /* total superblock size */ 146 u32 fixed_nsec; 147 s64 epoch; 148 149 /* what we really care is nid, rather than ino.. */ 150 erofs_nid_t root_nid; 151 erofs_nid_t packed_nid; 152 erofs_nid_t metabox_nid; 153 /* used for statfs, f_files - f_favail */ 154 u64 inos; 155 156 u32 feature_compat; 157 u32 feature_incompat; 158 159 /* sysfs support */ 160 struct kobject s_kobj; /* /sys/fs/erofs/<devname> */ 161 struct completion s_kobj_unregister; 162 erofs_off_t dir_ra_bytes; 163 164 /* fscache support */ 165 struct fscache_volume *volume; 166 struct erofs_domain *domain; 167 char *fsid; 168 char *domain_id; 169 }; 170 171 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info) 172 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info) 173 174 /* Mount flags set via mount options or defaults */ 175 #define EROFS_MOUNT_XATTR_USER 0x00000010 176 #define EROFS_MOUNT_POSIX_ACL 0x00000020 177 #define EROFS_MOUNT_DAX_ALWAYS 0x00000040 178 #define EROFS_MOUNT_DAX_NEVER 0x00000080 179 #define EROFS_MOUNT_DIRECT_IO 0x00000100 180 181 #define clear_opt(opt, option) ((opt)->mount_opt &= ~EROFS_MOUNT_##option) 182 #define set_opt(opt, option) ((opt)->mount_opt |= EROFS_MOUNT_##option) 183 #define test_opt(opt, option) ((opt)->mount_opt & EROFS_MOUNT_##option) 184 185 static inline bool erofs_is_fileio_mode(struct erofs_sb_info *sbi) 186 { 187 return IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) && sbi->dif0.file; 188 } 189 190 static inline bool erofs_is_fscache_mode(struct super_block *sb) 191 { 192 return IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && 193 !erofs_is_fileio_mode(EROFS_SB(sb)) && !sb->s_bdev; 194 } 195 196 enum { 197 EROFS_ZIP_CACHE_DISABLED, 198 EROFS_ZIP_CACHE_READAHEAD, 199 EROFS_ZIP_CACHE_READAROUND 200 }; 201 202 struct erofs_buf { 203 struct address_space *mapping; 204 struct file *file; 205 u64 off; 206 struct page *page; 207 void *base; 208 }; 209 #define __EROFS_BUF_INITIALIZER ((struct erofs_buf){ .page = NULL }) 210 211 #define erofs_blknr(sb, pos) ((erofs_blk_t)((pos) >> (sb)->s_blocksize_bits)) 212 #define erofs_blkoff(sb, pos) ((pos) & ((sb)->s_blocksize - 1)) 213 #define erofs_pos(sb, blk) ((erofs_off_t)(blk) << (sb)->s_blocksize_bits) 214 #define erofs_iblks(i) (round_up((i)->i_size, i_blocksize(i)) >> (i)->i_blkbits) 215 216 #define EROFS_FEATURE_FUNCS(name, compat, feature) \ 217 static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \ 218 { \ 219 return sbi->feature_##compat & EROFS_FEATURE_##feature; \ 220 } 221 222 EROFS_FEATURE_FUNCS(zero_padding, incompat, INCOMPAT_ZERO_PADDING) 223 EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS) 224 EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER) 225 EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE) 226 EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE) 227 EROFS_FEATURE_FUNCS(compr_head2, incompat, INCOMPAT_COMPR_HEAD2) 228 EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING) 229 EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS) 230 EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE) 231 EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES) 232 EROFS_FEATURE_FUNCS(48bit, incompat, INCOMPAT_48BIT) 233 EROFS_FEATURE_FUNCS(metabox, incompat, INCOMPAT_METABOX) 234 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM) 235 EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER) 236 EROFS_FEATURE_FUNCS(shared_ea_in_metabox, compat, COMPAT_SHARED_EA_IN_METABOX) 237 EROFS_FEATURE_FUNCS(plain_xattr_pfx, compat, COMPAT_PLAIN_XATTR_PFX) 238 239 static inline u64 erofs_nid_to_ino64(struct erofs_sb_info *sbi, erofs_nid_t nid) 240 { 241 if (!erofs_sb_has_metabox(sbi)) 242 return nid; 243 244 /* 245 * When metadata compression is enabled, avoid generating excessively 246 * large inode numbers for metadata-compressed inodes. Shift NIDs in 247 * the 31-62 bit range left by one and move the metabox flag to bit 31. 248 * 249 * Note: on-disk NIDs remain unchanged as they are primarily used for 250 * compatibility with non-LFS 32-bit applications. 251 */ 252 return ((nid << 1) & GENMASK_ULL(63, 32)) | (nid & GENMASK(30, 0)) | 253 ((nid >> EROFS_DIRENT_NID_METABOX_BIT) << 31); 254 } 255 256 /* atomic flag definitions */ 257 #define EROFS_I_EA_INITED_BIT 0 258 #define EROFS_I_Z_INITED_BIT 1 259 260 /* bitlock definitions (arranged in reverse order) */ 261 #define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1) 262 #define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2) 263 264 /* default readahead size of directories */ 265 #define EROFS_DIR_RA_BYTES 16384 266 267 struct erofs_inode { 268 erofs_nid_t nid; 269 270 /* atomic flags (including bitlocks) */ 271 unsigned long flags; 272 273 unsigned char datalayout; 274 unsigned char inode_isize; 275 bool dot_omitted; 276 unsigned int xattr_isize; 277 278 unsigned int xattr_name_filter; 279 unsigned int xattr_shared_count; 280 unsigned int *xattr_shared_xattrs; 281 282 union { 283 erofs_blk_t startblk; 284 struct { 285 unsigned short chunkformat; 286 unsigned char chunkbits; 287 }; 288 #ifdef CONFIG_EROFS_FS_ZIP 289 struct { 290 unsigned short z_advise; 291 unsigned char z_algorithmtype[2]; 292 unsigned char z_lclusterbits; 293 union { 294 u64 z_tailextent_headlcn; 295 u64 z_extents; 296 }; 297 erofs_off_t z_fragmentoff; 298 unsigned short z_idata_size; 299 }; 300 #endif /* CONFIG_EROFS_FS_ZIP */ 301 }; 302 /* the corresponding vfs inode */ 303 struct inode vfs_inode; 304 }; 305 306 #define EROFS_I(ptr) container_of(ptr, struct erofs_inode, vfs_inode) 307 308 static inline bool erofs_inode_in_metabox(struct inode *inode) 309 { 310 return EROFS_I(inode)->nid & BIT_ULL(EROFS_DIRENT_NID_METABOX_BIT); 311 } 312 313 static inline erofs_off_t erofs_iloc(struct inode *inode) 314 { 315 struct erofs_sb_info *sbi = EROFS_I_SB(inode); 316 erofs_nid_t nid_lo = EROFS_I(inode)->nid & EROFS_DIRENT_NID_MASK; 317 318 if (erofs_inode_in_metabox(inode)) 319 return nid_lo << sbi->islotbits; 320 return erofs_pos(inode->i_sb, sbi->meta_blkaddr) + 321 (nid_lo << sbi->islotbits); 322 } 323 324 static inline unsigned int erofs_inode_version(unsigned int ifmt) 325 { 326 return (ifmt >> EROFS_I_VERSION_BIT) & EROFS_I_VERSION_MASK; 327 } 328 329 static inline unsigned int erofs_inode_datalayout(unsigned int ifmt) 330 { 331 return (ifmt >> EROFS_I_DATALAYOUT_BIT) & EROFS_I_DATALAYOUT_MASK; 332 } 333 334 /* reclaiming is never triggered when allocating new folios. */ 335 static inline struct folio *erofs_grab_folio_nowait(struct address_space *as, 336 pgoff_t index) 337 { 338 return __filemap_get_folio(as, index, 339 FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT, 340 readahead_gfp_mask(as) & ~__GFP_RECLAIM); 341 } 342 343 /* Has a disk mapping */ 344 #define EROFS_MAP_MAPPED 0x0001 345 /* Located in metadata (could be copied from bd_inode) */ 346 #define EROFS_MAP_META 0x0002 347 /* The extent is encoded */ 348 #define EROFS_MAP_ENCODED 0x0004 349 /* The length of extent is full */ 350 #define EROFS_MAP_FULL_MAPPED 0x0008 351 /* Located in the special packed inode */ 352 #define __EROFS_MAP_FRAGMENT 0x0010 353 /* The extent refers to partial decompressed data */ 354 #define EROFS_MAP_PARTIAL_REF 0x0020 355 356 #define EROFS_MAP_FRAGMENT (EROFS_MAP_MAPPED | __EROFS_MAP_FRAGMENT) 357 358 struct erofs_map_blocks { 359 struct erofs_buf buf; 360 361 erofs_off_t m_pa, m_la; 362 u64 m_plen, m_llen; 363 364 unsigned short m_deviceid; 365 char m_algorithmformat; 366 unsigned int m_flags; 367 }; 368 369 /* 370 * Used to get the exact decompressed length, e.g. fiemap (consider lookback 371 * approach instead if possible since it's more metadata lightweight.) 372 */ 373 #define EROFS_GET_BLOCKS_FIEMAP 0x0001 374 /* Used to map the whole extent if non-negligible data is requested for LZMA */ 375 #define EROFS_GET_BLOCKS_READMORE 0x0002 376 /* Used to map tail extent for tailpacking inline or fragment pcluster */ 377 #define EROFS_GET_BLOCKS_FINDTAIL 0x0004 378 379 enum { 380 Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX, 381 Z_EROFS_COMPRESSION_INTERLACED, 382 Z_EROFS_COMPRESSION_RUNTIME_MAX 383 }; 384 385 struct erofs_map_dev { 386 struct super_block *m_sb; 387 struct erofs_device_info *m_dif; 388 struct block_device *m_bdev; 389 390 erofs_off_t m_pa; 391 unsigned int m_deviceid; 392 }; 393 394 extern const struct super_operations erofs_sops; 395 396 extern const struct address_space_operations erofs_aops; 397 extern const struct address_space_operations erofs_fileio_aops; 398 extern const struct address_space_operations z_erofs_aops; 399 extern const struct address_space_operations erofs_fscache_access_aops; 400 401 extern const struct inode_operations erofs_generic_iops; 402 extern const struct inode_operations erofs_symlink_iops; 403 extern const struct inode_operations erofs_fast_symlink_iops; 404 extern const struct inode_operations erofs_dir_iops; 405 406 extern const struct file_operations erofs_file_fops; 407 extern const struct file_operations erofs_dir_fops; 408 409 extern const struct iomap_ops z_erofs_iomap_report_ops; 410 411 /* flags for erofs_fscache_register_cookie() */ 412 #define EROFS_REG_COOKIE_SHARE 0x0001 413 #define EROFS_REG_COOKIE_NEED_NOEXIST 0x0002 414 415 void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf, 416 erofs_off_t *offset, int *lengthp); 417 void erofs_unmap_metabuf(struct erofs_buf *buf); 418 void erofs_put_metabuf(struct erofs_buf *buf); 419 void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap); 420 int erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb, 421 bool in_metabox); 422 void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb, 423 erofs_off_t offset, bool in_metabox); 424 int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev); 425 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 426 u64 start, u64 len); 427 int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map); 428 void erofs_onlinefolio_init(struct folio *folio); 429 void erofs_onlinefolio_split(struct folio *folio); 430 void erofs_onlinefolio_end(struct folio *folio, int err, bool dirty); 431 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid); 432 int erofs_getattr(struct mnt_idmap *idmap, const struct path *path, 433 struct kstat *stat, u32 request_mask, 434 unsigned int query_flags); 435 int erofs_namei(struct inode *dir, const struct qstr *name, 436 erofs_nid_t *nid, unsigned int *d_type); 437 438 static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count) 439 { 440 int retried = 0; 441 442 while (1) { 443 void *p = vm_map_ram(pages, count, -1); 444 445 /* retry two more times (totally 3 times) */ 446 if (p || ++retried >= 3) 447 return p; 448 vm_unmap_aliases(); 449 } 450 return NULL; 451 } 452 453 int erofs_register_sysfs(struct super_block *sb); 454 void erofs_unregister_sysfs(struct super_block *sb); 455 int __init erofs_init_sysfs(void); 456 void erofs_exit_sysfs(void); 457 458 struct page *__erofs_allocpage(struct page **pagepool, gfp_t gfp, bool tryrsv); 459 static inline struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp) 460 { 461 return __erofs_allocpage(pagepool, gfp, false); 462 } 463 static inline void erofs_pagepool_add(struct page **pagepool, struct page *page) 464 { 465 set_page_private(page, (unsigned long)*pagepool); 466 *pagepool = page; 467 } 468 void erofs_release_pages(struct page **pagepool); 469 470 #ifdef CONFIG_EROFS_FS_ZIP 471 #define MNGD_MAPPING(sbi) ((sbi)->managed_cache->i_mapping) 472 473 extern atomic_long_t erofs_global_shrink_cnt; 474 void erofs_shrinker_register(struct super_block *sb); 475 void erofs_shrinker_unregister(struct super_block *sb); 476 int __init erofs_init_shrinker(void); 477 void erofs_exit_shrinker(void); 478 int __init z_erofs_init_subsystem(void); 479 void z_erofs_exit_subsystem(void); 480 int z_erofs_init_super(struct super_block *sb); 481 unsigned long z_erofs_shrink_scan(struct erofs_sb_info *sbi, 482 unsigned long nr_shrink); 483 int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map, 484 int flags); 485 void *z_erofs_get_gbuf(unsigned int requiredpages); 486 void z_erofs_put_gbuf(void *ptr); 487 int z_erofs_gbuf_growsize(unsigned int nrpages); 488 int __init z_erofs_gbuf_init(void); 489 void z_erofs_gbuf_exit(void); 490 int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb); 491 #else 492 static inline void erofs_shrinker_register(struct super_block *sb) {} 493 static inline void erofs_shrinker_unregister(struct super_block *sb) {} 494 static inline int erofs_init_shrinker(void) { return 0; } 495 static inline void erofs_exit_shrinker(void) {} 496 static inline int z_erofs_init_subsystem(void) { return 0; } 497 static inline void z_erofs_exit_subsystem(void) {} 498 static inline int z_erofs_init_super(struct super_block *sb) { return 0; } 499 #endif /* !CONFIG_EROFS_FS_ZIP */ 500 501 #ifdef CONFIG_EROFS_FS_BACKED_BY_FILE 502 struct bio *erofs_fileio_bio_alloc(struct erofs_map_dev *mdev); 503 void erofs_fileio_submit_bio(struct bio *bio); 504 #else 505 static inline struct bio *erofs_fileio_bio_alloc(struct erofs_map_dev *mdev) { return NULL; } 506 static inline void erofs_fileio_submit_bio(struct bio *bio) {} 507 #endif 508 509 #ifdef CONFIG_EROFS_FS_ONDEMAND 510 int erofs_fscache_register_fs(struct super_block *sb); 511 void erofs_fscache_unregister_fs(struct super_block *sb); 512 513 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb, 514 char *name, unsigned int flags); 515 void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache); 516 struct bio *erofs_fscache_bio_alloc(struct erofs_map_dev *mdev); 517 void erofs_fscache_submit_bio(struct bio *bio); 518 #else 519 static inline int erofs_fscache_register_fs(struct super_block *sb) 520 { 521 return -EOPNOTSUPP; 522 } 523 static inline void erofs_fscache_unregister_fs(struct super_block *sb) {} 524 525 static inline 526 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb, 527 char *name, unsigned int flags) 528 { 529 return ERR_PTR(-EOPNOTSUPP); 530 } 531 532 static inline void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache) 533 { 534 } 535 static inline struct bio *erofs_fscache_bio_alloc(struct erofs_map_dev *mdev) { return NULL; } 536 static inline void erofs_fscache_submit_bio(struct bio *bio) {} 537 #endif 538 539 #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */ 540 541 #endif /* __EROFS_INTERNAL_H */ 542