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