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