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