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