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