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