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