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