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