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