blob: bd1c70d614984fd9f6e9a19e2396a32035ce4930 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002 * fs/f2fs/f2fs.h
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#ifndef _LINUX_F2FS_H
12#define _LINUX_F2FS_H
13
14#include <linux/types.h>
15#include <linux/page-flags.h>
16#include <linux/buffer_head.h>
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090017#include <linux/slab.h>
18#include <linux/crc32.h>
19#include <linux/magic.h>
Jaegeuk Kimc2d715d2013-08-08 15:56:49 +090020#include <linux/kobject.h>
Gu Zheng7bd59382013-10-22 14:52:26 +080021#include <linux/sched.h>
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070022#include <linux/writeback.h>
Jaegeuk Kim39307a82015-09-22 13:50:47 -070023#include <linux/vmalloc.h>
Jaegeuk Kim740432f2015-08-14 11:43:56 -070024#include <linux/bio.h>
Jaegeuk Kimd0239e12016-01-08 16:57:48 -080025#include <linux/blkdev.h>
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070026#ifdef CONFIG_F2FS_FS_ENCRYPTION
27#include <linux/fscrypt_supp.h>
28#else
29#include <linux/fscrypt_notsupp.h>
30#endif
Keith Mok43b65732016-03-02 12:04:24 -080031#include <crypto/hash.h>
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090032
Jaegeuk Kim5d56b672013-10-29 15:14:54 +090033#ifdef CONFIG_F2FS_CHECK_FS
Jaegeuk Kim9850cf42014-09-02 15:52:58 -070034#define f2fs_bug_on(sbi, condition) BUG_ON(condition)
Jaegeuk Kim5d56b672013-10-29 15:14:54 +090035#else
Jaegeuk Kim9850cf42014-09-02 15:52:58 -070036#define f2fs_bug_on(sbi, condition) \
37 do { \
38 if (unlikely(condition)) { \
39 WARN_ON(1); \
Chao Yucaf00472015-01-28 17:48:42 +080040 set_sbi_flag(sbi, SBI_NEED_FSCK); \
Jaegeuk Kim9850cf42014-09-02 15:52:58 -070041 } \
42 } while (0)
Jaegeuk Kim5d56b672013-10-29 15:14:54 +090043#endif
44
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070045#ifdef CONFIG_F2FS_FAULT_INJECTION
46enum {
47 FAULT_KMALLOC,
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -070048 FAULT_PAGE_ALLOC,
Jaegeuk Kimcb789422016-04-29 16:29:22 -070049 FAULT_ALLOC_NID,
50 FAULT_ORPHAN,
51 FAULT_BLOCK,
52 FAULT_DIR_DEPTH,
Jaegeuk Kim53aa6bb2016-05-25 15:24:18 -070053 FAULT_EVICT_INODE,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070054 FAULT_TRUNCATE,
Chao Yu8b038c72016-09-18 23:30:07 +080055 FAULT_IO,
Chao Yu0f348022016-09-26 19:45:55 +080056 FAULT_CHECKPOINT,
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070057 FAULT_MAX,
58};
59
Sheng Yong08796892016-05-16 12:38:50 +080060struct f2fs_fault_info {
61 atomic_t inject_ops;
62 unsigned int inject_rate;
63 unsigned int inject_type;
64};
65
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070066extern char *fault_name[FAULT_MAX];
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070067#define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070068#endif
69
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090070/*
71 * For mount options
72 */
73#define F2FS_MOUNT_BG_GC 0x00000001
74#define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
75#define F2FS_MOUNT_DISCARD 0x00000004
76#define F2FS_MOUNT_NOHEAP 0x00000008
77#define F2FS_MOUNT_XATTR_USER 0x00000010
78#define F2FS_MOUNT_POSIX_ACL 0x00000020
79#define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
Jaegeuk Kim444c5802013-08-08 15:16:22 +090080#define F2FS_MOUNT_INLINE_XATTR 0x00000080
Huajun Li1001b342013-11-10 23:13:16 +080081#define F2FS_MOUNT_INLINE_DATA 0x00000100
Chao Yu34d67de2014-09-24 18:15:19 +080082#define F2FS_MOUNT_INLINE_DENTRY 0x00000200
83#define F2FS_MOUNT_FLUSH_MERGE 0x00000400
84#define F2FS_MOUNT_NOBARRIER 0x00000800
Jaegeuk Kimd5053a342014-10-30 22:47:03 -070085#define F2FS_MOUNT_FASTBOOT 0x00001000
Chao Yu89672152015-02-05 17:55:51 +080086#define F2FS_MOUNT_EXTENT_CACHE 0x00002000
Jaegeuk Kim6aefd932015-10-05 11:02:54 -070087#define F2FS_MOUNT_FORCE_FG_GC 0x00004000
Chao Yu343f40f2015-12-16 13:12:16 +080088#define F2FS_MOUNT_DATA_FLUSH 0x00008000
Jaegeuk Kim73faec42016-04-29 15:34:32 -070089#define F2FS_MOUNT_FAULT_INJECTION 0x00010000
Jaegeuk Kim36abef42016-06-03 19:29:38 -070090#define F2FS_MOUNT_ADAPTIVE 0x00020000
91#define F2FS_MOUNT_LFS 0x00040000
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090092
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070093#define clear_opt(sbi, option) ((sbi)->mount_opt.opt &= ~F2FS_MOUNT_##option)
94#define set_opt(sbi, option) ((sbi)->mount_opt.opt |= F2FS_MOUNT_##option)
95#define test_opt(sbi, option) ((sbi)->mount_opt.opt & F2FS_MOUNT_##option)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090096
97#define ver_after(a, b) (typecheck(unsigned long long, a) && \
98 typecheck(unsigned long long, b) && \
99 ((long long)((a) - (b)) > 0))
100
Jaegeuk Kima9841c42013-05-24 12:41:04 +0900101typedef u32 block_t; /*
102 * should not change u32, since it is the on-disk block
103 * address format, __le32.
104 */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900105typedef u32 nid_t;
106
107struct f2fs_mount_info {
108 unsigned int opt;
109};
110
Jaegeuk Kimcde4de12015-04-20 13:57:51 -0700111#define F2FS_FEATURE_ENCRYPT 0x0001
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700112#define F2FS_FEATURE_BLKZONED 0x0002
Jaegeuk Kimcde4de12015-04-20 13:57:51 -0700113
Jaegeuk Kim76f105a2015-04-13 15:10:36 -0700114#define F2FS_HAS_FEATURE(sb, mask) \
115 ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
116#define F2FS_SET_FEATURE(sb, mask) \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700117 (F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
Jaegeuk Kim76f105a2015-04-13 15:10:36 -0700118#define F2FS_CLEAR_FEATURE(sb, mask) \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700119 (F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
Jaegeuk Kim76f105a2015-04-13 15:10:36 -0700120
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900121/*
122 * For checkpoint manager
123 */
124enum {
125 NAT_BITMAP,
126 SIT_BITMAP
127};
128
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700129#define CP_UMOUNT 0x00000001
130#define CP_FASTBOOT 0x00000002
131#define CP_SYNC 0x00000004
132#define CP_RECOVERY 0x00000008
133#define CP_DISCARD 0x00000010
134#define CP_TRIMMED 0x00000020
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -0700135
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700136#define DEF_BATCHED_TRIM_SECTIONS 2048
Jaegeuk Kimbba681c2015-01-26 17:41:23 -0800137#define BATCHED_TRIM_SEGMENTS(sbi) \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700138 (GET_SEG_FROM_SEC(sbi, SM_I(sbi)->trim_sections))
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700139#define BATCHED_TRIM_BLOCKS(sbi) \
140 (BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700141#define MAX_DISCARD_BLOCKS(sbi) BLKS_PER_SEC(sbi)
142#define DISCARD_ISSUE_RATE 8
Jaegeuk Kim60b99b42015-10-05 14:49:57 -0700143#define DEF_CP_INTERVAL 60 /* 60 secs */
Chao Yudcf25fe2016-07-15 19:25:47 +0800144#define DEF_IDLE_INTERVAL 5 /* 5 secs */
Jaegeuk Kimbba681c2015-01-26 17:41:23 -0800145
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -0700146struct cp_control {
147 int reason;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700148 __u64 trim_start;
149 __u64 trim_end;
150 __u64 trim_minlen;
151 __u64 trimmed;
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -0700152};
153
Chao Yu662befd2014-02-07 16:11:53 +0800154/*
Chao Yu81c1a0f12014-02-27 19:12:24 +0800155 * For CP/NAT/SIT/SSA readahead
Chao Yu662befd2014-02-07 16:11:53 +0800156 */
157enum {
158 META_CP,
159 META_NAT,
Chao Yu81c1a0f12014-02-27 19:12:24 +0800160 META_SIT,
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700161 META_SSA,
162 META_POR,
Chao Yu662befd2014-02-07 16:11:53 +0800163};
164
Jaegeuk Kim6451e042014-07-25 15:47:17 -0700165/* for the list of ino */
166enum {
167 ORPHAN_INO, /* for orphan ino list */
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700168 APPEND_INO, /* for append ino list */
169 UPDATE_INO, /* for update ino list */
Jaegeuk Kim6451e042014-07-25 15:47:17 -0700170 MAX_INO_ENTRY, /* max. list */
171};
172
173struct ino_entry {
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900174 struct list_head list; /* list head */
175 nid_t ino; /* inode number */
176};
177
Chao Yu2710fd72015-12-15 13:30:45 +0800178/* for the list of inodes to be GCed */
Chao Yu06292072014-12-29 15:56:18 +0800179struct inode_entry {
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900180 struct list_head list; /* list head */
181 struct inode *inode; /* vfs inode pointer */
182};
183
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700184/* for the bitmap indicate blocks to be discarded */
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +0900185struct discard_entry {
186 struct list_head list; /* list head */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700187 block_t start_blkaddr; /* start blockaddr of current segment */
188 unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +0900189};
190
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700191/* max discard pend list number */
192#define MAX_PLIST_NUM 512
193#define plist_idx(blk_num) ((blk_num) >= MAX_PLIST_NUM ? \
194 (MAX_PLIST_NUM - 1) : (blk_num - 1))
195
196enum {
197 D_PREP,
198 D_SUBMIT,
199 D_DONE,
200};
201
202struct discard_info {
203 block_t lstart; /* logical start address */
204 block_t len; /* length */
205 block_t start; /* actual start address in dev */
206};
207
208struct discard_cmd {
209 struct rb_node rb_node; /* rb node located in rb-tree */
210 union {
211 struct {
212 block_t lstart; /* logical start address */
213 block_t len; /* length */
214 block_t start; /* actual start address in dev */
215 };
216 struct discard_info di; /* discard info */
217
218 };
219 struct list_head list; /* command list */
220 struct completion wait; /* compleation */
221 struct block_device *bdev; /* bdev */
222 unsigned short ref; /* reference count */
223 unsigned char state; /* state */
224 int error; /* bio error */
225};
226
227struct discard_cmd_control {
228 struct task_struct *f2fs_issue_discard; /* discard thread */
229 struct list_head entry_list; /* 4KB discard entry list */
230 struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
231 struct list_head wait_list; /* store on-flushing entries */
232 wait_queue_head_t discard_wait_queue; /* waiting queue for wake-up */
233 struct mutex cmd_lock;
234 unsigned int nr_discards; /* # of discards in the list */
235 unsigned int max_discards; /* max. discards to be issued */
236 unsigned int undiscard_blks; /* # of undiscard blocks */
237 atomic_t issued_discard; /* # of issued discard */
238 atomic_t issing_discard; /* # of issing discard */
239 atomic_t discard_cmd_cnt; /* # of cached cmd count */
240 struct rb_root root; /* root of discard rb-tree */
Chao Yu275b66b2016-08-29 23:58:34 +0800241};
242
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900243/* for the list of fsync inodes, used only during recovery */
244struct fsync_inode_entry {
245 struct list_head list; /* list head */
246 struct inode *inode; /* vfs inode pointer */
Jaegeuk Kimc52e1b12014-09-11 14:29:06 -0700247 block_t blkaddr; /* block address locating the last fsync */
248 block_t last_dentry; /* block address locating the last dentry */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900249};
250
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700251#define nats_in_cursum(jnl) (le16_to_cpu((jnl)->n_nats))
252#define sits_in_cursum(jnl) (le16_to_cpu((jnl)->n_sits))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900253
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700254#define nat_in_journal(jnl, i) ((jnl)->nat_j.entries[i].ne)
255#define nid_in_journal(jnl, i) ((jnl)->nat_j.entries[i].nid)
256#define sit_in_journal(jnl, i) ((jnl)->sit_j.entries[i].se)
257#define segno_in_journal(jnl, i) ((jnl)->sit_j.entries[i].segno)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900258
Chao Yudfc08a12016-02-14 18:50:40 +0800259#define MAX_NAT_JENTRIES(jnl) (NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
260#define MAX_SIT_JENTRIES(jnl) (SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700261
Chao Yudfc08a12016-02-14 18:50:40 +0800262static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900263{
Chao Yudfc08a12016-02-14 18:50:40 +0800264 int before = nats_in_cursum(journal);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700265
Chao Yudfc08a12016-02-14 18:50:40 +0800266 journal->n_nats = cpu_to_le16(before + i);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900267 return before;
268}
269
Chao Yudfc08a12016-02-14 18:50:40 +0800270static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900271{
Chao Yudfc08a12016-02-14 18:50:40 +0800272 int before = sits_in_cursum(journal);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700273
Chao Yudfc08a12016-02-14 18:50:40 +0800274 journal->n_sits = cpu_to_le16(before + i);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900275 return before;
276}
277
Chao Yudfc08a12016-02-14 18:50:40 +0800278static inline bool __has_cursum_space(struct f2fs_journal *journal,
279 int size, int type)
Chao Yu184a5cd2014-09-04 18:13:01 +0800280{
281 if (type == NAT_JOURNAL)
Chao Yudfc08a12016-02-14 18:50:40 +0800282 return size <= MAX_NAT_JENTRIES(journal);
283 return size <= MAX_SIT_JENTRIES(journal);
Chao Yu184a5cd2014-09-04 18:13:01 +0800284}
285
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900286/*
Namjae Jeone9750822013-02-04 23:41:41 +0900287 * ioctl commands
288 */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700289#define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
290#define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
Chao Yud49f3e82015-01-23 20:36:04 +0800291#define F2FS_IOC_GETVERSION FS_IOC_GETVERSION
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700292
293#define F2FS_IOCTL_MAGIC 0xf5
294#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
295#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700296#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800297#define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
298#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700299#define F2FS_IOC_GARBAGE_COLLECT _IOW(F2FS_IOCTL_MAGIC, 6, __u32)
Chao Yu456b88e2015-10-05 22:24:19 +0800300#define F2FS_IOC_WRITE_CHECKPOINT _IO(F2FS_IOCTL_MAGIC, 7)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700301#define F2FS_IOC_DEFRAGMENT _IOWR(F2FS_IOCTL_MAGIC, 8, \
302 struct f2fs_defragment)
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -0700303#define F2FS_IOC_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
304 struct f2fs_move_range)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700305#define F2FS_IOC_FLUSH_DEVICE _IOW(F2FS_IOCTL_MAGIC, 10, \
306 struct f2fs_flush_device)
Namjae Jeone9750822013-02-04 23:41:41 +0900307
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700308#define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
309#define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
310#define F2FS_IOC_GET_ENCRYPTION_PWSALT FS_IOC_GET_ENCRYPTION_PWSALT
Jaegeuk Kimf424f662015-04-20 15:19:06 -0700311
Jaegeuk Kim1abff932015-01-08 19:15:53 -0800312/*
313 * should be same as XFS_IOC_GOINGDOWN.
314 * Flags for going down operation used by FS_IOC_GOINGDOWN
315 */
316#define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
317#define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
318#define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
319#define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
Jaegeuk Kimc912a822015-10-07 09:46:37 -0700320#define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */
Jaegeuk Kim1abff932015-01-08 19:15:53 -0800321
Namjae Jeone9750822013-02-04 23:41:41 +0900322#if defined(__KERNEL__) && defined(CONFIG_COMPAT)
323/*
324 * ioctl commands in 32 bit emulation
325 */
Chao Yu04ef4b62015-11-10 18:44:20 +0800326#define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
327#define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
328#define F2FS_IOC32_GETVERSION FS_IOC32_GETVERSION
Namjae Jeone9750822013-02-04 23:41:41 +0900329#endif
330
Chao Yud323d002015-10-27 09:53:45 +0800331struct f2fs_defragment {
332 u64 start;
333 u64 len;
334};
335
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -0700336struct f2fs_move_range {
337 u32 dst_fd; /* destination fd */
338 u64 pos_in; /* start position in src_fd */
339 u64 pos_out; /* start position in dst_fd */
340 u64 len; /* size to move */
341};
342
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700343struct f2fs_flush_device {
344 u32 dev_num; /* device number to flush */
345 u32 segments; /* # of segments to flush */
346};
347
Namjae Jeone9750822013-02-04 23:41:41 +0900348/*
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900349 * For INODE and NODE manager
350 */
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700351/* for directory operations */
352struct f2fs_dentry_ptr {
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700353 struct inode *inode;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700354 const void *bitmap;
355 struct f2fs_dir_entry *dentry;
356 __u8 (*filename)[F2FS_SLOT_LEN];
357 int max;
358};
359
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700360static inline void make_dentry_ptr_block(struct inode *inode,
361 struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700362{
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700363 d->inode = inode;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700364 d->max = NR_DENTRY_IN_BLOCK;
365 d->bitmap = &t->dentry_bitmap;
366 d->dentry = t->dentry;
367 d->filename = t->filename;
368}
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700369
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700370static inline void make_dentry_ptr_inline(struct inode *inode,
371 struct f2fs_dentry_ptr *d, struct f2fs_inline_dentry *t)
372{
373 d->inode = inode;
374 d->max = NR_INLINE_DENTRY;
375 d->bitmap = &t->dentry_bitmap;
376 d->dentry = t->dentry;
377 d->filename = t->filename;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700378}
379
Jaegeuk Kimdbe6a5f2013-08-09 08:14:06 +0900380/*
381 * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
382 * as its node offset to distinguish from index node blocks.
383 * But some bits are used to mark the node block.
384 */
385#define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
386 >> OFFSET_BIT_SHIFT)
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900387enum {
388 ALLOC_NODE, /* allocate a new node page if needed */
389 LOOKUP_NODE, /* look up a node without readahead */
390 LOOKUP_NODE_RA, /*
391 * look up a node with readahead called
Chao Yu4f4124d2013-12-21 18:02:14 +0800392 * by get_data_block.
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900393 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900394};
395
Jaegeuk Kima6db67f2015-08-10 15:01:12 -0700396#define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900397
Chao Yu817202d92014-04-28 17:59:43 +0800398#define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
399
Chao Yu13054c52015-02-05 17:52:58 +0800400/* vector size for gang look-up from extent cache that consists of radix tree */
401#define EXT_TREE_VEC_SIZE 64
402
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900403/* for in-memory extent cache entry */
Chao Yu13054c52015-02-05 17:52:58 +0800404#define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */
405
406/* number of extent info in extent cache we try to shrink */
407#define EXTENT_CACHE_SHRINK_NUMBER 128
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900408
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700409struct rb_entry {
410 struct rb_node rb_node; /* rb node located in rb-tree */
411 unsigned int ofs; /* start offset of the entry */
412 unsigned int len; /* length of the entry */
413};
414
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900415struct extent_info {
Chao Yu13054c52015-02-05 17:52:58 +0800416 unsigned int fofs; /* start offset in a file */
Chao Yu13054c52015-02-05 17:52:58 +0800417 unsigned int len; /* length of the extent */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700418 u32 blk; /* start block address of the extent */
Chao Yu13054c52015-02-05 17:52:58 +0800419};
420
421struct extent_node {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700422 struct rb_node rb_node;
423 union {
424 struct {
425 unsigned int fofs;
426 unsigned int len;
427 u32 blk;
428 };
429 struct extent_info ei; /* extent info */
430
431 };
Chao Yu13054c52015-02-05 17:52:58 +0800432 struct list_head list; /* node in global extent list of sbi */
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000433 struct extent_tree *et; /* extent tree pointer */
Chao Yu13054c52015-02-05 17:52:58 +0800434};
435
436struct extent_tree {
437 nid_t ino; /* inode number */
438 struct rb_root root; /* root of extent info rb-tree */
Chao Yu62c8af62015-02-05 18:01:39 +0800439 struct extent_node *cached_en; /* recently accessed extent node */
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700440 struct extent_info largest; /* largested extent info */
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800441 struct list_head list; /* to be used by sbi->zombie_list */
Chao Yu13054c52015-02-05 17:52:58 +0800442 rwlock_t lock; /* protect extent info rb-tree */
Chao Yu68e35382016-01-08 20:22:52 +0800443 atomic_t node_cnt; /* # of extent node in rb-tree*/
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900444};
445
446/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700447 * This structure is taken from ext4_map_blocks.
448 *
449 * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
450 */
451#define F2FS_MAP_NEW (1 << BH_New)
452#define F2FS_MAP_MAPPED (1 << BH_Mapped)
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700453#define F2FS_MAP_UNWRITTEN (1 << BH_Unwritten)
454#define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
455 F2FS_MAP_UNWRITTEN)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700456
457struct f2fs_map_blocks {
458 block_t m_pblk;
459 block_t m_lblk;
460 unsigned int m_len;
461 unsigned int m_flags;
Chao Yuda859852016-01-26 15:42:58 +0800462 pgoff_t *m_next_pgofs; /* point next possible non-hole pgofs */
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700463};
464
Chao Yue2b4e2b2015-08-19 19:11:19 +0800465/* for flag in get_data_block */
466#define F2FS_GET_BLOCK_READ 0
467#define F2FS_GET_BLOCK_DIO 1
468#define F2FS_GET_BLOCK_FIEMAP 2
469#define F2FS_GET_BLOCK_BMAP 3
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800470#define F2FS_GET_BLOCK_PRE_DIO 4
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800471#define F2FS_GET_BLOCK_PRE_AIO 5
Chao Yue2b4e2b2015-08-19 19:11:19 +0800472
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700473/*
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900474 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
475 */
476#define FADVISE_COLD_BIT 0x01
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900477#define FADVISE_LOST_PINO_BIT 0x02
Jaegeuk Kimcde4de12015-04-20 13:57:51 -0700478#define FADVISE_ENCRYPT_BIT 0x04
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700479#define FADVISE_ENC_NAME_BIT 0x08
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700480#define FADVISE_KEEP_SIZE_BIT 0x10
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900481
Jaegeuk Kimb5492af72015-04-20 13:44:41 -0700482#define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
483#define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
484#define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
485#define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
486#define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
487#define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
Jaegeuk Kimcde4de12015-04-20 13:57:51 -0700488#define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT)
489#define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
490#define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700491#define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT)
492#define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700493#define file_keep_isize(inode) is_file(inode, FADVISE_KEEP_SIZE_BIT)
494#define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
Jaegeuk Kimcde4de12015-04-20 13:57:51 -0700495
Jaegeuk Kimab9fa662014-02-27 20:09:05 +0900496#define DEF_DIR_LEVEL 0
497
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900498struct f2fs_inode_info {
499 struct inode vfs_inode; /* serve a vfs inode */
500 unsigned long i_flags; /* keep an inode flags for ioctl */
501 unsigned char i_advise; /* use to give file attribute hints */
Jaegeuk Kim38431542014-02-27 18:20:00 +0900502 unsigned char i_dir_level; /* use for dentry level for large dir */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900503 unsigned int i_current_depth; /* use only in directory structure */
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900504 unsigned int i_pino; /* parent inode number */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900505 umode_t i_acl_mode; /* keep file acl mode temporarily */
506
507 /* Use below internally in f2fs*/
508 unsigned long flags; /* use to pass per-file flags */
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900509 struct rw_semaphore i_sem; /* protect fi info */
Jaegeuk Kima43e1c42016-12-02 15:11:32 -0800510 atomic_t dirty_pages; /* # of dirty pages */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900511 f2fs_hash_t chash; /* hash value of given file name */
512 unsigned int clevel; /* maximum level of given file name */
Jaegeuk Kimd00d1b72017-02-14 09:54:37 -0800513 struct task_struct *task; /* lookup and create consistency */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900514 nid_t i_xattr_nid; /* node id that contains xattrs */
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700515 loff_t last_disk_size; /* lastly written file size */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700516
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700517 struct list_head dirty_list; /* dirty list for dirs and files */
518 struct list_head gdirty_list; /* linked in global dirty list */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700519 struct list_head inmem_pages; /* inmemory pages managed by f2fs */
520 struct mutex inmem_lock; /* lock for inmemory pages */
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700521 struct extent_tree *extent_tree; /* cached extent_tree entry */
Chao Yu82e0a5a2016-07-13 09:18:29 +0800522 struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
Qiuyang Sun7b23ea12017-05-18 11:06:45 +0800523 struct rw_semaphore i_mmap_sem;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900524};
525
526static inline void get_extent_info(struct extent_info *ext,
Chao Yubd933d42016-05-04 23:19:47 +0800527 struct f2fs_extent *i_ext)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900528{
Chao Yubd933d42016-05-04 23:19:47 +0800529 ext->fofs = le32_to_cpu(i_ext->fofs);
530 ext->blk = le32_to_cpu(i_ext->blk);
531 ext->len = le32_to_cpu(i_ext->len);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900532}
533
534static inline void set_raw_extent(struct extent_info *ext,
535 struct f2fs_extent *i_ext)
536{
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900537 i_ext->fofs = cpu_to_le32(ext->fofs);
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800538 i_ext->blk = cpu_to_le32(ext->blk);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900539 i_ext->len = cpu_to_le32(ext->len);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900540}
541
Chao Yu429511c2015-02-05 17:54:31 +0800542static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
543 u32 blk, unsigned int len)
544{
545 ei->fofs = fofs;
546 ei->blk = blk;
547 ei->len = len;
548}
549
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700550static inline bool __is_discard_mergeable(struct discard_info *back,
551 struct discard_info *front)
Chao Yu0bdee482015-03-19 19:27:51 +0800552{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700553 return back->lstart + back->len == front->lstart;
554}
555
556static inline bool __is_discard_back_mergeable(struct discard_info *cur,
557 struct discard_info *back)
558{
559 return __is_discard_mergeable(back, cur);
560}
561
562static inline bool __is_discard_front_mergeable(struct discard_info *cur,
563 struct discard_info *front)
564{
565 return __is_discard_mergeable(cur, front);
Chao Yu0bdee482015-03-19 19:27:51 +0800566}
567
Chao Yu429511c2015-02-05 17:54:31 +0800568static inline bool __is_extent_mergeable(struct extent_info *back,
569 struct extent_info *front)
570{
571 return (back->fofs + back->len == front->fofs &&
572 back->blk + back->len == front->blk);
573}
574
575static inline bool __is_back_mergeable(struct extent_info *cur,
576 struct extent_info *back)
577{
578 return __is_extent_mergeable(back, cur);
579}
580
581static inline bool __is_front_mergeable(struct extent_info *cur,
582 struct extent_info *front)
583{
584 return __is_extent_mergeable(cur, front);
585}
586
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700587extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700588static inline void __try_update_largest_extent(struct inode *inode,
589 struct extent_tree *et, struct extent_node *en)
Chao Yu4abd3f52015-09-22 21:07:47 +0800590{
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700591 if (en->ei.len > et->largest.len) {
Chao Yu4abd3f52015-09-22 21:07:47 +0800592 et->largest = en->ei;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700593 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700594 }
Chao Yu4abd3f52015-09-22 21:07:47 +0800595}
596
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700597enum nid_list {
598 FREE_NID_LIST,
599 ALLOC_NID_LIST,
600 MAX_NID_LIST,
601};
602
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900603struct f2fs_nm_info {
604 block_t nat_blkaddr; /* base disk address of NAT */
605 nid_t max_nid; /* maximum possible node ids */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700606 nid_t available_nids; /* # of available node ids */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900607 nid_t next_scan_nid; /* the next nid to be scanned */
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +0900608 unsigned int ram_thresh; /* control the memory footprint */
Chao Yuea1a29a02015-10-12 17:08:48 +0800609 unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */
Chao Yu2304cb02016-01-18 18:32:58 +0800610 unsigned int dirty_nats_ratio; /* control dirty nats ratio threshold */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900611
612 /* NAT cache management */
613 struct radix_tree_root nat_root;/* root of the nat entry cache */
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700614 struct radix_tree_root nat_set_root;/* root of the nat set cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700615 struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900616 struct list_head nat_entries; /* cached nat entry list (clean) */
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700617 unsigned int nat_cnt; /* the # of cached nat entries */
Chao Yuaec71382014-06-24 09:18:20 +0800618 unsigned int dirty_nat_cnt; /* total num of nat entries in set */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700619 unsigned int nat_blocks; /* # of nat blocks */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900620
621 /* free node ids management */
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +0900622 struct radix_tree_root free_nid_root;/* root of the free_nid cache */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700623 struct list_head nid_list[MAX_NID_LIST];/* lists for free nids */
624 unsigned int nid_cnt[MAX_NID_LIST]; /* the number of free node id */
625 spinlock_t nid_list_lock; /* protect nid lists ops */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900626 struct mutex build_lock; /* lock for build free nids */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700627 unsigned char (*free_nid_bitmap)[NAT_ENTRY_BITMAP_SIZE];
628 unsigned char *nat_block_bitmap;
629 unsigned short *free_nid_count; /* free nid count of NAT block */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900630
631 /* for checkpoint */
632 char *nat_bitmap; /* NAT bitmap pointer */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700633
634 unsigned int nat_bits_blocks; /* # of nat bits blocks */
635 unsigned char *nat_bits; /* NAT bits blocks */
636 unsigned char *full_nat_bits; /* full NAT pages */
637 unsigned char *empty_nat_bits; /* empty NAT pages */
638#ifdef CONFIG_F2FS_CHECK_FS
639 char *nat_bitmap_mir; /* NAT bitmap mirror */
640#endif
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900641 int bitmap_size; /* bitmap size */
642};
643
644/*
645 * this structure is used as one of function parameters.
646 * all the information are dedicated to a given direct node block determined
647 * by the data offset in a file.
648 */
649struct dnode_of_data {
650 struct inode *inode; /* vfs inode pointer */
651 struct page *inode_page; /* its inode page, NULL is possible */
652 struct page *node_page; /* cached direct node page */
653 nid_t nid; /* node id of the direct node block */
654 unsigned int ofs_in_node; /* data offset in the node page */
655 bool inode_page_locked; /* inode page is locked or not */
Jaegeuk Kim93bae092015-12-22 12:59:54 -0800656 bool node_changed; /* is node block changed */
Chao Yu3cf45742016-01-26 15:40:44 +0800657 char cur_level; /* level of hole node page */
658 char max_level; /* level of current page located */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900659 block_t data_blkaddr; /* block address of the node block */
660};
661
662static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
663 struct page *ipage, struct page *npage, nid_t nid)
664{
Jaegeuk Kimd66d1f72013-01-03 08:57:21 +0900665 memset(dn, 0, sizeof(*dn));
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900666 dn->inode = inode;
667 dn->inode_page = ipage;
668 dn->node_page = npage;
669 dn->nid = nid;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900670}
671
672/*
673 * For SIT manager
674 *
675 * By default, there are 6 active log areas across the whole main area.
676 * When considering hot and cold data separation to reduce cleaning overhead,
677 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
678 * respectively.
679 * In the current design, you should not change the numbers intentionally.
680 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
681 * logs individually according to the underlying devices. (default: 6)
682 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
683 * data and 8 for node logs.
684 */
685#define NR_CURSEG_DATA_TYPE (3)
686#define NR_CURSEG_NODE_TYPE (3)
687#define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
688
689enum {
690 CURSEG_HOT_DATA = 0, /* directory entry blocks */
691 CURSEG_WARM_DATA, /* data blocks */
692 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
693 CURSEG_HOT_NODE, /* direct node blocks of directory files */
694 CURSEG_WARM_NODE, /* direct node blocks of normal files */
695 CURSEG_COLD_NODE, /* indirect node blocks */
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800696 NO_CHECK_TYPE,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900697};
698
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900699struct flush_cmd {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900700 struct completion wait;
Gu Zheng721bd4d2014-09-05 18:31:00 +0800701 struct llist_node llnode;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900702 int ret;
703};
704
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800705struct flush_cmd_control {
706 struct task_struct *f2fs_issue_flush; /* flush thread */
707 wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700708 atomic_t issued_flush; /* # of issued flushes */
709 atomic_t issing_flush; /* # of issing flushes */
Gu Zheng721bd4d2014-09-05 18:31:00 +0800710 struct llist_head issue_list; /* list for command issue */
711 struct llist_node *dispatch_list; /* list for command dispatch */
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800712};
713
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900714struct f2fs_sm_info {
715 struct sit_info *sit_info; /* whole segment information */
716 struct free_segmap_info *free_info; /* free segment information */
717 struct dirty_seglist_info *dirty_info; /* dirty segment information */
718 struct curseg_info *curseg_array; /* active segment information */
719
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900720 block_t seg0_blkaddr; /* block address of 0'th segment */
721 block_t main_blkaddr; /* start block address of main area */
722 block_t ssa_blkaddr; /* start block address of SSA area */
723
724 unsigned int segment_count; /* total # of segments */
725 unsigned int main_segments; /* # of segments in main area */
726 unsigned int reserved_segments; /* # of reserved segments */
727 unsigned int ovp_segments; /* # of overprovision segments */
Jaegeuk Kim81eb8d62013-10-24 13:31:34 +0900728
729 /* a threshold to reclaim prefree segments */
730 unsigned int rec_prefree_segments;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +0900731
Jaegeuk Kimbba681c2015-01-26 17:41:23 -0800732 /* for batched trimming */
733 unsigned int trim_sections; /* # of sections to trim */
734
Chao Yu184a5cd2014-09-04 18:13:01 +0800735 struct list_head sit_entry_set; /* sit entry set list */
736
Jaegeuk Kim216fbd62013-11-07 13:13:42 +0900737 unsigned int ipu_policy; /* in-place-update policy */
738 unsigned int min_ipu_util; /* in-place-update threshold */
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700739 unsigned int min_fsync_blocks; /* threshold for fsync */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700740 unsigned int min_hot_blocks; /* threshold for hot block allocation */
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900741
742 /* for flush command control */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700743 struct flush_cmd_control *fcc_info;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800744
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700745 /* for discard command control */
746 struct discard_cmd_control *dcc_info;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900747};
748
749/*
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900750 * For superblock
751 */
752/*
753 * COUNT_TYPE for monitoring
754 *
755 * f2fs monitors the number of several block types such as on-writeback,
756 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
757 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700758#define WB_DATA_TYPE(p) (__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900759enum count_type {
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900760 F2FS_DIRTY_DENTS,
Chao Yuc227f912015-12-16 13:09:20 +0800761 F2FS_DIRTY_DATA,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900762 F2FS_DIRTY_NODES,
763 F2FS_DIRTY_META,
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800764 F2FS_INMEM_PAGES,
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700765 F2FS_DIRTY_IMETA,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700766 F2FS_WB_CP_DATA,
767 F2FS_WB_DATA,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900768 NR_COUNT_TYPE,
769};
770
771/*
arter97e1c42042014-08-06 23:22:50 +0900772 * The below are the page types of bios used in submit_bio().
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900773 * The available types are:
774 * DATA User data pages. It operates as async mode.
775 * NODE Node pages. It operates as async mode.
776 * META FS metadata pages such as SIT, NAT, CP.
777 * NR_PAGE_TYPE The number of page types.
778 * META_FLUSH Make sure the previous pages are written
779 * with waiting the bio's completion
780 * ... Only can be used with META.
781 */
Jaegeuk Kim7d5e5102013-11-18 17:13:35 +0900782#define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900783enum page_type {
784 DATA,
785 NODE,
786 META,
787 NR_PAGE_TYPE,
788 META_FLUSH,
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700789 INMEM, /* the below types are used by tracepoints only. */
790 INMEM_DROP,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700791 INMEM_INVALIDATE,
Chao Yu28bc1062016-02-06 14:40:34 +0800792 INMEM_REVOKE,
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700793 IPU,
794 OPU,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900795};
796
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700797enum temp_type {
798 HOT = 0, /* must be zero for meta bio */
799 WARM,
800 COLD,
801 NR_TEMP_TYPE,
802};
803
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -0700804enum need_lock_type {
805 LOCK_REQ = 0,
806 LOCK_DONE,
807 LOCK_RETRY,
808};
809
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900810struct f2fs_io_info {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700811 struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
Gu Zheng7e8f2302013-12-20 18:17:49 +0800812 enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700813 enum temp_type temp; /* contains HOT/WARM/COLD */
Mike Christie04d328d2016-06-05 14:31:55 -0500814 int op; /* contains REQ_OP_ */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700815 int op_flags; /* req_flag_bits */
Chao Yu7a9d7542016-02-22 18:36:38 +0800816 block_t new_blkaddr; /* new block address to be written */
Chao Yu28bc1062016-02-06 14:40:34 +0800817 block_t old_blkaddr; /* old block address before Cow */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700818 struct page *page; /* page to be written */
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700819 struct page *encrypted_page; /* encrypted page */
Chao Yuc52dc0f2017-05-19 23:37:01 +0800820 struct list_head list; /* serialize IOs */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700821 bool submitted; /* indicate IO submission */
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -0700822 int need_lock; /* indicate we need to lock cp_rwsem */
Chao Yuc52dc0f2017-05-19 23:37:01 +0800823 bool in_list; /* indicate fio is in io_list */
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900824};
825
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700826#define is_read_io(rw) ((rw) == READ)
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900827struct f2fs_bio_info {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900828 struct f2fs_sb_info *sbi; /* f2fs superblock */
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900829 struct bio *bio; /* bios to merge */
830 sector_t last_block_in_bio; /* last block number */
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900831 struct f2fs_io_info fio; /* store buffered io info. */
Chao Yudf0f8dc2014-03-22 14:57:23 +0800832 struct rw_semaphore io_rwsem; /* blocking op for bio */
Chao Yuc52dc0f2017-05-19 23:37:01 +0800833 spinlock_t io_lock; /* serialize DATA/NODE IOs */
834 struct list_head io_list; /* track fios */
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900835};
836
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700837#define FDEV(i) (sbi->devs[i])
838#define RDEV(i) (raw_super->devs[i])
839struct f2fs_dev_info {
840 struct block_device *bdev;
841 char path[MAX_PATH_LEN];
842 unsigned int total_segments;
843 block_t start_blk;
844 block_t end_blk;
845#ifdef CONFIG_BLK_DEV_ZONED
846 unsigned int nr_blkz; /* Total number of zones */
847 u8 *blkz_type; /* Array of zones type */
848#endif
849};
850
Chao Yuc227f912015-12-16 13:09:20 +0800851enum inode_type {
852 DIR_INODE, /* for dirty dir inode */
853 FILE_INODE, /* for dirty regular/symlink inode */
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700854 DIRTY_META, /* for all dirtied inode metadata */
Chao Yuc227f912015-12-16 13:09:20 +0800855 NR_INODE_TYPE,
856};
857
Chao Yu67298802014-11-18 11:18:36 +0800858/* for inner inode cache management */
859struct inode_management {
860 struct radix_tree_root ino_root; /* ino entry array */
861 spinlock_t ino_lock; /* for ino entry lock */
862 struct list_head ino_list; /* inode list head */
863 unsigned long ino_num; /* number of entries */
864};
865
Chao Yucaf00472015-01-28 17:48:42 +0800866/* For s_flag in struct f2fs_sb_info */
867enum {
868 SBI_IS_DIRTY, /* dirty flag for checkpoint */
869 SBI_IS_CLOSE, /* specify unmounting */
870 SBI_NEED_FSCK, /* need fsck.f2fs to fix */
871 SBI_POR_DOING, /* recovery is doing or not */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -0700872 SBI_NEED_SB_WRITE, /* need to recover superblock */
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700873 SBI_NEED_CP, /* need to checkpoint */
Chao Yucaf00472015-01-28 17:48:42 +0800874};
875
Jaegeuk Kim6beceb52016-01-08 15:51:50 -0800876enum {
877 CP_TIME,
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800878 REQ_TIME,
Jaegeuk Kim6beceb52016-01-08 15:51:50 -0800879 MAX_TIME,
880};
881
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900882struct f2fs_sb_info {
883 struct super_block *sb; /* pointer to VFS super block */
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900884 struct proc_dir_entry *s_proc; /* proc entry */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900885 struct f2fs_super_block *raw_super; /* raw super block pointer */
Chao Yue8240f62015-12-15 17:19:26 +0800886 int valid_super_block; /* valid super block no */
Chao Yufadb2fb2016-09-20 10:29:47 +0800887 unsigned long s_flag; /* flags for sbi */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900888
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700889#ifdef CONFIG_BLK_DEV_ZONED
890 unsigned int blocks_per_blkz; /* F2FS blocks per zone */
891 unsigned int log_blocks_per_blkz; /* log2 F2FS blocks per zone */
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700892#endif
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700893
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900894 /* for node-related operations */
895 struct f2fs_nm_info *nm_info; /* node manager */
896 struct inode *node_inode; /* cache node blocks */
897
898 /* for segment-related operations */
899 struct f2fs_sm_info *sm_info; /* segment manager */
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900900
901 /* for bio operations */
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700902 struct f2fs_bio_info *write_io[NR_PAGE_TYPE]; /* for write bios */
Chao Yu06504682017-05-19 23:37:00 +0800903 struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
904 /* bio ordering for NODE/DATA */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700905 int write_io_size_bits; /* Write IO size bits */
906 mempool_t *write_io_dummy; /* Dummy pages */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900907
908 /* for checkpoint */
909 struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
Jaegeuk Kim01e15b32016-11-24 12:45:15 -0800910 int cur_cp_pack; /* remain current cp pack */
Chao Yuaaec2b12016-09-20 11:04:18 +0800911 spinlock_t cp_lock; /* for flag in ckpt */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900912 struct inode *meta_inode; /* cache meta blocks */
Jaegeuk Kim39936832012-11-22 16:21:29 +0900913 struct mutex cp_mutex; /* checkpoint procedure lock */
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700914 struct rw_semaphore cp_rwsem; /* blocking FS operations */
Chao Yub3582c62014-07-03 18:58:39 +0800915 struct rw_semaphore node_write; /* locking node writes */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700916 struct rw_semaphore node_change; /* locking node change */
Changman Leefb51b5e2013-11-07 12:48:25 +0900917 wait_queue_head_t cp_wait;
Jaegeuk Kim6beceb52016-01-08 15:51:50 -0800918 unsigned long last_time[MAX_TIME]; /* to store time in jiffies */
919 long interval_time[MAX_TIME]; /* to store thresholds */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900920
Chao Yu67298802014-11-18 11:18:36 +0800921 struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
Jaegeuk Kim6451e042014-07-25 15:47:17 -0700922
923 /* for orphan inode, use 0'th array */
Gu Zheng0d47c1a2013-12-26 18:24:19 +0800924 unsigned int max_orphans; /* max orphan inodes */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900925
Chao Yuc227f912015-12-16 13:09:20 +0800926 /* for inode management */
927 struct list_head inode_list[NR_INODE_TYPE]; /* dirty inode list */
928 spinlock_t inode_lock[NR_INODE_TYPE]; /* for dirty inode list lock */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900929
Chao Yu13054c52015-02-05 17:52:58 +0800930 /* for extent tree cache */
931 struct radix_tree_root extent_tree_root;/* cache extent cache entries */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700932 struct mutex extent_tree_lock; /* locking extent radix tree */
Chao Yu13054c52015-02-05 17:52:58 +0800933 struct list_head extent_list; /* lru list for shrinker */
934 spinlock_t extent_lock; /* locking extent lru list */
Jaegeuk Kim7441cce2015-12-21 19:20:15 -0800935 atomic_t total_ext_tree; /* extent tree count */
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800936 struct list_head zombie_list; /* extent zombie tree list */
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800937 atomic_t total_zombie_tree; /* extent zombie tree count */
Chao Yu13054c52015-02-05 17:52:58 +0800938 atomic_t total_ext_node; /* extent info count */
939
arter97e1c42042014-08-06 23:22:50 +0900940 /* basic filesystem units */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900941 unsigned int log_sectors_per_block; /* log2 sectors per block */
942 unsigned int log_blocksize; /* log2 block size */
943 unsigned int blocksize; /* block size */
944 unsigned int root_ino_num; /* root inode number*/
945 unsigned int node_ino_num; /* node inode number*/
946 unsigned int meta_ino_num; /* meta inode number*/
947 unsigned int log_blocks_per_seg; /* log2 blocks per segment */
948 unsigned int blocks_per_seg; /* blocks per segment */
949 unsigned int segs_per_sec; /* segments per section */
950 unsigned int secs_per_zone; /* sections per zone */
951 unsigned int total_sections; /* total section count */
952 unsigned int total_node_count; /* total node block count */
953 unsigned int total_valid_node_count; /* valid node block count */
Chao Yue0afc4d2015-12-31 14:35:37 +0800954 loff_t max_file_blocks; /* max block index of file */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900955 int active_logs; /* # of active logs */
Jaegeuk Kimab9fa662014-02-27 20:09:05 +0900956 int dir_level; /* directory level */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900957
958 block_t user_block_count; /* # of user blocks */
959 block_t total_valid_block_count; /* # of valid blocks */
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700960 block_t discard_blks; /* discard command candidats */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900961 block_t last_valid_block_count; /* for recovery */
962 u32 s_next_generation; /* for NFS support */
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700963
964 /* # of pages, see count_type */
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -0700965 atomic_t nr_pages[NR_COUNT_TYPE];
Jaegeuk Kim41382ec2016-05-16 11:06:50 -0700966 /* # of allocated blocks */
967 struct percpu_counter alloc_valid_block_count;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900968
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700969 /* writeback control */
970 atomic_t wb_sync_req; /* count # of WB_SYNC threads */
971
Jaegeuk Kim513c5f32016-05-16 11:42:32 -0700972 /* valid inode count */
973 struct percpu_counter total_valid_inode_count;
974
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900975 struct f2fs_mount_info mount_opt; /* mount options */
976
977 /* for cleaning operations */
978 struct mutex gc_mutex; /* mutex for GC */
979 struct f2fs_gc_kthread *gc_thread; /* GC thread */
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900980 unsigned int cur_victim_sec; /* current victim section num */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900981
Hou Pengyang4992ba22017-02-16 12:34:31 +0000982 /* threshold for converting bg victims for fg */
983 u64 fggc_threshold;
984
Jaegeuk Kimb1c57c12014-01-08 13:45:08 +0900985 /* maximum # of trials to find a victim segment for SSR and GC */
986 unsigned int max_victim_search;
987
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900988 /*
989 * for stat information.
990 * one is for the LFS mode, and the other is for the SSR mode.
991 */
Namjae Jeon35b09d82013-05-23 22:57:53 +0900992#ifdef CONFIG_F2FS_STAT_FS
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900993 struct f2fs_stat_info *stat_info; /* FS status information */
994 unsigned int segment_count[2]; /* # of allocated segments */
995 unsigned int block_count[2]; /* # of allocated blocks */
Changman Leeb9a2c252014-12-24 02:16:54 +0900996 atomic_t inplace_count; /* # of inplace update */
Chao Yu5b7ee372015-09-30 17:38:48 +0800997 atomic64_t total_hit_ext; /* # of lookup extent cache */
998 atomic64_t read_hit_rbtree; /* # of hit rbtree extent node */
999 atomic64_t read_hit_largest; /* # of hit largest extent node */
1000 atomic64_t read_hit_cached; /* # of hit cached extent node */
Chao Yud5e8f6c2015-07-15 17:28:53 +08001001 atomic_t inline_xattr; /* # of inline_xattr inodes */
Chao Yu03e14d52014-12-08 19:08:20 +08001002 atomic_t inline_inode; /* # of inline_data inodes */
1003 atomic_t inline_dir; /* # of inline_dentry inodes */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001004 atomic_t aw_cnt; /* # of atomic writes */
1005 atomic_t vw_cnt; /* # of volatile writes */
1006 atomic_t max_aw_cnt; /* max # of atomic writes */
1007 atomic_t max_vw_cnt; /* max # of volatile writes */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001008 int bg_gc; /* background gc calls */
Chao Yu33fbd512015-12-17 17:14:44 +08001009 unsigned int ndirty_inode[NR_INODE_TYPE]; /* # of dirty inodes */
Namjae Jeon35b09d82013-05-23 22:57:53 +09001010#endif
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001011 spinlock_t stat_lock; /* lock for stat operations */
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09001012
1013 /* For sysfs suppport */
1014 struct kobject s_kobj;
1015 struct completion s_kobj_unregister;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001016
1017 /* For shrinker support */
1018 struct list_head s_list;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001019 int s_ndevs; /* number of devices */
1020 struct f2fs_dev_info *devs; /* for device list */
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001021 struct mutex umount_mutex;
1022 unsigned int shrinker_run_no;
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001023
1024 /* For write statistics */
1025 u64 sectors_written_start;
1026 u64 kbytes_written;
Keith Mok43b65732016-03-02 12:04:24 -08001027
1028 /* Reference to checksum algorithm driver via cryptoapi */
1029 struct crypto_shash *s_chksum_driver;
Chao Yu1ecc0c52016-09-23 21:30:09 +08001030
1031 /* For fault injection */
1032#ifdef CONFIG_F2FS_FAULT_INJECTION
1033 struct f2fs_fault_info fault_info;
1034#endif
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001035};
1036
Chao Yu1ecc0c52016-09-23 21:30:09 +08001037#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001038#define f2fs_show_injection_info(type) \
1039 printk("%sF2FS-fs : inject %s in %s of %pF\n", \
1040 KERN_INFO, fault_name[type], \
1041 __func__, __builtin_return_address(0))
Chao Yu1ecc0c52016-09-23 21:30:09 +08001042static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
1043{
1044 struct f2fs_fault_info *ffi = &sbi->fault_info;
1045
1046 if (!ffi->inject_rate)
1047 return false;
1048
1049 if (!IS_FAULT_SET(ffi, type))
1050 return false;
1051
1052 atomic_inc(&ffi->inject_ops);
1053 if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
1054 atomic_set(&ffi->inject_ops, 0);
Chao Yu1ecc0c52016-09-23 21:30:09 +08001055 return true;
1056 }
1057 return false;
1058}
1059#endif
1060
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001061/* For write statistics. Suppose sector size is 512 bytes,
1062 * and the return value is in kbytes. s is of struct f2fs_sb_info.
1063 */
1064#define BD_PART_WRITTEN(s) \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001065(((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) - \
1066 (s)->sectors_written_start) >> 1)
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001067
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08001068static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
1069{
1070 sbi->last_time[type] = jiffies;
1071}
1072
1073static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
1074{
1075 struct timespec ts = {sbi->interval_time[type], 0};
1076 unsigned long interval = timespec_to_jiffies(&ts);
1077
1078 return time_after(jiffies, sbi->last_time[type] + interval);
1079}
1080
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001081static inline bool is_idle(struct f2fs_sb_info *sbi)
1082{
1083 struct block_device *bdev = sbi->sb->s_bdev;
1084 struct request_queue *q = bdev_get_queue(bdev);
1085 struct request_list *rl = &q->root_rl;
1086
1087 if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1088 return 0;
1089
1090 return f2fs_time_over(sbi, REQ_TIME);
1091}
1092
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001093/*
1094 * Inline functions
1095 */
Keith Mok43b65732016-03-02 12:04:24 -08001096static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1097 unsigned int length)
1098{
1099 SHASH_DESC_ON_STACK(shash, sbi->s_chksum_driver);
1100 u32 *ctx = (u32 *)shash_desc_ctx(shash);
David Millerb355b892017-06-02 11:28:54 -04001101 u32 retval;
Keith Mok43b65732016-03-02 12:04:24 -08001102 int err;
1103
1104 shash->tfm = sbi->s_chksum_driver;
1105 shash->flags = 0;
1106 *ctx = F2FS_SUPER_MAGIC;
1107
1108 err = crypto_shash_update(shash, address, length);
1109 BUG_ON(err);
1110
David Millerb355b892017-06-02 11:28:54 -04001111 retval = *ctx;
1112 barrier_data(ctx);
1113 return retval;
Keith Mok43b65732016-03-02 12:04:24 -08001114}
1115
1116static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1117 void *buf, size_t buf_size)
1118{
1119 return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1120}
1121
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001122static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
1123{
1124 return container_of(inode, struct f2fs_inode_info, vfs_inode);
1125}
1126
1127static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
1128{
1129 return sb->s_fs_info;
1130}
1131
Jaegeuk Kim40813632014-09-02 15:31:18 -07001132static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
1133{
1134 return F2FS_SB(inode->i_sb);
1135}
1136
1137static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
1138{
1139 return F2FS_I_SB(mapping->host);
1140}
1141
1142static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
1143{
1144 return F2FS_M_SB(page->mapping);
1145}
1146
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001147static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
1148{
1149 return (struct f2fs_super_block *)(sbi->raw_super);
1150}
1151
1152static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
1153{
1154 return (struct f2fs_checkpoint *)(sbi->ckpt);
1155}
1156
Gu Zheng45590712013-07-15 17:57:38 +08001157static inline struct f2fs_node *F2FS_NODE(struct page *page)
1158{
1159 return (struct f2fs_node *)page_address(page);
1160}
1161
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001162static inline struct f2fs_inode *F2FS_INODE(struct page *page)
1163{
1164 return &((struct f2fs_node *)page_address(page))->i;
1165}
1166
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001167static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
1168{
1169 return (struct f2fs_nm_info *)(sbi->nm_info);
1170}
1171
1172static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
1173{
1174 return (struct f2fs_sm_info *)(sbi->sm_info);
1175}
1176
1177static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
1178{
1179 return (struct sit_info *)(SM_I(sbi)->sit_info);
1180}
1181
1182static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
1183{
1184 return (struct free_segmap_info *)(SM_I(sbi)->free_info);
1185}
1186
1187static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
1188{
1189 return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
1190}
1191
Gu Zheng9df27d92014-01-20 18:37:04 +08001192static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
1193{
1194 return sbi->meta_inode->i_mapping;
1195}
1196
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001197static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
1198{
1199 return sbi->node_inode->i_mapping;
1200}
1201
Chao Yucaf00472015-01-28 17:48:42 +08001202static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001203{
Chao Yufadb2fb2016-09-20 10:29:47 +08001204 return test_bit(type, &sbi->s_flag);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001205}
1206
Chao Yucaf00472015-01-28 17:48:42 +08001207static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001208{
Chao Yufadb2fb2016-09-20 10:29:47 +08001209 set_bit(type, &sbi->s_flag);
Chao Yucaf00472015-01-28 17:48:42 +08001210}
1211
1212static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1213{
Chao Yufadb2fb2016-09-20 10:29:47 +08001214 clear_bit(type, &sbi->s_flag);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001215}
1216
Jaegeuk Kimd71b5562013-08-09 15:03:21 +09001217static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1218{
1219 return le64_to_cpu(cp->checkpoint_ver);
1220}
1221
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001222static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1223{
1224 size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1225 return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1226}
1227
Chao Yuaaec2b12016-09-20 11:04:18 +08001228static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001229{
1230 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
Chao Yuaaec2b12016-09-20 11:04:18 +08001231
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001232 return ckpt_flags & f;
1233}
1234
Chao Yuaaec2b12016-09-20 11:04:18 +08001235static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001236{
Chao Yuaaec2b12016-09-20 11:04:18 +08001237 return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1238}
1239
1240static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1241{
1242 unsigned int ckpt_flags;
1243
1244 ckpt_flags = le32_to_cpu(cp->ckpt_flags);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001245 ckpt_flags |= f;
1246 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1247}
1248
Chao Yuaaec2b12016-09-20 11:04:18 +08001249static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001250{
Chao Yuaaec2b12016-09-20 11:04:18 +08001251 spin_lock(&sbi->cp_lock);
1252 __set_ckpt_flags(F2FS_CKPT(sbi), f);
1253 spin_unlock(&sbi->cp_lock);
1254}
1255
1256static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1257{
1258 unsigned int ckpt_flags;
1259
1260 ckpt_flags = le32_to_cpu(cp->ckpt_flags);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001261 ckpt_flags &= (~f);
1262 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1263}
1264
Chao Yuaaec2b12016-09-20 11:04:18 +08001265static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1266{
1267 spin_lock(&sbi->cp_lock);
1268 __clear_ckpt_flags(F2FS_CKPT(sbi), f);
1269 spin_unlock(&sbi->cp_lock);
1270}
1271
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001272static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001273{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001274 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001275
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001276 if (lock)
1277 spin_lock(&sbi->cp_lock);
1278 __clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
1279 kfree(NM_I(sbi)->nat_bits);
1280 NM_I(sbi)->nat_bits = NULL;
1281 if (lock)
1282 spin_unlock(&sbi->cp_lock);
1283}
1284
1285static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
1286 struct cp_control *cpc)
1287{
1288 bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
1289
1290 return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001291}
1292
Gu Zhenge4795562013-09-27 18:08:30 +08001293static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001294{
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001295 down_read(&sbi->cp_rwsem);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001296}
1297
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001298static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1299{
1300 return down_read_trylock(&sbi->cp_rwsem);
1301}
1302
Gu Zhenge4795562013-09-27 18:08:30 +08001303static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001304{
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001305 up_read(&sbi->cp_rwsem);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001306}
1307
Gu Zhenge4795562013-09-27 18:08:30 +08001308static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001309{
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001310 down_write(&sbi->cp_rwsem);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001311}
1312
Gu Zhenge4795562013-09-27 18:08:30 +08001313static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001314{
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001315 up_write(&sbi->cp_rwsem);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001316}
1317
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001318static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1319{
1320 int reason = CP_SYNC;
1321
1322 if (test_opt(sbi, FASTBOOT))
1323 reason = CP_FASTBOOT;
1324 if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1325 reason = CP_UMOUNT;
1326 return reason;
1327}
1328
1329static inline bool __remain_node_summaries(int reason)
1330{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001331 return (reason & (CP_UMOUNT | CP_FASTBOOT));
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001332}
1333
1334static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1335{
Chao Yuaaec2b12016-09-20 11:04:18 +08001336 return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1337 is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001338}
1339
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001340/*
1341 * Check whether the given nid is within node id range.
1342 */
Namjae Jeon064e0822013-03-17 17:27:20 +09001343static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001344{
Chao Yud6b7d4b2014-06-12 13:23:41 +08001345 if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1346 return -EINVAL;
Chao Yucfb271d2013-12-05 17:15:22 +08001347 if (unlikely(nid >= NM_I(sbi)->max_nid))
Namjae Jeon064e0822013-03-17 17:27:20 +09001348 return -EINVAL;
1349 return 0;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001350}
1351
1352#define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
1353
1354/*
1355 * Check whether the inode has blocks or not
1356 */
1357static inline int F2FS_HAS_BLOCKS(struct inode *inode)
1358{
1359 if (F2FS_I(inode)->i_xattr_nid)
Chris Fries6c311ec2014-01-17 14:44:39 -06001360 return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001361 else
Chris Fries6c311ec2014-01-17 14:44:39 -06001362 return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001363}
1364
Chao Yu4bc8e9b2014-03-17 16:35:06 +08001365static inline bool f2fs_has_xattr_block(unsigned int ofs)
1366{
1367 return ofs == XATTR_NODE_OFFSET;
1368}
1369
Jaegeuk Kim8edd03c2016-05-20 09:26:06 -07001370static inline void f2fs_i_blocks_write(struct inode *, blkcnt_t, bool);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001371static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
Chao Yu46008c62016-05-09 19:56:30 +08001372 struct inode *inode, blkcnt_t *count)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001373{
Jaegeuk Kimdd11a5d2016-07-19 19:20:11 -07001374 blkcnt_t diff;
1375
Jaegeuk Kimcb789422016-04-29 16:29:22 -07001376#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001377 if (time_to_inject(sbi, FAULT_BLOCK)) {
1378 f2fs_show_injection_info(FAULT_BLOCK);
Jaegeuk Kimcb789422016-04-29 16:29:22 -07001379 return false;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001380 }
Jaegeuk Kimcb789422016-04-29 16:29:22 -07001381#endif
Jaegeuk Kimdd11a5d2016-07-19 19:20:11 -07001382 /*
1383 * let's increase this in prior to actual block count change in order
1384 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1385 */
1386 percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
Chao Yucfb271d2013-12-05 17:15:22 +08001387
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001388 spin_lock(&sbi->stat_lock);
Jaegeuk Kim2555a2d2016-06-30 19:02:06 -07001389 sbi->total_valid_block_count += (block_t)(*count);
1390 if (unlikely(sbi->total_valid_block_count > sbi->user_block_count)) {
Jaegeuk Kimdd11a5d2016-07-19 19:20:11 -07001391 diff = sbi->total_valid_block_count - sbi->user_block_count;
1392 *count -= diff;
Jaegeuk Kim2555a2d2016-06-30 19:02:06 -07001393 sbi->total_valid_block_count = sbi->user_block_count;
Chao Yu46008c62016-05-09 19:56:30 +08001394 if (!*count) {
1395 spin_unlock(&sbi->stat_lock);
Jaegeuk Kimdd11a5d2016-07-19 19:20:11 -07001396 percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
Chao Yu46008c62016-05-09 19:56:30 +08001397 return false;
1398 }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001399 }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001400 spin_unlock(&sbi->stat_lock);
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001401
Jaegeuk Kim2555a2d2016-06-30 19:02:06 -07001402 f2fs_i_blocks_write(inode, *count, true);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001403 return true;
1404}
1405
Gu Zhengda19b0d2013-11-19 18:03:27 +08001406static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001407 struct inode *inode,
1408 blkcnt_t count)
1409{
1410 spin_lock(&sbi->stat_lock);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001411 f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
1412 f2fs_bug_on(sbi, inode->i_blocks < count);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001413 sbi->total_valid_block_count -= (block_t)count;
1414 spin_unlock(&sbi->stat_lock);
Jaegeuk Kim2555a2d2016-06-30 19:02:06 -07001415 f2fs_i_blocks_write(inode, count, false);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001416}
1417
1418static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
1419{
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07001420 atomic_inc(&sbi->nr_pages[count_type]);
Chao Yu7c4abcb2016-08-18 17:46:14 +08001421
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001422 if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
1423 count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
Chao Yu7c4abcb2016-08-18 17:46:14 +08001424 return;
1425
Chao Yucaf00472015-01-28 17:48:42 +08001426 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001427}
1428
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001429static inline void inode_inc_dirty_pages(struct inode *inode)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001430{
Jaegeuk Kima43e1c42016-12-02 15:11:32 -08001431 atomic_inc(&F2FS_I(inode)->dirty_pages);
Chao Yuc227f912015-12-16 13:09:20 +08001432 inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1433 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001434}
1435
1436static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
1437{
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07001438 atomic_dec(&sbi->nr_pages[count_type]);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001439}
1440
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001441static inline void inode_dec_dirty_pages(struct inode *inode)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001442{
Chao Yu5ac9f362015-06-29 18:14:10 +08001443 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1444 !S_ISLNK(inode->i_mode))
Jaegeuk Kim1fe54f92014-02-07 10:00:06 +09001445 return;
1446
Jaegeuk Kima43e1c42016-12-02 15:11:32 -08001447 atomic_dec(&F2FS_I(inode)->dirty_pages);
Chao Yuc227f912015-12-16 13:09:20 +08001448 dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1449 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001450}
1451
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001452static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001453{
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07001454 return atomic_read(&sbi->nr_pages[count_type]);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001455}
1456
Jaegeuk Kima43e1c42016-12-02 15:11:32 -08001457static inline int get_dirty_pages(struct inode *inode)
Jaegeuk Kimf8b2c1f2014-03-18 12:33:06 +09001458{
Jaegeuk Kima43e1c42016-12-02 15:11:32 -08001459 return atomic_read(&F2FS_I(inode)->dirty_pages);
Jaegeuk Kimf8b2c1f2014-03-18 12:33:06 +09001460}
1461
Namjae Jeon5ac206c2013-02-02 23:52:59 +09001462static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
1463{
Chao Yu3519e3f2015-12-01 11:56:52 +08001464 unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001465 unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1466 sbi->log_blocks_per_seg;
1467
1468 return segs / sbi->segs_per_sec;
Namjae Jeon5ac206c2013-02-02 23:52:59 +09001469}
1470
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001471static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
1472{
Jaegeuk Kim8b8343f2014-02-24 13:00:13 +09001473 return sbi->total_valid_block_count;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001474}
1475
Yunlei Hef83a2582016-08-18 21:01:18 +08001476static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1477{
1478 return sbi->discard_blks;
1479}
1480
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001481static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
1482{
1483 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1484
1485 /* return NAT or SIT bitmap */
1486 if (flag == NAT_BITMAP)
1487 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
1488 else if (flag == SIT_BITMAP)
1489 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
1490
1491 return 0;
1492}
1493
Wanpeng Li55141482015-02-26 07:57:20 +08001494static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
1495{
1496 return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
1497}
1498
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001499static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
1500{
1501 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Changman Lee1dbe4152014-05-12 12:27:43 +09001502 int offset;
1503
Wanpeng Li55141482015-02-26 07:57:20 +08001504 if (__cp_payload(sbi) > 0) {
Changman Lee1dbe4152014-05-12 12:27:43 +09001505 if (flag == NAT_BITMAP)
1506 return &ckpt->sit_nat_version_bitmap;
1507 else
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -07001508 return (unsigned char *)ckpt + F2FS_BLKSIZE;
Changman Lee1dbe4152014-05-12 12:27:43 +09001509 } else {
1510 offset = (flag == NAT_BITMAP) ?
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001511 le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
Changman Lee1dbe4152014-05-12 12:27:43 +09001512 return &ckpt->sit_nat_version_bitmap + offset;
1513 }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001514}
1515
1516static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
1517{
Jaegeuk Kim01e15b32016-11-24 12:45:15 -08001518 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001519
Jaegeuk Kim01e15b32016-11-24 12:45:15 -08001520 if (sbi->cur_cp_pack == 2)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001521 start_addr += sbi->blocks_per_seg;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001522 return start_addr;
1523}
1524
Jaegeuk Kim01e15b32016-11-24 12:45:15 -08001525static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
1526{
1527 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
1528
1529 if (sbi->cur_cp_pack == 1)
1530 start_addr += sbi->blocks_per_seg;
1531 return start_addr;
1532}
1533
1534static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
1535{
1536 sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
1537}
1538
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001539static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
1540{
1541 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
1542}
1543
1544static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
Gu Zhengef86d702013-11-19 18:03:38 +08001545 struct inode *inode)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001546{
1547 block_t valid_block_count;
1548 unsigned int valid_node_count;
1549
1550 spin_lock(&sbi->stat_lock);
1551
Gu Zhengef86d702013-11-19 18:03:38 +08001552 valid_block_count = sbi->total_valid_block_count + 1;
Chao Yucfb271d2013-12-05 17:15:22 +08001553 if (unlikely(valid_block_count > sbi->user_block_count)) {
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001554 spin_unlock(&sbi->stat_lock);
1555 return false;
1556 }
1557
Gu Zhengef86d702013-11-19 18:03:38 +08001558 valid_node_count = sbi->total_valid_node_count + 1;
Chao Yucfb271d2013-12-05 17:15:22 +08001559 if (unlikely(valid_node_count > sbi->total_node_count)) {
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001560 spin_unlock(&sbi->stat_lock);
1561 return false;
1562 }
1563
1564 if (inode)
Jaegeuk Kim8edd03c2016-05-20 09:26:06 -07001565 f2fs_i_blocks_write(inode, 1, true);
Gu Zhengef86d702013-11-19 18:03:38 +08001566
Gu Zhengef86d702013-11-19 18:03:38 +08001567 sbi->total_valid_node_count++;
1568 sbi->total_valid_block_count++;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001569 spin_unlock(&sbi->stat_lock);
1570
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001571 percpu_counter_inc(&sbi->alloc_valid_block_count);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001572 return true;
1573}
1574
1575static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
Gu Zhengef86d702013-11-19 18:03:38 +08001576 struct inode *inode)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001577{
1578 spin_lock(&sbi->stat_lock);
1579
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001580 f2fs_bug_on(sbi, !sbi->total_valid_block_count);
1581 f2fs_bug_on(sbi, !sbi->total_valid_node_count);
1582 f2fs_bug_on(sbi, !inode->i_blocks);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001583
Jaegeuk Kim8edd03c2016-05-20 09:26:06 -07001584 f2fs_i_blocks_write(inode, 1, false);
Gu Zhengef86d702013-11-19 18:03:38 +08001585 sbi->total_valid_node_count--;
1586 sbi->total_valid_block_count--;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001587
1588 spin_unlock(&sbi->stat_lock);
1589}
1590
1591static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
1592{
Jaegeuk Kim8b8343f2014-02-24 13:00:13 +09001593 return sbi->total_valid_node_count;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001594}
1595
1596static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
1597{
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001598 percpu_counter_inc(&sbi->total_valid_inode_count);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001599}
1600
Jaegeuk Kim0e802202013-11-26 16:36:20 +09001601static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001602{
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001603 percpu_counter_dec(&sbi->total_valid_inode_count);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001604}
1605
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001606static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001607{
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001608 return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001609}
1610
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001611static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1612 pgoff_t index, bool for_write)
1613{
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -07001614#ifdef CONFIG_F2FS_FAULT_INJECTION
1615 struct page *page = find_lock_page(mapping, index);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001616
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -07001617 if (page)
1618 return page;
1619
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001620 if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
1621 f2fs_show_injection_info(FAULT_PAGE_ALLOC);
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -07001622 return NULL;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001623 }
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -07001624#endif
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001625 if (!for_write)
1626 return grab_cache_page(mapping, index);
1627 return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1628}
1629
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001630static inline void f2fs_copy_page(struct page *src, struct page *dst)
1631{
1632 char *src_kaddr = kmap(src);
1633 char *dst_kaddr = kmap(dst);
1634
1635 memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
1636 kunmap(dst);
1637 kunmap(src);
1638}
1639
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001640static inline void f2fs_put_page(struct page *page, int unlock)
1641{
Jaegeuk Kim031fa8c2013-11-28 12:55:13 +09001642 if (!page)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001643 return;
1644
1645 if (unlock) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001646 f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001647 unlock_page(page);
1648 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001649 put_page(page);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001650}
1651
1652static inline void f2fs_put_dnode(struct dnode_of_data *dn)
1653{
1654 if (dn->node_page)
1655 f2fs_put_page(dn->node_page, 1);
1656 if (dn->inode_page && dn->node_page != dn->inode_page)
1657 f2fs_put_page(dn->inode_page, 0);
1658 dn->node_page = NULL;
1659 dn->inode_page = NULL;
1660}
1661
1662static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
Gu Zhenge8512d22014-03-07 18:43:28 +08001663 size_t size)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001664{
Gu Zhenge8512d22014-03-07 18:43:28 +08001665 return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001666}
1667
Gu Zheng7bd59382013-10-22 14:52:26 +08001668static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
1669 gfp_t flags)
1670{
1671 void *entry;
Gu Zheng7bd59382013-10-22 14:52:26 +08001672
Jaegeuk Kim80c54502015-08-20 08:51:56 -07001673 entry = kmem_cache_alloc(cachep, flags);
1674 if (!entry)
1675 entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
Gu Zheng7bd59382013-10-22 14:52:26 +08001676 return entry;
1677}
1678
Jaegeuk Kim740432f2015-08-14 11:43:56 -07001679static inline struct bio *f2fs_bio_alloc(int npages)
1680{
1681 struct bio *bio;
1682
1683 /* No failure on bio allocation */
Jaegeuk Kim740432f2015-08-14 11:43:56 -07001684 bio = bio_alloc(GFP_NOIO, npages);
Jaegeuk Kim80c54502015-08-20 08:51:56 -07001685 if (!bio)
1686 bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
Jaegeuk Kim740432f2015-08-14 11:43:56 -07001687 return bio;
1688}
1689
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08001690static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
1691 unsigned long index, void *item)
1692{
1693 while (radix_tree_insert(root, index, item))
1694 cond_resched();
1695}
1696
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001697#define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
1698
1699static inline bool IS_INODE(struct page *page)
1700{
Gu Zheng45590712013-07-15 17:57:38 +08001701 struct f2fs_node *p = F2FS_NODE(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001702
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001703 return RAW_IS_INODE(p);
1704}
1705
1706static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
1707{
1708 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
1709}
1710
1711static inline block_t datablock_addr(struct page *node_page,
1712 unsigned int offset)
1713{
1714 struct f2fs_node *raw_node;
1715 __le32 *addr_array;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001716
Gu Zheng45590712013-07-15 17:57:38 +08001717 raw_node = F2FS_NODE(node_page);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001718 addr_array = blkaddr_in_node(raw_node);
1719 return le32_to_cpu(addr_array[offset]);
1720}
1721
1722static inline int f2fs_test_bit(unsigned int nr, char *addr)
1723{
1724 int mask;
1725
1726 addr += (nr >> 3);
1727 mask = 1 << (7 - (nr & 0x07));
1728 return mask & *addr;
1729}
1730
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001731static inline void f2fs_set_bit(unsigned int nr, char *addr)
1732{
1733 int mask;
1734
1735 addr += (nr >> 3);
1736 mask = 1 << (7 - (nr & 0x07));
1737 *addr |= mask;
1738}
1739
1740static inline void f2fs_clear_bit(unsigned int nr, char *addr)
1741{
1742 int mask;
1743
1744 addr += (nr >> 3);
1745 mask = 1 << (7 - (nr & 0x07));
1746 *addr &= ~mask;
1747}
1748
Gu Zheng52aca072014-10-20 17:45:51 +08001749static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001750{
1751 int mask;
1752 int ret;
1753
1754 addr += (nr >> 3);
1755 mask = 1 << (7 - (nr & 0x07));
1756 ret = mask & *addr;
1757 *addr |= mask;
1758 return ret;
1759}
1760
Gu Zheng52aca072014-10-20 17:45:51 +08001761static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001762{
1763 int mask;
1764 int ret;
1765
1766 addr += (nr >> 3);
1767 mask = 1 << (7 - (nr & 0x07));
1768 ret = mask & *addr;
1769 *addr &= ~mask;
1770 return ret;
1771}
1772
Gu Zhengc6ac4c02014-10-20 17:45:50 +08001773static inline void f2fs_change_bit(unsigned int nr, char *addr)
1774{
1775 int mask;
1776
1777 addr += (nr >> 3);
1778 mask = 1 << (7 - (nr & 0x07));
1779 *addr ^= mask;
1780}
1781
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001782/* used for f2fs_inode_info->flags */
1783enum {
1784 FI_NEW_INODE, /* indicate newly allocated inode */
Jaegeuk Kimb3783872013-06-10 09:17:01 +09001785 FI_DIRTY_INODE, /* indicate inode is dirty or not */
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001786 FI_AUTO_RECOVER, /* indicate inode is recoverable */
Jaegeuk Kimed57c272014-04-15 11:19:28 +09001787 FI_DIRTY_DIR, /* indicate directory has dirty pages */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001788 FI_INC_LINK, /* need to increment i_nlink */
1789 FI_ACL_MODE, /* indicate acl mode */
1790 FI_NO_ALLOC, /* should not allocate any blocks */
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -07001791 FI_FREE_NID, /* free allocated nide */
Jaegeuk Kimc11abd12013-11-19 10:41:54 +09001792 FI_NO_EXTENT, /* not to use the extent cache */
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001793 FI_INLINE_XATTR, /* used for inline xattr */
Huajun Li1001b342013-11-10 23:13:16 +08001794 FI_INLINE_DATA, /* used for inline data*/
Chao Yu34d67de2014-09-24 18:15:19 +08001795 FI_INLINE_DENTRY, /* used for inline dentry */
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001796 FI_APPEND_WRITE, /* inode has appended data */
1797 FI_UPDATE_WRITE, /* inode has in-place-update data */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001798 FI_NEED_IPU, /* used for ipu per file */
1799 FI_ATOMIC_FILE, /* indicate atomic file */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001800 FI_ATOMIC_COMMIT, /* indicate the state of atomical committing */
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001801 FI_VOLATILE_FILE, /* indicate volatile file */
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001802 FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001803 FI_DROP_CACHE, /* drop dirty page cache */
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001804 FI_DATA_EXIST, /* indicate data exists */
Jaegeuk Kim510022a2015-03-30 15:07:16 -07001805 FI_INLINE_DOTS, /* indicate inline dot dentries */
Chao Yud323d002015-10-27 09:53:45 +08001806 FI_DO_DEFRAG, /* indicate defragment is running */
Chao Yuc227f912015-12-16 13:09:20 +08001807 FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001808 FI_NO_PREALLOC, /* indicate skipped preallocated blocks */
1809 FI_HOT_DATA, /* indicate file is hot */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001810};
1811
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001812static inline void __mark_inode_dirty_flag(struct inode *inode,
1813 int flag, bool set)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001814{
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001815 switch (flag) {
1816 case FI_INLINE_XATTR:
1817 case FI_INLINE_DATA:
1818 case FI_INLINE_DENTRY:
1819 if (set)
1820 return;
1821 case FI_DATA_EXIST:
1822 case FI_INLINE_DOTS:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001823 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001824 }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001825}
1826
Jaegeuk Kim91942322016-05-20 10:13:22 -07001827static inline void set_inode_flag(struct inode *inode, int flag)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001828{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001829 if (!test_bit(flag, &F2FS_I(inode)->flags))
1830 set_bit(flag, &F2FS_I(inode)->flags);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001831 __mark_inode_dirty_flag(inode, flag, true);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001832}
1833
Jaegeuk Kim91942322016-05-20 10:13:22 -07001834static inline int is_inode_flag_set(struct inode *inode, int flag)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001835{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001836 return test_bit(flag, &F2FS_I(inode)->flags);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001837}
1838
Jaegeuk Kim91942322016-05-20 10:13:22 -07001839static inline void clear_inode_flag(struct inode *inode, int flag)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001840{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001841 if (test_bit(flag, &F2FS_I(inode)->flags))
1842 clear_bit(flag, &F2FS_I(inode)->flags);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001843 __mark_inode_dirty_flag(inode, flag, false);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001844}
1845
Jaegeuk Kim91942322016-05-20 10:13:22 -07001846static inline void set_acl_inode(struct inode *inode, umode_t mode)
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001847{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001848 F2FS_I(inode)->i_acl_mode = mode;
1849 set_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001850 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001851}
1852
Jaegeuk Kima1961242016-05-20 09:43:20 -07001853static inline void f2fs_i_links_write(struct inode *inode, bool inc)
1854{
1855 if (inc)
1856 inc_nlink(inode);
1857 else
1858 drop_nlink(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001859 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kima1961242016-05-20 09:43:20 -07001860}
1861
Jaegeuk Kim8edd03c2016-05-20 09:26:06 -07001862static inline void f2fs_i_blocks_write(struct inode *inode,
1863 blkcnt_t diff, bool add)
1864{
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001865 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
1866 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
1867
Jaegeuk Kim8edd03c2016-05-20 09:26:06 -07001868 inode->i_blocks = add ? inode->i_blocks + diff :
1869 inode->i_blocks - diff;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001870 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001871 if (clean || recover)
1872 set_inode_flag(inode, FI_AUTO_RECOVER);
Jaegeuk Kim8edd03c2016-05-20 09:26:06 -07001873}
1874
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001875static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
1876{
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001877 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
1878 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
1879
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001880 if (i_size_read(inode) == i_size)
1881 return;
1882
1883 i_size_write(inode, i_size);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001884 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001885 if (clean || recover)
1886 set_inode_flag(inode, FI_AUTO_RECOVER);
1887}
1888
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001889static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
1890{
1891 F2FS_I(inode)->i_current_depth = depth;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001892 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001893}
1894
1895static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
1896{
1897 F2FS_I(inode)->i_xattr_nid = xnid;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001898 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001899}
1900
1901static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
1902{
1903 F2FS_I(inode)->i_pino = pino;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001904 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001905}
1906
Jaegeuk Kim91942322016-05-20 10:13:22 -07001907static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001908{
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001909 struct f2fs_inode_info *fi = F2FS_I(inode);
1910
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001911 if (ri->i_inline & F2FS_INLINE_XATTR)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001912 set_bit(FI_INLINE_XATTR, &fi->flags);
Huajun Li1001b342013-11-10 23:13:16 +08001913 if (ri->i_inline & F2FS_INLINE_DATA)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001914 set_bit(FI_INLINE_DATA, &fi->flags);
Chao Yu34d67de2014-09-24 18:15:19 +08001915 if (ri->i_inline & F2FS_INLINE_DENTRY)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001916 set_bit(FI_INLINE_DENTRY, &fi->flags);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001917 if (ri->i_inline & F2FS_DATA_EXIST)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001918 set_bit(FI_DATA_EXIST, &fi->flags);
Jaegeuk Kim510022a2015-03-30 15:07:16 -07001919 if (ri->i_inline & F2FS_INLINE_DOTS)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001920 set_bit(FI_INLINE_DOTS, &fi->flags);
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001921}
1922
Jaegeuk Kim91942322016-05-20 10:13:22 -07001923static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001924{
1925 ri->i_inline = 0;
1926
Jaegeuk Kim91942322016-05-20 10:13:22 -07001927 if (is_inode_flag_set(inode, FI_INLINE_XATTR))
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001928 ri->i_inline |= F2FS_INLINE_XATTR;
Jaegeuk Kim91942322016-05-20 10:13:22 -07001929 if (is_inode_flag_set(inode, FI_INLINE_DATA))
Huajun Li1001b342013-11-10 23:13:16 +08001930 ri->i_inline |= F2FS_INLINE_DATA;
Jaegeuk Kim91942322016-05-20 10:13:22 -07001931 if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
Chao Yu34d67de2014-09-24 18:15:19 +08001932 ri->i_inline |= F2FS_INLINE_DENTRY;
Jaegeuk Kim91942322016-05-20 10:13:22 -07001933 if (is_inode_flag_set(inode, FI_DATA_EXIST))
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001934 ri->i_inline |= F2FS_DATA_EXIST;
Jaegeuk Kim91942322016-05-20 10:13:22 -07001935 if (is_inode_flag_set(inode, FI_INLINE_DOTS))
Jaegeuk Kim510022a2015-03-30 15:07:16 -07001936 ri->i_inline |= F2FS_INLINE_DOTS;
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001937}
1938
Chao Yu987c7c32014-03-12 15:59:03 +08001939static inline int f2fs_has_inline_xattr(struct inode *inode)
1940{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001941 return is_inode_flag_set(inode, FI_INLINE_XATTR);
Chao Yu987c7c32014-03-12 15:59:03 +08001942}
1943
Chao Yu81ca7352016-01-26 15:39:35 +08001944static inline unsigned int addrs_per_inode(struct inode *inode)
Jaegeuk Kimde936532013-08-12 21:08:03 +09001945{
Chao Yu81ca7352016-01-26 15:39:35 +08001946 if (f2fs_has_inline_xattr(inode))
Jaegeuk Kimde936532013-08-12 21:08:03 +09001947 return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
1948 return DEF_ADDRS_PER_INODE;
1949}
1950
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001951static inline void *inline_xattr_addr(struct page *page)
1952{
Chao Yu695fd1e2014-02-27 19:52:21 +08001953 struct f2fs_inode *ri = F2FS_INODE(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001954
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001955 return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
1956 F2FS_INLINE_XATTR_ADDRS]);
1957}
1958
1959static inline int inline_xattr_size(struct inode *inode)
1960{
Chao Yu987c7c32014-03-12 15:59:03 +08001961 if (f2fs_has_inline_xattr(inode))
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001962 return F2FS_INLINE_XATTR_ADDRS << 2;
1963 else
1964 return 0;
1965}
1966
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09001967static inline int f2fs_has_inline_data(struct inode *inode)
1968{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001969 return is_inode_flag_set(inode, FI_INLINE_DATA);
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09001970}
1971
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001972static inline int f2fs_exist_data(struct inode *inode)
1973{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001974 return is_inode_flag_set(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001975}
1976
Jaegeuk Kim510022a2015-03-30 15:07:16 -07001977static inline int f2fs_has_inline_dots(struct inode *inode)
1978{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001979 return is_inode_flag_set(inode, FI_INLINE_DOTS);
Jaegeuk Kim510022a2015-03-30 15:07:16 -07001980}
1981
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001982static inline bool f2fs_is_atomic_file(struct inode *inode)
1983{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001984 return is_inode_flag_set(inode, FI_ATOMIC_FILE);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001985}
1986
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001987static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
1988{
1989 return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
1990}
1991
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001992static inline bool f2fs_is_volatile_file(struct inode *inode)
1993{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001994 return is_inode_flag_set(inode, FI_VOLATILE_FILE);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001995}
1996
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001997static inline bool f2fs_is_first_block_written(struct inode *inode)
1998{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001999 return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07002000}
2001
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002002static inline bool f2fs_is_drop_cache(struct inode *inode)
2003{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002004 return is_inode_flag_set(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002005}
2006
Huajun Li1001b342013-11-10 23:13:16 +08002007static inline void *inline_data_addr(struct page *page)
2008{
Chao Yu695fd1e2014-02-27 19:52:21 +08002009 struct f2fs_inode *ri = F2FS_INODE(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002010
Huajun Li1001b342013-11-10 23:13:16 +08002011 return (void *)&(ri->i_addr[1]);
2012}
2013
Chao Yu34d67de2014-09-24 18:15:19 +08002014static inline int f2fs_has_inline_dentry(struct inode *inode)
2015{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002016 return is_inode_flag_set(inode, FI_INLINE_DENTRY);
Chao Yu34d67de2014-09-24 18:15:19 +08002017}
2018
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -08002019static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
2020{
2021 if (!f2fs_has_inline_dentry(dir))
2022 kunmap(page);
2023}
2024
Jaegeuk Kimb5492af72015-04-20 13:44:41 -07002025static inline int is_file(struct inode *inode, int type)
2026{
2027 return F2FS_I(inode)->i_advise & type;
2028}
2029
2030static inline void set_file(struct inode *inode, int type)
2031{
2032 F2FS_I(inode)->i_advise |= type;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002033 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kimb5492af72015-04-20 13:44:41 -07002034}
2035
2036static inline void clear_file(struct inode *inode, int type)
2037{
2038 F2FS_I(inode)->i_advise &= ~type;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002039 f2fs_mark_inode_dirty_sync(inode, true);
2040}
2041
2042static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
2043{
2044 if (dsync) {
2045 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2046 bool ret;
2047
2048 spin_lock(&sbi->inode_lock[DIRTY_META]);
2049 ret = list_empty(&F2FS_I(inode)->gdirty_list);
2050 spin_unlock(&sbi->inode_lock[DIRTY_META]);
2051 return ret;
2052 }
2053 if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
2054 file_keep_isize(inode) ||
2055 i_size_read(inode) & PAGE_MASK)
2056 return false;
2057 return F2FS_I(inode)->last_disk_size == i_size_read(inode);
Jaegeuk Kimb5492af72015-04-20 13:44:41 -07002058}
2059
Jaegeuk Kim77888c12013-05-20 20:28:47 +09002060static inline int f2fs_readonly(struct super_block *sb)
2061{
2062 return sb->s_flags & MS_RDONLY;
2063}
2064
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07002065static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2066{
Chao Yuaaec2b12016-09-20 11:04:18 +08002067 return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07002068}
2069
Jaegeuk Kimeaa693f2015-04-26 00:15:29 -07002070static inline bool is_dot_dotdot(const struct qstr *str)
2071{
2072 if (str->len == 1 && str->name[0] == '.')
2073 return true;
2074
2075 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2076 return true;
2077
2078 return false;
2079}
2080
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07002081static inline bool f2fs_may_extent_tree(struct inode *inode)
2082{
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07002083 if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
Jaegeuk Kim91942322016-05-20 10:13:22 -07002084 is_inode_flag_set(inode, FI_NO_EXTENT))
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07002085 return false;
2086
Al Viro886f56f2015-12-05 03:56:06 -05002087 return S_ISREG(inode->i_mode);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07002088}
2089
Chao Yu1ecc0c52016-09-23 21:30:09 +08002090static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
2091 size_t size, gfp_t flags)
Jaegeuk Kim0414b002016-04-29 15:16:42 -07002092{
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -07002093#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002094 if (time_to_inject(sbi, FAULT_KMALLOC)) {
2095 f2fs_show_injection_info(FAULT_KMALLOC);
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -07002096 return NULL;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002097 }
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -07002098#endif
Jaegeuk Kim0414b002016-04-29 15:16:42 -07002099 return kmalloc(size, flags);
2100}
2101
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002102static inline void *kvmalloc(size_t size, gfp_t flags)
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002103{
2104 void *ret;
2105
2106 ret = kmalloc(size, flags | __GFP_NOWARN);
2107 if (!ret)
2108 ret = __vmalloc(size, flags, PAGE_KERNEL);
2109 return ret;
2110}
2111
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002112static inline void *kvzalloc(size_t size, gfp_t flags)
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002113{
2114 void *ret;
2115
2116 ret = kzalloc(size, flags | __GFP_NOWARN);
2117 if (!ret)
2118 ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
2119 return ret;
2120}
2121
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002122static inline int wbc_to_write_flags(struct writeback_control *wbc)
2123{
2124 if (wbc->sync_mode == WB_SYNC_ALL)
2125 return REQ_SYNC;
2126 else if (wbc->for_kupdate || wbc->for_background)
2127 return 0;
2128
2129 return 0;
2130}
2131
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08002132#define get_inode_mode(i) \
Jaegeuk Kim91942322016-05-20 10:13:22 -07002133 ((is_inode_flag_set(i, FI_ACL_MODE)) ? \
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08002134 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2135
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002136/*
2137 * file.c
2138 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002139int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2140void truncate_data_blocks(struct dnode_of_data *dn);
2141int truncate_blocks(struct inode *inode, u64 from, bool lock);
2142int f2fs_truncate(struct inode *inode);
2143int f2fs_getattr(struct vfsmount *mnt, struct dentry *dentry,
2144 struct kstat *stat);
2145int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2146int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2147int truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2148long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2149long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002150
2151/*
2152 * inode.c
2153 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002154void f2fs_set_inode_flags(struct inode *inode);
2155struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2156struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2157int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2158int update_inode(struct inode *inode, struct page *node_page);
2159int update_inode_page(struct inode *inode);
2160int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2161void f2fs_evict_inode(struct inode *inode);
2162void handle_failed_inode(struct inode *inode);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002163
2164/*
2165 * namei.c
2166 */
2167struct dentry *f2fs_get_parent(struct dentry *child);
2168
2169/*
2170 * dir.c
2171 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002172void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
2173unsigned char get_de_type(struct f2fs_dir_entry *de);
2174struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
2175 f2fs_hash_t namehash, int *max_slots,
2176 struct f2fs_dentry_ptr *d);
2177int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2178 unsigned int start_pos, struct fscrypt_str *fstr);
2179void do_make_empty_dir(struct inode *inode, struct inode *parent,
2180 struct f2fs_dentry_ptr *d);
2181struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
2182 const struct qstr *new_name,
2183 const struct qstr *orig_name, struct page *dpage);
2184void update_parent_metadata(struct inode *dir, struct inode *inode,
2185 unsigned int current_depth);
2186int room_for_filename(const void *bitmap, int slots, int max_slots);
2187void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2188struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2189 struct fscrypt_name *fname, struct page **res_page);
2190struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2191 const struct qstr *child, struct page **res_page);
2192struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2193ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2194 struct page **page);
2195void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2196 struct page *page, struct inode *inode);
2197void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2198 const struct qstr *name, f2fs_hash_t name_hash,
2199 unsigned int bit_pos);
2200int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2201 const struct qstr *orig_name,
2202 struct inode *inode, nid_t ino, umode_t mode);
2203int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
2204 struct inode *inode, nid_t ino, umode_t mode);
2205int __f2fs_add_link(struct inode *dir, const struct qstr *name,
2206 struct inode *inode, nid_t ino, umode_t mode);
2207void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2208 struct inode *dir, struct inode *inode);
2209int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2210bool f2fs_empty_dir(struct inode *dir);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002211
Al Virob7f7a5e2013-01-25 16:15:43 -05002212static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2213{
David Howells2b0143b2015-03-17 22:25:59 +00002214 return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -07002215 inode, inode->i_ino, inode->i_mode);
Al Virob7f7a5e2013-01-25 16:15:43 -05002216}
2217
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002218/*
2219 * super.c
2220 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002221int f2fs_inode_dirtied(struct inode *inode, bool sync);
2222void f2fs_inode_synced(struct inode *inode);
2223int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2224int f2fs_sync_fs(struct super_block *sb, int sync);
Namjae Jeona07ef782012-12-30 14:52:05 +09002225extern __printf(3, 4)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002226void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
Shawn Lin984ec632016-02-17 11:26:32 +08002227int sanity_check_ckpt(struct f2fs_sb_info *sbi);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002228
2229/*
2230 * hash.c
2231 */
Jaegeuk Kim8daed212017-04-24 10:00:08 -07002232f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
2233 struct fscrypt_name *fname);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002234
2235/*
2236 * node.c
2237 */
2238struct dnode_of_data;
2239struct node_info;
2240
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002241bool available_free_memory(struct f2fs_sb_info *sbi, int type);
2242int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2243bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2244bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2245void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
2246pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2247int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2248int truncate_inode_blocks(struct inode *inode, pgoff_t from);
2249int truncate_xattr_node(struct inode *inode, struct page *page);
2250int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2251int remove_inode_page(struct inode *inode);
2252struct page *new_inode_page(struct inode *inode);
2253struct page *new_node_page(struct dnode_of_data *dn,
2254 unsigned int ofs, struct page *ipage);
2255void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2256struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2257struct page *get_node_page_ra(struct page *parent, int start);
2258void move_node_page(struct page *node_page, int gc_type);
2259int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2260 struct writeback_control *wbc, bool atomic);
2261int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc);
2262void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2263bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2264void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2265void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2266int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2267void recover_inline_xattr(struct inode *inode, struct page *page);
2268int recover_xattr_data(struct inode *inode, struct page *page,
2269 block_t blkaddr);
2270int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2271int restore_node_summary(struct f2fs_sb_info *sbi,
2272 unsigned int segno, struct f2fs_summary_block *sum);
2273void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2274int build_node_manager(struct f2fs_sb_info *sbi);
2275void destroy_node_manager(struct f2fs_sb_info *sbi);
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002276int __init create_node_manager_caches(void);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002277void destroy_node_manager_caches(void);
2278
2279/*
2280 * segment.c
2281 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002282void register_inmem_page(struct inode *inode, struct page *page);
2283void drop_inmem_pages(struct inode *inode);
2284void drop_inmem_page(struct inode *inode, struct page *page);
2285int commit_inmem_pages(struct inode *inode);
2286void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2287void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
2288int f2fs_issue_flush(struct f2fs_sb_info *sbi);
2289int create_flush_cmd_control(struct f2fs_sb_info *sbi);
2290void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2291void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2292bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
2293void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new);
2294void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
2295void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2296void release_discard_addrs(struct f2fs_sb_info *sbi);
2297int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2298void allocate_new_segments(struct f2fs_sb_info *sbi);
2299int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2300bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2301struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2302void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2303void write_meta_page(struct f2fs_sb_info *sbi, struct page *page);
2304void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2305void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
2306int rewrite_data_page(struct f2fs_io_info *fio);
2307void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2308 block_t old_blkaddr, block_t new_blkaddr,
2309 bool recover_curseg, bool recover_newaddr);
2310void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2311 block_t old_addr, block_t new_addr,
2312 unsigned char version, bool recover_curseg,
2313 bool recover_newaddr);
2314void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2315 block_t old_blkaddr, block_t *new_blkaddr,
Chao Yuc52dc0f2017-05-19 23:37:01 +08002316 struct f2fs_summary *sum, int type,
2317 struct f2fs_io_info *fio, bool add_list);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002318void f2fs_wait_on_page_writeback(struct page *page,
2319 enum page_type type, bool ordered);
2320void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
2321 block_t blkaddr);
2322void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2323void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2324int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2325 unsigned int val, int alloc);
2326void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2327int build_segment_manager(struct f2fs_sb_info *sbi);
2328void destroy_segment_manager(struct f2fs_sb_info *sbi);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002329int __init create_segment_manager_caches(void);
2330void destroy_segment_manager_caches(void);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002331
2332/*
2333 * checkpoint.c
2334 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002335void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
2336struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2337struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2338struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2339bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type);
2340int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2341 int type, bool sync);
2342void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2343long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2344 long nr_to_write);
2345void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2346void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2347void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2348bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
2349int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
2350int acquire_orphan_inode(struct f2fs_sb_info *sbi);
2351void release_orphan_inode(struct f2fs_sb_info *sbi);
2352void add_orphan_inode(struct inode *inode);
2353void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2354int recover_orphan_inodes(struct f2fs_sb_info *sbi);
2355int get_valid_checkpoint(struct f2fs_sb_info *sbi);
2356void update_dirty_page(struct inode *inode, struct page *page);
2357void remove_dirty_inode(struct inode *inode);
2358int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2359int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2360void init_ino_entry_info(struct f2fs_sb_info *sbi);
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002361int __init create_checkpoint_caches(void);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002362void destroy_checkpoint_caches(void);
2363
2364/*
2365 * data.c
2366 */
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002367void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2368void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002369 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002370 enum page_type type);
2371void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002372int f2fs_submit_page_bio(struct f2fs_io_info *fio);
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002373int f2fs_submit_page_write(struct f2fs_io_info *fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002374struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2375 block_t blk_addr, struct bio *bio);
2376int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
2377void set_data_blkaddr(struct dnode_of_data *dn);
2378void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
2379int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2380int reserve_new_block(struct dnode_of_data *dn);
2381int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2382int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2383int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
2384struct page *get_read_data_page(struct inode *inode, pgoff_t index,
2385 int op_flags, bool for_write);
2386struct page *find_data_page(struct inode *inode, pgoff_t index);
2387struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
2388 bool for_write);
2389struct page *get_new_data_page(struct inode *inode,
2390 struct page *ipage, pgoff_t index, bool new_i_size);
2391int do_write_data_page(struct f2fs_io_info *fio);
2392int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2393 int create, int flag);
2394int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2395 u64 start, u64 len);
2396void f2fs_set_page_dirty_nobuffers(struct page *page);
2397void f2fs_invalidate_page(struct page *page, unsigned int offset,
2398 unsigned int length);
2399int f2fs_release_page(struct page *page, gfp_t wait);
Weichao Guo5b7a4872016-09-20 05:03:27 +08002400#ifdef CONFIG_MIGRATION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002401int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2402 struct page *page, enum migrate_mode mode);
Weichao Guo5b7a4872016-09-20 05:03:27 +08002403#endif
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002404
2405/*
2406 * gc.c
2407 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002408int start_gc_thread(struct f2fs_sb_info *sbi);
2409void stop_gc_thread(struct f2fs_sb_info *sbi);
2410block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2411int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2412 unsigned int segno);
2413void build_gc_manager(struct f2fs_sb_info *sbi);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002414
2415/*
2416 * recovery.c
2417 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002418int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
2419bool space_for_roll_forward(struct f2fs_sb_info *sbi);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002420
2421/*
2422 * debug.c
2423 */
2424#ifdef CONFIG_F2FS_STAT_FS
2425struct f2fs_stat_info {
2426 struct list_head stat_list;
2427 struct f2fs_sb_info *sbi;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002428 int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
2429 int main_area_segs, main_area_sections, main_area_zones;
Chao Yu5b7ee372015-09-30 17:38:48 +08002430 unsigned long long hit_largest, hit_cached, hit_rbtree;
2431 unsigned long long hit_total, total_ext;
Jaegeuk Kimc00ba552015-12-31 15:24:14 -08002432 int ext_tree, zombie_tree, ext_node;
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07002433 int ndirty_node, ndirty_dent, ndirty_meta, ndirty_data, ndirty_imeta;
2434 int inmem_pages;
Jaegeuk Kim0f18b462016-05-20 11:10:10 -07002435 unsigned int ndirty_dirs, ndirty_files, ndirty_all;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002436 int nats, dirty_nats, sits, dirty_sits;
2437 int free_nids, avail_nids, alloc_nids;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002438 int total_count, utilization;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002439 int bg_gc, nr_wb_cp_data, nr_wb_data;
2440 int nr_flushing, nr_flushed, nr_discarding, nr_discarded;
2441 int nr_discard_cmd;
2442 unsigned int undiscard_blks;
2443 int inline_xattr, inline_inode, inline_dir, append, update, orphans;
2444 int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
Yunlei Hef83a2582016-08-18 21:01:18 +08002445 unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002446 unsigned int bimodal, avg_vblocks;
2447 int util_free, util_valid, util_invalid;
2448 int rsvd_segs, overp_segs;
2449 int dirty_count, node_pages, meta_pages;
Jaegeuk Kim42190d22016-01-09 13:45:17 -08002450 int prefree_count, call_count, cp_count, bg_cp_count;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002451 int tot_segs, node_segs, data_segs, free_segs, free_secs;
Changman Leee1235982014-12-23 08:37:39 +09002452 int bg_node_segs, bg_data_segs;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002453 int tot_blks, data_blks, node_blks;
Changman Leee1235982014-12-23 08:37:39 +09002454 int bg_data_blks, bg_node_blks;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002455 int curseg[NR_CURSEG_TYPE];
2456 int cursec[NR_CURSEG_TYPE];
2457 int curzone[NR_CURSEG_TYPE];
2458
2459 unsigned int segment_count[2];
2460 unsigned int block_count[2];
Changman Leeb9a2c252014-12-24 02:16:54 +09002461 unsigned int inplace_count;
Chao Yu9edcdab2015-09-11 14:43:52 +08002462 unsigned long long base_mem, cache_mem, page_mem;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002463};
2464
Gu Zheng963d4f72013-07-12 14:47:11 +08002465static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
2466{
Chris Fries6c311ec2014-01-17 14:44:39 -06002467 return (struct f2fs_stat_info *)sbi->stat_info;
Gu Zheng963d4f72013-07-12 14:47:11 +08002468}
2469
Changman Lee942e0be2014-02-13 15:12:29 +09002470#define stat_inc_cp_count(si) ((si)->cp_count++)
Jaegeuk Kim42190d22016-01-09 13:45:17 -08002471#define stat_inc_bg_cp_count(si) ((si)->bg_cp_count++)
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002472#define stat_inc_call_count(si) ((si)->call_count++)
2473#define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
Chao Yu33fbd512015-12-17 17:14:44 +08002474#define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++)
2475#define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--)
Chao Yu5b7ee372015-09-30 17:38:48 +08002476#define stat_inc_total_hit(sbi) (atomic64_inc(&(sbi)->total_hit_ext))
2477#define stat_inc_rbtree_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_rbtree))
2478#define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest))
2479#define stat_inc_cached_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_cached))
Chao Yud5e8f6c2015-07-15 17:28:53 +08002480#define stat_inc_inline_xattr(inode) \
2481 do { \
2482 if (f2fs_has_inline_xattr(inode)) \
2483 (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \
2484 } while (0)
2485#define stat_dec_inline_xattr(inode) \
2486 do { \
2487 if (f2fs_has_inline_xattr(inode)) \
2488 (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \
2489 } while (0)
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09002490#define stat_inc_inline_inode(inode) \
2491 do { \
2492 if (f2fs_has_inline_data(inode)) \
Chao Yu03e14d52014-12-08 19:08:20 +08002493 (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09002494 } while (0)
2495#define stat_dec_inline_inode(inode) \
2496 do { \
2497 if (f2fs_has_inline_data(inode)) \
Chao Yu03e14d52014-12-08 19:08:20 +08002498 (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09002499 } while (0)
Jaegeuk Kim3289c062014-10-13 20:00:16 -07002500#define stat_inc_inline_dir(inode) \
2501 do { \
2502 if (f2fs_has_inline_dentry(inode)) \
Chao Yu03e14d52014-12-08 19:08:20 +08002503 (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
Jaegeuk Kim3289c062014-10-13 20:00:16 -07002504 } while (0)
2505#define stat_dec_inline_dir(inode) \
2506 do { \
2507 if (f2fs_has_inline_dentry(inode)) \
Chao Yu03e14d52014-12-08 19:08:20 +08002508 (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
Jaegeuk Kim3289c062014-10-13 20:00:16 -07002509 } while (0)
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002510#define stat_inc_seg_type(sbi, curseg) \
2511 ((sbi)->segment_count[(curseg)->alloc_type]++)
2512#define stat_inc_block_count(sbi, curseg) \
2513 ((sbi)->block_count[(curseg)->alloc_type]++)
Changman Leeb9a2c252014-12-24 02:16:54 +09002514#define stat_inc_inplace_blocks(sbi) \
2515 (atomic_inc(&(sbi)->inplace_count))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002516#define stat_inc_atomic_write(inode) \
2517 (atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
2518#define stat_dec_atomic_write(inode) \
2519 (atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
2520#define stat_update_max_atomic_write(inode) \
2521 do { \
2522 int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt); \
2523 int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt); \
2524 if (cur > max) \
2525 atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur); \
2526 } while (0)
2527#define stat_inc_volatile_write(inode) \
2528 (atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
2529#define stat_dec_volatile_write(inode) \
2530 (atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
2531#define stat_update_max_volatile_write(inode) \
2532 do { \
2533 int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt); \
2534 int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt); \
2535 if (cur > max) \
2536 atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur); \
2537 } while (0)
Changman Leee1235982014-12-23 08:37:39 +09002538#define stat_inc_seg_count(sbi, type, gc_type) \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002539 do { \
Gu Zheng963d4f72013-07-12 14:47:11 +08002540 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002541 si->tot_segs++; \
2542 if ((type) == SUM_TYPE_DATA) { \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002543 si->data_segs++; \
Changman Leee1235982014-12-23 08:37:39 +09002544 si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
2545 } else { \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002546 si->node_segs++; \
Changman Leee1235982014-12-23 08:37:39 +09002547 si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
2548 } \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002549 } while (0)
2550
2551#define stat_inc_tot_blk_count(si, blks) \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002552 ((si)->tot_blks += (blks))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002553
Changman Leee1235982014-12-23 08:37:39 +09002554#define stat_inc_data_blk_count(sbi, blks, gc_type) \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002555 do { \
Gu Zheng963d4f72013-07-12 14:47:11 +08002556 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002557 stat_inc_tot_blk_count(si, blks); \
2558 si->data_blks += (blks); \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002559 si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002560 } while (0)
2561
Changman Leee1235982014-12-23 08:37:39 +09002562#define stat_inc_node_blk_count(sbi, blks, gc_type) \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002563 do { \
Gu Zheng963d4f72013-07-12 14:47:11 +08002564 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002565 stat_inc_tot_blk_count(si, blks); \
2566 si->node_blks += (blks); \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002567 si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002568 } while (0)
2569
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002570int f2fs_build_stats(struct f2fs_sb_info *sbi);
2571void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
Chao Yu787c7b8c2015-10-29 09:13:04 +08002572int __init f2fs_create_root_stats(void);
Namjae Jeon4589d252013-01-15 19:58:47 +09002573void f2fs_destroy_root_stats(void);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002574#else
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002575#define stat_inc_cp_count(si) do { } while (0)
2576#define stat_inc_bg_cp_count(si) do { } while (0)
2577#define stat_inc_call_count(si) do { } while (0)
2578#define stat_inc_bggc_count(si) do { } while (0)
2579#define stat_inc_dirty_inode(sbi, type) do { } while (0)
2580#define stat_dec_dirty_inode(sbi, type) do { } while (0)
2581#define stat_inc_total_hit(sb) do { } while (0)
2582#define stat_inc_rbtree_node_hit(sb) do { } while (0)
2583#define stat_inc_largest_node_hit(sbi) do { } while (0)
2584#define stat_inc_cached_node_hit(sbi) do { } while (0)
2585#define stat_inc_inline_xattr(inode) do { } while (0)
2586#define stat_dec_inline_xattr(inode) do { } while (0)
2587#define stat_inc_inline_inode(inode) do { } while (0)
2588#define stat_dec_inline_inode(inode) do { } while (0)
2589#define stat_inc_inline_dir(inode) do { } while (0)
2590#define stat_dec_inline_dir(inode) do { } while (0)
2591#define stat_inc_atomic_write(inode) do { } while (0)
2592#define stat_dec_atomic_write(inode) do { } while (0)
2593#define stat_update_max_atomic_write(inode) do { } while (0)
2594#define stat_inc_volatile_write(inode) do { } while (0)
2595#define stat_dec_volatile_write(inode) do { } while (0)
2596#define stat_update_max_volatile_write(inode) do { } while (0)
2597#define stat_inc_seg_type(sbi, curseg) do { } while (0)
2598#define stat_inc_block_count(sbi, curseg) do { } while (0)
2599#define stat_inc_inplace_blocks(sbi) do { } while (0)
2600#define stat_inc_seg_count(sbi, type, gc_type) do { } while (0)
2601#define stat_inc_tot_blk_count(si, blks) do { } while (0)
2602#define stat_inc_data_blk_count(sbi, blks, gc_type) do { } while (0)
2603#define stat_inc_node_blk_count(sbi, blks, gc_type) do { } while (0)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002604
2605static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
2606static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
Chao Yu787c7b8c2015-10-29 09:13:04 +08002607static inline int __init f2fs_create_root_stats(void) { return 0; }
Namjae Jeon4589d252013-01-15 19:58:47 +09002608static inline void f2fs_destroy_root_stats(void) { }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002609#endif
2610
2611extern const struct file_operations f2fs_dir_operations;
2612extern const struct file_operations f2fs_file_operations;
2613extern const struct inode_operations f2fs_file_inode_operations;
2614extern const struct address_space_operations f2fs_dblock_aops;
2615extern const struct address_space_operations f2fs_node_aops;
2616extern const struct address_space_operations f2fs_meta_aops;
2617extern const struct inode_operations f2fs_dir_inode_operations;
2618extern const struct inode_operations f2fs_symlink_inode_operations;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07002619extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002620extern const struct inode_operations f2fs_special_inode_operations;
Jaegeuk Kim29e70432015-02-10 16:23:12 -08002621extern struct kmem_cache *inode_entry_slab;
Huajun Li1001b342013-11-10 23:13:16 +08002622
Huajun Lie18c65b2013-11-10 23:13:19 +08002623/*
2624 * inline.c
2625 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002626bool f2fs_may_inline_data(struct inode *inode);
2627bool f2fs_may_inline_dentry(struct inode *inode);
2628void read_inline_data(struct page *page, struct page *ipage);
2629void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
2630int f2fs_read_inline_data(struct inode *inode, struct page *page);
2631int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
2632int f2fs_convert_inline_inode(struct inode *inode);
2633int f2fs_write_inline_data(struct inode *inode, struct page *page);
2634bool recover_inline_data(struct inode *inode, struct page *npage);
2635struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
2636 struct fscrypt_name *fname, struct page **res_page);
2637int make_empty_inline_dir(struct inode *inode, struct inode *parent,
2638 struct page *ipage);
2639int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
2640 const struct qstr *orig_name,
2641 struct inode *inode, nid_t ino, umode_t mode);
2642void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
2643 struct inode *dir, struct inode *inode);
2644bool f2fs_empty_inline_dir(struct inode *dir);
2645int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
2646 struct fscrypt_str *fstr);
2647int f2fs_inline_data_fiemap(struct inode *inode,
2648 struct fiemap_extent_info *fieinfo,
2649 __u64 start, __u64 len);
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002650
2651/*
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002652 * shrinker.c
2653 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002654unsigned long f2fs_shrink_count(struct shrinker *shrink,
2655 struct shrink_control *sc);
2656unsigned long f2fs_shrink_scan(struct shrinker *shrink,
2657 struct shrink_control *sc);
2658void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
2659void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002660
2661/*
Chao Yua28ef1f2015-07-08 17:59:36 +08002662 * extent_cache.c
2663 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002664struct rb_entry *__lookup_rb_tree(struct rb_root *root,
2665 struct rb_entry *cached_re, unsigned int ofs);
2666struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
2667 struct rb_root *root, struct rb_node **parent,
2668 unsigned int ofs);
2669struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
2670 struct rb_entry *cached_re, unsigned int ofs,
2671 struct rb_entry **prev_entry, struct rb_entry **next_entry,
2672 struct rb_node ***insert_p, struct rb_node **insert_parent,
2673 bool force);
2674bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
2675 struct rb_root *root);
2676unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
2677bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
2678void f2fs_drop_extent_tree(struct inode *inode);
2679unsigned int f2fs_destroy_extent_node(struct inode *inode);
2680void f2fs_destroy_extent_tree(struct inode *inode);
2681bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
2682 struct extent_info *ei);
2683void f2fs_update_extent_cache(struct dnode_of_data *dn);
Chao Yu19b2c302015-08-26 20:34:48 +08002684void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002685 pgoff_t fofs, block_t blkaddr, unsigned int len);
2686void init_extent_cache_info(struct f2fs_sb_info *sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +08002687int __init create_extent_cache(void);
2688void destroy_extent_cache(void);
2689
2690/*
Chao Yu57fb30c2017-06-14 17:39:47 +08002691 * sysfs.c
2692 */
2693int __init f2fs_register_sysfs(void);
2694void f2fs_unregister_sysfs(void);
2695int f2fs_init_sysfs(struct f2fs_sb_info *sbi);
2696void f2fs_exit_sysfs(struct f2fs_sb_info *sbi);
2697
2698/*
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002699 * crypto support
2700 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002701static inline bool f2fs_encrypted_inode(struct inode *inode)
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002702{
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002703 return file_is_encrypt(inode);
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002704}
2705
2706static inline void f2fs_set_encrypted_inode(struct inode *inode)
2707{
2708#ifdef CONFIG_F2FS_FS_ENCRYPTION
2709 file_set_encrypt(inode);
2710#endif
2711}
2712
2713static inline bool f2fs_bio_encrypted(struct bio *bio)
2714{
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002715 return bio->bi_private != NULL;
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002716}
2717
2718static inline int f2fs_sb_has_crypto(struct super_block *sb)
2719{
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002720 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002721}
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002722
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002723static inline int f2fs_sb_mounted_blkzoned(struct super_block *sb)
Jaegeuk Kim52763a42016-06-13 09:47:48 -07002724{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002725 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_BLKZONED);
2726}
2727
2728#ifdef CONFIG_BLK_DEV_ZONED
2729static inline int get_blkz_type(struct f2fs_sb_info *sbi,
2730 struct block_device *bdev, block_t blkaddr)
2731{
2732 unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
2733 int i;
2734
2735 for (i = 0; i < sbi->s_ndevs; i++)
2736 if (FDEV(i).bdev == bdev)
2737 return FDEV(i).blkz_type[zno];
2738 return -EINVAL;
2739}
2740#endif
2741
2742static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
2743{
2744 struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
2745
2746 return blk_queue_discard(q) || f2fs_sb_mounted_blkzoned(sbi->sb);
Jaegeuk Kim52763a42016-06-13 09:47:48 -07002747}
2748
2749static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
2750{
2751 clear_opt(sbi, ADAPTIVE);
2752 clear_opt(sbi, LFS);
2753
2754 switch (mt) {
2755 case F2FS_MOUNT_ADAPTIVE:
2756 set_opt(sbi, ADAPTIVE);
2757 break;
2758 case F2FS_MOUNT_LFS:
2759 set_opt(sbi, LFS);
2760 break;
2761 }
2762}
2763
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002764static inline bool f2fs_may_encrypt(struct inode *inode)
2765{
2766#ifdef CONFIG_F2FS_FS_ENCRYPTION
Al Viro886f56f2015-12-05 03:56:06 -05002767 umode_t mode = inode->i_mode;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002768
2769 return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
2770#else
2771 return 0;
2772#endif
2773}
2774
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002775#endif