]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/f2fs/f2fs.h
c1c9670e642f75789ff5ef45d3c23c29f9579b55
[karo-tx-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
22 /*
23  * For mount options
24  */
25 #define F2FS_MOUNT_BG_GC                0x00000001
26 #define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
27 #define F2FS_MOUNT_DISCARD              0x00000004
28 #define F2FS_MOUNT_NOHEAP               0x00000008
29 #define F2FS_MOUNT_XATTR_USER           0x00000010
30 #define F2FS_MOUNT_POSIX_ACL            0x00000020
31 #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
32
33 #define clear_opt(sbi, option)  (sbi->mount_opt.opt &= ~F2FS_MOUNT_##option)
34 #define set_opt(sbi, option)    (sbi->mount_opt.opt |= F2FS_MOUNT_##option)
35 #define test_opt(sbi, option)   (sbi->mount_opt.opt & F2FS_MOUNT_##option)
36
37 #define ver_after(a, b) (typecheck(unsigned long long, a) &&            \
38                 typecheck(unsigned long long, b) &&                     \
39                 ((long long)((a) - (b)) > 0))
40
41 typedef u32 block_t;    /*
42                          * should not change u32, since it is the on-disk block
43                          * address format, __le32.
44                          */
45 typedef u32 nid_t;
46
47 struct f2fs_mount_info {
48         unsigned int    opt;
49 };
50
51 #define CRCPOLY_LE 0xedb88320
52
53 static inline __u32 f2fs_crc32(void *buf, size_t len)
54 {
55         unsigned char *p = (unsigned char *)buf;
56         __u32 crc = F2FS_SUPER_MAGIC;
57         int i;
58
59         while (len--) {
60                 crc ^= *p++;
61                 for (i = 0; i < 8; i++)
62                         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
63         }
64         return crc;
65 }
66
67 static inline bool f2fs_crc_valid(__u32 blk_crc, void *buf, size_t buf_size)
68 {
69         return f2fs_crc32(buf, buf_size) == blk_crc;
70 }
71
72 /*
73  * For checkpoint manager
74  */
75 enum {
76         NAT_BITMAP,
77         SIT_BITMAP
78 };
79
80 /* for the list of orphan inodes */
81 struct orphan_inode_entry {
82         struct list_head list;  /* list head */
83         nid_t ino;              /* inode number */
84 };
85
86 /* for the list of directory inodes */
87 struct dir_inode_entry {
88         struct list_head list;  /* list head */
89         struct inode *inode;    /* vfs inode pointer */
90 };
91
92 /* for the list of fsync inodes, used only during recovery */
93 struct fsync_inode_entry {
94         struct list_head list;  /* list head */
95         struct inode *inode;    /* vfs inode pointer */
96         block_t blkaddr;        /* block address locating the last inode */
97 };
98
99 #define nats_in_cursum(sum)             (le16_to_cpu(sum->n_nats))
100 #define sits_in_cursum(sum)             (le16_to_cpu(sum->n_sits))
101
102 #define nat_in_journal(sum, i)          (sum->nat_j.entries[i].ne)
103 #define nid_in_journal(sum, i)          (sum->nat_j.entries[i].nid)
104 #define sit_in_journal(sum, i)          (sum->sit_j.entries[i].se)
105 #define segno_in_journal(sum, i)        (sum->sit_j.entries[i].segno)
106
107 static inline int update_nats_in_cursum(struct f2fs_summary_block *rs, int i)
108 {
109         int before = nats_in_cursum(rs);
110         rs->n_nats = cpu_to_le16(before + i);
111         return before;
112 }
113
114 static inline int update_sits_in_cursum(struct f2fs_summary_block *rs, int i)
115 {
116         int before = sits_in_cursum(rs);
117         rs->n_sits = cpu_to_le16(before + i);
118         return before;
119 }
120
121 /*
122  * ioctl commands
123  */
124 #define F2FS_IOC_GETFLAGS               FS_IOC_GETFLAGS
125 #define F2FS_IOC_SETFLAGS               FS_IOC_SETFLAGS
126
127 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
128 /*
129  * ioctl commands in 32 bit emulation
130  */
131 #define F2FS_IOC32_GETFLAGS             FS_IOC32_GETFLAGS
132 #define F2FS_IOC32_SETFLAGS             FS_IOC32_SETFLAGS
133 #endif
134
135 /*
136  * For INODE and NODE manager
137  */
138 /*
139  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
140  * as its node offset to distinguish from index node blocks.
141  * But some bits are used to mark the node block.
142  */
143 #define XATTR_NODE_OFFSET       ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
144                                 >> OFFSET_BIT_SHIFT)
145 enum {
146         ALLOC_NODE,                     /* allocate a new node page if needed */
147         LOOKUP_NODE,                    /* look up a node without readahead */
148         LOOKUP_NODE_RA,                 /*
149                                          * look up a node with readahead called
150                                          * by get_datablock_ro.
151                                          */
152 };
153
154 #define F2FS_LINK_MAX           32000   /* maximum link count per file */
155
156 /* for in-memory extent cache entry */
157 struct extent_info {
158         rwlock_t ext_lock;      /* rwlock for consistency */
159         unsigned int fofs;      /* start offset in a file */
160         u32 blk_addr;           /* start block address of the extent */
161         unsigned int len;       /* length of the extent */
162 };
163
164 /*
165  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
166  */
167 #define FADVISE_COLD_BIT        0x01
168 #define FADVISE_LOST_PINO_BIT   0x02
169
170 struct f2fs_inode_info {
171         struct inode vfs_inode;         /* serve a vfs inode */
172         unsigned long i_flags;          /* keep an inode flags for ioctl */
173         unsigned char i_advise;         /* use to give file attribute hints */
174         unsigned int i_current_depth;   /* use only in directory structure */
175         unsigned int i_pino;            /* parent inode number */
176         umode_t i_acl_mode;             /* keep file acl mode temporarily */
177
178         /* Use below internally in f2fs*/
179         unsigned long flags;            /* use to pass per-file flags */
180         atomic_t dirty_dents;           /* # of dirty dentry pages */
181         f2fs_hash_t chash;              /* hash value of given file name */
182         unsigned int clevel;            /* maximum level of given file name */
183         nid_t i_xattr_nid;              /* node id that contains xattrs */
184         unsigned long long xattr_ver;   /* cp version of xattr modification */
185         struct extent_info ext;         /* in-memory extent cache entry */
186 };
187
188 static inline void get_extent_info(struct extent_info *ext,
189                                         struct f2fs_extent i_ext)
190 {
191         write_lock(&ext->ext_lock);
192         ext->fofs = le32_to_cpu(i_ext.fofs);
193         ext->blk_addr = le32_to_cpu(i_ext.blk_addr);
194         ext->len = le32_to_cpu(i_ext.len);
195         write_unlock(&ext->ext_lock);
196 }
197
198 static inline void set_raw_extent(struct extent_info *ext,
199                                         struct f2fs_extent *i_ext)
200 {
201         read_lock(&ext->ext_lock);
202         i_ext->fofs = cpu_to_le32(ext->fofs);
203         i_ext->blk_addr = cpu_to_le32(ext->blk_addr);
204         i_ext->len = cpu_to_le32(ext->len);
205         read_unlock(&ext->ext_lock);
206 }
207
208 struct f2fs_nm_info {
209         block_t nat_blkaddr;            /* base disk address of NAT */
210         nid_t max_nid;                  /* maximum possible node ids */
211         nid_t next_scan_nid;            /* the next nid to be scanned */
212
213         /* NAT cache management */
214         struct radix_tree_root nat_root;/* root of the nat entry cache */
215         rwlock_t nat_tree_lock;         /* protect nat_tree_lock */
216         unsigned int nat_cnt;           /* the # of cached nat entries */
217         struct list_head nat_entries;   /* cached nat entry list (clean) */
218         struct list_head dirty_nat_entries; /* cached nat entry list (dirty) */
219
220         /* free node ids management */
221         struct list_head free_nid_list; /* a list for free nids */
222         spinlock_t free_nid_list_lock;  /* protect free nid list */
223         unsigned int fcnt;              /* the number of free node id */
224         struct mutex build_lock;        /* lock for build free nids */
225
226         /* for checkpoint */
227         char *nat_bitmap;               /* NAT bitmap pointer */
228         int bitmap_size;                /* bitmap size */
229 };
230
231 /*
232  * this structure is used as one of function parameters.
233  * all the information are dedicated to a given direct node block determined
234  * by the data offset in a file.
235  */
236 struct dnode_of_data {
237         struct inode *inode;            /* vfs inode pointer */
238         struct page *inode_page;        /* its inode page, NULL is possible */
239         struct page *node_page;         /* cached direct node page */
240         nid_t nid;                      /* node id of the direct node block */
241         unsigned int ofs_in_node;       /* data offset in the node page */
242         bool inode_page_locked;         /* inode page is locked or not */
243         block_t data_blkaddr;           /* block address of the node block */
244 };
245
246 static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
247                 struct page *ipage, struct page *npage, nid_t nid)
248 {
249         memset(dn, 0, sizeof(*dn));
250         dn->inode = inode;
251         dn->inode_page = ipage;
252         dn->node_page = npage;
253         dn->nid = nid;
254 }
255
256 /*
257  * For SIT manager
258  *
259  * By default, there are 6 active log areas across the whole main area.
260  * When considering hot and cold data separation to reduce cleaning overhead,
261  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
262  * respectively.
263  * In the current design, you should not change the numbers intentionally.
264  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
265  * logs individually according to the underlying devices. (default: 6)
266  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
267  * data and 8 for node logs.
268  */
269 #define NR_CURSEG_DATA_TYPE     (3)
270 #define NR_CURSEG_NODE_TYPE     (3)
271 #define NR_CURSEG_TYPE  (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
272
273 enum {
274         CURSEG_HOT_DATA = 0,    /* directory entry blocks */
275         CURSEG_WARM_DATA,       /* data blocks */
276         CURSEG_COLD_DATA,       /* multimedia or GCed data blocks */
277         CURSEG_HOT_NODE,        /* direct node blocks of directory files */
278         CURSEG_WARM_NODE,       /* direct node blocks of normal files */
279         CURSEG_COLD_NODE,       /* indirect node blocks */
280         NO_CHECK_TYPE
281 };
282
283 struct f2fs_sm_info {
284         struct sit_info *sit_info;              /* whole segment information */
285         struct free_segmap_info *free_info;     /* free segment information */
286         struct dirty_seglist_info *dirty_info;  /* dirty segment information */
287         struct curseg_info *curseg_array;       /* active segment information */
288
289         struct list_head wblist_head;   /* list of under-writeback pages */
290         spinlock_t wblist_lock;         /* lock for checkpoint */
291
292         block_t seg0_blkaddr;           /* block address of 0'th segment */
293         block_t main_blkaddr;           /* start block address of main area */
294         block_t ssa_blkaddr;            /* start block address of SSA area */
295
296         unsigned int segment_count;     /* total # of segments */
297         unsigned int main_segments;     /* # of segments in main area */
298         unsigned int reserved_segments; /* # of reserved segments */
299         unsigned int ovp_segments;      /* # of overprovision segments */
300 };
301
302 /*
303  * For directory operation
304  */
305 #define NODE_DIR1_BLOCK         (ADDRS_PER_INODE + 1)
306 #define NODE_DIR2_BLOCK         (ADDRS_PER_INODE + 2)
307 #define NODE_IND1_BLOCK         (ADDRS_PER_INODE + 3)
308 #define NODE_IND2_BLOCK         (ADDRS_PER_INODE + 4)
309 #define NODE_DIND_BLOCK         (ADDRS_PER_INODE + 5)
310
311 /*
312  * For superblock
313  */
314 /*
315  * COUNT_TYPE for monitoring
316  *
317  * f2fs monitors the number of several block types such as on-writeback,
318  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
319  */
320 enum count_type {
321         F2FS_WRITEBACK,
322         F2FS_DIRTY_DENTS,
323         F2FS_DIRTY_NODES,
324         F2FS_DIRTY_META,
325         NR_COUNT_TYPE,
326 };
327
328 /*
329  * Uses as sbi->fs_lock[NR_GLOBAL_LOCKS].
330  * The checkpoint procedure blocks all the locks in this fs_lock array.
331  * Some FS operations grab free locks, and if there is no free lock,
332  * then wait to grab a lock in a round-robin manner.
333  */
334 #define NR_GLOBAL_LOCKS 8
335
336 /*
337  * The below are the page types of bios used in submti_bio().
338  * The available types are:
339  * DATA                 User data pages. It operates as async mode.
340  * NODE                 Node pages. It operates as async mode.
341  * META                 FS metadata pages such as SIT, NAT, CP.
342  * NR_PAGE_TYPE         The number of page types.
343  * META_FLUSH           Make sure the previous pages are written
344  *                      with waiting the bio's completion
345  * ...                  Only can be used with META.
346  */
347 enum page_type {
348         DATA,
349         NODE,
350         META,
351         NR_PAGE_TYPE,
352         META_FLUSH,
353 };
354
355 struct f2fs_sb_info {
356         struct super_block *sb;                 /* pointer to VFS super block */
357         struct proc_dir_entry *s_proc;          /* proc entry */
358         struct buffer_head *raw_super_buf;      /* buffer head of raw sb */
359         struct f2fs_super_block *raw_super;     /* raw super block pointer */
360         int s_dirty;                            /* dirty flag for checkpoint */
361
362         /* for node-related operations */
363         struct f2fs_nm_info *nm_info;           /* node manager */
364         struct inode *node_inode;               /* cache node blocks */
365
366         /* for segment-related operations */
367         struct f2fs_sm_info *sm_info;           /* segment manager */
368         struct bio *bio[NR_PAGE_TYPE];          /* bios to merge */
369         sector_t last_block_in_bio[NR_PAGE_TYPE];       /* last block number */
370         struct rw_semaphore bio_sem;            /* IO semaphore */
371
372         /* for checkpoint */
373         struct f2fs_checkpoint *ckpt;           /* raw checkpoint pointer */
374         struct inode *meta_inode;               /* cache meta blocks */
375         struct mutex cp_mutex;                  /* checkpoint procedure lock */
376         struct mutex fs_lock[NR_GLOBAL_LOCKS];  /* blocking FS operations */
377         struct mutex node_write;                /* locking node writes */
378         struct mutex writepages;                /* mutex for writepages() */
379         unsigned char next_lock_num;            /* round-robin global locks */
380         int por_doing;                          /* recovery is doing or not */
381         int on_build_free_nids;                 /* build_free_nids is doing */
382
383         /* for orphan inode management */
384         struct list_head orphan_inode_list;     /* orphan inode list */
385         struct mutex orphan_inode_mutex;        /* for orphan inode list */
386         unsigned int n_orphans;                 /* # of orphan inodes */
387
388         /* for directory inode management */
389         struct list_head dir_inode_list;        /* dir inode list */
390         spinlock_t dir_inode_lock;              /* for dir inode list lock */
391
392         /* basic file system units */
393         unsigned int log_sectors_per_block;     /* log2 sectors per block */
394         unsigned int log_blocksize;             /* log2 block size */
395         unsigned int blocksize;                 /* block size */
396         unsigned int root_ino_num;              /* root inode number*/
397         unsigned int node_ino_num;              /* node inode number*/
398         unsigned int meta_ino_num;              /* meta inode number*/
399         unsigned int log_blocks_per_seg;        /* log2 blocks per segment */
400         unsigned int blocks_per_seg;            /* blocks per segment */
401         unsigned int segs_per_sec;              /* segments per section */
402         unsigned int secs_per_zone;             /* sections per zone */
403         unsigned int total_sections;            /* total section count */
404         unsigned int total_node_count;          /* total node block count */
405         unsigned int total_valid_node_count;    /* valid node block count */
406         unsigned int total_valid_inode_count;   /* valid inode count */
407         int active_logs;                        /* # of active logs */
408
409         block_t user_block_count;               /* # of user blocks */
410         block_t total_valid_block_count;        /* # of valid blocks */
411         block_t alloc_valid_block_count;        /* # of allocated blocks */
412         block_t last_valid_block_count;         /* for recovery */
413         u32 s_next_generation;                  /* for NFS support */
414         atomic_t nr_pages[NR_COUNT_TYPE];       /* # of pages, see count_type */
415
416         struct f2fs_mount_info mount_opt;       /* mount options */
417
418         /* for cleaning operations */
419         struct mutex gc_mutex;                  /* mutex for GC */
420         struct f2fs_gc_kthread  *gc_thread;     /* GC thread */
421         unsigned int cur_victim_sec;            /* current victim section num */
422
423         /*
424          * for stat information.
425          * one is for the LFS mode, and the other is for the SSR mode.
426          */
427 #ifdef CONFIG_F2FS_STAT_FS
428         struct f2fs_stat_info *stat_info;       /* FS status information */
429         unsigned int segment_count[2];          /* # of allocated segments */
430         unsigned int block_count[2];            /* # of allocated blocks */
431         int total_hit_ext, read_hit_ext;        /* extent cache hit ratio */
432         int bg_gc;                              /* background gc calls */
433         unsigned int n_dirty_dirs;              /* # of dir inodes */
434 #endif
435         unsigned int last_victim[2];            /* last victim segment # */
436         spinlock_t stat_lock;                   /* lock for stat operations */
437
438         /* For sysfs suppport */
439         struct kobject s_kobj;
440         struct completion s_kobj_unregister;
441 };
442
443 /*
444  * Inline functions
445  */
446 static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
447 {
448         return container_of(inode, struct f2fs_inode_info, vfs_inode);
449 }
450
451 static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
452 {
453         return sb->s_fs_info;
454 }
455
456 static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
457 {
458         return (struct f2fs_super_block *)(sbi->raw_super);
459 }
460
461 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
462 {
463         return (struct f2fs_checkpoint *)(sbi->ckpt);
464 }
465
466 static inline struct f2fs_node *F2FS_NODE(struct page *page)
467 {
468         return (struct f2fs_node *)page_address(page);
469 }
470
471 static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
472 {
473         return (struct f2fs_nm_info *)(sbi->nm_info);
474 }
475
476 static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
477 {
478         return (struct f2fs_sm_info *)(sbi->sm_info);
479 }
480
481 static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
482 {
483         return (struct sit_info *)(SM_I(sbi)->sit_info);
484 }
485
486 static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
487 {
488         return (struct free_segmap_info *)(SM_I(sbi)->free_info);
489 }
490
491 static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
492 {
493         return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
494 }
495
496 static inline void F2FS_SET_SB_DIRT(struct f2fs_sb_info *sbi)
497 {
498         sbi->s_dirty = 1;
499 }
500
501 static inline void F2FS_RESET_SB_DIRT(struct f2fs_sb_info *sbi)
502 {
503         sbi->s_dirty = 0;
504 }
505
506 static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
507 {
508         unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
509         return ckpt_flags & f;
510 }
511
512 static inline void set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
513 {
514         unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
515         ckpt_flags |= f;
516         cp->ckpt_flags = cpu_to_le32(ckpt_flags);
517 }
518
519 static inline void clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
520 {
521         unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
522         ckpt_flags &= (~f);
523         cp->ckpt_flags = cpu_to_le32(ckpt_flags);
524 }
525
526 static inline void mutex_lock_all(struct f2fs_sb_info *sbi)
527 {
528         int i;
529
530         for (i = 0; i < NR_GLOBAL_LOCKS; i++) {
531                 /*
532                  * This is the only time we take multiple fs_lock[]
533                  * instances; the order is immaterial since we
534                  * always hold cp_mutex, which serializes multiple
535                  * such operations.
536                  */
537                 mutex_lock_nest_lock(&sbi->fs_lock[i], &sbi->cp_mutex);
538         }
539 }
540
541 static inline void mutex_unlock_all(struct f2fs_sb_info *sbi)
542 {
543         int i = 0;
544         for (; i < NR_GLOBAL_LOCKS; i++)
545                 mutex_unlock(&sbi->fs_lock[i]);
546 }
547
548 static inline int mutex_lock_op(struct f2fs_sb_info *sbi)
549 {
550         unsigned char next_lock = sbi->next_lock_num % NR_GLOBAL_LOCKS;
551         int i = 0;
552
553         for (; i < NR_GLOBAL_LOCKS; i++)
554                 if (mutex_trylock(&sbi->fs_lock[i]))
555                         return i;
556
557         mutex_lock(&sbi->fs_lock[next_lock]);
558         sbi->next_lock_num++;
559         return next_lock;
560 }
561
562 static inline void mutex_unlock_op(struct f2fs_sb_info *sbi, int ilock)
563 {
564         if (ilock < 0)
565                 return;
566         BUG_ON(ilock >= NR_GLOBAL_LOCKS);
567         mutex_unlock(&sbi->fs_lock[ilock]);
568 }
569
570 /*
571  * Check whether the given nid is within node id range.
572  */
573 static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
574 {
575         WARN_ON((nid >= NM_I(sbi)->max_nid));
576         if (nid >= NM_I(sbi)->max_nid)
577                 return -EINVAL;
578         return 0;
579 }
580
581 #define F2FS_DEFAULT_ALLOCATED_BLOCKS   1
582
583 /*
584  * Check whether the inode has blocks or not
585  */
586 static inline int F2FS_HAS_BLOCKS(struct inode *inode)
587 {
588         if (F2FS_I(inode)->i_xattr_nid)
589                 return (inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1);
590         else
591                 return (inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS);
592 }
593
594 static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
595                                  struct inode *inode, blkcnt_t count)
596 {
597         block_t valid_block_count;
598
599         spin_lock(&sbi->stat_lock);
600         valid_block_count =
601                 sbi->total_valid_block_count + (block_t)count;
602         if (valid_block_count > sbi->user_block_count) {
603                 spin_unlock(&sbi->stat_lock);
604                 return false;
605         }
606         inode->i_blocks += count;
607         sbi->total_valid_block_count = valid_block_count;
608         sbi->alloc_valid_block_count += (block_t)count;
609         spin_unlock(&sbi->stat_lock);
610         return true;
611 }
612
613 static inline int dec_valid_block_count(struct f2fs_sb_info *sbi,
614                                                 struct inode *inode,
615                                                 blkcnt_t count)
616 {
617         spin_lock(&sbi->stat_lock);
618         BUG_ON(sbi->total_valid_block_count < (block_t) count);
619         BUG_ON(inode->i_blocks < count);
620         inode->i_blocks -= count;
621         sbi->total_valid_block_count -= (block_t)count;
622         spin_unlock(&sbi->stat_lock);
623         return 0;
624 }
625
626 static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
627 {
628         atomic_inc(&sbi->nr_pages[count_type]);
629         F2FS_SET_SB_DIRT(sbi);
630 }
631
632 static inline void inode_inc_dirty_dents(struct inode *inode)
633 {
634         atomic_inc(&F2FS_I(inode)->dirty_dents);
635 }
636
637 static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
638 {
639         atomic_dec(&sbi->nr_pages[count_type]);
640 }
641
642 static inline void inode_dec_dirty_dents(struct inode *inode)
643 {
644         atomic_dec(&F2FS_I(inode)->dirty_dents);
645 }
646
647 static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
648 {
649         return atomic_read(&sbi->nr_pages[count_type]);
650 }
651
652 static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
653 {
654         unsigned int pages_per_sec = sbi->segs_per_sec *
655                                         (1 << sbi->log_blocks_per_seg);
656         return ((get_pages(sbi, block_type) + pages_per_sec - 1)
657                         >> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
658 }
659
660 static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
661 {
662         block_t ret;
663         spin_lock(&sbi->stat_lock);
664         ret = sbi->total_valid_block_count;
665         spin_unlock(&sbi->stat_lock);
666         return ret;
667 }
668
669 static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
670 {
671         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
672
673         /* return NAT or SIT bitmap */
674         if (flag == NAT_BITMAP)
675                 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
676         else if (flag == SIT_BITMAP)
677                 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
678
679         return 0;
680 }
681
682 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
683 {
684         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
685         int offset = (flag == NAT_BITMAP) ?
686                         le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
687         return &ckpt->sit_nat_version_bitmap + offset;
688 }
689
690 static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
691 {
692         block_t start_addr;
693         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
694         unsigned long long ckpt_version = le64_to_cpu(ckpt->checkpoint_ver);
695
696         start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
697
698         /*
699          * odd numbered checkpoint should at cp segment 0
700          * and even segent must be at cp segment 1
701          */
702         if (!(ckpt_version & 1))
703                 start_addr += sbi->blocks_per_seg;
704
705         return start_addr;
706 }
707
708 static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
709 {
710         return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
711 }
712
713 static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
714                                                 struct inode *inode,
715                                                 unsigned int count)
716 {
717         block_t valid_block_count;
718         unsigned int valid_node_count;
719
720         spin_lock(&sbi->stat_lock);
721
722         valid_block_count = sbi->total_valid_block_count + (block_t)count;
723         sbi->alloc_valid_block_count += (block_t)count;
724         valid_node_count = sbi->total_valid_node_count + count;
725
726         if (valid_block_count > sbi->user_block_count) {
727                 spin_unlock(&sbi->stat_lock);
728                 return false;
729         }
730
731         if (valid_node_count > sbi->total_node_count) {
732                 spin_unlock(&sbi->stat_lock);
733                 return false;
734         }
735
736         if (inode)
737                 inode->i_blocks += count;
738         sbi->total_valid_node_count = valid_node_count;
739         sbi->total_valid_block_count = valid_block_count;
740         spin_unlock(&sbi->stat_lock);
741
742         return true;
743 }
744
745 static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
746                                                 struct inode *inode,
747                                                 unsigned int count)
748 {
749         spin_lock(&sbi->stat_lock);
750
751         BUG_ON(sbi->total_valid_block_count < count);
752         BUG_ON(sbi->total_valid_node_count < count);
753         BUG_ON(inode->i_blocks < count);
754
755         inode->i_blocks -= count;
756         sbi->total_valid_node_count -= count;
757         sbi->total_valid_block_count -= (block_t)count;
758
759         spin_unlock(&sbi->stat_lock);
760 }
761
762 static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
763 {
764         unsigned int ret;
765         spin_lock(&sbi->stat_lock);
766         ret = sbi->total_valid_node_count;
767         spin_unlock(&sbi->stat_lock);
768         return ret;
769 }
770
771 static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
772 {
773         spin_lock(&sbi->stat_lock);
774         BUG_ON(sbi->total_valid_inode_count == sbi->total_node_count);
775         sbi->total_valid_inode_count++;
776         spin_unlock(&sbi->stat_lock);
777 }
778
779 static inline int dec_valid_inode_count(struct f2fs_sb_info *sbi)
780 {
781         spin_lock(&sbi->stat_lock);
782         BUG_ON(!sbi->total_valid_inode_count);
783         sbi->total_valid_inode_count--;
784         spin_unlock(&sbi->stat_lock);
785         return 0;
786 }
787
788 static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
789 {
790         unsigned int ret;
791         spin_lock(&sbi->stat_lock);
792         ret = sbi->total_valid_inode_count;
793         spin_unlock(&sbi->stat_lock);
794         return ret;
795 }
796
797 static inline void f2fs_put_page(struct page *page, int unlock)
798 {
799         if (!page || IS_ERR(page))
800                 return;
801
802         if (unlock) {
803                 BUG_ON(!PageLocked(page));
804                 unlock_page(page);
805         }
806         page_cache_release(page);
807 }
808
809 static inline void f2fs_put_dnode(struct dnode_of_data *dn)
810 {
811         if (dn->node_page)
812                 f2fs_put_page(dn->node_page, 1);
813         if (dn->inode_page && dn->node_page != dn->inode_page)
814                 f2fs_put_page(dn->inode_page, 0);
815         dn->node_page = NULL;
816         dn->inode_page = NULL;
817 }
818
819 static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
820                                         size_t size, void (*ctor)(void *))
821 {
822         return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, ctor);
823 }
824
825 #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
826
827 static inline bool IS_INODE(struct page *page)
828 {
829         struct f2fs_node *p = F2FS_NODE(page);
830         return RAW_IS_INODE(p);
831 }
832
833 static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
834 {
835         return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
836 }
837
838 static inline block_t datablock_addr(struct page *node_page,
839                 unsigned int offset)
840 {
841         struct f2fs_node *raw_node;
842         __le32 *addr_array;
843         raw_node = F2FS_NODE(node_page);
844         addr_array = blkaddr_in_node(raw_node);
845         return le32_to_cpu(addr_array[offset]);
846 }
847
848 static inline int f2fs_test_bit(unsigned int nr, char *addr)
849 {
850         int mask;
851
852         addr += (nr >> 3);
853         mask = 1 << (7 - (nr & 0x07));
854         return mask & *addr;
855 }
856
857 static inline int f2fs_set_bit(unsigned int nr, char *addr)
858 {
859         int mask;
860         int ret;
861
862         addr += (nr >> 3);
863         mask = 1 << (7 - (nr & 0x07));
864         ret = mask & *addr;
865         *addr |= mask;
866         return ret;
867 }
868
869 static inline int f2fs_clear_bit(unsigned int nr, char *addr)
870 {
871         int mask;
872         int ret;
873
874         addr += (nr >> 3);
875         mask = 1 << (7 - (nr & 0x07));
876         ret = mask & *addr;
877         *addr &= ~mask;
878         return ret;
879 }
880
881 /* used for f2fs_inode_info->flags */
882 enum {
883         FI_NEW_INODE,           /* indicate newly allocated inode */
884         FI_DIRTY_INODE,         /* indicate inode is dirty or not */
885         FI_INC_LINK,            /* need to increment i_nlink */
886         FI_ACL_MODE,            /* indicate acl mode */
887         FI_NO_ALLOC,            /* should not allocate any blocks */
888         FI_UPDATE_DIR,          /* should update inode block for consistency */
889         FI_DELAY_IPUT,          /* used for the recovery */
890 };
891
892 static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
893 {
894         set_bit(flag, &fi->flags);
895 }
896
897 static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
898 {
899         return test_bit(flag, &fi->flags);
900 }
901
902 static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
903 {
904         clear_bit(flag, &fi->flags);
905 }
906
907 static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
908 {
909         fi->i_acl_mode = mode;
910         set_inode_flag(fi, FI_ACL_MODE);
911 }
912
913 static inline int cond_clear_inode_flag(struct f2fs_inode_info *fi, int flag)
914 {
915         if (is_inode_flag_set(fi, FI_ACL_MODE)) {
916                 clear_inode_flag(fi, FI_ACL_MODE);
917                 return 1;
918         }
919         return 0;
920 }
921
922 static inline int f2fs_readonly(struct super_block *sb)
923 {
924         return sb->s_flags & MS_RDONLY;
925 }
926
927 /*
928  * file.c
929  */
930 int f2fs_sync_file(struct file *, loff_t, loff_t, int);
931 void truncate_data_blocks(struct dnode_of_data *);
932 void f2fs_truncate(struct inode *);
933 int f2fs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
934 int f2fs_setattr(struct dentry *, struct iattr *);
935 int truncate_hole(struct inode *, pgoff_t, pgoff_t);
936 int truncate_data_blocks_range(struct dnode_of_data *, int);
937 long f2fs_ioctl(struct file *, unsigned int, unsigned long);
938 long f2fs_compat_ioctl(struct file *, unsigned int, unsigned long);
939
940 /*
941  * inode.c
942  */
943 void f2fs_set_inode_flags(struct inode *);
944 struct inode *f2fs_iget(struct super_block *, unsigned long);
945 void update_inode(struct inode *, struct page *);
946 int update_inode_page(struct inode *);
947 int f2fs_write_inode(struct inode *, struct writeback_control *);
948 void f2fs_evict_inode(struct inode *);
949
950 /*
951  * namei.c
952  */
953 struct dentry *f2fs_get_parent(struct dentry *child);
954
955 /*
956  * dir.c
957  */
958 struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
959                                                         struct page **);
960 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
961 ino_t f2fs_inode_by_name(struct inode *, struct qstr *);
962 void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
963                                 struct page *, struct inode *);
964 int update_dent_inode(struct inode *, const struct qstr *);
965 int __f2fs_add_link(struct inode *, const struct qstr *, struct inode *);
966 void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *);
967 int f2fs_make_empty(struct inode *, struct inode *);
968 bool f2fs_empty_dir(struct inode *);
969
970 static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
971 {
972         return __f2fs_add_link(dentry->d_parent->d_inode, &dentry->d_name,
973                                 inode);
974 }
975
976 /*
977  * super.c
978  */
979 int f2fs_sync_fs(struct super_block *, int);
980 extern __printf(3, 4)
981 void f2fs_msg(struct super_block *, const char *, const char *, ...);
982
983 /*
984  * hash.c
985  */
986 f2fs_hash_t f2fs_dentry_hash(const char *, size_t);
987
988 /*
989  * node.c
990  */
991 struct dnode_of_data;
992 struct node_info;
993
994 int is_checkpointed_node(struct f2fs_sb_info *, nid_t);
995 void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
996 int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
997 int truncate_inode_blocks(struct inode *, pgoff_t);
998 int remove_inode_page(struct inode *);
999 struct page *new_inode_page(struct inode *, const struct qstr *);
1000 struct page *new_node_page(struct dnode_of_data *, unsigned int, struct page *);
1001 void ra_node_page(struct f2fs_sb_info *, nid_t);
1002 struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
1003 struct page *get_node_page_ra(struct page *, int);
1004 void sync_inode_page(struct dnode_of_data *);
1005 int sync_node_pages(struct f2fs_sb_info *, nid_t, struct writeback_control *);
1006 bool alloc_nid(struct f2fs_sb_info *, nid_t *);
1007 void alloc_nid_done(struct f2fs_sb_info *, nid_t);
1008 void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
1009 void recover_node_page(struct f2fs_sb_info *, struct page *,
1010                 struct f2fs_summary *, struct node_info *, block_t);
1011 int recover_inode_page(struct f2fs_sb_info *, struct page *);
1012 int restore_node_summary(struct f2fs_sb_info *, unsigned int,
1013                                 struct f2fs_summary_block *);
1014 void flush_nat_entries(struct f2fs_sb_info *);
1015 int build_node_manager(struct f2fs_sb_info *);
1016 void destroy_node_manager(struct f2fs_sb_info *);
1017 int __init create_node_manager_caches(void);
1018 void destroy_node_manager_caches(void);
1019
1020 /*
1021  * segment.c
1022  */
1023 void f2fs_balance_fs(struct f2fs_sb_info *);
1024 void invalidate_blocks(struct f2fs_sb_info *, block_t);
1025 void clear_prefree_segments(struct f2fs_sb_info *);
1026 int npages_for_summary_flush(struct f2fs_sb_info *);
1027 void allocate_new_segments(struct f2fs_sb_info *);
1028 struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
1029 struct bio *f2fs_bio_alloc(struct block_device *, int);
1030 void f2fs_submit_bio(struct f2fs_sb_info *, enum page_type, bool);
1031 void f2fs_wait_on_page_writeback(struct page *, enum page_type, bool);
1032 void write_meta_page(struct f2fs_sb_info *, struct page *);
1033 void write_node_page(struct f2fs_sb_info *, struct page *, unsigned int,
1034                                         block_t, block_t *);
1035 void write_data_page(struct inode *, struct page *, struct dnode_of_data*,
1036                                         block_t, block_t *);
1037 void rewrite_data_page(struct f2fs_sb_info *, struct page *, block_t);
1038 void recover_data_page(struct f2fs_sb_info *, struct page *,
1039                                 struct f2fs_summary *, block_t, block_t);
1040 void rewrite_node_page(struct f2fs_sb_info *, struct page *,
1041                                 struct f2fs_summary *, block_t, block_t);
1042 void write_data_summaries(struct f2fs_sb_info *, block_t);
1043 void write_node_summaries(struct f2fs_sb_info *, block_t);
1044 int lookup_journal_in_cursum(struct f2fs_summary_block *,
1045                                         int, unsigned int, int);
1046 void flush_sit_entries(struct f2fs_sb_info *);
1047 int build_segment_manager(struct f2fs_sb_info *);
1048 void destroy_segment_manager(struct f2fs_sb_info *);
1049
1050 /*
1051  * checkpoint.c
1052  */
1053 struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
1054 struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
1055 long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
1056 int acquire_orphan_inode(struct f2fs_sb_info *);
1057 void release_orphan_inode(struct f2fs_sb_info *);
1058 void add_orphan_inode(struct f2fs_sb_info *, nid_t);
1059 void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
1060 int recover_orphan_inodes(struct f2fs_sb_info *);
1061 int get_valid_checkpoint(struct f2fs_sb_info *);
1062 void set_dirty_dir_page(struct inode *, struct page *);
1063 void add_dirty_dir_inode(struct inode *);
1064 void remove_dirty_dir_inode(struct inode *);
1065 struct inode *check_dirty_dir_inode(struct f2fs_sb_info *, nid_t);
1066 void sync_dirty_dir_inodes(struct f2fs_sb_info *);
1067 void write_checkpoint(struct f2fs_sb_info *, bool);
1068 void init_orphan_info(struct f2fs_sb_info *);
1069 int __init create_checkpoint_caches(void);
1070 void destroy_checkpoint_caches(void);
1071
1072 /*
1073  * data.c
1074  */
1075 int reserve_new_block(struct dnode_of_data *);
1076 void update_extent_cache(block_t, struct dnode_of_data *);
1077 struct page *find_data_page(struct inode *, pgoff_t, bool);
1078 struct page *get_lock_data_page(struct inode *, pgoff_t);
1079 struct page *get_new_data_page(struct inode *, struct page *, pgoff_t, bool);
1080 int f2fs_readpage(struct f2fs_sb_info *, struct page *, block_t, int);
1081 int do_write_data_page(struct page *);
1082
1083 /*
1084  * gc.c
1085  */
1086 int start_gc_thread(struct f2fs_sb_info *);
1087 void stop_gc_thread(struct f2fs_sb_info *);
1088 block_t start_bidx_of_node(unsigned int);
1089 int f2fs_gc(struct f2fs_sb_info *);
1090 void build_gc_manager(struct f2fs_sb_info *);
1091 int __init create_gc_caches(void);
1092 void destroy_gc_caches(void);
1093
1094 /*
1095  * recovery.c
1096  */
1097 int recover_fsync_data(struct f2fs_sb_info *);
1098 bool space_for_roll_forward(struct f2fs_sb_info *);
1099
1100 /*
1101  * debug.c
1102  */
1103 #ifdef CONFIG_F2FS_STAT_FS
1104 struct f2fs_stat_info {
1105         struct list_head stat_list;
1106         struct f2fs_sb_info *sbi;
1107         struct mutex stat_lock;
1108         int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
1109         int main_area_segs, main_area_sections, main_area_zones;
1110         int hit_ext, total_ext;
1111         int ndirty_node, ndirty_dent, ndirty_dirs, ndirty_meta;
1112         int nats, sits, fnids;
1113         int total_count, utilization;
1114         int bg_gc;
1115         unsigned int valid_count, valid_node_count, valid_inode_count;
1116         unsigned int bimodal, avg_vblocks;
1117         int util_free, util_valid, util_invalid;
1118         int rsvd_segs, overp_segs;
1119         int dirty_count, node_pages, meta_pages;
1120         int prefree_count, call_count;
1121         int tot_segs, node_segs, data_segs, free_segs, free_secs;
1122         int tot_blks, data_blks, node_blks;
1123         int curseg[NR_CURSEG_TYPE];
1124         int cursec[NR_CURSEG_TYPE];
1125         int curzone[NR_CURSEG_TYPE];
1126
1127         unsigned int segment_count[2];
1128         unsigned int block_count[2];
1129         unsigned base_mem, cache_mem;
1130 };
1131
1132 static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
1133 {
1134         return (struct f2fs_stat_info*)sbi->stat_info;
1135 }
1136
1137 #define stat_inc_call_count(si) ((si)->call_count++)
1138
1139 #define stat_inc_seg_count(sbi, type)                                   \
1140         do {                                                            \
1141                 struct f2fs_stat_info *si = F2FS_STAT(sbi);             \
1142                 (si)->tot_segs++;                                       \
1143                 if (type == SUM_TYPE_DATA)                              \
1144                         si->data_segs++;                                \
1145                 else                                                    \
1146                         si->node_segs++;                                \
1147         } while (0)
1148
1149 #define stat_inc_tot_blk_count(si, blks)                                \
1150         (si->tot_blks += (blks))
1151
1152 #define stat_inc_data_blk_count(sbi, blks)                              \
1153         do {                                                            \
1154                 struct f2fs_stat_info *si = F2FS_STAT(sbi);             \
1155                 stat_inc_tot_blk_count(si, blks);                       \
1156                 si->data_blks += (blks);                                \
1157         } while (0)
1158
1159 #define stat_inc_node_blk_count(sbi, blks)                              \
1160         do {                                                            \
1161                 struct f2fs_stat_info *si = F2FS_STAT(sbi);             \
1162                 stat_inc_tot_blk_count(si, blks);                       \
1163                 si->node_blks += (blks);                                \
1164         } while (0)
1165
1166 int f2fs_build_stats(struct f2fs_sb_info *);
1167 void f2fs_destroy_stats(struct f2fs_sb_info *);
1168 void __init f2fs_create_root_stats(void);
1169 void f2fs_destroy_root_stats(void);
1170 #else
1171 #define stat_inc_call_count(si)
1172 #define stat_inc_seg_count(si, type)
1173 #define stat_inc_tot_blk_count(si, blks)
1174 #define stat_inc_data_blk_count(si, blks)
1175 #define stat_inc_node_blk_count(sbi, blks)
1176
1177 static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
1178 static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
1179 static inline void __init f2fs_create_root_stats(void) { }
1180 static inline void f2fs_destroy_root_stats(void) { }
1181 #endif
1182
1183 extern const struct file_operations f2fs_dir_operations;
1184 extern const struct file_operations f2fs_file_operations;
1185 extern const struct inode_operations f2fs_file_inode_operations;
1186 extern const struct address_space_operations f2fs_dblock_aops;
1187 extern const struct address_space_operations f2fs_node_aops;
1188 extern const struct address_space_operations f2fs_meta_aops;
1189 extern const struct inode_operations f2fs_dir_inode_operations;
1190 extern const struct inode_operations f2fs_symlink_inode_operations;
1191 extern const struct inode_operations f2fs_special_inode_operations;
1192 #endif