4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
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>
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
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)
37 #define ver_after(a, b) (typecheck(unsigned long long, a) && \
38 typecheck(unsigned long long, b) && \
39 ((long long)((a) - (b)) > 0))
41 typedef u32 block_t; /*
42 * should not change u32, since it is the on-disk block
43 * address format, __le32.
47 struct f2fs_mount_info {
51 #define CRCPOLY_LE 0xedb88320
53 static inline __u32 f2fs_crc32(void *buf, size_t len)
55 unsigned char *p = (unsigned char *)buf;
56 __u32 crc = F2FS_SUPER_MAGIC;
61 for (i = 0; i < 8; i++)
62 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
67 static inline bool f2fs_crc_valid(__u32 blk_crc, void *buf, size_t buf_size)
69 return f2fs_crc32(buf, buf_size) == blk_crc;
73 * For checkpoint manager
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 */
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 */
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 */
99 #define nats_in_cursum(sum) (le16_to_cpu(sum->n_nats))
100 #define sits_in_cursum(sum) (le16_to_cpu(sum->n_sits))
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)
107 static inline int update_nats_in_cursum(struct f2fs_summary_block *rs, int i)
109 int before = nats_in_cursum(rs);
110 rs->n_nats = cpu_to_le16(before + i);
114 static inline int update_sits_in_cursum(struct f2fs_summary_block *rs, int i)
116 int before = sits_in_cursum(rs);
117 rs->n_sits = cpu_to_le16(before + i);
124 #define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
125 #define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
127 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
129 * ioctl commands in 32 bit emulation
131 #define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
132 #define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
136 * For INODE and NODE manager
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.
143 #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
146 ALLOC_NODE, /* allocate a new node page if needed */
147 LOOKUP_NODE, /* look up a node without readahead */
149 * look up a node with readahead called
150 * by get_datablock_ro.
154 #define F2FS_LINK_MAX 32000 /* maximum link count per file */
156 /* for in-memory extent cache entry */
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 */
165 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
167 #define FADVISE_COLD_BIT 0x01
168 #define FADVISE_LOST_PINO_BIT 0x02
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 */
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 */
188 static inline void get_extent_info(struct extent_info *ext,
189 struct f2fs_extent i_ext)
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);
198 static inline void set_raw_extent(struct extent_info *ext,
199 struct f2fs_extent *i_ext)
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);
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 */
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) */
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 */
227 char *nat_bitmap; /* NAT bitmap pointer */
228 int bitmap_size; /* bitmap size */
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.
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 */
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)
249 memset(dn, 0, sizeof(*dn));
251 dn->inode_page = ipage;
252 dn->node_page = npage;
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,
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.
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)
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 */
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 */
289 struct list_head wblist_head; /* list of under-writeback pages */
290 spinlock_t wblist_lock; /* lock for checkpoint */
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 */
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 */
303 * For directory operation
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)
315 * COUNT_TYPE for monitoring
317 * f2fs monitors the number of several block types such as on-writeback,
318 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
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.
334 #define NR_GLOBAL_LOCKS 8
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.
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 */
362 /* for node-related operations */
363 struct f2fs_nm_info *nm_info; /* node manager */
364 struct inode *node_inode; /* cache node blocks */
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 */
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 */
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 */
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 */
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 */
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 */
416 struct f2fs_mount_info mount_opt; /* mount options */
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 */
424 * for stat information.
425 * one is for the LFS mode, and the other is for the SSR mode.
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 */
435 unsigned int last_victim[2]; /* last victim segment # */
436 spinlock_t stat_lock; /* lock for stat operations */
438 /* For sysfs suppport */
439 struct kobject s_kobj;
440 struct completion s_kobj_unregister;
446 static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
448 return container_of(inode, struct f2fs_inode_info, vfs_inode);
451 static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
453 return sb->s_fs_info;
456 static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
458 return (struct f2fs_super_block *)(sbi->raw_super);
461 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
463 return (struct f2fs_checkpoint *)(sbi->ckpt);
466 static inline struct f2fs_node *F2FS_NODE(struct page *page)
468 return (struct f2fs_node *)page_address(page);
471 static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
473 return (struct f2fs_nm_info *)(sbi->nm_info);
476 static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
478 return (struct f2fs_sm_info *)(sbi->sm_info);
481 static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
483 return (struct sit_info *)(SM_I(sbi)->sit_info);
486 static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
488 return (struct free_segmap_info *)(SM_I(sbi)->free_info);
491 static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
493 return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
496 static inline void F2FS_SET_SB_DIRT(struct f2fs_sb_info *sbi)
501 static inline void F2FS_RESET_SB_DIRT(struct f2fs_sb_info *sbi)
506 static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
508 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
509 return ckpt_flags & f;
512 static inline void set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
514 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
516 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
519 static inline void clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
521 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
523 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
526 static inline void mutex_lock_all(struct f2fs_sb_info *sbi)
530 for (i = 0; i < NR_GLOBAL_LOCKS; i++) {
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
537 mutex_lock_nest_lock(&sbi->fs_lock[i], &sbi->cp_mutex);
541 static inline void mutex_unlock_all(struct f2fs_sb_info *sbi)
544 for (; i < NR_GLOBAL_LOCKS; i++)
545 mutex_unlock(&sbi->fs_lock[i]);
548 static inline int mutex_lock_op(struct f2fs_sb_info *sbi)
550 unsigned char next_lock = sbi->next_lock_num % NR_GLOBAL_LOCKS;
553 for (; i < NR_GLOBAL_LOCKS; i++)
554 if (mutex_trylock(&sbi->fs_lock[i]))
557 mutex_lock(&sbi->fs_lock[next_lock]);
558 sbi->next_lock_num++;
562 static inline void mutex_unlock_op(struct f2fs_sb_info *sbi, int ilock)
566 BUG_ON(ilock >= NR_GLOBAL_LOCKS);
567 mutex_unlock(&sbi->fs_lock[ilock]);
571 * Check whether the given nid is within node id range.
573 static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
575 WARN_ON((nid >= NM_I(sbi)->max_nid));
576 if (nid >= NM_I(sbi)->max_nid)
581 #define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
584 * Check whether the inode has blocks or not
586 static inline int F2FS_HAS_BLOCKS(struct inode *inode)
588 if (F2FS_I(inode)->i_xattr_nid)
589 return (inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1);
591 return (inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS);
594 static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
595 struct inode *inode, blkcnt_t count)
597 block_t valid_block_count;
599 spin_lock(&sbi->stat_lock);
601 sbi->total_valid_block_count + (block_t)count;
602 if (valid_block_count > sbi->user_block_count) {
603 spin_unlock(&sbi->stat_lock);
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);
613 static inline int dec_valid_block_count(struct f2fs_sb_info *sbi,
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);
626 static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
628 atomic_inc(&sbi->nr_pages[count_type]);
629 F2FS_SET_SB_DIRT(sbi);
632 static inline void inode_inc_dirty_dents(struct inode *inode)
634 atomic_inc(&F2FS_I(inode)->dirty_dents);
637 static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
639 atomic_dec(&sbi->nr_pages[count_type]);
642 static inline void inode_dec_dirty_dents(struct inode *inode)
644 atomic_dec(&F2FS_I(inode)->dirty_dents);
647 static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
649 return atomic_read(&sbi->nr_pages[count_type]);
652 static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
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;
660 static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
663 spin_lock(&sbi->stat_lock);
664 ret = sbi->total_valid_block_count;
665 spin_unlock(&sbi->stat_lock);
669 static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
671 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
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);
682 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
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;
690 static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
693 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
694 unsigned long long ckpt_version = le64_to_cpu(ckpt->checkpoint_ver);
696 start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
699 * odd numbered checkpoint should at cp segment 0
700 * and even segent must be at cp segment 1
702 if (!(ckpt_version & 1))
703 start_addr += sbi->blocks_per_seg;
708 static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
710 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
713 static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
717 block_t valid_block_count;
718 unsigned int valid_node_count;
720 spin_lock(&sbi->stat_lock);
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;
726 if (valid_block_count > sbi->user_block_count) {
727 spin_unlock(&sbi->stat_lock);
731 if (valid_node_count > sbi->total_node_count) {
732 spin_unlock(&sbi->stat_lock);
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);
745 static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
749 spin_lock(&sbi->stat_lock);
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);
755 inode->i_blocks -= count;
756 sbi->total_valid_node_count -= count;
757 sbi->total_valid_block_count -= (block_t)count;
759 spin_unlock(&sbi->stat_lock);
762 static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
765 spin_lock(&sbi->stat_lock);
766 ret = sbi->total_valid_node_count;
767 spin_unlock(&sbi->stat_lock);
771 static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
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);
779 static inline int dec_valid_inode_count(struct f2fs_sb_info *sbi)
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);
788 static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
791 spin_lock(&sbi->stat_lock);
792 ret = sbi->total_valid_inode_count;
793 spin_unlock(&sbi->stat_lock);
797 static inline void f2fs_put_page(struct page *page, int unlock)
799 if (!page || IS_ERR(page))
803 BUG_ON(!PageLocked(page));
806 page_cache_release(page);
809 static inline void f2fs_put_dnode(struct dnode_of_data *dn)
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;
819 static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
820 size_t size, void (*ctor)(void *))
822 return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, ctor);
825 #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
827 static inline bool IS_INODE(struct page *page)
829 struct f2fs_node *p = F2FS_NODE(page);
830 return RAW_IS_INODE(p);
833 static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
835 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
838 static inline block_t datablock_addr(struct page *node_page,
841 struct f2fs_node *raw_node;
843 raw_node = F2FS_NODE(node_page);
844 addr_array = blkaddr_in_node(raw_node);
845 return le32_to_cpu(addr_array[offset]);
848 static inline int f2fs_test_bit(unsigned int nr, char *addr)
853 mask = 1 << (7 - (nr & 0x07));
857 static inline int f2fs_set_bit(unsigned int nr, char *addr)
863 mask = 1 << (7 - (nr & 0x07));
869 static inline int f2fs_clear_bit(unsigned int nr, char *addr)
875 mask = 1 << (7 - (nr & 0x07));
881 /* used for f2fs_inode_info->flags */
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 */
892 static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
894 set_bit(flag, &fi->flags);
897 static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
899 return test_bit(flag, &fi->flags);
902 static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
904 clear_bit(flag, &fi->flags);
907 static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
909 fi->i_acl_mode = mode;
910 set_inode_flag(fi, FI_ACL_MODE);
913 static inline int cond_clear_inode_flag(struct f2fs_inode_info *fi, int flag)
915 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
916 clear_inode_flag(fi, FI_ACL_MODE);
922 static inline int f2fs_readonly(struct super_block *sb)
924 return sb->s_flags & MS_RDONLY;
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);
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 *);
953 struct dentry *f2fs_get_parent(struct dentry *child);
958 struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
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 *);
970 static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
972 return __f2fs_add_link(dentry->d_parent->d_inode, &dentry->d_name,
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 *, ...);
986 f2fs_hash_t f2fs_dentry_hash(const char *, size_t);
991 struct dnode_of_data;
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);
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 *);
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);
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 *);
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);
1097 int recover_fsync_data(struct f2fs_sb_info *);
1098 bool space_for_roll_forward(struct f2fs_sb_info *);
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;
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];
1127 unsigned int segment_count[2];
1128 unsigned int block_count[2];
1129 unsigned base_mem, cache_mem;
1132 static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
1134 return (struct f2fs_stat_info*)sbi->stat_info;
1137 #define stat_inc_call_count(si) ((si)->call_count++)
1139 #define stat_inc_seg_count(sbi, type) \
1141 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
1143 if (type == SUM_TYPE_DATA) \
1149 #define stat_inc_tot_blk_count(si, blks) \
1150 (si->tot_blks += (blks))
1152 #define stat_inc_data_blk_count(sbi, blks) \
1154 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
1155 stat_inc_tot_blk_count(si, blks); \
1156 si->data_blks += (blks); \
1159 #define stat_inc_node_blk_count(sbi, blks) \
1161 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
1162 stat_inc_tot_blk_count(si, blks); \
1163 si->node_blks += (blks); \
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);
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)
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) { }
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;