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