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