OSDN Git Service

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