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>
21 #include <linux/wait.h>
22 #include <linux/sched.h>
27 #define F2FS_MOUNT_BG_GC 0x00000001
28 #define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
29 #define F2FS_MOUNT_DISCARD 0x00000004
30 #define F2FS_MOUNT_NOHEAP 0x00000008
31 #define F2FS_MOUNT_XATTR_USER 0x00000010
32 #define F2FS_MOUNT_POSIX_ACL 0x00000020
33 #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
34 #define F2FS_MOUNT_INLINE_XATTR 0x00000080
36 #define clear_opt(sbi, option) (sbi->mount_opt.opt &= ~F2FS_MOUNT_##option)
37 #define set_opt(sbi, option) (sbi->mount_opt.opt |= F2FS_MOUNT_##option)
38 #define test_opt(sbi, option) (sbi->mount_opt.opt & F2FS_MOUNT_##option)
40 #define ver_after(a, b) (typecheck(unsigned long long, a) && \
41 typecheck(unsigned long long, b) && \
42 ((long long)((a) - (b)) > 0))
44 typedef u32 block_t
; /*
45 * should not change u32, since it is the on-disk block
46 * address format, __le32.
50 struct f2fs_mount_info
{
54 #define CRCPOLY_LE 0xedb88320
56 static inline __u32
f2fs_crc32(void *buf
, size_t len
)
58 unsigned char *p
= (unsigned char *)buf
;
59 __u32 crc
= F2FS_SUPER_MAGIC
;
64 for (i
= 0; i
< 8; i
++)
65 crc
= (crc
>> 1) ^ ((crc
& 1) ? CRCPOLY_LE
: 0);
70 static inline bool f2fs_crc_valid(__u32 blk_crc
, void *buf
, size_t buf_size
)
72 return f2fs_crc32(buf
, buf_size
) == blk_crc
;
76 * For checkpoint manager
83 /* for the list of orphan inodes */
84 struct orphan_inode_entry
{
85 struct list_head list
; /* list head */
86 nid_t ino
; /* inode number */
89 /* for the list of directory inodes */
90 struct dir_inode_entry
{
91 struct list_head list
; /* list head */
92 struct inode
*inode
; /* vfs inode pointer */
95 /* for the list of fsync inodes, used only during recovery */
96 struct fsync_inode_entry
{
97 struct list_head list
; /* list head */
98 struct inode
*inode
; /* vfs inode pointer */
99 block_t blkaddr
; /* block address locating the last inode */
102 #define nats_in_cursum(sum) (le16_to_cpu(sum->n_nats))
103 #define sits_in_cursum(sum) (le16_to_cpu(sum->n_sits))
105 #define nat_in_journal(sum, i) (sum->nat_j.entries[i].ne)
106 #define nid_in_journal(sum, i) (sum->nat_j.entries[i].nid)
107 #define sit_in_journal(sum, i) (sum->sit_j.entries[i].se)
108 #define segno_in_journal(sum, i) (sum->sit_j.entries[i].segno)
110 static inline int update_nats_in_cursum(struct f2fs_summary_block
*rs
, int i
)
112 int before
= nats_in_cursum(rs
);
113 rs
->n_nats
= cpu_to_le16(before
+ i
);
117 static inline int update_sits_in_cursum(struct f2fs_summary_block
*rs
, int i
)
119 int before
= sits_in_cursum(rs
);
120 rs
->n_sits
= cpu_to_le16(before
+ i
);
127 #define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
128 #define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
130 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
132 * ioctl commands in 32 bit emulation
134 #define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
135 #define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
139 * For INODE and NODE manager
142 * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
143 * as its node offset to distinguish from index node blocks.
144 * But some bits are used to mark the node block.
146 #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
149 ALLOC_NODE
, /* allocate a new node page if needed */
150 LOOKUP_NODE
, /* look up a node without readahead */
152 * look up a node with readahead called
153 * by get_datablock_ro.
157 #define F2FS_LINK_MAX 32000 /* maximum link count per file */
159 /* for in-memory extent cache entry */
161 rwlock_t ext_lock
; /* rwlock for consistency */
162 unsigned int fofs
; /* start offset in a file */
163 u32 blk_addr
; /* start block address of the extent */
164 unsigned int len
; /* length of the extent */
168 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
170 #define FADVISE_COLD_BIT 0x01
171 #define FADVISE_LOST_PINO_BIT 0x02
173 struct f2fs_inode_info
{
174 struct inode vfs_inode
; /* serve a vfs inode */
175 unsigned long i_flags
; /* keep an inode flags for ioctl */
176 unsigned char i_advise
; /* use to give file attribute hints */
177 unsigned int i_current_depth
; /* use only in directory structure */
178 unsigned int i_pino
; /* parent inode number */
179 umode_t i_acl_mode
; /* keep file acl mode temporarily */
181 /* Use below internally in f2fs*/
182 unsigned long flags
; /* use to pass per-file flags */
183 atomic_t dirty_dents
; /* # of dirty dentry pages */
184 f2fs_hash_t chash
; /* hash value of given file name */
185 unsigned int clevel
; /* maximum level of given file name */
186 nid_t i_xattr_nid
; /* node id that contains xattrs */
187 unsigned long long xattr_ver
; /* cp version of xattr modification */
188 struct extent_info ext
; /* in-memory extent cache entry */
191 static inline void get_extent_info(struct extent_info
*ext
,
192 struct f2fs_extent i_ext
)
194 write_lock(&ext
->ext_lock
);
195 ext
->fofs
= le32_to_cpu(i_ext
.fofs
);
196 ext
->blk_addr
= le32_to_cpu(i_ext
.blk_addr
);
197 ext
->len
= le32_to_cpu(i_ext
.len
);
198 write_unlock(&ext
->ext_lock
);
201 static inline void set_raw_extent(struct extent_info
*ext
,
202 struct f2fs_extent
*i_ext
)
204 read_lock(&ext
->ext_lock
);
205 i_ext
->fofs
= cpu_to_le32(ext
->fofs
);
206 i_ext
->blk_addr
= cpu_to_le32(ext
->blk_addr
);
207 i_ext
->len
= cpu_to_le32(ext
->len
);
208 read_unlock(&ext
->ext_lock
);
211 struct f2fs_nm_info
{
212 block_t nat_blkaddr
; /* base disk address of NAT */
213 nid_t max_nid
; /* maximum possible node ids */
214 nid_t next_scan_nid
; /* the next nid to be scanned */
216 /* NAT cache management */
217 struct radix_tree_root nat_root
;/* root of the nat entry cache */
218 rwlock_t nat_tree_lock
; /* protect nat_tree_lock */
219 unsigned int nat_cnt
; /* the # of cached nat entries */
220 struct list_head nat_entries
; /* cached nat entry list (clean) */
221 struct list_head dirty_nat_entries
; /* cached nat entry list (dirty) */
223 /* free node ids management */
224 struct list_head free_nid_list
; /* a list for free nids */
225 spinlock_t free_nid_list_lock
; /* protect free nid list */
226 unsigned int fcnt
; /* the number of free node id */
227 struct mutex build_lock
; /* lock for build free nids */
230 char *nat_bitmap
; /* NAT bitmap pointer */
231 int bitmap_size
; /* bitmap size */
235 * this structure is used as one of function parameters.
236 * all the information are dedicated to a given direct node block determined
237 * by the data offset in a file.
239 struct dnode_of_data
{
240 struct inode
*inode
; /* vfs inode pointer */
241 struct page
*inode_page
; /* its inode page, NULL is possible */
242 struct page
*node_page
; /* cached direct node page */
243 nid_t nid
; /* node id of the direct node block */
244 unsigned int ofs_in_node
; /* data offset in the node page */
245 bool inode_page_locked
; /* inode page is locked or not */
246 block_t data_blkaddr
; /* block address of the node block */
249 static inline void set_new_dnode(struct dnode_of_data
*dn
, struct inode
*inode
,
250 struct page
*ipage
, struct page
*npage
, nid_t nid
)
252 memset(dn
, 0, sizeof(*dn
));
254 dn
->inode_page
= ipage
;
255 dn
->node_page
= npage
;
262 * By default, there are 6 active log areas across the whole main area.
263 * When considering hot and cold data separation to reduce cleaning overhead,
264 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
266 * In the current design, you should not change the numbers intentionally.
267 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
268 * logs individually according to the underlying devices. (default: 6)
269 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
270 * data and 8 for node logs.
272 #define NR_CURSEG_DATA_TYPE (3)
273 #define NR_CURSEG_NODE_TYPE (3)
274 #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
277 CURSEG_HOT_DATA
= 0, /* directory entry blocks */
278 CURSEG_WARM_DATA
, /* data blocks */
279 CURSEG_COLD_DATA
, /* multimedia or GCed data blocks */
280 CURSEG_HOT_NODE
, /* direct node blocks of directory files */
281 CURSEG_WARM_NODE
, /* direct node blocks of normal files */
282 CURSEG_COLD_NODE
, /* indirect node blocks */
286 struct f2fs_sm_info
{
287 struct sit_info
*sit_info
; /* whole segment information */
288 struct free_segmap_info
*free_info
; /* free segment information */
289 struct dirty_seglist_info
*dirty_info
; /* dirty segment information */
290 struct curseg_info
*curseg_array
; /* active segment information */
292 struct list_head wblist_head
; /* list of under-writeback pages */
293 spinlock_t wblist_lock
; /* lock for checkpoint */
295 block_t seg0_blkaddr
; /* block address of 0'th segment */
296 block_t main_blkaddr
; /* start block address of main area */
297 block_t ssa_blkaddr
; /* start block address of SSA area */
299 unsigned int segment_count
; /* total # of segments */
300 unsigned int main_segments
; /* # of segments in main area */
301 unsigned int reserved_segments
; /* # of reserved segments */
302 unsigned int ovp_segments
; /* # of overprovision segments */
309 * COUNT_TYPE for monitoring
311 * f2fs monitors the number of several block types such as on-writeback,
312 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
323 * The below are the page types of bios used in submti_bio().
324 * The available types are:
325 * DATA User data pages. It operates as async mode.
326 * NODE Node pages. It operates as async mode.
327 * META FS metadata pages such as SIT, NAT, CP.
328 * NR_PAGE_TYPE The number of page types.
329 * META_FLUSH Make sure the previous pages are written
330 * with waiting the bio's completion
331 * ... Only can be used with META.
341 struct f2fs_sb_info
{
342 struct super_block
*sb
; /* pointer to VFS super block */
343 struct proc_dir_entry
*s_proc
; /* proc entry */
344 struct buffer_head
*raw_super_buf
; /* buffer head of raw sb */
345 struct f2fs_super_block
*raw_super
; /* raw super block pointer */
346 int s_dirty
; /* dirty flag for checkpoint */
348 /* for node-related operations */
349 struct f2fs_nm_info
*nm_info
; /* node manager */
350 struct inode
*node_inode
; /* cache node blocks */
352 /* for segment-related operations */
353 struct f2fs_sm_info
*sm_info
; /* segment manager */
354 struct bio
*bio
[NR_PAGE_TYPE
]; /* bios to merge */
355 sector_t last_block_in_bio
[NR_PAGE_TYPE
]; /* last block number */
356 struct rw_semaphore bio_sem
; /* IO semaphore */
359 struct f2fs_checkpoint
*ckpt
; /* raw checkpoint pointer */
360 struct inode
*meta_inode
; /* cache meta blocks */
361 struct mutex cp_mutex
; /* checkpoint procedure lock */
362 struct rw_semaphore cp_rwsem
; /* blocking FS operations */
363 wait_queue_head_t cp_wait
; /* checkpoint wait queue */
364 struct mutex node_write
; /* locking node writes */
365 struct mutex writepages
; /* mutex for writepages() */
366 int por_doing
; /* recovery is doing or not */
367 int on_build_free_nids
; /* build_free_nids is doing */
369 /* for orphan inode management */
370 struct list_head orphan_inode_list
; /* orphan inode list */
371 struct mutex orphan_inode_mutex
; /* for orphan inode list */
372 unsigned int n_orphans
; /* # of orphan inodes */
374 /* for directory inode management */
375 struct list_head dir_inode_list
; /* dir inode list */
376 spinlock_t dir_inode_lock
; /* for dir inode list lock */
378 /* basic file system units */
379 unsigned int log_sectors_per_block
; /* log2 sectors per block */
380 unsigned int log_blocksize
; /* log2 block size */
381 unsigned int blocksize
; /* block size */
382 unsigned int root_ino_num
; /* root inode number*/
383 unsigned int node_ino_num
; /* node inode number*/
384 unsigned int meta_ino_num
; /* meta inode number*/
385 unsigned int log_blocks_per_seg
; /* log2 blocks per segment */
386 unsigned int blocks_per_seg
; /* blocks per segment */
387 unsigned int segs_per_sec
; /* segments per section */
388 unsigned int secs_per_zone
; /* sections per zone */
389 unsigned int total_sections
; /* total section count */
390 unsigned int total_node_count
; /* total node block count */
391 unsigned int total_valid_node_count
; /* valid node block count */
392 unsigned int total_valid_inode_count
; /* valid inode count */
393 int active_logs
; /* # of active logs */
395 block_t user_block_count
; /* # of user blocks */
396 block_t total_valid_block_count
; /* # of valid blocks */
397 block_t alloc_valid_block_count
; /* # of allocated blocks */
398 block_t last_valid_block_count
; /* for recovery */
399 u32 s_next_generation
; /* for NFS support */
400 atomic_t nr_pages
[NR_COUNT_TYPE
]; /* # of pages, see count_type */
402 struct f2fs_mount_info mount_opt
; /* mount options */
404 /* for cleaning operations */
405 struct mutex gc_mutex
; /* mutex for GC */
406 struct f2fs_gc_kthread
*gc_thread
; /* GC thread */
407 unsigned int cur_victim_sec
; /* current victim section num */
410 * for stat information.
411 * one is for the LFS mode, and the other is for the SSR mode.
413 #ifdef CONFIG_F2FS_STAT_FS
414 struct f2fs_stat_info
*stat_info
; /* FS status information */
415 unsigned int segment_count
[2]; /* # of allocated segments */
416 unsigned int block_count
[2]; /* # of allocated blocks */
417 int total_hit_ext
, read_hit_ext
; /* extent cache hit ratio */
418 int bg_gc
; /* background gc calls */
419 unsigned int n_dirty_dirs
; /* # of dir inodes */
421 unsigned int last_victim
[2]; /* last victim segment # */
422 spinlock_t stat_lock
; /* lock for stat operations */
424 /* For sysfs suppport */
425 struct kobject s_kobj
;
426 struct completion s_kobj_unregister
;
432 static inline struct f2fs_inode_info
*F2FS_I(struct inode
*inode
)
434 return container_of(inode
, struct f2fs_inode_info
, vfs_inode
);
437 static inline struct f2fs_sb_info
*F2FS_SB(struct super_block
*sb
)
439 return sb
->s_fs_info
;
442 static inline struct f2fs_super_block
*F2FS_RAW_SUPER(struct f2fs_sb_info
*sbi
)
444 return (struct f2fs_super_block
*)(sbi
->raw_super
);
447 static inline struct f2fs_checkpoint
*F2FS_CKPT(struct f2fs_sb_info
*sbi
)
449 return (struct f2fs_checkpoint
*)(sbi
->ckpt
);
452 static inline struct f2fs_node
*F2FS_NODE(struct page
*page
)
454 return (struct f2fs_node
*)page_address(page
);
457 static inline struct f2fs_nm_info
*NM_I(struct f2fs_sb_info
*sbi
)
459 return (struct f2fs_nm_info
*)(sbi
->nm_info
);
462 static inline struct f2fs_sm_info
*SM_I(struct f2fs_sb_info
*sbi
)
464 return (struct f2fs_sm_info
*)(sbi
->sm_info
);
467 static inline struct sit_info
*SIT_I(struct f2fs_sb_info
*sbi
)
469 return (struct sit_info
*)(SM_I(sbi
)->sit_info
);
472 static inline struct free_segmap_info
*FREE_I(struct f2fs_sb_info
*sbi
)
474 return (struct free_segmap_info
*)(SM_I(sbi
)->free_info
);
477 static inline struct dirty_seglist_info
*DIRTY_I(struct f2fs_sb_info
*sbi
)
479 return (struct dirty_seglist_info
*)(SM_I(sbi
)->dirty_info
);
482 static inline void F2FS_SET_SB_DIRT(struct f2fs_sb_info
*sbi
)
487 static inline void F2FS_RESET_SB_DIRT(struct f2fs_sb_info
*sbi
)
492 static inline unsigned long long cur_cp_version(struct f2fs_checkpoint
*cp
)
494 return le64_to_cpu(cp
->checkpoint_ver
);
497 static inline bool is_set_ckpt_flags(struct f2fs_checkpoint
*cp
, unsigned int f
)
499 unsigned int ckpt_flags
= le32_to_cpu(cp
->ckpt_flags
);
500 return ckpt_flags
& f
;
503 static inline void set_ckpt_flags(struct f2fs_checkpoint
*cp
, unsigned int f
)
505 unsigned int ckpt_flags
= le32_to_cpu(cp
->ckpt_flags
);
507 cp
->ckpt_flags
= cpu_to_le32(ckpt_flags
);
510 static inline void clear_ckpt_flags(struct f2fs_checkpoint
*cp
, unsigned int f
)
512 unsigned int ckpt_flags
= le32_to_cpu(cp
->ckpt_flags
);
514 cp
->ckpt_flags
= cpu_to_le32(ckpt_flags
);
517 static inline void f2fs_lock_op(struct f2fs_sb_info
*sbi
)
520 * If the checkpoint thread is waiting for cp_rwsem, add cuurent task
521 * into wait list to avoid the checkpoint thread starvation
523 while (!list_empty(&sbi
->cp_rwsem
.wait_list
))
524 wait_event_interruptible(sbi
->cp_wait
,
525 list_empty(&sbi
->cp_rwsem
.wait_list
));
526 down_read(&sbi
->cp_rwsem
);
529 static inline void f2fs_unlock_op(struct f2fs_sb_info
*sbi
)
531 up_read(&sbi
->cp_rwsem
);
534 static inline void f2fs_lock_all(struct f2fs_sb_info
*sbi
)
536 down_write_nest_lock(&sbi
->cp_rwsem
, &sbi
->cp_mutex
);
539 static inline void f2fs_unlock_all(struct f2fs_sb_info
*sbi
)
541 up_write(&sbi
->cp_rwsem
);
543 /* wake up all tasks blocked by checkpoint */
544 wake_up_all(&sbi
->cp_wait
);
548 * Check whether the given nid is within node id range.
550 static inline int check_nid_range(struct f2fs_sb_info
*sbi
, nid_t nid
)
552 WARN_ON((nid
>= NM_I(sbi
)->max_nid
));
553 if (nid
>= NM_I(sbi
)->max_nid
)
558 #define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
561 * Check whether the inode has blocks or not
563 static inline int F2FS_HAS_BLOCKS(struct inode
*inode
)
565 if (F2FS_I(inode
)->i_xattr_nid
)
566 return (inode
->i_blocks
> F2FS_DEFAULT_ALLOCATED_BLOCKS
+ 1);
568 return (inode
->i_blocks
> F2FS_DEFAULT_ALLOCATED_BLOCKS
);
571 static inline bool inc_valid_block_count(struct f2fs_sb_info
*sbi
,
572 struct inode
*inode
, blkcnt_t count
)
574 block_t valid_block_count
;
576 spin_lock(&sbi
->stat_lock
);
578 sbi
->total_valid_block_count
+ (block_t
)count
;
579 if (valid_block_count
> sbi
->user_block_count
) {
580 spin_unlock(&sbi
->stat_lock
);
583 inode
->i_blocks
+= count
;
584 sbi
->total_valid_block_count
= valid_block_count
;
585 sbi
->alloc_valid_block_count
+= (block_t
)count
;
586 spin_unlock(&sbi
->stat_lock
);
590 static inline int dec_valid_block_count(struct f2fs_sb_info
*sbi
,
594 spin_lock(&sbi
->stat_lock
);
595 BUG_ON(sbi
->total_valid_block_count
< (block_t
) count
);
596 BUG_ON(inode
->i_blocks
< count
);
597 inode
->i_blocks
-= count
;
598 sbi
->total_valid_block_count
-= (block_t
)count
;
599 spin_unlock(&sbi
->stat_lock
);
603 static inline void inc_page_count(struct f2fs_sb_info
*sbi
, int count_type
)
605 atomic_inc(&sbi
->nr_pages
[count_type
]);
606 F2FS_SET_SB_DIRT(sbi
);
609 static inline void inode_inc_dirty_dents(struct inode
*inode
)
611 atomic_inc(&F2FS_I(inode
)->dirty_dents
);
614 static inline void dec_page_count(struct f2fs_sb_info
*sbi
, int count_type
)
616 atomic_dec(&sbi
->nr_pages
[count_type
]);
619 static inline void inode_dec_dirty_dents(struct inode
*inode
)
621 atomic_dec(&F2FS_I(inode
)->dirty_dents
);
624 static inline int get_pages(struct f2fs_sb_info
*sbi
, int count_type
)
626 return atomic_read(&sbi
->nr_pages
[count_type
]);
629 static inline int get_blocktype_secs(struct f2fs_sb_info
*sbi
, int block_type
)
631 unsigned int pages_per_sec
= sbi
->segs_per_sec
*
632 (1 << sbi
->log_blocks_per_seg
);
633 return ((get_pages(sbi
, block_type
) + pages_per_sec
- 1)
634 >> sbi
->log_blocks_per_seg
) / sbi
->segs_per_sec
;
637 static inline block_t
valid_user_blocks(struct f2fs_sb_info
*sbi
)
640 spin_lock(&sbi
->stat_lock
);
641 ret
= sbi
->total_valid_block_count
;
642 spin_unlock(&sbi
->stat_lock
);
646 static inline unsigned long __bitmap_size(struct f2fs_sb_info
*sbi
, int flag
)
648 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
650 /* return NAT or SIT bitmap */
651 if (flag
== NAT_BITMAP
)
652 return le32_to_cpu(ckpt
->nat_ver_bitmap_bytesize
);
653 else if (flag
== SIT_BITMAP
)
654 return le32_to_cpu(ckpt
->sit_ver_bitmap_bytesize
);
659 static inline void *__bitmap_ptr(struct f2fs_sb_info
*sbi
, int flag
)
661 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
662 int offset
= (flag
== NAT_BITMAP
) ?
663 le32_to_cpu(ckpt
->sit_ver_bitmap_bytesize
) : 0;
664 return &ckpt
->sit_nat_version_bitmap
+ offset
;
667 static inline block_t
__start_cp_addr(struct f2fs_sb_info
*sbi
)
670 struct f2fs_checkpoint
*ckpt
= F2FS_CKPT(sbi
);
671 unsigned long long ckpt_version
= cur_cp_version(ckpt
);
673 start_addr
= le32_to_cpu(F2FS_RAW_SUPER(sbi
)->cp_blkaddr
);
676 * odd numbered checkpoint should at cp segment 0
677 * and even segent must be at cp segment 1
679 if (!(ckpt_version
& 1))
680 start_addr
+= sbi
->blocks_per_seg
;
685 static inline block_t
__start_sum_addr(struct f2fs_sb_info
*sbi
)
687 return le32_to_cpu(F2FS_CKPT(sbi
)->cp_pack_start_sum
);
690 static inline bool inc_valid_node_count(struct f2fs_sb_info
*sbi
,
694 block_t valid_block_count
;
695 unsigned int valid_node_count
;
697 spin_lock(&sbi
->stat_lock
);
699 valid_block_count
= sbi
->total_valid_block_count
+ (block_t
)count
;
700 sbi
->alloc_valid_block_count
+= (block_t
)count
;
701 valid_node_count
= sbi
->total_valid_node_count
+ count
;
703 if (valid_block_count
> sbi
->user_block_count
) {
704 spin_unlock(&sbi
->stat_lock
);
708 if (valid_node_count
> sbi
->total_node_count
) {
709 spin_unlock(&sbi
->stat_lock
);
714 inode
->i_blocks
+= count
;
715 sbi
->total_valid_node_count
= valid_node_count
;
716 sbi
->total_valid_block_count
= valid_block_count
;
717 spin_unlock(&sbi
->stat_lock
);
722 static inline void dec_valid_node_count(struct f2fs_sb_info
*sbi
,
726 spin_lock(&sbi
->stat_lock
);
728 BUG_ON(sbi
->total_valid_block_count
< count
);
729 BUG_ON(sbi
->total_valid_node_count
< count
);
730 BUG_ON(inode
->i_blocks
< count
);
732 inode
->i_blocks
-= count
;
733 sbi
->total_valid_node_count
-= count
;
734 sbi
->total_valid_block_count
-= (block_t
)count
;
736 spin_unlock(&sbi
->stat_lock
);
739 static inline unsigned int valid_node_count(struct f2fs_sb_info
*sbi
)
742 spin_lock(&sbi
->stat_lock
);
743 ret
= sbi
->total_valid_node_count
;
744 spin_unlock(&sbi
->stat_lock
);
748 static inline void inc_valid_inode_count(struct f2fs_sb_info
*sbi
)
750 spin_lock(&sbi
->stat_lock
);
751 BUG_ON(sbi
->total_valid_inode_count
== sbi
->total_node_count
);
752 sbi
->total_valid_inode_count
++;
753 spin_unlock(&sbi
->stat_lock
);
756 static inline int dec_valid_inode_count(struct f2fs_sb_info
*sbi
)
758 spin_lock(&sbi
->stat_lock
);
759 BUG_ON(!sbi
->total_valid_inode_count
);
760 sbi
->total_valid_inode_count
--;
761 spin_unlock(&sbi
->stat_lock
);
765 static inline unsigned int valid_inode_count(struct f2fs_sb_info
*sbi
)
768 spin_lock(&sbi
->stat_lock
);
769 ret
= sbi
->total_valid_inode_count
;
770 spin_unlock(&sbi
->stat_lock
);
774 static inline void f2fs_put_page(struct page
*page
, int unlock
)
776 if (!page
|| IS_ERR(page
))
780 BUG_ON(!PageLocked(page
));
783 page_cache_release(page
);
786 static inline void f2fs_put_dnode(struct dnode_of_data
*dn
)
789 f2fs_put_page(dn
->node_page
, 1);
790 if (dn
->inode_page
&& dn
->node_page
!= dn
->inode_page
)
791 f2fs_put_page(dn
->inode_page
, 0);
792 dn
->node_page
= NULL
;
793 dn
->inode_page
= NULL
;
796 static inline struct kmem_cache
*f2fs_kmem_cache_create(const char *name
,
797 size_t size
, void (*ctor
)(void *))
799 return kmem_cache_create(name
, size
, 0, SLAB_RECLAIM_ACCOUNT
, ctor
);
802 #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
804 static inline bool IS_INODE(struct page
*page
)
806 struct f2fs_node
*p
= F2FS_NODE(page
);
807 return RAW_IS_INODE(p
);
810 static inline __le32
*blkaddr_in_node(struct f2fs_node
*node
)
812 return RAW_IS_INODE(node
) ? node
->i
.i_addr
: node
->dn
.addr
;
815 static inline block_t
datablock_addr(struct page
*node_page
,
818 struct f2fs_node
*raw_node
;
820 raw_node
= F2FS_NODE(node_page
);
821 addr_array
= blkaddr_in_node(raw_node
);
822 return le32_to_cpu(addr_array
[offset
]);
825 static inline int f2fs_test_bit(unsigned int nr
, char *addr
)
830 mask
= 1 << (7 - (nr
& 0x07));
834 static inline int f2fs_set_bit(unsigned int nr
, char *addr
)
840 mask
= 1 << (7 - (nr
& 0x07));
846 static inline int f2fs_clear_bit(unsigned int nr
, char *addr
)
852 mask
= 1 << (7 - (nr
& 0x07));
858 /* used for f2fs_inode_info->flags */
860 FI_NEW_INODE
, /* indicate newly allocated inode */
861 FI_DIRTY_INODE
, /* indicate inode is dirty or not */
862 FI_INC_LINK
, /* need to increment i_nlink */
863 FI_ACL_MODE
, /* indicate acl mode */
864 FI_NO_ALLOC
, /* should not allocate any blocks */
865 FI_UPDATE_DIR
, /* should update inode block for consistency */
866 FI_DELAY_IPUT
, /* used for the recovery */
867 FI_INLINE_XATTR
, /* used for inline xattr */
870 static inline void set_inode_flag(struct f2fs_inode_info
*fi
, int flag
)
872 set_bit(flag
, &fi
->flags
);
875 static inline int is_inode_flag_set(struct f2fs_inode_info
*fi
, int flag
)
877 return test_bit(flag
, &fi
->flags
);
880 static inline void clear_inode_flag(struct f2fs_inode_info
*fi
, int flag
)
882 clear_bit(flag
, &fi
->flags
);
885 static inline void set_acl_inode(struct f2fs_inode_info
*fi
, umode_t mode
)
887 fi
->i_acl_mode
= mode
;
888 set_inode_flag(fi
, FI_ACL_MODE
);
891 static inline int cond_clear_inode_flag(struct f2fs_inode_info
*fi
, int flag
)
893 if (is_inode_flag_set(fi
, FI_ACL_MODE
)) {
894 clear_inode_flag(fi
, FI_ACL_MODE
);
900 static inline void get_inline_info(struct f2fs_inode_info
*fi
,
901 struct f2fs_inode
*ri
)
903 if (ri
->i_inline
& F2FS_INLINE_XATTR
)
904 set_inode_flag(fi
, FI_INLINE_XATTR
);
907 static inline void set_raw_inline(struct f2fs_inode_info
*fi
,
908 struct f2fs_inode
*ri
)
912 if (is_inode_flag_set(fi
, FI_INLINE_XATTR
))
913 ri
->i_inline
|= F2FS_INLINE_XATTR
;
916 static inline unsigned int addrs_per_inode(struct f2fs_inode_info
*fi
)
918 if (is_inode_flag_set(fi
, FI_INLINE_XATTR
))
919 return DEF_ADDRS_PER_INODE
- F2FS_INLINE_XATTR_ADDRS
;
920 return DEF_ADDRS_PER_INODE
;
923 static inline void *inline_xattr_addr(struct page
*page
)
925 struct f2fs_inode
*ri
;
926 ri
= (struct f2fs_inode
*)page_address(page
);
927 return (void *)&(ri
->i_addr
[DEF_ADDRS_PER_INODE
-
928 F2FS_INLINE_XATTR_ADDRS
]);
931 static inline int inline_xattr_size(struct inode
*inode
)
933 if (is_inode_flag_set(F2FS_I(inode
), FI_INLINE_XATTR
))
934 return F2FS_INLINE_XATTR_ADDRS
<< 2;
939 static inline int f2fs_readonly(struct super_block
*sb
)
941 return sb
->s_flags
& MS_RDONLY
;
947 int f2fs_sync_file(struct file
*, loff_t
, loff_t
, int);
948 void truncate_data_blocks(struct dnode_of_data
*);
949 void f2fs_truncate(struct inode
*);
950 int f2fs_getattr(struct vfsmount
*, struct dentry
*, struct kstat
*);
951 int f2fs_setattr(struct dentry
*, struct iattr
*);
952 int truncate_hole(struct inode
*, pgoff_t
, pgoff_t
);
953 int truncate_data_blocks_range(struct dnode_of_data
*, int);
954 long f2fs_ioctl(struct file
*, unsigned int, unsigned long);
955 long f2fs_compat_ioctl(struct file
*, unsigned int, unsigned long);
960 void f2fs_set_inode_flags(struct inode
*);
961 struct inode
*f2fs_iget(struct super_block
*, unsigned long);
962 void update_inode(struct inode
*, struct page
*);
963 int update_inode_page(struct inode
*);
964 int f2fs_write_inode(struct inode
*, struct writeback_control
*);
965 void f2fs_evict_inode(struct inode
*);
970 struct dentry
*f2fs_get_parent(struct dentry
*child
);
975 struct f2fs_dir_entry
*f2fs_find_entry(struct inode
*, struct qstr
*,
977 struct f2fs_dir_entry
*f2fs_parent_dir(struct inode
*, struct page
**);
978 ino_t
f2fs_inode_by_name(struct inode
*, struct qstr
*);
979 void f2fs_set_link(struct inode
*, struct f2fs_dir_entry
*,
980 struct page
*, struct inode
*);
981 int update_dent_inode(struct inode
*, const struct qstr
*);
982 int __f2fs_add_link(struct inode
*, const struct qstr
*, struct inode
*);
983 void f2fs_delete_entry(struct f2fs_dir_entry
*, struct page
*, struct inode
*);
984 int f2fs_make_empty(struct inode
*, struct inode
*);
985 bool f2fs_empty_dir(struct inode
*);
987 static inline int f2fs_add_link(struct dentry
*dentry
, struct inode
*inode
)
989 return __f2fs_add_link(dentry
->d_parent
->d_inode
, &dentry
->d_name
,
996 int f2fs_sync_fs(struct super_block
*, int);
997 extern __printf(3, 4)
998 void f2fs_msg(struct super_block
*, const char *, const char *, ...);
1003 f2fs_hash_t
f2fs_dentry_hash(const char *, size_t);
1008 struct dnode_of_data
;
1011 int is_checkpointed_node(struct f2fs_sb_info
*, nid_t
);
1012 void get_node_info(struct f2fs_sb_info
*, nid_t
, struct node_info
*);
1013 int get_dnode_of_data(struct dnode_of_data
*, pgoff_t
, int);
1014 int truncate_inode_blocks(struct inode
*, pgoff_t
);
1015 int truncate_xattr_node(struct inode
*, struct page
*);
1016 int remove_inode_page(struct inode
*);
1017 struct page
*new_inode_page(struct inode
*, const struct qstr
*);
1018 struct page
*new_node_page(struct dnode_of_data
*, unsigned int, struct page
*);
1019 void ra_node_page(struct f2fs_sb_info
*, nid_t
);
1020 struct page
*get_node_page(struct f2fs_sb_info
*, pgoff_t
);
1021 struct page
*get_node_page_ra(struct page
*, int);
1022 void sync_inode_page(struct dnode_of_data
*);
1023 int sync_node_pages(struct f2fs_sb_info
*, nid_t
, struct writeback_control
*);
1024 bool alloc_nid(struct f2fs_sb_info
*, nid_t
*);
1025 void alloc_nid_done(struct f2fs_sb_info
*, nid_t
);
1026 void alloc_nid_failed(struct f2fs_sb_info
*, nid_t
);
1027 void recover_node_page(struct f2fs_sb_info
*, struct page
*,
1028 struct f2fs_summary
*, struct node_info
*, block_t
);
1029 int recover_inode_page(struct f2fs_sb_info
*, struct page
*);
1030 int restore_node_summary(struct f2fs_sb_info
*, unsigned int,
1031 struct f2fs_summary_block
*);
1032 void flush_nat_entries(struct f2fs_sb_info
*);
1033 int build_node_manager(struct f2fs_sb_info
*);
1034 void destroy_node_manager(struct f2fs_sb_info
*);
1035 int __init
create_node_manager_caches(void);
1036 void destroy_node_manager_caches(void);
1041 void f2fs_balance_fs(struct f2fs_sb_info
*);
1042 void invalidate_blocks(struct f2fs_sb_info
*, block_t
);
1043 void clear_prefree_segments(struct f2fs_sb_info
*);
1044 int npages_for_summary_flush(struct f2fs_sb_info
*);
1045 void allocate_new_segments(struct f2fs_sb_info
*);
1046 struct page
*get_sum_page(struct f2fs_sb_info
*, unsigned int);
1047 struct bio
*f2fs_bio_alloc(struct block_device
*, int);
1048 void f2fs_submit_bio(struct f2fs_sb_info
*, enum page_type
, bool);
1049 void f2fs_wait_on_page_writeback(struct page
*, enum page_type
, bool);
1050 void write_meta_page(struct f2fs_sb_info
*, struct page
*);
1051 void write_node_page(struct f2fs_sb_info
*, struct page
*, unsigned int,
1052 block_t
, block_t
*);
1053 void write_data_page(struct inode
*, struct page
*, struct dnode_of_data
*,
1054 block_t
, block_t
*);
1055 void rewrite_data_page(struct f2fs_sb_info
*, struct page
*, block_t
);
1056 void recover_data_page(struct f2fs_sb_info
*, struct page
*,
1057 struct f2fs_summary
*, block_t
, block_t
);
1058 void rewrite_node_page(struct f2fs_sb_info
*, struct page
*,
1059 struct f2fs_summary
*, block_t
, block_t
);
1060 void write_data_summaries(struct f2fs_sb_info
*, block_t
);
1061 void write_node_summaries(struct f2fs_sb_info
*, block_t
);
1062 int lookup_journal_in_cursum(struct f2fs_summary_block
*,
1063 int, unsigned int, int);
1064 void flush_sit_entries(struct f2fs_sb_info
*);
1065 int build_segment_manager(struct f2fs_sb_info
*);
1066 void destroy_segment_manager(struct f2fs_sb_info
*);
1071 struct page
*grab_meta_page(struct f2fs_sb_info
*, pgoff_t
);
1072 struct page
*get_meta_page(struct f2fs_sb_info
*, pgoff_t
);
1073 long sync_meta_pages(struct f2fs_sb_info
*, enum page_type
, long);
1074 int acquire_orphan_inode(struct f2fs_sb_info
*);
1075 void release_orphan_inode(struct f2fs_sb_info
*);
1076 void add_orphan_inode(struct f2fs_sb_info
*, nid_t
);
1077 void remove_orphan_inode(struct f2fs_sb_info
*, nid_t
);
1078 int recover_orphan_inodes(struct f2fs_sb_info
*);
1079 int get_valid_checkpoint(struct f2fs_sb_info
*);
1080 void set_dirty_dir_page(struct inode
*, struct page
*);
1081 void add_dirty_dir_inode(struct inode
*);
1082 void remove_dirty_dir_inode(struct inode
*);
1083 struct inode
*check_dirty_dir_inode(struct f2fs_sb_info
*, nid_t
);
1084 void sync_dirty_dir_inodes(struct f2fs_sb_info
*);
1085 void write_checkpoint(struct f2fs_sb_info
*, bool);
1086 void init_orphan_info(struct f2fs_sb_info
*);
1087 int __init
create_checkpoint_caches(void);
1088 void destroy_checkpoint_caches(void);
1093 int reserve_new_block(struct dnode_of_data
*);
1094 void update_extent_cache(block_t
, struct dnode_of_data
*);
1095 struct page
*find_data_page(struct inode
*, pgoff_t
, bool);
1096 struct page
*get_lock_data_page(struct inode
*, pgoff_t
);
1097 struct page
*get_new_data_page(struct inode
*, struct page
*, pgoff_t
, bool);
1098 int f2fs_readpage(struct f2fs_sb_info
*, struct page
*, block_t
, int);
1099 int do_write_data_page(struct page
*);
1104 int start_gc_thread(struct f2fs_sb_info
*);
1105 void stop_gc_thread(struct f2fs_sb_info
*);
1106 block_t
start_bidx_of_node(unsigned int, struct f2fs_inode_info
*);
1107 int f2fs_gc(struct f2fs_sb_info
*);
1108 void build_gc_manager(struct f2fs_sb_info
*);
1109 int __init
create_gc_caches(void);
1110 void destroy_gc_caches(void);
1115 int recover_fsync_data(struct f2fs_sb_info
*);
1116 bool space_for_roll_forward(struct f2fs_sb_info
*);
1121 #ifdef CONFIG_F2FS_STAT_FS
1122 struct f2fs_stat_info
{
1123 struct list_head stat_list
;
1124 struct f2fs_sb_info
*sbi
;
1125 struct mutex stat_lock
;
1126 int all_area_segs
, sit_area_segs
, nat_area_segs
, ssa_area_segs
;
1127 int main_area_segs
, main_area_sections
, main_area_zones
;
1128 int hit_ext
, total_ext
;
1129 int ndirty_node
, ndirty_dent
, ndirty_dirs
, ndirty_meta
;
1130 int nats
, sits
, fnids
;
1131 int total_count
, utilization
;
1133 unsigned int valid_count
, valid_node_count
, valid_inode_count
;
1134 unsigned int bimodal
, avg_vblocks
;
1135 int util_free
, util_valid
, util_invalid
;
1136 int rsvd_segs
, overp_segs
;
1137 int dirty_count
, node_pages
, meta_pages
;
1138 int prefree_count
, call_count
;
1139 int tot_segs
, node_segs
, data_segs
, free_segs
, free_secs
;
1140 int tot_blks
, data_blks
, node_blks
;
1141 int curseg
[NR_CURSEG_TYPE
];
1142 int cursec
[NR_CURSEG_TYPE
];
1143 int curzone
[NR_CURSEG_TYPE
];
1145 unsigned int segment_count
[2];
1146 unsigned int block_count
[2];
1147 unsigned base_mem
, cache_mem
;
1150 static inline struct f2fs_stat_info
*F2FS_STAT(struct f2fs_sb_info
*sbi
)
1152 return (struct f2fs_stat_info
*)sbi
->stat_info
;
1155 #define stat_inc_call_count(si) ((si)->call_count++)
1157 #define stat_inc_seg_count(sbi, type) \
1159 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
1161 if (type == SUM_TYPE_DATA) \
1167 #define stat_inc_tot_blk_count(si, blks) \
1168 (si->tot_blks += (blks))
1170 #define stat_inc_data_blk_count(sbi, blks) \
1172 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
1173 stat_inc_tot_blk_count(si, blks); \
1174 si->data_blks += (blks); \
1177 #define stat_inc_node_blk_count(sbi, blks) \
1179 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
1180 stat_inc_tot_blk_count(si, blks); \
1181 si->node_blks += (blks); \
1184 int f2fs_build_stats(struct f2fs_sb_info
*);
1185 void f2fs_destroy_stats(struct f2fs_sb_info
*);
1186 void __init
f2fs_create_root_stats(void);
1187 void f2fs_destroy_root_stats(void);
1189 #define stat_inc_call_count(si)
1190 #define stat_inc_seg_count(si, type)
1191 #define stat_inc_tot_blk_count(si, blks)
1192 #define stat_inc_data_blk_count(si, blks)
1193 #define stat_inc_node_blk_count(sbi, blks)
1195 static inline int f2fs_build_stats(struct f2fs_sb_info
*sbi
) { return 0; }
1196 static inline void f2fs_destroy_stats(struct f2fs_sb_info
*sbi
) { }
1197 static inline void __init
f2fs_create_root_stats(void) { }
1198 static inline void f2fs_destroy_root_stats(void) { }
1201 extern const struct file_operations f2fs_dir_operations
;
1202 extern const struct file_operations f2fs_file_operations
;
1203 extern const struct inode_operations f2fs_file_inode_operations
;
1204 extern const struct address_space_operations f2fs_dblock_aops
;
1205 extern const struct address_space_operations f2fs_node_aops
;
1206 extern const struct address_space_operations f2fs_meta_aops
;
1207 extern const struct inode_operations f2fs_dir_inode_operations
;
1208 extern const struct inode_operations f2fs_symlink_inode_operations
;
1209 extern const struct inode_operations f2fs_special_inode_operations
;