]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/f2fs/f2fs.h
f2fs: remove unused return value
[mirror_ubuntu-hirsute-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>
c2d715d1 20#include <linux/kobject.h>
7bd59381 21#include <linux/sched.h>
39a53e0c 22
5d56b671 23#ifdef CONFIG_F2FS_CHECK_FS
9850cf4a 24#define f2fs_bug_on(sbi, condition) BUG_ON(condition)
0daaad97 25#define f2fs_down_write(x, y) down_write_nest_lock(x, y)
5d56b671 26#else
9850cf4a
JK
27#define f2fs_bug_on(sbi, condition) \
28 do { \
29 if (unlikely(condition)) { \
30 WARN_ON(1); \
31 sbi->need_fsck = true; \
32 } \
33 } while (0)
0daaad97 34#define f2fs_down_write(x, y) down_write(x)
5d56b671
JK
35#endif
36
39a53e0c
JK
37/*
38 * For mount options
39 */
40#define F2FS_MOUNT_BG_GC 0x00000001
41#define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
42#define F2FS_MOUNT_DISCARD 0x00000004
43#define F2FS_MOUNT_NOHEAP 0x00000008
44#define F2FS_MOUNT_XATTR_USER 0x00000010
45#define F2FS_MOUNT_POSIX_ACL 0x00000020
46#define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
444c580f 47#define F2FS_MOUNT_INLINE_XATTR 0x00000080
1001b347 48#define F2FS_MOUNT_INLINE_DATA 0x00000100
6b4afdd7 49#define F2FS_MOUNT_FLUSH_MERGE 0x00000200
0f7b2abd 50#define F2FS_MOUNT_NOBARRIER 0x00000400
39a53e0c
JK
51
52#define clear_opt(sbi, option) (sbi->mount_opt.opt &= ~F2FS_MOUNT_##option)
53#define set_opt(sbi, option) (sbi->mount_opt.opt |= F2FS_MOUNT_##option)
54#define test_opt(sbi, option) (sbi->mount_opt.opt & F2FS_MOUNT_##option)
55
56#define ver_after(a, b) (typecheck(unsigned long long, a) && \
57 typecheck(unsigned long long, b) && \
58 ((long long)((a) - (b)) > 0))
59
a9841c4d
JK
60typedef u32 block_t; /*
61 * should not change u32, since it is the on-disk block
62 * address format, __le32.
63 */
39a53e0c
JK
64typedef u32 nid_t;
65
66struct f2fs_mount_info {
67 unsigned int opt;
68};
69
7e586fa0
JK
70#define CRCPOLY_LE 0xedb88320
71
72static inline __u32 f2fs_crc32(void *buf, size_t len)
39a53e0c 73{
7e586fa0
JK
74 unsigned char *p = (unsigned char *)buf;
75 __u32 crc = F2FS_SUPER_MAGIC;
76 int i;
77
78 while (len--) {
79 crc ^= *p++;
80 for (i = 0; i < 8; i++)
81 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
82 }
83 return crc;
39a53e0c
JK
84}
85
7e586fa0 86static inline bool f2fs_crc_valid(__u32 blk_crc, void *buf, size_t buf_size)
39a53e0c 87{
7e586fa0 88 return f2fs_crc32(buf, buf_size) == blk_crc;
39a53e0c
JK
89}
90
91/*
92 * For checkpoint manager
93 */
94enum {
95 NAT_BITMAP,
96 SIT_BITMAP
97};
98
75ab4cb8
JK
99enum {
100 CP_UMOUNT,
101 CP_SYNC,
4b2fecc8 102 CP_DISCARD,
75ab4cb8
JK
103};
104
105struct cp_control {
106 int reason;
4b2fecc8
JK
107 __u64 trim_start;
108 __u64 trim_end;
109 __u64 trim_minlen;
110 __u64 trimmed;
75ab4cb8
JK
111};
112
662befda 113/*
81c1a0f1 114 * For CP/NAT/SIT/SSA readahead
662befda
CY
115 */
116enum {
117 META_CP,
118 META_NAT,
81c1a0f1 119 META_SIT,
4c521f49
JK
120 META_SSA,
121 META_POR,
662befda
CY
122};
123
6451e041
JK
124/* for the list of ino */
125enum {
126 ORPHAN_INO, /* for orphan ino list */
fff04f90
JK
127 APPEND_INO, /* for append ino list */
128 UPDATE_INO, /* for update ino list */
6451e041
JK
129 MAX_INO_ENTRY, /* max. list */
130};
131
132struct ino_entry {
39a53e0c
JK
133 struct list_head list; /* list head */
134 nid_t ino; /* inode number */
135};
136
137/* for the list of directory inodes */
138struct dir_inode_entry {
139 struct list_head list; /* list head */
140 struct inode *inode; /* vfs inode pointer */
141};
142
7fd9e544
JK
143/* for the list of blockaddresses to be discarded */
144struct discard_entry {
145 struct list_head list; /* list head */
146 block_t blkaddr; /* block address to be discarded */
147 int len; /* # of consecutive blocks of the discard */
148};
149
39a53e0c
JK
150/* for the list of fsync inodes, used only during recovery */
151struct fsync_inode_entry {
152 struct list_head list; /* list head */
153 struct inode *inode; /* vfs inode pointer */
c52e1b10
JK
154 block_t blkaddr; /* block address locating the last fsync */
155 block_t last_dentry; /* block address locating the last dentry */
156 block_t last_inode; /* block address locating the last inode */
39a53e0c
JK
157};
158
159#define nats_in_cursum(sum) (le16_to_cpu(sum->n_nats))
160#define sits_in_cursum(sum) (le16_to_cpu(sum->n_sits))
161
162#define nat_in_journal(sum, i) (sum->nat_j.entries[i].ne)
163#define nid_in_journal(sum, i) (sum->nat_j.entries[i].nid)
164#define sit_in_journal(sum, i) (sum->sit_j.entries[i].se)
165#define segno_in_journal(sum, i) (sum->sit_j.entries[i].segno)
166
309cc2b6
JK
167#define MAX_NAT_JENTRIES(sum) (NAT_JOURNAL_ENTRIES - nats_in_cursum(sum))
168#define MAX_SIT_JENTRIES(sum) (SIT_JOURNAL_ENTRIES - sits_in_cursum(sum))
169
39a53e0c
JK
170static inline int update_nats_in_cursum(struct f2fs_summary_block *rs, int i)
171{
172 int before = nats_in_cursum(rs);
173 rs->n_nats = cpu_to_le16(before + i);
174 return before;
175}
176
177static inline int update_sits_in_cursum(struct f2fs_summary_block *rs, int i)
178{
179 int before = sits_in_cursum(rs);
180 rs->n_sits = cpu_to_le16(before + i);
181 return before;
182}
183
184a5cd2
CY
184static inline bool __has_cursum_space(struct f2fs_summary_block *sum, int size,
185 int type)
186{
187 if (type == NAT_JOURNAL)
309cc2b6
JK
188 return size <= MAX_NAT_JENTRIES(sum);
189 return size <= MAX_SIT_JENTRIES(sum);
184a5cd2
CY
190}
191
e9750824
NJ
192/*
193 * ioctl commands
194 */
195#define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
196#define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
197
198#if defined(__KERNEL__) && defined(CONFIG_COMPAT)
199/*
200 * ioctl commands in 32 bit emulation
201 */
202#define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
203#define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
204#endif
205
39a53e0c
JK
206/*
207 * For INODE and NODE manager
208 */
dbe6a5ff
JK
209/*
210 * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
211 * as its node offset to distinguish from index node blocks.
212 * But some bits are used to mark the node block.
213 */
214#define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
215 >> OFFSET_BIT_SHIFT)
266e97a8
JK
216enum {
217 ALLOC_NODE, /* allocate a new node page if needed */
218 LOOKUP_NODE, /* look up a node without readahead */
219 LOOKUP_NODE_RA, /*
220 * look up a node with readahead called
4f4124d0 221 * by get_data_block.
39a53e0c 222 */
266e97a8
JK
223};
224
39a53e0c
JK
225#define F2FS_LINK_MAX 32000 /* maximum link count per file */
226
817202d9
CY
227#define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
228
39a53e0c 229/* for in-memory extent cache entry */
c11abd1a
JK
230#define F2FS_MIN_EXTENT_LEN 16 /* minimum extent length */
231
39a53e0c
JK
232struct extent_info {
233 rwlock_t ext_lock; /* rwlock for consistency */
234 unsigned int fofs; /* start offset in a file */
235 u32 blk_addr; /* start block address of the extent */
111d2495 236 unsigned int len; /* length of the extent */
39a53e0c
JK
237};
238
239/*
240 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
241 */
242#define FADVISE_COLD_BIT 0x01
354a3399 243#define FADVISE_LOST_PINO_BIT 0x02
39a53e0c 244
ab9fa662
JK
245#define DEF_DIR_LEVEL 0
246
39a53e0c
JK
247struct f2fs_inode_info {
248 struct inode vfs_inode; /* serve a vfs inode */
249 unsigned long i_flags; /* keep an inode flags for ioctl */
250 unsigned char i_advise; /* use to give file attribute hints */
38431545 251 unsigned char i_dir_level; /* use for dentry level for large dir */
39a53e0c 252 unsigned int i_current_depth; /* use only in directory structure */
6666e6aa 253 unsigned int i_pino; /* parent inode number */
39a53e0c
JK
254 umode_t i_acl_mode; /* keep file acl mode temporarily */
255
256 /* Use below internally in f2fs*/
257 unsigned long flags; /* use to pass per-file flags */
d928bfbf 258 struct rw_semaphore i_sem; /* protect fi info */
a7ffdbe2 259 atomic_t dirty_pages; /* # of dirty pages */
39a53e0c
JK
260 f2fs_hash_t chash; /* hash value of given file name */
261 unsigned int clevel; /* maximum level of given file name */
262 nid_t i_xattr_nid; /* node id that contains xattrs */
e518ff81 263 unsigned long long xattr_ver; /* cp version of xattr modification */
39a53e0c 264 struct extent_info ext; /* in-memory extent cache entry */
ed57c27f 265 struct dir_inode_entry *dirty_dir; /* the pointer of dirty dir */
39a53e0c
JK
266};
267
268static inline void get_extent_info(struct extent_info *ext,
269 struct f2fs_extent i_ext)
270{
271 write_lock(&ext->ext_lock);
272 ext->fofs = le32_to_cpu(i_ext.fofs);
273 ext->blk_addr = le32_to_cpu(i_ext.blk_addr);
274 ext->len = le32_to_cpu(i_ext.len);
275 write_unlock(&ext->ext_lock);
276}
277
278static inline void set_raw_extent(struct extent_info *ext,
279 struct f2fs_extent *i_ext)
280{
281 read_lock(&ext->ext_lock);
282 i_ext->fofs = cpu_to_le32(ext->fofs);
283 i_ext->blk_addr = cpu_to_le32(ext->blk_addr);
284 i_ext->len = cpu_to_le32(ext->len);
285 read_unlock(&ext->ext_lock);
286}
287
288struct f2fs_nm_info {
289 block_t nat_blkaddr; /* base disk address of NAT */
290 nid_t max_nid; /* maximum possible node ids */
7ee0eeab 291 nid_t available_nids; /* maximum available node ids */
39a53e0c 292 nid_t next_scan_nid; /* the next nid to be scanned */
cdfc41c1 293 unsigned int ram_thresh; /* control the memory footprint */
39a53e0c
JK
294
295 /* NAT cache management */
296 struct radix_tree_root nat_root;/* root of the nat entry cache */
309cc2b6 297 struct radix_tree_root nat_set_root;/* root of the nat set cache */
39a53e0c 298 rwlock_t nat_tree_lock; /* protect nat_tree_lock */
39a53e0c 299 struct list_head nat_entries; /* cached nat entry list (clean) */
309cc2b6 300 unsigned int nat_cnt; /* the # of cached nat entries */
aec71382 301 unsigned int dirty_nat_cnt; /* total num of nat entries in set */
39a53e0c
JK
302
303 /* free node ids management */
8a7ed66a 304 struct radix_tree_root free_nid_root;/* root of the free_nid cache */
39a53e0c
JK
305 struct list_head free_nid_list; /* a list for free nids */
306 spinlock_t free_nid_list_lock; /* protect free nid list */
307 unsigned int fcnt; /* the number of free node id */
308 struct mutex build_lock; /* lock for build free nids */
309
310 /* for checkpoint */
311 char *nat_bitmap; /* NAT bitmap pointer */
312 int bitmap_size; /* bitmap size */
313};
314
315/*
316 * this structure is used as one of function parameters.
317 * all the information are dedicated to a given direct node block determined
318 * by the data offset in a file.
319 */
320struct dnode_of_data {
321 struct inode *inode; /* vfs inode pointer */
322 struct page *inode_page; /* its inode page, NULL is possible */
323 struct page *node_page; /* cached direct node page */
324 nid_t nid; /* node id of the direct node block */
325 unsigned int ofs_in_node; /* data offset in the node page */
326 bool inode_page_locked; /* inode page is locked or not */
327 block_t data_blkaddr; /* block address of the node block */
328};
329
330static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
331 struct page *ipage, struct page *npage, nid_t nid)
332{
d66d1f76 333 memset(dn, 0, sizeof(*dn));
39a53e0c
JK
334 dn->inode = inode;
335 dn->inode_page = ipage;
336 dn->node_page = npage;
337 dn->nid = nid;
39a53e0c
JK
338}
339
340/*
341 * For SIT manager
342 *
343 * By default, there are 6 active log areas across the whole main area.
344 * When considering hot and cold data separation to reduce cleaning overhead,
345 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
346 * respectively.
347 * In the current design, you should not change the numbers intentionally.
348 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
349 * logs individually according to the underlying devices. (default: 6)
350 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
351 * data and 8 for node logs.
352 */
353#define NR_CURSEG_DATA_TYPE (3)
354#define NR_CURSEG_NODE_TYPE (3)
355#define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
356
357enum {
358 CURSEG_HOT_DATA = 0, /* directory entry blocks */
359 CURSEG_WARM_DATA, /* data blocks */
360 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
361 CURSEG_HOT_NODE, /* direct node blocks of directory files */
362 CURSEG_WARM_NODE, /* direct node blocks of normal files */
363 CURSEG_COLD_NODE, /* indirect node blocks */
364 NO_CHECK_TYPE
365};
366
6b4afdd7 367struct flush_cmd {
6b4afdd7 368 struct completion wait;
721bd4d5 369 struct llist_node llnode;
6b4afdd7
JK
370 int ret;
371};
372
a688b9d9
GZ
373struct flush_cmd_control {
374 struct task_struct *f2fs_issue_flush; /* flush thread */
375 wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
721bd4d5
GZ
376 struct llist_head issue_list; /* list for command issue */
377 struct llist_node *dispatch_list; /* list for command dispatch */
a688b9d9
GZ
378};
379
39a53e0c
JK
380struct f2fs_sm_info {
381 struct sit_info *sit_info; /* whole segment information */
382 struct free_segmap_info *free_info; /* free segment information */
383 struct dirty_seglist_info *dirty_info; /* dirty segment information */
384 struct curseg_info *curseg_array; /* active segment information */
385
39a53e0c
JK
386 block_t seg0_blkaddr; /* block address of 0'th segment */
387 block_t main_blkaddr; /* start block address of main area */
388 block_t ssa_blkaddr; /* start block address of SSA area */
389
390 unsigned int segment_count; /* total # of segments */
391 unsigned int main_segments; /* # of segments in main area */
392 unsigned int reserved_segments; /* # of reserved segments */
393 unsigned int ovp_segments; /* # of overprovision segments */
81eb8d6e
JK
394
395 /* a threshold to reclaim prefree segments */
396 unsigned int rec_prefree_segments;
7fd9e544
JK
397
398 /* for small discard management */
399 struct list_head discard_list; /* 4KB discard list */
400 int nr_discards; /* # of discards in the list */
401 int max_discards; /* max. discards to be issued */
216fbd64 402
184a5cd2
CY
403 struct list_head sit_entry_set; /* sit entry set list */
404
216fbd64
JK
405 unsigned int ipu_policy; /* in-place-update policy */
406 unsigned int min_ipu_util; /* in-place-update threshold */
c1ce1b02 407 unsigned int min_fsync_blocks; /* threshold for fsync */
6b4afdd7
JK
408
409 /* for flush command control */
a688b9d9
GZ
410 struct flush_cmd_control *cmd_control_info;
411
39a53e0c
JK
412};
413
39a53e0c
JK
414/*
415 * For superblock
416 */
417/*
418 * COUNT_TYPE for monitoring
419 *
420 * f2fs monitors the number of several block types such as on-writeback,
421 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
422 */
423enum count_type {
424 F2FS_WRITEBACK,
425 F2FS_DIRTY_DENTS,
426 F2FS_DIRTY_NODES,
427 F2FS_DIRTY_META,
428 NR_COUNT_TYPE,
429};
430
39a53e0c 431/*
e1c42045 432 * The below are the page types of bios used in submit_bio().
39a53e0c
JK
433 * The available types are:
434 * DATA User data pages. It operates as async mode.
435 * NODE Node pages. It operates as async mode.
436 * META FS metadata pages such as SIT, NAT, CP.
437 * NR_PAGE_TYPE The number of page types.
438 * META_FLUSH Make sure the previous pages are written
439 * with waiting the bio's completion
440 * ... Only can be used with META.
441 */
7d5e5109 442#define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
39a53e0c
JK
443enum page_type {
444 DATA,
445 NODE,
446 META,
447 NR_PAGE_TYPE,
448 META_FLUSH,
449};
450
458e6197 451struct f2fs_io_info {
7e8f2308
GZ
452 enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
453 int rw; /* contains R/RS/W/WS with REQ_META/REQ_PRIO */
458e6197
JK
454};
455
93dfe2ac 456#define is_read_io(rw) (((rw) & 1) == READ)
1ff7bd3b 457struct f2fs_bio_info {
458e6197 458 struct f2fs_sb_info *sbi; /* f2fs superblock */
1ff7bd3b
JK
459 struct bio *bio; /* bios to merge */
460 sector_t last_block_in_bio; /* last block number */
458e6197 461 struct f2fs_io_info fio; /* store buffered io info. */
df0f8dc0 462 struct rw_semaphore io_rwsem; /* blocking op for bio */
1ff7bd3b
JK
463};
464
39a53e0c
JK
465struct f2fs_sb_info {
466 struct super_block *sb; /* pointer to VFS super block */
5e176d54 467 struct proc_dir_entry *s_proc; /* proc entry */
39a53e0c
JK
468 struct buffer_head *raw_super_buf; /* buffer head of raw sb */
469 struct f2fs_super_block *raw_super; /* raw super block pointer */
470 int s_dirty; /* dirty flag for checkpoint */
2ae4c673 471 bool need_fsck; /* need fsck.f2fs to fix */
39a53e0c
JK
472
473 /* for node-related operations */
474 struct f2fs_nm_info *nm_info; /* node manager */
475 struct inode *node_inode; /* cache node blocks */
476
477 /* for segment-related operations */
478 struct f2fs_sm_info *sm_info; /* segment manager */
1ff7bd3b
JK
479
480 /* for bio operations */
924b720b 481 struct f2fs_bio_info read_io; /* for read bios */
1ff7bd3b 482 struct f2fs_bio_info write_io[NR_PAGE_TYPE]; /* for write bios */
1b1f559f 483 struct completion *wait_io; /* for completion bios */
39a53e0c
JK
484
485 /* for checkpoint */
486 struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
487 struct inode *meta_inode; /* cache meta blocks */
39936837 488 struct mutex cp_mutex; /* checkpoint procedure lock */
e479556b 489 struct rw_semaphore cp_rwsem; /* blocking FS operations */
b3582c68 490 struct rw_semaphore node_write; /* locking node writes */
39a53e0c 491 struct mutex writepages; /* mutex for writepages() */
aabe5136 492 bool por_doing; /* recovery is doing or not */
fb51b5ef 493 wait_queue_head_t cp_wait;
39a53e0c 494
6451e041 495 /* for inode management */
39efac41 496 struct radix_tree_root ino_root[MAX_INO_ENTRY]; /* ino entry array */
6451e041
JK
497 spinlock_t ino_lock[MAX_INO_ENTRY]; /* for ino entry lock */
498 struct list_head ino_list[MAX_INO_ENTRY]; /* inode list head */
499
500 /* for orphan inode, use 0'th array */
39a53e0c 501 unsigned int n_orphans; /* # of orphan inodes */
0d47c1ad 502 unsigned int max_orphans; /* max orphan inodes */
39a53e0c
JK
503
504 /* for directory inode management */
505 struct list_head dir_inode_list; /* dir inode list */
506 spinlock_t dir_inode_lock; /* for dir inode list lock */
39a53e0c 507
e1c42045 508 /* basic filesystem units */
39a53e0c
JK
509 unsigned int log_sectors_per_block; /* log2 sectors per block */
510 unsigned int log_blocksize; /* log2 block size */
511 unsigned int blocksize; /* block size */
512 unsigned int root_ino_num; /* root inode number*/
513 unsigned int node_ino_num; /* node inode number*/
514 unsigned int meta_ino_num; /* meta inode number*/
515 unsigned int log_blocks_per_seg; /* log2 blocks per segment */
516 unsigned int blocks_per_seg; /* blocks per segment */
517 unsigned int segs_per_sec; /* segments per section */
518 unsigned int secs_per_zone; /* sections per zone */
519 unsigned int total_sections; /* total section count */
520 unsigned int total_node_count; /* total node block count */
521 unsigned int total_valid_node_count; /* valid node block count */
522 unsigned int total_valid_inode_count; /* valid inode count */
523 int active_logs; /* # of active logs */
ab9fa662 524 int dir_level; /* directory level */
39a53e0c
JK
525
526 block_t user_block_count; /* # of user blocks */
527 block_t total_valid_block_count; /* # of valid blocks */
528 block_t alloc_valid_block_count; /* # of allocated blocks */
529 block_t last_valid_block_count; /* for recovery */
530 u32 s_next_generation; /* for NFS support */
531 atomic_t nr_pages[NR_COUNT_TYPE]; /* # of pages, see count_type */
532
533 struct f2fs_mount_info mount_opt; /* mount options */
534
535 /* for cleaning operations */
536 struct mutex gc_mutex; /* mutex for GC */
537 struct f2fs_gc_kthread *gc_thread; /* GC thread */
5ec4e49f 538 unsigned int cur_victim_sec; /* current victim section num */
39a53e0c 539
b1c57c1c
JK
540 /* maximum # of trials to find a victim segment for SSR and GC */
541 unsigned int max_victim_search;
542
39a53e0c
JK
543 /*
544 * for stat information.
545 * one is for the LFS mode, and the other is for the SSR mode.
546 */
35b09d82 547#ifdef CONFIG_F2FS_STAT_FS
39a53e0c
JK
548 struct f2fs_stat_info *stat_info; /* FS status information */
549 unsigned int segment_count[2]; /* # of allocated segments */
550 unsigned int block_count[2]; /* # of allocated blocks */
39a53e0c 551 int total_hit_ext, read_hit_ext; /* extent cache hit ratio */
0dbdc2ae 552 int inline_inode; /* # of inline_data inodes */
39a53e0c 553 int bg_gc; /* background gc calls */
35b09d82
NJ
554 unsigned int n_dirty_dirs; /* # of dir inodes */
555#endif
556 unsigned int last_victim[2]; /* last victim segment # */
39a53e0c 557 spinlock_t stat_lock; /* lock for stat operations */
b59d0bae
NJ
558
559 /* For sysfs suppport */
560 struct kobject s_kobj;
561 struct completion s_kobj_unregister;
39a53e0c
JK
562};
563
564/*
565 * Inline functions
566 */
567static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
568{
569 return container_of(inode, struct f2fs_inode_info, vfs_inode);
570}
571
572static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
573{
574 return sb->s_fs_info;
575}
576
4081363f
JK
577static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
578{
579 return F2FS_SB(inode->i_sb);
580}
581
582static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
583{
584 return F2FS_I_SB(mapping->host);
585}
586
587static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
588{
589 return F2FS_M_SB(page->mapping);
590}
591
39a53e0c
JK
592static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
593{
594 return (struct f2fs_super_block *)(sbi->raw_super);
595}
596
597static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
598{
599 return (struct f2fs_checkpoint *)(sbi->ckpt);
600}
601
45590710
GZ
602static inline struct f2fs_node *F2FS_NODE(struct page *page)
603{
604 return (struct f2fs_node *)page_address(page);
605}
606
58bfaf44
JK
607static inline struct f2fs_inode *F2FS_INODE(struct page *page)
608{
609 return &((struct f2fs_node *)page_address(page))->i;
610}
611
39a53e0c
JK
612static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
613{
614 return (struct f2fs_nm_info *)(sbi->nm_info);
615}
616
617static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
618{
619 return (struct f2fs_sm_info *)(sbi->sm_info);
620}
621
622static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
623{
624 return (struct sit_info *)(SM_I(sbi)->sit_info);
625}
626
627static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
628{
629 return (struct free_segmap_info *)(SM_I(sbi)->free_info);
630}
631
632static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
633{
634 return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
635}
636
9df27d98
GZ
637static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
638{
639 return sbi->meta_inode->i_mapping;
640}
641
4ef51a8f
JK
642static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
643{
644 return sbi->node_inode->i_mapping;
645}
646
39a53e0c
JK
647static inline void F2FS_SET_SB_DIRT(struct f2fs_sb_info *sbi)
648{
649 sbi->s_dirty = 1;
650}
651
652static inline void F2FS_RESET_SB_DIRT(struct f2fs_sb_info *sbi)
653{
654 sbi->s_dirty = 0;
655}
656
d71b5564
JK
657static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
658{
659 return le64_to_cpu(cp->checkpoint_ver);
660}
661
25ca923b
JK
662static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
663{
664 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
665 return ckpt_flags & f;
666}
667
668static inline void set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
669{
670 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
671 ckpt_flags |= f;
672 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
673}
674
675static inline void clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
676{
677 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
678 ckpt_flags &= (~f);
679 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
680}
681
e479556b 682static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
39936837 683{
e479556b 684 down_read(&sbi->cp_rwsem);
39936837
JK
685}
686
e479556b 687static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
39a53e0c 688{
e479556b 689 up_read(&sbi->cp_rwsem);
39a53e0c
JK
690}
691
e479556b 692static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
39a53e0c 693{
0daaad97 694 f2fs_down_write(&sbi->cp_rwsem, &sbi->cp_mutex);
39936837
JK
695}
696
e479556b 697static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
39936837 698{
e479556b 699 up_write(&sbi->cp_rwsem);
39a53e0c
JK
700}
701
702/*
703 * Check whether the given nid is within node id range.
704 */
064e0823 705static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
39a53e0c 706{
d6b7d4b3
CY
707 if (unlikely(nid < F2FS_ROOT_INO(sbi)))
708 return -EINVAL;
cfb271d4 709 if (unlikely(nid >= NM_I(sbi)->max_nid))
064e0823
NJ
710 return -EINVAL;
711 return 0;
39a53e0c
JK
712}
713
714#define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
715
716/*
717 * Check whether the inode has blocks or not
718 */
719static inline int F2FS_HAS_BLOCKS(struct inode *inode)
720{
721 if (F2FS_I(inode)->i_xattr_nid)
6c311ec6 722 return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1;
39a53e0c 723 else
6c311ec6 724 return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS;
39a53e0c
JK
725}
726
4bc8e9bc
CY
727static inline bool f2fs_has_xattr_block(unsigned int ofs)
728{
729 return ofs == XATTR_NODE_OFFSET;
730}
731
39a53e0c
JK
732static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
733 struct inode *inode, blkcnt_t count)
734{
735 block_t valid_block_count;
736
737 spin_lock(&sbi->stat_lock);
738 valid_block_count =
739 sbi->total_valid_block_count + (block_t)count;
cfb271d4 740 if (unlikely(valid_block_count > sbi->user_block_count)) {
39a53e0c
JK
741 spin_unlock(&sbi->stat_lock);
742 return false;
743 }
744 inode->i_blocks += count;
745 sbi->total_valid_block_count = valid_block_count;
746 sbi->alloc_valid_block_count += (block_t)count;
747 spin_unlock(&sbi->stat_lock);
748 return true;
749}
750
da19b0dc 751static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
39a53e0c
JK
752 struct inode *inode,
753 blkcnt_t count)
754{
755 spin_lock(&sbi->stat_lock);
9850cf4a
JK
756 f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
757 f2fs_bug_on(sbi, inode->i_blocks < count);
39a53e0c
JK
758 inode->i_blocks -= count;
759 sbi->total_valid_block_count -= (block_t)count;
760 spin_unlock(&sbi->stat_lock);
39a53e0c
JK
761}
762
763static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
764{
765 atomic_inc(&sbi->nr_pages[count_type]);
766 F2FS_SET_SB_DIRT(sbi);
767}
768
a7ffdbe2 769static inline void inode_inc_dirty_pages(struct inode *inode)
39a53e0c 770{
a7ffdbe2
JK
771 atomic_inc(&F2FS_I(inode)->dirty_pages);
772 if (S_ISDIR(inode->i_mode))
773 inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_DENTS);
39a53e0c
JK
774}
775
776static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
777{
778 atomic_dec(&sbi->nr_pages[count_type]);
779}
780
a7ffdbe2 781static inline void inode_dec_dirty_pages(struct inode *inode)
39a53e0c 782{
a7ffdbe2 783 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode))
1fe54f9d
JK
784 return;
785
a7ffdbe2
JK
786 atomic_dec(&F2FS_I(inode)->dirty_pages);
787
788 if (S_ISDIR(inode->i_mode))
789 dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_DENTS);
39a53e0c
JK
790}
791
792static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
793{
794 return atomic_read(&sbi->nr_pages[count_type]);
795}
796
a7ffdbe2 797static inline int get_dirty_pages(struct inode *inode)
f8b2c1f9 798{
a7ffdbe2 799 return atomic_read(&F2FS_I(inode)->dirty_pages);
f8b2c1f9
JK
800}
801
5ac206cf
NJ
802static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
803{
804 unsigned int pages_per_sec = sbi->segs_per_sec *
805 (1 << sbi->log_blocks_per_seg);
806 return ((get_pages(sbi, block_type) + pages_per_sec - 1)
807 >> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
808}
809
39a53e0c
JK
810static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
811{
8b8343fa 812 return sbi->total_valid_block_count;
39a53e0c
JK
813}
814
815static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
816{
817 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
818
819 /* return NAT or SIT bitmap */
820 if (flag == NAT_BITMAP)
821 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
822 else if (flag == SIT_BITMAP)
823 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
824
825 return 0;
826}
827
828static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
829{
830 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1dbe4152
CL
831 int offset;
832
833 if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload) > 0) {
834 if (flag == NAT_BITMAP)
835 return &ckpt->sit_nat_version_bitmap;
836 else
65b85ccc 837 return (unsigned char *)ckpt + F2FS_BLKSIZE;
1dbe4152
CL
838 } else {
839 offset = (flag == NAT_BITMAP) ?
25ca923b 840 le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
1dbe4152
CL
841 return &ckpt->sit_nat_version_bitmap + offset;
842 }
39a53e0c
JK
843}
844
845static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
846{
847 block_t start_addr;
848 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
d71b5564 849 unsigned long long ckpt_version = cur_cp_version(ckpt);
39a53e0c 850
25ca923b 851 start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
39a53e0c
JK
852
853 /*
854 * odd numbered checkpoint should at cp segment 0
e1c42045 855 * and even segment must be at cp segment 1
39a53e0c
JK
856 */
857 if (!(ckpt_version & 1))
858 start_addr += sbi->blocks_per_seg;
859
860 return start_addr;
861}
862
863static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
864{
865 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
866}
867
868static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
ef86d709 869 struct inode *inode)
39a53e0c
JK
870{
871 block_t valid_block_count;
872 unsigned int valid_node_count;
873
874 spin_lock(&sbi->stat_lock);
875
ef86d709 876 valid_block_count = sbi->total_valid_block_count + 1;
cfb271d4 877 if (unlikely(valid_block_count > sbi->user_block_count)) {
39a53e0c
JK
878 spin_unlock(&sbi->stat_lock);
879 return false;
880 }
881
ef86d709 882 valid_node_count = sbi->total_valid_node_count + 1;
cfb271d4 883 if (unlikely(valid_node_count > sbi->total_node_count)) {
39a53e0c
JK
884 spin_unlock(&sbi->stat_lock);
885 return false;
886 }
887
888 if (inode)
ef86d709
GZ
889 inode->i_blocks++;
890
891 sbi->alloc_valid_block_count++;
892 sbi->total_valid_node_count++;
893 sbi->total_valid_block_count++;
39a53e0c
JK
894 spin_unlock(&sbi->stat_lock);
895
896 return true;
897}
898
899static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
ef86d709 900 struct inode *inode)
39a53e0c
JK
901{
902 spin_lock(&sbi->stat_lock);
903
9850cf4a
JK
904 f2fs_bug_on(sbi, !sbi->total_valid_block_count);
905 f2fs_bug_on(sbi, !sbi->total_valid_node_count);
906 f2fs_bug_on(sbi, !inode->i_blocks);
39a53e0c 907
ef86d709
GZ
908 inode->i_blocks--;
909 sbi->total_valid_node_count--;
910 sbi->total_valid_block_count--;
39a53e0c
JK
911
912 spin_unlock(&sbi->stat_lock);
913}
914
915static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
916{
8b8343fa 917 return sbi->total_valid_node_count;
39a53e0c
JK
918}
919
920static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
921{
922 spin_lock(&sbi->stat_lock);
9850cf4a 923 f2fs_bug_on(sbi, sbi->total_valid_inode_count == sbi->total_node_count);
39a53e0c
JK
924 sbi->total_valid_inode_count++;
925 spin_unlock(&sbi->stat_lock);
926}
927
0e80220a 928static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
39a53e0c
JK
929{
930 spin_lock(&sbi->stat_lock);
9850cf4a 931 f2fs_bug_on(sbi, !sbi->total_valid_inode_count);
39a53e0c
JK
932 sbi->total_valid_inode_count--;
933 spin_unlock(&sbi->stat_lock);
39a53e0c
JK
934}
935
936static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
937{
8b8343fa 938 return sbi->total_valid_inode_count;
39a53e0c
JK
939}
940
941static inline void f2fs_put_page(struct page *page, int unlock)
942{
031fa8cc 943 if (!page)
39a53e0c
JK
944 return;
945
946 if (unlock) {
9850cf4a 947 f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
39a53e0c
JK
948 unlock_page(page);
949 }
950 page_cache_release(page);
951}
952
953static inline void f2fs_put_dnode(struct dnode_of_data *dn)
954{
955 if (dn->node_page)
956 f2fs_put_page(dn->node_page, 1);
957 if (dn->inode_page && dn->node_page != dn->inode_page)
958 f2fs_put_page(dn->inode_page, 0);
959 dn->node_page = NULL;
960 dn->inode_page = NULL;
961}
962
963static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
e8512d2e 964 size_t size)
39a53e0c 965{
e8512d2e 966 return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
39a53e0c
JK
967}
968
7bd59381
GZ
969static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
970 gfp_t flags)
971{
972 void *entry;
973retry:
974 entry = kmem_cache_alloc(cachep, flags);
975 if (!entry) {
976 cond_resched();
977 goto retry;
978 }
979
980 return entry;
981}
982
39a53e0c
JK
983#define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
984
985static inline bool IS_INODE(struct page *page)
986{
45590710 987 struct f2fs_node *p = F2FS_NODE(page);
39a53e0c
JK
988 return RAW_IS_INODE(p);
989}
990
991static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
992{
993 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
994}
995
996static inline block_t datablock_addr(struct page *node_page,
997 unsigned int offset)
998{
999 struct f2fs_node *raw_node;
1000 __le32 *addr_array;
45590710 1001 raw_node = F2FS_NODE(node_page);
39a53e0c
JK
1002 addr_array = blkaddr_in_node(raw_node);
1003 return le32_to_cpu(addr_array[offset]);
1004}
1005
1006static inline int f2fs_test_bit(unsigned int nr, char *addr)
1007{
1008 int mask;
1009
1010 addr += (nr >> 3);
1011 mask = 1 << (7 - (nr & 0x07));
1012 return mask & *addr;
1013}
1014
1015static inline int f2fs_set_bit(unsigned int nr, char *addr)
1016{
1017 int mask;
1018 int ret;
1019
1020 addr += (nr >> 3);
1021 mask = 1 << (7 - (nr & 0x07));
1022 ret = mask & *addr;
1023 *addr |= mask;
1024 return ret;
1025}
1026
1027static inline int f2fs_clear_bit(unsigned int nr, char *addr)
1028{
1029 int mask;
1030 int ret;
1031
1032 addr += (nr >> 3);
1033 mask = 1 << (7 - (nr & 0x07));
1034 ret = mask & *addr;
1035 *addr &= ~mask;
1036 return ret;
1037}
1038
1039/* used for f2fs_inode_info->flags */
1040enum {
1041 FI_NEW_INODE, /* indicate newly allocated inode */
b3783873 1042 FI_DIRTY_INODE, /* indicate inode is dirty or not */
ed57c27f 1043 FI_DIRTY_DIR, /* indicate directory has dirty pages */
39a53e0c
JK
1044 FI_INC_LINK, /* need to increment i_nlink */
1045 FI_ACL_MODE, /* indicate acl mode */
1046 FI_NO_ALLOC, /* should not allocate any blocks */
699489bb 1047 FI_UPDATE_DIR, /* should update inode block for consistency */
74d0b917 1048 FI_DELAY_IPUT, /* used for the recovery */
c11abd1a 1049 FI_NO_EXTENT, /* not to use the extent cache */
444c580f 1050 FI_INLINE_XATTR, /* used for inline xattr */
1001b347 1051 FI_INLINE_DATA, /* used for inline data*/
fff04f90
JK
1052 FI_APPEND_WRITE, /* inode has appended data */
1053 FI_UPDATE_WRITE, /* inode has in-place-update data */
ea1aa12c 1054 FI_NEED_IPU, /* used fo ipu for fdatasync */
39a53e0c
JK
1055};
1056
1057static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
1058{
61e0f2d0
JK
1059 if (!test_bit(flag, &fi->flags))
1060 set_bit(flag, &fi->flags);
39a53e0c
JK
1061}
1062
1063static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
1064{
1065 return test_bit(flag, &fi->flags);
1066}
1067
1068static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
1069{
61e0f2d0
JK
1070 if (test_bit(flag, &fi->flags))
1071 clear_bit(flag, &fi->flags);
39a53e0c
JK
1072}
1073
1074static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
1075{
1076 fi->i_acl_mode = mode;
1077 set_inode_flag(fi, FI_ACL_MODE);
1078}
1079
1080static inline int cond_clear_inode_flag(struct f2fs_inode_info *fi, int flag)
1081{
1082 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
1083 clear_inode_flag(fi, FI_ACL_MODE);
1084 return 1;
1085 }
1086 return 0;
1087}
1088
444c580f
JK
1089static inline void get_inline_info(struct f2fs_inode_info *fi,
1090 struct f2fs_inode *ri)
1091{
1092 if (ri->i_inline & F2FS_INLINE_XATTR)
1093 set_inode_flag(fi, FI_INLINE_XATTR);
1001b347
HL
1094 if (ri->i_inline & F2FS_INLINE_DATA)
1095 set_inode_flag(fi, FI_INLINE_DATA);
444c580f
JK
1096}
1097
1098static inline void set_raw_inline(struct f2fs_inode_info *fi,
1099 struct f2fs_inode *ri)
1100{
1101 ri->i_inline = 0;
1102
1103 if (is_inode_flag_set(fi, FI_INLINE_XATTR))
1104 ri->i_inline |= F2FS_INLINE_XATTR;
1001b347
HL
1105 if (is_inode_flag_set(fi, FI_INLINE_DATA))
1106 ri->i_inline |= F2FS_INLINE_DATA;
444c580f
JK
1107}
1108
987c7c31
CY
1109static inline int f2fs_has_inline_xattr(struct inode *inode)
1110{
1111 return is_inode_flag_set(F2FS_I(inode), FI_INLINE_XATTR);
1112}
1113
de93653f
JK
1114static inline unsigned int addrs_per_inode(struct f2fs_inode_info *fi)
1115{
987c7c31 1116 if (f2fs_has_inline_xattr(&fi->vfs_inode))
de93653f
JK
1117 return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
1118 return DEF_ADDRS_PER_INODE;
1119}
1120
65985d93
JK
1121static inline void *inline_xattr_addr(struct page *page)
1122{
695fd1ed 1123 struct f2fs_inode *ri = F2FS_INODE(page);
65985d93
JK
1124 return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
1125 F2FS_INLINE_XATTR_ADDRS]);
1126}
1127
1128static inline int inline_xattr_size(struct inode *inode)
1129{
987c7c31 1130 if (f2fs_has_inline_xattr(inode))
65985d93
JK
1131 return F2FS_INLINE_XATTR_ADDRS << 2;
1132 else
1133 return 0;
1134}
1135
0dbdc2ae
JK
1136static inline int f2fs_has_inline_data(struct inode *inode)
1137{
1138 return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DATA);
1139}
1140
1001b347
HL
1141static inline void *inline_data_addr(struct page *page)
1142{
695fd1ed 1143 struct f2fs_inode *ri = F2FS_INODE(page);
1001b347
HL
1144 return (void *)&(ri->i_addr[1]);
1145}
1146
77888c1e
JK
1147static inline int f2fs_readonly(struct super_block *sb)
1148{
1149 return sb->s_flags & MS_RDONLY;
1150}
1151
1e968fdf
JK
1152static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
1153{
1154 return is_set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
1155}
1156
744602cf
JK
1157static inline void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi)
1158{
1159 set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
1160 sbi->sb->s_flags |= MS_RDONLY;
1161}
1162
a6dda0e6
CH
1163#define get_inode_mode(i) \
1164 ((is_inode_flag_set(F2FS_I(i), FI_ACL_MODE)) ? \
1165 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
1166
267378d4
CY
1167/* get offset of first page in next direct node */
1168#define PGOFS_OF_NEXT_DNODE(pgofs, fi) \
1169 ((pgofs < ADDRS_PER_INODE(fi)) ? ADDRS_PER_INODE(fi) : \
1170 (pgofs - ADDRS_PER_INODE(fi) + ADDRS_PER_BLOCK) / \
1171 ADDRS_PER_BLOCK * ADDRS_PER_BLOCK + ADDRS_PER_INODE(fi))
1172
39a53e0c
JK
1173/*
1174 * file.c
1175 */
1176int f2fs_sync_file(struct file *, loff_t, loff_t, int);
1177void truncate_data_blocks(struct dnode_of_data *);
764aa3e9 1178int truncate_blocks(struct inode *, u64, bool);
39a53e0c 1179void f2fs_truncate(struct inode *);
2d4d9fb5 1180int f2fs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
39a53e0c
JK
1181int f2fs_setattr(struct dentry *, struct iattr *);
1182int truncate_hole(struct inode *, pgoff_t, pgoff_t);
b292dcab 1183int truncate_data_blocks_range(struct dnode_of_data *, int);
39a53e0c 1184long f2fs_ioctl(struct file *, unsigned int, unsigned long);
e9750824 1185long f2fs_compat_ioctl(struct file *, unsigned int, unsigned long);
39a53e0c
JK
1186
1187/*
1188 * inode.c
1189 */
1190void f2fs_set_inode_flags(struct inode *);
39a53e0c 1191struct inode *f2fs_iget(struct super_block *, unsigned long);
4660f9c0 1192int try_to_free_nats(struct f2fs_sb_info *, int);
39a53e0c 1193void update_inode(struct inode *, struct page *);
744602cf 1194void update_inode_page(struct inode *);
39a53e0c
JK
1195int f2fs_write_inode(struct inode *, struct writeback_control *);
1196void f2fs_evict_inode(struct inode *);
44c16156 1197void handle_failed_inode(struct inode *);
39a53e0c
JK
1198
1199/*
1200 * namei.c
1201 */
1202struct dentry *f2fs_get_parent(struct dentry *child);
1203
1204/*
1205 * dir.c
1206 */
1207struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
1208 struct page **);
1209struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
1210ino_t f2fs_inode_by_name(struct inode *, struct qstr *);
1211void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
1212 struct page *, struct inode *);
1cd14caf 1213int update_dent_inode(struct inode *, const struct qstr *);
b7f7a5e0 1214int __f2fs_add_link(struct inode *, const struct qstr *, struct inode *);
39a53e0c 1215void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *);
b97a9b5d 1216int f2fs_do_tmpfile(struct inode *, struct inode *);
39a53e0c
JK
1217int f2fs_make_empty(struct inode *, struct inode *);
1218bool f2fs_empty_dir(struct inode *);
1219
b7f7a5e0
AV
1220static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
1221{
1222 return __f2fs_add_link(dentry->d_parent->d_inode, &dentry->d_name,
1223 inode);
1224}
1225
39a53e0c
JK
1226/*
1227 * super.c
1228 */
1229int f2fs_sync_fs(struct super_block *, int);
a07ef784
NJ
1230extern __printf(3, 4)
1231void f2fs_msg(struct super_block *, const char *, const char *, ...);
39a53e0c
JK
1232
1233/*
1234 * hash.c
1235 */
eee6160f 1236f2fs_hash_t f2fs_dentry_hash(const struct qstr *);
39a53e0c
JK
1237
1238/*
1239 * node.c
1240 */
1241struct dnode_of_data;
1242struct node_info;
1243
6fb03f3a 1244bool available_free_memory(struct f2fs_sb_info *, int);
88bd02c9
JK
1245bool is_checkpointed_node(struct f2fs_sb_info *, nid_t);
1246bool has_fsynced_inode(struct f2fs_sb_info *, nid_t);
1247bool need_inode_block_update(struct f2fs_sb_info *, nid_t);
39a53e0c
JK
1248void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
1249int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
1250int truncate_inode_blocks(struct inode *, pgoff_t);
4f16fb0f 1251int truncate_xattr_node(struct inode *, struct page *);
cfe58f9d 1252int wait_on_node_pages_writeback(struct f2fs_sb_info *, nid_t);
58e674d6 1253void remove_inode_page(struct inode *);
a014e037 1254struct page *new_inode_page(struct inode *);
8ae8f162 1255struct page *new_node_page(struct dnode_of_data *, unsigned int, struct page *);
39a53e0c
JK
1256void ra_node_page(struct f2fs_sb_info *, nid_t);
1257struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
1258struct page *get_node_page_ra(struct page *, int);
1259void sync_inode_page(struct dnode_of_data *);
1260int sync_node_pages(struct f2fs_sb_info *, nid_t, struct writeback_control *);
1261bool alloc_nid(struct f2fs_sb_info *, nid_t *);
1262void alloc_nid_done(struct f2fs_sb_info *, nid_t);
1263void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
70cfed88 1264void recover_inline_xattr(struct inode *, struct page *);
1c35a90e 1265void recover_xattr_data(struct inode *, struct page *, block_t);
39a53e0c
JK
1266int recover_inode_page(struct f2fs_sb_info *, struct page *);
1267int restore_node_summary(struct f2fs_sb_info *, unsigned int,
1268 struct f2fs_summary_block *);
1269void flush_nat_entries(struct f2fs_sb_info *);
1270int build_node_manager(struct f2fs_sb_info *);
1271void destroy_node_manager(struct f2fs_sb_info *);
6e6093a8 1272int __init create_node_manager_caches(void);
39a53e0c
JK
1273void destroy_node_manager_caches(void);
1274
1275/*
1276 * segment.c
1277 */
1278void f2fs_balance_fs(struct f2fs_sb_info *);
4660f9c0 1279void f2fs_balance_fs_bg(struct f2fs_sb_info *);
6b4afdd7 1280int f2fs_issue_flush(struct f2fs_sb_info *);
2163d198
GZ
1281int create_flush_cmd_control(struct f2fs_sb_info *);
1282void destroy_flush_cmd_control(struct f2fs_sb_info *);
39a53e0c 1283void invalidate_blocks(struct f2fs_sb_info *, block_t);
5e443818 1284void refresh_sit_entry(struct f2fs_sb_info *, block_t, block_t);
39a53e0c 1285void clear_prefree_segments(struct f2fs_sb_info *);
4b2fecc8 1286void release_discard_addrs(struct f2fs_sb_info *);
cf2271e7 1287void discard_next_dnode(struct f2fs_sb_info *, block_t);
39a53e0c
JK
1288int npages_for_summary_flush(struct f2fs_sb_info *);
1289void allocate_new_segments(struct f2fs_sb_info *);
4b2fecc8 1290int f2fs_trim_fs(struct f2fs_sb_info *, struct fstrim_range *);
39a53e0c 1291struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
577e3495 1292void write_meta_page(struct f2fs_sb_info *, struct page *);
fb5566da
JK
1293void write_node_page(struct f2fs_sb_info *, struct page *,
1294 struct f2fs_io_info *, unsigned int, block_t, block_t *);
458e6197
JK
1295void write_data_page(struct page *, struct dnode_of_data *, block_t *,
1296 struct f2fs_io_info *);
1297void rewrite_data_page(struct page *, block_t, struct f2fs_io_info *);
39a53e0c
JK
1298void recover_data_page(struct f2fs_sb_info *, struct page *,
1299 struct f2fs_summary *, block_t, block_t);
bfad7c2d
JK
1300void allocate_data_block(struct f2fs_sb_info *, struct page *,
1301 block_t, block_t *, struct f2fs_summary *, int);
5514f0aa 1302void f2fs_wait_on_page_writeback(struct page *, enum page_type);
39a53e0c
JK
1303void write_data_summaries(struct f2fs_sb_info *, block_t);
1304void write_node_summaries(struct f2fs_sb_info *, block_t);
1305int lookup_journal_in_cursum(struct f2fs_summary_block *,
1306 int, unsigned int, int);
4b2fecc8 1307void flush_sit_entries(struct f2fs_sb_info *, struct cp_control *);
39a53e0c 1308int build_segment_manager(struct f2fs_sb_info *);
39a53e0c 1309void destroy_segment_manager(struct f2fs_sb_info *);
7fd9e544
JK
1310int __init create_segment_manager_caches(void);
1311void destroy_segment_manager_caches(void);
39a53e0c
JK
1312
1313/*
1314 * checkpoint.c
1315 */
1316struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
1317struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
4c521f49
JK
1318struct page *get_meta_page_ra(struct f2fs_sb_info *, pgoff_t);
1319int ra_meta_pages(struct f2fs_sb_info *, block_t, int, int);
39a53e0c 1320long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
fff04f90
JK
1321void add_dirty_inode(struct f2fs_sb_info *, nid_t, int type);
1322void remove_dirty_inode(struct f2fs_sb_info *, nid_t, int type);
6f12ac25 1323void release_dirty_inode(struct f2fs_sb_info *);
fff04f90 1324bool exist_written_data(struct f2fs_sb_info *, nid_t, int);
cbd56e7d
JK
1325int acquire_orphan_inode(struct f2fs_sb_info *);
1326void release_orphan_inode(struct f2fs_sb_info *);
39a53e0c
JK
1327void add_orphan_inode(struct f2fs_sb_info *, nid_t);
1328void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
8f99a946 1329void recover_orphan_inodes(struct f2fs_sb_info *);
39a53e0c 1330int get_valid_checkpoint(struct f2fs_sb_info *);
a7ffdbe2 1331void update_dirty_page(struct inode *, struct page *);
5deb8267 1332void add_dirty_dir_inode(struct inode *);
39a53e0c
JK
1333void remove_dirty_dir_inode(struct inode *);
1334void sync_dirty_dir_inodes(struct f2fs_sb_info *);
75ab4cb8 1335void write_checkpoint(struct f2fs_sb_info *, struct cp_control *);
6451e041 1336void init_ino_entry_info(struct f2fs_sb_info *);
6e6093a8 1337int __init create_checkpoint_caches(void);
39a53e0c
JK
1338void destroy_checkpoint_caches(void);
1339
1340/*
1341 * data.c
1342 */
458e6197 1343void f2fs_submit_merged_bio(struct f2fs_sb_info *, enum page_type, int);
93dfe2ac
JK
1344int f2fs_submit_page_bio(struct f2fs_sb_info *, struct page *, block_t, int);
1345void f2fs_submit_page_mbio(struct f2fs_sb_info *, struct page *, block_t,
458e6197 1346 struct f2fs_io_info *);
39a53e0c 1347int reserve_new_block(struct dnode_of_data *);
b600965c 1348int f2fs_reserve_block(struct dnode_of_data *, pgoff_t);
39a53e0c 1349void update_extent_cache(block_t, struct dnode_of_data *);
c718379b 1350struct page *find_data_page(struct inode *, pgoff_t, bool);
39a53e0c 1351struct page *get_lock_data_page(struct inode *, pgoff_t);
64aa7ed9 1352struct page *get_new_data_page(struct inode *, struct page *, pgoff_t, bool);
458e6197 1353int do_write_data_page(struct page *, struct f2fs_io_info *);
9ab70134 1354int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *, u64, u64);
39a53e0c
JK
1355
1356/*
1357 * gc.c
1358 */
1359int start_gc_thread(struct f2fs_sb_info *);
1360void stop_gc_thread(struct f2fs_sb_info *);
de93653f 1361block_t start_bidx_of_node(unsigned int, struct f2fs_inode_info *);
408e9375 1362int f2fs_gc(struct f2fs_sb_info *);
39a53e0c 1363void build_gc_manager(struct f2fs_sb_info *);
6e6093a8 1364int __init create_gc_caches(void);
39a53e0c
JK
1365void destroy_gc_caches(void);
1366
1367/*
1368 * recovery.c
1369 */
6ead1142 1370int recover_fsync_data(struct f2fs_sb_info *);
39a53e0c
JK
1371bool space_for_roll_forward(struct f2fs_sb_info *);
1372
1373/*
1374 * debug.c
1375 */
1376#ifdef CONFIG_F2FS_STAT_FS
1377struct f2fs_stat_info {
1378 struct list_head stat_list;
1379 struct f2fs_sb_info *sbi;
39a53e0c
JK
1380 int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
1381 int main_area_segs, main_area_sections, main_area_zones;
1382 int hit_ext, total_ext;
1383 int ndirty_node, ndirty_dent, ndirty_dirs, ndirty_meta;
1384 int nats, sits, fnids;
1385 int total_count, utilization;
0dbdc2ae 1386 int bg_gc, inline_inode;
39a53e0c
JK
1387 unsigned int valid_count, valid_node_count, valid_inode_count;
1388 unsigned int bimodal, avg_vblocks;
1389 int util_free, util_valid, util_invalid;
1390 int rsvd_segs, overp_segs;
1391 int dirty_count, node_pages, meta_pages;
942e0be6 1392 int prefree_count, call_count, cp_count;
39a53e0c
JK
1393 int tot_segs, node_segs, data_segs, free_segs, free_secs;
1394 int tot_blks, data_blks, node_blks;
1395 int curseg[NR_CURSEG_TYPE];
1396 int cursec[NR_CURSEG_TYPE];
1397 int curzone[NR_CURSEG_TYPE];
1398
1399 unsigned int segment_count[2];
1400 unsigned int block_count[2];
1401 unsigned base_mem, cache_mem;
1402};
1403
963d4f7d
GZ
1404static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
1405{
6c311ec6 1406 return (struct f2fs_stat_info *)sbi->stat_info;
963d4f7d
GZ
1407}
1408
942e0be6 1409#define stat_inc_cp_count(si) ((si)->cp_count++)
dcdfff65
JK
1410#define stat_inc_call_count(si) ((si)->call_count++)
1411#define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
1412#define stat_inc_dirty_dir(sbi) ((sbi)->n_dirty_dirs++)
1413#define stat_dec_dirty_dir(sbi) ((sbi)->n_dirty_dirs--)
1414#define stat_inc_total_hit(sb) ((F2FS_SB(sb))->total_hit_ext++)
1415#define stat_inc_read_hit(sb) ((F2FS_SB(sb))->read_hit_ext++)
0dbdc2ae
JK
1416#define stat_inc_inline_inode(inode) \
1417 do { \
1418 if (f2fs_has_inline_data(inode)) \
4081363f 1419 ((F2FS_I_SB(inode))->inline_inode++); \
0dbdc2ae
JK
1420 } while (0)
1421#define stat_dec_inline_inode(inode) \
1422 do { \
1423 if (f2fs_has_inline_data(inode)) \
4081363f 1424 ((F2FS_I_SB(inode))->inline_inode--); \
0dbdc2ae
JK
1425 } while (0)
1426
dcdfff65
JK
1427#define stat_inc_seg_type(sbi, curseg) \
1428 ((sbi)->segment_count[(curseg)->alloc_type]++)
1429#define stat_inc_block_count(sbi, curseg) \
1430 ((sbi)->block_count[(curseg)->alloc_type]++)
39a53e0c
JK
1431
1432#define stat_inc_seg_count(sbi, type) \
1433 do { \
963d4f7d 1434 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
39a53e0c
JK
1435 (si)->tot_segs++; \
1436 if (type == SUM_TYPE_DATA) \
1437 si->data_segs++; \
1438 else \
1439 si->node_segs++; \
1440 } while (0)
1441
1442#define stat_inc_tot_blk_count(si, blks) \
1443 (si->tot_blks += (blks))
1444
1445#define stat_inc_data_blk_count(sbi, blks) \
1446 do { \
963d4f7d 1447 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
39a53e0c
JK
1448 stat_inc_tot_blk_count(si, blks); \
1449 si->data_blks += (blks); \
1450 } while (0)
1451
1452#define stat_inc_node_blk_count(sbi, blks) \
1453 do { \
963d4f7d 1454 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
39a53e0c
JK
1455 stat_inc_tot_blk_count(si, blks); \
1456 si->node_blks += (blks); \
1457 } while (0)
1458
1459int f2fs_build_stats(struct f2fs_sb_info *);
1460void f2fs_destroy_stats(struct f2fs_sb_info *);
6e6093a8 1461void __init f2fs_create_root_stats(void);
4589d25d 1462void f2fs_destroy_root_stats(void);
39a53e0c 1463#else
942e0be6 1464#define stat_inc_cp_count(si)
39a53e0c 1465#define stat_inc_call_count(si)
dcdfff65
JK
1466#define stat_inc_bggc_count(si)
1467#define stat_inc_dirty_dir(sbi)
1468#define stat_dec_dirty_dir(sbi)
1469#define stat_inc_total_hit(sb)
1470#define stat_inc_read_hit(sb)
0dbdc2ae
JK
1471#define stat_inc_inline_inode(inode)
1472#define stat_dec_inline_inode(inode)
dcdfff65
JK
1473#define stat_inc_seg_type(sbi, curseg)
1474#define stat_inc_block_count(sbi, curseg)
39a53e0c
JK
1475#define stat_inc_seg_count(si, type)
1476#define stat_inc_tot_blk_count(si, blks)
1477#define stat_inc_data_blk_count(si, blks)
1478#define stat_inc_node_blk_count(sbi, blks)
1479
1480static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
1481static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
6e6093a8 1482static inline void __init f2fs_create_root_stats(void) { }
4589d25d 1483static inline void f2fs_destroy_root_stats(void) { }
39a53e0c
JK
1484#endif
1485
1486extern const struct file_operations f2fs_dir_operations;
1487extern const struct file_operations f2fs_file_operations;
1488extern const struct inode_operations f2fs_file_inode_operations;
1489extern const struct address_space_operations f2fs_dblock_aops;
1490extern const struct address_space_operations f2fs_node_aops;
1491extern const struct address_space_operations f2fs_meta_aops;
1492extern const struct inode_operations f2fs_dir_inode_operations;
1493extern const struct inode_operations f2fs_symlink_inode_operations;
1494extern const struct inode_operations f2fs_special_inode_operations;
1001b347 1495
e18c65b2
HL
1496/*
1497 * inline.c
1498 */
e18c65b2
HL
1499bool f2fs_may_inline(struct inode *);
1500int f2fs_read_inline_data(struct inode *, struct page *);
b067ba1f 1501int f2fs_convert_inline_data(struct inode *, pgoff_t, struct page *);
e18c65b2 1502int f2fs_write_inline_data(struct inode *, struct page *, unsigned int);
8aa6f1c5 1503void truncate_inline_data(struct inode *, u64);
0342fd30 1504bool recover_inline_data(struct inode *, struct page *);
39a53e0c 1505#endif