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