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