]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/f2fs/f2fs.h
f2fs: inject page allocation failures
[mirror_ubuntu-jammy-kernel.git] / fs / f2fs / f2fs.h
CommitLineData
0a8165d7 1/*
39a53e0c
JK
2 * fs/f2fs/f2fs.h
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#ifndef _LINUX_F2FS_H
12#define _LINUX_F2FS_H
13
14#include <linux/types.h>
15#include <linux/page-flags.h>
16#include <linux/buffer_head.h>
39a53e0c
JK
17#include <linux/slab.h>
18#include <linux/crc32.h>
19#include <linux/magic.h>
c2d715d1 20#include <linux/kobject.h>
7bd59381 21#include <linux/sched.h>
39307a8e 22#include <linux/vmalloc.h>
740432f8 23#include <linux/bio.h>
d0239e1b 24#include <linux/blkdev.h>
0b81d077 25#include <linux/fscrypto.h>
43b6573b 26#include <crypto/hash.h>
39a53e0c 27
5d56b671 28#ifdef CONFIG_F2FS_CHECK_FS
9850cf4a 29#define f2fs_bug_on(sbi, condition) BUG_ON(condition)
5d56b671 30#else
9850cf4a
JK
31#define f2fs_bug_on(sbi, condition) \
32 do { \
33 if (unlikely(condition)) { \
34 WARN_ON(1); \
caf0047e 35 set_sbi_flag(sbi, SBI_NEED_FSCK); \
9850cf4a
JK
36 } \
37 } while (0)
5d56b671
JK
38#endif
39
2c63fead
JK
40#ifdef CONFIG_F2FS_FAULT_INJECTION
41enum {
42 FAULT_KMALLOC,
c41f3cc3 43 FAULT_PAGE_ALLOC,
2c63fead
JK
44 FAULT_MAX,
45};
46
47extern u32 f2fs_fault_rate;
48extern atomic_t f2fs_ops;
49extern char *fault_name[FAULT_MAX];
50
51static inline bool time_to_inject(int type)
52{
53 atomic_inc(&f2fs_ops);
54 if (f2fs_fault_rate && (atomic_read(&f2fs_ops) >= f2fs_fault_rate)) {
55 atomic_set(&f2fs_ops, 0);
56 printk("%sF2FS-fs : inject %s in %pF\n",
57 KERN_INFO,
58 fault_name[type],
59 __builtin_return_address(0));
60 return true;
61 }
62 return false;
63}
64#endif
65
39a53e0c
JK
66/*
67 * For mount options
68 */
69#define F2FS_MOUNT_BG_GC 0x00000001
70#define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
71#define F2FS_MOUNT_DISCARD 0x00000004
72#define F2FS_MOUNT_NOHEAP 0x00000008
73#define F2FS_MOUNT_XATTR_USER 0x00000010
74#define F2FS_MOUNT_POSIX_ACL 0x00000020
75#define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
444c580f 76#define F2FS_MOUNT_INLINE_XATTR 0x00000080
1001b347 77#define F2FS_MOUNT_INLINE_DATA 0x00000100
34d67deb
CY
78#define F2FS_MOUNT_INLINE_DENTRY 0x00000200
79#define F2FS_MOUNT_FLUSH_MERGE 0x00000400
80#define F2FS_MOUNT_NOBARRIER 0x00000800
d5053a34 81#define F2FS_MOUNT_FASTBOOT 0x00001000
89672159 82#define F2FS_MOUNT_EXTENT_CACHE 0x00002000
6aefd93b 83#define F2FS_MOUNT_FORCE_FG_GC 0x00004000
343f40f0 84#define F2FS_MOUNT_DATA_FLUSH 0x00008000
73faec4d 85#define F2FS_MOUNT_FAULT_INJECTION 0x00010000
39a53e0c
JK
86
87#define clear_opt(sbi, option) (sbi->mount_opt.opt &= ~F2FS_MOUNT_##option)
88#define set_opt(sbi, option) (sbi->mount_opt.opt |= F2FS_MOUNT_##option)
89#define test_opt(sbi, option) (sbi->mount_opt.opt & F2FS_MOUNT_##option)
90
91#define ver_after(a, b) (typecheck(unsigned long long, a) && \
92 typecheck(unsigned long long, b) && \
93 ((long long)((a) - (b)) > 0))
94
a9841c4d
JK
95typedef u32 block_t; /*
96 * should not change u32, since it is the on-disk block
97 * address format, __le32.
98 */
39a53e0c
JK
99typedef u32 nid_t;
100
101struct f2fs_mount_info {
102 unsigned int opt;
103};
104
cde4de12
JK
105#define F2FS_FEATURE_ENCRYPT 0x0001
106
76f105a2
JK
107#define F2FS_HAS_FEATURE(sb, mask) \
108 ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
109#define F2FS_SET_FEATURE(sb, mask) \
110 F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask)
111#define F2FS_CLEAR_FEATURE(sb, mask) \
112 F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask)
113
39a53e0c
JK
114/*
115 * For checkpoint manager
116 */
117enum {
118 NAT_BITMAP,
119 SIT_BITMAP
120};
121
75ab4cb8
JK
122enum {
123 CP_UMOUNT,
119ee914 124 CP_FASTBOOT,
75ab4cb8 125 CP_SYNC,
10027551 126 CP_RECOVERY,
4b2fecc8 127 CP_DISCARD,
75ab4cb8
JK
128};
129
bba681cb
JK
130#define DEF_BATCHED_TRIM_SECTIONS 32
131#define BATCHED_TRIM_SEGMENTS(sbi) \
132 (SM_I(sbi)->trim_sections * (sbi)->segs_per_sec)
a66cdd98
JK
133#define BATCHED_TRIM_BLOCKS(sbi) \
134 (BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
60b99b48 135#define DEF_CP_INTERVAL 60 /* 60 secs */
d0239e1b 136#define DEF_IDLE_INTERVAL 120 /* 2 mins */
bba681cb 137
75ab4cb8
JK
138struct cp_control {
139 int reason;
4b2fecc8
JK
140 __u64 trim_start;
141 __u64 trim_end;
142 __u64 trim_minlen;
143 __u64 trimmed;
75ab4cb8
JK
144};
145
662befda 146/*
81c1a0f1 147 * For CP/NAT/SIT/SSA readahead
662befda
CY
148 */
149enum {
150 META_CP,
151 META_NAT,
81c1a0f1 152 META_SIT,
4c521f49
JK
153 META_SSA,
154 META_POR,
662befda
CY
155};
156
6451e041
JK
157/* for the list of ino */
158enum {
159 ORPHAN_INO, /* for orphan ino list */
fff04f90
JK
160 APPEND_INO, /* for append ino list */
161 UPDATE_INO, /* for update ino list */
6451e041
JK
162 MAX_INO_ENTRY, /* max. list */
163};
164
165struct ino_entry {
39a53e0c
JK
166 struct list_head list; /* list head */
167 nid_t ino; /* inode number */
168};
169
2710fd7e 170/* for the list of inodes to be GCed */
06292073 171struct inode_entry {
39a53e0c
JK
172 struct list_head list; /* list head */
173 struct inode *inode; /* vfs inode pointer */
174};
175
7fd9e544
JK
176/* for the list of blockaddresses to be discarded */
177struct discard_entry {
178 struct list_head list; /* list head */
179 block_t blkaddr; /* block address to be discarded */
180 int len; /* # of consecutive blocks of the discard */
181};
182
39a53e0c
JK
183/* for the list of fsync inodes, used only during recovery */
184struct fsync_inode_entry {
185 struct list_head list; /* list head */
186 struct inode *inode; /* vfs inode pointer */
c52e1b10
JK
187 block_t blkaddr; /* block address locating the last fsync */
188 block_t last_dentry; /* block address locating the last dentry */
39a53e0c
JK
189};
190
dfc08a12
CY
191#define nats_in_cursum(jnl) (le16_to_cpu(jnl->n_nats))
192#define sits_in_cursum(jnl) (le16_to_cpu(jnl->n_sits))
39a53e0c 193
dfc08a12
CY
194#define nat_in_journal(jnl, i) (jnl->nat_j.entries[i].ne)
195#define nid_in_journal(jnl, i) (jnl->nat_j.entries[i].nid)
196#define sit_in_journal(jnl, i) (jnl->sit_j.entries[i].se)
197#define segno_in_journal(jnl, i) (jnl->sit_j.entries[i].segno)
39a53e0c 198
dfc08a12
CY
199#define MAX_NAT_JENTRIES(jnl) (NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
200#define MAX_SIT_JENTRIES(jnl) (SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
309cc2b6 201
dfc08a12 202static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
39a53e0c 203{
dfc08a12
CY
204 int before = nats_in_cursum(journal);
205 journal->n_nats = cpu_to_le16(before + i);
39a53e0c
JK
206 return before;
207}
208
dfc08a12 209static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
39a53e0c 210{
dfc08a12
CY
211 int before = sits_in_cursum(journal);
212 journal->n_sits = cpu_to_le16(before + i);
39a53e0c
JK
213 return before;
214}
215
dfc08a12
CY
216static inline bool __has_cursum_space(struct f2fs_journal *journal,
217 int size, int type)
184a5cd2
CY
218{
219 if (type == NAT_JOURNAL)
dfc08a12
CY
220 return size <= MAX_NAT_JENTRIES(journal);
221 return size <= MAX_SIT_JENTRIES(journal);
184a5cd2
CY
222}
223
e9750824
NJ
224/*
225 * ioctl commands
226 */
88b88a66
JK
227#define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
228#define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
d49f3e89 229#define F2FS_IOC_GETVERSION FS_IOC_GETVERSION
88b88a66
JK
230
231#define F2FS_IOCTL_MAGIC 0xf5
232#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
233#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
02a1335f 234#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
1e84371f
JK
235#define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
236#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
c1c1b583 237#define F2FS_IOC_GARBAGE_COLLECT _IO(F2FS_IOCTL_MAGIC, 6)
456b88e4 238#define F2FS_IOC_WRITE_CHECKPOINT _IO(F2FS_IOCTL_MAGIC, 7)
d323d005 239#define F2FS_IOC_DEFRAGMENT _IO(F2FS_IOCTL_MAGIC, 8)
e9750824 240
0b81d077
JK
241#define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
242#define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
243#define F2FS_IOC_GET_ENCRYPTION_PWSALT FS_IOC_GET_ENCRYPTION_PWSALT
f424f664 244
1abff93d
JK
245/*
246 * should be same as XFS_IOC_GOINGDOWN.
247 * Flags for going down operation used by FS_IOC_GOINGDOWN
248 */
249#define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
250#define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
251#define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
252#define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
c912a829 253#define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */
1abff93d 254
e9750824
NJ
255#if defined(__KERNEL__) && defined(CONFIG_COMPAT)
256/*
257 * ioctl commands in 32 bit emulation
258 */
04ef4b62
CY
259#define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
260#define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
261#define F2FS_IOC32_GETVERSION FS_IOC32_GETVERSION
e9750824
NJ
262#endif
263
d323d005
CY
264struct f2fs_defragment {
265 u64 start;
266 u64 len;
267};
268
39a53e0c
JK
269/*
270 * For INODE and NODE manager
271 */
7b3cd7d6
JK
272/* for directory operations */
273struct f2fs_dentry_ptr {
d8c6822a 274 struct inode *inode;
7b3cd7d6
JK
275 const void *bitmap;
276 struct f2fs_dir_entry *dentry;
277 __u8 (*filename)[F2FS_SLOT_LEN];
278 int max;
279};
280
d8c6822a
JK
281static inline void make_dentry_ptr(struct inode *inode,
282 struct f2fs_dentry_ptr *d, void *src, int type)
7b3cd7d6 283{
d8c6822a
JK
284 d->inode = inode;
285
7b3cd7d6
JK
286 if (type == 1) {
287 struct f2fs_dentry_block *t = (struct f2fs_dentry_block *)src;
288 d->max = NR_DENTRY_IN_BLOCK;
289 d->bitmap = &t->dentry_bitmap;
290 d->dentry = t->dentry;
291 d->filename = t->filename;
292 } else {
293 struct f2fs_inline_dentry *t = (struct f2fs_inline_dentry *)src;
294 d->max = NR_INLINE_DENTRY;
295 d->bitmap = &t->dentry_bitmap;
296 d->dentry = t->dentry;
297 d->filename = t->filename;
298 }
299}
300
dbe6a5ff
JK
301/*
302 * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
303 * as its node offset to distinguish from index node blocks.
304 * But some bits are used to mark the node block.
305 */
306#define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
307 >> OFFSET_BIT_SHIFT)
266e97a8
JK
308enum {
309 ALLOC_NODE, /* allocate a new node page if needed */
310 LOOKUP_NODE, /* look up a node without readahead */
311 LOOKUP_NODE_RA, /*
312 * look up a node with readahead called
4f4124d0 313 * by get_data_block.
39a53e0c 314 */
266e97a8
JK
315};
316
a6db67f0 317#define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */
39a53e0c 318
817202d9
CY
319#define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
320
13054c54
CY
321/* vector size for gang look-up from extent cache that consists of radix tree */
322#define EXT_TREE_VEC_SIZE 64
323
39a53e0c 324/* for in-memory extent cache entry */
13054c54
CY
325#define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */
326
327/* number of extent info in extent cache we try to shrink */
328#define EXTENT_CACHE_SHRINK_NUMBER 128
c11abd1a 329
39a53e0c 330struct extent_info {
13054c54
CY
331 unsigned int fofs; /* start offset in a file */
332 u32 blk; /* start block address of the extent */
333 unsigned int len; /* length of the extent */
334};
335
336struct extent_node {
337 struct rb_node rb_node; /* rb node located in rb-tree */
338 struct list_head list; /* node in global extent list of sbi */
339 struct extent_info ei; /* extent info */
201ef5e0 340 struct extent_tree *et; /* extent tree pointer */
13054c54
CY
341};
342
343struct extent_tree {
344 nid_t ino; /* inode number */
345 struct rb_root root; /* root of extent info rb-tree */
62c8af65 346 struct extent_node *cached_en; /* recently accessed extent node */
3e72f721 347 struct extent_info largest; /* largested extent info */
137d09f0 348 struct list_head list; /* to be used by sbi->zombie_list */
13054c54 349 rwlock_t lock; /* protect extent info rb-tree */
68e35385 350 atomic_t node_cnt; /* # of extent node in rb-tree*/
39a53e0c
JK
351};
352
003a3e1d
JK
353/*
354 * This structure is taken from ext4_map_blocks.
355 *
356 * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
357 */
358#define F2FS_MAP_NEW (1 << BH_New)
359#define F2FS_MAP_MAPPED (1 << BH_Mapped)
7f63eb77
JK
360#define F2FS_MAP_UNWRITTEN (1 << BH_Unwritten)
361#define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
362 F2FS_MAP_UNWRITTEN)
003a3e1d
JK
363
364struct f2fs_map_blocks {
365 block_t m_pblk;
366 block_t m_lblk;
367 unsigned int m_len;
368 unsigned int m_flags;
da85985c 369 pgoff_t *m_next_pgofs; /* point next possible non-hole pgofs */
003a3e1d
JK
370};
371
e2b4e2bc
CY
372/* for flag in get_data_block */
373#define F2FS_GET_BLOCK_READ 0
374#define F2FS_GET_BLOCK_DIO 1
375#define F2FS_GET_BLOCK_FIEMAP 2
376#define F2FS_GET_BLOCK_BMAP 3
b439b103 377#define F2FS_GET_BLOCK_PRE_DIO 4
24b84912 378#define F2FS_GET_BLOCK_PRE_AIO 5
e2b4e2bc 379
39a53e0c
JK
380/*
381 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
382 */
383#define FADVISE_COLD_BIT 0x01
354a3399 384#define FADVISE_LOST_PINO_BIT 0x02
cde4de12 385#define FADVISE_ENCRYPT_BIT 0x04
e7d55452 386#define FADVISE_ENC_NAME_BIT 0x08
39a53e0c 387
b5492af7
JK
388#define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
389#define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
390#define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
391#define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
392#define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
393#define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
cde4de12
JK
394#define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT)
395#define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
396#define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
e7d55452
JK
397#define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT)
398#define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
cde4de12 399
ab9fa662
JK
400#define DEF_DIR_LEVEL 0
401
39a53e0c
JK
402struct f2fs_inode_info {
403 struct inode vfs_inode; /* serve a vfs inode */
404 unsigned long i_flags; /* keep an inode flags for ioctl */
405 unsigned char i_advise; /* use to give file attribute hints */
38431545 406 unsigned char i_dir_level; /* use for dentry level for large dir */
39a53e0c 407 unsigned int i_current_depth; /* use only in directory structure */
6666e6aa 408 unsigned int i_pino; /* parent inode number */
39a53e0c
JK
409 umode_t i_acl_mode; /* keep file acl mode temporarily */
410
411 /* Use below internally in f2fs*/
412 unsigned long flags; /* use to pass per-file flags */
d928bfbf 413 struct rw_semaphore i_sem; /* protect fi info */
a7ffdbe2 414 atomic_t dirty_pages; /* # of dirty pages */
39a53e0c
JK
415 f2fs_hash_t chash; /* hash value of given file name */
416 unsigned int clevel; /* maximum level of given file name */
417 nid_t i_xattr_nid; /* node id that contains xattrs */
e518ff81 418 unsigned long long xattr_ver; /* cp version of xattr modification */
88b88a66 419
2710fd7e 420 struct list_head dirty_list; /* linked in global dirty list */
88b88a66
JK
421 struct list_head inmem_pages; /* inmemory pages managed by f2fs */
422 struct mutex inmem_lock; /* lock for inmemory pages */
3e72f721 423 struct extent_tree *extent_tree; /* cached extent_tree entry */
39a53e0c
JK
424};
425
426static inline void get_extent_info(struct extent_info *ext,
427 struct f2fs_extent i_ext)
428{
39a53e0c 429 ext->fofs = le32_to_cpu(i_ext.fofs);
4d0b0bd4 430 ext->blk = le32_to_cpu(i_ext.blk);
39a53e0c 431 ext->len = le32_to_cpu(i_ext.len);
39a53e0c
JK
432}
433
434static inline void set_raw_extent(struct extent_info *ext,
435 struct f2fs_extent *i_ext)
436{
39a53e0c 437 i_ext->fofs = cpu_to_le32(ext->fofs);
4d0b0bd4 438 i_ext->blk = cpu_to_le32(ext->blk);
39a53e0c 439 i_ext->len = cpu_to_le32(ext->len);
39a53e0c
JK
440}
441
429511cd
CY
442static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
443 u32 blk, unsigned int len)
444{
445 ei->fofs = fofs;
446 ei->blk = blk;
447 ei->len = len;
448}
449
0bdee482
CY
450static inline bool __is_extent_same(struct extent_info *ei1,
451 struct extent_info *ei2)
452{
453 return (ei1->fofs == ei2->fofs && ei1->blk == ei2->blk &&
454 ei1->len == ei2->len);
455}
456
429511cd
CY
457static inline bool __is_extent_mergeable(struct extent_info *back,
458 struct extent_info *front)
459{
460 return (back->fofs + back->len == front->fofs &&
461 back->blk + back->len == front->blk);
462}
463
464static inline bool __is_back_mergeable(struct extent_info *cur,
465 struct extent_info *back)
466{
467 return __is_extent_mergeable(back, cur);
468}
469
470static inline bool __is_front_mergeable(struct extent_info *cur,
471 struct extent_info *front)
472{
473 return __is_extent_mergeable(cur, front);
474}
475
4abd3f5a
CY
476static inline void __try_update_largest_extent(struct extent_tree *et,
477 struct extent_node *en)
478{
479 if (en->ei.len > et->largest.len)
480 et->largest = en->ei;
481}
482
39a53e0c
JK
483struct f2fs_nm_info {
484 block_t nat_blkaddr; /* base disk address of NAT */
485 nid_t max_nid; /* maximum possible node ids */
7ee0eeab 486 nid_t available_nids; /* maximum available node ids */
39a53e0c 487 nid_t next_scan_nid; /* the next nid to be scanned */
cdfc41c1 488 unsigned int ram_thresh; /* control the memory footprint */
ea1a29a0 489 unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */
2304cb0c 490 unsigned int dirty_nats_ratio; /* control dirty nats ratio threshold */
39a53e0c
JK
491
492 /* NAT cache management */
493 struct radix_tree_root nat_root;/* root of the nat entry cache */
309cc2b6 494 struct radix_tree_root nat_set_root;/* root of the nat set cache */
8b26ef98 495 struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */
39a53e0c 496 struct list_head nat_entries; /* cached nat entry list (clean) */
309cc2b6 497 unsigned int nat_cnt; /* the # of cached nat entries */
aec71382 498 unsigned int dirty_nat_cnt; /* total num of nat entries in set */
39a53e0c
JK
499
500 /* free node ids management */
8a7ed66a 501 struct radix_tree_root free_nid_root;/* root of the free_nid cache */
39a53e0c
JK
502 struct list_head free_nid_list; /* a list for free nids */
503 spinlock_t free_nid_list_lock; /* protect free nid list */
504 unsigned int fcnt; /* the number of free node id */
505 struct mutex build_lock; /* lock for build free nids */
506
507 /* for checkpoint */
508 char *nat_bitmap; /* NAT bitmap pointer */
509 int bitmap_size; /* bitmap size */
510};
511
512/*
513 * this structure is used as one of function parameters.
514 * all the information are dedicated to a given direct node block determined
515 * by the data offset in a file.
516 */
517struct dnode_of_data {
518 struct inode *inode; /* vfs inode pointer */
519 struct page *inode_page; /* its inode page, NULL is possible */
520 struct page *node_page; /* cached direct node page */
521 nid_t nid; /* node id of the direct node block */
522 unsigned int ofs_in_node; /* data offset in the node page */
523 bool inode_page_locked; /* inode page is locked or not */
93bae099 524 bool node_changed; /* is node block changed */
3cf45747
CY
525 char cur_level; /* level of hole node page */
526 char max_level; /* level of current page located */
39a53e0c
JK
527 block_t data_blkaddr; /* block address of the node block */
528};
529
530static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
531 struct page *ipage, struct page *npage, nid_t nid)
532{
d66d1f76 533 memset(dn, 0, sizeof(*dn));
39a53e0c
JK
534 dn->inode = inode;
535 dn->inode_page = ipage;
536 dn->node_page = npage;
537 dn->nid = nid;
39a53e0c
JK
538}
539
540/*
541 * For SIT manager
542 *
543 * By default, there are 6 active log areas across the whole main area.
544 * When considering hot and cold data separation to reduce cleaning overhead,
545 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
546 * respectively.
547 * In the current design, you should not change the numbers intentionally.
548 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
549 * logs individually according to the underlying devices. (default: 6)
550 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
551 * data and 8 for node logs.
552 */
553#define NR_CURSEG_DATA_TYPE (3)
554#define NR_CURSEG_NODE_TYPE (3)
555#define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
556
557enum {
558 CURSEG_HOT_DATA = 0, /* directory entry blocks */
559 CURSEG_WARM_DATA, /* data blocks */
560 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
561 CURSEG_HOT_NODE, /* direct node blocks of directory files */
562 CURSEG_WARM_NODE, /* direct node blocks of normal files */
563 CURSEG_COLD_NODE, /* indirect node blocks */
38aa0889
JK
564 NO_CHECK_TYPE,
565 CURSEG_DIRECT_IO, /* to use for the direct IO path */
39a53e0c
JK
566};
567
6b4afdd7 568struct flush_cmd {
6b4afdd7 569 struct completion wait;
721bd4d5 570 struct llist_node llnode;
6b4afdd7
JK
571 int ret;
572};
573
a688b9d9
GZ
574struct flush_cmd_control {
575 struct task_struct *f2fs_issue_flush; /* flush thread */
576 wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
721bd4d5
GZ
577 struct llist_head issue_list; /* list for command issue */
578 struct llist_node *dispatch_list; /* list for command dispatch */
a688b9d9
GZ
579};
580
39a53e0c
JK
581struct f2fs_sm_info {
582 struct sit_info *sit_info; /* whole segment information */
583 struct free_segmap_info *free_info; /* free segment information */
584 struct dirty_seglist_info *dirty_info; /* dirty segment information */
585 struct curseg_info *curseg_array; /* active segment information */
586
39a53e0c
JK
587 block_t seg0_blkaddr; /* block address of 0'th segment */
588 block_t main_blkaddr; /* start block address of main area */
589 block_t ssa_blkaddr; /* start block address of SSA area */
590
591 unsigned int segment_count; /* total # of segments */
592 unsigned int main_segments; /* # of segments in main area */
593 unsigned int reserved_segments; /* # of reserved segments */
594 unsigned int ovp_segments; /* # of overprovision segments */
81eb8d6e
JK
595
596 /* a threshold to reclaim prefree segments */
597 unsigned int rec_prefree_segments;
7fd9e544
JK
598
599 /* for small discard management */
600 struct list_head discard_list; /* 4KB discard list */
601 int nr_discards; /* # of discards in the list */
602 int max_discards; /* max. discards to be issued */
216fbd64 603
bba681cb
JK
604 /* for batched trimming */
605 unsigned int trim_sections; /* # of sections to trim */
606
184a5cd2
CY
607 struct list_head sit_entry_set; /* sit entry set list */
608
216fbd64
JK
609 unsigned int ipu_policy; /* in-place-update policy */
610 unsigned int min_ipu_util; /* in-place-update threshold */
c1ce1b02 611 unsigned int min_fsync_blocks; /* threshold for fsync */
6b4afdd7
JK
612
613 /* for flush command control */
a688b9d9
GZ
614 struct flush_cmd_control *cmd_control_info;
615
39a53e0c
JK
616};
617
39a53e0c
JK
618/*
619 * For superblock
620 */
621/*
622 * COUNT_TYPE for monitoring
623 *
624 * f2fs monitors the number of several block types such as on-writeback,
625 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
626 */
627enum count_type {
628 F2FS_WRITEBACK,
629 F2FS_DIRTY_DENTS,
c227f912 630 F2FS_DIRTY_DATA,
39a53e0c
JK
631 F2FS_DIRTY_NODES,
632 F2FS_DIRTY_META,
8dcf2ff7 633 F2FS_INMEM_PAGES,
39a53e0c
JK
634 NR_COUNT_TYPE,
635};
636
39a53e0c 637/*
e1c42045 638 * The below are the page types of bios used in submit_bio().
39a53e0c
JK
639 * The available types are:
640 * DATA User data pages. It operates as async mode.
641 * NODE Node pages. It operates as async mode.
642 * META FS metadata pages such as SIT, NAT, CP.
643 * NR_PAGE_TYPE The number of page types.
644 * META_FLUSH Make sure the previous pages are written
645 * with waiting the bio's completion
646 * ... Only can be used with META.
647 */
7d5e5109 648#define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
39a53e0c
JK
649enum page_type {
650 DATA,
651 NODE,
652 META,
653 NR_PAGE_TYPE,
654 META_FLUSH,
8ce67cb0
JK
655 INMEM, /* the below types are used by tracepoints only. */
656 INMEM_DROP,
28bc106b 657 INMEM_REVOKE,
8ce67cb0
JK
658 IPU,
659 OPU,
39a53e0c
JK
660};
661
458e6197 662struct f2fs_io_info {
05ca3632 663 struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
7e8f2308
GZ
664 enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
665 int rw; /* contains R/RS/W/WS with REQ_META/REQ_PRIO */
7a9d7548 666 block_t new_blkaddr; /* new block address to be written */
28bc106b 667 block_t old_blkaddr; /* old block address before Cow */
05ca3632 668 struct page *page; /* page to be written */
4375a336 669 struct page *encrypted_page; /* encrypted page */
458e6197
JK
670};
671
93dfe2ac 672#define is_read_io(rw) (((rw) & 1) == READ)
1ff7bd3b 673struct f2fs_bio_info {
458e6197 674 struct f2fs_sb_info *sbi; /* f2fs superblock */
1ff7bd3b
JK
675 struct bio *bio; /* bios to merge */
676 sector_t last_block_in_bio; /* last block number */
458e6197 677 struct f2fs_io_info fio; /* store buffered io info. */
df0f8dc0 678 struct rw_semaphore io_rwsem; /* blocking op for bio */
1ff7bd3b
JK
679};
680
c227f912
CY
681enum inode_type {
682 DIR_INODE, /* for dirty dir inode */
683 FILE_INODE, /* for dirty regular/symlink inode */
684 NR_INODE_TYPE,
685};
686
67298804
CY
687/* for inner inode cache management */
688struct inode_management {
689 struct radix_tree_root ino_root; /* ino entry array */
690 spinlock_t ino_lock; /* for ino entry lock */
691 struct list_head ino_list; /* inode list head */
692 unsigned long ino_num; /* number of entries */
693};
694
caf0047e
CY
695/* For s_flag in struct f2fs_sb_info */
696enum {
697 SBI_IS_DIRTY, /* dirty flag for checkpoint */
698 SBI_IS_CLOSE, /* specify unmounting */
699 SBI_NEED_FSCK, /* need fsck.f2fs to fix */
700 SBI_POR_DOING, /* recovery is doing or not */
df728b0f 701 SBI_NEED_SB_WRITE, /* need to recover superblock */
caf0047e
CY
702};
703
6beceb54
JK
704enum {
705 CP_TIME,
d0239e1b 706 REQ_TIME,
6beceb54
JK
707 MAX_TIME,
708};
709
39a53e0c
JK
710struct f2fs_sb_info {
711 struct super_block *sb; /* pointer to VFS super block */
5e176d54 712 struct proc_dir_entry *s_proc; /* proc entry */
39a53e0c 713 struct f2fs_super_block *raw_super; /* raw super block pointer */
e8240f65 714 int valid_super_block; /* valid super block no */
caf0047e 715 int s_flag; /* flags for sbi */
39a53e0c
JK
716
717 /* for node-related operations */
718 struct f2fs_nm_info *nm_info; /* node manager */
719 struct inode *node_inode; /* cache node blocks */
720
721 /* for segment-related operations */
722 struct f2fs_sm_info *sm_info; /* segment manager */
1ff7bd3b
JK
723
724 /* for bio operations */
924b720b 725 struct f2fs_bio_info read_io; /* for read bios */
1ff7bd3b 726 struct f2fs_bio_info write_io[NR_PAGE_TYPE]; /* for write bios */
39a53e0c
JK
727
728 /* for checkpoint */
729 struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
730 struct inode *meta_inode; /* cache meta blocks */
39936837 731 struct mutex cp_mutex; /* checkpoint procedure lock */
e479556b 732 struct rw_semaphore cp_rwsem; /* blocking FS operations */
b3582c68 733 struct rw_semaphore node_write; /* locking node writes */
5463e7c1 734 struct mutex writepages; /* mutex for writepages() */
fb51b5ef 735 wait_queue_head_t cp_wait;
6beceb54
JK
736 unsigned long last_time[MAX_TIME]; /* to store time in jiffies */
737 long interval_time[MAX_TIME]; /* to store thresholds */
39a53e0c 738
67298804 739 struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
6451e041
JK
740
741 /* for orphan inode, use 0'th array */
0d47c1ad 742 unsigned int max_orphans; /* max orphan inodes */
39a53e0c 743
c227f912
CY
744 /* for inode management */
745 struct list_head inode_list[NR_INODE_TYPE]; /* dirty inode list */
746 spinlock_t inode_lock[NR_INODE_TYPE]; /* for dirty inode list lock */
39a53e0c 747
13054c54
CY
748 /* for extent tree cache */
749 struct radix_tree_root extent_tree_root;/* cache extent cache entries */
750 struct rw_semaphore extent_tree_lock; /* locking extent radix tree */
751 struct list_head extent_list; /* lru list for shrinker */
752 spinlock_t extent_lock; /* locking extent lru list */
7441ccef 753 atomic_t total_ext_tree; /* extent tree count */
137d09f0 754 struct list_head zombie_list; /* extent zombie tree list */
74fd8d99 755 atomic_t total_zombie_tree; /* extent zombie tree count */
13054c54
CY
756 atomic_t total_ext_node; /* extent info count */
757
e1c42045 758 /* basic filesystem units */
39a53e0c
JK
759 unsigned int log_sectors_per_block; /* log2 sectors per block */
760 unsigned int log_blocksize; /* log2 block size */
761 unsigned int blocksize; /* block size */
762 unsigned int root_ino_num; /* root inode number*/
763 unsigned int node_ino_num; /* node inode number*/
764 unsigned int meta_ino_num; /* meta inode number*/
765 unsigned int log_blocks_per_seg; /* log2 blocks per segment */
766 unsigned int blocks_per_seg; /* blocks per segment */
767 unsigned int segs_per_sec; /* segments per section */
768 unsigned int secs_per_zone; /* sections per zone */
769 unsigned int total_sections; /* total section count */
770 unsigned int total_node_count; /* total node block count */
771 unsigned int total_valid_node_count; /* valid node block count */
772 unsigned int total_valid_inode_count; /* valid inode count */
e0afc4d6 773 loff_t max_file_blocks; /* max block index of file */
39a53e0c 774 int active_logs; /* # of active logs */
ab9fa662 775 int dir_level; /* directory level */
39a53e0c
JK
776
777 block_t user_block_count; /* # of user blocks */
778 block_t total_valid_block_count; /* # of valid blocks */
779 block_t alloc_valid_block_count; /* # of allocated blocks */
a66cdd98 780 block_t discard_blks; /* discard command candidats */
39a53e0c
JK
781 block_t last_valid_block_count; /* for recovery */
782 u32 s_next_generation; /* for NFS support */
783 atomic_t nr_pages[NR_COUNT_TYPE]; /* # of pages, see count_type */
784
785 struct f2fs_mount_info mount_opt; /* mount options */
786
787 /* for cleaning operations */
788 struct mutex gc_mutex; /* mutex for GC */
789 struct f2fs_gc_kthread *gc_thread; /* GC thread */
5ec4e49f 790 unsigned int cur_victim_sec; /* current victim section num */
39a53e0c 791
b1c57c1c
JK
792 /* maximum # of trials to find a victim segment for SSR and GC */
793 unsigned int max_victim_search;
794
39a53e0c
JK
795 /*
796 * for stat information.
797 * one is for the LFS mode, and the other is for the SSR mode.
798 */
35b09d82 799#ifdef CONFIG_F2FS_STAT_FS
39a53e0c
JK
800 struct f2fs_stat_info *stat_info; /* FS status information */
801 unsigned int segment_count[2]; /* # of allocated segments */
802 unsigned int block_count[2]; /* # of allocated blocks */
b9a2c252 803 atomic_t inplace_count; /* # of inplace update */
5b7ee374
CY
804 atomic64_t total_hit_ext; /* # of lookup extent cache */
805 atomic64_t read_hit_rbtree; /* # of hit rbtree extent node */
806 atomic64_t read_hit_largest; /* # of hit largest extent node */
807 atomic64_t read_hit_cached; /* # of hit cached extent node */
d5e8f6c9 808 atomic_t inline_xattr; /* # of inline_xattr inodes */
03e14d52
CY
809 atomic_t inline_inode; /* # of inline_data inodes */
810 atomic_t inline_dir; /* # of inline_dentry inodes */
39a53e0c 811 int bg_gc; /* background gc calls */
33fbd510 812 unsigned int ndirty_inode[NR_INODE_TYPE]; /* # of dirty inodes */
35b09d82
NJ
813#endif
814 unsigned int last_victim[2]; /* last victim segment # */
39a53e0c 815 spinlock_t stat_lock; /* lock for stat operations */
b59d0bae
NJ
816
817 /* For sysfs suppport */
818 struct kobject s_kobj;
819 struct completion s_kobj_unregister;
2658e50d
JK
820
821 /* For shrinker support */
822 struct list_head s_list;
823 struct mutex umount_mutex;
824 unsigned int shrinker_run_no;
8f1dbbbb
SL
825
826 /* For write statistics */
827 u64 sectors_written_start;
828 u64 kbytes_written;
43b6573b
KM
829
830 /* Reference to checksum algorithm driver via cryptoapi */
831 struct crypto_shash *s_chksum_driver;
39a53e0c
JK
832};
833
8f1dbbbb
SL
834/* For write statistics. Suppose sector size is 512 bytes,
835 * and the return value is in kbytes. s is of struct f2fs_sb_info.
836 */
837#define BD_PART_WRITTEN(s) \
838(((u64)part_stat_read(s->sb->s_bdev->bd_part, sectors[1]) - \
839 s->sectors_written_start) >> 1)
840
6beceb54
JK
841static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
842{
843 sbi->last_time[type] = jiffies;
844}
845
846static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
847{
848 struct timespec ts = {sbi->interval_time[type], 0};
849 unsigned long interval = timespec_to_jiffies(&ts);
850
851 return time_after(jiffies, sbi->last_time[type] + interval);
852}
853
d0239e1b
JK
854static inline bool is_idle(struct f2fs_sb_info *sbi)
855{
856 struct block_device *bdev = sbi->sb->s_bdev;
857 struct request_queue *q = bdev_get_queue(bdev);
858 struct request_list *rl = &q->root_rl;
859
860 if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
861 return 0;
862
863 return f2fs_time_over(sbi, REQ_TIME);
864}
865
39a53e0c
JK
866/*
867 * Inline functions
868 */
43b6573b
KM
869static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
870 unsigned int length)
871{
872 SHASH_DESC_ON_STACK(shash, sbi->s_chksum_driver);
873 u32 *ctx = (u32 *)shash_desc_ctx(shash);
874 int err;
875
876 shash->tfm = sbi->s_chksum_driver;
877 shash->flags = 0;
878 *ctx = F2FS_SUPER_MAGIC;
879
880 err = crypto_shash_update(shash, address, length);
881 BUG_ON(err);
882
883 return *ctx;
884}
885
886static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
887 void *buf, size_t buf_size)
888{
889 return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
890}
891
39a53e0c
JK
892static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
893{
894 return container_of(inode, struct f2fs_inode_info, vfs_inode);
895}
896
897static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
898{
899 return sb->s_fs_info;
900}
901
4081363f
JK
902static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
903{
904 return F2FS_SB(inode->i_sb);
905}
906
907static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
908{
909 return F2FS_I_SB(mapping->host);
910}
911
912static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
913{
914 return F2FS_M_SB(page->mapping);
915}
916
39a53e0c
JK
917static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
918{
919 return (struct f2fs_super_block *)(sbi->raw_super);
920}
921
922static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
923{
924 return (struct f2fs_checkpoint *)(sbi->ckpt);
925}
926
45590710
GZ
927static inline struct f2fs_node *F2FS_NODE(struct page *page)
928{
929 return (struct f2fs_node *)page_address(page);
930}
931
58bfaf44
JK
932static inline struct f2fs_inode *F2FS_INODE(struct page *page)
933{
934 return &((struct f2fs_node *)page_address(page))->i;
935}
936
39a53e0c
JK
937static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
938{
939 return (struct f2fs_nm_info *)(sbi->nm_info);
940}
941
942static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
943{
944 return (struct f2fs_sm_info *)(sbi->sm_info);
945}
946
947static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
948{
949 return (struct sit_info *)(SM_I(sbi)->sit_info);
950}
951
952static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
953{
954 return (struct free_segmap_info *)(SM_I(sbi)->free_info);
955}
956
957static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
958{
959 return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
960}
961
9df27d98
GZ
962static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
963{
964 return sbi->meta_inode->i_mapping;
965}
966
4ef51a8f
JK
967static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
968{
969 return sbi->node_inode->i_mapping;
970}
971
caf0047e
CY
972static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
973{
974 return sbi->s_flag & (0x01 << type);
975}
976
977static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
39a53e0c 978{
caf0047e 979 sbi->s_flag |= (0x01 << type);
39a53e0c
JK
980}
981
caf0047e 982static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
39a53e0c 983{
caf0047e 984 sbi->s_flag &= ~(0x01 << type);
39a53e0c
JK
985}
986
d71b5564
JK
987static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
988{
989 return le64_to_cpu(cp->checkpoint_ver);
990}
991
25ca923b
JK
992static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
993{
994 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
995 return ckpt_flags & f;
996}
997
998static inline void set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
999{
1000 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1001 ckpt_flags |= f;
1002 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1003}
1004
1005static inline void clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1006{
1007 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1008 ckpt_flags &= (~f);
1009 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1010}
1011
e479556b 1012static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
39936837 1013{
e479556b 1014 down_read(&sbi->cp_rwsem);
39936837
JK
1015}
1016
e479556b 1017static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
39a53e0c 1018{
e479556b 1019 up_read(&sbi->cp_rwsem);
39a53e0c
JK
1020}
1021
e479556b 1022static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
39a53e0c 1023{
59692b7c 1024 down_write(&sbi->cp_rwsem);
39936837
JK
1025}
1026
e479556b 1027static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
39936837 1028{
e479556b 1029 up_write(&sbi->cp_rwsem);
39a53e0c
JK
1030}
1031
119ee914
JK
1032static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1033{
1034 int reason = CP_SYNC;
1035
1036 if (test_opt(sbi, FASTBOOT))
1037 reason = CP_FASTBOOT;
1038 if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1039 reason = CP_UMOUNT;
1040 return reason;
1041}
1042
1043static inline bool __remain_node_summaries(int reason)
1044{
1045 return (reason == CP_UMOUNT || reason == CP_FASTBOOT);
1046}
1047
1048static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1049{
1050 return (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG) ||
1051 is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FASTBOOT_FLAG));
1052}
1053
39a53e0c
JK
1054/*
1055 * Check whether the given nid is within node id range.
1056 */
064e0823 1057static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
39a53e0c 1058{
d6b7d4b3
CY
1059 if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1060 return -EINVAL;
cfb271d4 1061 if (unlikely(nid >= NM_I(sbi)->max_nid))
064e0823
NJ
1062 return -EINVAL;
1063 return 0;
39a53e0c
JK
1064}
1065
1066#define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
1067
1068/*
1069 * Check whether the inode has blocks or not
1070 */
1071static inline int F2FS_HAS_BLOCKS(struct inode *inode)
1072{
1073 if (F2FS_I(inode)->i_xattr_nid)
6c311ec6 1074 return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1;
39a53e0c 1075 else
6c311ec6 1076 return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS;
39a53e0c
JK
1077}
1078
4bc8e9bc
CY
1079static inline bool f2fs_has_xattr_block(unsigned int ofs)
1080{
1081 return ofs == XATTR_NODE_OFFSET;
1082}
1083
39a53e0c
JK
1084static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
1085 struct inode *inode, blkcnt_t count)
1086{
1087 block_t valid_block_count;
1088
1089 spin_lock(&sbi->stat_lock);
1090 valid_block_count =
1091 sbi->total_valid_block_count + (block_t)count;
cfb271d4 1092 if (unlikely(valid_block_count > sbi->user_block_count)) {
39a53e0c
JK
1093 spin_unlock(&sbi->stat_lock);
1094 return false;
1095 }
1096 inode->i_blocks += count;
1097 sbi->total_valid_block_count = valid_block_count;
1098 sbi->alloc_valid_block_count += (block_t)count;
1099 spin_unlock(&sbi->stat_lock);
1100 return true;
1101}
1102
da19b0dc 1103static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
39a53e0c
JK
1104 struct inode *inode,
1105 blkcnt_t count)
1106{
1107 spin_lock(&sbi->stat_lock);
9850cf4a
JK
1108 f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
1109 f2fs_bug_on(sbi, inode->i_blocks < count);
39a53e0c
JK
1110 inode->i_blocks -= count;
1111 sbi->total_valid_block_count -= (block_t)count;
1112 spin_unlock(&sbi->stat_lock);
39a53e0c
JK
1113}
1114
1115static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
1116{
1117 atomic_inc(&sbi->nr_pages[count_type]);
caf0047e 1118 set_sbi_flag(sbi, SBI_IS_DIRTY);
39a53e0c
JK
1119}
1120
a7ffdbe2 1121static inline void inode_inc_dirty_pages(struct inode *inode)
39a53e0c 1122{
a7ffdbe2 1123 atomic_inc(&F2FS_I(inode)->dirty_pages);
c227f912
CY
1124 inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1125 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
39a53e0c
JK
1126}
1127
1128static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
1129{
1130 atomic_dec(&sbi->nr_pages[count_type]);
1131}
1132
a7ffdbe2 1133static inline void inode_dec_dirty_pages(struct inode *inode)
39a53e0c 1134{
5ac9f36f
CY
1135 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1136 !S_ISLNK(inode->i_mode))
1fe54f9d
JK
1137 return;
1138
a7ffdbe2 1139 atomic_dec(&F2FS_I(inode)->dirty_pages);
c227f912
CY
1140 dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1141 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
39a53e0c
JK
1142}
1143
1144static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
1145{
1146 return atomic_read(&sbi->nr_pages[count_type]);
1147}
1148
a7ffdbe2 1149static inline int get_dirty_pages(struct inode *inode)
f8b2c1f9 1150{
a7ffdbe2 1151 return atomic_read(&F2FS_I(inode)->dirty_pages);
f8b2c1f9
JK
1152}
1153
5ac206cf
NJ
1154static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
1155{
3519e3f9 1156 unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
5ac206cf
NJ
1157 return ((get_pages(sbi, block_type) + pages_per_sec - 1)
1158 >> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
1159}
1160
39a53e0c
JK
1161static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
1162{
8b8343fa 1163 return sbi->total_valid_block_count;
39a53e0c
JK
1164}
1165
1166static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
1167{
1168 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1169
1170 /* return NAT or SIT bitmap */
1171 if (flag == NAT_BITMAP)
1172 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
1173 else if (flag == SIT_BITMAP)
1174 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
1175
1176 return 0;
1177}
1178
55141486
WL
1179static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
1180{
1181 return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
1182}
1183
39a53e0c
JK
1184static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
1185{
1186 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1dbe4152
CL
1187 int offset;
1188
55141486 1189 if (__cp_payload(sbi) > 0) {
1dbe4152
CL
1190 if (flag == NAT_BITMAP)
1191 return &ckpt->sit_nat_version_bitmap;
1192 else
65b85ccc 1193 return (unsigned char *)ckpt + F2FS_BLKSIZE;
1dbe4152
CL
1194 } else {
1195 offset = (flag == NAT_BITMAP) ?
25ca923b 1196 le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
1dbe4152
CL
1197 return &ckpt->sit_nat_version_bitmap + offset;
1198 }
39a53e0c
JK
1199}
1200
1201static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
1202{
1203 block_t start_addr;
1204 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
d71b5564 1205 unsigned long long ckpt_version = cur_cp_version(ckpt);
39a53e0c 1206
25ca923b 1207 start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
39a53e0c
JK
1208
1209 /*
1210 * odd numbered checkpoint should at cp segment 0
e1c42045 1211 * and even segment must be at cp segment 1
39a53e0c
JK
1212 */
1213 if (!(ckpt_version & 1))
1214 start_addr += sbi->blocks_per_seg;
1215
1216 return start_addr;
1217}
1218
1219static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
1220{
1221 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
1222}
1223
1224static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
ef86d709 1225 struct inode *inode)
39a53e0c
JK
1226{
1227 block_t valid_block_count;
1228 unsigned int valid_node_count;
1229
1230 spin_lock(&sbi->stat_lock);
1231
ef86d709 1232 valid_block_count = sbi->total_valid_block_count + 1;
cfb271d4 1233 if (unlikely(valid_block_count > sbi->user_block_count)) {
39a53e0c
JK
1234 spin_unlock(&sbi->stat_lock);
1235 return false;
1236 }
1237
ef86d709 1238 valid_node_count = sbi->total_valid_node_count + 1;
cfb271d4 1239 if (unlikely(valid_node_count > sbi->total_node_count)) {
39a53e0c
JK
1240 spin_unlock(&sbi->stat_lock);
1241 return false;
1242 }
1243
1244 if (inode)
ef86d709
GZ
1245 inode->i_blocks++;
1246
1247 sbi->alloc_valid_block_count++;
1248 sbi->total_valid_node_count++;
1249 sbi->total_valid_block_count++;
39a53e0c
JK
1250 spin_unlock(&sbi->stat_lock);
1251
1252 return true;
1253}
1254
1255static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
ef86d709 1256 struct inode *inode)
39a53e0c
JK
1257{
1258 spin_lock(&sbi->stat_lock);
1259
9850cf4a
JK
1260 f2fs_bug_on(sbi, !sbi->total_valid_block_count);
1261 f2fs_bug_on(sbi, !sbi->total_valid_node_count);
1262 f2fs_bug_on(sbi, !inode->i_blocks);
39a53e0c 1263
ef86d709
GZ
1264 inode->i_blocks--;
1265 sbi->total_valid_node_count--;
1266 sbi->total_valid_block_count--;
39a53e0c
JK
1267
1268 spin_unlock(&sbi->stat_lock);
1269}
1270
1271static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
1272{
8b8343fa 1273 return sbi->total_valid_node_count;
39a53e0c
JK
1274}
1275
1276static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
1277{
1278 spin_lock(&sbi->stat_lock);
9850cf4a 1279 f2fs_bug_on(sbi, sbi->total_valid_inode_count == sbi->total_node_count);
39a53e0c
JK
1280 sbi->total_valid_inode_count++;
1281 spin_unlock(&sbi->stat_lock);
1282}
1283
0e80220a 1284static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
39a53e0c
JK
1285{
1286 spin_lock(&sbi->stat_lock);
9850cf4a 1287 f2fs_bug_on(sbi, !sbi->total_valid_inode_count);
39a53e0c
JK
1288 sbi->total_valid_inode_count--;
1289 spin_unlock(&sbi->stat_lock);
39a53e0c
JK
1290}
1291
1292static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
1293{
8b8343fa 1294 return sbi->total_valid_inode_count;
39a53e0c
JK
1295}
1296
a56c7c6f
JK
1297static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1298 pgoff_t index, bool for_write)
1299{
c41f3cc3
JK
1300#ifdef CONFIG_F2FS_FAULT_INJECTION
1301 struct page *page = find_lock_page(mapping, index);
1302 if (page)
1303 return page;
1304
1305 if (time_to_inject(FAULT_PAGE_ALLOC))
1306 return NULL;
1307#endif
a56c7c6f
JK
1308 if (!for_write)
1309 return grab_cache_page(mapping, index);
1310 return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1311}
1312
6e2c64ad
JK
1313static inline void f2fs_copy_page(struct page *src, struct page *dst)
1314{
1315 char *src_kaddr = kmap(src);
1316 char *dst_kaddr = kmap(dst);
1317
1318 memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
1319 kunmap(dst);
1320 kunmap(src);
1321}
1322
39a53e0c
JK
1323static inline void f2fs_put_page(struct page *page, int unlock)
1324{
031fa8cc 1325 if (!page)
39a53e0c
JK
1326 return;
1327
1328 if (unlock) {
9850cf4a 1329 f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
39a53e0c
JK
1330 unlock_page(page);
1331 }
09cbfeaf 1332 put_page(page);
39a53e0c
JK
1333}
1334
1335static inline void f2fs_put_dnode(struct dnode_of_data *dn)
1336{
1337 if (dn->node_page)
1338 f2fs_put_page(dn->node_page, 1);
1339 if (dn->inode_page && dn->node_page != dn->inode_page)
1340 f2fs_put_page(dn->inode_page, 0);
1341 dn->node_page = NULL;
1342 dn->inode_page = NULL;
1343}
1344
1345static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
e8512d2e 1346 size_t size)
39a53e0c 1347{
e8512d2e 1348 return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
39a53e0c
JK
1349}
1350
7bd59381
GZ
1351static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
1352 gfp_t flags)
1353{
1354 void *entry;
7bd59381 1355
80c54505
JK
1356 entry = kmem_cache_alloc(cachep, flags);
1357 if (!entry)
1358 entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
7bd59381
GZ
1359 return entry;
1360}
1361
740432f8
JK
1362static inline struct bio *f2fs_bio_alloc(int npages)
1363{
1364 struct bio *bio;
1365
1366 /* No failure on bio allocation */
740432f8 1367 bio = bio_alloc(GFP_NOIO, npages);
80c54505
JK
1368 if (!bio)
1369 bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
740432f8
JK
1370 return bio;
1371}
1372
9be32d72
JK
1373static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
1374 unsigned long index, void *item)
1375{
1376 while (radix_tree_insert(root, index, item))
1377 cond_resched();
1378}
1379
39a53e0c
JK
1380#define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
1381
1382static inline bool IS_INODE(struct page *page)
1383{
45590710 1384 struct f2fs_node *p = F2FS_NODE(page);
39a53e0c
JK
1385 return RAW_IS_INODE(p);
1386}
1387
1388static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
1389{
1390 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
1391}
1392
1393static inline block_t datablock_addr(struct page *node_page,
1394 unsigned int offset)
1395{
1396 struct f2fs_node *raw_node;
1397 __le32 *addr_array;
45590710 1398 raw_node = F2FS_NODE(node_page);
39a53e0c
JK
1399 addr_array = blkaddr_in_node(raw_node);
1400 return le32_to_cpu(addr_array[offset]);
1401}
1402
1403static inline int f2fs_test_bit(unsigned int nr, char *addr)
1404{
1405 int mask;
1406
1407 addr += (nr >> 3);
1408 mask = 1 << (7 - (nr & 0x07));
1409 return mask & *addr;
1410}
1411
a66cdd98
JK
1412static inline void f2fs_set_bit(unsigned int nr, char *addr)
1413{
1414 int mask;
1415
1416 addr += (nr >> 3);
1417 mask = 1 << (7 - (nr & 0x07));
1418 *addr |= mask;
1419}
1420
1421static inline void f2fs_clear_bit(unsigned int nr, char *addr)
1422{
1423 int mask;
1424
1425 addr += (nr >> 3);
1426 mask = 1 << (7 - (nr & 0x07));
1427 *addr &= ~mask;
1428}
1429
52aca074 1430static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
39a53e0c
JK
1431{
1432 int mask;
1433 int ret;
1434
1435 addr += (nr >> 3);
1436 mask = 1 << (7 - (nr & 0x07));
1437 ret = mask & *addr;
1438 *addr |= mask;
1439 return ret;
1440}
1441
52aca074 1442static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
39a53e0c
JK
1443{
1444 int mask;
1445 int ret;
1446
1447 addr += (nr >> 3);
1448 mask = 1 << (7 - (nr & 0x07));
1449 ret = mask & *addr;
1450 *addr &= ~mask;
1451 return ret;
1452}
1453
c6ac4c0e
GZ
1454static inline void f2fs_change_bit(unsigned int nr, char *addr)
1455{
1456 int mask;
1457
1458 addr += (nr >> 3);
1459 mask = 1 << (7 - (nr & 0x07));
1460 *addr ^= mask;
1461}
1462
39a53e0c
JK
1463/* used for f2fs_inode_info->flags */
1464enum {
1465 FI_NEW_INODE, /* indicate newly allocated inode */
b3783873 1466 FI_DIRTY_INODE, /* indicate inode is dirty or not */
ed57c27f 1467 FI_DIRTY_DIR, /* indicate directory has dirty pages */
39a53e0c
JK
1468 FI_INC_LINK, /* need to increment i_nlink */
1469 FI_ACL_MODE, /* indicate acl mode */
1470 FI_NO_ALLOC, /* should not allocate any blocks */
c9b63bd0 1471 FI_FREE_NID, /* free allocated nide */
699489bb 1472 FI_UPDATE_DIR, /* should update inode block for consistency */
74d0b917 1473 FI_DELAY_IPUT, /* used for the recovery */
c11abd1a 1474 FI_NO_EXTENT, /* not to use the extent cache */
444c580f 1475 FI_INLINE_XATTR, /* used for inline xattr */
1001b347 1476 FI_INLINE_DATA, /* used for inline data*/
34d67deb 1477 FI_INLINE_DENTRY, /* used for inline dentry */
fff04f90
JK
1478 FI_APPEND_WRITE, /* inode has appended data */
1479 FI_UPDATE_WRITE, /* inode has in-place-update data */
88b88a66
JK
1480 FI_NEED_IPU, /* used for ipu per file */
1481 FI_ATOMIC_FILE, /* indicate atomic file */
02a1335f 1482 FI_VOLATILE_FILE, /* indicate volatile file */
3c6c2beb 1483 FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */
1e84371f 1484 FI_DROP_CACHE, /* drop dirty page cache */
b3d208f9 1485 FI_DATA_EXIST, /* indicate data exists */
510022a8 1486 FI_INLINE_DOTS, /* indicate inline dot dentries */
d323d005 1487 FI_DO_DEFRAG, /* indicate defragment is running */
c227f912 1488 FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */
39a53e0c
JK
1489};
1490
1491static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
1492{
61e0f2d0
JK
1493 if (!test_bit(flag, &fi->flags))
1494 set_bit(flag, &fi->flags);
39a53e0c
JK
1495}
1496
1497static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
1498{
1499 return test_bit(flag, &fi->flags);
1500}
1501
1502static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
1503{
61e0f2d0
JK
1504 if (test_bit(flag, &fi->flags))
1505 clear_bit(flag, &fi->flags);
39a53e0c
JK
1506}
1507
1508static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
1509{
1510 fi->i_acl_mode = mode;
1511 set_inode_flag(fi, FI_ACL_MODE);
1512}
1513
444c580f
JK
1514static inline void get_inline_info(struct f2fs_inode_info *fi,
1515 struct f2fs_inode *ri)
1516{
1517 if (ri->i_inline & F2FS_INLINE_XATTR)
1518 set_inode_flag(fi, FI_INLINE_XATTR);
1001b347
HL
1519 if (ri->i_inline & F2FS_INLINE_DATA)
1520 set_inode_flag(fi, FI_INLINE_DATA);
34d67deb
CY
1521 if (ri->i_inline & F2FS_INLINE_DENTRY)
1522 set_inode_flag(fi, FI_INLINE_DENTRY);
b3d208f9
JK
1523 if (ri->i_inline & F2FS_DATA_EXIST)
1524 set_inode_flag(fi, FI_DATA_EXIST);
510022a8
JK
1525 if (ri->i_inline & F2FS_INLINE_DOTS)
1526 set_inode_flag(fi, FI_INLINE_DOTS);
444c580f
JK
1527}
1528
1529static inline void set_raw_inline(struct f2fs_inode_info *fi,
1530 struct f2fs_inode *ri)
1531{
1532 ri->i_inline = 0;
1533
1534 if (is_inode_flag_set(fi, FI_INLINE_XATTR))
1535 ri->i_inline |= F2FS_INLINE_XATTR;
1001b347
HL
1536 if (is_inode_flag_set(fi, FI_INLINE_DATA))
1537 ri->i_inline |= F2FS_INLINE_DATA;
34d67deb
CY
1538 if (is_inode_flag_set(fi, FI_INLINE_DENTRY))
1539 ri->i_inline |= F2FS_INLINE_DENTRY;
b3d208f9
JK
1540 if (is_inode_flag_set(fi, FI_DATA_EXIST))
1541 ri->i_inline |= F2FS_DATA_EXIST;
510022a8
JK
1542 if (is_inode_flag_set(fi, FI_INLINE_DOTS))
1543 ri->i_inline |= F2FS_INLINE_DOTS;
444c580f
JK
1544}
1545
987c7c31
CY
1546static inline int f2fs_has_inline_xattr(struct inode *inode)
1547{
1548 return is_inode_flag_set(F2FS_I(inode), FI_INLINE_XATTR);
1549}
1550
81ca7350 1551static inline unsigned int addrs_per_inode(struct inode *inode)
de93653f 1552{
81ca7350 1553 if (f2fs_has_inline_xattr(inode))
de93653f
JK
1554 return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
1555 return DEF_ADDRS_PER_INODE;
1556}
1557
65985d93
JK
1558static inline void *inline_xattr_addr(struct page *page)
1559{
695fd1ed 1560 struct f2fs_inode *ri = F2FS_INODE(page);
65985d93
JK
1561 return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
1562 F2FS_INLINE_XATTR_ADDRS]);
1563}
1564
1565static inline int inline_xattr_size(struct inode *inode)
1566{
987c7c31 1567 if (f2fs_has_inline_xattr(inode))
65985d93
JK
1568 return F2FS_INLINE_XATTR_ADDRS << 2;
1569 else
1570 return 0;
1571}
1572
0dbdc2ae
JK
1573static inline int f2fs_has_inline_data(struct inode *inode)
1574{
1575 return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DATA);
1576}
1577
b3d208f9
JK
1578static inline void f2fs_clear_inline_inode(struct inode *inode)
1579{
1580 clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
1581 clear_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1582}
1583
1584static inline int f2fs_exist_data(struct inode *inode)
1585{
1586 return is_inode_flag_set(F2FS_I(inode), FI_DATA_EXIST);
1587}
1588
510022a8
JK
1589static inline int f2fs_has_inline_dots(struct inode *inode)
1590{
1591 return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DOTS);
1592}
1593
88b88a66
JK
1594static inline bool f2fs_is_atomic_file(struct inode *inode)
1595{
1596 return is_inode_flag_set(F2FS_I(inode), FI_ATOMIC_FILE);
1597}
1598
02a1335f
JK
1599static inline bool f2fs_is_volatile_file(struct inode *inode)
1600{
1601 return is_inode_flag_set(F2FS_I(inode), FI_VOLATILE_FILE);
1602}
1603
3c6c2beb
JK
1604static inline bool f2fs_is_first_block_written(struct inode *inode)
1605{
1606 return is_inode_flag_set(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
1607}
1608
1e84371f
JK
1609static inline bool f2fs_is_drop_cache(struct inode *inode)
1610{
1611 return is_inode_flag_set(F2FS_I(inode), FI_DROP_CACHE);
1612}
1613
1001b347
HL
1614static inline void *inline_data_addr(struct page *page)
1615{
695fd1ed 1616 struct f2fs_inode *ri = F2FS_INODE(page);
1001b347
HL
1617 return (void *)&(ri->i_addr[1]);
1618}
1619
34d67deb
CY
1620static inline int f2fs_has_inline_dentry(struct inode *inode)
1621{
1622 return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DENTRY);
1623}
1624
9486ba44
JK
1625static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
1626{
1627 if (!f2fs_has_inline_dentry(dir))
1628 kunmap(page);
1629}
1630
b5492af7
JK
1631static inline int is_file(struct inode *inode, int type)
1632{
1633 return F2FS_I(inode)->i_advise & type;
1634}
1635
1636static inline void set_file(struct inode *inode, int type)
1637{
1638 F2FS_I(inode)->i_advise |= type;
1639}
1640
1641static inline void clear_file(struct inode *inode, int type)
1642{
1643 F2FS_I(inode)->i_advise &= ~type;
1644}
1645
77888c1e
JK
1646static inline int f2fs_readonly(struct super_block *sb)
1647{
1648 return sb->s_flags & MS_RDONLY;
1649}
1650
1e968fdf
JK
1651static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
1652{
1653 return is_set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
1654}
1655
744602cf
JK
1656static inline void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi)
1657{
1658 set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
1659 sbi->sb->s_flags |= MS_RDONLY;
1660}
1661
eaa693f4
JK
1662static inline bool is_dot_dotdot(const struct qstr *str)
1663{
1664 if (str->len == 1 && str->name[0] == '.')
1665 return true;
1666
1667 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
1668 return true;
1669
1670 return false;
1671}
1672
3e72f721
JK
1673static inline bool f2fs_may_extent_tree(struct inode *inode)
1674{
3e72f721
JK
1675 if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
1676 is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
1677 return false;
1678
886f56f9 1679 return S_ISREG(inode->i_mode);
3e72f721
JK
1680}
1681
0414b004
JK
1682static inline void *f2fs_kmalloc(size_t size, gfp_t flags)
1683{
2c63fead
JK
1684#ifdef CONFIG_F2FS_FAULT_INJECTION
1685 if (time_to_inject(FAULT_KMALLOC))
1686 return NULL;
1687#endif
0414b004
JK
1688 return kmalloc(size, flags);
1689}
1690
39307a8e
JK
1691static inline void *f2fs_kvmalloc(size_t size, gfp_t flags)
1692{
1693 void *ret;
1694
1695 ret = kmalloc(size, flags | __GFP_NOWARN);
1696 if (!ret)
1697 ret = __vmalloc(size, flags, PAGE_KERNEL);
1698 return ret;
1699}
1700
1701static inline void *f2fs_kvzalloc(size_t size, gfp_t flags)
1702{
1703 void *ret;
1704
1705 ret = kzalloc(size, flags | __GFP_NOWARN);
1706 if (!ret)
1707 ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
1708 return ret;
1709}
1710
a6dda0e6
CH
1711#define get_inode_mode(i) \
1712 ((is_inode_flag_set(F2FS_I(i), FI_ACL_MODE)) ? \
1713 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
1714
267378d4 1715/* get offset of first page in next direct node */
81ca7350
CY
1716#define PGOFS_OF_NEXT_DNODE(pgofs, inode) \
1717 ((pgofs < ADDRS_PER_INODE(inode)) ? ADDRS_PER_INODE(inode) : \
1718 (pgofs - ADDRS_PER_INODE(inode) + ADDRS_PER_BLOCK) / \
1719 ADDRS_PER_BLOCK * ADDRS_PER_BLOCK + ADDRS_PER_INODE(inode))
267378d4 1720
39a53e0c
JK
1721/*
1722 * file.c
1723 */
1724int f2fs_sync_file(struct file *, loff_t, loff_t, int);
1725void truncate_data_blocks(struct dnode_of_data *);
764aa3e9 1726int truncate_blocks(struct inode *, u64, bool);
b0154891 1727int f2fs_truncate(struct inode *, bool);
2d4d9fb5 1728int f2fs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
39a53e0c
JK
1729int f2fs_setattr(struct dentry *, struct iattr *);
1730int truncate_hole(struct inode *, pgoff_t, pgoff_t);
b292dcab 1731int truncate_data_blocks_range(struct dnode_of_data *, int);
39a53e0c 1732long f2fs_ioctl(struct file *, unsigned int, unsigned long);
e9750824 1733long f2fs_compat_ioctl(struct file *, unsigned int, unsigned long);
39a53e0c
JK
1734
1735/*
1736 * inode.c
1737 */
1738void f2fs_set_inode_flags(struct inode *);
39a53e0c 1739struct inode *f2fs_iget(struct super_block *, unsigned long);
4660f9c0 1740int try_to_free_nats(struct f2fs_sb_info *, int);
12719ae1
JK
1741int update_inode(struct inode *, struct page *);
1742int update_inode_page(struct inode *);
39a53e0c
JK
1743int f2fs_write_inode(struct inode *, struct writeback_control *);
1744void f2fs_evict_inode(struct inode *);
44c16156 1745void handle_failed_inode(struct inode *);
39a53e0c
JK
1746
1747/*
1748 * namei.c
1749 */
1750struct dentry *f2fs_get_parent(struct dentry *child);
1751
1752/*
1753 * dir.c
1754 */
dbeacf02 1755extern unsigned char f2fs_filetype_table[F2FS_FT_MAX];
510022a8 1756void set_de_type(struct f2fs_dir_entry *, umode_t);
675f10bd 1757unsigned char get_de_type(struct f2fs_dir_entry *);
0b81d077 1758struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *,
6e22c691 1759 f2fs_hash_t, int *, struct f2fs_dentry_ptr *);
7b3cd7d6 1760bool f2fs_fill_dentries(struct dir_context *, struct f2fs_dentry_ptr *,
0b81d077 1761 unsigned int, struct fscrypt_str *);
062a3e7b
JK
1762void do_make_empty_dir(struct inode *, struct inode *,
1763 struct f2fs_dentry_ptr *);
dbeacf02 1764struct page *init_inode_metadata(struct inode *, struct inode *,
bce8d112 1765 const struct qstr *, struct page *);
dbeacf02 1766void update_parent_metadata(struct inode *, struct inode *, unsigned int);
a82afa20 1767int room_for_filename(const void *, int, int);
dbeacf02 1768void f2fs_drop_nlink(struct inode *, struct inode *, struct page *);
39a53e0c
JK
1769struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
1770 struct page **);
1771struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
1772ino_t f2fs_inode_by_name(struct inode *, struct qstr *);
1773void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
1774 struct page *, struct inode *);
e7d55452 1775int update_dent_inode(struct inode *, struct inode *, const struct qstr *);
510022a8 1776void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *,
3b4d732a 1777 const struct qstr *, f2fs_hash_t , unsigned int);
675f10bd
CY
1778int f2fs_add_regular_entry(struct inode *, const struct qstr *,
1779 struct inode *, nid_t, umode_t);
510022a8
JK
1780int __f2fs_add_link(struct inode *, const struct qstr *, struct inode *, nid_t,
1781 umode_t);
dbeacf02
CY
1782void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *,
1783 struct inode *);
b97a9b5d 1784int f2fs_do_tmpfile(struct inode *, struct inode *);
39a53e0c
JK
1785bool f2fs_empty_dir(struct inode *);
1786
b7f7a5e0
AV
1787static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
1788{
2b0143b5 1789 return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
510022a8 1790 inode, inode->i_ino, inode->i_mode);
b7f7a5e0
AV
1791}
1792
39a53e0c
JK
1793/*
1794 * super.c
1795 */
c5bda1c8 1796int f2fs_commit_super(struct f2fs_sb_info *, bool);
39a53e0c 1797int f2fs_sync_fs(struct super_block *, int);
a07ef784
NJ
1798extern __printf(3, 4)
1799void f2fs_msg(struct super_block *, const char *, const char *, ...);
984ec63c 1800int sanity_check_ckpt(struct f2fs_sb_info *sbi);
39a53e0c
JK
1801
1802/*
1803 * hash.c
1804 */
eee6160f 1805f2fs_hash_t f2fs_dentry_hash(const struct qstr *);
39a53e0c
JK
1806
1807/*
1808 * node.c
1809 */
1810struct dnode_of_data;
1811struct node_info;
1812
6fb03f3a 1813bool available_free_memory(struct f2fs_sb_info *, int);
2dcf51ab 1814int need_dentry_mark(struct f2fs_sb_info *, nid_t);
88bd02c9 1815bool is_checkpointed_node(struct f2fs_sb_info *, nid_t);
88bd02c9 1816bool need_inode_block_update(struct f2fs_sb_info *, nid_t);
39a53e0c 1817void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
3cf45747 1818pgoff_t get_next_page_offset(struct dnode_of_data *, pgoff_t);
39a53e0c
JK
1819int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
1820int truncate_inode_blocks(struct inode *, pgoff_t);
4f16fb0f 1821int truncate_xattr_node(struct inode *, struct page *);
cfe58f9d 1822int wait_on_node_pages_writeback(struct f2fs_sb_info *, nid_t);
13ec7297 1823int remove_inode_page(struct inode *);
a014e037 1824struct page *new_inode_page(struct inode *);
8ae8f162 1825struct page *new_node_page(struct dnode_of_data *, unsigned int, struct page *);
39a53e0c
JK
1826void ra_node_page(struct f2fs_sb_info *, nid_t);
1827struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
1828struct page *get_node_page_ra(struct page *, int);
1829void sync_inode_page(struct dnode_of_data *);
da011cc0 1830void move_node_page(struct page *, int);
608514de
JK
1831int fsync_node_pages(struct f2fs_sb_info *, nid_t, struct writeback_control *,
1832 bool);
52681375 1833int sync_node_pages(struct f2fs_sb_info *, struct writeback_control *);
39a53e0c
JK
1834bool alloc_nid(struct f2fs_sb_info *, nid_t *);
1835void alloc_nid_done(struct f2fs_sb_info *, nid_t);
1836void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
31696580 1837int try_to_free_nids(struct f2fs_sb_info *, int);
70cfed88 1838void recover_inline_xattr(struct inode *, struct page *);
1c35a90e 1839void recover_xattr_data(struct inode *, struct page *, block_t);
39a53e0c
JK
1840int recover_inode_page(struct f2fs_sb_info *, struct page *);
1841int restore_node_summary(struct f2fs_sb_info *, unsigned int,
1842 struct f2fs_summary_block *);
1843void flush_nat_entries(struct f2fs_sb_info *);
1844int build_node_manager(struct f2fs_sb_info *);
1845void destroy_node_manager(struct f2fs_sb_info *);
6e6093a8 1846int __init create_node_manager_caches(void);
39a53e0c
JK
1847void destroy_node_manager_caches(void);
1848
1849/*
1850 * segment.c
1851 */
88b88a66 1852void register_inmem_page(struct inode *, struct page *);
29b96b54
CY
1853void drop_inmem_pages(struct inode *);
1854int commit_inmem_pages(struct inode *);
2c4db1a6 1855void f2fs_balance_fs(struct f2fs_sb_info *, bool);
4660f9c0 1856void f2fs_balance_fs_bg(struct f2fs_sb_info *);
6b4afdd7 1857int f2fs_issue_flush(struct f2fs_sb_info *);
2163d198
GZ
1858int create_flush_cmd_control(struct f2fs_sb_info *);
1859void destroy_flush_cmd_control(struct f2fs_sb_info *);
39a53e0c 1860void invalidate_blocks(struct f2fs_sb_info *, block_t);
6e2c64ad 1861bool is_checkpointed_data(struct f2fs_sb_info *, block_t);
5e443818 1862void refresh_sit_entry(struct f2fs_sb_info *, block_t, block_t);
836b5a63 1863void clear_prefree_segments(struct f2fs_sb_info *, struct cp_control *);
4b2fecc8 1864void release_discard_addrs(struct f2fs_sb_info *);
e90c2d28 1865bool discard_next_dnode(struct f2fs_sb_info *, block_t);
3fa06d7b 1866int npages_for_summary_flush(struct f2fs_sb_info *, bool);
39a53e0c 1867void allocate_new_segments(struct f2fs_sb_info *);
4b2fecc8 1868int f2fs_trim_fs(struct f2fs_sb_info *, struct fstrim_range *);
39a53e0c 1869struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
381722d2 1870void update_meta_page(struct f2fs_sb_info *, void *, block_t);
577e3495 1871void write_meta_page(struct f2fs_sb_info *, struct page *);
05ca3632
JK
1872void write_node_page(unsigned int, struct f2fs_io_info *);
1873void write_data_page(struct dnode_of_data *, struct f2fs_io_info *);
1874void rewrite_data_page(struct f2fs_io_info *);
4356e48e
CY
1875void __f2fs_replace_block(struct f2fs_sb_info *, struct f2fs_summary *,
1876 block_t, block_t, bool, bool);
528e3459 1877void f2fs_replace_block(struct f2fs_sb_info *, struct dnode_of_data *,
28bc106b 1878 block_t, block_t, unsigned char, bool, bool);
bfad7c2d
JK
1879void allocate_data_block(struct f2fs_sb_info *, struct page *,
1880 block_t, block_t *, struct f2fs_summary *, int);
fec1d657 1881void f2fs_wait_on_page_writeback(struct page *, enum page_type, bool);
08b39fbd 1882void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *, block_t);
39a53e0c
JK
1883void write_data_summaries(struct f2fs_sb_info *, block_t);
1884void write_node_summaries(struct f2fs_sb_info *, block_t);
dfc08a12 1885int lookup_journal_in_cursum(struct f2fs_journal *, int, unsigned int, int);
4b2fecc8 1886void flush_sit_entries(struct f2fs_sb_info *, struct cp_control *);
39a53e0c 1887int build_segment_manager(struct f2fs_sb_info *);
39a53e0c 1888void destroy_segment_manager(struct f2fs_sb_info *);
7fd9e544
JK
1889int __init create_segment_manager_caches(void);
1890void destroy_segment_manager_caches(void);
39a53e0c
JK
1891
1892/*
1893 * checkpoint.c
1894 */
1895struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
1896struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
2b947003 1897struct page *get_tmp_page(struct f2fs_sb_info *, pgoff_t);
f0c9cada 1898bool is_valid_blkaddr(struct f2fs_sb_info *, block_t, int);
26879fb1 1899int ra_meta_pages(struct f2fs_sb_info *, block_t, int, int, bool);
635aee1f 1900void ra_meta_pages_cond(struct f2fs_sb_info *, pgoff_t);
39a53e0c 1901long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
a49324f1
CY
1902void add_ino_entry(struct f2fs_sb_info *, nid_t, int type);
1903void remove_ino_entry(struct f2fs_sb_info *, nid_t, int type);
1904void release_ino_entry(struct f2fs_sb_info *);
fff04f90 1905bool exist_written_data(struct f2fs_sb_info *, nid_t, int);
cbd56e7d
JK
1906int acquire_orphan_inode(struct f2fs_sb_info *);
1907void release_orphan_inode(struct f2fs_sb_info *);
39a53e0c
JK
1908void add_orphan_inode(struct f2fs_sb_info *, nid_t);
1909void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
8c14bfad 1910int recover_orphan_inodes(struct f2fs_sb_info *);
39a53e0c 1911int get_valid_checkpoint(struct f2fs_sb_info *);
a7ffdbe2 1912void update_dirty_page(struct inode *, struct page *);
5deb8267 1913void add_dirty_dir_inode(struct inode *);
c227f912 1914void remove_dirty_inode(struct inode *);
6d5a1495 1915int sync_dirty_inodes(struct f2fs_sb_info *, enum inode_type);
c34f42e2 1916int write_checkpoint(struct f2fs_sb_info *, struct cp_control *);
6451e041 1917void init_ino_entry_info(struct f2fs_sb_info *);
6e6093a8 1918int __init create_checkpoint_caches(void);
39a53e0c
JK
1919void destroy_checkpoint_caches(void);
1920
1921/*
1922 * data.c
1923 */
458e6197 1924void f2fs_submit_merged_bio(struct f2fs_sb_info *, enum page_type, int);
0c3a5797
CY
1925void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *, struct inode *,
1926 struct page *, nid_t, enum page_type, int);
406657dd 1927void f2fs_flush_merged_bios(struct f2fs_sb_info *);
05ca3632
JK
1928int f2fs_submit_page_bio(struct f2fs_io_info *);
1929void f2fs_submit_page_mbio(struct f2fs_io_info *);
216a620a 1930void set_data_blkaddr(struct dnode_of_data *);
f28b3434 1931void f2fs_update_data_blkaddr(struct dnode_of_data *, block_t);
39a53e0c 1932int reserve_new_block(struct dnode_of_data *);
759af1c9 1933int f2fs_get_block(struct dnode_of_data *, pgoff_t);
b439b103 1934ssize_t f2fs_preallocate_blocks(struct kiocb *, struct iov_iter *);
b600965c 1935int f2fs_reserve_block(struct dnode_of_data *, pgoff_t);
a56c7c6f 1936struct page *get_read_data_page(struct inode *, pgoff_t, int, bool);
43f3eae1 1937struct page *find_data_page(struct inode *, pgoff_t);
a56c7c6f 1938struct page *get_lock_data_page(struct inode *, pgoff_t, bool);
64aa7ed9 1939struct page *get_new_data_page(struct inode *, struct page *, pgoff_t, bool);
05ca3632 1940int do_write_data_page(struct f2fs_io_info *);
d323d005 1941int f2fs_map_blocks(struct inode *, struct f2fs_map_blocks *, int, int);
9ab70134 1942int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *, u64, u64);
487261f3
CY
1943void f2fs_invalidate_page(struct page *, unsigned int, unsigned int);
1944int f2fs_release_page(struct page *, gfp_t);
39a53e0c
JK
1945
1946/*
1947 * gc.c
1948 */
1949int start_gc_thread(struct f2fs_sb_info *);
1950void stop_gc_thread(struct f2fs_sb_info *);
81ca7350 1951block_t start_bidx_of_node(unsigned int, struct inode *);
d530d4d8 1952int f2fs_gc(struct f2fs_sb_info *, bool);
39a53e0c 1953void build_gc_manager(struct f2fs_sb_info *);
39a53e0c
JK
1954
1955/*
1956 * recovery.c
1957 */
6781eabb 1958int recover_fsync_data(struct f2fs_sb_info *, bool);
39a53e0c
JK
1959bool space_for_roll_forward(struct f2fs_sb_info *);
1960
1961/*
1962 * debug.c
1963 */
1964#ifdef CONFIG_F2FS_STAT_FS
1965struct f2fs_stat_info {
1966 struct list_head stat_list;
1967 struct f2fs_sb_info *sbi;
39a53e0c
JK
1968 int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
1969 int main_area_segs, main_area_sections, main_area_zones;
5b7ee374
CY
1970 unsigned long long hit_largest, hit_cached, hit_rbtree;
1971 unsigned long long hit_total, total_ext;
c00ba554 1972 int ext_tree, zombie_tree, ext_node;
33fbd510
CY
1973 int ndirty_node, ndirty_meta;
1974 int ndirty_dent, ndirty_dirs, ndirty_data, ndirty_files;
dd4e4b59 1975 int nats, dirty_nats, sits, dirty_sits, fnids;
39a53e0c 1976 int total_count, utilization;
d5e8f6c9
CY
1977 int bg_gc, inmem_pages, wb_pages;
1978 int inline_xattr, inline_inode, inline_dir;
39a53e0c
JK
1979 unsigned int valid_count, valid_node_count, valid_inode_count;
1980 unsigned int bimodal, avg_vblocks;
1981 int util_free, util_valid, util_invalid;
1982 int rsvd_segs, overp_segs;
1983 int dirty_count, node_pages, meta_pages;
42190d2a 1984 int prefree_count, call_count, cp_count, bg_cp_count;
39a53e0c 1985 int tot_segs, node_segs, data_segs, free_segs, free_secs;
e1235983 1986 int bg_node_segs, bg_data_segs;
39a53e0c 1987 int tot_blks, data_blks, node_blks;
e1235983 1988 int bg_data_blks, bg_node_blks;
39a53e0c
JK
1989 int curseg[NR_CURSEG_TYPE];
1990 int cursec[NR_CURSEG_TYPE];
1991 int curzone[NR_CURSEG_TYPE];
1992
1993 unsigned int segment_count[2];
1994 unsigned int block_count[2];
b9a2c252 1995 unsigned int inplace_count;
9edcdabf 1996 unsigned long long base_mem, cache_mem, page_mem;
39a53e0c
JK
1997};
1998
963d4f7d
GZ
1999static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
2000{
6c311ec6 2001 return (struct f2fs_stat_info *)sbi->stat_info;
963d4f7d
GZ
2002}
2003
942e0be6 2004#define stat_inc_cp_count(si) ((si)->cp_count++)
42190d2a 2005#define stat_inc_bg_cp_count(si) ((si)->bg_cp_count++)
dcdfff65
JK
2006#define stat_inc_call_count(si) ((si)->call_count++)
2007#define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
33fbd510
CY
2008#define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++)
2009#define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--)
5b7ee374
CY
2010#define stat_inc_total_hit(sbi) (atomic64_inc(&(sbi)->total_hit_ext))
2011#define stat_inc_rbtree_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_rbtree))
2012#define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest))
2013#define stat_inc_cached_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_cached))
d5e8f6c9
CY
2014#define stat_inc_inline_xattr(inode) \
2015 do { \
2016 if (f2fs_has_inline_xattr(inode)) \
2017 (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \
2018 } while (0)
2019#define stat_dec_inline_xattr(inode) \
2020 do { \
2021 if (f2fs_has_inline_xattr(inode)) \
2022 (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \
2023 } while (0)
0dbdc2ae
JK
2024#define stat_inc_inline_inode(inode) \
2025 do { \
2026 if (f2fs_has_inline_data(inode)) \
03e14d52 2027 (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
0dbdc2ae
JK
2028 } while (0)
2029#define stat_dec_inline_inode(inode) \
2030 do { \
2031 if (f2fs_has_inline_data(inode)) \
03e14d52 2032 (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
0dbdc2ae 2033 } while (0)
3289c061
JK
2034#define stat_inc_inline_dir(inode) \
2035 do { \
2036 if (f2fs_has_inline_dentry(inode)) \
03e14d52 2037 (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
3289c061
JK
2038 } while (0)
2039#define stat_dec_inline_dir(inode) \
2040 do { \
2041 if (f2fs_has_inline_dentry(inode)) \
03e14d52 2042 (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
3289c061 2043 } while (0)
dcdfff65
JK
2044#define stat_inc_seg_type(sbi, curseg) \
2045 ((sbi)->segment_count[(curseg)->alloc_type]++)
2046#define stat_inc_block_count(sbi, curseg) \
2047 ((sbi)->block_count[(curseg)->alloc_type]++)
b9a2c252
CL
2048#define stat_inc_inplace_blocks(sbi) \
2049 (atomic_inc(&(sbi)->inplace_count))
e1235983 2050#define stat_inc_seg_count(sbi, type, gc_type) \
39a53e0c 2051 do { \
963d4f7d 2052 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
39a53e0c 2053 (si)->tot_segs++; \
e1235983 2054 if (type == SUM_TYPE_DATA) { \
39a53e0c 2055 si->data_segs++; \
e1235983
CL
2056 si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
2057 } else { \
39a53e0c 2058 si->node_segs++; \
e1235983
CL
2059 si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
2060 } \
39a53e0c
JK
2061 } while (0)
2062
2063#define stat_inc_tot_blk_count(si, blks) \
2064 (si->tot_blks += (blks))
2065
e1235983 2066#define stat_inc_data_blk_count(sbi, blks, gc_type) \
39a53e0c 2067 do { \
963d4f7d 2068 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
39a53e0c
JK
2069 stat_inc_tot_blk_count(si, blks); \
2070 si->data_blks += (blks); \
e1235983 2071 si->bg_data_blks += (gc_type == BG_GC) ? (blks) : 0; \
39a53e0c
JK
2072 } while (0)
2073
e1235983 2074#define stat_inc_node_blk_count(sbi, blks, gc_type) \
39a53e0c 2075 do { \
963d4f7d 2076 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
39a53e0c
JK
2077 stat_inc_tot_blk_count(si, blks); \
2078 si->node_blks += (blks); \
e1235983 2079 si->bg_node_blks += (gc_type == BG_GC) ? (blks) : 0; \
39a53e0c
JK
2080 } while (0)
2081
2082int f2fs_build_stats(struct f2fs_sb_info *);
2083void f2fs_destroy_stats(struct f2fs_sb_info *);
787c7b8c 2084int __init f2fs_create_root_stats(void);
4589d25d 2085void f2fs_destroy_root_stats(void);
39a53e0c 2086#else
942e0be6 2087#define stat_inc_cp_count(si)
42190d2a 2088#define stat_inc_bg_cp_count(si)
39a53e0c 2089#define stat_inc_call_count(si)
dcdfff65 2090#define stat_inc_bggc_count(si)
33fbd510
CY
2091#define stat_inc_dirty_inode(sbi, type)
2092#define stat_dec_dirty_inode(sbi, type)
dcdfff65 2093#define stat_inc_total_hit(sb)
029e13cc 2094#define stat_inc_rbtree_node_hit(sb)
91c481ff
CY
2095#define stat_inc_largest_node_hit(sbi)
2096#define stat_inc_cached_node_hit(sbi)
d5e8f6c9
CY
2097#define stat_inc_inline_xattr(inode)
2098#define stat_dec_inline_xattr(inode)
0dbdc2ae
JK
2099#define stat_inc_inline_inode(inode)
2100#define stat_dec_inline_inode(inode)
3289c061
JK
2101#define stat_inc_inline_dir(inode)
2102#define stat_dec_inline_dir(inode)
dcdfff65
JK
2103#define stat_inc_seg_type(sbi, curseg)
2104#define stat_inc_block_count(sbi, curseg)
b9a2c252 2105#define stat_inc_inplace_blocks(sbi)
e1235983 2106#define stat_inc_seg_count(sbi, type, gc_type)
39a53e0c 2107#define stat_inc_tot_blk_count(si, blks)
e1235983
CL
2108#define stat_inc_data_blk_count(sbi, blks, gc_type)
2109#define stat_inc_node_blk_count(sbi, blks, gc_type)
39a53e0c
JK
2110
2111static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
2112static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
787c7b8c 2113static inline int __init f2fs_create_root_stats(void) { return 0; }
4589d25d 2114static inline void f2fs_destroy_root_stats(void) { }
39a53e0c
JK
2115#endif
2116
2117extern const struct file_operations f2fs_dir_operations;
2118extern const struct file_operations f2fs_file_operations;
2119extern const struct inode_operations f2fs_file_inode_operations;
2120extern const struct address_space_operations f2fs_dblock_aops;
2121extern const struct address_space_operations f2fs_node_aops;
2122extern const struct address_space_operations f2fs_meta_aops;
2123extern const struct inode_operations f2fs_dir_inode_operations;
2124extern const struct inode_operations f2fs_symlink_inode_operations;
cbaf042a 2125extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
39a53e0c 2126extern const struct inode_operations f2fs_special_inode_operations;
29e7043f 2127extern struct kmem_cache *inode_entry_slab;
1001b347 2128
e18c65b2
HL
2129/*
2130 * inline.c
2131 */
01b960e9
JK
2132bool f2fs_may_inline_data(struct inode *);
2133bool f2fs_may_inline_dentry(struct inode *);
b3d208f9 2134void read_inline_data(struct page *, struct page *);
0bfcfcca 2135bool truncate_inline_inode(struct page *, u64);
e18c65b2 2136int f2fs_read_inline_data(struct inode *, struct page *);
b3d208f9
JK
2137int f2fs_convert_inline_page(struct dnode_of_data *, struct page *);
2138int f2fs_convert_inline_inode(struct inode *);
2139int f2fs_write_inline_data(struct inode *, struct page *);
0342fd30 2140bool recover_inline_data(struct inode *, struct page *);
6e22c691 2141struct f2fs_dir_entry *find_in_inline_dir(struct inode *,
0b81d077 2142 struct fscrypt_name *, struct page **);
201a05be
CY
2143struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *, struct page **);
2144int make_empty_inline_dir(struct inode *inode, struct inode *, struct page *);
510022a8
JK
2145int f2fs_add_inline_entry(struct inode *, const struct qstr *, struct inode *,
2146 nid_t, umode_t);
201a05be
CY
2147void f2fs_delete_inline_entry(struct f2fs_dir_entry *, struct page *,
2148 struct inode *, struct inode *);
2149bool f2fs_empty_inline_dir(struct inode *);
d8c6822a 2150int f2fs_read_inline_dir(struct file *, struct dir_context *,
0b81d077 2151 struct fscrypt_str *);
67f8cf3c
JK
2152int f2fs_inline_data_fiemap(struct inode *,
2153 struct fiemap_extent_info *, __u64, __u64);
cde4de12 2154
2658e50d
JK
2155/*
2156 * shrinker.c
2157 */
2158unsigned long f2fs_shrink_count(struct shrinker *, struct shrink_control *);
2159unsigned long f2fs_shrink_scan(struct shrinker *, struct shrink_control *);
2160void f2fs_join_shrinker(struct f2fs_sb_info *);
2161void f2fs_leave_shrinker(struct f2fs_sb_info *);
2162
a28ef1f5
CY
2163/*
2164 * extent_cache.c
2165 */
2166unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *, int);
ed3d1256 2167bool f2fs_init_extent_tree(struct inode *, struct f2fs_extent *);
a28ef1f5
CY
2168unsigned int f2fs_destroy_extent_node(struct inode *);
2169void f2fs_destroy_extent_tree(struct inode *);
2170bool f2fs_lookup_extent_cache(struct inode *, pgoff_t, struct extent_info *);
2171void f2fs_update_extent_cache(struct dnode_of_data *);
19b2c30d
CY
2172void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
2173 pgoff_t, block_t, unsigned int);
a28ef1f5
CY
2174void init_extent_cache_info(struct f2fs_sb_info *);
2175int __init create_extent_cache(void);
2176void destroy_extent_cache(void);
2177
cde4de12
JK
2178/*
2179 * crypto support
2180 */
0b81d077 2181static inline bool f2fs_encrypted_inode(struct inode *inode)
cde4de12 2182{
cde4de12 2183 return file_is_encrypt(inode);
cde4de12
JK
2184}
2185
2186static inline void f2fs_set_encrypted_inode(struct inode *inode)
2187{
2188#ifdef CONFIG_F2FS_FS_ENCRYPTION
2189 file_set_encrypt(inode);
2190#endif
2191}
2192
2193static inline bool f2fs_bio_encrypted(struct bio *bio)
2194{
0b81d077 2195 return bio->bi_private != NULL;
cde4de12
JK
2196}
2197
2198static inline int f2fs_sb_has_crypto(struct super_block *sb)
2199{
cde4de12 2200 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
cde4de12 2201}
f424f664 2202
fcc85a4d
JK
2203static inline bool f2fs_may_encrypt(struct inode *inode)
2204{
2205#ifdef CONFIG_F2FS_FS_ENCRYPTION
886f56f9 2206 umode_t mode = inode->i_mode;
fcc85a4d
JK
2207
2208 return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
2209#else
2210 return 0;
2211#endif
2212}
2213
0b81d077
JK
2214#ifndef CONFIG_F2FS_FS_ENCRYPTION
2215#define fscrypt_set_d_op(i)
2216#define fscrypt_get_ctx fscrypt_notsupp_get_ctx
2217#define fscrypt_release_ctx fscrypt_notsupp_release_ctx
2218#define fscrypt_encrypt_page fscrypt_notsupp_encrypt_page
2219#define fscrypt_decrypt_page fscrypt_notsupp_decrypt_page
2220#define fscrypt_decrypt_bio_pages fscrypt_notsupp_decrypt_bio_pages
2221#define fscrypt_pullback_bio_page fscrypt_notsupp_pullback_bio_page
2222#define fscrypt_restore_control_page fscrypt_notsupp_restore_control_page
2223#define fscrypt_zeroout_range fscrypt_notsupp_zeroout_range
2224#define fscrypt_process_policy fscrypt_notsupp_process_policy
2225#define fscrypt_get_policy fscrypt_notsupp_get_policy
2226#define fscrypt_has_permitted_context fscrypt_notsupp_has_permitted_context
2227#define fscrypt_inherit_context fscrypt_notsupp_inherit_context
2228#define fscrypt_get_encryption_info fscrypt_notsupp_get_encryption_info
2229#define fscrypt_put_encryption_info fscrypt_notsupp_put_encryption_info
2230#define fscrypt_setup_filename fscrypt_notsupp_setup_filename
2231#define fscrypt_free_filename fscrypt_notsupp_free_filename
2232#define fscrypt_fname_encrypted_size fscrypt_notsupp_fname_encrypted_size
2233#define fscrypt_fname_alloc_buffer fscrypt_notsupp_fname_alloc_buffer
2234#define fscrypt_fname_free_buffer fscrypt_notsupp_fname_free_buffer
2235#define fscrypt_fname_disk_to_usr fscrypt_notsupp_fname_disk_to_usr
2236#define fscrypt_fname_usr_to_disk fscrypt_notsupp_fname_usr_to_disk
57e5055b 2237#endif
39a53e0c 2238#endif