• 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 	unsigned 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_INLINE_DOTS,		/* indicate inline dot dentries */
697 	FI_DO_DEFRAG,		/* indicate defragment is running */
698 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
699 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
700 	FI_HOT_DATA,		/* indicate file is hot */
701 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
702 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
703 	FI_PIN_FILE,		/* indicate file should not be gced */
704 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
705 	FI_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
706 	FI_COMPRESSED_FILE,	/* indicate file's data can be compressed */
707 	FI_MMAP_FILE,		/* indicate file was mmapped */
708 	FI_MAX,			/* max flag, never be used */
709 };
710 
711 struct f2fs_inode_info {
712 	struct inode vfs_inode;		/* serve a vfs inode */
713 	unsigned long i_flags;		/* keep an inode flags for ioctl */
714 	unsigned char i_advise;		/* use to give file attribute hints */
715 	unsigned char i_dir_level;	/* use for dentry level for large dir */
716 	unsigned int i_current_depth;	/* only for directory depth */
717 	/* for gc failure statistic */
718 	unsigned int i_gc_failures[MAX_GC_FAILURE];
719 	unsigned int i_pino;		/* parent inode number */
720 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
721 
722 	/* Use below internally in f2fs*/
723 	unsigned long flags[BITS_TO_LONGS(FI_MAX)];	/* use to pass per-file flags */
724 	struct rw_semaphore i_sem;	/* protect fi info */
725 	atomic_t dirty_pages;		/* # of dirty pages */
726 	f2fs_hash_t chash;		/* hash value of given file name */
727 	unsigned int clevel;		/* maximum level of given file name */
728 	struct task_struct *task;	/* lookup and create consistency */
729 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
730 	struct task_struct *wb_task;	/* indicate inode is in context of writeback */
731 	nid_t i_xattr_nid;		/* node id that contains xattrs */
732 	loff_t	last_disk_size;		/* lastly written file size */
733 	spinlock_t i_size_lock;		/* protect last_disk_size */
734 
735 #ifdef CONFIG_QUOTA
736 	struct dquot *i_dquot[MAXQUOTAS];
737 
738 	/* quota space reservation, managed internally by quota code */
739 	qsize_t i_reserved_quota;
740 #endif
741 	struct list_head dirty_list;	/* dirty list for dirs and files */
742 	struct list_head gdirty_list;	/* linked in global dirty list */
743 	struct list_head inmem_ilist;	/* list for inmem inodes */
744 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
745 	struct task_struct *inmem_task;	/* store inmemory task */
746 	struct mutex inmem_lock;	/* lock for inmemory pages */
747 	pgoff_t ra_offset;		/* ongoing readahead offset */
748 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
749 
750 	/* avoid racing between foreground op and gc */
751 	struct rw_semaphore i_gc_rwsem[2];
752 	struct rw_semaphore i_mmap_sem;
753 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
754 
755 	int i_extra_isize;		/* size of extra space located in i_addr */
756 	kprojid_t i_projid;		/* id for project quota */
757 	int i_inline_xattr_size;	/* inline xattr size */
758 	struct timespec64 i_crtime;	/* inode creation time */
759 	struct timespec64 i_disk_time[4];/* inode disk times */
760 
761 	/* for file compress */
762 	atomic_t i_compr_blocks;		/* # of compressed blocks */
763 	unsigned char i_compress_algorithm;	/* algorithm type */
764 	unsigned char i_log_cluster_size;	/* log of cluster size */
765 	unsigned int i_cluster_size;		/* cluster size */
766 };
767 
get_extent_info(struct extent_info * ext,struct f2fs_extent * i_ext)768 static inline void get_extent_info(struct extent_info *ext,
769 					struct f2fs_extent *i_ext)
770 {
771 	ext->fofs = le32_to_cpu(i_ext->fofs);
772 	ext->blk = le32_to_cpu(i_ext->blk);
773 	ext->len = le32_to_cpu(i_ext->len);
774 }
775 
set_raw_extent(struct extent_info * ext,struct f2fs_extent * i_ext)776 static inline void set_raw_extent(struct extent_info *ext,
777 					struct f2fs_extent *i_ext)
778 {
779 	i_ext->fofs = cpu_to_le32(ext->fofs);
780 	i_ext->blk = cpu_to_le32(ext->blk);
781 	i_ext->len = cpu_to_le32(ext->len);
782 }
783 
set_extent_info(struct extent_info * ei,unsigned int fofs,u32 blk,unsigned int len)784 static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
785 						u32 blk, unsigned int len)
786 {
787 	ei->fofs = fofs;
788 	ei->blk = blk;
789 	ei->len = len;
790 }
791 
__is_discard_mergeable(struct discard_info * back,struct discard_info * front,unsigned int max_len)792 static inline bool __is_discard_mergeable(struct discard_info *back,
793 			struct discard_info *front, unsigned int max_len)
794 {
795 	return (back->lstart + back->len == front->lstart) &&
796 		(back->len + front->len <= max_len);
797 }
798 
__is_discard_back_mergeable(struct discard_info * cur,struct discard_info * back,unsigned int max_len)799 static inline bool __is_discard_back_mergeable(struct discard_info *cur,
800 			struct discard_info *back, unsigned int max_len)
801 {
802 	return __is_discard_mergeable(back, cur, max_len);
803 }
804 
__is_discard_front_mergeable(struct discard_info * cur,struct discard_info * front,unsigned int max_len)805 static inline bool __is_discard_front_mergeable(struct discard_info *cur,
806 			struct discard_info *front, unsigned int max_len)
807 {
808 	return __is_discard_mergeable(cur, front, max_len);
809 }
810 
__is_extent_mergeable(struct extent_info * back,struct extent_info * front)811 static inline bool __is_extent_mergeable(struct extent_info *back,
812 						struct extent_info *front)
813 {
814 	return (back->fofs + back->len == front->fofs &&
815 			back->blk + back->len == front->blk);
816 }
817 
__is_back_mergeable(struct extent_info * cur,struct extent_info * back)818 static inline bool __is_back_mergeable(struct extent_info *cur,
819 						struct extent_info *back)
820 {
821 	return __is_extent_mergeable(back, cur);
822 }
823 
__is_front_mergeable(struct extent_info * cur,struct extent_info * front)824 static inline bool __is_front_mergeable(struct extent_info *cur,
825 						struct extent_info *front)
826 {
827 	return __is_extent_mergeable(cur, front);
828 }
829 
830 extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
__try_update_largest_extent(struct extent_tree * et,struct extent_node * en)831 static inline void __try_update_largest_extent(struct extent_tree *et,
832 						struct extent_node *en)
833 {
834 	if (en->ei.len > et->largest.len) {
835 		et->largest = en->ei;
836 		et->largest_updated = true;
837 	}
838 }
839 
840 /*
841  * For free nid management
842  */
843 enum nid_state {
844 	FREE_NID,		/* newly added to free nid list */
845 	PREALLOC_NID,		/* it is preallocated */
846 	MAX_NID_STATE,
847 };
848 
849 enum nat_state {
850 	TOTAL_NAT,
851 	DIRTY_NAT,
852 	RECLAIMABLE_NAT,
853 	MAX_NAT_STATE,
854 };
855 
856 struct f2fs_nm_info {
857 	block_t nat_blkaddr;		/* base disk address of NAT */
858 	nid_t max_nid;			/* maximum possible node ids */
859 	nid_t available_nids;		/* # of available node ids */
860 	nid_t next_scan_nid;		/* the next nid to be scanned */
861 	unsigned int ram_thresh;	/* control the memory footprint */
862 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
863 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
864 
865 	/* NAT cache management */
866 	struct radix_tree_root nat_root;/* root of the nat entry cache */
867 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
868 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
869 	struct list_head nat_entries;	/* cached nat entry list (clean) */
870 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
871 	unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */
872 	unsigned int nat_blocks;	/* # of nat blocks */
873 
874 	/* free node ids management */
875 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
876 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
877 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
878 	spinlock_t nid_list_lock;	/* protect nid lists ops */
879 	struct mutex build_lock;	/* lock for build free nids */
880 	unsigned char **free_nid_bitmap;
881 	unsigned char *nat_block_bitmap;
882 	unsigned short *free_nid_count;	/* free nid count of NAT block */
883 
884 	/* for checkpoint */
885 	char *nat_bitmap;		/* NAT bitmap pointer */
886 
887 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
888 	unsigned char *nat_bits;	/* NAT bits blocks */
889 	unsigned char *full_nat_bits;	/* full NAT pages */
890 	unsigned char *empty_nat_bits;	/* empty NAT pages */
891 #ifdef CONFIG_F2FS_CHECK_FS
892 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
893 #endif
894 	int bitmap_size;		/* bitmap size */
895 };
896 
897 /*
898  * this structure is used as one of function parameters.
899  * all the information are dedicated to a given direct node block determined
900  * by the data offset in a file.
901  */
902 struct dnode_of_data {
903 	struct inode *inode;		/* vfs inode pointer */
904 	struct page *inode_page;	/* its inode page, NULL is possible */
905 	struct page *node_page;		/* cached direct node page */
906 	nid_t nid;			/* node id of the direct node block */
907 	unsigned int ofs_in_node;	/* data offset in the node page */
908 	bool inode_page_locked;		/* inode page is locked or not */
909 	bool node_changed;		/* is node block changed */
910 	char cur_level;			/* level of hole node page */
911 	char max_level;			/* level of current page located */
912 	block_t	data_blkaddr;		/* block address of the node block */
913 };
914 
set_new_dnode(struct dnode_of_data * dn,struct inode * inode,struct page * ipage,struct page * npage,nid_t nid)915 static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
916 		struct page *ipage, struct page *npage, nid_t nid)
917 {
918 	memset(dn, 0, sizeof(*dn));
919 	dn->inode = inode;
920 	dn->inode_page = ipage;
921 	dn->node_page = npage;
922 	dn->nid = nid;
923 }
924 
925 /*
926  * For SIT manager
927  *
928  * By default, there are 6 active log areas across the whole main area.
929  * When considering hot and cold data separation to reduce cleaning overhead,
930  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
931  * respectively.
932  * In the current design, you should not change the numbers intentionally.
933  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
934  * logs individually according to the underlying devices. (default: 6)
935  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
936  * data and 8 for node logs.
937  */
938 #define	NR_CURSEG_DATA_TYPE	(3)
939 #define NR_CURSEG_NODE_TYPE	(3)
940 #define NR_CURSEG_INMEM_TYPE	(2)
941 #define NR_CURSEG_PERSIST_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
942 #define NR_CURSEG_TYPE		(NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE)
943 
944 enum {
945 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
946 	CURSEG_WARM_DATA,	/* data blocks */
947 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
948 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
949 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
950 	CURSEG_COLD_NODE,	/* indirect node blocks */
951 	NR_PERSISTENT_LOG,	/* number of persistent log */
952 	CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG,
953 				/* pinned file that needs consecutive block address */
954 	CURSEG_ALL_DATA_ATGC,	/* SSR alloctor in hot/warm/cold data area */
955 	NO_CHECK_TYPE,		/* number of persistent & inmem log */
956 };
957 
958 struct flush_cmd {
959 	struct completion wait;
960 	struct llist_node llnode;
961 	nid_t ino;
962 	int ret;
963 };
964 
965 struct flush_cmd_control {
966 	struct task_struct *f2fs_issue_flush;	/* flush thread */
967 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
968 	atomic_t issued_flush;			/* # of issued flushes */
969 	atomic_t queued_flush;			/* # of queued flushes */
970 	struct llist_head issue_list;		/* list for command issue */
971 	struct llist_node *dispatch_list;	/* list for command dispatch */
972 };
973 
974 struct f2fs_sm_info {
975 	struct sit_info *sit_info;		/* whole segment information */
976 	struct free_segmap_info *free_info;	/* free segment information */
977 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
978 	struct curseg_info *curseg_array;	/* active segment information */
979 
980 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
981 
982 	block_t seg0_blkaddr;		/* block address of 0'th segment */
983 	block_t main_blkaddr;		/* start block address of main area */
984 	block_t ssa_blkaddr;		/* start block address of SSA area */
985 
986 	unsigned int segment_count;	/* total # of segments */
987 	unsigned int main_segments;	/* # of segments in main area */
988 	unsigned int reserved_segments;	/* # of reserved segments */
989 	unsigned int additional_reserved_segments;/* reserved segs for IO align feature */
990 	unsigned int ovp_segments;	/* # of overprovision segments */
991 
992 	/* a threshold to reclaim prefree segments */
993 	unsigned int rec_prefree_segments;
994 
995 	/* for batched trimming */
996 	unsigned int trim_sections;		/* # of sections to trim */
997 
998 	struct list_head sit_entry_set;	/* sit entry set list */
999 
1000 	unsigned int ipu_policy;	/* in-place-update policy */
1001 	unsigned int min_ipu_util;	/* in-place-update threshold */
1002 	unsigned int min_fsync_blocks;	/* threshold for fsync */
1003 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
1004 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
1005 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
1006 
1007 	/* for flush command control */
1008 	struct flush_cmd_control *fcc_info;
1009 
1010 	/* for discard command control */
1011 	struct discard_cmd_control *dcc_info;
1012 };
1013 
1014 /*
1015  * For superblock
1016  */
1017 /*
1018  * COUNT_TYPE for monitoring
1019  *
1020  * f2fs monitors the number of several block types such as on-writeback,
1021  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
1022  */
1023 #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
1024 enum count_type {
1025 	F2FS_DIRTY_DENTS,
1026 	F2FS_DIRTY_DATA,
1027 	F2FS_DIRTY_QDATA,
1028 	F2FS_DIRTY_NODES,
1029 	F2FS_DIRTY_META,
1030 	F2FS_INMEM_PAGES,
1031 	F2FS_DIRTY_IMETA,
1032 	F2FS_WB_CP_DATA,
1033 	F2FS_WB_DATA,
1034 	F2FS_RD_DATA,
1035 	F2FS_RD_NODE,
1036 	F2FS_RD_META,
1037 	F2FS_DIO_WRITE,
1038 	F2FS_DIO_READ,
1039 	NR_COUNT_TYPE,
1040 };
1041 
1042 /*
1043  * The below are the page types of bios used in submit_bio().
1044  * The available types are:
1045  * DATA			User data pages. It operates as async mode.
1046  * NODE			Node pages. It operates as async mode.
1047  * META			FS metadata pages such as SIT, NAT, CP.
1048  * NR_PAGE_TYPE		The number of page types.
1049  * META_FLUSH		Make sure the previous pages are written
1050  *			with waiting the bio's completion
1051  * ...			Only can be used with META.
1052  */
1053 #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
1054 enum page_type {
1055 	DATA = 0,
1056 	NODE = 1,	/* should not change this */
1057 	META,
1058 	NR_PAGE_TYPE,
1059 	META_FLUSH,
1060 	INMEM,		/* the below types are used by tracepoints only. */
1061 	INMEM_DROP,
1062 	INMEM_INVALIDATE,
1063 	INMEM_REVOKE,
1064 	IPU,
1065 	OPU,
1066 };
1067 
1068 enum temp_type {
1069 	HOT = 0,	/* must be zero for meta bio */
1070 	WARM,
1071 	COLD,
1072 	NR_TEMP_TYPE,
1073 };
1074 
1075 enum need_lock_type {
1076 	LOCK_REQ = 0,
1077 	LOCK_DONE,
1078 	LOCK_RETRY,
1079 };
1080 
1081 enum cp_reason_type {
1082 	CP_NO_NEEDED,
1083 	CP_NON_REGULAR,
1084 	CP_COMPRESSED,
1085 	CP_HARDLINK,
1086 	CP_SB_NEED_CP,
1087 	CP_WRONG_PINO,
1088 	CP_NO_SPC_ROLL,
1089 	CP_NODE_NEED_CP,
1090 	CP_FASTBOOT_MODE,
1091 	CP_SPEC_LOG_NUM,
1092 	CP_RECOVER_DIR,
1093 };
1094 
1095 enum iostat_type {
1096 	/* WRITE IO */
1097 	APP_DIRECT_IO,			/* app direct write IOs */
1098 	APP_BUFFERED_IO,		/* app buffered write IOs */
1099 	APP_WRITE_IO,			/* app write IOs */
1100 	APP_MAPPED_IO,			/* app mapped IOs */
1101 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
1102 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1103 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1104 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1105 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1106 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1107 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1108 	FS_CP_META_IO,			/* meta IOs from checkpoint */
1109 
1110 	/* READ IO */
1111 	APP_DIRECT_READ_IO,		/* app direct read IOs */
1112 	APP_BUFFERED_READ_IO,		/* app buffered read IOs */
1113 	APP_READ_IO,			/* app read IOs */
1114 	APP_MAPPED_READ_IO,		/* app mapped read IOs */
1115 	FS_DATA_READ_IO,		/* data read IOs */
1116 	FS_GDATA_READ_IO,		/* data read IOs from background gc */
1117 	FS_CDATA_READ_IO,		/* compressed data read IOs */
1118 	FS_NODE_READ_IO,		/* node read IOs */
1119 	FS_META_READ_IO,		/* meta read IOs */
1120 
1121 	/* other */
1122 	FS_DISCARD,			/* discard */
1123 	NR_IO_TYPE,
1124 };
1125 
1126 struct f2fs_io_info {
1127 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
1128 	nid_t ino;		/* inode number */
1129 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1130 	enum temp_type temp;	/* contains HOT/WARM/COLD */
1131 	int op;			/* contains REQ_OP_ */
1132 	int op_flags;		/* req_flag_bits */
1133 	block_t new_blkaddr;	/* new block address to be written */
1134 	block_t old_blkaddr;	/* old block address before Cow */
1135 	struct page *page;	/* page to be written */
1136 	struct page *encrypted_page;	/* encrypted page */
1137 	struct page *compressed_page;	/* compressed page */
1138 	struct list_head list;		/* serialize IOs */
1139 	bool submitted;		/* indicate IO submission */
1140 	int need_lock;		/* indicate we need to lock cp_rwsem */
1141 	bool in_list;		/* indicate fio is in io_list */
1142 	bool is_por;		/* indicate IO is from recovery or not */
1143 	bool retry;		/* need to reallocate block address */
1144 	int compr_blocks;	/* # of compressed block addresses */
1145 	bool encrypted;		/* indicate file is encrypted */
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_INLINE_DOTS:
2673 	case FI_PIN_FILE:
2674 		f2fs_mark_inode_dirty_sync(inode, true);
2675 	}
2676 }
2677 
set_inode_flag(struct inode * inode,int flag)2678 static inline void set_inode_flag(struct inode *inode, int flag)
2679 {
2680 	set_bit(flag, F2FS_I(inode)->flags);
2681 	__mark_inode_dirty_flag(inode, flag, true);
2682 }
2683 
is_inode_flag_set(struct inode * inode,int flag)2684 static inline int is_inode_flag_set(struct inode *inode, int flag)
2685 {
2686 	return test_bit(flag, F2FS_I(inode)->flags);
2687 }
2688 
clear_inode_flag(struct inode * inode,int flag)2689 static inline void clear_inode_flag(struct inode *inode, int flag)
2690 {
2691 	clear_bit(flag, F2FS_I(inode)->flags);
2692 	__mark_inode_dirty_flag(inode, flag, false);
2693 }
2694 
f2fs_verity_in_progress(struct inode * inode)2695 static inline bool f2fs_verity_in_progress(struct inode *inode)
2696 {
2697 	return IS_ENABLED(CONFIG_FS_VERITY) &&
2698 	       is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
2699 }
2700 
set_acl_inode(struct inode * inode,umode_t mode)2701 static inline void set_acl_inode(struct inode *inode, umode_t mode)
2702 {
2703 	F2FS_I(inode)->i_acl_mode = mode;
2704 	set_inode_flag(inode, FI_ACL_MODE);
2705 	f2fs_mark_inode_dirty_sync(inode, false);
2706 }
2707 
f2fs_i_links_write(struct inode * inode,bool inc)2708 static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2709 {
2710 	if (inc)
2711 		inc_nlink(inode);
2712 	else
2713 		drop_nlink(inode);
2714 	f2fs_mark_inode_dirty_sync(inode, true);
2715 }
2716 
f2fs_i_blocks_write(struct inode * inode,block_t diff,bool add,bool claim)2717 static inline void f2fs_i_blocks_write(struct inode *inode,
2718 					block_t diff, bool add, bool claim)
2719 {
2720 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
2721 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
2722 
2723 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
2724 	if (add) {
2725 		if (claim)
2726 			dquot_claim_block(inode, diff);
2727 		else
2728 			dquot_alloc_block_nofail(inode, diff);
2729 	} else {
2730 		dquot_free_block(inode, diff);
2731 	}
2732 
2733 	f2fs_mark_inode_dirty_sync(inode, true);
2734 	if (clean || recover)
2735 		set_inode_flag(inode, FI_AUTO_RECOVER);
2736 }
2737 
f2fs_i_size_write(struct inode * inode,loff_t i_size)2738 static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2739 {
2740 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
2741 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
2742 
2743 	if (i_size_read(inode) == i_size)
2744 		return;
2745 
2746 	i_size_write(inode, i_size);
2747 	f2fs_mark_inode_dirty_sync(inode, true);
2748 	if (clean || recover)
2749 		set_inode_flag(inode, FI_AUTO_RECOVER);
2750 }
2751 
f2fs_i_depth_write(struct inode * inode,unsigned int depth)2752 static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2753 {
2754 	F2FS_I(inode)->i_current_depth = depth;
2755 	f2fs_mark_inode_dirty_sync(inode, true);
2756 }
2757 
f2fs_i_gc_failures_write(struct inode * inode,unsigned int count)2758 static inline void f2fs_i_gc_failures_write(struct inode *inode,
2759 					unsigned int count)
2760 {
2761 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
2762 	f2fs_mark_inode_dirty_sync(inode, true);
2763 }
2764 
f2fs_i_xnid_write(struct inode * inode,nid_t xnid)2765 static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2766 {
2767 	F2FS_I(inode)->i_xattr_nid = xnid;
2768 	f2fs_mark_inode_dirty_sync(inode, true);
2769 }
2770 
f2fs_i_pino_write(struct inode * inode,nid_t pino)2771 static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2772 {
2773 	F2FS_I(inode)->i_pino = pino;
2774 	f2fs_mark_inode_dirty_sync(inode, true);
2775 }
2776 
get_inline_info(struct inode * inode,struct f2fs_inode * ri)2777 static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2778 {
2779 	struct f2fs_inode_info *fi = F2FS_I(inode);
2780 
2781 	if (ri->i_inline & F2FS_INLINE_XATTR)
2782 		set_bit(FI_INLINE_XATTR, fi->flags);
2783 	if (ri->i_inline & F2FS_INLINE_DATA)
2784 		set_bit(FI_INLINE_DATA, fi->flags);
2785 	if (ri->i_inline & F2FS_INLINE_DENTRY)
2786 		set_bit(FI_INLINE_DENTRY, fi->flags);
2787 	if (ri->i_inline & F2FS_DATA_EXIST)
2788 		set_bit(FI_DATA_EXIST, fi->flags);
2789 	if (ri->i_inline & F2FS_INLINE_DOTS)
2790 		set_bit(FI_INLINE_DOTS, fi->flags);
2791 	if (ri->i_inline & F2FS_EXTRA_ATTR)
2792 		set_bit(FI_EXTRA_ATTR, fi->flags);
2793 	if (ri->i_inline & F2FS_PIN_FILE)
2794 		set_bit(FI_PIN_FILE, fi->flags);
2795 }
2796 
set_raw_inline(struct inode * inode,struct f2fs_inode * ri)2797 static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2798 {
2799 	ri->i_inline = 0;
2800 
2801 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2802 		ri->i_inline |= F2FS_INLINE_XATTR;
2803 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
2804 		ri->i_inline |= F2FS_INLINE_DATA;
2805 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
2806 		ri->i_inline |= F2FS_INLINE_DENTRY;
2807 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2808 		ri->i_inline |= F2FS_DATA_EXIST;
2809 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2810 		ri->i_inline |= F2FS_INLINE_DOTS;
2811 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
2812 		ri->i_inline |= F2FS_EXTRA_ATTR;
2813 	if (is_inode_flag_set(inode, FI_PIN_FILE))
2814 		ri->i_inline |= F2FS_PIN_FILE;
2815 }
2816 
f2fs_has_extra_attr(struct inode * inode)2817 static inline int f2fs_has_extra_attr(struct inode *inode)
2818 {
2819 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2820 }
2821 
f2fs_has_inline_xattr(struct inode * inode)2822 static inline int f2fs_has_inline_xattr(struct inode *inode)
2823 {
2824 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2825 }
2826 
f2fs_compressed_file(struct inode * inode)2827 static inline int f2fs_compressed_file(struct inode *inode)
2828 {
2829 	return S_ISREG(inode->i_mode) &&
2830 		is_inode_flag_set(inode, FI_COMPRESSED_FILE);
2831 }
2832 
addrs_per_inode(struct inode * inode)2833 static inline unsigned int addrs_per_inode(struct inode *inode)
2834 {
2835 	unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
2836 				get_inline_xattr_addrs(inode);
2837 
2838 	if (!f2fs_compressed_file(inode))
2839 		return addrs;
2840 	return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size);
2841 }
2842 
addrs_per_block(struct inode * inode)2843 static inline unsigned int addrs_per_block(struct inode *inode)
2844 {
2845 	if (!f2fs_compressed_file(inode))
2846 		return DEF_ADDRS_PER_BLOCK;
2847 	return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, F2FS_I(inode)->i_cluster_size);
2848 }
2849 
inline_xattr_addr(struct inode * inode,struct page * page)2850 static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
2851 {
2852 	struct f2fs_inode *ri = F2FS_INODE(page);
2853 
2854 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2855 					get_inline_xattr_addrs(inode)]);
2856 }
2857 
inline_xattr_size(struct inode * inode)2858 static inline int inline_xattr_size(struct inode *inode)
2859 {
2860 	if (f2fs_has_inline_xattr(inode))
2861 		return get_inline_xattr_addrs(inode) * sizeof(__le32);
2862 	return 0;
2863 }
2864 
f2fs_has_inline_data(struct inode * inode)2865 static inline int f2fs_has_inline_data(struct inode *inode)
2866 {
2867 	return is_inode_flag_set(inode, FI_INLINE_DATA);
2868 }
2869 
f2fs_exist_data(struct inode * inode)2870 static inline int f2fs_exist_data(struct inode *inode)
2871 {
2872 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2873 }
2874 
f2fs_has_inline_dots(struct inode * inode)2875 static inline int f2fs_has_inline_dots(struct inode *inode)
2876 {
2877 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2878 }
2879 
f2fs_is_mmap_file(struct inode * inode)2880 static inline int f2fs_is_mmap_file(struct inode *inode)
2881 {
2882 	return is_inode_flag_set(inode, FI_MMAP_FILE);
2883 }
2884 
f2fs_is_pinned_file(struct inode * inode)2885 static inline bool f2fs_is_pinned_file(struct inode *inode)
2886 {
2887 	return is_inode_flag_set(inode, FI_PIN_FILE);
2888 }
2889 
f2fs_is_atomic_file(struct inode * inode)2890 static inline bool f2fs_is_atomic_file(struct inode *inode)
2891 {
2892 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
2893 }
2894 
f2fs_is_commit_atomic_write(struct inode * inode)2895 static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
2896 {
2897 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
2898 }
2899 
f2fs_is_volatile_file(struct inode * inode)2900 static inline bool f2fs_is_volatile_file(struct inode *inode)
2901 {
2902 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
2903 }
2904 
f2fs_is_first_block_written(struct inode * inode)2905 static inline bool f2fs_is_first_block_written(struct inode *inode)
2906 {
2907 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
2908 }
2909 
f2fs_is_drop_cache(struct inode * inode)2910 static inline bool f2fs_is_drop_cache(struct inode *inode)
2911 {
2912 	return is_inode_flag_set(inode, FI_DROP_CACHE);
2913 }
2914 
inline_data_addr(struct inode * inode,struct page * page)2915 static inline void *inline_data_addr(struct inode *inode, struct page *page)
2916 {
2917 	struct f2fs_inode *ri = F2FS_INODE(page);
2918 	int extra_size = get_extra_isize(inode);
2919 
2920 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
2921 }
2922 
f2fs_has_inline_dentry(struct inode * inode)2923 static inline int f2fs_has_inline_dentry(struct inode *inode)
2924 {
2925 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
2926 }
2927 
is_file(struct inode * inode,int type)2928 static inline int is_file(struct inode *inode, int type)
2929 {
2930 	return F2FS_I(inode)->i_advise & type;
2931 }
2932 
set_file(struct inode * inode,int type)2933 static inline void set_file(struct inode *inode, int type)
2934 {
2935 	F2FS_I(inode)->i_advise |= type;
2936 	f2fs_mark_inode_dirty_sync(inode, true);
2937 }
2938 
clear_file(struct inode * inode,int type)2939 static inline void clear_file(struct inode *inode, int type)
2940 {
2941 	F2FS_I(inode)->i_advise &= ~type;
2942 	f2fs_mark_inode_dirty_sync(inode, true);
2943 }
2944 
f2fs_is_time_consistent(struct inode * inode)2945 static inline bool f2fs_is_time_consistent(struct inode *inode)
2946 {
2947 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
2948 		return false;
2949 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
2950 		return false;
2951 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
2952 		return false;
2953 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
2954 						&F2FS_I(inode)->i_crtime))
2955 		return false;
2956 	return true;
2957 }
2958 
f2fs_skip_inode_update(struct inode * inode,int dsync)2959 static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
2960 {
2961 	bool ret;
2962 
2963 	if (dsync) {
2964 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2965 
2966 		spin_lock(&sbi->inode_lock[DIRTY_META]);
2967 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
2968 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
2969 		return ret;
2970 	}
2971 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
2972 			file_keep_isize(inode) ||
2973 			i_size_read(inode) & ~PAGE_MASK)
2974 		return false;
2975 
2976 	if (!f2fs_is_time_consistent(inode))
2977 		return false;
2978 
2979 	spin_lock(&F2FS_I(inode)->i_size_lock);
2980 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2981 	spin_unlock(&F2FS_I(inode)->i_size_lock);
2982 
2983 	return ret;
2984 }
2985 
f2fs_readonly(struct super_block * sb)2986 static inline bool f2fs_readonly(struct super_block *sb)
2987 {
2988 	return sb_rdonly(sb);
2989 }
2990 
f2fs_cp_error(struct f2fs_sb_info * sbi)2991 static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2992 {
2993 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2994 }
2995 
is_dot_dotdot(const u8 * name,size_t len)2996 static inline bool is_dot_dotdot(const u8 *name, size_t len)
2997 {
2998 	if (len == 1 && name[0] == '.')
2999 		return true;
3000 
3001 	if (len == 2 && name[0] == '.' && name[1] == '.')
3002 		return true;
3003 
3004 	return false;
3005 }
3006 
f2fs_may_extent_tree(struct inode * inode)3007 static inline bool f2fs_may_extent_tree(struct inode *inode)
3008 {
3009 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3010 
3011 	if (!test_opt(sbi, EXTENT_CACHE) ||
3012 			is_inode_flag_set(inode, FI_NO_EXTENT) ||
3013 			is_inode_flag_set(inode, FI_COMPRESSED_FILE))
3014 		return false;
3015 
3016 	/*
3017 	 * for recovered files during mount do not create extents
3018 	 * if shrinker is not registered.
3019 	 */
3020 	if (list_empty(&sbi->s_list))
3021 		return false;
3022 
3023 	return S_ISREG(inode->i_mode);
3024 }
3025 
f2fs_kmalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)3026 static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
3027 					size_t size, gfp_t flags)
3028 {
3029 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
3030 		f2fs_show_injection_info(sbi, FAULT_KMALLOC);
3031 		return NULL;
3032 	}
3033 
3034 	return kmalloc(size, flags);
3035 }
3036 
f2fs_kzalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)3037 static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
3038 					size_t size, gfp_t flags)
3039 {
3040 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
3041 }
3042 
f2fs_kvmalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)3043 static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
3044 					size_t size, gfp_t flags)
3045 {
3046 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
3047 		f2fs_show_injection_info(sbi, FAULT_KVMALLOC);
3048 		return NULL;
3049 	}
3050 
3051 	return kvmalloc(size, flags);
3052 }
3053 
f2fs_kvzalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)3054 static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
3055 					size_t size, gfp_t flags)
3056 {
3057 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
3058 }
3059 
get_extra_isize(struct inode * inode)3060 static inline int get_extra_isize(struct inode *inode)
3061 {
3062 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
3063 }
3064 
get_inline_xattr_addrs(struct inode * inode)3065 static inline int get_inline_xattr_addrs(struct inode *inode)
3066 {
3067 	return F2FS_I(inode)->i_inline_xattr_size;
3068 }
3069 
3070 #define f2fs_get_inode_mode(i) \
3071 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
3072 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
3073 
3074 #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
3075 	(offsetof(struct f2fs_inode, i_extra_end) -	\
3076 	offsetof(struct f2fs_inode, i_extra_isize))	\
3077 
3078 #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
3079 #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
3080 		((offsetof(typeof(*(f2fs_inode)), field) +	\
3081 		sizeof((f2fs_inode)->field))			\
3082 		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
3083 
3084 #define DEFAULT_IOSTAT_PERIOD_MS	3000
3085 #define MIN_IOSTAT_PERIOD_MS		100
3086 /* maximum period of iostat tracing is 1 day */
3087 #define MAX_IOSTAT_PERIOD_MS		8640000
3088 
f2fs_reset_iostat(struct f2fs_sb_info * sbi)3089 static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
3090 {
3091 	int i;
3092 
3093 	spin_lock(&sbi->iostat_lock);
3094 	for (i = 0; i < NR_IO_TYPE; i++) {
3095 		sbi->rw_iostat[i] = 0;
3096 		sbi->prev_rw_iostat[i] = 0;
3097 	}
3098 	spin_unlock(&sbi->iostat_lock);
3099 }
3100 
3101 extern void f2fs_record_iostat(struct f2fs_sb_info *sbi);
3102 
f2fs_update_iostat(struct f2fs_sb_info * sbi,enum iostat_type type,unsigned long long io_bytes)3103 static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
3104 			enum iostat_type type, unsigned long long io_bytes)
3105 {
3106 	if (!sbi->iostat_enable)
3107 		return;
3108 	spin_lock(&sbi->iostat_lock);
3109 	sbi->rw_iostat[type] += io_bytes;
3110 
3111 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
3112 		sbi->rw_iostat[APP_BUFFERED_IO] =
3113 			sbi->rw_iostat[APP_WRITE_IO] -
3114 			sbi->rw_iostat[APP_DIRECT_IO];
3115 
3116 	if (type == APP_READ_IO || type == APP_DIRECT_READ_IO)
3117 		sbi->rw_iostat[APP_BUFFERED_READ_IO] =
3118 			sbi->rw_iostat[APP_READ_IO] -
3119 			sbi->rw_iostat[APP_DIRECT_READ_IO];
3120 	spin_unlock(&sbi->iostat_lock);
3121 
3122 	f2fs_record_iostat(sbi);
3123 }
3124 
fs_free_space_threshold(struct f2fs_sb_info * sbi)3125 static inline block_t fs_free_space_threshold(struct f2fs_sb_info *sbi)
3126 {
3127 	return (block_t)(SM_I(sbi)->main_segments * sbi->blocks_per_seg *
3128 		FS_FREE_SPACE_PERCENT) / HUNDRED_PERCENT;
3129 }
3130 
device_free_space_threshold(struct f2fs_sb_info * sbi)3131 static inline block_t device_free_space_threshold(struct f2fs_sb_info *sbi)
3132 {
3133 	return (block_t)(SM_I(sbi)->main_segments * sbi->blocks_per_seg *
3134 		DEVICE_FREE_SPACE_PERCENT) / HUNDRED_PERCENT;
3135 }
3136 
3137 #define __is_large_section(sbi)		((sbi)->segs_per_sec > 1)
3138 
3139 #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
3140 
3141 bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3142 					block_t blkaddr, int type);
verify_blkaddr(struct f2fs_sb_info * sbi,block_t blkaddr,int type)3143 static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
3144 					block_t blkaddr, int type)
3145 {
3146 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
3147 		f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
3148 			 blkaddr, type);
3149 		f2fs_bug_on(sbi, 1);
3150 	}
3151 }
3152 
__is_valid_data_blkaddr(block_t blkaddr)3153 static inline bool __is_valid_data_blkaddr(block_t blkaddr)
3154 {
3155 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
3156 			blkaddr == COMPRESS_ADDR)
3157 		return false;
3158 	return true;
3159 }
3160 
f2fs_set_page_private(struct page * page,unsigned long data)3161 static inline void f2fs_set_page_private(struct page *page,
3162 						unsigned long data)
3163 {
3164 	if (PagePrivate(page))
3165 		return;
3166 
3167 	attach_page_private(page, (void *)data);
3168 }
3169 
f2fs_clear_page_private(struct page * page)3170 static inline void f2fs_clear_page_private(struct page *page)
3171 {
3172 	detach_page_private(page);
3173 }
3174 
3175 /*
3176  * file.c
3177  */
3178 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
3179 void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
3180 int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock);
3181 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
3182 int f2fs_truncate(struct inode *inode);
3183 int f2fs_getattr(const struct path *path, struct kstat *stat,
3184 			u32 request_mask, unsigned int flags);
3185 int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
3186 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
3187 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
3188 int f2fs_precache_extents(struct inode *inode);
3189 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
3190 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
3191 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
3192 int f2fs_pin_file_control(struct inode *inode, bool inc);
3193 
3194 /*
3195  * inode.c
3196  */
3197 void f2fs_set_inode_flags(struct inode *inode);
3198 bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
3199 void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
3200 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
3201 struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
3202 int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
3203 void f2fs_update_inode(struct inode *inode, struct page *node_page);
3204 void f2fs_update_inode_page(struct inode *inode);
3205 int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
3206 void f2fs_evict_inode(struct inode *inode);
3207 void f2fs_handle_failed_inode(struct inode *inode);
3208 
3209 /*
3210  * namei.c
3211  */
3212 int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
3213 							bool hot, bool set);
3214 struct dentry *f2fs_get_parent(struct dentry *child);
3215 
3216 /*
3217  * dir.c
3218  */
3219 unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
3220 int f2fs_init_casefolded_name(const struct inode *dir,
3221 			      struct f2fs_filename *fname);
3222 int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
3223 			int lookup, struct f2fs_filename *fname);
3224 int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
3225 			struct f2fs_filename *fname);
3226 void f2fs_free_filename(struct f2fs_filename *fname);
3227 struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
3228 			const struct f2fs_filename *fname, int *max_slots);
3229 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
3230 			unsigned int start_pos, struct fscrypt_str *fstr);
3231 void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
3232 			struct f2fs_dentry_ptr *d);
3233 struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
3234 			const struct f2fs_filename *fname, struct page *dpage);
3235 void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
3236 			unsigned int current_depth);
3237 int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
3238 void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
3239 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
3240 					 const struct f2fs_filename *fname,
3241 					 struct page **res_page);
3242 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
3243 			const struct qstr *child, struct page **res_page);
3244 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
3245 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
3246 			struct page **page);
3247 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
3248 			struct page *page, struct inode *inode);
3249 bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
3250 			  const struct f2fs_filename *fname);
3251 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
3252 			const struct fscrypt_str *name, f2fs_hash_t name_hash,
3253 			unsigned int bit_pos);
3254 int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
3255 			struct inode *inode, nid_t ino, umode_t mode);
3256 int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
3257 			struct inode *inode, nid_t ino, umode_t mode);
3258 int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
3259 			struct inode *inode, nid_t ino, umode_t mode);
3260 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
3261 			struct inode *dir, struct inode *inode);
3262 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
3263 bool f2fs_empty_dir(struct inode *dir);
3264 
f2fs_add_link(struct dentry * dentry,struct inode * inode)3265 static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
3266 {
3267 	if (fscrypt_is_nokey_name(dentry))
3268 		return -ENOKEY;
3269 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
3270 				inode, inode->i_ino, inode->i_mode);
3271 }
3272 
3273 /*
3274  * super.c
3275  */
3276 int f2fs_inode_dirtied(struct inode *inode, bool sync);
3277 void f2fs_inode_synced(struct inode *inode);
3278 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
3279 int f2fs_quota_sync(struct super_block *sb, int type);
3280 void f2fs_quota_off_umount(struct super_block *sb);
3281 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
3282 int f2fs_sync_fs(struct super_block *sb, int sync);
3283 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
3284 
3285 /*
3286  * hash.c
3287  */
3288 void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname);
3289 
3290 /*
3291  * node.c
3292  */
3293 struct dnode_of_data;
3294 struct node_info;
3295 
3296 int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
3297 bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
3298 bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
3299 void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
3300 void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
3301 void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
3302 int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
3303 bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
3304 bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
3305 int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
3306 						struct node_info *ni);
3307 pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
3308 int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
3309 int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
3310 int f2fs_truncate_xattr_node(struct inode *inode);
3311 int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
3312 					unsigned int seq_id);
3313 int f2fs_remove_inode_page(struct inode *inode);
3314 struct page *f2fs_new_inode_page(struct inode *inode);
3315 struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
3316 void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
3317 struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
3318 struct page *f2fs_get_node_page_ra(struct page *parent, int start);
3319 int f2fs_move_node_page(struct page *node_page, int gc_type);
3320 void f2fs_flush_inline_data(struct f2fs_sb_info *sbi);
3321 int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
3322 			struct writeback_control *wbc, bool atomic,
3323 			unsigned int *seq_id);
3324 int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
3325 			struct writeback_control *wbc,
3326 			bool do_balance, enum iostat_type io_type);
3327 int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
3328 bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
3329 void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
3330 void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
3331 int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
3332 int f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
3333 int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
3334 int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
3335 int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3336 			unsigned int segno, struct f2fs_summary_block *sum);
3337 int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3338 int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
3339 void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
3340 int __init f2fs_create_node_manager_caches(void);
3341 void f2fs_destroy_node_manager_caches(void);
3342 
3343 /*
3344  * segment.c
3345  */
3346 unsigned long find_rev_next_bit(const unsigned long *addr,
3347 		unsigned long size, unsigned long offset);
3348 unsigned long find_rev_next_zero_bit(const unsigned long *addr,
3349 		unsigned long size, unsigned long offset);
3350 bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
3351 void f2fs_register_inmem_page(struct inode *inode, struct page *page);
3352 void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
3353 void f2fs_drop_inmem_pages(struct inode *inode);
3354 void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
3355 int f2fs_commit_inmem_pages(struct inode *inode);
3356 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
3357 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg);
3358 int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
3359 int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
3360 int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
3361 void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
3362 void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
3363 bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
3364 void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
3365 void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
3366 bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
3367 void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
3368 					struct cp_control *cpc);
3369 void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
3370 block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
3371 int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
3372 void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
3373 int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
3374 bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno);
3375 void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi);
3376 void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi);
3377 void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi);
3378 void f2fs_get_new_segment(struct f2fs_sb_info *sbi,
3379 			unsigned int *newseg, bool new_sec, int dir);
3380 void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
3381 					unsigned int start, unsigned int end);
3382 void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type);
3383 void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3384 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
3385 bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
3386 					struct cp_control *cpc);
3387 struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
3388 void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
3389 					block_t blk_addr);
3390 void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3391 						enum iostat_type io_type);
3392 void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
3393 void f2fs_outplace_write_data(struct dnode_of_data *dn,
3394 			struct f2fs_io_info *fio);
3395 int f2fs_inplace_write_data(struct f2fs_io_info *fio);
3396 void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3397 			block_t old_blkaddr, block_t new_blkaddr,
3398 			bool recover_curseg, bool recover_newaddr,
3399 			bool from_gc);
3400 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3401 			block_t old_addr, block_t new_addr,
3402 			unsigned char version, bool recover_curseg,
3403 			bool recover_newaddr);
3404 void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3405 			block_t old_blkaddr, block_t *new_blkaddr,
3406 			struct f2fs_summary *sum, int type,
3407 			struct f2fs_io_info *fio, int contig_level);
3408 void f2fs_wait_on_page_writeback(struct page *page,
3409 			enum page_type type, bool ordered, bool locked);
3410 void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
3411 void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
3412 								block_t len);
3413 void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
3414 void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
3415 int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3416 			unsigned int val, int alloc);
3417 void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3418 int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi);
3419 int f2fs_check_write_pointer(struct f2fs_sb_info *sbi);
3420 int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
3421 void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
3422 int __init f2fs_create_segment_manager_caches(void);
3423 void f2fs_destroy_segment_manager_caches(void);
3424 int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
3425 enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
3426 			enum page_type type, enum temp_type temp);
3427 unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
3428 			unsigned int segno);
3429 unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
3430 			unsigned int segno);
3431 
3432 /*
3433  * checkpoint.c
3434  */
3435 void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
3436 struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
3437 struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
3438 struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index);
3439 struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3440 bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3441 					block_t blkaddr, int type);
3442 int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3443 			int type, bool sync);
3444 void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
3445 long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3446 			long nr_to_write, enum iostat_type io_type);
3447 void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
3448 void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
3449 void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
3450 bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
3451 void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
3452 					unsigned int devidx, int type);
3453 bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
3454 					unsigned int devidx, int type);
3455 int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
3456 int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
3457 void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
3458 void f2fs_add_orphan_inode(struct inode *inode);
3459 void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
3460 int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
3461 int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
3462 void f2fs_update_dirty_page(struct inode *inode, struct page *page);
3463 void f2fs_remove_dirty_inode(struct inode *inode);
3464 int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type,
3465 								bool from_cp);
3466 void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
3467 int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3468 void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
3469 int __init f2fs_create_checkpoint_caches(void);
3470 void f2fs_destroy_checkpoint_caches(void);
3471 
3472 /*
3473  * data.c
3474  */
3475 int __init f2fs_init_bioset(void);
3476 void f2fs_destroy_bioset(void);
3477 struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi, int npages, bool noio);
3478 int f2fs_init_bio_entry_cache(void);
3479 void f2fs_destroy_bio_entry_cache(void);
3480 void f2fs_submit_bio(struct f2fs_sb_info *sbi,
3481 				struct bio *bio, enum page_type type);
3482 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3483 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3484 				struct inode *inode, struct page *page,
3485 				nid_t ino, enum page_type type);
3486 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
3487 					struct bio **bio, struct page *page);
3488 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3489 int f2fs_submit_page_bio(struct f2fs_io_info *fio);
3490 int f2fs_merge_page_bio(struct f2fs_io_info *fio);
3491 void f2fs_submit_page_write(struct f2fs_io_info *fio);
3492 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
3493 			block_t blk_addr, struct bio *bio);
3494 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
3495 void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3496 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
3497 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
3498 int f2fs_reserve_new_block(struct dnode_of_data *dn);
3499 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3500 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
3501 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
3502 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
3503 			int op_flags, bool for_write);
3504 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
3505 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3506 			bool for_write);
3507 struct page *f2fs_get_new_data_page(struct inode *inode,
3508 			struct page *ipage, pgoff_t index, bool new_i_size);
3509 int f2fs_do_write_data_page(struct f2fs_io_info *fio);
3510 void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock);
3511 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3512 			int create, int flag);
3513 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3514 			u64 start, u64 len);
3515 int f2fs_encrypt_one_page(struct f2fs_io_info *fio);
3516 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
3517 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
3518 int f2fs_write_single_data_page(struct page *page, int *submitted,
3519 				struct bio **bio, sector_t *last_block,
3520 				struct writeback_control *wbc,
3521 				enum iostat_type io_type,
3522 				int compr_blocks, bool allow_balance);
3523 void f2fs_invalidate_page(struct page *page, unsigned int offset,
3524 			unsigned int length);
3525 int f2fs_release_page(struct page *page, gfp_t wait);
3526 #ifdef CONFIG_MIGRATION
3527 int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
3528 			struct page *page, enum migrate_mode mode);
3529 #endif
3530 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
3531 void f2fs_clear_page_cache_dirty_tag(struct page *page);
3532 int f2fs_init_post_read_processing(void);
3533 void f2fs_destroy_post_read_processing(void);
3534 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi);
3535 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi);
3536 
3537 /*
3538  * gc.c
3539  */
3540 int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
3541 void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
3542 block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
3543 int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background, bool force,
3544 			unsigned int segno);
3545 void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
3546 int f2fs_resize_fs(struct file *filp, __u64 block_count);
3547 int __init f2fs_create_garbage_collection_cache(void);
3548 void f2fs_destroy_garbage_collection_cache(void);
3549 
3550 /*
3551  * recovery.c
3552  */
3553 int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
3554 bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
3555 int __init f2fs_create_recovery_cache(void);
3556 void f2fs_destroy_recovery_cache(void);
3557 
3558 /*
3559  * debug.c
3560  */
3561 #ifdef CONFIG_F2FS_STAT_FS
3562 struct f2fs_stat_info {
3563 	struct list_head stat_list;
3564 	struct f2fs_sb_info *sbi;
3565 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
3566 	int main_area_segs, main_area_sections, main_area_zones;
3567 	unsigned long long hit_largest, hit_cached, hit_rbtree;
3568 	unsigned long long hit_total, total_ext;
3569 	int ext_tree, zombie_tree, ext_node;
3570 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
3571 	int ndirty_data, ndirty_qdata;
3572 	int inmem_pages;
3573 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
3574 	int nats, dirty_nats, sits, dirty_sits;
3575 	int free_nids, avail_nids, alloc_nids;
3576 	int total_count, utilization;
3577 	int bg_gc, nr_wb_cp_data, nr_wb_data;
3578 	int nr_rd_data, nr_rd_node, nr_rd_meta;
3579 	int nr_dio_read, nr_dio_write;
3580 	unsigned int io_skip_bggc, other_skip_bggc;
3581 	int nr_flushing, nr_flushed, flush_list_empty;
3582 	int nr_discarding, nr_discarded;
3583 	int nr_discard_cmd;
3584 	unsigned int undiscard_blks;
3585 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3586 	int compr_inode;
3587 	unsigned long long compr_blocks;
3588 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3589 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
3590 	unsigned int bimodal, avg_vblocks;
3591 	int util_free, util_valid, util_invalid;
3592 	int rsvd_segs, overp_segs;
3593 	int dirty_count, node_pages, meta_pages;
3594 	int prefree_count, call_count, cp_count, bg_cp_count;
3595 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3596 	int bg_node_segs, bg_data_segs;
3597 	int tot_blks, data_blks, node_blks;
3598 	int bg_data_blks, bg_node_blks;
3599 	unsigned long long skipped_atomic_files[2];
3600 	int curseg[NR_CURSEG_TYPE];
3601 	int cursec[NR_CURSEG_TYPE];
3602 	int curzone[NR_CURSEG_TYPE];
3603 	unsigned int dirty_seg[NR_CURSEG_TYPE];
3604 	unsigned int full_seg[NR_CURSEG_TYPE];
3605 	unsigned int valid_blks[NR_CURSEG_TYPE];
3606 
3607 	unsigned int meta_count[META_MAX];
3608 	unsigned int segment_count[2];
3609 	unsigned int block_count[2];
3610 	unsigned int inplace_count;
3611 	unsigned long long base_mem, cache_mem, page_mem;
3612 };
3613 
F2FS_STAT(struct f2fs_sb_info * sbi)3614 static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3615 {
3616 	return (struct f2fs_stat_info *)sbi->stat_info;
3617 }
3618 
3619 #define stat_inc_cp_count(si)		((si)->cp_count++)
3620 #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
3621 #define stat_inc_call_count(si)		((si)->call_count++)
3622 #define stat_inc_bggc_count(si)		((si)->bg_gc++)
3623 #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3624 #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
3625 #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
3626 #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
3627 #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
3628 #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
3629 #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
3630 #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3631 #define stat_inc_inline_xattr(inode)					\
3632 	do {								\
3633 		if (f2fs_has_inline_xattr(inode))			\
3634 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3635 	} while (0)
3636 #define stat_dec_inline_xattr(inode)					\
3637 	do {								\
3638 		if (f2fs_has_inline_xattr(inode))			\
3639 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3640 	} while (0)
3641 #define stat_inc_inline_inode(inode)					\
3642 	do {								\
3643 		if (f2fs_has_inline_data(inode))			\
3644 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
3645 	} while (0)
3646 #define stat_dec_inline_inode(inode)					\
3647 	do {								\
3648 		if (f2fs_has_inline_data(inode))			\
3649 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
3650 	} while (0)
3651 #define stat_inc_inline_dir(inode)					\
3652 	do {								\
3653 		if (f2fs_has_inline_dentry(inode))			\
3654 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
3655 	} while (0)
3656 #define stat_dec_inline_dir(inode)					\
3657 	do {								\
3658 		if (f2fs_has_inline_dentry(inode))			\
3659 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
3660 	} while (0)
3661 #define stat_inc_compr_inode(inode)					\
3662 	do {								\
3663 		if (f2fs_compressed_file(inode))			\
3664 			(atomic_inc(&F2FS_I_SB(inode)->compr_inode));	\
3665 	} while (0)
3666 #define stat_dec_compr_inode(inode)					\
3667 	do {								\
3668 		if (f2fs_compressed_file(inode))			\
3669 			(atomic_dec(&F2FS_I_SB(inode)->compr_inode));	\
3670 	} while (0)
3671 #define stat_add_compr_blocks(inode, blocks)				\
3672 		(atomic64_add(blocks, &F2FS_I_SB(inode)->compr_blocks))
3673 #define stat_sub_compr_blocks(inode, blocks)				\
3674 		(atomic64_sub(blocks, &F2FS_I_SB(inode)->compr_blocks))
3675 #define stat_inc_meta_count(sbi, blkaddr)				\
3676 	do {								\
3677 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3678 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3679 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3680 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3681 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3682 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3683 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
3684 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
3685 	} while (0)
3686 #define stat_inc_seg_type(sbi, curseg)					\
3687 		((sbi)->segment_count[(curseg)->alloc_type]++)
3688 #define stat_inc_block_count(sbi, curseg)				\
3689 		((sbi)->block_count[(curseg)->alloc_type]++)
3690 #define stat_inc_inplace_blocks(sbi)					\
3691 		(atomic_inc(&(sbi)->inplace_count))
3692 #define stat_update_max_atomic_write(inode)				\
3693 	do {								\
3694 		int cur = F2FS_I_SB(inode)->atomic_files;	\
3695 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
3696 		if (cur > max)						\
3697 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
3698 	} while (0)
3699 #define stat_inc_volatile_write(inode)					\
3700 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3701 #define stat_dec_volatile_write(inode)					\
3702 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3703 #define stat_update_max_volatile_write(inode)				\
3704 	do {								\
3705 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3706 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3707 		if (cur > max)						\
3708 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3709 	} while (0)
3710 #define stat_inc_seg_count(sbi, type, gc_type)				\
3711 	do {								\
3712 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
3713 		si->tot_segs++;						\
3714 		if ((type) == SUM_TYPE_DATA) {				\
3715 			si->data_segs++;				\
3716 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3717 		} else {						\
3718 			si->node_segs++;				\
3719 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3720 		}							\
3721 	} while (0)
3722 
3723 #define stat_inc_tot_blk_count(si, blks)				\
3724 	((si)->tot_blks += (blks))
3725 
3726 #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
3727 	do {								\
3728 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
3729 		stat_inc_tot_blk_count(si, blks);			\
3730 		si->data_blks += (blks);				\
3731 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
3732 	} while (0)
3733 
3734 #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
3735 	do {								\
3736 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
3737 		stat_inc_tot_blk_count(si, blks);			\
3738 		si->node_blks += (blks);				\
3739 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
3740 	} while (0)
3741 
3742 int f2fs_build_stats(struct f2fs_sb_info *sbi);
3743 void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
3744 void __init f2fs_create_root_stats(void);
3745 void f2fs_destroy_root_stats(void);
3746 void f2fs_update_sit_info(struct f2fs_sb_info *sbi);
3747 #else
3748 #define stat_inc_cp_count(si)				do { } while (0)
3749 #define stat_inc_bg_cp_count(si)			do { } while (0)
3750 #define stat_inc_call_count(si)				do { } while (0)
3751 #define stat_inc_bggc_count(si)				do { } while (0)
3752 #define stat_io_skip_bggc_count(sbi)			do { } while (0)
3753 #define stat_other_skip_bggc_count(sbi)			do { } while (0)
3754 #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3755 #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3756 #define stat_inc_total_hit(sbi)				do { } while (0)
3757 #define stat_inc_rbtree_node_hit(sbi)			do { } while (0)
3758 #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3759 #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3760 #define stat_inc_inline_xattr(inode)			do { } while (0)
3761 #define stat_dec_inline_xattr(inode)			do { } while (0)
3762 #define stat_inc_inline_inode(inode)			do { } while (0)
3763 #define stat_dec_inline_inode(inode)			do { } while (0)
3764 #define stat_inc_inline_dir(inode)			do { } while (0)
3765 #define stat_dec_inline_dir(inode)			do { } while (0)
3766 #define stat_inc_compr_inode(inode)			do { } while (0)
3767 #define stat_dec_compr_inode(inode)			do { } while (0)
3768 #define stat_add_compr_blocks(inode, blocks)		do { } while (0)
3769 #define stat_sub_compr_blocks(inode, blocks)		do { } while (0)
3770 #define stat_inc_atomic_write(inode)			do { } while (0)
3771 #define stat_dec_atomic_write(inode)			do { } while (0)
3772 #define stat_update_max_atomic_write(inode)		do { } while (0)
3773 #define stat_inc_volatile_write(inode)			do { } while (0)
3774 #define stat_dec_volatile_write(inode)			do { } while (0)
3775 #define stat_update_max_volatile_write(inode)		do { } while (0)
3776 #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
3777 #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3778 #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3779 #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3780 #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3781 #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3782 #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3783 #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
3784 
f2fs_build_stats(struct f2fs_sb_info * sbi)3785 static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
f2fs_destroy_stats(struct f2fs_sb_info * sbi)3786 static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
f2fs_create_root_stats(void)3787 static inline void __init f2fs_create_root_stats(void) { }
f2fs_destroy_root_stats(void)3788 static inline void f2fs_destroy_root_stats(void) { }
f2fs_update_sit_info(struct f2fs_sb_info * sbi)3789 static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {}
3790 #endif
3791 
3792 extern const struct file_operations f2fs_dir_operations;
3793 #ifdef CONFIG_UNICODE
3794 extern const struct dentry_operations f2fs_dentry_ops;
3795 #endif
3796 extern const struct file_operations f2fs_file_operations;
3797 extern const struct inode_operations f2fs_file_inode_operations;
3798 extern const struct address_space_operations f2fs_dblock_aops;
3799 extern const struct address_space_operations f2fs_node_aops;
3800 extern const struct address_space_operations f2fs_meta_aops;
3801 extern const struct inode_operations f2fs_dir_inode_operations;
3802 extern const struct inode_operations f2fs_symlink_inode_operations;
3803 extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
3804 extern const struct inode_operations f2fs_special_inode_operations;
3805 extern struct kmem_cache *f2fs_inode_entry_slab;
3806 
3807 /*
3808  * inline.c
3809  */
3810 bool f2fs_may_inline_data(struct inode *inode);
3811 bool f2fs_sanity_check_inline_data(struct inode *inode);
3812 bool f2fs_may_inline_dentry(struct inode *inode);
3813 void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
3814 void f2fs_truncate_inline_inode(struct inode *inode,
3815 						struct page *ipage, u64 from);
3816 int f2fs_read_inline_data(struct inode *inode, struct page *page);
3817 int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3818 int f2fs_convert_inline_inode(struct inode *inode);
3819 int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
3820 int f2fs_write_inline_data(struct inode *inode, struct page *page);
3821 int f2fs_recover_inline_data(struct inode *inode, struct page *npage);
3822 struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
3823 					const struct f2fs_filename *fname,
3824 					struct page **res_page);
3825 int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
3826 			struct page *ipage);
3827 int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
3828 			struct inode *inode, nid_t ino, umode_t mode);
3829 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
3830 				struct page *page, struct inode *dir,
3831 				struct inode *inode);
3832 bool f2fs_empty_inline_dir(struct inode *dir);
3833 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3834 			struct fscrypt_str *fstr);
3835 int f2fs_inline_data_fiemap(struct inode *inode,
3836 			struct fiemap_extent_info *fieinfo,
3837 			__u64 start, __u64 len);
3838 
3839 /*
3840  * shrinker.c
3841  */
3842 unsigned long f2fs_shrink_count(struct shrinker *shrink,
3843 			struct shrink_control *sc);
3844 unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3845 			struct shrink_control *sc);
3846 void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3847 void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
3848 
3849 /*
3850  * extent_cache.c
3851  */
3852 struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
3853 				struct rb_entry *cached_re, unsigned int ofs);
3854 struct rb_node **f2fs_lookup_rb_tree_ext(struct f2fs_sb_info *sbi,
3855 				struct rb_root_cached *root,
3856 				struct rb_node **parent,
3857 				unsigned long long key, bool *left_most);
3858 struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
3859 				struct rb_root_cached *root,
3860 				struct rb_node **parent,
3861 				unsigned int ofs, bool *leftmost);
3862 struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
3863 		struct rb_entry *cached_re, unsigned int ofs,
3864 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3865 		struct rb_node ***insert_p, struct rb_node **insert_parent,
3866 		bool force, bool *leftmost);
3867 bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
3868 				struct rb_root_cached *root, bool check_key);
3869 unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3870 void f2fs_init_extent_tree(struct inode *inode, struct page *ipage);
3871 void f2fs_drop_extent_tree(struct inode *inode);
3872 unsigned int f2fs_destroy_extent_node(struct inode *inode);
3873 void f2fs_destroy_extent_tree(struct inode *inode);
3874 bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3875 			struct extent_info *ei);
3876 void f2fs_update_extent_cache(struct dnode_of_data *dn);
3877 void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3878 			pgoff_t fofs, block_t blkaddr, unsigned int len);
3879 void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
3880 int __init f2fs_create_extent_cache(void);
3881 void f2fs_destroy_extent_cache(void);
3882 
3883 /*
3884  * sysfs.c
3885  */
3886 int __init f2fs_init_sysfs(void);
3887 void f2fs_exit_sysfs(void);
3888 int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3889 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
3890 
3891 /* verity.c */
3892 extern const struct fsverity_operations f2fs_verityops;
3893 
3894 /*
3895  * crypto support
3896  */
f2fs_encrypted_file(struct inode * inode)3897 static inline bool f2fs_encrypted_file(struct inode *inode)
3898 {
3899 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
3900 }
3901 
f2fs_set_encrypted_inode(struct inode * inode)3902 static inline void f2fs_set_encrypted_inode(struct inode *inode)
3903 {
3904 #ifdef CONFIG_FS_ENCRYPTION
3905 	file_set_encrypt(inode);
3906 	f2fs_set_inode_flags(inode);
3907 #endif
3908 }
3909 
3910 /*
3911  * Returns true if the reads of the inode's data need to undergo some
3912  * postprocessing step, like decryption or authenticity verification.
3913  */
f2fs_post_read_required(struct inode * inode)3914 static inline bool f2fs_post_read_required(struct inode *inode)
3915 {
3916 	return f2fs_encrypted_file(inode) || fsverity_active(inode) ||
3917 		f2fs_compressed_file(inode);
3918 }
3919 
3920 /*
3921  * compress.c
3922  */
3923 #ifdef CONFIG_F2FS_FS_COMPRESSION
3924 bool f2fs_is_compressed_page(struct page *page);
3925 struct page *f2fs_compress_control_page(struct page *page);
3926 int f2fs_prepare_compress_overwrite(struct inode *inode,
3927 			struct page **pagep, pgoff_t index, void **fsdata);
3928 bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
3929 					pgoff_t index, unsigned copied);
3930 int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
3931 void f2fs_compress_write_end_io(struct bio *bio, struct page *page);
3932 bool f2fs_is_compress_backend_ready(struct inode *inode);
3933 int f2fs_init_compress_mempool(void);
3934 void f2fs_destroy_compress_mempool(void);
3935 void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity);
3936 bool f2fs_cluster_is_empty(struct compress_ctx *cc);
3937 bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
3938 void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page);
3939 int f2fs_write_multi_pages(struct compress_ctx *cc,
3940 						int *submitted,
3941 						struct writeback_control *wbc,
3942 						enum iostat_type io_type);
3943 int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index);
3944 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
3945 				unsigned nr_pages, sector_t *last_block_in_bio,
3946 				bool is_readahead, bool for_write);
3947 struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc);
3948 void f2fs_free_dic(struct decompress_io_ctx *dic);
3949 void f2fs_decompress_end_io(struct page **rpages,
3950 			unsigned int cluster_size, bool err, bool verity);
3951 int f2fs_init_compress_ctx(struct compress_ctx *cc);
3952 void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse);
3953 void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
3954 int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi);
3955 void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
3956 int __init f2fs_init_compress_cache(void);
3957 void f2fs_destroy_compress_cache(void);
3958 #else
f2fs_is_compressed_page(struct page * page)3959 static inline bool f2fs_is_compressed_page(struct page *page) { return false; }
f2fs_is_compress_backend_ready(struct inode * inode)3960 static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
3961 {
3962 	if (!f2fs_compressed_file(inode))
3963 		return true;
3964 	/* not support compression */
3965 	return false;
3966 }
f2fs_compress_control_page(struct page * page)3967 static inline struct page *f2fs_compress_control_page(struct page *page)
3968 {
3969 	WARN_ON_ONCE(1);
3970 	return ERR_PTR(-EINVAL);
3971 }
f2fs_init_compress_mempool(void)3972 static inline int f2fs_init_compress_mempool(void) { return 0; }
f2fs_destroy_compress_mempool(void)3973 static inline void f2fs_destroy_compress_mempool(void) { }
f2fs_init_page_array_cache(struct f2fs_sb_info * sbi)3974 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)3975 static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
f2fs_init_compress_cache(void)3976 static inline int __init f2fs_init_compress_cache(void) { return 0; }
f2fs_destroy_compress_cache(void)3977 static inline void f2fs_destroy_compress_cache(void) { }
3978 #endif
3979 
set_compress_context(struct inode * inode)3980 static inline void set_compress_context(struct inode *inode)
3981 {
3982 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3983 
3984 	F2FS_I(inode)->i_compress_algorithm =
3985 			F2FS_OPTION(sbi).compress_algorithm;
3986 	F2FS_I(inode)->i_log_cluster_size =
3987 			F2FS_OPTION(sbi).compress_log_size;
3988 	F2FS_I(inode)->i_cluster_size =
3989 			1 << F2FS_I(inode)->i_log_cluster_size;
3990 	F2FS_I(inode)->i_flags |= F2FS_COMPR_FL;
3991 	set_inode_flag(inode, FI_COMPRESSED_FILE);
3992 	stat_inc_compr_inode(inode);
3993 	f2fs_mark_inode_dirty_sync(inode, true);
3994 }
3995 
f2fs_disable_compressed_file(struct inode * inode)3996 static inline bool f2fs_disable_compressed_file(struct inode *inode)
3997 {
3998 	struct f2fs_inode_info *fi = F2FS_I(inode);
3999 
4000 	if (!f2fs_compressed_file(inode))
4001 		return true;
4002 	if (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))
4003 		return false;
4004 
4005 	fi->i_flags &= ~F2FS_COMPR_FL;
4006 	stat_dec_compr_inode(inode);
4007 	clear_inode_flag(inode, FI_COMPRESSED_FILE);
4008 	f2fs_mark_inode_dirty_sync(inode, true);
4009 	return true;
4010 }
4011 
4012 #define F2FS_FEATURE_FUNCS(name, flagname) \
4013 static inline int f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
4014 { \
4015 	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
4016 }
4017 
4018 F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
4019 F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
4020 F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
4021 F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
4022 F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
4023 F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
4024 F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
4025 F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
4026 F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
4027 F2FS_FEATURE_FUNCS(verity, VERITY);
4028 F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
4029 F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
4030 F2FS_FEATURE_FUNCS(compression, COMPRESSION);
4031 
4032 #ifdef CONFIG_BLK_DEV_ZONED
f2fs_blkz_is_seq(struct f2fs_sb_info * sbi,int devi,block_t blkaddr)4033 static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
4034 				    block_t blkaddr)
4035 {
4036 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
4037 
4038 	return test_bit(zno, FDEV(devi).blkz_seq);
4039 }
4040 #endif
4041 
f2fs_hw_should_discard(struct f2fs_sb_info * sbi)4042 static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
4043 {
4044 	return f2fs_sb_has_blkzoned(sbi);
4045 }
4046 
f2fs_bdev_support_discard(struct block_device * bdev)4047 static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
4048 {
4049 	return blk_queue_discard(bdev_get_queue(bdev)) ||
4050 	       bdev_is_zoned(bdev);
4051 }
4052 
f2fs_hw_support_discard(struct f2fs_sb_info * sbi)4053 static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
4054 {
4055 	int i;
4056 
4057 	if (!f2fs_is_multi_device(sbi))
4058 		return f2fs_bdev_support_discard(sbi->sb->s_bdev);
4059 
4060 	for (i = 0; i < sbi->s_ndevs; i++)
4061 		if (f2fs_bdev_support_discard(FDEV(i).bdev))
4062 			return true;
4063 	return false;
4064 }
4065 
f2fs_realtime_discard_enable(struct f2fs_sb_info * sbi)4066 static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
4067 {
4068 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
4069 					f2fs_hw_should_discard(sbi);
4070 }
4071 
f2fs_hw_is_readonly(struct f2fs_sb_info * sbi)4072 static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
4073 {
4074 	int i;
4075 
4076 	if (!f2fs_is_multi_device(sbi))
4077 		return bdev_read_only(sbi->sb->s_bdev);
4078 
4079 	for (i = 0; i < sbi->s_ndevs; i++)
4080 		if (bdev_read_only(FDEV(i).bdev))
4081 			return true;
4082 	return false;
4083 }
4084 
f2fs_lfs_mode(struct f2fs_sb_info * sbi)4085 static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
4086 {
4087 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
4088 }
4089 
f2fs_may_compress(struct inode * inode)4090 static inline bool f2fs_may_compress(struct inode *inode)
4091 {
4092 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
4093 				f2fs_is_atomic_file(inode) ||
4094 				f2fs_is_volatile_file(inode))
4095 		return false;
4096 	return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
4097 }
4098 
f2fs_i_compr_blocks_update(struct inode * inode,u64 blocks,bool add)4099 static inline void f2fs_i_compr_blocks_update(struct inode *inode,
4100 						u64 blocks, bool add)
4101 {
4102 	int diff = F2FS_I(inode)->i_cluster_size - blocks;
4103 	struct f2fs_inode_info *fi = F2FS_I(inode);
4104 
4105 	/* don't update i_compr_blocks if saved blocks were released */
4106 	if (!add && !atomic_read(&fi->i_compr_blocks))
4107 		return;
4108 
4109 	if (add) {
4110 		atomic_add(diff, &fi->i_compr_blocks);
4111 		stat_add_compr_blocks(inode, diff);
4112 	} else {
4113 		atomic_sub(diff, &fi->i_compr_blocks);
4114 		stat_sub_compr_blocks(inode, diff);
4115 	}
4116 	f2fs_mark_inode_dirty_sync(inode, true);
4117 }
4118 
block_unaligned_IO(struct inode * inode,struct kiocb * iocb,struct iov_iter * iter)4119 static inline int block_unaligned_IO(struct inode *inode,
4120 				struct kiocb *iocb, struct iov_iter *iter)
4121 {
4122 	unsigned int i_blkbits = READ_ONCE(inode->i_blkbits);
4123 	unsigned int blocksize_mask = (1 << i_blkbits) - 1;
4124 	loff_t offset = iocb->ki_pos;
4125 	unsigned long align = offset | iov_iter_alignment(iter);
4126 
4127 	return align & blocksize_mask;
4128 }
4129 
allow_outplace_dio(struct inode * inode,struct kiocb * iocb,struct iov_iter * iter)4130 static inline int allow_outplace_dio(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 	return (f2fs_lfs_mode(sbi) && (rw == WRITE) &&
4137 				!block_unaligned_IO(inode, iocb, iter));
4138 }
4139 
f2fs_force_buffered_io(struct inode * inode,struct kiocb * iocb,struct iov_iter * iter)4140 static inline bool f2fs_force_buffered_io(struct inode *inode,
4141 				struct kiocb *iocb, struct iov_iter *iter)
4142 {
4143 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4144 	int rw = iov_iter_rw(iter);
4145 
4146 	if (f2fs_post_read_required(inode))
4147 		return true;
4148 	if (f2fs_is_multi_device(sbi))
4149 		return true;
4150 	/*
4151 	 * for blkzoned device, fallback direct IO to buffered IO, so
4152 	 * all IOs can be serialized by log-structured write.
4153 	 */
4154 	if (f2fs_sb_has_blkzoned(sbi))
4155 		return true;
4156 	if (f2fs_lfs_mode(sbi) && (rw == WRITE)) {
4157 		if (block_unaligned_IO(inode, iocb, iter))
4158 			return true;
4159 		if (F2FS_IO_ALIGNED(sbi))
4160 			return true;
4161 	}
4162 	if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED) &&
4163 					!IS_SWAPFILE(inode))
4164 		return true;
4165 
4166 	return false;
4167 }
4168 
4169 #ifdef CONFIG_F2FS_FAULT_INJECTION
4170 extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
4171 							unsigned int type);
4172 #else
4173 #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
4174 #endif
4175 
is_journalled_quota(struct f2fs_sb_info * sbi)4176 static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
4177 {
4178 #ifdef CONFIG_QUOTA
4179 	if (f2fs_sb_has_quota_ino(sbi))
4180 		return true;
4181 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
4182 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
4183 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4184 		return true;
4185 #endif
4186 	return false;
4187 }
4188 
4189 #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
4190 #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
4191 
4192 #endif /* _LINUX_F2FS_H */
4193