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