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