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