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 #ifndef __EROFS_INTERNAL_H 8 #define __EROFS_INTERNAL_H 9 10 #include <linux/fs.h> 11 #include <linux/dcache.h> 12 #include <linux/mm.h> 13 #include <linux/pagemap.h> 14 #include <linux/bio.h> 15 #include <linux/buffer_head.h> 16 #include <linux/magic.h> 17 #include <linux/slab.h> 18 #include <linux/vmalloc.h> 19 #include "erofs_fs.h" 20 21 /* redefine pr_fmt "erofs: " */ 22 #undef pr_fmt 23 #define pr_fmt(fmt) "erofs: " fmt 24 25 #define errln(x, ...) pr_err(x "\n", ##__VA_ARGS__) 26 #define infoln(x, ...) pr_info(x "\n", ##__VA_ARGS__) 27 #ifdef CONFIG_EROFS_FS_DEBUG 28 #define debugln(x, ...) pr_debug(x "\n", ##__VA_ARGS__) 29 #define DBG_BUGON BUG_ON 30 #else 31 #define debugln(x, ...) ((void)0) 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 /* data type for filesystem-wide blocks number */ 41 typedef u32 erofs_blk_t; 42 43 struct erofs_sb_info { 44 #ifdef CONFIG_EROFS_FS_ZIP 45 /* list for all registered superblocks, mainly for shrinker */ 46 struct list_head list; 47 struct mutex umount_mutex; 48 49 /* the dedicated workstation for compression */ 50 struct radix_tree_root workstn_tree; 51 52 /* threshold for decompression synchronously */ 53 unsigned int max_sync_decompress_pages; 54 55 unsigned int shrinker_run_no; 56 57 /* current strategy of how to use managed cache */ 58 unsigned char cache_strategy; 59 60 /* pseudo inode to manage cached pages */ 61 struct inode *managed_cache; 62 #endif /* CONFIG_EROFS_FS_ZIP */ 63 u32 blocks; 64 u32 meta_blkaddr; 65 #ifdef CONFIG_EROFS_FS_XATTR 66 u32 xattr_blkaddr; 67 #endif 68 69 /* inode slot unit size in bit shift */ 70 unsigned char islotbits; 71 72 u32 build_time_nsec; 73 u64 build_time; 74 75 /* what we really care is nid, rather than ino.. */ 76 erofs_nid_t root_nid; 77 /* used for statfs, f_files - f_favail */ 78 u64 inos; 79 80 u8 uuid[16]; /* 128-bit uuid for volume */ 81 u8 volume_name[16]; /* volume name */ 82 u32 feature_incompat; 83 84 unsigned int mount_opt; 85 }; 86 87 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info) 88 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info) 89 90 /* Mount flags set via mount options or defaults */ 91 #define EROFS_MOUNT_XATTR_USER 0x00000010 92 #define EROFS_MOUNT_POSIX_ACL 0x00000020 93 94 #define clear_opt(sbi, option) ((sbi)->mount_opt &= ~EROFS_MOUNT_##option) 95 #define set_opt(sbi, option) ((sbi)->mount_opt |= EROFS_MOUNT_##option) 96 #define test_opt(sbi, option) ((sbi)->mount_opt & EROFS_MOUNT_##option) 97 98 #ifdef CONFIG_EROFS_FS_ZIP 99 enum { 100 EROFS_ZIP_CACHE_DISABLED, 101 EROFS_ZIP_CACHE_READAHEAD, 102 EROFS_ZIP_CACHE_READAROUND 103 }; 104 105 #define EROFS_LOCKED_MAGIC (INT_MIN | 0xE0F510CCL) 106 107 /* basic unit of the workstation of a super_block */ 108 struct erofs_workgroup { 109 /* the workgroup index in the workstation */ 110 pgoff_t index; 111 112 /* overall workgroup reference count */ 113 atomic_t refcount; 114 }; 115 116 #if defined(CONFIG_SMP) 117 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp, 118 int val) 119 { 120 preempt_disable(); 121 if (val != atomic_cmpxchg(&grp->refcount, val, EROFS_LOCKED_MAGIC)) { 122 preempt_enable(); 123 return false; 124 } 125 return true; 126 } 127 128 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp, 129 int orig_val) 130 { 131 /* 132 * other observers should notice all modifications 133 * in the freezing period. 134 */ 135 smp_mb(); 136 atomic_set(&grp->refcount, orig_val); 137 preempt_enable(); 138 } 139 140 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp) 141 { 142 return atomic_cond_read_relaxed(&grp->refcount, 143 VAL != EROFS_LOCKED_MAGIC); 144 } 145 #else 146 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp, 147 int val) 148 { 149 preempt_disable(); 150 /* no need to spin on UP platforms, let's just disable preemption. */ 151 if (val != atomic_read(&grp->refcount)) { 152 preempt_enable(); 153 return false; 154 } 155 return true; 156 } 157 158 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp, 159 int orig_val) 160 { 161 preempt_enable(); 162 } 163 164 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp) 165 { 166 int v = atomic_read(&grp->refcount); 167 168 /* workgroup is never freezed on uniprocessor systems */ 169 DBG_BUGON(v == EROFS_LOCKED_MAGIC); 170 return v; 171 } 172 #endif /* !CONFIG_SMP */ 173 174 /* hard limit of pages per compressed cluster */ 175 #define Z_EROFS_CLUSTER_MAX_PAGES (CONFIG_EROFS_FS_CLUSTER_PAGE_LIMIT) 176 #define EROFS_PCPUBUF_NR_PAGES Z_EROFS_CLUSTER_MAX_PAGES 177 #else 178 #define EROFS_PCPUBUF_NR_PAGES 0 179 #endif /* !CONFIG_EROFS_FS_ZIP */ 180 181 /* we strictly follow PAGE_SIZE and no buffer head yet */ 182 #define LOG_BLOCK_SIZE PAGE_SHIFT 183 184 #undef LOG_SECTORS_PER_BLOCK 185 #define LOG_SECTORS_PER_BLOCK (PAGE_SHIFT - 9) 186 187 #undef SECTORS_PER_BLOCK 188 #define SECTORS_PER_BLOCK (1 << SECTORS_PER_BLOCK) 189 190 #define EROFS_BLKSIZ (1 << LOG_BLOCK_SIZE) 191 192 #if (EROFS_BLKSIZ % 4096 || !EROFS_BLKSIZ) 193 #error erofs cannot be used in this platform 194 #endif 195 196 #define ROOT_NID(sb) ((sb)->root_nid) 197 198 #define erofs_blknr(addr) ((addr) / EROFS_BLKSIZ) 199 #define erofs_blkoff(addr) ((addr) % EROFS_BLKSIZ) 200 #define blknr_to_addr(nr) ((erofs_off_t)(nr) * EROFS_BLKSIZ) 201 202 static inline erofs_off_t iloc(struct erofs_sb_info *sbi, erofs_nid_t nid) 203 { 204 return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits); 205 } 206 207 /* atomic flag definitions */ 208 #define EROFS_I_EA_INITED_BIT 0 209 #define EROFS_I_Z_INITED_BIT 1 210 211 /* bitlock definitions (arranged in reverse order) */ 212 #define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1) 213 #define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2) 214 215 struct erofs_inode { 216 erofs_nid_t nid; 217 218 /* atomic flags (including bitlocks) */ 219 unsigned long flags; 220 221 unsigned char datalayout; 222 unsigned char inode_isize; 223 unsigned short xattr_isize; 224 225 unsigned int xattr_shared_count; 226 unsigned int *xattr_shared_xattrs; 227 228 union { 229 erofs_blk_t raw_blkaddr; 230 #ifdef CONFIG_EROFS_FS_ZIP 231 struct { 232 unsigned short z_advise; 233 unsigned char z_algorithmtype[2]; 234 unsigned char z_logical_clusterbits; 235 unsigned char z_physical_clusterbits[2]; 236 }; 237 #endif /* CONFIG_EROFS_FS_ZIP */ 238 }; 239 /* the corresponding vfs inode */ 240 struct inode vfs_inode; 241 }; 242 243 #define EROFS_I(ptr) \ 244 container_of(ptr, struct erofs_inode, vfs_inode) 245 246 static inline unsigned long erofs_inode_datablocks(struct inode *inode) 247 { 248 /* since i_size cannot be changed */ 249 return DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ); 250 } 251 252 static inline unsigned int erofs_bitrange(unsigned int value, unsigned int bit, 253 unsigned int bits) 254 { 255 256 return (value >> bit) & ((1 << bits) - 1); 257 } 258 259 260 static inline unsigned int erofs_inode_version(unsigned int value) 261 { 262 return erofs_bitrange(value, EROFS_I_VERSION_BIT, 263 EROFS_I_VERSION_BITS); 264 } 265 266 static inline unsigned int erofs_inode_datalayout(unsigned int value) 267 { 268 return erofs_bitrange(value, EROFS_I_DATALAYOUT_BIT, 269 EROFS_I_DATALAYOUT_BITS); 270 } 271 272 extern const struct super_operations erofs_sops; 273 274 extern const struct address_space_operations erofs_raw_access_aops; 275 #ifdef CONFIG_EROFS_FS_ZIP 276 extern const struct address_space_operations z_erofs_vle_normalaccess_aops; 277 #endif 278 279 /* 280 * Logical to physical block mapping, used by erofs_map_blocks() 281 * 282 * Different with other file systems, it is used for 2 access modes: 283 * 284 * 1) RAW access mode: 285 * 286 * Users pass a valid (m_lblk, m_lofs -- usually 0) pair, 287 * and get the valid m_pblk, m_pofs and the longest m_len(in bytes). 288 * 289 * Note that m_lblk in the RAW access mode refers to the number of 290 * the compressed ondisk block rather than the uncompressed 291 * in-memory block for the compressed file. 292 * 293 * m_pofs equals to m_lofs except for the inline data page. 294 * 295 * 2) Normal access mode: 296 * 297 * If the inode is not compressed, it has no difference with 298 * the RAW access mode. However, if the inode is compressed, 299 * users should pass a valid (m_lblk, m_lofs) pair, and get 300 * the needed m_pblk, m_pofs, m_len to get the compressed data 301 * and the updated m_lblk, m_lofs which indicates the start 302 * of the corresponding uncompressed data in the file. 303 */ 304 enum { 305 BH_Zipped = BH_PrivateStart, 306 BH_FullMapped, 307 }; 308 309 /* Has a disk mapping */ 310 #define EROFS_MAP_MAPPED (1 << BH_Mapped) 311 /* Located in metadata (could be copied from bd_inode) */ 312 #define EROFS_MAP_META (1 << BH_Meta) 313 /* The extent has been compressed */ 314 #define EROFS_MAP_ZIPPED (1 << BH_Zipped) 315 /* The length of extent is full */ 316 #define EROFS_MAP_FULL_MAPPED (1 << BH_FullMapped) 317 318 struct erofs_map_blocks { 319 erofs_off_t m_pa, m_la; 320 u64 m_plen, m_llen; 321 322 unsigned int m_flags; 323 324 struct page *mpage; 325 }; 326 327 /* Flags used by erofs_map_blocks() */ 328 #define EROFS_GET_BLOCKS_RAW 0x0001 329 330 /* zmap.c */ 331 #ifdef CONFIG_EROFS_FS_ZIP 332 int z_erofs_fill_inode(struct inode *inode); 333 int z_erofs_map_blocks_iter(struct inode *inode, 334 struct erofs_map_blocks *map, 335 int flags); 336 #else 337 static inline int z_erofs_fill_inode(struct inode *inode) { return -EOPNOTSUPP; } 338 static inline int z_erofs_map_blocks_iter(struct inode *inode, 339 struct erofs_map_blocks *map, 340 int flags) 341 { 342 return -EOPNOTSUPP; 343 } 344 #endif /* !CONFIG_EROFS_FS_ZIP */ 345 346 /* data.c */ 347 struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr); 348 349 int erofs_map_blocks(struct inode *, struct erofs_map_blocks *, int); 350 351 /* inode.c */ 352 static inline unsigned long erofs_inode_hash(erofs_nid_t nid) 353 { 354 #if BITS_PER_LONG == 32 355 return (nid >> 32) ^ (nid & 0xffffffff); 356 #else 357 return nid; 358 #endif 359 } 360 361 extern const struct inode_operations erofs_generic_iops; 362 extern const struct inode_operations erofs_symlink_iops; 363 extern const struct inode_operations erofs_fast_symlink_iops; 364 365 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid, bool dir); 366 int erofs_getattr(const struct path *path, struct kstat *stat, 367 u32 request_mask, unsigned int query_flags); 368 369 /* namei.c */ 370 extern const struct inode_operations erofs_dir_iops; 371 372 int erofs_namei(struct inode *dir, struct qstr *name, 373 erofs_nid_t *nid, unsigned int *d_type); 374 375 /* dir.c */ 376 extern const struct file_operations erofs_dir_fops; 377 378 /* utils.c / zdata.c */ 379 struct page *erofs_allocpage(struct list_head *pool, gfp_t gfp, bool nofail); 380 381 #if (EROFS_PCPUBUF_NR_PAGES > 0) 382 void *erofs_get_pcpubuf(unsigned int pagenr); 383 #define erofs_put_pcpubuf(buf) do { \ 384 (void)&(buf); \ 385 preempt_enable(); \ 386 } while (0) 387 #else 388 static inline void *erofs_get_pcpubuf(unsigned int pagenr) 389 { 390 return ERR_PTR(-EOPNOTSUPP); 391 } 392 393 #define erofs_put_pcpubuf(buf) do {} while (0) 394 #endif 395 396 #ifdef CONFIG_EROFS_FS_ZIP 397 int erofs_workgroup_put(struct erofs_workgroup *grp); 398 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb, 399 pgoff_t index, bool *tag); 400 int erofs_register_workgroup(struct super_block *sb, 401 struct erofs_workgroup *grp, bool tag); 402 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp); 403 void erofs_shrinker_register(struct super_block *sb); 404 void erofs_shrinker_unregister(struct super_block *sb); 405 int __init erofs_init_shrinker(void); 406 void erofs_exit_shrinker(void); 407 int __init z_erofs_init_zip_subsystem(void); 408 void z_erofs_exit_zip_subsystem(void); 409 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi, 410 struct erofs_workgroup *egrp); 411 int erofs_try_to_free_cached_page(struct address_space *mapping, 412 struct page *page); 413 #else 414 static inline void erofs_shrinker_register(struct super_block *sb) {} 415 static inline void erofs_shrinker_unregister(struct super_block *sb) {} 416 static inline int erofs_init_shrinker(void) { return 0; } 417 static inline void erofs_exit_shrinker(void) {} 418 static inline int z_erofs_init_zip_subsystem(void) { return 0; } 419 static inline void z_erofs_exit_zip_subsystem(void) {} 420 #endif /* !CONFIG_EROFS_FS_ZIP */ 421 422 #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */ 423 424 #endif /* __EROFS_INTERNAL_H */ 425 426