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