blob: 735122f27f09b9651ae63226bdf6e73198cf4838 [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 Kim462d7622018-01-04 21:36:09 -080023#include <linux/cred.h>
Jaegeuk Kim39307a82015-09-22 13:50:47 -070024#include <linux/vmalloc.h>
Jaegeuk Kim740432f2015-08-14 11:43:56 -070025#include <linux/bio.h>
Jaegeuk Kimd0239e12016-01-08 16:57:48 -080026#include <linux/blkdev.h>
Chao Yu09c3a722017-07-09 00:13:07 +080027#include <linux/quotaops.h>
Keith Mok43b65732016-03-02 12:04:24 -080028#include <crypto/hash.h>
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090029
Dave Chinner16179292017-10-09 12:15:34 -070030#define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_F2FS_FS_ENCRYPTION)
31#include <linux/fscrypt.h>
32
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,
Chao Yuead52592017-11-30 19:28:18 +080048 FAULT_KVMALLOC,
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -070049 FAULT_PAGE_ALLOC,
Chao Yu58ddec82017-10-28 16:52:30 +080050 FAULT_PAGE_GET,
Chao Yub5db2de2017-10-28 16:52:31 +080051 FAULT_ALLOC_BIO,
Jaegeuk Kimcb789422016-04-29 16:29:22 -070052 FAULT_ALLOC_NID,
53 FAULT_ORPHAN,
54 FAULT_BLOCK,
55 FAULT_DIR_DEPTH,
Jaegeuk Kim53aa6bb2016-05-25 15:24:18 -070056 FAULT_EVICT_INODE,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070057 FAULT_TRUNCATE,
Chao Yu8b038c72016-09-18 23:30:07 +080058 FAULT_IO,
Chao Yu0f348022016-09-26 19:45:55 +080059 FAULT_CHECKPOINT,
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070060 FAULT_MAX,
61};
62
Sheng Yong08796892016-05-16 12:38:50 +080063struct f2fs_fault_info {
64 atomic_t inject_ops;
65 unsigned int inject_rate;
66 unsigned int inject_type;
67};
68
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070069extern char *fault_name[FAULT_MAX];
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070070#define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -070071#endif
72
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090073/*
74 * For mount options
75 */
76#define F2FS_MOUNT_BG_GC 0x00000001
77#define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
78#define F2FS_MOUNT_DISCARD 0x00000004
79#define F2FS_MOUNT_NOHEAP 0x00000008
80#define F2FS_MOUNT_XATTR_USER 0x00000010
81#define F2FS_MOUNT_POSIX_ACL 0x00000020
82#define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
Jaegeuk Kim444c5802013-08-08 15:16:22 +090083#define F2FS_MOUNT_INLINE_XATTR 0x00000080
Huajun Li1001b342013-11-10 23:13:16 +080084#define F2FS_MOUNT_INLINE_DATA 0x00000100
Chao Yu34d67de2014-09-24 18:15:19 +080085#define F2FS_MOUNT_INLINE_DENTRY 0x00000200
86#define F2FS_MOUNT_FLUSH_MERGE 0x00000400
87#define F2FS_MOUNT_NOBARRIER 0x00000800
Jaegeuk Kimd5053a342014-10-30 22:47:03 -070088#define F2FS_MOUNT_FASTBOOT 0x00001000
Chao Yu89672152015-02-05 17:55:51 +080089#define F2FS_MOUNT_EXTENT_CACHE 0x00002000
Jaegeuk Kim6aefd932015-10-05 11:02:54 -070090#define F2FS_MOUNT_FORCE_FG_GC 0x00004000
Chao Yu343f40f2015-12-16 13:12:16 +080091#define F2FS_MOUNT_DATA_FLUSH 0x00008000
Jaegeuk Kim73faec42016-04-29 15:34:32 -070092#define F2FS_MOUNT_FAULT_INJECTION 0x00010000
Jaegeuk Kim36abef42016-06-03 19:29:38 -070093#define F2FS_MOUNT_ADAPTIVE 0x00020000
94#define F2FS_MOUNT_LFS 0x00040000
Chao Yu09c3a722017-07-09 00:13:07 +080095#define F2FS_MOUNT_USRQUOTA 0x00080000
96#define F2FS_MOUNT_GRPQUOTA 0x00100000
Chao Yu5647b302017-07-26 00:01:41 +080097#define F2FS_MOUNT_PRJQUOTA 0x00200000
Chao Yu41ad73f2017-08-08 10:54:31 +080098#define F2FS_MOUNT_QUOTA 0x00400000
Chao Yu50ffaa92017-09-06 21:59:50 +080099#define F2FS_MOUNT_INLINE_XATTR_SIZE 0x00800000
Jaegeuk Kim5fee5402017-12-27 15:05:52 -0800100#define F2FS_MOUNT_RESERVE_ROOT 0x01000000
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900101
Chao Yue9a50e62018-03-08 14:22:56 +0800102#define F2FS_OPTION(sbi) ((sbi)->mount_opt)
103#define clear_opt(sbi, option) (F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
104#define set_opt(sbi, option) (F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
105#define test_opt(sbi, option) (F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900106
107#define ver_after(a, b) (typecheck(unsigned long long, a) && \
108 typecheck(unsigned long long, b) && \
109 ((long long)((a) - (b)) > 0))
110
Jaegeuk Kima9841c42013-05-24 12:41:04 +0900111typedef u32 block_t; /*
112 * should not change u32, since it is the on-disk block
113 * address format, __le32.
114 */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900115typedef u32 nid_t;
116
117struct f2fs_mount_info {
Chao Yue9a50e62018-03-08 14:22:56 +0800118 unsigned int opt;
119 int write_io_size_bits; /* Write IO size bits */
120 block_t root_reserved_blocks; /* root reserved blocks */
121 kuid_t s_resuid; /* reserved blocks for uid */
122 kgid_t s_resgid; /* reserved blocks for gid */
123 int active_logs; /* # of active logs */
124 int inline_xattr_size; /* inline xattr size */
125#ifdef CONFIG_F2FS_FAULT_INJECTION
126 struct f2fs_fault_info fault_info; /* For fault injection */
127#endif
128#ifdef CONFIG_QUOTA
129 /* Names of quota files with journalled quota */
130 char *s_qf_names[MAXQUOTAS];
131 int s_jquota_fmt; /* Format of quota to use */
132#endif
133 /* For which write hints are passed down to block layer */
134 int whint_mode;
135 int alloc_mode; /* segment allocation policy */
136 int fsync_mode; /* fsync policy */
Sheng Yongaa5bcfd2018-03-15 18:51:42 +0800137 bool test_dummy_encryption; /* test dummy encryption */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900138};
139
Chao Yufbcf9312017-07-19 00:19:06 +0800140#define F2FS_FEATURE_ENCRYPT 0x0001
141#define F2FS_FEATURE_BLKZONED 0x0002
142#define F2FS_FEATURE_ATOMIC_WRITE 0x0004
143#define F2FS_FEATURE_EXTRA_ATTR 0x0008
Chao Yu5647b302017-07-26 00:01:41 +0800144#define F2FS_FEATURE_PRJQUOTA 0x0010
Chao Yu43101252017-07-31 20:19:09 +0800145#define F2FS_FEATURE_INODE_CHKSUM 0x0020
Chao Yu50ffaa92017-09-06 21:59:50 +0800146#define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR 0x0040
Jaegeuk Kimd4c292d2017-10-05 21:03:06 -0700147#define F2FS_FEATURE_QUOTA_INO 0x0080
Chao Yu71f8f042018-01-25 14:54:42 +0800148#define F2FS_FEATURE_INODE_CRTIME 0x0100
Sheng Yong9b880fe2018-03-15 18:51:41 +0800149#define F2FS_FEATURE_LOST_FOUND 0x0200
Eric Biggers29cead52018-03-28 11:15:09 -0700150#define F2FS_FEATURE_VERITY 0x0400 /* reserved */
Jaegeuk Kimcde4de12015-04-20 13:57:51 -0700151
Jaegeuk Kim76f105a2015-04-13 15:10:36 -0700152#define F2FS_HAS_FEATURE(sb, mask) \
153 ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
154#define F2FS_SET_FEATURE(sb, mask) \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700155 (F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
Jaegeuk Kim76f105a2015-04-13 15:10:36 -0700156#define F2FS_CLEAR_FEATURE(sb, mask) \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700157 (F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
Jaegeuk Kim76f105a2015-04-13 15:10:36 -0700158
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900159/*
Jaegeuk Kim462d7622018-01-04 21:36:09 -0800160 * Default values for user and/or group using reserved blocks
161 */
162#define F2FS_DEF_RESUID 0
163#define F2FS_DEF_RESGID 0
164
165/*
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900166 * For checkpoint manager
167 */
168enum {
169 NAT_BITMAP,
170 SIT_BITMAP
171};
172
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700173#define CP_UMOUNT 0x00000001
174#define CP_FASTBOOT 0x00000002
175#define CP_SYNC 0x00000004
176#define CP_RECOVERY 0x00000008
177#define CP_DISCARD 0x00000010
178#define CP_TRIMMED 0x00000020
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -0700179
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700180#define MAX_DISCARD_BLOCKS(sbi) BLKS_PER_SEC(sbi)
Chao Yu69a59672017-10-04 09:08:33 +0800181#define DEF_MAX_DISCARD_REQUEST 8 /* issue 8 discards per round */
Jaegeuk Kim8f6a2e72018-05-24 13:57:26 -0700182#define DEF_MAX_DISCARD_LEN 512 /* Max. 2MB per discard */
Chao Yuefe24da2017-08-07 23:09:56 +0800183#define DEF_MIN_DISCARD_ISSUE_TIME 50 /* 50 ms, if exists */
Yunlei He8d74ddc2018-04-08 15:11:11 +0800184#define DEF_MID_DISCARD_ISSUE_TIME 500 /* 500 ms, if device busy */
Chao Yuefe24da2017-08-07 23:09:56 +0800185#define DEF_MAX_DISCARD_ISSUE_TIME 60000 /* 60 s, if no candidates */
Jaegeuk Kim41059092018-05-29 09:58:42 -0700186#define DEF_DISCARD_URGENT_UTIL 80 /* do more discard over 80% */
Jaegeuk Kim60b99b42015-10-05 14:49:57 -0700187#define DEF_CP_INTERVAL 60 /* 60 secs */
Chao Yudcf25fe2016-07-15 19:25:47 +0800188#define DEF_IDLE_INTERVAL 5 /* 5 secs */
Jaegeuk Kimbba681c2015-01-26 17:41:23 -0800189
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -0700190struct cp_control {
191 int reason;
Jaegeuk Kim4b2fecc2014-09-20 22:06:39 -0700192 __u64 trim_start;
193 __u64 trim_end;
194 __u64 trim_minlen;
Jaegeuk Kim75ab4cb2014-09-20 21:57:51 -0700195};
196
Chao Yu662befd2014-02-07 16:11:53 +0800197/*
Chao Yu81c1a0f12014-02-27 19:12:24 +0800198 * For CP/NAT/SIT/SSA readahead
Chao Yu662befd2014-02-07 16:11:53 +0800199 */
200enum {
201 META_CP,
202 META_NAT,
Chao Yu81c1a0f12014-02-27 19:12:24 +0800203 META_SIT,
Jaegeuk Kim4c521f492014-09-11 13:49:55 -0700204 META_SSA,
205 META_POR,
Chao Yu662befd2014-02-07 16:11:53 +0800206};
207
Jaegeuk Kim6451e042014-07-25 15:47:17 -0700208/* for the list of ino */
209enum {
210 ORPHAN_INO, /* for orphan ino list */
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700211 APPEND_INO, /* for append ino list */
212 UPDATE_INO, /* for update ino list */
Jaegeuk Kim211cb7b2017-12-28 08:09:44 -0800213 TRANS_DIR_INO, /* for trasactions dir ino list */
Chao Yuf014be82017-09-29 13:59:38 +0800214 FLUSH_INO, /* for multiple device flushing */
Jaegeuk Kim6451e042014-07-25 15:47:17 -0700215 MAX_INO_ENTRY, /* max. list */
216};
217
218struct ino_entry {
Chao Yuf014be82017-09-29 13:59:38 +0800219 struct list_head list; /* list head */
220 nid_t ino; /* inode number */
221 unsigned int dirty_device; /* dirty device bitmap */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900222};
223
Chao Yu2710fd72015-12-15 13:30:45 +0800224/* for the list of inodes to be GCed */
Chao Yu06292072014-12-29 15:56:18 +0800225struct inode_entry {
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900226 struct list_head list; /* list head */
227 struct inode *inode; /* vfs inode pointer */
228};
229
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700230/* for the bitmap indicate blocks to be discarded */
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +0900231struct discard_entry {
232 struct list_head list; /* list head */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700233 block_t start_blkaddr; /* start blockaddr of current segment */
234 unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +0900235};
236
Chao Yuefe24da2017-08-07 23:09:56 +0800237/* default discard granularity of inner discard thread, unit: block count */
238#define DEFAULT_DISCARD_GRANULARITY 16
239
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700240/* max discard pend list number */
241#define MAX_PLIST_NUM 512
242#define plist_idx(blk_num) ((blk_num) >= MAX_PLIST_NUM ? \
243 (MAX_PLIST_NUM - 1) : (blk_num - 1))
244
245enum {
246 D_PREP,
247 D_SUBMIT,
248 D_DONE,
249};
250
251struct discard_info {
252 block_t lstart; /* logical start address */
253 block_t len; /* length */
254 block_t start; /* actual start address in dev */
255};
256
257struct discard_cmd {
258 struct rb_node rb_node; /* rb node located in rb-tree */
259 union {
260 struct {
261 block_t lstart; /* logical start address */
262 block_t len; /* length */
263 block_t start; /* actual start address in dev */
264 };
265 struct discard_info di; /* discard info */
266
267 };
268 struct list_head list; /* command list */
269 struct completion wait; /* compleation */
270 struct block_device *bdev; /* bdev */
271 unsigned short ref; /* reference count */
272 unsigned char state; /* state */
273 int error; /* bio error */
274};
275
Chao Yudaf437d2017-10-04 09:08:34 +0800276enum {
277 DPOLICY_BG,
278 DPOLICY_FORCE,
279 DPOLICY_FSTRIM,
280 DPOLICY_UMOUNT,
281 MAX_DPOLICY,
282};
283
Chao Yu69a59672017-10-04 09:08:33 +0800284struct discard_policy {
Chao Yudaf437d2017-10-04 09:08:34 +0800285 int type; /* type of discard */
Chao Yu69a59672017-10-04 09:08:33 +0800286 unsigned int min_interval; /* used for candidates exist */
Yunlei He8d74ddc2018-04-08 15:11:11 +0800287 unsigned int mid_interval; /* used for device busy */
Chao Yu69a59672017-10-04 09:08:33 +0800288 unsigned int max_interval; /* used for candidates not exist */
289 unsigned int max_requests; /* # of discards issued per round */
290 unsigned int io_aware_gran; /* minimum granularity discard not be aware of I/O */
291 bool io_aware; /* issue discard in idle time */
292 bool sync; /* submit discard with REQ_SYNC flag */
Chao Yudaf437d2017-10-04 09:08:34 +0800293 unsigned int granularity; /* discard granularity */
Chao Yu69a59672017-10-04 09:08:33 +0800294};
295
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700296struct discard_cmd_control {
297 struct task_struct *f2fs_issue_discard; /* discard thread */
298 struct list_head entry_list; /* 4KB discard entry list */
299 struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
300 struct list_head wait_list; /* store on-flushing entries */
Chao Yu28e10232017-10-04 09:08:32 +0800301 struct list_head fstrim_list; /* in-flight discard from fstrim */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700302 wait_queue_head_t discard_wait_queue; /* waiting queue for wake-up */
Chao Yuefe24da2017-08-07 23:09:56 +0800303 unsigned int discard_wake; /* to wake up discard thread */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700304 struct mutex cmd_lock;
305 unsigned int nr_discards; /* # of discards in the list */
306 unsigned int max_discards; /* max. discards to be issued */
Chao Yuefe24da2017-08-07 23:09:56 +0800307 unsigned int discard_granularity; /* discard granularity */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700308 unsigned int undiscard_blks; /* # of undiscard blocks */
309 atomic_t issued_discard; /* # of issued discard */
310 atomic_t issing_discard; /* # of issing discard */
311 atomic_t discard_cmd_cnt; /* # of cached cmd count */
312 struct rb_root root; /* root of discard rb-tree */
Chao Yu275b66b2016-08-29 23:58:34 +0800313};
314
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900315/* for the list of fsync inodes, used only during recovery */
316struct fsync_inode_entry {
317 struct list_head list; /* list head */
318 struct inode *inode; /* vfs inode pointer */
Jaegeuk Kimc52e1b12014-09-11 14:29:06 -0700319 block_t blkaddr; /* block address locating the last fsync */
320 block_t last_dentry; /* block address locating the last dentry */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900321};
322
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700323#define nats_in_cursum(jnl) (le16_to_cpu((jnl)->n_nats))
324#define sits_in_cursum(jnl) (le16_to_cpu((jnl)->n_sits))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900325
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700326#define nat_in_journal(jnl, i) ((jnl)->nat_j.entries[i].ne)
327#define nid_in_journal(jnl, i) ((jnl)->nat_j.entries[i].nid)
328#define sit_in_journal(jnl, i) ((jnl)->sit_j.entries[i].se)
329#define segno_in_journal(jnl, i) ((jnl)->sit_j.entries[i].segno)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900330
Chao Yudfc08a12016-02-14 18:50:40 +0800331#define MAX_NAT_JENTRIES(jnl) (NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
332#define MAX_SIT_JENTRIES(jnl) (SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700333
Chao Yudfc08a12016-02-14 18:50:40 +0800334static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900335{
Chao Yudfc08a12016-02-14 18:50:40 +0800336 int before = nats_in_cursum(journal);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700337
Chao Yudfc08a12016-02-14 18:50:40 +0800338 journal->n_nats = cpu_to_le16(before + i);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900339 return before;
340}
341
Chao Yudfc08a12016-02-14 18:50:40 +0800342static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900343{
Chao Yudfc08a12016-02-14 18:50:40 +0800344 int before = sits_in_cursum(journal);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700345
Chao Yudfc08a12016-02-14 18:50:40 +0800346 journal->n_sits = cpu_to_le16(before + i);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900347 return before;
348}
349
Chao Yudfc08a12016-02-14 18:50:40 +0800350static inline bool __has_cursum_space(struct f2fs_journal *journal,
351 int size, int type)
Chao Yu184a5cd2014-09-04 18:13:01 +0800352{
353 if (type == NAT_JOURNAL)
Chao Yudfc08a12016-02-14 18:50:40 +0800354 return size <= MAX_NAT_JENTRIES(journal);
355 return size <= MAX_SIT_JENTRIES(journal);
Chao Yu184a5cd2014-09-04 18:13:01 +0800356}
357
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900358/*
Namjae Jeone9750822013-02-04 23:41:41 +0900359 * ioctl commands
360 */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700361#define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
362#define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
Chao Yud49f3e82015-01-23 20:36:04 +0800363#define F2FS_IOC_GETVERSION FS_IOC_GETVERSION
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700364
365#define F2FS_IOCTL_MAGIC 0xf5
366#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
367#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
Jaegeuk Kim02a13352014-10-06 16:11:16 -0700368#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800369#define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
370#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700371#define F2FS_IOC_GARBAGE_COLLECT _IOW(F2FS_IOCTL_MAGIC, 6, __u32)
Chao Yu456b88e2015-10-05 22:24:19 +0800372#define F2FS_IOC_WRITE_CHECKPOINT _IO(F2FS_IOCTL_MAGIC, 7)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700373#define F2FS_IOC_DEFRAGMENT _IOWR(F2FS_IOCTL_MAGIC, 8, \
374 struct f2fs_defragment)
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -0700375#define F2FS_IOC_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
376 struct f2fs_move_range)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700377#define F2FS_IOC_FLUSH_DEVICE _IOW(F2FS_IOCTL_MAGIC, 10, \
378 struct f2fs_flush_device)
Jaegeuk Kim0bb53062017-06-15 16:44:42 -0700379#define F2FS_IOC_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11, \
380 struct f2fs_gc_range)
Jaegeuk Kim82925bb2017-07-21 12:58:59 -0700381#define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, __u32)
Jaegeuk Kim9ce3d6b2017-12-07 16:25:39 -0800382#define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32)
383#define F2FS_IOC_GET_PIN_FILE _IOR(F2FS_IOCTL_MAGIC, 14, __u32)
Chao Yu6bce9632018-01-11 14:42:30 +0800384#define F2FS_IOC_PRECACHE_EXTENTS _IO(F2FS_IOCTL_MAGIC, 15)
Namjae Jeone9750822013-02-04 23:41:41 +0900385
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700386#define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
387#define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
388#define F2FS_IOC_GET_ENCRYPTION_PWSALT FS_IOC_GET_ENCRYPTION_PWSALT
Jaegeuk Kimf424f662015-04-20 15:19:06 -0700389
Jaegeuk Kim1abff932015-01-08 19:15:53 -0800390/*
391 * should be same as XFS_IOC_GOINGDOWN.
392 * Flags for going down operation used by FS_IOC_GOINGDOWN
393 */
394#define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
395#define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
396#define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
397#define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
Jaegeuk Kimc912a822015-10-07 09:46:37 -0700398#define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */
Jaegeuk Kim1abff932015-01-08 19:15:53 -0800399
Namjae Jeone9750822013-02-04 23:41:41 +0900400#if defined(__KERNEL__) && defined(CONFIG_COMPAT)
401/*
402 * ioctl commands in 32 bit emulation
403 */
Chao Yu04ef4b62015-11-10 18:44:20 +0800404#define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
405#define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
406#define F2FS_IOC32_GETVERSION FS_IOC32_GETVERSION
Namjae Jeone9750822013-02-04 23:41:41 +0900407#endif
408
Jaegeuk Kim0bb53062017-06-15 16:44:42 -0700409struct f2fs_gc_range {
410 u32 sync;
411 u64 start;
412 u64 len;
413};
414
Chao Yud323d002015-10-27 09:53:45 +0800415struct f2fs_defragment {
416 u64 start;
417 u64 len;
418};
419
Jaegeuk Kim4dd6f972016-07-08 15:16:47 -0700420struct f2fs_move_range {
421 u32 dst_fd; /* destination fd */
422 u64 pos_in; /* start position in src_fd */
423 u64 pos_out; /* start position in dst_fd */
424 u64 len; /* size to move */
425};
426
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700427struct f2fs_flush_device {
428 u32 dev_num; /* device number to flush */
429 u32 segments; /* # of segments to flush */
430};
431
Chao Yue84f88e2017-07-19 00:19:05 +0800432/* for inline stuff */
433#define DEF_INLINE_RESERVED_SIZE 1
Chao Yu50ffaa92017-09-06 21:59:50 +0800434#define DEF_MIN_INLINE_SIZE 1
Chao Yufbcf9312017-07-19 00:19:06 +0800435static inline int get_extra_isize(struct inode *inode);
Chao Yu50ffaa92017-09-06 21:59:50 +0800436static inline int get_inline_xattr_addrs(struct inode *inode);
Chao Yu50ffaa92017-09-06 21:59:50 +0800437#define MAX_INLINE_DATA(inode) (sizeof(__le32) * \
438 (CUR_ADDRS_PER_INODE(inode) - \
Chao Yu3aa46e22018-01-17 16:31:36 +0800439 get_inline_xattr_addrs(inode) - \
Chao Yu50ffaa92017-09-06 21:59:50 +0800440 DEF_INLINE_RESERVED_SIZE))
Chao Yue84f88e2017-07-19 00:19:05 +0800441
442/* for inline dir */
443#define NR_INLINE_DENTRY(inode) (MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
444 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
445 BITS_PER_BYTE + 1))
446#define INLINE_DENTRY_BITMAP_SIZE(inode) ((NR_INLINE_DENTRY(inode) + \
447 BITS_PER_BYTE - 1) / BITS_PER_BYTE)
448#define INLINE_RESERVED_SIZE(inode) (MAX_INLINE_DATA(inode) - \
449 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
450 NR_INLINE_DENTRY(inode) + \
451 INLINE_DENTRY_BITMAP_SIZE(inode)))
452
Namjae Jeone9750822013-02-04 23:41:41 +0900453/*
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900454 * For INODE and NODE manager
455 */
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700456/* for directory operations */
457struct f2fs_dentry_ptr {
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700458 struct inode *inode;
Chao Yub75c5f62017-07-16 15:08:54 +0800459 void *bitmap;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700460 struct f2fs_dir_entry *dentry;
461 __u8 (*filename)[F2FS_SLOT_LEN];
462 int max;
Chao Yub75c5f62017-07-16 15:08:54 +0800463 int nr_bitmap;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700464};
465
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700466static inline void make_dentry_ptr_block(struct inode *inode,
467 struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700468{
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700469 d->inode = inode;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700470 d->max = NR_DENTRY_IN_BLOCK;
Chao Yub75c5f62017-07-16 15:08:54 +0800471 d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
Yunlong Song270deeb2018-04-02 20:22:20 +0800472 d->bitmap = t->dentry_bitmap;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700473 d->dentry = t->dentry;
474 d->filename = t->filename;
475}
Jaegeuk Kimd8c68222015-04-27 16:26:24 -0700476
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700477static inline void make_dentry_ptr_inline(struct inode *inode,
Chao Yue84f88e2017-07-19 00:19:05 +0800478 struct f2fs_dentry_ptr *d, void *t)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700479{
Chao Yue84f88e2017-07-19 00:19:05 +0800480 int entry_cnt = NR_INLINE_DENTRY(inode);
481 int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
482 int reserved_size = INLINE_RESERVED_SIZE(inode);
483
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700484 d->inode = inode;
Chao Yue84f88e2017-07-19 00:19:05 +0800485 d->max = entry_cnt;
486 d->nr_bitmap = bitmap_size;
487 d->bitmap = t;
488 d->dentry = t + bitmap_size + reserved_size;
489 d->filename = t + bitmap_size + reserved_size +
490 SIZE_OF_DIR_ENTRY * entry_cnt;
Jaegeuk Kim7b3cd7d2014-10-18 22:52:52 -0700491}
492
Jaegeuk Kimdbe6a5f2013-08-09 08:14:06 +0900493/*
494 * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
495 * as its node offset to distinguish from index node blocks.
496 * But some bits are used to mark the node block.
497 */
498#define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
499 >> OFFSET_BIT_SHIFT)
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900500enum {
501 ALLOC_NODE, /* allocate a new node page if needed */
502 LOOKUP_NODE, /* look up a node without readahead */
503 LOOKUP_NODE_RA, /*
504 * look up a node with readahead called
Chao Yu4f4124d2013-12-21 18:02:14 +0800505 * by get_data_block.
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900506 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900507};
508
Jaegeuk Kima6db67f2015-08-10 15:01:12 -0700509#define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900510
Chao Yu817202d92014-04-28 17:59:43 +0800511#define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
512
Chao Yu13054c52015-02-05 17:52:58 +0800513/* vector size for gang look-up from extent cache that consists of radix tree */
514#define EXT_TREE_VEC_SIZE 64
515
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900516/* for in-memory extent cache entry */
Chao Yu13054c52015-02-05 17:52:58 +0800517#define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */
518
519/* number of extent info in extent cache we try to shrink */
520#define EXTENT_CACHE_SHRINK_NUMBER 128
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900521
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700522struct rb_entry {
523 struct rb_node rb_node; /* rb node located in rb-tree */
524 unsigned int ofs; /* start offset of the entry */
525 unsigned int len; /* length of the entry */
526};
527
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900528struct extent_info {
Chao Yu13054c52015-02-05 17:52:58 +0800529 unsigned int fofs; /* start offset in a file */
Chao Yu13054c52015-02-05 17:52:58 +0800530 unsigned int len; /* length of the extent */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700531 u32 blk; /* start block address of the extent */
Chao Yu13054c52015-02-05 17:52:58 +0800532};
533
534struct extent_node {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700535 struct rb_node rb_node;
536 union {
537 struct {
538 unsigned int fofs;
539 unsigned int len;
540 u32 blk;
541 };
542 struct extent_info ei; /* extent info */
543
544 };
Chao Yu13054c52015-02-05 17:52:58 +0800545 struct list_head list; /* node in global extent list of sbi */
Hou Pengyang201ef5e2016-01-26 12:56:26 +0000546 struct extent_tree *et; /* extent tree pointer */
Chao Yu13054c52015-02-05 17:52:58 +0800547};
548
549struct extent_tree {
550 nid_t ino; /* inode number */
551 struct rb_root root; /* root of extent info rb-tree */
Chao Yu62c8af62015-02-05 18:01:39 +0800552 struct extent_node *cached_en; /* recently accessed extent node */
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700553 struct extent_info largest; /* largested extent info */
Jaegeuk Kim137d09f2015-12-31 15:02:16 -0800554 struct list_head list; /* to be used by sbi->zombie_list */
Chao Yu13054c52015-02-05 17:52:58 +0800555 rwlock_t lock; /* protect extent info rb-tree */
Chao Yu68e35382016-01-08 20:22:52 +0800556 atomic_t node_cnt; /* # of extent node in rb-tree*/
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900557};
558
559/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700560 * This structure is taken from ext4_map_blocks.
561 *
562 * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
563 */
564#define F2FS_MAP_NEW (1 << BH_New)
565#define F2FS_MAP_MAPPED (1 << BH_Mapped)
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700566#define F2FS_MAP_UNWRITTEN (1 << BH_Unwritten)
567#define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
568 F2FS_MAP_UNWRITTEN)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700569
570struct f2fs_map_blocks {
571 block_t m_pblk;
572 block_t m_lblk;
573 unsigned int m_len;
574 unsigned int m_flags;
Chao Yuda859852016-01-26 15:42:58 +0800575 pgoff_t *m_next_pgofs; /* point next possible non-hole pgofs */
Chao Yu6bce9632018-01-11 14:42:30 +0800576 pgoff_t *m_next_extent; /* point to next possible extent */
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +0900577 int m_seg_type;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700578};
579
Chao Yue2b4e2b2015-08-19 19:11:19 +0800580/* for flag in get_data_block */
Qiuyang Sun9afa1592017-08-09 17:27:30 +0800581enum {
582 F2FS_GET_BLOCK_DEFAULT,
583 F2FS_GET_BLOCK_FIEMAP,
584 F2FS_GET_BLOCK_BMAP,
585 F2FS_GET_BLOCK_PRE_DIO,
586 F2FS_GET_BLOCK_PRE_AIO,
Chao Yu6bce9632018-01-11 14:42:30 +0800587 F2FS_GET_BLOCK_PRECACHE,
Qiuyang Sun9afa1592017-08-09 17:27:30 +0800588};
Chao Yue2b4e2b2015-08-19 19:11:19 +0800589
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700590/*
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900591 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
592 */
593#define FADVISE_COLD_BIT 0x01
Jaegeuk Kim354a3392013-06-14 08:52:35 +0900594#define FADVISE_LOST_PINO_BIT 0x02
Jaegeuk Kimcde4de12015-04-20 13:57:51 -0700595#define FADVISE_ENCRYPT_BIT 0x04
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700596#define FADVISE_ENC_NAME_BIT 0x08
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700597#define FADVISE_KEEP_SIZE_BIT 0x10
Chao Yuac734c42018-02-28 17:07:27 +0800598#define FADVISE_HOT_BIT 0x20
Eric Biggers29cead52018-03-28 11:15:09 -0700599#define FADVISE_VERITY_BIT 0x40 /* reserved */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900600
Jaegeuk Kimb5492af72015-04-20 13:44:41 -0700601#define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
602#define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
603#define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
604#define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
605#define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
606#define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
Jaegeuk Kimcde4de12015-04-20 13:57:51 -0700607#define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT)
608#define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
609#define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
Jaegeuk Kime7d55452015-04-29 17:02:18 -0700610#define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT)
611#define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700612#define file_keep_isize(inode) is_file(inode, FADVISE_KEEP_SIZE_BIT)
613#define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
Chao Yuac734c42018-02-28 17:07:27 +0800614#define file_is_hot(inode) is_file(inode, FADVISE_HOT_BIT)
615#define file_set_hot(inode) set_file(inode, FADVISE_HOT_BIT)
616#define file_clear_hot(inode) clear_file(inode, FADVISE_HOT_BIT)
Jaegeuk Kimcde4de12015-04-20 13:57:51 -0700617
Jaegeuk Kimab9fa662014-02-27 20:09:05 +0900618#define DEF_DIR_LEVEL 0
619
Chao Yucaf10c62018-05-07 20:28:54 +0800620enum {
621 GC_FAILURE_PIN,
622 GC_FAILURE_ATOMIC,
623 MAX_GC_FAILURE
624};
625
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900626struct f2fs_inode_info {
627 struct inode vfs_inode; /* serve a vfs inode */
628 unsigned long i_flags; /* keep an inode flags for ioctl */
629 unsigned char i_advise; /* use to give file attribute hints */
Jaegeuk Kim38431542014-02-27 18:20:00 +0900630 unsigned char i_dir_level; /* use for dentry level for large dir */
Chao Yucaf10c62018-05-07 20:28:54 +0800631 unsigned int i_current_depth; /* only for directory depth */
632 /* for gc failure statistic */
633 unsigned int i_gc_failures[MAX_GC_FAILURE];
Jaegeuk Kim6666e6a2012-12-10 17:52:48 +0900634 unsigned int i_pino; /* parent inode number */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900635 umode_t i_acl_mode; /* keep file acl mode temporarily */
636
637 /* Use below internally in f2fs*/
638 unsigned long flags; /* use to pass per-file flags */
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900639 struct rw_semaphore i_sem; /* protect fi info */
Jaegeuk Kima43e1c42016-12-02 15:11:32 -0800640 atomic_t dirty_pages; /* # of dirty pages */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900641 f2fs_hash_t chash; /* hash value of given file name */
642 unsigned int clevel; /* maximum level of given file name */
Jaegeuk Kimd00d1b72017-02-14 09:54:37 -0800643 struct task_struct *task; /* lookup and create consistency */
Chao Yuc0fe4882017-08-02 23:21:48 +0800644 struct task_struct *cp_task; /* separate cp/wb IO stats*/
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900645 nid_t i_xattr_nid; /* node id that contains xattrs */
Jaegeuk Kim26de9b12016-05-20 20:42:37 -0700646 loff_t last_disk_size; /* lastly written file size */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700647
Chao Yu09c3a722017-07-09 00:13:07 +0800648#ifdef CONFIG_QUOTA
649 struct dquot *i_dquot[MAXQUOTAS];
650
651 /* quota space reservation, managed internally by quota code */
652 qsize_t i_reserved_quota;
653#endif
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700654 struct list_head dirty_list; /* dirty list for dirs and files */
655 struct list_head gdirty_list; /* linked in global dirty list */
Jaegeuk Kim35e13ca2017-10-18 19:05:57 -0700656 struct list_head inmem_ilist; /* list for inmem inodes */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700657 struct list_head inmem_pages; /* inmemory pages managed by f2fs */
Jaegeuk Kima778c4c2017-07-24 19:46:29 -0700658 struct task_struct *inmem_task; /* store inmemory task */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700659 struct mutex inmem_lock; /* lock for inmemory pages */
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700660 struct extent_tree *extent_tree; /* cached extent_tree entry */
Chao Yuf6bd7d42018-04-24 10:55:28 +0800661
662 /* avoid racing between foreground op and gc */
663 struct rw_semaphore i_gc_rwsem[2];
Qiuyang Sun7b23ea12017-05-18 11:06:45 +0800664 struct rw_semaphore i_mmap_sem;
Yunlei He5dd3add2017-09-07 10:40:54 +0800665 struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
Chao Yue84f88e2017-07-19 00:19:05 +0800666
Chao Yufbcf9312017-07-19 00:19:06 +0800667 int i_extra_isize; /* size of extra space located in i_addr */
Chao Yu5647b302017-07-26 00:01:41 +0800668 kprojid_t i_projid; /* id for project quota */
Chao Yu50ffaa92017-09-06 21:59:50 +0800669 int i_inline_xattr_size; /* inline xattr size */
Chao Yu71f8f042018-01-25 14:54:42 +0800670 struct timespec i_crtime; /* inode creation time */
Jaegeuk Kimac389af2018-03-29 22:50:41 -0700671 struct timespec i_disk_time[4]; /* inode disk times */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900672};
673
674static inline void get_extent_info(struct extent_info *ext,
Chao Yubd933d42016-05-04 23:19:47 +0800675 struct f2fs_extent *i_ext)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900676{
Chao Yubd933d42016-05-04 23:19:47 +0800677 ext->fofs = le32_to_cpu(i_ext->fofs);
678 ext->blk = le32_to_cpu(i_ext->blk);
679 ext->len = le32_to_cpu(i_ext->len);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900680}
681
682static inline void set_raw_extent(struct extent_info *ext,
683 struct f2fs_extent *i_ext)
684{
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900685 i_ext->fofs = cpu_to_le32(ext->fofs);
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800686 i_ext->blk = cpu_to_le32(ext->blk);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900687 i_ext->len = cpu_to_le32(ext->len);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900688}
689
Chao Yu429511c2015-02-05 17:54:31 +0800690static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
691 u32 blk, unsigned int len)
692{
693 ei->fofs = fofs;
694 ei->blk = blk;
695 ei->len = len;
696}
697
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700698static inline bool __is_discard_mergeable(struct discard_info *back,
699 struct discard_info *front)
Chao Yu0bdee482015-03-19 19:27:51 +0800700{
Jaegeuk Kim8f6a2e72018-05-24 13:57:26 -0700701 return (back->lstart + back->len == front->lstart) &&
702 (back->len + front->len < DEF_MAX_DISCARD_LEN);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700703}
704
705static inline bool __is_discard_back_mergeable(struct discard_info *cur,
706 struct discard_info *back)
707{
708 return __is_discard_mergeable(back, cur);
709}
710
711static inline bool __is_discard_front_mergeable(struct discard_info *cur,
712 struct discard_info *front)
713{
714 return __is_discard_mergeable(cur, front);
Chao Yu0bdee482015-03-19 19:27:51 +0800715}
716
Chao Yu429511c2015-02-05 17:54:31 +0800717static inline bool __is_extent_mergeable(struct extent_info *back,
718 struct extent_info *front)
719{
720 return (back->fofs + back->len == front->fofs &&
721 back->blk + back->len == front->blk);
722}
723
724static inline bool __is_back_mergeable(struct extent_info *cur,
725 struct extent_info *back)
726{
727 return __is_extent_mergeable(back, cur);
728}
729
730static inline bool __is_front_mergeable(struct extent_info *cur,
731 struct extent_info *front)
732{
733 return __is_extent_mergeable(cur, front);
734}
735
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700736extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700737static inline void __try_update_largest_extent(struct inode *inode,
738 struct extent_tree *et, struct extent_node *en)
Chao Yu4abd3f52015-09-22 21:07:47 +0800739{
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700740 if (en->ei.len > et->largest.len) {
Chao Yu4abd3f52015-09-22 21:07:47 +0800741 et->largest = en->ei;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700742 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -0700743 }
Chao Yu4abd3f52015-09-22 21:07:47 +0800744}
745
Chao Yu84af6ae2017-09-29 13:59:35 +0800746/*
747 * For free nid management
748 */
749enum nid_state {
750 FREE_NID, /* newly added to free nid list */
751 PREALLOC_NID, /* it is preallocated */
752 MAX_NID_STATE,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700753};
754
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900755struct f2fs_nm_info {
756 block_t nat_blkaddr; /* base disk address of NAT */
757 nid_t max_nid; /* maximum possible node ids */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700758 nid_t available_nids; /* # of available node ids */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900759 nid_t next_scan_nid; /* the next nid to be scanned */
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +0900760 unsigned int ram_thresh; /* control the memory footprint */
Chao Yuea1a29a02015-10-12 17:08:48 +0800761 unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */
Chao Yu2304cb02016-01-18 18:32:58 +0800762 unsigned int dirty_nats_ratio; /* control dirty nats ratio threshold */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900763
764 /* NAT cache management */
765 struct radix_tree_root nat_root;/* root of the nat entry cache */
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700766 struct radix_tree_root nat_set_root;/* root of the nat set cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700767 struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900768 struct list_head nat_entries; /* cached nat entry list (clean) */
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700769 unsigned int nat_cnt; /* the # of cached nat entries */
Chao Yuaec71382014-06-24 09:18:20 +0800770 unsigned int dirty_nat_cnt; /* total num of nat entries in set */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700771 unsigned int nat_blocks; /* # of nat blocks */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900772
773 /* free node ids management */
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +0900774 struct radix_tree_root free_nid_root;/* root of the free_nid cache */
Chao Yu84af6ae2017-09-29 13:59:35 +0800775 struct list_head free_nid_list; /* list for free nids excluding preallocated nids */
776 unsigned int nid_cnt[MAX_NID_STATE]; /* the number of free node id */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700777 spinlock_t nid_list_lock; /* protect nid lists ops */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900778 struct mutex build_lock; /* lock for build free nids */
Jaegeuk Kim9f1896c2018-03-09 17:42:28 -0800779 unsigned char **free_nid_bitmap;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700780 unsigned char *nat_block_bitmap;
781 unsigned short *free_nid_count; /* free nid count of NAT block */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900782
783 /* for checkpoint */
784 char *nat_bitmap; /* NAT bitmap pointer */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700785
786 unsigned int nat_bits_blocks; /* # of nat bits blocks */
787 unsigned char *nat_bits; /* NAT bits blocks */
788 unsigned char *full_nat_bits; /* full NAT pages */
789 unsigned char *empty_nat_bits; /* empty NAT pages */
790#ifdef CONFIG_F2FS_CHECK_FS
791 char *nat_bitmap_mir; /* NAT bitmap mirror */
792#endif
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900793 int bitmap_size; /* bitmap size */
794};
795
796/*
797 * this structure is used as one of function parameters.
798 * all the information are dedicated to a given direct node block determined
799 * by the data offset in a file.
800 */
801struct dnode_of_data {
802 struct inode *inode; /* vfs inode pointer */
803 struct page *inode_page; /* its inode page, NULL is possible */
804 struct page *node_page; /* cached direct node page */
805 nid_t nid; /* node id of the direct node block */
806 unsigned int ofs_in_node; /* data offset in the node page */
807 bool inode_page_locked; /* inode page is locked or not */
Jaegeuk Kim93bae092015-12-22 12:59:54 -0800808 bool node_changed; /* is node block changed */
Chao Yu3cf45742016-01-26 15:40:44 +0800809 char cur_level; /* level of hole node page */
810 char max_level; /* level of current page located */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900811 block_t data_blkaddr; /* block address of the node block */
812};
813
814static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
815 struct page *ipage, struct page *npage, nid_t nid)
816{
Jaegeuk Kimd66d1f72013-01-03 08:57:21 +0900817 memset(dn, 0, sizeof(*dn));
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900818 dn->inode = inode;
819 dn->inode_page = ipage;
820 dn->node_page = npage;
821 dn->nid = nid;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900822}
823
824/*
825 * For SIT manager
826 *
827 * By default, there are 6 active log areas across the whole main area.
828 * When considering hot and cold data separation to reduce cleaning overhead,
829 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
830 * respectively.
831 * In the current design, you should not change the numbers intentionally.
832 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
833 * logs individually according to the underlying devices. (default: 6)
834 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
835 * data and 8 for node logs.
836 */
837#define NR_CURSEG_DATA_TYPE (3)
838#define NR_CURSEG_NODE_TYPE (3)
839#define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
840
841enum {
842 CURSEG_HOT_DATA = 0, /* directory entry blocks */
843 CURSEG_WARM_DATA, /* data blocks */
844 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
845 CURSEG_HOT_NODE, /* direct node blocks of directory files */
846 CURSEG_WARM_NODE, /* direct node blocks of normal files */
847 CURSEG_COLD_NODE, /* indirect node blocks */
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800848 NO_CHECK_TYPE,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900849};
850
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900851struct flush_cmd {
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900852 struct completion wait;
Gu Zheng721bd4d2014-09-05 18:31:00 +0800853 struct llist_node llnode;
Chao Yuf014be82017-09-29 13:59:38 +0800854 nid_t ino;
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900855 int ret;
856};
857
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800858struct flush_cmd_control {
859 struct task_struct *f2fs_issue_flush; /* flush thread */
860 wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700861 atomic_t issued_flush; /* # of issued flushes */
862 atomic_t issing_flush; /* # of issing flushes */
Gu Zheng721bd4d2014-09-05 18:31:00 +0800863 struct llist_head issue_list; /* list for command issue */
864 struct llist_node *dispatch_list; /* list for command dispatch */
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800865};
866
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900867struct f2fs_sm_info {
868 struct sit_info *sit_info; /* whole segment information */
869 struct free_segmap_info *free_info; /* free segment information */
870 struct dirty_seglist_info *dirty_info; /* dirty segment information */
871 struct curseg_info *curseg_array; /* active segment information */
872
Chao Yu7f41aab2017-11-02 20:41:03 +0800873 struct rw_semaphore curseg_lock; /* for preventing curseg change */
874
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900875 block_t seg0_blkaddr; /* block address of 0'th segment */
876 block_t main_blkaddr; /* start block address of main area */
877 block_t ssa_blkaddr; /* start block address of SSA area */
878
879 unsigned int segment_count; /* total # of segments */
880 unsigned int main_segments; /* # of segments in main area */
881 unsigned int reserved_segments; /* # of reserved segments */
882 unsigned int ovp_segments; /* # of overprovision segments */
Jaegeuk Kim81eb8d62013-10-24 13:31:34 +0900883
884 /* a threshold to reclaim prefree segments */
885 unsigned int rec_prefree_segments;
Jaegeuk Kim7fd9e542013-11-15 13:55:58 +0900886
Jaegeuk Kimbba681c2015-01-26 17:41:23 -0800887 /* for batched trimming */
888 unsigned int trim_sections; /* # of sections to trim */
889
Chao Yu184a5cd2014-09-04 18:13:01 +0800890 struct list_head sit_entry_set; /* sit entry set list */
891
Jaegeuk Kim216fbd62013-11-07 13:13:42 +0900892 unsigned int ipu_policy; /* in-place-update policy */
893 unsigned int min_ipu_util; /* in-place-update threshold */
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700894 unsigned int min_fsync_blocks; /* threshold for fsync */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700895 unsigned int min_hot_blocks; /* threshold for hot block allocation */
Chao Yub9f73872017-10-28 16:52:33 +0800896 unsigned int min_ssr_sections; /* threshold to trigger SSR allocation */
Jaegeuk Kim6b4afdd2014-04-02 15:34:36 +0900897
898 /* for flush command control */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700899 struct flush_cmd_control *fcc_info;
Gu Zhenga688b9d9e2014-04-27 14:21:21 +0800900
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700901 /* for discard command control */
902 struct discard_cmd_control *dcc_info;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900903};
904
905/*
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900906 * For superblock
907 */
908/*
909 * COUNT_TYPE for monitoring
910 *
911 * f2fs monitors the number of several block types such as on-writeback,
912 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
913 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700914#define WB_DATA_TYPE(p) (__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900915enum count_type {
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900916 F2FS_DIRTY_DENTS,
Chao Yuc227f912015-12-16 13:09:20 +0800917 F2FS_DIRTY_DATA,
Jaegeuk Kim931ecc22017-11-13 17:46:38 -0800918 F2FS_DIRTY_QDATA,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900919 F2FS_DIRTY_NODES,
920 F2FS_DIRTY_META,
Jaegeuk Kim8dcf2ff72014-12-05 17:18:15 -0800921 F2FS_INMEM_PAGES,
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700922 F2FS_DIRTY_IMETA,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700923 F2FS_WB_CP_DATA,
924 F2FS_WB_DATA,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900925 NR_COUNT_TYPE,
926};
927
928/*
arter97e1c42042014-08-06 23:22:50 +0900929 * The below are the page types of bios used in submit_bio().
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900930 * The available types are:
931 * DATA User data pages. It operates as async mode.
932 * NODE Node pages. It operates as async mode.
933 * META FS metadata pages such as SIT, NAT, CP.
934 * NR_PAGE_TYPE The number of page types.
935 * META_FLUSH Make sure the previous pages are written
936 * with waiting the bio's completion
937 * ... Only can be used with META.
938 */
Jaegeuk Kim7d5e5102013-11-18 17:13:35 +0900939#define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900940enum page_type {
941 DATA,
942 NODE,
943 META,
944 NR_PAGE_TYPE,
945 META_FLUSH,
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700946 INMEM, /* the below types are used by tracepoints only. */
947 INMEM_DROP,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700948 INMEM_INVALIDATE,
Chao Yu28bc1062016-02-06 14:40:34 +0800949 INMEM_REVOKE,
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -0700950 IPU,
951 OPU,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900952};
953
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700954enum temp_type {
955 HOT = 0, /* must be zero for meta bio */
956 WARM,
957 COLD,
958 NR_TEMP_TYPE,
959};
960
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -0700961enum need_lock_type {
962 LOCK_REQ = 0,
963 LOCK_DONE,
964 LOCK_RETRY,
965};
966
Chao Yud75eb8d2017-11-06 22:51:45 +0800967enum cp_reason_type {
968 CP_NO_NEEDED,
969 CP_NON_REGULAR,
970 CP_HARDLINK,
971 CP_SB_NEED_CP,
972 CP_WRONG_PINO,
973 CP_NO_SPC_ROLL,
974 CP_NODE_NEED_CP,
975 CP_FASTBOOT_MODE,
976 CP_SPEC_LOG_NUM,
Jaegeuk Kim211cb7b2017-12-28 08:09:44 -0800977 CP_RECOVER_DIR,
Chao Yud75eb8d2017-11-06 22:51:45 +0800978};
979
Chao Yuc0fe4882017-08-02 23:21:48 +0800980enum iostat_type {
981 APP_DIRECT_IO, /* app direct IOs */
982 APP_BUFFERED_IO, /* app buffered IOs */
983 APP_WRITE_IO, /* app write IOs */
984 APP_MAPPED_IO, /* app mapped IOs */
985 FS_DATA_IO, /* data IOs from kworker/fsync/reclaimer */
986 FS_NODE_IO, /* node IOs from kworker/fsync/reclaimer */
987 FS_META_IO, /* meta IOs from kworker/reclaimer */
988 FS_GC_DATA_IO, /* data IOs from forground gc */
989 FS_GC_NODE_IO, /* node IOs from forground gc */
990 FS_CP_DATA_IO, /* data IOs from checkpoint */
991 FS_CP_NODE_IO, /* node IOs from checkpoint */
992 FS_CP_META_IO, /* meta IOs from checkpoint */
993 FS_DISCARD, /* discard */
994 NR_IO_TYPE,
995};
996
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900997struct f2fs_io_info {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700998 struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
Chao Yuf014be82017-09-29 13:59:38 +0800999 nid_t ino; /* inode number */
Gu Zheng7e8f2302013-12-20 18:17:49 +08001000 enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
Jaegeuk Kimc4127262017-05-10 11:18:25 -07001001 enum temp_type temp; /* contains HOT/WARM/COLD */
Mike Christie04d328d2016-06-05 14:31:55 -05001002 int op; /* contains REQ_OP_ */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001003 int op_flags; /* req_flag_bits */
Chao Yu7a9d7542016-02-22 18:36:38 +08001004 block_t new_blkaddr; /* new block address to be written */
Chao Yu28bc1062016-02-06 14:40:34 +08001005 block_t old_blkaddr; /* old block address before Cow */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001006 struct page *page; /* page to be written */
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001007 struct page *encrypted_page; /* encrypted page */
Chao Yuc52dc0f2017-05-19 23:37:01 +08001008 struct list_head list; /* serialize IOs */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001009 bool submitted; /* indicate IO submission */
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001010 int need_lock; /* indicate we need to lock cp_rwsem */
Chao Yuc52dc0f2017-05-19 23:37:01 +08001011 bool in_list; /* indicate fio is in io_list */
Yunlei Hea3f8ec82018-03-08 16:29:13 +08001012 bool is_meta; /* indicate borrow meta inode mapping or not */
Chao Yu34880e02018-05-28 23:47:18 +08001013 bool retry; /* need to reallocate block address */
Chao Yuc0fe4882017-08-02 23:21:48 +08001014 enum iostat_type io_type; /* io type */
Yufen Yucbd5e5a2018-01-09 19:33:39 +08001015 struct writeback_control *io_wbc; /* writeback control */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001016};
1017
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001018#define is_read_io(rw) ((rw) == READ)
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +09001019struct f2fs_bio_info {
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001020 struct f2fs_sb_info *sbi; /* f2fs superblock */
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +09001021 struct bio *bio; /* bios to merge */
1022 sector_t last_block_in_bio; /* last block number */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001023 struct f2fs_io_info fio; /* store buffered io info. */
Chao Yudf0f8dc2014-03-22 14:57:23 +08001024 struct rw_semaphore io_rwsem; /* blocking op for bio */
Chao Yuc52dc0f2017-05-19 23:37:01 +08001025 spinlock_t io_lock; /* serialize DATA/NODE IOs */
1026 struct list_head io_list; /* track fios */
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +09001027};
1028
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001029#define FDEV(i) (sbi->devs[i])
1030#define RDEV(i) (raw_super->devs[i])
1031struct f2fs_dev_info {
1032 struct block_device *bdev;
1033 char path[MAX_PATH_LEN];
1034 unsigned int total_segments;
1035 block_t start_blk;
1036 block_t end_blk;
1037#ifdef CONFIG_BLK_DEV_ZONED
1038 unsigned int nr_blkz; /* Total number of zones */
1039 u8 *blkz_type; /* Array of zones type */
1040#endif
1041};
1042
Chao Yuc227f912015-12-16 13:09:20 +08001043enum inode_type {
1044 DIR_INODE, /* for dirty dir inode */
1045 FILE_INODE, /* for dirty regular/symlink inode */
Jaegeuk Kim0f18b462016-05-20 11:10:10 -07001046 DIRTY_META, /* for all dirtied inode metadata */
Jaegeuk Kim35e13ca2017-10-18 19:05:57 -07001047 ATOMIC_FILE, /* for all atomic files */
Chao Yuc227f912015-12-16 13:09:20 +08001048 NR_INODE_TYPE,
1049};
1050
Chao Yu67298802014-11-18 11:18:36 +08001051/* for inner inode cache management */
1052struct inode_management {
1053 struct radix_tree_root ino_root; /* ino entry array */
1054 spinlock_t ino_lock; /* for ino entry lock */
1055 struct list_head ino_list; /* inode list head */
1056 unsigned long ino_num; /* number of entries */
1057};
1058
Chao Yucaf00472015-01-28 17:48:42 +08001059/* For s_flag in struct f2fs_sb_info */
1060enum {
1061 SBI_IS_DIRTY, /* dirty flag for checkpoint */
1062 SBI_IS_CLOSE, /* specify unmounting */
1063 SBI_NEED_FSCK, /* need fsck.f2fs to fix */
1064 SBI_POR_DOING, /* recovery is doing or not */
Jaegeuk Kimdf728b02016-03-23 17:05:27 -07001065 SBI_NEED_SB_WRITE, /* need to recover superblock */
Jaegeuk Kimbbf156f2016-08-29 18:23:45 -07001066 SBI_NEED_CP, /* need to checkpoint */
Chao Yucaf00472015-01-28 17:48:42 +08001067};
1068
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08001069enum {
1070 CP_TIME,
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001071 REQ_TIME,
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08001072 MAX_TIME,
1073};
1074
Hyunchul Leebcdc5712018-01-31 11:36:57 +09001075enum {
Jaegeuk Kim0390d832018-05-07 14:22:40 -07001076 GC_NORMAL,
1077 GC_IDLE_CB,
1078 GC_IDLE_GREEDY,
1079 GC_URGENT,
1080};
1081
1082enum {
Hyunchul Leebcdc5712018-01-31 11:36:57 +09001083 WHINT_MODE_OFF, /* not pass down write hints */
1084 WHINT_MODE_USER, /* try to pass down hints given by users */
Hyunchul Lee87c18062018-01-31 11:36:58 +09001085 WHINT_MODE_FS, /* pass down hints with F2FS policy */
Hyunchul Leebcdc5712018-01-31 11:36:57 +09001086};
1087
Jaegeuk Kim78c1fc22018-02-18 08:50:49 -08001088enum {
1089 ALLOC_MODE_DEFAULT, /* stay default */
1090 ALLOC_MODE_REUSE, /* reuse segments as much as possible */
1091};
1092
Junling Zheng9a954812018-03-07 12:07:49 +08001093enum fsync_mode {
1094 FSYNC_MODE_POSIX, /* fsync follows posix semantics */
1095 FSYNC_MODE_STRICT, /* fsync behaves in line with ext4 */
Jaegeuk Kim3a38cf12018-05-25 18:02:58 -07001096 FSYNC_MODE_NOBARRIER, /* fsync behaves nobarrier based on posix */
Junling Zheng9a954812018-03-07 12:07:49 +08001097};
1098
Sheng Yongaa5bcfd2018-03-15 18:51:42 +08001099#ifdef CONFIG_F2FS_FS_ENCRYPTION
1100#define DUMMY_ENCRYPTION_ENABLED(sbi) \
1101 (unlikely(F2FS_OPTION(sbi).test_dummy_encryption))
1102#else
1103#define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
1104#endif
1105
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001106struct f2fs_sb_info {
1107 struct super_block *sb; /* pointer to VFS super block */
Jaegeuk Kim5e176d52013-06-28 12:47:01 +09001108 struct proc_dir_entry *s_proc; /* proc entry */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001109 struct f2fs_super_block *raw_super; /* raw super block pointer */
Chao Yu1e72cb22018-02-26 22:04:13 +08001110 struct rw_semaphore sb_lock; /* lock for raw super block */
Chao Yue8240f62015-12-15 17:19:26 +08001111 int valid_super_block; /* valid super block no */
Chao Yufadb2fb2016-09-20 10:29:47 +08001112 unsigned long s_flag; /* flags for sbi */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001113
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001114#ifdef CONFIG_BLK_DEV_ZONED
1115 unsigned int blocks_per_blkz; /* F2FS blocks per zone */
1116 unsigned int log_blocks_per_blkz; /* log2 F2FS blocks per zone */
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -07001117#endif
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001118
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001119 /* for node-related operations */
1120 struct f2fs_nm_info *nm_info; /* node manager */
1121 struct inode *node_inode; /* cache node blocks */
1122
1123 /* for segment-related operations */
1124 struct f2fs_sm_info *sm_info; /* segment manager */
Jaegeuk Kim1ff7bd32013-11-19 12:47:22 +09001125
1126 /* for bio operations */
Jaegeuk Kimc4127262017-05-10 11:18:25 -07001127 struct f2fs_bio_info *write_io[NR_PAGE_TYPE]; /* for write bios */
Chao Yu06504682017-05-19 23:37:00 +08001128 struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
1129 /* bio ordering for NODE/DATA */
Chao Yubbab2dc2018-05-26 09:00:13 +08001130 /* keep migration IO order for LFS mode */
1131 struct rw_semaphore io_order_lock;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001132 mempool_t *write_io_dummy; /* Dummy pages */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001133
1134 /* for checkpoint */
1135 struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
Jaegeuk Kim01e15b32016-11-24 12:45:15 -08001136 int cur_cp_pack; /* remain current cp pack */
Chao Yuaaec2b12016-09-20 11:04:18 +08001137 spinlock_t cp_lock; /* for flag in ckpt */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001138 struct inode *meta_inode; /* cache meta blocks */
Jaegeuk Kim39936832012-11-22 16:21:29 +09001139 struct mutex cp_mutex; /* checkpoint procedure lock */
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001140 struct rw_semaphore cp_rwsem; /* blocking FS operations */
Chao Yub3582c62014-07-03 18:58:39 +08001141 struct rw_semaphore node_write; /* locking node writes */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001142 struct rw_semaphore node_change; /* locking node change */
Changman Leefb51b5e2013-11-07 12:48:25 +09001143 wait_queue_head_t cp_wait;
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08001144 unsigned long last_time[MAX_TIME]; /* to store time in jiffies */
1145 long interval_time[MAX_TIME]; /* to store thresholds */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001146
Chao Yu67298802014-11-18 11:18:36 +08001147 struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
Jaegeuk Kim6451e042014-07-25 15:47:17 -07001148
1149 /* for orphan inode, use 0'th array */
Gu Zheng0d47c1a2013-12-26 18:24:19 +08001150 unsigned int max_orphans; /* max orphan inodes */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001151
Chao Yuc227f912015-12-16 13:09:20 +08001152 /* for inode management */
1153 struct list_head inode_list[NR_INODE_TYPE]; /* dirty inode list */
1154 spinlock_t inode_lock[NR_INODE_TYPE]; /* for dirty inode list lock */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001155
Chao Yu13054c52015-02-05 17:52:58 +08001156 /* for extent tree cache */
1157 struct radix_tree_root extent_tree_root;/* cache extent cache entries */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001158 struct mutex extent_tree_lock; /* locking extent radix tree */
Chao Yu13054c52015-02-05 17:52:58 +08001159 struct list_head extent_list; /* lru list for shrinker */
1160 spinlock_t extent_lock; /* locking extent lru list */
Jaegeuk Kim7441cce2015-12-21 19:20:15 -08001161 atomic_t total_ext_tree; /* extent tree count */
Jaegeuk Kim137d09f2015-12-31 15:02:16 -08001162 struct list_head zombie_list; /* extent zombie tree list */
Jaegeuk Kim74fd8d92015-12-21 19:25:50 -08001163 atomic_t total_zombie_tree; /* extent zombie tree count */
Chao Yu13054c52015-02-05 17:52:58 +08001164 atomic_t total_ext_node; /* extent info count */
1165
arter97e1c42042014-08-06 23:22:50 +09001166 /* basic filesystem units */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001167 unsigned int log_sectors_per_block; /* log2 sectors per block */
1168 unsigned int log_blocksize; /* log2 block size */
1169 unsigned int blocksize; /* block size */
1170 unsigned int root_ino_num; /* root inode number*/
1171 unsigned int node_ino_num; /* node inode number*/
1172 unsigned int meta_ino_num; /* meta inode number*/
1173 unsigned int log_blocks_per_seg; /* log2 blocks per segment */
1174 unsigned int blocks_per_seg; /* blocks per segment */
1175 unsigned int segs_per_sec; /* segments per section */
1176 unsigned int secs_per_zone; /* sections per zone */
1177 unsigned int total_sections; /* total section count */
1178 unsigned int total_node_count; /* total node block count */
1179 unsigned int total_valid_node_count; /* valid node block count */
Chao Yue0afc4d2015-12-31 14:35:37 +08001180 loff_t max_file_blocks; /* max block index of file */
Jaegeuk Kimab9fa662014-02-27 20:09:05 +09001181 int dir_level; /* directory level */
Chao Yub9f73872017-10-28 16:52:33 +08001182 unsigned int trigger_ssr_threshold; /* threshold to trigger ssr */
Sheng Yong1ed75332017-11-22 18:23:38 +08001183 int readdir_ra; /* readahead inode in readdir */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001184
1185 block_t user_block_count; /* # of user blocks */
1186 block_t total_valid_block_count; /* # of valid blocks */
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07001187 block_t discard_blks; /* discard command candidats */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001188 block_t last_valid_block_count; /* for recovery */
Chao Yu026bd9d2017-06-26 16:24:41 +08001189 block_t reserved_blocks; /* configurable reserved blocks */
Yunlong Song68ab6f82017-10-27 20:45:05 +08001190 block_t current_reserved_blocks; /* current reserved blocks */
Chao Yu026bd9d2017-06-26 16:24:41 +08001191
Chao Yu09234be2017-11-16 16:59:14 +08001192 unsigned int nquota_files; /* # of quota sysfile */
1193
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001194 u32 s_next_generation; /* for NFS support */
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001195
1196 /* # of pages, see count_type */
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07001197 atomic_t nr_pages[NR_COUNT_TYPE];
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001198 /* # of allocated blocks */
1199 struct percpu_counter alloc_valid_block_count;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001200
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001201 /* writeback control */
1202 atomic_t wb_sync_req; /* count # of WB_SYNC threads */
1203
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001204 /* valid inode count */
1205 struct percpu_counter total_valid_inode_count;
1206
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001207 struct f2fs_mount_info mount_opt; /* mount options */
1208
1209 /* for cleaning operations */
1210 struct mutex gc_mutex; /* mutex for GC */
1211 struct f2fs_gc_kthread *gc_thread; /* GC thread */
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +09001212 unsigned int cur_victim_sec; /* current victim section num */
Jaegeuk Kim0390d832018-05-07 14:22:40 -07001213 unsigned int gc_mode; /* current GC state */
Chao Yucaf10c62018-05-07 20:28:54 +08001214 /* for skip statistic */
1215 unsigned long long skipped_atomic_files[2]; /* FG_GC and BG_GC */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001216
Jaegeuk Kim9ce3d6b2017-12-07 16:25:39 -08001217 /* threshold for gc trials on pinned files */
1218 u64 gc_pin_file_threshold;
1219
Jaegeuk Kimb1c57c12014-01-08 13:45:08 +09001220 /* maximum # of trials to find a victim segment for SSR and GC */
1221 unsigned int max_victim_search;
1222
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001223 /*
1224 * for stat information.
1225 * one is for the LFS mode, and the other is for the SSR mode.
1226 */
Namjae Jeon35b09d82013-05-23 22:57:53 +09001227#ifdef CONFIG_F2FS_STAT_FS
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001228 struct f2fs_stat_info *stat_info; /* FS status information */
1229 unsigned int segment_count[2]; /* # of allocated segments */
1230 unsigned int block_count[2]; /* # of allocated blocks */
Changman Leeb9a2c252014-12-24 02:16:54 +09001231 atomic_t inplace_count; /* # of inplace update */
Chao Yu5b7ee372015-09-30 17:38:48 +08001232 atomic64_t total_hit_ext; /* # of lookup extent cache */
1233 atomic64_t read_hit_rbtree; /* # of hit rbtree extent node */
1234 atomic64_t read_hit_largest; /* # of hit largest extent node */
1235 atomic64_t read_hit_cached; /* # of hit cached extent node */
Chao Yud5e8f6c2015-07-15 17:28:53 +08001236 atomic_t inline_xattr; /* # of inline_xattr inodes */
Chao Yu03e14d52014-12-08 19:08:20 +08001237 atomic_t inline_inode; /* # of inline_data inodes */
1238 atomic_t inline_dir; /* # of inline_dentry inodes */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001239 atomic_t aw_cnt; /* # of atomic writes */
1240 atomic_t vw_cnt; /* # of volatile writes */
1241 atomic_t max_aw_cnt; /* max # of atomic writes */
1242 atomic_t max_vw_cnt; /* max # of volatile writes */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001243 int bg_gc; /* background gc calls */
Chao Yu33fbd512015-12-17 17:14:44 +08001244 unsigned int ndirty_inode[NR_INODE_TYPE]; /* # of dirty inodes */
Namjae Jeon35b09d82013-05-23 22:57:53 +09001245#endif
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001246 spinlock_t stat_lock; /* lock for stat operations */
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09001247
Chao Yuc0fe4882017-08-02 23:21:48 +08001248 /* For app/fs IO statistics */
1249 spinlock_t iostat_lock;
1250 unsigned long long write_iostat[NR_IO_TYPE];
1251 bool iostat_enable;
1252
Namjae Jeonb59d0ba2013-08-04 23:09:40 +09001253 /* For sysfs suppport */
1254 struct kobject s_kobj;
1255 struct completion s_kobj_unregister;
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001256
1257 /* For shrinker support */
1258 struct list_head s_list;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001259 int s_ndevs; /* number of devices */
1260 struct f2fs_dev_info *devs; /* for device list */
Chao Yufd6422e2017-09-29 13:59:39 +08001261 unsigned int dirty_device; /* for checkpoint data flush */
1262 spinlock_t dev_lock; /* protect dirty_device */
Jaegeuk Kim2658e502015-06-19 12:01:21 -07001263 struct mutex umount_mutex;
1264 unsigned int shrinker_run_no;
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001265
1266 /* For write statistics */
1267 u64 sectors_written_start;
1268 u64 kbytes_written;
Keith Mok43b65732016-03-02 12:04:24 -08001269
1270 /* Reference to checksum algorithm driver via cryptoapi */
1271 struct crypto_shash *s_chksum_driver;
Chao Yu1ecc0c52016-09-23 21:30:09 +08001272
Chao Yu43101252017-07-31 20:19:09 +08001273 /* Precomputed FS UUID checksum for seeding other checksums */
1274 __u32 s_chksum_seed;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001275};
1276
Chao Yu1ecc0c52016-09-23 21:30:09 +08001277#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001278#define f2fs_show_injection_info(type) \
1279 printk("%sF2FS-fs : inject %s in %s of %pF\n", \
1280 KERN_INFO, fault_name[type], \
1281 __func__, __builtin_return_address(0))
Chao Yu1ecc0c52016-09-23 21:30:09 +08001282static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
1283{
Chao Yue9a50e62018-03-08 14:22:56 +08001284 struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
Chao Yu1ecc0c52016-09-23 21:30:09 +08001285
1286 if (!ffi->inject_rate)
1287 return false;
1288
1289 if (!IS_FAULT_SET(ffi, type))
1290 return false;
1291
1292 atomic_inc(&ffi->inject_ops);
1293 if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
1294 atomic_set(&ffi->inject_ops, 0);
Chao Yu1ecc0c52016-09-23 21:30:09 +08001295 return true;
1296 }
1297 return false;
1298}
1299#endif
1300
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001301/* For write statistics. Suppose sector size is 512 bytes,
1302 * and the return value is in kbytes. s is of struct f2fs_sb_info.
1303 */
1304#define BD_PART_WRITTEN(s) \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001305(((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) - \
1306 (s)->sectors_written_start) >> 1)
Shuoran Liu8f1dbbb2016-01-27 09:57:30 +08001307
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08001308static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
1309{
1310 sbi->last_time[type] = jiffies;
1311}
1312
1313static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
1314{
Arnd Bergmannbf823c82017-10-19 11:52:47 +02001315 unsigned long interval = sbi->interval_time[type] * HZ;
Jaegeuk Kim6beceb52016-01-08 15:51:50 -08001316
1317 return time_after(jiffies, sbi->last_time[type] + interval);
1318}
1319
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001320static inline bool is_idle(struct f2fs_sb_info *sbi)
1321{
1322 struct block_device *bdev = sbi->sb->s_bdev;
1323 struct request_queue *q = bdev_get_queue(bdev);
1324 struct request_list *rl = &q->root_rl;
1325
1326 if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1327 return 0;
1328
1329 return f2fs_time_over(sbi, REQ_TIME);
1330}
1331
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001332/*
1333 * Inline functions
1334 */
Chao Yud7714cb2017-11-30 19:28:21 +08001335static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
Chao Yu43101252017-07-31 20:19:09 +08001336 const void *address, unsigned int length)
1337{
1338 struct {
1339 struct shash_desc shash;
1340 char ctx[4];
1341 } desc;
1342 int err;
1343
1344 BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1345
1346 desc.shash.tfm = sbi->s_chksum_driver;
1347 desc.shash.flags = 0;
1348 *(u32 *)desc.ctx = crc;
1349
1350 err = crypto_shash_update(&desc.shash, address, length);
1351 BUG_ON(err);
1352
1353 return *(u32 *)desc.ctx;
1354}
1355
Chao Yud7714cb2017-11-30 19:28:21 +08001356static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1357 unsigned int length)
1358{
1359 return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1360}
1361
1362static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1363 void *buf, size_t buf_size)
1364{
1365 return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1366}
1367
1368static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1369 const void *address, unsigned int length)
1370{
1371 return __f2fs_crc32(sbi, crc, address, length);
1372}
1373
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001374static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
1375{
1376 return container_of(inode, struct f2fs_inode_info, vfs_inode);
1377}
1378
1379static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
1380{
1381 return sb->s_fs_info;
1382}
1383
Jaegeuk Kim40813632014-09-02 15:31:18 -07001384static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
1385{
1386 return F2FS_SB(inode->i_sb);
1387}
1388
1389static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
1390{
1391 return F2FS_I_SB(mapping->host);
1392}
1393
1394static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
1395{
1396 return F2FS_M_SB(page->mapping);
1397}
1398
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001399static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
1400{
1401 return (struct f2fs_super_block *)(sbi->raw_super);
1402}
1403
1404static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
1405{
1406 return (struct f2fs_checkpoint *)(sbi->ckpt);
1407}
1408
Gu Zheng45590712013-07-15 17:57:38 +08001409static inline struct f2fs_node *F2FS_NODE(struct page *page)
1410{
1411 return (struct f2fs_node *)page_address(page);
1412}
1413
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001414static inline struct f2fs_inode *F2FS_INODE(struct page *page)
1415{
1416 return &((struct f2fs_node *)page_address(page))->i;
1417}
1418
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001419static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
1420{
1421 return (struct f2fs_nm_info *)(sbi->nm_info);
1422}
1423
1424static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
1425{
1426 return (struct f2fs_sm_info *)(sbi->sm_info);
1427}
1428
1429static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
1430{
1431 return (struct sit_info *)(SM_I(sbi)->sit_info);
1432}
1433
1434static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
1435{
1436 return (struct free_segmap_info *)(SM_I(sbi)->free_info);
1437}
1438
1439static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
1440{
1441 return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
1442}
1443
Gu Zheng9df27d92014-01-20 18:37:04 +08001444static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
1445{
1446 return sbi->meta_inode->i_mapping;
1447}
1448
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001449static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
1450{
1451 return sbi->node_inode->i_mapping;
1452}
1453
Chao Yucaf00472015-01-28 17:48:42 +08001454static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001455{
Chao Yufadb2fb2016-09-20 10:29:47 +08001456 return test_bit(type, &sbi->s_flag);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001457}
1458
Chao Yucaf00472015-01-28 17:48:42 +08001459static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001460{
Chao Yufadb2fb2016-09-20 10:29:47 +08001461 set_bit(type, &sbi->s_flag);
Chao Yucaf00472015-01-28 17:48:42 +08001462}
1463
1464static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1465{
Chao Yufadb2fb2016-09-20 10:29:47 +08001466 clear_bit(type, &sbi->s_flag);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001467}
1468
Jaegeuk Kimd71b5562013-08-09 15:03:21 +09001469static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1470{
1471 return le64_to_cpu(cp->checkpoint_ver);
1472}
1473
Jaegeuk Kim0108c482017-10-06 09:14:28 -07001474static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1475{
1476 if (type < F2FS_MAX_QUOTAS)
1477 return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1478 return 0;
1479}
1480
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001481static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1482{
1483 size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1484 return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1485}
1486
Chao Yuaaec2b12016-09-20 11:04:18 +08001487static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001488{
1489 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
Chao Yuaaec2b12016-09-20 11:04:18 +08001490
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001491 return ckpt_flags & f;
1492}
1493
Chao Yuaaec2b12016-09-20 11:04:18 +08001494static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001495{
Chao Yuaaec2b12016-09-20 11:04:18 +08001496 return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1497}
1498
1499static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1500{
1501 unsigned int ckpt_flags;
1502
1503 ckpt_flags = le32_to_cpu(cp->ckpt_flags);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001504 ckpt_flags |= f;
1505 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1506}
1507
Chao Yuaaec2b12016-09-20 11:04:18 +08001508static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001509{
Chao Yu67295cd2017-07-07 14:10:15 +08001510 unsigned long flags;
1511
1512 spin_lock_irqsave(&sbi->cp_lock, flags);
Chao Yuaaec2b12016-09-20 11:04:18 +08001513 __set_ckpt_flags(F2FS_CKPT(sbi), f);
Chao Yu67295cd2017-07-07 14:10:15 +08001514 spin_unlock_irqrestore(&sbi->cp_lock, flags);
Chao Yuaaec2b12016-09-20 11:04:18 +08001515}
1516
1517static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1518{
1519 unsigned int ckpt_flags;
1520
1521 ckpt_flags = le32_to_cpu(cp->ckpt_flags);
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001522 ckpt_flags &= (~f);
1523 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1524}
1525
Chao Yuaaec2b12016-09-20 11:04:18 +08001526static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1527{
Chao Yu67295cd2017-07-07 14:10:15 +08001528 unsigned long flags;
1529
1530 spin_lock_irqsave(&sbi->cp_lock, flags);
Chao Yuaaec2b12016-09-20 11:04:18 +08001531 __clear_ckpt_flags(F2FS_CKPT(sbi), f);
Chao Yu67295cd2017-07-07 14:10:15 +08001532 spin_unlock_irqrestore(&sbi->cp_lock, flags);
Chao Yuaaec2b12016-09-20 11:04:18 +08001533}
1534
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001535static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001536{
Chao Yu67295cd2017-07-07 14:10:15 +08001537 unsigned long flags;
1538
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001539 set_sbi_flag(sbi, SBI_NEED_FSCK);
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001540
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001541 if (lock)
Chao Yu67295cd2017-07-07 14:10:15 +08001542 spin_lock_irqsave(&sbi->cp_lock, flags);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001543 __clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
1544 kfree(NM_I(sbi)->nat_bits);
1545 NM_I(sbi)->nat_bits = NULL;
1546 if (lock)
Chao Yu67295cd2017-07-07 14:10:15 +08001547 spin_unlock_irqrestore(&sbi->cp_lock, flags);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001548}
1549
1550static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
1551 struct cp_control *cpc)
1552{
1553 bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
1554
1555 return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
Jaegeuk Kim3e025742016-08-02 10:56:40 -07001556}
1557
Gu Zhenge4795562013-09-27 18:08:30 +08001558static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001559{
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001560 down_read(&sbi->cp_rwsem);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001561}
1562
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001563static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1564{
1565 return down_read_trylock(&sbi->cp_rwsem);
1566}
1567
Gu Zhenge4795562013-09-27 18:08:30 +08001568static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001569{
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001570 up_read(&sbi->cp_rwsem);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001571}
1572
Gu Zhenge4795562013-09-27 18:08:30 +08001573static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001574{
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001575 down_write(&sbi->cp_rwsem);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001576}
1577
Gu Zhenge4795562013-09-27 18:08:30 +08001578static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001579{
Jaegeuk Kimb873b792016-08-04 11:38:25 -07001580 up_write(&sbi->cp_rwsem);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001581}
1582
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001583static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1584{
1585 int reason = CP_SYNC;
1586
1587 if (test_opt(sbi, FASTBOOT))
1588 reason = CP_FASTBOOT;
1589 if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1590 reason = CP_UMOUNT;
1591 return reason;
1592}
1593
1594static inline bool __remain_node_summaries(int reason)
1595{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001596 return (reason & (CP_UMOUNT | CP_FASTBOOT));
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001597}
1598
1599static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1600{
Chao Yuaaec2b12016-09-20 11:04:18 +08001601 return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1602 is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
Jaegeuk Kim119ee912015-01-29 11:45:33 -08001603}
1604
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001605/*
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001606 * Check whether the inode has blocks or not
1607 */
1608static inline int F2FS_HAS_BLOCKS(struct inode *inode)
1609{
Chao Yu56a0d932017-06-14 23:00:56 +08001610 block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
1611
Chao Yud9bfbbb2017-07-06 01:11:31 +08001612 return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001613}
1614
Chao Yu4bc8e9b2014-03-17 16:35:06 +08001615static inline bool f2fs_has_xattr_block(unsigned int ofs)
1616{
1617 return ofs == XATTR_NODE_OFFSET;
1618}
1619
Jaegeuk Kim4be98c92018-01-05 16:02:36 -08001620static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
Jaegeuk Kimc8fb2452018-04-20 23:44:59 -07001621 struct inode *inode, bool cap)
Jaegeuk Kim462d7622018-01-04 21:36:09 -08001622{
Jaegeuk Kim4be98c92018-01-05 16:02:36 -08001623 if (!inode)
1624 return true;
Jaegeuk Kim462d7622018-01-04 21:36:09 -08001625 if (!test_opt(sbi, RESERVE_ROOT))
1626 return false;
Jaegeuk Kim4be98c92018-01-05 16:02:36 -08001627 if (IS_NOQUOTA(inode))
1628 return true;
Chao Yue9a50e62018-03-08 14:22:56 +08001629 if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
Jaegeuk Kim462d7622018-01-04 21:36:09 -08001630 return true;
Chao Yue9a50e62018-03-08 14:22:56 +08001631 if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
1632 in_group_p(F2FS_OPTION(sbi).s_resgid))
Jaegeuk Kim462d7622018-01-04 21:36:09 -08001633 return true;
Jaegeuk Kimc8fb2452018-04-20 23:44:59 -07001634 if (cap && capable(CAP_SYS_RESOURCE))
Jaegeuk Kimc07478e2018-03-08 20:47:33 -08001635 return true;
Jaegeuk Kim462d7622018-01-04 21:36:09 -08001636 return false;
1637}
1638
Chao Yu09c3a722017-07-09 00:13:07 +08001639static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
1640static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
Chao Yu46008c62016-05-09 19:56:30 +08001641 struct inode *inode, blkcnt_t *count)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001642{
Chao Yu09c3a722017-07-09 00:13:07 +08001643 blkcnt_t diff = 0, release = 0;
Chao Yu026bd9d2017-06-26 16:24:41 +08001644 block_t avail_user_block_count;
Chao Yu09c3a722017-07-09 00:13:07 +08001645 int ret;
1646
1647 ret = dquot_reserve_block(inode, *count);
1648 if (ret)
1649 return ret;
Jaegeuk Kimdd11a5d2016-07-19 19:20:11 -07001650
Jaegeuk Kimcb789422016-04-29 16:29:22 -07001651#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001652 if (time_to_inject(sbi, FAULT_BLOCK)) {
1653 f2fs_show_injection_info(FAULT_BLOCK);
Chao Yu09c3a722017-07-09 00:13:07 +08001654 release = *count;
1655 goto enospc;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001656 }
Jaegeuk Kimcb789422016-04-29 16:29:22 -07001657#endif
Jaegeuk Kimdd11a5d2016-07-19 19:20:11 -07001658 /*
1659 * let's increase this in prior to actual block count change in order
1660 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1661 */
1662 percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
Chao Yucfb271d2013-12-05 17:15:22 +08001663
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001664 spin_lock(&sbi->stat_lock);
Jaegeuk Kim2555a2d2016-06-30 19:02:06 -07001665 sbi->total_valid_block_count += (block_t)(*count);
Yunlong Song68ab6f82017-10-27 20:45:05 +08001666 avail_user_block_count = sbi->user_block_count -
1667 sbi->current_reserved_blocks;
Jaegeuk Kim5fee5402017-12-27 15:05:52 -08001668
Jaegeuk Kimc8fb2452018-04-20 23:44:59 -07001669 if (!__allow_reserved_blocks(sbi, inode, true))
Chao Yue9a50e62018-03-08 14:22:56 +08001670 avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
Jaegeuk Kim5fee5402017-12-27 15:05:52 -08001671
Chao Yu026bd9d2017-06-26 16:24:41 +08001672 if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1673 diff = sbi->total_valid_block_count - avail_user_block_count;
Jaegeuk Kim5fee5402017-12-27 15:05:52 -08001674 if (diff > *count)
1675 diff = *count;
Jaegeuk Kimdd11a5d2016-07-19 19:20:11 -07001676 *count -= diff;
Chao Yu09c3a722017-07-09 00:13:07 +08001677 release = diff;
Jaegeuk Kim5fee5402017-12-27 15:05:52 -08001678 sbi->total_valid_block_count -= diff;
Chao Yu46008c62016-05-09 19:56:30 +08001679 if (!*count) {
1680 spin_unlock(&sbi->stat_lock);
Jaegeuk Kimdd11a5d2016-07-19 19:20:11 -07001681 percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
Chao Yu09c3a722017-07-09 00:13:07 +08001682 goto enospc;
Chao Yu46008c62016-05-09 19:56:30 +08001683 }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001684 }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001685 spin_unlock(&sbi->stat_lock);
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001686
LiFanfdd41a82017-12-05 16:38:01 +08001687 if (unlikely(release))
Chao Yu09c3a722017-07-09 00:13:07 +08001688 dquot_release_reservation_block(inode, release);
1689 f2fs_i_blocks_write(inode, *count, true, true);
1690 return 0;
1691
1692enospc:
1693 dquot_release_reservation_block(inode, release);
1694 return -ENOSPC;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001695}
1696
Gu Zhengda19b0d2013-11-19 18:03:27 +08001697static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001698 struct inode *inode,
Chao Yu56a0d932017-06-14 23:00:56 +08001699 block_t count)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001700{
Chao Yu56a0d932017-06-14 23:00:56 +08001701 blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
1702
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001703 spin_lock(&sbi->stat_lock);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001704 f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
Chao Yu56a0d932017-06-14 23:00:56 +08001705 f2fs_bug_on(sbi, inode->i_blocks < sectors);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001706 sbi->total_valid_block_count -= (block_t)count;
Yunlong Song68ab6f82017-10-27 20:45:05 +08001707 if (sbi->reserved_blocks &&
1708 sbi->current_reserved_blocks < sbi->reserved_blocks)
1709 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
1710 sbi->current_reserved_blocks + count);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001711 spin_unlock(&sbi->stat_lock);
Chao Yu09c3a722017-07-09 00:13:07 +08001712 f2fs_i_blocks_write(inode, count, false, true);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001713}
1714
1715static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
1716{
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07001717 atomic_inc(&sbi->nr_pages[count_type]);
Chao Yu7c4abcb2016-08-18 17:46:14 +08001718
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001719 if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
1720 count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
Chao Yu7c4abcb2016-08-18 17:46:14 +08001721 return;
1722
Chao Yucaf00472015-01-28 17:48:42 +08001723 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001724}
1725
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001726static inline void inode_inc_dirty_pages(struct inode *inode)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001727{
Jaegeuk Kima43e1c42016-12-02 15:11:32 -08001728 atomic_inc(&F2FS_I(inode)->dirty_pages);
Chao Yuc227f912015-12-16 13:09:20 +08001729 inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1730 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
Jaegeuk Kim931ecc22017-11-13 17:46:38 -08001731 if (IS_NOQUOTA(inode))
1732 inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001733}
1734
1735static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
1736{
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07001737 atomic_dec(&sbi->nr_pages[count_type]);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001738}
1739
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001740static inline void inode_dec_dirty_pages(struct inode *inode)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001741{
Chao Yu5ac9f362015-06-29 18:14:10 +08001742 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1743 !S_ISLNK(inode->i_mode))
Jaegeuk Kim1fe54f92014-02-07 10:00:06 +09001744 return;
1745
Jaegeuk Kima43e1c42016-12-02 15:11:32 -08001746 atomic_dec(&F2FS_I(inode)->dirty_pages);
Chao Yuc227f912015-12-16 13:09:20 +08001747 dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1748 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
Jaegeuk Kim931ecc22017-11-13 17:46:38 -08001749 if (IS_NOQUOTA(inode))
1750 dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001751}
1752
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001753static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001754{
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07001755 return atomic_read(&sbi->nr_pages[count_type]);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001756}
1757
Jaegeuk Kima43e1c42016-12-02 15:11:32 -08001758static inline int get_dirty_pages(struct inode *inode)
Jaegeuk Kimf8b2c1f2014-03-18 12:33:06 +09001759{
Jaegeuk Kima43e1c42016-12-02 15:11:32 -08001760 return atomic_read(&F2FS_I(inode)->dirty_pages);
Jaegeuk Kimf8b2c1f2014-03-18 12:33:06 +09001761}
1762
Namjae Jeon5ac206c2013-02-02 23:52:59 +09001763static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
1764{
Chao Yu3519e3f2015-12-01 11:56:52 +08001765 unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
Jaegeuk Kim523be8a2016-05-13 12:36:58 -07001766 unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1767 sbi->log_blocks_per_seg;
1768
1769 return segs / sbi->segs_per_sec;
Namjae Jeon5ac206c2013-02-02 23:52:59 +09001770}
1771
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001772static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
1773{
Jaegeuk Kim8b8343f2014-02-24 13:00:13 +09001774 return sbi->total_valid_block_count;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001775}
1776
Yunlei Hef83a2582016-08-18 21:01:18 +08001777static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1778{
1779 return sbi->discard_blks;
1780}
1781
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001782static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
1783{
1784 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1785
1786 /* return NAT or SIT bitmap */
1787 if (flag == NAT_BITMAP)
1788 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
1789 else if (flag == SIT_BITMAP)
1790 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
1791
1792 return 0;
1793}
1794
Wanpeng Li55141482015-02-26 07:57:20 +08001795static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
1796{
1797 return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
1798}
1799
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001800static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
1801{
1802 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
Changman Lee1dbe4152014-05-12 12:27:43 +09001803 int offset;
1804
Chao Yua1afb552018-01-25 19:40:08 +08001805 if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
1806 offset = (flag == SIT_BITMAP) ?
1807 le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
1808 return &ckpt->sit_nat_version_bitmap + offset;
1809 }
1810
Wanpeng Li55141482015-02-26 07:57:20 +08001811 if (__cp_payload(sbi) > 0) {
Changman Lee1dbe4152014-05-12 12:27:43 +09001812 if (flag == NAT_BITMAP)
1813 return &ckpt->sit_nat_version_bitmap;
1814 else
Jaegeuk Kim65b85cc2014-07-30 17:25:54 -07001815 return (unsigned char *)ckpt + F2FS_BLKSIZE;
Changman Lee1dbe4152014-05-12 12:27:43 +09001816 } else {
1817 offset = (flag == NAT_BITMAP) ?
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001818 le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
Changman Lee1dbe4152014-05-12 12:27:43 +09001819 return &ckpt->sit_nat_version_bitmap + offset;
1820 }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001821}
1822
1823static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
1824{
Jaegeuk Kim01e15b32016-11-24 12:45:15 -08001825 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001826
Jaegeuk Kim01e15b32016-11-24 12:45:15 -08001827 if (sbi->cur_cp_pack == 2)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001828 start_addr += sbi->blocks_per_seg;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001829 return start_addr;
1830}
1831
Jaegeuk Kim01e15b32016-11-24 12:45:15 -08001832static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
1833{
1834 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
1835
1836 if (sbi->cur_cp_pack == 1)
1837 start_addr += sbi->blocks_per_seg;
1838 return start_addr;
1839}
1840
1841static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
1842{
1843 sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
1844}
1845
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001846static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
1847{
1848 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
1849}
1850
Chao Yu09c3a722017-07-09 00:13:07 +08001851static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
Chao Yud9bfbbb2017-07-06 01:11:31 +08001852 struct inode *inode, bool is_inode)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001853{
1854 block_t valid_block_count;
1855 unsigned int valid_node_count;
Chao Yu09c3a722017-07-09 00:13:07 +08001856 bool quota = inode && !is_inode;
1857
1858 if (quota) {
1859 int ret = dquot_reserve_block(inode, 1);
1860 if (ret)
1861 return ret;
1862 }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001863
Chao Yu05dac2e2017-11-13 17:32:40 +08001864#ifdef CONFIG_F2FS_FAULT_INJECTION
1865 if (time_to_inject(sbi, FAULT_BLOCK)) {
1866 f2fs_show_injection_info(FAULT_BLOCK);
1867 goto enospc;
1868 }
1869#endif
1870
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001871 spin_lock(&sbi->stat_lock);
1872
Jaegeuk Kim5fee5402017-12-27 15:05:52 -08001873 valid_block_count = sbi->total_valid_block_count +
1874 sbi->current_reserved_blocks + 1;
1875
Jaegeuk Kimc8fb2452018-04-20 23:44:59 -07001876 if (!__allow_reserved_blocks(sbi, inode, false))
Chao Yue9a50e62018-03-08 14:22:56 +08001877 valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
Jaegeuk Kim5fee5402017-12-27 15:05:52 -08001878
1879 if (unlikely(valid_block_count > sbi->user_block_count)) {
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001880 spin_unlock(&sbi->stat_lock);
Chao Yu09c3a722017-07-09 00:13:07 +08001881 goto enospc;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001882 }
1883
Gu Zhengef86d702013-11-19 18:03:38 +08001884 valid_node_count = sbi->total_valid_node_count + 1;
Chao Yucfb271d2013-12-05 17:15:22 +08001885 if (unlikely(valid_node_count > sbi->total_node_count)) {
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001886 spin_unlock(&sbi->stat_lock);
Chao Yu09c3a722017-07-09 00:13:07 +08001887 goto enospc;
Chao Yud9bfbbb2017-07-06 01:11:31 +08001888 }
Gu Zhengef86d702013-11-19 18:03:38 +08001889
Gu Zhengef86d702013-11-19 18:03:38 +08001890 sbi->total_valid_node_count++;
1891 sbi->total_valid_block_count++;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001892 spin_unlock(&sbi->stat_lock);
1893
Chao Yu09c3a722017-07-09 00:13:07 +08001894 if (inode) {
1895 if (is_inode)
1896 f2fs_mark_inode_dirty_sync(inode, true);
1897 else
1898 f2fs_i_blocks_write(inode, 1, true, true);
1899 }
1900
Jaegeuk Kim41382ec2016-05-16 11:06:50 -07001901 percpu_counter_inc(&sbi->alloc_valid_block_count);
Chao Yu09c3a722017-07-09 00:13:07 +08001902 return 0;
1903
1904enospc:
1905 if (quota)
1906 dquot_release_reservation_block(inode, 1);
1907 return -ENOSPC;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001908}
1909
1910static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
Chao Yud9bfbbb2017-07-06 01:11:31 +08001911 struct inode *inode, bool is_inode)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001912{
1913 spin_lock(&sbi->stat_lock);
1914
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001915 f2fs_bug_on(sbi, !sbi->total_valid_block_count);
1916 f2fs_bug_on(sbi, !sbi->total_valid_node_count);
Chao Yud9bfbbb2017-07-06 01:11:31 +08001917 f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001918
Gu Zhengef86d702013-11-19 18:03:38 +08001919 sbi->total_valid_node_count--;
1920 sbi->total_valid_block_count--;
Yunlong Song68ab6f82017-10-27 20:45:05 +08001921 if (sbi->reserved_blocks &&
1922 sbi->current_reserved_blocks < sbi->reserved_blocks)
1923 sbi->current_reserved_blocks++;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001924
1925 spin_unlock(&sbi->stat_lock);
Chao Yu09c3a722017-07-09 00:13:07 +08001926
1927 if (!is_inode)
1928 f2fs_i_blocks_write(inode, 1, false, true);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001929}
1930
1931static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
1932{
Jaegeuk Kim8b8343f2014-02-24 13:00:13 +09001933 return sbi->total_valid_node_count;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001934}
1935
1936static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
1937{
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001938 percpu_counter_inc(&sbi->total_valid_inode_count);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001939}
1940
Jaegeuk Kim0e802202013-11-26 16:36:20 +09001941static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001942{
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001943 percpu_counter_dec(&sbi->total_valid_inode_count);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001944}
1945
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001946static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001947{
Jaegeuk Kim513c5f32016-05-16 11:42:32 -07001948 return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001949}
1950
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001951static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1952 pgoff_t index, bool for_write)
1953{
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -07001954#ifdef CONFIG_F2FS_FAULT_INJECTION
1955 struct page *page = find_lock_page(mapping, index);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001956
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -07001957 if (page)
1958 return page;
1959
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001960 if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
1961 f2fs_show_injection_info(FAULT_PAGE_ALLOC);
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -07001962 return NULL;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001963 }
Jaegeuk Kimc41f3cc32016-04-29 16:17:09 -07001964#endif
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001965 if (!for_write)
1966 return grab_cache_page(mapping, index);
1967 return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1968}
1969
Chao Yu58ddec82017-10-28 16:52:30 +08001970static inline struct page *f2fs_pagecache_get_page(
1971 struct address_space *mapping, pgoff_t index,
1972 int fgp_flags, gfp_t gfp_mask)
1973{
1974#ifdef CONFIG_F2FS_FAULT_INJECTION
1975 if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
1976 f2fs_show_injection_info(FAULT_PAGE_GET);
1977 return NULL;
1978 }
1979#endif
1980 return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
1981}
1982
Jaegeuk Kim6e2c64a2015-10-07 12:28:41 -07001983static inline void f2fs_copy_page(struct page *src, struct page *dst)
1984{
1985 char *src_kaddr = kmap(src);
1986 char *dst_kaddr = kmap(dst);
1987
1988 memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
1989 kunmap(dst);
1990 kunmap(src);
1991}
1992
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001993static inline void f2fs_put_page(struct page *page, int unlock)
1994{
Jaegeuk Kim031fa8c2013-11-28 12:55:13 +09001995 if (!page)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09001996 return;
1997
1998 if (unlock) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001999 f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002000 unlock_page(page);
2001 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002002 put_page(page);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002003}
2004
2005static inline void f2fs_put_dnode(struct dnode_of_data *dn)
2006{
2007 if (dn->node_page)
2008 f2fs_put_page(dn->node_page, 1);
2009 if (dn->inode_page && dn->node_page != dn->inode_page)
2010 f2fs_put_page(dn->inode_page, 0);
2011 dn->node_page = NULL;
2012 dn->inode_page = NULL;
2013}
2014
2015static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
Gu Zhenge8512d22014-03-07 18:43:28 +08002016 size_t size)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002017{
Gu Zhenge8512d22014-03-07 18:43:28 +08002018 return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002019}
2020
Gu Zheng7bd59382013-10-22 14:52:26 +08002021static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
2022 gfp_t flags)
2023{
2024 void *entry;
Gu Zheng7bd59382013-10-22 14:52:26 +08002025
Jaegeuk Kim80c54502015-08-20 08:51:56 -07002026 entry = kmem_cache_alloc(cachep, flags);
2027 if (!entry)
2028 entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
Gu Zheng7bd59382013-10-22 14:52:26 +08002029 return entry;
2030}
2031
Chao Yub5db2de2017-10-28 16:52:31 +08002032static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
2033 int npages, bool no_fail)
Jaegeuk Kim740432f2015-08-14 11:43:56 -07002034{
2035 struct bio *bio;
2036
Chao Yub5db2de2017-10-28 16:52:31 +08002037 if (no_fail) {
2038 /* No failure on bio allocation */
2039 bio = bio_alloc(GFP_NOIO, npages);
2040 if (!bio)
2041 bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
2042 return bio;
2043 }
2044#ifdef CONFIG_F2FS_FAULT_INJECTION
2045 if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
2046 f2fs_show_injection_info(FAULT_ALLOC_BIO);
2047 return NULL;
2048 }
2049#endif
2050 return bio_alloc(GFP_KERNEL, npages);
Jaegeuk Kim740432f2015-08-14 11:43:56 -07002051}
2052
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08002053static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
2054 unsigned long index, void *item)
2055{
2056 while (radix_tree_insert(root, index, item))
2057 cond_resched();
2058}
2059
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002060#define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
2061
2062static inline bool IS_INODE(struct page *page)
2063{
Gu Zheng45590712013-07-15 17:57:38 +08002064 struct f2fs_node *p = F2FS_NODE(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002065
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002066 return RAW_IS_INODE(p);
2067}
2068
Chao Yufbcf9312017-07-19 00:19:06 +08002069static inline int offset_in_addr(struct f2fs_inode *i)
2070{
2071 return (i->i_inline & F2FS_EXTRA_ATTR) ?
2072 (le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
2073}
2074
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002075static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
2076{
2077 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
2078}
2079
Chao Yufbcf9312017-07-19 00:19:06 +08002080static inline int f2fs_has_extra_attr(struct inode *inode);
2081static inline block_t datablock_addr(struct inode *inode,
2082 struct page *node_page, unsigned int offset)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002083{
2084 struct f2fs_node *raw_node;
2085 __le32 *addr_array;
Chao Yufbcf9312017-07-19 00:19:06 +08002086 int base = 0;
2087 bool is_inode = IS_INODE(node_page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002088
Gu Zheng45590712013-07-15 17:57:38 +08002089 raw_node = F2FS_NODE(node_page);
Chao Yufbcf9312017-07-19 00:19:06 +08002090
2091 /* from GC path only */
LiFan8234ed52017-11-28 20:17:41 +08002092 if (is_inode) {
2093 if (!inode)
Chao Yufbcf9312017-07-19 00:19:06 +08002094 base = offset_in_addr(&raw_node->i);
LiFan8234ed52017-11-28 20:17:41 +08002095 else if (f2fs_has_extra_attr(inode))
2096 base = get_extra_isize(inode);
Chao Yufbcf9312017-07-19 00:19:06 +08002097 }
2098
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002099 addr_array = blkaddr_in_node(raw_node);
Chao Yufbcf9312017-07-19 00:19:06 +08002100 return le32_to_cpu(addr_array[base + offset]);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002101}
2102
2103static inline int f2fs_test_bit(unsigned int nr, char *addr)
2104{
2105 int mask;
2106
2107 addr += (nr >> 3);
2108 mask = 1 << (7 - (nr & 0x07));
2109 return mask & *addr;
2110}
2111
Jaegeuk Kima66cdd92015-04-30 22:37:50 -07002112static inline void f2fs_set_bit(unsigned int nr, char *addr)
2113{
2114 int mask;
2115
2116 addr += (nr >> 3);
2117 mask = 1 << (7 - (nr & 0x07));
2118 *addr |= mask;
2119}
2120
2121static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2122{
2123 int mask;
2124
2125 addr += (nr >> 3);
2126 mask = 1 << (7 - (nr & 0x07));
2127 *addr &= ~mask;
2128}
2129
Gu Zheng52aca072014-10-20 17:45:51 +08002130static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002131{
2132 int mask;
2133 int ret;
2134
2135 addr += (nr >> 3);
2136 mask = 1 << (7 - (nr & 0x07));
2137 ret = mask & *addr;
2138 *addr |= mask;
2139 return ret;
2140}
2141
Gu Zheng52aca072014-10-20 17:45:51 +08002142static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002143{
2144 int mask;
2145 int ret;
2146
2147 addr += (nr >> 3);
2148 mask = 1 << (7 - (nr & 0x07));
2149 ret = mask & *addr;
2150 *addr &= ~mask;
2151 return ret;
2152}
2153
Gu Zhengc6ac4c02014-10-20 17:45:50 +08002154static inline void f2fs_change_bit(unsigned int nr, char *addr)
2155{
2156 int mask;
2157
2158 addr += (nr >> 3);
2159 mask = 1 << (7 - (nr & 0x07));
2160 *addr ^= mask;
2161}
2162
Chao Yu9bafde62018-04-03 15:08:17 +08002163/*
2164 * Inode flags
2165 */
2166#define F2FS_SECRM_FL 0x00000001 /* Secure deletion */
2167#define F2FS_UNRM_FL 0x00000002 /* Undelete */
2168#define F2FS_COMPR_FL 0x00000004 /* Compress file */
2169#define F2FS_SYNC_FL 0x00000008 /* Synchronous updates */
2170#define F2FS_IMMUTABLE_FL 0x00000010 /* Immutable file */
2171#define F2FS_APPEND_FL 0x00000020 /* writes to file may only append */
2172#define F2FS_NODUMP_FL 0x00000040 /* do not dump file */
2173#define F2FS_NOATIME_FL 0x00000080 /* do not update atime */
2174/* Reserved for compression usage... */
2175#define F2FS_DIRTY_FL 0x00000100
2176#define F2FS_COMPRBLK_FL 0x00000200 /* One or more compressed clusters */
2177#define F2FS_NOCOMPR_FL 0x00000400 /* Don't compress */
2178#define F2FS_ENCRYPT_FL 0x00000800 /* encrypted file */
2179/* End compression flags --- maybe not all used */
2180#define F2FS_INDEX_FL 0x00001000 /* hash-indexed directory */
2181#define F2FS_IMAGIC_FL 0x00002000 /* AFS directory */
2182#define F2FS_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
2183#define F2FS_NOTAIL_FL 0x00008000 /* file tail should not be merged */
2184#define F2FS_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
2185#define F2FS_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
2186#define F2FS_HUGE_FILE_FL 0x00040000 /* Set to each huge file */
2187#define F2FS_EXTENTS_FL 0x00080000 /* Inode uses extents */
2188#define F2FS_EA_INODE_FL 0x00200000 /* Inode used for large EA */
2189#define F2FS_EOFBLOCKS_FL 0x00400000 /* Blocks allocated beyond EOF */
2190#define F2FS_INLINE_DATA_FL 0x10000000 /* Inode has inline data. */
2191#define F2FS_PROJINHERIT_FL 0x20000000 /* Create with parents projid */
2192#define F2FS_RESERVED_FL 0x80000000 /* reserved for ext4 lib */
2193
2194#define F2FS_FL_USER_VISIBLE 0x304BDFFF /* User visible flags */
2195#define F2FS_FL_USER_MODIFIABLE 0x204BC0FF /* User modifiable flags */
2196
2197/* Flags we can manipulate with through F2FS_IOC_FSSETXATTR */
2198#define F2FS_FL_XFLAG_VISIBLE (F2FS_SYNC_FL | \
2199 F2FS_IMMUTABLE_FL | \
2200 F2FS_APPEND_FL | \
2201 F2FS_NODUMP_FL | \
2202 F2FS_NOATIME_FL | \
2203 F2FS_PROJINHERIT_FL)
2204
2205/* Flags that should be inherited by new inodes from their parent. */
2206#define F2FS_FL_INHERITED (F2FS_SECRM_FL | F2FS_UNRM_FL | F2FS_COMPR_FL |\
2207 F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL |\
2208 F2FS_NOCOMPR_FL | F2FS_JOURNAL_DATA_FL |\
2209 F2FS_NOTAIL_FL | F2FS_DIRSYNC_FL |\
2210 F2FS_PROJINHERIT_FL)
2211
2212/* Flags that are appropriate for regular files (all but dir-specific ones). */
2213#define F2FS_REG_FLMASK (~(F2FS_DIRSYNC_FL | F2FS_TOPDIR_FL))
2214
2215/* Flags that are appropriate for non-directories/regular files. */
2216#define F2FS_OTHER_FLMASK (F2FS_NODUMP_FL | F2FS_NOATIME_FL)
Chao Yu5647b302017-07-26 00:01:41 +08002217
2218static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
2219{
2220 if (S_ISDIR(mode))
2221 return flags;
2222 else if (S_ISREG(mode))
2223 return flags & F2FS_REG_FLMASK;
2224 else
2225 return flags & F2FS_OTHER_FLMASK;
2226}
2227
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002228/* used for f2fs_inode_info->flags */
2229enum {
2230 FI_NEW_INODE, /* indicate newly allocated inode */
Jaegeuk Kimb3783872013-06-10 09:17:01 +09002231 FI_DIRTY_INODE, /* indicate inode is dirty or not */
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07002232 FI_AUTO_RECOVER, /* indicate inode is recoverable */
Jaegeuk Kimed57c272014-04-15 11:19:28 +09002233 FI_DIRTY_DIR, /* indicate directory has dirty pages */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002234 FI_INC_LINK, /* need to increment i_nlink */
2235 FI_ACL_MODE, /* indicate acl mode */
2236 FI_NO_ALLOC, /* should not allocate any blocks */
Jaegeuk Kimc9b63bd2015-06-23 10:36:08 -07002237 FI_FREE_NID, /* free allocated nide */
Jaegeuk Kimc11abd12013-11-19 10:41:54 +09002238 FI_NO_EXTENT, /* not to use the extent cache */
Jaegeuk Kim444c5802013-08-08 15:16:22 +09002239 FI_INLINE_XATTR, /* used for inline xattr */
Huajun Li1001b342013-11-10 23:13:16 +08002240 FI_INLINE_DATA, /* used for inline data*/
Chao Yu34d67de2014-09-24 18:15:19 +08002241 FI_INLINE_DENTRY, /* used for inline dentry */
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07002242 FI_APPEND_WRITE, /* inode has appended data */
2243 FI_UPDATE_WRITE, /* inode has in-place-update data */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002244 FI_NEED_IPU, /* used for ipu per file */
2245 FI_ATOMIC_FILE, /* indicate atomic file */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002246 FI_ATOMIC_COMMIT, /* indicate the state of atomical committing */
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002247 FI_VOLATILE_FILE, /* indicate volatile file */
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07002248 FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002249 FI_DROP_CACHE, /* drop dirty page cache */
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002250 FI_DATA_EXIST, /* indicate data exists */
Jaegeuk Kim510022a2015-03-30 15:07:16 -07002251 FI_INLINE_DOTS, /* indicate inline dot dentries */
Chao Yud323d002015-10-27 09:53:45 +08002252 FI_DO_DEFRAG, /* indicate defragment is running */
Chao Yuc227f912015-12-16 13:09:20 +08002253 FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002254 FI_NO_PREALLOC, /* indicate skipped preallocated blocks */
2255 FI_HOT_DATA, /* indicate file is hot */
Chao Yufbcf9312017-07-19 00:19:06 +08002256 FI_EXTRA_ATTR, /* indicate file has extra attribute */
Chao Yu5647b302017-07-26 00:01:41 +08002257 FI_PROJ_INHERIT, /* indicate file inherits projectid */
Jaegeuk Kim9ce3d6b2017-12-07 16:25:39 -08002258 FI_PIN_FILE, /* indicate file should not be gced */
Chao Yucaf10c62018-05-07 20:28:54 +08002259 FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002260};
2261
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002262static inline void __mark_inode_dirty_flag(struct inode *inode,
2263 int flag, bool set)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002264{
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002265 switch (flag) {
2266 case FI_INLINE_XATTR:
2267 case FI_INLINE_DATA:
2268 case FI_INLINE_DENTRY:
Daeho Jeong9ef5e652018-01-11 11:26:19 +09002269 case FI_NEW_INODE:
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002270 if (set)
2271 return;
2272 case FI_DATA_EXIST:
2273 case FI_INLINE_DOTS:
Jaegeuk Kim9ce3d6b2017-12-07 16:25:39 -08002274 case FI_PIN_FILE:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002275 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002276 }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002277}
2278
Jaegeuk Kim91942322016-05-20 10:13:22 -07002279static inline void set_inode_flag(struct inode *inode, int flag)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002280{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002281 if (!test_bit(flag, &F2FS_I(inode)->flags))
2282 set_bit(flag, &F2FS_I(inode)->flags);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002283 __mark_inode_dirty_flag(inode, flag, true);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002284}
2285
Jaegeuk Kim91942322016-05-20 10:13:22 -07002286static inline int is_inode_flag_set(struct inode *inode, int flag)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002287{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002288 return test_bit(flag, &F2FS_I(inode)->flags);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002289}
2290
Jaegeuk Kim91942322016-05-20 10:13:22 -07002291static inline void clear_inode_flag(struct inode *inode, int flag)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002292{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002293 if (test_bit(flag, &F2FS_I(inode)->flags))
2294 clear_bit(flag, &F2FS_I(inode)->flags);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002295 __mark_inode_dirty_flag(inode, flag, false);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002296}
2297
Jaegeuk Kim91942322016-05-20 10:13:22 -07002298static inline void set_acl_inode(struct inode *inode, umode_t mode)
Jaegeuk Kim444c5802013-08-08 15:16:22 +09002299{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002300 F2FS_I(inode)->i_acl_mode = mode;
2301 set_inode_flag(inode, FI_ACL_MODE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002302 f2fs_mark_inode_dirty_sync(inode, false);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002303}
2304
Jaegeuk Kima1961242016-05-20 09:43:20 -07002305static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2306{
2307 if (inc)
2308 inc_nlink(inode);
2309 else
2310 drop_nlink(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002311 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kima1961242016-05-20 09:43:20 -07002312}
2313
Jaegeuk Kim8edd03c2016-05-20 09:26:06 -07002314static inline void f2fs_i_blocks_write(struct inode *inode,
Chao Yu09c3a722017-07-09 00:13:07 +08002315 block_t diff, bool add, bool claim)
Jaegeuk Kim8edd03c2016-05-20 09:26:06 -07002316{
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07002317 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
2318 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
2319
Chao Yu09c3a722017-07-09 00:13:07 +08002320 /* add = 1, claim = 1 should be dquot_reserve_block in pair */
2321 if (add) {
2322 if (claim)
2323 dquot_claim_block(inode, diff);
2324 else
2325 dquot_alloc_block_nofail(inode, diff);
2326 } else {
2327 dquot_free_block(inode, diff);
2328 }
2329
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002330 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07002331 if (clean || recover)
2332 set_inode_flag(inode, FI_AUTO_RECOVER);
Jaegeuk Kim8edd03c2016-05-20 09:26:06 -07002333}
2334
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002335static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2336{
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07002337 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
2338 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
2339
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002340 if (i_size_read(inode) == i_size)
2341 return;
2342
2343 i_size_write(inode, i_size);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002344 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07002345 if (clean || recover)
2346 set_inode_flag(inode, FI_AUTO_RECOVER);
2347}
2348
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002349static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2350{
2351 F2FS_I(inode)->i_current_depth = depth;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002352 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002353}
2354
Jaegeuk Kim9ce3d6b2017-12-07 16:25:39 -08002355static inline void f2fs_i_gc_failures_write(struct inode *inode,
2356 unsigned int count)
2357{
Chao Yucaf10c62018-05-07 20:28:54 +08002358 F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
Jaegeuk Kim9ce3d6b2017-12-07 16:25:39 -08002359 f2fs_mark_inode_dirty_sync(inode, true);
2360}
2361
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002362static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2363{
2364 F2FS_I(inode)->i_xattr_nid = xnid;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002365 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002366}
2367
2368static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2369{
2370 F2FS_I(inode)->i_pino = pino;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002371 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002372}
2373
Jaegeuk Kim91942322016-05-20 10:13:22 -07002374static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
Jaegeuk Kim444c5802013-08-08 15:16:22 +09002375{
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002376 struct f2fs_inode_info *fi = F2FS_I(inode);
2377
Jaegeuk Kim444c5802013-08-08 15:16:22 +09002378 if (ri->i_inline & F2FS_INLINE_XATTR)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002379 set_bit(FI_INLINE_XATTR, &fi->flags);
Huajun Li1001b342013-11-10 23:13:16 +08002380 if (ri->i_inline & F2FS_INLINE_DATA)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002381 set_bit(FI_INLINE_DATA, &fi->flags);
Chao Yu34d67de2014-09-24 18:15:19 +08002382 if (ri->i_inline & F2FS_INLINE_DENTRY)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002383 set_bit(FI_INLINE_DENTRY, &fi->flags);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002384 if (ri->i_inline & F2FS_DATA_EXIST)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002385 set_bit(FI_DATA_EXIST, &fi->flags);
Jaegeuk Kim510022a2015-03-30 15:07:16 -07002386 if (ri->i_inline & F2FS_INLINE_DOTS)
Jaegeuk Kim205b9822016-05-20 09:52:20 -07002387 set_bit(FI_INLINE_DOTS, &fi->flags);
Chao Yufbcf9312017-07-19 00:19:06 +08002388 if (ri->i_inline & F2FS_EXTRA_ATTR)
2389 set_bit(FI_EXTRA_ATTR, &fi->flags);
Jaegeuk Kim9ce3d6b2017-12-07 16:25:39 -08002390 if (ri->i_inline & F2FS_PIN_FILE)
2391 set_bit(FI_PIN_FILE, &fi->flags);
Jaegeuk Kim444c5802013-08-08 15:16:22 +09002392}
2393
Jaegeuk Kim91942322016-05-20 10:13:22 -07002394static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
Jaegeuk Kim444c5802013-08-08 15:16:22 +09002395{
2396 ri->i_inline = 0;
2397
Jaegeuk Kim91942322016-05-20 10:13:22 -07002398 if (is_inode_flag_set(inode, FI_INLINE_XATTR))
Jaegeuk Kim444c5802013-08-08 15:16:22 +09002399 ri->i_inline |= F2FS_INLINE_XATTR;
Jaegeuk Kim91942322016-05-20 10:13:22 -07002400 if (is_inode_flag_set(inode, FI_INLINE_DATA))
Huajun Li1001b342013-11-10 23:13:16 +08002401 ri->i_inline |= F2FS_INLINE_DATA;
Jaegeuk Kim91942322016-05-20 10:13:22 -07002402 if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
Chao Yu34d67de2014-09-24 18:15:19 +08002403 ri->i_inline |= F2FS_INLINE_DENTRY;
Jaegeuk Kim91942322016-05-20 10:13:22 -07002404 if (is_inode_flag_set(inode, FI_DATA_EXIST))
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002405 ri->i_inline |= F2FS_DATA_EXIST;
Jaegeuk Kim91942322016-05-20 10:13:22 -07002406 if (is_inode_flag_set(inode, FI_INLINE_DOTS))
Jaegeuk Kim510022a2015-03-30 15:07:16 -07002407 ri->i_inline |= F2FS_INLINE_DOTS;
Chao Yufbcf9312017-07-19 00:19:06 +08002408 if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
2409 ri->i_inline |= F2FS_EXTRA_ATTR;
Jaegeuk Kim9ce3d6b2017-12-07 16:25:39 -08002410 if (is_inode_flag_set(inode, FI_PIN_FILE))
2411 ri->i_inline |= F2FS_PIN_FILE;
Chao Yufbcf9312017-07-19 00:19:06 +08002412}
2413
2414static inline int f2fs_has_extra_attr(struct inode *inode)
2415{
2416 return is_inode_flag_set(inode, FI_EXTRA_ATTR);
Jaegeuk Kim444c5802013-08-08 15:16:22 +09002417}
2418
Chao Yu987c7c32014-03-12 15:59:03 +08002419static inline int f2fs_has_inline_xattr(struct inode *inode)
2420{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002421 return is_inode_flag_set(inode, FI_INLINE_XATTR);
Chao Yu987c7c32014-03-12 15:59:03 +08002422}
2423
Chao Yu81ca7352016-01-26 15:39:35 +08002424static inline unsigned int addrs_per_inode(struct inode *inode)
Jaegeuk Kimde936532013-08-12 21:08:03 +09002425{
Chao Yu3aa46e22018-01-17 16:31:36 +08002426 return CUR_ADDRS_PER_INODE(inode) - get_inline_xattr_addrs(inode);
Jaegeuk Kimde936532013-08-12 21:08:03 +09002427}
2428
Chao Yu50ffaa92017-09-06 21:59:50 +08002429static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
Jaegeuk Kim65985d92013-08-14 21:57:27 +09002430{
Chao Yu695fd1e2014-02-27 19:52:21 +08002431 struct f2fs_inode *ri = F2FS_INODE(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002432
Jaegeuk Kim65985d92013-08-14 21:57:27 +09002433 return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
Chao Yu3aa46e22018-01-17 16:31:36 +08002434 get_inline_xattr_addrs(inode)]);
Jaegeuk Kim65985d92013-08-14 21:57:27 +09002435}
2436
2437static inline int inline_xattr_size(struct inode *inode)
2438{
Chao Yu50ffaa92017-09-06 21:59:50 +08002439 return get_inline_xattr_addrs(inode) * sizeof(__le32);
Jaegeuk Kim65985d92013-08-14 21:57:27 +09002440}
2441
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09002442static inline int f2fs_has_inline_data(struct inode *inode)
2443{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002444 return is_inode_flag_set(inode, FI_INLINE_DATA);
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09002445}
2446
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002447static inline int f2fs_exist_data(struct inode *inode)
2448{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002449 return is_inode_flag_set(inode, FI_DATA_EXIST);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002450}
2451
Jaegeuk Kim510022a2015-03-30 15:07:16 -07002452static inline int f2fs_has_inline_dots(struct inode *inode)
2453{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002454 return is_inode_flag_set(inode, FI_INLINE_DOTS);
Jaegeuk Kim510022a2015-03-30 15:07:16 -07002455}
2456
Jaegeuk Kim9ce3d6b2017-12-07 16:25:39 -08002457static inline bool f2fs_is_pinned_file(struct inode *inode)
2458{
2459 return is_inode_flag_set(inode, FI_PIN_FILE);
2460}
2461
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002462static inline bool f2fs_is_atomic_file(struct inode *inode)
2463{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002464 return is_inode_flag_set(inode, FI_ATOMIC_FILE);
Jaegeuk Kim88b88a62014-10-06 17:39:50 -07002465}
2466
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002467static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
2468{
2469 return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
2470}
2471
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002472static inline bool f2fs_is_volatile_file(struct inode *inode)
2473{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002474 return is_inode_flag_set(inode, FI_VOLATILE_FILE);
Jaegeuk Kim02a13352014-10-06 16:11:16 -07002475}
2476
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07002477static inline bool f2fs_is_first_block_written(struct inode *inode)
2478{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002479 return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07002480}
2481
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002482static inline bool f2fs_is_drop_cache(struct inode *inode)
2483{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002484 return is_inode_flag_set(inode, FI_DROP_CACHE);
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002485}
2486
Chao Yue84f88e2017-07-19 00:19:05 +08002487static inline void *inline_data_addr(struct inode *inode, struct page *page)
Huajun Li1001b342013-11-10 23:13:16 +08002488{
Chao Yu695fd1e2014-02-27 19:52:21 +08002489 struct f2fs_inode *ri = F2FS_INODE(page);
Chao Yufbcf9312017-07-19 00:19:06 +08002490 int extra_size = get_extra_isize(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002491
Chao Yufbcf9312017-07-19 00:19:06 +08002492 return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
Huajun Li1001b342013-11-10 23:13:16 +08002493}
2494
Chao Yu34d67de2014-09-24 18:15:19 +08002495static inline int f2fs_has_inline_dentry(struct inode *inode)
2496{
Jaegeuk Kim91942322016-05-20 10:13:22 -07002497 return is_inode_flag_set(inode, FI_INLINE_DENTRY);
Chao Yu34d67de2014-09-24 18:15:19 +08002498}
2499
Jaegeuk Kimb5492af72015-04-20 13:44:41 -07002500static inline int is_file(struct inode *inode, int type)
2501{
2502 return F2FS_I(inode)->i_advise & type;
2503}
2504
2505static inline void set_file(struct inode *inode, int type)
2506{
2507 F2FS_I(inode)->i_advise |= type;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002508 f2fs_mark_inode_dirty_sync(inode, true);
Jaegeuk Kimb5492af72015-04-20 13:44:41 -07002509}
2510
2511static inline void clear_file(struct inode *inode, int type)
2512{
2513 F2FS_I(inode)->i_advise &= ~type;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002514 f2fs_mark_inode_dirty_sync(inode, true);
2515}
2516
2517static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
2518{
Chao Yu33fdebb2017-10-09 17:55:19 +08002519 bool ret;
2520
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002521 if (dsync) {
2522 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002523
2524 spin_lock(&sbi->inode_lock[DIRTY_META]);
2525 ret = list_empty(&F2FS_I(inode)->gdirty_list);
2526 spin_unlock(&sbi->inode_lock[DIRTY_META]);
2527 return ret;
2528 }
2529 if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
2530 file_keep_isize(inode) ||
Junling Zheng44789702018-03-29 19:27:12 +08002531 i_size_read(inode) & ~PAGE_MASK)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002532 return false;
Chao Yu33fdebb2017-10-09 17:55:19 +08002533
Jaegeuk Kimac389af2018-03-29 22:50:41 -07002534 if (!timespec_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
2535 return false;
2536 if (!timespec_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
2537 return false;
2538 if (!timespec_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
2539 return false;
2540 if (!timespec_equal(F2FS_I(inode)->i_disk_time + 3,
2541 &F2FS_I(inode)->i_crtime))
2542 return false;
2543
Chao Yu33fdebb2017-10-09 17:55:19 +08002544 down_read(&F2FS_I(inode)->i_sem);
2545 ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2546 up_read(&F2FS_I(inode)->i_sem);
2547
2548 return ret;
Jaegeuk Kimb5492af72015-04-20 13:44:41 -07002549}
2550
Chao Yu8f711c32018-03-01 23:40:31 +08002551static inline bool f2fs_readonly(struct super_block *sb)
Jaegeuk Kim77888c12013-05-20 20:28:47 +09002552{
2553 return sb->s_flags & MS_RDONLY;
2554}
2555
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07002556static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2557{
Chao Yuaaec2b12016-09-20 11:04:18 +08002558 return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
Jaegeuk Kim1e968fd2014-08-11 16:49:25 -07002559}
2560
Jaegeuk Kimeaa693f2015-04-26 00:15:29 -07002561static inline bool is_dot_dotdot(const struct qstr *str)
2562{
2563 if (str->len == 1 && str->name[0] == '.')
2564 return true;
2565
2566 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2567 return true;
2568
2569 return false;
2570}
2571
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07002572static inline bool f2fs_may_extent_tree(struct inode *inode)
2573{
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07002574 if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
Jaegeuk Kim91942322016-05-20 10:13:22 -07002575 is_inode_flag_set(inode, FI_NO_EXTENT))
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07002576 return false;
2577
Al Viro886f56f2015-12-05 03:56:06 -05002578 return S_ISREG(inode->i_mode);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -07002579}
2580
Chao Yu1ecc0c52016-09-23 21:30:09 +08002581static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
2582 size_t size, gfp_t flags)
Jaegeuk Kim0414b002016-04-29 15:16:42 -07002583{
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -07002584#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002585 if (time_to_inject(sbi, FAULT_KMALLOC)) {
2586 f2fs_show_injection_info(FAULT_KMALLOC);
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -07002587 return NULL;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002588 }
Jaegeuk Kim2c63fea2016-04-29 15:49:56 -07002589#endif
Jaegeuk Kim0414b002016-04-29 15:16:42 -07002590 return kmalloc(size, flags);
2591}
2592
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002593static inline void *kvmalloc(size_t size, gfp_t flags)
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002594{
2595 void *ret;
2596
2597 ret = kmalloc(size, flags | __GFP_NOWARN);
2598 if (!ret)
2599 ret = __vmalloc(size, flags, PAGE_KERNEL);
2600 return ret;
2601}
2602
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002603static inline void *kvzalloc(size_t size, gfp_t flags)
Jaegeuk Kim39307a82015-09-22 13:50:47 -07002604{
2605 void *ret;
2606
2607 ret = kzalloc(size, flags | __GFP_NOWARN);
2608 if (!ret)
2609 ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
2610 return ret;
2611}
2612
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002613static inline int wbc_to_write_flags(struct writeback_control *wbc)
2614{
2615 if (wbc->sync_mode == WB_SYNC_ALL)
2616 return REQ_SYNC;
2617 else if (wbc->for_kupdate || wbc->for_background)
2618 return 0;
2619
2620 return 0;
2621}
2622
Chao Yue585ca22017-11-30 19:28:17 +08002623static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2624 size_t size, gfp_t flags)
2625{
2626 return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2627}
2628
Chao Yuead52592017-11-30 19:28:18 +08002629static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2630 size_t size, gfp_t flags)
2631{
2632#ifdef CONFIG_F2FS_FAULT_INJECTION
2633 if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2634 f2fs_show_injection_info(FAULT_KVMALLOC);
2635 return NULL;
2636 }
2637#endif
2638 return kvmalloc(size, flags);
2639}
2640
2641static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2642 size_t size, gfp_t flags)
2643{
2644 return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2645}
2646
Chao Yufbcf9312017-07-19 00:19:06 +08002647static inline int get_extra_isize(struct inode *inode)
Chao Yue84f88e2017-07-19 00:19:05 +08002648{
Chao Yufbcf9312017-07-19 00:19:06 +08002649 return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
Chao Yue84f88e2017-07-19 00:19:05 +08002650}
2651
Chao Yu50ffaa92017-09-06 21:59:50 +08002652static inline int get_inline_xattr_addrs(struct inode *inode)
2653{
2654 return F2FS_I(inode)->i_inline_xattr_size;
2655}
2656
Chao Yu6b4d6a82018-05-30 00:20:41 +08002657#define f2fs_get_inode_mode(i) \
Jaegeuk Kim91942322016-05-20 10:13:22 -07002658 ((is_inode_flag_set(i, FI_ACL_MODE)) ? \
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -08002659 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2660
Chao Yufbcf9312017-07-19 00:19:06 +08002661#define F2FS_TOTAL_EXTRA_ATTR_SIZE \
2662 (offsetof(struct f2fs_inode, i_extra_end) - \
2663 offsetof(struct f2fs_inode, i_extra_isize)) \
2664
Chao Yu5647b302017-07-26 00:01:41 +08002665#define F2FS_OLD_ATTRIBUTE_SIZE (offsetof(struct f2fs_inode, i_addr))
2666#define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field) \
2667 ((offsetof(typeof(*f2fs_inode), field) + \
2668 sizeof((f2fs_inode)->field)) \
2669 <= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize)) \
2670
Chao Yuc0fe4882017-08-02 23:21:48 +08002671static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2672{
2673 int i;
2674
2675 spin_lock(&sbi->iostat_lock);
2676 for (i = 0; i < NR_IO_TYPE; i++)
2677 sbi->write_iostat[i] = 0;
2678 spin_unlock(&sbi->iostat_lock);
2679}
2680
2681static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2682 enum iostat_type type, unsigned long long io_bytes)
2683{
2684 if (!sbi->iostat_enable)
2685 return;
2686 spin_lock(&sbi->iostat_lock);
2687 sbi->write_iostat[type] += io_bytes;
2688
2689 if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2690 sbi->write_iostat[APP_BUFFERED_IO] =
2691 sbi->write_iostat[APP_WRITE_IO] -
2692 sbi->write_iostat[APP_DIRECT_IO];
2693 spin_unlock(&sbi->iostat_lock);
2694}
2695
Chao Yu158d9bb2018-05-23 22:25:08 +08002696static inline bool is_valid_blkaddr(block_t blkaddr)
2697{
2698 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
2699 return false;
2700 return true;
2701}
2702
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002703/*
2704 * file.c
2705 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002706int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002707void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
2708int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002709int f2fs_truncate(struct inode *inode);
2710int f2fs_getattr(struct vfsmount *mnt, struct dentry *dentry,
2711 struct kstat *stat);
2712int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002713int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2714void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
Chao Yu6bce9632018-01-11 14:42:30 +08002715int f2fs_precache_extents(struct inode *inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002716long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2717long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Jaegeuk Kim9ce3d6b2017-12-07 16:25:39 -08002718int f2fs_pin_file_control(struct inode *inode, bool inc);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002719
2720/*
2721 * inode.c
2722 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002723void f2fs_set_inode_flags(struct inode *inode);
Chao Yu43101252017-07-31 20:19:09 +08002724bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2725void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002726struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2727struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002728int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2729void f2fs_update_inode(struct inode *inode, struct page *node_page);
2730void f2fs_update_inode_page(struct inode *inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002731int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2732void f2fs_evict_inode(struct inode *inode);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002733void f2fs_handle_failed_inode(struct inode *inode);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002734
2735/*
2736 * namei.c
2737 */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002738int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
Chao Yuac734c42018-02-28 17:07:27 +08002739 bool hot, bool set);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002740struct dentry *f2fs_get_parent(struct dentry *child);
2741
2742/*
2743 * dir.c
2744 */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002745unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
2746struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002747 f2fs_hash_t namehash, int *max_slots,
2748 struct f2fs_dentry_ptr *d);
2749int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2750 unsigned int start_pos, struct fscrypt_str *fstr);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002751void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002752 struct f2fs_dentry_ptr *d);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002753struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002754 const struct qstr *new_name,
2755 const struct qstr *orig_name, struct page *dpage);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002756void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002757 unsigned int current_depth);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002758int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002759void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2760struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2761 struct fscrypt_name *fname, struct page **res_page);
2762struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2763 const struct qstr *child, struct page **res_page);
2764struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2765ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2766 struct page **page);
2767void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2768 struct page *page, struct inode *inode);
2769void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2770 const struct qstr *name, f2fs_hash_t name_hash,
2771 unsigned int bit_pos);
2772int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2773 const struct qstr *orig_name,
2774 struct inode *inode, nid_t ino, umode_t mode);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002775int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002776 struct inode *inode, nid_t ino, umode_t mode);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002777int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002778 struct inode *inode, nid_t ino, umode_t mode);
2779void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2780 struct inode *dir, struct inode *inode);
2781int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2782bool f2fs_empty_dir(struct inode *dir);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002783
Al Virob7f7a5e2013-01-25 16:15:43 -05002784static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2785{
Chao Yu6b4d6a82018-05-30 00:20:41 +08002786 return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
Jaegeuk Kim510022a2015-03-30 15:07:16 -07002787 inode, inode->i_ino, inode->i_mode);
Al Virob7f7a5e2013-01-25 16:15:43 -05002788}
2789
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002790/*
2791 * super.c
2792 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002793int f2fs_inode_dirtied(struct inode *inode, bool sync);
2794void f2fs_inode_synced(struct inode *inode);
Jaegeuk Kim0108c482017-10-06 09:14:28 -07002795int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
Chao Yu41ad73f2017-08-08 10:54:31 +08002796void f2fs_quota_off_umount(struct super_block *sb);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002797int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2798int f2fs_sync_fs(struct super_block *sb, int sync);
Namjae Jeona07ef782012-12-30 14:52:05 +09002799extern __printf(3, 4)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002800void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002801int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002802
2803/*
2804 * hash.c
2805 */
Jaegeuk Kim8daed212017-04-24 10:00:08 -07002806f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
2807 struct fscrypt_name *fname);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002808
2809/*
2810 * node.c
2811 */
2812struct dnode_of_data;
2813struct node_info;
2814
Chao Yu6b4d6a82018-05-30 00:20:41 +08002815int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
2816bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
2817int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2818bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2819bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2820void f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
2821 struct node_info *ni);
2822pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2823int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2824int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
2825int f2fs_truncate_xattr_node(struct inode *inode);
2826int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2827int f2fs_remove_inode_page(struct inode *inode);
2828struct page *f2fs_new_inode_page(struct inode *inode);
2829struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
2830void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2831struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2832struct page *f2fs_get_node_page_ra(struct page *parent, int start);
2833void f2fs_move_node_page(struct page *node_page, int gc_type);
2834int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002835 struct writeback_control *wbc, bool atomic);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002836int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
2837 struct writeback_control *wbc,
Chao Yuc0fe4882017-08-02 23:21:48 +08002838 bool do_balance, enum iostat_type io_type);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002839void f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2840bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2841void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2842void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2843int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2844void f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
2845int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
2846int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2847void f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002848 unsigned int segno, struct f2fs_summary_block *sum);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002849void f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2850int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
2851void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
2852int __init f2fs_create_node_manager_caches(void);
2853void f2fs_destroy_node_manager_caches(void);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002854
2855/*
2856 * segment.c
2857 */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002858bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
2859void f2fs_register_inmem_page(struct inode *inode, struct page *page);
2860void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
2861void f2fs_drop_inmem_pages(struct inode *inode);
2862void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
2863int f2fs_commit_inmem_pages(struct inode *inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002864void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2865void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
Chao Yuf014be82017-09-29 13:59:38 +08002866int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002867int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
Chao Yufd6422e2017-09-29 13:59:39 +08002868int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002869void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2870void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2871bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
2872void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
2873void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
Chao Yu1e5305a2017-10-04 09:08:37 +08002874bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002875void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
2876 struct cp_control *cpc);
2877void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
2878int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2879void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002880int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002881bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
2882 struct cp_control *cpc);
2883struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2884void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
2885 block_t blk_addr);
2886void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
Chao Yuc0fe4882017-08-02 23:21:48 +08002887 enum iostat_type io_type);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002888void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2889void f2fs_outplace_write_data(struct dnode_of_data *dn,
2890 struct f2fs_io_info *fio);
2891int f2fs_inplace_write_data(struct f2fs_io_info *fio);
2892void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002893 block_t old_blkaddr, block_t new_blkaddr,
2894 bool recover_curseg, bool recover_newaddr);
2895void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2896 block_t old_addr, block_t new_addr,
2897 unsigned char version, bool recover_curseg,
2898 bool recover_newaddr);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002899void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002900 block_t old_blkaddr, block_t *new_blkaddr,
Chao Yuc52dc0f2017-05-19 23:37:01 +08002901 struct f2fs_summary *sum, int type,
2902 struct f2fs_io_info *fio, bool add_list);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002903void f2fs_wait_on_page_writeback(struct page *page,
2904 enum page_type type, bool ordered);
Jaegeuk Kimfb605d02017-09-05 17:04:35 -07002905void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002906void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2907void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2908int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002909 unsigned int val, int alloc);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002910void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2911int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
2912void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
2913int __init f2fs_create_segment_manager_caches(void);
2914void f2fs_destroy_segment_manager_caches(void);
2915int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
2916enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
2917 enum page_type type, enum temp_type temp);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002918
2919/*
2920 * checkpoint.c
2921 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002922void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002923struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2924struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2925struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2926bool f2fs_is_valid_meta_blkaddr(struct f2fs_sb_info *sbi,
2927 block_t blkaddr, int type);
2928int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002929 int type, bool sync);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002930void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2931long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
Chao Yuc0fe4882017-08-02 23:21:48 +08002932 long nr_to_write, enum iostat_type io_type);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002933void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2934void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2935void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2936bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
2937void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
Chao Yuf014be82017-09-29 13:59:38 +08002938 unsigned int devidx, int type);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002939bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
Chao Yuf014be82017-09-29 13:59:38 +08002940 unsigned int devidx, int type);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002941int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002942int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
2943void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
2944void f2fs_add_orphan_inode(struct inode *inode);
2945void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2946int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
2947int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
2948void f2fs_update_dirty_page(struct inode *inode, struct page *page);
2949void f2fs_remove_dirty_inode(struct inode *inode);
2950int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2951int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2952void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
2953int __init f2fs_create_checkpoint_caches(void);
2954void f2fs_destroy_checkpoint_caches(void);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002955
2956/*
2957 * data.c
2958 */
Eric Biggersf69e8142018-04-18 11:09:48 -07002959int f2fs_init_post_read_processing(void);
2960void f2fs_destroy_post_read_processing(void);
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002961void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2962void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002963 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002964 enum page_type type);
2965void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002966int f2fs_submit_page_bio(struct f2fs_io_info *fio);
Chao Yu34880e02018-05-28 23:47:18 +08002967void f2fs_submit_page_write(struct f2fs_io_info *fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002968struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2969 block_t blk_addr, struct bio *bio);
2970int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002971void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002972void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002973int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2974int f2fs_reserve_new_block(struct dnode_of_data *dn);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002975int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2976int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2977int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002978struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002979 int op_flags, bool for_write);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002980struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
2981struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002982 bool for_write);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002983struct page *f2fs_get_new_data_page(struct inode *inode,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002984 struct page *ipage, pgoff_t index, bool new_i_size);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002985int f2fs_do_write_data_page(struct f2fs_io_info *fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002986int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2987 int create, int flag);
2988int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2989 u64 start, u64 len);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002990bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
2991bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002992void f2fs_invalidate_page(struct page *page, unsigned int offset,
2993 unsigned int length);
2994int f2fs_release_page(struct page *page, gfp_t wait);
Weichao Guo5b7a4872016-09-20 05:03:27 +08002995#ifdef CONFIG_MIGRATION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002996int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2997 struct page *page, enum migrate_mode mode);
Weichao Guo5b7a4872016-09-20 05:03:27 +08002998#endif
Hyunchul Lee355d2342018-03-08 19:34:38 +09002999bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
Chao Yu6b4d6a82018-05-30 00:20:41 +08003000void f2fs_clear_radix_tree_dirty_tag(struct page *page);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003001
3002/*
3003 * gc.c
3004 */
Chao Yu6b4d6a82018-05-30 00:20:41 +08003005int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
3006void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
3007block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003008int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
3009 unsigned int segno);
Chao Yu6b4d6a82018-05-30 00:20:41 +08003010void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003011
3012/*
3013 * recovery.c
3014 */
Chao Yu6b4d6a82018-05-30 00:20:41 +08003015int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
3016bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003017
3018/*
3019 * debug.c
3020 */
3021#ifdef CONFIG_F2FS_STAT_FS
3022struct f2fs_stat_info {
3023 struct list_head stat_list;
3024 struct f2fs_sb_info *sbi;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003025 int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
3026 int main_area_segs, main_area_sections, main_area_zones;
Chao Yu5b7ee372015-09-30 17:38:48 +08003027 unsigned long long hit_largest, hit_cached, hit_rbtree;
3028 unsigned long long hit_total, total_ext;
Jaegeuk Kimc00ba552015-12-31 15:24:14 -08003029 int ext_tree, zombie_tree, ext_node;
Jaegeuk Kim931ecc22017-11-13 17:46:38 -08003030 int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
3031 int ndirty_data, ndirty_qdata;
Jaegeuk Kim725ba1a2016-10-20 19:09:57 -07003032 int inmem_pages;
Jaegeuk Kim931ecc22017-11-13 17:46:38 -08003033 unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003034 int nats, dirty_nats, sits, dirty_sits;
3035 int free_nids, avail_nids, alloc_nids;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003036 int total_count, utilization;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003037 int bg_gc, nr_wb_cp_data, nr_wb_data;
Chao Yu2f0df252017-09-14 10:18:01 +08003038 int nr_flushing, nr_flushed, flush_list_empty;
3039 int nr_discarding, nr_discarded;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003040 int nr_discard_cmd;
3041 unsigned int undiscard_blks;
3042 int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3043 int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
Yunlei Hef83a2582016-08-18 21:01:18 +08003044 unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003045 unsigned int bimodal, avg_vblocks;
3046 int util_free, util_valid, util_invalid;
3047 int rsvd_segs, overp_segs;
3048 int dirty_count, node_pages, meta_pages;
Jaegeuk Kim42190d22016-01-09 13:45:17 -08003049 int prefree_count, call_count, cp_count, bg_cp_count;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003050 int tot_segs, node_segs, data_segs, free_segs, free_secs;
Changman Leee1235982014-12-23 08:37:39 +09003051 int bg_node_segs, bg_data_segs;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003052 int tot_blks, data_blks, node_blks;
Changman Leee1235982014-12-23 08:37:39 +09003053 int bg_data_blks, bg_node_blks;
Chao Yucaf10c62018-05-07 20:28:54 +08003054 unsigned long long skipped_atomic_files[2];
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003055 int curseg[NR_CURSEG_TYPE];
3056 int cursec[NR_CURSEG_TYPE];
3057 int curzone[NR_CURSEG_TYPE];
3058
3059 unsigned int segment_count[2];
3060 unsigned int block_count[2];
Changman Leeb9a2c252014-12-24 02:16:54 +09003061 unsigned int inplace_count;
Chao Yu9edcdab2015-09-11 14:43:52 +08003062 unsigned long long base_mem, cache_mem, page_mem;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003063};
3064
Gu Zheng963d4f72013-07-12 14:47:11 +08003065static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3066{
Chris Fries6c311ec2014-01-17 14:44:39 -06003067 return (struct f2fs_stat_info *)sbi->stat_info;
Gu Zheng963d4f72013-07-12 14:47:11 +08003068}
3069
Changman Lee942e0be2014-02-13 15:12:29 +09003070#define stat_inc_cp_count(si) ((si)->cp_count++)
Jaegeuk Kim42190d22016-01-09 13:45:17 -08003071#define stat_inc_bg_cp_count(si) ((si)->bg_cp_count++)
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09003072#define stat_inc_call_count(si) ((si)->call_count++)
3073#define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
Chao Yu33fbd512015-12-17 17:14:44 +08003074#define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++)
3075#define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--)
Chao Yu5b7ee372015-09-30 17:38:48 +08003076#define stat_inc_total_hit(sbi) (atomic64_inc(&(sbi)->total_hit_ext))
3077#define stat_inc_rbtree_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_rbtree))
3078#define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest))
3079#define stat_inc_cached_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_cached))
Chao Yud5e8f6c2015-07-15 17:28:53 +08003080#define stat_inc_inline_xattr(inode) \
3081 do { \
3082 if (f2fs_has_inline_xattr(inode)) \
3083 (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \
3084 } while (0)
3085#define stat_dec_inline_xattr(inode) \
3086 do { \
3087 if (f2fs_has_inline_xattr(inode)) \
3088 (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \
3089 } while (0)
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09003090#define stat_inc_inline_inode(inode) \
3091 do { \
3092 if (f2fs_has_inline_data(inode)) \
Chao Yu03e14d52014-12-08 19:08:20 +08003093 (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09003094 } while (0)
3095#define stat_dec_inline_inode(inode) \
3096 do { \
3097 if (f2fs_has_inline_data(inode)) \
Chao Yu03e14d52014-12-08 19:08:20 +08003098 (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +09003099 } while (0)
Jaegeuk Kim3289c062014-10-13 20:00:16 -07003100#define stat_inc_inline_dir(inode) \
3101 do { \
3102 if (f2fs_has_inline_dentry(inode)) \
Chao Yu03e14d52014-12-08 19:08:20 +08003103 (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
Jaegeuk Kim3289c062014-10-13 20:00:16 -07003104 } while (0)
3105#define stat_dec_inline_dir(inode) \
3106 do { \
3107 if (f2fs_has_inline_dentry(inode)) \
Chao Yu03e14d52014-12-08 19:08:20 +08003108 (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
Jaegeuk Kim3289c062014-10-13 20:00:16 -07003109 } while (0)
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +09003110#define stat_inc_seg_type(sbi, curseg) \
3111 ((sbi)->segment_count[(curseg)->alloc_type]++)
3112#define stat_inc_block_count(sbi, curseg) \
3113 ((sbi)->block_count[(curseg)->alloc_type]++)
Changman Leeb9a2c252014-12-24 02:16:54 +09003114#define stat_inc_inplace_blocks(sbi) \
3115 (atomic_inc(&(sbi)->inplace_count))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003116#define stat_inc_atomic_write(inode) \
3117 (atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
3118#define stat_dec_atomic_write(inode) \
3119 (atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
3120#define stat_update_max_atomic_write(inode) \
3121 do { \
3122 int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt); \
3123 int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt); \
3124 if (cur > max) \
3125 atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur); \
3126 } while (0)
3127#define stat_inc_volatile_write(inode) \
3128 (atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3129#define stat_dec_volatile_write(inode) \
3130 (atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3131#define stat_update_max_volatile_write(inode) \
3132 do { \
3133 int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt); \
3134 int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt); \
3135 if (cur > max) \
3136 atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur); \
3137 } while (0)
Changman Leee1235982014-12-23 08:37:39 +09003138#define stat_inc_seg_count(sbi, type, gc_type) \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003139 do { \
Gu Zheng963d4f72013-07-12 14:47:11 +08003140 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003141 si->tot_segs++; \
3142 if ((type) == SUM_TYPE_DATA) { \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003143 si->data_segs++; \
Changman Leee1235982014-12-23 08:37:39 +09003144 si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
3145 } else { \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003146 si->node_segs++; \
Changman Leee1235982014-12-23 08:37:39 +09003147 si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
3148 } \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003149 } while (0)
3150
3151#define stat_inc_tot_blk_count(si, blks) \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003152 ((si)->tot_blks += (blks))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003153
Changman Leee1235982014-12-23 08:37:39 +09003154#define stat_inc_data_blk_count(sbi, blks, gc_type) \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003155 do { \
Gu Zheng963d4f72013-07-12 14:47:11 +08003156 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003157 stat_inc_tot_blk_count(si, blks); \
3158 si->data_blks += (blks); \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003159 si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003160 } while (0)
3161
Changman Leee1235982014-12-23 08:37:39 +09003162#define stat_inc_node_blk_count(sbi, blks, gc_type) \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003163 do { \
Gu Zheng963d4f72013-07-12 14:47:11 +08003164 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003165 stat_inc_tot_blk_count(si, blks); \
3166 si->node_blks += (blks); \
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003167 si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003168 } while (0)
3169
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003170int f2fs_build_stats(struct f2fs_sb_info *sbi);
3171void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
Chao Yu787c7b8c2015-10-29 09:13:04 +08003172int __init f2fs_create_root_stats(void);
Namjae Jeon4589d252013-01-15 19:58:47 +09003173void f2fs_destroy_root_stats(void);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003174#else
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003175#define stat_inc_cp_count(si) do { } while (0)
3176#define stat_inc_bg_cp_count(si) do { } while (0)
3177#define stat_inc_call_count(si) do { } while (0)
3178#define stat_inc_bggc_count(si) do { } while (0)
3179#define stat_inc_dirty_inode(sbi, type) do { } while (0)
3180#define stat_dec_dirty_inode(sbi, type) do { } while (0)
3181#define stat_inc_total_hit(sb) do { } while (0)
3182#define stat_inc_rbtree_node_hit(sb) do { } while (0)
3183#define stat_inc_largest_node_hit(sbi) do { } while (0)
3184#define stat_inc_cached_node_hit(sbi) do { } while (0)
3185#define stat_inc_inline_xattr(inode) do { } while (0)
3186#define stat_dec_inline_xattr(inode) do { } while (0)
3187#define stat_inc_inline_inode(inode) do { } while (0)
3188#define stat_dec_inline_inode(inode) do { } while (0)
3189#define stat_inc_inline_dir(inode) do { } while (0)
3190#define stat_dec_inline_dir(inode) do { } while (0)
3191#define stat_inc_atomic_write(inode) do { } while (0)
3192#define stat_dec_atomic_write(inode) do { } while (0)
3193#define stat_update_max_atomic_write(inode) do { } while (0)
3194#define stat_inc_volatile_write(inode) do { } while (0)
3195#define stat_dec_volatile_write(inode) do { } while (0)
3196#define stat_update_max_volatile_write(inode) do { } while (0)
3197#define stat_inc_seg_type(sbi, curseg) do { } while (0)
3198#define stat_inc_block_count(sbi, curseg) do { } while (0)
3199#define stat_inc_inplace_blocks(sbi) do { } while (0)
3200#define stat_inc_seg_count(sbi, type, gc_type) do { } while (0)
3201#define stat_inc_tot_blk_count(si, blks) do { } while (0)
3202#define stat_inc_data_blk_count(sbi, blks, gc_type) do { } while (0)
3203#define stat_inc_node_blk_count(sbi, blks, gc_type) do { } while (0)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003204
3205static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
3206static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
Chao Yu787c7b8c2015-10-29 09:13:04 +08003207static inline int __init f2fs_create_root_stats(void) { return 0; }
Namjae Jeon4589d252013-01-15 19:58:47 +09003208static inline void f2fs_destroy_root_stats(void) { }
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003209#endif
3210
3211extern const struct file_operations f2fs_dir_operations;
3212extern const struct file_operations f2fs_file_operations;
3213extern const struct inode_operations f2fs_file_inode_operations;
3214extern const struct address_space_operations f2fs_dblock_aops;
3215extern const struct address_space_operations f2fs_node_aops;
3216extern const struct address_space_operations f2fs_meta_aops;
3217extern const struct inode_operations f2fs_dir_inode_operations;
3218extern const struct inode_operations f2fs_symlink_inode_operations;
Jaegeuk Kimcbaf0422015-04-29 15:10:53 -07003219extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003220extern const struct inode_operations f2fs_special_inode_operations;
Chao Yu6b4d6a82018-05-30 00:20:41 +08003221extern struct kmem_cache *f2fs_inode_entry_slab;
Huajun Li1001b342013-11-10 23:13:16 +08003222
Huajun Lie18c65b2013-11-10 23:13:19 +08003223/*
3224 * inline.c
3225 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003226bool f2fs_may_inline_data(struct inode *inode);
3227bool f2fs_may_inline_dentry(struct inode *inode);
Chao Yu6b4d6a82018-05-30 00:20:41 +08003228void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
3229void f2fs_truncate_inline_inode(struct inode *inode,
3230 struct page *ipage, u64 from);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003231int f2fs_read_inline_data(struct inode *inode, struct page *page);
3232int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3233int f2fs_convert_inline_inode(struct inode *inode);
3234int f2fs_write_inline_data(struct inode *inode, struct page *page);
Chao Yu6b4d6a82018-05-30 00:20:41 +08003235bool f2fs_recover_inline_data(struct inode *inode, struct page *npage);
3236struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003237 struct fscrypt_name *fname, struct page **res_page);
Chao Yu6b4d6a82018-05-30 00:20:41 +08003238int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003239 struct page *ipage);
3240int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3241 const struct qstr *orig_name,
3242 struct inode *inode, nid_t ino, umode_t mode);
Chao Yu6b4d6a82018-05-30 00:20:41 +08003243void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
3244 struct page *page, struct inode *dir,
3245 struct inode *inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003246bool f2fs_empty_inline_dir(struct inode *dir);
3247int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3248 struct fscrypt_str *fstr);
3249int f2fs_inline_data_fiemap(struct inode *inode,
3250 struct fiemap_extent_info *fieinfo,
3251 __u64 start, __u64 len);
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07003252
3253/*
Jaegeuk Kim2658e502015-06-19 12:01:21 -07003254 * shrinker.c
3255 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003256unsigned long f2fs_shrink_count(struct shrinker *shrink,
3257 struct shrink_control *sc);
3258unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3259 struct shrink_control *sc);
3260void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3261void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
Jaegeuk Kim2658e502015-06-19 12:01:21 -07003262
3263/*
Chao Yua28ef1f2015-07-08 17:59:36 +08003264 * extent_cache.c
3265 */
Chao Yu6b4d6a82018-05-30 00:20:41 +08003266struct rb_entry *f2fs_lookup_rb_tree(struct rb_root *root,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003267 struct rb_entry *cached_re, unsigned int ofs);
Chao Yu6b4d6a82018-05-30 00:20:41 +08003268struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003269 struct rb_root *root, struct rb_node **parent,
3270 unsigned int ofs);
Chao Yu6b4d6a82018-05-30 00:20:41 +08003271struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root *root,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003272 struct rb_entry *cached_re, unsigned int ofs,
3273 struct rb_entry **prev_entry, struct rb_entry **next_entry,
3274 struct rb_node ***insert_p, struct rb_node **insert_parent,
3275 bool force);
Chao Yu6b4d6a82018-05-30 00:20:41 +08003276bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003277 struct rb_root *root);
3278unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3279bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3280void f2fs_drop_extent_tree(struct inode *inode);
3281unsigned int f2fs_destroy_extent_node(struct inode *inode);
3282void f2fs_destroy_extent_tree(struct inode *inode);
3283bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3284 struct extent_info *ei);
3285void f2fs_update_extent_cache(struct dnode_of_data *dn);
Chao Yu19b2c302015-08-26 20:34:48 +08003286void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003287 pgoff_t fofs, block_t blkaddr, unsigned int len);
Chao Yu6b4d6a82018-05-30 00:20:41 +08003288void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
3289int __init f2fs_create_extent_cache(void);
3290void f2fs_destroy_extent_cache(void);
Chao Yua28ef1f2015-07-08 17:59:36 +08003291
3292/*
Chao Yu57fb30c2017-06-14 17:39:47 +08003293 * sysfs.c
3294 */
Jaegeuk Kim883d5532017-07-26 11:24:13 -07003295int __init f2fs_init_sysfs(void);
3296void f2fs_exit_sysfs(void);
3297int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3298void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
Chao Yu57fb30c2017-06-14 17:39:47 +08003299
3300/*
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07003301 * crypto support
3302 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07003303static inline bool f2fs_encrypted_inode(struct inode *inode)
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07003304{
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07003305 return file_is_encrypt(inode);
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07003306}
3307
Jaegeuk Kima4fb1892017-09-05 16:54:24 -07003308static inline bool f2fs_encrypted_file(struct inode *inode)
3309{
3310 return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
3311}
3312
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07003313static inline void f2fs_set_encrypted_inode(struct inode *inode)
3314{
3315#ifdef CONFIG_F2FS_FS_ENCRYPTION
3316 file_set_encrypt(inode);
Eric Biggers685285b2017-10-09 12:15:35 -07003317 inode->i_flags |= S_ENCRYPTED;
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07003318#endif
3319}
3320
Eric Biggersf69e8142018-04-18 11:09:48 -07003321/*
3322 * Returns true if the reads of the inode's data need to undergo some
3323 * postprocessing step, like decryption or authenticity verification.
3324 */
3325static inline bool f2fs_post_read_required(struct inode *inode)
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07003326{
Eric Biggersf69e8142018-04-18 11:09:48 -07003327 return f2fs_encrypted_file(inode);
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07003328}
3329
Sheng Yong770611e2018-02-06 12:31:17 +08003330#define F2FS_FEATURE_FUNCS(name, flagname) \
3331static inline int f2fs_sb_has_##name(struct super_block *sb) \
3332{ \
3333 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_##flagname); \
Jaegeuk Kimcde4de12015-04-20 13:57:51 -07003334}
Jaegeuk Kimf424f662015-04-20 15:19:06 -07003335
Sheng Yong770611e2018-02-06 12:31:17 +08003336F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3337F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3338F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3339F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3340F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3341F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3342F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3343F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
Sheng Yong9b880fe2018-03-15 18:51:41 +08003344F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
Chao Yu71f8f042018-01-25 14:54:42 +08003345
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003346#ifdef CONFIG_BLK_DEV_ZONED
3347static inline int get_blkz_type(struct f2fs_sb_info *sbi,
3348 struct block_device *bdev, block_t blkaddr)
3349{
3350 unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
3351 int i;
3352
3353 for (i = 0; i < sbi->s_ndevs; i++)
3354 if (FDEV(i).bdev == bdev)
3355 return FDEV(i).blkz_type[zno];
3356 return -EINVAL;
3357}
3358#endif
3359
3360static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
3361{
3362 struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
3363
Sheng Yong770611e2018-02-06 12:31:17 +08003364 return blk_queue_discard(q) || f2fs_sb_has_blkzoned(sbi->sb);
Jaegeuk Kim52763a42016-06-13 09:47:48 -07003365}
3366
3367static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
3368{
3369 clear_opt(sbi, ADAPTIVE);
3370 clear_opt(sbi, LFS);
3371
3372 switch (mt) {
3373 case F2FS_MOUNT_ADAPTIVE:
3374 set_opt(sbi, ADAPTIVE);
3375 break;
3376 case F2FS_MOUNT_LFS:
3377 set_opt(sbi, LFS);
3378 break;
3379 }
3380}
3381
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003382static inline bool f2fs_may_encrypt(struct inode *inode)
3383{
3384#ifdef CONFIG_F2FS_FS_ENCRYPTION
Al Viro886f56f2015-12-05 03:56:06 -05003385 umode_t mode = inode->i_mode;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003386
3387 return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3388#else
3389 return 0;
3390#endif
3391}
3392
Hyunchul Lee355d2342018-03-08 19:34:38 +09003393static inline bool f2fs_force_buffered_io(struct inode *inode, int rw)
3394{
Eric Biggersf69e8142018-04-18 11:09:48 -07003395 return (f2fs_post_read_required(inode) ||
Hyunchul Lee355d2342018-03-08 19:34:38 +09003396 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
3397 F2FS_I_SB(inode)->s_ndevs);
3398}
3399
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09003400#endif