aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaegeuk Kim <jaegeuk.kim@samsung.com>2012-11-27 23:37:31 -0500
committerJaegeuk Kim <jaegeuk.kim@samsung.com>2012-12-10 23:43:40 -0500
commit39a53e0ce0df01b3cf4bb898c7ae2fd2189647d5 (patch)
tree45c7028592e7520eb94afc8cd31f757f6a2c7553
parentdd31866b0d55c9b70722ebad6ccd643223d9269e (diff)
f2fs: add superblock and major in-memory structure
This adds the following major in-memory structures in f2fs. - f2fs_sb_info: contains f2fs-specific information, two special inode pointers for node and meta address spaces, and orphan inode management. - f2fs_inode_info: contains vfs_inode and other fs-specific information. - f2fs_nm_info: contains node manager information such as NAT entry cache, free nid list, and NAT page management. - f2fs_node_info: represents a node as node id, inode number, block address, and its version. - f2fs_sm_info: contains segment manager information such as SIT entry cache, free segment map, current active logs, dirty segment management, and segment utilization. The specific structures are sit_info, free_segmap_info, dirty_seglist_info, curseg_info. In addition, add F2FS_SUPER_MAGIC in magic.h. Signed-off-by: Chul Lee <chur.lee@samsung.com> Signed-off-by: Jaegeuk Kim <jaegeuk.kim@samsung.com>
-rw-r--r--fs/f2fs/f2fs.h1062
-rw-r--r--fs/f2fs/node.h353
-rw-r--r--fs/f2fs/segment.h615
-rw-r--r--include/uapi/linux/magic.h1
4 files changed, 2031 insertions, 0 deletions
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
new file mode 100644
index 000000000000..7aa70b54172d
--- /dev/null
+++ b/fs/f2fs/f2fs.h
@@ -0,0 +1,1062 @@
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/version.h>
18#include <linux/slab.h>
19#include <linux/crc32.h>
20#include <linux/magic.h>
21
22/*
23 * For mount options
24 */
25#define F2FS_MOUNT_BG_GC 0x00000001
26#define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
27#define F2FS_MOUNT_DISCARD 0x00000004
28#define F2FS_MOUNT_NOHEAP 0x00000008
29#define F2FS_MOUNT_XATTR_USER 0x00000010
30#define F2FS_MOUNT_POSIX_ACL 0x00000020
31#define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
32
33#define clear_opt(sbi, option) (sbi->mount_opt.opt &= ~F2FS_MOUNT_##option)
34#define set_opt(sbi, option) (sbi->mount_opt.opt |= F2FS_MOUNT_##option)
35#define test_opt(sbi, option) (sbi->mount_opt.opt & F2FS_MOUNT_##option)
36
37#define ver_after(a, b) (typecheck(unsigned long long, a) && \
38 typecheck(unsigned long long, b) && \
39 ((long long)((a) - (b)) > 0))
40
41typedef u64 block_t;
42typedef u32 nid_t;
43
44struct f2fs_mount_info {
45 unsigned int opt;
46};
47
48static inline __u32 f2fs_crc32(void *buff, size_t len)
49{
50 return crc32_le(F2FS_SUPER_MAGIC, buff, len);
51}
52
53static inline bool f2fs_crc_valid(__u32 blk_crc, void *buff, size_t buff_size)
54{
55 return f2fs_crc32(buff, buff_size) == blk_crc;
56}
57
58/*
59 * For checkpoint manager
60 */
61enum {
62 NAT_BITMAP,
63 SIT_BITMAP
64};
65
66/* for the list of orphan inodes */
67struct orphan_inode_entry {
68 struct list_head list; /* list head */
69 nid_t ino; /* inode number */
70};
71
72/* for the list of directory inodes */
73struct dir_inode_entry {
74 struct list_head list; /* list head */
75 struct inode *inode; /* vfs inode pointer */
76};
77
78/* for the list of fsync inodes, used only during recovery */
79struct fsync_inode_entry {
80 struct list_head list; /* list head */
81 struct inode *inode; /* vfs inode pointer */
82 block_t blkaddr; /* block address locating the last inode */
83};
84
85#define nats_in_cursum(sum) (le16_to_cpu(sum->n_nats))
86#define sits_in_cursum(sum) (le16_to_cpu(sum->n_sits))
87
88#define nat_in_journal(sum, i) (sum->nat_j.entries[i].ne)
89#define nid_in_journal(sum, i) (sum->nat_j.entries[i].nid)
90#define sit_in_journal(sum, i) (sum->sit_j.entries[i].se)
91#define segno_in_journal(sum, i) (sum->sit_j.entries[i].segno)
92
93static inline int update_nats_in_cursum(struct f2fs_summary_block *rs, int i)
94{
95 int before = nats_in_cursum(rs);
96 rs->n_nats = cpu_to_le16(before + i);
97 return before;
98}
99
100static inline int update_sits_in_cursum(struct f2fs_summary_block *rs, int i)
101{
102 int before = sits_in_cursum(rs);
103 rs->n_sits = cpu_to_le16(before + i);
104 return before;
105}
106
107/*
108 * For INODE and NODE manager
109 */
110#define XATTR_NODE_OFFSET (-1) /*
111 * store xattrs to one node block per
112 * file keeping -1 as its node offset to
113 * distinguish from index node blocks.
114 */
115#define RDONLY_NODE 1 /*
116 * specify a read-only mode when getting
117 * a node block. 0 is read-write mode.
118 * used by get_dnode_of_data().
119 */
120#define F2FS_LINK_MAX 32000 /* maximum link count per file */
121
122/* for in-memory extent cache entry */
123struct extent_info {
124 rwlock_t ext_lock; /* rwlock for consistency */
125 unsigned int fofs; /* start offset in a file */
126 u32 blk_addr; /* start block address of the extent */
127 unsigned int len; /* lenth of the extent */
128};
129
130/*
131 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
132 */
133#define FADVISE_COLD_BIT 0x01
134
135struct f2fs_inode_info {
136 struct inode vfs_inode; /* serve a vfs inode */
137 unsigned long i_flags; /* keep an inode flags for ioctl */
138 unsigned char i_advise; /* use to give file attribute hints */
139 unsigned int i_current_depth; /* use only in directory structure */
140 umode_t i_acl_mode; /* keep file acl mode temporarily */
141
142 /* Use below internally in f2fs*/
143 unsigned long flags; /* use to pass per-file flags */
144 unsigned long long data_version;/* lastes version of data for fsync */
145 atomic_t dirty_dents; /* # of dirty dentry pages */
146 f2fs_hash_t chash; /* hash value of given file name */
147 unsigned int clevel; /* maximum level of given file name */
148 nid_t i_xattr_nid; /* node id that contains xattrs */
149 struct extent_info ext; /* in-memory extent cache entry */
150};
151
152static inline void get_extent_info(struct extent_info *ext,
153 struct f2fs_extent i_ext)
154{
155 write_lock(&ext->ext_lock);
156 ext->fofs = le32_to_cpu(i_ext.fofs);
157 ext->blk_addr = le32_to_cpu(i_ext.blk_addr);
158 ext->len = le32_to_cpu(i_ext.len);
159 write_unlock(&ext->ext_lock);
160}
161
162static inline void set_raw_extent(struct extent_info *ext,
163 struct f2fs_extent *i_ext)
164{
165 read_lock(&ext->ext_lock);
166 i_ext->fofs = cpu_to_le32(ext->fofs);
167 i_ext->blk_addr = cpu_to_le32(ext->blk_addr);
168 i_ext->len = cpu_to_le32(ext->len);
169 read_unlock(&ext->ext_lock);
170}
171
172struct f2fs_nm_info {
173 block_t nat_blkaddr; /* base disk address of NAT */
174 nid_t max_nid; /* maximum possible node ids */
175 nid_t init_scan_nid; /* the first nid to be scanned */
176 nid_t next_scan_nid; /* the next nid to be scanned */
177
178 /* NAT cache management */
179 struct radix_tree_root nat_root;/* root of the nat entry cache */
180 rwlock_t nat_tree_lock; /* protect nat_tree_lock */
181 unsigned int nat_cnt; /* the # of cached nat entries */
182 struct list_head nat_entries; /* cached nat entry list (clean) */
183 struct list_head dirty_nat_entries; /* cached nat entry list (dirty) */
184
185 /* free node ids management */
186 struct list_head free_nid_list; /* a list for free nids */
187 spinlock_t free_nid_list_lock; /* protect free nid list */
188 unsigned int fcnt; /* the number of free node id */
189 struct mutex build_lock; /* lock for build free nids */
190
191 /* for checkpoint */
192 char *nat_bitmap; /* NAT bitmap pointer */
193 int bitmap_size; /* bitmap size */
194};
195
196/*
197 * this structure is used as one of function parameters.
198 * all the information are dedicated to a given direct node block determined
199 * by the data offset in a file.
200 */
201struct dnode_of_data {
202 struct inode *inode; /* vfs inode pointer */
203 struct page *inode_page; /* its inode page, NULL is possible */
204 struct page *node_page; /* cached direct node page */
205 nid_t nid; /* node id of the direct node block */
206 unsigned int ofs_in_node; /* data offset in the node page */
207 bool inode_page_locked; /* inode page is locked or not */
208 block_t data_blkaddr; /* block address of the node block */
209};
210
211static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
212 struct page *ipage, struct page *npage, nid_t nid)
213{
214 dn->inode = inode;
215 dn->inode_page = ipage;
216 dn->node_page = npage;
217 dn->nid = nid;
218 dn->inode_page_locked = 0;
219}
220
221/*
222 * For SIT manager
223 *
224 * By default, there are 6 active log areas across the whole main area.
225 * When considering hot and cold data separation to reduce cleaning overhead,
226 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
227 * respectively.
228 * In the current design, you should not change the numbers intentionally.
229 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
230 * logs individually according to the underlying devices. (default: 6)
231 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
232 * data and 8 for node logs.
233 */
234#define NR_CURSEG_DATA_TYPE (3)
235#define NR_CURSEG_NODE_TYPE (3)
236#define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
237
238enum {
239 CURSEG_HOT_DATA = 0, /* directory entry blocks */
240 CURSEG_WARM_DATA, /* data blocks */
241 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
242 CURSEG_HOT_NODE, /* direct node blocks of directory files */
243 CURSEG_WARM_NODE, /* direct node blocks of normal files */
244 CURSEG_COLD_NODE, /* indirect node blocks */
245 NO_CHECK_TYPE
246};
247
248struct f2fs_sm_info {
249 struct sit_info *sit_info; /* whole segment information */
250 struct free_segmap_info *free_info; /* free segment information */
251 struct dirty_seglist_info *dirty_info; /* dirty segment information */
252 struct curseg_info *curseg_array; /* active segment information */
253
254 struct list_head wblist_head; /* list of under-writeback pages */
255 spinlock_t wblist_lock; /* lock for checkpoint */
256
257 block_t seg0_blkaddr; /* block address of 0'th segment */
258 block_t main_blkaddr; /* start block address of main area */
259 block_t ssa_blkaddr; /* start block address of SSA area */
260
261 unsigned int segment_count; /* total # of segments */
262 unsigned int main_segments; /* # of segments in main area */
263 unsigned int reserved_segments; /* # of reserved segments */
264 unsigned int ovp_segments; /* # of overprovision segments */
265};
266
267/*
268 * For directory operation
269 */
270#define NODE_DIR1_BLOCK (ADDRS_PER_INODE + 1)
271#define NODE_DIR2_BLOCK (ADDRS_PER_INODE + 2)
272#define NODE_IND1_BLOCK (ADDRS_PER_INODE + 3)
273#define NODE_IND2_BLOCK (ADDRS_PER_INODE + 4)
274#define NODE_DIND_BLOCK (ADDRS_PER_INODE + 5)
275
276/*
277 * For superblock
278 */
279/*
280 * COUNT_TYPE for monitoring
281 *
282 * f2fs monitors the number of several block types such as on-writeback,
283 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
284 */
285enum count_type {
286 F2FS_WRITEBACK,
287 F2FS_DIRTY_DENTS,
288 F2FS_DIRTY_NODES,
289 F2FS_DIRTY_META,
290 NR_COUNT_TYPE,
291};
292
293/*
294 * FS_LOCK nesting subclasses for the lock validator:
295 *
296 * The locking order between these classes is
297 * RENAME -> DENTRY_OPS -> DATA_WRITE -> DATA_NEW
298 * -> DATA_TRUNC -> NODE_WRITE -> NODE_NEW -> NODE_TRUNC
299 */
300enum lock_type {
301 RENAME, /* for renaming operations */
302 DENTRY_OPS, /* for directory operations */
303 DATA_WRITE, /* for data write */
304 DATA_NEW, /* for data allocation */
305 DATA_TRUNC, /* for data truncate */
306 NODE_NEW, /* for node allocation */
307 NODE_TRUNC, /* for node truncate */
308 NODE_WRITE, /* for node write */
309 NR_LOCK_TYPE,
310};
311
312/*
313 * The below are the page types of bios used in submti_bio().
314 * The available types are:
315 * DATA User data pages. It operates as async mode.
316 * NODE Node pages. It operates as async mode.
317 * META FS metadata pages such as SIT, NAT, CP.
318 * NR_PAGE_TYPE The number of page types.
319 * META_FLUSH Make sure the previous pages are written
320 * with waiting the bio's completion
321 * ... Only can be used with META.
322 */
323enum page_type {
324 DATA,
325 NODE,
326 META,
327 NR_PAGE_TYPE,
328 META_FLUSH,
329};
330
331struct f2fs_sb_info {
332 struct super_block *sb; /* pointer to VFS super block */
333 struct buffer_head *raw_super_buf; /* buffer head of raw sb */
334 struct f2fs_super_block *raw_super; /* raw super block pointer */
335 int s_dirty; /* dirty flag for checkpoint */
336
337 /* for node-related operations */
338 struct f2fs_nm_info *nm_info; /* node manager */
339 struct inode *node_inode; /* cache node blocks */
340
341 /* for segment-related operations */
342 struct f2fs_sm_info *sm_info; /* segment manager */
343 struct bio *bio[NR_PAGE_TYPE]; /* bios to merge */
344 sector_t last_block_in_bio[NR_PAGE_TYPE]; /* last block number */
345 struct rw_semaphore bio_sem; /* IO semaphore */
346
347 /* for checkpoint */
348 struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
349 struct inode *meta_inode; /* cache meta blocks */
350 struct mutex cp_mutex; /* for checkpoint procedure */
351 struct mutex fs_lock[NR_LOCK_TYPE]; /* for blocking FS operations */
352 struct mutex write_inode; /* mutex for write inode */
353 struct mutex writepages; /* mutex for writepages() */
354 int por_doing; /* recovery is doing or not */
355
356 /* for orphan inode management */
357 struct list_head orphan_inode_list; /* orphan inode list */
358 struct mutex orphan_inode_mutex; /* for orphan inode list */
359 unsigned int n_orphans; /* # of orphan inodes */
360
361 /* for directory inode management */
362 struct list_head dir_inode_list; /* dir inode list */
363 spinlock_t dir_inode_lock; /* for dir inode list lock */
364 unsigned int n_dirty_dirs; /* # of dir inodes */
365
366 /* basic file system units */
367 unsigned int log_sectors_per_block; /* log2 sectors per block */
368 unsigned int log_blocksize; /* log2 block size */
369 unsigned int blocksize; /* block size */
370 unsigned int root_ino_num; /* root inode number*/
371 unsigned int node_ino_num; /* node inode number*/
372 unsigned int meta_ino_num; /* meta inode number*/
373 unsigned int log_blocks_per_seg; /* log2 blocks per segment */
374 unsigned int blocks_per_seg; /* blocks per segment */
375 unsigned int segs_per_sec; /* segments per section */
376 unsigned int secs_per_zone; /* sections per zone */
377 unsigned int total_sections; /* total section count */
378 unsigned int total_node_count; /* total node block count */
379 unsigned int total_valid_node_count; /* valid node block count */
380 unsigned int total_valid_inode_count; /* valid inode count */
381 int active_logs; /* # of active logs */
382
383 block_t user_block_count; /* # of user blocks */
384 block_t total_valid_block_count; /* # of valid blocks */
385 block_t alloc_valid_block_count; /* # of allocated blocks */
386 block_t last_valid_block_count; /* for recovery */
387 u32 s_next_generation; /* for NFS support */
388 atomic_t nr_pages[NR_COUNT_TYPE]; /* # of pages, see count_type */
389
390 struct f2fs_mount_info mount_opt; /* mount options */
391
392 /* for cleaning operations */
393 struct mutex gc_mutex; /* mutex for GC */
394 struct f2fs_gc_kthread *gc_thread; /* GC thread */
395
396 /*
397 * for stat information.
398 * one is for the LFS mode, and the other is for the SSR mode.
399 */
400 struct f2fs_stat_info *stat_info; /* FS status information */
401 unsigned int segment_count[2]; /* # of allocated segments */
402 unsigned int block_count[2]; /* # of allocated blocks */
403 unsigned int last_victim[2]; /* last victim segment # */
404 int total_hit_ext, read_hit_ext; /* extent cache hit ratio */
405 int bg_gc; /* background gc calls */
406 spinlock_t stat_lock; /* lock for stat operations */
407};
408
409/*
410 * Inline functions
411 */
412static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
413{
414 return container_of(inode, struct f2fs_inode_info, vfs_inode);
415}
416
417static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
418{
419 return sb->s_fs_info;
420}
421
422static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
423{
424 return (struct f2fs_super_block *)(sbi->raw_super);
425}
426
427static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
428{
429 return (struct f2fs_checkpoint *)(sbi->ckpt);
430}
431
432static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
433{
434 return (struct f2fs_nm_info *)(sbi->nm_info);
435}
436
437static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
438{
439 return (struct f2fs_sm_info *)(sbi->sm_info);
440}
441
442static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
443{
444 return (struct sit_info *)(SM_I(sbi)->sit_info);
445}
446
447static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
448{
449 return (struct free_segmap_info *)(SM_I(sbi)->free_info);
450}
451
452static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
453{
454 return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
455}
456
457static inline void F2FS_SET_SB_DIRT(struct f2fs_sb_info *sbi)
458{
459 sbi->s_dirty = 1;
460}
461
462static inline void F2FS_RESET_SB_DIRT(struct f2fs_sb_info *sbi)
463{
464 sbi->s_dirty = 0;
465}
466
467static inline void mutex_lock_op(struct f2fs_sb_info *sbi, enum lock_type t)
468{
469 mutex_lock_nested(&sbi->fs_lock[t], t);
470}
471
472static inline void mutex_unlock_op(struct f2fs_sb_info *sbi, enum lock_type t)
473{
474 mutex_unlock(&sbi->fs_lock[t]);
475}
476
477/*
478 * Check whether the given nid is within node id range.
479 */
480static inline void check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
481{
482 BUG_ON((nid >= NM_I(sbi)->max_nid));
483}
484
485#define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
486
487/*
488 * Check whether the inode has blocks or not
489 */
490static inline int F2FS_HAS_BLOCKS(struct inode *inode)
491{
492 if (F2FS_I(inode)->i_xattr_nid)
493 return (inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1);
494 else
495 return (inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS);
496}
497
498static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
499 struct inode *inode, blkcnt_t count)
500{
501 block_t valid_block_count;
502
503 spin_lock(&sbi->stat_lock);
504 valid_block_count =
505 sbi->total_valid_block_count + (block_t)count;
506 if (valid_block_count > sbi->user_block_count) {
507 spin_unlock(&sbi->stat_lock);
508 return false;
509 }
510 inode->i_blocks += count;
511 sbi->total_valid_block_count = valid_block_count;
512 sbi->alloc_valid_block_count += (block_t)count;
513 spin_unlock(&sbi->stat_lock);
514 return true;
515}
516
517static inline int dec_valid_block_count(struct f2fs_sb_info *sbi,
518 struct inode *inode,
519 blkcnt_t count)
520{
521 spin_lock(&sbi->stat_lock);
522 BUG_ON(sbi->total_valid_block_count < (block_t) count);
523 BUG_ON(inode->i_blocks < count);
524 inode->i_blocks -= count;
525 sbi->total_valid_block_count -= (block_t)count;
526 spin_unlock(&sbi->stat_lock);
527 return 0;
528}
529
530static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
531{
532 atomic_inc(&sbi->nr_pages[count_type]);
533 F2FS_SET_SB_DIRT(sbi);
534}
535
536static inline void inode_inc_dirty_dents(struct inode *inode)
537{
538 atomic_inc(&F2FS_I(inode)->dirty_dents);
539}
540
541static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
542{
543 atomic_dec(&sbi->nr_pages[count_type]);
544}
545
546static inline void inode_dec_dirty_dents(struct inode *inode)
547{
548 atomic_dec(&F2FS_I(inode)->dirty_dents);
549}
550
551static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
552{
553 return atomic_read(&sbi->nr_pages[count_type]);
554}
555
556static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
557{
558 block_t ret;
559 spin_lock(&sbi->stat_lock);
560 ret = sbi->total_valid_block_count;
561 spin_unlock(&sbi->stat_lock);
562 return ret;
563}
564
565static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
566{
567 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
568
569 /* return NAT or SIT bitmap */
570 if (flag == NAT_BITMAP)
571 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
572 else if (flag == SIT_BITMAP)
573 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
574
575 return 0;
576}
577
578static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
579{
580 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
581 int offset = (flag == NAT_BITMAP) ? ckpt->sit_ver_bitmap_bytesize : 0;
582 return &ckpt->sit_nat_version_bitmap + offset;
583}
584
585static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
586{
587 block_t start_addr;
588 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
589 unsigned long long ckpt_version = le64_to_cpu(ckpt->checkpoint_ver);
590
591 start_addr = le64_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
592
593 /*
594 * odd numbered checkpoint should at cp segment 0
595 * and even segent must be at cp segment 1
596 */
597 if (!(ckpt_version & 1))
598 start_addr += sbi->blocks_per_seg;
599
600 return start_addr;
601}
602
603static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
604{
605 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
606}
607
608static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
609 struct inode *inode,
610 unsigned int count)
611{
612 block_t valid_block_count;
613 unsigned int valid_node_count;
614
615 spin_lock(&sbi->stat_lock);
616
617 valid_block_count = sbi->total_valid_block_count + (block_t)count;
618 sbi->alloc_valid_block_count += (block_t)count;
619 valid_node_count = sbi->total_valid_node_count + count;
620
621 if (valid_block_count > sbi->user_block_count) {
622 spin_unlock(&sbi->stat_lock);
623 return false;
624 }
625
626 if (valid_node_count > sbi->total_node_count) {
627 spin_unlock(&sbi->stat_lock);
628 return false;
629 }
630
631 if (inode)
632 inode->i_blocks += count;
633 sbi->total_valid_node_count = valid_node_count;
634 sbi->total_valid_block_count = valid_block_count;
635 spin_unlock(&sbi->stat_lock);
636
637 return true;
638}
639
640static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
641 struct inode *inode,
642 unsigned int count)
643{
644 spin_lock(&sbi->stat_lock);
645
646 BUG_ON(sbi->total_valid_block_count < count);
647 BUG_ON(sbi->total_valid_node_count < count);
648 BUG_ON(inode->i_blocks < count);
649
650 inode->i_blocks -= count;
651 sbi->total_valid_node_count -= count;
652 sbi->total_valid_block_count -= (block_t)count;
653
654 spin_unlock(&sbi->stat_lock);
655}
656
657static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
658{
659 unsigned int ret;
660 spin_lock(&sbi->stat_lock);
661 ret = sbi->total_valid_node_count;
662 spin_unlock(&sbi->stat_lock);
663 return ret;
664}
665
666static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
667{
668 spin_lock(&sbi->stat_lock);
669 BUG_ON(sbi->total_valid_inode_count == sbi->total_node_count);
670 sbi->total_valid_inode_count++;
671 spin_unlock(&sbi->stat_lock);
672}
673
674static inline int dec_valid_inode_count(struct f2fs_sb_info *sbi)
675{
676 spin_lock(&sbi->stat_lock);
677 BUG_ON(!sbi->total_valid_inode_count);
678 sbi->total_valid_inode_count--;
679 spin_unlock(&sbi->stat_lock);
680 return 0;
681}
682
683static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
684{
685 unsigned int ret;
686 spin_lock(&sbi->stat_lock);
687 ret = sbi->total_valid_inode_count;
688 spin_unlock(&sbi->stat_lock);
689 return ret;
690}
691
692static inline void f2fs_put_page(struct page *page, int unlock)
693{
694 if (!page || IS_ERR(page))
695 return;
696
697 if (unlock) {
698 BUG_ON(!PageLocked(page));
699 unlock_page(page);
700 }
701 page_cache_release(page);
702}
703
704static inline void f2fs_put_dnode(struct dnode_of_data *dn)
705{
706 if (dn->node_page)
707 f2fs_put_page(dn->node_page, 1);
708 if (dn->inode_page && dn->node_page != dn->inode_page)
709 f2fs_put_page(dn->inode_page, 0);
710 dn->node_page = NULL;
711 dn->inode_page = NULL;
712}
713
714static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
715 size_t size, void (*ctor)(void *))
716{
717 return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, ctor);
718}
719
720#define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
721
722static inline bool IS_INODE(struct page *page)
723{
724 struct f2fs_node *p = (struct f2fs_node *)page_address(page);
725 return RAW_IS_INODE(p);
726}
727
728static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
729{
730 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
731}
732
733static inline block_t datablock_addr(struct page *node_page,
734 unsigned int offset)
735{
736 struct f2fs_node *raw_node;
737 __le32 *addr_array;
738 raw_node = (struct f2fs_node *)page_address(node_page);
739 addr_array = blkaddr_in_node(raw_node);
740 return le32_to_cpu(addr_array[offset]);
741}
742
743static inline int f2fs_test_bit(unsigned int nr, char *addr)
744{
745 int mask;
746
747 addr += (nr >> 3);
748 mask = 1 << (7 - (nr & 0x07));
749 return mask & *addr;
750}
751
752static inline int f2fs_set_bit(unsigned int nr, char *addr)
753{
754 int mask;
755 int ret;
756
757 addr += (nr >> 3);
758 mask = 1 << (7 - (nr & 0x07));
759 ret = mask & *addr;
760 *addr |= mask;
761 return ret;
762}
763
764static inline int f2fs_clear_bit(unsigned int nr, char *addr)
765{
766 int mask;
767 int ret;
768
769 addr += (nr >> 3);
770 mask = 1 << (7 - (nr & 0x07));
771 ret = mask & *addr;
772 *addr &= ~mask;
773 return ret;
774}
775
776/* used for f2fs_inode_info->flags */
777enum {
778 FI_NEW_INODE, /* indicate newly allocated inode */
779 FI_NEED_CP, /* need to do checkpoint during fsync */
780 FI_INC_LINK, /* need to increment i_nlink */
781 FI_ACL_MODE, /* indicate acl mode */
782 FI_NO_ALLOC, /* should not allocate any blocks */
783};
784
785static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
786{
787 set_bit(flag, &fi->flags);
788}
789
790static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
791{
792 return test_bit(flag, &fi->flags);
793}
794
795static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
796{
797 clear_bit(flag, &fi->flags);
798}
799
800static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
801{
802 fi->i_acl_mode = mode;
803 set_inode_flag(fi, FI_ACL_MODE);
804}
805
806static inline int cond_clear_inode_flag(struct f2fs_inode_info *fi, int flag)
807{
808 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
809 clear_inode_flag(fi, FI_ACL_MODE);
810 return 1;
811 }
812 return 0;
813}
814
815/*
816 * file.c
817 */
818int f2fs_sync_file(struct file *, loff_t, loff_t, int);
819void truncate_data_blocks(struct dnode_of_data *);
820void f2fs_truncate(struct inode *);
821int f2fs_setattr(struct dentry *, struct iattr *);
822int truncate_hole(struct inode *, pgoff_t, pgoff_t);
823long f2fs_ioctl(struct file *, unsigned int, unsigned long);
824
825/*
826 * inode.c
827 */
828void f2fs_set_inode_flags(struct inode *);
829struct inode *f2fs_iget_nowait(struct super_block *, unsigned long);
830struct inode *f2fs_iget(struct super_block *, unsigned long);
831void update_inode(struct inode *, struct page *);
832int f2fs_write_inode(struct inode *, struct writeback_control *);
833void f2fs_evict_inode(struct inode *);
834
835/*
836 * namei.c
837 */
838struct dentry *f2fs_get_parent(struct dentry *child);
839
840/*
841 * dir.c
842 */
843struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
844 struct page **);
845struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
846ino_t f2fs_inode_by_name(struct inode *, struct qstr *);
847void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
848 struct page *, struct inode *);
849void init_dent_inode(struct dentry *, struct page *);
850int f2fs_add_link(struct dentry *, struct inode *);
851void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *);
852int f2fs_make_empty(struct inode *, struct inode *);
853bool f2fs_empty_dir(struct inode *);
854
855/*
856 * super.c
857 */
858int f2fs_sync_fs(struct super_block *, int);
859
860/*
861 * hash.c
862 */
863f2fs_hash_t f2fs_dentry_hash(const char *, int);
864
865/*
866 * node.c
867 */
868struct dnode_of_data;
869struct node_info;
870
871int is_checkpointed_node(struct f2fs_sb_info *, nid_t);
872void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
873int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
874int truncate_inode_blocks(struct inode *, pgoff_t);
875int remove_inode_page(struct inode *);
876int new_inode_page(struct inode *, struct dentry *);
877struct page *new_node_page(struct dnode_of_data *, unsigned int);
878void ra_node_page(struct f2fs_sb_info *, nid_t);
879struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
880struct page *get_node_page_ra(struct page *, int);
881void sync_inode_page(struct dnode_of_data *);
882int sync_node_pages(struct f2fs_sb_info *, nid_t, struct writeback_control *);
883bool alloc_nid(struct f2fs_sb_info *, nid_t *);
884void alloc_nid_done(struct f2fs_sb_info *, nid_t);
885void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
886void recover_node_page(struct f2fs_sb_info *, struct page *,
887 struct f2fs_summary *, struct node_info *, block_t);
888int recover_inode_page(struct f2fs_sb_info *, struct page *);
889int restore_node_summary(struct f2fs_sb_info *, unsigned int,
890 struct f2fs_summary_block *);
891void flush_nat_entries(struct f2fs_sb_info *);
892int build_node_manager(struct f2fs_sb_info *);
893void destroy_node_manager(struct f2fs_sb_info *);
894int create_node_manager_caches(void);
895void destroy_node_manager_caches(void);
896
897/*
898 * segment.c
899 */
900void f2fs_balance_fs(struct f2fs_sb_info *);
901void invalidate_blocks(struct f2fs_sb_info *, block_t);
902void locate_dirty_segment(struct f2fs_sb_info *, unsigned int);
903void clear_prefree_segments(struct f2fs_sb_info *);
904int npages_for_summary_flush(struct f2fs_sb_info *);
905void allocate_new_segments(struct f2fs_sb_info *);
906struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
907struct bio *f2fs_bio_alloc(struct block_device *, sector_t, int, gfp_t);
908void f2fs_submit_bio(struct f2fs_sb_info *, enum page_type, bool sync);
909int write_meta_page(struct f2fs_sb_info *, struct page *,
910 struct writeback_control *);
911void write_node_page(struct f2fs_sb_info *, struct page *, unsigned int,
912 block_t, block_t *);
913void write_data_page(struct inode *, struct page *, struct dnode_of_data*,
914 block_t, block_t *);
915void rewrite_data_page(struct f2fs_sb_info *, struct page *, block_t);
916void recover_data_page(struct f2fs_sb_info *, struct page *,
917 struct f2fs_summary *, block_t, block_t);
918void rewrite_node_page(struct f2fs_sb_info *, struct page *,
919 struct f2fs_summary *, block_t, block_t);
920void write_data_summaries(struct f2fs_sb_info *, block_t);
921void write_node_summaries(struct f2fs_sb_info *, block_t);
922int lookup_journal_in_cursum(struct f2fs_summary_block *,
923 int, unsigned int, int);
924void flush_sit_entries(struct f2fs_sb_info *);
925int build_segment_manager(struct f2fs_sb_info *);
926void reset_victim_segmap(struct f2fs_sb_info *);
927void destroy_segment_manager(struct f2fs_sb_info *);
928
929/*
930 * checkpoint.c
931 */
932struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
933struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
934long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
935int check_orphan_space(struct f2fs_sb_info *);
936void add_orphan_inode(struct f2fs_sb_info *, nid_t);
937void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
938int recover_orphan_inodes(struct f2fs_sb_info *);
939int get_valid_checkpoint(struct f2fs_sb_info *);
940void set_dirty_dir_page(struct inode *, struct page *);
941void remove_dirty_dir_inode(struct inode *);
942void sync_dirty_dir_inodes(struct f2fs_sb_info *);
943void block_operations(struct f2fs_sb_info *);
944void write_checkpoint(struct f2fs_sb_info *, bool, bool);
945void init_orphan_info(struct f2fs_sb_info *);
946int create_checkpoint_caches(void);
947void destroy_checkpoint_caches(void);
948
949/*
950 * data.c
951 */
952int reserve_new_block(struct dnode_of_data *);
953void update_extent_cache(block_t, struct dnode_of_data *);
954struct page *find_data_page(struct inode *, pgoff_t);
955struct page *get_lock_data_page(struct inode *, pgoff_t);
956struct page *get_new_data_page(struct inode *, pgoff_t, bool);
957int f2fs_readpage(struct f2fs_sb_info *, struct page *, block_t, int);
958int do_write_data_page(struct page *);
959
960/*
961 * gc.c
962 */
963int start_gc_thread(struct f2fs_sb_info *);
964void stop_gc_thread(struct f2fs_sb_info *);
965block_t start_bidx_of_node(unsigned int);
966int f2fs_gc(struct f2fs_sb_info *, int);
967void build_gc_manager(struct f2fs_sb_info *);
968int create_gc_caches(void);
969void destroy_gc_caches(void);
970
971/*
972 * recovery.c
973 */
974void recover_fsync_data(struct f2fs_sb_info *);
975bool space_for_roll_forward(struct f2fs_sb_info *);
976
977/*
978 * debug.c
979 */
980#ifdef CONFIG_F2FS_STAT_FS
981struct f2fs_stat_info {
982 struct list_head stat_list;
983 struct f2fs_sb_info *sbi;
984 struct mutex stat_lock;
985 int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
986 int main_area_segs, main_area_sections, main_area_zones;
987 int hit_ext, total_ext;
988 int ndirty_node, ndirty_dent, ndirty_dirs, ndirty_meta;
989 int nats, sits, fnids;
990 int total_count, utilization;
991 int bg_gc;
992 unsigned int valid_count, valid_node_count, valid_inode_count;
993 unsigned int bimodal, avg_vblocks;
994 int util_free, util_valid, util_invalid;
995 int rsvd_segs, overp_segs;
996 int dirty_count, node_pages, meta_pages;
997 int prefree_count, call_count;
998 int tot_segs, node_segs, data_segs, free_segs, free_secs;
999 int tot_blks, data_blks, node_blks;
1000 int curseg[NR_CURSEG_TYPE];
1001 int cursec[NR_CURSEG_TYPE];
1002 int curzone[NR_CURSEG_TYPE];
1003
1004 unsigned int segment_count[2];
1005 unsigned int block_count[2];
1006 unsigned base_mem, cache_mem;
1007};
1008
1009#define stat_inc_call_count(si) ((si)->call_count++)
1010
1011#define stat_inc_seg_count(sbi, type) \
1012 do { \
1013 struct f2fs_stat_info *si = sbi->stat_info; \
1014 (si)->tot_segs++; \
1015 if (type == SUM_TYPE_DATA) \
1016 si->data_segs++; \
1017 else \
1018 si->node_segs++; \
1019 } while (0)
1020
1021#define stat_inc_tot_blk_count(si, blks) \
1022 (si->tot_blks += (blks))
1023
1024#define stat_inc_data_blk_count(sbi, blks) \
1025 do { \
1026 struct f2fs_stat_info *si = sbi->stat_info; \
1027 stat_inc_tot_blk_count(si, blks); \
1028 si->data_blks += (blks); \
1029 } while (0)
1030
1031#define stat_inc_node_blk_count(sbi, blks) \
1032 do { \
1033 struct f2fs_stat_info *si = sbi->stat_info; \
1034 stat_inc_tot_blk_count(si, blks); \
1035 si->node_blks += (blks); \
1036 } while (0)
1037
1038int f2fs_build_stats(struct f2fs_sb_info *);
1039void f2fs_destroy_stats(struct f2fs_sb_info *);
1040void destroy_root_stats(void);
1041#else
1042#define stat_inc_call_count(si)
1043#define stat_inc_seg_count(si, type)
1044#define stat_inc_tot_blk_count(si, blks)
1045#define stat_inc_data_blk_count(si, blks)
1046#define stat_inc_node_blk_count(sbi, blks)
1047
1048static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
1049static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
1050static inline void destroy_root_stats(void) { }
1051#endif
1052
1053extern const struct file_operations f2fs_dir_operations;
1054extern const struct file_operations f2fs_file_operations;
1055extern const struct inode_operations f2fs_file_inode_operations;
1056extern const struct address_space_operations f2fs_dblock_aops;
1057extern const struct address_space_operations f2fs_node_aops;
1058extern const struct address_space_operations f2fs_meta_aops;
1059extern const struct inode_operations f2fs_dir_inode_operations;
1060extern const struct inode_operations f2fs_symlink_inode_operations;
1061extern const struct inode_operations f2fs_special_inode_operations;
1062#endif
diff --git a/fs/f2fs/node.h b/fs/f2fs/node.h
new file mode 100644
index 000000000000..5d525ed312ba
--- /dev/null
+++ b/fs/f2fs/node.h
@@ -0,0 +1,353 @@
1/**
2 * fs/f2fs/node.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/* start node id of a node block dedicated to the given node id */
12#define START_NID(nid) ((nid / NAT_ENTRY_PER_BLOCK) * NAT_ENTRY_PER_BLOCK)
13
14/* node block offset on the NAT area dedicated to the given start node id */
15#define NAT_BLOCK_OFFSET(start_nid) (start_nid / NAT_ENTRY_PER_BLOCK)
16
17/* # of pages to perform readahead before building free nids */
18#define FREE_NID_PAGES 4
19
20/* maximum # of free node ids to produce during build_free_nids */
21#define MAX_FREE_NIDS (NAT_ENTRY_PER_BLOCK * FREE_NID_PAGES)
22
23/* maximum readahead size for node during getting data blocks */
24#define MAX_RA_NODE 128
25
26/* maximum cached nat entries to manage memory footprint */
27#define NM_WOUT_THRESHOLD (64 * NAT_ENTRY_PER_BLOCK)
28
29/* vector size for gang look-up from nat cache that consists of radix tree */
30#define NATVEC_SIZE 64
31
32/*
33 * For node information
34 */
35struct node_info {
36 nid_t nid; /* node id */
37 nid_t ino; /* inode number of the node's owner */
38 block_t blk_addr; /* block address of the node */
39 unsigned char version; /* version of the node */
40};
41
42struct nat_entry {
43 struct list_head list; /* for clean or dirty nat list */
44 bool checkpointed; /* whether it is checkpointed or not */
45 struct node_info ni; /* in-memory node information */
46};
47
48#define nat_get_nid(nat) (nat->ni.nid)
49#define nat_set_nid(nat, n) (nat->ni.nid = n)
50#define nat_get_blkaddr(nat) (nat->ni.blk_addr)
51#define nat_set_blkaddr(nat, b) (nat->ni.blk_addr = b)
52#define nat_get_ino(nat) (nat->ni.ino)
53#define nat_set_ino(nat, i) (nat->ni.ino = i)
54#define nat_get_version(nat) (nat->ni.version)
55#define nat_set_version(nat, v) (nat->ni.version = v)
56
57#define __set_nat_cache_dirty(nm_i, ne) \
58 list_move_tail(&ne->list, &nm_i->dirty_nat_entries);
59#define __clear_nat_cache_dirty(nm_i, ne) \
60 list_move_tail(&ne->list, &nm_i->nat_entries);
61#define inc_node_version(version) (++version)
62
63static inline void node_info_from_raw_nat(struct node_info *ni,
64 struct f2fs_nat_entry *raw_ne)
65{
66 ni->ino = le32_to_cpu(raw_ne->ino);
67 ni->blk_addr = le32_to_cpu(raw_ne->block_addr);
68 ni->version = raw_ne->version;
69}
70
71/*
72 * For free nid mangement
73 */
74enum nid_state {
75 NID_NEW, /* newly added to free nid list */
76 NID_ALLOC /* it is allocated */
77};
78
79struct free_nid {
80 struct list_head list; /* for free node id list */
81 nid_t nid; /* node id */
82 int state; /* in use or not: NID_NEW or NID_ALLOC */
83};
84
85static inline int next_free_nid(struct f2fs_sb_info *sbi, nid_t *nid)
86{
87 struct f2fs_nm_info *nm_i = NM_I(sbi);
88 struct free_nid *fnid;
89
90 if (nm_i->fcnt <= 0)
91 return -1;
92 spin_lock(&nm_i->free_nid_list_lock);
93 fnid = list_entry(nm_i->free_nid_list.next, struct free_nid, list);
94 *nid = fnid->nid;
95 spin_unlock(&nm_i->free_nid_list_lock);
96 return 0;
97}
98
99/*
100 * inline functions
101 */
102static inline void get_nat_bitmap(struct f2fs_sb_info *sbi, void *addr)
103{
104 struct f2fs_nm_info *nm_i = NM_I(sbi);
105 memcpy(addr, nm_i->nat_bitmap, nm_i->bitmap_size);
106}
107
108static inline pgoff_t current_nat_addr(struct f2fs_sb_info *sbi, nid_t start)
109{
110 struct f2fs_nm_info *nm_i = NM_I(sbi);
111 pgoff_t block_off;
112 pgoff_t block_addr;
113 int seg_off;
114
115 block_off = NAT_BLOCK_OFFSET(start);
116 seg_off = block_off >> sbi->log_blocks_per_seg;
117
118 block_addr = (pgoff_t)(nm_i->nat_blkaddr +
119 (seg_off << sbi->log_blocks_per_seg << 1) +
120 (block_off & ((1 << sbi->log_blocks_per_seg) - 1)));
121
122 if (f2fs_test_bit(block_off, nm_i->nat_bitmap))
123 block_addr += sbi->blocks_per_seg;
124
125 return block_addr;
126}
127
128static inline pgoff_t next_nat_addr(struct f2fs_sb_info *sbi,
129 pgoff_t block_addr)
130{
131 struct f2fs_nm_info *nm_i = NM_I(sbi);
132
133 block_addr -= nm_i->nat_blkaddr;
134 if ((block_addr >> sbi->log_blocks_per_seg) % 2)
135 block_addr -= sbi->blocks_per_seg;
136 else
137 block_addr += sbi->blocks_per_seg;
138
139 return block_addr + nm_i->nat_blkaddr;
140}
141
142static inline void set_to_next_nat(struct f2fs_nm_info *nm_i, nid_t start_nid)
143{
144 unsigned int block_off = NAT_BLOCK_OFFSET(start_nid);
145
146 if (f2fs_test_bit(block_off, nm_i->nat_bitmap))
147 f2fs_clear_bit(block_off, nm_i->nat_bitmap);
148 else
149 f2fs_set_bit(block_off, nm_i->nat_bitmap);
150}
151
152static inline void fill_node_footer(struct page *page, nid_t nid,
153 nid_t ino, unsigned int ofs, bool reset)
154{
155 void *kaddr = page_address(page);
156 struct f2fs_node *rn = (struct f2fs_node *)kaddr;
157 if (reset)
158 memset(rn, 0, sizeof(*rn));
159 rn->footer.nid = cpu_to_le32(nid);
160 rn->footer.ino = cpu_to_le32(ino);
161 rn->footer.flag = cpu_to_le32(ofs << OFFSET_BIT_SHIFT);
162}
163
164static inline void copy_node_footer(struct page *dst, struct page *src)
165{
166 void *src_addr = page_address(src);
167 void *dst_addr = page_address(dst);
168 struct f2fs_node *src_rn = (struct f2fs_node *)src_addr;
169 struct f2fs_node *dst_rn = (struct f2fs_node *)dst_addr;
170 memcpy(&dst_rn->footer, &src_rn->footer, sizeof(struct node_footer));
171}
172
173static inline void fill_node_footer_blkaddr(struct page *page, block_t blkaddr)
174{
175 struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
176 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
177 void *kaddr = page_address(page);
178 struct f2fs_node *rn = (struct f2fs_node *)kaddr;
179 rn->footer.cp_ver = ckpt->checkpoint_ver;
180 rn->footer.next_blkaddr = blkaddr;
181}
182
183static inline nid_t ino_of_node(struct page *node_page)
184{
185 void *kaddr = page_address(node_page);
186 struct f2fs_node *rn = (struct f2fs_node *)kaddr;
187 return le32_to_cpu(rn->footer.ino);
188}
189
190static inline nid_t nid_of_node(struct page *node_page)
191{
192 void *kaddr = page_address(node_page);
193 struct f2fs_node *rn = (struct f2fs_node *)kaddr;
194 return le32_to_cpu(rn->footer.nid);
195}
196
197static inline unsigned int ofs_of_node(struct page *node_page)
198{
199 void *kaddr = page_address(node_page);
200 struct f2fs_node *rn = (struct f2fs_node *)kaddr;
201 unsigned flag = le32_to_cpu(rn->footer.flag);
202 return flag >> OFFSET_BIT_SHIFT;
203}
204
205static inline unsigned long long cpver_of_node(struct page *node_page)
206{
207 void *kaddr = page_address(node_page);
208 struct f2fs_node *rn = (struct f2fs_node *)kaddr;
209 return le64_to_cpu(rn->footer.cp_ver);
210}
211
212static inline block_t next_blkaddr_of_node(struct page *node_page)
213{
214 void *kaddr = page_address(node_page);
215 struct f2fs_node *rn = (struct f2fs_node *)kaddr;
216 return le32_to_cpu(rn->footer.next_blkaddr);
217}
218
219/*
220 * f2fs assigns the following node offsets described as (num).
221 * N = NIDS_PER_BLOCK
222 *
223 * Inode block (0)
224 * |- direct node (1)
225 * |- direct node (2)
226 * |- indirect node (3)
227 * | `- direct node (4 => 4 + N - 1)
228 * |- indirect node (4 + N)
229 * | `- direct node (5 + N => 5 + 2N - 1)
230 * `- double indirect node (5 + 2N)
231 * `- indirect node (6 + 2N)
232 * `- direct node (x(N + 1))
233 */
234static inline bool IS_DNODE(struct page *node_page)
235{
236 unsigned int ofs = ofs_of_node(node_page);
237 if (ofs == 3 || ofs == 4 + NIDS_PER_BLOCK ||
238 ofs == 5 + 2 * NIDS_PER_BLOCK)
239 return false;
240 if (ofs >= 6 + 2 * NIDS_PER_BLOCK) {
241 ofs -= 6 + 2 * NIDS_PER_BLOCK;
242 if ((long int)ofs % (NIDS_PER_BLOCK + 1))
243 return false;
244 }
245 return true;
246}
247
248static inline void set_nid(struct page *p, int off, nid_t nid, bool i)
249{
250 struct f2fs_node *rn = (struct f2fs_node *)page_address(p);
251
252 wait_on_page_writeback(p);
253
254 if (i)
255 rn->i.i_nid[off - NODE_DIR1_BLOCK] = cpu_to_le32(nid);
256 else
257 rn->in.nid[off] = cpu_to_le32(nid);
258 set_page_dirty(p);
259}
260
261static inline nid_t get_nid(struct page *p, int off, bool i)
262{
263 struct f2fs_node *rn = (struct f2fs_node *)page_address(p);
264 if (i)
265 return le32_to_cpu(rn->i.i_nid[off - NODE_DIR1_BLOCK]);
266 return le32_to_cpu(rn->in.nid[off]);
267}
268
269/*
270 * Coldness identification:
271 * - Mark cold files in f2fs_inode_info
272 * - Mark cold node blocks in their node footer
273 * - Mark cold data pages in page cache
274 */
275static inline int is_cold_file(struct inode *inode)
276{
277 return F2FS_I(inode)->i_advise & FADVISE_COLD_BIT;
278}
279
280static inline int is_cold_data(struct page *page)
281{
282 return PageChecked(page);
283}
284
285static inline void set_cold_data(struct page *page)
286{
287 SetPageChecked(page);
288}
289
290static inline void clear_cold_data(struct page *page)
291{
292 ClearPageChecked(page);
293}
294
295static inline int is_cold_node(struct page *page)
296{
297 void *kaddr = page_address(page);
298 struct f2fs_node *rn = (struct f2fs_node *)kaddr;
299 unsigned int flag = le32_to_cpu(rn->footer.flag);
300 return flag & (0x1 << COLD_BIT_SHIFT);
301}
302
303static inline unsigned char is_fsync_dnode(struct page *page)
304{
305 void *kaddr = page_address(page);
306 struct f2fs_node *rn = (struct f2fs_node *)kaddr;
307 unsigned int flag = le32_to_cpu(rn->footer.flag);
308 return flag & (0x1 << FSYNC_BIT_SHIFT);
309}
310
311static inline unsigned char is_dent_dnode(struct page *page)
312{
313 void *kaddr = page_address(page);
314 struct f2fs_node *rn = (struct f2fs_node *)kaddr;
315 unsigned int flag = le32_to_cpu(rn->footer.flag);
316 return flag & (0x1 << DENT_BIT_SHIFT);
317}
318
319static inline void set_cold_node(struct inode *inode, struct page *page)
320{
321 struct f2fs_node *rn = (struct f2fs_node *)page_address(page);
322 unsigned int flag = le32_to_cpu(rn->footer.flag);
323
324 if (S_ISDIR(inode->i_mode))
325 flag &= ~(0x1 << COLD_BIT_SHIFT);
326 else
327 flag |= (0x1 << COLD_BIT_SHIFT);
328 rn->footer.flag = cpu_to_le32(flag);
329}
330
331static inline void set_fsync_mark(struct page *page, int mark)
332{
333 void *kaddr = page_address(page);
334 struct f2fs_node *rn = (struct f2fs_node *)kaddr;
335 unsigned int flag = le32_to_cpu(rn->footer.flag);
336 if (mark)
337 flag |= (0x1 << FSYNC_BIT_SHIFT);
338 else
339 flag &= ~(0x1 << FSYNC_BIT_SHIFT);
340 rn->footer.flag = cpu_to_le32(flag);
341}
342
343static inline void set_dentry_mark(struct page *page, int mark)
344{
345 void *kaddr = page_address(page);
346 struct f2fs_node *rn = (struct f2fs_node *)kaddr;
347 unsigned int flag = le32_to_cpu(rn->footer.flag);
348 if (mark)
349 flag |= (0x1 << DENT_BIT_SHIFT);
350 else
351 flag &= ~(0x1 << DENT_BIT_SHIFT);
352 rn->footer.flag = cpu_to_le32(flag);
353}
diff --git a/fs/f2fs/segment.h b/fs/f2fs/segment.h
new file mode 100644
index 000000000000..e380a8ef13f5
--- /dev/null
+++ b/fs/f2fs/segment.h
@@ -0,0 +1,615 @@
1/**
2 * fs/f2fs/segment.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/* constant macro */
12#define NULL_SEGNO ((unsigned int)(~0))
13
14/* V: Logical segment # in volume, R: Relative segment # in main area */
15#define GET_L2R_SEGNO(free_i, segno) (segno - free_i->start_segno)
16#define GET_R2L_SEGNO(free_i, segno) (segno + free_i->start_segno)
17
18#define IS_DATASEG(t) \
19 ((t == CURSEG_HOT_DATA) || (t == CURSEG_COLD_DATA) || \
20 (t == CURSEG_WARM_DATA))
21
22#define IS_NODESEG(t) \
23 ((t == CURSEG_HOT_NODE) || (t == CURSEG_COLD_NODE) || \
24 (t == CURSEG_WARM_NODE))
25
26#define IS_CURSEG(sbi, segno) \
27 ((segno == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno) || \
28 (segno == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno) || \
29 (segno == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno) || \
30 (segno == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno) || \
31 (segno == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno) || \
32 (segno == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno))
33
34#define IS_CURSEC(sbi, secno) \
35 ((secno == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno / \
36 sbi->segs_per_sec) || \
37 (secno == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno / \
38 sbi->segs_per_sec) || \
39 (secno == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno / \
40 sbi->segs_per_sec) || \
41 (secno == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno / \
42 sbi->segs_per_sec) || \
43 (secno == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno / \
44 sbi->segs_per_sec) || \
45 (secno == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno / \
46 sbi->segs_per_sec)) \
47
48#define START_BLOCK(sbi, segno) \
49 (SM_I(sbi)->seg0_blkaddr + \
50 (GET_R2L_SEGNO(FREE_I(sbi), segno) << sbi->log_blocks_per_seg))
51#define NEXT_FREE_BLKADDR(sbi, curseg) \
52 (START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff)
53
54#define MAIN_BASE_BLOCK(sbi) (SM_I(sbi)->main_blkaddr)
55
56#define GET_SEGOFF_FROM_SEG0(sbi, blk_addr) \
57 ((blk_addr) - SM_I(sbi)->seg0_blkaddr)
58#define GET_SEGNO_FROM_SEG0(sbi, blk_addr) \
59 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
60#define GET_SEGNO(sbi, blk_addr) \
61 (((blk_addr == NULL_ADDR) || (blk_addr == NEW_ADDR)) ? \
62 NULL_SEGNO : GET_L2R_SEGNO(FREE_I(sbi), \
63 GET_SEGNO_FROM_SEG0(sbi, blk_addr)))
64#define GET_SECNO(sbi, segno) \
65 ((segno) / sbi->segs_per_sec)
66#define GET_ZONENO_FROM_SEGNO(sbi, segno) \
67 ((segno / sbi->segs_per_sec) / sbi->secs_per_zone)
68
69#define GET_SUM_BLOCK(sbi, segno) \
70 ((sbi->sm_info->ssa_blkaddr) + segno)
71
72#define GET_SUM_TYPE(footer) ((footer)->entry_type)
73#define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
74
75#define SIT_ENTRY_OFFSET(sit_i, segno) \
76 (segno % sit_i->sents_per_block)
77#define SIT_BLOCK_OFFSET(sit_i, segno) \
78 (segno / SIT_ENTRY_PER_BLOCK)
79#define START_SEGNO(sit_i, segno) \
80 (SIT_BLOCK_OFFSET(sit_i, segno) * SIT_ENTRY_PER_BLOCK)
81#define f2fs_bitmap_size(nr) \
82 (BITS_TO_LONGS(nr) * sizeof(unsigned long))
83#define TOTAL_SEGS(sbi) (SM_I(sbi)->main_segments)
84
85/* during checkpoint, bio_private is used to synchronize the last bio */
86struct bio_private {
87 struct f2fs_sb_info *sbi;
88 bool is_sync;
89 void *wait;
90};
91
92/*
93 * indicate a block allocation direction: RIGHT and LEFT.
94 * RIGHT means allocating new sections towards the end of volume.
95 * LEFT means the opposite direction.
96 */
97enum {
98 ALLOC_RIGHT = 0,
99 ALLOC_LEFT
100};
101
102/*
103 * In the victim_sel_policy->alloc_mode, there are two block allocation modes.
104 * LFS writes data sequentially with cleaning operations.
105 * SSR (Slack Space Recycle) reuses obsolete space without cleaning operations.
106 */
107enum {
108 LFS = 0,
109 SSR
110};
111
112/*
113 * In the victim_sel_policy->gc_mode, there are two gc, aka cleaning, modes.
114 * GC_CB is based on cost-benefit algorithm.
115 * GC_GREEDY is based on greedy algorithm.
116 */
117enum {
118 GC_CB = 0,
119 GC_GREEDY
120};
121
122/*
123 * BG_GC means the background cleaning job.
124 * FG_GC means the on-demand cleaning job.
125 */
126enum {
127 BG_GC = 0,
128 FG_GC
129};
130
131/* for a function parameter to select a victim segment */
132struct victim_sel_policy {
133 int alloc_mode; /* LFS or SSR */
134 int gc_mode; /* GC_CB or GC_GREEDY */
135 unsigned long *dirty_segmap; /* dirty segment bitmap */
136 unsigned int offset; /* last scanned bitmap offset */
137 unsigned int ofs_unit; /* bitmap search unit */
138 unsigned int min_cost; /* minimum cost */
139 unsigned int min_segno; /* segment # having min. cost */
140};
141
142struct seg_entry {
143 unsigned short valid_blocks; /* # of valid blocks */
144 unsigned char *cur_valid_map; /* validity bitmap of blocks */
145 /*
146 * # of valid blocks and the validity bitmap stored in the the last
147 * checkpoint pack. This information is used by the SSR mode.
148 */
149 unsigned short ckpt_valid_blocks;
150 unsigned char *ckpt_valid_map;
151 unsigned char type; /* segment type like CURSEG_XXX_TYPE */
152 unsigned long long mtime; /* modification time of the segment */
153};
154
155struct sec_entry {
156 unsigned int valid_blocks; /* # of valid blocks in a section */
157};
158
159struct segment_allocation {
160 void (*allocate_segment)(struct f2fs_sb_info *, int, bool);
161};
162
163struct sit_info {
164 const struct segment_allocation *s_ops;
165
166 block_t sit_base_addr; /* start block address of SIT area */
167 block_t sit_blocks; /* # of blocks used by SIT area */
168 block_t written_valid_blocks; /* # of valid blocks in main area */
169 char *sit_bitmap; /* SIT bitmap pointer */
170 unsigned int bitmap_size; /* SIT bitmap size */
171
172 unsigned long *dirty_sentries_bitmap; /* bitmap for dirty sentries */
173 unsigned int dirty_sentries; /* # of dirty sentries */
174 unsigned int sents_per_block; /* # of SIT entries per block */
175 struct mutex sentry_lock; /* to protect SIT cache */
176 struct seg_entry *sentries; /* SIT segment-level cache */
177 struct sec_entry *sec_entries; /* SIT section-level cache */
178
179 /* for cost-benefit algorithm in cleaning procedure */
180 unsigned long long elapsed_time; /* elapsed time after mount */
181 unsigned long long mounted_time; /* mount time */
182 unsigned long long min_mtime; /* min. modification time */
183 unsigned long long max_mtime; /* max. modification time */
184};
185
186struct free_segmap_info {
187 unsigned int start_segno; /* start segment number logically */
188 unsigned int free_segments; /* # of free segments */
189 unsigned int free_sections; /* # of free sections */
190 rwlock_t segmap_lock; /* free segmap lock */
191 unsigned long *free_segmap; /* free segment bitmap */
192 unsigned long *free_secmap; /* free section bitmap */
193};
194
195/* Notice: The order of dirty type is same with CURSEG_XXX in f2fs.h */
196enum dirty_type {
197 DIRTY_HOT_DATA, /* dirty segments assigned as hot data logs */
198 DIRTY_WARM_DATA, /* dirty segments assigned as warm data logs */
199 DIRTY_COLD_DATA, /* dirty segments assigned as cold data logs */
200 DIRTY_HOT_NODE, /* dirty segments assigned as hot node logs */
201 DIRTY_WARM_NODE, /* dirty segments assigned as warm node logs */
202 DIRTY_COLD_NODE, /* dirty segments assigned as cold node logs */
203 DIRTY, /* to count # of dirty segments */
204 PRE, /* to count # of entirely obsolete segments */
205 NR_DIRTY_TYPE
206};
207
208struct dirty_seglist_info {
209 const struct victim_selection *v_ops; /* victim selction operation */
210 unsigned long *dirty_segmap[NR_DIRTY_TYPE];
211 struct mutex seglist_lock; /* lock for segment bitmaps */
212 int nr_dirty[NR_DIRTY_TYPE]; /* # of dirty segments */
213 unsigned long *victim_segmap[2]; /* BG_GC, FG_GC */
214};
215
216/* victim selection function for cleaning and SSR */
217struct victim_selection {
218 int (*get_victim)(struct f2fs_sb_info *, unsigned int *,
219 int, int, char);
220};
221
222/* for active log information */
223struct curseg_info {
224 struct mutex curseg_mutex; /* lock for consistency */
225 struct f2fs_summary_block *sum_blk; /* cached summary block */
226 unsigned char alloc_type; /* current allocation type */
227 unsigned int segno; /* current segment number */
228 unsigned short next_blkoff; /* next block offset to write */
229 unsigned int zone; /* current zone number */
230 unsigned int next_segno; /* preallocated segment */
231};
232
233/*
234 * inline functions
235 */
236static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
237{
238 return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
239}
240
241static inline struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
242 unsigned int segno)
243{
244 struct sit_info *sit_i = SIT_I(sbi);
245 return &sit_i->sentries[segno];
246}
247
248static inline struct sec_entry *get_sec_entry(struct f2fs_sb_info *sbi,
249 unsigned int segno)
250{
251 struct sit_info *sit_i = SIT_I(sbi);
252 return &sit_i->sec_entries[GET_SECNO(sbi, segno)];
253}
254
255static inline unsigned int get_valid_blocks(struct f2fs_sb_info *sbi,
256 unsigned int segno, int section)
257{
258 /*
259 * In order to get # of valid blocks in a section instantly from many
260 * segments, f2fs manages two counting structures separately.
261 */
262 if (section > 1)
263 return get_sec_entry(sbi, segno)->valid_blocks;
264 else
265 return get_seg_entry(sbi, segno)->valid_blocks;
266}
267
268static inline void seg_info_from_raw_sit(struct seg_entry *se,
269 struct f2fs_sit_entry *rs)
270{
271 se->valid_blocks = GET_SIT_VBLOCKS(rs);
272 se->ckpt_valid_blocks = GET_SIT_VBLOCKS(rs);
273 memcpy(se->cur_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
274 memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
275 se->type = GET_SIT_TYPE(rs);
276 se->mtime = le64_to_cpu(rs->mtime);
277}
278
279static inline void seg_info_to_raw_sit(struct seg_entry *se,
280 struct f2fs_sit_entry *rs)
281{
282 unsigned short raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) |
283 se->valid_blocks;
284 rs->vblocks = cpu_to_le16(raw_vblocks);
285 memcpy(rs->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
286 memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
287 se->ckpt_valid_blocks = se->valid_blocks;
288 rs->mtime = cpu_to_le64(se->mtime);
289}
290
291static inline unsigned int find_next_inuse(struct free_segmap_info *free_i,
292 unsigned int max, unsigned int segno)
293{
294 unsigned int ret;
295 read_lock(&free_i->segmap_lock);
296 ret = find_next_bit(free_i->free_segmap, max, segno);
297 read_unlock(&free_i->segmap_lock);
298 return ret;
299}
300
301static inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
302{
303 struct free_segmap_info *free_i = FREE_I(sbi);
304 unsigned int secno = segno / sbi->segs_per_sec;
305 unsigned int start_segno = secno * sbi->segs_per_sec;
306 unsigned int next;
307
308 write_lock(&free_i->segmap_lock);
309 clear_bit(segno, free_i->free_segmap);
310 free_i->free_segments++;
311
312 next = find_next_bit(free_i->free_segmap, TOTAL_SEGS(sbi), start_segno);
313 if (next >= start_segno + sbi->segs_per_sec) {
314 clear_bit(secno, free_i->free_secmap);
315 free_i->free_sections++;
316 }
317 write_unlock(&free_i->segmap_lock);
318}
319
320static inline void __set_inuse(struct f2fs_sb_info *sbi,
321 unsigned int segno)
322{
323 struct free_segmap_info *free_i = FREE_I(sbi);
324 unsigned int secno = segno / sbi->segs_per_sec;
325 set_bit(segno, free_i->free_segmap);
326 free_i->free_segments--;
327 if (!test_and_set_bit(secno, free_i->free_secmap))
328 free_i->free_sections--;
329}
330
331static inline void __set_test_and_free(struct f2fs_sb_info *sbi,
332 unsigned int segno)
333{
334 struct free_segmap_info *free_i = FREE_I(sbi);
335 unsigned int secno = segno / sbi->segs_per_sec;
336 unsigned int start_segno = secno * sbi->segs_per_sec;
337 unsigned int next;
338
339 write_lock(&free_i->segmap_lock);
340 if (test_and_clear_bit(segno, free_i->free_segmap)) {
341 free_i->free_segments++;
342
343 next = find_next_bit(free_i->free_segmap, TOTAL_SEGS(sbi),
344 start_segno);
345 if (next >= start_segno + sbi->segs_per_sec) {
346 if (test_and_clear_bit(secno, free_i->free_secmap))
347 free_i->free_sections++;
348 }
349 }
350 write_unlock(&free_i->segmap_lock);
351}
352
353static inline void __set_test_and_inuse(struct f2fs_sb_info *sbi,
354 unsigned int segno)
355{
356 struct free_segmap_info *free_i = FREE_I(sbi);
357 unsigned int secno = segno / sbi->segs_per_sec;
358 write_lock(&free_i->segmap_lock);
359 if (!test_and_set_bit(segno, free_i->free_segmap)) {
360 free_i->free_segments--;
361 if (!test_and_set_bit(secno, free_i->free_secmap))
362 free_i->free_sections--;
363 }
364 write_unlock(&free_i->segmap_lock);
365}
366
367static inline void get_sit_bitmap(struct f2fs_sb_info *sbi,
368 void *dst_addr)
369{
370 struct sit_info *sit_i = SIT_I(sbi);
371 memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
372}
373
374static inline block_t written_block_count(struct f2fs_sb_info *sbi)
375{
376 struct sit_info *sit_i = SIT_I(sbi);
377 block_t vblocks;
378
379 mutex_lock(&sit_i->sentry_lock);
380 vblocks = sit_i->written_valid_blocks;
381 mutex_unlock(&sit_i->sentry_lock);
382
383 return vblocks;
384}
385
386static inline unsigned int free_segments(struct f2fs_sb_info *sbi)
387{
388 struct free_segmap_info *free_i = FREE_I(sbi);
389 unsigned int free_segs;
390
391 read_lock(&free_i->segmap_lock);
392 free_segs = free_i->free_segments;
393 read_unlock(&free_i->segmap_lock);
394
395 return free_segs;
396}
397
398static inline int reserved_segments(struct f2fs_sb_info *sbi)
399{
400 return SM_I(sbi)->reserved_segments;
401}
402
403static inline unsigned int free_sections(struct f2fs_sb_info *sbi)
404{
405 struct free_segmap_info *free_i = FREE_I(sbi);
406 unsigned int free_secs;
407
408 read_lock(&free_i->segmap_lock);
409 free_secs = free_i->free_sections;
410 read_unlock(&free_i->segmap_lock);
411
412 return free_secs;
413}
414
415static inline unsigned int prefree_segments(struct f2fs_sb_info *sbi)
416{
417 return DIRTY_I(sbi)->nr_dirty[PRE];
418}
419
420static inline unsigned int dirty_segments(struct f2fs_sb_info *sbi)
421{
422 return DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_DATA] +
423 DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_DATA] +
424 DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_DATA] +
425 DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_NODE] +
426 DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_NODE] +
427 DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_NODE];
428}
429
430static inline int overprovision_segments(struct f2fs_sb_info *sbi)
431{
432 return SM_I(sbi)->ovp_segments;
433}
434
435static inline int overprovision_sections(struct f2fs_sb_info *sbi)
436{
437 return ((unsigned int) overprovision_segments(sbi)) / sbi->segs_per_sec;
438}
439
440static inline int reserved_sections(struct f2fs_sb_info *sbi)
441{
442 return ((unsigned int) reserved_segments(sbi)) / sbi->segs_per_sec;
443}
444
445static inline bool need_SSR(struct f2fs_sb_info *sbi)
446{
447 return (free_sections(sbi) < overprovision_sections(sbi));
448}
449
450static inline int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
451{
452 struct curseg_info *curseg = CURSEG_I(sbi, type);
453 return DIRTY_I(sbi)->v_ops->get_victim(sbi,
454 &(curseg)->next_segno, BG_GC, type, SSR);
455}
456
457static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi)
458{
459 return free_sections(sbi) <= reserved_sections(sbi);
460}
461
462static inline int utilization(struct f2fs_sb_info *sbi)
463{
464 return (long int)valid_user_blocks(sbi) * 100 /
465 (long int)sbi->user_block_count;
466}
467
468/*
469 * Sometimes f2fs may be better to drop out-of-place update policy.
470 * So, if fs utilization is over MIN_IPU_UTIL, then f2fs tries to write
471 * data in the original place likewise other traditional file systems.
472 * But, currently set 100 in percentage, which means it is disabled.
473 * See below need_inplace_update().
474 */
475#define MIN_IPU_UTIL 100
476static inline bool need_inplace_update(struct inode *inode)
477{
478 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
479 if (S_ISDIR(inode->i_mode))
480 return false;
481 if (need_SSR(sbi) && utilization(sbi) > MIN_IPU_UTIL)
482 return true;
483 return false;
484}
485
486static inline unsigned int curseg_segno(struct f2fs_sb_info *sbi,
487 int type)
488{
489 struct curseg_info *curseg = CURSEG_I(sbi, type);
490 return curseg->segno;
491}
492
493static inline unsigned char curseg_alloc_type(struct f2fs_sb_info *sbi,
494 int type)
495{
496 struct curseg_info *curseg = CURSEG_I(sbi, type);
497 return curseg->alloc_type;
498}
499
500static inline unsigned short curseg_blkoff(struct f2fs_sb_info *sbi, int type)
501{
502 struct curseg_info *curseg = CURSEG_I(sbi, type);
503 return curseg->next_blkoff;
504}
505
506static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
507{
508 unsigned int end_segno = SM_I(sbi)->segment_count - 1;
509 BUG_ON(segno > end_segno);
510}
511
512/*
513 * This function is used for only debugging.
514 * NOTE: In future, we have to remove this function.
515 */
516static inline void verify_block_addr(struct f2fs_sb_info *sbi, block_t blk_addr)
517{
518 struct f2fs_sm_info *sm_info = SM_I(sbi);
519 block_t total_blks = sm_info->segment_count << sbi->log_blocks_per_seg;
520 block_t start_addr = sm_info->seg0_blkaddr;
521 block_t end_addr = start_addr + total_blks - 1;
522 BUG_ON(blk_addr < start_addr);
523 BUG_ON(blk_addr > end_addr);
524}
525
526/*
527 * Summary block is always treated as invalid block
528 */
529static inline void check_block_count(struct f2fs_sb_info *sbi,
530 int segno, struct f2fs_sit_entry *raw_sit)
531{
532 struct f2fs_sm_info *sm_info = SM_I(sbi);
533 unsigned int end_segno = sm_info->segment_count - 1;
534 int valid_blocks = 0;
535 int i;
536
537 /* check segment usage */
538 BUG_ON(GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg);
539
540 /* check boundary of a given segment number */
541 BUG_ON(segno > end_segno);
542
543 /* check bitmap with valid block count */
544 for (i = 0; i < sbi->blocks_per_seg; i++)
545 if (f2fs_test_bit(i, raw_sit->valid_map))
546 valid_blocks++;
547 BUG_ON(GET_SIT_VBLOCKS(raw_sit) != valid_blocks);
548}
549
550static inline pgoff_t current_sit_addr(struct f2fs_sb_info *sbi,
551 unsigned int start)
552{
553 struct sit_info *sit_i = SIT_I(sbi);
554 unsigned int offset = SIT_BLOCK_OFFSET(sit_i, start);
555 block_t blk_addr = sit_i->sit_base_addr + offset;
556
557 check_seg_range(sbi, start);
558
559 /* calculate sit block address */
560 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
561 blk_addr += sit_i->sit_blocks;
562
563 return blk_addr;
564}
565
566static inline pgoff_t next_sit_addr(struct f2fs_sb_info *sbi,
567 pgoff_t block_addr)
568{
569 struct sit_info *sit_i = SIT_I(sbi);
570 block_addr -= sit_i->sit_base_addr;
571 if (block_addr < sit_i->sit_blocks)
572 block_addr += sit_i->sit_blocks;
573 else
574 block_addr -= sit_i->sit_blocks;
575
576 return block_addr + sit_i->sit_base_addr;
577}
578
579static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
580{
581 unsigned int block_off = SIT_BLOCK_OFFSET(sit_i, start);
582
583 if (f2fs_test_bit(block_off, sit_i->sit_bitmap))
584 f2fs_clear_bit(block_off, sit_i->sit_bitmap);
585 else
586 f2fs_set_bit(block_off, sit_i->sit_bitmap);
587}
588
589static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi)
590{
591 struct sit_info *sit_i = SIT_I(sbi);
592 return sit_i->elapsed_time + CURRENT_TIME_SEC.tv_sec -
593 sit_i->mounted_time;
594}
595
596static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
597 unsigned int ofs_in_node, unsigned char version)
598{
599 sum->nid = cpu_to_le32(nid);
600 sum->ofs_in_node = cpu_to_le16(ofs_in_node);
601 sum->version = version;
602}
603
604static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
605{
606 return __start_cp_addr(sbi) +
607 le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
608}
609
610static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
611{
612 return __start_cp_addr(sbi) +
613 le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
614 - (base + 1) + type;
615}
diff --git a/include/uapi/linux/magic.h b/include/uapi/linux/magic.h
index e15192cb9cf4..66353ffd06a7 100644
--- a/include/uapi/linux/magic.h
+++ b/include/uapi/linux/magic.h
@@ -23,6 +23,7 @@
23#define EXT4_SUPER_MAGIC 0xEF53 23#define EXT4_SUPER_MAGIC 0xEF53
24#define BTRFS_SUPER_MAGIC 0x9123683E 24#define BTRFS_SUPER_MAGIC 0x9123683E
25#define NILFS_SUPER_MAGIC 0x3434 25#define NILFS_SUPER_MAGIC 0x3434
26#define F2FS_SUPER_MAGIC 0xF2F52010
26#define HPFS_SUPER_MAGIC 0xf995e849 27#define HPFS_SUPER_MAGIC 0xf995e849
27#define ISOFS_SUPER_MAGIC 0x9660 28#define ISOFS_SUPER_MAGIC 0x9660
28#define JFFS2_SUPER_MAGIC 0x72b6 29#define JFFS2_SUPER_MAGIC 0x72b6