• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * fs/f2fs/f2fs.h
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5  *             http://www.samsung.com/
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #ifndef _LINUX_F2FS_H
12 #define _LINUX_F2FS_H
13 
14 #include <linux/types.h>
15 #include <linux/page-flags.h>
16 #include <linux/buffer_head.h>
17 #include <linux/slab.h>
18 #include <linux/crc32.h>
19 #include <linux/magic.h>
20 #include <linux/kobject.h>
21 #include <linux/sched.h>
22 #include <linux/writeback.h>
23 #include <linux/cred.h>
24 #include <linux/vmalloc.h>
25 #include <linux/bio.h>
26 #include <linux/blkdev.h>
27 #include <linux/quotaops.h>
28 #include <crypto/hash.h>
29 
30 #define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_F2FS_FS_ENCRYPTION)
31 #include <linux/fscrypt.h>
32 
33 #ifdef CONFIG_F2FS_CHECK_FS
34 #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
35 #else
36 #define f2fs_bug_on(sbi, condition)					\
37 	do {								\
38 		if (unlikely(condition)) {				\
39 			WARN_ON(1);					\
40 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
41 		}							\
42 	} while (0)
43 #endif
44 
45 #ifdef CONFIG_F2FS_FAULT_INJECTION
46 enum {
47 	FAULT_KMALLOC,
48 	FAULT_KVMALLOC,
49 	FAULT_PAGE_ALLOC,
50 	FAULT_PAGE_GET,
51 	FAULT_ALLOC_BIO,
52 	FAULT_ALLOC_NID,
53 	FAULT_ORPHAN,
54 	FAULT_BLOCK,
55 	FAULT_DIR_DEPTH,
56 	FAULT_EVICT_INODE,
57 	FAULT_TRUNCATE,
58 	FAULT_IO,
59 	FAULT_CHECKPOINT,
60 	FAULT_MAX,
61 };
62 
63 struct f2fs_fault_info {
64 	atomic_t inject_ops;
65 	unsigned int inject_rate;
66 	unsigned int inject_type;
67 };
68 
69 extern char *fault_name[FAULT_MAX];
70 #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
71 #endif
72 
73 /*
74  * For mount options
75  */
76 #define F2FS_MOUNT_BG_GC		0x00000001
77 #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
78 #define F2FS_MOUNT_DISCARD		0x00000004
79 #define F2FS_MOUNT_NOHEAP		0x00000008
80 #define F2FS_MOUNT_XATTR_USER		0x00000010
81 #define F2FS_MOUNT_POSIX_ACL		0x00000020
82 #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
83 #define F2FS_MOUNT_INLINE_XATTR		0x00000080
84 #define F2FS_MOUNT_INLINE_DATA		0x00000100
85 #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
86 #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
87 #define F2FS_MOUNT_NOBARRIER		0x00000800
88 #define F2FS_MOUNT_FASTBOOT		0x00001000
89 #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
90 #define F2FS_MOUNT_FORCE_FG_GC		0x00004000
91 #define F2FS_MOUNT_DATA_FLUSH		0x00008000
92 #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
93 #define F2FS_MOUNT_ADAPTIVE		0x00020000
94 #define F2FS_MOUNT_LFS			0x00040000
95 #define F2FS_MOUNT_USRQUOTA		0x00080000
96 #define F2FS_MOUNT_GRPQUOTA		0x00100000
97 #define F2FS_MOUNT_PRJQUOTA		0x00200000
98 #define F2FS_MOUNT_QUOTA		0x00400000
99 #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00800000
100 #define F2FS_MOUNT_RESERVE_ROOT		0x01000000
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 struct f2fs_mount_info {
118 	unsigned int opt;
119 	int write_io_size_bits;		/* Write IO size bits */
120 	block_t root_reserved_blocks;	/* root reserved blocks */
121 	kuid_t s_resuid;		/* reserved blocks for uid */
122 	kgid_t s_resgid;		/* reserved blocks for gid */
123 	int active_logs;		/* # of active logs */
124 	int inline_xattr_size;		/* inline xattr size */
125 #ifdef CONFIG_F2FS_FAULT_INJECTION
126 	struct f2fs_fault_info fault_info;	/* For fault injection */
127 #endif
128 #ifdef CONFIG_QUOTA
129 	/* Names of quota files with journalled quota */
130 	char *s_qf_names[MAXQUOTAS];
131 	int s_jquota_fmt;			/* Format of quota to use */
132 #endif
133 	/* For which write hints are passed down to block layer */
134 	int whint_mode;
135 	int alloc_mode;			/* segment allocation policy */
136 	int fsync_mode;			/* fsync policy */
137 	bool test_dummy_encryption;	/* test dummy encryption */
138 };
139 
140 #define F2FS_FEATURE_ENCRYPT		0x0001
141 #define F2FS_FEATURE_BLKZONED		0x0002
142 #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
143 #define F2FS_FEATURE_EXTRA_ATTR		0x0008
144 #define F2FS_FEATURE_PRJQUOTA		0x0010
145 #define F2FS_FEATURE_INODE_CHKSUM	0x0020
146 #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
147 #define F2FS_FEATURE_QUOTA_INO		0x0080
148 #define F2FS_FEATURE_INODE_CRTIME	0x0100
149 #define F2FS_FEATURE_LOST_FOUND		0x0200
150 #define F2FS_FEATURE_VERITY		0x0400	/* reserved */
151 
152 #define F2FS_HAS_FEATURE(sb, mask)					\
153 	((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
154 #define F2FS_SET_FEATURE(sb, mask)					\
155 	(F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
156 #define F2FS_CLEAR_FEATURE(sb, mask)					\
157 	(F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
158 
159 /*
160  * Default values for user and/or group using reserved blocks
161  */
162 #define	F2FS_DEF_RESUID		0
163 #define	F2FS_DEF_RESGID		0
164 
165 /*
166  * For checkpoint manager
167  */
168 enum {
169 	NAT_BITMAP,
170 	SIT_BITMAP
171 };
172 
173 #define	CP_UMOUNT	0x00000001
174 #define	CP_FASTBOOT	0x00000002
175 #define	CP_SYNC		0x00000004
176 #define	CP_RECOVERY	0x00000008
177 #define	CP_DISCARD	0x00000010
178 #define CP_TRIMMED	0x00000020
179 
180 #define DEF_BATCHED_TRIM_SECTIONS	2048
181 #define BATCHED_TRIM_SEGMENTS(sbi)	\
182 		(GET_SEG_FROM_SEC(sbi, SM_I(sbi)->trim_sections))
183 #define BATCHED_TRIM_BLOCKS(sbi)	\
184 		(BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
185 #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
186 #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
187 #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
188 #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
189 #define DEF_CP_INTERVAL			60	/* 60 secs */
190 #define DEF_IDLE_INTERVAL		5	/* 5 secs */
191 
192 struct cp_control {
193 	int reason;
194 	__u64 trim_start;
195 	__u64 trim_end;
196 	__u64 trim_minlen;
197 };
198 
199 /*
200  * For CP/NAT/SIT/SSA readahead
201  */
202 enum {
203 	META_CP,
204 	META_NAT,
205 	META_SIT,
206 	META_SSA,
207 	META_POR,
208 };
209 
210 /* for the list of ino */
211 enum {
212 	ORPHAN_INO,		/* for orphan ino list */
213 	APPEND_INO,		/* for append ino list */
214 	UPDATE_INO,		/* for update ino list */
215 	TRANS_DIR_INO,		/* for trasactions dir ino list */
216 	FLUSH_INO,		/* for multiple device flushing */
217 	MAX_INO_ENTRY,		/* max. list */
218 };
219 
220 struct ino_entry {
221 	struct list_head list;		/* list head */
222 	nid_t ino;			/* inode number */
223 	unsigned int dirty_device;	/* dirty device bitmap */
224 };
225 
226 /* for the list of inodes to be GCed */
227 struct inode_entry {
228 	struct list_head list;	/* list head */
229 	struct inode *inode;	/* vfs inode pointer */
230 };
231 
232 /* for the bitmap indicate blocks to be discarded */
233 struct discard_entry {
234 	struct list_head list;	/* list head */
235 	block_t start_blkaddr;	/* start blockaddr of current segment */
236 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
237 };
238 
239 /* default discard granularity of inner discard thread, unit: block count */
240 #define DEFAULT_DISCARD_GRANULARITY		16
241 
242 /* max discard pend list number */
243 #define MAX_PLIST_NUM		512
244 #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
245 					(MAX_PLIST_NUM - 1) : (blk_num - 1))
246 
247 enum {
248 	D_PREP,
249 	D_SUBMIT,
250 	D_DONE,
251 };
252 
253 struct discard_info {
254 	block_t lstart;			/* logical start address */
255 	block_t len;			/* length */
256 	block_t start;			/* actual start address in dev */
257 };
258 
259 struct discard_cmd {
260 	struct rb_node rb_node;		/* rb node located in rb-tree */
261 	union {
262 		struct {
263 			block_t lstart;	/* logical start address */
264 			block_t len;	/* length */
265 			block_t start;	/* actual start address in dev */
266 		};
267 		struct discard_info di;	/* discard info */
268 
269 	};
270 	struct list_head list;		/* command list */
271 	struct completion wait;		/* compleation */
272 	struct block_device *bdev;	/* bdev */
273 	unsigned short ref;		/* reference count */
274 	unsigned char state;		/* state */
275 	int error;			/* bio error */
276 };
277 
278 enum {
279 	DPOLICY_BG,
280 	DPOLICY_FORCE,
281 	DPOLICY_FSTRIM,
282 	DPOLICY_UMOUNT,
283 	MAX_DPOLICY,
284 };
285 
286 struct discard_policy {
287 	int type;			/* type of discard */
288 	unsigned int min_interval;	/* used for candidates exist */
289 	unsigned int max_interval;	/* used for candidates not exist */
290 	unsigned int max_requests;	/* # of discards issued per round */
291 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
292 	bool io_aware;			/* issue discard in idle time */
293 	bool sync;			/* submit discard with REQ_SYNC flag */
294 	unsigned int granularity;	/* discard granularity */
295 };
296 
297 struct discard_cmd_control {
298 	struct task_struct *f2fs_issue_discard;	/* discard thread */
299 	struct list_head entry_list;		/* 4KB discard entry list */
300 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
301 	struct list_head wait_list;		/* store on-flushing entries */
302 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
303 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
304 	unsigned int discard_wake;		/* to wake up discard thread */
305 	struct mutex cmd_lock;
306 	unsigned int nr_discards;		/* # of discards in the list */
307 	unsigned int max_discards;		/* max. discards to be issued */
308 	unsigned int discard_granularity;	/* discard granularity */
309 	unsigned int undiscard_blks;		/* # of undiscard blocks */
310 	atomic_t issued_discard;		/* # of issued discard */
311 	atomic_t issing_discard;		/* # of issing discard */
312 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
313 	struct rb_root root;			/* root of discard rb-tree */
314 };
315 
316 /* for the list of fsync inodes, used only during recovery */
317 struct fsync_inode_entry {
318 	struct list_head list;	/* list head */
319 	struct inode *inode;	/* vfs inode pointer */
320 	block_t blkaddr;	/* block address locating the last fsync */
321 	block_t last_dentry;	/* block address locating the last dentry */
322 };
323 
324 #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
325 #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
326 
327 #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
328 #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
329 #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
330 #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
331 
332 #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
333 #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
334 
update_nats_in_cursum(struct f2fs_journal * journal,int i)335 static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
336 {
337 	int before = nats_in_cursum(journal);
338 
339 	journal->n_nats = cpu_to_le16(before + i);
340 	return before;
341 }
342 
update_sits_in_cursum(struct f2fs_journal * journal,int i)343 static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
344 {
345 	int before = sits_in_cursum(journal);
346 
347 	journal->n_sits = cpu_to_le16(before + i);
348 	return before;
349 }
350 
__has_cursum_space(struct f2fs_journal * journal,int size,int type)351 static inline bool __has_cursum_space(struct f2fs_journal *journal,
352 							int size, int type)
353 {
354 	if (type == NAT_JOURNAL)
355 		return size <= MAX_NAT_JENTRIES(journal);
356 	return size <= MAX_SIT_JENTRIES(journal);
357 }
358 
359 /*
360  * ioctl commands
361  */
362 #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
363 #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
364 #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
365 
366 #define F2FS_IOCTL_MAGIC		0xf5
367 #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
368 #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
369 #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
370 #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
371 #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
372 #define F2FS_IOC_GARBAGE_COLLECT	_IOW(F2FS_IOCTL_MAGIC, 6, __u32)
373 #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
374 #define F2FS_IOC_DEFRAGMENT		_IOWR(F2FS_IOCTL_MAGIC, 8,	\
375 						struct f2fs_defragment)
376 #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
377 						struct f2fs_move_range)
378 #define F2FS_IOC_FLUSH_DEVICE		_IOW(F2FS_IOCTL_MAGIC, 10,	\
379 						struct f2fs_flush_device)
380 #define F2FS_IOC_GARBAGE_COLLECT_RANGE	_IOW(F2FS_IOCTL_MAGIC, 11,	\
381 						struct f2fs_gc_range)
382 #define F2FS_IOC_GET_FEATURES		_IOR(F2FS_IOCTL_MAGIC, 12, __u32)
383 #define F2FS_IOC_SET_PIN_FILE		_IOW(F2FS_IOCTL_MAGIC, 13, __u32)
384 #define F2FS_IOC_GET_PIN_FILE		_IOR(F2FS_IOCTL_MAGIC, 14, __u32)
385 #define F2FS_IOC_PRECACHE_EXTENTS	_IO(F2FS_IOCTL_MAGIC, 15)
386 
387 #define F2FS_IOC_SET_ENCRYPTION_POLICY	FS_IOC_SET_ENCRYPTION_POLICY
388 #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
389 #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
390 
391 /*
392  * should be same as XFS_IOC_GOINGDOWN.
393  * Flags for going down operation used by FS_IOC_GOINGDOWN
394  */
395 #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
396 #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
397 #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
398 #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
399 #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
400 
401 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
402 /*
403  * ioctl commands in 32 bit emulation
404  */
405 #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
406 #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
407 #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
408 #endif
409 
410 struct f2fs_gc_range {
411 	u32 sync;
412 	u64 start;
413 	u64 len;
414 };
415 
416 struct f2fs_defragment {
417 	u64 start;
418 	u64 len;
419 };
420 
421 struct f2fs_move_range {
422 	u32 dst_fd;		/* destination fd */
423 	u64 pos_in;		/* start position in src_fd */
424 	u64 pos_out;		/* start position in dst_fd */
425 	u64 len;		/* size to move */
426 };
427 
428 struct f2fs_flush_device {
429 	u32 dev_num;		/* device number to flush */
430 	u32 segments;		/* # of segments to flush */
431 };
432 
433 /* for inline stuff */
434 #define DEF_INLINE_RESERVED_SIZE	1
435 #define DEF_MIN_INLINE_SIZE		1
436 static inline int get_extra_isize(struct inode *inode);
437 static inline int get_inline_xattr_addrs(struct inode *inode);
438 #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
439 				(CUR_ADDRS_PER_INODE(inode) -		\
440 				get_inline_xattr_addrs(inode) -	\
441 				DEF_INLINE_RESERVED_SIZE))
442 
443 /* for inline dir */
444 #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
445 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
446 				BITS_PER_BYTE + 1))
447 #define INLINE_DENTRY_BITMAP_SIZE(inode)	((NR_INLINE_DENTRY(inode) + \
448 					BITS_PER_BYTE - 1) / BITS_PER_BYTE)
449 #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
450 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
451 				NR_INLINE_DENTRY(inode) + \
452 				INLINE_DENTRY_BITMAP_SIZE(inode)))
453 
454 /*
455  * For INODE and NODE manager
456  */
457 /* for directory operations */
458 struct f2fs_dentry_ptr {
459 	struct inode *inode;
460 	void *bitmap;
461 	struct f2fs_dir_entry *dentry;
462 	__u8 (*filename)[F2FS_SLOT_LEN];
463 	int max;
464 	int nr_bitmap;
465 };
466 
make_dentry_ptr_block(struct inode * inode,struct f2fs_dentry_ptr * d,struct f2fs_dentry_block * t)467 static inline void make_dentry_ptr_block(struct inode *inode,
468 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
469 {
470 	d->inode = inode;
471 	d->max = NR_DENTRY_IN_BLOCK;
472 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
473 	d->bitmap = t->dentry_bitmap;
474 	d->dentry = t->dentry;
475 	d->filename = t->filename;
476 }
477 
make_dentry_ptr_inline(struct inode * inode,struct f2fs_dentry_ptr * d,void * t)478 static inline void make_dentry_ptr_inline(struct inode *inode,
479 					struct f2fs_dentry_ptr *d, void *t)
480 {
481 	int entry_cnt = NR_INLINE_DENTRY(inode);
482 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
483 	int reserved_size = INLINE_RESERVED_SIZE(inode);
484 
485 	d->inode = inode;
486 	d->max = entry_cnt;
487 	d->nr_bitmap = bitmap_size;
488 	d->bitmap = t;
489 	d->dentry = t + bitmap_size + reserved_size;
490 	d->filename = t + bitmap_size + reserved_size +
491 					SIZE_OF_DIR_ENTRY * entry_cnt;
492 }
493 
494 /*
495  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
496  * as its node offset to distinguish from index node blocks.
497  * But some bits are used to mark the node block.
498  */
499 #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
500 				>> OFFSET_BIT_SHIFT)
501 enum {
502 	ALLOC_NODE,			/* allocate a new node page if needed */
503 	LOOKUP_NODE,			/* look up a node without readahead */
504 	LOOKUP_NODE_RA,			/*
505 					 * look up a node with readahead called
506 					 * by get_data_block.
507 					 */
508 };
509 
510 #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
511 
512 #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
513 
514 /* vector size for gang look-up from extent cache that consists of radix tree */
515 #define EXT_TREE_VEC_SIZE	64
516 
517 /* for in-memory extent cache entry */
518 #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
519 
520 /* number of extent info in extent cache we try to shrink */
521 #define EXTENT_CACHE_SHRINK_NUMBER	128
522 
523 struct rb_entry {
524 	struct rb_node rb_node;		/* rb node located in rb-tree */
525 	unsigned int ofs;		/* start offset of the entry */
526 	unsigned int len;		/* length of the entry */
527 };
528 
529 struct extent_info {
530 	unsigned int fofs;		/* start offset in a file */
531 	unsigned int len;		/* length of the extent */
532 	u32 blk;			/* start block address of the extent */
533 };
534 
535 struct extent_node {
536 	struct rb_node rb_node;
537 	union {
538 		struct {
539 			unsigned int fofs;
540 			unsigned int len;
541 			u32 blk;
542 		};
543 		struct extent_info ei;	/* extent info */
544 
545 	};
546 	struct list_head list;		/* node in global extent list of sbi */
547 	struct extent_tree *et;		/* extent tree pointer */
548 };
549 
550 struct extent_tree {
551 	nid_t ino;			/* inode number */
552 	struct rb_root root;		/* root of extent info rb-tree */
553 	struct extent_node *cached_en;	/* recently accessed extent node */
554 	struct extent_info largest;	/* largested extent info */
555 	struct list_head list;		/* to be used by sbi->zombie_list */
556 	rwlock_t lock;			/* protect extent info rb-tree */
557 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
558 };
559 
560 /*
561  * This structure is taken from ext4_map_blocks.
562  *
563  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
564  */
565 #define F2FS_MAP_NEW		(1 << BH_New)
566 #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
567 #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
568 #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
569 				F2FS_MAP_UNWRITTEN)
570 
571 struct f2fs_map_blocks {
572 	block_t m_pblk;
573 	block_t m_lblk;
574 	unsigned int m_len;
575 	unsigned int m_flags;
576 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
577 	pgoff_t *m_next_extent;		/* point to next possible extent */
578 	int m_seg_type;
579 };
580 
581 /* for flag in get_data_block */
582 enum {
583 	F2FS_GET_BLOCK_DEFAULT,
584 	F2FS_GET_BLOCK_FIEMAP,
585 	F2FS_GET_BLOCK_BMAP,
586 	F2FS_GET_BLOCK_PRE_DIO,
587 	F2FS_GET_BLOCK_PRE_AIO,
588 	F2FS_GET_BLOCK_PRECACHE,
589 };
590 
591 /*
592  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
593  */
594 #define FADVISE_COLD_BIT	0x01
595 #define FADVISE_LOST_PINO_BIT	0x02
596 #define FADVISE_ENCRYPT_BIT	0x04
597 #define FADVISE_ENC_NAME_BIT	0x08
598 #define FADVISE_KEEP_SIZE_BIT	0x10
599 #define FADVISE_HOT_BIT		0x20
600 #define FADVISE_VERITY_BIT	0x40	/* reserved */
601 
602 #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
603 #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
604 #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
605 #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
606 #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
607 #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
608 #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
609 #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
610 #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
611 #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
612 #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
613 #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
614 #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
615 #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
616 #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
617 #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
618 
619 #define DEF_DIR_LEVEL		0
620 
621 struct f2fs_inode_info {
622 	struct inode vfs_inode;		/* serve a vfs inode */
623 	unsigned long i_flags;		/* keep an inode flags for ioctl */
624 	unsigned char i_advise;		/* use to give file attribute hints */
625 	unsigned char i_dir_level;	/* use for dentry level for large dir */
626 	union {
627 		unsigned int i_current_depth;	/* only for directory depth */
628 		unsigned short i_gc_failures;	/* only for regular file */
629 	};
630 	unsigned int i_pino;		/* parent inode number */
631 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
632 
633 	/* Use below internally in f2fs*/
634 	unsigned long flags;		/* use to pass per-file flags */
635 	struct rw_semaphore i_sem;	/* protect fi info */
636 	atomic_t dirty_pages;		/* # of dirty pages */
637 	f2fs_hash_t chash;		/* hash value of given file name */
638 	unsigned int clevel;		/* maximum level of given file name */
639 	struct task_struct *task;	/* lookup and create consistency */
640 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
641 	nid_t i_xattr_nid;		/* node id that contains xattrs */
642 	loff_t	last_disk_size;		/* lastly written file size */
643 
644 #ifdef CONFIG_QUOTA
645 	struct dquot *i_dquot[MAXQUOTAS];
646 
647 	/* quota space reservation, managed internally by quota code */
648 	qsize_t i_reserved_quota;
649 #endif
650 	struct list_head dirty_list;	/* dirty list for dirs and files */
651 	struct list_head gdirty_list;	/* linked in global dirty list */
652 	struct list_head inmem_ilist;	/* list for inmem inodes */
653 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
654 	struct task_struct *inmem_task;	/* store inmemory task */
655 	struct mutex inmem_lock;	/* lock for inmemory pages */
656 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
657 	struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
658 	struct rw_semaphore i_mmap_sem;
659 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
660 
661 	int i_extra_isize;		/* size of extra space located in i_addr */
662 	kprojid_t i_projid;		/* id for project quota */
663 	int i_inline_xattr_size;	/* inline xattr size */
664 	struct timespec i_crtime;	/* inode creation time */
665 	struct timespec i_disk_time[4];	/* inode disk times */
666 };
667 
get_extent_info(struct extent_info * ext,struct f2fs_extent * i_ext)668 static inline void get_extent_info(struct extent_info *ext,
669 					struct f2fs_extent *i_ext)
670 {
671 	ext->fofs = le32_to_cpu(i_ext->fofs);
672 	ext->blk = le32_to_cpu(i_ext->blk);
673 	ext->len = le32_to_cpu(i_ext->len);
674 }
675 
set_raw_extent(struct extent_info * ext,struct f2fs_extent * i_ext)676 static inline void set_raw_extent(struct extent_info *ext,
677 					struct f2fs_extent *i_ext)
678 {
679 	i_ext->fofs = cpu_to_le32(ext->fofs);
680 	i_ext->blk = cpu_to_le32(ext->blk);
681 	i_ext->len = cpu_to_le32(ext->len);
682 }
683 
set_extent_info(struct extent_info * ei,unsigned int fofs,u32 blk,unsigned int len)684 static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
685 						u32 blk, unsigned int len)
686 {
687 	ei->fofs = fofs;
688 	ei->blk = blk;
689 	ei->len = len;
690 }
691 
__is_discard_mergeable(struct discard_info * back,struct discard_info * front)692 static inline bool __is_discard_mergeable(struct discard_info *back,
693 						struct discard_info *front)
694 {
695 	return back->lstart + back->len == front->lstart;
696 }
697 
__is_discard_back_mergeable(struct discard_info * cur,struct discard_info * back)698 static inline bool __is_discard_back_mergeable(struct discard_info *cur,
699 						struct discard_info *back)
700 {
701 	return __is_discard_mergeable(back, cur);
702 }
703 
__is_discard_front_mergeable(struct discard_info * cur,struct discard_info * front)704 static inline bool __is_discard_front_mergeable(struct discard_info *cur,
705 						struct discard_info *front)
706 {
707 	return __is_discard_mergeable(cur, front);
708 }
709 
__is_extent_mergeable(struct extent_info * back,struct extent_info * front)710 static inline bool __is_extent_mergeable(struct extent_info *back,
711 						struct extent_info *front)
712 {
713 	return (back->fofs + back->len == front->fofs &&
714 			back->blk + back->len == front->blk);
715 }
716 
__is_back_mergeable(struct extent_info * cur,struct extent_info * back)717 static inline bool __is_back_mergeable(struct extent_info *cur,
718 						struct extent_info *back)
719 {
720 	return __is_extent_mergeable(back, cur);
721 }
722 
__is_front_mergeable(struct extent_info * cur,struct extent_info * front)723 static inline bool __is_front_mergeable(struct extent_info *cur,
724 						struct extent_info *front)
725 {
726 	return __is_extent_mergeable(cur, front);
727 }
728 
729 extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
__try_update_largest_extent(struct inode * inode,struct extent_tree * et,struct extent_node * en)730 static inline void __try_update_largest_extent(struct inode *inode,
731 			struct extent_tree *et, struct extent_node *en)
732 {
733 	if (en->ei.len > et->largest.len) {
734 		et->largest = en->ei;
735 		f2fs_mark_inode_dirty_sync(inode, true);
736 	}
737 }
738 
739 /*
740  * For free nid management
741  */
742 enum nid_state {
743 	FREE_NID,		/* newly added to free nid list */
744 	PREALLOC_NID,		/* it is preallocated */
745 	MAX_NID_STATE,
746 };
747 
748 struct f2fs_nm_info {
749 	block_t nat_blkaddr;		/* base disk address of NAT */
750 	nid_t max_nid;			/* maximum possible node ids */
751 	nid_t available_nids;		/* # of available node ids */
752 	nid_t next_scan_nid;		/* the next nid to be scanned */
753 	unsigned int ram_thresh;	/* control the memory footprint */
754 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
755 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
756 
757 	/* NAT cache management */
758 	struct radix_tree_root nat_root;/* root of the nat entry cache */
759 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
760 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
761 	struct list_head nat_entries;	/* cached nat entry list (clean) */
762 	unsigned int nat_cnt;		/* the # of cached nat entries */
763 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
764 	unsigned int nat_blocks;	/* # of nat blocks */
765 
766 	/* free node ids management */
767 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
768 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
769 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
770 	spinlock_t nid_list_lock;	/* protect nid lists ops */
771 	struct mutex build_lock;	/* lock for build free nids */
772 	unsigned char **free_nid_bitmap;
773 	unsigned char *nat_block_bitmap;
774 	unsigned short *free_nid_count;	/* free nid count of NAT block */
775 
776 	/* for checkpoint */
777 	char *nat_bitmap;		/* NAT bitmap pointer */
778 
779 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
780 	unsigned char *nat_bits;	/* NAT bits blocks */
781 	unsigned char *full_nat_bits;	/* full NAT pages */
782 	unsigned char *empty_nat_bits;	/* empty NAT pages */
783 #ifdef CONFIG_F2FS_CHECK_FS
784 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
785 #endif
786 	int bitmap_size;		/* bitmap size */
787 };
788 
789 /*
790  * this structure is used as one of function parameters.
791  * all the information are dedicated to a given direct node block determined
792  * by the data offset in a file.
793  */
794 struct dnode_of_data {
795 	struct inode *inode;		/* vfs inode pointer */
796 	struct page *inode_page;	/* its inode page, NULL is possible */
797 	struct page *node_page;		/* cached direct node page */
798 	nid_t nid;			/* node id of the direct node block */
799 	unsigned int ofs_in_node;	/* data offset in the node page */
800 	bool inode_page_locked;		/* inode page is locked or not */
801 	bool node_changed;		/* is node block changed */
802 	char cur_level;			/* level of hole node page */
803 	char max_level;			/* level of current page located */
804 	block_t	data_blkaddr;		/* block address of the node block */
805 };
806 
set_new_dnode(struct dnode_of_data * dn,struct inode * inode,struct page * ipage,struct page * npage,nid_t nid)807 static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
808 		struct page *ipage, struct page *npage, nid_t nid)
809 {
810 	memset(dn, 0, sizeof(*dn));
811 	dn->inode = inode;
812 	dn->inode_page = ipage;
813 	dn->node_page = npage;
814 	dn->nid = nid;
815 }
816 
817 /*
818  * For SIT manager
819  *
820  * By default, there are 6 active log areas across the whole main area.
821  * When considering hot and cold data separation to reduce cleaning overhead,
822  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
823  * respectively.
824  * In the current design, you should not change the numbers intentionally.
825  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
826  * logs individually according to the underlying devices. (default: 6)
827  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
828  * data and 8 for node logs.
829  */
830 #define	NR_CURSEG_DATA_TYPE	(3)
831 #define NR_CURSEG_NODE_TYPE	(3)
832 #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
833 
834 enum {
835 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
836 	CURSEG_WARM_DATA,	/* data blocks */
837 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
838 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
839 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
840 	CURSEG_COLD_NODE,	/* indirect node blocks */
841 	NO_CHECK_TYPE,
842 };
843 
844 struct flush_cmd {
845 	struct completion wait;
846 	struct llist_node llnode;
847 	nid_t ino;
848 	int ret;
849 };
850 
851 struct flush_cmd_control {
852 	struct task_struct *f2fs_issue_flush;	/* flush thread */
853 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
854 	atomic_t issued_flush;			/* # of issued flushes */
855 	atomic_t issing_flush;			/* # of issing flushes */
856 	struct llist_head issue_list;		/* list for command issue */
857 	struct llist_node *dispatch_list;	/* list for command dispatch */
858 };
859 
860 struct f2fs_sm_info {
861 	struct sit_info *sit_info;		/* whole segment information */
862 	struct free_segmap_info *free_info;	/* free segment information */
863 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
864 	struct curseg_info *curseg_array;	/* active segment information */
865 
866 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
867 
868 	block_t seg0_blkaddr;		/* block address of 0'th segment */
869 	block_t main_blkaddr;		/* start block address of main area */
870 	block_t ssa_blkaddr;		/* start block address of SSA area */
871 
872 	unsigned int segment_count;	/* total # of segments */
873 	unsigned int main_segments;	/* # of segments in main area */
874 	unsigned int reserved_segments;	/* # of reserved segments */
875 	unsigned int ovp_segments;	/* # of overprovision segments */
876 
877 	/* a threshold to reclaim prefree segments */
878 	unsigned int rec_prefree_segments;
879 
880 	/* for batched trimming */
881 	unsigned int trim_sections;		/* # of sections to trim */
882 
883 	struct list_head sit_entry_set;	/* sit entry set list */
884 
885 	unsigned int ipu_policy;	/* in-place-update policy */
886 	unsigned int min_ipu_util;	/* in-place-update threshold */
887 	unsigned int min_fsync_blocks;	/* threshold for fsync */
888 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
889 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
890 
891 	/* for flush command control */
892 	struct flush_cmd_control *fcc_info;
893 
894 	/* for discard command control */
895 	struct discard_cmd_control *dcc_info;
896 };
897 
898 /*
899  * For superblock
900  */
901 /*
902  * COUNT_TYPE for monitoring
903  *
904  * f2fs monitors the number of several block types such as on-writeback,
905  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
906  */
907 #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
908 enum count_type {
909 	F2FS_DIRTY_DENTS,
910 	F2FS_DIRTY_DATA,
911 	F2FS_DIRTY_QDATA,
912 	F2FS_DIRTY_NODES,
913 	F2FS_DIRTY_META,
914 	F2FS_INMEM_PAGES,
915 	F2FS_DIRTY_IMETA,
916 	F2FS_WB_CP_DATA,
917 	F2FS_WB_DATA,
918 	NR_COUNT_TYPE,
919 };
920 
921 /*
922  * The below are the page types of bios used in submit_bio().
923  * The available types are:
924  * DATA			User data pages. It operates as async mode.
925  * NODE			Node pages. It operates as async mode.
926  * META			FS metadata pages such as SIT, NAT, CP.
927  * NR_PAGE_TYPE		The number of page types.
928  * META_FLUSH		Make sure the previous pages are written
929  *			with waiting the bio's completion
930  * ...			Only can be used with META.
931  */
932 #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
933 enum page_type {
934 	DATA,
935 	NODE,
936 	META,
937 	NR_PAGE_TYPE,
938 	META_FLUSH,
939 	INMEM,		/* the below types are used by tracepoints only. */
940 	INMEM_DROP,
941 	INMEM_INVALIDATE,
942 	INMEM_REVOKE,
943 	IPU,
944 	OPU,
945 };
946 
947 enum temp_type {
948 	HOT = 0,	/* must be zero for meta bio */
949 	WARM,
950 	COLD,
951 	NR_TEMP_TYPE,
952 };
953 
954 enum need_lock_type {
955 	LOCK_REQ = 0,
956 	LOCK_DONE,
957 	LOCK_RETRY,
958 };
959 
960 enum cp_reason_type {
961 	CP_NO_NEEDED,
962 	CP_NON_REGULAR,
963 	CP_HARDLINK,
964 	CP_SB_NEED_CP,
965 	CP_WRONG_PINO,
966 	CP_NO_SPC_ROLL,
967 	CP_NODE_NEED_CP,
968 	CP_FASTBOOT_MODE,
969 	CP_SPEC_LOG_NUM,
970 	CP_RECOVER_DIR,
971 };
972 
973 enum iostat_type {
974 	APP_DIRECT_IO,			/* app direct IOs */
975 	APP_BUFFERED_IO,		/* app buffered IOs */
976 	APP_WRITE_IO,			/* app write IOs */
977 	APP_MAPPED_IO,			/* app mapped IOs */
978 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
979 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
980 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
981 	FS_GC_DATA_IO,			/* data IOs from forground gc */
982 	FS_GC_NODE_IO,			/* node IOs from forground gc */
983 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
984 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
985 	FS_CP_META_IO,			/* meta IOs from checkpoint */
986 	FS_DISCARD,			/* discard */
987 	NR_IO_TYPE,
988 };
989 
990 struct f2fs_io_info {
991 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
992 	nid_t ino;		/* inode number */
993 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
994 	enum temp_type temp;	/* contains HOT/WARM/COLD */
995 	int op;			/* contains REQ_OP_ */
996 	int op_flags;		/* req_flag_bits */
997 	block_t new_blkaddr;	/* new block address to be written */
998 	block_t old_blkaddr;	/* old block address before Cow */
999 	struct page *page;	/* page to be written */
1000 	struct page *encrypted_page;	/* encrypted page */
1001 	struct list_head list;		/* serialize IOs */
1002 	bool submitted;		/* indicate IO submission */
1003 	int need_lock;		/* indicate we need to lock cp_rwsem */
1004 	bool in_list;		/* indicate fio is in io_list */
1005 	bool is_meta;		/* indicate borrow meta inode mapping or not */
1006 	enum iostat_type io_type;	/* io type */
1007 	struct writeback_control *io_wbc; /* writeback control */
1008 };
1009 
1010 #define is_read_io(rw) ((rw) == READ)
1011 struct f2fs_bio_info {
1012 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
1013 	struct bio *bio;		/* bios to merge */
1014 	sector_t last_block_in_bio;	/* last block number */
1015 	struct f2fs_io_info fio;	/* store buffered io info. */
1016 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
1017 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1018 	struct list_head io_list;	/* track fios */
1019 };
1020 
1021 #define FDEV(i)				(sbi->devs[i])
1022 #define RDEV(i)				(raw_super->devs[i])
1023 struct f2fs_dev_info {
1024 	struct block_device *bdev;
1025 	char path[MAX_PATH_LEN];
1026 	unsigned int total_segments;
1027 	block_t start_blk;
1028 	block_t end_blk;
1029 #ifdef CONFIG_BLK_DEV_ZONED
1030 	unsigned int nr_blkz;			/* Total number of zones */
1031 	u8 *blkz_type;				/* Array of zones type */
1032 #endif
1033 };
1034 
1035 enum inode_type {
1036 	DIR_INODE,			/* for dirty dir inode */
1037 	FILE_INODE,			/* for dirty regular/symlink inode */
1038 	DIRTY_META,			/* for all dirtied inode metadata */
1039 	ATOMIC_FILE,			/* for all atomic files */
1040 	NR_INODE_TYPE,
1041 };
1042 
1043 /* for inner inode cache management */
1044 struct inode_management {
1045 	struct radix_tree_root ino_root;	/* ino entry array */
1046 	spinlock_t ino_lock;			/* for ino entry lock */
1047 	struct list_head ino_list;		/* inode list head */
1048 	unsigned long ino_num;			/* number of entries */
1049 };
1050 
1051 /* For s_flag in struct f2fs_sb_info */
1052 enum {
1053 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1054 	SBI_IS_CLOSE,				/* specify unmounting */
1055 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1056 	SBI_POR_DOING,				/* recovery is doing or not */
1057 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1058 	SBI_NEED_CP,				/* need to checkpoint */
1059 };
1060 
1061 enum {
1062 	CP_TIME,
1063 	REQ_TIME,
1064 	MAX_TIME,
1065 };
1066 
1067 enum {
1068 	WHINT_MODE_OFF,		/* not pass down write hints */
1069 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1070 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
1071 };
1072 
1073 enum {
1074 	ALLOC_MODE_DEFAULT,	/* stay default */
1075 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
1076 };
1077 
1078 enum fsync_mode {
1079 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
1080 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1081 };
1082 
1083 #ifdef CONFIG_F2FS_FS_ENCRYPTION
1084 #define DUMMY_ENCRYPTION_ENABLED(sbi) \
1085 			(unlikely(F2FS_OPTION(sbi).test_dummy_encryption))
1086 #else
1087 #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
1088 #endif
1089 
1090 struct f2fs_sb_info {
1091 	struct super_block *sb;			/* pointer to VFS super block */
1092 	struct proc_dir_entry *s_proc;		/* proc entry */
1093 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1094 	struct rw_semaphore sb_lock;		/* lock for raw super block */
1095 	int valid_super_block;			/* valid super block no */
1096 	unsigned long s_flag;				/* flags for sbi */
1097 
1098 #ifdef CONFIG_BLK_DEV_ZONED
1099 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1100 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1101 #endif
1102 
1103 	/* for node-related operations */
1104 	struct f2fs_nm_info *nm_info;		/* node manager */
1105 	struct inode *node_inode;		/* cache node blocks */
1106 
1107 	/* for segment-related operations */
1108 	struct f2fs_sm_info *sm_info;		/* segment manager */
1109 
1110 	/* for bio operations */
1111 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1112 	struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
1113 						/* bio ordering for NODE/DATA */
1114 	mempool_t *write_io_dummy;		/* Dummy pages */
1115 
1116 	/* for checkpoint */
1117 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
1118 	int cur_cp_pack;			/* remain current cp pack */
1119 	spinlock_t cp_lock;			/* for flag in ckpt */
1120 	struct inode *meta_inode;		/* cache meta blocks */
1121 	struct mutex cp_mutex;			/* checkpoint procedure lock */
1122 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1123 	struct rw_semaphore node_write;		/* locking node writes */
1124 	struct rw_semaphore node_change;	/* locking node change */
1125 	wait_queue_head_t cp_wait;
1126 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
1127 	long interval_time[MAX_TIME];		/* to store thresholds */
1128 
1129 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
1130 
1131 	/* for orphan inode, use 0'th array */
1132 	unsigned int max_orphans;		/* max orphan inodes */
1133 
1134 	/* for inode management */
1135 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1136 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
1137 
1138 	/* for extent tree cache */
1139 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
1140 	struct mutex extent_tree_lock;	/* locking extent radix tree */
1141 	struct list_head extent_list;		/* lru list for shrinker */
1142 	spinlock_t extent_lock;			/* locking extent lru list */
1143 	atomic_t total_ext_tree;		/* extent tree count */
1144 	struct list_head zombie_list;		/* extent zombie tree list */
1145 	atomic_t total_zombie_tree;		/* extent zombie tree count */
1146 	atomic_t total_ext_node;		/* extent info count */
1147 
1148 	/* basic filesystem units */
1149 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
1150 	unsigned int log_blocksize;		/* log2 block size */
1151 	unsigned int blocksize;			/* block size */
1152 	unsigned int root_ino_num;		/* root inode number*/
1153 	unsigned int node_ino_num;		/* node inode number*/
1154 	unsigned int meta_ino_num;		/* meta inode number*/
1155 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
1156 	unsigned int blocks_per_seg;		/* blocks per segment */
1157 	unsigned int segs_per_sec;		/* segments per section */
1158 	unsigned int secs_per_zone;		/* sections per zone */
1159 	unsigned int total_sections;		/* total section count */
1160 	unsigned int total_node_count;		/* total node block count */
1161 	unsigned int total_valid_node_count;	/* valid node block count */
1162 	loff_t max_file_blocks;			/* max block index of file */
1163 	int dir_level;				/* directory level */
1164 	unsigned int trigger_ssr_threshold;	/* threshold to trigger ssr */
1165 	int readdir_ra;				/* readahead inode in readdir */
1166 
1167 	block_t user_block_count;		/* # of user blocks */
1168 	block_t total_valid_block_count;	/* # of valid blocks */
1169 	block_t discard_blks;			/* discard command candidats */
1170 	block_t last_valid_block_count;		/* for recovery */
1171 	block_t reserved_blocks;		/* configurable reserved blocks */
1172 	block_t current_reserved_blocks;	/* current reserved blocks */
1173 
1174 	unsigned int nquota_files;		/* # of quota sysfile */
1175 
1176 	u32 s_next_generation;			/* for NFS support */
1177 
1178 	/* # of pages, see count_type */
1179 	atomic_t nr_pages[NR_COUNT_TYPE];
1180 	/* # of allocated blocks */
1181 	struct percpu_counter alloc_valid_block_count;
1182 
1183 	/* writeback control */
1184 	atomic_t wb_sync_req;			/* count # of WB_SYNC threads */
1185 
1186 	/* valid inode count */
1187 	struct percpu_counter total_valid_inode_count;
1188 
1189 	struct f2fs_mount_info mount_opt;	/* mount options */
1190 
1191 	/* for cleaning operations */
1192 	struct mutex gc_mutex;			/* mutex for GC */
1193 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
1194 	unsigned int cur_victim_sec;		/* current victim section num */
1195 
1196 	/* threshold for converting bg victims for fg */
1197 	u64 fggc_threshold;
1198 
1199 	/* threshold for gc trials on pinned files */
1200 	u64 gc_pin_file_threshold;
1201 
1202 	/* maximum # of trials to find a victim segment for SSR and GC */
1203 	unsigned int max_victim_search;
1204 
1205 	/*
1206 	 * for stat information.
1207 	 * one is for the LFS mode, and the other is for the SSR mode.
1208 	 */
1209 #ifdef CONFIG_F2FS_STAT_FS
1210 	struct f2fs_stat_info *stat_info;	/* FS status information */
1211 	unsigned int segment_count[2];		/* # of allocated segments */
1212 	unsigned int block_count[2];		/* # of allocated blocks */
1213 	atomic_t inplace_count;		/* # of inplace update */
1214 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
1215 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
1216 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
1217 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1218 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
1219 	atomic_t inline_inode;			/* # of inline_data inodes */
1220 	atomic_t inline_dir;			/* # of inline_dentry inodes */
1221 	atomic_t aw_cnt;			/* # of atomic writes */
1222 	atomic_t vw_cnt;			/* # of volatile writes */
1223 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1224 	atomic_t max_vw_cnt;			/* max # of volatile writes */
1225 	int bg_gc;				/* background gc calls */
1226 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
1227 #endif
1228 	spinlock_t stat_lock;			/* lock for stat operations */
1229 
1230 	/* For app/fs IO statistics */
1231 	spinlock_t iostat_lock;
1232 	unsigned long long write_iostat[NR_IO_TYPE];
1233 	bool iostat_enable;
1234 
1235 	/* For sysfs suppport */
1236 	struct kobject s_kobj;
1237 	struct completion s_kobj_unregister;
1238 
1239 	/* For shrinker support */
1240 	struct list_head s_list;
1241 	int s_ndevs;				/* number of devices */
1242 	struct f2fs_dev_info *devs;		/* for device list */
1243 	unsigned int dirty_device;		/* for checkpoint data flush */
1244 	spinlock_t dev_lock;			/* protect dirty_device */
1245 	struct mutex umount_mutex;
1246 	unsigned int shrinker_run_no;
1247 
1248 	/* For write statistics */
1249 	u64 sectors_written_start;
1250 	u64 kbytes_written;
1251 
1252 	/* Reference to checksum algorithm driver via cryptoapi */
1253 	struct crypto_shash *s_chksum_driver;
1254 
1255 	/* Precomputed FS UUID checksum for seeding other checksums */
1256 	__u32 s_chksum_seed;
1257 };
1258 
1259 #ifdef CONFIG_F2FS_FAULT_INJECTION
1260 #define f2fs_show_injection_info(type)				\
1261 	printk("%sF2FS-fs : inject %s in %s of %pF\n",		\
1262 		KERN_INFO, fault_name[type],			\
1263 		__func__, __builtin_return_address(0))
time_to_inject(struct f2fs_sb_info * sbi,int type)1264 static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
1265 {
1266 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
1267 
1268 	if (!ffi->inject_rate)
1269 		return false;
1270 
1271 	if (!IS_FAULT_SET(ffi, type))
1272 		return false;
1273 
1274 	atomic_inc(&ffi->inject_ops);
1275 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
1276 		atomic_set(&ffi->inject_ops, 0);
1277 		return true;
1278 	}
1279 	return false;
1280 }
1281 #endif
1282 
1283 /* For write statistics. Suppose sector size is 512 bytes,
1284  * and the return value is in kbytes. s is of struct f2fs_sb_info.
1285  */
1286 #define BD_PART_WRITTEN(s)						 \
1287 (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) -		 \
1288 		(s)->sectors_written_start) >> 1)
1289 
f2fs_update_time(struct f2fs_sb_info * sbi,int type)1290 static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
1291 {
1292 	sbi->last_time[type] = jiffies;
1293 }
1294 
f2fs_time_over(struct f2fs_sb_info * sbi,int type)1295 static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
1296 {
1297 	unsigned long interval = sbi->interval_time[type] * HZ;
1298 
1299 	return time_after(jiffies, sbi->last_time[type] + interval);
1300 }
1301 
is_idle(struct f2fs_sb_info * sbi)1302 static inline bool is_idle(struct f2fs_sb_info *sbi)
1303 {
1304 	struct block_device *bdev = sbi->sb->s_bdev;
1305 	struct request_queue *q = bdev_get_queue(bdev);
1306 	struct request_list *rl = &q->root_rl;
1307 
1308 	if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1309 		return 0;
1310 
1311 	return f2fs_time_over(sbi, REQ_TIME);
1312 }
1313 
1314 /*
1315  * Inline functions
1316  */
__f2fs_crc32(struct f2fs_sb_info * sbi,u32 crc,const void * address,unsigned int length)1317 static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1318 			      const void *address, unsigned int length)
1319 {
1320 	struct {
1321 		struct shash_desc shash;
1322 		char ctx[4];
1323 	} desc;
1324 	int err;
1325 
1326 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1327 
1328 	desc.shash.tfm = sbi->s_chksum_driver;
1329 	desc.shash.flags = 0;
1330 	*(u32 *)desc.ctx = crc;
1331 
1332 	err = crypto_shash_update(&desc.shash, address, length);
1333 	BUG_ON(err);
1334 
1335 	return *(u32 *)desc.ctx;
1336 }
1337 
f2fs_crc32(struct f2fs_sb_info * sbi,const void * address,unsigned int length)1338 static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1339 			   unsigned int length)
1340 {
1341 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1342 }
1343 
f2fs_crc_valid(struct f2fs_sb_info * sbi,__u32 blk_crc,void * buf,size_t buf_size)1344 static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1345 				  void *buf, size_t buf_size)
1346 {
1347 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1348 }
1349 
f2fs_chksum(struct f2fs_sb_info * sbi,u32 crc,const void * address,unsigned int length)1350 static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1351 			      const void *address, unsigned int length)
1352 {
1353 	return __f2fs_crc32(sbi, crc, address, length);
1354 }
1355 
F2FS_I(struct inode * inode)1356 static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
1357 {
1358 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
1359 }
1360 
F2FS_SB(struct super_block * sb)1361 static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
1362 {
1363 	return sb->s_fs_info;
1364 }
1365 
F2FS_I_SB(struct inode * inode)1366 static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
1367 {
1368 	return F2FS_SB(inode->i_sb);
1369 }
1370 
F2FS_M_SB(struct address_space * mapping)1371 static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
1372 {
1373 	return F2FS_I_SB(mapping->host);
1374 }
1375 
F2FS_P_SB(struct page * page)1376 static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
1377 {
1378 	return F2FS_M_SB(page->mapping);
1379 }
1380 
F2FS_RAW_SUPER(struct f2fs_sb_info * sbi)1381 static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
1382 {
1383 	return (struct f2fs_super_block *)(sbi->raw_super);
1384 }
1385 
F2FS_CKPT(struct f2fs_sb_info * sbi)1386 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
1387 {
1388 	return (struct f2fs_checkpoint *)(sbi->ckpt);
1389 }
1390 
F2FS_NODE(struct page * page)1391 static inline struct f2fs_node *F2FS_NODE(struct page *page)
1392 {
1393 	return (struct f2fs_node *)page_address(page);
1394 }
1395 
F2FS_INODE(struct page * page)1396 static inline struct f2fs_inode *F2FS_INODE(struct page *page)
1397 {
1398 	return &((struct f2fs_node *)page_address(page))->i;
1399 }
1400 
NM_I(struct f2fs_sb_info * sbi)1401 static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
1402 {
1403 	return (struct f2fs_nm_info *)(sbi->nm_info);
1404 }
1405 
SM_I(struct f2fs_sb_info * sbi)1406 static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
1407 {
1408 	return (struct f2fs_sm_info *)(sbi->sm_info);
1409 }
1410 
SIT_I(struct f2fs_sb_info * sbi)1411 static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
1412 {
1413 	return (struct sit_info *)(SM_I(sbi)->sit_info);
1414 }
1415 
FREE_I(struct f2fs_sb_info * sbi)1416 static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
1417 {
1418 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
1419 }
1420 
DIRTY_I(struct f2fs_sb_info * sbi)1421 static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
1422 {
1423 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
1424 }
1425 
META_MAPPING(struct f2fs_sb_info * sbi)1426 static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
1427 {
1428 	return sbi->meta_inode->i_mapping;
1429 }
1430 
NODE_MAPPING(struct f2fs_sb_info * sbi)1431 static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
1432 {
1433 	return sbi->node_inode->i_mapping;
1434 }
1435 
is_sbi_flag_set(struct f2fs_sb_info * sbi,unsigned int type)1436 static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
1437 {
1438 	return test_bit(type, &sbi->s_flag);
1439 }
1440 
set_sbi_flag(struct f2fs_sb_info * sbi,unsigned int type)1441 static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1442 {
1443 	set_bit(type, &sbi->s_flag);
1444 }
1445 
clear_sbi_flag(struct f2fs_sb_info * sbi,unsigned int type)1446 static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1447 {
1448 	clear_bit(type, &sbi->s_flag);
1449 }
1450 
cur_cp_version(struct f2fs_checkpoint * cp)1451 static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1452 {
1453 	return le64_to_cpu(cp->checkpoint_ver);
1454 }
1455 
f2fs_qf_ino(struct super_block * sb,int type)1456 static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1457 {
1458 	if (type < F2FS_MAX_QUOTAS)
1459 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1460 	return 0;
1461 }
1462 
cur_cp_crc(struct f2fs_checkpoint * cp)1463 static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1464 {
1465 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1466 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1467 }
1468 
__is_set_ckpt_flags(struct f2fs_checkpoint * cp,unsigned int f)1469 static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1470 {
1471 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1472 
1473 	return ckpt_flags & f;
1474 }
1475 
is_set_ckpt_flags(struct f2fs_sb_info * sbi,unsigned int f)1476 static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1477 {
1478 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1479 }
1480 
__set_ckpt_flags(struct f2fs_checkpoint * cp,unsigned int f)1481 static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1482 {
1483 	unsigned int ckpt_flags;
1484 
1485 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1486 	ckpt_flags |= f;
1487 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1488 }
1489 
set_ckpt_flags(struct f2fs_sb_info * sbi,unsigned int f)1490 static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1491 {
1492 	unsigned long flags;
1493 
1494 	spin_lock_irqsave(&sbi->cp_lock, flags);
1495 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1496 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1497 }
1498 
__clear_ckpt_flags(struct f2fs_checkpoint * cp,unsigned int f)1499 static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1500 {
1501 	unsigned int ckpt_flags;
1502 
1503 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1504 	ckpt_flags &= (~f);
1505 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1506 }
1507 
clear_ckpt_flags(struct f2fs_sb_info * sbi,unsigned int f)1508 static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1509 {
1510 	unsigned long flags;
1511 
1512 	spin_lock_irqsave(&sbi->cp_lock, flags);
1513 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1514 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1515 }
1516 
disable_nat_bits(struct f2fs_sb_info * sbi,bool lock)1517 static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
1518 {
1519 	unsigned long flags;
1520 
1521 	set_sbi_flag(sbi, SBI_NEED_FSCK);
1522 
1523 	if (lock)
1524 		spin_lock_irqsave(&sbi->cp_lock, flags);
1525 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
1526 	kfree(NM_I(sbi)->nat_bits);
1527 	NM_I(sbi)->nat_bits = NULL;
1528 	if (lock)
1529 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
1530 }
1531 
enabled_nat_bits(struct f2fs_sb_info * sbi,struct cp_control * cpc)1532 static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
1533 					struct cp_control *cpc)
1534 {
1535 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
1536 
1537 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
1538 }
1539 
f2fs_lock_op(struct f2fs_sb_info * sbi)1540 static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
1541 {
1542 	down_read(&sbi->cp_rwsem);
1543 }
1544 
f2fs_trylock_op(struct f2fs_sb_info * sbi)1545 static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1546 {
1547 	return down_read_trylock(&sbi->cp_rwsem);
1548 }
1549 
f2fs_unlock_op(struct f2fs_sb_info * sbi)1550 static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
1551 {
1552 	up_read(&sbi->cp_rwsem);
1553 }
1554 
f2fs_lock_all(struct f2fs_sb_info * sbi)1555 static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
1556 {
1557 	down_write(&sbi->cp_rwsem);
1558 }
1559 
f2fs_unlock_all(struct f2fs_sb_info * sbi)1560 static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
1561 {
1562 	up_write(&sbi->cp_rwsem);
1563 }
1564 
__get_cp_reason(struct f2fs_sb_info * sbi)1565 static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1566 {
1567 	int reason = CP_SYNC;
1568 
1569 	if (test_opt(sbi, FASTBOOT))
1570 		reason = CP_FASTBOOT;
1571 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1572 		reason = CP_UMOUNT;
1573 	return reason;
1574 }
1575 
__remain_node_summaries(int reason)1576 static inline bool __remain_node_summaries(int reason)
1577 {
1578 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
1579 }
1580 
__exist_node_summaries(struct f2fs_sb_info * sbi)1581 static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1582 {
1583 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1584 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1585 }
1586 
1587 /*
1588  * Check whether the given nid is within node id range.
1589  */
check_nid_range(struct f2fs_sb_info * sbi,nid_t nid)1590 static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
1591 {
1592 	if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1593 		return -EINVAL;
1594 	if (unlikely(nid >= NM_I(sbi)->max_nid))
1595 		return -EINVAL;
1596 	return 0;
1597 }
1598 
1599 /*
1600  * Check whether the inode has blocks or not
1601  */
F2FS_HAS_BLOCKS(struct inode * inode)1602 static inline int F2FS_HAS_BLOCKS(struct inode *inode)
1603 {
1604 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
1605 
1606 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
1607 }
1608 
f2fs_has_xattr_block(unsigned int ofs)1609 static inline bool f2fs_has_xattr_block(unsigned int ofs)
1610 {
1611 	return ofs == XATTR_NODE_OFFSET;
1612 }
1613 
__allow_reserved_blocks(struct f2fs_sb_info * sbi,struct inode * inode,bool cap)1614 static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1615 					struct inode *inode, bool cap)
1616 {
1617 	if (!inode)
1618 		return true;
1619 	if (!test_opt(sbi, RESERVE_ROOT))
1620 		return false;
1621 	if (IS_NOQUOTA(inode))
1622 		return true;
1623 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
1624 		return true;
1625 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
1626 					in_group_p(F2FS_OPTION(sbi).s_resgid))
1627 		return true;
1628 	if (cap && capable(CAP_SYS_RESOURCE))
1629 		return true;
1630 	return false;
1631 }
1632 
1633 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)1634 static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
1635 				 struct inode *inode, blkcnt_t *count)
1636 {
1637 	blkcnt_t diff = 0, release = 0;
1638 	block_t avail_user_block_count;
1639 	int ret;
1640 
1641 	ret = dquot_reserve_block(inode, *count);
1642 	if (ret)
1643 		return ret;
1644 
1645 #ifdef CONFIG_F2FS_FAULT_INJECTION
1646 	if (time_to_inject(sbi, FAULT_BLOCK)) {
1647 		f2fs_show_injection_info(FAULT_BLOCK);
1648 		release = *count;
1649 		goto enospc;
1650 	}
1651 #endif
1652 	/*
1653 	 * let's increase this in prior to actual block count change in order
1654 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1655 	 */
1656 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1657 
1658 	spin_lock(&sbi->stat_lock);
1659 	sbi->total_valid_block_count += (block_t)(*count);
1660 	avail_user_block_count = sbi->user_block_count -
1661 					sbi->current_reserved_blocks;
1662 
1663 	if (!__allow_reserved_blocks(sbi, inode, true))
1664 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
1665 
1666 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1667 		diff = sbi->total_valid_block_count - avail_user_block_count;
1668 		if (diff > *count)
1669 			diff = *count;
1670 		*count -= diff;
1671 		release = diff;
1672 		sbi->total_valid_block_count -= diff;
1673 		if (!*count) {
1674 			spin_unlock(&sbi->stat_lock);
1675 			percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
1676 			goto enospc;
1677 		}
1678 	}
1679 	spin_unlock(&sbi->stat_lock);
1680 
1681 	if (unlikely(release))
1682 		dquot_release_reservation_block(inode, release);
1683 	f2fs_i_blocks_write(inode, *count, true, true);
1684 	return 0;
1685 
1686 enospc:
1687 	dquot_release_reservation_block(inode, release);
1688 	return -ENOSPC;
1689 }
1690 
dec_valid_block_count(struct f2fs_sb_info * sbi,struct inode * inode,block_t count)1691 static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
1692 						struct inode *inode,
1693 						block_t count)
1694 {
1695 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
1696 
1697 	spin_lock(&sbi->stat_lock);
1698 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
1699 	f2fs_bug_on(sbi, inode->i_blocks < sectors);
1700 	sbi->total_valid_block_count -= (block_t)count;
1701 	if (sbi->reserved_blocks &&
1702 		sbi->current_reserved_blocks < sbi->reserved_blocks)
1703 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
1704 					sbi->current_reserved_blocks + count);
1705 	spin_unlock(&sbi->stat_lock);
1706 	f2fs_i_blocks_write(inode, count, false, true);
1707 }
1708 
inc_page_count(struct f2fs_sb_info * sbi,int count_type)1709 static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
1710 {
1711 	atomic_inc(&sbi->nr_pages[count_type]);
1712 
1713 	if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
1714 		count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
1715 		return;
1716 
1717 	set_sbi_flag(sbi, SBI_IS_DIRTY);
1718 }
1719 
inode_inc_dirty_pages(struct inode * inode)1720 static inline void inode_inc_dirty_pages(struct inode *inode)
1721 {
1722 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1723 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1724 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
1725 	if (IS_NOQUOTA(inode))
1726 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
1727 }
1728 
dec_page_count(struct f2fs_sb_info * sbi,int count_type)1729 static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
1730 {
1731 	atomic_dec(&sbi->nr_pages[count_type]);
1732 }
1733 
inode_dec_dirty_pages(struct inode * inode)1734 static inline void inode_dec_dirty_pages(struct inode *inode)
1735 {
1736 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1737 			!S_ISLNK(inode->i_mode))
1738 		return;
1739 
1740 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1741 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1742 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
1743 	if (IS_NOQUOTA(inode))
1744 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
1745 }
1746 
get_pages(struct f2fs_sb_info * sbi,int count_type)1747 static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
1748 {
1749 	return atomic_read(&sbi->nr_pages[count_type]);
1750 }
1751 
get_dirty_pages(struct inode * inode)1752 static inline int get_dirty_pages(struct inode *inode)
1753 {
1754 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1755 }
1756 
get_blocktype_secs(struct f2fs_sb_info * sbi,int block_type)1757 static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
1758 {
1759 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1760 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1761 						sbi->log_blocks_per_seg;
1762 
1763 	return segs / sbi->segs_per_sec;
1764 }
1765 
valid_user_blocks(struct f2fs_sb_info * sbi)1766 static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
1767 {
1768 	return sbi->total_valid_block_count;
1769 }
1770 
discard_blocks(struct f2fs_sb_info * sbi)1771 static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1772 {
1773 	return sbi->discard_blks;
1774 }
1775 
__bitmap_size(struct f2fs_sb_info * sbi,int flag)1776 static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
1777 {
1778 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1779 
1780 	/* return NAT or SIT bitmap */
1781 	if (flag == NAT_BITMAP)
1782 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
1783 	else if (flag == SIT_BITMAP)
1784 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
1785 
1786 	return 0;
1787 }
1788 
__cp_payload(struct f2fs_sb_info * sbi)1789 static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
1790 {
1791 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
1792 }
1793 
__bitmap_ptr(struct f2fs_sb_info * sbi,int flag)1794 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
1795 {
1796 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1797 	int offset;
1798 
1799 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
1800 		offset = (flag == SIT_BITMAP) ?
1801 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
1802 		return &ckpt->sit_nat_version_bitmap + offset;
1803 	}
1804 
1805 	if (__cp_payload(sbi) > 0) {
1806 		if (flag == NAT_BITMAP)
1807 			return &ckpt->sit_nat_version_bitmap;
1808 		else
1809 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
1810 	} else {
1811 		offset = (flag == NAT_BITMAP) ?
1812 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
1813 		return &ckpt->sit_nat_version_bitmap + offset;
1814 	}
1815 }
1816 
__start_cp_addr(struct f2fs_sb_info * sbi)1817 static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
1818 {
1819 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
1820 
1821 	if (sbi->cur_cp_pack == 2)
1822 		start_addr += sbi->blocks_per_seg;
1823 	return start_addr;
1824 }
1825 
__start_cp_next_addr(struct f2fs_sb_info * sbi)1826 static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
1827 {
1828 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
1829 
1830 	if (sbi->cur_cp_pack == 1)
1831 		start_addr += sbi->blocks_per_seg;
1832 	return start_addr;
1833 }
1834 
__set_cp_next_pack(struct f2fs_sb_info * sbi)1835 static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
1836 {
1837 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
1838 }
1839 
__start_sum_addr(struct f2fs_sb_info * sbi)1840 static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
1841 {
1842 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
1843 }
1844 
inc_valid_node_count(struct f2fs_sb_info * sbi,struct inode * inode,bool is_inode)1845 static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1846 					struct inode *inode, bool is_inode)
1847 {
1848 	block_t	valid_block_count;
1849 	unsigned int valid_node_count;
1850 	bool quota = inode && !is_inode;
1851 
1852 	if (quota) {
1853 		int ret = dquot_reserve_block(inode, 1);
1854 		if (ret)
1855 			return ret;
1856 	}
1857 
1858 #ifdef CONFIG_F2FS_FAULT_INJECTION
1859 	if (time_to_inject(sbi, FAULT_BLOCK)) {
1860 		f2fs_show_injection_info(FAULT_BLOCK);
1861 		goto enospc;
1862 	}
1863 #endif
1864 
1865 	spin_lock(&sbi->stat_lock);
1866 
1867 	valid_block_count = sbi->total_valid_block_count +
1868 					sbi->current_reserved_blocks + 1;
1869 
1870 	if (!__allow_reserved_blocks(sbi, inode, false))
1871 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
1872 
1873 	if (unlikely(valid_block_count > sbi->user_block_count)) {
1874 		spin_unlock(&sbi->stat_lock);
1875 		goto enospc;
1876 	}
1877 
1878 	valid_node_count = sbi->total_valid_node_count + 1;
1879 	if (unlikely(valid_node_count > sbi->total_node_count)) {
1880 		spin_unlock(&sbi->stat_lock);
1881 		goto enospc;
1882 	}
1883 
1884 	sbi->total_valid_node_count++;
1885 	sbi->total_valid_block_count++;
1886 	spin_unlock(&sbi->stat_lock);
1887 
1888 	if (inode) {
1889 		if (is_inode)
1890 			f2fs_mark_inode_dirty_sync(inode, true);
1891 		else
1892 			f2fs_i_blocks_write(inode, 1, true, true);
1893 	}
1894 
1895 	percpu_counter_inc(&sbi->alloc_valid_block_count);
1896 	return 0;
1897 
1898 enospc:
1899 	if (quota)
1900 		dquot_release_reservation_block(inode, 1);
1901 	return -ENOSPC;
1902 }
1903 
dec_valid_node_count(struct f2fs_sb_info * sbi,struct inode * inode,bool is_inode)1904 static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1905 					struct inode *inode, bool is_inode)
1906 {
1907 	spin_lock(&sbi->stat_lock);
1908 
1909 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
1910 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
1911 	f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
1912 
1913 	sbi->total_valid_node_count--;
1914 	sbi->total_valid_block_count--;
1915 	if (sbi->reserved_blocks &&
1916 		sbi->current_reserved_blocks < sbi->reserved_blocks)
1917 		sbi->current_reserved_blocks++;
1918 
1919 	spin_unlock(&sbi->stat_lock);
1920 
1921 	if (!is_inode)
1922 		f2fs_i_blocks_write(inode, 1, false, true);
1923 }
1924 
valid_node_count(struct f2fs_sb_info * sbi)1925 static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
1926 {
1927 	return sbi->total_valid_node_count;
1928 }
1929 
inc_valid_inode_count(struct f2fs_sb_info * sbi)1930 static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
1931 {
1932 	percpu_counter_inc(&sbi->total_valid_inode_count);
1933 }
1934 
dec_valid_inode_count(struct f2fs_sb_info * sbi)1935 static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
1936 {
1937 	percpu_counter_dec(&sbi->total_valid_inode_count);
1938 }
1939 
valid_inode_count(struct f2fs_sb_info * sbi)1940 static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
1941 {
1942 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
1943 }
1944 
f2fs_grab_cache_page(struct address_space * mapping,pgoff_t index,bool for_write)1945 static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1946 						pgoff_t index, bool for_write)
1947 {
1948 #ifdef CONFIG_F2FS_FAULT_INJECTION
1949 	struct page *page = find_lock_page(mapping, index);
1950 
1951 	if (page)
1952 		return page;
1953 
1954 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
1955 		f2fs_show_injection_info(FAULT_PAGE_ALLOC);
1956 		return NULL;
1957 	}
1958 #endif
1959 	if (!for_write)
1960 		return grab_cache_page(mapping, index);
1961 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1962 }
1963 
f2fs_pagecache_get_page(struct address_space * mapping,pgoff_t index,int fgp_flags,gfp_t gfp_mask)1964 static inline struct page *f2fs_pagecache_get_page(
1965 				struct address_space *mapping, pgoff_t index,
1966 				int fgp_flags, gfp_t gfp_mask)
1967 {
1968 #ifdef CONFIG_F2FS_FAULT_INJECTION
1969 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
1970 		f2fs_show_injection_info(FAULT_PAGE_GET);
1971 		return NULL;
1972 	}
1973 #endif
1974 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
1975 }
1976 
f2fs_copy_page(struct page * src,struct page * dst)1977 static inline void f2fs_copy_page(struct page *src, struct page *dst)
1978 {
1979 	char *src_kaddr = kmap(src);
1980 	char *dst_kaddr = kmap(dst);
1981 
1982 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
1983 	kunmap(dst);
1984 	kunmap(src);
1985 }
1986 
f2fs_put_page(struct page * page,int unlock)1987 static inline void f2fs_put_page(struct page *page, int unlock)
1988 {
1989 	if (!page)
1990 		return;
1991 
1992 	if (unlock) {
1993 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
1994 		unlock_page(page);
1995 	}
1996 	put_page(page);
1997 }
1998 
f2fs_put_dnode(struct dnode_of_data * dn)1999 static inline void f2fs_put_dnode(struct dnode_of_data *dn)
2000 {
2001 	if (dn->node_page)
2002 		f2fs_put_page(dn->node_page, 1);
2003 	if (dn->inode_page && dn->node_page != dn->inode_page)
2004 		f2fs_put_page(dn->inode_page, 0);
2005 	dn->node_page = NULL;
2006 	dn->inode_page = NULL;
2007 }
2008 
f2fs_kmem_cache_create(const char * name,size_t size)2009 static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2010 					size_t size)
2011 {
2012 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
2013 }
2014 
f2fs_kmem_cache_alloc(struct kmem_cache * cachep,gfp_t flags)2015 static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
2016 						gfp_t flags)
2017 {
2018 	void *entry;
2019 
2020 	entry = kmem_cache_alloc(cachep, flags);
2021 	if (!entry)
2022 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
2023 	return entry;
2024 }
2025 
f2fs_bio_alloc(struct f2fs_sb_info * sbi,int npages,bool no_fail)2026 static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
2027 						int npages, bool no_fail)
2028 {
2029 	struct bio *bio;
2030 
2031 	if (no_fail) {
2032 		/* No failure on bio allocation */
2033 		bio = bio_alloc(GFP_NOIO, npages);
2034 		if (!bio)
2035 			bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
2036 		return bio;
2037 	}
2038 #ifdef CONFIG_F2FS_FAULT_INJECTION
2039 	if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
2040 		f2fs_show_injection_info(FAULT_ALLOC_BIO);
2041 		return NULL;
2042 	}
2043 #endif
2044 	return bio_alloc(GFP_KERNEL, npages);
2045 }
2046 
f2fs_radix_tree_insert(struct radix_tree_root * root,unsigned long index,void * item)2047 static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
2048 				unsigned long index, void *item)
2049 {
2050 	while (radix_tree_insert(root, index, item))
2051 		cond_resched();
2052 }
2053 
2054 #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
2055 
IS_INODE(struct page * page)2056 static inline bool IS_INODE(struct page *page)
2057 {
2058 	struct f2fs_node *p = F2FS_NODE(page);
2059 
2060 	return RAW_IS_INODE(p);
2061 }
2062 
offset_in_addr(struct f2fs_inode * i)2063 static inline int offset_in_addr(struct f2fs_inode *i)
2064 {
2065 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
2066 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
2067 }
2068 
blkaddr_in_node(struct f2fs_node * node)2069 static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
2070 {
2071 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
2072 }
2073 
2074 static inline int f2fs_has_extra_attr(struct inode *inode);
datablock_addr(struct inode * inode,struct page * node_page,unsigned int offset)2075 static inline block_t datablock_addr(struct inode *inode,
2076 			struct page *node_page, unsigned int offset)
2077 {
2078 	struct f2fs_node *raw_node;
2079 	__le32 *addr_array;
2080 	int base = 0;
2081 	bool is_inode = IS_INODE(node_page);
2082 
2083 	raw_node = F2FS_NODE(node_page);
2084 
2085 	/* from GC path only */
2086 	if (is_inode) {
2087 		if (!inode)
2088 			base = offset_in_addr(&raw_node->i);
2089 		else if (f2fs_has_extra_attr(inode))
2090 			base = get_extra_isize(inode);
2091 	}
2092 
2093 	addr_array = blkaddr_in_node(raw_node);
2094 	return le32_to_cpu(addr_array[base + offset]);
2095 }
2096 
f2fs_test_bit(unsigned int nr,char * addr)2097 static inline int f2fs_test_bit(unsigned int nr, char *addr)
2098 {
2099 	int mask;
2100 
2101 	addr += (nr >> 3);
2102 	mask = 1 << (7 - (nr & 0x07));
2103 	return mask & *addr;
2104 }
2105 
f2fs_set_bit(unsigned int nr,char * addr)2106 static inline void f2fs_set_bit(unsigned int nr, char *addr)
2107 {
2108 	int mask;
2109 
2110 	addr += (nr >> 3);
2111 	mask = 1 << (7 - (nr & 0x07));
2112 	*addr |= mask;
2113 }
2114 
f2fs_clear_bit(unsigned int nr,char * addr)2115 static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2116 {
2117 	int mask;
2118 
2119 	addr += (nr >> 3);
2120 	mask = 1 << (7 - (nr & 0x07));
2121 	*addr &= ~mask;
2122 }
2123 
f2fs_test_and_set_bit(unsigned int nr,char * addr)2124 static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
2125 {
2126 	int mask;
2127 	int ret;
2128 
2129 	addr += (nr >> 3);
2130 	mask = 1 << (7 - (nr & 0x07));
2131 	ret = mask & *addr;
2132 	*addr |= mask;
2133 	return ret;
2134 }
2135 
f2fs_test_and_clear_bit(unsigned int nr,char * addr)2136 static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
2137 {
2138 	int mask;
2139 	int ret;
2140 
2141 	addr += (nr >> 3);
2142 	mask = 1 << (7 - (nr & 0x07));
2143 	ret = mask & *addr;
2144 	*addr &= ~mask;
2145 	return ret;
2146 }
2147 
f2fs_change_bit(unsigned int nr,char * addr)2148 static inline void f2fs_change_bit(unsigned int nr, char *addr)
2149 {
2150 	int mask;
2151 
2152 	addr += (nr >> 3);
2153 	mask = 1 << (7 - (nr & 0x07));
2154 	*addr ^= mask;
2155 }
2156 
2157 #define F2FS_REG_FLMASK		(~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
2158 #define F2FS_OTHER_FLMASK	(FS_NODUMP_FL | FS_NOATIME_FL)
2159 #define F2FS_FL_INHERITED	(FS_PROJINHERIT_FL)
2160 
f2fs_mask_flags(umode_t mode,__u32 flags)2161 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
2162 {
2163 	if (S_ISDIR(mode))
2164 		return flags;
2165 	else if (S_ISREG(mode))
2166 		return flags & F2FS_REG_FLMASK;
2167 	else
2168 		return flags & F2FS_OTHER_FLMASK;
2169 }
2170 
2171 /* used for f2fs_inode_info->flags */
2172 enum {
2173 	FI_NEW_INODE,		/* indicate newly allocated inode */
2174 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
2175 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
2176 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
2177 	FI_INC_LINK,		/* need to increment i_nlink */
2178 	FI_ACL_MODE,		/* indicate acl mode */
2179 	FI_NO_ALLOC,		/* should not allocate any blocks */
2180 	FI_FREE_NID,		/* free allocated nide */
2181 	FI_NO_EXTENT,		/* not to use the extent cache */
2182 	FI_INLINE_XATTR,	/* used for inline xattr */
2183 	FI_INLINE_DATA,		/* used for inline data*/
2184 	FI_INLINE_DENTRY,	/* used for inline dentry */
2185 	FI_APPEND_WRITE,	/* inode has appended data */
2186 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
2187 	FI_NEED_IPU,		/* used for ipu per file */
2188 	FI_ATOMIC_FILE,		/* indicate atomic file */
2189 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
2190 	FI_VOLATILE_FILE,	/* indicate volatile file */
2191 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
2192 	FI_DROP_CACHE,		/* drop dirty page cache */
2193 	FI_DATA_EXIST,		/* indicate data exists */
2194 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
2195 	FI_DO_DEFRAG,		/* indicate defragment is running */
2196 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
2197 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
2198 	FI_HOT_DATA,		/* indicate file is hot */
2199 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
2200 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
2201 	FI_PIN_FILE,		/* indicate file should not be gced */
2202 };
2203 
__mark_inode_dirty_flag(struct inode * inode,int flag,bool set)2204 static inline void __mark_inode_dirty_flag(struct inode *inode,
2205 						int flag, bool set)
2206 {
2207 	switch (flag) {
2208 	case FI_INLINE_XATTR:
2209 	case FI_INLINE_DATA:
2210 	case FI_INLINE_DENTRY:
2211 	case FI_NEW_INODE:
2212 		if (set)
2213 			return;
2214 	case FI_DATA_EXIST:
2215 	case FI_INLINE_DOTS:
2216 	case FI_PIN_FILE:
2217 		f2fs_mark_inode_dirty_sync(inode, true);
2218 	}
2219 }
2220 
set_inode_flag(struct inode * inode,int flag)2221 static inline void set_inode_flag(struct inode *inode, int flag)
2222 {
2223 	if (!test_bit(flag, &F2FS_I(inode)->flags))
2224 		set_bit(flag, &F2FS_I(inode)->flags);
2225 	__mark_inode_dirty_flag(inode, flag, true);
2226 }
2227 
is_inode_flag_set(struct inode * inode,int flag)2228 static inline int is_inode_flag_set(struct inode *inode, int flag)
2229 {
2230 	return test_bit(flag, &F2FS_I(inode)->flags);
2231 }
2232 
clear_inode_flag(struct inode * inode,int flag)2233 static inline void clear_inode_flag(struct inode *inode, int flag)
2234 {
2235 	if (test_bit(flag, &F2FS_I(inode)->flags))
2236 		clear_bit(flag, &F2FS_I(inode)->flags);
2237 	__mark_inode_dirty_flag(inode, flag, false);
2238 }
2239 
set_acl_inode(struct inode * inode,umode_t mode)2240 static inline void set_acl_inode(struct inode *inode, umode_t mode)
2241 {
2242 	F2FS_I(inode)->i_acl_mode = mode;
2243 	set_inode_flag(inode, FI_ACL_MODE);
2244 	f2fs_mark_inode_dirty_sync(inode, false);
2245 }
2246 
f2fs_i_links_write(struct inode * inode,bool inc)2247 static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2248 {
2249 	if (inc)
2250 		inc_nlink(inode);
2251 	else
2252 		drop_nlink(inode);
2253 	f2fs_mark_inode_dirty_sync(inode, true);
2254 }
2255 
f2fs_i_blocks_write(struct inode * inode,block_t diff,bool add,bool claim)2256 static inline void f2fs_i_blocks_write(struct inode *inode,
2257 					block_t diff, bool add, bool claim)
2258 {
2259 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
2260 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
2261 
2262 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
2263 	if (add) {
2264 		if (claim)
2265 			dquot_claim_block(inode, diff);
2266 		else
2267 			dquot_alloc_block_nofail(inode, diff);
2268 	} else {
2269 		dquot_free_block(inode, diff);
2270 	}
2271 
2272 	f2fs_mark_inode_dirty_sync(inode, true);
2273 	if (clean || recover)
2274 		set_inode_flag(inode, FI_AUTO_RECOVER);
2275 }
2276 
f2fs_i_size_write(struct inode * inode,loff_t i_size)2277 static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2278 {
2279 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
2280 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
2281 
2282 	if (i_size_read(inode) == i_size)
2283 		return;
2284 
2285 	i_size_write(inode, i_size);
2286 	f2fs_mark_inode_dirty_sync(inode, true);
2287 	if (clean || recover)
2288 		set_inode_flag(inode, FI_AUTO_RECOVER);
2289 }
2290 
f2fs_i_depth_write(struct inode * inode,unsigned int depth)2291 static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2292 {
2293 	F2FS_I(inode)->i_current_depth = depth;
2294 	f2fs_mark_inode_dirty_sync(inode, true);
2295 }
2296 
f2fs_i_gc_failures_write(struct inode * inode,unsigned int count)2297 static inline void f2fs_i_gc_failures_write(struct inode *inode,
2298 					unsigned int count)
2299 {
2300 	F2FS_I(inode)->i_gc_failures = count;
2301 	f2fs_mark_inode_dirty_sync(inode, true);
2302 }
2303 
f2fs_i_xnid_write(struct inode * inode,nid_t xnid)2304 static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2305 {
2306 	F2FS_I(inode)->i_xattr_nid = xnid;
2307 	f2fs_mark_inode_dirty_sync(inode, true);
2308 }
2309 
f2fs_i_pino_write(struct inode * inode,nid_t pino)2310 static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2311 {
2312 	F2FS_I(inode)->i_pino = pino;
2313 	f2fs_mark_inode_dirty_sync(inode, true);
2314 }
2315 
get_inline_info(struct inode * inode,struct f2fs_inode * ri)2316 static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2317 {
2318 	struct f2fs_inode_info *fi = F2FS_I(inode);
2319 
2320 	if (ri->i_inline & F2FS_INLINE_XATTR)
2321 		set_bit(FI_INLINE_XATTR, &fi->flags);
2322 	if (ri->i_inline & F2FS_INLINE_DATA)
2323 		set_bit(FI_INLINE_DATA, &fi->flags);
2324 	if (ri->i_inline & F2FS_INLINE_DENTRY)
2325 		set_bit(FI_INLINE_DENTRY, &fi->flags);
2326 	if (ri->i_inline & F2FS_DATA_EXIST)
2327 		set_bit(FI_DATA_EXIST, &fi->flags);
2328 	if (ri->i_inline & F2FS_INLINE_DOTS)
2329 		set_bit(FI_INLINE_DOTS, &fi->flags);
2330 	if (ri->i_inline & F2FS_EXTRA_ATTR)
2331 		set_bit(FI_EXTRA_ATTR, &fi->flags);
2332 	if (ri->i_inline & F2FS_PIN_FILE)
2333 		set_bit(FI_PIN_FILE, &fi->flags);
2334 }
2335 
set_raw_inline(struct inode * inode,struct f2fs_inode * ri)2336 static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2337 {
2338 	ri->i_inline = 0;
2339 
2340 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2341 		ri->i_inline |= F2FS_INLINE_XATTR;
2342 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
2343 		ri->i_inline |= F2FS_INLINE_DATA;
2344 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
2345 		ri->i_inline |= F2FS_INLINE_DENTRY;
2346 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2347 		ri->i_inline |= F2FS_DATA_EXIST;
2348 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2349 		ri->i_inline |= F2FS_INLINE_DOTS;
2350 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
2351 		ri->i_inline |= F2FS_EXTRA_ATTR;
2352 	if (is_inode_flag_set(inode, FI_PIN_FILE))
2353 		ri->i_inline |= F2FS_PIN_FILE;
2354 }
2355 
f2fs_has_extra_attr(struct inode * inode)2356 static inline int f2fs_has_extra_attr(struct inode *inode)
2357 {
2358 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2359 }
2360 
f2fs_has_inline_xattr(struct inode * inode)2361 static inline int f2fs_has_inline_xattr(struct inode *inode)
2362 {
2363 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2364 }
2365 
addrs_per_inode(struct inode * inode)2366 static inline unsigned int addrs_per_inode(struct inode *inode)
2367 {
2368 	return CUR_ADDRS_PER_INODE(inode) - get_inline_xattr_addrs(inode);
2369 }
2370 
inline_xattr_addr(struct inode * inode,struct page * page)2371 static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
2372 {
2373 	struct f2fs_inode *ri = F2FS_INODE(page);
2374 
2375 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2376 					get_inline_xattr_addrs(inode)]);
2377 }
2378 
inline_xattr_size(struct inode * inode)2379 static inline int inline_xattr_size(struct inode *inode)
2380 {
2381 	return get_inline_xattr_addrs(inode) * sizeof(__le32);
2382 }
2383 
f2fs_has_inline_data(struct inode * inode)2384 static inline int f2fs_has_inline_data(struct inode *inode)
2385 {
2386 	return is_inode_flag_set(inode, FI_INLINE_DATA);
2387 }
2388 
f2fs_exist_data(struct inode * inode)2389 static inline int f2fs_exist_data(struct inode *inode)
2390 {
2391 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2392 }
2393 
f2fs_has_inline_dots(struct inode * inode)2394 static inline int f2fs_has_inline_dots(struct inode *inode)
2395 {
2396 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2397 }
2398 
f2fs_is_pinned_file(struct inode * inode)2399 static inline bool f2fs_is_pinned_file(struct inode *inode)
2400 {
2401 	return is_inode_flag_set(inode, FI_PIN_FILE);
2402 }
2403 
f2fs_is_atomic_file(struct inode * inode)2404 static inline bool f2fs_is_atomic_file(struct inode *inode)
2405 {
2406 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
2407 }
2408 
f2fs_is_commit_atomic_write(struct inode * inode)2409 static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
2410 {
2411 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
2412 }
2413 
f2fs_is_volatile_file(struct inode * inode)2414 static inline bool f2fs_is_volatile_file(struct inode *inode)
2415 {
2416 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
2417 }
2418 
f2fs_is_first_block_written(struct inode * inode)2419 static inline bool f2fs_is_first_block_written(struct inode *inode)
2420 {
2421 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
2422 }
2423 
f2fs_is_drop_cache(struct inode * inode)2424 static inline bool f2fs_is_drop_cache(struct inode *inode)
2425 {
2426 	return is_inode_flag_set(inode, FI_DROP_CACHE);
2427 }
2428 
inline_data_addr(struct inode * inode,struct page * page)2429 static inline void *inline_data_addr(struct inode *inode, struct page *page)
2430 {
2431 	struct f2fs_inode *ri = F2FS_INODE(page);
2432 	int extra_size = get_extra_isize(inode);
2433 
2434 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
2435 }
2436 
f2fs_has_inline_dentry(struct inode * inode)2437 static inline int f2fs_has_inline_dentry(struct inode *inode)
2438 {
2439 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
2440 }
2441 
is_file(struct inode * inode,int type)2442 static inline int is_file(struct inode *inode, int type)
2443 {
2444 	return F2FS_I(inode)->i_advise & type;
2445 }
2446 
set_file(struct inode * inode,int type)2447 static inline void set_file(struct inode *inode, int type)
2448 {
2449 	F2FS_I(inode)->i_advise |= type;
2450 	f2fs_mark_inode_dirty_sync(inode, true);
2451 }
2452 
clear_file(struct inode * inode,int type)2453 static inline void clear_file(struct inode *inode, int type)
2454 {
2455 	F2FS_I(inode)->i_advise &= ~type;
2456 	f2fs_mark_inode_dirty_sync(inode, true);
2457 }
2458 
f2fs_skip_inode_update(struct inode * inode,int dsync)2459 static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
2460 {
2461 	bool ret;
2462 
2463 	if (dsync) {
2464 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2465 
2466 		spin_lock(&sbi->inode_lock[DIRTY_META]);
2467 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
2468 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
2469 		return ret;
2470 	}
2471 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
2472 			file_keep_isize(inode) ||
2473 			i_size_read(inode) & ~PAGE_MASK)
2474 		return false;
2475 
2476 	if (!timespec_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
2477 		return false;
2478 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
2479 		return false;
2480 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
2481 		return false;
2482 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 3,
2483 						&F2FS_I(inode)->i_crtime))
2484 		return false;
2485 
2486 	down_read(&F2FS_I(inode)->i_sem);
2487 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2488 	up_read(&F2FS_I(inode)->i_sem);
2489 
2490 	return ret;
2491 }
2492 
f2fs_readonly(struct super_block * sb)2493 static inline bool f2fs_readonly(struct super_block *sb)
2494 {
2495 	return sb->s_flags & MS_RDONLY;
2496 }
2497 
f2fs_cp_error(struct f2fs_sb_info * sbi)2498 static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2499 {
2500 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2501 }
2502 
is_dot_dotdot(const struct qstr * str)2503 static inline bool is_dot_dotdot(const struct qstr *str)
2504 {
2505 	if (str->len == 1 && str->name[0] == '.')
2506 		return true;
2507 
2508 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2509 		return true;
2510 
2511 	return false;
2512 }
2513 
f2fs_may_extent_tree(struct inode * inode)2514 static inline bool f2fs_may_extent_tree(struct inode *inode)
2515 {
2516 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
2517 			is_inode_flag_set(inode, FI_NO_EXTENT))
2518 		return false;
2519 
2520 	return S_ISREG(inode->i_mode);
2521 }
2522 
f2fs_kmalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)2523 static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
2524 					size_t size, gfp_t flags)
2525 {
2526 #ifdef CONFIG_F2FS_FAULT_INJECTION
2527 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
2528 		f2fs_show_injection_info(FAULT_KMALLOC);
2529 		return NULL;
2530 	}
2531 #endif
2532 	return kmalloc(size, flags);
2533 }
2534 
kvmalloc(size_t size,gfp_t flags)2535 static inline void *kvmalloc(size_t size, gfp_t flags)
2536 {
2537 	void *ret;
2538 
2539 	ret = kmalloc(size, flags | __GFP_NOWARN);
2540 	if (!ret)
2541 		ret = __vmalloc(size, flags, PAGE_KERNEL);
2542 	return ret;
2543 }
2544 
kvzalloc(size_t size,gfp_t flags)2545 static inline void *kvzalloc(size_t size, gfp_t flags)
2546 {
2547 	void *ret;
2548 
2549 	ret = kzalloc(size, flags | __GFP_NOWARN);
2550 	if (!ret)
2551 		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
2552 	return ret;
2553 }
2554 
wbc_to_write_flags(struct writeback_control * wbc)2555 static inline int wbc_to_write_flags(struct writeback_control *wbc)
2556 {
2557 	if (wbc->sync_mode == WB_SYNC_ALL)
2558 		return REQ_SYNC;
2559 	else if (wbc->for_kupdate || wbc->for_background)
2560 		return 0;
2561 
2562 	return 0;
2563 }
2564 
f2fs_kzalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)2565 static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2566 					size_t size, gfp_t flags)
2567 {
2568 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2569 }
2570 
f2fs_kvmalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)2571 static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2572 					size_t size, gfp_t flags)
2573 {
2574 #ifdef CONFIG_F2FS_FAULT_INJECTION
2575 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2576 		f2fs_show_injection_info(FAULT_KVMALLOC);
2577 		return NULL;
2578 	}
2579 #endif
2580 	return kvmalloc(size, flags);
2581 }
2582 
f2fs_kvzalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)2583 static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2584 					size_t size, gfp_t flags)
2585 {
2586 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2587 }
2588 
get_extra_isize(struct inode * inode)2589 static inline int get_extra_isize(struct inode *inode)
2590 {
2591 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2592 }
2593 
get_inline_xattr_addrs(struct inode * inode)2594 static inline int get_inline_xattr_addrs(struct inode *inode)
2595 {
2596 	return F2FS_I(inode)->i_inline_xattr_size;
2597 }
2598 
2599 #define get_inode_mode(i) \
2600 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2601 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2602 
2603 #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
2604 	(offsetof(struct f2fs_inode, i_extra_end) -	\
2605 	offsetof(struct f2fs_inode, i_extra_isize))	\
2606 
2607 #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
2608 #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
2609 		((offsetof(typeof(*f2fs_inode), field) +	\
2610 		sizeof((f2fs_inode)->field))			\
2611 		<= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize))	\
2612 
f2fs_reset_iostat(struct f2fs_sb_info * sbi)2613 static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2614 {
2615 	int i;
2616 
2617 	spin_lock(&sbi->iostat_lock);
2618 	for (i = 0; i < NR_IO_TYPE; i++)
2619 		sbi->write_iostat[i] = 0;
2620 	spin_unlock(&sbi->iostat_lock);
2621 }
2622 
f2fs_update_iostat(struct f2fs_sb_info * sbi,enum iostat_type type,unsigned long long io_bytes)2623 static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2624 			enum iostat_type type, unsigned long long io_bytes)
2625 {
2626 	if (!sbi->iostat_enable)
2627 		return;
2628 	spin_lock(&sbi->iostat_lock);
2629 	sbi->write_iostat[type] += io_bytes;
2630 
2631 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2632 		sbi->write_iostat[APP_BUFFERED_IO] =
2633 			sbi->write_iostat[APP_WRITE_IO] -
2634 			sbi->write_iostat[APP_DIRECT_IO];
2635 	spin_unlock(&sbi->iostat_lock);
2636 }
2637 
2638 /*
2639  * file.c
2640  */
2641 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2642 void truncate_data_blocks(struct dnode_of_data *dn);
2643 int truncate_blocks(struct inode *inode, u64 from, bool lock);
2644 int f2fs_truncate(struct inode *inode);
2645 int f2fs_getattr(struct vfsmount *mnt, struct dentry *dentry,
2646 			struct kstat *stat);
2647 int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2648 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2649 void truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2650 int f2fs_precache_extents(struct inode *inode);
2651 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2652 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
2653 int f2fs_pin_file_control(struct inode *inode, bool inc);
2654 
2655 /*
2656  * inode.c
2657  */
2658 void f2fs_set_inode_flags(struct inode *inode);
2659 bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2660 void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2661 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2662 struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2663 int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2664 void update_inode(struct inode *inode, struct page *node_page);
2665 void update_inode_page(struct inode *inode);
2666 int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2667 void f2fs_evict_inode(struct inode *inode);
2668 void handle_failed_inode(struct inode *inode);
2669 
2670 /*
2671  * namei.c
2672  */
2673 int update_extension_list(struct f2fs_sb_info *sbi, const char *name,
2674 							bool hot, bool set);
2675 struct dentry *f2fs_get_parent(struct dentry *child);
2676 
2677 /*
2678  * dir.c
2679  */
2680 void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
2681 unsigned char get_de_type(struct f2fs_dir_entry *de);
2682 struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
2683 			f2fs_hash_t namehash, int *max_slots,
2684 			struct f2fs_dentry_ptr *d);
2685 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2686 			unsigned int start_pos, struct fscrypt_str *fstr);
2687 void do_make_empty_dir(struct inode *inode, struct inode *parent,
2688 			struct f2fs_dentry_ptr *d);
2689 struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
2690 			const struct qstr *new_name,
2691 			const struct qstr *orig_name, struct page *dpage);
2692 void update_parent_metadata(struct inode *dir, struct inode *inode,
2693 			unsigned int current_depth);
2694 int room_for_filename(const void *bitmap, int slots, int max_slots);
2695 void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2696 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2697 			struct fscrypt_name *fname, struct page **res_page);
2698 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2699 			const struct qstr *child, struct page **res_page);
2700 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2701 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2702 			struct page **page);
2703 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2704 			struct page *page, struct inode *inode);
2705 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2706 			const struct qstr *name, f2fs_hash_t name_hash,
2707 			unsigned int bit_pos);
2708 int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2709 			const struct qstr *orig_name,
2710 			struct inode *inode, nid_t ino, umode_t mode);
2711 int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
2712 			struct inode *inode, nid_t ino, umode_t mode);
2713 int __f2fs_add_link(struct inode *dir, const struct qstr *name,
2714 			struct inode *inode, nid_t ino, umode_t mode);
2715 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2716 			struct inode *dir, struct inode *inode);
2717 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2718 bool f2fs_empty_dir(struct inode *dir);
2719 
f2fs_add_link(struct dentry * dentry,struct inode * inode)2720 static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2721 {
2722 	return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2723 				inode, inode->i_ino, inode->i_mode);
2724 }
2725 
2726 /*
2727  * super.c
2728  */
2729 int f2fs_inode_dirtied(struct inode *inode, bool sync);
2730 void f2fs_inode_synced(struct inode *inode);
2731 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
2732 void f2fs_quota_off_umount(struct super_block *sb);
2733 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2734 int f2fs_sync_fs(struct super_block *sb, int sync);
2735 extern __printf(3, 4)
2736 void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
2737 int sanity_check_ckpt(struct f2fs_sb_info *sbi);
2738 
2739 /*
2740  * hash.c
2741  */
2742 f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
2743 				struct fscrypt_name *fname);
2744 
2745 /*
2746  * node.c
2747  */
2748 struct dnode_of_data;
2749 struct node_info;
2750 
2751 bool available_free_memory(struct f2fs_sb_info *sbi, int type);
2752 int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2753 bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2754 bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2755 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
2756 pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2757 int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2758 int truncate_inode_blocks(struct inode *inode, pgoff_t from);
2759 int truncate_xattr_node(struct inode *inode);
2760 int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2761 int remove_inode_page(struct inode *inode);
2762 struct page *new_inode_page(struct inode *inode);
2763 struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs);
2764 void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2765 struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2766 struct page *get_node_page_ra(struct page *parent, int start);
2767 void move_node_page(struct page *node_page, int gc_type);
2768 int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2769 			struct writeback_control *wbc, bool atomic);
2770 int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc,
2771 			bool do_balance, enum iostat_type io_type);
2772 void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2773 bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2774 void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2775 void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2776 int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2777 void recover_inline_xattr(struct inode *inode, struct page *page);
2778 int recover_xattr_data(struct inode *inode, struct page *page);
2779 int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2780 void restore_node_summary(struct f2fs_sb_info *sbi,
2781 			unsigned int segno, struct f2fs_summary_block *sum);
2782 void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2783 int build_node_manager(struct f2fs_sb_info *sbi);
2784 void destroy_node_manager(struct f2fs_sb_info *sbi);
2785 int __init create_node_manager_caches(void);
2786 void destroy_node_manager_caches(void);
2787 
2788 /*
2789  * segment.c
2790  */
2791 bool need_SSR(struct f2fs_sb_info *sbi);
2792 void register_inmem_page(struct inode *inode, struct page *page);
2793 void drop_inmem_pages_all(struct f2fs_sb_info *sbi);
2794 void drop_inmem_pages(struct inode *inode);
2795 void drop_inmem_page(struct inode *inode, struct page *page);
2796 int commit_inmem_pages(struct inode *inode);
2797 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2798 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
2799 int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
2800 int create_flush_cmd_control(struct f2fs_sb_info *sbi);
2801 int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
2802 void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2803 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2804 bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
2805 void init_discard_policy(struct discard_policy *dpolicy, int discard_type,
2806 						unsigned int granularity);
2807 void drop_discard_cmd(struct f2fs_sb_info *sbi);
2808 void stop_discard_thread(struct f2fs_sb_info *sbi);
2809 bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
2810 void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2811 void release_discard_addrs(struct f2fs_sb_info *sbi);
2812 int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2813 void allocate_new_segments(struct f2fs_sb_info *sbi);
2814 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2815 bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2816 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2817 void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2818 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2819 						enum iostat_type io_type);
2820 void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2821 void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
2822 int rewrite_data_page(struct f2fs_io_info *fio);
2823 void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2824 			block_t old_blkaddr, block_t new_blkaddr,
2825 			bool recover_curseg, bool recover_newaddr);
2826 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2827 			block_t old_addr, block_t new_addr,
2828 			unsigned char version, bool recover_curseg,
2829 			bool recover_newaddr);
2830 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2831 			block_t old_blkaddr, block_t *new_blkaddr,
2832 			struct f2fs_summary *sum, int type,
2833 			struct f2fs_io_info *fio, bool add_list);
2834 void f2fs_wait_on_page_writeback(struct page *page,
2835 			enum page_type type, bool ordered);
2836 void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr);
2837 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2838 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2839 int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2840 			unsigned int val, int alloc);
2841 void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2842 int build_segment_manager(struct f2fs_sb_info *sbi);
2843 void destroy_segment_manager(struct f2fs_sb_info *sbi);
2844 int __init create_segment_manager_caches(void);
2845 void destroy_segment_manager_caches(void);
2846 int rw_hint_to_seg_type(enum rw_hint hint);
2847 enum rw_hint io_type_to_rw_hint(struct f2fs_sb_info *sbi, enum page_type type,
2848 				enum temp_type temp);
2849 
2850 /*
2851  * checkpoint.c
2852  */
2853 void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
2854 struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2855 struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2856 struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2857 bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type);
2858 int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2859 			int type, bool sync);
2860 void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2861 long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2862 			long nr_to_write, enum iostat_type io_type);
2863 void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2864 void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2865 void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2866 bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
2867 void set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
2868 					unsigned int devidx, int type);
2869 bool is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
2870 					unsigned int devidx, int type);
2871 int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
2872 int acquire_orphan_inode(struct f2fs_sb_info *sbi);
2873 void release_orphan_inode(struct f2fs_sb_info *sbi);
2874 void add_orphan_inode(struct inode *inode);
2875 void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2876 int recover_orphan_inodes(struct f2fs_sb_info *sbi);
2877 int get_valid_checkpoint(struct f2fs_sb_info *sbi);
2878 void update_dirty_page(struct inode *inode, struct page *page);
2879 void remove_dirty_inode(struct inode *inode);
2880 int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2881 int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2882 void init_ino_entry_info(struct f2fs_sb_info *sbi);
2883 int __init create_checkpoint_caches(void);
2884 void destroy_checkpoint_caches(void);
2885 
2886 /*
2887  * data.c
2888  */
2889 int f2fs_init_post_read_processing(void);
2890 void f2fs_destroy_post_read_processing(void);
2891 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2892 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
2893 				struct inode *inode, nid_t ino, pgoff_t idx,
2894 				enum page_type type);
2895 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
2896 int f2fs_submit_page_bio(struct f2fs_io_info *fio);
2897 int f2fs_submit_page_write(struct f2fs_io_info *fio);
2898 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2899 			block_t blk_addr, struct bio *bio);
2900 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
2901 void set_data_blkaddr(struct dnode_of_data *dn);
2902 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
2903 int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2904 int reserve_new_block(struct dnode_of_data *dn);
2905 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2906 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2907 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
2908 struct page *get_read_data_page(struct inode *inode, pgoff_t index,
2909 			int op_flags, bool for_write);
2910 struct page *find_data_page(struct inode *inode, pgoff_t index);
2911 struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
2912 			bool for_write);
2913 struct page *get_new_data_page(struct inode *inode,
2914 			struct page *ipage, pgoff_t index, bool new_i_size);
2915 int do_write_data_page(struct f2fs_io_info *fio);
2916 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2917 			int create, int flag);
2918 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2919 			u64 start, u64 len);
2920 bool should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
2921 bool should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
2922 int __f2fs_write_data_pages(struct address_space *mapping,
2923 						struct writeback_control *wbc,
2924 						enum iostat_type io_type);
2925 void f2fs_invalidate_page(struct page *page, unsigned int offset,
2926 			unsigned int length);
2927 int f2fs_release_page(struct page *page, gfp_t wait);
2928 #ifdef CONFIG_MIGRATION
2929 int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2930 			struct page *page, enum migrate_mode mode);
2931 #endif
2932 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
2933 
2934 /*
2935  * gc.c
2936  */
2937 int start_gc_thread(struct f2fs_sb_info *sbi);
2938 void stop_gc_thread(struct f2fs_sb_info *sbi);
2939 block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2940 int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2941 			unsigned int segno);
2942 void build_gc_manager(struct f2fs_sb_info *sbi);
2943 
2944 /*
2945  * recovery.c
2946  */
2947 int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
2948 bool space_for_roll_forward(struct f2fs_sb_info *sbi);
2949 
2950 /*
2951  * debug.c
2952  */
2953 #ifdef CONFIG_F2FS_STAT_FS
2954 struct f2fs_stat_info {
2955 	struct list_head stat_list;
2956 	struct f2fs_sb_info *sbi;
2957 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
2958 	int main_area_segs, main_area_sections, main_area_zones;
2959 	unsigned long long hit_largest, hit_cached, hit_rbtree;
2960 	unsigned long long hit_total, total_ext;
2961 	int ext_tree, zombie_tree, ext_node;
2962 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
2963 	int ndirty_data, ndirty_qdata;
2964 	int inmem_pages;
2965 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
2966 	int nats, dirty_nats, sits, dirty_sits;
2967 	int free_nids, avail_nids, alloc_nids;
2968 	int total_count, utilization;
2969 	int bg_gc, nr_wb_cp_data, nr_wb_data;
2970 	int nr_flushing, nr_flushed, flush_list_empty;
2971 	int nr_discarding, nr_discarded;
2972 	int nr_discard_cmd;
2973 	unsigned int undiscard_blks;
2974 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
2975 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
2976 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
2977 	unsigned int bimodal, avg_vblocks;
2978 	int util_free, util_valid, util_invalid;
2979 	int rsvd_segs, overp_segs;
2980 	int dirty_count, node_pages, meta_pages;
2981 	int prefree_count, call_count, cp_count, bg_cp_count;
2982 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
2983 	int bg_node_segs, bg_data_segs;
2984 	int tot_blks, data_blks, node_blks;
2985 	int bg_data_blks, bg_node_blks;
2986 	int curseg[NR_CURSEG_TYPE];
2987 	int cursec[NR_CURSEG_TYPE];
2988 	int curzone[NR_CURSEG_TYPE];
2989 
2990 	unsigned int segment_count[2];
2991 	unsigned int block_count[2];
2992 	unsigned int inplace_count;
2993 	unsigned long long base_mem, cache_mem, page_mem;
2994 };
2995 
F2FS_STAT(struct f2fs_sb_info * sbi)2996 static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
2997 {
2998 	return (struct f2fs_stat_info *)sbi->stat_info;
2999 }
3000 
3001 #define stat_inc_cp_count(si)		((si)->cp_count++)
3002 #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
3003 #define stat_inc_call_count(si)		((si)->call_count++)
3004 #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
3005 #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
3006 #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
3007 #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
3008 #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
3009 #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
3010 #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3011 #define stat_inc_inline_xattr(inode)					\
3012 	do {								\
3013 		if (f2fs_has_inline_xattr(inode))			\
3014 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3015 	} while (0)
3016 #define stat_dec_inline_xattr(inode)					\
3017 	do {								\
3018 		if (f2fs_has_inline_xattr(inode))			\
3019 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3020 	} while (0)
3021 #define stat_inc_inline_inode(inode)					\
3022 	do {								\
3023 		if (f2fs_has_inline_data(inode))			\
3024 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
3025 	} while (0)
3026 #define stat_dec_inline_inode(inode)					\
3027 	do {								\
3028 		if (f2fs_has_inline_data(inode))			\
3029 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
3030 	} while (0)
3031 #define stat_inc_inline_dir(inode)					\
3032 	do {								\
3033 		if (f2fs_has_inline_dentry(inode))			\
3034 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
3035 	} while (0)
3036 #define stat_dec_inline_dir(inode)					\
3037 	do {								\
3038 		if (f2fs_has_inline_dentry(inode))			\
3039 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
3040 	} while (0)
3041 #define stat_inc_seg_type(sbi, curseg)					\
3042 		((sbi)->segment_count[(curseg)->alloc_type]++)
3043 #define stat_inc_block_count(sbi, curseg)				\
3044 		((sbi)->block_count[(curseg)->alloc_type]++)
3045 #define stat_inc_inplace_blocks(sbi)					\
3046 		(atomic_inc(&(sbi)->inplace_count))
3047 #define stat_inc_atomic_write(inode)					\
3048 		(atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
3049 #define stat_dec_atomic_write(inode)					\
3050 		(atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
3051 #define stat_update_max_atomic_write(inode)				\
3052 	do {								\
3053 		int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);	\
3054 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
3055 		if (cur > max)						\
3056 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
3057 	} while (0)
3058 #define stat_inc_volatile_write(inode)					\
3059 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3060 #define stat_dec_volatile_write(inode)					\
3061 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3062 #define stat_update_max_volatile_write(inode)				\
3063 	do {								\
3064 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3065 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3066 		if (cur > max)						\
3067 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3068 	} while (0)
3069 #define stat_inc_seg_count(sbi, type, gc_type)				\
3070 	do {								\
3071 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
3072 		si->tot_segs++;						\
3073 		if ((type) == SUM_TYPE_DATA) {				\
3074 			si->data_segs++;				\
3075 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3076 		} else {						\
3077 			si->node_segs++;				\
3078 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3079 		}							\
3080 	} while (0)
3081 
3082 #define stat_inc_tot_blk_count(si, blks)				\
3083 	((si)->tot_blks += (blks))
3084 
3085 #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
3086 	do {								\
3087 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
3088 		stat_inc_tot_blk_count(si, blks);			\
3089 		si->data_blks += (blks);				\
3090 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
3091 	} while (0)
3092 
3093 #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
3094 	do {								\
3095 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
3096 		stat_inc_tot_blk_count(si, blks);			\
3097 		si->node_blks += (blks);				\
3098 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
3099 	} while (0)
3100 
3101 int f2fs_build_stats(struct f2fs_sb_info *sbi);
3102 void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
3103 int __init f2fs_create_root_stats(void);
3104 void f2fs_destroy_root_stats(void);
3105 #else
3106 #define stat_inc_cp_count(si)				do { } while (0)
3107 #define stat_inc_bg_cp_count(si)			do { } while (0)
3108 #define stat_inc_call_count(si)				do { } while (0)
3109 #define stat_inc_bggc_count(si)				do { } while (0)
3110 #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3111 #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3112 #define stat_inc_total_hit(sb)				do { } while (0)
3113 #define stat_inc_rbtree_node_hit(sb)			do { } while (0)
3114 #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3115 #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3116 #define stat_inc_inline_xattr(inode)			do { } while (0)
3117 #define stat_dec_inline_xattr(inode)			do { } while (0)
3118 #define stat_inc_inline_inode(inode)			do { } while (0)
3119 #define stat_dec_inline_inode(inode)			do { } while (0)
3120 #define stat_inc_inline_dir(inode)			do { } while (0)
3121 #define stat_dec_inline_dir(inode)			do { } while (0)
3122 #define stat_inc_atomic_write(inode)			do { } while (0)
3123 #define stat_dec_atomic_write(inode)			do { } while (0)
3124 #define stat_update_max_atomic_write(inode)		do { } while (0)
3125 #define stat_inc_volatile_write(inode)			do { } while (0)
3126 #define stat_dec_volatile_write(inode)			do { } while (0)
3127 #define stat_update_max_volatile_write(inode)		do { } while (0)
3128 #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3129 #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3130 #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3131 #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3132 #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3133 #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3134 #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
3135 
f2fs_build_stats(struct f2fs_sb_info * sbi)3136 static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
f2fs_destroy_stats(struct f2fs_sb_info * sbi)3137 static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
f2fs_create_root_stats(void)3138 static inline int __init f2fs_create_root_stats(void) { return 0; }
f2fs_destroy_root_stats(void)3139 static inline void f2fs_destroy_root_stats(void) { }
3140 #endif
3141 
3142 extern const struct file_operations f2fs_dir_operations;
3143 extern const struct file_operations f2fs_file_operations;
3144 extern const struct inode_operations f2fs_file_inode_operations;
3145 extern const struct address_space_operations f2fs_dblock_aops;
3146 extern const struct address_space_operations f2fs_node_aops;
3147 extern const struct address_space_operations f2fs_meta_aops;
3148 extern const struct inode_operations f2fs_dir_inode_operations;
3149 extern const struct inode_operations f2fs_symlink_inode_operations;
3150 extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
3151 extern const struct inode_operations f2fs_special_inode_operations;
3152 extern struct kmem_cache *inode_entry_slab;
3153 
3154 /*
3155  * inline.c
3156  */
3157 bool f2fs_may_inline_data(struct inode *inode);
3158 bool f2fs_may_inline_dentry(struct inode *inode);
3159 void read_inline_data(struct page *page, struct page *ipage);
3160 void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
3161 int f2fs_read_inline_data(struct inode *inode, struct page *page);
3162 int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3163 int f2fs_convert_inline_inode(struct inode *inode);
3164 int f2fs_write_inline_data(struct inode *inode, struct page *page);
3165 bool recover_inline_data(struct inode *inode, struct page *npage);
3166 struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
3167 			struct fscrypt_name *fname, struct page **res_page);
3168 int make_empty_inline_dir(struct inode *inode, struct inode *parent,
3169 			struct page *ipage);
3170 int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3171 			const struct qstr *orig_name,
3172 			struct inode *inode, nid_t ino, umode_t mode);
3173 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
3174 			struct inode *dir, struct inode *inode);
3175 bool f2fs_empty_inline_dir(struct inode *dir);
3176 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3177 			struct fscrypt_str *fstr);
3178 int f2fs_inline_data_fiemap(struct inode *inode,
3179 			struct fiemap_extent_info *fieinfo,
3180 			__u64 start, __u64 len);
3181 
3182 /*
3183  * shrinker.c
3184  */
3185 unsigned long f2fs_shrink_count(struct shrinker *shrink,
3186 			struct shrink_control *sc);
3187 unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3188 			struct shrink_control *sc);
3189 void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3190 void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
3191 
3192 /*
3193  * extent_cache.c
3194  */
3195 struct rb_entry *__lookup_rb_tree(struct rb_root *root,
3196 				struct rb_entry *cached_re, unsigned int ofs);
3197 struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
3198 				struct rb_root *root, struct rb_node **parent,
3199 				unsigned int ofs);
3200 struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
3201 		struct rb_entry *cached_re, unsigned int ofs,
3202 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3203 		struct rb_node ***insert_p, struct rb_node **insert_parent,
3204 		bool force);
3205 bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
3206 						struct rb_root *root);
3207 unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3208 bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3209 void f2fs_drop_extent_tree(struct inode *inode);
3210 unsigned int f2fs_destroy_extent_node(struct inode *inode);
3211 void f2fs_destroy_extent_tree(struct inode *inode);
3212 bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3213 			struct extent_info *ei);
3214 void f2fs_update_extent_cache(struct dnode_of_data *dn);
3215 void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3216 			pgoff_t fofs, block_t blkaddr, unsigned int len);
3217 void init_extent_cache_info(struct f2fs_sb_info *sbi);
3218 int __init create_extent_cache(void);
3219 void destroy_extent_cache(void);
3220 
3221 /*
3222  * sysfs.c
3223  */
3224 int __init f2fs_init_sysfs(void);
3225 void f2fs_exit_sysfs(void);
3226 int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3227 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
3228 
3229 /*
3230  * crypto support
3231  */
f2fs_encrypted_inode(struct inode * inode)3232 static inline bool f2fs_encrypted_inode(struct inode *inode)
3233 {
3234 	return file_is_encrypt(inode);
3235 }
3236 
f2fs_encrypted_file(struct inode * inode)3237 static inline bool f2fs_encrypted_file(struct inode *inode)
3238 {
3239 	return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
3240 }
3241 
f2fs_set_encrypted_inode(struct inode * inode)3242 static inline void f2fs_set_encrypted_inode(struct inode *inode)
3243 {
3244 #ifdef CONFIG_F2FS_FS_ENCRYPTION
3245 	file_set_encrypt(inode);
3246 	inode->i_flags |= S_ENCRYPTED;
3247 #endif
3248 }
3249 
3250 /*
3251  * Returns true if the reads of the inode's data need to undergo some
3252  * postprocessing step, like decryption or authenticity verification.
3253  */
f2fs_post_read_required(struct inode * inode)3254 static inline bool f2fs_post_read_required(struct inode *inode)
3255 {
3256 	return f2fs_encrypted_file(inode);
3257 }
3258 
3259 #define F2FS_FEATURE_FUNCS(name, flagname) \
3260 static inline int f2fs_sb_has_##name(struct super_block *sb) \
3261 { \
3262 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_##flagname); \
3263 }
3264 
3265 F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3266 F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3267 F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3268 F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3269 F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3270 F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3271 F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3272 F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
3273 F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
3274 
3275 #ifdef CONFIG_BLK_DEV_ZONED
get_blkz_type(struct f2fs_sb_info * sbi,struct block_device * bdev,block_t blkaddr)3276 static inline int get_blkz_type(struct f2fs_sb_info *sbi,
3277 			struct block_device *bdev, block_t blkaddr)
3278 {
3279 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
3280 	int i;
3281 
3282 	for (i = 0; i < sbi->s_ndevs; i++)
3283 		if (FDEV(i).bdev == bdev)
3284 			return FDEV(i).blkz_type[zno];
3285 	return -EINVAL;
3286 }
3287 #endif
3288 
f2fs_discard_en(struct f2fs_sb_info * sbi)3289 static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
3290 {
3291 	struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
3292 
3293 	return blk_queue_discard(q) || f2fs_sb_has_blkzoned(sbi->sb);
3294 }
3295 
set_opt_mode(struct f2fs_sb_info * sbi,unsigned int mt)3296 static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
3297 {
3298 	clear_opt(sbi, ADAPTIVE);
3299 	clear_opt(sbi, LFS);
3300 
3301 	switch (mt) {
3302 	case F2FS_MOUNT_ADAPTIVE:
3303 		set_opt(sbi, ADAPTIVE);
3304 		break;
3305 	case F2FS_MOUNT_LFS:
3306 		set_opt(sbi, LFS);
3307 		break;
3308 	}
3309 }
3310 
f2fs_may_encrypt(struct inode * inode)3311 static inline bool f2fs_may_encrypt(struct inode *inode)
3312 {
3313 #ifdef CONFIG_F2FS_FS_ENCRYPTION
3314 	umode_t mode = inode->i_mode;
3315 
3316 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3317 #else
3318 	return 0;
3319 #endif
3320 }
3321 
f2fs_force_buffered_io(struct inode * inode,int rw)3322 static inline bool f2fs_force_buffered_io(struct inode *inode, int rw)
3323 {
3324 	return (f2fs_post_read_required(inode) ||
3325 			(rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
3326 			F2FS_I_SB(inode)->s_ndevs);
3327 }
3328 
3329 #endif
3330