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