File Index
Files and directories
Sign in
FILE

f2fs.h

File preview with syntax highlighting.

linux-6.19.14/fs/f2fs/f2fs.h C
   1/* SPDX-License-Identifier: GPL-2.0 */
   2/*
   3 * fs/f2fs/f2fs.h
   4 *
   5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
   6 *             http://www.samsung.com/
   7 */
   8#ifndef _LINUX_F2FS_H
   9#define _LINUX_F2FS_H
  10
  11#include <linux/uio.h>
  12#include <linux/types.h>
  13#include <linux/page-flags.h>
  14#include <linux/slab.h>
  15#include <linux/crc32.h>
  16#include <linux/magic.h>
  17#include <linux/kobject.h>
  18#include <linux/sched.h>
  19#include <linux/cred.h>
  20#include <linux/sched/mm.h>
  21#include <linux/vmalloc.h>
  22#include <linux/bio.h>
  23#include <linux/blkdev.h>
  24#include <linux/quotaops.h>
  25#include <linux/part_stat.h>
  26#include <linux/rw_hint.h>
  27
  28#include <linux/fscrypt.h>
  29#include <linux/fsverity.h>
  30
  31struct pagevec;
  32
  33#ifdef CONFIG_F2FS_CHECK_FS
  34#define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
  35#else
  36#define f2fs_bug_on(sbi, condition)					\
  37	do {								\
  38		if (WARN_ON(condition))					\
  39			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
  40	} while (0)
  41#endif
  42
  43enum {
  44	FAULT_KMALLOC,
  45	FAULT_KVMALLOC,
  46	FAULT_PAGE_ALLOC,
  47	FAULT_PAGE_GET,
  48	FAULT_ALLOC_BIO,	/* it's obsolete due to bio_alloc() will never fail */
  49	FAULT_ALLOC_NID,
  50	FAULT_ORPHAN,
  51	FAULT_BLOCK,
  52	FAULT_DIR_DEPTH,
  53	FAULT_EVICT_INODE,
  54	FAULT_TRUNCATE,
  55	FAULT_READ_IO,
  56	FAULT_CHECKPOINT,
  57	FAULT_DISCARD,
  58	FAULT_WRITE_IO,
  59	FAULT_SLAB_ALLOC,
  60	FAULT_DQUOT_INIT,
  61	FAULT_LOCK_OP,
  62	FAULT_BLKADDR_VALIDITY,
  63	FAULT_BLKADDR_CONSISTENCE,
  64	FAULT_NO_SEGMENT,
  65	FAULT_INCONSISTENT_FOOTER,
  66	FAULT_TIMEOUT,
  67	FAULT_VMALLOC,
  68	FAULT_MAX,
  69};
  70
  71/* indicate which option to update */
  72enum fault_option {
  73	FAULT_RATE	= 1,	/* only update fault rate */
  74	FAULT_TYPE	= 2,	/* only update fault type */
  75	FAULT_ALL	= 4,	/* reset all fault injection options/stats */
  76};
  77
  78#ifdef CONFIG_F2FS_FAULT_INJECTION
  79struct f2fs_fault_info {
  80	atomic_t inject_ops;
  81	int inject_rate;
  82	unsigned int inject_type;
  83	/* Used to account total count of injection for each type */
  84	unsigned int inject_count[FAULT_MAX];
  85};
  86
  87extern const char *f2fs_fault_name[FAULT_MAX];
  88#define IS_FAULT_SET(fi, type) ((fi)->inject_type & BIT(type))
  89
  90/* maximum retry count for injected failure */
  91#define DEFAULT_FAILURE_RETRY_COUNT		8
  92#else
  93#define DEFAULT_FAILURE_RETRY_COUNT		1
  94#endif
  95
  96/*
  97 * For mount options
  98 */
  99enum f2fs_mount_opt {
 100	F2FS_MOUNT_DISABLE_ROLL_FORWARD,
 101	F2FS_MOUNT_DISCARD,
 102	F2FS_MOUNT_NOHEAP,
 103	F2FS_MOUNT_XATTR_USER,
 104	F2FS_MOUNT_POSIX_ACL,
 105	F2FS_MOUNT_DISABLE_EXT_IDENTIFY,
 106	F2FS_MOUNT_INLINE_XATTR,
 107	F2FS_MOUNT_INLINE_DATA,
 108	F2FS_MOUNT_INLINE_DENTRY,
 109	F2FS_MOUNT_FLUSH_MERGE,
 110	F2FS_MOUNT_NOBARRIER,
 111	F2FS_MOUNT_FASTBOOT,
 112	F2FS_MOUNT_READ_EXTENT_CACHE,
 113	F2FS_MOUNT_DATA_FLUSH,
 114	F2FS_MOUNT_FAULT_INJECTION,
 115	F2FS_MOUNT_USRQUOTA,
 116	F2FS_MOUNT_GRPQUOTA,
 117	F2FS_MOUNT_PRJQUOTA,
 118	F2FS_MOUNT_QUOTA,
 119	F2FS_MOUNT_INLINE_XATTR_SIZE,
 120	F2FS_MOUNT_RESERVE_ROOT,
 121	F2FS_MOUNT_DISABLE_CHECKPOINT,
 122	F2FS_MOUNT_NORECOVERY,
 123	F2FS_MOUNT_ATGC,
 124	F2FS_MOUNT_MERGE_CHECKPOINT,
 125	F2FS_MOUNT_GC_MERGE,
 126	F2FS_MOUNT_COMPRESS_CACHE,
 127	F2FS_MOUNT_AGE_EXTENT_CACHE,
 128	F2FS_MOUNT_NAT_BITS,
 129	F2FS_MOUNT_INLINECRYPT,
 130	/*
 131	 * Some f2fs environments expect to be able to pass the "lazytime" option
 132	 * string rather than using the MS_LAZYTIME flag, so this must remain.
 133	 */
 134	F2FS_MOUNT_LAZYTIME,
 135	F2FS_MOUNT_RESERVE_NODE,
 136};
 137
 138#define F2FS_OPTION(sbi)	((sbi)->mount_opt)
 139#define clear_opt(sbi, option)		\
 140	(F2FS_OPTION(sbi).opt &= ~BIT(F2FS_MOUNT_##option))
 141#define set_opt(sbi, option)		\
 142	(F2FS_OPTION(sbi).opt |= BIT(F2FS_MOUNT_##option))
 143#define test_opt(sbi, option)		\
 144	(F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
 145
 146#define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
 147		typecheck(unsigned long long, b) &&			\
 148		((long long)((a) - (b)) > 0))
 149
 150typedef u32 block_t;	/*
 151			 * should not change u32, since it is the on-disk block
 152			 * address format, __le32.
 153			 */
 154typedef u32 nid_t;
 155
 156#define COMPRESS_EXT_NUM		16
 157
 158enum blkzone_allocation_policy {
 159	BLKZONE_ALLOC_PRIOR_SEQ,	/* Prioritize writing to sequential zones */
 160	BLKZONE_ALLOC_ONLY_SEQ,		/* Only allow writing to sequential zones */
 161	BLKZONE_ALLOC_PRIOR_CONV,	/* Prioritize writing to conventional zones */
 162};
 163
 164enum bggc_io_aware_policy {
 165	AWARE_ALL_IO,		/* skip background GC if there is any kind of pending IO */
 166	AWARE_READ_IO,		/* skip background GC if there is pending read IO */
 167	AWARE_NONE,			/* don't aware IO for background GC */
 168};
 169
 170enum device_allocation_policy {
 171	ALLOCATE_FORWARD_NOHINT,
 172	ALLOCATE_FORWARD_WITHIN_HINT,
 173	ALLOCATE_FORWARD_FROM_HINT,
 174};
 175
 176/*
 177 * An implementation of an rwsem that is explicitly unfair to readers. This
 178 * prevents priority inversion when a low-priority reader acquires the read lock
 179 * while sleeping on the write lock but the write lock is needed by
 180 * higher-priority clients.
 181 */
 182
 183struct f2fs_rwsem {
 184        struct rw_semaphore internal_rwsem;
 185#ifdef CONFIG_F2FS_UNFAIR_RWSEM
 186        wait_queue_head_t read_waiters;
 187#endif
 188};
 189
 190struct f2fs_mount_info {
 191	unsigned long long opt;
 192	block_t root_reserved_blocks;	/* root reserved blocks */
 193	block_t root_reserved_nodes;	/* root reserved nodes */
 194	kuid_t s_resuid;		/* reserved blocks for uid */
 195	kgid_t s_resgid;		/* reserved blocks for gid */
 196	int active_logs;		/* # of active logs */
 197	int inline_xattr_size;		/* inline xattr size */
 198#ifdef CONFIG_F2FS_FAULT_INJECTION
 199	struct f2fs_fault_info fault_info;	/* For fault injection */
 200#endif
 201#ifdef CONFIG_QUOTA
 202	/* Names of quota files with journalled quota */
 203	char *s_qf_names[MAXQUOTAS];
 204	int s_jquota_fmt;			/* Format of quota to use */
 205#endif
 206	/* For which write hints are passed down to block layer */
 207	int alloc_mode;			/* segment allocation policy */
 208	int fsync_mode;			/* fsync policy */
 209	int fs_mode;			/* fs mode: LFS or ADAPTIVE */
 210	int bggc_mode;			/* bggc mode: off, on or sync */
 211	int memory_mode;		/* memory mode */
 212	int errors;			/* errors parameter */
 213	int discard_unit;		/*
 214					 * discard command's offset/size should
 215					 * be aligned to this unit: block,
 216					 * segment or section
 217					 */
 218	struct fscrypt_dummy_policy dummy_enc_policy; /* test dummy encryption */
 219	block_t unusable_cap_perc;	/* percentage for cap */
 220	block_t unusable_cap;		/* Amount of space allowed to be
 221					 * unusable when disabling checkpoint
 222					 */
 223
 224	/* For compression */
 225	unsigned char compress_algorithm;	/* algorithm type */
 226	unsigned char compress_log_size;	/* cluster log size */
 227	unsigned char compress_level;		/* compress level */
 228	bool compress_chksum;			/* compressed data chksum */
 229	unsigned char compress_ext_cnt;		/* extension count */
 230	unsigned char nocompress_ext_cnt;		/* nocompress extension count */
 231	int compress_mode;			/* compression mode */
 232	unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN];	/* extensions */
 233	unsigned char noextensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */
 234	unsigned int lookup_mode;
 235};
 236
 237#define F2FS_FEATURE_ENCRYPT			0x00000001
 238#define F2FS_FEATURE_BLKZONED			0x00000002
 239#define F2FS_FEATURE_ATOMIC_WRITE		0x00000004
 240#define F2FS_FEATURE_EXTRA_ATTR			0x00000008
 241#define F2FS_FEATURE_PRJQUOTA			0x00000010
 242#define F2FS_FEATURE_INODE_CHKSUM		0x00000020
 243#define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x00000040
 244#define F2FS_FEATURE_QUOTA_INO			0x00000080
 245#define F2FS_FEATURE_INODE_CRTIME		0x00000100
 246#define F2FS_FEATURE_LOST_FOUND			0x00000200
 247#define F2FS_FEATURE_VERITY			0x00000400
 248#define F2FS_FEATURE_SB_CHKSUM			0x00000800
 249#define F2FS_FEATURE_CASEFOLD			0x00001000
 250#define F2FS_FEATURE_COMPRESSION		0x00002000
 251#define F2FS_FEATURE_RO				0x00004000
 252#define F2FS_FEATURE_DEVICE_ALIAS		0x00008000
 253#define F2FS_FEATURE_PACKED_SSA			0x00010000
 254
 255#define __F2FS_HAS_FEATURE(raw_super, mask)				\
 256	((raw_super->feature & cpu_to_le32(mask)) != 0)
 257#define F2FS_HAS_FEATURE(sbi, mask)	__F2FS_HAS_FEATURE(sbi->raw_super, mask)
 258
 259/*
 260 * Default values for user and/or group using reserved blocks
 261 */
 262#define	F2FS_DEF_RESUID		0
 263#define	F2FS_DEF_RESGID		0
 264
 265/*
 266 * For checkpoint manager
 267 */
 268enum {
 269	NAT_BITMAP,
 270	SIT_BITMAP
 271};
 272
 273#define	CP_UMOUNT	0x00000001
 274#define	CP_FASTBOOT	0x00000002
 275#define	CP_SYNC		0x00000004
 276#define	CP_RECOVERY	0x00000008
 277#define	CP_DISCARD	0x00000010
 278#define CP_TRIMMED	0x00000020
 279#define CP_PAUSE	0x00000040
 280#define CP_RESIZE 	0x00000080
 281
 282#define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
 283#define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
 284#define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
 285#define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
 286#define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
 287#define DEF_CP_INTERVAL			60	/* 60 secs */
 288#define DEF_IDLE_INTERVAL		5	/* 5 secs */
 289#define DEF_DISABLE_INTERVAL		5	/* 5 secs */
 290#define DEF_ENABLE_INTERVAL		16	/* 16 secs */
 291#define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
 292#define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
 293
 294enum cp_time {
 295	CP_TIME_START,		/* begin */
 296	CP_TIME_LOCK,		/* after cp_global_sem */
 297	CP_TIME_OP_LOCK,	/* after block_operation */
 298	CP_TIME_FLUSH_META,	/* after flush sit/nat */
 299	CP_TIME_SYNC_META,	/* after sync_meta_pages */
 300	CP_TIME_SYNC_CP_META,	/* after sync cp meta pages */
 301	CP_TIME_WAIT_DIRTY_META,/* after wait on dirty meta */
 302	CP_TIME_WAIT_CP_DATA,	/* after wait on cp data */
 303	CP_TIME_FLUSH_DEVICE,	/* after flush device cache */
 304	CP_TIME_WAIT_LAST_CP,	/* after wait on last cp pack */
 305	CP_TIME_END,		/* after unblock_operation */
 306	CP_TIME_MAX,
 307};
 308
 309/* time cost stats of checkpoint */
 310struct cp_stats {
 311	ktime_t times[CP_TIME_MAX];
 312};
 313
 314struct cp_control {
 315	int reason;
 316	__u64 trim_start;
 317	__u64 trim_end;
 318	__u64 trim_minlen;
 319	struct cp_stats stats;
 320};
 321
 322enum f2fs_cp_phase {
 323	CP_PHASE_START_BLOCK_OPS,
 324	CP_PHASE_FINISH_BLOCK_OPS,
 325	CP_PHASE_FINISH_CHECKPOINT,
 326};
 327
 328/*
 329 * indicate meta/data type
 330 */
 331enum {
 332	META_CP,
 333	META_NAT,
 334	META_SIT,
 335	META_SSA,
 336	META_MAX,
 337	META_POR,
 338	DATA_GENERIC,		/* check range only */
 339	DATA_GENERIC_ENHANCE,	/* strong check on range and segment bitmap */
 340	DATA_GENERIC_ENHANCE_READ,	/*
 341					 * strong check on range and segment
 342					 * bitmap but no warning due to race
 343					 * condition of read on truncated area
 344					 * by extent_cache
 345					 */
 346	DATA_GENERIC_ENHANCE_UPDATE,	/*
 347					 * strong check on range and segment
 348					 * bitmap for update case
 349					 */
 350	META_GENERIC,
 351};
 352
 353/* for the list of ino */
 354enum {
 355	ORPHAN_INO,		/* for orphan ino list */
 356	APPEND_INO,		/* for append ino list */
 357	UPDATE_INO,		/* for update ino list */
 358	TRANS_DIR_INO,		/* for transactions dir ino list */
 359	XATTR_DIR_INO,		/* for xattr updated dir ino list */
 360	FLUSH_INO,		/* for multiple device flushing */
 361	MAX_INO_ENTRY,		/* max. list */
 362};
 363
 364struct ino_entry {
 365	struct list_head list;		/* list head */
 366	nid_t ino;			/* inode number */
 367	unsigned int dirty_device;	/* dirty device bitmap */
 368};
 369
 370/* for the list of inodes to be GCed */
 371struct inode_entry {
 372	struct list_head list;	/* list head */
 373	struct inode *inode;	/* vfs inode pointer */
 374};
 375
 376struct fsync_node_entry {
 377	struct list_head list;	/* list head */
 378	struct folio *folio;	/* warm node folio pointer */
 379	unsigned int seq_id;	/* sequence id */
 380};
 381
 382struct ckpt_req {
 383	struct completion wait;		/* completion for checkpoint done */
 384	struct llist_node llnode;	/* llist_node to be linked in wait queue */
 385	int ret;			/* return code of checkpoint */
 386	union {
 387		ktime_t queue_time;	/* request queued time */
 388		ktime_t delta_time;	/* time in queue */
 389	};
 390};
 391
 392struct ckpt_req_control {
 393	struct task_struct *f2fs_issue_ckpt;	/* checkpoint task */
 394	int ckpt_thread_ioprio;			/* checkpoint merge thread ioprio */
 395	wait_queue_head_t ckpt_wait_queue;	/* waiting queue for wake-up */
 396	atomic_t issued_ckpt;		/* # of actually issued ckpts */
 397	atomic_t total_ckpt;		/* # of total ckpts */
 398	atomic_t queued_ckpt;		/* # of queued ckpts */
 399	struct llist_head issue_list;	/* list for command issue */
 400	spinlock_t stat_lock;		/* lock for below checkpoint time stats */
 401	unsigned int cur_time;		/* cur wait time in msec for currently issued checkpoint */
 402	unsigned int peak_time;		/* peak wait time in msec until now */
 403};
 404
 405/* a time threshold that checkpoint was blocked for, unit: ms */
 406#define CP_LONG_LATENCY_THRESHOLD	5000
 407
 408/* for the bitmap indicate blocks to be discarded */
 409struct discard_entry {
 410	struct list_head list;	/* list head */
 411	block_t start_blkaddr;	/* start blockaddr of current segment */
 412	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
 413};
 414
 415/* minimum discard granularity, unit: block count */
 416#define MIN_DISCARD_GRANULARITY		1
 417/* default discard granularity of inner discard thread, unit: block count */
 418#define DEFAULT_DISCARD_GRANULARITY		16
 419/* default maximum discard granularity of ordered discard, unit: block count */
 420#define DEFAULT_MAX_ORDERED_DISCARD_GRANULARITY	16
 421/* default interval of periodical discard submission */
 422#define DEFAULT_DISCARD_INTERVAL	(msecs_to_jiffies(20))
 423
 424/* max discard pend list number */
 425#define MAX_PLIST_NUM		512
 426#define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
 427					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
 428
 429enum {
 430	D_PREP,			/* initial */
 431	D_PARTIAL,		/* partially submitted */
 432	D_SUBMIT,		/* all submitted */
 433	D_DONE,			/* finished */
 434};
 435
 436struct discard_info {
 437	block_t lstart;			/* logical start address */
 438	block_t len;			/* length */
 439	block_t start;			/* actual start address in dev */
 440};
 441
 442struct discard_cmd {
 443	struct rb_node rb_node;		/* rb node located in rb-tree */
 444	struct discard_info di;		/* discard info */
 445	struct list_head list;		/* command list */
 446	struct completion wait;		/* completion */
 447	struct block_device *bdev;	/* bdev */
 448	unsigned short ref;		/* reference count */
 449	unsigned char state;		/* state */
 450	unsigned char queued;		/* queued discard */
 451	int error;			/* bio error */
 452	spinlock_t lock;		/* for state/bio_ref updating */
 453	unsigned short bio_ref;		/* bio reference count */
 454};
 455
 456enum {
 457	DPOLICY_BG,
 458	DPOLICY_FORCE,
 459	DPOLICY_FSTRIM,
 460	DPOLICY_UMOUNT,
 461	MAX_DPOLICY,
 462};
 463
 464enum {
 465	DPOLICY_IO_AWARE_DISABLE,	/* force to not be aware of IO */
 466	DPOLICY_IO_AWARE_ENABLE,	/* force to be aware of IO */
 467	DPOLICY_IO_AWARE_MAX,
 468};
 469
 470struct discard_policy {
 471	int type;			/* type of discard */
 472	unsigned int min_interval;	/* used for candidates exist */
 473	unsigned int mid_interval;	/* used for device busy */
 474	unsigned int max_interval;	/* used for candidates not exist */
 475	unsigned int max_requests;	/* # of discards issued per round */
 476	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
 477	bool io_aware;			/* issue discard in idle time */
 478	bool sync;			/* submit discard with REQ_SYNC flag */
 479	bool ordered;			/* issue discard by lba order */
 480	bool timeout;			/* discard timeout for put_super */
 481	unsigned int granularity;	/* discard granularity */
 482};
 483
 484struct discard_cmd_control {
 485	struct task_struct *f2fs_issue_discard;	/* discard thread */
 486	struct list_head entry_list;		/* 4KB discard entry list */
 487	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
 488	struct list_head wait_list;		/* store on-flushing entries */
 489	struct list_head fstrim_list;		/* in-flight discard from fstrim */
 490	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
 491	struct mutex cmd_lock;
 492	unsigned int nr_discards;		/* # of discards in the list */
 493	unsigned int max_discards;		/* max. discards to be issued */
 494	unsigned int max_discard_request;	/* max. discard request per round */
 495	unsigned int min_discard_issue_time;	/* min. interval between discard issue */
 496	unsigned int mid_discard_issue_time;	/* mid. interval between discard issue */
 497	unsigned int max_discard_issue_time;	/* max. interval between discard issue */
 498	unsigned int discard_io_aware_gran; /* minimum discard granularity not be aware of I/O */
 499	unsigned int discard_urgent_util;	/* utilization which issue discard proactively */
 500	unsigned int discard_granularity;	/* discard granularity */
 501	unsigned int max_ordered_discard;	/* maximum discard granularity issued by lba order */
 502	unsigned int discard_io_aware;		/* io_aware policy */
 503	unsigned int undiscard_blks;		/* # of undiscard blocks */
 504	unsigned int next_pos;			/* next discard position */
 505	atomic_t issued_discard;		/* # of issued discard */
 506	atomic_t queued_discard;		/* # of queued discard */
 507	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
 508	struct rb_root_cached root;		/* root of discard rb-tree */
 509	bool rbtree_check;			/* config for consistence check */
 510	bool discard_wake;			/* to wake up discard thread */
 511};
 512
 513/* for the list of fsync inodes, used only during recovery */
 514struct fsync_inode_entry {
 515	struct list_head list;	/* list head */
 516	struct inode *inode;	/* vfs inode pointer */
 517	block_t blkaddr;	/* block address locating the last fsync */
 518	block_t last_dentry;	/* block address locating the last dentry */
 519};
 520
 521#define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
 522#define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
 523
 524#define nat_in_journal(jnl, i) \
 525	(((struct nat_journal_entry *)(jnl)->nat_j.entries)[i].ne)
 526#define nid_in_journal(jnl, i) \
 527	(((struct nat_journal_entry *)(jnl)->nat_j.entries)[i].nid)
 528#define sit_in_journal(jnl, i) \
 529	(((struct sit_journal_entry *)(jnl)->sit_j.entries)[i].se)
 530#define segno_in_journal(jnl, i) \
 531	(((struct sit_journal_entry *)(jnl)->sit_j.entries)[i].segno)
 532
 533#define sum_entries(sum)	((struct f2fs_summary *)(sum))
 534#define sum_journal(sbi, sum) \
 535	((struct f2fs_journal *)((char *)(sum) + \
 536	((sbi)->entries_in_sum * sizeof(struct f2fs_summary))))
 537#define sum_footer(sbi, sum) \
 538	((struct summary_footer *)((char *)(sum) + (sbi)->sum_blocksize - \
 539	sizeof(struct summary_footer)))
 540
 541#define MAX_NAT_JENTRIES(sbi, jnl)	((sbi)->nat_journal_entries - nats_in_cursum(jnl))
 542#define MAX_SIT_JENTRIES(sbi, jnl)	((sbi)->sit_journal_entries - sits_in_cursum(jnl))
 543
 544static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
 545{
 546	int before = nats_in_cursum(journal);
 547
 548	journal->n_nats = cpu_to_le16(before + i);
 549	return before;
 550}
 551
 552static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
 553{
 554	int before = sits_in_cursum(journal);
 555
 556	journal->n_sits = cpu_to_le16(before + i);
 557	return before;
 558}
 559
 560/* for inline stuff */
 561#define DEF_INLINE_RESERVED_SIZE	1
 562static inline int get_extra_isize(struct inode *inode);
 563static inline int get_inline_xattr_addrs(struct inode *inode);
 564#define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
 565				(CUR_ADDRS_PER_INODE(inode) -		\
 566				get_inline_xattr_addrs(inode) -	\
 567				DEF_INLINE_RESERVED_SIZE))
 568
 569/* for inline dir */
 570#define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
 571				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
 572				BITS_PER_BYTE + 1))
 573#define INLINE_DENTRY_BITMAP_SIZE(inode) \
 574	DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
 575#define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
 576				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
 577				NR_INLINE_DENTRY(inode) + \
 578				INLINE_DENTRY_BITMAP_SIZE(inode)))
 579
 580/*
 581 * For INODE and NODE manager
 582 */
 583/* for directory operations */
 584
 585struct f2fs_filename {
 586	/*
 587	 * The filename the user specified.  This is NULL for some
 588	 * filesystem-internal operations, e.g. converting an inline directory
 589	 * to a non-inline one, or roll-forward recovering an encrypted dentry.
 590	 */
 591	const struct qstr *usr_fname;
 592
 593	/*
 594	 * The on-disk filename.  For encrypted directories, this is encrypted.
 595	 * This may be NULL for lookups in an encrypted dir without the key.
 596	 */
 597	struct fscrypt_str disk_name;
 598
 599	/* The dirhash of this filename */
 600	f2fs_hash_t hash;
 601
 602#ifdef CONFIG_FS_ENCRYPTION
 603	/*
 604	 * For lookups in encrypted directories: either the buffer backing
 605	 * disk_name, or a buffer that holds the decoded no-key name.
 606	 */
 607	struct fscrypt_str crypto_buf;
 608#endif
 609#if IS_ENABLED(CONFIG_UNICODE)
 610	/*
 611	 * For casefolded directories: the casefolded name, but it's left NULL
 612	 * if the original name is not valid Unicode, if the original name is
 613	 * "." or "..", if the directory is both casefolded and encrypted and
 614	 * its encryption key is unavailable, or if the filesystem is doing an
 615	 * internal operation where usr_fname is also NULL.  In all these cases
 616	 * we fall back to treating the name as an opaque byte sequence.
 617	 */
 618	struct qstr cf_name;
 619#endif
 620};
 621
 622struct f2fs_dentry_ptr {
 623	struct inode *inode;
 624	void *bitmap;
 625	struct f2fs_dir_entry *dentry;
 626	__u8 (*filename)[F2FS_SLOT_LEN];
 627	int max;
 628	int nr_bitmap;
 629};
 630
 631static inline void make_dentry_ptr_block(struct inode *inode,
 632		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
 633{
 634	d->inode = inode;
 635	d->max = NR_DENTRY_IN_BLOCK;
 636	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
 637	d->bitmap = t->dentry_bitmap;
 638	d->dentry = t->dentry;
 639	d->filename = t->filename;
 640}
 641
 642static inline void make_dentry_ptr_inline(struct inode *inode,
 643					struct f2fs_dentry_ptr *d, void *t)
 644{
 645	int entry_cnt = NR_INLINE_DENTRY(inode);
 646	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
 647	int reserved_size = INLINE_RESERVED_SIZE(inode);
 648
 649	d->inode = inode;
 650	d->max = entry_cnt;
 651	d->nr_bitmap = bitmap_size;
 652	d->bitmap = t;
 653	d->dentry = t + bitmap_size + reserved_size;
 654	d->filename = t + bitmap_size + reserved_size +
 655					SIZE_OF_DIR_ENTRY * entry_cnt;
 656}
 657
 658/*
 659 * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
 660 * as its node offset to distinguish from index node blocks.
 661 * But some bits are used to mark the node block.
 662 */
 663#define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
 664				>> OFFSET_BIT_SHIFT)
 665enum {
 666	ALLOC_NODE,			/* allocate a new node page if needed */
 667	LOOKUP_NODE,			/* look up a node without readahead */
 668	LOOKUP_NODE_RA,			/*
 669					 * look up a node with readahead called
 670					 * by get_data_block.
 671					 */
 672};
 673
 674#define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO or flush count */
 675
 676/* IO/non-IO congestion wait timeout value, default: 1ms */
 677#define	DEFAULT_SCHEDULE_TIMEOUT	(msecs_to_jiffies(1))
 678
 679/* timeout value injected, default: 1000ms */
 680#define DEFAULT_FAULT_TIMEOUT	(msecs_to_jiffies(1000))
 681
 682/* maximum retry quota flush count */
 683#define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
 684
 685/* maximum retry of EIO'ed page */
 686#define MAX_RETRY_PAGE_EIO			100
 687
 688#define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
 689
 690#define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
 691
 692/* dirty segments threshold for triggering CP */
 693#define DEFAULT_DIRTY_THRESHOLD		4
 694
 695#define RECOVERY_MAX_RA_BLOCKS		BIO_MAX_VECS
 696#define RECOVERY_MIN_RA_BLOCKS		1
 697
 698#define F2FS_ONSTACK_PAGES	16	/* nr of onstack pages */
 699
 700/* for in-memory extent cache entry */
 701#define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
 702
 703/* number of extent info in extent cache we try to shrink */
 704#define READ_EXTENT_CACHE_SHRINK_NUMBER	128
 705
 706/* number of age extent info in extent cache we try to shrink */
 707#define AGE_EXTENT_CACHE_SHRINK_NUMBER	128
 708#define LAST_AGE_WEIGHT			30
 709#define SAME_AGE_REGION			1024
 710
 711/*
 712 * Define data block with age less than 1GB as hot data
 713 * define data block with age less than 10GB but more than 1GB as warm data
 714 */
 715#define DEF_HOT_DATA_AGE_THRESHOLD	262144
 716#define DEF_WARM_DATA_AGE_THRESHOLD	2621440
 717
 718/* default max read extent count per inode */
 719#define DEF_MAX_READ_EXTENT_COUNT	10240
 720
 721/* extent cache type */
 722enum extent_type {
 723	EX_READ,
 724	EX_BLOCK_AGE,
 725	NR_EXTENT_CACHES,
 726};
 727
 728/*
 729 * Reserved value to mark invalid age extents, hence valid block range
 730 * from 0 to ULLONG_MAX-1
 731 */
 732#define F2FS_EXTENT_AGE_INVALID	ULLONG_MAX
 733
 734struct extent_info {
 735	unsigned int fofs;		/* start offset in a file */
 736	unsigned int len;		/* length of the extent */
 737	union {
 738		/* read extent_cache */
 739		struct {
 740			/* start block address of the extent */
 741			block_t blk;
 742#ifdef CONFIG_F2FS_FS_COMPRESSION
 743			/* physical extent length of compressed blocks */
 744			unsigned int c_len;
 745#endif
 746		};
 747		/* block age extent_cache */
 748		struct {
 749			/* block age of the extent */
 750			unsigned long long age;
 751			/* last total blocks allocated */
 752			unsigned long long last_blocks;
 753		};
 754	};
 755};
 756
 757struct extent_node {
 758	struct rb_node rb_node;		/* rb node located in rb-tree */
 759	struct extent_info ei;		/* extent info */
 760	struct list_head list;		/* node in global extent list of sbi */
 761	struct extent_tree *et;		/* extent tree pointer */
 762};
 763
 764struct extent_tree {
 765	nid_t ino;			/* inode number */
 766	enum extent_type type;		/* keep the extent tree type */
 767	struct rb_root_cached root;	/* root of extent info rb-tree */
 768	struct extent_node *cached_en;	/* recently accessed extent node */
 769	struct list_head list;		/* to be used by sbi->zombie_list */
 770	rwlock_t lock;			/* protect extent info rb-tree */
 771	atomic_t node_cnt;		/* # of extent node in rb-tree*/
 772	bool largest_updated;		/* largest extent updated */
 773	struct extent_info largest;	/* largest cached extent for EX_READ */
 774};
 775
 776struct extent_tree_info {
 777	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
 778	struct mutex extent_tree_lock;	/* locking extent radix tree */
 779	struct list_head extent_list;		/* lru list for shrinker */
 780	spinlock_t extent_lock;			/* locking extent lru list */
 781	atomic_t total_ext_tree;		/* extent tree count */
 782	struct list_head zombie_list;		/* extent zombie tree list */
 783	atomic_t total_zombie_tree;		/* extent zombie tree count */
 784	atomic_t total_ext_node;		/* extent info count */
 785};
 786
 787/*
 788 * State of block returned by f2fs_map_blocks.
 789 */
 790#define F2FS_MAP_NEW		(1U << 0)
 791#define F2FS_MAP_MAPPED		(1U << 1)
 792#define F2FS_MAP_DELALLOC	(1U << 2)
 793#define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
 794				F2FS_MAP_DELALLOC)
 795
 796struct f2fs_map_blocks {
 797	struct block_device *m_bdev;	/* for multi-device dio */
 798	block_t m_pblk;
 799	block_t m_lblk;
 800	unsigned int m_len;
 801	unsigned int m_flags;
 802	unsigned long m_last_pblk;	/* last allocated block, only used for DIO in LFS mode */
 803	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
 804	pgoff_t *m_next_extent;		/* point to next possible extent */
 805	int m_seg_type;
 806	bool m_may_create;		/* indicate it is from write path */
 807	bool m_multidev_dio;		/* indicate it allows multi-device dio */
 808};
 809
 810/* for flag in get_data_block */
 811enum {
 812	F2FS_GET_BLOCK_DEFAULT,
 813	F2FS_GET_BLOCK_FIEMAP,
 814	F2FS_GET_BLOCK_BMAP,
 815	F2FS_GET_BLOCK_DIO,
 816	F2FS_GET_BLOCK_PRE_DIO,
 817	F2FS_GET_BLOCK_PRE_AIO,
 818	F2FS_GET_BLOCK_PRECACHE,
 819};
 820
 821/*
 822 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
 823 */
 824#define FADVISE_COLD_BIT	0x01
 825#define FADVISE_LOST_PINO_BIT	0x02
 826#define FADVISE_ENCRYPT_BIT	0x04
 827#define FADVISE_ENC_NAME_BIT	0x08
 828#define FADVISE_KEEP_SIZE_BIT	0x10
 829#define FADVISE_HOT_BIT		0x20
 830#define FADVISE_VERITY_BIT	0x40
 831#define FADVISE_TRUNC_BIT	0x80
 832
 833#define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
 834
 835#define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
 836#define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
 837#define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
 838
 839#define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
 840#define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
 841#define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
 842
 843#define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
 844#define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
 845
 846#define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
 847#define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
 848
 849#define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
 850#define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
 851
 852#define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
 853#define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
 854#define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
 855
 856#define file_is_verity(inode)	is_file(inode, FADVISE_VERITY_BIT)
 857#define file_set_verity(inode)	set_file(inode, FADVISE_VERITY_BIT)
 858
 859#define file_should_truncate(inode)	is_file(inode, FADVISE_TRUNC_BIT)
 860#define file_need_truncate(inode)	set_file(inode, FADVISE_TRUNC_BIT)
 861#define file_dont_truncate(inode)	clear_file(inode, FADVISE_TRUNC_BIT)
 862
 863#define DEF_DIR_LEVEL		0
 864
 865/* used for f2fs_inode_info->flags */
 866enum {
 867	FI_NEW_INODE,		/* indicate newly allocated inode */
 868	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
 869	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
 870	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
 871	FI_INC_LINK,		/* need to increment i_nlink */
 872	FI_ACL_MODE,		/* indicate acl mode */
 873	FI_NO_ALLOC,		/* should not allocate any blocks */
 874	FI_FREE_NID,		/* free allocated nide */
 875	FI_NO_EXTENT,		/* not to use the extent cache */
 876	FI_INLINE_XATTR,	/* used for inline xattr */
 877	FI_INLINE_DATA,		/* used for inline data*/
 878	FI_INLINE_DENTRY,	/* used for inline dentry */
 879	FI_APPEND_WRITE,	/* inode has appended data */
 880	FI_UPDATE_WRITE,	/* inode has in-place-update data */
 881	FI_NEED_IPU,		/* used for ipu per file */
 882	FI_ATOMIC_FILE,		/* indicate atomic file */
 883	FI_DATA_EXIST,		/* indicate data exists */
 884	FI_SKIP_WRITES,		/* should skip data page writeback */
 885	FI_OPU_WRITE,		/* used for opu per file */
 886	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
 887	FI_PREALLOCATED_ALL,	/* all blocks for write were preallocated */
 888	FI_HOT_DATA,		/* indicate file is hot */
 889	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
 890	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
 891	FI_PIN_FILE,		/* indicate file should not be gced */
 892	FI_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
 893	FI_COMPRESSED_FILE,	/* indicate file's data can be compressed */
 894	FI_COMPRESS_CORRUPT,	/* indicate compressed cluster is corrupted */
 895	FI_MMAP_FILE,		/* indicate file was mmapped */
 896	FI_ENABLE_COMPRESS,	/* enable compression in "user" compression mode */
 897	FI_COMPRESS_RELEASED,	/* compressed blocks were released */
 898	FI_ALIGNED_WRITE,	/* enable aligned write */
 899	FI_COW_FILE,		/* indicate COW file */
 900	FI_ATOMIC_COMMITTED,	/* indicate atomic commit completed except disk sync */
 901	FI_ATOMIC_DIRTIED,	/* indicate atomic file is dirtied */
 902	FI_ATOMIC_REPLACE,	/* indicate atomic replace */
 903	FI_OPENED_FILE,		/* indicate file has been opened */
 904	FI_DONATE_FINISHED,	/* indicate page donation of file has been finished */
 905	FI_MAX,			/* max flag, never be used */
 906};
 907
 908struct f2fs_inode_info {
 909	struct inode vfs_inode;		/* serve a vfs inode */
 910	unsigned long i_flags;		/* keep an inode flags for ioctl */
 911	unsigned char i_advise;		/* use to give file attribute hints */
 912	unsigned char i_dir_level;	/* use for dentry level for large dir */
 913	union {
 914		unsigned int i_current_depth;	/* only for directory depth */
 915		unsigned short i_gc_failures;	/* for gc failure statistic */
 916	};
 917	unsigned int i_pino;		/* parent inode number */
 918	umode_t i_acl_mode;		/* keep file acl mode temporarily */
 919
 920	/* Use below internally in f2fs*/
 921	unsigned long flags[BITS_TO_LONGS(FI_MAX)];	/* use to pass per-file flags */
 922	unsigned int ioprio_hint;	/* hint for IO priority */
 923	struct f2fs_rwsem i_sem;	/* protect fi info */
 924	atomic_t dirty_pages;		/* # of dirty pages */
 925	f2fs_hash_t chash;		/* hash value of given file name */
 926	unsigned int clevel;		/* maximum level of given file name */
 927	struct task_struct *task;	/* lookup and create consistency */
 928	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
 929	struct task_struct *wb_task;	/* indicate inode is in context of writeback */
 930	nid_t i_xattr_nid;		/* node id that contains xattrs */
 931	loff_t	last_disk_size;		/* lastly written file size */
 932	spinlock_t i_size_lock;		/* protect last_disk_size */
 933
 934#ifdef CONFIG_QUOTA
 935	struct dquot __rcu *i_dquot[MAXQUOTAS];
 936
 937	/* quota space reservation, managed internally by quota code */
 938	qsize_t i_reserved_quota;
 939#endif
 940	struct list_head dirty_list;	/* dirty list for dirs and files */
 941	struct list_head gdirty_list;	/* linked in global dirty list */
 942
 943	/* linked in global inode list for cache donation */
 944	struct list_head gdonate_list;
 945	pgoff_t donate_start, donate_end; /* inclusive */
 946	atomic_t open_count;		/* # of open files */
 947
 948	struct task_struct *atomic_write_task;	/* store atomic write task */
 949	struct extent_tree *extent_tree[NR_EXTENT_CACHES];
 950					/* cached extent_tree entry */
 951	union {
 952		struct inode *cow_inode;	/* copy-on-write inode for atomic write */
 953		struct inode *atomic_inode;
 954					/* point to atomic_inode, available only for cow_inode */
 955	};
 956
 957	/* avoid racing between foreground op and gc */
 958	struct f2fs_rwsem i_gc_rwsem[2];
 959	struct f2fs_rwsem i_xattr_sem; /* avoid racing between reading and changing EAs */
 960
 961	int i_extra_isize;		/* size of extra space located in i_addr */
 962	kprojid_t i_projid;		/* id for project quota */
 963	int i_inline_xattr_size;	/* inline xattr size */
 964	struct timespec64 i_crtime;	/* inode creation time */
 965	struct timespec64 i_disk_time[3];/* inode disk times */
 966
 967	/* for file compress */
 968	atomic_t i_compr_blocks;		/* # of compressed blocks */
 969	unsigned char i_compress_algorithm;	/* algorithm type */
 970	unsigned char i_log_cluster_size;	/* log of cluster size */
 971	unsigned char i_compress_level;		/* compress level (lz4hc,zstd) */
 972	unsigned char i_compress_flag;		/* compress flag */
 973	unsigned int i_cluster_size;		/* cluster size */
 974	atomic_t writeback;			/* count # of writeback thread */
 975
 976	unsigned int atomic_write_cnt;
 977	loff_t original_i_size;		/* original i_size before atomic write */
 978#ifdef CONFIG_FS_ENCRYPTION
 979	struct fscrypt_inode_info *i_crypt_info; /* filesystem encryption info */
 980#endif
 981#ifdef CONFIG_FS_VERITY
 982	struct fsverity_info *i_verity_info; /* filesystem verity info */
 983#endif
 984};
 985
 986static inline void get_read_extent_info(struct extent_info *ext,
 987					struct f2fs_extent *i_ext)
 988{
 989	ext->fofs = le32_to_cpu(i_ext->fofs);
 990	ext->blk = le32_to_cpu(i_ext->blk);
 991	ext->len = le32_to_cpu(i_ext->len);
 992}
 993
 994static inline void set_raw_read_extent(struct extent_info *ext,
 995					struct f2fs_extent *i_ext)
 996{
 997	i_ext->fofs = cpu_to_le32(ext->fofs);
 998	i_ext->blk = cpu_to_le32(ext->blk);
 999	i_ext->len = cpu_to_le32(ext->len);
1000}
1001
1002static inline bool __is_discard_mergeable(struct discard_info *back,
1003			struct discard_info *front, unsigned int max_len)
1004{
1005	return (back->lstart + back->len == front->lstart) &&
1006		(back->len + front->len <= max_len);
1007}
1008
1009static inline bool __is_discard_back_mergeable(struct discard_info *cur,
1010			struct discard_info *back, unsigned int max_len)
1011{
1012	return __is_discard_mergeable(back, cur, max_len);
1013}
1014
1015static inline bool __is_discard_front_mergeable(struct discard_info *cur,
1016			struct discard_info *front, unsigned int max_len)
1017{
1018	return __is_discard_mergeable(cur, front, max_len);
1019}
1020
1021/*
1022 * For free nid management
1023 */
1024enum nid_state {
1025	FREE_NID,		/* newly added to free nid list */
1026	PREALLOC_NID,		/* it is preallocated */
1027	MAX_NID_STATE,
1028};
1029
1030enum nat_state {
1031	TOTAL_NAT,
1032	DIRTY_NAT,
1033	RECLAIMABLE_NAT,
1034	MAX_NAT_STATE,
1035};
1036
1037struct f2fs_nm_info {
1038	block_t nat_blkaddr;		/* base disk address of NAT */
1039	nid_t max_nid;			/* maximum possible node ids */
1040	nid_t available_nids;		/* # of available node ids */
1041	nid_t next_scan_nid;		/* the next nid to be scanned */
1042	nid_t max_rf_node_blocks;	/* max # of nodes for recovery */
1043	unsigned int ram_thresh;	/* control the memory footprint */
1044	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
1045	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
1046
1047	/* NAT cache management */
1048	struct radix_tree_root nat_root;/* root of the nat entry cache */
1049	struct radix_tree_root nat_set_root;/* root of the nat set cache */
1050	struct f2fs_rwsem nat_tree_lock;	/* protect nat entry tree */
1051	struct list_head nat_entries;	/* cached nat entry list (clean) */
1052	spinlock_t nat_list_lock;	/* protect clean nat entry list */
1053	unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */
1054	unsigned int nat_blocks;	/* # of nat blocks */
1055
1056	/* free node ids management */
1057	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
1058	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
1059	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
1060	spinlock_t nid_list_lock;	/* protect nid lists ops */
1061	struct mutex build_lock;	/* lock for build free nids */
1062	unsigned char **free_nid_bitmap;
1063	unsigned char *nat_block_bitmap;
1064	unsigned short *free_nid_count;	/* free nid count of NAT block */
1065
1066	/* for checkpoint */
1067	char *nat_bitmap;		/* NAT bitmap pointer */
1068
1069	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
1070	unsigned char *nat_bits;	/* NAT bits blocks */
1071	unsigned char *full_nat_bits;	/* full NAT pages */
1072	unsigned char *empty_nat_bits;	/* empty NAT pages */
1073#ifdef CONFIG_F2FS_CHECK_FS
1074	char *nat_bitmap_mir;		/* NAT bitmap mirror */
1075#endif
1076	int bitmap_size;		/* bitmap size */
1077};
1078
1079/*
1080 * this structure is used as one of function parameters.
1081 * all the information are dedicated to a given direct node block determined
1082 * by the data offset in a file.
1083 */
1084struct dnode_of_data {
1085	struct inode *inode;		/* vfs inode pointer */
1086	struct folio *inode_folio;	/* its inode folio, NULL is possible */
1087	struct folio *node_folio;	/* cached direct node folio */
1088	nid_t nid;			/* node id of the direct node block */
1089	unsigned int ofs_in_node;	/* data offset in the node page */
1090	bool inode_folio_locked;	/* inode folio is locked or not */
1091	bool node_changed;		/* is node block changed */
1092	char cur_level;			/* level of hole node page */
1093	char max_level;			/* level of current page located */
1094	block_t	data_blkaddr;		/* block address of the node block */
1095};
1096
1097static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
1098		struct folio *ifolio, struct folio *nfolio, nid_t nid)
1099{
1100	memset(dn, 0, sizeof(*dn));
1101	dn->inode = inode;
1102	dn->inode_folio = ifolio;
1103	dn->node_folio = nfolio;
1104	dn->nid = nid;
1105}
1106
1107/*
1108 * For SIT manager
1109 *
1110 * By default, there are 6 active log areas across the whole main area.
1111 * When considering hot and cold data separation to reduce cleaning overhead,
1112 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
1113 * respectively.
1114 * In the current design, you should not change the numbers intentionally.
1115 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
1116 * logs individually according to the underlying devices. (default: 6)
1117 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
1118 * data and 8 for node logs.
1119 */
1120#define	NR_CURSEG_DATA_TYPE	(3)
1121#define NR_CURSEG_NODE_TYPE	(3)
1122#define NR_CURSEG_INMEM_TYPE	(2)
1123#define NR_CURSEG_RO_TYPE	(2)
1124#define NR_CURSEG_PERSIST_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
1125#define NR_CURSEG_TYPE		(NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE)
1126
1127enum log_type {
1128	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
1129	CURSEG_WARM_DATA,	/* data blocks */
1130	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
1131	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
1132	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
1133	CURSEG_COLD_NODE,	/* indirect node blocks */
1134	NR_PERSISTENT_LOG,	/* number of persistent log */
1135	CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG,
1136				/* pinned file that needs consecutive block address */
1137	CURSEG_ALL_DATA_ATGC,	/* SSR alloctor in hot/warm/cold data area */
1138	NO_CHECK_TYPE,		/* number of persistent & inmem log */
1139};
1140
1141struct flush_cmd {
1142	struct completion wait;
1143	struct llist_node llnode;
1144	nid_t ino;
1145	int ret;
1146};
1147
1148struct flush_cmd_control {
1149	struct task_struct *f2fs_issue_flush;	/* flush thread */
1150	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
1151	atomic_t issued_flush;			/* # of issued flushes */
1152	atomic_t queued_flush;			/* # of queued flushes */
1153	struct llist_head issue_list;		/* list for command issue */
1154	struct llist_node *dispatch_list;	/* list for command dispatch */
1155};
1156
1157struct f2fs_sm_info {
1158	struct sit_info *sit_info;		/* whole segment information */
1159	struct free_segmap_info *free_info;	/* free segment information */
1160	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
1161	struct curseg_info *curseg_array;	/* active segment information */
1162
1163	struct f2fs_rwsem curseg_lock;	/* for preventing curseg change */
1164
1165	block_t seg0_blkaddr;		/* block address of 0'th segment */
1166	block_t main_blkaddr;		/* start block address of main area */
1167	block_t ssa_blkaddr;		/* start block address of SSA area */
1168
1169	unsigned int segment_count;	/* total # of segments */
1170	unsigned int main_segments;	/* # of segments in main area */
1171	unsigned int reserved_segments;	/* # of reserved segments */
1172	unsigned int ovp_segments;	/* # of overprovision segments */
1173
1174	/* a threshold to reclaim prefree segments */
1175	unsigned int rec_prefree_segments;
1176
1177	struct list_head sit_entry_set;	/* sit entry set list */
1178
1179	unsigned int ipu_policy;	/* in-place-update policy */
1180	unsigned int min_ipu_util;	/* in-place-update threshold */
1181	unsigned int min_fsync_blocks;	/* threshold for fsync */
1182	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
1183	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
1184	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
1185
1186	/* for flush command control */
1187	struct flush_cmd_control *fcc_info;
1188
1189	/* for discard command control */
1190	struct discard_cmd_control *dcc_info;
1191};
1192
1193/*
1194 * For superblock
1195 */
1196/*
1197 * COUNT_TYPE for monitoring
1198 *
1199 * f2fs monitors the number of several block types such as on-writeback,
1200 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
1201 */
1202#define WB_DATA_TYPE(folio, f)			\
1203	(f || f2fs_is_cp_guaranteed(folio) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
1204enum count_type {
1205	F2FS_DIRTY_DENTS,
1206	F2FS_DIRTY_DATA,
1207	F2FS_DIRTY_QDATA,
1208	F2FS_DIRTY_NODES,
1209	F2FS_DIRTY_META,
1210	F2FS_DIRTY_IMETA,
1211	F2FS_WB_CP_DATA,
1212	F2FS_WB_DATA,
1213	F2FS_RD_DATA,
1214	F2FS_RD_NODE,
1215	F2FS_RD_META,
1216	F2FS_DIO_WRITE,
1217	F2FS_DIO_READ,
1218	NR_COUNT_TYPE,
1219};
1220
1221/*
1222 * The below are the page types of bios used in submit_bio().
1223 * The available types are:
1224 * DATA			User data pages. It operates as async mode.
1225 * NODE			Node pages. It operates as async mode.
1226 * META			FS metadata pages such as SIT, NAT, CP.
1227 * NR_PAGE_TYPE		The number of page types.
1228 * META_FLUSH		Make sure the previous pages are written
1229 *			with waiting the bio's completion
1230 * ...			Only can be used with META.
1231 */
1232#define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
1233#define PAGE_TYPE_ON_MAIN(type)	((type) == DATA || (type) == NODE)
1234enum page_type {
1235	DATA = 0,
1236	NODE = 1,	/* should not change this */
1237	META,
1238	NR_PAGE_TYPE,
1239	META_FLUSH,
1240	IPU,		/* the below types are used by tracepoints only. */
1241	OPU,
1242};
1243
1244enum temp_type {
1245	HOT = 0,	/* must be zero for meta bio */
1246	WARM,
1247	COLD,
1248	NR_TEMP_TYPE,
1249};
1250
1251enum need_lock_type {
1252	LOCK_REQ = 0,
1253	LOCK_DONE,
1254	LOCK_RETRY,
1255};
1256
1257enum cp_reason_type {
1258	CP_NO_NEEDED,
1259	CP_NON_REGULAR,
1260	CP_COMPRESSED,
1261	CP_HARDLINK,
1262	CP_SB_NEED_CP,
1263	CP_WRONG_PINO,
1264	CP_NO_SPC_ROLL,
1265	CP_NODE_NEED_CP,
1266	CP_FASTBOOT_MODE,
1267	CP_SPEC_LOG_NUM,
1268	CP_RECOVER_DIR,
1269	CP_XATTR_DIR,
1270};
1271
1272enum iostat_type {
1273	/* WRITE IO */
1274	APP_DIRECT_IO,			/* app direct write IOs */
1275	APP_BUFFERED_IO,		/* app buffered write IOs */
1276	APP_WRITE_IO,			/* app write IOs */
1277	APP_MAPPED_IO,			/* app mapped IOs */
1278	APP_BUFFERED_CDATA_IO,		/* app buffered write IOs on compressed file */
1279	APP_MAPPED_CDATA_IO,		/* app mapped write IOs on compressed file */
1280	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
1281	FS_CDATA_IO,			/* data IOs from kworker/fsync/reclaimer on compressed file */
1282	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1283	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1284	FS_GC_DATA_IO,			/* data IOs from forground gc */
1285	FS_GC_NODE_IO,			/* node IOs from forground gc */
1286	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1287	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1288	FS_CP_META_IO,			/* meta IOs from checkpoint */
1289
1290	/* READ IO */
1291	APP_DIRECT_READ_IO,		/* app direct read IOs */
1292	APP_BUFFERED_READ_IO,		/* app buffered read IOs */
1293	APP_READ_IO,			/* app read IOs */
1294	APP_MAPPED_READ_IO,		/* app mapped read IOs */
1295	APP_BUFFERED_CDATA_READ_IO,	/* app buffered read IOs on compressed file  */
1296	APP_MAPPED_CDATA_READ_IO,	/* app mapped read IOs on compressed file  */
1297	FS_DATA_READ_IO,		/* data read IOs */
1298	FS_GDATA_READ_IO,		/* data read IOs from background gc */
1299	FS_CDATA_READ_IO,		/* compressed data read IOs */
1300	FS_NODE_READ_IO,		/* node read IOs */
1301	FS_META_READ_IO,		/* meta read IOs */
1302
1303	/* other */
1304	FS_DISCARD_IO,			/* discard */
1305	FS_FLUSH_IO,			/* flush */
1306	FS_ZONE_RESET_IO,		/* zone reset */
1307	NR_IO_TYPE,
1308};
1309
1310struct f2fs_io_info {
1311	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
1312	nid_t ino;		/* inode number */
1313	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1314	enum temp_type temp;	/* contains HOT/WARM/COLD */
1315	enum req_op op;		/* contains REQ_OP_ */
1316	blk_opf_t op_flags;	/* req_flag_bits */
1317	block_t new_blkaddr;	/* new block address to be written */
1318	block_t old_blkaddr;	/* old block address before Cow */
1319	union {
1320		struct page *page;	/* page to be written */
1321		struct folio *folio;
1322	};
1323	struct page *encrypted_page;	/* encrypted page */
1324	struct page *compressed_page;	/* compressed page */
1325	struct list_head list;		/* serialize IOs */
1326	unsigned int compr_blocks;	/* # of compressed block addresses */
1327	unsigned int need_lock:8;	/* indicate we need to lock cp_rwsem */
1328	unsigned int version:8;		/* version of the node */
1329	unsigned int submitted:1;	/* indicate IO submission */
1330	unsigned int in_list:1;		/* indicate fio is in io_list */
1331	unsigned int is_por:1;		/* indicate IO is from recovery or not */
1332	unsigned int encrypted:1;	/* indicate file is encrypted */
1333	unsigned int meta_gc:1;		/* require meta inode GC */
1334	enum iostat_type io_type;	/* io type */
1335	struct writeback_control *io_wbc; /* writeback control */
1336	struct bio **bio;		/* bio for ipu */
1337	sector_t *last_block;		/* last block number in bio */
1338};
1339
1340struct bio_entry {
1341	struct bio *bio;
1342	struct list_head list;
1343};
1344
1345#define is_read_io(rw) ((rw) == READ)
1346struct f2fs_bio_info {
1347	struct f2fs_sb_info *sbi;	/* f2fs superblock */
1348	struct bio *bio;		/* bios to merge */
1349	sector_t last_block_in_bio;	/* last block number */
1350	struct f2fs_io_info fio;	/* store buffered io info. */
1351#ifdef CONFIG_BLK_DEV_ZONED
1352	struct completion zone_wait;	/* condition value for the previous open zone to close */
1353	struct bio *zone_pending_bio;	/* pending bio for the previous zone */
1354	void *bi_private;		/* previous bi_private for pending bio */
1355#endif
1356	struct f2fs_rwsem io_rwsem;	/* blocking op for bio */
1357	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1358	struct list_head io_list;	/* track fios */
1359	struct list_head bio_list;	/* bio entry list head */
1360	struct f2fs_rwsem bio_list_lock;	/* lock to protect bio entry list */
1361};
1362
1363#define FDEV(i)				(sbi->devs[i])
1364#define RDEV(i)				(raw_super->devs[i])
1365struct f2fs_dev_info {
1366	struct file *bdev_file;
1367	struct block_device *bdev;
1368	char path[MAX_PATH_LEN + 1];
1369	unsigned int total_segments;
1370	block_t start_blk;
1371	block_t end_blk;
1372#ifdef CONFIG_BLK_DEV_ZONED
1373	unsigned int nr_blkz;		/* Total number of zones */
1374	unsigned long *blkz_seq;	/* Bitmap indicating sequential zones */
1375#endif
1376};
1377
1378enum inode_type {
1379	DIR_INODE,			/* for dirty dir inode */
1380	FILE_INODE,			/* for dirty regular/symlink inode */
1381	DIRTY_META,			/* for all dirtied inode metadata */
1382	DONATE_INODE,			/* for all inode to donate pages */
1383	NR_INODE_TYPE,
1384};
1385
1386/* for inner inode cache management */
1387struct inode_management {
1388	struct radix_tree_root ino_root;	/* ino entry array */
1389	spinlock_t ino_lock;			/* for ino entry lock */
1390	struct list_head ino_list;		/* inode list head */
1391	unsigned long ino_num;			/* number of entries */
1392};
1393
1394/* for GC_AT */
1395struct atgc_management {
1396	bool atgc_enabled;			/* ATGC is enabled or not */
1397	struct rb_root_cached root;		/* root of victim rb-tree */
1398	struct list_head victim_list;		/* linked with all victim entries */
1399	unsigned int victim_count;		/* victim count in rb-tree */
1400	unsigned int candidate_ratio;		/* candidate ratio */
1401	unsigned int max_candidate_count;	/* max candidate count */
1402	unsigned int age_weight;		/* age weight, vblock_weight = 100 - age_weight */
1403	unsigned long long age_threshold;	/* age threshold */
1404};
1405
1406struct f2fs_gc_control {
1407	unsigned int victim_segno;	/* target victim segment number */
1408	int init_gc_type;		/* FG_GC or BG_GC */
1409	bool no_bg_gc;			/* check the space and stop bg_gc */
1410	bool should_migrate_blocks;	/* should migrate blocks */
1411	bool err_gc_skipped;		/* return EAGAIN if GC skipped */
1412	bool one_time;			/* require one time GC in one migration unit */
1413	unsigned int nr_free_secs;	/* # of free sections to do GC */
1414};
1415
1416/*
1417 * For s_flag in struct f2fs_sb_info
1418 * Modification on enum should be synchronized with s_flag array
1419 */
1420enum {
1421	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1422	SBI_IS_CLOSE,				/* specify unmounting */
1423	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1424	SBI_POR_DOING,				/* recovery is doing or not */
1425	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1426	SBI_NEED_CP,				/* need to checkpoint */
1427	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
1428	SBI_IS_RECOVERED,			/* recovered orphan/data */
1429	SBI_CP_DISABLED,			/* CP was disabled last mount */
1430	SBI_CP_DISABLED_QUICK,			/* CP was disabled quickly */
1431	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1432	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1433	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
1434	SBI_IS_RESIZEFS,			/* resizefs is in process */
1435	SBI_IS_FREEZING,			/* freezefs is in process */
1436	SBI_IS_WRITABLE,			/* remove ro mountoption transiently */
1437	MAX_SBI_FLAG,
1438};
1439
1440enum {
1441	CP_TIME,
1442	REQ_TIME,
1443	DISCARD_TIME,
1444	GC_TIME,
1445	DISABLE_TIME,
1446	ENABLE_TIME,
1447	UMOUNT_DISCARD_TIMEOUT,
1448	MAX_TIME,
1449};
1450
1451/* Note that you need to keep synchronization with this gc_mode_names array */
1452enum {
1453	GC_NORMAL,
1454	GC_IDLE_CB,
1455	GC_IDLE_GREEDY,
1456	GC_IDLE_AT,
1457	GC_URGENT_HIGH,
1458	GC_URGENT_LOW,
1459	GC_URGENT_MID,
1460	MAX_GC_MODE,
1461};
1462
1463enum {
1464	BGGC_MODE_ON,		/* background gc is on */
1465	BGGC_MODE_OFF,		/* background gc is off */
1466	BGGC_MODE_SYNC,		/*
1467				 * background gc is on, migrating blocks
1468				 * like foreground gc
1469				 */
1470};
1471
1472enum {
1473	FS_MODE_ADAPTIVE,		/* use both lfs/ssr allocation */
1474	FS_MODE_LFS,			/* use lfs allocation only */
1475	FS_MODE_FRAGMENT_SEG,		/* segment fragmentation mode */
1476	FS_MODE_FRAGMENT_BLK,		/* block fragmentation mode */
1477};
1478
1479enum {
1480	ALLOC_MODE_DEFAULT,	/* stay default */
1481	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
1482};
1483
1484enum fsync_mode {
1485	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
1486	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1487	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
1488};
1489
1490enum {
1491	COMPR_MODE_FS,		/*
1492				 * automatically compress compression
1493				 * enabled files
1494				 */
1495	COMPR_MODE_USER,	/*
1496				 * automatical compression is disabled.
1497				 * user can control the file compression
1498				 * using ioctls
1499				 */
1500};
1501
1502enum {
1503	DISCARD_UNIT_BLOCK,	/* basic discard unit is block */
1504	DISCARD_UNIT_SEGMENT,	/* basic discard unit is segment */
1505	DISCARD_UNIT_SECTION,	/* basic discard unit is section */
1506};
1507
1508enum {
1509	MEMORY_MODE_NORMAL,	/* memory mode for normal devices */
1510	MEMORY_MODE_LOW,	/* memory mode for low memory devices */
1511};
1512
1513enum errors_option {
1514	MOUNT_ERRORS_READONLY,	/* remount fs ro on errors */
1515	MOUNT_ERRORS_CONTINUE,	/* continue on errors */
1516	MOUNT_ERRORS_PANIC,	/* panic on errors */
1517};
1518
1519enum {
1520	BACKGROUND,
1521	FOREGROUND,
1522	MAX_CALL_TYPE,
1523	TOTAL_CALL = FOREGROUND,
1524};
1525
1526enum f2fs_lookup_mode {
1527	LOOKUP_PERF,
1528	LOOKUP_COMPAT,
1529	LOOKUP_AUTO,
1530};
1531
1532/* For node type in __get_node_folio() */
1533enum node_type {
1534	NODE_TYPE_REGULAR,
1535	NODE_TYPE_INODE,
1536	NODE_TYPE_XATTR,
1537	NODE_TYPE_NON_INODE,
1538};
1539
1540
1541static inline int f2fs_test_bit(unsigned int nr, char *addr);
1542static inline void f2fs_set_bit(unsigned int nr, char *addr);
1543static inline void f2fs_clear_bit(unsigned int nr, char *addr);
1544
1545/*
1546 * Layout of f2fs page.private:
1547 *
1548 * Layout A: lowest bit should be 1
1549 * | bit0 = 1 | bit1 | bit2 | ... | bit MAX | private data .... |
1550 * bit 0	PAGE_PRIVATE_NOT_POINTER
1551 * bit 1	PAGE_PRIVATE_ONGOING_MIGRATION
1552 * bit 2	PAGE_PRIVATE_INLINE_INODE
1553 * bit 3	PAGE_PRIVATE_REF_RESOURCE
1554 * bit 4	PAGE_PRIVATE_ATOMIC_WRITE
1555 * bit 5-	f2fs private data
1556 *
1557 * Layout B: lowest bit should be 0
1558 * page.private is a wrapped pointer.
1559 */
1560enum {
1561	PAGE_PRIVATE_NOT_POINTER,		/* private contains non-pointer data */
1562	PAGE_PRIVATE_ONGOING_MIGRATION,		/* data page which is on-going migrating */
1563	PAGE_PRIVATE_INLINE_INODE,		/* inode page contains inline data */
1564	PAGE_PRIVATE_REF_RESOURCE,		/* dirty page has referenced resources */
1565	PAGE_PRIVATE_ATOMIC_WRITE,		/* data page from atomic write path */
1566	PAGE_PRIVATE_MAX
1567};
1568
1569/* For compression */
1570enum compress_algorithm_type {
1571	COMPRESS_LZO,
1572	COMPRESS_LZ4,
1573	COMPRESS_ZSTD,
1574	COMPRESS_LZORLE,
1575	COMPRESS_MAX,
1576};
1577
1578enum compress_flag {
1579	COMPRESS_CHKSUM,
1580	COMPRESS_MAX_FLAG,
1581};
1582
1583#define	COMPRESS_WATERMARK			20
1584#define	COMPRESS_PERCENT			20
1585
1586#define COMPRESS_DATA_RESERVED_SIZE		4
1587struct compress_data {
1588	__le32 clen;			/* compressed data size */
1589	__le32 chksum;			/* compressed data checksum */
1590	__le32 reserved[COMPRESS_DATA_RESERVED_SIZE];	/* reserved */
1591	u8 cdata[];			/* compressed data */
1592};
1593
1594#define COMPRESS_HEADER_SIZE	(sizeof(struct compress_data))
1595
1596#define F2FS_COMPRESSED_PAGE_MAGIC	0xF5F2C000
1597
1598#define F2FS_ZSTD_DEFAULT_CLEVEL	1
1599
1600#define	COMPRESS_LEVEL_OFFSET	8
1601
1602/* compress context */
1603struct compress_ctx {
1604	struct inode *inode;		/* inode the context belong to */
1605	pgoff_t cluster_idx;		/* cluster index number */
1606	unsigned int cluster_size;	/* page count in cluster */
1607	unsigned int log_cluster_size;	/* log of cluster size */
1608	struct page **rpages;		/* pages store raw data in cluster */
1609	unsigned int nr_rpages;		/* total page number in rpages */
1610	struct page **cpages;		/* pages store compressed data in cluster */
1611	unsigned int nr_cpages;		/* total page number in cpages */
1612	unsigned int valid_nr_cpages;	/* valid page number in cpages */
1613	void *rbuf;			/* virtual mapped address on rpages */
1614	struct compress_data *cbuf;	/* virtual mapped address on cpages */
1615	size_t rlen;			/* valid data length in rbuf */
1616	size_t clen;			/* valid data length in cbuf */
1617	void *private;			/* payload buffer for specified compression algorithm */
1618	void *private2;			/* extra payload buffer */
1619};
1620
1621/* compress context for write IO path */
1622struct compress_io_ctx {
1623	u32 magic;			/* magic number to indicate page is compressed */
1624	struct inode *inode;		/* inode the context belong to */
1625	struct page **rpages;		/* pages store raw data in cluster */
1626	unsigned int nr_rpages;		/* total page number in rpages */
1627	atomic_t pending_pages;		/* in-flight compressed page count */
1628};
1629
1630/* Context for decompressing one cluster on the read IO path */
1631struct decompress_io_ctx {
1632	u32 magic;			/* magic number to indicate page is compressed */
1633	struct inode *inode;		/* inode the context belong to */
1634	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
1635	pgoff_t cluster_idx;		/* cluster index number */
1636	unsigned int cluster_size;	/* page count in cluster */
1637	unsigned int log_cluster_size;	/* log of cluster size */
1638	struct page **rpages;		/* pages store raw data in cluster */
1639	unsigned int nr_rpages;		/* total page number in rpages */
1640	struct page **cpages;		/* pages store compressed data in cluster */
1641	unsigned int nr_cpages;		/* total page number in cpages */
1642	struct page **tpages;		/* temp pages to pad holes in cluster */
1643	void *rbuf;			/* virtual mapped address on rpages */
1644	struct compress_data *cbuf;	/* virtual mapped address on cpages */
1645	size_t rlen;			/* valid data length in rbuf */
1646	size_t clen;			/* valid data length in cbuf */
1647
1648	/*
1649	 * The number of compressed pages remaining to be read in this cluster.
1650	 * This is initially nr_cpages.  It is decremented by 1 each time a page
1651	 * has been read (or failed to be read).  When it reaches 0, the cluster
1652	 * is decompressed (or an error is reported).
1653	 *
1654	 * If an error occurs before all the pages have been submitted for I/O,
1655	 * then this will never reach 0.  In this case the I/O submitter is
1656	 * responsible for calling f2fs_decompress_end_io() instead.
1657	 */
1658	atomic_t remaining_pages;
1659
1660	/*
1661	 * Number of references to this decompress_io_ctx.
1662	 *
1663	 * One reference is held for I/O completion.  This reference is dropped
1664	 * after the pagecache pages are updated and unlocked -- either after
1665	 * decompression (and verity if enabled), or after an error.
1666	 *
1667	 * In addition, each compressed page holds a reference while it is in a
1668	 * bio.  These references are necessary prevent compressed pages from
1669	 * being freed while they are still in a bio.
1670	 */
1671	refcount_t refcnt;
1672
1673	bool failed;			/* IO error occurred before decompression? */
1674	bool need_verity;		/* need fs-verity verification after decompression? */
1675	unsigned char compress_algorithm;	/* backup algorithm type */
1676	void *private;			/* payload buffer for specified decompression algorithm */
1677	void *private2;			/* extra payload buffer */
1678	struct work_struct verity_work;	/* work to verify the decompressed pages */
1679	struct work_struct free_work;	/* work for late free this structure itself */
1680};
1681
1682#define NULL_CLUSTER			((unsigned int)(~0))
1683#define MIN_COMPRESS_LOG_SIZE		2
1684#define MAX_COMPRESS_LOG_SIZE		8
1685#define MAX_COMPRESS_WINDOW_SIZE(log_size)	((PAGE_SIZE) << (log_size))
1686
1687struct f2fs_sb_info {
1688	struct super_block *sb;			/* pointer to VFS super block */
1689	struct proc_dir_entry *s_proc;		/* proc entry */
1690	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1691	struct f2fs_rwsem sb_lock;		/* lock for raw super block */
1692	int valid_super_block;			/* valid super block no */
1693	unsigned long s_flag;				/* flags for sbi */
1694	struct mutex writepages;		/* mutex for writepages() */
1695
1696#ifdef CONFIG_BLK_DEV_ZONED
1697	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1698	unsigned int unusable_blocks_per_sec;   /* unusable blocks per section */
1699	unsigned int max_open_zones;		/* max open zone resources of the zoned device */
1700	/* For adjust the priority writing position of data in zone UFS */
1701	unsigned int blkzone_alloc_policy;
1702#endif
1703
1704	/* for node-related operations */
1705	struct f2fs_nm_info *nm_info;		/* node manager */
1706	struct inode *node_inode;		/* cache node blocks */
1707
1708	/* for segment-related operations */
1709	struct f2fs_sm_info *sm_info;		/* segment manager */
1710
1711	/* for bio operations */
1712	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1713	/* keep migration IO order for LFS mode */
1714	struct f2fs_rwsem io_order_lock;
1715	pgoff_t page_eio_ofs[NR_PAGE_TYPE];	/* EIO page offset */
1716	int page_eio_cnt[NR_PAGE_TYPE];		/* EIO count */
1717
1718	/* for checkpoint */
1719	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
1720	int cur_cp_pack;			/* remain current cp pack */
1721	spinlock_t cp_lock;			/* for flag in ckpt */
1722	struct inode *meta_inode;		/* cache meta blocks */
1723	struct f2fs_rwsem cp_global_sem;	/* checkpoint procedure lock */
1724	struct f2fs_rwsem cp_rwsem;		/* blocking FS operations */
1725	struct f2fs_rwsem node_write;		/* locking node writes */
1726	struct f2fs_rwsem node_change;	/* locking node change */
1727	wait_queue_head_t cp_wait;
1728	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
1729	long interval_time[MAX_TIME];		/* to store thresholds */
1730	struct ckpt_req_control cprc_info;	/* for checkpoint request control */
1731	struct cp_stats cp_stats;		/* for time stat of checkpoint */
1732
1733	struct inode_management im[MAX_INO_ENTRY];	/* manage inode cache */
1734
1735	spinlock_t fsync_node_lock;		/* for node entry lock */
1736	struct list_head fsync_node_list;	/* node list head */
1737	unsigned int fsync_seg_id;		/* sequence id */
1738	unsigned int fsync_node_num;		/* number of node entries */
1739
1740	/* for orphan inode, use 0'th array */
1741	unsigned int max_orphans;		/* max orphan inodes */
1742
1743	/* for inode management */
1744	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1745	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
1746	struct mutex flush_lock;		/* for flush exclusion */
1747
1748	/* for extent tree cache */
1749	struct extent_tree_info extent_tree[NR_EXTENT_CACHES];
1750	atomic64_t allocated_data_blocks;	/* for block age extent_cache */
1751	unsigned int max_read_extent_count;	/* max read extent count per inode */
1752
1753	/* The threshold used for hot and warm data seperation*/
1754	unsigned int hot_data_age_threshold;
1755	unsigned int warm_data_age_threshold;
1756	unsigned int last_age_weight;
1757
1758	/* control donate caches */
1759	unsigned int donate_files;
1760
1761	/* basic filesystem units */
1762	unsigned int log_sectors_per_block;	/* log2 sectors per block */
1763	unsigned int log_blocksize;		/* log2 block size */
1764	unsigned int blocksize;			/* block size */
1765	unsigned int root_ino_num;		/* root inode number*/
1766	unsigned int node_ino_num;		/* node inode number*/
1767	unsigned int meta_ino_num;		/* meta inode number*/
1768	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
1769	unsigned int blocks_per_seg;		/* blocks per segment */
1770	unsigned int segs_per_sec;		/* segments per section */
1771	unsigned int secs_per_zone;		/* sections per zone */
1772	unsigned int total_sections;		/* total section count */
1773	unsigned int total_node_count;		/* total node block count */
1774	unsigned int total_valid_node_count;	/* valid node block count */
1775	int dir_level;				/* directory level */
1776	bool readdir_ra;			/* readahead inode in readdir */
1777	u64 max_io_bytes;			/* max io bytes to merge IOs */
1778
1779	/* variable summary block units */
1780	unsigned int sum_blocksize;		/* sum block size */
1781	unsigned int sums_per_block;		/* sum block count per block */
1782	unsigned int entries_in_sum;		/* entry count in sum block */
1783	unsigned int sum_entry_size;		/* total entry size in sum block */
1784	unsigned int sum_journal_size;		/* journal size in sum block */
1785	unsigned int nat_journal_entries;	/* nat journal entry count in the journal */
1786	unsigned int sit_journal_entries;	/* sit journal entry count in the journal */
1787
1788	block_t user_block_count;		/* # of user blocks */
1789	block_t total_valid_block_count;	/* # of valid blocks */
1790	block_t discard_blks;			/* discard command candidats */
1791	block_t last_valid_block_count;		/* for recovery */
1792	block_t reserved_blocks;		/* configurable reserved blocks */
1793	block_t current_reserved_blocks;	/* current reserved blocks */
1794
1795	/* Additional tracking for no checkpoint mode */
1796	block_t unusable_block_count;		/* # of blocks saved by last cp */
1797
1798	unsigned int nquota_files;		/* # of quota sysfile */
1799	struct f2fs_rwsem quota_sem;		/* blocking cp for flags */
1800	struct task_struct *umount_lock_holder;	/* s_umount lock holder */
1801
1802	/* # of pages, see count_type */
1803	atomic_t nr_pages[NR_COUNT_TYPE];
1804	/* # of allocated blocks */
1805	struct percpu_counter alloc_valid_block_count;
1806	/* # of node block writes as roll forward recovery */
1807	struct percpu_counter rf_node_block_count;
1808
1809	/* writeback control */
1810	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1811
1812	/* valid inode count */
1813	struct percpu_counter total_valid_inode_count;
1814
1815	struct f2fs_mount_info mount_opt;	/* mount options */
1816
1817	/* for cleaning operations */
1818	struct f2fs_rwsem gc_lock;		/*
1819						 * semaphore for GC, avoid
1820						 * race between GC and GC or CP
1821						 */
1822	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
1823	struct atgc_management am;		/* atgc management */
1824	unsigned int cur_victim_sec;		/* current victim section num */
1825	unsigned int gc_mode;			/* current GC state */
1826	unsigned int next_victim_seg[2];	/* next segment in victim section */
1827	spinlock_t gc_remaining_trials_lock;
1828	/* remaining trial count for GC_URGENT_* and GC_IDLE_* */
1829	unsigned int gc_remaining_trials;
1830
1831	/* for skip statistic */
1832	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
1833
1834	/* free sections reserved for pinned file */
1835	unsigned int reserved_pin_section;
1836
1837	/* threshold for gc trials on pinned files */
1838	unsigned short gc_pin_file_threshold;
1839	struct f2fs_rwsem pin_sem;
1840
1841	/* maximum # of trials to find a victim segment for SSR and GC */
1842	unsigned int max_victim_search;
1843	/* migration granularity of garbage collection, unit: segment */
1844	unsigned int migration_granularity;
1845	/* migration window granularity of garbage collection, unit: segment */
1846	unsigned int migration_window_granularity;
1847
1848	/*
1849	 * for stat information.
1850	 * one is for the LFS mode, and the other is for the SSR mode.
1851	 */
1852#ifdef CONFIG_F2FS_STAT_FS
1853	struct f2fs_stat_info *stat_info;	/* FS status information */
1854	atomic_t meta_count[META_MAX];		/* # of meta blocks */
1855	unsigned int segment_count[2];		/* # of allocated segments */
1856	unsigned int block_count[2];		/* # of allocated blocks */
1857	atomic_t inplace_count;		/* # of inplace update */
1858	/* # of lookup extent cache */
1859	atomic64_t total_hit_ext[NR_EXTENT_CACHES];
1860	/* # of hit rbtree extent node */
1861	atomic64_t read_hit_rbtree[NR_EXTENT_CACHES];
1862	/* # of hit cached extent node */
1863	atomic64_t read_hit_cached[NR_EXTENT_CACHES];
1864	/* # of hit largest extent node in read extent cache */
1865	atomic64_t read_hit_largest;
1866	atomic_t inline_xattr;			/* # of inline_xattr inodes */
1867	atomic_t inline_inode;			/* # of inline_data inodes */
1868	atomic_t inline_dir;			/* # of inline_dentry inodes */
1869	atomic_t compr_inode;			/* # of compressed inodes */
1870	atomic64_t compr_blocks;		/* # of compressed blocks */
1871	atomic_t swapfile_inode;		/* # of swapfile inodes */
1872	atomic_t atomic_files;			/* # of opened atomic file */
1873	atomic_t max_aw_cnt;			/* max # of atomic writes */
1874	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1875	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
1876	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
1877	atomic_t cp_call_count[MAX_CALL_TYPE];	/* # of cp call */
1878#endif
1879	spinlock_t stat_lock;			/* lock for stat operations */
1880
1881	/* to attach REQ_META|REQ_FUA flags */
1882	unsigned int data_io_flag;
1883	unsigned int node_io_flag;
1884
1885	/* For sysfs support */
1886	struct kobject s_kobj;			/* /sys/fs/f2fs/<devname> */
1887	struct completion s_kobj_unregister;
1888
1889	struct kobject s_stat_kobj;		/* /sys/fs/f2fs/<devname>/stat */
1890	struct completion s_stat_kobj_unregister;
1891
1892	struct kobject s_feature_list_kobj;		/* /sys/fs/f2fs/<devname>/feature_list */
1893	struct completion s_feature_list_kobj_unregister;
1894
1895	/* For shrinker support */
1896	struct list_head s_list;
1897	struct mutex umount_mutex;
1898	unsigned int shrinker_run_no;
1899
1900	/* For multi devices */
1901	int s_ndevs;				/* number of devices */
1902	struct f2fs_dev_info *devs;		/* for device list */
1903	unsigned int dirty_device;		/* for checkpoint data flush */
1904	spinlock_t dev_lock;			/* protect dirty_device */
1905	bool aligned_blksize;			/* all devices has the same logical blksize */
1906	unsigned int first_seq_zone_segno;	/* first segno in sequential zone */
1907	unsigned int bggc_io_aware;		/* For adjust the BG_GC priority when pending IO */
1908	unsigned int allocate_section_hint;	/* the boundary position between devices */
1909	unsigned int allocate_section_policy;	/* determine the section writing priority */
1910
1911	/* For write statistics */
1912	u64 sectors_written_start;
1913	u64 kbytes_written;
1914
1915	/* Precomputed FS UUID checksum for seeding other checksums */
1916	__u32 s_chksum_seed;
1917
1918	struct workqueue_struct *post_read_wq;	/* post read workqueue */
1919
1920	/*
1921	 * If we are in irq context, let's update error information into
1922	 * on-disk superblock in the work.
1923	 */
1924	struct work_struct s_error_work;
1925	unsigned char errors[MAX_F2FS_ERRORS];		/* error flags */
1926	unsigned char stop_reason[MAX_STOP_REASON];	/* stop reason */
1927	spinlock_t error_lock;			/* protect errors/stop_reason array */
1928	bool error_dirty;			/* errors of sb is dirty */
1929
1930	/* For reclaimed segs statistics per each GC mode */
1931	unsigned int gc_segment_mode;		/* GC state for reclaimed segments */
1932	unsigned int gc_reclaimed_segs[MAX_GC_MODE];	/* Reclaimed segs for each mode */
1933
1934	unsigned long seq_file_ra_mul;		/* multiplier for ra_pages of seq. files in fadvise */
1935
1936	int max_fragment_chunk;			/* max chunk size for block fragmentation mode */
1937	int max_fragment_hole;			/* max hole size for block fragmentation mode */
1938
1939	/* For atomic write statistics */
1940	atomic64_t current_atomic_write;
1941	s64 peak_atomic_write;
1942	u64 committed_atomic_block;
1943	u64 revoked_atomic_block;
1944
1945	/* carve out reserved_blocks from total blocks */
1946	bool carve_out;
1947
1948#ifdef CONFIG_F2FS_FS_COMPRESSION
1949	struct kmem_cache *page_array_slab;	/* page array entry */
1950	unsigned int page_array_slab_size;	/* default page array slab size */
1951
1952	/* For runtime compression statistics */
1953	u64 compr_written_block;
1954	u64 compr_saved_block;
1955	u32 compr_new_inode;
1956
1957	/* For compressed block cache */
1958	struct inode *compress_inode;		/* cache compressed blocks */
1959	unsigned int compress_percent;		/* cache page percentage */
1960	unsigned int compress_watermark;	/* cache page watermark */
1961	atomic_t compress_page_hit;		/* cache hit count */
1962#endif
1963
1964#ifdef CONFIG_F2FS_IOSTAT
1965	/* For app/fs IO statistics */
1966	spinlock_t iostat_lock;
1967	unsigned long long iostat_count[NR_IO_TYPE];
1968	unsigned long long iostat_bytes[NR_IO_TYPE];
1969	unsigned long long prev_iostat_bytes[NR_IO_TYPE];
1970	bool iostat_enable;
1971	unsigned long iostat_next_period;
1972	unsigned int iostat_period_ms;
1973
1974	/* For io latency related statistics info in one iostat period */
1975	spinlock_t iostat_lat_lock;
1976	struct iostat_lat_info *iostat_io_lat;
1977#endif
1978};
1979
1980/* Definitions to access f2fs_sb_info */
1981#define SEGS_TO_BLKS(sbi, segs)					\
1982		((segs) << (sbi)->log_blocks_per_seg)
1983#define BLKS_TO_SEGS(sbi, blks)					\
1984		((blks) >> (sbi)->log_blocks_per_seg)
1985
1986#define BLKS_PER_SEG(sbi)	((sbi)->blocks_per_seg)
1987#define BLKS_PER_SEC(sbi)	(SEGS_TO_BLKS(sbi, (sbi)->segs_per_sec))
1988#define SEGS_PER_SEC(sbi)	((sbi)->segs_per_sec)
1989
1990__printf(3, 4)
1991void f2fs_printk(struct f2fs_sb_info *sbi, bool limit_rate, const char *fmt, ...);
1992
1993#define f2fs_err(sbi, fmt, ...)						\
1994	f2fs_printk(sbi, false, KERN_ERR fmt, ##__VA_ARGS__)
1995#define f2fs_warn(sbi, fmt, ...)					\
1996	f2fs_printk(sbi, false, KERN_WARNING fmt, ##__VA_ARGS__)
1997#define f2fs_notice(sbi, fmt, ...)					\
1998	f2fs_printk(sbi, false, KERN_NOTICE fmt, ##__VA_ARGS__)
1999#define f2fs_info(sbi, fmt, ...)					\
2000	f2fs_printk(sbi, false, KERN_INFO fmt, ##__VA_ARGS__)
2001#define f2fs_debug(sbi, fmt, ...)					\
2002	f2fs_printk(sbi, false, KERN_DEBUG fmt, ##__VA_ARGS__)
2003
2004#define f2fs_err_ratelimited(sbi, fmt, ...)				\
2005	f2fs_printk(sbi, true, KERN_ERR fmt, ##__VA_ARGS__)
2006#define f2fs_warn_ratelimited(sbi, fmt, ...)				\
2007	f2fs_printk(sbi, true, KERN_WARNING fmt, ##__VA_ARGS__)
2008#define f2fs_info_ratelimited(sbi, fmt, ...)				\
2009	f2fs_printk(sbi, true, KERN_INFO fmt, ##__VA_ARGS__)
2010
2011#ifdef CONFIG_F2FS_FAULT_INJECTION
2012#define time_to_inject(sbi, type) __time_to_inject(sbi, type, __func__,	\
2013									__builtin_return_address(0))
2014static inline bool __time_to_inject(struct f2fs_sb_info *sbi, int type,
2015				const char *func, const char *parent_func)
2016{
2017	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
2018
2019	if (!ffi->inject_rate)
2020		return false;
2021
2022	if (!IS_FAULT_SET(ffi, type))
2023		return false;
2024
2025	atomic_inc(&ffi->inject_ops);
2026	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
2027		atomic_set(&ffi->inject_ops, 0);
2028		ffi->inject_count[type]++;
2029		f2fs_info_ratelimited(sbi, "inject %s in %s of %pS",
2030				f2fs_fault_name[type], func, parent_func);
2031		return true;
2032	}
2033	return false;
2034}
2035#else
2036static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
2037{
2038	return false;
2039}
2040#endif
2041
2042/*
2043 * Test if the mounted volume is a multi-device volume.
2044 *   - For a single regular disk volume, sbi->s_ndevs is 0.
2045 *   - For a single zoned disk volume, sbi->s_ndevs is 1.
2046 *   - For a multi-device volume, sbi->s_ndevs is always 2 or more.
2047 */
2048static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
2049{
2050	return sbi->s_ndevs > 1;
2051}
2052
2053static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
2054{
2055	unsigned long now = jiffies;
2056
2057	sbi->last_time[type] = now;
2058
2059	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
2060	if (type == REQ_TIME) {
2061		sbi->last_time[DISCARD_TIME] = now;
2062		sbi->last_time[GC_TIME] = now;
2063	}
2064}
2065
2066static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
2067{
2068	unsigned long interval = sbi->interval_time[type] * HZ;
2069
2070	return time_after(jiffies, sbi->last_time[type] + interval);
2071}
2072
2073static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
2074						int type)
2075{
2076	unsigned long interval = sbi->interval_time[type] * HZ;
2077	unsigned int wait_ms = 0;
2078	long delta;
2079
2080	delta = (sbi->last_time[type] + interval) - jiffies;
2081	if (delta > 0)
2082		wait_ms = jiffies_to_msecs(delta);
2083
2084	return wait_ms;
2085}
2086
2087/*
2088 * Inline functions
2089 */
2090static inline u32 __f2fs_crc32(u32 crc, const void *address,
2091			       unsigned int length)
2092{
2093	return crc32(crc, address, length);
2094}
2095
2096static inline u32 f2fs_crc32(const void *address, unsigned int length)
2097{
2098	return __f2fs_crc32(F2FS_SUPER_MAGIC, address, length);
2099}
2100
2101static inline u32 f2fs_chksum(u32 crc, const void *address, unsigned int length)
2102{
2103	return __f2fs_crc32(crc, address, length);
2104}
2105
2106static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
2107{
2108	return container_of(inode, struct f2fs_inode_info, vfs_inode);
2109}
2110
2111static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
2112{
2113	return sb->s_fs_info;
2114}
2115
2116static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
2117{
2118	return F2FS_SB(inode->i_sb);
2119}
2120
2121static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
2122{
2123	return F2FS_I_SB(mapping->host);
2124}
2125
2126static inline struct f2fs_sb_info *F2FS_F_SB(const struct folio *folio)
2127{
2128	return F2FS_M_SB(folio->mapping);
2129}
2130
2131static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
2132{
2133	return (struct f2fs_super_block *)(sbi->raw_super);
2134}
2135
2136static inline struct f2fs_super_block *F2FS_SUPER_BLOCK(struct folio *folio,
2137								pgoff_t index)
2138{
2139	pgoff_t idx_in_folio = index % folio_nr_pages(folio);
2140
2141	return (struct f2fs_super_block *)
2142		(page_address(folio_page(folio, idx_in_folio)) +
2143						F2FS_SUPER_OFFSET);
2144}
2145
2146static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
2147{
2148	return (struct f2fs_checkpoint *)(sbi->ckpt);
2149}
2150
2151static inline struct f2fs_node *F2FS_NODE(const struct folio *folio)
2152{
2153	return (struct f2fs_node *)folio_address(folio);
2154}
2155
2156static inline struct f2fs_inode *F2FS_INODE(const struct folio *folio)
2157{
2158	return &((struct f2fs_node *)folio_address(folio))->i;
2159}
2160
2161static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
2162{
2163	return (struct f2fs_nm_info *)(sbi->nm_info);
2164}
2165
2166static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
2167{
2168	return (struct f2fs_sm_info *)(sbi->sm_info);
2169}
2170
2171static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
2172{
2173	return (struct sit_info *)(SM_I(sbi)->sit_info);
2174}
2175
2176static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
2177{
2178	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
2179}
2180
2181static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
2182{
2183	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
2184}
2185
2186static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
2187{
2188	return sbi->meta_inode->i_mapping;
2189}
2190
2191static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
2192{
2193	return sbi->node_inode->i_mapping;
2194}
2195
2196static inline bool is_meta_folio(struct folio *folio)
2197{
2198	return folio->mapping == META_MAPPING(F2FS_F_SB(folio));
2199}
2200
2201static inline bool is_node_folio(struct folio *folio)
2202{
2203	return folio->mapping == NODE_MAPPING(F2FS_F_SB(folio));
2204}
2205
2206static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
2207{
2208	return test_bit(type, &sbi->s_flag);
2209}
2210
2211static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
2212{
2213	set_bit(type, &sbi->s_flag);
2214}
2215
2216static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
2217{
2218	clear_bit(type, &sbi->s_flag);
2219}
2220
2221static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
2222{
2223	return le64_to_cpu(cp->checkpoint_ver);
2224}
2225
2226static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
2227{
2228	if (type < F2FS_MAX_QUOTAS)
2229		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
2230	return 0;
2231}
2232
2233static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
2234{
2235	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
2236	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
2237}
2238
2239static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2240{
2241	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
2242
2243	return ckpt_flags & f;
2244}
2245
2246static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
2247{
2248	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
2249}
2250
2251static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2252{
2253	unsigned int ckpt_flags;
2254
2255	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
2256	ckpt_flags |= f;
2257	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
2258}
2259
2260static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
2261{
2262	unsigned long flags;
2263
2264	spin_lock_irqsave(&sbi->cp_lock, flags);
2265	__set_ckpt_flags(F2FS_CKPT(sbi), f);
2266	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2267}
2268
2269static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2270{
2271	unsigned int ckpt_flags;
2272
2273	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
2274	ckpt_flags &= (~f);
2275	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
2276}
2277
2278static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
2279{
2280	unsigned long flags;
2281
2282	spin_lock_irqsave(&sbi->cp_lock, flags);
2283	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
2284	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2285}
2286
2287#define init_f2fs_rwsem(sem)					\
2288do {								\
2289	static struct lock_class_key __key;			\
2290								\
2291	__init_f2fs_rwsem((sem), #sem, &__key);			\
2292} while (0)
2293
2294static inline void __init_f2fs_rwsem(struct f2fs_rwsem *sem,
2295		const char *sem_name, struct lock_class_key *key)
2296{
2297	__init_rwsem(&sem->internal_rwsem, sem_name, key);
2298#ifdef CONFIG_F2FS_UNFAIR_RWSEM
2299	init_waitqueue_head(&sem->read_waiters);
2300#endif
2301}
2302
2303static inline int f2fs_rwsem_is_locked(struct f2fs_rwsem *sem)
2304{
2305	return rwsem_is_locked(&sem->internal_rwsem);
2306}
2307
2308static inline int f2fs_rwsem_is_contended(struct f2fs_rwsem *sem)
2309{
2310	return rwsem_is_contended(&sem->internal_rwsem);
2311}
2312
2313static inline void f2fs_down_read(struct f2fs_rwsem *sem)
2314{
2315#ifdef CONFIG_F2FS_UNFAIR_RWSEM
2316	wait_event(sem->read_waiters, down_read_trylock(&sem->internal_rwsem));
2317#else
2318	down_read(&sem->internal_rwsem);
2319#endif
2320}
2321
2322static inline int f2fs_down_read_trylock(struct f2fs_rwsem *sem)
2323{
2324	return down_read_trylock(&sem->internal_rwsem);
2325}
2326
2327static inline void f2fs_up_read(struct f2fs_rwsem *sem)
2328{
2329	up_read(&sem->internal_rwsem);
2330}
2331
2332static inline void f2fs_down_write(struct f2fs_rwsem *sem)
2333{
2334	down_write(&sem->internal_rwsem);
2335}
2336
2337#ifdef CONFIG_DEBUG_LOCK_ALLOC
2338static inline void f2fs_down_read_nested(struct f2fs_rwsem *sem, int subclass)
2339{
2340	down_read_nested(&sem->internal_rwsem, subclass);
2341}
2342
2343static inline void f2fs_down_write_nested(struct f2fs_rwsem *sem, int subclass)
2344{
2345	down_write_nested(&sem->internal_rwsem, subclass);
2346}
2347#else
2348#define f2fs_down_read_nested(sem, subclass) f2fs_down_read(sem)
2349#define f2fs_down_write_nested(sem, subclass) f2fs_down_write(sem)
2350#endif
2351
2352static inline int f2fs_down_write_trylock(struct f2fs_rwsem *sem)
2353{
2354	return down_write_trylock(&sem->internal_rwsem);
2355}
2356
2357static inline void f2fs_up_write(struct f2fs_rwsem *sem)
2358{
2359	up_write(&sem->internal_rwsem);
2360#ifdef CONFIG_F2FS_UNFAIR_RWSEM
2361	wake_up_all(&sem->read_waiters);
2362#endif
2363}
2364
2365static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
2366{
2367	unsigned long flags;
2368	unsigned char *nat_bits;
2369
2370	/*
2371	 * In order to re-enable nat_bits we need to call fsck.f2fs by
2372	 * set_sbi_flag(sbi, SBI_NEED_FSCK). But it may give huge cost,
2373	 * so let's rely on regular fsck or unclean shutdown.
2374	 */
2375
2376	if (lock)
2377		spin_lock_irqsave(&sbi->cp_lock, flags);
2378	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
2379	nat_bits = NM_I(sbi)->nat_bits;
2380	NM_I(sbi)->nat_bits = NULL;
2381	if (lock)
2382		spin_unlock_irqrestore(&sbi->cp_lock, flags);
2383
2384	kvfree(nat_bits);
2385}
2386
2387static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
2388					struct cp_control *cpc)
2389{
2390	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
2391
2392	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
2393}
2394
2395static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
2396{
2397	f2fs_down_read(&sbi->cp_rwsem);
2398}
2399
2400static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
2401{
2402	if (time_to_inject(sbi, FAULT_LOCK_OP))
2403		return 0;
2404	return f2fs_down_read_trylock(&sbi->cp_rwsem);
2405}
2406
2407static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
2408{
2409	f2fs_up_read(&sbi->cp_rwsem);
2410}
2411
2412static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
2413{
2414	f2fs_down_write(&sbi->cp_rwsem);
2415}
2416
2417static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
2418{
2419	f2fs_up_write(&sbi->cp_rwsem);
2420}
2421
2422static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
2423{
2424	int reason = CP_SYNC;
2425
2426	if (test_opt(sbi, FASTBOOT))
2427		reason = CP_FASTBOOT;
2428	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
2429		reason = CP_UMOUNT;
2430	return reason;
2431}
2432
2433static inline bool __remain_node_summaries(int reason)
2434{
2435	return (reason & (CP_UMOUNT | CP_FASTBOOT));
2436}
2437
2438static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
2439{
2440	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
2441			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
2442}
2443
2444/*
2445 * Check whether the inode has blocks or not
2446 */
2447static inline int F2FS_HAS_BLOCKS(struct inode *inode)
2448{
2449	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
2450
2451	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
2452}
2453
2454static inline bool f2fs_has_xattr_block(unsigned int ofs)
2455{
2456	return ofs == XATTR_NODE_OFFSET;
2457}
2458
2459static inline bool __allow_reserved_root(struct f2fs_sb_info *sbi,
2460					struct inode *inode, bool cap)
2461{
2462	if (!inode)
2463		return true;
2464	if (IS_NOQUOTA(inode))
2465		return true;
2466	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
2467		return true;
2468	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
2469					in_group_p(F2FS_OPTION(sbi).s_resgid))
2470		return true;
2471	if (cap && capable(CAP_SYS_RESOURCE))
2472		return true;
2473	return false;
2474}
2475
2476static inline unsigned int get_available_block_count(struct f2fs_sb_info *sbi,
2477						struct inode *inode, bool cap)
2478{
2479	block_t avail_user_block_count;
2480
2481	avail_user_block_count = sbi->user_block_count -
2482					sbi->current_reserved_blocks;
2483
2484	if (test_opt(sbi, RESERVE_ROOT) && !__allow_reserved_root(sbi, inode, cap))
2485		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
2486
2487	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2488		if (avail_user_block_count > sbi->unusable_block_count)
2489			avail_user_block_count -= sbi->unusable_block_count;
2490		else
2491			avail_user_block_count = 0;
2492	}
2493
2494	return avail_user_block_count;
2495}
2496
2497static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
2498static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
2499				 struct inode *inode, blkcnt_t *count, bool partial)
2500{
2501	long long diff = 0, release = 0;
2502	block_t avail_user_block_count;
2503	int ret;
2504
2505	ret = dquot_reserve_block(inode, *count);
2506	if (ret)
2507		return ret;
2508
2509	if (time_to_inject(sbi, FAULT_BLOCK)) {
2510		release = *count;
2511		goto release_quota;
2512	}
2513
2514	/*
2515	 * let's increase this in prior to actual block count change in order
2516	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
2517	 */
2518	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
2519
2520	spin_lock(&sbi->stat_lock);
2521
2522	avail_user_block_count = get_available_block_count(sbi, inode, true);
2523	diff = (long long)sbi->total_valid_block_count + *count -
2524						avail_user_block_count;
2525	if (unlikely(diff > 0)) {
2526		if (!partial) {
2527			spin_unlock(&sbi->stat_lock);
2528			release = *count;
2529			goto enospc;
2530		}
2531		if (diff > *count)
2532			diff = *count;
2533		*count -= diff;
2534		release = diff;
2535		if (!*count) {
2536			spin_unlock(&sbi->stat_lock);
2537			goto enospc;
2538		}
2539	}
2540	sbi->total_valid_block_count += (block_t)(*count);
2541
2542	spin_unlock(&sbi->stat_lock);
2543
2544	if (unlikely(release)) {
2545		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
2546		dquot_release_reservation_block(inode, release);
2547	}
2548	f2fs_i_blocks_write(inode, *count, true, true);
2549	return 0;
2550
2551enospc:
2552	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
2553release_quota:
2554	dquot_release_reservation_block(inode, release);
2555	return -ENOSPC;
2556}
2557
2558#define PAGE_PRIVATE_GET_FUNC(name, flagname) \
2559static inline bool folio_test_f2fs_##name(const struct folio *folio)	\
2560{									\
2561	unsigned long priv = (unsigned long)folio->private;		\
2562	unsigned long v = (1UL << PAGE_PRIVATE_NOT_POINTER) |		\
2563			     (1UL << PAGE_PRIVATE_##flagname);		\
2564	return (priv & v) == v;						\
2565}									\
2566static inline bool page_private_##name(struct page *page) \
2567{ \
2568	return PagePrivate(page) && \
2569		test_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)) && \
2570		test_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
2571}
2572
2573#define PAGE_PRIVATE_SET_FUNC(name, flagname) \
2574static inline void folio_set_f2fs_##name(struct folio *folio)		\
2575{									\
2576	unsigned long v = (1UL << PAGE_PRIVATE_NOT_POINTER) |		\
2577			     (1UL << PAGE_PRIVATE_##flagname);		\
2578	if (!folio->private)						\
2579		folio_attach_private(folio, (void *)v);			\
2580	else {								\
2581		v |= (unsigned long)folio->private;			\
2582		folio->private = (void *)v;				\
2583	}								\
2584}									\
2585static inline void set_page_private_##name(struct page *page) \
2586{ \
2587	if (!PagePrivate(page)) \
2588		attach_page_private(page, (void *)0); \
2589	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); \
2590	set_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
2591}
2592
2593#define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \
2594static inline void folio_clear_f2fs_##name(struct folio *folio)		\
2595{									\
2596	unsigned long v = (unsigned long)folio->private;		\
2597									\
2598	v &= ~(1UL << PAGE_PRIVATE_##flagname);				\
2599	if (v == (1UL << PAGE_PRIVATE_NOT_POINTER))			\
2600		folio_detach_private(folio);				\
2601	else								\
2602		folio->private = (void *)v;				\
2603}									\
2604static inline void clear_page_private_##name(struct page *page) \
2605{ \
2606	clear_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
2607	if (page_private(page) == BIT(PAGE_PRIVATE_NOT_POINTER)) \
2608		detach_page_private(page); \
2609}
2610
2611PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER);
2612PAGE_PRIVATE_GET_FUNC(inline, INLINE_INODE);
2613PAGE_PRIVATE_GET_FUNC(gcing, ONGOING_MIGRATION);
2614PAGE_PRIVATE_GET_FUNC(atomic, ATOMIC_WRITE);
2615
2616PAGE_PRIVATE_SET_FUNC(reference, REF_RESOURCE);
2617PAGE_PRIVATE_SET_FUNC(inline, INLINE_INODE);
2618PAGE_PRIVATE_SET_FUNC(gcing, ONGOING_MIGRATION);
2619PAGE_PRIVATE_SET_FUNC(atomic, ATOMIC_WRITE);
2620
2621PAGE_PRIVATE_CLEAR_FUNC(reference, REF_RESOURCE);
2622PAGE_PRIVATE_CLEAR_FUNC(inline, INLINE_INODE);
2623PAGE_PRIVATE_CLEAR_FUNC(gcing, ONGOING_MIGRATION);
2624PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
2625
2626static inline unsigned long folio_get_f2fs_data(struct folio *folio)
2627{
2628	unsigned long data = (unsigned long)folio->private;
2629
2630	if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data))
2631		return 0;
2632	return data >> PAGE_PRIVATE_MAX;
2633}
2634
2635static inline void folio_set_f2fs_data(struct folio *folio, unsigned long data)
2636{
2637	data = (1UL << PAGE_PRIVATE_NOT_POINTER) | (data << PAGE_PRIVATE_MAX);
2638
2639	if (!folio_test_private(folio))
2640		folio_attach_private(folio, (void *)data);
2641	else
2642		folio->private = (void *)((unsigned long)folio->private | data);
2643}
2644
2645static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
2646						struct inode *inode,
2647						block_t count)
2648{
2649	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
2650
2651	spin_lock(&sbi->stat_lock);
2652	if (unlikely(sbi->total_valid_block_count < count)) {
2653		f2fs_warn(sbi, "Inconsistent total_valid_block_count:%u, ino:%lu, count:%u",
2654			  sbi->total_valid_block_count, inode->i_ino, count);
2655		sbi->total_valid_block_count = 0;
2656		set_sbi_flag(sbi, SBI_NEED_FSCK);
2657	} else {
2658		sbi->total_valid_block_count -= count;
2659	}
2660	if (sbi->reserved_blocks &&
2661		sbi->current_reserved_blocks < sbi->reserved_blocks)
2662		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
2663					sbi->current_reserved_blocks + count);
2664	spin_unlock(&sbi->stat_lock);
2665	if (unlikely(inode->i_blocks < sectors)) {
2666		f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
2667			  inode->i_ino,
2668			  (unsigned long long)inode->i_blocks,
2669			  (unsigned long long)sectors);
2670		set_sbi_flag(sbi, SBI_NEED_FSCK);
2671		return;
2672	}
2673	f2fs_i_blocks_write(inode, count, false, true);
2674}
2675
2676static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
2677{
2678	atomic_inc(&sbi->nr_pages[count_type]);
2679
2680	if (count_type == F2FS_DIRTY_DENTS ||
2681			count_type == F2FS_DIRTY_NODES ||
2682			count_type == F2FS_DIRTY_META ||
2683			count_type == F2FS_DIRTY_QDATA ||
2684			count_type == F2FS_DIRTY_IMETA)
2685		set_sbi_flag(sbi, SBI_IS_DIRTY);
2686}
2687
2688static inline void inode_inc_dirty_pages(struct inode *inode)
2689{
2690	atomic_inc(&F2FS_I(inode)->dirty_pages);
2691	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2692				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
2693	if (IS_NOQUOTA(inode))
2694		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
2695}
2696
2697static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
2698{
2699	atomic_dec(&sbi->nr_pages[count_type]);
2700}
2701
2702static inline void inode_dec_dirty_pages(struct inode *inode)
2703{
2704	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
2705			!S_ISLNK(inode->i_mode))
2706		return;
2707
2708	atomic_dec(&F2FS_I(inode)->dirty_pages);
2709	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2710				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
2711	if (IS_NOQUOTA(inode))
2712		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
2713}
2714
2715static inline void inc_atomic_write_cnt(struct inode *inode)
2716{
2717	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2718	struct f2fs_inode_info *fi = F2FS_I(inode);
2719	u64 current_write;
2720
2721	fi->atomic_write_cnt++;
2722	atomic64_inc(&sbi->current_atomic_write);
2723	current_write = atomic64_read(&sbi->current_atomic_write);
2724	if (current_write > sbi->peak_atomic_write)
2725		sbi->peak_atomic_write = current_write;
2726}
2727
2728static inline void release_atomic_write_cnt(struct inode *inode)
2729{
2730	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2731	struct f2fs_inode_info *fi = F2FS_I(inode);
2732
2733	atomic64_sub(fi->atomic_write_cnt, &sbi->current_atomic_write);
2734	fi->atomic_write_cnt = 0;
2735}
2736
2737static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
2738{
2739	return atomic_read(&sbi->nr_pages[count_type]);
2740}
2741
2742static inline int get_dirty_pages(struct inode *inode)
2743{
2744	return atomic_read(&F2FS_I(inode)->dirty_pages);
2745}
2746
2747static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
2748{
2749	return div_u64(get_pages(sbi, block_type) + BLKS_PER_SEC(sbi) - 1,
2750							BLKS_PER_SEC(sbi));
2751}
2752
2753static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
2754{
2755	return sbi->total_valid_block_count;
2756}
2757
2758static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
2759{
2760	return sbi->discard_blks;
2761}
2762
2763static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
2764{
2765	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2766
2767	/* return NAT or SIT bitmap */
2768	if (flag == NAT_BITMAP)
2769		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
2770	else if (flag == SIT_BITMAP)
2771		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
2772
2773	return 0;
2774}
2775
2776static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
2777{
2778	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
2779}
2780
2781static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
2782{
2783	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2784	void *tmp_ptr = &ckpt->sit_nat_version_bitmap;
2785	int offset;
2786
2787	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
2788		offset = (flag == SIT_BITMAP) ?
2789			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
2790		/*
2791		 * if large_nat_bitmap feature is enabled, leave checksum
2792		 * protection for all nat/sit bitmaps.
2793		 */
2794		return tmp_ptr + offset + sizeof(__le32);
2795	}
2796
2797	if (__cp_payload(sbi) > 0) {
2798		if (flag == NAT_BITMAP)
2799			return tmp_ptr;
2800		else
2801			return (unsigned char *)ckpt + F2FS_BLKSIZE;
2802	} else {
2803		offset = (flag == NAT_BITMAP) ?
2804			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
2805		return tmp_ptr + offset;
2806	}
2807}
2808
2809static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
2810{
2811	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
2812
2813	if (sbi->cur_cp_pack == 2)
2814		start_addr += BLKS_PER_SEG(sbi);
2815	return start_addr;
2816}
2817
2818static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
2819{
2820	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
2821
2822	if (sbi->cur_cp_pack == 1)
2823		start_addr += BLKS_PER_SEG(sbi);
2824	return start_addr;
2825}
2826
2827static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
2828{
2829	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
2830}
2831
2832static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
2833{
2834	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
2835}
2836
2837static inline bool __has_cursum_space(struct f2fs_sb_info *sbi,
2838			struct f2fs_journal *journal, int size, int type)
2839{
2840	if (type == NAT_JOURNAL)
2841		return size <= MAX_NAT_JENTRIES(sbi, journal);
2842	return size <= MAX_SIT_JENTRIES(sbi, journal);
2843}
2844
2845extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
2846static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
2847					struct inode *inode, bool is_inode)
2848{
2849	block_t	valid_block_count;
2850	unsigned int valid_node_count, avail_user_node_count;
2851	unsigned int avail_user_block_count;
2852	int err;
2853
2854	if (is_inode) {
2855		if (inode) {
2856			err = dquot_alloc_inode(inode);
2857			if (err)
2858				return err;
2859		}
2860	} else {
2861		err = dquot_reserve_block(inode, 1);
2862		if (err)
2863			return err;
2864	}
2865
2866	if (time_to_inject(sbi, FAULT_BLOCK))
2867		goto enospc;
2868
2869	spin_lock(&sbi->stat_lock);
2870
2871	valid_block_count = sbi->total_valid_block_count + 1;
2872	avail_user_block_count = get_available_block_count(sbi, inode,
2873			test_opt(sbi, RESERVE_NODE));
2874
2875	if (unlikely(valid_block_count > avail_user_block_count)) {
2876		spin_unlock(&sbi->stat_lock);
2877		goto enospc;
2878	}
2879
2880	avail_user_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
2881	if (test_opt(sbi, RESERVE_NODE) &&
2882			!__allow_reserved_root(sbi, inode, true))
2883		avail_user_node_count -= F2FS_OPTION(sbi).root_reserved_nodes;
2884	valid_node_count = sbi->total_valid_node_count + 1;
2885	if (unlikely(valid_node_count > avail_user_node_count)) {
2886		spin_unlock(&sbi->stat_lock);
2887		goto enospc;
2888	}
2889
2890	sbi->total_valid_node_count++;
2891	sbi->total_valid_block_count++;
2892	spin_unlock(&sbi->stat_lock);
2893
2894	if (inode) {
2895		if (is_inode)
2896			f2fs_mark_inode_dirty_sync(inode, true);
2897		else
2898			f2fs_i_blocks_write(inode, 1, true, true);
2899	}
2900
2901	percpu_counter_inc(&sbi->alloc_valid_block_count);
2902	return 0;
2903
2904enospc:
2905	if (is_inode) {
2906		if (inode)
2907			dquot_free_inode(inode);
2908	} else {
2909		dquot_release_reservation_block(inode, 1);
2910	}
2911	return -ENOSPC;
2912}
2913
2914static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2915					struct inode *inode, bool is_inode)
2916{
2917	spin_lock(&sbi->stat_lock);
2918
2919	if (unlikely(!sbi->total_valid_block_count ||
2920			!sbi->total_valid_node_count)) {
2921		f2fs_warn(sbi, "dec_valid_node_count: inconsistent block counts, total_valid_block:%u, total_valid_node:%u",
2922			  sbi->total_valid_block_count,
2923			  sbi->total_valid_node_count);
2924		set_sbi_flag(sbi, SBI_NEED_FSCK);
2925	} else {
2926		sbi->total_valid_block_count--;
2927		sbi->total_valid_node_count--;
2928	}
2929
2930	if (sbi->reserved_blocks &&
2931		sbi->current_reserved_blocks < sbi->reserved_blocks)
2932		sbi->current_reserved_blocks++;
2933
2934	spin_unlock(&sbi->stat_lock);
2935
2936	if (is_inode) {
2937		dquot_free_inode(inode);
2938	} else {
2939		if (unlikely(inode->i_blocks == 0)) {
2940			f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu",
2941				  inode->i_ino,
2942				  (unsigned long long)inode->i_blocks);
2943			set_sbi_flag(sbi, SBI_NEED_FSCK);
2944			return;
2945		}
2946		f2fs_i_blocks_write(inode, 1, false, true);
2947	}
2948}
2949
2950static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
2951{
2952	return sbi->total_valid_node_count;
2953}
2954
2955static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
2956{
2957	percpu_counter_inc(&sbi->total_valid_inode_count);
2958}
2959
2960static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
2961{
2962	percpu_counter_dec(&sbi->total_valid_inode_count);
2963}
2964
2965static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
2966{
2967	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
2968}
2969
2970static inline struct folio *f2fs_grab_cache_folio(struct address_space *mapping,
2971		pgoff_t index, bool for_write)
2972{
2973	struct folio *folio;
2974	unsigned int flags;
2975
2976	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
2977		fgf_t fgf_flags;
2978
2979		if (!for_write)
2980			fgf_flags = FGP_LOCK | FGP_ACCESSED;
2981		else
2982			fgf_flags = FGP_LOCK;
2983		folio = __filemap_get_folio(mapping, index, fgf_flags, 0);
2984		if (!IS_ERR(folio))
2985			return folio;
2986
2987		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC))
2988			return ERR_PTR(-ENOMEM);
2989	}
2990
2991	if (!for_write)
2992		return filemap_grab_folio(mapping, index);
2993
2994	flags = memalloc_nofs_save();
2995	folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
2996			mapping_gfp_mask(mapping));
2997	memalloc_nofs_restore(flags);
2998
2999	return folio;
3000}
3001
3002static inline struct folio *f2fs_filemap_get_folio(
3003				struct address_space *mapping, pgoff_t index,
3004				fgf_t fgp_flags, gfp_t gfp_mask)
3005{
3006	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET))
3007		return ERR_PTR(-ENOMEM);
3008
3009	return __filemap_get_folio(mapping, index, fgp_flags, gfp_mask);
3010}
3011
3012static inline void f2fs_folio_put(struct folio *folio, bool unlock)
3013{
3014	if (IS_ERR_OR_NULL(folio))
3015		return;
3016
3017	if (unlock) {
3018		f2fs_bug_on(F2FS_F_SB(folio), !folio_test_locked(folio));
3019		folio_unlock(folio);
3020	}
3021	folio_put(folio);
3022}
3023
3024static inline void f2fs_put_page(struct page *page, bool unlock)
3025{
3026	if (!page)
3027		return;
3028	f2fs_folio_put(page_folio(page), unlock);
3029}
3030
3031static inline void f2fs_put_dnode(struct dnode_of_data *dn)
3032{
3033	if (dn->node_folio)
3034		f2fs_folio_put(dn->node_folio, true);
3035	if (dn->inode_folio && dn->node_folio != dn->inode_folio)
3036		f2fs_folio_put(dn->inode_folio, false);
3037	dn->node_folio = NULL;
3038	dn->inode_folio = NULL;
3039}
3040
3041static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
3042					size_t size)
3043{
3044	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
3045}
3046
3047static inline void *f2fs_kmem_cache_alloc_nofail(struct kmem_cache *cachep,
3048						gfp_t flags)
3049{
3050	void *entry;
3051
3052	entry = kmem_cache_alloc(cachep, flags);
3053	if (!entry)
3054		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
3055	return entry;
3056}
3057
3058static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
3059			gfp_t flags, bool nofail, struct f2fs_sb_info *sbi)
3060{
3061	if (nofail)
3062		return f2fs_kmem_cache_alloc_nofail(cachep, flags);
3063
3064	if (time_to_inject(sbi, FAULT_SLAB_ALLOC))
3065		return NULL;
3066
3067	return kmem_cache_alloc(cachep, flags);
3068}
3069
3070static inline bool is_inflight_io(struct f2fs_sb_info *sbi, int type)
3071{
3072	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
3073		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
3074		get_pages(sbi, F2FS_WB_CP_DATA) ||
3075		get_pages(sbi, F2FS_DIO_READ) ||
3076		get_pages(sbi, F2FS_DIO_WRITE))
3077		return true;
3078
3079	if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
3080			atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
3081		return true;
3082
3083	if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
3084			atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
3085		return true;
3086	return false;
3087}
3088
3089static inline bool is_inflight_read_io(struct f2fs_sb_info *sbi)
3090{
3091	return get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_DIO_READ);
3092}
3093
3094static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
3095{
3096	bool zoned_gc = (type == GC_TIME &&
3097			F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_BLKZONED));
3098
3099	if (sbi->gc_mode == GC_URGENT_HIGH)
3100		return true;
3101
3102	if (sbi->bggc_io_aware == AWARE_READ_IO && is_inflight_read_io(sbi))
3103		return false;
3104	if (sbi->bggc_io_aware == AWARE_ALL_IO && is_inflight_io(sbi, type))
3105		return false;
3106
3107	if (sbi->gc_mode == GC_URGENT_MID)
3108		return true;
3109
3110	if (sbi->gc_mode == GC_URGENT_LOW &&
3111			(type == DISCARD_TIME || type == GC_TIME))
3112		return true;
3113
3114	if (zoned_gc)
3115		return true;
3116
3117	return f2fs_time_over(sbi, type);
3118}
3119
3120static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
3121				unsigned long index, void *item)
3122{
3123	while (radix_tree_insert(root, index, item))
3124		cond_resched();
3125}
3126
3127#define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
3128
3129static inline bool IS_INODE(const struct folio *folio)
3130{
3131	struct f2fs_node *p = F2FS_NODE(folio);
3132
3133	return RAW_IS_INODE(p);
3134}
3135
3136static inline int offset_in_addr(struct f2fs_inode *i)
3137{
3138	return (i->i_inline & F2FS_EXTRA_ATTR) ?
3139			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
3140}
3141
3142static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
3143{
3144	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
3145}
3146
3147static inline int f2fs_has_extra_attr(struct inode *inode);
3148static inline unsigned int get_dnode_base(struct inode *inode,
3149					struct folio *node_folio)
3150{
3151	if (!IS_INODE(node_folio))
3152		return 0;
3153
3154	return inode ? get_extra_isize(inode) :
3155			offset_in_addr(&F2FS_NODE(node_folio)->i);
3156}
3157
3158static inline __le32 *get_dnode_addr(struct inode *inode,
3159					struct folio *node_folio)
3160{
3161	return blkaddr_in_node(F2FS_NODE(node_folio)) +
3162			get_dnode_base(inode, node_folio);
3163}
3164
3165static inline block_t data_blkaddr(struct inode *inode,
3166			struct folio *node_folio, unsigned int offset)
3167{
3168	return le32_to_cpu(*(get_dnode_addr(inode, node_folio) + offset));
3169}
3170
3171static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn)
3172{
3173	return data_blkaddr(dn->inode, dn->node_folio, dn->ofs_in_node);
3174}
3175
3176static inline int f2fs_test_bit(unsigned int nr, char *addr)
3177{
3178	int mask;
3179
3180	addr += (nr >> 3);
3181	mask = BIT(7 - (nr & 0x07));
3182	return mask & *addr;
3183}
3184
3185static inline void f2fs_set_bit(unsigned int nr, char *addr)
3186{
3187	int mask;
3188
3189	addr += (nr >> 3);
3190	mask = BIT(7 - (nr & 0x07));
3191	*addr |= mask;
3192}
3193
3194static inline void f2fs_clear_bit(unsigned int nr, char *addr)
3195{
3196	int mask;
3197
3198	addr += (nr >> 3);
3199	mask = BIT(7 - (nr & 0x07));
3200	*addr &= ~mask;
3201}
3202
3203static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
3204{
3205	int mask;
3206	int ret;
3207
3208	addr += (nr >> 3);
3209	mask = BIT(7 - (nr & 0x07));
3210	ret = mask & *addr;
3211	*addr |= mask;
3212	return ret;
3213}
3214
3215static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
3216{
3217	int mask;
3218	int ret;
3219
3220	addr += (nr >> 3);
3221	mask = BIT(7 - (nr & 0x07));
3222	ret = mask & *addr;
3223	*addr &= ~mask;
3224	return ret;
3225}
3226
3227static inline void f2fs_change_bit(unsigned int nr, char *addr)
3228{
3229	int mask;
3230
3231	addr += (nr >> 3);
3232	mask = BIT(7 - (nr & 0x07));
3233	*addr ^= mask;
3234}
3235
3236/*
3237 * On-disk inode flags (f2fs_inode::i_flags)
3238 */
3239#define F2FS_COMPR_FL			0x00000004 /* Compress file */
3240#define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
3241#define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
3242#define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
3243#define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
3244#define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
3245#define F2FS_NOCOMP_FL			0x00000400 /* Don't compress */
3246#define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
3247#define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
3248#define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
3249#define F2FS_CASEFOLD_FL		0x40000000 /* Casefolded file */
3250#define F2FS_DEVICE_ALIAS_FL		0x80000000 /* File for aliasing a device */
3251
3252#define F2FS_QUOTA_DEFAULT_FL		(F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL)
3253
3254/* Flags that should be inherited by new inodes from their parent. */
3255#define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
3256			   F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
3257			   F2FS_CASEFOLD_FL)
3258
3259/* Flags that are appropriate for regular files (all but dir-specific ones). */
3260#define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
3261				F2FS_CASEFOLD_FL))
3262
3263/* Flags that are appropriate for non-directories/regular files. */
3264#define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
3265
3266#define IS_DEVICE_ALIASING(inode)	(F2FS_I(inode)->i_flags & F2FS_DEVICE_ALIAS_FL)
3267
3268static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
3269{
3270	if (S_ISDIR(mode))
3271		return flags;
3272	else if (S_ISREG(mode))
3273		return flags & F2FS_REG_FLMASK;
3274	else
3275		return flags & F2FS_OTHER_FLMASK;
3276}
3277
3278static inline void __mark_inode_dirty_flag(struct inode *inode,
3279						int flag, bool set)
3280{
3281	switch (flag) {
3282	case FI_INLINE_XATTR:
3283	case FI_INLINE_DATA:
3284	case FI_INLINE_DENTRY:
3285	case FI_NEW_INODE:
3286		if (set)
3287			return;
3288		fallthrough;
3289	case FI_DATA_EXIST:
3290	case FI_PIN_FILE:
3291	case FI_COMPRESS_RELEASED:
3292		f2fs_mark_inode_dirty_sync(inode, true);
3293	}
3294}
3295
3296static inline void set_inode_flag(struct inode *inode, int flag)
3297{
3298	set_bit(flag, F2FS_I(inode)->flags);
3299	__mark_inode_dirty_flag(inode, flag, true);
3300}
3301
3302static inline int is_inode_flag_set(struct inode *inode, int flag)
3303{
3304	return test_bit(flag, F2FS_I(inode)->flags);
3305}
3306
3307static inline void clear_inode_flag(struct inode *inode, int flag)
3308{
3309	clear_bit(flag, F2FS_I(inode)->flags);
3310	__mark_inode_dirty_flag(inode, flag, false);
3311}
3312
3313static inline bool f2fs_verity_in_progress(struct inode *inode)
3314{
3315	return IS_ENABLED(CONFIG_FS_VERITY) &&
3316	       is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
3317}
3318
3319static inline void set_acl_inode(struct inode *inode, umode_t mode)
3320{
3321	F2FS_I(inode)->i_acl_mode = mode;
3322	set_inode_flag(inode, FI_ACL_MODE);
3323	f2fs_mark_inode_dirty_sync(inode, false);
3324}
3325
3326static inline void f2fs_i_links_write(struct inode *inode, bool inc)
3327{
3328	if (inc)
3329		inc_nlink(inode);
3330	else
3331		drop_nlink(inode);
3332	f2fs_mark_inode_dirty_sync(inode, true);
3333}
3334
3335static inline void f2fs_i_blocks_write(struct inode *inode,
3336					block_t diff, bool add, bool claim)
3337{
3338	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
3339	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
3340
3341	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
3342	if (add) {
3343		if (claim)
3344			dquot_claim_block(inode, diff);
3345		else
3346			dquot_alloc_block_nofail(inode, diff);
3347	} else {
3348		dquot_free_block(inode, diff);
3349	}
3350
3351	f2fs_mark_inode_dirty_sync(inode, true);
3352	if (clean || recover)
3353		set_inode_flag(inode, FI_AUTO_RECOVER);
3354}
3355
3356static inline bool f2fs_is_atomic_file(struct inode *inode);
3357
3358static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
3359{
3360	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
3361	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
3362
3363	if (i_size_read(inode) == i_size)
3364		return;
3365
3366	i_size_write(inode, i_size);
3367
3368	if (f2fs_is_atomic_file(inode))
3369		return;
3370
3371	f2fs_mark_inode_dirty_sync(inode, true);
3372	if (clean || recover)
3373		set_inode_flag(inode, FI_AUTO_RECOVER);
3374}
3375
3376static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
3377{
3378	F2FS_I(inode)->i_current_depth = depth;
3379	f2fs_mark_inode_dirty_sync(inode, true);
3380}
3381
3382static inline void f2fs_i_gc_failures_write(struct inode *inode,
3383					unsigned int count)
3384{
3385	F2FS_I(inode)->i_gc_failures = count;
3386	f2fs_mark_inode_dirty_sync(inode, true);
3387}
3388
3389static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
3390{
3391	F2FS_I(inode)->i_xattr_nid = xnid;
3392	f2fs_mark_inode_dirty_sync(inode, true);
3393}
3394
3395static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
3396{
3397	F2FS_I(inode)->i_pino = pino;
3398	f2fs_mark_inode_dirty_sync(inode, true);
3399}
3400
3401static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
3402{
3403	struct f2fs_inode_info *fi = F2FS_I(inode);
3404
3405	if (ri->i_inline & F2FS_INLINE_XATTR)
3406		set_bit(FI_INLINE_XATTR, fi->flags);
3407	if (ri->i_inline & F2FS_INLINE_DATA)
3408		set_bit(FI_INLINE_DATA, fi->flags);
3409	if (ri->i_inline & F2FS_INLINE_DENTRY)
3410		set_bit(FI_INLINE_DENTRY, fi->flags);
3411	if (ri->i_inline & F2FS_DATA_EXIST)
3412		set_bit(FI_DATA_EXIST, fi->flags);
3413	if (ri->i_inline & F2FS_EXTRA_ATTR)
3414		set_bit(FI_EXTRA_ATTR, fi->flags);
3415	if (ri->i_inline & F2FS_PIN_FILE)
3416		set_bit(FI_PIN_FILE, fi->flags);
3417	if (ri->i_inline & F2FS_COMPRESS_RELEASED)
3418		set_bit(FI_COMPRESS_RELEASED, fi->flags);
3419}
3420
3421static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
3422{
3423	ri->i_inline = 0;
3424
3425	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
3426		ri->i_inline |= F2FS_INLINE_XATTR;
3427	if (is_inode_flag_set(inode, FI_INLINE_DATA))
3428		ri->i_inline |= F2FS_INLINE_DATA;
3429	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
3430		ri->i_inline |= F2FS_INLINE_DENTRY;
3431	if (is_inode_flag_set(inode, FI_DATA_EXIST))
3432		ri->i_inline |= F2FS_DATA_EXIST;
3433	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
3434		ri->i_inline |= F2FS_EXTRA_ATTR;
3435	if (is_inode_flag_set(inode, FI_PIN_FILE))
3436		ri->i_inline |= F2FS_PIN_FILE;
3437	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
3438		ri->i_inline |= F2FS_COMPRESS_RELEASED;
3439}
3440
3441static inline int f2fs_has_extra_attr(struct inode *inode)
3442{
3443	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
3444}
3445
3446static inline int f2fs_has_inline_xattr(struct inode *inode)
3447{
3448	return is_inode_flag_set(inode, FI_INLINE_XATTR);
3449}
3450
3451static inline int f2fs_compressed_file(struct inode *inode)
3452{
3453	return S_ISREG(inode->i_mode) &&
3454		is_inode_flag_set(inode, FI_COMPRESSED_FILE);
3455}
3456
3457static inline bool f2fs_need_compress_data(struct inode *inode)
3458{
3459	int compress_mode = F2FS_OPTION(F2FS_I_SB(inode)).compress_mode;
3460
3461	if (!f2fs_compressed_file(inode))
3462		return false;
3463
3464	if (compress_mode == COMPR_MODE_FS)
3465		return true;
3466	else if (compress_mode == COMPR_MODE_USER &&
3467			is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
3468		return true;
3469
3470	return false;
3471}
3472
3473static inline unsigned int addrs_per_page(struct inode *inode,
3474							bool is_inode)
3475{
3476	unsigned int addrs = is_inode ? (CUR_ADDRS_PER_INODE(inode) -
3477			get_inline_xattr_addrs(inode)) : DEF_ADDRS_PER_BLOCK;
3478
3479	if (f2fs_compressed_file(inode))
3480		return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size);
3481	return addrs;
3482}
3483
3484static inline
3485void *inline_xattr_addr(struct inode *inode, const struct folio *folio)
3486{
3487	struct f2fs_inode *ri = F2FS_INODE(folio);
3488
3489	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
3490					get_inline_xattr_addrs(inode)]);
3491}
3492
3493static inline int inline_xattr_size(struct inode *inode)
3494{
3495	if (f2fs_has_inline_xattr(inode))
3496		return get_inline_xattr_addrs(inode) * sizeof(__le32);
3497	return 0;
3498}
3499
3500/*
3501 * Notice: check inline_data flag without inode page lock is unsafe.
3502 * It could change at any time by f2fs_convert_inline_folio().
3503 */
3504static inline int f2fs_has_inline_data(struct inode *inode)
3505{
3506	return is_inode_flag_set(inode, FI_INLINE_DATA);
3507}
3508
3509static inline int f2fs_exist_data(struct inode *inode)
3510{
3511	return is_inode_flag_set(inode, FI_DATA_EXIST);
3512}
3513
3514static inline int f2fs_is_mmap_file(struct inode *inode)
3515{
3516	return is_inode_flag_set(inode, FI_MMAP_FILE);
3517}
3518
3519static inline bool f2fs_is_pinned_file(struct inode *inode)
3520{
3521	return is_inode_flag_set(inode, FI_PIN_FILE);
3522}
3523
3524static inline bool f2fs_is_atomic_file(struct inode *inode)
3525{
3526	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
3527}
3528
3529static inline bool f2fs_is_cow_file(struct inode *inode)
3530{
3531	return is_inode_flag_set(inode, FI_COW_FILE);
3532}
3533
3534static inline void *inline_data_addr(struct inode *inode, struct folio *folio)
3535{
3536	__le32 *addr = get_dnode_addr(inode, folio);
3537
3538	return (void *)(addr + DEF_INLINE_RESERVED_SIZE);
3539}
3540
3541static inline int f2fs_has_inline_dentry(struct inode *inode)
3542{
3543	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
3544}
3545
3546static inline int is_file(struct inode *inode, int type)
3547{
3548	return F2FS_I(inode)->i_advise & type;
3549}
3550
3551static inline void set_file(struct inode *inode, int type)
3552{
3553	if (is_file(inode, type))
3554		return;
3555	F2FS_I(inode)->i_advise |= type;
3556	f2fs_mark_inode_dirty_sync(inode, true);
3557}
3558
3559static inline void clear_file(struct inode *inode, int type)
3560{
3561	if (!is_file(inode, type))
3562		return;
3563	F2FS_I(inode)->i_advise &= ~type;
3564	f2fs_mark_inode_dirty_sync(inode, true);
3565}
3566
3567static inline bool f2fs_is_time_consistent(struct inode *inode)
3568{
3569	struct timespec64 ts = inode_get_atime(inode);
3570
3571	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &ts))
3572		return false;
3573	ts = inode_get_ctime(inode);
3574	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &ts))
3575		return false;
3576	ts = inode_get_mtime(inode);
3577	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &ts))
3578		return false;
3579	return true;
3580}
3581
3582static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
3583{
3584	bool ret;
3585
3586	if (dsync) {
3587		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3588
3589		spin_lock(&sbi->inode_lock[DIRTY_META]);
3590		ret = list_empty(&F2FS_I(inode)->gdirty_list);
3591		spin_unlock(&sbi->inode_lock[DIRTY_META]);
3592		return ret;
3593	}
3594	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
3595			file_keep_isize(inode) ||
3596			i_size_read(inode) & ~PAGE_MASK)
3597		return false;
3598
3599	if (!f2fs_is_time_consistent(inode))
3600		return false;
3601
3602	spin_lock(&F2FS_I(inode)->i_size_lock);
3603	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
3604	spin_unlock(&F2FS_I(inode)->i_size_lock);
3605
3606	return ret;
3607}
3608
3609static inline bool f2fs_readonly(struct super_block *sb)
3610{
3611	return sb_rdonly(sb);
3612}
3613
3614static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
3615{
3616	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
3617}
3618
3619static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
3620					size_t size, gfp_t flags)
3621{
3622	if (time_to_inject(sbi, FAULT_KMALLOC))
3623		return NULL;
3624
3625	return kmalloc(size, flags);
3626}
3627
3628static inline void *f2fs_getname(struct f2fs_sb_info *sbi)
3629{
3630	if (time_to_inject(sbi, FAULT_KMALLOC))
3631		return NULL;
3632
3633	return __getname();
3634}
3635
3636static inline void f2fs_putname(char *buf)
3637{
3638	__putname(buf);
3639}
3640
3641static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
3642					size_t size, gfp_t flags)
3643{
3644	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
3645}
3646
3647static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
3648					size_t size, gfp_t flags)
3649{
3650	if (time_to_inject(sbi, FAULT_KVMALLOC))
3651		return NULL;
3652
3653	return kvmalloc(size, flags);
3654}
3655
3656static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
3657					size_t size, gfp_t flags)
3658{
3659	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
3660}
3661
3662static inline void *f2fs_vmalloc(struct f2fs_sb_info *sbi, size_t size)
3663{
3664	if (time_to_inject(sbi, FAULT_VMALLOC))
3665		return NULL;
3666
3667	return vmalloc(size);
3668}
3669
3670static inline int get_extra_isize(struct inode *inode)
3671{
3672	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
3673}
3674
3675static inline int get_inline_xattr_addrs(struct inode *inode)
3676{
3677	return F2FS_I(inode)->i_inline_xattr_size;
3678}
3679
3680#define f2fs_get_inode_mode(i) \
3681	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
3682	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
3683
3684#define F2FS_MIN_EXTRA_ATTR_SIZE		(sizeof(__le32))
3685
3686#define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
3687	(offsetof(struct f2fs_inode, i_extra_end) -	\
3688	offsetof(struct f2fs_inode, i_extra_isize))	\
3689
3690#define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
3691#define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
3692		((offsetof(typeof(*(f2fs_inode)), field) +	\
3693		sizeof((f2fs_inode)->field))			\
3694		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
3695
3696#define __is_large_section(sbi)		(SEGS_PER_SEC(sbi) > 1)
3697
3698#define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
3699
3700bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3701					block_t blkaddr, int type);
3702static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
3703					block_t blkaddr, int type)
3704{
3705	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type))
3706		f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
3707			 blkaddr, type);
3708}
3709
3710static inline bool __is_valid_data_blkaddr(block_t blkaddr)
3711{
3712	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
3713			blkaddr == COMPRESS_ADDR)
3714		return false;
3715	return true;
3716}
3717
3718/*
3719 * file.c
3720 */
3721int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
3722int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock);
3723int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
3724int f2fs_truncate(struct inode *inode);
3725int f2fs_getattr(struct mnt_idmap *idmap, const struct path *path,
3726		 struct kstat *stat, u32 request_mask, unsigned int flags);
3727int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
3728		 struct iattr *attr);
3729int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
3730void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
3731int f2fs_do_shutdown(struct f2fs_sb_info *sbi, unsigned int flag,
3732						bool readonly, bool need_lock);
3733int f2fs_precache_extents(struct inode *inode);
3734int f2fs_fileattr_get(struct dentry *dentry, struct file_kattr *fa);
3735int f2fs_fileattr_set(struct mnt_idmap *idmap,
3736		      struct dentry *dentry, struct file_kattr *fa);
3737long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
3738long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
3739int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
3740int f2fs_pin_file_control(struct inode *inode, bool inc);
3741
3742/*
3743 * inode.c
3744 */
3745void f2fs_set_inode_flags(struct inode *inode);
3746bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct folio *folio);
3747void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct folio *folio);
3748struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
3749struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
3750int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
3751void f2fs_update_inode(struct inode *inode, struct folio *node_folio);
3752void f2fs_update_inode_page(struct inode *inode);
3753int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
3754void f2fs_remove_donate_inode(struct inode *inode);
3755void f2fs_evict_inode(struct inode *inode);
3756void f2fs_handle_failed_inode(struct inode *inode);
3757
3758/*
3759 * namei.c
3760 */
3761int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
3762							bool hot, bool set);
3763struct dentry *f2fs_get_parent(struct dentry *child);
3764int f2fs_get_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
3765		     struct inode **new_inode);
3766
3767/*
3768 * dir.c
3769 */
3770#if IS_ENABLED(CONFIG_UNICODE)
3771int f2fs_init_casefolded_name(const struct inode *dir,
3772			      struct f2fs_filename *fname);
3773void f2fs_free_casefolded_name(struct f2fs_filename *fname);
3774#else
3775static inline int f2fs_init_casefolded_name(const struct inode *dir,
3776					    struct f2fs_filename *fname)
3777{
3778	return 0;
3779}
3780
3781static inline void f2fs_free_casefolded_name(struct f2fs_filename *fname)
3782{
3783}
3784#endif /* CONFIG_UNICODE */
3785
3786int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
3787			int lookup, struct f2fs_filename *fname);
3788int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
3789			struct f2fs_filename *fname);
3790void f2fs_free_filename(struct f2fs_filename *fname);
3791struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
3792			const struct f2fs_filename *fname, int *max_slots,
3793			bool use_hash);
3794int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
3795			unsigned int start_pos, struct fscrypt_str *fstr);
3796void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
3797			struct f2fs_dentry_ptr *d);
3798struct folio *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
3799		const struct f2fs_filename *fname, struct folio *dfolio);
3800void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
3801			unsigned int current_depth);
3802int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
3803void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
3804struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
3805		const struct f2fs_filename *fname, struct folio **res_folio);
3806struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
3807			const struct qstr *child, struct folio **res_folio);
3808struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct folio **f);
3809ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
3810			struct folio **folio);
3811void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
3812			struct folio *folio, struct inode *inode);
3813bool f2fs_has_enough_room(struct inode *dir, struct folio *ifolio,
3814			  const struct f2fs_filename *fname);
3815void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
3816			const struct fscrypt_str *name, f2fs_hash_t name_hash,
3817			unsigned int bit_pos);
3818int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
3819			struct inode *inode, nid_t ino, umode_t mode);
3820int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
3821			struct inode *inode, nid_t ino, umode_t mode);
3822int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
3823			struct inode *inode, nid_t ino, umode_t mode);
3824void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct folio *folio,
3825			struct inode *dir, struct inode *inode);
3826int f2fs_do_tmpfile(struct inode *inode, struct inode *dir,
3827					struct f2fs_filename *fname);
3828bool f2fs_empty_dir(struct inode *dir);
3829
3830static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
3831{
3832	if (fscrypt_is_nokey_name(dentry))
3833		return -ENOKEY;
3834	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
3835				inode, inode->i_ino, inode->i_mode);
3836}
3837
3838/*
3839 * super.c
3840 */
3841int f2fs_inode_dirtied(struct inode *inode, bool sync);
3842void f2fs_inode_synced(struct inode *inode);
3843int f2fs_dquot_initialize(struct inode *inode);
3844int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
3845int f2fs_do_quota_sync(struct super_block *sb, int type);
3846loff_t max_file_blocks(struct inode *inode);
3847void f2fs_quota_off_umount(struct super_block *sb);
3848void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag);
3849void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason);
3850void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error);
3851int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
3852int f2fs_sync_fs(struct super_block *sb, int sync);
3853int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
3854
3855/*
3856 * hash.c
3857 */
3858void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname);
3859
3860/*
3861 * node.c
3862 */
3863struct node_info;
3864enum node_type;
3865
3866int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
3867bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
3868bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct folio *folio);
3869void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
3870void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct folio *folio);
3871void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
3872int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
3873bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
3874bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
3875int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
3876				struct node_info *ni, bool checkpoint_context);
3877pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
3878int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
3879int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
3880int f2fs_truncate_xattr_node(struct inode *inode);
3881int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
3882					unsigned int seq_id);
3883int f2fs_remove_inode_page(struct inode *inode);
3884struct folio *f2fs_new_inode_folio(struct inode *inode);
3885struct folio *f2fs_new_node_folio(struct dnode_of_data *dn, unsigned int ofs);
3886void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
3887struct folio *f2fs_get_node_folio(struct f2fs_sb_info *sbi, pgoff_t nid,
3888						enum node_type node_type);
3889int f2fs_sanity_check_node_footer(struct f2fs_sb_info *sbi,
3890					struct folio *folio, pgoff_t nid,
3891					enum node_type ntype, bool in_irq);
3892struct folio *f2fs_get_inode_folio(struct f2fs_sb_info *sbi, pgoff_t ino);
3893struct folio *f2fs_get_xnode_folio(struct f2fs_sb_info *sbi, pgoff_t xnid);
3894int f2fs_move_node_folio(struct folio *node_folio, int gc_type);
3895void f2fs_flush_inline_data(struct f2fs_sb_info *sbi);
3896int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
3897			struct writeback_control *wbc, bool atomic,
3898			unsigned int *seq_id);
3899int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
3900			struct writeback_control *wbc,
3901			bool do_balance, enum iostat_type io_type);
3902int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
3903bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
3904void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
3905void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
3906int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
3907int f2fs_recover_inline_xattr(struct inode *inode, struct folio *folio);
3908int f2fs_recover_xattr_data(struct inode *inode, struct folio *folio);
3909int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct folio *folio);
3910int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3911			unsigned int segno, struct f2fs_summary_block *sum);
3912int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3913int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
3914void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
3915int __init f2fs_create_node_manager_caches(void);
3916void f2fs_destroy_node_manager_caches(void);
3917
3918/*
3919 * segment.c
3920 */
3921bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
3922int f2fs_commit_atomic_write(struct inode *inode);
3923void f2fs_abort_atomic_write(struct inode *inode, bool clean);
3924void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
3925void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg);
3926int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
3927int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
3928int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
3929void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
3930void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
3931						unsigned int len);
3932bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
3933int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
3934void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
3935void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
3936bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
3937void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
3938					struct cp_control *cpc);
3939void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
3940block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
3941int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
3942void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
3943int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
3944bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno);
3945int f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi);
3946int f2fs_reinit_atgc_curseg(struct f2fs_sb_info *sbi);
3947void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi);
3948void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi);
3949int f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
3950					unsigned int start, unsigned int end);
3951int f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force);
3952int f2fs_allocate_pinning_section(struct f2fs_sb_info *sbi);
3953int f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3954int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
3955bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
3956					struct cp_control *cpc);
3957struct folio *f2fs_get_sum_folio(struct f2fs_sb_info *sbi, unsigned int segno);
3958void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
3959					block_t blk_addr);
3960void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct folio *folio,
3961						enum iostat_type io_type);
3962void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
3963void f2fs_outplace_write_data(struct dnode_of_data *dn,
3964			struct f2fs_io_info *fio);
3965int f2fs_inplace_write_data(struct f2fs_io_info *fio);
3966void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3967			block_t old_blkaddr, block_t new_blkaddr,
3968			bool recover_curseg, bool recover_newaddr,
3969			bool from_gc);
3970void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3971			block_t old_addr, block_t new_addr,
3972			unsigned char version, bool recover_curseg,
3973			bool recover_newaddr);
3974enum temp_type f2fs_get_segment_temp(struct f2fs_sb_info *sbi,
3975						enum log_type seg_type);
3976int f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct folio *folio,
3977			block_t old_blkaddr, block_t *new_blkaddr,
3978			struct f2fs_summary *sum, int type,
3979			struct f2fs_io_info *fio);
3980void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino,
3981					block_t blkaddr, unsigned int blkcnt);
3982void f2fs_folio_wait_writeback(struct folio *folio, enum page_type type,
3983		bool ordered, bool locked);
3984#define f2fs_wait_on_page_writeback(page, type, ordered, locked)	\
3985		f2fs_folio_wait_writeback(page_folio(page), type, ordered, locked)
3986void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
3987void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
3988								block_t len);
3989void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
3990void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
3991int f2fs_lookup_journal_in_cursum(struct f2fs_sb_info *sbi,
3992			struct f2fs_journal *journal, int type,
3993			unsigned int val, int alloc);
3994void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3995int f2fs_check_and_fix_write_pointer(struct f2fs_sb_info *sbi);
3996int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
3997void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
3998int __init f2fs_create_segment_manager_caches(void);
3999void f2fs_destroy_segment_manager_caches(void);
4000int f2fs_rw_hint_to_seg_type(struct f2fs_sb_info *sbi, enum rw_hint hint);
4001enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
4002			enum page_type type, enum temp_type temp);
4003unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi);
4004unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
4005			unsigned int segno);
4006unsigned long long f2fs_get_section_mtime(struct f2fs_sb_info *sbi,
4007			unsigned int segno);
4008
4009static inline struct inode *fio_inode(struct f2fs_io_info *fio)
4010{
4011	return fio->folio->mapping->host;
4012}
4013
4014#define DEF_FRAGMENT_SIZE	4
4015#define MIN_FRAGMENT_SIZE	1
4016#define MAX_FRAGMENT_SIZE	512
4017
4018static inline bool f2fs_need_rand_seg(struct f2fs_sb_info *sbi)
4019{
4020	return F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG ||
4021		F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK;
4022}
4023
4024/*
4025 * checkpoint.c
4026 */
4027void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io,
4028							unsigned char reason);
4029void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi);
4030struct folio *f2fs_grab_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index);
4031struct folio *f2fs_get_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index);
4032struct folio *f2fs_get_meta_folio_retry(struct f2fs_sb_info *sbi, pgoff_t index);
4033struct folio *f2fs_get_tmp_folio(struct f2fs_sb_info *sbi, pgoff_t index);
4034bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
4035					block_t blkaddr, int type);
4036bool f2fs_is_valid_blkaddr_raw(struct f2fs_sb_info *sbi,
4037					block_t blkaddr, int type);
4038int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
4039			int type, bool sync);
4040void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
4041							unsigned int ra_blocks);
4042long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
4043			long nr_to_write, enum iostat_type io_type);
4044void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
4045void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
4046void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
4047bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
4048void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
4049					unsigned int devidx, int type);
4050bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
4051					unsigned int devidx, int type);
4052int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
4053void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
4054void f2fs_add_orphan_inode(struct inode *inode);
4055void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
4056int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
4057int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
4058void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio);
4059void f2fs_remove_dirty_inode(struct inode *inode);
4060int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type,
4061								bool from_cp);
4062void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
4063u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi);
4064int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
4065void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
4066int __init f2fs_create_checkpoint_caches(void);
4067void f2fs_destroy_checkpoint_caches(void);
4068int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi);
4069int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi);
4070void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi);
4071void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi);
4072
4073/*
4074 * data.c
4075 */
4076int __init f2fs_init_bioset(void);
4077void f2fs_destroy_bioset(void);
4078bool f2fs_is_cp_guaranteed(const struct folio *folio);
4079int f2fs_init_bio_entry_cache(void);
4080void f2fs_destroy_bio_entry_cache(void);
4081void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, struct bio *bio,
4082			  enum page_type type);
4083int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi);
4084void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
4085void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
4086				struct inode *inode, struct folio *folio,
4087				nid_t ino, enum page_type type);
4088void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
4089					struct bio **bio, struct folio *folio);
4090void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
4091int f2fs_submit_page_bio(struct f2fs_io_info *fio);
4092int f2fs_merge_page_bio(struct f2fs_io_info *fio);
4093void f2fs_submit_page_write(struct f2fs_io_info *fio);
4094struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
4095		block_t blk_addr, sector_t *sector);
4096int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
4097void f2fs_set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
4098void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
4099int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
4100int f2fs_reserve_new_block(struct dnode_of_data *dn);
4101int f2fs_get_block_locked(struct dnode_of_data *dn, pgoff_t index);
4102int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
4103struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index,
4104		blk_opf_t op_flags, bool for_write, pgoff_t *next_pgofs);
4105struct folio *f2fs_find_data_folio(struct inode *inode, pgoff_t index,
4106		pgoff_t *next_pgofs);
4107struct folio *f2fs_get_lock_data_folio(struct inode *inode, pgoff_t index,
4108			bool for_write);
4109struct folio *f2fs_get_new_data_folio(struct inode *inode,
4110			struct folio *ifolio, pgoff_t index, bool new_i_size);
4111int f2fs_do_write_data_page(struct f2fs_io_info *fio);
4112int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag);
4113int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4114			u64 start, u64 len);
4115int f2fs_encrypt_one_page(struct f2fs_io_info *fio);
4116bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
4117bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
4118int f2fs_write_single_data_page(struct folio *folio, int *submitted,
4119				struct bio **bio, sector_t *last_block,
4120				struct writeback_control *wbc,
4121				enum iostat_type io_type,
4122				int compr_blocks, bool allow_balance);
4123void f2fs_write_failed(struct inode *inode, loff_t to);
4124void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
4125bool f2fs_release_folio(struct folio *folio, gfp_t wait);
4126bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
4127void f2fs_clear_page_cache_dirty_tag(struct folio *folio);
4128int f2fs_init_post_read_processing(void);
4129void f2fs_destroy_post_read_processing(void);
4130int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi);
4131void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi);
4132extern const struct iomap_ops f2fs_iomap_ops;
4133
4134/*
4135 * gc.c
4136 */
4137int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
4138void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
4139block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
4140int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control);
4141void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
4142int f2fs_gc_range(struct f2fs_sb_info *sbi,
4143		unsigned int start_seg, unsigned int end_seg,
4144		bool dry_run, unsigned int dry_run_sections);
4145int f2fs_resize_fs(struct file *filp, __u64 block_count);
4146int __init f2fs_create_garbage_collection_cache(void);
4147void f2fs_destroy_garbage_collection_cache(void);
4148/* victim selection function for cleaning and SSR */
4149int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result,
4150			int gc_type, int type, char alloc_mode,
4151			unsigned long long age, bool one_time);
4152
4153/*
4154 * recovery.c
4155 */
4156int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
4157bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
4158int __init f2fs_create_recovery_cache(void);
4159void f2fs_destroy_recovery_cache(void);
4160
4161/*
4162 * debug.c
4163 */
4164#ifdef CONFIG_F2FS_STAT_FS
4165enum {
4166	DEVSTAT_INUSE,
4167	DEVSTAT_DIRTY,
4168	DEVSTAT_FULL,
4169	DEVSTAT_FREE,
4170	DEVSTAT_PREFREE,
4171	DEVSTAT_MAX,
4172};
4173
4174struct f2fs_dev_stats {
4175	unsigned int devstats[2][DEVSTAT_MAX];		/* 0: segs, 1: secs */
4176};
4177
4178struct f2fs_stat_info {
4179	struct list_head stat_list;
4180	struct f2fs_sb_info *sbi;
4181	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
4182	int main_area_segs, main_area_sections, main_area_zones;
4183	unsigned long long hit_cached[NR_EXTENT_CACHES];
4184	unsigned long long hit_rbtree[NR_EXTENT_CACHES];
4185	unsigned long long total_ext[NR_EXTENT_CACHES];
4186	unsigned long long hit_total[NR_EXTENT_CACHES];
4187	int ext_tree[NR_EXTENT_CACHES];
4188	int zombie_tree[NR_EXTENT_CACHES];
4189	int ext_node[NR_EXTENT_CACHES];
4190	/* to count memory footprint */
4191	unsigned long long ext_mem[NR_EXTENT_CACHES];
4192	/* for read extent cache */
4193	unsigned long long hit_largest;
4194	/* for block age extent cache */
4195	unsigned long long allocated_data_blocks;
4196	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
4197	int ndirty_data, ndirty_qdata;
4198	unsigned int ndirty_dirs, ndirty_files, ndirty_all;
4199	unsigned int nquota_files, ndonate_files;
4200	int nats, dirty_nats, sits, dirty_sits;
4201	int free_nids, avail_nids, alloc_nids;
4202	int total_count, utilization;
4203	int nr_wb_cp_data, nr_wb_data;
4204	int nr_rd_data, nr_rd_node, nr_rd_meta;
4205	int nr_dio_read, nr_dio_write;
4206	unsigned int io_skip_bggc, other_skip_bggc;
4207	int nr_flushing, nr_flushed, flush_list_empty;
4208	int nr_discarding, nr_discarded;
4209	int nr_discard_cmd;
4210	unsigned int undiscard_blks;
4211	int nr_issued_ckpt, nr_total_ckpt, nr_queued_ckpt;
4212	unsigned int cur_ckpt_time, peak_ckpt_time;
4213	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
4214	int compr_inode, swapfile_inode;
4215	unsigned long long compr_blocks;
4216	int aw_cnt, max_aw_cnt;
4217	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
4218	unsigned int bimodal, avg_vblocks;
4219	int util_free, util_valid, util_invalid;
4220	int rsvd_segs, overp_segs;
4221	int dirty_count, node_pages, meta_pages, compress_pages;
4222	int compress_page_hit;
4223	int prefree_count, free_segs, free_secs;
4224	int cp_call_count[MAX_CALL_TYPE], cp_count;
4225	int gc_call_count[MAX_CALL_TYPE];
4226	int gc_segs[2][2];
4227	int gc_secs[2][2];
4228	int tot_blks, data_blks, node_blks;
4229	int bg_data_blks, bg_node_blks;
4230	int blkoff[NR_CURSEG_TYPE];
4231	int curseg[NR_CURSEG_TYPE];
4232	int cursec[NR_CURSEG_TYPE];
4233	int curzone[NR_CURSEG_TYPE];
4234	unsigned int dirty_seg[NR_CURSEG_TYPE];
4235	unsigned int full_seg[NR_CURSEG_TYPE];
4236	unsigned int valid_blks[NR_CURSEG_TYPE];
4237
4238	unsigned int meta_count[META_MAX];
4239	unsigned int segment_count[2];
4240	unsigned int block_count[2];
4241	unsigned int inplace_count;
4242	unsigned long long base_mem, cache_mem, page_mem;
4243	struct f2fs_dev_stats *dev_stats;
4244};
4245
4246static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
4247{
4248	return (struct f2fs_stat_info *)sbi->stat_info;
4249}
4250
4251#define stat_inc_cp_call_count(sbi, foreground)				\
4252		atomic_inc(&sbi->cp_call_count[(foreground)])
4253#define stat_inc_cp_count(sbi)		(F2FS_STAT(sbi)->cp_count++)
4254#define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
4255#define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
4256#define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
4257#define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
4258#define stat_inc_total_hit(sbi, type)		(atomic64_inc(&(sbi)->total_hit_ext[type]))
4259#define stat_inc_rbtree_node_hit(sbi, type)	(atomic64_inc(&(sbi)->read_hit_rbtree[type]))
4260#define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
4261#define stat_inc_cached_node_hit(sbi, type)	(atomic64_inc(&(sbi)->read_hit_cached[type]))
4262#define stat_inc_inline_xattr(inode)					\
4263	do {								\
4264		if (f2fs_has_inline_xattr(inode))			\
4265			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
4266	} while (0)
4267#define stat_dec_inline_xattr(inode)					\
4268	do {								\
4269		if (f2fs_has_inline_xattr(inode))			\
4270			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
4271	} while (0)
4272#define stat_inc_inline_inode(inode)					\
4273	do {								\
4274		if (f2fs_has_inline_data(inode))			\
4275			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
4276	} while (0)
4277#define stat_dec_inline_inode(inode)					\
4278	do {								\
4279		if (f2fs_has_inline_data(inode))			\
4280			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
4281	} while (0)
4282#define stat_inc_inline_dir(inode)					\
4283	do {								\
4284		if (f2fs_has_inline_dentry(inode))			\
4285			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
4286	} while (0)
4287#define stat_dec_inline_dir(inode)					\
4288	do {								\
4289		if (f2fs_has_inline_dentry(inode))			\
4290			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
4291	} while (0)
4292#define stat_inc_compr_inode(inode)					\
4293	do {								\
4294		if (f2fs_compressed_file(inode))			\
4295			(atomic_inc(&F2FS_I_SB(inode)->compr_inode));	\
4296	} while (0)
4297#define stat_dec_compr_inode(inode)					\
4298	do {								\
4299		if (f2fs_compressed_file(inode))			\
4300			(atomic_dec(&F2FS_I_SB(inode)->compr_inode));	\
4301	} while (0)
4302#define stat_add_compr_blocks(inode, blocks)				\
4303		(atomic64_add(blocks, &F2FS_I_SB(inode)->compr_blocks))
4304#define stat_sub_compr_blocks(inode, blocks)				\
4305		(atomic64_sub(blocks, &F2FS_I_SB(inode)->compr_blocks))
4306#define stat_inc_swapfile_inode(inode)					\
4307		(atomic_inc(&F2FS_I_SB(inode)->swapfile_inode))
4308#define stat_dec_swapfile_inode(inode)					\
4309		(atomic_dec(&F2FS_I_SB(inode)->swapfile_inode))
4310#define stat_inc_atomic_inode(inode)					\
4311			(atomic_inc(&F2FS_I_SB(inode)->atomic_files))
4312#define stat_dec_atomic_inode(inode)					\
4313			(atomic_dec(&F2FS_I_SB(inode)->atomic_files))
4314#define stat_inc_meta_count(sbi, blkaddr)				\
4315	do {								\
4316		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
4317			atomic_inc(&(sbi)->meta_count[META_CP]);	\
4318		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
4319			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
4320		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
4321			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
4322		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
4323			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
4324	} while (0)
4325#define stat_inc_seg_type(sbi, curseg)					\
4326		((sbi)->segment_count[(curseg)->alloc_type]++)
4327#define stat_inc_block_count(sbi, curseg)				\
4328		((sbi)->block_count[(curseg)->alloc_type]++)
4329#define stat_inc_inplace_blocks(sbi)					\
4330		(atomic_inc(&(sbi)->inplace_count))
4331#define stat_update_max_atomic_write(inode)				\
4332	do {								\
4333		int cur = atomic_read(&F2FS_I_SB(inode)->atomic_files);	\
4334		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
4335		if (cur > max)						\
4336			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
4337	} while (0)
4338#define stat_inc_gc_call_count(sbi, foreground)				\
4339		(F2FS_STAT(sbi)->gc_call_count[(foreground)]++)
4340#define stat_inc_gc_sec_count(sbi, type, gc_type)			\
4341		(F2FS_STAT(sbi)->gc_secs[(type)][(gc_type)]++)
4342#define stat_inc_gc_seg_count(sbi, type, gc_type)			\
4343		(F2FS_STAT(sbi)->gc_segs[(type)][(gc_type)]++)
4344
4345#define stat_inc_tot_blk_count(si, blks)				\
4346	((si)->tot_blks += (blks))
4347
4348#define stat_inc_data_blk_count(sbi, blks, gc_type)			\
4349	do {								\
4350		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
4351		stat_inc_tot_blk_count(si, blks);			\
4352		si->data_blks += (blks);				\
4353		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
4354	} while (0)
4355
4356#define stat_inc_node_blk_count(sbi, blks, gc_type)			\
4357	do {								\
4358		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
4359		stat_inc_tot_blk_count(si, blks);			\
4360		si->node_blks += (blks);				\
4361		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
4362	} while (0)
4363
4364int f2fs_build_stats(struct f2fs_sb_info *sbi);
4365void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
4366void __init f2fs_create_root_stats(void);
4367void f2fs_destroy_root_stats(void);
4368void f2fs_update_sit_info(struct f2fs_sb_info *sbi);
4369#else
4370#define stat_inc_cp_call_count(sbi, foreground)		do { } while (0)
4371#define stat_inc_cp_count(sbi)				do { } while (0)
4372#define stat_io_skip_bggc_count(sbi)			do { } while (0)
4373#define stat_other_skip_bggc_count(sbi)			do { } while (0)
4374#define stat_inc_dirty_inode(sbi, type)			do { } while (0)
4375#define stat_dec_dirty_inode(sbi, type)			do { } while (0)
4376#define stat_inc_total_hit(sbi, type)			do { } while (0)
4377#define stat_inc_rbtree_node_hit(sbi, type)		do { } while (0)
4378#define stat_inc_largest_node_hit(sbi)			do { } while (0)
4379#define stat_inc_cached_node_hit(sbi, type)		do { } while (0)
4380#define stat_inc_inline_xattr(inode)			do { } while (0)
4381#define stat_dec_inline_xattr(inode)			do { } while (0)
4382#define stat_inc_inline_inode(inode)			do { } while (0)
4383#define stat_dec_inline_inode(inode)			do { } while (0)
4384#define stat_inc_inline_dir(inode)			do { } while (0)
4385#define stat_dec_inline_dir(inode)			do { } while (0)
4386#define stat_inc_compr_inode(inode)			do { } while (0)
4387#define stat_dec_compr_inode(inode)			do { } while (0)
4388#define stat_add_compr_blocks(inode, blocks)		do { } while (0)
4389#define stat_sub_compr_blocks(inode, blocks)		do { } while (0)
4390#define stat_inc_swapfile_inode(inode)			do { } while (0)
4391#define stat_dec_swapfile_inode(inode)			do { } while (0)
4392#define stat_inc_atomic_inode(inode)			do { } while (0)
4393#define stat_dec_atomic_inode(inode)			do { } while (0)
4394#define stat_update_max_atomic_write(inode)		do { } while (0)
4395#define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
4396#define stat_inc_seg_type(sbi, curseg)			do { } while (0)
4397#define stat_inc_block_count(sbi, curseg)		do { } while (0)
4398#define stat_inc_inplace_blocks(sbi)			do { } while (0)
4399#define stat_inc_gc_call_count(sbi, foreground)		do { } while (0)
4400#define stat_inc_gc_sec_count(sbi, type, gc_type)	do { } while (0)
4401#define stat_inc_gc_seg_count(sbi, type, gc_type)	do { } while (0)
4402#define stat_inc_tot_blk_count(si, blks)		do { } while (0)
4403#define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
4404#define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
4405
4406static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
4407static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
4408static inline void __init f2fs_create_root_stats(void) { }
4409static inline void f2fs_destroy_root_stats(void) { }
4410static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {}
4411#endif
4412
4413extern const struct file_operations f2fs_dir_operations;
4414extern const struct file_operations f2fs_file_operations;
4415extern const struct inode_operations f2fs_file_inode_operations;
4416extern const struct address_space_operations f2fs_dblock_aops;
4417extern const struct address_space_operations f2fs_node_aops;
4418extern const struct address_space_operations f2fs_meta_aops;
4419extern const struct inode_operations f2fs_dir_inode_operations;
4420extern const struct inode_operations f2fs_symlink_inode_operations;
4421extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
4422extern const struct inode_operations f2fs_special_inode_operations;
4423extern struct kmem_cache *f2fs_inode_entry_slab;
4424
4425/*
4426 * inline.c
4427 */
4428bool f2fs_may_inline_data(struct inode *inode);
4429bool f2fs_sanity_check_inline_data(struct inode *inode, struct folio *ifolio);
4430bool f2fs_may_inline_dentry(struct inode *inode);
4431void f2fs_do_read_inline_data(struct folio *folio, struct folio *ifolio);
4432void f2fs_truncate_inline_inode(struct inode *inode, struct folio *ifolio,
4433		u64 from);
4434int f2fs_read_inline_data(struct inode *inode, struct folio *folio);
4435int f2fs_convert_inline_folio(struct dnode_of_data *dn, struct folio *folio);
4436int f2fs_convert_inline_inode(struct inode *inode);
4437int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
4438int f2fs_write_inline_data(struct inode *inode, struct folio *folio);
4439int f2fs_recover_inline_data(struct inode *inode, struct folio *nfolio);
4440struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
4441		const struct f2fs_filename *fname, struct folio **res_folio,
4442		bool use_hash);
4443int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
4444			struct folio *ifolio);
4445int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
4446			struct inode *inode, nid_t ino, umode_t mode);
4447void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
4448		struct folio *folio, struct inode *dir, struct inode *inode);
4449bool f2fs_empty_inline_dir(struct inode *dir);
4450int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
4451			struct fscrypt_str *fstr);
4452int f2fs_inline_data_fiemap(struct inode *inode,
4453			struct fiemap_extent_info *fieinfo,
4454			__u64 start, __u64 len);
4455
4456/*
4457 * shrinker.c
4458 */
4459unsigned long f2fs_shrink_count(struct shrinker *shrink,
4460			struct shrink_control *sc);
4461unsigned long f2fs_shrink_scan(struct shrinker *shrink,
4462			struct shrink_control *sc);
4463unsigned int f2fs_donate_files(void);
4464void f2fs_reclaim_caches(unsigned int reclaim_caches_kb);
4465void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
4466void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
4467
4468/*
4469 * extent_cache.c
4470 */
4471bool sanity_check_extent_cache(struct inode *inode, struct folio *ifolio);
4472void f2fs_init_extent_tree(struct inode *inode);
4473void f2fs_drop_extent_tree(struct inode *inode);
4474void f2fs_destroy_extent_node(struct inode *inode);
4475void f2fs_destroy_extent_tree(struct inode *inode);
4476void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
4477int __init f2fs_create_extent_cache(void);
4478void f2fs_destroy_extent_cache(void);
4479
4480/* read extent cache ops */
4481void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio);
4482bool f2fs_lookup_read_extent_cache(struct inode *inode, pgoff_t pgofs,
4483			struct extent_info *ei);
4484bool f2fs_lookup_read_extent_cache_block(struct inode *inode, pgoff_t index,
4485			block_t *blkaddr);
4486void f2fs_update_read_extent_cache(struct dnode_of_data *dn);
4487void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
4488			pgoff_t fofs, block_t blkaddr, unsigned int len);
4489unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
4490			int nr_shrink);
4491
4492/* block age extent cache ops */
4493void f2fs_init_age_extent_tree(struct inode *inode);
4494bool f2fs_lookup_age_extent_cache(struct inode *inode, pgoff_t pgofs,
4495			struct extent_info *ei);
4496void f2fs_update_age_extent_cache(struct dnode_of_data *dn);
4497void f2fs_update_age_extent_cache_range(struct dnode_of_data *dn,
4498			pgoff_t fofs, unsigned int len);
4499unsigned int f2fs_shrink_age_extent_tree(struct f2fs_sb_info *sbi,
4500			int nr_shrink);
4501
4502/*
4503 * sysfs.c
4504 */
4505#define MIN_RA_MUL	2
4506#define MAX_RA_MUL	256
4507
4508int __init f2fs_init_sysfs(void);
4509void f2fs_exit_sysfs(void);
4510int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
4511void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
4512
4513/* verity.c */
4514extern const struct fsverity_operations f2fs_verityops;
4515
4516/*
4517 * crypto support
4518 */
4519static inline bool f2fs_encrypted_file(struct inode *inode)
4520{
4521	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
4522}
4523
4524static inline void f2fs_set_encrypted_inode(struct inode *inode)
4525{
4526#ifdef CONFIG_FS_ENCRYPTION
4527	file_set_encrypt(inode);
4528	f2fs_set_inode_flags(inode);
4529#endif
4530}
4531
4532/*
4533 * Returns true if the reads of the inode's data need to undergo some
4534 * postprocessing step, like decryption or authenticity verification.
4535 */
4536static inline bool f2fs_post_read_required(struct inode *inode)
4537{
4538	return f2fs_encrypted_file(inode) || fsverity_active(inode) ||
4539		f2fs_compressed_file(inode);
4540}
4541
4542static inline bool f2fs_used_in_atomic_write(struct inode *inode)
4543{
4544	return f2fs_is_atomic_file(inode) || f2fs_is_cow_file(inode);
4545}
4546
4547static inline bool f2fs_meta_inode_gc_required(struct inode *inode)
4548{
4549	return f2fs_post_read_required(inode) || f2fs_used_in_atomic_write(inode);
4550}
4551
4552/*
4553 * compress.c
4554 */
4555#ifdef CONFIG_F2FS_FS_COMPRESSION
4556enum cluster_check_type {
4557	CLUSTER_IS_COMPR,   /* check only if compressed cluster */
4558	CLUSTER_COMPR_BLKS, /* return # of compressed blocks in a cluster */
4559	CLUSTER_RAW_BLKS    /* return # of raw blocks in a cluster */
4560};
4561bool f2fs_is_compressed_page(struct folio *folio);
4562struct folio *f2fs_compress_control_folio(struct folio *folio);
4563int f2fs_prepare_compress_overwrite(struct inode *inode,
4564			struct page **pagep, pgoff_t index, void **fsdata);
4565bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
4566					pgoff_t index, unsigned copied);
4567int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
4568void f2fs_compress_write_end_io(struct bio *bio, struct folio *folio);
4569bool f2fs_is_compress_backend_ready(struct inode *inode);
4570bool f2fs_is_compress_level_valid(int alg, int lvl);
4571int __init f2fs_init_compress_mempool(void);
4572void f2fs_destroy_compress_mempool(void);
4573void f2fs_decompress_cluster(struct decompress_io_ctx *dic, bool in_task);
4574void f2fs_end_read_compressed_page(struct folio *folio, bool failed,
4575				block_t blkaddr, bool in_task);
4576bool f2fs_cluster_is_empty(struct compress_ctx *cc);
4577bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
4578bool f2fs_all_cluster_page_ready(struct compress_ctx *cc, struct page **pages,
4579				int index, int nr_pages, bool uptodate);
4580bool f2fs_sanity_check_cluster(struct dnode_of_data *dn);
4581void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct folio *folio);
4582int f2fs_write_multi_pages(struct compress_ctx *cc,
4583						int *submitted,
4584						struct writeback_control *wbc,
4585						enum iostat_type io_type);
4586int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index);
4587bool f2fs_is_sparse_cluster(struct inode *inode, pgoff_t index);
4588void f2fs_update_read_extent_tree_range_compressed(struct inode *inode,
4589				pgoff_t fofs, block_t blkaddr,
4590				unsigned int llen, unsigned int c_len);
4591int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
4592				unsigned nr_pages, sector_t *last_block_in_bio,
4593				struct readahead_control *rac, bool for_write);
4594struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc);
4595void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
4596				bool in_task);
4597void f2fs_put_folio_dic(struct folio *folio, bool in_task);
4598unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,
4599						unsigned int ofs_in_node);
4600int f2fs_init_compress_ctx(struct compress_ctx *cc);
4601void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse);
4602void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
4603int f2fs_init_compress_inode(struct f2fs_sb_info *sbi);
4604void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi);
4605int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi);
4606void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
4607int __init f2fs_init_compress_cache(void);
4608void f2fs_destroy_compress_cache(void);
4609struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
4610void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
4611					block_t blkaddr, unsigned int len);
4612bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,
4613								block_t blkaddr);
4614void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino);
4615#define inc_compr_inode_stat(inode)					\
4616	do {								\
4617		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
4618		sbi->compr_new_inode++;					\
4619	} while (0)
4620#define add_compr_block_stat(inode, blocks)				\
4621	do {								\
4622		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
4623		int diff = F2FS_I(inode)->i_cluster_size - blocks;	\
4624		sbi->compr_written_block += blocks;			\
4625		sbi->compr_saved_block += diff;				\
4626	} while (0)
4627#else
4628static inline bool f2fs_is_compressed_page(struct folio *folio) { return false; }
4629static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
4630{
4631	if (!f2fs_compressed_file(inode))
4632		return true;
4633	/* not support compression */
4634	return false;
4635}
4636static inline bool f2fs_is_compress_level_valid(int alg, int lvl) { return false; }
4637static inline struct folio *f2fs_compress_control_folio(struct folio *folio)
4638{
4639	WARN_ON_ONCE(1);
4640	return ERR_PTR(-EINVAL);
4641}
4642static inline int __init f2fs_init_compress_mempool(void) { return 0; }
4643static inline void f2fs_destroy_compress_mempool(void) { }
4644static inline void f2fs_decompress_cluster(struct decompress_io_ctx *dic,
4645				bool in_task) { }
4646static inline void f2fs_end_read_compressed_page(struct folio *folio,
4647				bool failed, block_t blkaddr, bool in_task)
4648{
4649	WARN_ON_ONCE(1);
4650}
4651static inline void f2fs_put_folio_dic(struct folio *folio, bool in_task)
4652{
4653	WARN_ON_ONCE(1);
4654}
4655static inline unsigned int f2fs_cluster_blocks_are_contiguous(
4656			struct dnode_of_data *dn, unsigned int ofs_in_node) { return 0; }
4657static inline bool f2fs_sanity_check_cluster(struct dnode_of_data *dn) { return false; }
4658static inline int f2fs_init_compress_inode(struct f2fs_sb_info *sbi) { return 0; }
4659static inline void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi) { }
4660static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return 0; }
4661static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
4662static inline int __init f2fs_init_compress_cache(void) { return 0; }
4663static inline void f2fs_destroy_compress_cache(void) { }
4664static inline void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
4665				block_t blkaddr, unsigned int len) { }
4666static inline bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi,
4667		struct folio *folio, block_t blkaddr) { return false; }
4668static inline void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi,
4669							nid_t ino) { }
4670#define inc_compr_inode_stat(inode)		do { } while (0)
4671static inline int f2fs_is_compressed_cluster(
4672				struct inode *inode,
4673				pgoff_t index) { return 0; }
4674static inline bool f2fs_is_sparse_cluster(
4675				struct inode *inode,
4676				pgoff_t index) { return true; }
4677static inline void f2fs_update_read_extent_tree_range_compressed(
4678				struct inode *inode,
4679				pgoff_t fofs, block_t blkaddr,
4680				unsigned int llen, unsigned int c_len) { }
4681#endif
4682
4683static inline int set_compress_context(struct inode *inode)
4684{
4685#ifdef CONFIG_F2FS_FS_COMPRESSION
4686	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4687	struct f2fs_inode_info *fi = F2FS_I(inode);
4688
4689	fi->i_compress_algorithm = F2FS_OPTION(sbi).compress_algorithm;
4690	fi->i_log_cluster_size = F2FS_OPTION(sbi).compress_log_size;
4691	fi->i_compress_flag = F2FS_OPTION(sbi).compress_chksum ?
4692					BIT(COMPRESS_CHKSUM) : 0;
4693	fi->i_cluster_size = BIT(fi->i_log_cluster_size);
4694	if ((fi->i_compress_algorithm == COMPRESS_LZ4 ||
4695		fi->i_compress_algorithm == COMPRESS_ZSTD) &&
4696			F2FS_OPTION(sbi).compress_level)
4697		fi->i_compress_level = F2FS_OPTION(sbi).compress_level;
4698	fi->i_flags |= F2FS_COMPR_FL;
4699	set_inode_flag(inode, FI_COMPRESSED_FILE);
4700	stat_inc_compr_inode(inode);
4701	inc_compr_inode_stat(inode);
4702	f2fs_mark_inode_dirty_sync(inode, true);
4703	return 0;
4704#else
4705	return -EOPNOTSUPP;
4706#endif
4707}
4708
4709static inline bool f2fs_disable_compressed_file(struct inode *inode)
4710{
4711	struct f2fs_inode_info *fi = F2FS_I(inode);
4712
4713	f2fs_down_write(&fi->i_sem);
4714
4715	if (!f2fs_compressed_file(inode)) {
4716		f2fs_up_write(&fi->i_sem);
4717		return true;
4718	}
4719	if (f2fs_is_mmap_file(inode) || atomic_read(&fi->writeback) ||
4720		(S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))) {
4721		f2fs_up_write(&fi->i_sem);
4722		return false;
4723	}
4724
4725	fi->i_flags &= ~F2FS_COMPR_FL;
4726	stat_dec_compr_inode(inode);
4727	clear_inode_flag(inode, FI_COMPRESSED_FILE);
4728	f2fs_mark_inode_dirty_sync(inode, true);
4729
4730	f2fs_up_write(&fi->i_sem);
4731	return true;
4732}
4733
4734#define F2FS_FEATURE_FUNCS(name, flagname) \
4735static inline bool f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
4736{ \
4737	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
4738}
4739
4740F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
4741F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
4742F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
4743F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
4744F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
4745F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
4746F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
4747F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
4748F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
4749F2FS_FEATURE_FUNCS(verity, VERITY);
4750F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
4751F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
4752F2FS_FEATURE_FUNCS(compression, COMPRESSION);
4753F2FS_FEATURE_FUNCS(readonly, RO);
4754F2FS_FEATURE_FUNCS(device_alias, DEVICE_ALIAS);
4755F2FS_FEATURE_FUNCS(packed_ssa, PACKED_SSA);
4756
4757#ifdef CONFIG_BLK_DEV_ZONED
4758static inline bool f2fs_zone_is_seq(struct f2fs_sb_info *sbi, int devi,
4759							unsigned int zone)
4760{
4761	return test_bit(zone, FDEV(devi).blkz_seq);
4762}
4763
4764static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
4765								block_t blkaddr)
4766{
4767	return f2fs_zone_is_seq(sbi, devi, blkaddr / sbi->blocks_per_blkz);
4768}
4769#endif
4770
4771static inline int f2fs_bdev_index(struct f2fs_sb_info *sbi,
4772				  struct block_device *bdev)
4773{
4774	int i;
4775
4776	if (!f2fs_is_multi_device(sbi))
4777		return 0;
4778
4779	for (i = 0; i < sbi->s_ndevs; i++)
4780		if (FDEV(i).bdev == bdev)
4781			return i;
4782
4783	WARN_ON(1);
4784	return -1;
4785}
4786
4787static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
4788{
4789	return f2fs_sb_has_blkzoned(sbi);
4790}
4791
4792static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
4793{
4794	return bdev_max_discard_sectors(bdev) || bdev_is_zoned(bdev);
4795}
4796
4797static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
4798{
4799	int i;
4800
4801	if (!f2fs_is_multi_device(sbi))
4802		return f2fs_bdev_support_discard(sbi->sb->s_bdev);
4803
4804	for (i = 0; i < sbi->s_ndevs; i++)
4805		if (f2fs_bdev_support_discard(FDEV(i).bdev))
4806			return true;
4807	return false;
4808}
4809
4810static inline unsigned int f2fs_hw_discard_granularity(struct f2fs_sb_info *sbi)
4811{
4812	int i = 1;
4813	unsigned int discard_granularity = bdev_discard_granularity(sbi->sb->s_bdev);
4814
4815	if (f2fs_is_multi_device(sbi))
4816		for (; i < sbi->s_ndevs && !bdev_is_zoned(FDEV(i).bdev); i++)
4817			discard_granularity = max_t(unsigned int, discard_granularity,
4818						bdev_discard_granularity(FDEV(i).bdev));
4819	return discard_granularity;
4820}
4821
4822static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
4823{
4824	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
4825					f2fs_hw_should_discard(sbi);
4826}
4827
4828static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
4829{
4830	int i;
4831
4832	if (!f2fs_is_multi_device(sbi))
4833		return bdev_read_only(sbi->sb->s_bdev);
4834
4835	for (i = 0; i < sbi->s_ndevs; i++)
4836		if (bdev_read_only(FDEV(i).bdev))
4837			return true;
4838	return false;
4839}
4840
4841static inline bool f2fs_dev_is_readonly(struct f2fs_sb_info *sbi)
4842{
4843	return f2fs_sb_has_readonly(sbi) || f2fs_hw_is_readonly(sbi);
4844}
4845
4846static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
4847{
4848	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
4849}
4850
4851static inline bool f2fs_is_sequential_zone_area(struct f2fs_sb_info *sbi,
4852					  block_t blkaddr)
4853{
4854	if (f2fs_sb_has_blkzoned(sbi)) {
4855#ifdef CONFIG_BLK_DEV_ZONED
4856		int devi = f2fs_target_device_index(sbi, blkaddr);
4857
4858		if (!bdev_is_zoned(FDEV(devi).bdev))
4859			return false;
4860
4861		if (f2fs_is_multi_device(sbi)) {
4862			if (blkaddr < FDEV(devi).start_blk ||
4863				blkaddr > FDEV(devi).end_blk) {
4864				f2fs_err(sbi, "Invalid block %x", blkaddr);
4865				return false;
4866			}
4867			blkaddr -= FDEV(devi).start_blk;
4868		}
4869
4870		return f2fs_blkz_is_seq(sbi, devi, blkaddr);
4871#else
4872		return false;
4873#endif
4874	}
4875	return false;
4876}
4877
4878static inline bool f2fs_low_mem_mode(struct f2fs_sb_info *sbi)
4879{
4880	return F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW;
4881}
4882
4883static inline bool f2fs_may_compress(struct inode *inode)
4884{
4885	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
4886		f2fs_is_atomic_file(inode) || f2fs_has_inline_data(inode) ||
4887		f2fs_is_mmap_file(inode))
4888		return false;
4889	return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
4890}
4891
4892static inline void f2fs_i_compr_blocks_update(struct inode *inode,
4893						u64 blocks, bool add)
4894{
4895	struct f2fs_inode_info *fi = F2FS_I(inode);
4896	int diff = fi->i_cluster_size - blocks;
4897
4898	/* don't update i_compr_blocks if saved blocks were released */
4899	if (!add && !atomic_read(&fi->i_compr_blocks))
4900		return;
4901
4902	if (add) {
4903		atomic_add(diff, &fi->i_compr_blocks);
4904		stat_add_compr_blocks(inode, diff);
4905	} else {
4906		atomic_sub(diff, &fi->i_compr_blocks);
4907		stat_sub_compr_blocks(inode, diff);
4908	}
4909	f2fs_mark_inode_dirty_sync(inode, true);
4910}
4911
4912static inline bool f2fs_allow_multi_device_dio(struct f2fs_sb_info *sbi,
4913								int flag)
4914{
4915	if (!f2fs_is_multi_device(sbi))
4916		return false;
4917	if (flag != F2FS_GET_BLOCK_DIO)
4918		return false;
4919	return sbi->aligned_blksize;
4920}
4921
4922static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
4923{
4924	return fsverity_active(inode) &&
4925	       idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
4926}
4927
4928#ifdef CONFIG_F2FS_FAULT_INJECTION
4929extern int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
4930					unsigned long type, enum fault_option fo);
4931#else
4932static inline int f2fs_build_fault_attr(struct f2fs_sb_info *sbi,
4933					unsigned long rate, unsigned long type,
4934					enum fault_option fo)
4935{
4936	return 0;
4937}
4938#endif
4939
4940static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
4941{
4942#ifdef CONFIG_QUOTA
4943	if (f2fs_sb_has_quota_ino(sbi))
4944		return true;
4945	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
4946		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
4947		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4948		return true;
4949#endif
4950	return false;
4951}
4952
4953static inline bool f2fs_block_unit_discard(struct f2fs_sb_info *sbi)
4954{
4955	return F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK;
4956}
4957
4958static inline void __f2fs_schedule_timeout(long timeout, bool io)
4959{
4960	set_current_state(TASK_UNINTERRUPTIBLE);
4961	if (io)
4962		io_schedule_timeout(timeout);
4963	else
4964		schedule_timeout(timeout);
4965}
4966
4967#define f2fs_io_schedule_timeout(timeout)		\
4968			__f2fs_schedule_timeout(timeout, true)
4969#define f2fs_schedule_timeout(timeout)			\
4970			__f2fs_schedule_timeout(timeout, false)
4971
4972static inline void f2fs_io_schedule_timeout_killable(long timeout)
4973{
4974	while (timeout) {
4975		if (fatal_signal_pending(current))
4976			return;
4977		set_current_state(TASK_UNINTERRUPTIBLE);
4978		io_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT);
4979		if (timeout <= DEFAULT_SCHEDULE_TIMEOUT)
4980			return;
4981		timeout -= DEFAULT_SCHEDULE_TIMEOUT;
4982	}
4983}
4984
4985static inline void f2fs_handle_page_eio(struct f2fs_sb_info *sbi,
4986				struct folio *folio, enum page_type type)
4987{
4988	pgoff_t ofs = folio->index;
4989
4990	if (unlikely(f2fs_cp_error(sbi)))
4991		return;
4992
4993	if (ofs == sbi->page_eio_ofs[type]) {
4994		if (sbi->page_eio_cnt[type]++ == MAX_RETRY_PAGE_EIO)
4995			set_ckpt_flags(sbi, CP_ERROR_FLAG);
4996	} else {
4997		sbi->page_eio_ofs[type] = ofs;
4998		sbi->page_eio_cnt[type] = 0;
4999	}
5000}
5001
5002static inline bool f2fs_is_readonly(struct f2fs_sb_info *sbi)
5003{
5004	return f2fs_sb_has_readonly(sbi) || f2fs_readonly(sbi->sb);
5005}
5006
5007static inline void f2fs_truncate_meta_inode_pages(struct f2fs_sb_info *sbi,
5008					block_t blkaddr, unsigned int cnt)
5009{
5010	bool need_submit = false;
5011	int i = 0;
5012
5013	do {
5014		struct folio *folio;
5015
5016		folio = filemap_get_folio(META_MAPPING(sbi), blkaddr + i);
5017		if (!IS_ERR(folio)) {
5018			if (folio_test_writeback(folio))
5019				need_submit = true;
5020			f2fs_folio_put(folio, false);
5021		}
5022	} while (++i < cnt && !need_submit);
5023
5024	if (need_submit)
5025		f2fs_submit_merged_write_cond(sbi, sbi->meta_inode,
5026							NULL, 0, DATA);
5027
5028	truncate_inode_pages_range(META_MAPPING(sbi),
5029			F2FS_BLK_TO_BYTES((loff_t)blkaddr),
5030			F2FS_BLK_END_BYTES((loff_t)(blkaddr + cnt - 1)));
5031}
5032
5033static inline void f2fs_invalidate_internal_cache(struct f2fs_sb_info *sbi,
5034						block_t blkaddr, unsigned int len)
5035{
5036	f2fs_truncate_meta_inode_pages(sbi, blkaddr, len);
5037	f2fs_invalidate_compress_pages_range(sbi, blkaddr, len);
5038}
5039
5040#define EFSBADCRC	EBADMSG		/* Bad CRC detected */
5041#define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
5042
5043#endif /* _LINUX_F2FS_H */
~/ Read-only file preview.