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