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