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