]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - fs/f2fs/f2fs.h
f2fs: make max inline size changeable
[mirror_ubuntu-focal-kernel.git] / fs / f2fs / f2fs.h
CommitLineData
0a8165d7 1/*
39a53e0c
JK
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>
39a53e0c
JK
17#include <linux/slab.h>
18#include <linux/crc32.h>
19#include <linux/magic.h>
c2d715d1 20#include <linux/kobject.h>
7bd59381 21#include <linux/sched.h>
39307a8e 22#include <linux/vmalloc.h>
740432f8 23#include <linux/bio.h>
d0239e1b 24#include <linux/blkdev.h>
0abd675e 25#include <linux/quotaops.h>
46f47e48
EB
26#ifdef CONFIG_F2FS_FS_ENCRYPTION
27#include <linux/fscrypt_supp.h>
28#else
29#include <linux/fscrypt_notsupp.h>
30#endif
43b6573b 31#include <crypto/hash.h>
39a53e0c 32
5d56b671 33#ifdef CONFIG_F2FS_CHECK_FS
9850cf4a 34#define f2fs_bug_on(sbi, condition) BUG_ON(condition)
5d56b671 35#else
9850cf4a
JK
36#define f2fs_bug_on(sbi, condition) \
37 do { \
38 if (unlikely(condition)) { \
39 WARN_ON(1); \
caf0047e 40 set_sbi_flag(sbi, SBI_NEED_FSCK); \
9850cf4a
JK
41 } \
42 } while (0)
5d56b671
JK
43#endif
44
2c63fead
JK
45#ifdef CONFIG_F2FS_FAULT_INJECTION
46enum {
47 FAULT_KMALLOC,
c41f3cc3 48 FAULT_PAGE_ALLOC,
cb78942b
JK
49 FAULT_ALLOC_NID,
50 FAULT_ORPHAN,
51 FAULT_BLOCK,
52 FAULT_DIR_DEPTH,
53aa6bbf 53 FAULT_EVICT_INODE,
14b44d23 54 FAULT_TRUNCATE,
8b038c70 55 FAULT_IO,
0f348028 56 FAULT_CHECKPOINT,
2c63fead
JK
57 FAULT_MAX,
58};
59
08796897
SY
60struct f2fs_fault_info {
61 atomic_t inject_ops;
62 unsigned int inject_rate;
63 unsigned int inject_type;
64};
65
2c63fead 66extern char *fault_name[FAULT_MAX];
68afcf2d 67#define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
2c63fead
JK
68#endif
69
39a53e0c
JK
70/*
71 * For mount options
72 */
73#define F2FS_MOUNT_BG_GC 0x00000001
74#define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
75#define F2FS_MOUNT_DISCARD 0x00000004
76#define F2FS_MOUNT_NOHEAP 0x00000008
77#define F2FS_MOUNT_XATTR_USER 0x00000010
78#define F2FS_MOUNT_POSIX_ACL 0x00000020
79#define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
444c580f 80#define F2FS_MOUNT_INLINE_XATTR 0x00000080
1001b347 81#define F2FS_MOUNT_INLINE_DATA 0x00000100
34d67deb
CY
82#define F2FS_MOUNT_INLINE_DENTRY 0x00000200
83#define F2FS_MOUNT_FLUSH_MERGE 0x00000400
84#define F2FS_MOUNT_NOBARRIER 0x00000800
d5053a34 85#define F2FS_MOUNT_FASTBOOT 0x00001000
89672159 86#define F2FS_MOUNT_EXTENT_CACHE 0x00002000
6aefd93b 87#define F2FS_MOUNT_FORCE_FG_GC 0x00004000
343f40f0 88#define F2FS_MOUNT_DATA_FLUSH 0x00008000
73faec4d 89#define F2FS_MOUNT_FAULT_INJECTION 0x00010000
36abef4e
JK
90#define F2FS_MOUNT_ADAPTIVE 0x00020000
91#define F2FS_MOUNT_LFS 0x00040000
0abd675e
CY
92#define F2FS_MOUNT_USRQUOTA 0x00080000
93#define F2FS_MOUNT_GRPQUOTA 0x00100000
39a53e0c 94
68afcf2d
TK
95#define clear_opt(sbi, option) ((sbi)->mount_opt.opt &= ~F2FS_MOUNT_##option)
96#define set_opt(sbi, option) ((sbi)->mount_opt.opt |= F2FS_MOUNT_##option)
97#define test_opt(sbi, option) ((sbi)->mount_opt.opt & F2FS_MOUNT_##option)
39a53e0c
JK
98
99#define ver_after(a, b) (typecheck(unsigned long long, a) && \
100 typecheck(unsigned long long, b) && \
101 ((long long)((a) - (b)) > 0))
102
a9841c4d
JK
103typedef u32 block_t; /*
104 * should not change u32, since it is the on-disk block
105 * address format, __le32.
106 */
39a53e0c
JK
107typedef u32 nid_t;
108
109struct f2fs_mount_info {
110 unsigned int opt;
111};
112
cde4de12 113#define F2FS_FEATURE_ENCRYPT 0x0001
0bfd7a09 114#define F2FS_FEATURE_BLKZONED 0x0002
e65ef207 115#define F2FS_FEATURE_ATOMIC_WRITE 0x0004
cde4de12 116
76f105a2
JK
117#define F2FS_HAS_FEATURE(sb, mask) \
118 ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
119#define F2FS_SET_FEATURE(sb, mask) \
c64ab12e 120 (F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
76f105a2 121#define F2FS_CLEAR_FEATURE(sb, mask) \
c64ab12e 122 (F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
76f105a2 123
39a53e0c
JK
124/*
125 * For checkpoint manager
126 */
127enum {
128 NAT_BITMAP,
129 SIT_BITMAP
130};
131
c473f1a9
CY
132#define CP_UMOUNT 0x00000001
133#define CP_FASTBOOT 0x00000002
134#define CP_SYNC 0x00000004
135#define CP_RECOVERY 0x00000008
136#define CP_DISCARD 0x00000010
1f43e2ad 137#define CP_TRIMMED 0x00000020
75ab4cb8 138
47b89808 139#define DEF_BATCHED_TRIM_SECTIONS 2048
bba681cb 140#define BATCHED_TRIM_SEGMENTS(sbi) \
4ddb1a4d 141 (GET_SEG_FROM_SEC(sbi, SM_I(sbi)->trim_sections))
a66cdd98
JK
142#define BATCHED_TRIM_BLOCKS(sbi) \
143 (BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
4ddb1a4d
JK
144#define MAX_DISCARD_BLOCKS(sbi) BLKS_PER_SEC(sbi)
145#define DISCARD_ISSUE_RATE 8
60b99b48 146#define DEF_CP_INTERVAL 60 /* 60 secs */
dcf25fe8 147#define DEF_IDLE_INTERVAL 5 /* 5 secs */
bba681cb 148
75ab4cb8
JK
149struct cp_control {
150 int reason;
4b2fecc8
JK
151 __u64 trim_start;
152 __u64 trim_end;
153 __u64 trim_minlen;
154 __u64 trimmed;
75ab4cb8
JK
155};
156
662befda 157/*
81c1a0f1 158 * For CP/NAT/SIT/SSA readahead
662befda
CY
159 */
160enum {
161 META_CP,
162 META_NAT,
81c1a0f1 163 META_SIT,
4c521f49
JK
164 META_SSA,
165 META_POR,
662befda
CY
166};
167
6451e041
JK
168/* for the list of ino */
169enum {
170 ORPHAN_INO, /* for orphan ino list */
fff04f90
JK
171 APPEND_INO, /* for append ino list */
172 UPDATE_INO, /* for update ino list */
6451e041
JK
173 MAX_INO_ENTRY, /* max. list */
174};
175
176struct ino_entry {
39a53e0c
JK
177 struct list_head list; /* list head */
178 nid_t ino; /* inode number */
179};
180
2710fd7e 181/* for the list of inodes to be GCed */
06292073 182struct inode_entry {
39a53e0c
JK
183 struct list_head list; /* list head */
184 struct inode *inode; /* vfs inode pointer */
185};
186
a7eeb823 187/* for the bitmap indicate blocks to be discarded */
7fd9e544
JK
188struct discard_entry {
189 struct list_head list; /* list head */
a7eeb823
CY
190 block_t start_blkaddr; /* start blockaddr of current segment */
191 unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */
7fd9e544
JK
192};
193
ba48a33e
CY
194/* max discard pend list number */
195#define MAX_PLIST_NUM 512
196#define plist_idx(blk_num) ((blk_num) >= MAX_PLIST_NUM ? \
197 (MAX_PLIST_NUM - 1) : (blk_num - 1))
198
15469963
JK
199enum {
200 D_PREP,
201 D_SUBMIT,
202 D_DONE,
203};
204
004b6862
CY
205struct discard_info {
206 block_t lstart; /* logical start address */
207 block_t len; /* length */
208 block_t start; /* actual start address in dev */
209};
210
b01a9201 211struct discard_cmd {
004b6862
CY
212 struct rb_node rb_node; /* rb node located in rb-tree */
213 union {
214 struct {
215 block_t lstart; /* logical start address */
216 block_t len; /* length */
217 block_t start; /* actual start address in dev */
218 };
219 struct discard_info di; /* discard info */
220
221 };
b01a9201
JK
222 struct list_head list; /* command list */
223 struct completion wait; /* compleation */
c81abe34 224 struct block_device *bdev; /* bdev */
ec9895ad 225 unsigned short ref; /* reference count */
9a744b92 226 unsigned char state; /* state */
c81abe34 227 int error; /* bio error */
275b66b0
CY
228};
229
0b54fb84 230struct discard_cmd_control {
15469963 231 struct task_struct *f2fs_issue_discard; /* discard thread */
46f84c2c 232 struct list_head entry_list; /* 4KB discard entry list */
ba48a33e 233 struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
46f84c2c 234 struct list_head wait_list; /* store on-flushing entries */
15469963
JK
235 wait_queue_head_t discard_wait_queue; /* waiting queue for wake-up */
236 struct mutex cmd_lock;
d618ebaf
CY
237 unsigned int nr_discards; /* # of discards in the list */
238 unsigned int max_discards; /* max. discards to be issued */
d84d1cbd 239 unsigned int undiscard_blks; /* # of undiscard blocks */
8b8dd65f
CY
240 atomic_t issued_discard; /* # of issued discard */
241 atomic_t issing_discard; /* # of issing discard */
5f32366a 242 atomic_t discard_cmd_cnt; /* # of cached cmd count */
004b6862 243 struct rb_root root; /* root of discard rb-tree */
275b66b0
CY
244};
245
39a53e0c
JK
246/* for the list of fsync inodes, used only during recovery */
247struct fsync_inode_entry {
248 struct list_head list; /* list head */
249 struct inode *inode; /* vfs inode pointer */
c52e1b10
JK
250 block_t blkaddr; /* block address locating the last fsync */
251 block_t last_dentry; /* block address locating the last dentry */
39a53e0c
JK
252};
253
68afcf2d
TK
254#define nats_in_cursum(jnl) (le16_to_cpu((jnl)->n_nats))
255#define sits_in_cursum(jnl) (le16_to_cpu((jnl)->n_sits))
39a53e0c 256
68afcf2d
TK
257#define nat_in_journal(jnl, i) ((jnl)->nat_j.entries[i].ne)
258#define nid_in_journal(jnl, i) ((jnl)->nat_j.entries[i].nid)
259#define sit_in_journal(jnl, i) ((jnl)->sit_j.entries[i].se)
260#define segno_in_journal(jnl, i) ((jnl)->sit_j.entries[i].segno)
39a53e0c 261
dfc08a12
CY
262#define MAX_NAT_JENTRIES(jnl) (NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
263#define MAX_SIT_JENTRIES(jnl) (SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
309cc2b6 264
dfc08a12 265static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
39a53e0c 266{
dfc08a12 267 int before = nats_in_cursum(journal);
cac5a3d8 268
dfc08a12 269 journal->n_nats = cpu_to_le16(before + i);
39a53e0c
JK
270 return before;
271}
272
dfc08a12 273static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
39a53e0c 274{
dfc08a12 275 int before = sits_in_cursum(journal);
cac5a3d8 276
dfc08a12 277 journal->n_sits = cpu_to_le16(before + i);
39a53e0c
JK
278 return before;
279}
280
dfc08a12
CY
281static inline bool __has_cursum_space(struct f2fs_journal *journal,
282 int size, int type)
184a5cd2
CY
283{
284 if (type == NAT_JOURNAL)
dfc08a12
CY
285 return size <= MAX_NAT_JENTRIES(journal);
286 return size <= MAX_SIT_JENTRIES(journal);
184a5cd2
CY
287}
288
e9750824
NJ
289/*
290 * ioctl commands
291 */
88b88a66
JK
292#define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
293#define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
d49f3e89 294#define F2FS_IOC_GETVERSION FS_IOC_GETVERSION
88b88a66
JK
295
296#define F2FS_IOCTL_MAGIC 0xf5
297#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
298#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
02a1335f 299#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
1e84371f
JK
300#define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
301#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
d07efb50 302#define F2FS_IOC_GARBAGE_COLLECT _IOW(F2FS_IOCTL_MAGIC, 6, __u32)
456b88e4 303#define F2FS_IOC_WRITE_CHECKPOINT _IO(F2FS_IOCTL_MAGIC, 7)
d07efb50
JK
304#define F2FS_IOC_DEFRAGMENT _IOWR(F2FS_IOCTL_MAGIC, 8, \
305 struct f2fs_defragment)
4dd6f977
JK
306#define F2FS_IOC_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
307 struct f2fs_move_range)
e066b83c
JK
308#define F2FS_IOC_FLUSH_DEVICE _IOW(F2FS_IOCTL_MAGIC, 10, \
309 struct f2fs_flush_device)
34dc77ad
JK
310#define F2FS_IOC_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11, \
311 struct f2fs_gc_range)
e65ef207 312#define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, __u32)
e9750824 313
0b81d077
JK
314#define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
315#define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
316#define F2FS_IOC_GET_ENCRYPTION_PWSALT FS_IOC_GET_ENCRYPTION_PWSALT
f424f664 317
1abff93d
JK
318/*
319 * should be same as XFS_IOC_GOINGDOWN.
320 * Flags for going down operation used by FS_IOC_GOINGDOWN
321 */
322#define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
323#define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
324#define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
325#define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
c912a829 326#define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */
1abff93d 327
e9750824
NJ
328#if defined(__KERNEL__) && defined(CONFIG_COMPAT)
329/*
330 * ioctl commands in 32 bit emulation
331 */
04ef4b62
CY
332#define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
333#define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
334#define F2FS_IOC32_GETVERSION FS_IOC32_GETVERSION
e9750824
NJ
335#endif
336
34dc77ad
JK
337struct f2fs_gc_range {
338 u32 sync;
339 u64 start;
340 u64 len;
341};
342
d323d005
CY
343struct f2fs_defragment {
344 u64 start;
345 u64 len;
346};
347
4dd6f977
JK
348struct f2fs_move_range {
349 u32 dst_fd; /* destination fd */
350 u64 pos_in; /* start position in src_fd */
351 u64 pos_out; /* start position in dst_fd */
352 u64 len; /* size to move */
353};
354
e066b83c
JK
355struct f2fs_flush_device {
356 u32 dev_num; /* device number to flush */
357 u32 segments; /* # of segments to flush */
358};
359
f2470371
CY
360/* for inline stuff */
361#define DEF_INLINE_RESERVED_SIZE 1
362
363static inline int get_inline_reserved_size(struct inode *inode);
364#define MAX_INLINE_DATA(inode) (sizeof(__le32) * (DEF_ADDRS_PER_INODE -\
365 get_inline_reserved_size(inode) -\
366 F2FS_INLINE_XATTR_ADDRS))
367
368/* for inline dir */
369#define NR_INLINE_DENTRY(inode) (MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
370 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
371 BITS_PER_BYTE + 1))
372#define INLINE_DENTRY_BITMAP_SIZE(inode) ((NR_INLINE_DENTRY(inode) + \
373 BITS_PER_BYTE - 1) / BITS_PER_BYTE)
374#define INLINE_RESERVED_SIZE(inode) (MAX_INLINE_DATA(inode) - \
375 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
376 NR_INLINE_DENTRY(inode) + \
377 INLINE_DENTRY_BITMAP_SIZE(inode)))
378
39a53e0c
JK
379/*
380 * For INODE and NODE manager
381 */
7b3cd7d6
JK
382/* for directory operations */
383struct f2fs_dentry_ptr {
d8c6822a 384 struct inode *inode;
76a9dd85 385 void *bitmap;
7b3cd7d6
JK
386 struct f2fs_dir_entry *dentry;
387 __u8 (*filename)[F2FS_SLOT_LEN];
388 int max;
76a9dd85 389 int nr_bitmap;
7b3cd7d6
JK
390};
391
64c24ecb
TK
392static inline void make_dentry_ptr_block(struct inode *inode,
393 struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
7b3cd7d6 394{
d8c6822a 395 d->inode = inode;
64c24ecb 396 d->max = NR_DENTRY_IN_BLOCK;
76a9dd85 397 d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
64c24ecb
TK
398 d->bitmap = &t->dentry_bitmap;
399 d->dentry = t->dentry;
400 d->filename = t->filename;
401}
d8c6822a 402
64c24ecb 403static inline void make_dentry_ptr_inline(struct inode *inode,
f2470371 404 struct f2fs_dentry_ptr *d, void *t)
64c24ecb 405{
f2470371
CY
406 int entry_cnt = NR_INLINE_DENTRY(inode);
407 int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
408 int reserved_size = INLINE_RESERVED_SIZE(inode);
409
64c24ecb 410 d->inode = inode;
f2470371
CY
411 d->max = entry_cnt;
412 d->nr_bitmap = bitmap_size;
413 d->bitmap = t;
414 d->dentry = t + bitmap_size + reserved_size;
415 d->filename = t + bitmap_size + reserved_size +
416 SIZE_OF_DIR_ENTRY * entry_cnt;
7b3cd7d6
JK
417}
418
dbe6a5ff
JK
419/*
420 * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
421 * as its node offset to distinguish from index node blocks.
422 * But some bits are used to mark the node block.
423 */
424#define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
425 >> OFFSET_BIT_SHIFT)
266e97a8
JK
426enum {
427 ALLOC_NODE, /* allocate a new node page if needed */
428 LOOKUP_NODE, /* look up a node without readahead */
429 LOOKUP_NODE_RA, /*
430 * look up a node with readahead called
4f4124d0 431 * by get_data_block.
39a53e0c 432 */
266e97a8
JK
433};
434
a6db67f0 435#define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */
39a53e0c 436
817202d9
CY
437#define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
438
13054c54
CY
439/* vector size for gang look-up from extent cache that consists of radix tree */
440#define EXT_TREE_VEC_SIZE 64
441
39a53e0c 442/* for in-memory extent cache entry */
13054c54
CY
443#define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */
444
445/* number of extent info in extent cache we try to shrink */
446#define EXTENT_CACHE_SHRINK_NUMBER 128
c11abd1a 447
54c2258c
CY
448struct rb_entry {
449 struct rb_node rb_node; /* rb node located in rb-tree */
450 unsigned int ofs; /* start offset of the entry */
451 unsigned int len; /* length of the entry */
452};
453
39a53e0c 454struct extent_info {
13054c54 455 unsigned int fofs; /* start offset in a file */
13054c54 456 unsigned int len; /* length of the extent */
54c2258c 457 u32 blk; /* start block address of the extent */
13054c54
CY
458};
459
460struct extent_node {
54c2258c
CY
461 struct rb_node rb_node;
462 union {
463 struct {
464 unsigned int fofs;
465 unsigned int len;
466 u32 blk;
467 };
468 struct extent_info ei; /* extent info */
469
470 };
13054c54 471 struct list_head list; /* node in global extent list of sbi */
201ef5e0 472 struct extent_tree *et; /* extent tree pointer */
13054c54
CY
473};
474
475struct extent_tree {
476 nid_t ino; /* inode number */
477 struct rb_root root; /* root of extent info rb-tree */
62c8af65 478 struct extent_node *cached_en; /* recently accessed extent node */
3e72f721 479 struct extent_info largest; /* largested extent info */
137d09f0 480 struct list_head list; /* to be used by sbi->zombie_list */
13054c54 481 rwlock_t lock; /* protect extent info rb-tree */
68e35385 482 atomic_t node_cnt; /* # of extent node in rb-tree*/
39a53e0c
JK
483};
484
003a3e1d
JK
485/*
486 * This structure is taken from ext4_map_blocks.
487 *
488 * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
489 */
490#define F2FS_MAP_NEW (1 << BH_New)
491#define F2FS_MAP_MAPPED (1 << BH_Mapped)
7f63eb77
JK
492#define F2FS_MAP_UNWRITTEN (1 << BH_Unwritten)
493#define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
494 F2FS_MAP_UNWRITTEN)
003a3e1d
JK
495
496struct f2fs_map_blocks {
497 block_t m_pblk;
498 block_t m_lblk;
499 unsigned int m_len;
500 unsigned int m_flags;
da85985c 501 pgoff_t *m_next_pgofs; /* point next possible non-hole pgofs */
003a3e1d
JK
502};
503
e2b4e2bc
CY
504/* for flag in get_data_block */
505#define F2FS_GET_BLOCK_READ 0
506#define F2FS_GET_BLOCK_DIO 1
507#define F2FS_GET_BLOCK_FIEMAP 2
508#define F2FS_GET_BLOCK_BMAP 3
b439b103 509#define F2FS_GET_BLOCK_PRE_DIO 4
24b84912 510#define F2FS_GET_BLOCK_PRE_AIO 5
e2b4e2bc 511
39a53e0c
JK
512/*
513 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
514 */
515#define FADVISE_COLD_BIT 0x01
354a3399 516#define FADVISE_LOST_PINO_BIT 0x02
cde4de12 517#define FADVISE_ENCRYPT_BIT 0x04
e7d55452 518#define FADVISE_ENC_NAME_BIT 0x08
26787236 519#define FADVISE_KEEP_SIZE_BIT 0x10
39a53e0c 520
b5492af7
JK
521#define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
522#define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
523#define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
524#define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
525#define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
526#define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
cde4de12
JK
527#define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT)
528#define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
529#define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
e7d55452
JK
530#define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT)
531#define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
26787236
JK
532#define file_keep_isize(inode) is_file(inode, FADVISE_KEEP_SIZE_BIT)
533#define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
cde4de12 534
ab9fa662
JK
535#define DEF_DIR_LEVEL 0
536
39a53e0c
JK
537struct f2fs_inode_info {
538 struct inode vfs_inode; /* serve a vfs inode */
539 unsigned long i_flags; /* keep an inode flags for ioctl */
540 unsigned char i_advise; /* use to give file attribute hints */
38431545 541 unsigned char i_dir_level; /* use for dentry level for large dir */
39a53e0c 542 unsigned int i_current_depth; /* use only in directory structure */
6666e6aa 543 unsigned int i_pino; /* parent inode number */
39a53e0c
JK
544 umode_t i_acl_mode; /* keep file acl mode temporarily */
545
546 /* Use below internally in f2fs*/
547 unsigned long flags; /* use to pass per-file flags */
d928bfbf 548 struct rw_semaphore i_sem; /* protect fi info */
204706c7 549 atomic_t dirty_pages; /* # of dirty pages */
39a53e0c
JK
550 f2fs_hash_t chash; /* hash value of given file name */
551 unsigned int clevel; /* maximum level of given file name */
88c5c13a 552 struct task_struct *task; /* lookup and create consistency */
39a53e0c 553 nid_t i_xattr_nid; /* node id that contains xattrs */
26de9b11 554 loff_t last_disk_size; /* lastly written file size */
88b88a66 555
0abd675e
CY
556#ifdef CONFIG_QUOTA
557 struct dquot *i_dquot[MAXQUOTAS];
558
559 /* quota space reservation, managed internally by quota code */
560 qsize_t i_reserved_quota;
561#endif
0f18b462
JK
562 struct list_head dirty_list; /* dirty list for dirs and files */
563 struct list_head gdirty_list; /* linked in global dirty list */
88b88a66 564 struct list_head inmem_pages; /* inmemory pages managed by f2fs */
7a10f017 565 struct task_struct *inmem_task; /* store inmemory task */
88b88a66 566 struct mutex inmem_lock; /* lock for inmemory pages */
3e72f721 567 struct extent_tree *extent_tree; /* cached extent_tree entry */
82e0a5aa 568 struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
5a3a2d83 569 struct rw_semaphore i_mmap_sem;
f2470371
CY
570
571 int i_inline_reserved; /* reserved size in inline data */
39a53e0c
JK
572};
573
574static inline void get_extent_info(struct extent_info *ext,
bd933d4f 575 struct f2fs_extent *i_ext)
39a53e0c 576{
bd933d4f
CY
577 ext->fofs = le32_to_cpu(i_ext->fofs);
578 ext->blk = le32_to_cpu(i_ext->blk);
579 ext->len = le32_to_cpu(i_ext->len);
39a53e0c
JK
580}
581
582static inline void set_raw_extent(struct extent_info *ext,
583 struct f2fs_extent *i_ext)
584{
39a53e0c 585 i_ext->fofs = cpu_to_le32(ext->fofs);
4d0b0bd4 586 i_ext->blk = cpu_to_le32(ext->blk);
39a53e0c 587 i_ext->len = cpu_to_le32(ext->len);
39a53e0c
JK
588}
589
429511cd
CY
590static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
591 u32 blk, unsigned int len)
592{
593 ei->fofs = fofs;
594 ei->blk = blk;
595 ei->len = len;
596}
597
004b6862
CY
598static inline bool __is_discard_mergeable(struct discard_info *back,
599 struct discard_info *front)
600{
601 return back->lstart + back->len == front->lstart;
602}
603
604static inline bool __is_discard_back_mergeable(struct discard_info *cur,
605 struct discard_info *back)
606{
607 return __is_discard_mergeable(back, cur);
608}
609
610static inline bool __is_discard_front_mergeable(struct discard_info *cur,
611 struct discard_info *front)
612{
613 return __is_discard_mergeable(cur, front);
614}
615
429511cd
CY
616static inline bool __is_extent_mergeable(struct extent_info *back,
617 struct extent_info *front)
618{
619 return (back->fofs + back->len == front->fofs &&
620 back->blk + back->len == front->blk);
621}
622
623static inline bool __is_back_mergeable(struct extent_info *cur,
624 struct extent_info *back)
625{
626 return __is_extent_mergeable(back, cur);
627}
628
629static inline bool __is_front_mergeable(struct extent_info *cur,
630 struct extent_info *front)
631{
632 return __is_extent_mergeable(cur, front);
633}
634
cac5a3d8 635extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
205b9822
JK
636static inline void __try_update_largest_extent(struct inode *inode,
637 struct extent_tree *et, struct extent_node *en)
4abd3f5a 638{
205b9822 639 if (en->ei.len > et->largest.len) {
4abd3f5a 640 et->largest = en->ei;
7c45729a 641 f2fs_mark_inode_dirty_sync(inode, true);
205b9822 642 }
4abd3f5a
CY
643}
644
b8559dc2
CY
645enum nid_list {
646 FREE_NID_LIST,
647 ALLOC_NID_LIST,
648 MAX_NID_LIST,
649};
650
39a53e0c
JK
651struct f2fs_nm_info {
652 block_t nat_blkaddr; /* base disk address of NAT */
653 nid_t max_nid; /* maximum possible node ids */
04d47e67 654 nid_t available_nids; /* # of available node ids */
39a53e0c 655 nid_t next_scan_nid; /* the next nid to be scanned */
cdfc41c1 656 unsigned int ram_thresh; /* control the memory footprint */
ea1a29a0 657 unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */
2304cb0c 658 unsigned int dirty_nats_ratio; /* control dirty nats ratio threshold */
39a53e0c
JK
659
660 /* NAT cache management */
661 struct radix_tree_root nat_root;/* root of the nat entry cache */
309cc2b6 662 struct radix_tree_root nat_set_root;/* root of the nat set cache */
b873b798 663 struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */
39a53e0c 664 struct list_head nat_entries; /* cached nat entry list (clean) */
309cc2b6 665 unsigned int nat_cnt; /* the # of cached nat entries */
aec71382 666 unsigned int dirty_nat_cnt; /* total num of nat entries in set */
22ad0b6a 667 unsigned int nat_blocks; /* # of nat blocks */
39a53e0c
JK
668
669 /* free node ids management */
8a7ed66a 670 struct radix_tree_root free_nid_root;/* root of the free_nid cache */
b8559dc2
CY
671 struct list_head nid_list[MAX_NID_LIST];/* lists for free nids */
672 unsigned int nid_cnt[MAX_NID_LIST]; /* the number of free node id */
673 spinlock_t nid_list_lock; /* protect nid lists ops */
39a53e0c 674 struct mutex build_lock; /* lock for build free nids */
4ac91242
CY
675 unsigned char (*free_nid_bitmap)[NAT_ENTRY_BITMAP_SIZE];
676 unsigned char *nat_block_bitmap;
586d1492 677 unsigned short *free_nid_count; /* free nid count of NAT block */
39a53e0c
JK
678
679 /* for checkpoint */
680 char *nat_bitmap; /* NAT bitmap pointer */
22ad0b6a
JK
681
682 unsigned int nat_bits_blocks; /* # of nat bits blocks */
683 unsigned char *nat_bits; /* NAT bits blocks */
684 unsigned char *full_nat_bits; /* full NAT pages */
685 unsigned char *empty_nat_bits; /* empty NAT pages */
599a09b2
CY
686#ifdef CONFIG_F2FS_CHECK_FS
687 char *nat_bitmap_mir; /* NAT bitmap mirror */
688#endif
39a53e0c
JK
689 int bitmap_size; /* bitmap size */
690};
691
692/*
693 * this structure is used as one of function parameters.
694 * all the information are dedicated to a given direct node block determined
695 * by the data offset in a file.
696 */
697struct dnode_of_data {
698 struct inode *inode; /* vfs inode pointer */
699 struct page *inode_page; /* its inode page, NULL is possible */
700 struct page *node_page; /* cached direct node page */
701 nid_t nid; /* node id of the direct node block */
702 unsigned int ofs_in_node; /* data offset in the node page */
703 bool inode_page_locked; /* inode page is locked or not */
93bae099 704 bool node_changed; /* is node block changed */
3cf45747
CY
705 char cur_level; /* level of hole node page */
706 char max_level; /* level of current page located */
39a53e0c
JK
707 block_t data_blkaddr; /* block address of the node block */
708};
709
710static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
711 struct page *ipage, struct page *npage, nid_t nid)
712{
d66d1f76 713 memset(dn, 0, sizeof(*dn));
39a53e0c
JK
714 dn->inode = inode;
715 dn->inode_page = ipage;
716 dn->node_page = npage;
717 dn->nid = nid;
39a53e0c
JK
718}
719
720/*
721 * For SIT manager
722 *
723 * By default, there are 6 active log areas across the whole main area.
724 * When considering hot and cold data separation to reduce cleaning overhead,
725 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
726 * respectively.
727 * In the current design, you should not change the numbers intentionally.
728 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
729 * logs individually according to the underlying devices. (default: 6)
730 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
731 * data and 8 for node logs.
732 */
733#define NR_CURSEG_DATA_TYPE (3)
734#define NR_CURSEG_NODE_TYPE (3)
735#define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
736
737enum {
738 CURSEG_HOT_DATA = 0, /* directory entry blocks */
739 CURSEG_WARM_DATA, /* data blocks */
740 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
741 CURSEG_HOT_NODE, /* direct node blocks of directory files */
742 CURSEG_WARM_NODE, /* direct node blocks of normal files */
743 CURSEG_COLD_NODE, /* indirect node blocks */
38aa0889 744 NO_CHECK_TYPE,
39a53e0c
JK
745};
746
6b4afdd7 747struct flush_cmd {
6b4afdd7 748 struct completion wait;
721bd4d5 749 struct llist_node llnode;
6b4afdd7
JK
750 int ret;
751};
752
a688b9d9
GZ
753struct flush_cmd_control {
754 struct task_struct *f2fs_issue_flush; /* flush thread */
755 wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
8b8dd65f
CY
756 atomic_t issued_flush; /* # of issued flushes */
757 atomic_t issing_flush; /* # of issing flushes */
721bd4d5
GZ
758 struct llist_head issue_list; /* list for command issue */
759 struct llist_node *dispatch_list; /* list for command dispatch */
a688b9d9
GZ
760};
761
39a53e0c
JK
762struct f2fs_sm_info {
763 struct sit_info *sit_info; /* whole segment information */
764 struct free_segmap_info *free_info; /* free segment information */
765 struct dirty_seglist_info *dirty_info; /* dirty segment information */
766 struct curseg_info *curseg_array; /* active segment information */
767
39a53e0c
JK
768 block_t seg0_blkaddr; /* block address of 0'th segment */
769 block_t main_blkaddr; /* start block address of main area */
770 block_t ssa_blkaddr; /* start block address of SSA area */
771
772 unsigned int segment_count; /* total # of segments */
773 unsigned int main_segments; /* # of segments in main area */
774 unsigned int reserved_segments; /* # of reserved segments */
775 unsigned int ovp_segments; /* # of overprovision segments */
81eb8d6e
JK
776
777 /* a threshold to reclaim prefree segments */
778 unsigned int rec_prefree_segments;
7fd9e544 779
bba681cb
JK
780 /* for batched trimming */
781 unsigned int trim_sections; /* # of sections to trim */
782
184a5cd2
CY
783 struct list_head sit_entry_set; /* sit entry set list */
784
216fbd64
JK
785 unsigned int ipu_policy; /* in-place-update policy */
786 unsigned int min_ipu_util; /* in-place-update threshold */
c1ce1b02 787 unsigned int min_fsync_blocks; /* threshold for fsync */
ef095d19 788 unsigned int min_hot_blocks; /* threshold for hot block allocation */
6b4afdd7
JK
789
790 /* for flush command control */
b01a9201 791 struct flush_cmd_control *fcc_info;
a688b9d9 792
0b54fb84
JK
793 /* for discard command control */
794 struct discard_cmd_control *dcc_info;
39a53e0c
JK
795};
796
39a53e0c
JK
797/*
798 * For superblock
799 */
800/*
801 * COUNT_TYPE for monitoring
802 *
803 * f2fs monitors the number of several block types such as on-writeback,
804 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
805 */
36951b38 806#define WB_DATA_TYPE(p) (__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
39a53e0c 807enum count_type {
39a53e0c 808 F2FS_DIRTY_DENTS,
c227f912 809 F2FS_DIRTY_DATA,
39a53e0c
JK
810 F2FS_DIRTY_NODES,
811 F2FS_DIRTY_META,
8dcf2ff7 812 F2FS_INMEM_PAGES,
0f18b462 813 F2FS_DIRTY_IMETA,
36951b38
CY
814 F2FS_WB_CP_DATA,
815 F2FS_WB_DATA,
39a53e0c
JK
816 NR_COUNT_TYPE,
817};
818
39a53e0c 819/*
e1c42045 820 * The below are the page types of bios used in submit_bio().
39a53e0c
JK
821 * The available types are:
822 * DATA User data pages. It operates as async mode.
823 * NODE Node pages. It operates as async mode.
824 * META FS metadata pages such as SIT, NAT, CP.
825 * NR_PAGE_TYPE The number of page types.
826 * META_FLUSH Make sure the previous pages are written
827 * with waiting the bio's completion
828 * ... Only can be used with META.
829 */
7d5e5109 830#define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
39a53e0c
JK
831enum page_type {
832 DATA,
833 NODE,
834 META,
835 NR_PAGE_TYPE,
836 META_FLUSH,
8ce67cb0
JK
837 INMEM, /* the below types are used by tracepoints only. */
838 INMEM_DROP,
8c242db9 839 INMEM_INVALIDATE,
28bc106b 840 INMEM_REVOKE,
8ce67cb0
JK
841 IPU,
842 OPU,
39a53e0c
JK
843};
844
a912b54d
JK
845enum temp_type {
846 HOT = 0, /* must be zero for meta bio */
847 WARM,
848 COLD,
849 NR_TEMP_TYPE,
850};
851
cc15620b
JK
852enum need_lock_type {
853 LOCK_REQ = 0,
854 LOCK_DONE,
855 LOCK_RETRY,
856};
857
458e6197 858struct f2fs_io_info {
05ca3632 859 struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
7e8f2308 860 enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
a912b54d 861 enum temp_type temp; /* contains HOT/WARM/COLD */
04d328de 862 int op; /* contains REQ_OP_ */
ef295ecf 863 int op_flags; /* req_flag_bits */
7a9d7548 864 block_t new_blkaddr; /* new block address to be written */
28bc106b 865 block_t old_blkaddr; /* old block address before Cow */
05ca3632 866 struct page *page; /* page to be written */
4375a336 867 struct page *encrypted_page; /* encrypted page */
fb830fc5 868 struct list_head list; /* serialize IOs */
d68f735b 869 bool submitted; /* indicate IO submission */
cc15620b 870 int need_lock; /* indicate we need to lock cp_rwsem */
fb830fc5 871 bool in_list; /* indicate fio is in io_list */
458e6197
JK
872};
873
68afcf2d 874#define is_read_io(rw) ((rw) == READ)
1ff7bd3b 875struct f2fs_bio_info {
458e6197 876 struct f2fs_sb_info *sbi; /* f2fs superblock */
1ff7bd3b
JK
877 struct bio *bio; /* bios to merge */
878 sector_t last_block_in_bio; /* last block number */
458e6197 879 struct f2fs_io_info fio; /* store buffered io info. */
df0f8dc0 880 struct rw_semaphore io_rwsem; /* blocking op for bio */
fb830fc5
CY
881 spinlock_t io_lock; /* serialize DATA/NODE IOs */
882 struct list_head io_list; /* track fios */
1ff7bd3b
JK
883};
884
3c62be17
JK
885#define FDEV(i) (sbi->devs[i])
886#define RDEV(i) (raw_super->devs[i])
887struct f2fs_dev_info {
888 struct block_device *bdev;
889 char path[MAX_PATH_LEN];
890 unsigned int total_segments;
891 block_t start_blk;
892 block_t end_blk;
893#ifdef CONFIG_BLK_DEV_ZONED
894 unsigned int nr_blkz; /* Total number of zones */
895 u8 *blkz_type; /* Array of zones type */
896#endif
897};
898
c227f912
CY
899enum inode_type {
900 DIR_INODE, /* for dirty dir inode */
901 FILE_INODE, /* for dirty regular/symlink inode */
0f18b462 902 DIRTY_META, /* for all dirtied inode metadata */
c227f912
CY
903 NR_INODE_TYPE,
904};
905
67298804
CY
906/* for inner inode cache management */
907struct inode_management {
908 struct radix_tree_root ino_root; /* ino entry array */
909 spinlock_t ino_lock; /* for ino entry lock */
910 struct list_head ino_list; /* inode list head */
911 unsigned long ino_num; /* number of entries */
912};
913
caf0047e
CY
914/* For s_flag in struct f2fs_sb_info */
915enum {
916 SBI_IS_DIRTY, /* dirty flag for checkpoint */
917 SBI_IS_CLOSE, /* specify unmounting */
918 SBI_NEED_FSCK, /* need fsck.f2fs to fix */
919 SBI_POR_DOING, /* recovery is doing or not */
df728b0f 920 SBI_NEED_SB_WRITE, /* need to recover superblock */
bbf156f7 921 SBI_NEED_CP, /* need to checkpoint */
caf0047e
CY
922};
923
6beceb54
JK
924enum {
925 CP_TIME,
d0239e1b 926 REQ_TIME,
6beceb54
JK
927 MAX_TIME,
928};
929
39a53e0c
JK
930struct f2fs_sb_info {
931 struct super_block *sb; /* pointer to VFS super block */
5e176d54 932 struct proc_dir_entry *s_proc; /* proc entry */
39a53e0c 933 struct f2fs_super_block *raw_super; /* raw super block pointer */
e8240f65 934 int valid_super_block; /* valid super block no */
fadb2fb8 935 unsigned long s_flag; /* flags for sbi */
39a53e0c 936
178053e2 937#ifdef CONFIG_BLK_DEV_ZONED
178053e2
DLM
938 unsigned int blocks_per_blkz; /* F2FS blocks per zone */
939 unsigned int log_blocks_per_blkz; /* log2 F2FS blocks per zone */
178053e2
DLM
940#endif
941
39a53e0c
JK
942 /* for node-related operations */
943 struct f2fs_nm_info *nm_info; /* node manager */
944 struct inode *node_inode; /* cache node blocks */
945
946 /* for segment-related operations */
947 struct f2fs_sm_info *sm_info; /* segment manager */
1ff7bd3b
JK
948
949 /* for bio operations */
a912b54d 950 struct f2fs_bio_info *write_io[NR_PAGE_TYPE]; /* for write bios */
e41e6d75
CY
951 struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
952 /* bio ordering for NODE/DATA */
0a595eba
JK
953 int write_io_size_bits; /* Write IO size bits */
954 mempool_t *write_io_dummy; /* Dummy pages */
39a53e0c
JK
955
956 /* for checkpoint */
957 struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
8508e44a 958 int cur_cp_pack; /* remain current cp pack */
aaec2b1d 959 spinlock_t cp_lock; /* for flag in ckpt */
39a53e0c 960 struct inode *meta_inode; /* cache meta blocks */
39936837 961 struct mutex cp_mutex; /* checkpoint procedure lock */
b873b798 962 struct rw_semaphore cp_rwsem; /* blocking FS operations */
b3582c68 963 struct rw_semaphore node_write; /* locking node writes */
59c9081b 964 struct rw_semaphore node_change; /* locking node change */
fb51b5ef 965 wait_queue_head_t cp_wait;
6beceb54
JK
966 unsigned long last_time[MAX_TIME]; /* to store time in jiffies */
967 long interval_time[MAX_TIME]; /* to store thresholds */
39a53e0c 968
67298804 969 struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
6451e041
JK
970
971 /* for orphan inode, use 0'th array */
0d47c1ad 972 unsigned int max_orphans; /* max orphan inodes */
39a53e0c 973
c227f912
CY
974 /* for inode management */
975 struct list_head inode_list[NR_INODE_TYPE]; /* dirty inode list */
976 spinlock_t inode_lock[NR_INODE_TYPE]; /* for dirty inode list lock */
39a53e0c 977
13054c54
CY
978 /* for extent tree cache */
979 struct radix_tree_root extent_tree_root;/* cache extent cache entries */
5e8256ac 980 struct mutex extent_tree_lock; /* locking extent radix tree */
13054c54
CY
981 struct list_head extent_list; /* lru list for shrinker */
982 spinlock_t extent_lock; /* locking extent lru list */
7441ccef 983 atomic_t total_ext_tree; /* extent tree count */
137d09f0 984 struct list_head zombie_list; /* extent zombie tree list */
74fd8d99 985 atomic_t total_zombie_tree; /* extent zombie tree count */
13054c54
CY
986 atomic_t total_ext_node; /* extent info count */
987
e1c42045 988 /* basic filesystem units */
39a53e0c
JK
989 unsigned int log_sectors_per_block; /* log2 sectors per block */
990 unsigned int log_blocksize; /* log2 block size */
991 unsigned int blocksize; /* block size */
992 unsigned int root_ino_num; /* root inode number*/
993 unsigned int node_ino_num; /* node inode number*/
994 unsigned int meta_ino_num; /* meta inode number*/
995 unsigned int log_blocks_per_seg; /* log2 blocks per segment */
996 unsigned int blocks_per_seg; /* blocks per segment */
997 unsigned int segs_per_sec; /* segments per section */
998 unsigned int secs_per_zone; /* sections per zone */
999 unsigned int total_sections; /* total section count */
1000 unsigned int total_node_count; /* total node block count */
1001 unsigned int total_valid_node_count; /* valid node block count */
e0afc4d6 1002 loff_t max_file_blocks; /* max block index of file */
39a53e0c 1003 int active_logs; /* # of active logs */
ab9fa662 1004 int dir_level; /* directory level */
39a53e0c
JK
1005
1006 block_t user_block_count; /* # of user blocks */
1007 block_t total_valid_block_count; /* # of valid blocks */
a66cdd98 1008 block_t discard_blks; /* discard command candidats */
39a53e0c 1009 block_t last_valid_block_count; /* for recovery */
daeb433e
CY
1010 block_t reserved_blocks; /* configurable reserved blocks */
1011
39a53e0c 1012 u32 s_next_generation; /* for NFS support */
523be8a6
JK
1013
1014 /* # of pages, see count_type */
35782b23 1015 atomic_t nr_pages[NR_COUNT_TYPE];
41382ec4
JK
1016 /* # of allocated blocks */
1017 struct percpu_counter alloc_valid_block_count;
39a53e0c 1018
687de7f1
JK
1019 /* writeback control */
1020 atomic_t wb_sync_req; /* count # of WB_SYNC threads */
1021
513c5f37
JK
1022 /* valid inode count */
1023 struct percpu_counter total_valid_inode_count;
1024
39a53e0c
JK
1025 struct f2fs_mount_info mount_opt; /* mount options */
1026
1027 /* for cleaning operations */
1028 struct mutex gc_mutex; /* mutex for GC */
1029 struct f2fs_gc_kthread *gc_thread; /* GC thread */
5ec4e49f 1030 unsigned int cur_victim_sec; /* current victim section num */
39a53e0c 1031
e93b9865
HP
1032 /* threshold for converting bg victims for fg */
1033 u64 fggc_threshold;
1034
b1c57c1c
JK
1035 /* maximum # of trials to find a victim segment for SSR and GC */
1036 unsigned int max_victim_search;
1037
39a53e0c
JK
1038 /*
1039 * for stat information.
1040 * one is for the LFS mode, and the other is for the SSR mode.
1041 */
35b09d82 1042#ifdef CONFIG_F2FS_STAT_FS
39a53e0c
JK
1043 struct f2fs_stat_info *stat_info; /* FS status information */
1044 unsigned int segment_count[2]; /* # of allocated segments */
1045 unsigned int block_count[2]; /* # of allocated blocks */
b9a2c252 1046 atomic_t inplace_count; /* # of inplace update */
5b7ee374
CY
1047 atomic64_t total_hit_ext; /* # of lookup extent cache */
1048 atomic64_t read_hit_rbtree; /* # of hit rbtree extent node */
1049 atomic64_t read_hit_largest; /* # of hit largest extent node */
1050 atomic64_t read_hit_cached; /* # of hit cached extent node */
d5e8f6c9 1051 atomic_t inline_xattr; /* # of inline_xattr inodes */
03e14d52
CY
1052 atomic_t inline_inode; /* # of inline_data inodes */
1053 atomic_t inline_dir; /* # of inline_dentry inodes */
26a28a0c 1054 atomic_t aw_cnt; /* # of atomic writes */
648d50ba 1055 atomic_t vw_cnt; /* # of volatile writes */
26a28a0c 1056 atomic_t max_aw_cnt; /* max # of atomic writes */
648d50ba 1057 atomic_t max_vw_cnt; /* max # of volatile writes */
39a53e0c 1058 int bg_gc; /* background gc calls */
33fbd510 1059 unsigned int ndirty_inode[NR_INODE_TYPE]; /* # of dirty inodes */
35b09d82 1060#endif
39a53e0c 1061 spinlock_t stat_lock; /* lock for stat operations */
b59d0bae
NJ
1062
1063 /* For sysfs suppport */
1064 struct kobject s_kobj;
1065 struct completion s_kobj_unregister;
2658e50d
JK
1066
1067 /* For shrinker support */
1068 struct list_head s_list;
3c62be17
JK
1069 int s_ndevs; /* number of devices */
1070 struct f2fs_dev_info *devs; /* for device list */
2658e50d
JK
1071 struct mutex umount_mutex;
1072 unsigned int shrinker_run_no;
8f1dbbbb
SL
1073
1074 /* For write statistics */
1075 u64 sectors_written_start;
1076 u64 kbytes_written;
43b6573b
KM
1077
1078 /* Reference to checksum algorithm driver via cryptoapi */
1079 struct crypto_shash *s_chksum_driver;
1ecc0c5c
CY
1080
1081 /* For fault injection */
1082#ifdef CONFIG_F2FS_FAULT_INJECTION
1083 struct f2fs_fault_info fault_info;
1084#endif
39a53e0c
JK
1085};
1086
1ecc0c5c 1087#ifdef CONFIG_F2FS_FAULT_INJECTION
55523519
CY
1088#define f2fs_show_injection_info(type) \
1089 printk("%sF2FS-fs : inject %s in %s of %pF\n", \
1090 KERN_INFO, fault_name[type], \
1091 __func__, __builtin_return_address(0))
1ecc0c5c
CY
1092static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
1093{
1094 struct f2fs_fault_info *ffi = &sbi->fault_info;
1095
1096 if (!ffi->inject_rate)
1097 return false;
1098
1099 if (!IS_FAULT_SET(ffi, type))
1100 return false;
1101
1102 atomic_inc(&ffi->inject_ops);
1103 if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
1104 atomic_set(&ffi->inject_ops, 0);
1ecc0c5c
CY
1105 return true;
1106 }
1107 return false;
1108}
1109#endif
1110
8f1dbbbb
SL
1111/* For write statistics. Suppose sector size is 512 bytes,
1112 * and the return value is in kbytes. s is of struct f2fs_sb_info.
1113 */
1114#define BD_PART_WRITTEN(s) \
68afcf2d
TK
1115(((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) - \
1116 (s)->sectors_written_start) >> 1)
8f1dbbbb 1117
6beceb54
JK
1118static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
1119{
1120 sbi->last_time[type] = jiffies;
1121}
1122
1123static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
1124{
1125 struct timespec ts = {sbi->interval_time[type], 0};
1126 unsigned long interval = timespec_to_jiffies(&ts);
1127
1128 return time_after(jiffies, sbi->last_time[type] + interval);
1129}
1130
d0239e1b
JK
1131static inline bool is_idle(struct f2fs_sb_info *sbi)
1132{
1133 struct block_device *bdev = sbi->sb->s_bdev;
1134 struct request_queue *q = bdev_get_queue(bdev);
1135 struct request_list *rl = &q->root_rl;
1136
1137 if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1138 return 0;
1139
1140 return f2fs_time_over(sbi, REQ_TIME);
1141}
1142
39a53e0c
JK
1143/*
1144 * Inline functions
1145 */
43b6573b
KM
1146static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1147 unsigned int length)
1148{
1149 SHASH_DESC_ON_STACK(shash, sbi->s_chksum_driver);
1150 u32 *ctx = (u32 *)shash_desc_ctx(shash);
d41519a6 1151 u32 retval;
43b6573b
KM
1152 int err;
1153
1154 shash->tfm = sbi->s_chksum_driver;
1155 shash->flags = 0;
1156 *ctx = F2FS_SUPER_MAGIC;
1157
1158 err = crypto_shash_update(shash, address, length);
1159 BUG_ON(err);
1160
d41519a6
DM
1161 retval = *ctx;
1162 barrier_data(ctx);
1163 return retval;
43b6573b
KM
1164}
1165
1166static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1167 void *buf, size_t buf_size)
1168{
1169 return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1170}
1171
39a53e0c
JK
1172static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
1173{
1174 return container_of(inode, struct f2fs_inode_info, vfs_inode);
1175}
1176
1177static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
1178{
1179 return sb->s_fs_info;
1180}
1181
4081363f
JK
1182static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
1183{
1184 return F2FS_SB(inode->i_sb);
1185}
1186
1187static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
1188{
1189 return F2FS_I_SB(mapping->host);
1190}
1191
1192static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
1193{
1194 return F2FS_M_SB(page->mapping);
1195}
1196
39a53e0c
JK
1197static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
1198{
1199 return (struct f2fs_super_block *)(sbi->raw_super);
1200}
1201
1202static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
1203{
1204 return (struct f2fs_checkpoint *)(sbi->ckpt);
1205}
1206
45590710
GZ
1207static inline struct f2fs_node *F2FS_NODE(struct page *page)
1208{
1209 return (struct f2fs_node *)page_address(page);
1210}
1211
58bfaf44
JK
1212static inline struct f2fs_inode *F2FS_INODE(struct page *page)
1213{
1214 return &((struct f2fs_node *)page_address(page))->i;
1215}
1216
39a53e0c
JK
1217static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
1218{
1219 return (struct f2fs_nm_info *)(sbi->nm_info);
1220}
1221
1222static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
1223{
1224 return (struct f2fs_sm_info *)(sbi->sm_info);
1225}
1226
1227static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
1228{
1229 return (struct sit_info *)(SM_I(sbi)->sit_info);
1230}
1231
1232static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
1233{
1234 return (struct free_segmap_info *)(SM_I(sbi)->free_info);
1235}
1236
1237static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
1238{
1239 return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
1240}
1241
9df27d98
GZ
1242static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
1243{
1244 return sbi->meta_inode->i_mapping;
1245}
1246
4ef51a8f
JK
1247static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
1248{
1249 return sbi->node_inode->i_mapping;
1250}
1251
caf0047e
CY
1252static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
1253{
fadb2fb8 1254 return test_bit(type, &sbi->s_flag);
caf0047e
CY
1255}
1256
1257static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
39a53e0c 1258{
fadb2fb8 1259 set_bit(type, &sbi->s_flag);
39a53e0c
JK
1260}
1261
caf0047e 1262static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
39a53e0c 1263{
fadb2fb8 1264 clear_bit(type, &sbi->s_flag);
39a53e0c
JK
1265}
1266
d71b5564
JK
1267static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1268{
1269 return le64_to_cpu(cp->checkpoint_ver);
1270}
1271
ced2c7ea
KM
1272static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1273{
1274 size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1275 return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1276}
1277
aaec2b1d 1278static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
25ca923b
JK
1279{
1280 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
aaec2b1d 1281
25ca923b
JK
1282 return ckpt_flags & f;
1283}
1284
aaec2b1d 1285static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
25ca923b 1286{
aaec2b1d
CY
1287 return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1288}
1289
1290static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1291{
1292 unsigned int ckpt_flags;
1293
1294 ckpt_flags = le32_to_cpu(cp->ckpt_flags);
25ca923b
JK
1295 ckpt_flags |= f;
1296 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1297}
1298
aaec2b1d 1299static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
25ca923b 1300{
d1aa2453
CY
1301 unsigned long flags;
1302
1303 spin_lock_irqsave(&sbi->cp_lock, flags);
aaec2b1d 1304 __set_ckpt_flags(F2FS_CKPT(sbi), f);
d1aa2453 1305 spin_unlock_irqrestore(&sbi->cp_lock, flags);
aaec2b1d
CY
1306}
1307
1308static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1309{
1310 unsigned int ckpt_flags;
1311
1312 ckpt_flags = le32_to_cpu(cp->ckpt_flags);
25ca923b
JK
1313 ckpt_flags &= (~f);
1314 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1315}
1316
aaec2b1d
CY
1317static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1318{
d1aa2453
CY
1319 unsigned long flags;
1320
1321 spin_lock_irqsave(&sbi->cp_lock, flags);
aaec2b1d 1322 __clear_ckpt_flags(F2FS_CKPT(sbi), f);
d1aa2453 1323 spin_unlock_irqrestore(&sbi->cp_lock, flags);
aaec2b1d
CY
1324}
1325
22ad0b6a
JK
1326static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
1327{
d1aa2453
CY
1328 unsigned long flags;
1329
22ad0b6a
JK
1330 set_sbi_flag(sbi, SBI_NEED_FSCK);
1331
1332 if (lock)
d1aa2453 1333 spin_lock_irqsave(&sbi->cp_lock, flags);
22ad0b6a
JK
1334 __clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
1335 kfree(NM_I(sbi)->nat_bits);
1336 NM_I(sbi)->nat_bits = NULL;
1337 if (lock)
d1aa2453 1338 spin_unlock_irqrestore(&sbi->cp_lock, flags);
22ad0b6a
JK
1339}
1340
1341static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
1342 struct cp_control *cpc)
1343{
1344 bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
1345
c473f1a9 1346 return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
22ad0b6a
JK
1347}
1348
e479556b 1349static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
39936837 1350{
b873b798 1351 down_read(&sbi->cp_rwsem);
39936837
JK
1352}
1353
cc15620b
JK
1354static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1355{
1356 return down_read_trylock(&sbi->cp_rwsem);
1357}
1358
e479556b 1359static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
39a53e0c 1360{
b873b798 1361 up_read(&sbi->cp_rwsem);
39a53e0c
JK
1362}
1363
e479556b 1364static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
39a53e0c 1365{
b873b798 1366 down_write(&sbi->cp_rwsem);
39936837
JK
1367}
1368
e479556b 1369static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
39936837 1370{
b873b798 1371 up_write(&sbi->cp_rwsem);
39a53e0c
JK
1372}
1373
119ee914
JK
1374static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1375{
1376 int reason = CP_SYNC;
1377
1378 if (test_opt(sbi, FASTBOOT))
1379 reason = CP_FASTBOOT;
1380 if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1381 reason = CP_UMOUNT;
1382 return reason;
1383}
1384
1385static inline bool __remain_node_summaries(int reason)
1386{
c473f1a9 1387 return (reason & (CP_UMOUNT | CP_FASTBOOT));
119ee914
JK
1388}
1389
1390static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1391{
aaec2b1d
CY
1392 return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1393 is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
119ee914
JK
1394}
1395
39a53e0c
JK
1396/*
1397 * Check whether the given nid is within node id range.
1398 */
064e0823 1399static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
39a53e0c 1400{
d6b7d4b3
CY
1401 if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1402 return -EINVAL;
cfb271d4 1403 if (unlikely(nid >= NM_I(sbi)->max_nid))
064e0823
NJ
1404 return -EINVAL;
1405 return 0;
39a53e0c
JK
1406}
1407
39a53e0c
JK
1408/*
1409 * Check whether the inode has blocks or not
1410 */
1411static inline int F2FS_HAS_BLOCKS(struct inode *inode)
1412{
0eb0adad
CY
1413 block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
1414
000519f2 1415 return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
39a53e0c
JK
1416}
1417
4bc8e9bc
CY
1418static inline bool f2fs_has_xattr_block(unsigned int ofs)
1419{
1420 return ofs == XATTR_NODE_OFFSET;
1421}
1422
0abd675e
CY
1423static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
1424static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
46008c6d 1425 struct inode *inode, blkcnt_t *count)
39a53e0c 1426{
0abd675e 1427 blkcnt_t diff = 0, release = 0;
daeb433e 1428 block_t avail_user_block_count;
0abd675e
CY
1429 int ret;
1430
1431 ret = dquot_reserve_block(inode, *count);
1432 if (ret)
1433 return ret;
39a53e0c 1434
cb78942b 1435#ifdef CONFIG_F2FS_FAULT_INJECTION
55523519
CY
1436 if (time_to_inject(sbi, FAULT_BLOCK)) {
1437 f2fs_show_injection_info(FAULT_BLOCK);
0abd675e
CY
1438 release = *count;
1439 goto enospc;
55523519 1440 }
cb78942b 1441#endif
dd11a5df
JK
1442 /*
1443 * let's increase this in prior to actual block count change in order
1444 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1445 */
1446 percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1447
2555a2d5
JK
1448 spin_lock(&sbi->stat_lock);
1449 sbi->total_valid_block_count += (block_t)(*count);
daeb433e
CY
1450 avail_user_block_count = sbi->user_block_count - sbi->reserved_blocks;
1451 if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1452 diff = sbi->total_valid_block_count - avail_user_block_count;
dd11a5df 1453 *count -= diff;
0abd675e 1454 release = diff;
daeb433e 1455 sbi->total_valid_block_count = avail_user_block_count;
46008c6d
CY
1456 if (!*count) {
1457 spin_unlock(&sbi->stat_lock);
dd11a5df 1458 percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
0abd675e 1459 goto enospc;
46008c6d 1460 }
39a53e0c 1461 }
39a53e0c 1462 spin_unlock(&sbi->stat_lock);
41382ec4 1463
0abd675e
CY
1464 if (release)
1465 dquot_release_reservation_block(inode, release);
1466 f2fs_i_blocks_write(inode, *count, true, true);
1467 return 0;
1468
1469enospc:
1470 dquot_release_reservation_block(inode, release);
1471 return -ENOSPC;
39a53e0c
JK
1472}
1473
da19b0dc 1474static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
39a53e0c 1475 struct inode *inode,
0eb0adad 1476 block_t count)
39a53e0c 1477{
0eb0adad
CY
1478 blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
1479
39a53e0c 1480 spin_lock(&sbi->stat_lock);
9850cf4a 1481 f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
0eb0adad 1482 f2fs_bug_on(sbi, inode->i_blocks < sectors);
39a53e0c
JK
1483 sbi->total_valid_block_count -= (block_t)count;
1484 spin_unlock(&sbi->stat_lock);
0abd675e 1485 f2fs_i_blocks_write(inode, count, false, true);
39a53e0c
JK
1486}
1487
1488static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
1489{
35782b23 1490 atomic_inc(&sbi->nr_pages[count_type]);
7c4abcbe 1491
36951b38
CY
1492 if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
1493 count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
7c4abcbe
CY
1494 return;
1495
caf0047e 1496 set_sbi_flag(sbi, SBI_IS_DIRTY);
39a53e0c
JK
1497}
1498
a7ffdbe2 1499static inline void inode_inc_dirty_pages(struct inode *inode)
39a53e0c 1500{
204706c7 1501 atomic_inc(&F2FS_I(inode)->dirty_pages);
c227f912
CY
1502 inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1503 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
39a53e0c
JK
1504}
1505
1506static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
1507{
35782b23 1508 atomic_dec(&sbi->nr_pages[count_type]);
39a53e0c
JK
1509}
1510
a7ffdbe2 1511static inline void inode_dec_dirty_pages(struct inode *inode)
39a53e0c 1512{
5ac9f36f
CY
1513 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1514 !S_ISLNK(inode->i_mode))
1fe54f9d
JK
1515 return;
1516
204706c7 1517 atomic_dec(&F2FS_I(inode)->dirty_pages);
c227f912
CY
1518 dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1519 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
39a53e0c
JK
1520}
1521
523be8a6 1522static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
39a53e0c 1523{
35782b23 1524 return atomic_read(&sbi->nr_pages[count_type]);
39a53e0c
JK
1525}
1526
204706c7 1527static inline int get_dirty_pages(struct inode *inode)
f8b2c1f9 1528{
204706c7 1529 return atomic_read(&F2FS_I(inode)->dirty_pages);
f8b2c1f9
JK
1530}
1531
5ac206cf
NJ
1532static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
1533{
3519e3f9 1534 unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
523be8a6
JK
1535 unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1536 sbi->log_blocks_per_seg;
1537
1538 return segs / sbi->segs_per_sec;
5ac206cf
NJ
1539}
1540
39a53e0c
JK
1541static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
1542{
8b8343fa 1543 return sbi->total_valid_block_count;
39a53e0c
JK
1544}
1545
f83a2584
YH
1546static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1547{
1548 return sbi->discard_blks;
1549}
1550
39a53e0c
JK
1551static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
1552{
1553 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1554
1555 /* return NAT or SIT bitmap */
1556 if (flag == NAT_BITMAP)
1557 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
1558 else if (flag == SIT_BITMAP)
1559 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
1560
1561 return 0;
1562}
1563
55141486
WL
1564static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
1565{
1566 return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
1567}
1568
39a53e0c
JK
1569static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
1570{
1571 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1dbe4152
CL
1572 int offset;
1573
55141486 1574 if (__cp_payload(sbi) > 0) {
1dbe4152
CL
1575 if (flag == NAT_BITMAP)
1576 return &ckpt->sit_nat_version_bitmap;
1577 else
65b85ccc 1578 return (unsigned char *)ckpt + F2FS_BLKSIZE;
1dbe4152
CL
1579 } else {
1580 offset = (flag == NAT_BITMAP) ?
25ca923b 1581 le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
1dbe4152
CL
1582 return &ckpt->sit_nat_version_bitmap + offset;
1583 }
39a53e0c
JK
1584}
1585
1586static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
1587{
8508e44a 1588 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
39a53e0c 1589
8508e44a 1590 if (sbi->cur_cp_pack == 2)
39a53e0c 1591 start_addr += sbi->blocks_per_seg;
8508e44a
JK
1592 return start_addr;
1593}
39a53e0c 1594
8508e44a
JK
1595static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
1596{
1597 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
39a53e0c 1598
8508e44a
JK
1599 if (sbi->cur_cp_pack == 1)
1600 start_addr += sbi->blocks_per_seg;
39a53e0c
JK
1601 return start_addr;
1602}
1603
8508e44a
JK
1604static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
1605{
1606 sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
1607}
1608
39a53e0c
JK
1609static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
1610{
1611 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
1612}
1613
0abd675e 1614static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
000519f2 1615 struct inode *inode, bool is_inode)
39a53e0c
JK
1616{
1617 block_t valid_block_count;
1618 unsigned int valid_node_count;
0abd675e
CY
1619 bool quota = inode && !is_inode;
1620
1621 if (quota) {
1622 int ret = dquot_reserve_block(inode, 1);
1623 if (ret)
1624 return ret;
1625 }
39a53e0c
JK
1626
1627 spin_lock(&sbi->stat_lock);
1628
ef86d709 1629 valid_block_count = sbi->total_valid_block_count + 1;
daeb433e
CY
1630 if (unlikely(valid_block_count + sbi->reserved_blocks >
1631 sbi->user_block_count)) {
39a53e0c 1632 spin_unlock(&sbi->stat_lock);
0abd675e 1633 goto enospc;
39a53e0c
JK
1634 }
1635
ef86d709 1636 valid_node_count = sbi->total_valid_node_count + 1;
cfb271d4 1637 if (unlikely(valid_node_count > sbi->total_node_count)) {
39a53e0c 1638 spin_unlock(&sbi->stat_lock);
0abd675e 1639 goto enospc;
39a53e0c
JK
1640 }
1641
ef86d709
GZ
1642 sbi->total_valid_node_count++;
1643 sbi->total_valid_block_count++;
39a53e0c
JK
1644 spin_unlock(&sbi->stat_lock);
1645
000519f2
CY
1646 if (inode) {
1647 if (is_inode)
1648 f2fs_mark_inode_dirty_sync(inode, true);
1649 else
0abd675e 1650 f2fs_i_blocks_write(inode, 1, true, true);
000519f2 1651 }
ef86d709 1652
41382ec4 1653 percpu_counter_inc(&sbi->alloc_valid_block_count);
0abd675e
CY
1654 return 0;
1655
1656enospc:
1657 if (quota)
1658 dquot_release_reservation_block(inode, 1);
1659 return -ENOSPC;
39a53e0c
JK
1660}
1661
1662static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
000519f2 1663 struct inode *inode, bool is_inode)
39a53e0c
JK
1664{
1665 spin_lock(&sbi->stat_lock);
1666
9850cf4a
JK
1667 f2fs_bug_on(sbi, !sbi->total_valid_block_count);
1668 f2fs_bug_on(sbi, !sbi->total_valid_node_count);
000519f2 1669 f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
39a53e0c 1670
ef86d709
GZ
1671 sbi->total_valid_node_count--;
1672 sbi->total_valid_block_count--;
39a53e0c
JK
1673
1674 spin_unlock(&sbi->stat_lock);
0abd675e
CY
1675
1676 if (!is_inode)
1677 f2fs_i_blocks_write(inode, 1, false, true);
39a53e0c
JK
1678}
1679
1680static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
1681{
8b8343fa 1682 return sbi->total_valid_node_count;
39a53e0c
JK
1683}
1684
1685static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
1686{
513c5f37 1687 percpu_counter_inc(&sbi->total_valid_inode_count);
39a53e0c
JK
1688}
1689
0e80220a 1690static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
39a53e0c 1691{
513c5f37 1692 percpu_counter_dec(&sbi->total_valid_inode_count);
39a53e0c
JK
1693}
1694
513c5f37 1695static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
39a53e0c 1696{
513c5f37 1697 return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
39a53e0c
JK
1698}
1699
a56c7c6f
JK
1700static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1701 pgoff_t index, bool for_write)
1702{
c41f3cc3
JK
1703#ifdef CONFIG_F2FS_FAULT_INJECTION
1704 struct page *page = find_lock_page(mapping, index);
cac5a3d8 1705
c41f3cc3
JK
1706 if (page)
1707 return page;
1708
55523519
CY
1709 if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
1710 f2fs_show_injection_info(FAULT_PAGE_ALLOC);
c41f3cc3 1711 return NULL;
55523519 1712 }
c41f3cc3 1713#endif
a56c7c6f
JK
1714 if (!for_write)
1715 return grab_cache_page(mapping, index);
1716 return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1717}
1718
6e2c64ad
JK
1719static inline void f2fs_copy_page(struct page *src, struct page *dst)
1720{
1721 char *src_kaddr = kmap(src);
1722 char *dst_kaddr = kmap(dst);
1723
1724 memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
1725 kunmap(dst);
1726 kunmap(src);
1727}
1728
39a53e0c
JK
1729static inline void f2fs_put_page(struct page *page, int unlock)
1730{
031fa8cc 1731 if (!page)
39a53e0c
JK
1732 return;
1733
1734 if (unlock) {
9850cf4a 1735 f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
39a53e0c
JK
1736 unlock_page(page);
1737 }
09cbfeaf 1738 put_page(page);
39a53e0c
JK
1739}
1740
1741static inline void f2fs_put_dnode(struct dnode_of_data *dn)
1742{
1743 if (dn->node_page)
1744 f2fs_put_page(dn->node_page, 1);
1745 if (dn->inode_page && dn->node_page != dn->inode_page)
1746 f2fs_put_page(dn->inode_page, 0);
1747 dn->node_page = NULL;
1748 dn->inode_page = NULL;
1749}
1750
1751static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
e8512d2e 1752 size_t size)
39a53e0c 1753{
e8512d2e 1754 return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
39a53e0c
JK
1755}
1756
7bd59381
GZ
1757static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
1758 gfp_t flags)
1759{
1760 void *entry;
7bd59381 1761
80c54505
JK
1762 entry = kmem_cache_alloc(cachep, flags);
1763 if (!entry)
1764 entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
7bd59381
GZ
1765 return entry;
1766}
1767
740432f8
JK
1768static inline struct bio *f2fs_bio_alloc(int npages)
1769{
1770 struct bio *bio;
1771
1772 /* No failure on bio allocation */
740432f8 1773 bio = bio_alloc(GFP_NOIO, npages);
80c54505
JK
1774 if (!bio)
1775 bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
740432f8
JK
1776 return bio;
1777}
1778
9be32d72
JK
1779static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
1780 unsigned long index, void *item)
1781{
1782 while (radix_tree_insert(root, index, item))
1783 cond_resched();
1784}
1785
39a53e0c
JK
1786#define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
1787
1788static inline bool IS_INODE(struct page *page)
1789{
45590710 1790 struct f2fs_node *p = F2FS_NODE(page);
cac5a3d8 1791
39a53e0c
JK
1792 return RAW_IS_INODE(p);
1793}
1794
1795static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
1796{
1797 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
1798}
1799
1800static inline block_t datablock_addr(struct page *node_page,
1801 unsigned int offset)
1802{
1803 struct f2fs_node *raw_node;
1804 __le32 *addr_array;
cac5a3d8 1805
45590710 1806 raw_node = F2FS_NODE(node_page);
39a53e0c
JK
1807 addr_array = blkaddr_in_node(raw_node);
1808 return le32_to_cpu(addr_array[offset]);
1809}
1810
1811static inline int f2fs_test_bit(unsigned int nr, char *addr)
1812{
1813 int mask;
1814
1815 addr += (nr >> 3);
1816 mask = 1 << (7 - (nr & 0x07));
1817 return mask & *addr;
1818}
1819
a66cdd98
JK
1820static inline void f2fs_set_bit(unsigned int nr, char *addr)
1821{
1822 int mask;
1823
1824 addr += (nr >> 3);
1825 mask = 1 << (7 - (nr & 0x07));
1826 *addr |= mask;
1827}
1828
1829static inline void f2fs_clear_bit(unsigned int nr, char *addr)
1830{
1831 int mask;
1832
1833 addr += (nr >> 3);
1834 mask = 1 << (7 - (nr & 0x07));
1835 *addr &= ~mask;
1836}
1837
52aca074 1838static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
39a53e0c
JK
1839{
1840 int mask;
1841 int ret;
1842
1843 addr += (nr >> 3);
1844 mask = 1 << (7 - (nr & 0x07));
1845 ret = mask & *addr;
1846 *addr |= mask;
1847 return ret;
1848}
1849
52aca074 1850static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
39a53e0c
JK
1851{
1852 int mask;
1853 int ret;
1854
1855 addr += (nr >> 3);
1856 mask = 1 << (7 - (nr & 0x07));
1857 ret = mask & *addr;
1858 *addr &= ~mask;
1859 return ret;
1860}
1861
c6ac4c0e
GZ
1862static inline void f2fs_change_bit(unsigned int nr, char *addr)
1863{
1864 int mask;
1865
1866 addr += (nr >> 3);
1867 mask = 1 << (7 - (nr & 0x07));
1868 *addr ^= mask;
1869}
1870
39a53e0c
JK
1871/* used for f2fs_inode_info->flags */
1872enum {
1873 FI_NEW_INODE, /* indicate newly allocated inode */
b3783873 1874 FI_DIRTY_INODE, /* indicate inode is dirty or not */
26de9b11 1875 FI_AUTO_RECOVER, /* indicate inode is recoverable */
ed57c27f 1876 FI_DIRTY_DIR, /* indicate directory has dirty pages */
39a53e0c
JK
1877 FI_INC_LINK, /* need to increment i_nlink */
1878 FI_ACL_MODE, /* indicate acl mode */
1879 FI_NO_ALLOC, /* should not allocate any blocks */
c9b63bd0 1880 FI_FREE_NID, /* free allocated nide */
c11abd1a 1881 FI_NO_EXTENT, /* not to use the extent cache */
444c580f 1882 FI_INLINE_XATTR, /* used for inline xattr */
1001b347 1883 FI_INLINE_DATA, /* used for inline data*/
34d67deb 1884 FI_INLINE_DENTRY, /* used for inline dentry */
fff04f90
JK
1885 FI_APPEND_WRITE, /* inode has appended data */
1886 FI_UPDATE_WRITE, /* inode has in-place-update data */
88b88a66
JK
1887 FI_NEED_IPU, /* used for ipu per file */
1888 FI_ATOMIC_FILE, /* indicate atomic file */
5fe45743 1889 FI_ATOMIC_COMMIT, /* indicate the state of atomical committing */
02a1335f 1890 FI_VOLATILE_FILE, /* indicate volatile file */
3c6c2beb 1891 FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */
1e84371f 1892 FI_DROP_CACHE, /* drop dirty page cache */
b3d208f9 1893 FI_DATA_EXIST, /* indicate data exists */
510022a8 1894 FI_INLINE_DOTS, /* indicate inline dot dentries */
d323d005 1895 FI_DO_DEFRAG, /* indicate defragment is running */
c227f912 1896 FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */
dc91de78 1897 FI_NO_PREALLOC, /* indicate skipped preallocated blocks */
ef095d19 1898 FI_HOT_DATA, /* indicate file is hot */
39a53e0c
JK
1899};
1900
205b9822
JK
1901static inline void __mark_inode_dirty_flag(struct inode *inode,
1902 int flag, bool set)
1903{
1904 switch (flag) {
1905 case FI_INLINE_XATTR:
1906 case FI_INLINE_DATA:
1907 case FI_INLINE_DENTRY:
1908 if (set)
1909 return;
1910 case FI_DATA_EXIST:
1911 case FI_INLINE_DOTS:
7c45729a 1912 f2fs_mark_inode_dirty_sync(inode, true);
205b9822
JK
1913 }
1914}
1915
91942321 1916static inline void set_inode_flag(struct inode *inode, int flag)
39a53e0c 1917{
91942321
JK
1918 if (!test_bit(flag, &F2FS_I(inode)->flags))
1919 set_bit(flag, &F2FS_I(inode)->flags);
205b9822 1920 __mark_inode_dirty_flag(inode, flag, true);
39a53e0c
JK
1921}
1922
91942321 1923static inline int is_inode_flag_set(struct inode *inode, int flag)
39a53e0c 1924{
91942321 1925 return test_bit(flag, &F2FS_I(inode)->flags);
39a53e0c
JK
1926}
1927
91942321 1928static inline void clear_inode_flag(struct inode *inode, int flag)
39a53e0c 1929{
91942321
JK
1930 if (test_bit(flag, &F2FS_I(inode)->flags))
1931 clear_bit(flag, &F2FS_I(inode)->flags);
205b9822 1932 __mark_inode_dirty_flag(inode, flag, false);
39a53e0c
JK
1933}
1934
91942321 1935static inline void set_acl_inode(struct inode *inode, umode_t mode)
39a53e0c 1936{
91942321
JK
1937 F2FS_I(inode)->i_acl_mode = mode;
1938 set_inode_flag(inode, FI_ACL_MODE);
7c45729a 1939 f2fs_mark_inode_dirty_sync(inode, false);
39a53e0c
JK
1940}
1941
a1961246 1942static inline void f2fs_i_links_write(struct inode *inode, bool inc)
39a53e0c 1943{
a1961246
JK
1944 if (inc)
1945 inc_nlink(inode);
1946 else
1947 drop_nlink(inode);
7c45729a 1948 f2fs_mark_inode_dirty_sync(inode, true);
a1961246
JK
1949}
1950
8edd03c8 1951static inline void f2fs_i_blocks_write(struct inode *inode,
0abd675e 1952 block_t diff, bool add, bool claim)
8edd03c8 1953{
26de9b11
JK
1954 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
1955 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
1956
0abd675e
CY
1957 /* add = 1, claim = 1 should be dquot_reserve_block in pair */
1958 if (add) {
1959 if (claim)
1960 dquot_claim_block(inode, diff);
1961 else
1962 dquot_alloc_block_nofail(inode, diff);
1963 } else {
1964 dquot_free_block(inode, diff);
1965 }
1966
7c45729a 1967 f2fs_mark_inode_dirty_sync(inode, true);
26de9b11
JK
1968 if (clean || recover)
1969 set_inode_flag(inode, FI_AUTO_RECOVER);
8edd03c8
JK
1970}
1971
fc9581c8
JK
1972static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
1973{
26de9b11
JK
1974 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
1975 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
1976
fc9581c8
JK
1977 if (i_size_read(inode) == i_size)
1978 return;
1979
1980 i_size_write(inode, i_size);
7c45729a 1981 f2fs_mark_inode_dirty_sync(inode, true);
26de9b11
JK
1982 if (clean || recover)
1983 set_inode_flag(inode, FI_AUTO_RECOVER);
39a53e0c
JK
1984}
1985
205b9822 1986static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
39a53e0c 1987{
205b9822 1988 F2FS_I(inode)->i_current_depth = depth;
7c45729a 1989 f2fs_mark_inode_dirty_sync(inode, true);
39a53e0c
JK
1990}
1991
205b9822 1992static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
444c580f 1993{
205b9822 1994 F2FS_I(inode)->i_xattr_nid = xnid;
7c45729a 1995 f2fs_mark_inode_dirty_sync(inode, true);
205b9822
JK
1996}
1997
1998static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
1999{
2000 F2FS_I(inode)->i_pino = pino;
7c45729a 2001 f2fs_mark_inode_dirty_sync(inode, true);
205b9822
JK
2002}
2003
91942321 2004static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
444c580f 2005{
205b9822
JK
2006 struct f2fs_inode_info *fi = F2FS_I(inode);
2007
444c580f 2008 if (ri->i_inline & F2FS_INLINE_XATTR)
205b9822 2009 set_bit(FI_INLINE_XATTR, &fi->flags);
1001b347 2010 if (ri->i_inline & F2FS_INLINE_DATA)
205b9822 2011 set_bit(FI_INLINE_DATA, &fi->flags);
34d67deb 2012 if (ri->i_inline & F2FS_INLINE_DENTRY)
205b9822 2013 set_bit(FI_INLINE_DENTRY, &fi->flags);
b3d208f9 2014 if (ri->i_inline & F2FS_DATA_EXIST)
205b9822 2015 set_bit(FI_DATA_EXIST, &fi->flags);
510022a8 2016 if (ri->i_inline & F2FS_INLINE_DOTS)
205b9822 2017 set_bit(FI_INLINE_DOTS, &fi->flags);
444c580f
JK
2018}
2019
91942321 2020static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
444c580f
JK
2021{
2022 ri->i_inline = 0;
2023
91942321 2024 if (is_inode_flag_set(inode, FI_INLINE_XATTR))
444c580f 2025 ri->i_inline |= F2FS_INLINE_XATTR;
91942321 2026 if (is_inode_flag_set(inode, FI_INLINE_DATA))
1001b347 2027 ri->i_inline |= F2FS_INLINE_DATA;
91942321 2028 if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
34d67deb 2029 ri->i_inline |= F2FS_INLINE_DENTRY;
91942321 2030 if (is_inode_flag_set(inode, FI_DATA_EXIST))
b3d208f9 2031 ri->i_inline |= F2FS_DATA_EXIST;
91942321 2032 if (is_inode_flag_set(inode, FI_INLINE_DOTS))
510022a8 2033 ri->i_inline |= F2FS_INLINE_DOTS;
444c580f
JK
2034}
2035
987c7c31
CY
2036static inline int f2fs_has_inline_xattr(struct inode *inode)
2037{
91942321 2038 return is_inode_flag_set(inode, FI_INLINE_XATTR);
987c7c31
CY
2039}
2040
81ca7350 2041static inline unsigned int addrs_per_inode(struct inode *inode)
de93653f 2042{
81ca7350 2043 if (f2fs_has_inline_xattr(inode))
de93653f
JK
2044 return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
2045 return DEF_ADDRS_PER_INODE;
2046}
2047
65985d93
JK
2048static inline void *inline_xattr_addr(struct page *page)
2049{
695fd1ed 2050 struct f2fs_inode *ri = F2FS_INODE(page);
cac5a3d8 2051
65985d93
JK
2052 return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2053 F2FS_INLINE_XATTR_ADDRS]);
2054}
2055
2056static inline int inline_xattr_size(struct inode *inode)
2057{
987c7c31 2058 if (f2fs_has_inline_xattr(inode))
65985d93
JK
2059 return F2FS_INLINE_XATTR_ADDRS << 2;
2060 else
2061 return 0;
2062}
2063
0dbdc2ae
JK
2064static inline int f2fs_has_inline_data(struct inode *inode)
2065{
91942321 2066 return is_inode_flag_set(inode, FI_INLINE_DATA);
0dbdc2ae
JK
2067}
2068
b3d208f9
JK
2069static inline int f2fs_exist_data(struct inode *inode)
2070{
91942321 2071 return is_inode_flag_set(inode, FI_DATA_EXIST);
b3d208f9
JK
2072}
2073
510022a8
JK
2074static inline int f2fs_has_inline_dots(struct inode *inode)
2075{
91942321 2076 return is_inode_flag_set(inode, FI_INLINE_DOTS);
510022a8
JK
2077}
2078
88b88a66
JK
2079static inline bool f2fs_is_atomic_file(struct inode *inode)
2080{
91942321 2081 return is_inode_flag_set(inode, FI_ATOMIC_FILE);
88b88a66
JK
2082}
2083
5fe45743
CY
2084static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
2085{
2086 return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
2087}
2088
02a1335f
JK
2089static inline bool f2fs_is_volatile_file(struct inode *inode)
2090{
91942321 2091 return is_inode_flag_set(inode, FI_VOLATILE_FILE);
02a1335f
JK
2092}
2093
3c6c2beb
JK
2094static inline bool f2fs_is_first_block_written(struct inode *inode)
2095{
91942321 2096 return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
3c6c2beb
JK
2097}
2098
1e84371f
JK
2099static inline bool f2fs_is_drop_cache(struct inode *inode)
2100{
91942321 2101 return is_inode_flag_set(inode, FI_DROP_CACHE);
1e84371f
JK
2102}
2103
f2470371 2104static inline void *inline_data_addr(struct inode *inode, struct page *page)
1001b347 2105{
695fd1ed 2106 struct f2fs_inode *ri = F2FS_INODE(page);
f2470371 2107 int reserved_size = get_inline_reserved_size(inode);
cac5a3d8 2108
f2470371 2109 return (void *)&(ri->i_addr[reserved_size]);
1001b347
HL
2110}
2111
34d67deb
CY
2112static inline int f2fs_has_inline_dentry(struct inode *inode)
2113{
91942321 2114 return is_inode_flag_set(inode, FI_INLINE_DENTRY);
34d67deb
CY
2115}
2116
9486ba44
JK
2117static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
2118{
2119 if (!f2fs_has_inline_dentry(dir))
2120 kunmap(page);
2121}
2122
b5492af7
JK
2123static inline int is_file(struct inode *inode, int type)
2124{
2125 return F2FS_I(inode)->i_advise & type;
2126}
2127
2128static inline void set_file(struct inode *inode, int type)
2129{
2130 F2FS_I(inode)->i_advise |= type;
7c45729a 2131 f2fs_mark_inode_dirty_sync(inode, true);
b5492af7
JK
2132}
2133
2134static inline void clear_file(struct inode *inode, int type)
2135{
2136 F2FS_I(inode)->i_advise &= ~type;
7c45729a 2137 f2fs_mark_inode_dirty_sync(inode, true);
b5492af7
JK
2138}
2139
26787236
JK
2140static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
2141{
2142 if (dsync) {
2143 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2144 bool ret;
2145
2146 spin_lock(&sbi->inode_lock[DIRTY_META]);
2147 ret = list_empty(&F2FS_I(inode)->gdirty_list);
2148 spin_unlock(&sbi->inode_lock[DIRTY_META]);
2149 return ret;
2150 }
2151 if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
2152 file_keep_isize(inode) ||
2153 i_size_read(inode) & PAGE_MASK)
2154 return false;
2155 return F2FS_I(inode)->last_disk_size == i_size_read(inode);
b5492af7
JK
2156}
2157
77888c1e
JK
2158static inline int f2fs_readonly(struct super_block *sb)
2159{
2160 return sb->s_flags & MS_RDONLY;
2161}
2162
1e968fdf
JK
2163static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2164{
aaec2b1d 2165 return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
1e968fdf
JK
2166}
2167
eaa693f4
JK
2168static inline bool is_dot_dotdot(const struct qstr *str)
2169{
2170 if (str->len == 1 && str->name[0] == '.')
2171 return true;
2172
2173 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2174 return true;
2175
2176 return false;
2177}
2178
3e72f721
JK
2179static inline bool f2fs_may_extent_tree(struct inode *inode)
2180{
3e72f721 2181 if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
91942321 2182 is_inode_flag_set(inode, FI_NO_EXTENT))
3e72f721
JK
2183 return false;
2184
886f56f9 2185 return S_ISREG(inode->i_mode);
3e72f721
JK
2186}
2187
1ecc0c5c
CY
2188static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
2189 size_t size, gfp_t flags)
0414b004 2190{
2c63fead 2191#ifdef CONFIG_F2FS_FAULT_INJECTION
55523519
CY
2192 if (time_to_inject(sbi, FAULT_KMALLOC)) {
2193 f2fs_show_injection_info(FAULT_KMALLOC);
2c63fead 2194 return NULL;
55523519 2195 }
2c63fead 2196#endif
0414b004
JK
2197 return kmalloc(size, flags);
2198}
2199
f2470371
CY
2200static inline int get_inline_reserved_size(struct inode *inode)
2201{
2202 return F2FS_I(inode)->i_inline_reserved;
2203}
2204
a6dda0e6 2205#define get_inode_mode(i) \
91942321 2206 ((is_inode_flag_set(i, FI_ACL_MODE)) ? \
a6dda0e6
CH
2207 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2208
39a53e0c
JK
2209/*
2210 * file.c
2211 */
cac5a3d8
DS
2212int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2213void truncate_data_blocks(struct dnode_of_data *dn);
2214int truncate_blocks(struct inode *inode, u64 from, bool lock);
2215int f2fs_truncate(struct inode *inode);
a528d35e
DH
2216int f2fs_getattr(const struct path *path, struct kstat *stat,
2217 u32 request_mask, unsigned int flags);
cac5a3d8
DS
2218int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2219int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2220int truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2221long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2222long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
39a53e0c
JK
2223
2224/*
2225 * inode.c
2226 */
cac5a3d8
DS
2227void f2fs_set_inode_flags(struct inode *inode);
2228struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2229struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2230int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2231int update_inode(struct inode *inode, struct page *node_page);
2232int update_inode_page(struct inode *inode);
2233int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2234void f2fs_evict_inode(struct inode *inode);
2235void handle_failed_inode(struct inode *inode);
39a53e0c
JK
2236
2237/*
2238 * namei.c
2239 */
2240struct dentry *f2fs_get_parent(struct dentry *child);
2241
2242/*
2243 * dir.c
2244 */
cac5a3d8
DS
2245void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
2246unsigned char get_de_type(struct f2fs_dir_entry *de);
2247struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
2248 f2fs_hash_t namehash, int *max_slots,
2249 struct f2fs_dentry_ptr *d);
2250int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2251 unsigned int start_pos, struct fscrypt_str *fstr);
2252void do_make_empty_dir(struct inode *inode, struct inode *parent,
2253 struct f2fs_dentry_ptr *d);
2254struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
2255 const struct qstr *new_name,
2256 const struct qstr *orig_name, struct page *dpage);
2257void update_parent_metadata(struct inode *dir, struct inode *inode,
2258 unsigned int current_depth);
2259int room_for_filename(const void *bitmap, int slots, int max_slots);
2260void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2261struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2262 struct fscrypt_name *fname, struct page **res_page);
2263struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2264 const struct qstr *child, struct page **res_page);
2265struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2266ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2267 struct page **page);
2268void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2269 struct page *page, struct inode *inode);
cac5a3d8
DS
2270void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2271 const struct qstr *name, f2fs_hash_t name_hash,
2272 unsigned int bit_pos);
2273int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2274 const struct qstr *orig_name,
2275 struct inode *inode, nid_t ino, umode_t mode);
2276int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
2277 struct inode *inode, nid_t ino, umode_t mode);
2278int __f2fs_add_link(struct inode *dir, const struct qstr *name,
2279 struct inode *inode, nid_t ino, umode_t mode);
2280void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2281 struct inode *dir, struct inode *inode);
2282int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2283bool f2fs_empty_dir(struct inode *dir);
39a53e0c 2284
b7f7a5e0
AV
2285static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2286{
2b0143b5 2287 return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
510022a8 2288 inode, inode->i_ino, inode->i_mode);
b7f7a5e0
AV
2289}
2290
39a53e0c
JK
2291/*
2292 * super.c
2293 */
cac5a3d8
DS
2294int f2fs_inode_dirtied(struct inode *inode, bool sync);
2295void f2fs_inode_synced(struct inode *inode);
2296int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2297int f2fs_sync_fs(struct super_block *sb, int sync);
a07ef784 2298extern __printf(3, 4)
cac5a3d8 2299void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
984ec63c 2300int sanity_check_ckpt(struct f2fs_sb_info *sbi);
39a53e0c
JK
2301
2302/*
2303 * hash.c
2304 */
6332cd32
JK
2305f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
2306 struct fscrypt_name *fname);
39a53e0c
JK
2307
2308/*
2309 * node.c
2310 */
2311struct dnode_of_data;
2312struct node_info;
2313
cac5a3d8
DS
2314bool available_free_memory(struct f2fs_sb_info *sbi, int type);
2315int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2316bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2317bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2318void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
2319pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2320int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2321int truncate_inode_blocks(struct inode *inode, pgoff_t from);
2322int truncate_xattr_node(struct inode *inode, struct page *page);
2323int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2324int remove_inode_page(struct inode *inode);
2325struct page *new_inode_page(struct inode *inode);
5f4ce6ab 2326struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs);
cac5a3d8
DS
2327void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2328struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2329struct page *get_node_page_ra(struct page *parent, int start);
2330void move_node_page(struct page *node_page, int gc_type);
2331int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2332 struct writeback_control *wbc, bool atomic);
2333int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc);
22ad0b6a 2334void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
cac5a3d8
DS
2335bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2336void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2337void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2338int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2339void recover_inline_xattr(struct inode *inode, struct page *page);
d260081c 2340int recover_xattr_data(struct inode *inode, struct page *page,
cac5a3d8
DS
2341 block_t blkaddr);
2342int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2343int restore_node_summary(struct f2fs_sb_info *sbi,
2344 unsigned int segno, struct f2fs_summary_block *sum);
22ad0b6a 2345void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
cac5a3d8
DS
2346int build_node_manager(struct f2fs_sb_info *sbi);
2347void destroy_node_manager(struct f2fs_sb_info *sbi);
6e6093a8 2348int __init create_node_manager_caches(void);
39a53e0c
JK
2349void destroy_node_manager_caches(void);
2350
2351/*
2352 * segment.c
2353 */
cac5a3d8
DS
2354void register_inmem_page(struct inode *inode, struct page *page);
2355void drop_inmem_pages(struct inode *inode);
8c242db9 2356void drop_inmem_page(struct inode *inode, struct page *page);
cac5a3d8
DS
2357int commit_inmem_pages(struct inode *inode);
2358void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2359void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
2360int f2fs_issue_flush(struct f2fs_sb_info *sbi);
2361int create_flush_cmd_control(struct f2fs_sb_info *sbi);
2362void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2363void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2364bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
2365void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new);
cce13252 2366void stop_discard_thread(struct f2fs_sb_info *sbi);
d431413f 2367void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
cac5a3d8
DS
2368void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2369void release_discard_addrs(struct f2fs_sb_info *sbi);
2370int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2371void allocate_new_segments(struct f2fs_sb_info *sbi);
2372int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2373bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2374struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2375void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2376void write_meta_page(struct f2fs_sb_info *sbi, struct page *page);
2377void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2378void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
d1b3e72d 2379int rewrite_data_page(struct f2fs_io_info *fio);
cac5a3d8
DS
2380void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2381 block_t old_blkaddr, block_t new_blkaddr,
2382 bool recover_curseg, bool recover_newaddr);
2383void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2384 block_t old_addr, block_t new_addr,
2385 unsigned char version, bool recover_curseg,
2386 bool recover_newaddr);
2387void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2388 block_t old_blkaddr, block_t *new_blkaddr,
fb830fc5
CY
2389 struct f2fs_summary *sum, int type,
2390 struct f2fs_io_info *fio, bool add_list);
cac5a3d8
DS
2391void f2fs_wait_on_page_writeback(struct page *page,
2392 enum page_type type, bool ordered);
2393void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
2394 block_t blkaddr);
2395void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2396void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2397int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2398 unsigned int val, int alloc);
2399void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2400int build_segment_manager(struct f2fs_sb_info *sbi);
2401void destroy_segment_manager(struct f2fs_sb_info *sbi);
7fd9e544
JK
2402int __init create_segment_manager_caches(void);
2403void destroy_segment_manager_caches(void);
39a53e0c
JK
2404
2405/*
2406 * checkpoint.c
2407 */
cac5a3d8
DS
2408void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
2409struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2410struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2411struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2412bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type);
2413int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2414 int type, bool sync);
2415void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2416long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2417 long nr_to_write);
2418void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2419void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2420void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2421bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
2422int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
2423int acquire_orphan_inode(struct f2fs_sb_info *sbi);
2424void release_orphan_inode(struct f2fs_sb_info *sbi);
2425void add_orphan_inode(struct inode *inode);
2426void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2427int recover_orphan_inodes(struct f2fs_sb_info *sbi);
2428int get_valid_checkpoint(struct f2fs_sb_info *sbi);
2429void update_dirty_page(struct inode *inode, struct page *page);
2430void remove_dirty_inode(struct inode *inode);
2431int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2432int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2433void init_ino_entry_info(struct f2fs_sb_info *sbi);
6e6093a8 2434int __init create_checkpoint_caches(void);
39a53e0c
JK
2435void destroy_checkpoint_caches(void);
2436
2437/*
2438 * data.c
2439 */
b9109b0e
JK
2440void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2441void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
942fd319 2442 struct inode *inode, nid_t ino, pgoff_t idx,
b9109b0e
JK
2443 enum page_type type);
2444void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
cac5a3d8 2445int f2fs_submit_page_bio(struct f2fs_io_info *fio);
b9109b0e 2446int f2fs_submit_page_write(struct f2fs_io_info *fio);
cac5a3d8
DS
2447struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2448 block_t blk_addr, struct bio *bio);
2449int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
2450void set_data_blkaddr(struct dnode_of_data *dn);
2451void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
2452int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2453int reserve_new_block(struct dnode_of_data *dn);
2454int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2455int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2456int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
2457struct page *get_read_data_page(struct inode *inode, pgoff_t index,
2458 int op_flags, bool for_write);
2459struct page *find_data_page(struct inode *inode, pgoff_t index);
2460struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
2461 bool for_write);
2462struct page *get_new_data_page(struct inode *inode,
2463 struct page *ipage, pgoff_t index, bool new_i_size);
2464int do_write_data_page(struct f2fs_io_info *fio);
2465int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2466 int create, int flag);
2467int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2468 u64 start, u64 len);
2469void f2fs_set_page_dirty_nobuffers(struct page *page);
2470void f2fs_invalidate_page(struct page *page, unsigned int offset,
2471 unsigned int length);
2472int f2fs_release_page(struct page *page, gfp_t wait);
5b7a487c 2473#ifdef CONFIG_MIGRATION
cac5a3d8
DS
2474int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2475 struct page *page, enum migrate_mode mode);
5b7a487c 2476#endif
39a53e0c
JK
2477
2478/*
2479 * gc.c
2480 */
cac5a3d8
DS
2481int start_gc_thread(struct f2fs_sb_info *sbi);
2482void stop_gc_thread(struct f2fs_sb_info *sbi);
2483block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
e066b83c
JK
2484int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2485 unsigned int segno);
cac5a3d8 2486void build_gc_manager(struct f2fs_sb_info *sbi);
39a53e0c
JK
2487
2488/*
2489 * recovery.c
2490 */
cac5a3d8
DS
2491int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
2492bool space_for_roll_forward(struct f2fs_sb_info *sbi);
39a53e0c
JK
2493
2494/*
2495 * debug.c
2496 */
2497#ifdef CONFIG_F2FS_STAT_FS
2498struct f2fs_stat_info {
2499 struct list_head stat_list;
2500 struct f2fs_sb_info *sbi;
39a53e0c
JK
2501 int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
2502 int main_area_segs, main_area_sections, main_area_zones;
5b7ee374
CY
2503 unsigned long long hit_largest, hit_cached, hit_rbtree;
2504 unsigned long long hit_total, total_ext;
c00ba554 2505 int ext_tree, zombie_tree, ext_node;
35782b23
JK
2506 int ndirty_node, ndirty_dent, ndirty_meta, ndirty_data, ndirty_imeta;
2507 int inmem_pages;
0f18b462 2508 unsigned int ndirty_dirs, ndirty_files, ndirty_all;
5b0ef73c
JK
2509 int nats, dirty_nats, sits, dirty_sits;
2510 int free_nids, avail_nids, alloc_nids;
39a53e0c 2511 int total_count, utilization;
8b8dd65f
CY
2512 int bg_gc, nr_wb_cp_data, nr_wb_data;
2513 int nr_flushing, nr_flushed, nr_discarding, nr_discarded;
5f32366a 2514 int nr_discard_cmd;
d84d1cbd 2515 unsigned int undiscard_blks;
a00861db 2516 int inline_xattr, inline_inode, inline_dir, append, update, orphans;
648d50ba 2517 int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
f83a2584 2518 unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
39a53e0c
JK
2519 unsigned int bimodal, avg_vblocks;
2520 int util_free, util_valid, util_invalid;
2521 int rsvd_segs, overp_segs;
2522 int dirty_count, node_pages, meta_pages;
42190d2a 2523 int prefree_count, call_count, cp_count, bg_cp_count;
39a53e0c 2524 int tot_segs, node_segs, data_segs, free_segs, free_secs;
e1235983 2525 int bg_node_segs, bg_data_segs;
39a53e0c 2526 int tot_blks, data_blks, node_blks;
e1235983 2527 int bg_data_blks, bg_node_blks;
39a53e0c
JK
2528 int curseg[NR_CURSEG_TYPE];
2529 int cursec[NR_CURSEG_TYPE];
2530 int curzone[NR_CURSEG_TYPE];
2531
2532 unsigned int segment_count[2];
2533 unsigned int block_count[2];
b9a2c252 2534 unsigned int inplace_count;
9edcdabf 2535 unsigned long long base_mem, cache_mem, page_mem;
39a53e0c
JK
2536};
2537
963d4f7d
GZ
2538static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
2539{
6c311ec6 2540 return (struct f2fs_stat_info *)sbi->stat_info;
963d4f7d
GZ
2541}
2542
942e0be6 2543#define stat_inc_cp_count(si) ((si)->cp_count++)
42190d2a 2544#define stat_inc_bg_cp_count(si) ((si)->bg_cp_count++)
dcdfff65
JK
2545#define stat_inc_call_count(si) ((si)->call_count++)
2546#define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
33fbd510
CY
2547#define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++)
2548#define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--)
5b7ee374
CY
2549#define stat_inc_total_hit(sbi) (atomic64_inc(&(sbi)->total_hit_ext))
2550#define stat_inc_rbtree_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_rbtree))
2551#define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest))
2552#define stat_inc_cached_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_cached))
d5e8f6c9
CY
2553#define stat_inc_inline_xattr(inode) \
2554 do { \
2555 if (f2fs_has_inline_xattr(inode)) \
2556 (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \
2557 } while (0)
2558#define stat_dec_inline_xattr(inode) \
2559 do { \
2560 if (f2fs_has_inline_xattr(inode)) \
2561 (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \
2562 } while (0)
0dbdc2ae
JK
2563#define stat_inc_inline_inode(inode) \
2564 do { \
2565 if (f2fs_has_inline_data(inode)) \
03e14d52 2566 (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
0dbdc2ae
JK
2567 } while (0)
2568#define stat_dec_inline_inode(inode) \
2569 do { \
2570 if (f2fs_has_inline_data(inode)) \
03e14d52 2571 (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
0dbdc2ae 2572 } while (0)
3289c061
JK
2573#define stat_inc_inline_dir(inode) \
2574 do { \
2575 if (f2fs_has_inline_dentry(inode)) \
03e14d52 2576 (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
3289c061
JK
2577 } while (0)
2578#define stat_dec_inline_dir(inode) \
2579 do { \
2580 if (f2fs_has_inline_dentry(inode)) \
03e14d52 2581 (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
3289c061 2582 } while (0)
dcdfff65
JK
2583#define stat_inc_seg_type(sbi, curseg) \
2584 ((sbi)->segment_count[(curseg)->alloc_type]++)
2585#define stat_inc_block_count(sbi, curseg) \
2586 ((sbi)->block_count[(curseg)->alloc_type]++)
b9a2c252
CL
2587#define stat_inc_inplace_blocks(sbi) \
2588 (atomic_inc(&(sbi)->inplace_count))
26a28a0c 2589#define stat_inc_atomic_write(inode) \
cac5a3d8 2590 (atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
26a28a0c 2591#define stat_dec_atomic_write(inode) \
cac5a3d8 2592 (atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
26a28a0c
JK
2593#define stat_update_max_atomic_write(inode) \
2594 do { \
2595 int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt); \
2596 int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt); \
2597 if (cur > max) \
2598 atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur); \
2599 } while (0)
648d50ba
CY
2600#define stat_inc_volatile_write(inode) \
2601 (atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
2602#define stat_dec_volatile_write(inode) \
2603 (atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
2604#define stat_update_max_volatile_write(inode) \
2605 do { \
2606 int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt); \
2607 int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt); \
2608 if (cur > max) \
2609 atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur); \
2610 } while (0)
e1235983 2611#define stat_inc_seg_count(sbi, type, gc_type) \
39a53e0c 2612 do { \
963d4f7d 2613 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
68afcf2d
TK
2614 si->tot_segs++; \
2615 if ((type) == SUM_TYPE_DATA) { \
39a53e0c 2616 si->data_segs++; \
e1235983
CL
2617 si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
2618 } else { \
39a53e0c 2619 si->node_segs++; \
e1235983
CL
2620 si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
2621 } \
39a53e0c
JK
2622 } while (0)
2623
2624#define stat_inc_tot_blk_count(si, blks) \
68afcf2d 2625 ((si)->tot_blks += (blks))
39a53e0c 2626
e1235983 2627#define stat_inc_data_blk_count(sbi, blks, gc_type) \
39a53e0c 2628 do { \
963d4f7d 2629 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
39a53e0c
JK
2630 stat_inc_tot_blk_count(si, blks); \
2631 si->data_blks += (blks); \
68afcf2d 2632 si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
39a53e0c
JK
2633 } while (0)
2634
e1235983 2635#define stat_inc_node_blk_count(sbi, blks, gc_type) \
39a53e0c 2636 do { \
963d4f7d 2637 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
39a53e0c
JK
2638 stat_inc_tot_blk_count(si, blks); \
2639 si->node_blks += (blks); \
68afcf2d 2640 si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
39a53e0c
JK
2641 } while (0)
2642
cac5a3d8
DS
2643int f2fs_build_stats(struct f2fs_sb_info *sbi);
2644void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
787c7b8c 2645int __init f2fs_create_root_stats(void);
4589d25d 2646void f2fs_destroy_root_stats(void);
39a53e0c 2647#else
d66450e7
AB
2648#define stat_inc_cp_count(si) do { } while (0)
2649#define stat_inc_bg_cp_count(si) do { } while (0)
2650#define stat_inc_call_count(si) do { } while (0)
2651#define stat_inc_bggc_count(si) do { } while (0)
2652#define stat_inc_dirty_inode(sbi, type) do { } while (0)
2653#define stat_dec_dirty_inode(sbi, type) do { } while (0)
2654#define stat_inc_total_hit(sb) do { } while (0)
2655#define stat_inc_rbtree_node_hit(sb) do { } while (0)
2656#define stat_inc_largest_node_hit(sbi) do { } while (0)
2657#define stat_inc_cached_node_hit(sbi) do { } while (0)
2658#define stat_inc_inline_xattr(inode) do { } while (0)
2659#define stat_dec_inline_xattr(inode) do { } while (0)
2660#define stat_inc_inline_inode(inode) do { } while (0)
2661#define stat_dec_inline_inode(inode) do { } while (0)
2662#define stat_inc_inline_dir(inode) do { } while (0)
2663#define stat_dec_inline_dir(inode) do { } while (0)
2664#define stat_inc_atomic_write(inode) do { } while (0)
2665#define stat_dec_atomic_write(inode) do { } while (0)
2666#define stat_update_max_atomic_write(inode) do { } while (0)
2667#define stat_inc_volatile_write(inode) do { } while (0)
2668#define stat_dec_volatile_write(inode) do { } while (0)
2669#define stat_update_max_volatile_write(inode) do { } while (0)
2670#define stat_inc_seg_type(sbi, curseg) do { } while (0)
2671#define stat_inc_block_count(sbi, curseg) do { } while (0)
2672#define stat_inc_inplace_blocks(sbi) do { } while (0)
2673#define stat_inc_seg_count(sbi, type, gc_type) do { } while (0)
2674#define stat_inc_tot_blk_count(si, blks) do { } while (0)
2675#define stat_inc_data_blk_count(sbi, blks, gc_type) do { } while (0)
2676#define stat_inc_node_blk_count(sbi, blks, gc_type) do { } while (0)
39a53e0c
JK
2677
2678static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
2679static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
787c7b8c 2680static inline int __init f2fs_create_root_stats(void) { return 0; }
4589d25d 2681static inline void f2fs_destroy_root_stats(void) { }
39a53e0c
JK
2682#endif
2683
2684extern const struct file_operations f2fs_dir_operations;
2685extern const struct file_operations f2fs_file_operations;
2686extern const struct inode_operations f2fs_file_inode_operations;
2687extern const struct address_space_operations f2fs_dblock_aops;
2688extern const struct address_space_operations f2fs_node_aops;
2689extern const struct address_space_operations f2fs_meta_aops;
2690extern const struct inode_operations f2fs_dir_inode_operations;
2691extern const struct inode_operations f2fs_symlink_inode_operations;
cbaf042a 2692extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
39a53e0c 2693extern const struct inode_operations f2fs_special_inode_operations;
29e7043f 2694extern struct kmem_cache *inode_entry_slab;
1001b347 2695
e18c65b2
HL
2696/*
2697 * inline.c
2698 */
cac5a3d8
DS
2699bool f2fs_may_inline_data(struct inode *inode);
2700bool f2fs_may_inline_dentry(struct inode *inode);
2701void read_inline_data(struct page *page, struct page *ipage);
bd4667cb 2702void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
cac5a3d8
DS
2703int f2fs_read_inline_data(struct inode *inode, struct page *page);
2704int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
2705int f2fs_convert_inline_inode(struct inode *inode);
2706int f2fs_write_inline_data(struct inode *inode, struct page *page);
2707bool recover_inline_data(struct inode *inode, struct page *npage);
2708struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
2709 struct fscrypt_name *fname, struct page **res_page);
2710int make_empty_inline_dir(struct inode *inode, struct inode *parent,
2711 struct page *ipage);
2712int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
2713 const struct qstr *orig_name,
2714 struct inode *inode, nid_t ino, umode_t mode);
2715void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
2716 struct inode *dir, struct inode *inode);
2717bool f2fs_empty_inline_dir(struct inode *dir);
2718int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
2719 struct fscrypt_str *fstr);
2720int f2fs_inline_data_fiemap(struct inode *inode,
2721 struct fiemap_extent_info *fieinfo,
2722 __u64 start, __u64 len);
cde4de12 2723
2658e50d
JK
2724/*
2725 * shrinker.c
2726 */
cac5a3d8
DS
2727unsigned long f2fs_shrink_count(struct shrinker *shrink,
2728 struct shrink_control *sc);
2729unsigned long f2fs_shrink_scan(struct shrinker *shrink,
2730 struct shrink_control *sc);
2731void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
2732void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
2658e50d 2733
a28ef1f5
CY
2734/*
2735 * extent_cache.c
2736 */
004b6862
CY
2737struct rb_entry *__lookup_rb_tree(struct rb_root *root,
2738 struct rb_entry *cached_re, unsigned int ofs);
2739struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
2740 struct rb_root *root, struct rb_node **parent,
2741 unsigned int ofs);
2742struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
2743 struct rb_entry *cached_re, unsigned int ofs,
2744 struct rb_entry **prev_entry, struct rb_entry **next_entry,
2745 struct rb_node ***insert_p, struct rb_node **insert_parent,
2746 bool force);
df0f6b44
CY
2747bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
2748 struct rb_root *root);
cac5a3d8
DS
2749unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
2750bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
2751void f2fs_drop_extent_tree(struct inode *inode);
2752unsigned int f2fs_destroy_extent_node(struct inode *inode);
2753void f2fs_destroy_extent_tree(struct inode *inode);
2754bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
2755 struct extent_info *ei);
2756void f2fs_update_extent_cache(struct dnode_of_data *dn);
19b2c30d 2757void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
cac5a3d8
DS
2758 pgoff_t fofs, block_t blkaddr, unsigned int len);
2759void init_extent_cache_info(struct f2fs_sb_info *sbi);
a28ef1f5
CY
2760int __init create_extent_cache(void);
2761void destroy_extent_cache(void);
2762
8ceffcb2
CY
2763/*
2764 * sysfs.c
2765 */
2766int __init f2fs_register_sysfs(void);
2767void f2fs_unregister_sysfs(void);
2768int f2fs_init_sysfs(struct f2fs_sb_info *sbi);
2769void f2fs_exit_sysfs(struct f2fs_sb_info *sbi);
2770
cde4de12
JK
2771/*
2772 * crypto support
2773 */
0b81d077 2774static inline bool f2fs_encrypted_inode(struct inode *inode)
cde4de12 2775{
cde4de12 2776 return file_is_encrypt(inode);
cde4de12
JK
2777}
2778
2779static inline void f2fs_set_encrypted_inode(struct inode *inode)
2780{
2781#ifdef CONFIG_F2FS_FS_ENCRYPTION
2782 file_set_encrypt(inode);
2783#endif
2784}
2785
2786static inline bool f2fs_bio_encrypted(struct bio *bio)
2787{
0b81d077 2788 return bio->bi_private != NULL;
cde4de12
JK
2789}
2790
2791static inline int f2fs_sb_has_crypto(struct super_block *sb)
2792{
cde4de12 2793 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
cde4de12 2794}
f424f664 2795
0bfd7a09 2796static inline int f2fs_sb_mounted_blkzoned(struct super_block *sb)
52763a4b 2797{
0bfd7a09 2798 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_BLKZONED);
52763a4b
JK
2799}
2800
178053e2
DLM
2801#ifdef CONFIG_BLK_DEV_ZONED
2802static inline int get_blkz_type(struct f2fs_sb_info *sbi,
3c62be17 2803 struct block_device *bdev, block_t blkaddr)
178053e2
DLM
2804{
2805 unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
3c62be17 2806 int i;
178053e2 2807
3c62be17
JK
2808 for (i = 0; i < sbi->s_ndevs; i++)
2809 if (FDEV(i).bdev == bdev)
2810 return FDEV(i).blkz_type[zno];
2811 return -EINVAL;
178053e2
DLM
2812}
2813#endif
2814
96ba2dec 2815static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
52763a4b 2816{
96ba2dec
DLM
2817 struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
2818
2819 return blk_queue_discard(q) || f2fs_sb_mounted_blkzoned(sbi->sb);
52763a4b
JK
2820}
2821
2822static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
2823{
2824 clear_opt(sbi, ADAPTIVE);
2825 clear_opt(sbi, LFS);
2826
2827 switch (mt) {
2828 case F2FS_MOUNT_ADAPTIVE:
2829 set_opt(sbi, ADAPTIVE);
2830 break;
2831 case F2FS_MOUNT_LFS:
2832 set_opt(sbi, LFS);
2833 break;
2834 }
2835}
2836
fcc85a4d
JK
2837static inline bool f2fs_may_encrypt(struct inode *inode)
2838{
2839#ifdef CONFIG_F2FS_FS_ENCRYPTION
886f56f9 2840 umode_t mode = inode->i_mode;
fcc85a4d
JK
2841
2842 return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
2843#else
2844 return 0;
2845#endif
2846}
2847
39a53e0c 2848#endif