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