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