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