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