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