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>
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
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)
36 #define ver_after(a, b) (typecheck(unsigned long long, a) && \
37 typecheck(unsigned long long, b) && \
38 ((long long)((a) - (b)) > 0))
43 struct f2fs_mount_info {
47 static inline __u32 f2fs_crc32(void *buff, size_t len)
49 return crc32_le(F2FS_SUPER_MAGIC, buff, len);
52 static inline bool f2fs_crc_valid(__u32 blk_crc, void *buff, size_t buff_size)
54 return f2fs_crc32(buff, buff_size) == blk_crc;
58 * For checkpoint manager
65 /* for the list of orphan inodes */
66 struct orphan_inode_entry {
67 struct list_head list; /* list head */
68 nid_t ino; /* inode number */
71 /* for the list of directory inodes */
72 struct dir_inode_entry {
73 struct list_head list; /* list head */
74 struct inode *inode; /* vfs inode pointer */
77 /* for the list of fsync inodes, used only during recovery */
78 struct 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 */
84 #define nats_in_cursum(sum) (le16_to_cpu(sum->n_nats))
85 #define sits_in_cursum(sum) (le16_to_cpu(sum->n_sits))
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)
92 static inline int update_nats_in_cursum(struct f2fs_summary_block *rs, int i)
94 int before = nats_in_cursum(rs);
95 rs->n_nats = cpu_to_le16(before + i);
99 static inline int update_sits_in_cursum(struct f2fs_summary_block *rs, int i)
101 int before = sits_in_cursum(rs);
102 rs->n_sits = cpu_to_le16(before + i);
109 #define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
110 #define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
112 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
114 * ioctl commands in 32 bit emulation
116 #define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
117 #define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
121 * For INODE and NODE manager
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.
129 ALLOC_NODE, /* allocate a new node page if needed */
130 LOOKUP_NODE, /* look up a node without readahead */
132 * look up a node with readahead called
133 * by get_datablock_ro.
137 #define F2FS_LINK_MAX 32000 /* maximum link count per file */
139 /* for in-memory extent cache entry */
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 */
144 unsigned int len; /* lenth of the extent */
148 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
150 #define FADVISE_COLD_BIT 0x01
152 struct 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 */
157 unsigned int i_pino; /* parent inode number */
158 umode_t i_acl_mode; /* keep file acl mode temporarily */
160 /* Use below internally in f2fs*/
161 unsigned long flags; /* use to pass per-file flags */
162 unsigned long long data_version;/* latest version of data for fsync */
163 atomic_t dirty_dents; /* # of dirty dentry pages */
164 f2fs_hash_t chash; /* hash value of given file name */
165 unsigned int clevel; /* maximum level of given file name */
166 nid_t i_xattr_nid; /* node id that contains xattrs */
167 struct extent_info ext; /* in-memory extent cache entry */
170 static inline void get_extent_info(struct extent_info *ext,
171 struct f2fs_extent i_ext)
173 write_lock(&ext->ext_lock);
174 ext->fofs = le32_to_cpu(i_ext.fofs);
175 ext->blk_addr = le32_to_cpu(i_ext.blk_addr);
176 ext->len = le32_to_cpu(i_ext.len);
177 write_unlock(&ext->ext_lock);
180 static inline void set_raw_extent(struct extent_info *ext,
181 struct f2fs_extent *i_ext)
183 read_lock(&ext->ext_lock);
184 i_ext->fofs = cpu_to_le32(ext->fofs);
185 i_ext->blk_addr = cpu_to_le32(ext->blk_addr);
186 i_ext->len = cpu_to_le32(ext->len);
187 read_unlock(&ext->ext_lock);
190 struct f2fs_nm_info {
191 block_t nat_blkaddr; /* base disk address of NAT */
192 nid_t max_nid; /* maximum possible node ids */
193 nid_t init_scan_nid; /* the first nid to be scanned */
194 nid_t next_scan_nid; /* the next nid to be scanned */
196 /* NAT cache management */
197 struct radix_tree_root nat_root;/* root of the nat entry cache */
198 rwlock_t nat_tree_lock; /* protect nat_tree_lock */
199 unsigned int nat_cnt; /* the # of cached nat entries */
200 struct list_head nat_entries; /* cached nat entry list (clean) */
201 struct list_head dirty_nat_entries; /* cached nat entry list (dirty) */
203 /* free node ids management */
204 struct list_head free_nid_list; /* a list for free nids */
205 spinlock_t free_nid_list_lock; /* protect free nid list */
206 unsigned int fcnt; /* the number of free node id */
207 struct mutex build_lock; /* lock for build free nids */
210 char *nat_bitmap; /* NAT bitmap pointer */
211 int bitmap_size; /* bitmap size */
215 * this structure is used as one of function parameters.
216 * all the information are dedicated to a given direct node block determined
217 * by the data offset in a file.
219 struct dnode_of_data {
220 struct inode *inode; /* vfs inode pointer */
221 struct page *inode_page; /* its inode page, NULL is possible */
222 struct page *node_page; /* cached direct node page */
223 nid_t nid; /* node id of the direct node block */
224 unsigned int ofs_in_node; /* data offset in the node page */
225 bool inode_page_locked; /* inode page is locked or not */
226 block_t data_blkaddr; /* block address of the node block */
229 static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
230 struct page *ipage, struct page *npage, nid_t nid)
232 memset(dn, 0, sizeof(*dn));
234 dn->inode_page = ipage;
235 dn->node_page = npage;
242 * By default, there are 6 active log areas across the whole main area.
243 * When considering hot and cold data separation to reduce cleaning overhead,
244 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
246 * In the current design, you should not change the numbers intentionally.
247 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
248 * logs individually according to the underlying devices. (default: 6)
249 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
250 * data and 8 for node logs.
252 #define NR_CURSEG_DATA_TYPE (3)
253 #define NR_CURSEG_NODE_TYPE (3)
254 #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
257 CURSEG_HOT_DATA = 0, /* directory entry blocks */
258 CURSEG_WARM_DATA, /* data blocks */
259 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
260 CURSEG_HOT_NODE, /* direct node blocks of directory files */
261 CURSEG_WARM_NODE, /* direct node blocks of normal files */
262 CURSEG_COLD_NODE, /* indirect node blocks */
266 struct f2fs_sm_info {
267 struct sit_info *sit_info; /* whole segment information */
268 struct free_segmap_info *free_info; /* free segment information */
269 struct dirty_seglist_info *dirty_info; /* dirty segment information */
270 struct curseg_info *curseg_array; /* active segment information */
272 struct list_head wblist_head; /* list of under-writeback pages */
273 spinlock_t wblist_lock; /* lock for checkpoint */
275 block_t seg0_blkaddr; /* block address of 0'th segment */
276 block_t main_blkaddr; /* start block address of main area */
277 block_t ssa_blkaddr; /* start block address of SSA area */
279 unsigned int segment_count; /* total # of segments */
280 unsigned int main_segments; /* # of segments in main area */
281 unsigned int reserved_segments; /* # of reserved segments */
282 unsigned int ovp_segments; /* # of overprovision segments */
286 * For directory operation
288 #define NODE_DIR1_BLOCK (ADDRS_PER_INODE + 1)
289 #define NODE_DIR2_BLOCK (ADDRS_PER_INODE + 2)
290 #define NODE_IND1_BLOCK (ADDRS_PER_INODE + 3)
291 #define NODE_IND2_BLOCK (ADDRS_PER_INODE + 4)
292 #define NODE_DIND_BLOCK (ADDRS_PER_INODE + 5)
298 * COUNT_TYPE for monitoring
300 * f2fs monitors the number of several block types such as on-writeback,
301 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
312 * FS_LOCK nesting subclasses for the lock validator:
314 * The locking order between these classes is
315 * RENAME -> DENTRY_OPS -> DATA_WRITE -> DATA_NEW
316 * -> DATA_TRUNC -> NODE_WRITE -> NODE_NEW -> NODE_TRUNC
319 RENAME, /* for renaming operations */
320 DENTRY_OPS, /* for directory operations */
321 DATA_WRITE, /* for data write */
322 DATA_NEW, /* for data allocation */
323 DATA_TRUNC, /* for data truncate */
324 NODE_NEW, /* for node allocation */
325 NODE_TRUNC, /* for node truncate */
326 NODE_WRITE, /* for node write */
331 * The below are the page types of bios used in submti_bio().
332 * The available types are:
333 * DATA User data pages. It operates as async mode.
334 * NODE Node pages. It operates as async mode.
335 * META FS metadata pages such as SIT, NAT, CP.
336 * NR_PAGE_TYPE The number of page types.
337 * META_FLUSH Make sure the previous pages are written
338 * with waiting the bio's completion
339 * ... Only can be used with META.
349 struct f2fs_sb_info {
350 struct super_block *sb; /* pointer to VFS super block */
351 struct buffer_head *raw_super_buf; /* buffer head of raw sb */
352 struct f2fs_super_block *raw_super; /* raw super block pointer */
353 int s_dirty; /* dirty flag for checkpoint */
355 /* for node-related operations */
356 struct f2fs_nm_info *nm_info; /* node manager */
357 struct inode *node_inode; /* cache node blocks */
359 /* for segment-related operations */
360 struct f2fs_sm_info *sm_info; /* segment manager */
361 struct bio *bio[NR_PAGE_TYPE]; /* bios to merge */
362 sector_t last_block_in_bio[NR_PAGE_TYPE]; /* last block number */
363 struct rw_semaphore bio_sem; /* IO semaphore */
366 struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
367 struct inode *meta_inode; /* cache meta blocks */
368 struct mutex cp_mutex; /* for checkpoint procedure */
369 struct mutex fs_lock[NR_LOCK_TYPE]; /* for blocking FS operations */
370 struct mutex write_inode; /* mutex for write inode */
371 struct mutex writepages; /* mutex for writepages() */
372 int por_doing; /* recovery is doing or not */
374 /* for orphan inode management */
375 struct list_head orphan_inode_list; /* orphan inode list */
376 struct mutex orphan_inode_mutex; /* for orphan inode list */
377 unsigned int n_orphans; /* # of orphan inodes */
379 /* for directory inode management */
380 struct list_head dir_inode_list; /* dir inode list */
381 spinlock_t dir_inode_lock; /* for dir inode list lock */
382 unsigned int n_dirty_dirs; /* # of dir inodes */
384 /* basic file system units */
385 unsigned int log_sectors_per_block; /* log2 sectors per block */
386 unsigned int log_blocksize; /* log2 block size */
387 unsigned int blocksize; /* block size */
388 unsigned int root_ino_num; /* root inode number*/
389 unsigned int node_ino_num; /* node inode number*/
390 unsigned int meta_ino_num; /* meta inode number*/
391 unsigned int log_blocks_per_seg; /* log2 blocks per segment */
392 unsigned int blocks_per_seg; /* blocks per segment */
393 unsigned int segs_per_sec; /* segments per section */
394 unsigned int secs_per_zone; /* sections per zone */
395 unsigned int total_sections; /* total section count */
396 unsigned int total_node_count; /* total node block count */
397 unsigned int total_valid_node_count; /* valid node block count */
398 unsigned int total_valid_inode_count; /* valid inode count */
399 int active_logs; /* # of active logs */
401 block_t user_block_count; /* # of user blocks */
402 block_t total_valid_block_count; /* # of valid blocks */
403 block_t alloc_valid_block_count; /* # of allocated blocks */
404 block_t last_valid_block_count; /* for recovery */
405 u32 s_next_generation; /* for NFS support */
406 atomic_t nr_pages[NR_COUNT_TYPE]; /* # of pages, see count_type */
408 struct f2fs_mount_info mount_opt; /* mount options */
410 /* for cleaning operations */
411 struct mutex gc_mutex; /* mutex for GC */
412 struct f2fs_gc_kthread *gc_thread; /* GC thread */
415 * for stat information.
416 * one is for the LFS mode, and the other is for the SSR mode.
418 struct f2fs_stat_info *stat_info; /* FS status information */
419 unsigned int segment_count[2]; /* # of allocated segments */
420 unsigned int block_count[2]; /* # of allocated blocks */
421 unsigned int last_victim[2]; /* last victim segment # */
422 int total_hit_ext, read_hit_ext; /* extent cache hit ratio */
423 int bg_gc; /* background gc calls */
424 spinlock_t stat_lock; /* lock for stat operations */
430 static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
432 return container_of(inode, struct f2fs_inode_info, vfs_inode);
435 static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
437 return sb->s_fs_info;
440 static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
442 return (struct f2fs_super_block *)(sbi->raw_super);
445 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
447 return (struct f2fs_checkpoint *)(sbi->ckpt);
450 static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
452 return (struct f2fs_nm_info *)(sbi->nm_info);
455 static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
457 return (struct f2fs_sm_info *)(sbi->sm_info);
460 static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
462 return (struct sit_info *)(SM_I(sbi)->sit_info);
465 static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
467 return (struct free_segmap_info *)(SM_I(sbi)->free_info);
470 static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
472 return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
475 static inline void F2FS_SET_SB_DIRT(struct f2fs_sb_info *sbi)
480 static inline void F2FS_RESET_SB_DIRT(struct f2fs_sb_info *sbi)
485 static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
487 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
488 return ckpt_flags & f;
491 static inline void set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
493 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
495 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
498 static inline void clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
500 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
502 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
505 static inline void mutex_lock_op(struct f2fs_sb_info *sbi, enum lock_type t)
507 mutex_lock_nested(&sbi->fs_lock[t], t);
510 static inline void mutex_unlock_op(struct f2fs_sb_info *sbi, enum lock_type t)
512 mutex_unlock(&sbi->fs_lock[t]);
516 * Check whether the given nid is within node id range.
518 static inline void check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
520 BUG_ON((nid >= NM_I(sbi)->max_nid));
523 #define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
526 * Check whether the inode has blocks or not
528 static inline int F2FS_HAS_BLOCKS(struct inode *inode)
530 if (F2FS_I(inode)->i_xattr_nid)
531 return (inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1);
533 return (inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS);
536 static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
537 struct inode *inode, blkcnt_t count)
539 block_t valid_block_count;
541 spin_lock(&sbi->stat_lock);
543 sbi->total_valid_block_count + (block_t)count;
544 if (valid_block_count > sbi->user_block_count) {
545 spin_unlock(&sbi->stat_lock);
548 inode->i_blocks += count;
549 sbi->total_valid_block_count = valid_block_count;
550 sbi->alloc_valid_block_count += (block_t)count;
551 spin_unlock(&sbi->stat_lock);
555 static inline int dec_valid_block_count(struct f2fs_sb_info *sbi,
559 spin_lock(&sbi->stat_lock);
560 BUG_ON(sbi->total_valid_block_count < (block_t) count);
561 BUG_ON(inode->i_blocks < count);
562 inode->i_blocks -= count;
563 sbi->total_valid_block_count -= (block_t)count;
564 spin_unlock(&sbi->stat_lock);
568 static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
570 atomic_inc(&sbi->nr_pages[count_type]);
571 F2FS_SET_SB_DIRT(sbi);
574 static inline void inode_inc_dirty_dents(struct inode *inode)
576 atomic_inc(&F2FS_I(inode)->dirty_dents);
579 static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
581 atomic_dec(&sbi->nr_pages[count_type]);
584 static inline void inode_dec_dirty_dents(struct inode *inode)
586 atomic_dec(&F2FS_I(inode)->dirty_dents);
589 static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
591 return atomic_read(&sbi->nr_pages[count_type]);
594 static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
596 unsigned int pages_per_sec = sbi->segs_per_sec *
597 (1 << sbi->log_blocks_per_seg);
598 return ((get_pages(sbi, block_type) + pages_per_sec - 1)
599 >> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
602 static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
605 spin_lock(&sbi->stat_lock);
606 ret = sbi->total_valid_block_count;
607 spin_unlock(&sbi->stat_lock);
611 static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
613 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
615 /* return NAT or SIT bitmap */
616 if (flag == NAT_BITMAP)
617 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
618 else if (flag == SIT_BITMAP)
619 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
624 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
626 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
627 int offset = (flag == NAT_BITMAP) ?
628 le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
629 return &ckpt->sit_nat_version_bitmap + offset;
632 static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
635 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
636 unsigned long long ckpt_version = le64_to_cpu(ckpt->checkpoint_ver);
638 start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
641 * odd numbered checkpoint should at cp segment 0
642 * and even segent must be at cp segment 1
644 if (!(ckpt_version & 1))
645 start_addr += sbi->blocks_per_seg;
650 static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
652 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
655 static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
659 block_t valid_block_count;
660 unsigned int valid_node_count;
662 spin_lock(&sbi->stat_lock);
664 valid_block_count = sbi->total_valid_block_count + (block_t)count;
665 sbi->alloc_valid_block_count += (block_t)count;
666 valid_node_count = sbi->total_valid_node_count + count;
668 if (valid_block_count > sbi->user_block_count) {
669 spin_unlock(&sbi->stat_lock);
673 if (valid_node_count > sbi->total_node_count) {
674 spin_unlock(&sbi->stat_lock);
679 inode->i_blocks += count;
680 sbi->total_valid_node_count = valid_node_count;
681 sbi->total_valid_block_count = valid_block_count;
682 spin_unlock(&sbi->stat_lock);
687 static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
691 spin_lock(&sbi->stat_lock);
693 BUG_ON(sbi->total_valid_block_count < count);
694 BUG_ON(sbi->total_valid_node_count < count);
695 BUG_ON(inode->i_blocks < count);
697 inode->i_blocks -= count;
698 sbi->total_valid_node_count -= count;
699 sbi->total_valid_block_count -= (block_t)count;
701 spin_unlock(&sbi->stat_lock);
704 static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
707 spin_lock(&sbi->stat_lock);
708 ret = sbi->total_valid_node_count;
709 spin_unlock(&sbi->stat_lock);
713 static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
715 spin_lock(&sbi->stat_lock);
716 BUG_ON(sbi->total_valid_inode_count == sbi->total_node_count);
717 sbi->total_valid_inode_count++;
718 spin_unlock(&sbi->stat_lock);
721 static inline int dec_valid_inode_count(struct f2fs_sb_info *sbi)
723 spin_lock(&sbi->stat_lock);
724 BUG_ON(!sbi->total_valid_inode_count);
725 sbi->total_valid_inode_count--;
726 spin_unlock(&sbi->stat_lock);
730 static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
733 spin_lock(&sbi->stat_lock);
734 ret = sbi->total_valid_inode_count;
735 spin_unlock(&sbi->stat_lock);
739 static inline void f2fs_put_page(struct page *page, int unlock)
741 if (!page || IS_ERR(page))
745 BUG_ON(!PageLocked(page));
748 page_cache_release(page);
751 static inline void f2fs_put_dnode(struct dnode_of_data *dn)
754 f2fs_put_page(dn->node_page, 1);
755 if (dn->inode_page && dn->node_page != dn->inode_page)
756 f2fs_put_page(dn->inode_page, 0);
757 dn->node_page = NULL;
758 dn->inode_page = NULL;
761 static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
762 size_t size, void (*ctor)(void *))
764 return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, ctor);
767 #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
769 static inline bool IS_INODE(struct page *page)
771 struct f2fs_node *p = (struct f2fs_node *)page_address(page);
772 return RAW_IS_INODE(p);
775 static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
777 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
780 static inline block_t datablock_addr(struct page *node_page,
783 struct f2fs_node *raw_node;
785 raw_node = (struct f2fs_node *)page_address(node_page);
786 addr_array = blkaddr_in_node(raw_node);
787 return le32_to_cpu(addr_array[offset]);
790 static inline int f2fs_test_bit(unsigned int nr, char *addr)
795 mask = 1 << (7 - (nr & 0x07));
799 static inline int f2fs_set_bit(unsigned int nr, char *addr)
805 mask = 1 << (7 - (nr & 0x07));
811 static inline int f2fs_clear_bit(unsigned int nr, char *addr)
817 mask = 1 << (7 - (nr & 0x07));
823 /* used for f2fs_inode_info->flags */
825 FI_NEW_INODE, /* indicate newly allocated inode */
826 FI_NEED_CP, /* need to do checkpoint during fsync */
827 FI_INC_LINK, /* need to increment i_nlink */
828 FI_ACL_MODE, /* indicate acl mode */
829 FI_NO_ALLOC, /* should not allocate any blocks */
832 static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
834 set_bit(flag, &fi->flags);
837 static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
839 return test_bit(flag, &fi->flags);
842 static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
844 clear_bit(flag, &fi->flags);
847 static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
849 fi->i_acl_mode = mode;
850 set_inode_flag(fi, FI_ACL_MODE);
853 static inline int cond_clear_inode_flag(struct f2fs_inode_info *fi, int flag)
855 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
856 clear_inode_flag(fi, FI_ACL_MODE);
865 int f2fs_sync_file(struct file *, loff_t, loff_t, int);
866 void truncate_data_blocks(struct dnode_of_data *);
867 void f2fs_truncate(struct inode *);
868 int f2fs_setattr(struct dentry *, struct iattr *);
869 int truncate_hole(struct inode *, pgoff_t, pgoff_t);
870 long f2fs_ioctl(struct file *, unsigned int, unsigned long);
871 long f2fs_compat_ioctl(struct file *, unsigned int, unsigned long);
876 void f2fs_set_inode_flags(struct inode *);
877 struct inode *f2fs_iget(struct super_block *, unsigned long);
878 void update_inode(struct inode *, struct page *);
879 int f2fs_write_inode(struct inode *, struct writeback_control *);
880 void f2fs_evict_inode(struct inode *);
885 struct dentry *f2fs_get_parent(struct dentry *child);
890 struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
892 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
893 ino_t f2fs_inode_by_name(struct inode *, struct qstr *);
894 void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
895 struct page *, struct inode *);
896 void init_dent_inode(const struct qstr *, struct page *);
897 int __f2fs_add_link(struct inode *, const struct qstr *, struct inode *);
898 void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *);
899 int f2fs_make_empty(struct inode *, struct inode *);
900 bool f2fs_empty_dir(struct inode *);
902 static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
904 return __f2fs_add_link(dentry->d_parent->d_inode, &dentry->d_name,
911 int f2fs_sync_fs(struct super_block *, int);
912 extern __printf(3, 4)
913 void f2fs_msg(struct super_block *, const char *, const char *, ...);
918 f2fs_hash_t f2fs_dentry_hash(const char *, size_t);
923 struct dnode_of_data;
926 int is_checkpointed_node(struct f2fs_sb_info *, nid_t);
927 void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
928 int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
929 int truncate_inode_blocks(struct inode *, pgoff_t);
930 int remove_inode_page(struct inode *);
931 int new_inode_page(struct inode *, const struct qstr *);
932 struct page *new_node_page(struct dnode_of_data *, unsigned int);
933 void ra_node_page(struct f2fs_sb_info *, nid_t);
934 struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
935 struct page *get_node_page_ra(struct page *, int);
936 void sync_inode_page(struct dnode_of_data *);
937 int sync_node_pages(struct f2fs_sb_info *, nid_t, struct writeback_control *);
938 bool alloc_nid(struct f2fs_sb_info *, nid_t *);
939 void alloc_nid_done(struct f2fs_sb_info *, nid_t);
940 void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
941 void recover_node_page(struct f2fs_sb_info *, struct page *,
942 struct f2fs_summary *, struct node_info *, block_t);
943 int recover_inode_page(struct f2fs_sb_info *, struct page *);
944 int restore_node_summary(struct f2fs_sb_info *, unsigned int,
945 struct f2fs_summary_block *);
946 void flush_nat_entries(struct f2fs_sb_info *);
947 int build_node_manager(struct f2fs_sb_info *);
948 void destroy_node_manager(struct f2fs_sb_info *);
949 int __init create_node_manager_caches(void);
950 void destroy_node_manager_caches(void);
955 void f2fs_balance_fs(struct f2fs_sb_info *);
956 void invalidate_blocks(struct f2fs_sb_info *, block_t);
957 void locate_dirty_segment(struct f2fs_sb_info *, unsigned int);
958 void clear_prefree_segments(struct f2fs_sb_info *);
959 int npages_for_summary_flush(struct f2fs_sb_info *);
960 void allocate_new_segments(struct f2fs_sb_info *);
961 struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
962 struct bio *f2fs_bio_alloc(struct block_device *, int);
963 void f2fs_submit_bio(struct f2fs_sb_info *, enum page_type, bool sync);
964 void write_meta_page(struct f2fs_sb_info *, struct page *);
965 void write_node_page(struct f2fs_sb_info *, struct page *, unsigned int,
967 void write_data_page(struct inode *, struct page *, struct dnode_of_data*,
969 void rewrite_data_page(struct f2fs_sb_info *, struct page *, block_t);
970 void recover_data_page(struct f2fs_sb_info *, struct page *,
971 struct f2fs_summary *, block_t, block_t);
972 void rewrite_node_page(struct f2fs_sb_info *, struct page *,
973 struct f2fs_summary *, block_t, block_t);
974 void write_data_summaries(struct f2fs_sb_info *, block_t);
975 void write_node_summaries(struct f2fs_sb_info *, block_t);
976 int lookup_journal_in_cursum(struct f2fs_summary_block *,
977 int, unsigned int, int);
978 void flush_sit_entries(struct f2fs_sb_info *);
979 int build_segment_manager(struct f2fs_sb_info *);
980 void reset_victim_segmap(struct f2fs_sb_info *);
981 void destroy_segment_manager(struct f2fs_sb_info *);
986 struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
987 struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
988 long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
989 int check_orphan_space(struct f2fs_sb_info *);
990 void add_orphan_inode(struct f2fs_sb_info *, nid_t);
991 void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
992 int recover_orphan_inodes(struct f2fs_sb_info *);
993 int get_valid_checkpoint(struct f2fs_sb_info *);
994 void set_dirty_dir_page(struct inode *, struct page *);
995 void remove_dirty_dir_inode(struct inode *);
996 void sync_dirty_dir_inodes(struct f2fs_sb_info *);
997 void write_checkpoint(struct f2fs_sb_info *, bool);
998 void init_orphan_info(struct f2fs_sb_info *);
999 int __init create_checkpoint_caches(void);
1000 void destroy_checkpoint_caches(void);
1005 int reserve_new_block(struct dnode_of_data *);
1006 void update_extent_cache(block_t, struct dnode_of_data *);
1007 struct page *find_data_page(struct inode *, pgoff_t);
1008 struct page *get_lock_data_page(struct inode *, pgoff_t);
1009 struct page *get_new_data_page(struct inode *, pgoff_t, bool);
1010 int f2fs_readpage(struct f2fs_sb_info *, struct page *, block_t, int);
1011 int do_write_data_page(struct page *);
1016 int start_gc_thread(struct f2fs_sb_info *);
1017 void stop_gc_thread(struct f2fs_sb_info *);
1018 block_t start_bidx_of_node(unsigned int);
1019 int f2fs_gc(struct f2fs_sb_info *);
1020 void build_gc_manager(struct f2fs_sb_info *);
1021 int __init create_gc_caches(void);
1022 void destroy_gc_caches(void);
1027 void recover_fsync_data(struct f2fs_sb_info *);
1028 bool space_for_roll_forward(struct f2fs_sb_info *);
1033 #ifdef CONFIG_F2FS_STAT_FS
1034 struct f2fs_stat_info {
1035 struct list_head stat_list;
1036 struct f2fs_sb_info *sbi;
1037 struct mutex stat_lock;
1038 int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
1039 int main_area_segs, main_area_sections, main_area_zones;
1040 int hit_ext, total_ext;
1041 int ndirty_node, ndirty_dent, ndirty_dirs, ndirty_meta;
1042 int nats, sits, fnids;
1043 int total_count, utilization;
1045 unsigned int valid_count, valid_node_count, valid_inode_count;
1046 unsigned int bimodal, avg_vblocks;
1047 int util_free, util_valid, util_invalid;
1048 int rsvd_segs, overp_segs;
1049 int dirty_count, node_pages, meta_pages;
1050 int prefree_count, call_count;
1051 int tot_segs, node_segs, data_segs, free_segs, free_secs;
1052 int tot_blks, data_blks, node_blks;
1053 int curseg[NR_CURSEG_TYPE];
1054 int cursec[NR_CURSEG_TYPE];
1055 int curzone[NR_CURSEG_TYPE];
1057 unsigned int segment_count[2];
1058 unsigned int block_count[2];
1059 unsigned base_mem, cache_mem;
1062 #define stat_inc_call_count(si) ((si)->call_count++)
1064 #define stat_inc_seg_count(sbi, type) \
1066 struct f2fs_stat_info *si = sbi->stat_info; \
1068 if (type == SUM_TYPE_DATA) \
1074 #define stat_inc_tot_blk_count(si, blks) \
1075 (si->tot_blks += (blks))
1077 #define stat_inc_data_blk_count(sbi, blks) \
1079 struct f2fs_stat_info *si = sbi->stat_info; \
1080 stat_inc_tot_blk_count(si, blks); \
1081 si->data_blks += (blks); \
1084 #define stat_inc_node_blk_count(sbi, blks) \
1086 struct f2fs_stat_info *si = sbi->stat_info; \
1087 stat_inc_tot_blk_count(si, blks); \
1088 si->node_blks += (blks); \
1091 int f2fs_build_stats(struct f2fs_sb_info *);
1092 void f2fs_destroy_stats(struct f2fs_sb_info *);
1093 void __init f2fs_create_root_stats(void);
1094 void f2fs_destroy_root_stats(void);
1096 #define stat_inc_call_count(si)
1097 #define stat_inc_seg_count(si, type)
1098 #define stat_inc_tot_blk_count(si, blks)
1099 #define stat_inc_data_blk_count(si, blks)
1100 #define stat_inc_node_blk_count(sbi, blks)
1102 static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
1103 static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
1104 static inline void __init f2fs_create_root_stats(void) { }
1105 static inline void f2fs_destroy_root_stats(void) { }
1108 extern const struct file_operations f2fs_dir_operations;
1109 extern const struct file_operations f2fs_file_operations;
1110 extern const struct inode_operations f2fs_file_inode_operations;
1111 extern const struct address_space_operations f2fs_dblock_aops;
1112 extern const struct address_space_operations f2fs_node_aops;
1113 extern const struct address_space_operations f2fs_meta_aops;
1114 extern const struct inode_operations f2fs_dir_inode_operations;
1115 extern const struct inode_operations f2fs_symlink_inode_operations;
1116 extern const struct inode_operations f2fs_special_inode_operations;