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