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