1d29fbcdbSNishad Kamdar /* SPDX-License-Identifier: GPL-2.0 */ 20a8165d7SJaegeuk Kim /* 339a53e0cSJaegeuk Kim * fs/f2fs/f2fs.h 439a53e0cSJaegeuk Kim * 539a53e0cSJaegeuk Kim * Copyright (c) 2012 Samsung Electronics Co., Ltd. 639a53e0cSJaegeuk Kim * http://www.samsung.com/ 739a53e0cSJaegeuk Kim */ 839a53e0cSJaegeuk Kim #ifndef _LINUX_F2FS_H 939a53e0cSJaegeuk Kim #define _LINUX_F2FS_H 1039a53e0cSJaegeuk Kim 11f847c699SChao Yu #include <linux/uio.h> 1239a53e0cSJaegeuk Kim #include <linux/types.h> 1339a53e0cSJaegeuk Kim #include <linux/page-flags.h> 1439a53e0cSJaegeuk Kim #include <linux/buffer_head.h> 1539a53e0cSJaegeuk Kim #include <linux/slab.h> 1639a53e0cSJaegeuk Kim #include <linux/crc32.h> 1739a53e0cSJaegeuk Kim #include <linux/magic.h> 18c2d715d1SJaegeuk Kim #include <linux/kobject.h> 197bd59381SGu Zheng #include <linux/sched.h> 207c2e5963SJaegeuk Kim #include <linux/cred.h> 2139307a8eSJaegeuk Kim #include <linux/vmalloc.h> 22740432f8SJaegeuk Kim #include <linux/bio.h> 23d0239e1bSJaegeuk Kim #include <linux/blkdev.h> 240abd675eSChao Yu #include <linux/quotaops.h> 25c6a564ffSChristoph Hellwig #include <linux/part_stat.h> 2643b6573bSKeith Mok #include <crypto/hash.h> 2739a53e0cSJaegeuk Kim 28734f0d24SDave Chinner #include <linux/fscrypt.h> 2995ae251fSEric Biggers #include <linux/fsverity.h> 30734f0d24SDave Chinner 315d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS 329850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition) BUG_ON(condition) 335d56b671SJaegeuk Kim #else 349850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition) \ 359850cf4aSJaegeuk Kim do { \ 36db489652SYangtao Li if (WARN_ON(condition)) \ 37caf0047eSChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); \ 389850cf4aSJaegeuk Kim } while (0) 395d56b671SJaegeuk Kim #endif 405d56b671SJaegeuk Kim 412c63feadSJaegeuk Kim enum { 422c63feadSJaegeuk Kim FAULT_KMALLOC, 43628b3d14SChao Yu FAULT_KVMALLOC, 44c41f3cc3SJaegeuk Kim FAULT_PAGE_ALLOC, 4501eccef7SChao Yu FAULT_PAGE_GET, 46d62fe971SChao Yu FAULT_ALLOC_BIO, 47cb78942bSJaegeuk Kim FAULT_ALLOC_NID, 48cb78942bSJaegeuk Kim FAULT_ORPHAN, 49cb78942bSJaegeuk Kim FAULT_BLOCK, 50cb78942bSJaegeuk Kim FAULT_DIR_DEPTH, 5153aa6bbfSJaegeuk Kim FAULT_EVICT_INODE, 5214b44d23SJaegeuk Kim FAULT_TRUNCATE, 536f5c2ed0SChao Yu FAULT_READ_IO, 540f348028SChao Yu FAULT_CHECKPOINT, 55b83dcfe6SChao Yu FAULT_DISCARD, 566f5c2ed0SChao Yu FAULT_WRITE_IO, 572c63feadSJaegeuk Kim FAULT_MAX, 582c63feadSJaegeuk Kim }; 592c63feadSJaegeuk Kim 607fa750a1SArnd Bergmann #ifdef CONFIG_F2FS_FAULT_INJECTION 61d494500aSChao Yu #define F2FS_ALL_FAULT_TYPE ((1 << FAULT_MAX) - 1) 62d494500aSChao Yu 6308796897SSheng Yong struct f2fs_fault_info { 6408796897SSheng Yong atomic_t inject_ops; 6508796897SSheng Yong unsigned int inject_rate; 6608796897SSheng Yong unsigned int inject_type; 6708796897SSheng Yong }; 6808796897SSheng Yong 6919880e6eSAlexey Dobriyan extern const char *f2fs_fault_name[FAULT_MAX]; 7068afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type))) 712c63feadSJaegeuk Kim #endif 722c63feadSJaegeuk Kim 7339a53e0cSJaegeuk Kim /* 7439a53e0cSJaegeuk Kim * For mount options 7539a53e0cSJaegeuk Kim */ 7639a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002 7739a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD 0x00000004 7839a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP 0x00000008 7939a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER 0x00000010 8039a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL 0x00000020 8139a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040 82444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR 0x00000080 831001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA 0x00000100 8434d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY 0x00000200 8534d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE 0x00000400 8634d67debSChao Yu #define F2FS_MOUNT_NOBARRIER 0x00000800 87d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT 0x00001000 8889672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE 0x00002000 89343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH 0x00008000 9073faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION 0x00010000 910abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA 0x00080000 920abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA 0x00100000 935c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA 0x00200000 944b2414d0SChao Yu #define F2FS_MOUNT_QUOTA 0x00400000 956afc662eSChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE 0x00800000 967e65be49SJaegeuk Kim #define F2FS_MOUNT_RESERVE_ROOT 0x01000000 974354994fSDaniel Rosenberg #define F2FS_MOUNT_DISABLE_CHECKPOINT 0x02000000 98a9117ecaSChao Yu #define F2FS_MOUNT_NORECOVERY 0x04000000 99093749e2SChao Yu #define F2FS_MOUNT_ATGC 0x08000000 10039a53e0cSJaegeuk Kim 10163189b78SChao Yu #define F2FS_OPTION(sbi) ((sbi)->mount_opt) 10263189b78SChao Yu #define clear_opt(sbi, option) (F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option) 10363189b78SChao Yu #define set_opt(sbi, option) (F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option) 10463189b78SChao Yu #define test_opt(sbi, option) (F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option) 10539a53e0cSJaegeuk Kim 10639a53e0cSJaegeuk Kim #define ver_after(a, b) (typecheck(unsigned long long, a) && \ 10739a53e0cSJaegeuk Kim typecheck(unsigned long long, b) && \ 10839a53e0cSJaegeuk Kim ((long long)((a) - (b)) > 0)) 10939a53e0cSJaegeuk Kim 110a9841c4dSJaegeuk Kim typedef u32 block_t; /* 111a9841c4dSJaegeuk Kim * should not change u32, since it is the on-disk block 112a9841c4dSJaegeuk Kim * address format, __le32. 113a9841c4dSJaegeuk Kim */ 11439a53e0cSJaegeuk Kim typedef u32 nid_t; 11539a53e0cSJaegeuk Kim 1164c8ff709SChao Yu #define COMPRESS_EXT_NUM 16 1174c8ff709SChao Yu 11839a53e0cSJaegeuk Kim struct f2fs_mount_info { 11939a53e0cSJaegeuk Kim unsigned int opt; 12063189b78SChao Yu int write_io_size_bits; /* Write IO size bits */ 12163189b78SChao Yu block_t root_reserved_blocks; /* root reserved blocks */ 12263189b78SChao Yu kuid_t s_resuid; /* reserved blocks for uid */ 12363189b78SChao Yu kgid_t s_resgid; /* reserved blocks for gid */ 12463189b78SChao Yu int active_logs; /* # of active logs */ 12563189b78SChao Yu int inline_xattr_size; /* inline xattr size */ 12663189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION 12763189b78SChao Yu struct f2fs_fault_info fault_info; /* For fault injection */ 12863189b78SChao Yu #endif 12963189b78SChao Yu #ifdef CONFIG_QUOTA 13063189b78SChao Yu /* Names of quota files with journalled quota */ 13163189b78SChao Yu char *s_qf_names[MAXQUOTAS]; 13263189b78SChao Yu int s_jquota_fmt; /* Format of quota to use */ 13363189b78SChao Yu #endif 13463189b78SChao Yu /* For which write hints are passed down to block layer */ 13563189b78SChao Yu int whint_mode; 13663189b78SChao Yu int alloc_mode; /* segment allocation policy */ 13763189b78SChao Yu int fsync_mode; /* fsync policy */ 138b0332a0fSChao Yu int fs_mode; /* fs mode: LFS or ADAPTIVE */ 139bbbc34fdSChao Yu int bggc_mode; /* bggc mode: off, on or sync */ 140ac4acb1fSEric Biggers struct fscrypt_dummy_policy dummy_enc_policy; /* test dummy encryption */ 1411ae18f71SJaegeuk Kim block_t unusable_cap_perc; /* percentage for cap */ 1424d3aed70SDaniel Rosenberg block_t unusable_cap; /* Amount of space allowed to be 1434d3aed70SDaniel Rosenberg * unusable when disabling checkpoint 1444d3aed70SDaniel Rosenberg */ 1454c8ff709SChao Yu 1464c8ff709SChao Yu /* For compression */ 1474c8ff709SChao Yu unsigned char compress_algorithm; /* algorithm type */ 148b28f047bSChao Yu unsigned char compress_log_size; /* cluster log size */ 149b28f047bSChao Yu bool compress_chksum; /* compressed data chksum */ 1504c8ff709SChao Yu unsigned char compress_ext_cnt; /* extension count */ 151602a16d5SDaeho Jeong int compress_mode; /* compression mode */ 1524c8ff709SChao Yu unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */ 15339a53e0cSJaegeuk Kim }; 15439a53e0cSJaegeuk Kim 155cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT 0x0001 1560bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED 0x0002 157e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE 0x0004 1587a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR 0x0008 1595c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA 0x0010 160704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM 0x0020 1616afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR 0x0040 162234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO 0x0080 1631c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME 0x0100 164b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND 0x0200 16595ae251fSEric Biggers #define F2FS_FEATURE_VERITY 0x0400 166d440c52dSJunling Zheng #define F2FS_FEATURE_SB_CHKSUM 0x0800 1675aba5430SDaniel Rosenberg #define F2FS_FEATURE_CASEFOLD 0x1000 1684c8ff709SChao Yu #define F2FS_FEATURE_COMPRESSION 0x2000 169cde4de12SJaegeuk Kim 1707beb01f7SChao Yu #define __F2FS_HAS_FEATURE(raw_super, mask) \ 1717beb01f7SChao Yu ((raw_super->feature & cpu_to_le32(mask)) != 0) 1727beb01f7SChao Yu #define F2FS_HAS_FEATURE(sbi, mask) __F2FS_HAS_FEATURE(sbi->raw_super, mask) 1737beb01f7SChao Yu #define F2FS_SET_FEATURE(sbi, mask) \ 1747beb01f7SChao Yu (sbi->raw_super->feature |= cpu_to_le32(mask)) 1757beb01f7SChao Yu #define F2FS_CLEAR_FEATURE(sbi, mask) \ 1767beb01f7SChao Yu (sbi->raw_super->feature &= ~cpu_to_le32(mask)) 17776f105a2SJaegeuk Kim 17839a53e0cSJaegeuk Kim /* 1797c2e5963SJaegeuk Kim * Default values for user and/or group using reserved blocks 1807c2e5963SJaegeuk Kim */ 1817c2e5963SJaegeuk Kim #define F2FS_DEF_RESUID 0 1827c2e5963SJaegeuk Kim #define F2FS_DEF_RESGID 0 1837c2e5963SJaegeuk Kim 1847c2e5963SJaegeuk Kim /* 18539a53e0cSJaegeuk Kim * For checkpoint manager 18639a53e0cSJaegeuk Kim */ 18739a53e0cSJaegeuk Kim enum { 18839a53e0cSJaegeuk Kim NAT_BITMAP, 18939a53e0cSJaegeuk Kim SIT_BITMAP 19039a53e0cSJaegeuk Kim }; 19139a53e0cSJaegeuk Kim 192c473f1a9SChao Yu #define CP_UMOUNT 0x00000001 193c473f1a9SChao Yu #define CP_FASTBOOT 0x00000002 194c473f1a9SChao Yu #define CP_SYNC 0x00000004 195c473f1a9SChao Yu #define CP_RECOVERY 0x00000008 196c473f1a9SChao Yu #define CP_DISCARD 0x00000010 1971f43e2adSChao Yu #define CP_TRIMMED 0x00000020 1984354994fSDaniel Rosenberg #define CP_PAUSE 0x00000040 199b4b10061SJaegeuk Kim #define CP_RESIZE 0x00000080 20075ab4cb8SJaegeuk Kim 2014ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi) BLKS_PER_SEC(sbi) 202ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST 8 /* issue 8 discards per round */ 203969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME 50 /* 50 ms, if exists */ 204f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME 500 /* 500 ms, if device busy */ 205969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME 60000 /* 60 s, if no candidates */ 2068bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL 80 /* do more discard over 80% */ 20760b99b48SJaegeuk Kim #define DEF_CP_INTERVAL 60 /* 60 secs */ 208dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL 5 /* 5 secs */ 2094354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL 5 /* 5 secs */ 210db610a64SJaegeuk Kim #define DEF_DISABLE_QUICK_INTERVAL 1 /* 1 secs */ 21103f2c02dSJaegeuk Kim #define DEF_UMOUNT_DISCARD_TIMEOUT 5 /* 5 secs */ 212bba681cbSJaegeuk Kim 21375ab4cb8SJaegeuk Kim struct cp_control { 21475ab4cb8SJaegeuk Kim int reason; 2154b2fecc8SJaegeuk Kim __u64 trim_start; 2164b2fecc8SJaegeuk Kim __u64 trim_end; 2174b2fecc8SJaegeuk Kim __u64 trim_minlen; 21875ab4cb8SJaegeuk Kim }; 21975ab4cb8SJaegeuk Kim 220662befdaSChao Yu /* 221e1da7872SChao Yu * indicate meta/data type 222662befdaSChao Yu */ 223662befdaSChao Yu enum { 224662befdaSChao Yu META_CP, 225662befdaSChao Yu META_NAT, 22681c1a0f1SChao Yu META_SIT, 2274c521f49SJaegeuk Kim META_SSA, 228b63e7be5SChao Yu META_MAX, 2294c521f49SJaegeuk Kim META_POR, 23093770ab7SChao Yu DATA_GENERIC, /* check range only */ 23193770ab7SChao Yu DATA_GENERIC_ENHANCE, /* strong check on range and segment bitmap */ 23293770ab7SChao Yu DATA_GENERIC_ENHANCE_READ, /* 23393770ab7SChao Yu * strong check on range and segment 23493770ab7SChao Yu * bitmap but no warning due to race 23593770ab7SChao Yu * condition of read on truncated area 23693770ab7SChao Yu * by extent_cache 23793770ab7SChao Yu */ 238e1da7872SChao Yu META_GENERIC, 239662befdaSChao Yu }; 240662befdaSChao Yu 2416451e041SJaegeuk Kim /* for the list of ino */ 2426451e041SJaegeuk Kim enum { 2436451e041SJaegeuk Kim ORPHAN_INO, /* for orphan ino list */ 244fff04f90SJaegeuk Kim APPEND_INO, /* for append ino list */ 245fff04f90SJaegeuk Kim UPDATE_INO, /* for update ino list */ 2460a007b97SJaegeuk Kim TRANS_DIR_INO, /* for trasactions dir ino list */ 24739d787beSChao Yu FLUSH_INO, /* for multiple device flushing */ 2486451e041SJaegeuk Kim MAX_INO_ENTRY, /* max. list */ 2496451e041SJaegeuk Kim }; 2506451e041SJaegeuk Kim 2516451e041SJaegeuk Kim struct ino_entry { 25239a53e0cSJaegeuk Kim struct list_head list; /* list head */ 25339a53e0cSJaegeuk Kim nid_t ino; /* inode number */ 25439d787beSChao Yu unsigned int dirty_device; /* dirty device bitmap */ 25539a53e0cSJaegeuk Kim }; 25639a53e0cSJaegeuk Kim 2572710fd7eSChao Yu /* for the list of inodes to be GCed */ 25806292073SChao Yu struct inode_entry { 25939a53e0cSJaegeuk Kim struct list_head list; /* list head */ 26039a53e0cSJaegeuk Kim struct inode *inode; /* vfs inode pointer */ 26139a53e0cSJaegeuk Kim }; 26239a53e0cSJaegeuk Kim 26350fa53ecSChao Yu struct fsync_node_entry { 26450fa53ecSChao Yu struct list_head list; /* list head */ 26550fa53ecSChao Yu struct page *page; /* warm node page pointer */ 26650fa53ecSChao Yu unsigned int seq_id; /* sequence id */ 26750fa53ecSChao Yu }; 26850fa53ecSChao Yu 269a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */ 2707fd9e544SJaegeuk Kim struct discard_entry { 2717fd9e544SJaegeuk Kim struct list_head list; /* list head */ 272a7eeb823SChao Yu block_t start_blkaddr; /* start blockaddr of current segment */ 273a7eeb823SChao Yu unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */ 2747fd9e544SJaegeuk Kim }; 2757fd9e544SJaegeuk Kim 276969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */ 277969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY 16 278969d1b18SChao Yu 279ba48a33eSChao Yu /* max discard pend list number */ 280ba48a33eSChao Yu #define MAX_PLIST_NUM 512 281ba48a33eSChao Yu #define plist_idx(blk_num) ((blk_num) >= MAX_PLIST_NUM ? \ 2822f84babfSSheng Yong (MAX_PLIST_NUM - 1) : ((blk_num) - 1)) 283ba48a33eSChao Yu 28415469963SJaegeuk Kim enum { 28535ec7d57SChao Yu D_PREP, /* initial */ 28635ec7d57SChao Yu D_PARTIAL, /* partially submitted */ 28735ec7d57SChao Yu D_SUBMIT, /* all submitted */ 28835ec7d57SChao Yu D_DONE, /* finished */ 28915469963SJaegeuk Kim }; 29015469963SJaegeuk Kim 291004b6862SChao Yu struct discard_info { 292b01a9201SJaegeuk Kim block_t lstart; /* logical start address */ 293b01a9201SJaegeuk Kim block_t len; /* length */ 294004b6862SChao Yu block_t start; /* actual start address in dev */ 295004b6862SChao Yu }; 296004b6862SChao Yu 297b01a9201SJaegeuk Kim struct discard_cmd { 298004b6862SChao Yu struct rb_node rb_node; /* rb node located in rb-tree */ 299004b6862SChao Yu union { 300004b6862SChao Yu struct { 301004b6862SChao Yu block_t lstart; /* logical start address */ 302004b6862SChao Yu block_t len; /* length */ 303004b6862SChao Yu block_t start; /* actual start address in dev */ 304004b6862SChao Yu }; 305004b6862SChao Yu struct discard_info di; /* discard info */ 306004b6862SChao Yu 307004b6862SChao Yu }; 308b01a9201SJaegeuk Kim struct list_head list; /* command list */ 309b01a9201SJaegeuk Kim struct completion wait; /* compleation */ 310c81abe34SJaegeuk Kim struct block_device *bdev; /* bdev */ 311ec9895adSChao Yu unsigned short ref; /* reference count */ 3129a744b92SChao Yu unsigned char state; /* state */ 31372691af6SJaegeuk Kim unsigned char queued; /* queued discard */ 314c81abe34SJaegeuk Kim int error; /* bio error */ 31535ec7d57SChao Yu spinlock_t lock; /* for state/bio_ref updating */ 31635ec7d57SChao Yu unsigned short bio_ref; /* bio reference count */ 317275b66b0SChao Yu }; 318275b66b0SChao Yu 31978997b56SChao Yu enum { 32078997b56SChao Yu DPOLICY_BG, 32178997b56SChao Yu DPOLICY_FORCE, 32278997b56SChao Yu DPOLICY_FSTRIM, 32378997b56SChao Yu DPOLICY_UMOUNT, 32478997b56SChao Yu MAX_DPOLICY, 32578997b56SChao Yu }; 32678997b56SChao Yu 327ecc9aa00SChao Yu struct discard_policy { 32878997b56SChao Yu int type; /* type of discard */ 329ecc9aa00SChao Yu unsigned int min_interval; /* used for candidates exist */ 330f9d1dcedSYunlei He unsigned int mid_interval; /* used for device busy */ 331ecc9aa00SChao Yu unsigned int max_interval; /* used for candidates not exist */ 332ecc9aa00SChao Yu unsigned int max_requests; /* # of discards issued per round */ 333ecc9aa00SChao Yu unsigned int io_aware_gran; /* minimum granularity discard not be aware of I/O */ 334ecc9aa00SChao Yu bool io_aware; /* issue discard in idle time */ 335ecc9aa00SChao Yu bool sync; /* submit discard with REQ_SYNC flag */ 33620ee4382SChao Yu bool ordered; /* issue discard by lba order */ 3376ce48b0cSChao Yu bool timeout; /* discard timeout for put_super */ 33878997b56SChao Yu unsigned int granularity; /* discard granularity */ 339ecc9aa00SChao Yu }; 340ecc9aa00SChao Yu 3410b54fb84SJaegeuk Kim struct discard_cmd_control { 34215469963SJaegeuk Kim struct task_struct *f2fs_issue_discard; /* discard thread */ 34346f84c2cSChao Yu struct list_head entry_list; /* 4KB discard entry list */ 344ba48a33eSChao Yu struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */ 34546f84c2cSChao Yu struct list_head wait_list; /* store on-flushing entries */ 3468412663dSChao Yu struct list_head fstrim_list; /* in-flight discard from fstrim */ 34715469963SJaegeuk Kim wait_queue_head_t discard_wait_queue; /* waiting queue for wake-up */ 348969d1b18SChao Yu unsigned int discard_wake; /* to wake up discard thread */ 34915469963SJaegeuk Kim struct mutex cmd_lock; 350d618ebafSChao Yu unsigned int nr_discards; /* # of discards in the list */ 351d618ebafSChao Yu unsigned int max_discards; /* max. discards to be issued */ 352969d1b18SChao Yu unsigned int discard_granularity; /* discard granularity */ 353d84d1cbdSChao Yu unsigned int undiscard_blks; /* # of undiscard blocks */ 35420ee4382SChao Yu unsigned int next_pos; /* next discard position */ 3558b8dd65fSChao Yu atomic_t issued_discard; /* # of issued discard */ 35672691af6SJaegeuk Kim atomic_t queued_discard; /* # of queued discard */ 3575f32366aSChao Yu atomic_t discard_cmd_cnt; /* # of cached cmd count */ 3584dada3fdSChao Yu struct rb_root_cached root; /* root of discard rb-tree */ 35967fce70bSChao Yu bool rbtree_check; /* config for consistence check */ 36039a53e0cSJaegeuk Kim }; 36139a53e0cSJaegeuk Kim 36239a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */ 36339a53e0cSJaegeuk Kim struct fsync_inode_entry { 36439a53e0cSJaegeuk Kim struct list_head list; /* list head */ 36539a53e0cSJaegeuk Kim struct inode *inode; /* vfs inode pointer */ 366c52e1b10SJaegeuk Kim block_t blkaddr; /* block address locating the last fsync */ 367c52e1b10SJaegeuk Kim block_t last_dentry; /* block address locating the last dentry */ 36839a53e0cSJaegeuk Kim }; 36939a53e0cSJaegeuk Kim 37068afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl) (le16_to_cpu((jnl)->n_nats)) 37168afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl) (le16_to_cpu((jnl)->n_sits)) 37239a53e0cSJaegeuk Kim 37368afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i) ((jnl)->nat_j.entries[i].ne) 37468afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i) ((jnl)->nat_j.entries[i].nid) 37568afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i) ((jnl)->sit_j.entries[i].se) 37668afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i) ((jnl)->sit_j.entries[i].segno) 37739a53e0cSJaegeuk Kim 378dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl) (NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl)) 379dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl) (SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl)) 380309cc2b6SJaegeuk Kim 381dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i) 38239a53e0cSJaegeuk Kim { 383dfc08a12SChao Yu int before = nats_in_cursum(journal); 384cac5a3d8SDongOh Shin 385dfc08a12SChao Yu journal->n_nats = cpu_to_le16(before + i); 38639a53e0cSJaegeuk Kim return before; 38739a53e0cSJaegeuk Kim } 38839a53e0cSJaegeuk Kim 389dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i) 39039a53e0cSJaegeuk Kim { 391dfc08a12SChao Yu int before = sits_in_cursum(journal); 392cac5a3d8SDongOh Shin 393dfc08a12SChao Yu journal->n_sits = cpu_to_le16(before + i); 39439a53e0cSJaegeuk Kim return before; 39539a53e0cSJaegeuk Kim } 39639a53e0cSJaegeuk Kim 397dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal, 398dfc08a12SChao Yu int size, int type) 399184a5cd2SChao Yu { 400184a5cd2SChao Yu if (type == NAT_JOURNAL) 401dfc08a12SChao Yu return size <= MAX_NAT_JENTRIES(journal); 402dfc08a12SChao Yu return size <= MAX_SIT_JENTRIES(journal); 403184a5cd2SChao Yu } 404184a5cd2SChao Yu 405f2470371SChao Yu /* for inline stuff */ 406f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE 1 4077a2af766SChao Yu static inline int get_extra_isize(struct inode *inode); 4086afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode); 4097a2af766SChao Yu #define MAX_INLINE_DATA(inode) (sizeof(__le32) * \ 4107a2af766SChao Yu (CUR_ADDRS_PER_INODE(inode) - \ 411b323fd28SChao Yu get_inline_xattr_addrs(inode) - \ 4126afc662eSChao Yu DEF_INLINE_RESERVED_SIZE)) 413f2470371SChao Yu 414f2470371SChao Yu /* for inline dir */ 415f2470371SChao Yu #define NR_INLINE_DENTRY(inode) (MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \ 416f2470371SChao Yu ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \ 417f2470371SChao Yu BITS_PER_BYTE + 1)) 418f91108b8SGeert Uytterhoeven #define INLINE_DENTRY_BITMAP_SIZE(inode) \ 419f91108b8SGeert Uytterhoeven DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE) 420f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode) (MAX_INLINE_DATA(inode) - \ 421f2470371SChao Yu ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \ 422f2470371SChao Yu NR_INLINE_DENTRY(inode) + \ 423f2470371SChao Yu INLINE_DENTRY_BITMAP_SIZE(inode))) 424f2470371SChao Yu 425e9750824SNamjae Jeon /* 42639a53e0cSJaegeuk Kim * For INODE and NODE manager 42739a53e0cSJaegeuk Kim */ 4287b3cd7d6SJaegeuk Kim /* for directory operations */ 42943c780baSEric Biggers 43043c780baSEric Biggers struct f2fs_filename { 43143c780baSEric Biggers /* 43243c780baSEric Biggers * The filename the user specified. This is NULL for some 43343c780baSEric Biggers * filesystem-internal operations, e.g. converting an inline directory 43443c780baSEric Biggers * to a non-inline one, or roll-forward recovering an encrypted dentry. 43543c780baSEric Biggers */ 43643c780baSEric Biggers const struct qstr *usr_fname; 43743c780baSEric Biggers 43843c780baSEric Biggers /* 43943c780baSEric Biggers * The on-disk filename. For encrypted directories, this is encrypted. 44043c780baSEric Biggers * This may be NULL for lookups in an encrypted dir without the key. 44143c780baSEric Biggers */ 44243c780baSEric Biggers struct fscrypt_str disk_name; 44343c780baSEric Biggers 44443c780baSEric Biggers /* The dirhash of this filename */ 44543c780baSEric Biggers f2fs_hash_t hash; 44643c780baSEric Biggers 44743c780baSEric Biggers #ifdef CONFIG_FS_ENCRYPTION 44843c780baSEric Biggers /* 44943c780baSEric Biggers * For lookups in encrypted directories: either the buffer backing 45043c780baSEric Biggers * disk_name, or a buffer that holds the decoded no-key name. 45143c780baSEric Biggers */ 45243c780baSEric Biggers struct fscrypt_str crypto_buf; 45343c780baSEric Biggers #endif 45443c780baSEric Biggers #ifdef CONFIG_UNICODE 45543c780baSEric Biggers /* 45643c780baSEric Biggers * For casefolded directories: the casefolded name, but it's left NULL 4577ad08a58SDaniel Rosenberg * if the original name is not valid Unicode, if the directory is both 4587ad08a58SDaniel Rosenberg * casefolded and encrypted and its encryption key is unavailable, or if 4597ad08a58SDaniel Rosenberg * the filesystem is doing an internal operation where usr_fname is also 4607ad08a58SDaniel Rosenberg * NULL. In all these cases we fall back to treating the name as an 4617ad08a58SDaniel Rosenberg * opaque byte sequence. 46243c780baSEric Biggers */ 46343c780baSEric Biggers struct fscrypt_str cf_name; 46443c780baSEric Biggers #endif 46543c780baSEric Biggers }; 46643c780baSEric Biggers 4677b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr { 468d8c6822aSJaegeuk Kim struct inode *inode; 46976a9dd85SChao Yu void *bitmap; 4707b3cd7d6SJaegeuk Kim struct f2fs_dir_entry *dentry; 4717b3cd7d6SJaegeuk Kim __u8 (*filename)[F2FS_SLOT_LEN]; 4727b3cd7d6SJaegeuk Kim int max; 47376a9dd85SChao Yu int nr_bitmap; 4747b3cd7d6SJaegeuk Kim }; 4757b3cd7d6SJaegeuk Kim 47664c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode, 47764c24ecbSTomohiro Kusumi struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t) 4787b3cd7d6SJaegeuk Kim { 479d8c6822aSJaegeuk Kim d->inode = inode; 4807b3cd7d6SJaegeuk Kim d->max = NR_DENTRY_IN_BLOCK; 48176a9dd85SChao Yu d->nr_bitmap = SIZE_OF_DENTRY_BITMAP; 482c79d1520SYunlong Song d->bitmap = t->dentry_bitmap; 4837b3cd7d6SJaegeuk Kim d->dentry = t->dentry; 4847b3cd7d6SJaegeuk Kim d->filename = t->filename; 48564c24ecbSTomohiro Kusumi } 486cac5a3d8SDongOh Shin 48764c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode, 488f2470371SChao Yu struct f2fs_dentry_ptr *d, void *t) 48964c24ecbSTomohiro Kusumi { 490f2470371SChao Yu int entry_cnt = NR_INLINE_DENTRY(inode); 491f2470371SChao Yu int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode); 492f2470371SChao Yu int reserved_size = INLINE_RESERVED_SIZE(inode); 493f2470371SChao Yu 49464c24ecbSTomohiro Kusumi d->inode = inode; 495f2470371SChao Yu d->max = entry_cnt; 496f2470371SChao Yu d->nr_bitmap = bitmap_size; 497f2470371SChao Yu d->bitmap = t; 498f2470371SChao Yu d->dentry = t + bitmap_size + reserved_size; 499f2470371SChao Yu d->filename = t + bitmap_size + reserved_size + 500f2470371SChao Yu SIZE_OF_DIR_ENTRY * entry_cnt; 5017b3cd7d6SJaegeuk Kim } 5027b3cd7d6SJaegeuk Kim 503dbe6a5ffSJaegeuk Kim /* 504dbe6a5ffSJaegeuk Kim * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1 505dbe6a5ffSJaegeuk Kim * as its node offset to distinguish from index node blocks. 506dbe6a5ffSJaegeuk Kim * But some bits are used to mark the node block. 50739a53e0cSJaegeuk Kim */ 508dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \ 509dbe6a5ffSJaegeuk Kim >> OFFSET_BIT_SHIFT) 510266e97a8SJaegeuk Kim enum { 511266e97a8SJaegeuk Kim ALLOC_NODE, /* allocate a new node page if needed */ 512266e97a8SJaegeuk Kim LOOKUP_NODE, /* look up a node without readahead */ 513266e97a8SJaegeuk Kim LOOKUP_NODE_RA, /* 514266e97a8SJaegeuk Kim * look up a node with readahead called 5154f4124d0SChao Yu * by get_data_block. 51639a53e0cSJaegeuk Kim */ 517266e97a8SJaegeuk Kim }; 518266e97a8SJaegeuk Kim 5197735730dSChao Yu #define DEFAULT_RETRY_IO_COUNT 8 /* maximum retry read IO count */ 5207735730dSChao Yu 5215df7731fSChao Yu /* congestion wait timeout value, default: 20ms */ 5225df7731fSChao Yu #define DEFAULT_IO_TIMEOUT (msecs_to_jiffies(20)) 5235df7731fSChao Yu 524af033b2aSChao Yu /* maximum retry quota flush count */ 525af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT 8 526af033b2aSChao Yu 527a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */ 52839a53e0cSJaegeuk Kim 529817202d9SChao Yu #define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */ 530817202d9SChao Yu 53139a53e0cSJaegeuk Kim /* for in-memory extent cache entry */ 53213054c54SChao Yu #define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */ 53313054c54SChao Yu 53413054c54SChao Yu /* number of extent info in extent cache we try to shrink */ 53513054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER 128 536c11abd1aSJaegeuk Kim 53754c2258cSChao Yu struct rb_entry { 53854c2258cSChao Yu struct rb_node rb_node; /* rb node located in rb-tree */ 5392e9b2bb2SChao Yu union { 5402e9b2bb2SChao Yu struct { 54154c2258cSChao Yu unsigned int ofs; /* start offset of the entry */ 54254c2258cSChao Yu unsigned int len; /* length of the entry */ 54354c2258cSChao Yu }; 5442e9b2bb2SChao Yu unsigned long long key; /* 64-bits key */ 54548046cb5SJaegeuk Kim } __packed; 5462e9b2bb2SChao Yu }; 54754c2258cSChao Yu 54839a53e0cSJaegeuk Kim struct extent_info { 54939a53e0cSJaegeuk Kim unsigned int fofs; /* start offset in a file */ 550111d2495SMasanari Iida unsigned int len; /* length of the extent */ 55154c2258cSChao Yu u32 blk; /* start block address of the extent */ 55239a53e0cSJaegeuk Kim }; 55339a53e0cSJaegeuk Kim 55413054c54SChao Yu struct extent_node { 555c0362117SChao Yu struct rb_node rb_node; /* rb node located in rb-tree */ 55613054c54SChao Yu struct extent_info ei; /* extent info */ 55754c2258cSChao Yu struct list_head list; /* node in global extent list of sbi */ 558201ef5e0SHou Pengyang struct extent_tree *et; /* extent tree pointer */ 55913054c54SChao Yu }; 56013054c54SChao Yu 56113054c54SChao Yu struct extent_tree { 56213054c54SChao Yu nid_t ino; /* inode number */ 5634dada3fdSChao Yu struct rb_root_cached root; /* root of extent info rb-tree */ 56462c8af65SChao Yu struct extent_node *cached_en; /* recently accessed extent node */ 5653e72f721SJaegeuk Kim struct extent_info largest; /* largested extent info */ 566137d09f0SJaegeuk Kim struct list_head list; /* to be used by sbi->zombie_list */ 56713054c54SChao Yu rwlock_t lock; /* protect extent info rb-tree */ 56868e35385SChao Yu atomic_t node_cnt; /* # of extent node in rb-tree*/ 569b430f726SZhikang Zhang bool largest_updated; /* largest extent updated */ 57013054c54SChao Yu }; 57113054c54SChao Yu 57239a53e0cSJaegeuk Kim /* 573003a3e1dSJaegeuk Kim * This structure is taken from ext4_map_blocks. 574003a3e1dSJaegeuk Kim * 575003a3e1dSJaegeuk Kim * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks(). 576003a3e1dSJaegeuk Kim */ 577003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW (1 << BH_New) 578003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED (1 << BH_Mapped) 5797f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN (1 << BH_Unwritten) 5807f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\ 5817f63eb77SJaegeuk Kim F2FS_MAP_UNWRITTEN) 582003a3e1dSJaegeuk Kim 583003a3e1dSJaegeuk Kim struct f2fs_map_blocks { 584003a3e1dSJaegeuk Kim block_t m_pblk; 585003a3e1dSJaegeuk Kim block_t m_lblk; 586003a3e1dSJaegeuk Kim unsigned int m_len; 587003a3e1dSJaegeuk Kim unsigned int m_flags; 588da85985cSChao Yu pgoff_t *m_next_pgofs; /* point next possible non-hole pgofs */ 589c4020b2dSChao Yu pgoff_t *m_next_extent; /* point to next possible extent */ 590d5097be5SHyunchul Lee int m_seg_type; 591f9d6d059SChao Yu bool m_may_create; /* indicate it is from write path */ 592003a3e1dSJaegeuk Kim }; 593003a3e1dSJaegeuk Kim 594e2b4e2bcSChao Yu /* for flag in get_data_block */ 595f2220c7fSQiuyang Sun enum { 596f2220c7fSQiuyang Sun F2FS_GET_BLOCK_DEFAULT, 597f2220c7fSQiuyang Sun F2FS_GET_BLOCK_FIEMAP, 598f2220c7fSQiuyang Sun F2FS_GET_BLOCK_BMAP, 5990a4daae5SJaegeuk Kim F2FS_GET_BLOCK_DIO, 600f2220c7fSQiuyang Sun F2FS_GET_BLOCK_PRE_DIO, 601f2220c7fSQiuyang Sun F2FS_GET_BLOCK_PRE_AIO, 602c4020b2dSChao Yu F2FS_GET_BLOCK_PRECACHE, 603f2220c7fSQiuyang Sun }; 604e2b4e2bcSChao Yu 605003a3e1dSJaegeuk Kim /* 60639a53e0cSJaegeuk Kim * i_advise uses FADVISE_XXX_BIT. We can add additional hints later. 60739a53e0cSJaegeuk Kim */ 60839a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT 0x01 609354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT 0x02 610cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT 0x04 611e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT 0x08 61226787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT 0x10 613b6a06cbbSChao Yu #define FADVISE_HOT_BIT 0x20 61495ae251fSEric Biggers #define FADVISE_VERITY_BIT 0x40 61539a53e0cSJaegeuk Kim 616797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS (FADVISE_COLD_BIT | FADVISE_HOT_BIT) 617797c1cb5SChao Yu 618b5492af7SJaegeuk Kim #define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT) 619b5492af7SJaegeuk Kim #define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT) 620b5492af7SJaegeuk Kim #define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT) 621b5492af7SJaegeuk Kim #define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT) 622b5492af7SJaegeuk Kim #define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT) 623b5492af7SJaegeuk Kim #define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT) 624cde4de12SJaegeuk Kim #define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT) 625cde4de12SJaegeuk Kim #define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT) 626cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT) 627e7d55452SJaegeuk Kim #define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT) 628e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT) 62926787236SJaegeuk Kim #define file_keep_isize(inode) is_file(inode, FADVISE_KEEP_SIZE_BIT) 63026787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT) 631b6a06cbbSChao Yu #define file_is_hot(inode) is_file(inode, FADVISE_HOT_BIT) 632b6a06cbbSChao Yu #define file_set_hot(inode) set_file(inode, FADVISE_HOT_BIT) 633b6a06cbbSChao Yu #define file_clear_hot(inode) clear_file(inode, FADVISE_HOT_BIT) 63495ae251fSEric Biggers #define file_is_verity(inode) is_file(inode, FADVISE_VERITY_BIT) 63595ae251fSEric Biggers #define file_set_verity(inode) set_file(inode, FADVISE_VERITY_BIT) 636cde4de12SJaegeuk Kim 637ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL 0 638ab9fa662SJaegeuk Kim 6392ef79ecbSChao Yu enum { 6402ef79ecbSChao Yu GC_FAILURE_PIN, 6412ef79ecbSChao Yu GC_FAILURE_ATOMIC, 6422ef79ecbSChao Yu MAX_GC_FAILURE 6432ef79ecbSChao Yu }; 6442ef79ecbSChao Yu 6457653b9d8SChao Yu /* used for f2fs_inode_info->flags */ 6467653b9d8SChao Yu enum { 6477653b9d8SChao Yu FI_NEW_INODE, /* indicate newly allocated inode */ 6487653b9d8SChao Yu FI_DIRTY_INODE, /* indicate inode is dirty or not */ 6497653b9d8SChao Yu FI_AUTO_RECOVER, /* indicate inode is recoverable */ 6507653b9d8SChao Yu FI_DIRTY_DIR, /* indicate directory has dirty pages */ 6517653b9d8SChao Yu FI_INC_LINK, /* need to increment i_nlink */ 6527653b9d8SChao Yu FI_ACL_MODE, /* indicate acl mode */ 6537653b9d8SChao Yu FI_NO_ALLOC, /* should not allocate any blocks */ 6547653b9d8SChao Yu FI_FREE_NID, /* free allocated nide */ 6557653b9d8SChao Yu FI_NO_EXTENT, /* not to use the extent cache */ 6567653b9d8SChao Yu FI_INLINE_XATTR, /* used for inline xattr */ 6577653b9d8SChao Yu FI_INLINE_DATA, /* used for inline data*/ 6587653b9d8SChao Yu FI_INLINE_DENTRY, /* used for inline dentry */ 6597653b9d8SChao Yu FI_APPEND_WRITE, /* inode has appended data */ 6607653b9d8SChao Yu FI_UPDATE_WRITE, /* inode has in-place-update data */ 6617653b9d8SChao Yu FI_NEED_IPU, /* used for ipu per file */ 6627653b9d8SChao Yu FI_ATOMIC_FILE, /* indicate atomic file */ 6637653b9d8SChao Yu FI_ATOMIC_COMMIT, /* indicate the state of atomical committing */ 6647653b9d8SChao Yu FI_VOLATILE_FILE, /* indicate volatile file */ 6657653b9d8SChao Yu FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */ 6667653b9d8SChao Yu FI_DROP_CACHE, /* drop dirty page cache */ 6677653b9d8SChao Yu FI_DATA_EXIST, /* indicate data exists */ 6687653b9d8SChao Yu FI_INLINE_DOTS, /* indicate inline dot dentries */ 6697653b9d8SChao Yu FI_DO_DEFRAG, /* indicate defragment is running */ 6707653b9d8SChao Yu FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */ 6717653b9d8SChao Yu FI_NO_PREALLOC, /* indicate skipped preallocated blocks */ 6727653b9d8SChao Yu FI_HOT_DATA, /* indicate file is hot */ 6737653b9d8SChao Yu FI_EXTRA_ATTR, /* indicate file has extra attribute */ 6747653b9d8SChao Yu FI_PROJ_INHERIT, /* indicate file inherits projectid */ 6757653b9d8SChao Yu FI_PIN_FILE, /* indicate file should not be gced */ 6767653b9d8SChao Yu FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */ 6777653b9d8SChao Yu FI_VERITY_IN_PROGRESS, /* building fs-verity Merkle tree */ 6787653b9d8SChao Yu FI_COMPRESSED_FILE, /* indicate file's data can be compressed */ 679b28f047bSChao Yu FI_COMPRESS_CORRUPT, /* indicate compressed cluster is corrupted */ 6807653b9d8SChao Yu FI_MMAP_FILE, /* indicate file was mmapped */ 681602a16d5SDaeho Jeong FI_ENABLE_COMPRESS, /* enable compression in "user" compression mode */ 6827653b9d8SChao Yu FI_MAX, /* max flag, never be used */ 6837653b9d8SChao Yu }; 6847653b9d8SChao Yu 68539a53e0cSJaegeuk Kim struct f2fs_inode_info { 68639a53e0cSJaegeuk Kim struct inode vfs_inode; /* serve a vfs inode */ 68739a53e0cSJaegeuk Kim unsigned long i_flags; /* keep an inode flags for ioctl */ 68839a53e0cSJaegeuk Kim unsigned char i_advise; /* use to give file attribute hints */ 68938431545SJaegeuk Kim unsigned char i_dir_level; /* use for dentry level for large dir */ 6901ad71a27SJaegeuk Kim unsigned int i_current_depth; /* only for directory depth */ 6912ef79ecbSChao Yu /* for gc failure statistic */ 6922ef79ecbSChao Yu unsigned int i_gc_failures[MAX_GC_FAILURE]; 6936666e6aaSJaegeuk Kim unsigned int i_pino; /* parent inode number */ 69439a53e0cSJaegeuk Kim umode_t i_acl_mode; /* keep file acl mode temporarily */ 69539a53e0cSJaegeuk Kim 69639a53e0cSJaegeuk Kim /* Use below internally in f2fs*/ 6977653b9d8SChao Yu unsigned long flags[BITS_TO_LONGS(FI_MAX)]; /* use to pass per-file flags */ 698d928bfbfSJaegeuk Kim struct rw_semaphore i_sem; /* protect fi info */ 699204706c7SJaegeuk Kim atomic_t dirty_pages; /* # of dirty pages */ 70039a53e0cSJaegeuk Kim f2fs_hash_t chash; /* hash value of given file name */ 70139a53e0cSJaegeuk Kim unsigned int clevel; /* maximum level of given file name */ 70288c5c13aSJaegeuk Kim struct task_struct *task; /* lookup and create consistency */ 703b0af6d49SChao Yu struct task_struct *cp_task; /* separate cp/wb IO stats*/ 70439a53e0cSJaegeuk Kim nid_t i_xattr_nid; /* node id that contains xattrs */ 70526de9b11SJaegeuk Kim loff_t last_disk_size; /* lastly written file size */ 706c10c9820SChao Yu spinlock_t i_size_lock; /* protect last_disk_size */ 70788b88a66SJaegeuk Kim 7080abd675eSChao Yu #ifdef CONFIG_QUOTA 7090abd675eSChao Yu struct dquot *i_dquot[MAXQUOTAS]; 7100abd675eSChao Yu 7110abd675eSChao Yu /* quota space reservation, managed internally by quota code */ 7120abd675eSChao Yu qsize_t i_reserved_quota; 7130abd675eSChao Yu #endif 7140f18b462SJaegeuk Kim struct list_head dirty_list; /* dirty list for dirs and files */ 7150f18b462SJaegeuk Kim struct list_head gdirty_list; /* linked in global dirty list */ 71657864ae5SJaegeuk Kim struct list_head inmem_ilist; /* list for inmem inodes */ 71788b88a66SJaegeuk Kim struct list_head inmem_pages; /* inmemory pages managed by f2fs */ 7187a10f017SJaegeuk Kim struct task_struct *inmem_task; /* store inmemory task */ 71988b88a66SJaegeuk Kim struct mutex inmem_lock; /* lock for inmemory pages */ 7206b12367dSJaegeuk Kim pgoff_t ra_offset; /* ongoing readahead offset */ 7213e72f721SJaegeuk Kim struct extent_tree *extent_tree; /* cached extent_tree entry */ 722b2532c69SChao Yu 723b2532c69SChao Yu /* avoid racing between foreground op and gc */ 724b2532c69SChao Yu struct rw_semaphore i_gc_rwsem[2]; 7255a3a2d83SQiuyang Sun struct rw_semaphore i_mmap_sem; 72627161f13SYunlei He struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */ 727f2470371SChao Yu 7287a2af766SChao Yu int i_extra_isize; /* size of extra space located in i_addr */ 7295c57132eSChao Yu kprojid_t i_projid; /* id for project quota */ 7306afc662eSChao Yu int i_inline_xattr_size; /* inline xattr size */ 73124b81dfcSArnd Bergmann struct timespec64 i_crtime; /* inode creation time */ 73224b81dfcSArnd Bergmann struct timespec64 i_disk_time[4];/* inode disk times */ 7334c8ff709SChao Yu 7344c8ff709SChao Yu /* for file compress */ 735c2759ebaSDaeho Jeong atomic_t i_compr_blocks; /* # of compressed blocks */ 7364c8ff709SChao Yu unsigned char i_compress_algorithm; /* algorithm type */ 7374c8ff709SChao Yu unsigned char i_log_cluster_size; /* log of cluster size */ 738b28f047bSChao Yu unsigned short i_compress_flag; /* compress flag */ 7394c8ff709SChao Yu unsigned int i_cluster_size; /* cluster size */ 74039a53e0cSJaegeuk Kim }; 74139a53e0cSJaegeuk Kim 74239a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext, 743bd933d4fSChao Yu struct f2fs_extent *i_ext) 74439a53e0cSJaegeuk Kim { 745bd933d4fSChao Yu ext->fofs = le32_to_cpu(i_ext->fofs); 746bd933d4fSChao Yu ext->blk = le32_to_cpu(i_ext->blk); 747bd933d4fSChao Yu ext->len = le32_to_cpu(i_ext->len); 74839a53e0cSJaegeuk Kim } 74939a53e0cSJaegeuk Kim 75039a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext, 75139a53e0cSJaegeuk Kim struct f2fs_extent *i_ext) 75239a53e0cSJaegeuk Kim { 75339a53e0cSJaegeuk Kim i_ext->fofs = cpu_to_le32(ext->fofs); 7544d0b0bd4SChao Yu i_ext->blk = cpu_to_le32(ext->blk); 75539a53e0cSJaegeuk Kim i_ext->len = cpu_to_le32(ext->len); 75639a53e0cSJaegeuk Kim } 75739a53e0cSJaegeuk Kim 758429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs, 759429511cdSChao Yu u32 blk, unsigned int len) 760429511cdSChao Yu { 761429511cdSChao Yu ei->fofs = fofs; 762429511cdSChao Yu ei->blk = blk; 763429511cdSChao Yu ei->len = len; 764429511cdSChao Yu } 765429511cdSChao Yu 766004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back, 76735ec7d57SChao Yu struct discard_info *front, unsigned int max_len) 768004b6862SChao Yu { 7699a997188SJaegeuk Kim return (back->lstart + back->len == front->lstart) && 77035ec7d57SChao Yu (back->len + front->len <= max_len); 771004b6862SChao Yu } 772004b6862SChao Yu 773004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur, 77435ec7d57SChao Yu struct discard_info *back, unsigned int max_len) 775004b6862SChao Yu { 77635ec7d57SChao Yu return __is_discard_mergeable(back, cur, max_len); 777004b6862SChao Yu } 778004b6862SChao Yu 779004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur, 78035ec7d57SChao Yu struct discard_info *front, unsigned int max_len) 781004b6862SChao Yu { 78235ec7d57SChao Yu return __is_discard_mergeable(cur, front, max_len); 783004b6862SChao Yu } 784004b6862SChao Yu 785429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back, 786429511cdSChao Yu struct extent_info *front) 787429511cdSChao Yu { 788429511cdSChao Yu return (back->fofs + back->len == front->fofs && 789429511cdSChao Yu back->blk + back->len == front->blk); 790429511cdSChao Yu } 791429511cdSChao Yu 792429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur, 793429511cdSChao Yu struct extent_info *back) 794429511cdSChao Yu { 795429511cdSChao Yu return __is_extent_mergeable(back, cur); 796429511cdSChao Yu } 797429511cdSChao Yu 798429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur, 799429511cdSChao Yu struct extent_info *front) 800429511cdSChao Yu { 801429511cdSChao Yu return __is_extent_mergeable(cur, front); 802429511cdSChao Yu } 803429511cdSChao Yu 804cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync); 805b430f726SZhikang Zhang static inline void __try_update_largest_extent(struct extent_tree *et, 806b430f726SZhikang Zhang struct extent_node *en) 8074abd3f5aSChao Yu { 808205b9822SJaegeuk Kim if (en->ei.len > et->largest.len) { 8094abd3f5aSChao Yu et->largest = en->ei; 810b430f726SZhikang Zhang et->largest_updated = true; 811205b9822SJaegeuk Kim } 8124abd3f5aSChao Yu } 8134abd3f5aSChao Yu 8149a4ffdf5SChao Yu /* 8159a4ffdf5SChao Yu * For free nid management 8169a4ffdf5SChao Yu */ 8179a4ffdf5SChao Yu enum nid_state { 8189a4ffdf5SChao Yu FREE_NID, /* newly added to free nid list */ 8199a4ffdf5SChao Yu PREALLOC_NID, /* it is preallocated */ 8209a4ffdf5SChao Yu MAX_NID_STATE, 821b8559dc2SChao Yu }; 822b8559dc2SChao Yu 823a95ba66aSJaegeuk Kim enum nat_state { 824a95ba66aSJaegeuk Kim TOTAL_NAT, 825a95ba66aSJaegeuk Kim DIRTY_NAT, 826a95ba66aSJaegeuk Kim RECLAIMABLE_NAT, 827a95ba66aSJaegeuk Kim MAX_NAT_STATE, 828a95ba66aSJaegeuk Kim }; 829a95ba66aSJaegeuk Kim 83039a53e0cSJaegeuk Kim struct f2fs_nm_info { 83139a53e0cSJaegeuk Kim block_t nat_blkaddr; /* base disk address of NAT */ 83239a53e0cSJaegeuk Kim nid_t max_nid; /* maximum possible node ids */ 83304d47e67SChao Yu nid_t available_nids; /* # of available node ids */ 83439a53e0cSJaegeuk Kim nid_t next_scan_nid; /* the next nid to be scanned */ 835cdfc41c1SJaegeuk Kim unsigned int ram_thresh; /* control the memory footprint */ 836ea1a29a0SChao Yu unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */ 8372304cb0cSChao Yu unsigned int dirty_nats_ratio; /* control dirty nats ratio threshold */ 83839a53e0cSJaegeuk Kim 83939a53e0cSJaegeuk Kim /* NAT cache management */ 84039a53e0cSJaegeuk Kim struct radix_tree_root nat_root;/* root of the nat entry cache */ 841309cc2b6SJaegeuk Kim struct radix_tree_root nat_set_root;/* root of the nat set cache */ 842b873b798SJaegeuk Kim struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */ 84339a53e0cSJaegeuk Kim struct list_head nat_entries; /* cached nat entry list (clean) */ 84422969158SChao Yu spinlock_t nat_list_lock; /* protect clean nat entry list */ 845a95ba66aSJaegeuk Kim unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */ 84622ad0b6aSJaegeuk Kim unsigned int nat_blocks; /* # of nat blocks */ 84739a53e0cSJaegeuk Kim 84839a53e0cSJaegeuk Kim /* free node ids management */ 8498a7ed66aSJaegeuk Kim struct radix_tree_root free_nid_root;/* root of the free_nid cache */ 8509a4ffdf5SChao Yu struct list_head free_nid_list; /* list for free nids excluding preallocated nids */ 8519a4ffdf5SChao Yu unsigned int nid_cnt[MAX_NID_STATE]; /* the number of free node id */ 852b8559dc2SChao Yu spinlock_t nid_list_lock; /* protect nid lists ops */ 85339a53e0cSJaegeuk Kim struct mutex build_lock; /* lock for build free nids */ 854bb1105e4SJaegeuk Kim unsigned char **free_nid_bitmap; 8554ac91242SChao Yu unsigned char *nat_block_bitmap; 856586d1492SChao Yu unsigned short *free_nid_count; /* free nid count of NAT block */ 85739a53e0cSJaegeuk Kim 85839a53e0cSJaegeuk Kim /* for checkpoint */ 85939a53e0cSJaegeuk Kim char *nat_bitmap; /* NAT bitmap pointer */ 86022ad0b6aSJaegeuk Kim 86122ad0b6aSJaegeuk Kim unsigned int nat_bits_blocks; /* # of nat bits blocks */ 86222ad0b6aSJaegeuk Kim unsigned char *nat_bits; /* NAT bits blocks */ 86322ad0b6aSJaegeuk Kim unsigned char *full_nat_bits; /* full NAT pages */ 86422ad0b6aSJaegeuk Kim unsigned char *empty_nat_bits; /* empty NAT pages */ 865599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS 866599a09b2SChao Yu char *nat_bitmap_mir; /* NAT bitmap mirror */ 867599a09b2SChao Yu #endif 86839a53e0cSJaegeuk Kim int bitmap_size; /* bitmap size */ 86939a53e0cSJaegeuk Kim }; 87039a53e0cSJaegeuk Kim 87139a53e0cSJaegeuk Kim /* 87239a53e0cSJaegeuk Kim * this structure is used as one of function parameters. 87339a53e0cSJaegeuk Kim * all the information are dedicated to a given direct node block determined 87439a53e0cSJaegeuk Kim * by the data offset in a file. 87539a53e0cSJaegeuk Kim */ 87639a53e0cSJaegeuk Kim struct dnode_of_data { 87739a53e0cSJaegeuk Kim struct inode *inode; /* vfs inode pointer */ 87839a53e0cSJaegeuk Kim struct page *inode_page; /* its inode page, NULL is possible */ 87939a53e0cSJaegeuk Kim struct page *node_page; /* cached direct node page */ 88039a53e0cSJaegeuk Kim nid_t nid; /* node id of the direct node block */ 88139a53e0cSJaegeuk Kim unsigned int ofs_in_node; /* data offset in the node page */ 88239a53e0cSJaegeuk Kim bool inode_page_locked; /* inode page is locked or not */ 88393bae099SJaegeuk Kim bool node_changed; /* is node block changed */ 8843cf45747SChao Yu char cur_level; /* level of hole node page */ 8853cf45747SChao Yu char max_level; /* level of current page located */ 88639a53e0cSJaegeuk Kim block_t data_blkaddr; /* block address of the node block */ 88739a53e0cSJaegeuk Kim }; 88839a53e0cSJaegeuk Kim 88939a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode, 89039a53e0cSJaegeuk Kim struct page *ipage, struct page *npage, nid_t nid) 89139a53e0cSJaegeuk Kim { 892d66d1f76SJaegeuk Kim memset(dn, 0, sizeof(*dn)); 89339a53e0cSJaegeuk Kim dn->inode = inode; 89439a53e0cSJaegeuk Kim dn->inode_page = ipage; 89539a53e0cSJaegeuk Kim dn->node_page = npage; 89639a53e0cSJaegeuk Kim dn->nid = nid; 89739a53e0cSJaegeuk Kim } 89839a53e0cSJaegeuk Kim 89939a53e0cSJaegeuk Kim /* 90039a53e0cSJaegeuk Kim * For SIT manager 90139a53e0cSJaegeuk Kim * 90239a53e0cSJaegeuk Kim * By default, there are 6 active log areas across the whole main area. 90339a53e0cSJaegeuk Kim * When considering hot and cold data separation to reduce cleaning overhead, 90439a53e0cSJaegeuk Kim * we split 3 for data logs and 3 for node logs as hot, warm, and cold types, 90539a53e0cSJaegeuk Kim * respectively. 90639a53e0cSJaegeuk Kim * In the current design, you should not change the numbers intentionally. 90739a53e0cSJaegeuk Kim * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6 90839a53e0cSJaegeuk Kim * logs individually according to the underlying devices. (default: 6) 90939a53e0cSJaegeuk Kim * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for 91039a53e0cSJaegeuk Kim * data and 8 for node logs. 91139a53e0cSJaegeuk Kim */ 91239a53e0cSJaegeuk Kim #define NR_CURSEG_DATA_TYPE (3) 91339a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE (3) 914093749e2SChao Yu #define NR_CURSEG_INMEM_TYPE (2) 915d0b9e42aSChao Yu #define NR_CURSEG_PERSIST_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE) 916d0b9e42aSChao Yu #define NR_CURSEG_TYPE (NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE) 91739a53e0cSJaegeuk Kim 91839a53e0cSJaegeuk Kim enum { 91939a53e0cSJaegeuk Kim CURSEG_HOT_DATA = 0, /* directory entry blocks */ 92039a53e0cSJaegeuk Kim CURSEG_WARM_DATA, /* data blocks */ 92139a53e0cSJaegeuk Kim CURSEG_COLD_DATA, /* multimedia or GCed data blocks */ 92239a53e0cSJaegeuk Kim CURSEG_HOT_NODE, /* direct node blocks of directory files */ 92339a53e0cSJaegeuk Kim CURSEG_WARM_NODE, /* direct node blocks of normal files */ 92439a53e0cSJaegeuk Kim CURSEG_COLD_NODE, /* indirect node blocks */ 925d0b9e42aSChao Yu NR_PERSISTENT_LOG, /* number of persistent log */ 926d0b9e42aSChao Yu CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG, 927d0b9e42aSChao Yu /* pinned file that needs consecutive block address */ 928093749e2SChao Yu CURSEG_ALL_DATA_ATGC, /* SSR alloctor in hot/warm/cold data area */ 929d0b9e42aSChao Yu NO_CHECK_TYPE, /* number of persistent & inmem log */ 93039a53e0cSJaegeuk Kim }; 93139a53e0cSJaegeuk Kim 9326b4afdd7SJaegeuk Kim struct flush_cmd { 9336b4afdd7SJaegeuk Kim struct completion wait; 934721bd4d5SGu Zheng struct llist_node llnode; 93539d787beSChao Yu nid_t ino; 9366b4afdd7SJaegeuk Kim int ret; 9376b4afdd7SJaegeuk Kim }; 9386b4afdd7SJaegeuk Kim 939a688b9d9SGu Zheng struct flush_cmd_control { 940a688b9d9SGu Zheng struct task_struct *f2fs_issue_flush; /* flush thread */ 941a688b9d9SGu Zheng wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */ 9428b8dd65fSChao Yu atomic_t issued_flush; /* # of issued flushes */ 94372691af6SJaegeuk Kim atomic_t queued_flush; /* # of queued flushes */ 944721bd4d5SGu Zheng struct llist_head issue_list; /* list for command issue */ 945721bd4d5SGu Zheng struct llist_node *dispatch_list; /* list for command dispatch */ 946a688b9d9SGu Zheng }; 947a688b9d9SGu Zheng 94839a53e0cSJaegeuk Kim struct f2fs_sm_info { 94939a53e0cSJaegeuk Kim struct sit_info *sit_info; /* whole segment information */ 95039a53e0cSJaegeuk Kim struct free_segmap_info *free_info; /* free segment information */ 95139a53e0cSJaegeuk Kim struct dirty_seglist_info *dirty_info; /* dirty segment information */ 95239a53e0cSJaegeuk Kim struct curseg_info *curseg_array; /* active segment information */ 95339a53e0cSJaegeuk Kim 9542b60311dSChao Yu struct rw_semaphore curseg_lock; /* for preventing curseg change */ 9552b60311dSChao Yu 95639a53e0cSJaegeuk Kim block_t seg0_blkaddr; /* block address of 0'th segment */ 95739a53e0cSJaegeuk Kim block_t main_blkaddr; /* start block address of main area */ 95839a53e0cSJaegeuk Kim block_t ssa_blkaddr; /* start block address of SSA area */ 95939a53e0cSJaegeuk Kim 96039a53e0cSJaegeuk Kim unsigned int segment_count; /* total # of segments */ 96139a53e0cSJaegeuk Kim unsigned int main_segments; /* # of segments in main area */ 96239a53e0cSJaegeuk Kim unsigned int reserved_segments; /* # of reserved segments */ 96339a53e0cSJaegeuk Kim unsigned int ovp_segments; /* # of overprovision segments */ 96481eb8d6eSJaegeuk Kim 96581eb8d6eSJaegeuk Kim /* a threshold to reclaim prefree segments */ 96681eb8d6eSJaegeuk Kim unsigned int rec_prefree_segments; 9677fd9e544SJaegeuk Kim 968bba681cbSJaegeuk Kim /* for batched trimming */ 969bba681cbSJaegeuk Kim unsigned int trim_sections; /* # of sections to trim */ 970bba681cbSJaegeuk Kim 971184a5cd2SChao Yu struct list_head sit_entry_set; /* sit entry set list */ 972184a5cd2SChao Yu 973216fbd64SJaegeuk Kim unsigned int ipu_policy; /* in-place-update policy */ 974216fbd64SJaegeuk Kim unsigned int min_ipu_util; /* in-place-update threshold */ 975c1ce1b02SJaegeuk Kim unsigned int min_fsync_blocks; /* threshold for fsync */ 976853137ceSJaegeuk Kim unsigned int min_seq_blocks; /* threshold for sequential blocks */ 977ef095d19SJaegeuk Kim unsigned int min_hot_blocks; /* threshold for hot block allocation */ 978a2a12b67SChao Yu unsigned int min_ssr_sections; /* threshold to trigger SSR allocation */ 9796b4afdd7SJaegeuk Kim 9806b4afdd7SJaegeuk Kim /* for flush command control */ 981b01a9201SJaegeuk Kim struct flush_cmd_control *fcc_info; 982a688b9d9SGu Zheng 9830b54fb84SJaegeuk Kim /* for discard command control */ 9840b54fb84SJaegeuk Kim struct discard_cmd_control *dcc_info; 98539a53e0cSJaegeuk Kim }; 98639a53e0cSJaegeuk Kim 98739a53e0cSJaegeuk Kim /* 98839a53e0cSJaegeuk Kim * For superblock 98939a53e0cSJaegeuk Kim */ 99039a53e0cSJaegeuk Kim /* 99139a53e0cSJaegeuk Kim * COUNT_TYPE for monitoring 99239a53e0cSJaegeuk Kim * 99339a53e0cSJaegeuk Kim * f2fs monitors the number of several block types such as on-writeback, 99439a53e0cSJaegeuk Kim * dirty dentry blocks, dirty node blocks, and dirty meta blocks. 99539a53e0cSJaegeuk Kim */ 99636951b38SChao Yu #define WB_DATA_TYPE(p) (__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA) 99739a53e0cSJaegeuk Kim enum count_type { 99839a53e0cSJaegeuk Kim F2FS_DIRTY_DENTS, 999c227f912SChao Yu F2FS_DIRTY_DATA, 10002c8a4a28SJaegeuk Kim F2FS_DIRTY_QDATA, 100139a53e0cSJaegeuk Kim F2FS_DIRTY_NODES, 100239a53e0cSJaegeuk Kim F2FS_DIRTY_META, 10038dcf2ff7SJaegeuk Kim F2FS_INMEM_PAGES, 10040f18b462SJaegeuk Kim F2FS_DIRTY_IMETA, 100536951b38SChao Yu F2FS_WB_CP_DATA, 100636951b38SChao Yu F2FS_WB_DATA, 10075f9abab4SJaegeuk Kim F2FS_RD_DATA, 10085f9abab4SJaegeuk Kim F2FS_RD_NODE, 10095f9abab4SJaegeuk Kim F2FS_RD_META, 101002b16d0aSChao Yu F2FS_DIO_WRITE, 101102b16d0aSChao Yu F2FS_DIO_READ, 101239a53e0cSJaegeuk Kim NR_COUNT_TYPE, 101339a53e0cSJaegeuk Kim }; 101439a53e0cSJaegeuk Kim 101539a53e0cSJaegeuk Kim /* 1016e1c42045Sarter97 * The below are the page types of bios used in submit_bio(). 101739a53e0cSJaegeuk Kim * The available types are: 101839a53e0cSJaegeuk Kim * DATA User data pages. It operates as async mode. 101939a53e0cSJaegeuk Kim * NODE Node pages. It operates as async mode. 102039a53e0cSJaegeuk Kim * META FS metadata pages such as SIT, NAT, CP. 102139a53e0cSJaegeuk Kim * NR_PAGE_TYPE The number of page types. 102239a53e0cSJaegeuk Kim * META_FLUSH Make sure the previous pages are written 102339a53e0cSJaegeuk Kim * with waiting the bio's completion 102439a53e0cSJaegeuk Kim * ... Only can be used with META. 102539a53e0cSJaegeuk Kim */ 10267d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type)) 102739a53e0cSJaegeuk Kim enum page_type { 102839a53e0cSJaegeuk Kim DATA, 102939a53e0cSJaegeuk Kim NODE, 103039a53e0cSJaegeuk Kim META, 103139a53e0cSJaegeuk Kim NR_PAGE_TYPE, 103239a53e0cSJaegeuk Kim META_FLUSH, 10338ce67cb0SJaegeuk Kim INMEM, /* the below types are used by tracepoints only. */ 10348ce67cb0SJaegeuk Kim INMEM_DROP, 10358c242db9SJaegeuk Kim INMEM_INVALIDATE, 103628bc106bSChao Yu INMEM_REVOKE, 10378ce67cb0SJaegeuk Kim IPU, 10388ce67cb0SJaegeuk Kim OPU, 103939a53e0cSJaegeuk Kim }; 104039a53e0cSJaegeuk Kim 1041a912b54dSJaegeuk Kim enum temp_type { 1042a912b54dSJaegeuk Kim HOT = 0, /* must be zero for meta bio */ 1043a912b54dSJaegeuk Kim WARM, 1044a912b54dSJaegeuk Kim COLD, 1045a912b54dSJaegeuk Kim NR_TEMP_TYPE, 1046a912b54dSJaegeuk Kim }; 1047a912b54dSJaegeuk Kim 1048cc15620bSJaegeuk Kim enum need_lock_type { 1049cc15620bSJaegeuk Kim LOCK_REQ = 0, 1050cc15620bSJaegeuk Kim LOCK_DONE, 1051cc15620bSJaegeuk Kim LOCK_RETRY, 1052cc15620bSJaegeuk Kim }; 1053cc15620bSJaegeuk Kim 1054a5fd5050SChao Yu enum cp_reason_type { 1055a5fd5050SChao Yu CP_NO_NEEDED, 1056a5fd5050SChao Yu CP_NON_REGULAR, 10574c8ff709SChao Yu CP_COMPRESSED, 1058a5fd5050SChao Yu CP_HARDLINK, 1059a5fd5050SChao Yu CP_SB_NEED_CP, 1060a5fd5050SChao Yu CP_WRONG_PINO, 1061a5fd5050SChao Yu CP_NO_SPC_ROLL, 1062a5fd5050SChao Yu CP_NODE_NEED_CP, 1063a5fd5050SChao Yu CP_FASTBOOT_MODE, 1064a5fd5050SChao Yu CP_SPEC_LOG_NUM, 10650a007b97SJaegeuk Kim CP_RECOVER_DIR, 1066a5fd5050SChao Yu }; 1067a5fd5050SChao Yu 1068b0af6d49SChao Yu enum iostat_type { 10698b83ac81SChao Yu /* WRITE IO */ 10708b83ac81SChao Yu APP_DIRECT_IO, /* app direct write IOs */ 10718b83ac81SChao Yu APP_BUFFERED_IO, /* app buffered write IOs */ 1072b0af6d49SChao Yu APP_WRITE_IO, /* app write IOs */ 1073b0af6d49SChao Yu APP_MAPPED_IO, /* app mapped IOs */ 1074b0af6d49SChao Yu FS_DATA_IO, /* data IOs from kworker/fsync/reclaimer */ 1075b0af6d49SChao Yu FS_NODE_IO, /* node IOs from kworker/fsync/reclaimer */ 1076b0af6d49SChao Yu FS_META_IO, /* meta IOs from kworker/reclaimer */ 1077b0af6d49SChao Yu FS_GC_DATA_IO, /* data IOs from forground gc */ 1078b0af6d49SChao Yu FS_GC_NODE_IO, /* node IOs from forground gc */ 1079b0af6d49SChao Yu FS_CP_DATA_IO, /* data IOs from checkpoint */ 1080b0af6d49SChao Yu FS_CP_NODE_IO, /* node IOs from checkpoint */ 1081b0af6d49SChao Yu FS_CP_META_IO, /* meta IOs from checkpoint */ 10828b83ac81SChao Yu 10838b83ac81SChao Yu /* READ IO */ 10848b83ac81SChao Yu APP_DIRECT_READ_IO, /* app direct read IOs */ 10858b83ac81SChao Yu APP_BUFFERED_READ_IO, /* app buffered read IOs */ 10868b83ac81SChao Yu APP_READ_IO, /* app read IOs */ 10878b83ac81SChao Yu APP_MAPPED_READ_IO, /* app mapped read IOs */ 10888b83ac81SChao Yu FS_DATA_READ_IO, /* data read IOs */ 10899c122384SChao Yu FS_GDATA_READ_IO, /* data read IOs from background gc */ 10909c122384SChao Yu FS_CDATA_READ_IO, /* compressed data read IOs */ 10918b83ac81SChao Yu FS_NODE_READ_IO, /* node read IOs */ 10928b83ac81SChao Yu FS_META_READ_IO, /* meta read IOs */ 10938b83ac81SChao Yu 10948b83ac81SChao Yu /* other */ 1095b0af6d49SChao Yu FS_DISCARD, /* discard */ 1096b0af6d49SChao Yu NR_IO_TYPE, 1097b0af6d49SChao Yu }; 1098b0af6d49SChao Yu 1099458e6197SJaegeuk Kim struct f2fs_io_info { 110005ca3632SJaegeuk Kim struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */ 110139d787beSChao Yu nid_t ino; /* inode number */ 1102458e6197SJaegeuk Kim enum page_type type; /* contains DATA/NODE/META/META_FLUSH */ 1103a912b54dSJaegeuk Kim enum temp_type temp; /* contains HOT/WARM/COLD */ 110404d328deSMike Christie int op; /* contains REQ_OP_ */ 1105ef295ecfSChristoph Hellwig int op_flags; /* req_flag_bits */ 11067a9d7548SChao Yu block_t new_blkaddr; /* new block address to be written */ 110728bc106bSChao Yu block_t old_blkaddr; /* old block address before Cow */ 110805ca3632SJaegeuk Kim struct page *page; /* page to be written */ 11094375a336SJaegeuk Kim struct page *encrypted_page; /* encrypted page */ 11104c8ff709SChao Yu struct page *compressed_page; /* compressed page */ 1111fb830fc5SChao Yu struct list_head list; /* serialize IOs */ 1112d68f735bSJaegeuk Kim bool submitted; /* indicate IO submission */ 1113cc15620bSJaegeuk Kim int need_lock; /* indicate we need to lock cp_rwsem */ 1114fb830fc5SChao Yu bool in_list; /* indicate fio is in io_list */ 11156dc3a126SChao Yu bool is_por; /* indicate IO is from recovery or not */ 1116fe16efe6SChao Yu bool retry; /* need to reallocate block address */ 11174c8ff709SChao Yu int compr_blocks; /* # of compressed block addresses */ 11184c8ff709SChao Yu bool encrypted; /* indicate file is encrypted */ 1119b0af6d49SChao Yu enum iostat_type io_type; /* io type */ 1120578c6478SYufen Yu struct writeback_control *io_wbc; /* writeback control */ 11218648de2cSChao Yu struct bio **bio; /* bio for ipu */ 11228648de2cSChao Yu sector_t *last_block; /* last block number in bio */ 11237735730dSChao Yu unsigned char version; /* version of the node */ 1124458e6197SJaegeuk Kim }; 1125458e6197SJaegeuk Kim 11260b20fcecSChao Yu struct bio_entry { 11270b20fcecSChao Yu struct bio *bio; 11280b20fcecSChao Yu struct list_head list; 11290b20fcecSChao Yu }; 11300b20fcecSChao Yu 113168afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ) 11321ff7bd3bSJaegeuk Kim struct f2fs_bio_info { 1133458e6197SJaegeuk Kim struct f2fs_sb_info *sbi; /* f2fs superblock */ 11341ff7bd3bSJaegeuk Kim struct bio *bio; /* bios to merge */ 11351ff7bd3bSJaegeuk Kim sector_t last_block_in_bio; /* last block number */ 1136458e6197SJaegeuk Kim struct f2fs_io_info fio; /* store buffered io info. */ 1137df0f8dc0SChao Yu struct rw_semaphore io_rwsem; /* blocking op for bio */ 1138fb830fc5SChao Yu spinlock_t io_lock; /* serialize DATA/NODE IOs */ 1139fb830fc5SChao Yu struct list_head io_list; /* track fios */ 11400b20fcecSChao Yu struct list_head bio_list; /* bio entry list head */ 11410b20fcecSChao Yu struct rw_semaphore bio_list_lock; /* lock to protect bio entry list */ 11421ff7bd3bSJaegeuk Kim }; 11431ff7bd3bSJaegeuk Kim 11443c62be17SJaegeuk Kim #define FDEV(i) (sbi->devs[i]) 11453c62be17SJaegeuk Kim #define RDEV(i) (raw_super->devs[i]) 11463c62be17SJaegeuk Kim struct f2fs_dev_info { 11473c62be17SJaegeuk Kim struct block_device *bdev; 11483c62be17SJaegeuk Kim char path[MAX_PATH_LEN]; 11493c62be17SJaegeuk Kim unsigned int total_segments; 11503c62be17SJaegeuk Kim block_t start_blk; 11513c62be17SJaegeuk Kim block_t end_blk; 11523c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED 11533c62be17SJaegeuk Kim unsigned int nr_blkz; /* Total number of zones */ 115495175dafSDamien Le Moal unsigned long *blkz_seq; /* Bitmap indicating sequential zones */ 1155de881df9SAravind Ramesh block_t *zone_capacity_blocks; /* Array of zone capacity in blks */ 11563c62be17SJaegeuk Kim #endif 11573c62be17SJaegeuk Kim }; 11583c62be17SJaegeuk Kim 1159c227f912SChao Yu enum inode_type { 1160c227f912SChao Yu DIR_INODE, /* for dirty dir inode */ 1161c227f912SChao Yu FILE_INODE, /* for dirty regular/symlink inode */ 11620f18b462SJaegeuk Kim DIRTY_META, /* for all dirtied inode metadata */ 116357864ae5SJaegeuk Kim ATOMIC_FILE, /* for all atomic files */ 1164c227f912SChao Yu NR_INODE_TYPE, 1165c227f912SChao Yu }; 1166c227f912SChao Yu 116767298804SChao Yu /* for inner inode cache management */ 116867298804SChao Yu struct inode_management { 116967298804SChao Yu struct radix_tree_root ino_root; /* ino entry array */ 117067298804SChao Yu spinlock_t ino_lock; /* for ino entry lock */ 117167298804SChao Yu struct list_head ino_list; /* inode list head */ 117267298804SChao Yu unsigned long ino_num; /* number of entries */ 117367298804SChao Yu }; 117467298804SChao Yu 1175093749e2SChao Yu /* for GC_AT */ 1176093749e2SChao Yu struct atgc_management { 1177093749e2SChao Yu bool atgc_enabled; /* ATGC is enabled or not */ 1178093749e2SChao Yu struct rb_root_cached root; /* root of victim rb-tree */ 1179093749e2SChao Yu struct list_head victim_list; /* linked with all victim entries */ 1180093749e2SChao Yu unsigned int victim_count; /* victim count in rb-tree */ 1181093749e2SChao Yu unsigned int candidate_ratio; /* candidate ratio */ 1182093749e2SChao Yu unsigned int max_candidate_count; /* max candidate count */ 1183093749e2SChao Yu unsigned int age_weight; /* age weight, vblock_weight = 100 - age_weight */ 1184093749e2SChao Yu unsigned long long age_threshold; /* age threshold */ 1185093749e2SChao Yu }; 1186093749e2SChao Yu 1187caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */ 1188caf0047eSChao Yu enum { 1189caf0047eSChao Yu SBI_IS_DIRTY, /* dirty flag for checkpoint */ 1190caf0047eSChao Yu SBI_IS_CLOSE, /* specify unmounting */ 1191caf0047eSChao Yu SBI_NEED_FSCK, /* need fsck.f2fs to fix */ 1192caf0047eSChao Yu SBI_POR_DOING, /* recovery is doing or not */ 1193df728b0fSJaegeuk Kim SBI_NEED_SB_WRITE, /* need to recover superblock */ 1194bbf156f7SJaegeuk Kim SBI_NEED_CP, /* need to checkpoint */ 119583a3bfdbSJaegeuk Kim SBI_IS_SHUTDOWN, /* shutdown by ioctl */ 11961378752bSChao Yu SBI_IS_RECOVERED, /* recovered orphan/data */ 11974354994fSDaniel Rosenberg SBI_CP_DISABLED, /* CP was disabled last mount */ 1198db610a64SJaegeuk Kim SBI_CP_DISABLED_QUICK, /* CP was disabled quickly */ 1199af033b2aSChao Yu SBI_QUOTA_NEED_FLUSH, /* need to flush quota info in CP */ 1200af033b2aSChao Yu SBI_QUOTA_SKIP_FLUSH, /* skip flushing quota in current CP */ 1201af033b2aSChao Yu SBI_QUOTA_NEED_REPAIR, /* quota file may be corrupted */ 120204f0b2eaSQiuyang Sun SBI_IS_RESIZEFS, /* resizefs is in process */ 1203caf0047eSChao Yu }; 1204caf0047eSChao Yu 12056beceb54SJaegeuk Kim enum { 12066beceb54SJaegeuk Kim CP_TIME, 1207d0239e1bSJaegeuk Kim REQ_TIME, 1208a7d10cf3SSahitya Tummala DISCARD_TIME, 1209a7d10cf3SSahitya Tummala GC_TIME, 12104354994fSDaniel Rosenberg DISABLE_TIME, 121103f2c02dSJaegeuk Kim UMOUNT_DISCARD_TIMEOUT, 12126beceb54SJaegeuk Kim MAX_TIME, 12136beceb54SJaegeuk Kim }; 12146beceb54SJaegeuk Kim 12150cdd3195SHyunchul Lee enum { 12165b0e9539SJaegeuk Kim GC_NORMAL, 12175b0e9539SJaegeuk Kim GC_IDLE_CB, 12185b0e9539SJaegeuk Kim GC_IDLE_GREEDY, 1219093749e2SChao Yu GC_IDLE_AT, 12200e5e8111SDaeho Jeong GC_URGENT_HIGH, 12210e5e8111SDaeho Jeong GC_URGENT_LOW, 12225b0e9539SJaegeuk Kim }; 12235b0e9539SJaegeuk Kim 12245b0e9539SJaegeuk Kim enum { 1225bbbc34fdSChao Yu BGGC_MODE_ON, /* background gc is on */ 1226bbbc34fdSChao Yu BGGC_MODE_OFF, /* background gc is off */ 1227bbbc34fdSChao Yu BGGC_MODE_SYNC, /* 1228bbbc34fdSChao Yu * background gc is on, migrating blocks 1229bbbc34fdSChao Yu * like foreground gc 1230bbbc34fdSChao Yu */ 1231bbbc34fdSChao Yu }; 1232bbbc34fdSChao Yu 1233bbbc34fdSChao Yu enum { 1234b0332a0fSChao Yu FS_MODE_ADAPTIVE, /* use both lfs/ssr allocation */ 1235b0332a0fSChao Yu FS_MODE_LFS, /* use lfs allocation only */ 1236b0332a0fSChao Yu }; 1237b0332a0fSChao Yu 1238b0332a0fSChao Yu enum { 12390cdd3195SHyunchul Lee WHINT_MODE_OFF, /* not pass down write hints */ 12400cdd3195SHyunchul Lee WHINT_MODE_USER, /* try to pass down hints given by users */ 1241f2e703f9SHyunchul Lee WHINT_MODE_FS, /* pass down hints with F2FS policy */ 12420cdd3195SHyunchul Lee }; 12430cdd3195SHyunchul Lee 124407939627SJaegeuk Kim enum { 124507939627SJaegeuk Kim ALLOC_MODE_DEFAULT, /* stay default */ 124607939627SJaegeuk Kim ALLOC_MODE_REUSE, /* reuse segments as much as possible */ 124707939627SJaegeuk Kim }; 124807939627SJaegeuk Kim 124993cf93f1SJunling Zheng enum fsync_mode { 125093cf93f1SJunling Zheng FSYNC_MODE_POSIX, /* fsync follows posix semantics */ 125193cf93f1SJunling Zheng FSYNC_MODE_STRICT, /* fsync behaves in line with ext4 */ 1252d6290814SJaegeuk Kim FSYNC_MODE_NOBARRIER, /* fsync behaves nobarrier based on posix */ 125393cf93f1SJunling Zheng }; 125493cf93f1SJunling Zheng 1255602a16d5SDaeho Jeong enum { 1256602a16d5SDaeho Jeong COMPR_MODE_FS, /* 1257602a16d5SDaeho Jeong * automatically compress compression 1258602a16d5SDaeho Jeong * enabled files 1259602a16d5SDaeho Jeong */ 1260602a16d5SDaeho Jeong COMPR_MODE_USER, /* 1261602a16d5SDaeho Jeong * automatical compression is disabled. 1262602a16d5SDaeho Jeong * user can control the file compression 1263602a16d5SDaeho Jeong * using ioctls 1264602a16d5SDaeho Jeong */ 1265602a16d5SDaeho Jeong }; 1266602a16d5SDaeho Jeong 12674c8ff709SChao Yu /* 12684c8ff709SChao Yu * this value is set in page as a private data which indicate that 12694c8ff709SChao Yu * the page is atomically written, and it is in inmem_pages list. 12704c8ff709SChao Yu */ 12714c8ff709SChao Yu #define ATOMIC_WRITTEN_PAGE ((unsigned long)-1) 12724c8ff709SChao Yu #define DUMMY_WRITTEN_PAGE ((unsigned long)-2) 12734c8ff709SChao Yu 12744c8ff709SChao Yu #define IS_ATOMIC_WRITTEN_PAGE(page) \ 1275e90027d2SXiaojun Wang (page_private(page) == ATOMIC_WRITTEN_PAGE) 12764c8ff709SChao Yu #define IS_DUMMY_WRITTEN_PAGE(page) \ 1277e90027d2SXiaojun Wang (page_private(page) == DUMMY_WRITTEN_PAGE) 12784c8ff709SChao Yu 127929b993c7SYu Changchun #ifdef CONFIG_F2FS_IO_TRACE 128029b993c7SYu Changchun #define IS_IO_TRACED_PAGE(page) \ 128129b993c7SYu Changchun (page_private(page) > 0 && \ 128229b993c7SYu Changchun page_private(page) < (unsigned long)PID_MAX_LIMIT) 128329b993c7SYu Changchun #else 128429b993c7SYu Changchun #define IS_IO_TRACED_PAGE(page) (0) 128529b993c7SYu Changchun #endif 128629b993c7SYu Changchun 12874c8ff709SChao Yu /* For compression */ 12884c8ff709SChao Yu enum compress_algorithm_type { 12894c8ff709SChao Yu COMPRESS_LZO, 12904c8ff709SChao Yu COMPRESS_LZ4, 129150cfa66fSChao Yu COMPRESS_ZSTD, 12926d92b201SChao Yu COMPRESS_LZORLE, 12934c8ff709SChao Yu COMPRESS_MAX, 12944c8ff709SChao Yu }; 12954c8ff709SChao Yu 1296b28f047bSChao Yu enum compress_flag { 1297b28f047bSChao Yu COMPRESS_CHKSUM, 1298b28f047bSChao Yu COMPRESS_MAX_FLAG, 1299b28f047bSChao Yu }; 1300b28f047bSChao Yu 1301b28f047bSChao Yu #define COMPRESS_DATA_RESERVED_SIZE 4 13024c8ff709SChao Yu struct compress_data { 13034c8ff709SChao Yu __le32 clen; /* compressed data size */ 1304b28f047bSChao Yu __le32 chksum; /* compressed data chksum */ 13054c8ff709SChao Yu __le32 reserved[COMPRESS_DATA_RESERVED_SIZE]; /* reserved */ 13064c8ff709SChao Yu u8 cdata[]; /* compressed data */ 13074c8ff709SChao Yu }; 13084c8ff709SChao Yu 13094c8ff709SChao Yu #define COMPRESS_HEADER_SIZE (sizeof(struct compress_data)) 13104c8ff709SChao Yu 13114c8ff709SChao Yu #define F2FS_COMPRESSED_PAGE_MAGIC 0xF5F2C000 13124c8ff709SChao Yu 13134c8ff709SChao Yu /* compress context */ 13144c8ff709SChao Yu struct compress_ctx { 13154c8ff709SChao Yu struct inode *inode; /* inode the context belong to */ 13164c8ff709SChao Yu pgoff_t cluster_idx; /* cluster index number */ 13174c8ff709SChao Yu unsigned int cluster_size; /* page count in cluster */ 13184c8ff709SChao Yu unsigned int log_cluster_size; /* log of cluster size */ 13194c8ff709SChao Yu struct page **rpages; /* pages store raw data in cluster */ 13204c8ff709SChao Yu unsigned int nr_rpages; /* total page number in rpages */ 13214c8ff709SChao Yu struct page **cpages; /* pages store compressed data in cluster */ 13224c8ff709SChao Yu unsigned int nr_cpages; /* total page number in cpages */ 13234c8ff709SChao Yu void *rbuf; /* virtual mapped address on rpages */ 13244c8ff709SChao Yu struct compress_data *cbuf; /* virtual mapped address on cpages */ 13254c8ff709SChao Yu size_t rlen; /* valid data length in rbuf */ 13264c8ff709SChao Yu size_t clen; /* valid data length in cbuf */ 13274c8ff709SChao Yu void *private; /* payload buffer for specified compression algorithm */ 132850cfa66fSChao Yu void *private2; /* extra payload buffer */ 13294c8ff709SChao Yu }; 13304c8ff709SChao Yu 13314c8ff709SChao Yu /* compress context for write IO path */ 13324c8ff709SChao Yu struct compress_io_ctx { 13334c8ff709SChao Yu u32 magic; /* magic number to indicate page is compressed */ 13344c8ff709SChao Yu struct inode *inode; /* inode the context belong to */ 13354c8ff709SChao Yu struct page **rpages; /* pages store raw data in cluster */ 13364c8ff709SChao Yu unsigned int nr_rpages; /* total page number in rpages */ 1337e6c3948dSChao Yu atomic_t pending_pages; /* in-flight compressed page count */ 13384c8ff709SChao Yu }; 13394c8ff709SChao Yu 13404c8ff709SChao Yu /* decompress io context for read IO path */ 13414c8ff709SChao Yu struct decompress_io_ctx { 13424c8ff709SChao Yu u32 magic; /* magic number to indicate page is compressed */ 13434c8ff709SChao Yu struct inode *inode; /* inode the context belong to */ 13444c8ff709SChao Yu pgoff_t cluster_idx; /* cluster index number */ 13454c8ff709SChao Yu unsigned int cluster_size; /* page count in cluster */ 13464c8ff709SChao Yu unsigned int log_cluster_size; /* log of cluster size */ 13474c8ff709SChao Yu struct page **rpages; /* pages store raw data in cluster */ 13484c8ff709SChao Yu unsigned int nr_rpages; /* total page number in rpages */ 13494c8ff709SChao Yu struct page **cpages; /* pages store compressed data in cluster */ 13504c8ff709SChao Yu unsigned int nr_cpages; /* total page number in cpages */ 13514c8ff709SChao Yu struct page **tpages; /* temp pages to pad holes in cluster */ 13524c8ff709SChao Yu void *rbuf; /* virtual mapped address on rpages */ 13534c8ff709SChao Yu struct compress_data *cbuf; /* virtual mapped address on cpages */ 13544c8ff709SChao Yu size_t rlen; /* valid data length in rbuf */ 13554c8ff709SChao Yu size_t clen; /* valid data length in cbuf */ 1356e6c3948dSChao Yu atomic_t pending_pages; /* in-flight compressed page count */ 13576422a71eSDaeho Jeong atomic_t verity_pages; /* in-flight page count for verity */ 13584c8ff709SChao Yu bool failed; /* indicate IO error during decompression */ 135950cfa66fSChao Yu void *private; /* payload buffer for specified decompression algorithm */ 136050cfa66fSChao Yu void *private2; /* extra payload buffer */ 13614c8ff709SChao Yu }; 13624c8ff709SChao Yu 13634c8ff709SChao Yu #define NULL_CLUSTER ((unsigned int)(~0)) 13644c8ff709SChao Yu #define MIN_COMPRESS_LOG_SIZE 2 13654c8ff709SChao Yu #define MAX_COMPRESS_LOG_SIZE 8 13660e2b7385SChao Yu #define MAX_COMPRESS_WINDOW_SIZE(log_size) ((PAGE_SIZE) << (log_size)) 13674c8ff709SChao Yu 136839a53e0cSJaegeuk Kim struct f2fs_sb_info { 136939a53e0cSJaegeuk Kim struct super_block *sb; /* pointer to VFS super block */ 13705e176d54SJaegeuk Kim struct proc_dir_entry *s_proc; /* proc entry */ 137139a53e0cSJaegeuk Kim struct f2fs_super_block *raw_super; /* raw super block pointer */ 1372846ae671SChao Yu struct rw_semaphore sb_lock; /* lock for raw super block */ 1373e8240f65SChao Yu int valid_super_block; /* valid super block no */ 1374fadb2fb8SChao Yu unsigned long s_flag; /* flags for sbi */ 1375853137ceSJaegeuk Kim struct mutex writepages; /* mutex for writepages() */ 137639a53e0cSJaegeuk Kim 1377178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED 1378178053e2SDamien Le Moal unsigned int blocks_per_blkz; /* F2FS blocks per zone */ 1379178053e2SDamien Le Moal unsigned int log_blocks_per_blkz; /* log2 F2FS blocks per zone */ 1380178053e2SDamien Le Moal #endif 1381178053e2SDamien Le Moal 138239a53e0cSJaegeuk Kim /* for node-related operations */ 138339a53e0cSJaegeuk Kim struct f2fs_nm_info *nm_info; /* node manager */ 138439a53e0cSJaegeuk Kim struct inode *node_inode; /* cache node blocks */ 138539a53e0cSJaegeuk Kim 138639a53e0cSJaegeuk Kim /* for segment-related operations */ 138739a53e0cSJaegeuk Kim struct f2fs_sm_info *sm_info; /* segment manager */ 13881ff7bd3bSJaegeuk Kim 13891ff7bd3bSJaegeuk Kim /* for bio operations */ 1390a912b54dSJaegeuk Kim struct f2fs_bio_info *write_io[NR_PAGE_TYPE]; /* for write bios */ 1391107a805dSChao Yu /* keep migration IO order for LFS mode */ 1392107a805dSChao Yu struct rw_semaphore io_order_lock; 13930a595ebaSJaegeuk Kim mempool_t *write_io_dummy; /* Dummy pages */ 139439a53e0cSJaegeuk Kim 139539a53e0cSJaegeuk Kim /* for checkpoint */ 139639a53e0cSJaegeuk Kim struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */ 13978508e44aSJaegeuk Kim int cur_cp_pack; /* remain current cp pack */ 1398aaec2b1dSChao Yu spinlock_t cp_lock; /* for flag in ckpt */ 139939a53e0cSJaegeuk Kim struct inode *meta_inode; /* cache meta blocks */ 14008769918bSSahitya Tummala struct rw_semaphore cp_global_sem; /* checkpoint procedure lock */ 1401b873b798SJaegeuk Kim struct rw_semaphore cp_rwsem; /* blocking FS operations */ 1402b3582c68SChao Yu struct rw_semaphore node_write; /* locking node writes */ 140359c9081bSYunlei He struct rw_semaphore node_change; /* locking node change */ 1404fb51b5efSChangman Lee wait_queue_head_t cp_wait; 14056beceb54SJaegeuk Kim unsigned long last_time[MAX_TIME]; /* to store time in jiffies */ 14066beceb54SJaegeuk Kim long interval_time[MAX_TIME]; /* to store thresholds */ 140739a53e0cSJaegeuk Kim 140867298804SChao Yu struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */ 14096451e041SJaegeuk Kim 141050fa53ecSChao Yu spinlock_t fsync_node_lock; /* for node entry lock */ 141150fa53ecSChao Yu struct list_head fsync_node_list; /* node list head */ 141250fa53ecSChao Yu unsigned int fsync_seg_id; /* sequence id */ 141350fa53ecSChao Yu unsigned int fsync_node_num; /* number of node entries */ 141450fa53ecSChao Yu 14156451e041SJaegeuk Kim /* for orphan inode, use 0'th array */ 14160d47c1adSGu Zheng unsigned int max_orphans; /* max orphan inodes */ 141739a53e0cSJaegeuk Kim 1418c227f912SChao Yu /* for inode management */ 1419c227f912SChao Yu struct list_head inode_list[NR_INODE_TYPE]; /* dirty inode list */ 1420c227f912SChao Yu spinlock_t inode_lock[NR_INODE_TYPE]; /* for dirty inode list lock */ 1421040d2bb3SChao Yu struct mutex flush_lock; /* for flush exclusion */ 142239a53e0cSJaegeuk Kim 142313054c54SChao Yu /* for extent tree cache */ 142413054c54SChao Yu struct radix_tree_root extent_tree_root;/* cache extent cache entries */ 14255e8256acSYunlei He struct mutex extent_tree_lock; /* locking extent radix tree */ 142613054c54SChao Yu struct list_head extent_list; /* lru list for shrinker */ 142713054c54SChao Yu spinlock_t extent_lock; /* locking extent lru list */ 14287441ccefSJaegeuk Kim atomic_t total_ext_tree; /* extent tree count */ 1429137d09f0SJaegeuk Kim struct list_head zombie_list; /* extent zombie tree list */ 143074fd8d99SJaegeuk Kim atomic_t total_zombie_tree; /* extent zombie tree count */ 143113054c54SChao Yu atomic_t total_ext_node; /* extent info count */ 143213054c54SChao Yu 143339a53e0cSJaegeuk Kim /* basic filesystem units */ 143439a53e0cSJaegeuk Kim unsigned int log_sectors_per_block; /* log2 sectors per block */ 143539a53e0cSJaegeuk Kim unsigned int log_blocksize; /* log2 block size */ 143639a53e0cSJaegeuk Kim unsigned int blocksize; /* block size */ 143739a53e0cSJaegeuk Kim unsigned int root_ino_num; /* root inode number*/ 143839a53e0cSJaegeuk Kim unsigned int node_ino_num; /* node inode number*/ 143939a53e0cSJaegeuk Kim unsigned int meta_ino_num; /* meta inode number*/ 144039a53e0cSJaegeuk Kim unsigned int log_blocks_per_seg; /* log2 blocks per segment */ 144139a53e0cSJaegeuk Kim unsigned int blocks_per_seg; /* blocks per segment */ 144239a53e0cSJaegeuk Kim unsigned int segs_per_sec; /* segments per section */ 144339a53e0cSJaegeuk Kim unsigned int secs_per_zone; /* sections per zone */ 144439a53e0cSJaegeuk Kim unsigned int total_sections; /* total section count */ 144539a53e0cSJaegeuk Kim unsigned int total_node_count; /* total node block count */ 144639a53e0cSJaegeuk Kim unsigned int total_valid_node_count; /* valid node block count */ 1447e0afc4d6SChao Yu loff_t max_file_blocks; /* max block index of file */ 1448ab9fa662SJaegeuk Kim int dir_level; /* directory level */ 1449f6df8f23SSheng Yong int readdir_ra; /* readahead inode in readdir */ 145010208567SJaegeuk Kim u64 max_io_bytes; /* max io bytes to merge IOs */ 145139a53e0cSJaegeuk Kim 145239a53e0cSJaegeuk Kim block_t user_block_count; /* # of user blocks */ 145339a53e0cSJaegeuk Kim block_t total_valid_block_count; /* # of valid blocks */ 1454a66cdd98SJaegeuk Kim block_t discard_blks; /* discard command candidats */ 145539a53e0cSJaegeuk Kim block_t last_valid_block_count; /* for recovery */ 1456daeb433eSChao Yu block_t reserved_blocks; /* configurable reserved blocks */ 145780d42145SYunlong Song block_t current_reserved_blocks; /* current reserved blocks */ 1458daeb433eSChao Yu 14594354994fSDaniel Rosenberg /* Additional tracking for no checkpoint mode */ 14604354994fSDaniel Rosenberg block_t unusable_block_count; /* # of blocks saved by last cp */ 14614354994fSDaniel Rosenberg 1462292c196aSChao Yu unsigned int nquota_files; /* # of quota sysfile */ 1463db6ec53bSJaegeuk Kim struct rw_semaphore quota_sem; /* blocking cp for flags */ 1464292c196aSChao Yu 1465523be8a6SJaegeuk Kim /* # of pages, see count_type */ 146635782b23SJaegeuk Kim atomic_t nr_pages[NR_COUNT_TYPE]; 146741382ec4SJaegeuk Kim /* # of allocated blocks */ 146841382ec4SJaegeuk Kim struct percpu_counter alloc_valid_block_count; 146939a53e0cSJaegeuk Kim 1470687de7f1SJaegeuk Kim /* writeback control */ 1471c29fd0c0SChao Yu atomic_t wb_sync_req[META]; /* count # of WB_SYNC threads */ 1472687de7f1SJaegeuk Kim 1473513c5f37SJaegeuk Kim /* valid inode count */ 1474513c5f37SJaegeuk Kim struct percpu_counter total_valid_inode_count; 1475513c5f37SJaegeuk Kim 147639a53e0cSJaegeuk Kim struct f2fs_mount_info mount_opt; /* mount options */ 147739a53e0cSJaegeuk Kim 147839a53e0cSJaegeuk Kim /* for cleaning operations */ 1479fb24fea7SChao Yu struct rw_semaphore gc_lock; /* 1480fb24fea7SChao Yu * semaphore for GC, avoid 1481fb24fea7SChao Yu * race between GC and GC or CP 1482fb24fea7SChao Yu */ 148339a53e0cSJaegeuk Kim struct f2fs_gc_kthread *gc_thread; /* GC thread */ 1484093749e2SChao Yu struct atgc_management am; /* atgc management */ 14855ec4e49fSJaegeuk Kim unsigned int cur_victim_sec; /* current victim section num */ 14865b0e9539SJaegeuk Kim unsigned int gc_mode; /* current GC state */ 1487e3080b01SChao Yu unsigned int next_victim_seg[2]; /* next segment in victim section */ 14880e5e8111SDaeho Jeong 14892ef79ecbSChao Yu /* for skip statistic */ 1490677017d1SSahitya Tummala unsigned int atomic_files; /* # of opened atomic file */ 14912ef79ecbSChao Yu unsigned long long skipped_atomic_files[2]; /* FG_GC and BG_GC */ 14926f8d4455SJaegeuk Kim unsigned long long skipped_gc_rwsem; /* FG_GC only */ 149339a53e0cSJaegeuk Kim 14941ad71a27SJaegeuk Kim /* threshold for gc trials on pinned files */ 14951ad71a27SJaegeuk Kim u64 gc_pin_file_threshold; 1496f5a53edcSJaegeuk Kim struct rw_semaphore pin_sem; 14971ad71a27SJaegeuk Kim 1498b1c57c1cSJaegeuk Kim /* maximum # of trials to find a victim segment for SSR and GC */ 1499b1c57c1cSJaegeuk Kim unsigned int max_victim_search; 1500e3080b01SChao Yu /* migration granularity of garbage collection, unit: segment */ 1501e3080b01SChao Yu unsigned int migration_granularity; 1502b1c57c1cSJaegeuk Kim 150339a53e0cSJaegeuk Kim /* 150439a53e0cSJaegeuk Kim * for stat information. 150539a53e0cSJaegeuk Kim * one is for the LFS mode, and the other is for the SSR mode. 150639a53e0cSJaegeuk Kim */ 150735b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS 150839a53e0cSJaegeuk Kim struct f2fs_stat_info *stat_info; /* FS status information */ 1509b63e7be5SChao Yu atomic_t meta_count[META_MAX]; /* # of meta blocks */ 151039a53e0cSJaegeuk Kim unsigned int segment_count[2]; /* # of allocated segments */ 151139a53e0cSJaegeuk Kim unsigned int block_count[2]; /* # of allocated blocks */ 1512b9a2c252SChangman Lee atomic_t inplace_count; /* # of inplace update */ 15135b7ee374SChao Yu atomic64_t total_hit_ext; /* # of lookup extent cache */ 15145b7ee374SChao Yu atomic64_t read_hit_rbtree; /* # of hit rbtree extent node */ 15155b7ee374SChao Yu atomic64_t read_hit_largest; /* # of hit largest extent node */ 15165b7ee374SChao Yu atomic64_t read_hit_cached; /* # of hit cached extent node */ 1517d5e8f6c9SChao Yu atomic_t inline_xattr; /* # of inline_xattr inodes */ 151803e14d52SChao Yu atomic_t inline_inode; /* # of inline_data inodes */ 151903e14d52SChao Yu atomic_t inline_dir; /* # of inline_dentry inodes */ 15204c8ff709SChao Yu atomic_t compr_inode; /* # of compressed inodes */ 1521ae999bb9SDaeho Jeong atomic64_t compr_blocks; /* # of compressed blocks */ 1522648d50baSChao Yu atomic_t vw_cnt; /* # of volatile writes */ 152326a28a0cSJaegeuk Kim atomic_t max_aw_cnt; /* max # of atomic writes */ 1524648d50baSChao Yu atomic_t max_vw_cnt; /* max # of volatile writes */ 1525274bd9baSChao Yu unsigned int io_skip_bggc; /* skip background gc for in-flight IO */ 1526274bd9baSChao Yu unsigned int other_skip_bggc; /* skip background gc for other reasons */ 152733fbd510SChao Yu unsigned int ndirty_inode[NR_INODE_TYPE]; /* # of dirty inodes */ 152835b09d82SNamjae Jeon #endif 152939a53e0cSJaegeuk Kim spinlock_t stat_lock; /* lock for stat operations */ 1530b59d0baeSNamjae Jeon 1531b0af6d49SChao Yu /* For app/fs IO statistics */ 1532b0af6d49SChao Yu spinlock_t iostat_lock; 15338b83ac81SChao Yu unsigned long long rw_iostat[NR_IO_TYPE]; 15348b83ac81SChao Yu unsigned long long prev_rw_iostat[NR_IO_TYPE]; 1535b0af6d49SChao Yu bool iostat_enable; 15362bc4bea3SDaeho Jeong unsigned long iostat_next_period; 15372bc4bea3SDaeho Jeong unsigned int iostat_period_ms; 1538b0af6d49SChao Yu 1539da9953b7SJaegeuk Kim /* to attach REQ_META|REQ_FUA flags */ 1540da9953b7SJaegeuk Kim unsigned int data_io_flag; 154132b6aba8SJaegeuk Kim unsigned int node_io_flag; 1542b59d0baeSNamjae Jeon 1543b59d0baeSNamjae Jeon /* For sysfs suppport */ 1544b59d0baeSNamjae Jeon struct kobject s_kobj; 1545b59d0baeSNamjae Jeon struct completion s_kobj_unregister; 15462658e50dSJaegeuk Kim 15472658e50dSJaegeuk Kim /* For shrinker support */ 15482658e50dSJaegeuk Kim struct list_head s_list; 15493c62be17SJaegeuk Kim int s_ndevs; /* number of devices */ 15503c62be17SJaegeuk Kim struct f2fs_dev_info *devs; /* for device list */ 15511228b482SChao Yu unsigned int dirty_device; /* for checkpoint data flush */ 15521228b482SChao Yu spinlock_t dev_lock; /* protect dirty_device */ 15532658e50dSJaegeuk Kim struct mutex umount_mutex; 15542658e50dSJaegeuk Kim unsigned int shrinker_run_no; 15558f1dbbbbSShuoran Liu 15568f1dbbbbSShuoran Liu /* For write statistics */ 15578f1dbbbbSShuoran Liu u64 sectors_written_start; 15588f1dbbbbSShuoran Liu u64 kbytes_written; 155943b6573bSKeith Mok 156043b6573bSKeith Mok /* Reference to checksum algorithm driver via cryptoapi */ 156143b6573bSKeith Mok struct crypto_shash *s_chksum_driver; 15621ecc0c5cSChao Yu 1563704956ecSChao Yu /* Precomputed FS UUID checksum for seeding other checksums */ 1564704956ecSChao Yu __u32 s_chksum_seed; 15654c8ff709SChao Yu 15664c8ff709SChao Yu struct workqueue_struct *post_read_wq; /* post read workqueue */ 1567a999150fSChao Yu 1568a999150fSChao Yu struct kmem_cache *inline_xattr_slab; /* inline xattr entry */ 1569a999150fSChao Yu unsigned int inline_xattr_slab_size; /* default inline xattr slab size */ 157031083031SChao Yu 157131083031SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION 157231083031SChao Yu struct kmem_cache *page_array_slab; /* page array entry */ 157331083031SChao Yu unsigned int page_array_slab_size; /* default page array slab size */ 157431083031SChao Yu #endif 157539a53e0cSJaegeuk Kim }; 157639a53e0cSJaegeuk Kim 157702b16d0aSChao Yu struct f2fs_private_dio { 157802b16d0aSChao Yu struct inode *inode; 157902b16d0aSChao Yu void *orig_private; 158002b16d0aSChao Yu bio_end_io_t *orig_end_io; 158102b16d0aSChao Yu bool write; 158202b16d0aSChao Yu }; 158302b16d0aSChao Yu 15841ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION 1585c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type) \ 1586c45d6002SChao Yu printk_ratelimited("%sF2FS-fs (%s) : inject %s in %s of %pS\n", \ 1587c45d6002SChao Yu KERN_INFO, sbi->sb->s_id, \ 1588c45d6002SChao Yu f2fs_fault_name[type], \ 158955523519SChao Yu __func__, __builtin_return_address(0)) 15901ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type) 15911ecc0c5cSChao Yu { 159263189b78SChao Yu struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info; 15931ecc0c5cSChao Yu 15941ecc0c5cSChao Yu if (!ffi->inject_rate) 15951ecc0c5cSChao Yu return false; 15961ecc0c5cSChao Yu 15971ecc0c5cSChao Yu if (!IS_FAULT_SET(ffi, type)) 15981ecc0c5cSChao Yu return false; 15991ecc0c5cSChao Yu 16001ecc0c5cSChao Yu atomic_inc(&ffi->inject_ops); 16011ecc0c5cSChao Yu if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) { 16021ecc0c5cSChao Yu atomic_set(&ffi->inject_ops, 0); 16031ecc0c5cSChao Yu return true; 16041ecc0c5cSChao Yu } 16051ecc0c5cSChao Yu return false; 16061ecc0c5cSChao Yu } 16077fa750a1SArnd Bergmann #else 1608c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type) do { } while (0) 16097fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type) 16107fa750a1SArnd Bergmann { 16117fa750a1SArnd Bergmann return false; 16127fa750a1SArnd Bergmann } 16131ecc0c5cSChao Yu #endif 16141ecc0c5cSChao Yu 16150916878dSDamien Le Moal /* 16160916878dSDamien Le Moal * Test if the mounted volume is a multi-device volume. 16170916878dSDamien Le Moal * - For a single regular disk volume, sbi->s_ndevs is 0. 16180916878dSDamien Le Moal * - For a single zoned disk volume, sbi->s_ndevs is 1. 16190916878dSDamien Le Moal * - For a multi-device volume, sbi->s_ndevs is always 2 or more. 16200916878dSDamien Le Moal */ 16210916878dSDamien Le Moal static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi) 16220916878dSDamien Le Moal { 16230916878dSDamien Le Moal return sbi->s_ndevs > 1; 16240916878dSDamien Le Moal } 16250916878dSDamien Le Moal 16266beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type) 16276beceb54SJaegeuk Kim { 1628a7d10cf3SSahitya Tummala unsigned long now = jiffies; 1629a7d10cf3SSahitya Tummala 1630a7d10cf3SSahitya Tummala sbi->last_time[type] = now; 1631a7d10cf3SSahitya Tummala 1632a7d10cf3SSahitya Tummala /* DISCARD_TIME and GC_TIME are based on REQ_TIME */ 1633a7d10cf3SSahitya Tummala if (type == REQ_TIME) { 1634a7d10cf3SSahitya Tummala sbi->last_time[DISCARD_TIME] = now; 1635a7d10cf3SSahitya Tummala sbi->last_time[GC_TIME] = now; 1636a7d10cf3SSahitya Tummala } 16376beceb54SJaegeuk Kim } 16386beceb54SJaegeuk Kim 16396beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type) 16406beceb54SJaegeuk Kim { 16416bccfa19SArnd Bergmann unsigned long interval = sbi->interval_time[type] * HZ; 16426beceb54SJaegeuk Kim 16436beceb54SJaegeuk Kim return time_after(jiffies, sbi->last_time[type] + interval); 16446beceb54SJaegeuk Kim } 16456beceb54SJaegeuk Kim 1646a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi, 1647a7d10cf3SSahitya Tummala int type) 1648a7d10cf3SSahitya Tummala { 1649a7d10cf3SSahitya Tummala unsigned long interval = sbi->interval_time[type] * HZ; 1650a7d10cf3SSahitya Tummala unsigned int wait_ms = 0; 1651a7d10cf3SSahitya Tummala long delta; 1652a7d10cf3SSahitya Tummala 1653a7d10cf3SSahitya Tummala delta = (sbi->last_time[type] + interval) - jiffies; 1654a7d10cf3SSahitya Tummala if (delta > 0) 1655a7d10cf3SSahitya Tummala wait_ms = jiffies_to_msecs(delta); 1656a7d10cf3SSahitya Tummala 1657a7d10cf3SSahitya Tummala return wait_ms; 1658a7d10cf3SSahitya Tummala } 1659a7d10cf3SSahitya Tummala 166039a53e0cSJaegeuk Kim /* 166139a53e0cSJaegeuk Kim * Inline functions 166239a53e0cSJaegeuk Kim */ 1663416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc, 1664704956ecSChao Yu const void *address, unsigned int length) 1665704956ecSChao Yu { 1666704956ecSChao Yu struct { 1667704956ecSChao Yu struct shash_desc shash; 1668704956ecSChao Yu char ctx[4]; 1669704956ecSChao Yu } desc; 1670704956ecSChao Yu int err; 1671704956ecSChao Yu 1672704956ecSChao Yu BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx)); 1673704956ecSChao Yu 1674704956ecSChao Yu desc.shash.tfm = sbi->s_chksum_driver; 1675704956ecSChao Yu *(u32 *)desc.ctx = crc; 1676704956ecSChao Yu 1677704956ecSChao Yu err = crypto_shash_update(&desc.shash, address, length); 1678704956ecSChao Yu BUG_ON(err); 1679704956ecSChao Yu 1680704956ecSChao Yu return *(u32 *)desc.ctx; 1681704956ecSChao Yu } 1682704956ecSChao Yu 1683416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address, 1684416d2dbbSChao Yu unsigned int length) 1685416d2dbbSChao Yu { 1686416d2dbbSChao Yu return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length); 1687416d2dbbSChao Yu } 1688416d2dbbSChao Yu 1689416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc, 1690416d2dbbSChao Yu void *buf, size_t buf_size) 1691416d2dbbSChao Yu { 1692416d2dbbSChao Yu return f2fs_crc32(sbi, buf, buf_size) == blk_crc; 1693416d2dbbSChao Yu } 1694416d2dbbSChao Yu 1695416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc, 1696416d2dbbSChao Yu const void *address, unsigned int length) 1697416d2dbbSChao Yu { 1698416d2dbbSChao Yu return __f2fs_crc32(sbi, crc, address, length); 1699416d2dbbSChao Yu } 1700416d2dbbSChao Yu 170139a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode) 170239a53e0cSJaegeuk Kim { 170339a53e0cSJaegeuk Kim return container_of(inode, struct f2fs_inode_info, vfs_inode); 170439a53e0cSJaegeuk Kim } 170539a53e0cSJaegeuk Kim 170639a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb) 170739a53e0cSJaegeuk Kim { 170839a53e0cSJaegeuk Kim return sb->s_fs_info; 170939a53e0cSJaegeuk Kim } 171039a53e0cSJaegeuk Kim 17114081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode) 17124081363fSJaegeuk Kim { 17134081363fSJaegeuk Kim return F2FS_SB(inode->i_sb); 17144081363fSJaegeuk Kim } 17154081363fSJaegeuk Kim 17164081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping) 17174081363fSJaegeuk Kim { 17184081363fSJaegeuk Kim return F2FS_I_SB(mapping->host); 17194081363fSJaegeuk Kim } 17204081363fSJaegeuk Kim 17214081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page) 17224081363fSJaegeuk Kim { 17234969c06aSJaegeuk Kim return F2FS_M_SB(page_file_mapping(page)); 17244081363fSJaegeuk Kim } 17254081363fSJaegeuk Kim 172639a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi) 172739a53e0cSJaegeuk Kim { 172839a53e0cSJaegeuk Kim return (struct f2fs_super_block *)(sbi->raw_super); 172939a53e0cSJaegeuk Kim } 173039a53e0cSJaegeuk Kim 173139a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi) 173239a53e0cSJaegeuk Kim { 173339a53e0cSJaegeuk Kim return (struct f2fs_checkpoint *)(sbi->ckpt); 173439a53e0cSJaegeuk Kim } 173539a53e0cSJaegeuk Kim 173645590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page) 173745590710SGu Zheng { 173845590710SGu Zheng return (struct f2fs_node *)page_address(page); 173945590710SGu Zheng } 174045590710SGu Zheng 174158bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page) 174258bfaf44SJaegeuk Kim { 174358bfaf44SJaegeuk Kim return &((struct f2fs_node *)page_address(page))->i; 174458bfaf44SJaegeuk Kim } 174558bfaf44SJaegeuk Kim 174639a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi) 174739a53e0cSJaegeuk Kim { 174839a53e0cSJaegeuk Kim return (struct f2fs_nm_info *)(sbi->nm_info); 174939a53e0cSJaegeuk Kim } 175039a53e0cSJaegeuk Kim 175139a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi) 175239a53e0cSJaegeuk Kim { 175339a53e0cSJaegeuk Kim return (struct f2fs_sm_info *)(sbi->sm_info); 175439a53e0cSJaegeuk Kim } 175539a53e0cSJaegeuk Kim 175639a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi) 175739a53e0cSJaegeuk Kim { 175839a53e0cSJaegeuk Kim return (struct sit_info *)(SM_I(sbi)->sit_info); 175939a53e0cSJaegeuk Kim } 176039a53e0cSJaegeuk Kim 176139a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi) 176239a53e0cSJaegeuk Kim { 176339a53e0cSJaegeuk Kim return (struct free_segmap_info *)(SM_I(sbi)->free_info); 176439a53e0cSJaegeuk Kim } 176539a53e0cSJaegeuk Kim 176639a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi) 176739a53e0cSJaegeuk Kim { 176839a53e0cSJaegeuk Kim return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info); 176939a53e0cSJaegeuk Kim } 177039a53e0cSJaegeuk Kim 17719df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi) 17729df27d98SGu Zheng { 17739df27d98SGu Zheng return sbi->meta_inode->i_mapping; 17749df27d98SGu Zheng } 17759df27d98SGu Zheng 17764ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi) 17774ef51a8fSJaegeuk Kim { 17784ef51a8fSJaegeuk Kim return sbi->node_inode->i_mapping; 17794ef51a8fSJaegeuk Kim } 17804ef51a8fSJaegeuk Kim 1781caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type) 178239a53e0cSJaegeuk Kim { 1783fadb2fb8SChao Yu return test_bit(type, &sbi->s_flag); 178439a53e0cSJaegeuk Kim } 178539a53e0cSJaegeuk Kim 1786caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type) 178739a53e0cSJaegeuk Kim { 1788fadb2fb8SChao Yu set_bit(type, &sbi->s_flag); 1789caf0047eSChao Yu } 1790caf0047eSChao Yu 1791caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type) 1792caf0047eSChao Yu { 1793fadb2fb8SChao Yu clear_bit(type, &sbi->s_flag); 179439a53e0cSJaegeuk Kim } 179539a53e0cSJaegeuk Kim 1796d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp) 1797d71b5564SJaegeuk Kim { 1798d71b5564SJaegeuk Kim return le64_to_cpu(cp->checkpoint_ver); 1799d71b5564SJaegeuk Kim } 1800d71b5564SJaegeuk Kim 1801ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type) 1802ea676733SJaegeuk Kim { 1803ea676733SJaegeuk Kim if (type < F2FS_MAX_QUOTAS) 1804ea676733SJaegeuk Kim return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]); 1805ea676733SJaegeuk Kim return 0; 1806ea676733SJaegeuk Kim } 1807ea676733SJaegeuk Kim 1808ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp) 1809ced2c7eaSKinglong Mee { 1810ced2c7eaSKinglong Mee size_t crc_offset = le32_to_cpu(cp->checksum_offset); 1811ced2c7eaSKinglong Mee return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset))); 1812ced2c7eaSKinglong Mee } 1813ced2c7eaSKinglong Mee 1814aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f) 181525ca923bSJaegeuk Kim { 181625ca923bSJaegeuk Kim unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags); 1817aaec2b1dSChao Yu 181825ca923bSJaegeuk Kim return ckpt_flags & f; 181925ca923bSJaegeuk Kim } 182025ca923bSJaegeuk Kim 1821aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f) 182225ca923bSJaegeuk Kim { 1823aaec2b1dSChao Yu return __is_set_ckpt_flags(F2FS_CKPT(sbi), f); 1824aaec2b1dSChao Yu } 1825aaec2b1dSChao Yu 1826aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f) 1827aaec2b1dSChao Yu { 1828aaec2b1dSChao Yu unsigned int ckpt_flags; 1829aaec2b1dSChao Yu 1830aaec2b1dSChao Yu ckpt_flags = le32_to_cpu(cp->ckpt_flags); 183125ca923bSJaegeuk Kim ckpt_flags |= f; 183225ca923bSJaegeuk Kim cp->ckpt_flags = cpu_to_le32(ckpt_flags); 183325ca923bSJaegeuk Kim } 183425ca923bSJaegeuk Kim 1835aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f) 183625ca923bSJaegeuk Kim { 1837d1aa2453SChao Yu unsigned long flags; 1838d1aa2453SChao Yu 1839d1aa2453SChao Yu spin_lock_irqsave(&sbi->cp_lock, flags); 1840aaec2b1dSChao Yu __set_ckpt_flags(F2FS_CKPT(sbi), f); 1841d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags); 1842aaec2b1dSChao Yu } 1843aaec2b1dSChao Yu 1844aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f) 1845aaec2b1dSChao Yu { 1846aaec2b1dSChao Yu unsigned int ckpt_flags; 1847aaec2b1dSChao Yu 1848aaec2b1dSChao Yu ckpt_flags = le32_to_cpu(cp->ckpt_flags); 184925ca923bSJaegeuk Kim ckpt_flags &= (~f); 185025ca923bSJaegeuk Kim cp->ckpt_flags = cpu_to_le32(ckpt_flags); 185125ca923bSJaegeuk Kim } 185225ca923bSJaegeuk Kim 1853aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f) 1854aaec2b1dSChao Yu { 1855d1aa2453SChao Yu unsigned long flags; 1856d1aa2453SChao Yu 1857d1aa2453SChao Yu spin_lock_irqsave(&sbi->cp_lock, flags); 1858aaec2b1dSChao Yu __clear_ckpt_flags(F2FS_CKPT(sbi), f); 1859d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags); 1860aaec2b1dSChao Yu } 1861aaec2b1dSChao Yu 186222ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock) 186322ad0b6aSJaegeuk Kim { 1864d1aa2453SChao Yu unsigned long flags; 18650921835cSChao Yu unsigned char *nat_bits; 1866d1aa2453SChao Yu 1867a742fd41SJaegeuk Kim /* 1868a742fd41SJaegeuk Kim * In order to re-enable nat_bits we need to call fsck.f2fs by 1869a742fd41SJaegeuk Kim * set_sbi_flag(sbi, SBI_NEED_FSCK). But it may give huge cost, 1870a742fd41SJaegeuk Kim * so let's rely on regular fsck or unclean shutdown. 1871a742fd41SJaegeuk Kim */ 187222ad0b6aSJaegeuk Kim 187322ad0b6aSJaegeuk Kim if (lock) 1874d1aa2453SChao Yu spin_lock_irqsave(&sbi->cp_lock, flags); 187522ad0b6aSJaegeuk Kim __clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG); 18760921835cSChao Yu nat_bits = NM_I(sbi)->nat_bits; 187722ad0b6aSJaegeuk Kim NM_I(sbi)->nat_bits = NULL; 187822ad0b6aSJaegeuk Kim if (lock) 1879d1aa2453SChao Yu spin_unlock_irqrestore(&sbi->cp_lock, flags); 18800921835cSChao Yu 18810921835cSChao Yu kvfree(nat_bits); 188222ad0b6aSJaegeuk Kim } 188322ad0b6aSJaegeuk Kim 188422ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi, 188522ad0b6aSJaegeuk Kim struct cp_control *cpc) 188622ad0b6aSJaegeuk Kim { 188722ad0b6aSJaegeuk Kim bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG); 188822ad0b6aSJaegeuk Kim 1889c473f1a9SChao Yu return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set; 189022ad0b6aSJaegeuk Kim } 189122ad0b6aSJaegeuk Kim 1892e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi) 189339a53e0cSJaegeuk Kim { 1894b873b798SJaegeuk Kim down_read(&sbi->cp_rwsem); 189539a53e0cSJaegeuk Kim } 189639a53e0cSJaegeuk Kim 1897cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi) 1898cc15620bSJaegeuk Kim { 1899cc15620bSJaegeuk Kim return down_read_trylock(&sbi->cp_rwsem); 1900cc15620bSJaegeuk Kim } 1901cc15620bSJaegeuk Kim 1902e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi) 190339a53e0cSJaegeuk Kim { 1904b873b798SJaegeuk Kim up_read(&sbi->cp_rwsem); 190539936837SJaegeuk Kim } 190639936837SJaegeuk Kim 1907e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi) 190839936837SJaegeuk Kim { 1909b873b798SJaegeuk Kim down_write(&sbi->cp_rwsem); 191039936837SJaegeuk Kim } 191139936837SJaegeuk Kim 1912e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi) 191339936837SJaegeuk Kim { 1914b873b798SJaegeuk Kim up_write(&sbi->cp_rwsem); 191539a53e0cSJaegeuk Kim } 191639a53e0cSJaegeuk Kim 1917119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi) 1918119ee914SJaegeuk Kim { 1919119ee914SJaegeuk Kim int reason = CP_SYNC; 1920119ee914SJaegeuk Kim 1921119ee914SJaegeuk Kim if (test_opt(sbi, FASTBOOT)) 1922119ee914SJaegeuk Kim reason = CP_FASTBOOT; 1923119ee914SJaegeuk Kim if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) 1924119ee914SJaegeuk Kim reason = CP_UMOUNT; 1925119ee914SJaegeuk Kim return reason; 1926119ee914SJaegeuk Kim } 1927119ee914SJaegeuk Kim 1928119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason) 1929119ee914SJaegeuk Kim { 1930c473f1a9SChao Yu return (reason & (CP_UMOUNT | CP_FASTBOOT)); 1931119ee914SJaegeuk Kim } 1932119ee914SJaegeuk Kim 1933119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi) 1934119ee914SJaegeuk Kim { 1935aaec2b1dSChao Yu return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) || 1936aaec2b1dSChao Yu is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG)); 1937119ee914SJaegeuk Kim } 1938119ee914SJaegeuk Kim 193939a53e0cSJaegeuk Kim /* 194039a53e0cSJaegeuk Kim * Check whether the inode has blocks or not 194139a53e0cSJaegeuk Kim */ 194239a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode) 194339a53e0cSJaegeuk Kim { 19440eb0adadSChao Yu block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0; 19450eb0adadSChao Yu 1946000519f2SChao Yu return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block; 194739a53e0cSJaegeuk Kim } 194839a53e0cSJaegeuk Kim 19494bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs) 19504bc8e9bcSChao Yu { 19514bc8e9bcSChao Yu return ofs == XATTR_NODE_OFFSET; 19524bc8e9bcSChao Yu } 19534bc8e9bcSChao Yu 1954d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi, 1955a90a0884SJaegeuk Kim struct inode *inode, bool cap) 19567c2e5963SJaegeuk Kim { 1957d8a9a229SJaegeuk Kim if (!inode) 1958d8a9a229SJaegeuk Kim return true; 19597c2e5963SJaegeuk Kim if (!test_opt(sbi, RESERVE_ROOT)) 19607c2e5963SJaegeuk Kim return false; 1961d8a9a229SJaegeuk Kim if (IS_NOQUOTA(inode)) 1962d8a9a229SJaegeuk Kim return true; 196363189b78SChao Yu if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid())) 19647c2e5963SJaegeuk Kim return true; 196563189b78SChao Yu if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) && 196663189b78SChao Yu in_group_p(F2FS_OPTION(sbi).s_resgid)) 19677c2e5963SJaegeuk Kim return true; 1968a90a0884SJaegeuk Kim if (cap && capable(CAP_SYS_RESOURCE)) 1969162b27aeSJaegeuk Kim return true; 19707c2e5963SJaegeuk Kim return false; 19717c2e5963SJaegeuk Kim } 19727c2e5963SJaegeuk Kim 19730abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool); 19740abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi, 197546008c6dSChao Yu struct inode *inode, blkcnt_t *count) 197639a53e0cSJaegeuk Kim { 19770abd675eSChao Yu blkcnt_t diff = 0, release = 0; 1978daeb433eSChao Yu block_t avail_user_block_count; 19790abd675eSChao Yu int ret; 19800abd675eSChao Yu 19810abd675eSChao Yu ret = dquot_reserve_block(inode, *count); 19820abd675eSChao Yu if (ret) 19830abd675eSChao Yu return ret; 1984dd11a5dfSJaegeuk Kim 198555523519SChao Yu if (time_to_inject(sbi, FAULT_BLOCK)) { 1986c45d6002SChao Yu f2fs_show_injection_info(sbi, FAULT_BLOCK); 19870abd675eSChao Yu release = *count; 19889ea2f0beSChao Yu goto release_quota; 198955523519SChao Yu } 19907fa750a1SArnd Bergmann 1991dd11a5dfSJaegeuk Kim /* 1992dd11a5dfSJaegeuk Kim * let's increase this in prior to actual block count change in order 1993dd11a5dfSJaegeuk Kim * for f2fs_sync_file to avoid data races when deciding checkpoint. 1994dd11a5dfSJaegeuk Kim */ 1995dd11a5dfSJaegeuk Kim percpu_counter_add(&sbi->alloc_valid_block_count, (*count)); 199639a53e0cSJaegeuk Kim 199739a53e0cSJaegeuk Kim spin_lock(&sbi->stat_lock); 19982555a2d5SJaegeuk Kim sbi->total_valid_block_count += (block_t)(*count); 199980d42145SYunlong Song avail_user_block_count = sbi->user_block_count - 200080d42145SYunlong Song sbi->current_reserved_blocks; 20017e65be49SJaegeuk Kim 2002a90a0884SJaegeuk Kim if (!__allow_reserved_blocks(sbi, inode, true)) 200363189b78SChao Yu avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks; 2004a4c3ecaaSDaniel Rosenberg if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 2005a4c3ecaaSDaniel Rosenberg if (avail_user_block_count > sbi->unusable_block_count) 20064354994fSDaniel Rosenberg avail_user_block_count -= sbi->unusable_block_count; 2007a4c3ecaaSDaniel Rosenberg else 2008a4c3ecaaSDaniel Rosenberg avail_user_block_count = 0; 2009a4c3ecaaSDaniel Rosenberg } 2010daeb433eSChao Yu if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) { 2011daeb433eSChao Yu diff = sbi->total_valid_block_count - avail_user_block_count; 20127e65be49SJaegeuk Kim if (diff > *count) 20137e65be49SJaegeuk Kim diff = *count; 2014dd11a5dfSJaegeuk Kim *count -= diff; 20150abd675eSChao Yu release = diff; 20167e65be49SJaegeuk Kim sbi->total_valid_block_count -= diff; 201746008c6dSChao Yu if (!*count) { 201839a53e0cSJaegeuk Kim spin_unlock(&sbi->stat_lock); 20190abd675eSChao Yu goto enospc; 202039a53e0cSJaegeuk Kim } 202146008c6dSChao Yu } 202239a53e0cSJaegeuk Kim spin_unlock(&sbi->stat_lock); 202341382ec4SJaegeuk Kim 202436b877afSDaniel Rosenberg if (unlikely(release)) { 202536b877afSDaniel Rosenberg percpu_counter_sub(&sbi->alloc_valid_block_count, release); 20260abd675eSChao Yu dquot_release_reservation_block(inode, release); 202736b877afSDaniel Rosenberg } 20280abd675eSChao Yu f2fs_i_blocks_write(inode, *count, true, true); 20290abd675eSChao Yu return 0; 20300abd675eSChao Yu 20310abd675eSChao Yu enospc: 203236b877afSDaniel Rosenberg percpu_counter_sub(&sbi->alloc_valid_block_count, release); 20339ea2f0beSChao Yu release_quota: 20340abd675eSChao Yu dquot_release_reservation_block(inode, release); 20350abd675eSChao Yu return -ENOSPC; 203639a53e0cSJaegeuk Kim } 203739a53e0cSJaegeuk Kim 2038dcbb4c10SJoe Perches __printf(2, 3) 2039dcbb4c10SJoe Perches void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...); 2040dcbb4c10SJoe Perches 2041dcbb4c10SJoe Perches #define f2fs_err(sbi, fmt, ...) \ 2042dcbb4c10SJoe Perches f2fs_printk(sbi, KERN_ERR fmt, ##__VA_ARGS__) 2043dcbb4c10SJoe Perches #define f2fs_warn(sbi, fmt, ...) \ 2044dcbb4c10SJoe Perches f2fs_printk(sbi, KERN_WARNING fmt, ##__VA_ARGS__) 2045dcbb4c10SJoe Perches #define f2fs_notice(sbi, fmt, ...) \ 2046dcbb4c10SJoe Perches f2fs_printk(sbi, KERN_NOTICE fmt, ##__VA_ARGS__) 2047dcbb4c10SJoe Perches #define f2fs_info(sbi, fmt, ...) \ 2048dcbb4c10SJoe Perches f2fs_printk(sbi, KERN_INFO fmt, ##__VA_ARGS__) 2049dcbb4c10SJoe Perches #define f2fs_debug(sbi, fmt, ...) \ 2050dcbb4c10SJoe Perches f2fs_printk(sbi, KERN_DEBUG fmt, ##__VA_ARGS__) 2051dcbb4c10SJoe Perches 2052da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi, 205339a53e0cSJaegeuk Kim struct inode *inode, 20540eb0adadSChao Yu block_t count) 205539a53e0cSJaegeuk Kim { 20560eb0adadSChao Yu blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK; 20570eb0adadSChao Yu 205839a53e0cSJaegeuk Kim spin_lock(&sbi->stat_lock); 20599850cf4aSJaegeuk Kim f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count); 206039a53e0cSJaegeuk Kim sbi->total_valid_block_count -= (block_t)count; 206180d42145SYunlong Song if (sbi->reserved_blocks && 206280d42145SYunlong Song sbi->current_reserved_blocks < sbi->reserved_blocks) 206380d42145SYunlong Song sbi->current_reserved_blocks = min(sbi->reserved_blocks, 206480d42145SYunlong Song sbi->current_reserved_blocks + count); 206539a53e0cSJaegeuk Kim spin_unlock(&sbi->stat_lock); 20665e159cd3SChao Yu if (unlikely(inode->i_blocks < sectors)) { 2067dcbb4c10SJoe Perches f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu", 20685e159cd3SChao Yu inode->i_ino, 20695e159cd3SChao Yu (unsigned long long)inode->i_blocks, 20705e159cd3SChao Yu (unsigned long long)sectors); 20715e159cd3SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); 20725e159cd3SChao Yu return; 20735e159cd3SChao Yu } 20740abd675eSChao Yu f2fs_i_blocks_write(inode, count, false, true); 207539a53e0cSJaegeuk Kim } 207639a53e0cSJaegeuk Kim 207739a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type) 207839a53e0cSJaegeuk Kim { 207935782b23SJaegeuk Kim atomic_inc(&sbi->nr_pages[count_type]); 20807c4abcbeSChao Yu 208120109873SChao Yu if (count_type == F2FS_DIRTY_DENTS || 208220109873SChao Yu count_type == F2FS_DIRTY_NODES || 208320109873SChao Yu count_type == F2FS_DIRTY_META || 208420109873SChao Yu count_type == F2FS_DIRTY_QDATA || 208520109873SChao Yu count_type == F2FS_DIRTY_IMETA) 2086caf0047eSChao Yu set_sbi_flag(sbi, SBI_IS_DIRTY); 208739a53e0cSJaegeuk Kim } 208839a53e0cSJaegeuk Kim 2089a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode) 209039a53e0cSJaegeuk Kim { 2091204706c7SJaegeuk Kim atomic_inc(&F2FS_I(inode)->dirty_pages); 2092c227f912SChao Yu inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ? 2093c227f912SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA); 20942c8a4a28SJaegeuk Kim if (IS_NOQUOTA(inode)) 20952c8a4a28SJaegeuk Kim inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA); 209639a53e0cSJaegeuk Kim } 209739a53e0cSJaegeuk Kim 209839a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type) 209939a53e0cSJaegeuk Kim { 210035782b23SJaegeuk Kim atomic_dec(&sbi->nr_pages[count_type]); 210139a53e0cSJaegeuk Kim } 210239a53e0cSJaegeuk Kim 2103a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode) 210439a53e0cSJaegeuk Kim { 21055ac9f36fSChao Yu if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && 21065ac9f36fSChao Yu !S_ISLNK(inode->i_mode)) 21071fe54f9dSJaegeuk Kim return; 21081fe54f9dSJaegeuk Kim 2109204706c7SJaegeuk Kim atomic_dec(&F2FS_I(inode)->dirty_pages); 2110c227f912SChao Yu dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ? 2111c227f912SChao Yu F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA); 21122c8a4a28SJaegeuk Kim if (IS_NOQUOTA(inode)) 21132c8a4a28SJaegeuk Kim dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA); 211439a53e0cSJaegeuk Kim } 211539a53e0cSJaegeuk Kim 2116523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type) 211739a53e0cSJaegeuk Kim { 211835782b23SJaegeuk Kim return atomic_read(&sbi->nr_pages[count_type]); 211939a53e0cSJaegeuk Kim } 212039a53e0cSJaegeuk Kim 2121204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode) 2122f8b2c1f9SJaegeuk Kim { 2123204706c7SJaegeuk Kim return atomic_read(&F2FS_I(inode)->dirty_pages); 2124f8b2c1f9SJaegeuk Kim } 2125f8b2c1f9SJaegeuk Kim 21265ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type) 21275ac206cfSNamjae Jeon { 21283519e3f9SChao Yu unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg; 2129523be8a6SJaegeuk Kim unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >> 2130523be8a6SJaegeuk Kim sbi->log_blocks_per_seg; 2131523be8a6SJaegeuk Kim 2132523be8a6SJaegeuk Kim return segs / sbi->segs_per_sec; 21335ac206cfSNamjae Jeon } 21345ac206cfSNamjae Jeon 213539a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi) 213639a53e0cSJaegeuk Kim { 21378b8343faSJaegeuk Kim return sbi->total_valid_block_count; 213839a53e0cSJaegeuk Kim } 213939a53e0cSJaegeuk Kim 2140f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi) 2141f83a2584SYunlei He { 2142f83a2584SYunlei He return sbi->discard_blks; 2143f83a2584SYunlei He } 2144f83a2584SYunlei He 214539a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag) 214639a53e0cSJaegeuk Kim { 214739a53e0cSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 214839a53e0cSJaegeuk Kim 214939a53e0cSJaegeuk Kim /* return NAT or SIT bitmap */ 215039a53e0cSJaegeuk Kim if (flag == NAT_BITMAP) 215139a53e0cSJaegeuk Kim return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize); 215239a53e0cSJaegeuk Kim else if (flag == SIT_BITMAP) 215339a53e0cSJaegeuk Kim return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize); 215439a53e0cSJaegeuk Kim 215539a53e0cSJaegeuk Kim return 0; 215639a53e0cSJaegeuk Kim } 215739a53e0cSJaegeuk Kim 215855141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi) 215955141486SWanpeng Li { 216055141486SWanpeng Li return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload); 216155141486SWanpeng Li } 216255141486SWanpeng Li 216339a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag) 216439a53e0cSJaegeuk Kim { 216539a53e0cSJaegeuk Kim struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 21661dbe4152SChangman Lee int offset; 21671dbe4152SChangman Lee 2168199bc3feSChao Yu if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) { 2169199bc3feSChao Yu offset = (flag == SIT_BITMAP) ? 2170199bc3feSChao Yu le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0; 2171b471eb99SChao Yu /* 2172b471eb99SChao Yu * if large_nat_bitmap feature is enabled, leave checksum 2173b471eb99SChao Yu * protection for all nat/sit bitmaps. 2174b471eb99SChao Yu */ 2175b471eb99SChao Yu return &ckpt->sit_nat_version_bitmap + offset + sizeof(__le32); 2176199bc3feSChao Yu } 2177199bc3feSChao Yu 217855141486SWanpeng Li if (__cp_payload(sbi) > 0) { 21791dbe4152SChangman Lee if (flag == NAT_BITMAP) 21801dbe4152SChangman Lee return &ckpt->sit_nat_version_bitmap; 21811dbe4152SChangman Lee else 218265b85cccSJaegeuk Kim return (unsigned char *)ckpt + F2FS_BLKSIZE; 21831dbe4152SChangman Lee } else { 21841dbe4152SChangman Lee offset = (flag == NAT_BITMAP) ? 218525ca923bSJaegeuk Kim le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0; 218639a53e0cSJaegeuk Kim return &ckpt->sit_nat_version_bitmap + offset; 218739a53e0cSJaegeuk Kim } 21881dbe4152SChangman Lee } 218939a53e0cSJaegeuk Kim 219039a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi) 219139a53e0cSJaegeuk Kim { 21928508e44aSJaegeuk Kim block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr); 219339a53e0cSJaegeuk Kim 21948508e44aSJaegeuk Kim if (sbi->cur_cp_pack == 2) 219539a53e0cSJaegeuk Kim start_addr += sbi->blocks_per_seg; 219639a53e0cSJaegeuk Kim return start_addr; 219739a53e0cSJaegeuk Kim } 219839a53e0cSJaegeuk Kim 21998508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi) 22008508e44aSJaegeuk Kim { 22018508e44aSJaegeuk Kim block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr); 22028508e44aSJaegeuk Kim 22038508e44aSJaegeuk Kim if (sbi->cur_cp_pack == 1) 22048508e44aSJaegeuk Kim start_addr += sbi->blocks_per_seg; 22058508e44aSJaegeuk Kim return start_addr; 22068508e44aSJaegeuk Kim } 22078508e44aSJaegeuk Kim 22088508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi) 22098508e44aSJaegeuk Kim { 22108508e44aSJaegeuk Kim sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1; 22118508e44aSJaegeuk Kim } 22128508e44aSJaegeuk Kim 221339a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi) 221439a53e0cSJaegeuk Kim { 221539a53e0cSJaegeuk Kim return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum); 221639a53e0cSJaegeuk Kim } 221739a53e0cSJaegeuk Kim 22180abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi, 2219000519f2SChao Yu struct inode *inode, bool is_inode) 222039a53e0cSJaegeuk Kim { 222139a53e0cSJaegeuk Kim block_t valid_block_count; 2222a4c3ecaaSDaniel Rosenberg unsigned int valid_node_count, user_block_count; 2223af033b2aSChao Yu int err; 22240abd675eSChao Yu 2225af033b2aSChao Yu if (is_inode) { 2226af033b2aSChao Yu if (inode) { 2227af033b2aSChao Yu err = dquot_alloc_inode(inode); 2228af033b2aSChao Yu if (err) 2229af033b2aSChao Yu return err; 2230af033b2aSChao Yu } 2231af033b2aSChao Yu } else { 2232af033b2aSChao Yu err = dquot_reserve_block(inode, 1); 2233af033b2aSChao Yu if (err) 2234af033b2aSChao Yu return err; 22350abd675eSChao Yu } 223639a53e0cSJaegeuk Kim 2237812c6056SChao Yu if (time_to_inject(sbi, FAULT_BLOCK)) { 2238c45d6002SChao Yu f2fs_show_injection_info(sbi, FAULT_BLOCK); 2239812c6056SChao Yu goto enospc; 2240812c6056SChao Yu } 2241812c6056SChao Yu 224239a53e0cSJaegeuk Kim spin_lock(&sbi->stat_lock); 224339a53e0cSJaegeuk Kim 22447e65be49SJaegeuk Kim valid_block_count = sbi->total_valid_block_count + 22457e65be49SJaegeuk Kim sbi->current_reserved_blocks + 1; 22467e65be49SJaegeuk Kim 2247a90a0884SJaegeuk Kim if (!__allow_reserved_blocks(sbi, inode, false)) 224863189b78SChao Yu valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks; 2249a4c3ecaaSDaniel Rosenberg user_block_count = sbi->user_block_count; 22504354994fSDaniel Rosenberg if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) 2251a4c3ecaaSDaniel Rosenberg user_block_count -= sbi->unusable_block_count; 22527e65be49SJaegeuk Kim 2253a4c3ecaaSDaniel Rosenberg if (unlikely(valid_block_count > user_block_count)) { 225439a53e0cSJaegeuk Kim spin_unlock(&sbi->stat_lock); 22550abd675eSChao Yu goto enospc; 225639a53e0cSJaegeuk Kim } 225739a53e0cSJaegeuk Kim 2258ef86d709SGu Zheng valid_node_count = sbi->total_valid_node_count + 1; 2259cfb271d4SChao Yu if (unlikely(valid_node_count > sbi->total_node_count)) { 226039a53e0cSJaegeuk Kim spin_unlock(&sbi->stat_lock); 22610abd675eSChao Yu goto enospc; 226239a53e0cSJaegeuk Kim } 226339a53e0cSJaegeuk Kim 2264ef86d709SGu Zheng sbi->total_valid_node_count++; 2265ef86d709SGu Zheng sbi->total_valid_block_count++; 226639a53e0cSJaegeuk Kim spin_unlock(&sbi->stat_lock); 226739a53e0cSJaegeuk Kim 22680abd675eSChao Yu if (inode) { 22690abd675eSChao Yu if (is_inode) 22700abd675eSChao Yu f2fs_mark_inode_dirty_sync(inode, true); 22710abd675eSChao Yu else 22720abd675eSChao Yu f2fs_i_blocks_write(inode, 1, true, true); 22730abd675eSChao Yu } 22740abd675eSChao Yu 227541382ec4SJaegeuk Kim percpu_counter_inc(&sbi->alloc_valid_block_count); 22760abd675eSChao Yu return 0; 22770abd675eSChao Yu 22780abd675eSChao Yu enospc: 2279af033b2aSChao Yu if (is_inode) { 2280af033b2aSChao Yu if (inode) 2281af033b2aSChao Yu dquot_free_inode(inode); 2282af033b2aSChao Yu } else { 22830abd675eSChao Yu dquot_release_reservation_block(inode, 1); 2284af033b2aSChao Yu } 22850abd675eSChao Yu return -ENOSPC; 228639a53e0cSJaegeuk Kim } 228739a53e0cSJaegeuk Kim 228839a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi, 2289000519f2SChao Yu struct inode *inode, bool is_inode) 229039a53e0cSJaegeuk Kim { 229139a53e0cSJaegeuk Kim spin_lock(&sbi->stat_lock); 229239a53e0cSJaegeuk Kim 22939850cf4aSJaegeuk Kim f2fs_bug_on(sbi, !sbi->total_valid_block_count); 22949850cf4aSJaegeuk Kim f2fs_bug_on(sbi, !sbi->total_valid_node_count); 229539a53e0cSJaegeuk Kim 2296ef86d709SGu Zheng sbi->total_valid_node_count--; 2297ef86d709SGu Zheng sbi->total_valid_block_count--; 229880d42145SYunlong Song if (sbi->reserved_blocks && 229980d42145SYunlong Song sbi->current_reserved_blocks < sbi->reserved_blocks) 230080d42145SYunlong Song sbi->current_reserved_blocks++; 230139a53e0cSJaegeuk Kim 230239a53e0cSJaegeuk Kim spin_unlock(&sbi->stat_lock); 23030abd675eSChao Yu 2304ea6d7e72SChao Yu if (is_inode) { 2305af033b2aSChao Yu dquot_free_inode(inode); 2306ea6d7e72SChao Yu } else { 2307ea6d7e72SChao Yu if (unlikely(inode->i_blocks == 0)) { 2308097a7686SChao Yu f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu", 2309ea6d7e72SChao Yu inode->i_ino, 2310ea6d7e72SChao Yu (unsigned long long)inode->i_blocks); 2311ea6d7e72SChao Yu set_sbi_flag(sbi, SBI_NEED_FSCK); 2312ea6d7e72SChao Yu return; 2313ea6d7e72SChao Yu } 23140abd675eSChao Yu f2fs_i_blocks_write(inode, 1, false, true); 231539a53e0cSJaegeuk Kim } 2316ea6d7e72SChao Yu } 231739a53e0cSJaegeuk Kim 231839a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi) 231939a53e0cSJaegeuk Kim { 23208b8343faSJaegeuk Kim return sbi->total_valid_node_count; 232139a53e0cSJaegeuk Kim } 232239a53e0cSJaegeuk Kim 232339a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi) 232439a53e0cSJaegeuk Kim { 2325513c5f37SJaegeuk Kim percpu_counter_inc(&sbi->total_valid_inode_count); 232639a53e0cSJaegeuk Kim } 232739a53e0cSJaegeuk Kim 23280e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi) 232939a53e0cSJaegeuk Kim { 2330513c5f37SJaegeuk Kim percpu_counter_dec(&sbi->total_valid_inode_count); 233139a53e0cSJaegeuk Kim } 233239a53e0cSJaegeuk Kim 2333513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi) 233439a53e0cSJaegeuk Kim { 2335513c5f37SJaegeuk Kim return percpu_counter_sum_positive(&sbi->total_valid_inode_count); 233639a53e0cSJaegeuk Kim } 233739a53e0cSJaegeuk Kim 2338a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping, 2339a56c7c6fSJaegeuk Kim pgoff_t index, bool for_write) 2340a56c7c6fSJaegeuk Kim { 234182cf4f13SChao Yu struct page *page; 2342cac5a3d8SDongOh Shin 23437fa750a1SArnd Bergmann if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) { 234482cf4f13SChao Yu if (!for_write) 234582cf4f13SChao Yu page = find_get_page_flags(mapping, index, 234682cf4f13SChao Yu FGP_LOCK | FGP_ACCESSED); 234782cf4f13SChao Yu else 234882cf4f13SChao Yu page = find_lock_page(mapping, index); 2349c41f3cc3SJaegeuk Kim if (page) 2350c41f3cc3SJaegeuk Kim return page; 2351c41f3cc3SJaegeuk Kim 235255523519SChao Yu if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) { 2353c45d6002SChao Yu f2fs_show_injection_info(F2FS_M_SB(mapping), 2354c45d6002SChao Yu FAULT_PAGE_ALLOC); 2355c41f3cc3SJaegeuk Kim return NULL; 235655523519SChao Yu } 23577fa750a1SArnd Bergmann } 23587fa750a1SArnd Bergmann 2359a56c7c6fSJaegeuk Kim if (!for_write) 2360a56c7c6fSJaegeuk Kim return grab_cache_page(mapping, index); 2361a56c7c6fSJaegeuk Kim return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS); 2362a56c7c6fSJaegeuk Kim } 2363a56c7c6fSJaegeuk Kim 236401eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page( 236501eccef7SChao Yu struct address_space *mapping, pgoff_t index, 236601eccef7SChao Yu int fgp_flags, gfp_t gfp_mask) 236701eccef7SChao Yu { 236801eccef7SChao Yu if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) { 2369c45d6002SChao Yu f2fs_show_injection_info(F2FS_M_SB(mapping), FAULT_PAGE_GET); 237001eccef7SChao Yu return NULL; 237101eccef7SChao Yu } 23727fa750a1SArnd Bergmann 237301eccef7SChao Yu return pagecache_get_page(mapping, index, fgp_flags, gfp_mask); 237401eccef7SChao Yu } 237501eccef7SChao Yu 23766e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst) 23776e2c64adSJaegeuk Kim { 23786e2c64adSJaegeuk Kim char *src_kaddr = kmap(src); 23796e2c64adSJaegeuk Kim char *dst_kaddr = kmap(dst); 23806e2c64adSJaegeuk Kim 23816e2c64adSJaegeuk Kim memcpy(dst_kaddr, src_kaddr, PAGE_SIZE); 23826e2c64adSJaegeuk Kim kunmap(dst); 23836e2c64adSJaegeuk Kim kunmap(src); 23846e2c64adSJaegeuk Kim } 23856e2c64adSJaegeuk Kim 238639a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock) 238739a53e0cSJaegeuk Kim { 2388031fa8ccSJaegeuk Kim if (!page) 238939a53e0cSJaegeuk Kim return; 239039a53e0cSJaegeuk Kim 239139a53e0cSJaegeuk Kim if (unlock) { 23929850cf4aSJaegeuk Kim f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page)); 239339a53e0cSJaegeuk Kim unlock_page(page); 239439a53e0cSJaegeuk Kim } 239509cbfeafSKirill A. Shutemov put_page(page); 239639a53e0cSJaegeuk Kim } 239739a53e0cSJaegeuk Kim 239839a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn) 239939a53e0cSJaegeuk Kim { 240039a53e0cSJaegeuk Kim if (dn->node_page) 240139a53e0cSJaegeuk Kim f2fs_put_page(dn->node_page, 1); 240239a53e0cSJaegeuk Kim if (dn->inode_page && dn->node_page != dn->inode_page) 240339a53e0cSJaegeuk Kim f2fs_put_page(dn->inode_page, 0); 240439a53e0cSJaegeuk Kim dn->node_page = NULL; 240539a53e0cSJaegeuk Kim dn->inode_page = NULL; 240639a53e0cSJaegeuk Kim } 240739a53e0cSJaegeuk Kim 240839a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name, 2409e8512d2eSGu Zheng size_t size) 241039a53e0cSJaegeuk Kim { 2411e8512d2eSGu Zheng return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL); 241239a53e0cSJaegeuk Kim } 241339a53e0cSJaegeuk Kim 24147bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep, 24157bd59381SGu Zheng gfp_t flags) 24167bd59381SGu Zheng { 24177bd59381SGu Zheng void *entry; 24187bd59381SGu Zheng 241980c54505SJaegeuk Kim entry = kmem_cache_alloc(cachep, flags); 242080c54505SJaegeuk Kim if (!entry) 242180c54505SJaegeuk Kim entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL); 24227bd59381SGu Zheng return entry; 24237bd59381SGu Zheng } 24247bd59381SGu Zheng 2425493720a4SChao Yu static inline bool is_inflight_io(struct f2fs_sb_info *sbi, int type) 24265f9abab4SJaegeuk Kim { 24275f9abab4SJaegeuk Kim if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) || 24285f9abab4SJaegeuk Kim get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) || 2429fef4129eSChao Yu get_pages(sbi, F2FS_WB_CP_DATA) || 2430fef4129eSChao Yu get_pages(sbi, F2FS_DIO_READ) || 243111ac8ef8SJaegeuk Kim get_pages(sbi, F2FS_DIO_WRITE)) 2432493720a4SChao Yu return true; 243311ac8ef8SJaegeuk Kim 243456659ce8SSahitya Tummala if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info && 243511ac8ef8SJaegeuk Kim atomic_read(&SM_I(sbi)->dcc_info->queued_discard)) 2436493720a4SChao Yu return true; 243711ac8ef8SJaegeuk Kim 243811ac8ef8SJaegeuk Kim if (SM_I(sbi) && SM_I(sbi)->fcc_info && 243972691af6SJaegeuk Kim atomic_read(&SM_I(sbi)->fcc_info->queued_flush)) 2440493720a4SChao Yu return true; 2441493720a4SChao Yu return false; 2442493720a4SChao Yu } 2443493720a4SChao Yu 2444493720a4SChao Yu static inline bool is_idle(struct f2fs_sb_info *sbi, int type) 2445493720a4SChao Yu { 2446493720a4SChao Yu if (sbi->gc_mode == GC_URGENT_HIGH) 2447493720a4SChao Yu return true; 2448493720a4SChao Yu 2449493720a4SChao Yu if (is_inflight_io(sbi, type)) 24505f9abab4SJaegeuk Kim return false; 245111ac8ef8SJaegeuk Kim 24520e5e8111SDaeho Jeong if (sbi->gc_mode == GC_URGENT_LOW && 24530e5e8111SDaeho Jeong (type == DISCARD_TIME || type == GC_TIME)) 24540e5e8111SDaeho Jeong return true; 24550e5e8111SDaeho Jeong 24565f9abab4SJaegeuk Kim return f2fs_time_over(sbi, type); 24575f9abab4SJaegeuk Kim } 24585f9abab4SJaegeuk Kim 24599be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root, 24609be32d72SJaegeuk Kim unsigned long index, void *item) 24619be32d72SJaegeuk Kim { 24629be32d72SJaegeuk Kim while (radix_tree_insert(root, index, item)) 24639be32d72SJaegeuk Kim cond_resched(); 24649be32d72SJaegeuk Kim } 24659be32d72SJaegeuk Kim 246639a53e0cSJaegeuk Kim #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino) 246739a53e0cSJaegeuk Kim 246839a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page) 246939a53e0cSJaegeuk Kim { 247045590710SGu Zheng struct f2fs_node *p = F2FS_NODE(page); 2471cac5a3d8SDongOh Shin 247239a53e0cSJaegeuk Kim return RAW_IS_INODE(p); 247339a53e0cSJaegeuk Kim } 247439a53e0cSJaegeuk Kim 24757a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i) 24767a2af766SChao Yu { 24777a2af766SChao Yu return (i->i_inline & F2FS_EXTRA_ATTR) ? 24787a2af766SChao Yu (le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0; 24797a2af766SChao Yu } 24807a2af766SChao Yu 248139a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node) 248239a53e0cSJaegeuk Kim { 248339a53e0cSJaegeuk Kim return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr; 248439a53e0cSJaegeuk Kim } 248539a53e0cSJaegeuk Kim 24867a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode); 2487a2ced1ceSChao Yu static inline block_t data_blkaddr(struct inode *inode, 24887a2af766SChao Yu struct page *node_page, unsigned int offset) 248939a53e0cSJaegeuk Kim { 249039a53e0cSJaegeuk Kim struct f2fs_node *raw_node; 249139a53e0cSJaegeuk Kim __le32 *addr_array; 24927a2af766SChao Yu int base = 0; 24937a2af766SChao Yu bool is_inode = IS_INODE(node_page); 2494cac5a3d8SDongOh Shin 249545590710SGu Zheng raw_node = F2FS_NODE(node_page); 24967a2af766SChao Yu 2497979f492fSLiFan if (is_inode) { 2498979f492fSLiFan if (!inode) 24997a88ddb5SChao Yu /* from GC path only */ 25007a2af766SChao Yu base = offset_in_addr(&raw_node->i); 2501979f492fSLiFan else if (f2fs_has_extra_attr(inode)) 25027a2af766SChao Yu base = get_extra_isize(inode); 25037a2af766SChao Yu } 25047a2af766SChao Yu 250539a53e0cSJaegeuk Kim addr_array = blkaddr_in_node(raw_node); 25067a2af766SChao Yu return le32_to_cpu(addr_array[base + offset]); 250739a53e0cSJaegeuk Kim } 250839a53e0cSJaegeuk Kim 2509a2ced1ceSChao Yu static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn) 2510a2ced1ceSChao Yu { 2511a2ced1ceSChao Yu return data_blkaddr(dn->inode, dn->node_page, dn->ofs_in_node); 2512a2ced1ceSChao Yu } 2513a2ced1ceSChao Yu 251439a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr) 251539a53e0cSJaegeuk Kim { 251639a53e0cSJaegeuk Kim int mask; 251739a53e0cSJaegeuk Kim 251839a53e0cSJaegeuk Kim addr += (nr >> 3); 251939a53e0cSJaegeuk Kim mask = 1 << (7 - (nr & 0x07)); 252039a53e0cSJaegeuk Kim return mask & *addr; 252139a53e0cSJaegeuk Kim } 252239a53e0cSJaegeuk Kim 2523a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr) 2524a66cdd98SJaegeuk Kim { 2525a66cdd98SJaegeuk Kim int mask; 2526a66cdd98SJaegeuk Kim 2527a66cdd98SJaegeuk Kim addr += (nr >> 3); 2528a66cdd98SJaegeuk Kim mask = 1 << (7 - (nr & 0x07)); 2529a66cdd98SJaegeuk Kim *addr |= mask; 2530a66cdd98SJaegeuk Kim } 2531a66cdd98SJaegeuk Kim 2532a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr) 2533a66cdd98SJaegeuk Kim { 2534a66cdd98SJaegeuk Kim int mask; 2535a66cdd98SJaegeuk Kim 2536a66cdd98SJaegeuk Kim addr += (nr >> 3); 2537a66cdd98SJaegeuk Kim mask = 1 << (7 - (nr & 0x07)); 2538a66cdd98SJaegeuk Kim *addr &= ~mask; 2539a66cdd98SJaegeuk Kim } 2540a66cdd98SJaegeuk Kim 254152aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr) 254239a53e0cSJaegeuk Kim { 254339a53e0cSJaegeuk Kim int mask; 254439a53e0cSJaegeuk Kim int ret; 254539a53e0cSJaegeuk Kim 254639a53e0cSJaegeuk Kim addr += (nr >> 3); 254739a53e0cSJaegeuk Kim mask = 1 << (7 - (nr & 0x07)); 254839a53e0cSJaegeuk Kim ret = mask & *addr; 254939a53e0cSJaegeuk Kim *addr |= mask; 255039a53e0cSJaegeuk Kim return ret; 255139a53e0cSJaegeuk Kim } 255239a53e0cSJaegeuk Kim 255352aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr) 255439a53e0cSJaegeuk Kim { 255539a53e0cSJaegeuk Kim int mask; 255639a53e0cSJaegeuk Kim int ret; 255739a53e0cSJaegeuk Kim 255839a53e0cSJaegeuk Kim addr += (nr >> 3); 255939a53e0cSJaegeuk Kim mask = 1 << (7 - (nr & 0x07)); 256039a53e0cSJaegeuk Kim ret = mask & *addr; 256139a53e0cSJaegeuk Kim *addr &= ~mask; 256239a53e0cSJaegeuk Kim return ret; 256339a53e0cSJaegeuk Kim } 256439a53e0cSJaegeuk Kim 2565c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr) 2566c6ac4c0eSGu Zheng { 2567c6ac4c0eSGu Zheng int mask; 2568c6ac4c0eSGu Zheng 2569c6ac4c0eSGu Zheng addr += (nr >> 3); 2570c6ac4c0eSGu Zheng mask = 1 << (7 - (nr & 0x07)); 2571c6ac4c0eSGu Zheng *addr ^= mask; 2572c6ac4c0eSGu Zheng } 2573c6ac4c0eSGu Zheng 257459c84408SChao Yu /* 257536098557SEric Biggers * On-disk inode flags (f2fs_inode::i_flags) 257659c84408SChao Yu */ 25774c8ff709SChao Yu #define F2FS_COMPR_FL 0x00000004 /* Compress file */ 257859c84408SChao Yu #define F2FS_SYNC_FL 0x00000008 /* Synchronous updates */ 257959c84408SChao Yu #define F2FS_IMMUTABLE_FL 0x00000010 /* Immutable file */ 258059c84408SChao Yu #define F2FS_APPEND_FL 0x00000020 /* writes to file may only append */ 258159c84408SChao Yu #define F2FS_NODUMP_FL 0x00000040 /* do not dump file */ 258259c84408SChao Yu #define F2FS_NOATIME_FL 0x00000080 /* do not update atime */ 25834c8ff709SChao Yu #define F2FS_NOCOMP_FL 0x00000400 /* Don't compress */ 258459c84408SChao Yu #define F2FS_INDEX_FL 0x00001000 /* hash-indexed directory */ 258559c84408SChao Yu #define F2FS_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */ 258659c84408SChao Yu #define F2FS_PROJINHERIT_FL 0x20000000 /* Create with parents projid */ 25872c2eb7a3SDaniel Rosenberg #define F2FS_CASEFOLD_FL 0x40000000 /* Casefolded file */ 258859c84408SChao Yu 258959c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */ 259036098557SEric Biggers #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \ 25912c2eb7a3SDaniel Rosenberg F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \ 25924c8ff709SChao Yu F2FS_CASEFOLD_FL | F2FS_COMPR_FL | F2FS_NOCOMP_FL) 259359c84408SChao Yu 259459c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */ 25952c2eb7a3SDaniel Rosenberg #define F2FS_REG_FLMASK (~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \ 25962c2eb7a3SDaniel Rosenberg F2FS_CASEFOLD_FL)) 259759c84408SChao Yu 259859c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */ 259959c84408SChao Yu #define F2FS_OTHER_FLMASK (F2FS_NODUMP_FL | F2FS_NOATIME_FL) 26005c57132eSChao Yu 26015c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags) 26025c57132eSChao Yu { 26035c57132eSChao Yu if (S_ISDIR(mode)) 26045c57132eSChao Yu return flags; 26055c57132eSChao Yu else if (S_ISREG(mode)) 26065c57132eSChao Yu return flags & F2FS_REG_FLMASK; 26075c57132eSChao Yu else 26085c57132eSChao Yu return flags & F2FS_OTHER_FLMASK; 26095c57132eSChao Yu } 26105c57132eSChao Yu 2611205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode, 2612205b9822SJaegeuk Kim int flag, bool set) 261339a53e0cSJaegeuk Kim { 2614205b9822SJaegeuk Kim switch (flag) { 2615205b9822SJaegeuk Kim case FI_INLINE_XATTR: 2616205b9822SJaegeuk Kim case FI_INLINE_DATA: 2617205b9822SJaegeuk Kim case FI_INLINE_DENTRY: 26189ac1e2d8SDaeho Jeong case FI_NEW_INODE: 2619205b9822SJaegeuk Kim if (set) 2620205b9822SJaegeuk Kim return; 2621df561f66SGustavo A. R. Silva fallthrough; 2622205b9822SJaegeuk Kim case FI_DATA_EXIST: 2623205b9822SJaegeuk Kim case FI_INLINE_DOTS: 26241ad71a27SJaegeuk Kim case FI_PIN_FILE: 26257c45729aSJaegeuk Kim f2fs_mark_inode_dirty_sync(inode, true); 2626205b9822SJaegeuk Kim } 262739a53e0cSJaegeuk Kim } 262839a53e0cSJaegeuk Kim 262991942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag) 263039a53e0cSJaegeuk Kim { 263158f7e00fSYufen Yu set_bit(flag, F2FS_I(inode)->flags); 2632205b9822SJaegeuk Kim __mark_inode_dirty_flag(inode, flag, true); 263339a53e0cSJaegeuk Kim } 263439a53e0cSJaegeuk Kim 263591942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag) 263639a53e0cSJaegeuk Kim { 26377653b9d8SChao Yu return test_bit(flag, F2FS_I(inode)->flags); 263839a53e0cSJaegeuk Kim } 263939a53e0cSJaegeuk Kim 264091942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag) 264139a53e0cSJaegeuk Kim { 264258f7e00fSYufen Yu clear_bit(flag, F2FS_I(inode)->flags); 2643205b9822SJaegeuk Kim __mark_inode_dirty_flag(inode, flag, false); 264439a53e0cSJaegeuk Kim } 264539a53e0cSJaegeuk Kim 264695ae251fSEric Biggers static inline bool f2fs_verity_in_progress(struct inode *inode) 264795ae251fSEric Biggers { 264895ae251fSEric Biggers return IS_ENABLED(CONFIG_FS_VERITY) && 264995ae251fSEric Biggers is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS); 265095ae251fSEric Biggers } 265195ae251fSEric Biggers 265291942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode) 2653444c580fSJaegeuk Kim { 265491942321SJaegeuk Kim F2FS_I(inode)->i_acl_mode = mode; 265591942321SJaegeuk Kim set_inode_flag(inode, FI_ACL_MODE); 26567c45729aSJaegeuk Kim f2fs_mark_inode_dirty_sync(inode, false); 265739a53e0cSJaegeuk Kim } 265839a53e0cSJaegeuk Kim 2659a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc) 2660a1961246SJaegeuk Kim { 2661a1961246SJaegeuk Kim if (inc) 2662a1961246SJaegeuk Kim inc_nlink(inode); 2663a1961246SJaegeuk Kim else 2664a1961246SJaegeuk Kim drop_nlink(inode); 26657c45729aSJaegeuk Kim f2fs_mark_inode_dirty_sync(inode, true); 2666a1961246SJaegeuk Kim } 2667a1961246SJaegeuk Kim 26688edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode, 26690abd675eSChao Yu block_t diff, bool add, bool claim) 26708edd03c8SJaegeuk Kim { 267126de9b11SJaegeuk Kim bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE); 267226de9b11SJaegeuk Kim bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER); 267326de9b11SJaegeuk Kim 26740abd675eSChao Yu /* add = 1, claim = 1 should be dquot_reserve_block in pair */ 26750abd675eSChao Yu if (add) { 26760abd675eSChao Yu if (claim) 26770abd675eSChao Yu dquot_claim_block(inode, diff); 26780abd675eSChao Yu else 26790abd675eSChao Yu dquot_alloc_block_nofail(inode, diff); 26800abd675eSChao Yu } else { 26810abd675eSChao Yu dquot_free_block(inode, diff); 26820abd675eSChao Yu } 26830abd675eSChao Yu 26847c45729aSJaegeuk Kim f2fs_mark_inode_dirty_sync(inode, true); 268526de9b11SJaegeuk Kim if (clean || recover) 268626de9b11SJaegeuk Kim set_inode_flag(inode, FI_AUTO_RECOVER); 26878edd03c8SJaegeuk Kim } 26888edd03c8SJaegeuk Kim 2689fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size) 2690fc9581c8SJaegeuk Kim { 269126de9b11SJaegeuk Kim bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE); 269226de9b11SJaegeuk Kim bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER); 269326de9b11SJaegeuk Kim 2694fc9581c8SJaegeuk Kim if (i_size_read(inode) == i_size) 2695fc9581c8SJaegeuk Kim return; 2696fc9581c8SJaegeuk Kim 2697fc9581c8SJaegeuk Kim i_size_write(inode, i_size); 26987c45729aSJaegeuk Kim f2fs_mark_inode_dirty_sync(inode, true); 269926de9b11SJaegeuk Kim if (clean || recover) 270026de9b11SJaegeuk Kim set_inode_flag(inode, FI_AUTO_RECOVER); 270126de9b11SJaegeuk Kim } 270226de9b11SJaegeuk Kim 2703205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth) 2704205b9822SJaegeuk Kim { 2705205b9822SJaegeuk Kim F2FS_I(inode)->i_current_depth = depth; 27067c45729aSJaegeuk Kim f2fs_mark_inode_dirty_sync(inode, true); 2707205b9822SJaegeuk Kim } 2708205b9822SJaegeuk Kim 27091ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode, 27101ad71a27SJaegeuk Kim unsigned int count) 27111ad71a27SJaegeuk Kim { 27122ef79ecbSChao Yu F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count; 27131ad71a27SJaegeuk Kim f2fs_mark_inode_dirty_sync(inode, true); 27141ad71a27SJaegeuk Kim } 27151ad71a27SJaegeuk Kim 2716205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid) 2717205b9822SJaegeuk Kim { 2718205b9822SJaegeuk Kim F2FS_I(inode)->i_xattr_nid = xnid; 27197c45729aSJaegeuk Kim f2fs_mark_inode_dirty_sync(inode, true); 2720205b9822SJaegeuk Kim } 2721205b9822SJaegeuk Kim 2722205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino) 2723205b9822SJaegeuk Kim { 2724205b9822SJaegeuk Kim F2FS_I(inode)->i_pino = pino; 27257c45729aSJaegeuk Kim f2fs_mark_inode_dirty_sync(inode, true); 2726205b9822SJaegeuk Kim } 2727205b9822SJaegeuk Kim 272891942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri) 2729444c580fSJaegeuk Kim { 2730205b9822SJaegeuk Kim struct f2fs_inode_info *fi = F2FS_I(inode); 2731205b9822SJaegeuk Kim 2732444c580fSJaegeuk Kim if (ri->i_inline & F2FS_INLINE_XATTR) 27337653b9d8SChao Yu set_bit(FI_INLINE_XATTR, fi->flags); 27341001b347SHuajun Li if (ri->i_inline & F2FS_INLINE_DATA) 27357653b9d8SChao Yu set_bit(FI_INLINE_DATA, fi->flags); 273634d67debSChao Yu if (ri->i_inline & F2FS_INLINE_DENTRY) 27377653b9d8SChao Yu set_bit(FI_INLINE_DENTRY, fi->flags); 2738b3d208f9SJaegeuk Kim if (ri->i_inline & F2FS_DATA_EXIST) 27397653b9d8SChao Yu set_bit(FI_DATA_EXIST, fi->flags); 2740510022a8SJaegeuk Kim if (ri->i_inline & F2FS_INLINE_DOTS) 27417653b9d8SChao Yu set_bit(FI_INLINE_DOTS, fi->flags); 27427a2af766SChao Yu if (ri->i_inline & F2FS_EXTRA_ATTR) 27437653b9d8SChao Yu set_bit(FI_EXTRA_ATTR, fi->flags); 27441ad71a27SJaegeuk Kim if (ri->i_inline & F2FS_PIN_FILE) 27457653b9d8SChao Yu set_bit(FI_PIN_FILE, fi->flags); 2746444c580fSJaegeuk Kim } 2747444c580fSJaegeuk Kim 274891942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri) 2749444c580fSJaegeuk Kim { 2750444c580fSJaegeuk Kim ri->i_inline = 0; 2751444c580fSJaegeuk Kim 275291942321SJaegeuk Kim if (is_inode_flag_set(inode, FI_INLINE_XATTR)) 2753444c580fSJaegeuk Kim ri->i_inline |= F2FS_INLINE_XATTR; 275491942321SJaegeuk Kim if (is_inode_flag_set(inode, FI_INLINE_DATA)) 27551001b347SHuajun Li ri->i_inline |= F2FS_INLINE_DATA; 275691942321SJaegeuk Kim if (is_inode_flag_set(inode, FI_INLINE_DENTRY)) 275734d67debSChao Yu ri->i_inline |= F2FS_INLINE_DENTRY; 275891942321SJaegeuk Kim if (is_inode_flag_set(inode, FI_DATA_EXIST)) 2759b3d208f9SJaegeuk Kim ri->i_inline |= F2FS_DATA_EXIST; 276091942321SJaegeuk Kim if (is_inode_flag_set(inode, FI_INLINE_DOTS)) 2761510022a8SJaegeuk Kim ri->i_inline |= F2FS_INLINE_DOTS; 27627a2af766SChao Yu if (is_inode_flag_set(inode, FI_EXTRA_ATTR)) 27637a2af766SChao Yu ri->i_inline |= F2FS_EXTRA_ATTR; 27641ad71a27SJaegeuk Kim if (is_inode_flag_set(inode, FI_PIN_FILE)) 27651ad71a27SJaegeuk Kim ri->i_inline |= F2FS_PIN_FILE; 27667a2af766SChao Yu } 27677a2af766SChao Yu 27687a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode) 27697a2af766SChao Yu { 27707a2af766SChao Yu return is_inode_flag_set(inode, FI_EXTRA_ATTR); 2771444c580fSJaegeuk Kim } 2772444c580fSJaegeuk Kim 2773987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode) 2774987c7c31SChao Yu { 277591942321SJaegeuk Kim return is_inode_flag_set(inode, FI_INLINE_XATTR); 2776987c7c31SChao Yu } 2777987c7c31SChao Yu 27784c8ff709SChao Yu static inline int f2fs_compressed_file(struct inode *inode) 27794c8ff709SChao Yu { 27804c8ff709SChao Yu return S_ISREG(inode->i_mode) && 27814c8ff709SChao Yu is_inode_flag_set(inode, FI_COMPRESSED_FILE); 27824c8ff709SChao Yu } 27834c8ff709SChao Yu 2784602a16d5SDaeho Jeong static inline bool f2fs_need_compress_data(struct inode *inode) 2785602a16d5SDaeho Jeong { 2786602a16d5SDaeho Jeong int compress_mode = F2FS_OPTION(F2FS_I_SB(inode)).compress_mode; 2787602a16d5SDaeho Jeong 2788602a16d5SDaeho Jeong if (!f2fs_compressed_file(inode)) 2789602a16d5SDaeho Jeong return false; 2790602a16d5SDaeho Jeong 2791602a16d5SDaeho Jeong if (compress_mode == COMPR_MODE_FS) 2792602a16d5SDaeho Jeong return true; 2793602a16d5SDaeho Jeong else if (compress_mode == COMPR_MODE_USER && 2794602a16d5SDaeho Jeong is_inode_flag_set(inode, FI_ENABLE_COMPRESS)) 2795602a16d5SDaeho Jeong return true; 2796602a16d5SDaeho Jeong 2797602a16d5SDaeho Jeong return false; 2798602a16d5SDaeho Jeong } 2799602a16d5SDaeho Jeong 280081ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode) 2801de93653fSJaegeuk Kim { 2802d02a6e61SChao Yu unsigned int addrs = CUR_ADDRS_PER_INODE(inode) - 2803d02a6e61SChao Yu get_inline_xattr_addrs(inode); 28044c8ff709SChao Yu 28054c8ff709SChao Yu if (!f2fs_compressed_file(inode)) 28064c8ff709SChao Yu return addrs; 28074c8ff709SChao Yu return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size); 2808d02a6e61SChao Yu } 2809d02a6e61SChao Yu 2810d02a6e61SChao Yu static inline unsigned int addrs_per_block(struct inode *inode) 2811d02a6e61SChao Yu { 28124c8ff709SChao Yu if (!f2fs_compressed_file(inode)) 28134c8ff709SChao Yu return DEF_ADDRS_PER_BLOCK; 28144c8ff709SChao Yu return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, F2FS_I(inode)->i_cluster_size); 2815de93653fSJaegeuk Kim } 2816de93653fSJaegeuk Kim 28176afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page) 281865985d93SJaegeuk Kim { 2819695fd1edSChao Yu struct f2fs_inode *ri = F2FS_INODE(page); 2820cac5a3d8SDongOh Shin 282165985d93SJaegeuk Kim return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE - 2822b323fd28SChao Yu get_inline_xattr_addrs(inode)]); 282365985d93SJaegeuk Kim } 282465985d93SJaegeuk Kim 282565985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode) 282665985d93SJaegeuk Kim { 2827622927f3SChao Yu if (f2fs_has_inline_xattr(inode)) 28286afc662eSChao Yu return get_inline_xattr_addrs(inode) * sizeof(__le32); 2829622927f3SChao Yu return 0; 283065985d93SJaegeuk Kim } 283165985d93SJaegeuk Kim 28320dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode) 28330dbdc2aeSJaegeuk Kim { 283491942321SJaegeuk Kim return is_inode_flag_set(inode, FI_INLINE_DATA); 28350dbdc2aeSJaegeuk Kim } 28360dbdc2aeSJaegeuk Kim 2837b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode) 2838b3d208f9SJaegeuk Kim { 283991942321SJaegeuk Kim return is_inode_flag_set(inode, FI_DATA_EXIST); 2840b3d208f9SJaegeuk Kim } 2841b3d208f9SJaegeuk Kim 2842510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode) 2843510022a8SJaegeuk Kim { 284491942321SJaegeuk Kim return is_inode_flag_set(inode, FI_INLINE_DOTS); 2845510022a8SJaegeuk Kim } 2846510022a8SJaegeuk Kim 28474c8ff709SChao Yu static inline int f2fs_is_mmap_file(struct inode *inode) 28484c8ff709SChao Yu { 28494c8ff709SChao Yu return is_inode_flag_set(inode, FI_MMAP_FILE); 28504c8ff709SChao Yu } 28514c8ff709SChao Yu 28521ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode) 28531ad71a27SJaegeuk Kim { 28541ad71a27SJaegeuk Kim return is_inode_flag_set(inode, FI_PIN_FILE); 28551ad71a27SJaegeuk Kim } 28561ad71a27SJaegeuk Kim 285788b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode) 285888b88a66SJaegeuk Kim { 285991942321SJaegeuk Kim return is_inode_flag_set(inode, FI_ATOMIC_FILE); 286088b88a66SJaegeuk Kim } 286188b88a66SJaegeuk Kim 28625fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode) 28635fe45743SChao Yu { 28645fe45743SChao Yu return is_inode_flag_set(inode, FI_ATOMIC_COMMIT); 28655fe45743SChao Yu } 28665fe45743SChao Yu 286702a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode) 286802a1335fSJaegeuk Kim { 286991942321SJaegeuk Kim return is_inode_flag_set(inode, FI_VOLATILE_FILE); 287002a1335fSJaegeuk Kim } 287102a1335fSJaegeuk Kim 28723c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode) 28733c6c2bebSJaegeuk Kim { 287491942321SJaegeuk Kim return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN); 28753c6c2bebSJaegeuk Kim } 28763c6c2bebSJaegeuk Kim 28771e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode) 28781e84371fSJaegeuk Kim { 287991942321SJaegeuk Kim return is_inode_flag_set(inode, FI_DROP_CACHE); 28801e84371fSJaegeuk Kim } 28811e84371fSJaegeuk Kim 2882f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page) 28831001b347SHuajun Li { 2884695fd1edSChao Yu struct f2fs_inode *ri = F2FS_INODE(page); 28857a2af766SChao Yu int extra_size = get_extra_isize(inode); 2886cac5a3d8SDongOh Shin 28877a2af766SChao Yu return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]); 28881001b347SHuajun Li } 28891001b347SHuajun Li 289034d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode) 289134d67debSChao Yu { 289291942321SJaegeuk Kim return is_inode_flag_set(inode, FI_INLINE_DENTRY); 289334d67debSChao Yu } 289434d67debSChao Yu 2895b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type) 2896b5492af7SJaegeuk Kim { 2897b5492af7SJaegeuk Kim return F2FS_I(inode)->i_advise & type; 2898b5492af7SJaegeuk Kim } 2899b5492af7SJaegeuk Kim 2900b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type) 2901b5492af7SJaegeuk Kim { 2902b5492af7SJaegeuk Kim F2FS_I(inode)->i_advise |= type; 29037c45729aSJaegeuk Kim f2fs_mark_inode_dirty_sync(inode, true); 2904b5492af7SJaegeuk Kim } 2905b5492af7SJaegeuk Kim 2906b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type) 2907b5492af7SJaegeuk Kim { 2908b5492af7SJaegeuk Kim F2FS_I(inode)->i_advise &= ~type; 29097c45729aSJaegeuk Kim f2fs_mark_inode_dirty_sync(inode, true); 2910b5492af7SJaegeuk Kim } 2911b5492af7SJaegeuk Kim 2912fe1897eaSChao Yu static inline bool f2fs_is_time_consistent(struct inode *inode) 2913fe1897eaSChao Yu { 2914fe1897eaSChao Yu if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime)) 2915fe1897eaSChao Yu return false; 2916fe1897eaSChao Yu if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime)) 2917fe1897eaSChao Yu return false; 2918fe1897eaSChao Yu if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime)) 2919fe1897eaSChao Yu return false; 2920fe1897eaSChao Yu if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3, 2921fe1897eaSChao Yu &F2FS_I(inode)->i_crtime)) 2922fe1897eaSChao Yu return false; 2923fe1897eaSChao Yu return true; 2924fe1897eaSChao Yu } 2925fe1897eaSChao Yu 292626787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync) 292726787236SJaegeuk Kim { 2928a0d00fadSChao Yu bool ret; 2929a0d00fadSChao Yu 293026787236SJaegeuk Kim if (dsync) { 293126787236SJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 293226787236SJaegeuk Kim 293326787236SJaegeuk Kim spin_lock(&sbi->inode_lock[DIRTY_META]); 293426787236SJaegeuk Kim ret = list_empty(&F2FS_I(inode)->gdirty_list); 293526787236SJaegeuk Kim spin_unlock(&sbi->inode_lock[DIRTY_META]); 293626787236SJaegeuk Kim return ret; 293726787236SJaegeuk Kim } 293826787236SJaegeuk Kim if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) || 293926787236SJaegeuk Kim file_keep_isize(inode) || 2940235831d7SJunling Zheng i_size_read(inode) & ~PAGE_MASK) 294126787236SJaegeuk Kim return false; 2942a0d00fadSChao Yu 2943fe1897eaSChao Yu if (!f2fs_is_time_consistent(inode)) 2944214c2461SJaegeuk Kim return false; 2945214c2461SJaegeuk Kim 2946c10c9820SChao Yu spin_lock(&F2FS_I(inode)->i_size_lock); 2947a0d00fadSChao Yu ret = F2FS_I(inode)->last_disk_size == i_size_read(inode); 2948c10c9820SChao Yu spin_unlock(&F2FS_I(inode)->i_size_lock); 2949a0d00fadSChao Yu 2950a0d00fadSChao Yu return ret; 2951267378d4SChao Yu } 2952267378d4SChao Yu 2953deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb) 295477888c1eSJaegeuk Kim { 2955deeedd71SChao Yu return sb_rdonly(sb); 295677888c1eSJaegeuk Kim } 295777888c1eSJaegeuk Kim 2958744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi) 2959744602cfSJaegeuk Kim { 2960aaec2b1dSChao Yu return is_set_ckpt_flags(sbi, CP_ERROR_FLAG); 2961744602cfSJaegeuk Kim } 2962744602cfSJaegeuk Kim 296343c780baSEric Biggers static inline bool is_dot_dotdot(const u8 *name, size_t len) 2964eaa693f4SJaegeuk Kim { 296543c780baSEric Biggers if (len == 1 && name[0] == '.') 2966eaa693f4SJaegeuk Kim return true; 2967eaa693f4SJaegeuk Kim 296843c780baSEric Biggers if (len == 2 && name[0] == '.' && name[1] == '.') 2969eaa693f4SJaegeuk Kim return true; 2970eaa693f4SJaegeuk Kim 2971eaa693f4SJaegeuk Kim return false; 2972eaa693f4SJaegeuk Kim } 2973eaa693f4SJaegeuk Kim 29743e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode) 29753e72f721SJaegeuk Kim { 2976e4589fa5SSahitya Tummala struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2977e4589fa5SSahitya Tummala 2978e4589fa5SSahitya Tummala if (!test_opt(sbi, EXTENT_CACHE) || 29794c8ff709SChao Yu is_inode_flag_set(inode, FI_NO_EXTENT) || 29804c8ff709SChao Yu is_inode_flag_set(inode, FI_COMPRESSED_FILE)) 29813e72f721SJaegeuk Kim return false; 29823e72f721SJaegeuk Kim 2983e4589fa5SSahitya Tummala /* 2984e4589fa5SSahitya Tummala * for recovered files during mount do not create extents 2985e4589fa5SSahitya Tummala * if shrinker is not registered. 2986e4589fa5SSahitya Tummala */ 2987e4589fa5SSahitya Tummala if (list_empty(&sbi->s_list)) 2988e4589fa5SSahitya Tummala return false; 2989e4589fa5SSahitya Tummala 2990886f56f9SAl Viro return S_ISREG(inode->i_mode); 29913e72f721SJaegeuk Kim } 29923e72f721SJaegeuk Kim 29931ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi, 29941ecc0c5cSChao Yu size_t size, gfp_t flags) 29950414b004SJaegeuk Kim { 299655523519SChao Yu if (time_to_inject(sbi, FAULT_KMALLOC)) { 2997c45d6002SChao Yu f2fs_show_injection_info(sbi, FAULT_KMALLOC); 29982c63feadSJaegeuk Kim return NULL; 299955523519SChao Yu } 30007fa750a1SArnd Bergmann 30010b6d4ca0SEric Biggers return kmalloc(size, flags); 30020414b004SJaegeuk Kim } 30030414b004SJaegeuk Kim 3004acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi, 3005acbf054dSChao Yu size_t size, gfp_t flags) 3006acbf054dSChao Yu { 3007acbf054dSChao Yu return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO); 3008acbf054dSChao Yu } 3009acbf054dSChao Yu 3010628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi, 3011628b3d14SChao Yu size_t size, gfp_t flags) 3012628b3d14SChao Yu { 3013628b3d14SChao Yu if (time_to_inject(sbi, FAULT_KVMALLOC)) { 3014c45d6002SChao Yu f2fs_show_injection_info(sbi, FAULT_KVMALLOC); 3015628b3d14SChao Yu return NULL; 3016628b3d14SChao Yu } 30177fa750a1SArnd Bergmann 3018628b3d14SChao Yu return kvmalloc(size, flags); 3019628b3d14SChao Yu } 3020628b3d14SChao Yu 3021628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi, 3022628b3d14SChao Yu size_t size, gfp_t flags) 3023628b3d14SChao Yu { 3024628b3d14SChao Yu return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO); 3025628b3d14SChao Yu } 3026628b3d14SChao Yu 30277a2af766SChao Yu static inline int get_extra_isize(struct inode *inode) 3028f2470371SChao Yu { 30297a2af766SChao Yu return F2FS_I(inode)->i_extra_isize / sizeof(__le32); 3030f2470371SChao Yu } 3031f2470371SChao Yu 30326afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode) 30336afc662eSChao Yu { 30346afc662eSChao Yu return F2FS_I(inode)->i_inline_xattr_size; 30356afc662eSChao Yu } 30366afc662eSChao Yu 30374d57b86dSChao Yu #define f2fs_get_inode_mode(i) \ 303891942321SJaegeuk Kim ((is_inode_flag_set(i, FI_ACL_MODE)) ? \ 3039a6dda0e6SChristoph Hellwig (F2FS_I(i)->i_acl_mode) : ((i)->i_mode)) 3040a6dda0e6SChristoph Hellwig 30417a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE \ 30427a2af766SChao Yu (offsetof(struct f2fs_inode, i_extra_end) - \ 30437a2af766SChao Yu offsetof(struct f2fs_inode, i_extra_isize)) \ 30447a2af766SChao Yu 30455c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE (offsetof(struct f2fs_inode, i_addr)) 30465c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field) \ 30472f84babfSSheng Yong ((offsetof(typeof(*(f2fs_inode)), field) + \ 30485c57132eSChao Yu sizeof((f2fs_inode)->field)) \ 30492f84babfSSheng Yong <= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize))) \ 30505c57132eSChao Yu 30512bc4bea3SDaeho Jeong #define DEFAULT_IOSTAT_PERIOD_MS 3000 30522bc4bea3SDaeho Jeong #define MIN_IOSTAT_PERIOD_MS 100 30532bc4bea3SDaeho Jeong /* maximum period of iostat tracing is 1 day */ 30542bc4bea3SDaeho Jeong #define MAX_IOSTAT_PERIOD_MS 8640000 30552bc4bea3SDaeho Jeong 3056b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi) 3057b0af6d49SChao Yu { 3058b0af6d49SChao Yu int i; 3059b0af6d49SChao Yu 3060b0af6d49SChao Yu spin_lock(&sbi->iostat_lock); 30612bc4bea3SDaeho Jeong for (i = 0; i < NR_IO_TYPE; i++) { 30628b83ac81SChao Yu sbi->rw_iostat[i] = 0; 30638b83ac81SChao Yu sbi->prev_rw_iostat[i] = 0; 30642bc4bea3SDaeho Jeong } 3065b0af6d49SChao Yu spin_unlock(&sbi->iostat_lock); 3066b0af6d49SChao Yu } 3067b0af6d49SChao Yu 30682bc4bea3SDaeho Jeong extern void f2fs_record_iostat(struct f2fs_sb_info *sbi); 30692bc4bea3SDaeho Jeong 3070b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi, 3071b0af6d49SChao Yu enum iostat_type type, unsigned long long io_bytes) 3072b0af6d49SChao Yu { 3073b0af6d49SChao Yu if (!sbi->iostat_enable) 3074b0af6d49SChao Yu return; 3075b0af6d49SChao Yu spin_lock(&sbi->iostat_lock); 30768b83ac81SChao Yu sbi->rw_iostat[type] += io_bytes; 3077b0af6d49SChao Yu 3078b0af6d49SChao Yu if (type == APP_WRITE_IO || type == APP_DIRECT_IO) 30798b83ac81SChao Yu sbi->rw_iostat[APP_BUFFERED_IO] = 30808b83ac81SChao Yu sbi->rw_iostat[APP_WRITE_IO] - 30818b83ac81SChao Yu sbi->rw_iostat[APP_DIRECT_IO]; 30828b83ac81SChao Yu 30838b83ac81SChao Yu if (type == APP_READ_IO || type == APP_DIRECT_READ_IO) 30848b83ac81SChao Yu sbi->rw_iostat[APP_BUFFERED_READ_IO] = 30858b83ac81SChao Yu sbi->rw_iostat[APP_READ_IO] - 30868b83ac81SChao Yu sbi->rw_iostat[APP_DIRECT_READ_IO]; 3087b0af6d49SChao Yu spin_unlock(&sbi->iostat_lock); 30882bc4bea3SDaeho Jeong 30892bc4bea3SDaeho Jeong f2fs_record_iostat(sbi); 3090b0af6d49SChao Yu } 3091b0af6d49SChao Yu 30922c70c5e3SChao Yu #define __is_large_section(sbi) ((sbi)->segs_per_sec > 1) 30932c70c5e3SChao Yu 30946dc3a126SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META) 3095c9b60788SChao Yu 3096e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi, 3097e1da7872SChao Yu block_t blkaddr, int type); 3098e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi, 3099e1da7872SChao Yu block_t blkaddr, int type) 3100e1da7872SChao Yu { 3101e1da7872SChao Yu if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) { 3102dcbb4c10SJoe Perches f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.", 3103e1da7872SChao Yu blkaddr, type); 3104e1da7872SChao Yu f2fs_bug_on(sbi, 1); 3105e1da7872SChao Yu } 3106e1da7872SChao Yu } 3107e1da7872SChao Yu 3108e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr) 31097b525dd0SChao Yu { 31104c8ff709SChao Yu if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR || 31114c8ff709SChao Yu blkaddr == COMPRESS_ADDR) 31127b525dd0SChao Yu return false; 31137b525dd0SChao Yu return true; 31147b525dd0SChao Yu } 31157b525dd0SChao Yu 3116240a5915SChao Yu static inline void f2fs_set_page_private(struct page *page, 3117240a5915SChao Yu unsigned long data) 3118240a5915SChao Yu { 3119240a5915SChao Yu if (PagePrivate(page)) 3120240a5915SChao Yu return; 3121240a5915SChao Yu 31227128cf9aSGuoqing Jiang attach_page_private(page, (void *)data); 3123240a5915SChao Yu } 3124240a5915SChao Yu 3125240a5915SChao Yu static inline void f2fs_clear_page_private(struct page *page) 3126240a5915SChao Yu { 31277128cf9aSGuoqing Jiang detach_page_private(page); 3128240a5915SChao Yu } 3129240a5915SChao Yu 313039a53e0cSJaegeuk Kim /* 313139a53e0cSJaegeuk Kim * file.c 313239a53e0cSJaegeuk Kim */ 3133cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync); 31344d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn); 31353265d3dbSChao Yu int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock); 3136c42d28ceSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock); 3137cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode); 3138*549c7297SChristian Brauner int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path, 3139*549c7297SChristian Brauner struct kstat *stat, u32 request_mask, unsigned int flags); 3140*549c7297SChristian Brauner int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, 3141*549c7297SChristian Brauner struct iattr *attr); 31424d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end); 31434d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count); 3144c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode); 3145cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg); 3146cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 314778130819SChao Yu int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid); 31481ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc); 314939a53e0cSJaegeuk Kim 315039a53e0cSJaegeuk Kim /* 315139a53e0cSJaegeuk Kim * inode.c 315239a53e0cSJaegeuk Kim */ 3153cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode); 3154704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page); 3155704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page); 3156cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino); 3157cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino); 31584d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink); 31594d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page); 31604d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode); 3161cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc); 3162cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode); 31634d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode); 316439a53e0cSJaegeuk Kim 316539a53e0cSJaegeuk Kim /* 316639a53e0cSJaegeuk Kim * namei.c 316739a53e0cSJaegeuk Kim */ 31684d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name, 3169b6a06cbbSChao Yu bool hot, bool set); 317039a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child); 317139a53e0cSJaegeuk Kim 317239a53e0cSJaegeuk Kim /* 317339a53e0cSJaegeuk Kim * dir.c 317439a53e0cSJaegeuk Kim */ 31754d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de); 317643c780baSEric Biggers int f2fs_init_casefolded_name(const struct inode *dir, 317743c780baSEric Biggers struct f2fs_filename *fname); 317843c780baSEric Biggers int f2fs_setup_filename(struct inode *dir, const struct qstr *iname, 317943c780baSEric Biggers int lookup, struct f2fs_filename *fname); 318043c780baSEric Biggers int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry, 318143c780baSEric Biggers struct f2fs_filename *fname); 318243c780baSEric Biggers void f2fs_free_filename(struct f2fs_filename *fname); 318343c780baSEric Biggers struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d, 318443c780baSEric Biggers const struct f2fs_filename *fname, int *max_slots); 3185cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d, 3186cac5a3d8SDongOh Shin unsigned int start_pos, struct fscrypt_str *fstr); 31874d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent, 3188cac5a3d8SDongOh Shin struct f2fs_dentry_ptr *d); 31894d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir, 319043c780baSEric Biggers const struct f2fs_filename *fname, struct page *dpage); 31914d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode, 3192cac5a3d8SDongOh Shin unsigned int current_depth); 31934d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots); 3194cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode); 3195cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir, 319643c780baSEric Biggers const struct f2fs_filename *fname, 319743c780baSEric Biggers struct page **res_page); 3198cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir, 3199cac5a3d8SDongOh Shin const struct qstr *child, struct page **res_page); 3200cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p); 3201cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr, 3202cac5a3d8SDongOh Shin struct page **page); 3203cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de, 3204cac5a3d8SDongOh Shin struct page *page, struct inode *inode); 3205b06af2afSJaegeuk Kim bool f2fs_has_enough_room(struct inode *dir, struct page *ipage, 320643c780baSEric Biggers const struct f2fs_filename *fname); 3207cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d, 320843c780baSEric Biggers const struct fscrypt_str *name, f2fs_hash_t name_hash, 3209cac5a3d8SDongOh Shin unsigned int bit_pos); 321043c780baSEric Biggers int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname, 3211cac5a3d8SDongOh Shin struct inode *inode, nid_t ino, umode_t mode); 321243c780baSEric Biggers int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname, 3213cac5a3d8SDongOh Shin struct inode *inode, nid_t ino, umode_t mode); 32144d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name, 3215cac5a3d8SDongOh Shin struct inode *inode, nid_t ino, umode_t mode); 3216cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page, 3217cac5a3d8SDongOh Shin struct inode *dir, struct inode *inode); 3218cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir); 3219cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir); 322039a53e0cSJaegeuk Kim 3221b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode) 3222b7f7a5e0SAl Viro { 3223bfc2b7e8SEric Biggers if (fscrypt_is_nokey_name(dentry)) 3224bfc2b7e8SEric Biggers return -ENOKEY; 32254d57b86dSChao Yu return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name, 3226510022a8SJaegeuk Kim inode, inode->i_ino, inode->i_mode); 3227b7f7a5e0SAl Viro } 3228b7f7a5e0SAl Viro 322939a53e0cSJaegeuk Kim /* 323039a53e0cSJaegeuk Kim * super.c 323139a53e0cSJaegeuk Kim */ 3232cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync); 3233cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode); 3234ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly); 3235af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type); 32364b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb); 3237cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover); 3238cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync); 32394d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi); 324039a53e0cSJaegeuk Kim 324139a53e0cSJaegeuk Kim /* 324239a53e0cSJaegeuk Kim * hash.c 324339a53e0cSJaegeuk Kim */ 324443c780baSEric Biggers void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname); 324539a53e0cSJaegeuk Kim 324639a53e0cSJaegeuk Kim /* 324739a53e0cSJaegeuk Kim * node.c 324839a53e0cSJaegeuk Kim */ 324939a53e0cSJaegeuk Kim struct dnode_of_data; 325039a53e0cSJaegeuk Kim struct node_info; 325139a53e0cSJaegeuk Kim 32524d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid); 32534d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type); 325450fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page); 325550fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi); 325650fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page); 325750fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi); 32584d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid); 32594d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid); 32604d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino); 32617735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid, 32624d57b86dSChao Yu struct node_info *ni); 32634d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs); 32644d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode); 32654d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from); 32664d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode); 326750fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, 326850fa53ecSChao Yu unsigned int seq_id); 32694d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode); 32704d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode); 32714d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs); 32724d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid); 32734d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid); 32744d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start); 327548018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type); 327668e79bafSJia Yang void f2fs_flush_inline_data(struct f2fs_sb_info *sbi); 32774d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode, 327850fa53ecSChao Yu struct writeback_control *wbc, bool atomic, 327950fa53ecSChao Yu unsigned int *seq_id); 32804d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi, 32814d57b86dSChao Yu struct writeback_control *wbc, 3282b0af6d49SChao Yu bool do_balance, enum iostat_type io_type); 3283e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount); 32844d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid); 32854d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid); 32864d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid); 32874d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink); 32889627a7b3SChao Yu int f2fs_recover_inline_xattr(struct inode *inode, struct page *page); 32894d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page); 32904d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page); 32917735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi, 3292cac5a3d8SDongOh Shin unsigned int segno, struct f2fs_summary_block *sum); 3293edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc); 32944d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi); 32954d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi); 32964d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void); 32974d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void); 329839a53e0cSJaegeuk Kim 329939a53e0cSJaegeuk Kim /* 330039a53e0cSJaegeuk Kim * segment.c 330139a53e0cSJaegeuk Kim */ 33024d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi); 33034d57b86dSChao Yu void f2fs_register_inmem_page(struct inode *inode, struct page *page); 33044d57b86dSChao Yu void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure); 33054d57b86dSChao Yu void f2fs_drop_inmem_pages(struct inode *inode); 33064d57b86dSChao Yu void f2fs_drop_inmem_page(struct inode *inode, struct page *page); 33074d57b86dSChao Yu int f2fs_commit_inmem_pages(struct inode *inode); 3308cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need); 33097bcd0cfaSChao Yu void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg); 331039d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino); 33114d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi); 33121228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi); 33134d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free); 33144d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr); 33154d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr); 33164d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi); 33174d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi); 331803f2c02dSJaegeuk Kim bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi); 33194d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi, 33204d57b86dSChao Yu struct cp_control *cpc); 33214354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi); 33224d3aed70SDaniel Rosenberg block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi); 33234d3aed70SDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable); 33244d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi); 33254d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra); 3326093749e2SChao Yu void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi); 3327093749e2SChao Yu void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi); 3328093749e2SChao Yu void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi); 3329093749e2SChao Yu void f2fs_get_new_segment(struct f2fs_sb_info *sbi, 3330093749e2SChao Yu unsigned int *newseg, bool new_sec, int dir); 33310ef81833SChao Yu void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type, 333204f0b2eaSQiuyang Sun unsigned int start, unsigned int end); 3333901d745fSChao Yu void f2fs_allocate_new_segment(struct f2fs_sb_info *sbi, int type); 3334901d745fSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi); 3335cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range); 33364d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi, 33374d57b86dSChao Yu struct cp_control *cpc); 33384d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno); 33394d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src, 33404d57b86dSChao Yu block_t blk_addr); 33414d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page, 3342b0af6d49SChao Yu enum iostat_type io_type); 33434d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio); 33444d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn, 33454d57b86dSChao Yu struct f2fs_io_info *fio); 33464d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio); 33474d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, 3348cac5a3d8SDongOh Shin block_t old_blkaddr, block_t new_blkaddr, 3349c5d02785SChao Yu bool recover_curseg, bool recover_newaddr, 3350c5d02785SChao Yu bool from_gc); 3351cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn, 3352cac5a3d8SDongOh Shin block_t old_addr, block_t new_addr, 3353cac5a3d8SDongOh Shin unsigned char version, bool recover_curseg, 3354cac5a3d8SDongOh Shin bool recover_newaddr); 33554d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page, 3356cac5a3d8SDongOh Shin block_t old_blkaddr, block_t *new_blkaddr, 3357fb830fc5SChao Yu struct f2fs_summary *sum, int type, 3358f608c38cSChao Yu struct f2fs_io_info *fio); 3359cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page, 3360bae0ee7aSChao Yu enum page_type type, bool ordered, bool locked); 33610ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr); 33621e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr, 33631e78e8bdSSahitya Tummala block_t len); 33644d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk); 33654d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk); 33664d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type, 3367cac5a3d8SDongOh Shin unsigned int val, int alloc); 33684d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc); 3369c426d991SShin'ichiro Kawasaki int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi); 3370d508c94eSShin'ichiro Kawasaki int f2fs_check_write_pointer(struct f2fs_sb_info *sbi); 33714d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi); 33724d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi); 33734d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void); 33744d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void); 33754d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint); 33764d57b86dSChao Yu enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi, 33774d57b86dSChao Yu enum page_type type, enum temp_type temp); 3378de881df9SAravind Ramesh unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi, 3379de881df9SAravind Ramesh unsigned int segno); 3380de881df9SAravind Ramesh unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi, 3381de881df9SAravind Ramesh unsigned int segno); 338239a53e0cSJaegeuk Kim 338339a53e0cSJaegeuk Kim /* 338439a53e0cSJaegeuk Kim * checkpoint.c 338539a53e0cSJaegeuk Kim */ 3386cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io); 33874d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index); 33884d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index); 338986f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index); 33904d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index); 3391e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi, 33924d57b86dSChao Yu block_t blkaddr, int type); 33934d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages, 3394cac5a3d8SDongOh Shin int type, bool sync); 33954d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index); 33964d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type, 3397b0af6d49SChao Yu long nr_to_write, enum iostat_type io_type); 33984d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type); 33994d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type); 34004d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all); 34014d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode); 34024d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 340339d787beSChao Yu unsigned int devidx, int type); 34044d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino, 340539d787beSChao Yu unsigned int devidx, int type); 3406cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi); 34074d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi); 34084d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi); 34094d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode); 34104d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino); 34114d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi); 34124d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi); 34134d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page); 34144d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode); 34154d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type); 3416bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type); 34173a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi); 34184d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc); 34194d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi); 34204d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void); 34214d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void); 342239a53e0cSJaegeuk Kim 342339a53e0cSJaegeuk Kim /* 342439a53e0cSJaegeuk Kim * data.c 342539a53e0cSJaegeuk Kim */ 3426f543805fSChao Yu int __init f2fs_init_bioset(void); 3427f543805fSChao Yu void f2fs_destroy_bioset(void); 3428ca9e968aSChao Yu struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi, int npages, bool noio); 34290b20fcecSChao Yu int f2fs_init_bio_entry_cache(void); 34300b20fcecSChao Yu void f2fs_destroy_bio_entry_cache(void); 34314c8ff709SChao Yu void f2fs_submit_bio(struct f2fs_sb_info *sbi, 34324c8ff709SChao Yu struct bio *bio, enum page_type type); 3433b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type); 3434b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi, 3435bab475c5SChao Yu struct inode *inode, struct page *page, 3436bab475c5SChao Yu nid_t ino, enum page_type type); 34370b20fcecSChao Yu void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi, 34380b20fcecSChao Yu struct bio **bio, struct page *page); 3439b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi); 3440cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio); 34418648de2cSChao Yu int f2fs_merge_page_bio(struct f2fs_io_info *fio); 3442fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio); 3443cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi, 3444cac5a3d8SDongOh Shin block_t blk_addr, struct bio *bio); 3445cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr); 34464d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn); 3447cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr); 34484d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count); 34494d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn); 3450cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index); 3451cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from); 3452cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index); 34534d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index, 3454cac5a3d8SDongOh Shin int op_flags, bool for_write); 34554d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index); 34564d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index, 3457cac5a3d8SDongOh Shin bool for_write); 34584d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode, 3459cac5a3d8SDongOh Shin struct page *ipage, pgoff_t index, bool new_i_size); 34604d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio); 34610ef81833SChao Yu void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock); 3462cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, 3463cac5a3d8SDongOh Shin int create, int flag); 3464cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 3465cac5a3d8SDongOh Shin u64 start, u64 len); 34664c8ff709SChao Yu int f2fs_encrypt_one_page(struct f2fs_io_info *fio); 34674d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio); 34684d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio); 34694c8ff709SChao Yu int f2fs_write_single_data_page(struct page *page, int *submitted, 34704c8ff709SChao Yu struct bio **bio, sector_t *last_block, 34714c8ff709SChao Yu struct writeback_control *wbc, 34724c8ff709SChao Yu enum iostat_type io_type, 34734c8ff709SChao Yu int compr_blocks); 3474cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset, 3475cac5a3d8SDongOh Shin unsigned int length); 3476cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait); 34775b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION 3478cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage, 3479cac5a3d8SDongOh Shin struct page *page, enum migrate_mode mode); 34805b7a487cSWeichao Guo #endif 3481b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len); 34825ec2d99dSMatthew Wilcox void f2fs_clear_page_cache_dirty_tag(struct page *page); 34834c8ff709SChao Yu int f2fs_init_post_read_processing(void); 34844c8ff709SChao Yu void f2fs_destroy_post_read_processing(void); 34854c8ff709SChao Yu int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi); 34864c8ff709SChao Yu void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi); 348739a53e0cSJaegeuk Kim 348839a53e0cSJaegeuk Kim /* 348939a53e0cSJaegeuk Kim * gc.c 349039a53e0cSJaegeuk Kim */ 34914d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi); 34924d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi); 34934d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode); 3494e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background, 3495e066b83cSJaegeuk Kim unsigned int segno); 34964d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi); 349704f0b2eaSQiuyang Sun int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count); 3498093749e2SChao Yu int __init f2fs_create_garbage_collection_cache(void); 3499093749e2SChao Yu void f2fs_destroy_garbage_collection_cache(void); 350039a53e0cSJaegeuk Kim 350139a53e0cSJaegeuk Kim /* 350239a53e0cSJaegeuk Kim * recovery.c 350339a53e0cSJaegeuk Kim */ 35044d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only); 35054d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi); 350639a53e0cSJaegeuk Kim 350739a53e0cSJaegeuk Kim /* 350839a53e0cSJaegeuk Kim * debug.c 350939a53e0cSJaegeuk Kim */ 351039a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS 351139a53e0cSJaegeuk Kim struct f2fs_stat_info { 351239a53e0cSJaegeuk Kim struct list_head stat_list; 351339a53e0cSJaegeuk Kim struct f2fs_sb_info *sbi; 351439a53e0cSJaegeuk Kim int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs; 351539a53e0cSJaegeuk Kim int main_area_segs, main_area_sections, main_area_zones; 35165b7ee374SChao Yu unsigned long long hit_largest, hit_cached, hit_rbtree; 35175b7ee374SChao Yu unsigned long long hit_total, total_ext; 3518c00ba554SJaegeuk Kim int ext_tree, zombie_tree, ext_node; 35192c8a4a28SJaegeuk Kim int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta; 35202c8a4a28SJaegeuk Kim int ndirty_data, ndirty_qdata; 352135782b23SJaegeuk Kim int inmem_pages; 35222c8a4a28SJaegeuk Kim unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all; 35235b0ef73cSJaegeuk Kim int nats, dirty_nats, sits, dirty_sits; 35245b0ef73cSJaegeuk Kim int free_nids, avail_nids, alloc_nids; 352539a53e0cSJaegeuk Kim int total_count, utilization; 35268b8dd65fSChao Yu int bg_gc, nr_wb_cp_data, nr_wb_data; 35275f9abab4SJaegeuk Kim int nr_rd_data, nr_rd_node, nr_rd_meta; 352802b16d0aSChao Yu int nr_dio_read, nr_dio_write; 3529274bd9baSChao Yu unsigned int io_skip_bggc, other_skip_bggc; 353014d8d5f7SChao Yu int nr_flushing, nr_flushed, flush_list_empty; 353114d8d5f7SChao Yu int nr_discarding, nr_discarded; 35325f32366aSChao Yu int nr_discard_cmd; 3533d84d1cbdSChao Yu unsigned int undiscard_blks; 3534a00861dbSJaegeuk Kim int inline_xattr, inline_inode, inline_dir, append, update, orphans; 3535ae999bb9SDaeho Jeong int compr_inode; 3536ae999bb9SDaeho Jeong unsigned long long compr_blocks; 3537648d50baSChao Yu int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt; 3538f83a2584SYunlei He unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks; 353939a53e0cSJaegeuk Kim unsigned int bimodal, avg_vblocks; 354039a53e0cSJaegeuk Kim int util_free, util_valid, util_invalid; 354139a53e0cSJaegeuk Kim int rsvd_segs, overp_segs; 354239a53e0cSJaegeuk Kim int dirty_count, node_pages, meta_pages; 354342190d2aSJaegeuk Kim int prefree_count, call_count, cp_count, bg_cp_count; 354439a53e0cSJaegeuk Kim int tot_segs, node_segs, data_segs, free_segs, free_secs; 3545e1235983SChangman Lee int bg_node_segs, bg_data_segs; 354639a53e0cSJaegeuk Kim int tot_blks, data_blks, node_blks; 3547e1235983SChangman Lee int bg_data_blks, bg_node_blks; 35482ef79ecbSChao Yu unsigned long long skipped_atomic_files[2]; 354939a53e0cSJaegeuk Kim int curseg[NR_CURSEG_TYPE]; 355039a53e0cSJaegeuk Kim int cursec[NR_CURSEG_TYPE]; 355139a53e0cSJaegeuk Kim int curzone[NR_CURSEG_TYPE]; 35520759e2c1SChao Yu unsigned int dirty_seg[NR_CURSEG_TYPE]; 35530759e2c1SChao Yu unsigned int full_seg[NR_CURSEG_TYPE]; 35540759e2c1SChao Yu unsigned int valid_blks[NR_CURSEG_TYPE]; 355539a53e0cSJaegeuk Kim 3556b63e7be5SChao Yu unsigned int meta_count[META_MAX]; 355739a53e0cSJaegeuk Kim unsigned int segment_count[2]; 355839a53e0cSJaegeuk Kim unsigned int block_count[2]; 3559b9a2c252SChangman Lee unsigned int inplace_count; 35609edcdabfSChao Yu unsigned long long base_mem, cache_mem, page_mem; 356139a53e0cSJaegeuk Kim }; 356239a53e0cSJaegeuk Kim 3563963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi) 3564963d4f7dSGu Zheng { 3565963d4f7dSGu Zheng return (struct f2fs_stat_info *)sbi->stat_info; 3566963d4f7dSGu Zheng } 3567963d4f7dSGu Zheng 3568942e0be6SChangman Lee #define stat_inc_cp_count(si) ((si)->cp_count++) 356942190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si) ((si)->bg_cp_count++) 357039a53e0cSJaegeuk Kim #define stat_inc_call_count(si) ((si)->call_count++) 3571fc7100eaSHridya Valsaraju #define stat_inc_bggc_count(si) ((si)->bg_gc++) 3572274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi) ((sbi)->io_skip_bggc++) 3573274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi) ((sbi)->other_skip_bggc++) 357433fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++) 357533fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--) 35765b7ee374SChao Yu #define stat_inc_total_hit(sbi) (atomic64_inc(&(sbi)->total_hit_ext)) 35775b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_rbtree)) 35785b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest)) 35795b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_cached)) 3580d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode) \ 3581d5e8f6c9SChao Yu do { \ 3582d5e8f6c9SChao Yu if (f2fs_has_inline_xattr(inode)) \ 3583d5e8f6c9SChao Yu (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \ 3584d5e8f6c9SChao Yu } while (0) 3585d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode) \ 3586d5e8f6c9SChao Yu do { \ 3587d5e8f6c9SChao Yu if (f2fs_has_inline_xattr(inode)) \ 3588d5e8f6c9SChao Yu (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \ 3589d5e8f6c9SChao Yu } while (0) 35900dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode) \ 35910dbdc2aeSJaegeuk Kim do { \ 35920dbdc2aeSJaegeuk Kim if (f2fs_has_inline_data(inode)) \ 359303e14d52SChao Yu (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \ 35940dbdc2aeSJaegeuk Kim } while (0) 35950dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode) \ 35960dbdc2aeSJaegeuk Kim do { \ 35970dbdc2aeSJaegeuk Kim if (f2fs_has_inline_data(inode)) \ 359803e14d52SChao Yu (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \ 35990dbdc2aeSJaegeuk Kim } while (0) 36003289c061SJaegeuk Kim #define stat_inc_inline_dir(inode) \ 36013289c061SJaegeuk Kim do { \ 36023289c061SJaegeuk Kim if (f2fs_has_inline_dentry(inode)) \ 360303e14d52SChao Yu (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \ 36043289c061SJaegeuk Kim } while (0) 36053289c061SJaegeuk Kim #define stat_dec_inline_dir(inode) \ 36063289c061SJaegeuk Kim do { \ 36073289c061SJaegeuk Kim if (f2fs_has_inline_dentry(inode)) \ 360803e14d52SChao Yu (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \ 36093289c061SJaegeuk Kim } while (0) 36104c8ff709SChao Yu #define stat_inc_compr_inode(inode) \ 36114c8ff709SChao Yu do { \ 36124c8ff709SChao Yu if (f2fs_compressed_file(inode)) \ 36134c8ff709SChao Yu (atomic_inc(&F2FS_I_SB(inode)->compr_inode)); \ 36144c8ff709SChao Yu } while (0) 36154c8ff709SChao Yu #define stat_dec_compr_inode(inode) \ 36164c8ff709SChao Yu do { \ 36174c8ff709SChao Yu if (f2fs_compressed_file(inode)) \ 36184c8ff709SChao Yu (atomic_dec(&F2FS_I_SB(inode)->compr_inode)); \ 36194c8ff709SChao Yu } while (0) 36204c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks) \ 3621ae999bb9SDaeho Jeong (atomic64_add(blocks, &F2FS_I_SB(inode)->compr_blocks)) 36224c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks) \ 3623ae999bb9SDaeho Jeong (atomic64_sub(blocks, &F2FS_I_SB(inode)->compr_blocks)) 3624b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr) \ 3625b63e7be5SChao Yu do { \ 3626b63e7be5SChao Yu if (blkaddr < SIT_I(sbi)->sit_base_addr) \ 3627b63e7be5SChao Yu atomic_inc(&(sbi)->meta_count[META_CP]); \ 3628b63e7be5SChao Yu else if (blkaddr < NM_I(sbi)->nat_blkaddr) \ 3629b63e7be5SChao Yu atomic_inc(&(sbi)->meta_count[META_SIT]); \ 3630b63e7be5SChao Yu else if (blkaddr < SM_I(sbi)->ssa_blkaddr) \ 3631b63e7be5SChao Yu atomic_inc(&(sbi)->meta_count[META_NAT]); \ 3632b63e7be5SChao Yu else if (blkaddr < SM_I(sbi)->main_blkaddr) \ 3633b63e7be5SChao Yu atomic_inc(&(sbi)->meta_count[META_SSA]); \ 3634b63e7be5SChao Yu } while (0) 3635dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg) \ 3636dcdfff65SJaegeuk Kim ((sbi)->segment_count[(curseg)->alloc_type]++) 3637dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg) \ 3638dcdfff65SJaegeuk Kim ((sbi)->block_count[(curseg)->alloc_type]++) 3639b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi) \ 3640b9a2c252SChangman Lee (atomic_inc(&(sbi)->inplace_count)) 364126a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode) \ 364226a28a0cSJaegeuk Kim do { \ 36430e6d0164SSahitya Tummala int cur = F2FS_I_SB(inode)->atomic_files; \ 364426a28a0cSJaegeuk Kim int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt); \ 364526a28a0cSJaegeuk Kim if (cur > max) \ 364626a28a0cSJaegeuk Kim atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur); \ 364726a28a0cSJaegeuk Kim } while (0) 3648648d50baSChao Yu #define stat_inc_volatile_write(inode) \ 3649648d50baSChao Yu (atomic_inc(&F2FS_I_SB(inode)->vw_cnt)) 3650648d50baSChao Yu #define stat_dec_volatile_write(inode) \ 3651648d50baSChao Yu (atomic_dec(&F2FS_I_SB(inode)->vw_cnt)) 3652648d50baSChao Yu #define stat_update_max_volatile_write(inode) \ 3653648d50baSChao Yu do { \ 3654648d50baSChao Yu int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt); \ 3655648d50baSChao Yu int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt); \ 3656648d50baSChao Yu if (cur > max) \ 3657648d50baSChao Yu atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur); \ 3658648d50baSChao Yu } while (0) 3659e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type) \ 366039a53e0cSJaegeuk Kim do { \ 3661963d4f7dSGu Zheng struct f2fs_stat_info *si = F2FS_STAT(sbi); \ 366268afcf2dSTomohiro Kusumi si->tot_segs++; \ 366368afcf2dSTomohiro Kusumi if ((type) == SUM_TYPE_DATA) { \ 366439a53e0cSJaegeuk Kim si->data_segs++; \ 3665e1235983SChangman Lee si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \ 3666e1235983SChangman Lee } else { \ 366739a53e0cSJaegeuk Kim si->node_segs++; \ 3668e1235983SChangman Lee si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \ 3669e1235983SChangman Lee } \ 367039a53e0cSJaegeuk Kim } while (0) 367139a53e0cSJaegeuk Kim 367239a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks) \ 367368afcf2dSTomohiro Kusumi ((si)->tot_blks += (blks)) 367439a53e0cSJaegeuk Kim 3675e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type) \ 367639a53e0cSJaegeuk Kim do { \ 3677963d4f7dSGu Zheng struct f2fs_stat_info *si = F2FS_STAT(sbi); \ 367839a53e0cSJaegeuk Kim stat_inc_tot_blk_count(si, blks); \ 367939a53e0cSJaegeuk Kim si->data_blks += (blks); \ 368068afcf2dSTomohiro Kusumi si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0; \ 368139a53e0cSJaegeuk Kim } while (0) 368239a53e0cSJaegeuk Kim 3683e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type) \ 368439a53e0cSJaegeuk Kim do { \ 3685963d4f7dSGu Zheng struct f2fs_stat_info *si = F2FS_STAT(sbi); \ 368639a53e0cSJaegeuk Kim stat_inc_tot_blk_count(si, blks); \ 368739a53e0cSJaegeuk Kim si->node_blks += (blks); \ 368868afcf2dSTomohiro Kusumi si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0; \ 368939a53e0cSJaegeuk Kim } while (0) 369039a53e0cSJaegeuk Kim 3691cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi); 3692cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi); 369321acc07dSGreg Kroah-Hartman void __init f2fs_create_root_stats(void); 36944589d25dSNamjae Jeon void f2fs_destroy_root_stats(void); 3695fc7100eaSHridya Valsaraju void f2fs_update_sit_info(struct f2fs_sb_info *sbi); 369639a53e0cSJaegeuk Kim #else 3697d66450e7SArnd Bergmann #define stat_inc_cp_count(si) do { } while (0) 3698d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si) do { } while (0) 3699d66450e7SArnd Bergmann #define stat_inc_call_count(si) do { } while (0) 3700d66450e7SArnd Bergmann #define stat_inc_bggc_count(si) do { } while (0) 3701274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi) do { } while (0) 3702274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi) do { } while (0) 3703d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type) do { } while (0) 3704d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type) do { } while (0) 3705fc7100eaSHridya Valsaraju #define stat_inc_total_hit(sbi) do { } while (0) 3706fc7100eaSHridya Valsaraju #define stat_inc_rbtree_node_hit(sbi) do { } while (0) 3707d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi) do { } while (0) 3708d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi) do { } while (0) 3709d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode) do { } while (0) 3710d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode) do { } while (0) 3711d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode) do { } while (0) 3712d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode) do { } while (0) 3713d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode) do { } while (0) 3714d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode) do { } while (0) 37154c8ff709SChao Yu #define stat_inc_compr_inode(inode) do { } while (0) 37164c8ff709SChao Yu #define stat_dec_compr_inode(inode) do { } while (0) 37174c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks) do { } while (0) 37184c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks) do { } while (0) 3719d66450e7SArnd Bergmann #define stat_inc_atomic_write(inode) do { } while (0) 3720d66450e7SArnd Bergmann #define stat_dec_atomic_write(inode) do { } while (0) 3721d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode) do { } while (0) 3722d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode) do { } while (0) 3723d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode) do { } while (0) 3724d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode) do { } while (0) 3725b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr) do { } while (0) 3726d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg) do { } while (0) 3727d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg) do { } while (0) 3728d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi) do { } while (0) 3729d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type) do { } while (0) 3730d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks) do { } while (0) 3731d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type) do { } while (0) 3732d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type) do { } while (0) 373339a53e0cSJaegeuk Kim 373439a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; } 373539a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { } 373621acc07dSGreg Kroah-Hartman static inline void __init f2fs_create_root_stats(void) { } 37374589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { } 373848abe91aSYueHaibing static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {} 373939a53e0cSJaegeuk Kim #endif 374039a53e0cSJaegeuk Kim 374139a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations; 374239a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations; 374339a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations; 374439a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops; 374539a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops; 374639a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops; 374739a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations; 374839a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations; 3749cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations; 375039a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations; 37514d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab; 37521001b347SHuajun Li 3753e18c65b2SHuajun Li /* 3754e18c65b2SHuajun Li * inline.c 3755e18c65b2SHuajun Li */ 3756cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode); 3757cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode); 37584d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage); 37594d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode, 37604d57b86dSChao Yu struct page *ipage, u64 from); 3761cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page); 3762cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page); 3763cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode); 3764b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry); 3765cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page); 37669627a7b3SChao Yu int f2fs_recover_inline_data(struct inode *inode, struct page *npage); 37674d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir, 376843c780baSEric Biggers const struct f2fs_filename *fname, 376943c780baSEric Biggers struct page **res_page); 37704d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent, 3771cac5a3d8SDongOh Shin struct page *ipage); 377243c780baSEric Biggers int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname, 3773cac5a3d8SDongOh Shin struct inode *inode, nid_t ino, umode_t mode); 37744d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, 37754d57b86dSChao Yu struct page *page, struct inode *dir, 37764d57b86dSChao Yu struct inode *inode); 3777cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir); 3778cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx, 3779cac5a3d8SDongOh Shin struct fscrypt_str *fstr); 3780cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode, 3781cac5a3d8SDongOh Shin struct fiemap_extent_info *fieinfo, 3782cac5a3d8SDongOh Shin __u64 start, __u64 len); 3783cde4de12SJaegeuk Kim 3784cde4de12SJaegeuk Kim /* 37852658e50dSJaegeuk Kim * shrinker.c 37862658e50dSJaegeuk Kim */ 3787cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink, 3788cac5a3d8SDongOh Shin struct shrink_control *sc); 3789cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink, 3790cac5a3d8SDongOh Shin struct shrink_control *sc); 3791cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi); 3792cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi); 37932658e50dSJaegeuk Kim 37942658e50dSJaegeuk Kim /* 3795a28ef1f5SChao Yu * extent_cache.c 3796a28ef1f5SChao Yu */ 37974dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root, 3798004b6862SChao Yu struct rb_entry *cached_re, unsigned int ofs); 37992e9b2bb2SChao Yu struct rb_node **f2fs_lookup_rb_tree_ext(struct f2fs_sb_info *sbi, 38002e9b2bb2SChao Yu struct rb_root_cached *root, 38012e9b2bb2SChao Yu struct rb_node **parent, 38022e9b2bb2SChao Yu unsigned long long key, bool *left_most); 38034d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi, 38044dada3fdSChao Yu struct rb_root_cached *root, 38054dada3fdSChao Yu struct rb_node **parent, 38064dada3fdSChao Yu unsigned int ofs, bool *leftmost); 38074dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root, 3808004b6862SChao Yu struct rb_entry *cached_re, unsigned int ofs, 3809004b6862SChao Yu struct rb_entry **prev_entry, struct rb_entry **next_entry, 3810004b6862SChao Yu struct rb_node ***insert_p, struct rb_node **insert_parent, 38114dada3fdSChao Yu bool force, bool *leftmost); 38124d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi, 38132e9b2bb2SChao Yu struct rb_root_cached *root, bool check_key); 3814cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink); 3815a6d601f3SChao Yu void f2fs_init_extent_tree(struct inode *inode, struct page *ipage); 3816cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode); 3817cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode); 3818cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode); 3819cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs, 3820cac5a3d8SDongOh Shin struct extent_info *ei); 3821cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn); 382219b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn, 3823cac5a3d8SDongOh Shin pgoff_t fofs, block_t blkaddr, unsigned int len); 38244d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi); 38254d57b86dSChao Yu int __init f2fs_create_extent_cache(void); 38264d57b86dSChao Yu void f2fs_destroy_extent_cache(void); 3827a28ef1f5SChao Yu 3828a28ef1f5SChao Yu /* 38298ceffcb2SChao Yu * sysfs.c 38308ceffcb2SChao Yu */ 3831dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void); 3832dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void); 3833dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi); 3834dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi); 38358ceffcb2SChao Yu 383695ae251fSEric Biggers /* verity.c */ 383795ae251fSEric Biggers extern const struct fsverity_operations f2fs_verityops; 383895ae251fSEric Biggers 38398ceffcb2SChao Yu /* 3840cde4de12SJaegeuk Kim * crypto support 3841cde4de12SJaegeuk Kim */ 38421958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode) 38431958593eSJaegeuk Kim { 384462230e0dSChandan Rajendra return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode); 38451958593eSJaegeuk Kim } 38461958593eSJaegeuk Kim 3847cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode) 3848cde4de12SJaegeuk Kim { 3849643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 3850cde4de12SJaegeuk Kim file_set_encrypt(inode); 38519149a5ebSChao Yu f2fs_set_inode_flags(inode); 3852cde4de12SJaegeuk Kim #endif 3853cde4de12SJaegeuk Kim } 3854cde4de12SJaegeuk Kim 38556dbb1796SEric Biggers /* 38566dbb1796SEric Biggers * Returns true if the reads of the inode's data need to undergo some 38576dbb1796SEric Biggers * postprocessing step, like decryption or authenticity verification. 38586dbb1796SEric Biggers */ 38596dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode) 3860cde4de12SJaegeuk Kim { 38614c8ff709SChao Yu return f2fs_encrypted_file(inode) || fsverity_active(inode) || 38624c8ff709SChao Yu f2fs_compressed_file(inode); 38634c8ff709SChao Yu } 38644c8ff709SChao Yu 38654c8ff709SChao Yu /* 38664c8ff709SChao Yu * compress.c 38674c8ff709SChao Yu */ 38684c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION 38694c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page); 38704c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page); 38714c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode, 38724c8ff709SChao Yu struct page **pagep, pgoff_t index, void **fsdata); 38734c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata, 38744c8ff709SChao Yu pgoff_t index, unsigned copied); 38753265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock); 38764c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page); 38774c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode); 38785e6bbde9SChao Yu int f2fs_init_compress_mempool(void); 38795e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void); 38804c8ff709SChao Yu void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity); 38814c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc); 38824c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index); 38834c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page); 38844c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc, 38854c8ff709SChao Yu int *submitted, 38864c8ff709SChao Yu struct writeback_control *wbc, 38874c8ff709SChao Yu enum iostat_type io_type); 38884c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index); 38894c8ff709SChao Yu int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret, 38904c8ff709SChao Yu unsigned nr_pages, sector_t *last_block_in_bio, 38910683728aSChao Yu bool is_readahead, bool for_write); 38924c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc); 38934c8ff709SChao Yu void f2fs_free_dic(struct decompress_io_ctx *dic); 38944c8ff709SChao Yu void f2fs_decompress_end_io(struct page **rpages, 38954c8ff709SChao Yu unsigned int cluster_size, bool err, bool verity); 38964c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc); 38974c8ff709SChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc); 38984c8ff709SChao Yu void f2fs_init_compress_info(struct f2fs_sb_info *sbi); 389931083031SChao Yu int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi); 390031083031SChao Yu void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi); 3901c68d6c88SChao Yu int __init f2fs_init_compress_cache(void); 3902c68d6c88SChao Yu void f2fs_destroy_compress_cache(void); 39034c8ff709SChao Yu #else 39044c8ff709SChao Yu static inline bool f2fs_is_compressed_page(struct page *page) { return false; } 39054c8ff709SChao Yu static inline bool f2fs_is_compress_backend_ready(struct inode *inode) 39064c8ff709SChao Yu { 39074c8ff709SChao Yu if (!f2fs_compressed_file(inode)) 39084c8ff709SChao Yu return true; 39094c8ff709SChao Yu /* not support compression */ 39104c8ff709SChao Yu return false; 39114c8ff709SChao Yu } 39124c8ff709SChao Yu static inline struct page *f2fs_compress_control_page(struct page *page) 39134c8ff709SChao Yu { 39144c8ff709SChao Yu WARN_ON_ONCE(1); 39154c8ff709SChao Yu return ERR_PTR(-EINVAL); 39164c8ff709SChao Yu } 39175e6bbde9SChao Yu static inline int f2fs_init_compress_mempool(void) { return 0; } 39185e6bbde9SChao Yu static inline void f2fs_destroy_compress_mempool(void) { } 391931083031SChao Yu static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return 0; } 392031083031SChao Yu static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { } 3921c68d6c88SChao Yu static inline int __init f2fs_init_compress_cache(void) { return 0; } 3922c68d6c88SChao Yu static inline void f2fs_destroy_compress_cache(void) { } 39234c8ff709SChao Yu #endif 39244c8ff709SChao Yu 39254c8ff709SChao Yu static inline void set_compress_context(struct inode *inode) 39264c8ff709SChao Yu { 39274c8ff709SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 39284c8ff709SChao Yu 39294c8ff709SChao Yu F2FS_I(inode)->i_compress_algorithm = 39304c8ff709SChao Yu F2FS_OPTION(sbi).compress_algorithm; 39314c8ff709SChao Yu F2FS_I(inode)->i_log_cluster_size = 39324c8ff709SChao Yu F2FS_OPTION(sbi).compress_log_size; 3933b28f047bSChao Yu F2FS_I(inode)->i_compress_flag = 3934b28f047bSChao Yu F2FS_OPTION(sbi).compress_chksum ? 3935b28f047bSChao Yu 1 << COMPRESS_CHKSUM : 0; 39364c8ff709SChao Yu F2FS_I(inode)->i_cluster_size = 39374c8ff709SChao Yu 1 << F2FS_I(inode)->i_log_cluster_size; 39384c8ff709SChao Yu F2FS_I(inode)->i_flags |= F2FS_COMPR_FL; 39394c8ff709SChao Yu set_inode_flag(inode, FI_COMPRESSED_FILE); 39404c8ff709SChao Yu stat_inc_compr_inode(inode); 3941530e0704SChao Yu f2fs_mark_inode_dirty_sync(inode, true); 39424c8ff709SChao Yu } 39434c8ff709SChao Yu 394478134d03SDaeho Jeong static inline bool f2fs_disable_compressed_file(struct inode *inode) 39454c8ff709SChao Yu { 39464c8ff709SChao Yu struct f2fs_inode_info *fi = F2FS_I(inode); 39474c8ff709SChao Yu 39484c8ff709SChao Yu if (!f2fs_compressed_file(inode)) 394978134d03SDaeho Jeong return true; 395078134d03SDaeho Jeong if (S_ISREG(inode->i_mode) && 395178134d03SDaeho Jeong (get_dirty_pages(inode) || atomic_read(&fi->i_compr_blocks))) 395278134d03SDaeho Jeong return false; 39534c8ff709SChao Yu 39544c8ff709SChao Yu fi->i_flags &= ~F2FS_COMPR_FL; 39554c8ff709SChao Yu stat_dec_compr_inode(inode); 395696f5b4faSChao Yu clear_inode_flag(inode, FI_COMPRESSED_FILE); 3957530e0704SChao Yu f2fs_mark_inode_dirty_sync(inode, true); 395878134d03SDaeho Jeong return true; 3959cde4de12SJaegeuk Kim } 3960cde4de12SJaegeuk Kim 3961ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \ 39627beb01f7SChao Yu static inline int f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \ 3963ccd31cb2SSheng Yong { \ 39647beb01f7SChao Yu return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \ 3965cde4de12SJaegeuk Kim } 3966f424f664SJaegeuk Kim 3967ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT); 3968ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED); 3969ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR); 3970ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA); 3971ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM); 3972ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR); 3973ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO); 3974ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME); 3975b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND); 397695ae251fSEric Biggers F2FS_FEATURE_FUNCS(verity, VERITY); 3977d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM); 39785aba5430SDaniel Rosenberg F2FS_FEATURE_FUNCS(casefold, CASEFOLD); 39794c8ff709SChao Yu F2FS_FEATURE_FUNCS(compression, COMPRESSION); 39801c1d35dfSChao Yu 3981178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED 398295175dafSDamien Le Moal static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi, 398395175dafSDamien Le Moal block_t blkaddr) 3984178053e2SDamien Le Moal { 3985178053e2SDamien Le Moal unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz; 3986178053e2SDamien Le Moal 398795175dafSDamien Le Moal return test_bit(zno, FDEV(devi).blkz_seq); 3988178053e2SDamien Le Moal } 3989178053e2SDamien Le Moal #endif 3990178053e2SDamien Le Moal 39917d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi) 399296ba2decSDamien Le Moal { 39937beb01f7SChao Yu return f2fs_sb_has_blkzoned(sbi); 39947d20c8abSChao Yu } 399596ba2decSDamien Le Moal 39967f3d7719SDamien Le Moal static inline bool f2fs_bdev_support_discard(struct block_device *bdev) 39977f3d7719SDamien Le Moal { 39987f3d7719SDamien Le Moal return blk_queue_discard(bdev_get_queue(bdev)) || 39997f3d7719SDamien Le Moal bdev_is_zoned(bdev); 40007f3d7719SDamien Le Moal } 40017f3d7719SDamien Le Moal 40027d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi) 40037d20c8abSChao Yu { 40047f3d7719SDamien Le Moal int i; 40057f3d7719SDamien Le Moal 40067f3d7719SDamien Le Moal if (!f2fs_is_multi_device(sbi)) 40077f3d7719SDamien Le Moal return f2fs_bdev_support_discard(sbi->sb->s_bdev); 40087f3d7719SDamien Le Moal 40097f3d7719SDamien Le Moal for (i = 0; i < sbi->s_ndevs; i++) 40107f3d7719SDamien Le Moal if (f2fs_bdev_support_discard(FDEV(i).bdev)) 40117f3d7719SDamien Le Moal return true; 40127f3d7719SDamien Le Moal return false; 40137d20c8abSChao Yu } 40147d20c8abSChao Yu 40157d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi) 40167d20c8abSChao Yu { 40177d20c8abSChao Yu return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) || 40187d20c8abSChao Yu f2fs_hw_should_discard(sbi); 401952763a4bSJaegeuk Kim } 402052763a4bSJaegeuk Kim 4021f824deb5SChao Yu static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi) 4022f824deb5SChao Yu { 4023f824deb5SChao Yu int i; 4024f824deb5SChao Yu 4025f824deb5SChao Yu if (!f2fs_is_multi_device(sbi)) 4026f824deb5SChao Yu return bdev_read_only(sbi->sb->s_bdev); 4027f824deb5SChao Yu 4028f824deb5SChao Yu for (i = 0; i < sbi->s_ndevs; i++) 4029f824deb5SChao Yu if (bdev_read_only(FDEV(i).bdev)) 4030f824deb5SChao Yu return true; 4031f824deb5SChao Yu return false; 4032f824deb5SChao Yu } 4033f824deb5SChao Yu 4034b0332a0fSChao Yu static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi) 403552763a4bSJaegeuk Kim { 4036b0332a0fSChao Yu return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS; 403752763a4bSJaegeuk Kim } 403852763a4bSJaegeuk Kim 40394c8ff709SChao Yu static inline bool f2fs_may_compress(struct inode *inode) 40404c8ff709SChao Yu { 40414c8ff709SChao Yu if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) || 40424c8ff709SChao Yu f2fs_is_atomic_file(inode) || 40434c8ff709SChao Yu f2fs_is_volatile_file(inode)) 40444c8ff709SChao Yu return false; 40454c8ff709SChao Yu return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode); 40464c8ff709SChao Yu } 40474c8ff709SChao Yu 40484c8ff709SChao Yu static inline void f2fs_i_compr_blocks_update(struct inode *inode, 40494c8ff709SChao Yu u64 blocks, bool add) 40504c8ff709SChao Yu { 40514c8ff709SChao Yu int diff = F2FS_I(inode)->i_cluster_size - blocks; 4052c2759ebaSDaeho Jeong struct f2fs_inode_info *fi = F2FS_I(inode); 40534c8ff709SChao Yu 4054ef8d563fSChao Yu /* don't update i_compr_blocks if saved blocks were released */ 4055c2759ebaSDaeho Jeong if (!add && !atomic_read(&fi->i_compr_blocks)) 4056ef8d563fSChao Yu return; 4057ef8d563fSChao Yu 40584c8ff709SChao Yu if (add) { 4059c2759ebaSDaeho Jeong atomic_add(diff, &fi->i_compr_blocks); 40604c8ff709SChao Yu stat_add_compr_blocks(inode, diff); 40614c8ff709SChao Yu } else { 4062c2759ebaSDaeho Jeong atomic_sub(diff, &fi->i_compr_blocks); 40634c8ff709SChao Yu stat_sub_compr_blocks(inode, diff); 40644c8ff709SChao Yu } 40654c8ff709SChao Yu f2fs_mark_inode_dirty_sync(inode, true); 40664c8ff709SChao Yu } 40674c8ff709SChao Yu 4068f847c699SChao Yu static inline int block_unaligned_IO(struct inode *inode, 4069f847c699SChao Yu struct kiocb *iocb, struct iov_iter *iter) 4070b91050a8SHyunchul Lee { 4071f847c699SChao Yu unsigned int i_blkbits = READ_ONCE(inode->i_blkbits); 4072f847c699SChao Yu unsigned int blocksize_mask = (1 << i_blkbits) - 1; 4073f847c699SChao Yu loff_t offset = iocb->ki_pos; 4074f847c699SChao Yu unsigned long align = offset | iov_iter_alignment(iter); 4075f847c699SChao Yu 4076f847c699SChao Yu return align & blocksize_mask; 4077f847c699SChao Yu } 4078f847c699SChao Yu 4079f847c699SChao Yu static inline int allow_outplace_dio(struct inode *inode, 4080f847c699SChao Yu struct kiocb *iocb, struct iov_iter *iter) 4081f847c699SChao Yu { 4082f847c699SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 4083f847c699SChao Yu int rw = iov_iter_rw(iter); 4084f847c699SChao Yu 4085b0332a0fSChao Yu return (f2fs_lfs_mode(sbi) && (rw == WRITE) && 4086f847c699SChao Yu !block_unaligned_IO(inode, iocb, iter)); 4087f847c699SChao Yu } 4088f847c699SChao Yu 4089f847c699SChao Yu static inline bool f2fs_force_buffered_io(struct inode *inode, 4090f847c699SChao Yu struct kiocb *iocb, struct iov_iter *iter) 4091f847c699SChao Yu { 4092f847c699SChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 4093f847c699SChao Yu int rw = iov_iter_rw(iter); 4094f847c699SChao Yu 4095f847c699SChao Yu if (f2fs_post_read_required(inode)) 4096f847c699SChao Yu return true; 40970916878dSDamien Le Moal if (f2fs_is_multi_device(sbi)) 4098f847c699SChao Yu return true; 4099f847c699SChao Yu /* 4100f847c699SChao Yu * for blkzoned device, fallback direct IO to buffered IO, so 4101f847c699SChao Yu * all IOs can be serialized by log-structured write. 4102f847c699SChao Yu */ 41037beb01f7SChao Yu if (f2fs_sb_has_blkzoned(sbi)) 4104f847c699SChao Yu return true; 4105b0332a0fSChao Yu if (f2fs_lfs_mode(sbi) && (rw == WRITE)) { 41069720ee80SChao Yu if (block_unaligned_IO(inode, iocb, iter)) 4107f847c699SChao Yu return true; 41089720ee80SChao Yu if (F2FS_IO_ALIGNED(sbi)) 41099720ee80SChao Yu return true; 41109720ee80SChao Yu } 41114969c06aSJaegeuk Kim if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED) && 41123ee0c5d3SChao Yu !IS_SWAPFILE(inode)) 41134354994fSDaniel Rosenberg return true; 41144354994fSDaniel Rosenberg 4115f847c699SChao Yu return false; 4116b91050a8SHyunchul Lee } 4117b91050a8SHyunchul Lee 411883a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION 4119d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate, 4120d494500aSChao Yu unsigned int type); 412183a3bfdbSJaegeuk Kim #else 4122d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type) do { } while (0) 412383a3bfdbSJaegeuk Kim #endif 412483a3bfdbSJaegeuk Kim 4125af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi) 4126af033b2aSChao Yu { 4127af033b2aSChao Yu #ifdef CONFIG_QUOTA 41287beb01f7SChao Yu if (f2fs_sb_has_quota_ino(sbi)) 4129af033b2aSChao Yu return true; 4130af033b2aSChao Yu if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] || 4131af033b2aSChao Yu F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] || 4132af033b2aSChao Yu F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]) 4133af033b2aSChao Yu return true; 4134af033b2aSChao Yu #endif 4135af033b2aSChao Yu return false; 4136af033b2aSChao Yu } 41370af725fcSJaegeuk Kim 413810f966bbSChao Yu #define EFSBADCRC EBADMSG /* Bad CRC detected */ 413910f966bbSChao Yu #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */ 414010f966bbSChao Yu 4141e1074d4bSChao Yu #endif /* _LINUX_F2FS_H */ 4142