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