blob: da1dda33d546427a5c00dc744ecff0f9219e1104 [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)
Jaegeuk Kim0bb53062017-06-15 16:44:42 -0700307#define F2FS_IOC_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11, \
308 struct f2fs_gc_range)
Namjae Jeone9750822013-02-04 23:41:41 +0900309
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700310#define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
311#define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
312#define F2FS_IOC_GET_ENCRYPTION_PWSALT FS_IOC_GET_ENCRYPTION_PWSALT
Jaegeuk Kimf424f662015-04-20 15:19:06 -0700313
Jaegeuk Kim1abff932015-01-08 19:15:53 -0800314/*
315 * should be same as XFS_IOC_GOINGDOWN.
316 * Flags for going down operation used by FS_IOC_GOINGDOWN
317 */
318#define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
319#define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
320#define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
321#define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
Jaegeuk Kimc912a822015-10-07 09:46:37 -0700322#define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */
Jaegeuk Kim1abff932015-01-08 19:15:53 -0800323
Namjae Jeone9750822013-02-04 23:41:41 +0900324#if defined(__KERNEL__) && defined(CONFIG_COMPAT)
325/*
326 * ioctl commands in 32 bit emulation
327 */
Chao Yu04ef4b62015-11-10 18:44:20 +0800328#define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
329#define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
330#define F2FS_IOC32_GETVERSION FS_IOC32_GETVERSION
Namjae Jeone9750822013-02-04 23:41:41 +0900331#endif
332
Jaegeuk Kim0bb53062017-06-15 16:44:42 -0700333struct f2fs_gc_range {
334 u32 sync;
335 u64 start;
336 u64 len;
337};
338
Chao Yud323d002015-10-27 09:53:45 +0800339struct f2fs_defragment {
340 u64 start;
341 u64 len;
342};
343
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -0700344struct f2fs_move_range {
345 u32 dst_fd; /* destination fd */
346 u64 pos_in; /* start position in src_fd */
347 u64 pos_out; /* start position in dst_fd */
348 u64 len; /* size to move */
349};
350
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700351struct f2fs_flush_device {
352 u32 dev_num; /* device number to flush */
353 u32 segments; /* # of segments to flush */
354};
355
Namjae Jeone9750822013-02-04 23:41:41 +0900356/*
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900357 * For INODE and NODE manager
358 */
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700359/* for directory operations */
360struct f2fs_dentry_ptr {
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700361 struct inode *inode;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700362 const void *bitmap;
363 struct f2fs_dir_entry *dentry;
364 __u8 (*filename)[F2FS_SLOT_LEN];
365 int max;
366};
367
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700368static inline void make_dentry_ptr_block(struct inode *inode,
369 struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700370{
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700371 d->inode = inode;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700372 d->max = NR_DENTRY_IN_BLOCK;
373 d->bitmap = &t->dentry_bitmap;
374 d->dentry = t->dentry;
375 d->filename = t->filename;
376}
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700377
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700378static inline void make_dentry_ptr_inline(struct inode *inode,
379 struct f2fs_dentry_ptr *d, struct f2fs_inline_dentry *t)
380{
381 d->inode = inode;
382 d->max = NR_INLINE_DENTRY;
383 d->bitmap = &t->dentry_bitmap;
384 d->dentry = t->dentry;
385 d->filename = t->filename;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700386}
387
Jaegeuk Kimdbe6a5f2013-08-09 08:14:06 +0900388/*
389 * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
390 * as its node offset to distinguish from index node blocks.
391 * But some bits are used to mark the node block.
392 */
393#define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
394 >> OFFSET_BIT_SHIFT)
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900395enum {
396 ALLOC_NODE, /* allocate a new node page if needed */
397 LOOKUP_NODE, /* look up a node without readahead */
398 LOOKUP_NODE_RA, /*
399 * look up a node with readahead called
Chao Yu4f4124d2013-12-21 18:02:14 +0800400 * by get_data_block.
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900401 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900402};
403
Jaegeuk Kima6db67f2015-08-10 15:01:12 -0700404#define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900405
Chao Yu817202d92014-04-28 17:59:43 +0800406#define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
407
Chao Yu13054c52015-02-05 17:52:58 +0800408/* vector size for gang look-up from extent cache that consists of radix tree */
409#define EXT_TREE_VEC_SIZE 64
410
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900411/* for in-memory extent cache entry */
Chao Yu13054c52015-02-05 17:52:58 +0800412#define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */
413
414/* number of extent info in extent cache we try to shrink */
415#define EXTENT_CACHE_SHRINK_NUMBER 128
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900416
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700417struct rb_entry {
418 struct rb_node rb_node; /* rb node located in rb-tree */
419 unsigned int ofs; /* start offset of the entry */
420 unsigned int len; /* length of the entry */
421};
422
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900423struct extent_info {
Chao Yu13054c52015-02-05 17:52:58 +0800424 unsigned int fofs; /* start offset in a file */
Chao Yu13054c52015-02-05 17:52:58 +0800425 unsigned int len; /* length of the extent */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700426 u32 blk; /* start block address of the extent */
Chao Yu13054c52015-02-05 17:52:58 +0800427};
428
429struct extent_node {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700430 struct rb_node rb_node;
431 union {
432 struct {
433 unsigned int fofs;
434 unsigned int len;
435 u32 blk;
436 };
437 struct extent_info ei; /* extent info */
438
439 };
Chao Yu13054c52015-02-05 17:52:58 +0800440 struct list_head list; /* node in global extent list of sbi */
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000441 struct extent_tree *et; /* extent tree pointer */
Chao Yu13054c52015-02-05 17:52:58 +0800442};
443
444struct extent_tree {
445 nid_t ino; /* inode number */
446 struct rb_root root; /* root of extent info rb-tree */
Chao Yu62c8af62015-02-05 18:01:39 +0800447 struct extent_node *cached_en; /* recently accessed extent node */
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700448 struct extent_info largest; /* largested extent info */
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800449 struct list_head list; /* to be used by sbi->zombie_list */
Chao Yu13054c52015-02-05 17:52:58 +0800450 rwlock_t lock; /* protect extent info rb-tree */
Chao Yu68e35382016-01-08 20:22:52 +0800451 atomic_t node_cnt; /* # of extent node in rb-tree*/
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900452};
453
454/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700455 * This structure is taken from ext4_map_blocks.
456 *
457 * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
458 */
459#define F2FS_MAP_NEW (1 << BH_New)
460#define F2FS_MAP_MAPPED (1 << BH_Mapped)
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700461#define F2FS_MAP_UNWRITTEN (1 << BH_Unwritten)
462#define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
463 F2FS_MAP_UNWRITTEN)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700464
465struct f2fs_map_blocks {
466 block_t m_pblk;
467 block_t m_lblk;
468 unsigned int m_len;
469 unsigned int m_flags;
Chao Yuda859852016-01-26 15:42:58 +0800470 pgoff_t *m_next_pgofs; /* point next possible non-hole pgofs */
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700471};
472
Chao Yue2b4e2b2015-08-19 19:11:19 +0800473/* for flag in get_data_block */
474#define F2FS_GET_BLOCK_READ 0
475#define F2FS_GET_BLOCK_DIO 1
476#define F2FS_GET_BLOCK_FIEMAP 2
477#define F2FS_GET_BLOCK_BMAP 3
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800478#define F2FS_GET_BLOCK_PRE_DIO 4
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800479#define F2FS_GET_BLOCK_PRE_AIO 5
Chao Yue2b4e2b2015-08-19 19:11:19 +0800480
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700481/*
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900482 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
483 */
484#define FADVISE_COLD_BIT 0x01
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900485#define FADVISE_LOST_PINO_BIT 0x02
Jaegeuk Kimcde4de12015-04-20 13:57:51 -0700486#define FADVISE_ENCRYPT_BIT 0x04
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700487#define FADVISE_ENC_NAME_BIT 0x08
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700488#define FADVISE_KEEP_SIZE_BIT 0x10
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900489
Jaegeuk Kimb5492af72015-04-20 13:44:41 -0700490#define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
491#define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
492#define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
493#define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
494#define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
495#define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
Jaegeuk Kimcde4de12015-04-20 13:57:51 -0700496#define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT)
497#define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
498#define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700499#define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT)
500#define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700501#define file_keep_isize(inode) is_file(inode, FADVISE_KEEP_SIZE_BIT)
502#define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
Jaegeuk Kimcde4de12015-04-20 13:57:51 -0700503
Jaegeuk Kimab9fa662014-02-27 20:09:05 +0900504#define DEF_DIR_LEVEL 0
505
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900506struct f2fs_inode_info {
507 struct inode vfs_inode; /* serve a vfs inode */
508 unsigned long i_flags; /* keep an inode flags for ioctl */
509 unsigned char i_advise; /* use to give file attribute hints */
Jaegeuk Kim38431542014-02-27 18:20:00 +0900510 unsigned char i_dir_level; /* use for dentry level for large dir */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900511 unsigned int i_current_depth; /* use only in directory structure */
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900512 unsigned int i_pino; /* parent inode number */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900513 umode_t i_acl_mode; /* keep file acl mode temporarily */
514
515 /* Use below internally in f2fs*/
516 unsigned long flags; /* use to pass per-file flags */
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900517 struct rw_semaphore i_sem; /* protect fi info */
Jaegeuk Kima43e1c42016-12-02 15:11:32 -0800518 atomic_t dirty_pages; /* # of dirty pages */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900519 f2fs_hash_t chash; /* hash value of given file name */
520 unsigned int clevel; /* maximum level of given file name */
Jaegeuk Kimd00d1b72017-02-14 09:54:37 -0800521 struct task_struct *task; /* lookup and create consistency */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900522 nid_t i_xattr_nid; /* node id that contains xattrs */
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700523 loff_t last_disk_size; /* lastly written file size */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700524
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700525 struct list_head dirty_list; /* dirty list for dirs and files */
526 struct list_head gdirty_list; /* linked in global dirty list */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700527 struct list_head inmem_pages; /* inmemory pages managed by f2fs */
528 struct mutex inmem_lock; /* lock for inmemory pages */
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700529 struct extent_tree *extent_tree; /* cached extent_tree entry */
Chao Yu82e0a5a2016-07-13 09:18:29 +0800530 struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
Qiuyang Sun7b23ea12017-05-18 11:06:45 +0800531 struct rw_semaphore i_mmap_sem;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900532};
533
534static inline void get_extent_info(struct extent_info *ext,
Chao Yubd933d42016-05-04 23:19:47 +0800535 struct f2fs_extent *i_ext)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900536{
Chao Yubd933d42016-05-04 23:19:47 +0800537 ext->fofs = le32_to_cpu(i_ext->fofs);
538 ext->blk = le32_to_cpu(i_ext->blk);
539 ext->len = le32_to_cpu(i_ext->len);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900540}
541
542static inline void set_raw_extent(struct extent_info *ext,
543 struct f2fs_extent *i_ext)
544{
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900545 i_ext->fofs = cpu_to_le32(ext->fofs);
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800546 i_ext->blk = cpu_to_le32(ext->blk);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900547 i_ext->len = cpu_to_le32(ext->len);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900548}
549
Chao Yu429511c2015-02-05 17:54:31 +0800550static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
551 u32 blk, unsigned int len)
552{
553 ei->fofs = fofs;
554 ei->blk = blk;
555 ei->len = len;
556}
557
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700558static inline bool __is_discard_mergeable(struct discard_info *back,
559 struct discard_info *front)
Chao Yu0bdee482015-03-19 19:27:51 +0800560{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700561 return back->lstart + back->len == front->lstart;
562}
563
564static inline bool __is_discard_back_mergeable(struct discard_info *cur,
565 struct discard_info *back)
566{
567 return __is_discard_mergeable(back, cur);
568}
569
570static inline bool __is_discard_front_mergeable(struct discard_info *cur,
571 struct discard_info *front)
572{
573 return __is_discard_mergeable(cur, front);
Chao Yu0bdee482015-03-19 19:27:51 +0800574}
575
Chao Yu429511c2015-02-05 17:54:31 +0800576static inline bool __is_extent_mergeable(struct extent_info *back,
577 struct extent_info *front)
578{
579 return (back->fofs + back->len == front->fofs &&
580 back->blk + back->len == front->blk);
581}
582
583static inline bool __is_back_mergeable(struct extent_info *cur,
584 struct extent_info *back)
585{
586 return __is_extent_mergeable(back, cur);
587}
588
589static inline bool __is_front_mergeable(struct extent_info *cur,
590 struct extent_info *front)
591{
592 return __is_extent_mergeable(cur, front);
593}
594
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700595extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700596static inline void __try_update_largest_extent(struct inode *inode,
597 struct extent_tree *et, struct extent_node *en)
Chao Yu4abd3f52015-09-22 21:07:47 +0800598{
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700599 if (en->ei.len > et->largest.len) {
Chao Yu4abd3f52015-09-22 21:07:47 +0800600 et->largest = en->ei;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700601 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700602 }
Chao Yu4abd3f52015-09-22 21:07:47 +0800603}
604
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700605enum nid_list {
606 FREE_NID_LIST,
607 ALLOC_NID_LIST,
608 MAX_NID_LIST,
609};
610
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900611struct f2fs_nm_info {
612 block_t nat_blkaddr; /* base disk address of NAT */
613 nid_t max_nid; /* maximum possible node ids */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700614 nid_t available_nids; /* # of available node ids */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900615 nid_t next_scan_nid; /* the next nid to be scanned */
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +0900616 unsigned int ram_thresh; /* control the memory footprint */
Chao Yuea1a29a02015-10-12 17:08:48 +0800617 unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */
Chao Yu2304cb02016-01-18 18:32:58 +0800618 unsigned int dirty_nats_ratio; /* control dirty nats ratio threshold */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900619
620 /* NAT cache management */
621 struct radix_tree_root nat_root;/* root of the nat entry cache */
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700622 struct radix_tree_root nat_set_root;/* root of the nat set cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700623 struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900624 struct list_head nat_entries; /* cached nat entry list (clean) */
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700625 unsigned int nat_cnt; /* the # of cached nat entries */
Chao Yuaec71382014-06-24 09:18:20 +0800626 unsigned int dirty_nat_cnt; /* total num of nat entries in set */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700627 unsigned int nat_blocks; /* # of nat blocks */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900628
629 /* free node ids management */
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +0900630 struct radix_tree_root free_nid_root;/* root of the free_nid cache */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700631 struct list_head nid_list[MAX_NID_LIST];/* lists for free nids */
632 unsigned int nid_cnt[MAX_NID_LIST]; /* the number of free node id */
633 spinlock_t nid_list_lock; /* protect nid lists ops */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900634 struct mutex build_lock; /* lock for build free nids */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700635 unsigned char (*free_nid_bitmap)[NAT_ENTRY_BITMAP_SIZE];
636 unsigned char *nat_block_bitmap;
637 unsigned short *free_nid_count; /* free nid count of NAT block */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900638
639 /* for checkpoint */
640 char *nat_bitmap; /* NAT bitmap pointer */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700641
642 unsigned int nat_bits_blocks; /* # of nat bits blocks */
643 unsigned char *nat_bits; /* NAT bits blocks */
644 unsigned char *full_nat_bits; /* full NAT pages */
645 unsigned char *empty_nat_bits; /* empty NAT pages */
646#ifdef CONFIG_F2FS_CHECK_FS
647 char *nat_bitmap_mir; /* NAT bitmap mirror */
648#endif
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900649 int bitmap_size; /* bitmap size */
650};
651
652/*
653 * this structure is used as one of function parameters.
654 * all the information are dedicated to a given direct node block determined
655 * by the data offset in a file.
656 */
657struct dnode_of_data {
658 struct inode *inode; /* vfs inode pointer */
659 struct page *inode_page; /* its inode page, NULL is possible */
660 struct page *node_page; /* cached direct node page */
661 nid_t nid; /* node id of the direct node block */
662 unsigned int ofs_in_node; /* data offset in the node page */
663 bool inode_page_locked; /* inode page is locked or not */
Jaegeuk Kim93bae092015-12-22 12:59:54 -0800664 bool node_changed; /* is node block changed */
Chao Yu3cf45742016-01-26 15:40:44 +0800665 char cur_level; /* level of hole node page */
666 char max_level; /* level of current page located */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900667 block_t data_blkaddr; /* block address of the node block */
668};
669
670static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
671 struct page *ipage, struct page *npage, nid_t nid)
672{
Jaegeuk Kimd66d1f72013-01-03 08:57:21 +0900673 memset(dn, 0, sizeof(*dn));
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900674 dn->inode = inode;
675 dn->inode_page = ipage;
676 dn->node_page = npage;
677 dn->nid = nid;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900678}
679
680/*
681 * For SIT manager
682 *
683 * By default, there are 6 active log areas across the whole main area.
684 * When considering hot and cold data separation to reduce cleaning overhead,
685 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
686 * respectively.
687 * In the current design, you should not change the numbers intentionally.
688 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
689 * logs individually according to the underlying devices. (default: 6)
690 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
691 * data and 8 for node logs.
692 */
693#define NR_CURSEG_DATA_TYPE (3)
694#define NR_CURSEG_NODE_TYPE (3)
695#define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
696
697enum {
698 CURSEG_HOT_DATA = 0, /* directory entry blocks */
699 CURSEG_WARM_DATA, /* data blocks */
700 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
701 CURSEG_HOT_NODE, /* direct node blocks of directory files */
702 CURSEG_WARM_NODE, /* direct node blocks of normal files */
703 CURSEG_COLD_NODE, /* indirect node blocks */
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800704 NO_CHECK_TYPE,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900705};
706
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900707struct flush_cmd {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900708 struct completion wait;
Gu Zheng721bd4d2014-09-05 18:31:00 +0800709 struct llist_node llnode;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900710 int ret;
711};
712
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800713struct flush_cmd_control {
714 struct task_struct *f2fs_issue_flush; /* flush thread */
715 wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700716 atomic_t issued_flush; /* # of issued flushes */
717 atomic_t issing_flush; /* # of issing flushes */
Gu Zheng721bd4d2014-09-05 18:31:00 +0800718 struct llist_head issue_list; /* list for command issue */
719 struct llist_node *dispatch_list; /* list for command dispatch */
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800720};
721
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900722struct f2fs_sm_info {
723 struct sit_info *sit_info; /* whole segment information */
724 struct free_segmap_info *free_info; /* free segment information */
725 struct dirty_seglist_info *dirty_info; /* dirty segment information */
726 struct curseg_info *curseg_array; /* active segment information */
727
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900728 block_t seg0_blkaddr; /* block address of 0'th segment */
729 block_t main_blkaddr; /* start block address of main area */
730 block_t ssa_blkaddr; /* start block address of SSA area */
731
732 unsigned int segment_count; /* total # of segments */
733 unsigned int main_segments; /* # of segments in main area */
734 unsigned int reserved_segments; /* # of reserved segments */
735 unsigned int ovp_segments; /* # of overprovision segments */
Jaegeuk Kim81eb8d62013-10-24 13:31:34 +0900736
737 /* a threshold to reclaim prefree segments */
738 unsigned int rec_prefree_segments;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +0900739
Jaegeuk Kimbba681c2015-01-26 17:41:23 -0800740 /* for batched trimming */
741 unsigned int trim_sections; /* # of sections to trim */
742
Chao Yu184a5cd2014-09-04 18:13:01 +0800743 struct list_head sit_entry_set; /* sit entry set list */
744
Jaegeuk Kim216fbd62013-11-07 13:13:42 +0900745 unsigned int ipu_policy; /* in-place-update policy */
746 unsigned int min_ipu_util; /* in-place-update threshold */
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700747 unsigned int min_fsync_blocks; /* threshold for fsync */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700748 unsigned int min_hot_blocks; /* threshold for hot block allocation */
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900749
750 /* for flush command control */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700751 struct flush_cmd_control *fcc_info;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800752
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700753 /* for discard command control */
754 struct discard_cmd_control *dcc_info;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900755};
756
757/*
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900758 * For superblock
759 */
760/*
761 * COUNT_TYPE for monitoring
762 *
763 * f2fs monitors the number of several block types such as on-writeback,
764 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
765 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700766#define WB_DATA_TYPE(p) (__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900767enum count_type {
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900768 F2FS_DIRTY_DENTS,
Chao Yuc227f912015-12-16 13:09:20 +0800769 F2FS_DIRTY_DATA,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900770 F2FS_DIRTY_NODES,
771 F2FS_DIRTY_META,
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800772 F2FS_INMEM_PAGES,
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700773 F2FS_DIRTY_IMETA,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700774 F2FS_WB_CP_DATA,
775 F2FS_WB_DATA,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900776 NR_COUNT_TYPE,
777};
778
779/*
arter97e1c42042014-08-06 23:22:50 +0900780 * The below are the page types of bios used in submit_bio().
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900781 * The available types are:
782 * DATA User data pages. It operates as async mode.
783 * NODE Node pages. It operates as async mode.
784 * META FS metadata pages such as SIT, NAT, CP.
785 * NR_PAGE_TYPE The number of page types.
786 * META_FLUSH Make sure the previous pages are written
787 * with waiting the bio's completion
788 * ... Only can be used with META.
789 */
Jaegeuk Kim7d5e5102013-11-18 17:13:35 +0900790#define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900791enum page_type {
792 DATA,
793 NODE,
794 META,
795 NR_PAGE_TYPE,
796 META_FLUSH,
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700797 INMEM, /* the below types are used by tracepoints only. */
798 INMEM_DROP,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700799 INMEM_INVALIDATE,
Chao Yu28bc1062016-02-06 14:40:34 +0800800 INMEM_REVOKE,
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700801 IPU,
802 OPU,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900803};
804
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700805enum temp_type {
806 HOT = 0, /* must be zero for meta bio */
807 WARM,
808 COLD,
809 NR_TEMP_TYPE,
810};
811
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -0700812enum need_lock_type {
813 LOCK_REQ = 0,
814 LOCK_DONE,
815 LOCK_RETRY,
816};
817
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900818struct f2fs_io_info {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700819 struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
Gu Zheng7e8f2302013-12-20 18:17:49 +0800820 enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700821 enum temp_type temp; /* contains HOT/WARM/COLD */
Mike Christie04d328d2016-06-05 14:31:55 -0500822 int op; /* contains REQ_OP_ */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700823 int op_flags; /* req_flag_bits */
Chao Yu7a9d7542016-02-22 18:36:38 +0800824 block_t new_blkaddr; /* new block address to be written */
Chao Yu28bc1062016-02-06 14:40:34 +0800825 block_t old_blkaddr; /* old block address before Cow */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700826 struct page *page; /* page to be written */
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700827 struct page *encrypted_page; /* encrypted page */
Chao Yuc52dc0f2017-05-19 23:37:01 +0800828 struct list_head list; /* serialize IOs */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700829 bool submitted; /* indicate IO submission */
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -0700830 int need_lock; /* indicate we need to lock cp_rwsem */
Chao Yuc52dc0f2017-05-19 23:37:01 +0800831 bool in_list; /* indicate fio is in io_list */
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900832};
833
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700834#define is_read_io(rw) ((rw) == READ)
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900835struct f2fs_bio_info {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900836 struct f2fs_sb_info *sbi; /* f2fs superblock */
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900837 struct bio *bio; /* bios to merge */
838 sector_t last_block_in_bio; /* last block number */
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900839 struct f2fs_io_info fio; /* store buffered io info. */
Chao Yudf0f8dc2014-03-22 14:57:23 +0800840 struct rw_semaphore io_rwsem; /* blocking op for bio */
Chao Yuc52dc0f2017-05-19 23:37:01 +0800841 spinlock_t io_lock; /* serialize DATA/NODE IOs */
842 struct list_head io_list; /* track fios */
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900843};
844
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700845#define FDEV(i) (sbi->devs[i])
846#define RDEV(i) (raw_super->devs[i])
847struct f2fs_dev_info {
848 struct block_device *bdev;
849 char path[MAX_PATH_LEN];
850 unsigned int total_segments;
851 block_t start_blk;
852 block_t end_blk;
853#ifdef CONFIG_BLK_DEV_ZONED
854 unsigned int nr_blkz; /* Total number of zones */
855 u8 *blkz_type; /* Array of zones type */
856#endif
857};
858
Chao Yuc227f912015-12-16 13:09:20 +0800859enum inode_type {
860 DIR_INODE, /* for dirty dir inode */
861 FILE_INODE, /* for dirty regular/symlink inode */
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700862 DIRTY_META, /* for all dirtied inode metadata */
Chao Yuc227f912015-12-16 13:09:20 +0800863 NR_INODE_TYPE,
864};
865
Chao Yu67298802014-11-18 11:18:36 +0800866/* for inner inode cache management */
867struct inode_management {
868 struct radix_tree_root ino_root; /* ino entry array */
869 spinlock_t ino_lock; /* for ino entry lock */
870 struct list_head ino_list; /* inode list head */
871 unsigned long ino_num; /* number of entries */
872};
873
Chao Yucaf00472015-01-28 17:48:42 +0800874/* For s_flag in struct f2fs_sb_info */
875enum {
876 SBI_IS_DIRTY, /* dirty flag for checkpoint */
877 SBI_IS_CLOSE, /* specify unmounting */
878 SBI_NEED_FSCK, /* need fsck.f2fs to fix */
879 SBI_POR_DOING, /* recovery is doing or not */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -0700880 SBI_NEED_SB_WRITE, /* need to recover superblock */
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -0700881 SBI_NEED_CP, /* need to checkpoint */
Chao Yucaf00472015-01-28 17:48:42 +0800882};
883
Jaegeuk Kim6beceb52016-01-08 15:51:50 -0800884enum {
885 CP_TIME,
Jaegeuk Kimd0239e12016-01-08 16:57:48 -0800886 REQ_TIME,
Jaegeuk Kim6beceb52016-01-08 15:51:50 -0800887 MAX_TIME,
888};
889
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900890struct f2fs_sb_info {
891 struct super_block *sb; /* pointer to VFS super block */
Jaegeuk Kim5e176d52013-06-28 12:47:01 +0900892 struct proc_dir_entry *s_proc; /* proc entry */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900893 struct f2fs_super_block *raw_super; /* raw super block pointer */
Chao Yue8240f62015-12-15 17:19:26 +0800894 int valid_super_block; /* valid super block no */
Chao Yufadb2fb2016-09-20 10:29:47 +0800895 unsigned long s_flag; /* flags for sbi */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900896
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700897#ifdef CONFIG_BLK_DEV_ZONED
898 unsigned int blocks_per_blkz; /* F2FS blocks per zone */
899 unsigned int log_blocks_per_blkz; /* log2 F2FS blocks per zone */
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700900#endif
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700901
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900902 /* for node-related operations */
903 struct f2fs_nm_info *nm_info; /* node manager */
904 struct inode *node_inode; /* cache node blocks */
905
906 /* for segment-related operations */
907 struct f2fs_sm_info *sm_info; /* segment manager */
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +0900908
909 /* for bio operations */
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700910 struct f2fs_bio_info *write_io[NR_PAGE_TYPE]; /* for write bios */
Chao Yu06504682017-05-19 23:37:00 +0800911 struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
912 /* bio ordering for NODE/DATA */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700913 int write_io_size_bits; /* Write IO size bits */
914 mempool_t *write_io_dummy; /* Dummy pages */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900915
916 /* for checkpoint */
917 struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
Jaegeuk Kim01e15b32016-11-24 12:45:15 -0800918 int cur_cp_pack; /* remain current cp pack */
Chao Yuaaec2b12016-09-20 11:04:18 +0800919 spinlock_t cp_lock; /* for flag in ckpt */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900920 struct inode *meta_inode; /* cache meta blocks */
Jaegeuk Kim39936832012-11-22 16:21:29 +0900921 struct mutex cp_mutex; /* checkpoint procedure lock */
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700922 struct rw_semaphore cp_rwsem; /* blocking FS operations */
Chao Yub3582c62014-07-03 18:58:39 +0800923 struct rw_semaphore node_write; /* locking node writes */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700924 struct rw_semaphore node_change; /* locking node change */
Changman Leefb51b5e2013-11-07 12:48:25 +0900925 wait_queue_head_t cp_wait;
Jaegeuk Kim6beceb52016-01-08 15:51:50 -0800926 unsigned long last_time[MAX_TIME]; /* to store time in jiffies */
927 long interval_time[MAX_TIME]; /* to store thresholds */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900928
Chao Yu67298802014-11-18 11:18:36 +0800929 struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
Jaegeuk Kim6451e042014-07-25 15:47:17 -0700930
931 /* for orphan inode, use 0'th array */
Gu Zheng0d47c1a2013-12-26 18:24:19 +0800932 unsigned int max_orphans; /* max orphan inodes */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900933
Chao Yuc227f912015-12-16 13:09:20 +0800934 /* for inode management */
935 struct list_head inode_list[NR_INODE_TYPE]; /* dirty inode list */
936 spinlock_t inode_lock[NR_INODE_TYPE]; /* for dirty inode list lock */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900937
Chao Yu13054c52015-02-05 17:52:58 +0800938 /* for extent tree cache */
939 struct radix_tree_root extent_tree_root;/* cache extent cache entries */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700940 struct mutex extent_tree_lock; /* locking extent radix tree */
Chao Yu13054c52015-02-05 17:52:58 +0800941 struct list_head extent_list; /* lru list for shrinker */
942 spinlock_t extent_lock; /* locking extent lru list */
Jaegeuk Kim7441cce2015-12-21 19:20:15 -0800943 atomic_t total_ext_tree; /* extent tree count */
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800944 struct list_head zombie_list; /* extent zombie tree list */
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -0800945 atomic_t total_zombie_tree; /* extent zombie tree count */
Chao Yu13054c52015-02-05 17:52:58 +0800946 atomic_t total_ext_node; /* extent info count */
947
arter97e1c42042014-08-06 23:22:50 +0900948 /* basic filesystem units */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900949 unsigned int log_sectors_per_block; /* log2 sectors per block */
950 unsigned int log_blocksize; /* log2 block size */
951 unsigned int blocksize; /* block size */
952 unsigned int root_ino_num; /* root inode number*/
953 unsigned int node_ino_num; /* node inode number*/
954 unsigned int meta_ino_num; /* meta inode number*/
955 unsigned int log_blocks_per_seg; /* log2 blocks per segment */
956 unsigned int blocks_per_seg; /* blocks per segment */
957 unsigned int segs_per_sec; /* segments per section */
958 unsigned int secs_per_zone; /* sections per zone */
959 unsigned int total_sections; /* total section count */
960 unsigned int total_node_count; /* total node block count */
961 unsigned int total_valid_node_count; /* valid node block count */
Chao Yue0afc4d2015-12-31 14:35:37 +0800962 loff_t max_file_blocks; /* max block index of file */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900963 int active_logs; /* # of active logs */
Jaegeuk Kimab9fa662014-02-27 20:09:05 +0900964 int dir_level; /* directory level */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900965
966 block_t user_block_count; /* # of user blocks */
967 block_t total_valid_block_count; /* # of valid blocks */
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700968 block_t discard_blks; /* discard command candidats */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900969 block_t last_valid_block_count; /* for recovery */
Chao Yu026bd9d2017-06-26 16:24:41 +0800970 block_t reserved_blocks; /* configurable reserved blocks */
971
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900972 u32 s_next_generation; /* for NFS support */
Jaegeuk Kim523be8a2016-05-13 12:36:58 -0700973
974 /* # of pages, see count_type */
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -0700975 atomic_t nr_pages[NR_COUNT_TYPE];
Jaegeuk Kim41382ec2016-05-16 11:06:50 -0700976 /* # of allocated blocks */
977 struct percpu_counter alloc_valid_block_count;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900978
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700979 /* writeback control */
980 atomic_t wb_sync_req; /* count # of WB_SYNC threads */
981
Jaegeuk Kim513c5f32016-05-16 11:42:32 -0700982 /* valid inode count */
983 struct percpu_counter total_valid_inode_count;
984
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900985 struct f2fs_mount_info mount_opt; /* mount options */
986
987 /* for cleaning operations */
988 struct mutex gc_mutex; /* mutex for GC */
989 struct f2fs_gc_kthread *gc_thread; /* GC thread */
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900990 unsigned int cur_victim_sec; /* current victim section num */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900991
Hou Pengyang4992ba22017-02-16 12:34:31 +0000992 /* threshold for converting bg victims for fg */
993 u64 fggc_threshold;
994
Jaegeuk Kimb1c57c12014-01-08 13:45:08 +0900995 /* maximum # of trials to find a victim segment for SSR and GC */
996 unsigned int max_victim_search;
997
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900998 /*
999 * for stat information.
1000 * one is for the LFS mode, and the other is for the SSR mode.
1001 */
Namjae Jeon35b09d82013-05-23 22:57:53 +09001002#ifdef CONFIG_F2FS_STAT_FS
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001003 struct f2fs_stat_info *stat_info; /* FS status information */
1004 unsigned int segment_count[2]; /* # of allocated segments */
1005 unsigned int block_count[2]; /* # of allocated blocks */
Changman Leeb9a2c252014-12-24 02:16:54 +09001006 atomic_t inplace_count; /* # of inplace update */
Chao Yu5b7ee372015-09-30 17:38:48 +08001007 atomic64_t total_hit_ext; /* # of lookup extent cache */
1008 atomic64_t read_hit_rbtree; /* # of hit rbtree extent node */
1009 atomic64_t read_hit_largest; /* # of hit largest extent node */
1010 atomic64_t read_hit_cached; /* # of hit cached extent node */
Chao Yud5e8f6c2015-07-15 17:28:53 +08001011 atomic_t inline_xattr; /* # of inline_xattr inodes */
Chao Yu03e14d52014-12-08 19:08:20 +08001012 atomic_t inline_inode; /* # of inline_data inodes */
1013 atomic_t inline_dir; /* # of inline_dentry inodes */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001014 atomic_t aw_cnt; /* # of atomic writes */
1015 atomic_t vw_cnt; /* # of volatile writes */
1016 atomic_t max_aw_cnt; /* max # of atomic writes */
1017 atomic_t max_vw_cnt; /* max # of volatile writes */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001018 int bg_gc; /* background gc calls */
Chao Yu33fbd512015-12-17 17:14:44 +08001019 unsigned int ndirty_inode[NR_INODE_TYPE]; /* # of dirty inodes */
Namjae Jeon35b09d82013-05-23 22:57:53 +09001020#endif
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001021 spinlock_t stat_lock; /* lock for stat operations */
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09001022
1023 /* For sysfs suppport */
1024 struct kobject s_kobj;
1025 struct completion s_kobj_unregister;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001026
1027 /* For shrinker support */
1028 struct list_head s_list;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001029 int s_ndevs; /* number of devices */
1030 struct f2fs_dev_info *devs; /* for device list */
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001031 struct mutex umount_mutex;
1032 unsigned int shrinker_run_no;
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001033
1034 /* For write statistics */
1035 u64 sectors_written_start;
1036 u64 kbytes_written;
Keith Mok43b65732016-03-02 12:04:24 -08001037
1038 /* Reference to checksum algorithm driver via cryptoapi */
1039 struct crypto_shash *s_chksum_driver;
Chao Yu1ecc0c52016-09-23 21:30:09 +08001040
1041 /* For fault injection */
1042#ifdef CONFIG_F2FS_FAULT_INJECTION
1043 struct f2fs_fault_info fault_info;
1044#endif
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001045};
1046
Chao Yu1ecc0c52016-09-23 21:30:09 +08001047#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001048#define f2fs_show_injection_info(type) \
1049 printk("%sF2FS-fs : inject %s in %s of %pF\n", \
1050 KERN_INFO, fault_name[type], \
1051 __func__, __builtin_return_address(0))
Chao Yu1ecc0c52016-09-23 21:30:09 +08001052static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
1053{
1054 struct f2fs_fault_info *ffi = &sbi->fault_info;
1055
1056 if (!ffi->inject_rate)
1057 return false;
1058
1059 if (!IS_FAULT_SET(ffi, type))
1060 return false;
1061
1062 atomic_inc(&ffi->inject_ops);
1063 if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
1064 atomic_set(&ffi->inject_ops, 0);
Chao Yu1ecc0c52016-09-23 21:30:09 +08001065 return true;
1066 }
1067 return false;
1068}
1069#endif
1070
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001071/* For write statistics. Suppose sector size is 512 bytes,
1072 * and the return value is in kbytes. s is of struct f2fs_sb_info.
1073 */
1074#define BD_PART_WRITTEN(s) \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001075(((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) - \
1076 (s)->sectors_written_start) >> 1)
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001077
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08001078static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
1079{
1080 sbi->last_time[type] = jiffies;
1081}
1082
1083static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
1084{
1085 struct timespec ts = {sbi->interval_time[type], 0};
1086 unsigned long interval = timespec_to_jiffies(&ts);
1087
1088 return time_after(jiffies, sbi->last_time[type] + interval);
1089}
1090
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001091static inline bool is_idle(struct f2fs_sb_info *sbi)
1092{
1093 struct block_device *bdev = sbi->sb->s_bdev;
1094 struct request_queue *q = bdev_get_queue(bdev);
1095 struct request_list *rl = &q->root_rl;
1096
1097 if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1098 return 0;
1099
1100 return f2fs_time_over(sbi, REQ_TIME);
1101}
1102
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001103/*
1104 * Inline functions
1105 */
Keith Mok43b65732016-03-02 12:04:24 -08001106static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1107 unsigned int length)
1108{
1109 SHASH_DESC_ON_STACK(shash, sbi->s_chksum_driver);
1110 u32 *ctx = (u32 *)shash_desc_ctx(shash);
David Millerb355b892017-06-02 11:28:54 -04001111 u32 retval;
Keith Mok43b65732016-03-02 12:04:24 -08001112 int err;
1113
1114 shash->tfm = sbi->s_chksum_driver;
1115 shash->flags = 0;
1116 *ctx = F2FS_SUPER_MAGIC;
1117
1118 err = crypto_shash_update(shash, address, length);
1119 BUG_ON(err);
1120
David Millerb355b892017-06-02 11:28:54 -04001121 retval = *ctx;
1122 barrier_data(ctx);
1123 return retval;
Keith Mok43b65732016-03-02 12:04:24 -08001124}
1125
1126static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1127 void *buf, size_t buf_size)
1128{
1129 return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1130}
1131
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001132static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
1133{
1134 return container_of(inode, struct f2fs_inode_info, vfs_inode);
1135}
1136
1137static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
1138{
1139 return sb->s_fs_info;
1140}
1141
Jaegeuk Kim40813632014-09-02 15:31:18 -07001142static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
1143{
1144 return F2FS_SB(inode->i_sb);
1145}
1146
1147static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
1148{
1149 return F2FS_I_SB(mapping->host);
1150}
1151
1152static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
1153{
1154 return F2FS_M_SB(page->mapping);
1155}
1156
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001157static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
1158{
1159 return (struct f2fs_super_block *)(sbi->raw_super);
1160}
1161
1162static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
1163{
1164 return (struct f2fs_checkpoint *)(sbi->ckpt);
1165}
1166
Gu Zheng45590712013-07-15 17:57:38 +08001167static inline struct f2fs_node *F2FS_NODE(struct page *page)
1168{
1169 return (struct f2fs_node *)page_address(page);
1170}
1171
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001172static inline struct f2fs_inode *F2FS_INODE(struct page *page)
1173{
1174 return &((struct f2fs_node *)page_address(page))->i;
1175}
1176
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001177static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
1178{
1179 return (struct f2fs_nm_info *)(sbi->nm_info);
1180}
1181
1182static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
1183{
1184 return (struct f2fs_sm_info *)(sbi->sm_info);
1185}
1186
1187static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
1188{
1189 return (struct sit_info *)(SM_I(sbi)->sit_info);
1190}
1191
1192static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
1193{
1194 return (struct free_segmap_info *)(SM_I(sbi)->free_info);
1195}
1196
1197static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
1198{
1199 return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
1200}
1201
Gu Zheng9df27d92014-01-20 18:37:04 +08001202static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
1203{
1204 return sbi->meta_inode->i_mapping;
1205}
1206
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001207static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
1208{
1209 return sbi->node_inode->i_mapping;
1210}
1211
Chao Yucaf00472015-01-28 17:48:42 +08001212static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001213{
Chao Yufadb2fb2016-09-20 10:29:47 +08001214 return test_bit(type, &sbi->s_flag);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001215}
1216
Chao Yucaf00472015-01-28 17:48:42 +08001217static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001218{
Chao Yufadb2fb2016-09-20 10:29:47 +08001219 set_bit(type, &sbi->s_flag);
Chao Yucaf00472015-01-28 17:48:42 +08001220}
1221
1222static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1223{
Chao Yufadb2fb2016-09-20 10:29:47 +08001224 clear_bit(type, &sbi->s_flag);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001225}
1226
Jaegeuk Kimd71b5562013-08-09 15:03:21 +09001227static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1228{
1229 return le64_to_cpu(cp->checkpoint_ver);
1230}
1231
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001232static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1233{
1234 size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1235 return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1236}
1237
Chao Yuaaec2b12016-09-20 11:04:18 +08001238static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001239{
1240 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
Chao Yuaaec2b12016-09-20 11:04:18 +08001241
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001242 return ckpt_flags & f;
1243}
1244
Chao Yuaaec2b12016-09-20 11:04:18 +08001245static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001246{
Chao Yuaaec2b12016-09-20 11:04:18 +08001247 return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1248}
1249
1250static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1251{
1252 unsigned int ckpt_flags;
1253
1254 ckpt_flags = le32_to_cpu(cp->ckpt_flags);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001255 ckpt_flags |= f;
1256 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1257}
1258
Chao Yuaaec2b12016-09-20 11:04:18 +08001259static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001260{
Chao Yuaaec2b12016-09-20 11:04:18 +08001261 spin_lock(&sbi->cp_lock);
1262 __set_ckpt_flags(F2FS_CKPT(sbi), f);
1263 spin_unlock(&sbi->cp_lock);
1264}
1265
1266static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1267{
1268 unsigned int ckpt_flags;
1269
1270 ckpt_flags = le32_to_cpu(cp->ckpt_flags);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001271 ckpt_flags &= (~f);
1272 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1273}
1274
Chao Yuaaec2b12016-09-20 11:04:18 +08001275static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1276{
1277 spin_lock(&sbi->cp_lock);
1278 __clear_ckpt_flags(F2FS_CKPT(sbi), f);
1279 spin_unlock(&sbi->cp_lock);
1280}
1281
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001282static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001283{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001284 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001285
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001286 if (lock)
1287 spin_lock(&sbi->cp_lock);
1288 __clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
1289 kfree(NM_I(sbi)->nat_bits);
1290 NM_I(sbi)->nat_bits = NULL;
1291 if (lock)
1292 spin_unlock(&sbi->cp_lock);
1293}
1294
1295static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
1296 struct cp_control *cpc)
1297{
1298 bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
1299
1300 return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001301}
1302
Gu Zhenge4795562013-09-27 18:08:30 +08001303static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001304{
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001305 down_read(&sbi->cp_rwsem);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001306}
1307
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001308static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1309{
1310 return down_read_trylock(&sbi->cp_rwsem);
1311}
1312
Gu Zhenge4795562013-09-27 18:08:30 +08001313static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001314{
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001315 up_read(&sbi->cp_rwsem);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001316}
1317
Gu Zhenge4795562013-09-27 18:08:30 +08001318static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001319{
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001320 down_write(&sbi->cp_rwsem);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001321}
1322
Gu Zhenge4795562013-09-27 18:08:30 +08001323static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001324{
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001325 up_write(&sbi->cp_rwsem);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001326}
1327
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001328static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1329{
1330 int reason = CP_SYNC;
1331
1332 if (test_opt(sbi, FASTBOOT))
1333 reason = CP_FASTBOOT;
1334 if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1335 reason = CP_UMOUNT;
1336 return reason;
1337}
1338
1339static inline bool __remain_node_summaries(int reason)
1340{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001341 return (reason & (CP_UMOUNT | CP_FASTBOOT));
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001342}
1343
1344static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1345{
Chao Yuaaec2b12016-09-20 11:04:18 +08001346 return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1347 is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001348}
1349
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001350/*
1351 * Check whether the given nid is within node id range.
1352 */
Namjae Jeon064e0822013-03-17 17:27:20 +09001353static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001354{
Chao Yud6b7d4b2014-06-12 13:23:41 +08001355 if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1356 return -EINVAL;
Chao Yucfb271d2013-12-05 17:15:22 +08001357 if (unlikely(nid >= NM_I(sbi)->max_nid))
Namjae Jeon064e0822013-03-17 17:27:20 +09001358 return -EINVAL;
1359 return 0;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001360}
1361
1362#define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
1363
1364/*
1365 * Check whether the inode has blocks or not
1366 */
1367static inline int F2FS_HAS_BLOCKS(struct inode *inode)
1368{
Chao Yu56a0d932017-06-14 23:00:56 +08001369 block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
1370
1371 return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) >
1372 (F2FS_DEFAULT_ALLOCATED_BLOCKS + xattr_block);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001373}
1374
Chao Yu4bc8e9b2014-03-17 16:35:06 +08001375static inline bool f2fs_has_xattr_block(unsigned int ofs)
1376{
1377 return ofs == XATTR_NODE_OFFSET;
1378}
1379
Chao Yu56a0d932017-06-14 23:00:56 +08001380static inline void f2fs_i_blocks_write(struct inode *, block_t, bool);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001381static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
Chao Yu46008c62016-05-09 19:56:30 +08001382 struct inode *inode, blkcnt_t *count)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001383{
Jaegeuk Kimdd11a5d2016-07-19 19:20:11 -07001384 blkcnt_t diff;
Chao Yu026bd9d2017-06-26 16:24:41 +08001385 block_t avail_user_block_count;
Jaegeuk Kimdd11a5d2016-07-19 19:20:11 -07001386
Jaegeuk Kimcb789422016-04-29 16:29:22 -07001387#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001388 if (time_to_inject(sbi, FAULT_BLOCK)) {
1389 f2fs_show_injection_info(FAULT_BLOCK);
Jaegeuk Kimcb789422016-04-29 16:29:22 -07001390 return false;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001391 }
Jaegeuk Kimcb789422016-04-29 16:29:22 -07001392#endif
Jaegeuk Kimdd11a5d2016-07-19 19:20:11 -07001393 /*
1394 * let's increase this in prior to actual block count change in order
1395 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1396 */
1397 percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
Chao Yucfb271d2013-12-05 17:15:22 +08001398
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001399 spin_lock(&sbi->stat_lock);
Jaegeuk Kim2555a2d2016-06-30 19:02:06 -07001400 sbi->total_valid_block_count += (block_t)(*count);
Chao Yu026bd9d2017-06-26 16:24:41 +08001401 avail_user_block_count = sbi->user_block_count - sbi->reserved_blocks;
1402 if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1403 diff = sbi->total_valid_block_count - avail_user_block_count;
Jaegeuk Kimdd11a5d2016-07-19 19:20:11 -07001404 *count -= diff;
Chao Yu026bd9d2017-06-26 16:24:41 +08001405 sbi->total_valid_block_count = avail_user_block_count;
Chao Yu46008c62016-05-09 19:56:30 +08001406 if (!*count) {
1407 spin_unlock(&sbi->stat_lock);
Jaegeuk Kimdd11a5d2016-07-19 19:20:11 -07001408 percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
Chao Yu46008c62016-05-09 19:56:30 +08001409 return false;
1410 }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001411 }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001412 spin_unlock(&sbi->stat_lock);
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001413
Jaegeuk Kim2555a2d2016-06-30 19:02:06 -07001414 f2fs_i_blocks_write(inode, *count, true);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001415 return true;
1416}
1417
Gu Zhengda19b0d2013-11-19 18:03:27 +08001418static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001419 struct inode *inode,
Chao Yu56a0d932017-06-14 23:00:56 +08001420 block_t count)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001421{
Chao Yu56a0d932017-06-14 23:00:56 +08001422 blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
1423
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001424 spin_lock(&sbi->stat_lock);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001425 f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
Chao Yu56a0d932017-06-14 23:00:56 +08001426 f2fs_bug_on(sbi, inode->i_blocks < sectors);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001427 sbi->total_valid_block_count -= (block_t)count;
1428 spin_unlock(&sbi->stat_lock);
Jaegeuk Kim2555a2d2016-06-30 19:02:06 -07001429 f2fs_i_blocks_write(inode, count, false);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001430}
1431
1432static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
1433{
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07001434 atomic_inc(&sbi->nr_pages[count_type]);
Chao Yu7c4abcb2016-08-18 17:46:14 +08001435
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001436 if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
1437 count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
Chao Yu7c4abcb2016-08-18 17:46:14 +08001438 return;
1439
Chao Yucaf00472015-01-28 17:48:42 +08001440 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001441}
1442
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001443static inline void inode_inc_dirty_pages(struct inode *inode)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001444{
Jaegeuk Kima43e1c42016-12-02 15:11:32 -08001445 atomic_inc(&F2FS_I(inode)->dirty_pages);
Chao Yuc227f912015-12-16 13:09:20 +08001446 inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1447 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001448}
1449
1450static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
1451{
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07001452 atomic_dec(&sbi->nr_pages[count_type]);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001453}
1454
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001455static inline void inode_dec_dirty_pages(struct inode *inode)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001456{
Chao Yu5ac9f362015-06-29 18:14:10 +08001457 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1458 !S_ISLNK(inode->i_mode))
Jaegeuk Kim1fe54f92014-02-07 10:00:06 +09001459 return;
1460
Jaegeuk Kima43e1c42016-12-02 15:11:32 -08001461 atomic_dec(&F2FS_I(inode)->dirty_pages);
Chao Yuc227f912015-12-16 13:09:20 +08001462 dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1463 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001464}
1465
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001466static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001467{
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07001468 return atomic_read(&sbi->nr_pages[count_type]);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001469}
1470
Jaegeuk Kima43e1c42016-12-02 15:11:32 -08001471static inline int get_dirty_pages(struct inode *inode)
Jaegeuk Kimf8b2c1f2014-03-18 12:33:06 +09001472{
Jaegeuk Kima43e1c42016-12-02 15:11:32 -08001473 return atomic_read(&F2FS_I(inode)->dirty_pages);
Jaegeuk Kimf8b2c1f2014-03-18 12:33:06 +09001474}
1475
Namjae Jeon5ac206c2013-02-02 23:52:59 +09001476static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
1477{
Chao Yu3519e3f2015-12-01 11:56:52 +08001478 unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001479 unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1480 sbi->log_blocks_per_seg;
1481
1482 return segs / sbi->segs_per_sec;
Namjae Jeon5ac206c2013-02-02 23:52:59 +09001483}
1484
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001485static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
1486{
Jaegeuk Kim8b8343f2014-02-24 13:00:13 +09001487 return sbi->total_valid_block_count;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001488}
1489
Yunlei Hef83a2582016-08-18 21:01:18 +08001490static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1491{
1492 return sbi->discard_blks;
1493}
1494
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001495static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
1496{
1497 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1498
1499 /* return NAT or SIT bitmap */
1500 if (flag == NAT_BITMAP)
1501 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
1502 else if (flag == SIT_BITMAP)
1503 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
1504
1505 return 0;
1506}
1507
Wanpeng Li55141482015-02-26 07:57:20 +08001508static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
1509{
1510 return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
1511}
1512
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001513static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
1514{
1515 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Changman Lee1dbe4152014-05-12 12:27:43 +09001516 int offset;
1517
Wanpeng Li55141482015-02-26 07:57:20 +08001518 if (__cp_payload(sbi) > 0) {
Changman Lee1dbe4152014-05-12 12:27:43 +09001519 if (flag == NAT_BITMAP)
1520 return &ckpt->sit_nat_version_bitmap;
1521 else
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -07001522 return (unsigned char *)ckpt + F2FS_BLKSIZE;
Changman Lee1dbe4152014-05-12 12:27:43 +09001523 } else {
1524 offset = (flag == NAT_BITMAP) ?
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001525 le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
Changman Lee1dbe4152014-05-12 12:27:43 +09001526 return &ckpt->sit_nat_version_bitmap + offset;
1527 }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001528}
1529
1530static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
1531{
Jaegeuk Kim01e15b32016-11-24 12:45:15 -08001532 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001533
Jaegeuk Kim01e15b32016-11-24 12:45:15 -08001534 if (sbi->cur_cp_pack == 2)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001535 start_addr += sbi->blocks_per_seg;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001536 return start_addr;
1537}
1538
Jaegeuk Kim01e15b32016-11-24 12:45:15 -08001539static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
1540{
1541 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
1542
1543 if (sbi->cur_cp_pack == 1)
1544 start_addr += sbi->blocks_per_seg;
1545 return start_addr;
1546}
1547
1548static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
1549{
1550 sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
1551}
1552
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001553static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
1554{
1555 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
1556}
1557
1558static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
Gu Zhengef86d702013-11-19 18:03:38 +08001559 struct inode *inode)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001560{
1561 block_t valid_block_count;
1562 unsigned int valid_node_count;
1563
1564 spin_lock(&sbi->stat_lock);
1565
Gu Zhengef86d702013-11-19 18:03:38 +08001566 valid_block_count = sbi->total_valid_block_count + 1;
Chao Yu026bd9d2017-06-26 16:24:41 +08001567 if (unlikely(valid_block_count + sbi->reserved_blocks >
1568 sbi->user_block_count)) {
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001569 spin_unlock(&sbi->stat_lock);
1570 return false;
1571 }
1572
Gu Zhengef86d702013-11-19 18:03:38 +08001573 valid_node_count = sbi->total_valid_node_count + 1;
Chao Yucfb271d2013-12-05 17:15:22 +08001574 if (unlikely(valid_node_count > sbi->total_node_count)) {
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001575 spin_unlock(&sbi->stat_lock);
1576 return false;
1577 }
1578
1579 if (inode)
Jaegeuk Kim8edd03c2016-05-20 09:26:06 -07001580 f2fs_i_blocks_write(inode, 1, true);
Gu Zhengef86d702013-11-19 18:03:38 +08001581
Gu Zhengef86d702013-11-19 18:03:38 +08001582 sbi->total_valid_node_count++;
1583 sbi->total_valid_block_count++;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001584 spin_unlock(&sbi->stat_lock);
1585
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001586 percpu_counter_inc(&sbi->alloc_valid_block_count);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001587 return true;
1588}
1589
1590static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
Gu Zhengef86d702013-11-19 18:03:38 +08001591 struct inode *inode)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001592{
1593 spin_lock(&sbi->stat_lock);
1594
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001595 f2fs_bug_on(sbi, !sbi->total_valid_block_count);
1596 f2fs_bug_on(sbi, !sbi->total_valid_node_count);
1597 f2fs_bug_on(sbi, !inode->i_blocks);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001598
Jaegeuk Kim8edd03c2016-05-20 09:26:06 -07001599 f2fs_i_blocks_write(inode, 1, false);
Gu Zhengef86d702013-11-19 18:03:38 +08001600 sbi->total_valid_node_count--;
1601 sbi->total_valid_block_count--;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001602
1603 spin_unlock(&sbi->stat_lock);
1604}
1605
1606static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
1607{
Jaegeuk Kim8b8343f2014-02-24 13:00:13 +09001608 return sbi->total_valid_node_count;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001609}
1610
1611static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
1612{
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001613 percpu_counter_inc(&sbi->total_valid_inode_count);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001614}
1615
Jaegeuk Kim0e802202013-11-26 16:36:20 +09001616static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001617{
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001618 percpu_counter_dec(&sbi->total_valid_inode_count);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001619}
1620
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001621static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001622{
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001623 return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001624}
1625
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001626static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1627 pgoff_t index, bool for_write)
1628{
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -07001629#ifdef CONFIG_F2FS_FAULT_INJECTION
1630 struct page *page = find_lock_page(mapping, index);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001631
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -07001632 if (page)
1633 return page;
1634
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001635 if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
1636 f2fs_show_injection_info(FAULT_PAGE_ALLOC);
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -07001637 return NULL;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001638 }
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -07001639#endif
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001640 if (!for_write)
1641 return grab_cache_page(mapping, index);
1642 return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1643}
1644
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001645static inline void f2fs_copy_page(struct page *src, struct page *dst)
1646{
1647 char *src_kaddr = kmap(src);
1648 char *dst_kaddr = kmap(dst);
1649
1650 memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
1651 kunmap(dst);
1652 kunmap(src);
1653}
1654
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001655static inline void f2fs_put_page(struct page *page, int unlock)
1656{
Jaegeuk Kim031fa8c2013-11-28 12:55:13 +09001657 if (!page)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001658 return;
1659
1660 if (unlock) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001661 f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001662 unlock_page(page);
1663 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001664 put_page(page);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001665}
1666
1667static inline void f2fs_put_dnode(struct dnode_of_data *dn)
1668{
1669 if (dn->node_page)
1670 f2fs_put_page(dn->node_page, 1);
1671 if (dn->inode_page && dn->node_page != dn->inode_page)
1672 f2fs_put_page(dn->inode_page, 0);
1673 dn->node_page = NULL;
1674 dn->inode_page = NULL;
1675}
1676
1677static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
Gu Zhenge8512d22014-03-07 18:43:28 +08001678 size_t size)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001679{
Gu Zhenge8512d22014-03-07 18:43:28 +08001680 return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001681}
1682
Gu Zheng7bd59382013-10-22 14:52:26 +08001683static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
1684 gfp_t flags)
1685{
1686 void *entry;
Gu Zheng7bd59382013-10-22 14:52:26 +08001687
Jaegeuk Kim80c54502015-08-20 08:51:56 -07001688 entry = kmem_cache_alloc(cachep, flags);
1689 if (!entry)
1690 entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
Gu Zheng7bd59382013-10-22 14:52:26 +08001691 return entry;
1692}
1693
Jaegeuk Kim740432f2015-08-14 11:43:56 -07001694static inline struct bio *f2fs_bio_alloc(int npages)
1695{
1696 struct bio *bio;
1697
1698 /* No failure on bio allocation */
Jaegeuk Kim740432f2015-08-14 11:43:56 -07001699 bio = bio_alloc(GFP_NOIO, npages);
Jaegeuk Kim80c54502015-08-20 08:51:56 -07001700 if (!bio)
1701 bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
Jaegeuk Kim740432f2015-08-14 11:43:56 -07001702 return bio;
1703}
1704
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08001705static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
1706 unsigned long index, void *item)
1707{
1708 while (radix_tree_insert(root, index, item))
1709 cond_resched();
1710}
1711
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001712#define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
1713
1714static inline bool IS_INODE(struct page *page)
1715{
Gu Zheng45590712013-07-15 17:57:38 +08001716 struct f2fs_node *p = F2FS_NODE(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001717
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001718 return RAW_IS_INODE(p);
1719}
1720
1721static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
1722{
1723 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
1724}
1725
1726static inline block_t datablock_addr(struct page *node_page,
1727 unsigned int offset)
1728{
1729 struct f2fs_node *raw_node;
1730 __le32 *addr_array;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001731
Gu Zheng45590712013-07-15 17:57:38 +08001732 raw_node = F2FS_NODE(node_page);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001733 addr_array = blkaddr_in_node(raw_node);
1734 return le32_to_cpu(addr_array[offset]);
1735}
1736
1737static inline int f2fs_test_bit(unsigned int nr, char *addr)
1738{
1739 int mask;
1740
1741 addr += (nr >> 3);
1742 mask = 1 << (7 - (nr & 0x07));
1743 return mask & *addr;
1744}
1745
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001746static inline void f2fs_set_bit(unsigned int nr, char *addr)
1747{
1748 int mask;
1749
1750 addr += (nr >> 3);
1751 mask = 1 << (7 - (nr & 0x07));
1752 *addr |= mask;
1753}
1754
1755static inline void f2fs_clear_bit(unsigned int nr, char *addr)
1756{
1757 int mask;
1758
1759 addr += (nr >> 3);
1760 mask = 1 << (7 - (nr & 0x07));
1761 *addr &= ~mask;
1762}
1763
Gu Zheng52aca072014-10-20 17:45:51 +08001764static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001765{
1766 int mask;
1767 int ret;
1768
1769 addr += (nr >> 3);
1770 mask = 1 << (7 - (nr & 0x07));
1771 ret = mask & *addr;
1772 *addr |= mask;
1773 return ret;
1774}
1775
Gu Zheng52aca072014-10-20 17:45:51 +08001776static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001777{
1778 int mask;
1779 int ret;
1780
1781 addr += (nr >> 3);
1782 mask = 1 << (7 - (nr & 0x07));
1783 ret = mask & *addr;
1784 *addr &= ~mask;
1785 return ret;
1786}
1787
Gu Zhengc6ac4c02014-10-20 17:45:50 +08001788static inline void f2fs_change_bit(unsigned int nr, char *addr)
1789{
1790 int mask;
1791
1792 addr += (nr >> 3);
1793 mask = 1 << (7 - (nr & 0x07));
1794 *addr ^= mask;
1795}
1796
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001797/* used for f2fs_inode_info->flags */
1798enum {
1799 FI_NEW_INODE, /* indicate newly allocated inode */
Jaegeuk Kimb3783872013-06-10 09:17:01 +09001800 FI_DIRTY_INODE, /* indicate inode is dirty or not */
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001801 FI_AUTO_RECOVER, /* indicate inode is recoverable */
Jaegeuk Kimed57c272014-04-15 11:19:28 +09001802 FI_DIRTY_DIR, /* indicate directory has dirty pages */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001803 FI_INC_LINK, /* need to increment i_nlink */
1804 FI_ACL_MODE, /* indicate acl mode */
1805 FI_NO_ALLOC, /* should not allocate any blocks */
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -07001806 FI_FREE_NID, /* free allocated nide */
Jaegeuk Kimc11abd12013-11-19 10:41:54 +09001807 FI_NO_EXTENT, /* not to use the extent cache */
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001808 FI_INLINE_XATTR, /* used for inline xattr */
Huajun Li1001b342013-11-10 23:13:16 +08001809 FI_INLINE_DATA, /* used for inline data*/
Chao Yu34d67de2014-09-24 18:15:19 +08001810 FI_INLINE_DENTRY, /* used for inline dentry */
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001811 FI_APPEND_WRITE, /* inode has appended data */
1812 FI_UPDATE_WRITE, /* inode has in-place-update data */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001813 FI_NEED_IPU, /* used for ipu per file */
1814 FI_ATOMIC_FILE, /* indicate atomic file */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001815 FI_ATOMIC_COMMIT, /* indicate the state of atomical committing */
Jaegeuk Kim02a13352014-10-06 16:11:16 -07001816 FI_VOLATILE_FILE, /* indicate volatile file */
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001817 FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001818 FI_DROP_CACHE, /* drop dirty page cache */
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001819 FI_DATA_EXIST, /* indicate data exists */
Jaegeuk Kim510022a2015-03-30 15:07:16 -07001820 FI_INLINE_DOTS, /* indicate inline dot dentries */
Chao Yud323d002015-10-27 09:53:45 +08001821 FI_DO_DEFRAG, /* indicate defragment is running */
Chao Yuc227f912015-12-16 13:09:20 +08001822 FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001823 FI_NO_PREALLOC, /* indicate skipped preallocated blocks */
1824 FI_HOT_DATA, /* indicate file is hot */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001825};
1826
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001827static inline void __mark_inode_dirty_flag(struct inode *inode,
1828 int flag, bool set)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001829{
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001830 switch (flag) {
1831 case FI_INLINE_XATTR:
1832 case FI_INLINE_DATA:
1833 case FI_INLINE_DENTRY:
1834 if (set)
1835 return;
1836 case FI_DATA_EXIST:
1837 case FI_INLINE_DOTS:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001838 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001839 }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001840}
1841
Jaegeuk Kim91942322016-05-20 10:13:22 -07001842static inline void set_inode_flag(struct inode *inode, int flag)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001843{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001844 if (!test_bit(flag, &F2FS_I(inode)->flags))
1845 set_bit(flag, &F2FS_I(inode)->flags);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001846 __mark_inode_dirty_flag(inode, flag, true);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001847}
1848
Jaegeuk Kim91942322016-05-20 10:13:22 -07001849static inline int is_inode_flag_set(struct inode *inode, int flag)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001850{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001851 return test_bit(flag, &F2FS_I(inode)->flags);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001852}
1853
Jaegeuk Kim91942322016-05-20 10:13:22 -07001854static inline void clear_inode_flag(struct inode *inode, int flag)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001855{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001856 if (test_bit(flag, &F2FS_I(inode)->flags))
1857 clear_bit(flag, &F2FS_I(inode)->flags);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001858 __mark_inode_dirty_flag(inode, flag, false);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001859}
1860
Jaegeuk Kim91942322016-05-20 10:13:22 -07001861static inline void set_acl_inode(struct inode *inode, umode_t mode)
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001862{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001863 F2FS_I(inode)->i_acl_mode = mode;
1864 set_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001865 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001866}
1867
Jaegeuk Kima1961242016-05-20 09:43:20 -07001868static inline void f2fs_i_links_write(struct inode *inode, bool inc)
1869{
1870 if (inc)
1871 inc_nlink(inode);
1872 else
1873 drop_nlink(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001874 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kima1961242016-05-20 09:43:20 -07001875}
1876
Jaegeuk Kim8edd03c2016-05-20 09:26:06 -07001877static inline void f2fs_i_blocks_write(struct inode *inode,
Chao Yu56a0d932017-06-14 23:00:56 +08001878 block_t diff, bool add)
Jaegeuk Kim8edd03c2016-05-20 09:26:06 -07001879{
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001880 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
1881 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
Chao Yu56a0d932017-06-14 23:00:56 +08001882 blkcnt_t sectors = diff << F2FS_LOG_SECTORS_PER_BLOCK;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001883
Chao Yu56a0d932017-06-14 23:00:56 +08001884 inode->i_blocks = add ? inode->i_blocks + sectors :
1885 inode->i_blocks - sectors;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001886 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001887 if (clean || recover)
1888 set_inode_flag(inode, FI_AUTO_RECOVER);
Jaegeuk Kim8edd03c2016-05-20 09:26:06 -07001889}
1890
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001891static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
1892{
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001893 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
1894 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
1895
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001896 if (i_size_read(inode) == i_size)
1897 return;
1898
1899 i_size_write(inode, i_size);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001900 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001901 if (clean || recover)
1902 set_inode_flag(inode, FI_AUTO_RECOVER);
1903}
1904
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001905static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
1906{
1907 F2FS_I(inode)->i_current_depth = depth;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001908 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001909}
1910
1911static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
1912{
1913 F2FS_I(inode)->i_xattr_nid = xnid;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001914 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001915}
1916
1917static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
1918{
1919 F2FS_I(inode)->i_pino = pino;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001920 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001921}
1922
Jaegeuk Kim91942322016-05-20 10:13:22 -07001923static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001924{
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001925 struct f2fs_inode_info *fi = F2FS_I(inode);
1926
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001927 if (ri->i_inline & F2FS_INLINE_XATTR)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001928 set_bit(FI_INLINE_XATTR, &fi->flags);
Huajun Li1001b342013-11-10 23:13:16 +08001929 if (ri->i_inline & F2FS_INLINE_DATA)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001930 set_bit(FI_INLINE_DATA, &fi->flags);
Chao Yu34d67de2014-09-24 18:15:19 +08001931 if (ri->i_inline & F2FS_INLINE_DENTRY)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001932 set_bit(FI_INLINE_DENTRY, &fi->flags);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001933 if (ri->i_inline & F2FS_DATA_EXIST)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001934 set_bit(FI_DATA_EXIST, &fi->flags);
Jaegeuk Kim510022a2015-03-30 15:07:16 -07001935 if (ri->i_inline & F2FS_INLINE_DOTS)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001936 set_bit(FI_INLINE_DOTS, &fi->flags);
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001937}
1938
Jaegeuk Kim91942322016-05-20 10:13:22 -07001939static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001940{
1941 ri->i_inline = 0;
1942
Jaegeuk Kim91942322016-05-20 10:13:22 -07001943 if (is_inode_flag_set(inode, FI_INLINE_XATTR))
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001944 ri->i_inline |= F2FS_INLINE_XATTR;
Jaegeuk Kim91942322016-05-20 10:13:22 -07001945 if (is_inode_flag_set(inode, FI_INLINE_DATA))
Huajun Li1001b342013-11-10 23:13:16 +08001946 ri->i_inline |= F2FS_INLINE_DATA;
Jaegeuk Kim91942322016-05-20 10:13:22 -07001947 if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
Chao Yu34d67de2014-09-24 18:15:19 +08001948 ri->i_inline |= F2FS_INLINE_DENTRY;
Jaegeuk Kim91942322016-05-20 10:13:22 -07001949 if (is_inode_flag_set(inode, FI_DATA_EXIST))
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001950 ri->i_inline |= F2FS_DATA_EXIST;
Jaegeuk Kim91942322016-05-20 10:13:22 -07001951 if (is_inode_flag_set(inode, FI_INLINE_DOTS))
Jaegeuk Kim510022a2015-03-30 15:07:16 -07001952 ri->i_inline |= F2FS_INLINE_DOTS;
Jaegeuk Kim444c5802013-08-08 15:16:22 +09001953}
1954
Chao Yu987c7c32014-03-12 15:59:03 +08001955static inline int f2fs_has_inline_xattr(struct inode *inode)
1956{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001957 return is_inode_flag_set(inode, FI_INLINE_XATTR);
Chao Yu987c7c32014-03-12 15:59:03 +08001958}
1959
Chao Yu81ca7352016-01-26 15:39:35 +08001960static inline unsigned int addrs_per_inode(struct inode *inode)
Jaegeuk Kimde936532013-08-12 21:08:03 +09001961{
Chao Yu81ca7352016-01-26 15:39:35 +08001962 if (f2fs_has_inline_xattr(inode))
Jaegeuk Kimde936532013-08-12 21:08:03 +09001963 return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
1964 return DEF_ADDRS_PER_INODE;
1965}
1966
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001967static inline void *inline_xattr_addr(struct page *page)
1968{
Chao Yu695fd1e2014-02-27 19:52:21 +08001969 struct f2fs_inode *ri = F2FS_INODE(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001970
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001971 return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
1972 F2FS_INLINE_XATTR_ADDRS]);
1973}
1974
1975static inline int inline_xattr_size(struct inode *inode)
1976{
Chao Yu987c7c32014-03-12 15:59:03 +08001977 if (f2fs_has_inline_xattr(inode))
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001978 return F2FS_INLINE_XATTR_ADDRS << 2;
1979 else
1980 return 0;
1981}
1982
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09001983static inline int f2fs_has_inline_data(struct inode *inode)
1984{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001985 return is_inode_flag_set(inode, FI_INLINE_DATA);
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09001986}
1987
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001988static inline int f2fs_exist_data(struct inode *inode)
1989{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001990 return is_inode_flag_set(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001991}
1992
Jaegeuk Kim510022a2015-03-30 15:07:16 -07001993static inline int f2fs_has_inline_dots(struct inode *inode)
1994{
Jaegeuk Kim91942322016-05-20 10:13:22 -07001995 return is_inode_flag_set(inode, FI_INLINE_DOTS);
Jaegeuk Kim510022a2015-03-30 15:07:16 -07001996}
1997
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07001998static inline bool f2fs_is_atomic_file(struct inode *inode)
1999{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002000 return is_inode_flag_set(inode, FI_ATOMIC_FILE);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002001}
2002
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002003static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
2004{
2005 return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
2006}
2007
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002008static inline bool f2fs_is_volatile_file(struct inode *inode)
2009{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002010 return is_inode_flag_set(inode, FI_VOLATILE_FILE);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002011}
2012
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07002013static inline bool f2fs_is_first_block_written(struct inode *inode)
2014{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002015 return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07002016}
2017
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002018static inline bool f2fs_is_drop_cache(struct inode *inode)
2019{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002020 return is_inode_flag_set(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002021}
2022
Huajun Li1001b342013-11-10 23:13:16 +08002023static inline void *inline_data_addr(struct page *page)
2024{
Chao Yu695fd1e2014-02-27 19:52:21 +08002025 struct f2fs_inode *ri = F2FS_INODE(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002026
Huajun Li1001b342013-11-10 23:13:16 +08002027 return (void *)&(ri->i_addr[1]);
2028}
2029
Chao Yu34d67de2014-09-24 18:15:19 +08002030static inline int f2fs_has_inline_dentry(struct inode *inode)
2031{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002032 return is_inode_flag_set(inode, FI_INLINE_DENTRY);
Chao Yu34d67de2014-09-24 18:15:19 +08002033}
2034
Jaegeuk Kim9486ba4422014-11-21 16:36:28 -08002035static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
2036{
2037 if (!f2fs_has_inline_dentry(dir))
2038 kunmap(page);
2039}
2040
Jaegeuk Kimb5492af72015-04-20 13:44:41 -07002041static inline int is_file(struct inode *inode, int type)
2042{
2043 return F2FS_I(inode)->i_advise & type;
2044}
2045
2046static inline void set_file(struct inode *inode, int type)
2047{
2048 F2FS_I(inode)->i_advise |= type;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002049 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kimb5492af72015-04-20 13:44:41 -07002050}
2051
2052static inline void clear_file(struct inode *inode, int type)
2053{
2054 F2FS_I(inode)->i_advise &= ~type;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002055 f2fs_mark_inode_dirty_sync(inode, true);
2056}
2057
2058static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
2059{
2060 if (dsync) {
2061 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2062 bool ret;
2063
2064 spin_lock(&sbi->inode_lock[DIRTY_META]);
2065 ret = list_empty(&F2FS_I(inode)->gdirty_list);
2066 spin_unlock(&sbi->inode_lock[DIRTY_META]);
2067 return ret;
2068 }
2069 if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
2070 file_keep_isize(inode) ||
2071 i_size_read(inode) & PAGE_MASK)
2072 return false;
2073 return F2FS_I(inode)->last_disk_size == i_size_read(inode);
Jaegeuk Kimb5492af72015-04-20 13:44:41 -07002074}
2075
Jaegeuk Kim77888c12013-05-20 20:28:47 +09002076static inline int f2fs_readonly(struct super_block *sb)
2077{
2078 return sb->s_flags & MS_RDONLY;
2079}
2080
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07002081static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2082{
Chao Yuaaec2b12016-09-20 11:04:18 +08002083 return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07002084}
2085
Jaegeuk Kimeaa693f2015-04-26 00:15:29 -07002086static inline bool is_dot_dotdot(const struct qstr *str)
2087{
2088 if (str->len == 1 && str->name[0] == '.')
2089 return true;
2090
2091 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2092 return true;
2093
2094 return false;
2095}
2096
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07002097static inline bool f2fs_may_extent_tree(struct inode *inode)
2098{
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07002099 if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
Jaegeuk Kim91942322016-05-20 10:13:22 -07002100 is_inode_flag_set(inode, FI_NO_EXTENT))
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07002101 return false;
2102
Al Viro886f56f2015-12-05 03:56:06 -05002103 return S_ISREG(inode->i_mode);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07002104}
2105
Chao Yu1ecc0c52016-09-23 21:30:09 +08002106static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
2107 size_t size, gfp_t flags)
Jaegeuk Kim0414b002016-04-29 15:16:42 -07002108{
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -07002109#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002110 if (time_to_inject(sbi, FAULT_KMALLOC)) {
2111 f2fs_show_injection_info(FAULT_KMALLOC);
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -07002112 return NULL;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002113 }
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -07002114#endif
Jaegeuk Kim0414b002016-04-29 15:16:42 -07002115 return kmalloc(size, flags);
2116}
2117
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002118static inline void *kvmalloc(size_t size, gfp_t flags)
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002119{
2120 void *ret;
2121
2122 ret = kmalloc(size, flags | __GFP_NOWARN);
2123 if (!ret)
2124 ret = __vmalloc(size, flags, PAGE_KERNEL);
2125 return ret;
2126}
2127
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002128static inline void *kvzalloc(size_t size, gfp_t flags)
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002129{
2130 void *ret;
2131
2132 ret = kzalloc(size, flags | __GFP_NOWARN);
2133 if (!ret)
2134 ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
2135 return ret;
2136}
2137
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002138static inline int wbc_to_write_flags(struct writeback_control *wbc)
2139{
2140 if (wbc->sync_mode == WB_SYNC_ALL)
2141 return REQ_SYNC;
2142 else if (wbc->for_kupdate || wbc->for_background)
2143 return 0;
2144
2145 return 0;
2146}
2147
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08002148#define get_inode_mode(i) \
Jaegeuk Kim91942322016-05-20 10:13:22 -07002149 ((is_inode_flag_set(i, FI_ACL_MODE)) ? \
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08002150 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2151
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002152/*
2153 * file.c
2154 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002155int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2156void truncate_data_blocks(struct dnode_of_data *dn);
2157int truncate_blocks(struct inode *inode, u64 from, bool lock);
2158int f2fs_truncate(struct inode *inode);
2159int f2fs_getattr(struct vfsmount *mnt, struct dentry *dentry,
2160 struct kstat *stat);
2161int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2162int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2163int truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2164long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2165long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002166
2167/*
2168 * inode.c
2169 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002170void f2fs_set_inode_flags(struct inode *inode);
2171struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2172struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2173int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2174int update_inode(struct inode *inode, struct page *node_page);
2175int update_inode_page(struct inode *inode);
2176int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2177void f2fs_evict_inode(struct inode *inode);
2178void handle_failed_inode(struct inode *inode);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002179
2180/*
2181 * namei.c
2182 */
2183struct dentry *f2fs_get_parent(struct dentry *child);
2184
2185/*
2186 * dir.c
2187 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002188void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
2189unsigned char get_de_type(struct f2fs_dir_entry *de);
2190struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
2191 f2fs_hash_t namehash, int *max_slots,
2192 struct f2fs_dentry_ptr *d);
2193int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2194 unsigned int start_pos, struct fscrypt_str *fstr);
2195void do_make_empty_dir(struct inode *inode, struct inode *parent,
2196 struct f2fs_dentry_ptr *d);
2197struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
2198 const struct qstr *new_name,
2199 const struct qstr *orig_name, struct page *dpage);
2200void update_parent_metadata(struct inode *dir, struct inode *inode,
2201 unsigned int current_depth);
2202int room_for_filename(const void *bitmap, int slots, int max_slots);
2203void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2204struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2205 struct fscrypt_name *fname, struct page **res_page);
2206struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2207 const struct qstr *child, struct page **res_page);
2208struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2209ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2210 struct page **page);
2211void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2212 struct page *page, struct inode *inode);
2213void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2214 const struct qstr *name, f2fs_hash_t name_hash,
2215 unsigned int bit_pos);
2216int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2217 const struct qstr *orig_name,
2218 struct inode *inode, nid_t ino, umode_t mode);
2219int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
2220 struct inode *inode, nid_t ino, umode_t mode);
2221int __f2fs_add_link(struct inode *dir, const struct qstr *name,
2222 struct inode *inode, nid_t ino, umode_t mode);
2223void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2224 struct inode *dir, struct inode *inode);
2225int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2226bool f2fs_empty_dir(struct inode *dir);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002227
Al Virob7f7a5e2013-01-25 16:15:43 -05002228static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2229{
David Howells2b0143b2015-03-17 22:25:59 +00002230 return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -07002231 inode, inode->i_ino, inode->i_mode);
Al Virob7f7a5e2013-01-25 16:15:43 -05002232}
2233
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002234/*
2235 * super.c
2236 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002237int f2fs_inode_dirtied(struct inode *inode, bool sync);
2238void f2fs_inode_synced(struct inode *inode);
2239int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2240int f2fs_sync_fs(struct super_block *sb, int sync);
Namjae Jeona07ef782012-12-30 14:52:05 +09002241extern __printf(3, 4)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002242void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
Shawn Lin984ec632016-02-17 11:26:32 +08002243int sanity_check_ckpt(struct f2fs_sb_info *sbi);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002244
2245/*
2246 * hash.c
2247 */
Jaegeuk Kim8daed212017-04-24 10:00:08 -07002248f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
2249 struct fscrypt_name *fname);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002250
2251/*
2252 * node.c
2253 */
2254struct dnode_of_data;
2255struct node_info;
2256
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002257bool available_free_memory(struct f2fs_sb_info *sbi, int type);
2258int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2259bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2260bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2261void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
2262pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2263int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2264int truncate_inode_blocks(struct inode *inode, pgoff_t from);
2265int truncate_xattr_node(struct inode *inode, struct page *page);
2266int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2267int remove_inode_page(struct inode *inode);
2268struct page *new_inode_page(struct inode *inode);
2269struct page *new_node_page(struct dnode_of_data *dn,
2270 unsigned int ofs, struct page *ipage);
2271void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2272struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2273struct page *get_node_page_ra(struct page *parent, int start);
2274void move_node_page(struct page *node_page, int gc_type);
2275int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2276 struct writeback_control *wbc, bool atomic);
2277int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc);
2278void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2279bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2280void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2281void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2282int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2283void recover_inline_xattr(struct inode *inode, struct page *page);
2284int recover_xattr_data(struct inode *inode, struct page *page,
2285 block_t blkaddr);
2286int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2287int restore_node_summary(struct f2fs_sb_info *sbi,
2288 unsigned int segno, struct f2fs_summary_block *sum);
2289void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2290int build_node_manager(struct f2fs_sb_info *sbi);
2291void destroy_node_manager(struct f2fs_sb_info *sbi);
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002292int __init create_node_manager_caches(void);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002293void destroy_node_manager_caches(void);
2294
2295/*
2296 * segment.c
2297 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002298void register_inmem_page(struct inode *inode, struct page *page);
2299void drop_inmem_pages(struct inode *inode);
2300void drop_inmem_page(struct inode *inode, struct page *page);
2301int commit_inmem_pages(struct inode *inode);
2302void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2303void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
2304int f2fs_issue_flush(struct f2fs_sb_info *sbi);
2305int create_flush_cmd_control(struct f2fs_sb_info *sbi);
2306void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2307void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2308bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
2309void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new);
Chao Yud9d85cc2017-06-29 23:17:45 +08002310void stop_discard_thread(struct f2fs_sb_info *sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002311void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
2312void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2313void release_discard_addrs(struct f2fs_sb_info *sbi);
2314int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2315void allocate_new_segments(struct f2fs_sb_info *sbi);
2316int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2317bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2318struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2319void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2320void write_meta_page(struct f2fs_sb_info *sbi, struct page *page);
2321void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2322void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
2323int rewrite_data_page(struct f2fs_io_info *fio);
2324void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2325 block_t old_blkaddr, block_t new_blkaddr,
2326 bool recover_curseg, bool recover_newaddr);
2327void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2328 block_t old_addr, block_t new_addr,
2329 unsigned char version, bool recover_curseg,
2330 bool recover_newaddr);
2331void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2332 block_t old_blkaddr, block_t *new_blkaddr,
Chao Yuc52dc0f2017-05-19 23:37:01 +08002333 struct f2fs_summary *sum, int type,
2334 struct f2fs_io_info *fio, bool add_list);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002335void f2fs_wait_on_page_writeback(struct page *page,
2336 enum page_type type, bool ordered);
2337void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
2338 block_t blkaddr);
2339void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2340void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2341int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2342 unsigned int val, int alloc);
2343void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2344int build_segment_manager(struct f2fs_sb_info *sbi);
2345void destroy_segment_manager(struct f2fs_sb_info *sbi);
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +09002346int __init create_segment_manager_caches(void);
2347void destroy_segment_manager_caches(void);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002348
2349/*
2350 * checkpoint.c
2351 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002352void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
2353struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2354struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2355struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2356bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type);
2357int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2358 int type, bool sync);
2359void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2360long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2361 long nr_to_write);
2362void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2363void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2364void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2365bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
2366int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
2367int acquire_orphan_inode(struct f2fs_sb_info *sbi);
2368void release_orphan_inode(struct f2fs_sb_info *sbi);
2369void add_orphan_inode(struct inode *inode);
2370void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2371int recover_orphan_inodes(struct f2fs_sb_info *sbi);
2372int get_valid_checkpoint(struct f2fs_sb_info *sbi);
2373void update_dirty_page(struct inode *inode, struct page *page);
2374void remove_dirty_inode(struct inode *inode);
2375int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2376int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2377void init_ino_entry_info(struct f2fs_sb_info *sbi);
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002378int __init create_checkpoint_caches(void);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002379void destroy_checkpoint_caches(void);
2380
2381/*
2382 * data.c
2383 */
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002384void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2385void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002386 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002387 enum page_type type);
2388void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002389int f2fs_submit_page_bio(struct f2fs_io_info *fio);
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002390int f2fs_submit_page_write(struct f2fs_io_info *fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002391struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2392 block_t blk_addr, struct bio *bio);
2393int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
2394void set_data_blkaddr(struct dnode_of_data *dn);
2395void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
2396int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2397int reserve_new_block(struct dnode_of_data *dn);
2398int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2399int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2400int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
2401struct page *get_read_data_page(struct inode *inode, pgoff_t index,
2402 int op_flags, bool for_write);
2403struct page *find_data_page(struct inode *inode, pgoff_t index);
2404struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
2405 bool for_write);
2406struct page *get_new_data_page(struct inode *inode,
2407 struct page *ipage, pgoff_t index, bool new_i_size);
2408int do_write_data_page(struct f2fs_io_info *fio);
2409int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2410 int create, int flag);
2411int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2412 u64 start, u64 len);
2413void f2fs_set_page_dirty_nobuffers(struct page *page);
2414void f2fs_invalidate_page(struct page *page, unsigned int offset,
2415 unsigned int length);
2416int f2fs_release_page(struct page *page, gfp_t wait);
Weichao Guo5b7a4872016-09-20 05:03:27 +08002417#ifdef CONFIG_MIGRATION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002418int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2419 struct page *page, enum migrate_mode mode);
Weichao Guo5b7a4872016-09-20 05:03:27 +08002420#endif
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002421
2422/*
2423 * gc.c
2424 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002425int start_gc_thread(struct f2fs_sb_info *sbi);
2426void stop_gc_thread(struct f2fs_sb_info *sbi);
2427block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2428int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2429 unsigned int segno);
2430void build_gc_manager(struct f2fs_sb_info *sbi);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002431
2432/*
2433 * recovery.c
2434 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002435int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
2436bool space_for_roll_forward(struct f2fs_sb_info *sbi);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002437
2438/*
2439 * debug.c
2440 */
2441#ifdef CONFIG_F2FS_STAT_FS
2442struct f2fs_stat_info {
2443 struct list_head stat_list;
2444 struct f2fs_sb_info *sbi;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002445 int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
2446 int main_area_segs, main_area_sections, main_area_zones;
Chao Yu5b7ee372015-09-30 17:38:48 +08002447 unsigned long long hit_largest, hit_cached, hit_rbtree;
2448 unsigned long long hit_total, total_ext;
Jaegeuk Kimc00ba552015-12-31 15:24:14 -08002449 int ext_tree, zombie_tree, ext_node;
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07002450 int ndirty_node, ndirty_dent, ndirty_meta, ndirty_data, ndirty_imeta;
2451 int inmem_pages;
Jaegeuk Kim0f18b462016-05-20 11:10:10 -07002452 unsigned int ndirty_dirs, ndirty_files, ndirty_all;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002453 int nats, dirty_nats, sits, dirty_sits;
2454 int free_nids, avail_nids, alloc_nids;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002455 int total_count, utilization;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002456 int bg_gc, nr_wb_cp_data, nr_wb_data;
2457 int nr_flushing, nr_flushed, nr_discarding, nr_discarded;
2458 int nr_discard_cmd;
2459 unsigned int undiscard_blks;
2460 int inline_xattr, inline_inode, inline_dir, append, update, orphans;
2461 int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
Yunlei Hef83a2582016-08-18 21:01:18 +08002462 unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002463 unsigned int bimodal, avg_vblocks;
2464 int util_free, util_valid, util_invalid;
2465 int rsvd_segs, overp_segs;
2466 int dirty_count, node_pages, meta_pages;
Jaegeuk Kim42190d22016-01-09 13:45:17 -08002467 int prefree_count, call_count, cp_count, bg_cp_count;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002468 int tot_segs, node_segs, data_segs, free_segs, free_secs;
Changman Leee1235982014-12-23 08:37:39 +09002469 int bg_node_segs, bg_data_segs;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002470 int tot_blks, data_blks, node_blks;
Changman Leee1235982014-12-23 08:37:39 +09002471 int bg_data_blks, bg_node_blks;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002472 int curseg[NR_CURSEG_TYPE];
2473 int cursec[NR_CURSEG_TYPE];
2474 int curzone[NR_CURSEG_TYPE];
2475
2476 unsigned int segment_count[2];
2477 unsigned int block_count[2];
Changman Leeb9a2c252014-12-24 02:16:54 +09002478 unsigned int inplace_count;
Chao Yu9edcdab2015-09-11 14:43:52 +08002479 unsigned long long base_mem, cache_mem, page_mem;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002480};
2481
Gu Zheng963d4f72013-07-12 14:47:11 +08002482static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
2483{
Chris Fries6c311ec2014-01-17 14:44:39 -06002484 return (struct f2fs_stat_info *)sbi->stat_info;
Gu Zheng963d4f72013-07-12 14:47:11 +08002485}
2486
Changman Lee942e0be2014-02-13 15:12:29 +09002487#define stat_inc_cp_count(si) ((si)->cp_count++)
Jaegeuk Kim42190d22016-01-09 13:45:17 -08002488#define stat_inc_bg_cp_count(si) ((si)->bg_cp_count++)
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002489#define stat_inc_call_count(si) ((si)->call_count++)
2490#define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
Chao Yu33fbd512015-12-17 17:14:44 +08002491#define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++)
2492#define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--)
Chao Yu5b7ee372015-09-30 17:38:48 +08002493#define stat_inc_total_hit(sbi) (atomic64_inc(&(sbi)->total_hit_ext))
2494#define stat_inc_rbtree_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_rbtree))
2495#define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest))
2496#define stat_inc_cached_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_cached))
Chao Yud5e8f6c2015-07-15 17:28:53 +08002497#define stat_inc_inline_xattr(inode) \
2498 do { \
2499 if (f2fs_has_inline_xattr(inode)) \
2500 (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \
2501 } while (0)
2502#define stat_dec_inline_xattr(inode) \
2503 do { \
2504 if (f2fs_has_inline_xattr(inode)) \
2505 (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \
2506 } while (0)
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09002507#define stat_inc_inline_inode(inode) \
2508 do { \
2509 if (f2fs_has_inline_data(inode)) \
Chao Yu03e14d52014-12-08 19:08:20 +08002510 (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09002511 } while (0)
2512#define stat_dec_inline_inode(inode) \
2513 do { \
2514 if (f2fs_has_inline_data(inode)) \
Chao Yu03e14d52014-12-08 19:08:20 +08002515 (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09002516 } while (0)
Jaegeuk Kim3289c062014-10-13 20:00:16 -07002517#define stat_inc_inline_dir(inode) \
2518 do { \
2519 if (f2fs_has_inline_dentry(inode)) \
Chao Yu03e14d52014-12-08 19:08:20 +08002520 (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
Jaegeuk Kim3289c062014-10-13 20:00:16 -07002521 } while (0)
2522#define stat_dec_inline_dir(inode) \
2523 do { \
2524 if (f2fs_has_inline_dentry(inode)) \
Chao Yu03e14d52014-12-08 19:08:20 +08002525 (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
Jaegeuk Kim3289c062014-10-13 20:00:16 -07002526 } while (0)
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09002527#define stat_inc_seg_type(sbi, curseg) \
2528 ((sbi)->segment_count[(curseg)->alloc_type]++)
2529#define stat_inc_block_count(sbi, curseg) \
2530 ((sbi)->block_count[(curseg)->alloc_type]++)
Changman Leeb9a2c252014-12-24 02:16:54 +09002531#define stat_inc_inplace_blocks(sbi) \
2532 (atomic_inc(&(sbi)->inplace_count))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002533#define stat_inc_atomic_write(inode) \
2534 (atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
2535#define stat_dec_atomic_write(inode) \
2536 (atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
2537#define stat_update_max_atomic_write(inode) \
2538 do { \
2539 int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt); \
2540 int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt); \
2541 if (cur > max) \
2542 atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur); \
2543 } while (0)
2544#define stat_inc_volatile_write(inode) \
2545 (atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
2546#define stat_dec_volatile_write(inode) \
2547 (atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
2548#define stat_update_max_volatile_write(inode) \
2549 do { \
2550 int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt); \
2551 int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt); \
2552 if (cur > max) \
2553 atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur); \
2554 } while (0)
Changman Leee1235982014-12-23 08:37:39 +09002555#define stat_inc_seg_count(sbi, type, gc_type) \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002556 do { \
Gu Zheng963d4f72013-07-12 14:47:11 +08002557 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002558 si->tot_segs++; \
2559 if ((type) == SUM_TYPE_DATA) { \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002560 si->data_segs++; \
Changman Leee1235982014-12-23 08:37:39 +09002561 si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
2562 } else { \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002563 si->node_segs++; \
Changman Leee1235982014-12-23 08:37:39 +09002564 si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
2565 } \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002566 } while (0)
2567
2568#define stat_inc_tot_blk_count(si, blks) \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002569 ((si)->tot_blks += (blks))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002570
Changman Leee1235982014-12-23 08:37:39 +09002571#define stat_inc_data_blk_count(sbi, blks, gc_type) \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002572 do { \
Gu Zheng963d4f72013-07-12 14:47:11 +08002573 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002574 stat_inc_tot_blk_count(si, blks); \
2575 si->data_blks += (blks); \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002576 si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002577 } while (0)
2578
Changman Leee1235982014-12-23 08:37:39 +09002579#define stat_inc_node_blk_count(sbi, blks, gc_type) \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002580 do { \
Gu Zheng963d4f72013-07-12 14:47:11 +08002581 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002582 stat_inc_tot_blk_count(si, blks); \
2583 si->node_blks += (blks); \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002584 si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002585 } while (0)
2586
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002587int f2fs_build_stats(struct f2fs_sb_info *sbi);
2588void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
Chao Yu787c7b8c2015-10-29 09:13:04 +08002589int __init f2fs_create_root_stats(void);
Namjae Jeon4589d252013-01-15 19:58:47 +09002590void f2fs_destroy_root_stats(void);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002591#else
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002592#define stat_inc_cp_count(si) do { } while (0)
2593#define stat_inc_bg_cp_count(si) do { } while (0)
2594#define stat_inc_call_count(si) do { } while (0)
2595#define stat_inc_bggc_count(si) do { } while (0)
2596#define stat_inc_dirty_inode(sbi, type) do { } while (0)
2597#define stat_dec_dirty_inode(sbi, type) do { } while (0)
2598#define stat_inc_total_hit(sb) do { } while (0)
2599#define stat_inc_rbtree_node_hit(sb) do { } while (0)
2600#define stat_inc_largest_node_hit(sbi) do { } while (0)
2601#define stat_inc_cached_node_hit(sbi) do { } while (0)
2602#define stat_inc_inline_xattr(inode) do { } while (0)
2603#define stat_dec_inline_xattr(inode) do { } while (0)
2604#define stat_inc_inline_inode(inode) do { } while (0)
2605#define stat_dec_inline_inode(inode) do { } while (0)
2606#define stat_inc_inline_dir(inode) do { } while (0)
2607#define stat_dec_inline_dir(inode) do { } while (0)
2608#define stat_inc_atomic_write(inode) do { } while (0)
2609#define stat_dec_atomic_write(inode) do { } while (0)
2610#define stat_update_max_atomic_write(inode) do { } while (0)
2611#define stat_inc_volatile_write(inode) do { } while (0)
2612#define stat_dec_volatile_write(inode) do { } while (0)
2613#define stat_update_max_volatile_write(inode) do { } while (0)
2614#define stat_inc_seg_type(sbi, curseg) do { } while (0)
2615#define stat_inc_block_count(sbi, curseg) do { } while (0)
2616#define stat_inc_inplace_blocks(sbi) do { } while (0)
2617#define stat_inc_seg_count(sbi, type, gc_type) do { } while (0)
2618#define stat_inc_tot_blk_count(si, blks) do { } while (0)
2619#define stat_inc_data_blk_count(sbi, blks, gc_type) do { } while (0)
2620#define stat_inc_node_blk_count(sbi, blks, gc_type) do { } while (0)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002621
2622static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
2623static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
Chao Yu787c7b8c2015-10-29 09:13:04 +08002624static inline int __init f2fs_create_root_stats(void) { return 0; }
Namjae Jeon4589d252013-01-15 19:58:47 +09002625static inline void f2fs_destroy_root_stats(void) { }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002626#endif
2627
2628extern const struct file_operations f2fs_dir_operations;
2629extern const struct file_operations f2fs_file_operations;
2630extern const struct inode_operations f2fs_file_inode_operations;
2631extern const struct address_space_operations f2fs_dblock_aops;
2632extern const struct address_space_operations f2fs_node_aops;
2633extern const struct address_space_operations f2fs_meta_aops;
2634extern const struct inode_operations f2fs_dir_inode_operations;
2635extern const struct inode_operations f2fs_symlink_inode_operations;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07002636extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002637extern const struct inode_operations f2fs_special_inode_operations;
Jaegeuk Kim29e70432015-02-10 16:23:12 -08002638extern struct kmem_cache *inode_entry_slab;
Huajun Li1001b342013-11-10 23:13:16 +08002639
Huajun Lie18c65b2013-11-10 23:13:19 +08002640/*
2641 * inline.c
2642 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002643bool f2fs_may_inline_data(struct inode *inode);
2644bool f2fs_may_inline_dentry(struct inode *inode);
2645void read_inline_data(struct page *page, struct page *ipage);
2646void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
2647int f2fs_read_inline_data(struct inode *inode, struct page *page);
2648int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
2649int f2fs_convert_inline_inode(struct inode *inode);
2650int f2fs_write_inline_data(struct inode *inode, struct page *page);
2651bool recover_inline_data(struct inode *inode, struct page *npage);
2652struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
2653 struct fscrypt_name *fname, struct page **res_page);
2654int make_empty_inline_dir(struct inode *inode, struct inode *parent,
2655 struct page *ipage);
2656int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
2657 const struct qstr *orig_name,
2658 struct inode *inode, nid_t ino, umode_t mode);
2659void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
2660 struct inode *dir, struct inode *inode);
2661bool f2fs_empty_inline_dir(struct inode *dir);
2662int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
2663 struct fscrypt_str *fstr);
2664int f2fs_inline_data_fiemap(struct inode *inode,
2665 struct fiemap_extent_info *fieinfo,
2666 __u64 start, __u64 len);
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002667
2668/*
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002669 * shrinker.c
2670 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002671unsigned long f2fs_shrink_count(struct shrinker *shrink,
2672 struct shrink_control *sc);
2673unsigned long f2fs_shrink_scan(struct shrinker *shrink,
2674 struct shrink_control *sc);
2675void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
2676void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07002677
2678/*
Chao Yua28ef1f2015-07-08 17:59:36 +08002679 * extent_cache.c
2680 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002681struct rb_entry *__lookup_rb_tree(struct rb_root *root,
2682 struct rb_entry *cached_re, unsigned int ofs);
2683struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
2684 struct rb_root *root, struct rb_node **parent,
2685 unsigned int ofs);
2686struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
2687 struct rb_entry *cached_re, unsigned int ofs,
2688 struct rb_entry **prev_entry, struct rb_entry **next_entry,
2689 struct rb_node ***insert_p, struct rb_node **insert_parent,
2690 bool force);
2691bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
2692 struct rb_root *root);
2693unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
2694bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
2695void f2fs_drop_extent_tree(struct inode *inode);
2696unsigned int f2fs_destroy_extent_node(struct inode *inode);
2697void f2fs_destroy_extent_tree(struct inode *inode);
2698bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
2699 struct extent_info *ei);
2700void f2fs_update_extent_cache(struct dnode_of_data *dn);
Chao Yu19b2c302015-08-26 20:34:48 +08002701void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002702 pgoff_t fofs, block_t blkaddr, unsigned int len);
2703void init_extent_cache_info(struct f2fs_sb_info *sbi);
Chao Yua28ef1f2015-07-08 17:59:36 +08002704int __init create_extent_cache(void);
2705void destroy_extent_cache(void);
2706
2707/*
Chao Yu57fb30c2017-06-14 17:39:47 +08002708 * sysfs.c
2709 */
2710int __init f2fs_register_sysfs(void);
2711void f2fs_unregister_sysfs(void);
2712int f2fs_init_sysfs(struct f2fs_sb_info *sbi);
2713void f2fs_exit_sysfs(struct f2fs_sb_info *sbi);
2714
2715/*
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002716 * crypto support
2717 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002718static inline bool f2fs_encrypted_inode(struct inode *inode)
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002719{
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002720 return file_is_encrypt(inode);
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002721}
2722
2723static inline void f2fs_set_encrypted_inode(struct inode *inode)
2724{
2725#ifdef CONFIG_F2FS_FS_ENCRYPTION
2726 file_set_encrypt(inode);
2727#endif
2728}
2729
2730static inline bool f2fs_bio_encrypted(struct bio *bio)
2731{
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002732 return bio->bi_private != NULL;
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002733}
2734
2735static inline int f2fs_sb_has_crypto(struct super_block *sb)
2736{
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002737 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07002738}
Jaegeuk Kimf424f662015-04-20 15:19:06 -07002739
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002740static inline int f2fs_sb_mounted_blkzoned(struct super_block *sb)
Jaegeuk Kim52763a42016-06-13 09:47:48 -07002741{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002742 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_BLKZONED);
2743}
2744
2745#ifdef CONFIG_BLK_DEV_ZONED
2746static inline int get_blkz_type(struct f2fs_sb_info *sbi,
2747 struct block_device *bdev, block_t blkaddr)
2748{
2749 unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
2750 int i;
2751
2752 for (i = 0; i < sbi->s_ndevs; i++)
2753 if (FDEV(i).bdev == bdev)
2754 return FDEV(i).blkz_type[zno];
2755 return -EINVAL;
2756}
2757#endif
2758
2759static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
2760{
2761 struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
2762
2763 return blk_queue_discard(q) || f2fs_sb_mounted_blkzoned(sbi->sb);
Jaegeuk Kim52763a42016-06-13 09:47:48 -07002764}
2765
2766static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
2767{
2768 clear_opt(sbi, ADAPTIVE);
2769 clear_opt(sbi, LFS);
2770
2771 switch (mt) {
2772 case F2FS_MOUNT_ADAPTIVE:
2773 set_opt(sbi, ADAPTIVE);
2774 break;
2775 case F2FS_MOUNT_LFS:
2776 set_opt(sbi, LFS);
2777 break;
2778 }
2779}
2780
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002781static inline bool f2fs_may_encrypt(struct inode *inode)
2782{
2783#ifdef CONFIG_F2FS_FS_ENCRYPTION
Al Viro886f56f2015-12-05 03:56:06 -05002784 umode_t mode = inode->i_mode;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002785
2786 return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
2787#else
2788 return 0;
2789#endif
2790}
2791
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002792#endif