usb: gadget: f_mtp: Avoid race between mtp_read and mtp_function_disable
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / fs / f2fs / f2fs.h
CommitLineData
0a8165d7 1/*
39a53e0c
JK
2 * fs/f2fs/f2fs.h
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#ifndef _LINUX_F2FS_H
12#define _LINUX_F2FS_H
13
14#include <linux/types.h>
15#include <linux/page-flags.h>
16#include <linux/buffer_head.h>
39a53e0c
JK
17#include <linux/slab.h>
18#include <linux/crc32.h>
19#include <linux/magic.h>
c2d715d1 20#include <linux/kobject.h>
7bd59381 21#include <linux/sched.h>
39307a8e 22#include <linux/vmalloc.h>
740432f8 23#include <linux/bio.h>
c1286ff4 24#include <linux/blkdev.h>
13f00235
JK
25#include <linux/quotaops.h>
26#ifdef CONFIG_F2FS_FS_ENCRYPTION
27#include <linux/fscrypt_supp.h>
28#else
29#include <linux/fscrypt_notsupp.h>
30#endif
c1286ff4 31#include <crypto/hash.h>
dc45fd9e 32#include <linux/writeback.h>
39a53e0c 33
5d56b671 34#ifdef CONFIG_F2FS_CHECK_FS
9850cf4a 35#define f2fs_bug_on(sbi, condition) BUG_ON(condition)
5d56b671 36#else
9850cf4a
JK
37#define f2fs_bug_on(sbi, condition) \
38 do { \
39 if (unlikely(condition)) { \
40 WARN_ON(1); \
caf0047e 41 set_sbi_flag(sbi, SBI_NEED_FSCK); \
9850cf4a
JK
42 } \
43 } while (0)
c1286ff4
JK
44#endif
45
46#ifdef CONFIG_F2FS_FAULT_INJECTION
47enum {
48 FAULT_KMALLOC,
49 FAULT_PAGE_ALLOC,
353c1624
JK
50 FAULT_PAGE_GET,
51 FAULT_ALLOC_BIO,
c1286ff4
JK
52 FAULT_ALLOC_NID,
53 FAULT_ORPHAN,
54 FAULT_BLOCK,
55 FAULT_DIR_DEPTH,
56 FAULT_EVICT_INODE,
13f00235 57 FAULT_TRUNCATE,
c1286ff4
JK
58 FAULT_IO,
59 FAULT_CHECKPOINT,
60 FAULT_MAX,
61};
62
63struct f2fs_fault_info {
64 atomic_t inject_ops;
65 unsigned int inject_rate;
66 unsigned int inject_type;
67};
68
69extern char *fault_name[FAULT_MAX];
13f00235 70#define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
5d56b671
JK
71#endif
72
39a53e0c
JK
73/*
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
444c580f 83#define F2FS_MOUNT_INLINE_XATTR 0x00000080
1001b347 84#define F2FS_MOUNT_INLINE_DATA 0x00000100
34d67deb
CY
85#define F2FS_MOUNT_INLINE_DENTRY 0x00000200
86#define F2FS_MOUNT_FLUSH_MERGE 0x00000400
87#define F2FS_MOUNT_NOBARRIER 0x00000800
d5053a34 88#define F2FS_MOUNT_FASTBOOT 0x00001000
89672159 89#define F2FS_MOUNT_EXTENT_CACHE 0x00002000
6aefd93b 90#define F2FS_MOUNT_FORCE_FG_GC 0x00004000
c1286ff4
JK
91#define F2FS_MOUNT_DATA_FLUSH 0x00008000
92#define F2FS_MOUNT_FAULT_INJECTION 0x00010000
93#define F2FS_MOUNT_ADAPTIVE 0x00020000
94#define F2FS_MOUNT_LFS 0x00040000
13f00235
JK
95#define F2FS_MOUNT_USRQUOTA 0x00080000
96#define F2FS_MOUNT_GRPQUOTA 0x00100000
97#define F2FS_MOUNT_PRJQUOTA 0x00200000
98#define F2FS_MOUNT_QUOTA 0x00400000
353c1624 99#define F2FS_MOUNT_INLINE_XATTR_SIZE 0x00800000
39a53e0c 100
13f00235
JK
101#define clear_opt(sbi, option) ((sbi)->mount_opt.opt &= ~F2FS_MOUNT_##option)
102#define set_opt(sbi, option) ((sbi)->mount_opt.opt |= F2FS_MOUNT_##option)
103#define test_opt(sbi, option) ((sbi)->mount_opt.opt & F2FS_MOUNT_##option)
39a53e0c
JK
104
105#define ver_after(a, b) (typecheck(unsigned long long, a) && \
106 typecheck(unsigned long long, b) && \
107 ((long long)((a) - (b)) > 0))
108
a9841c4d
JK
109typedef u32 block_t; /*
110 * should not change u32, since it is the on-disk block
111 * address format, __le32.
112 */
39a53e0c
JK
113typedef u32 nid_t;
114
115struct f2fs_mount_info {
116 unsigned int opt;
117};
118
13f00235
JK
119#define F2FS_FEATURE_ENCRYPT 0x0001
120#define F2FS_FEATURE_BLKZONED 0x0002
121#define F2FS_FEATURE_ATOMIC_WRITE 0x0004
122#define F2FS_FEATURE_EXTRA_ATTR 0x0008
123#define F2FS_FEATURE_PRJQUOTA 0x0010
124#define F2FS_FEATURE_INODE_CHKSUM 0x0020
353c1624
JK
125#define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR 0x0040
126#define F2FS_FEATURE_QUOTA_INO 0x0080
cde4de12 127
76f105a2
JK
128#define F2FS_HAS_FEATURE(sb, mask) \
129 ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
130#define F2FS_SET_FEATURE(sb, mask) \
13f00235 131 (F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
76f105a2 132#define F2FS_CLEAR_FEATURE(sb, mask) \
13f00235 133 (F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
76f105a2 134
dc45fd9e
JK
135/* bio stuffs */
136#define REQ_OP_READ READ
137#define REQ_OP_WRITE WRITE
138#define bio_op(bio) ((bio)->bi_rw & 1)
139
140static inline void bio_set_op_attrs(struct bio *bio, unsigned op,
141 unsigned op_flags)
142{
143 bio->bi_rw = op | op_flags;
144}
145
146static inline int wbc_to_write_flags(struct writeback_control *wbc)
147{
148 if (wbc->sync_mode == WB_SYNC_ALL)
401c465b 149 return REQ_SYNC | REQ_NOIDLE;
dc45fd9e
JK
150 return 0;
151}
152
c1286ff4
JK
153/**
154 * wq_has_sleeper - check if there are any waiting processes
155 * @wq: wait queue head
156 *
157 * Returns true if wq has waiting processes
158 *
159 * Please refer to the comment for waitqueue_active.
160 */
161static inline bool wq_has_sleeper(wait_queue_head_t *wq)
39a53e0c 162{
c1286ff4
JK
163 /*
164 * We need to be sure we are in sync with the
165 * add_wait_queue modifications to the wait queue.
166 *
167 * This memory barrier should be paired with one on the
168 * waiting side.
169 */
170 smp_mb();
171 return waitqueue_active(wq);
39a53e0c
JK
172}
173
c1286ff4 174static inline void inode_nohighmem(struct inode *inode)
39a53e0c 175{
c1286ff4 176 mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
39a53e0c
JK
177}
178
dafac77e
JK
179/**
180 * current_time - Return FS time
181 * @inode: inode.
182 *
183 * Return the current time truncated to the time granularity supported by
184 * the fs.
185 *
186 * Note that inode and inode->sb cannot be NULL.
187 * Otherwise, the function warns and returns time without truncation.
188 */
189static inline struct timespec current_time(struct inode *inode)
190{
191 struct timespec now = current_kernel_time();
192
193 if (unlikely(!inode->i_sb)) {
194 WARN(1, "current_time() called with uninitialized super_block in the inode");
195 return now;
196 }
197
198 return timespec_trunc(now, inode->i_sb->s_time_gran);
199}
200
39a53e0c
JK
201/*
202 * For checkpoint manager
203 */
204enum {
205 NAT_BITMAP,
206 SIT_BITMAP
207};
208
13f00235
JK
209#define CP_UMOUNT 0x00000001
210#define CP_FASTBOOT 0x00000002
211#define CP_SYNC 0x00000004
212#define CP_RECOVERY 0x00000008
213#define CP_DISCARD 0x00000010
214#define CP_TRIMMED 0x00000020
75ab4cb8 215
13f00235 216#define DEF_BATCHED_TRIM_SECTIONS 2048
bba681cb 217#define BATCHED_TRIM_SEGMENTS(sbi) \
13f00235 218 (GET_SEG_FROM_SEC(sbi, SM_I(sbi)->trim_sections))
a66cdd98
JK
219#define BATCHED_TRIM_BLOCKS(sbi) \
220 (BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
13f00235 221#define MAX_DISCARD_BLOCKS(sbi) BLKS_PER_SEC(sbi)
353c1624 222#define DEF_MAX_DISCARD_REQUEST 8 /* issue 8 discards per round */
13f00235
JK
223#define DEF_MIN_DISCARD_ISSUE_TIME 50 /* 50 ms, if exists */
224#define DEF_MAX_DISCARD_ISSUE_TIME 60000 /* 60 s, if no candidates */
60b99b48 225#define DEF_CP_INTERVAL 60 /* 60 secs */
c1286ff4 226#define DEF_IDLE_INTERVAL 5 /* 5 secs */
bba681cb 227
75ab4cb8
JK
228struct cp_control {
229 int reason;
4b2fecc8
JK
230 __u64 trim_start;
231 __u64 trim_end;
232 __u64 trim_minlen;
75ab4cb8
JK
233};
234
662befda 235/*
81c1a0f1 236 * For CP/NAT/SIT/SSA readahead
662befda
CY
237 */
238enum {
239 META_CP,
240 META_NAT,
81c1a0f1 241 META_SIT,
4c521f49
JK
242 META_SSA,
243 META_POR,
662befda
CY
244};
245
6451e041
JK
246/* for the list of ino */
247enum {
248 ORPHAN_INO, /* for orphan ino list */
fff04f90
JK
249 APPEND_INO, /* for append ino list */
250 UPDATE_INO, /* for update ino list */
353c1624 251 FLUSH_INO, /* for multiple device flushing */
6451e041
JK
252 MAX_INO_ENTRY, /* max. list */
253};
254
255struct ino_entry {
353c1624
JK
256 struct list_head list; /* list head */
257 nid_t ino; /* inode number */
258 unsigned int dirty_device; /* dirty device bitmap */
39a53e0c
JK
259};
260
c1286ff4 261/* for the list of inodes to be GCed */
06292073 262struct inode_entry {
39a53e0c
JK
263 struct list_head list; /* list head */
264 struct inode *inode; /* vfs inode pointer */
265};
266
13f00235 267/* for the bitmap indicate blocks to be discarded */
7fd9e544
JK
268struct discard_entry {
269 struct list_head list; /* list head */
13f00235
JK
270 block_t start_blkaddr; /* start blockaddr of current segment */
271 unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */
272};
273
274/* default discard granularity of inner discard thread, unit: block count */
275#define DEFAULT_DISCARD_GRANULARITY 16
276
277/* max discard pend list number */
278#define MAX_PLIST_NUM 512
279#define plist_idx(blk_num) ((blk_num) >= MAX_PLIST_NUM ? \
280 (MAX_PLIST_NUM - 1) : (blk_num - 1))
281
13f00235
JK
282enum {
283 D_PREP,
284 D_SUBMIT,
285 D_DONE,
286};
287
288struct discard_info {
289 block_t lstart; /* logical start address */
290 block_t len; /* length */
291 block_t start; /* actual start address in dev */
7fd9e544
JK
292};
293
eee3f1f5 294struct discard_cmd {
13f00235
JK
295 struct rb_node rb_node; /* rb node located in rb-tree */
296 union {
297 struct {
298 block_t lstart; /* logical start address */
299 block_t len; /* length */
300 block_t start; /* actual start address in dev */
301 };
302 struct discard_info di; /* discard info */
303
304 };
eee3f1f5
JK
305 struct list_head list; /* command list */
306 struct completion wait; /* compleation */
13f00235
JK
307 struct block_device *bdev; /* bdev */
308 unsigned short ref; /* reference count */
309 unsigned char state; /* state */
310 int error; /* bio error */
311};
312
353c1624
JK
313enum {
314 DPOLICY_BG,
315 DPOLICY_FORCE,
316 DPOLICY_FSTRIM,
317 DPOLICY_UMOUNT,
318 MAX_DPOLICY,
319};
320
321struct discard_policy {
322 int type; /* type of discard */
323 unsigned int min_interval; /* used for candidates exist */
324 unsigned int max_interval; /* used for candidates not exist */
325 unsigned int max_requests; /* # of discards issued per round */
326 unsigned int io_aware_gran; /* minimum granularity discard not be aware of I/O */
327 bool io_aware; /* issue discard in idle time */
328 bool sync; /* submit discard with REQ_SYNC flag */
329 unsigned int granularity; /* discard granularity */
330};
331
13f00235
JK
332struct discard_cmd_control {
333 struct task_struct *f2fs_issue_discard; /* discard thread */
334 struct list_head entry_list; /* 4KB discard entry list */
335 struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
336 unsigned char pend_list_tag[MAX_PLIST_NUM];/* tag for pending entries */
337 struct list_head wait_list; /* store on-flushing entries */
353c1624 338 struct list_head fstrim_list; /* in-flight discard from fstrim */
13f00235
JK
339 wait_queue_head_t discard_wait_queue; /* waiting queue for wake-up */
340 unsigned int discard_wake; /* to wake up discard thread */
341 struct mutex cmd_lock;
342 unsigned int nr_discards; /* # of discards in the list */
343 unsigned int max_discards; /* max. discards to be issued */
344 unsigned int discard_granularity; /* discard granularity */
345 unsigned int undiscard_blks; /* # of undiscard blocks */
346 atomic_t issued_discard; /* # of issued discard */
347 atomic_t issing_discard; /* # of issing discard */
348 atomic_t discard_cmd_cnt; /* # of cached cmd count */
349 struct rb_root root; /* root of discard rb-tree */
c1286ff4
JK
350};
351
39a53e0c
JK
352/* for the list of fsync inodes, used only during recovery */
353struct fsync_inode_entry {
354 struct list_head list; /* list head */
355 struct inode *inode; /* vfs inode pointer */
c52e1b10
JK
356 block_t blkaddr; /* block address locating the last fsync */
357 block_t last_dentry; /* block address locating the last dentry */
39a53e0c
JK
358};
359
13f00235
JK
360#define nats_in_cursum(jnl) (le16_to_cpu((jnl)->n_nats))
361#define sits_in_cursum(jnl) (le16_to_cpu((jnl)->n_sits))
39a53e0c 362
13f00235
JK
363#define nat_in_journal(jnl, i) ((jnl)->nat_j.entries[i].ne)
364#define nid_in_journal(jnl, i) ((jnl)->nat_j.entries[i].nid)
365#define sit_in_journal(jnl, i) ((jnl)->sit_j.entries[i].se)
366#define segno_in_journal(jnl, i) ((jnl)->sit_j.entries[i].segno)
39a53e0c 367
c1286ff4
JK
368#define MAX_NAT_JENTRIES(jnl) (NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
369#define MAX_SIT_JENTRIES(jnl) (SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
309cc2b6 370
c1286ff4 371static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
39a53e0c 372{
c1286ff4 373 int before = nats_in_cursum(journal);
13f00235 374
c1286ff4 375 journal->n_nats = cpu_to_le16(before + i);
39a53e0c
JK
376 return before;
377}
378
c1286ff4 379static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
39a53e0c 380{
c1286ff4 381 int before = sits_in_cursum(journal);
13f00235 382
c1286ff4 383 journal->n_sits = cpu_to_le16(before + i);
39a53e0c
JK
384 return before;
385}
386
c1286ff4
JK
387static inline bool __has_cursum_space(struct f2fs_journal *journal,
388 int size, int type)
184a5cd2
CY
389{
390 if (type == NAT_JOURNAL)
c1286ff4
JK
391 return size <= MAX_NAT_JENTRIES(journal);
392 return size <= MAX_SIT_JENTRIES(journal);
184a5cd2
CY
393}
394
e9750824
NJ
395/*
396 * ioctl commands
397 */
88b88a66
JK
398#define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
399#define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
d49f3e89 400#define F2FS_IOC_GETVERSION FS_IOC_GETVERSION
88b88a66
JK
401
402#define F2FS_IOCTL_MAGIC 0xf5
403#define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
404#define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
02a1335f 405#define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
1e84371f
JK
406#define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
407#define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
13f00235 408#define F2FS_IOC_GARBAGE_COLLECT _IOW(F2FS_IOCTL_MAGIC, 6, __u32)
456b88e4 409#define F2FS_IOC_WRITE_CHECKPOINT _IO(F2FS_IOCTL_MAGIC, 7)
13f00235
JK
410#define F2FS_IOC_DEFRAGMENT _IOWR(F2FS_IOCTL_MAGIC, 8, \
411 struct f2fs_defragment)
c1286ff4
JK
412#define F2FS_IOC_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
413 struct f2fs_move_range)
13f00235
JK
414#define F2FS_IOC_FLUSH_DEVICE _IOW(F2FS_IOCTL_MAGIC, 10, \
415 struct f2fs_flush_device)
416#define F2FS_IOC_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11, \
417 struct f2fs_gc_range)
418#define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, __u32)
e9750824 419
c1286ff4
JK
420#define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
421#define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
422#define F2FS_IOC_GET_ENCRYPTION_PWSALT FS_IOC_GET_ENCRYPTION_PWSALT
f424f664 423
1abff93d
JK
424/*
425 * should be same as XFS_IOC_GOINGDOWN.
426 * Flags for going down operation used by FS_IOC_GOINGDOWN
427 */
428#define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
429#define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
430#define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
431#define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
c912a829 432#define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */
1abff93d 433
e9750824
NJ
434#if defined(__KERNEL__) && defined(CONFIG_COMPAT)
435/*
436 * ioctl commands in 32 bit emulation
437 */
c1286ff4
JK
438#define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
439#define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
440#define F2FS_IOC32_GETVERSION FS_IOC32_GETVERSION
e9750824
NJ
441#endif
442
13f00235
JK
443struct f2fs_gc_range {
444 u32 sync;
445 u64 start;
446 u64 len;
447};
448
c1286ff4
JK
449struct f2fs_defragment {
450 u64 start;
451 u64 len;
6b3bd08f
JK
452};
453
c1286ff4
JK
454struct f2fs_move_range {
455 u32 dst_fd; /* destination fd */
456 u64 pos_in; /* start position in src_fd */
457 u64 pos_out; /* start position in dst_fd */
458 u64 len; /* size to move */
6b3bd08f
JK
459};
460
13f00235
JK
461struct f2fs_flush_device {
462 u32 dev_num; /* device number to flush */
463 u32 segments; /* # of segments to flush */
464};
465
466/* for inline stuff */
467#define DEF_INLINE_RESERVED_SIZE 1
353c1624 468#define DEF_MIN_INLINE_SIZE 1
13f00235 469static inline int get_extra_isize(struct inode *inode);
353c1624
JK
470static inline int get_inline_xattr_addrs(struct inode *inode);
471#define F2FS_INLINE_XATTR_ADDRS(inode) get_inline_xattr_addrs(inode)
472#define MAX_INLINE_DATA(inode) (sizeof(__le32) * \
473 (CUR_ADDRS_PER_INODE(inode) - \
474 F2FS_INLINE_XATTR_ADDRS(inode) - \
475 DEF_INLINE_RESERVED_SIZE))
13f00235
JK
476
477/* for inline dir */
478#define NR_INLINE_DENTRY(inode) (MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
479 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
480 BITS_PER_BYTE + 1))
481#define INLINE_DENTRY_BITMAP_SIZE(inode) ((NR_INLINE_DENTRY(inode) + \
482 BITS_PER_BYTE - 1) / BITS_PER_BYTE)
483#define INLINE_RESERVED_SIZE(inode) (MAX_INLINE_DATA(inode) - \
484 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
485 NR_INLINE_DENTRY(inode) + \
486 INLINE_DENTRY_BITMAP_SIZE(inode)))
487
c1286ff4
JK
488/*
489 * For INODE and NODE manager
490 */
491/* for directory operations */
7b3cd7d6 492struct f2fs_dentry_ptr {
d8c6822a 493 struct inode *inode;
13f00235 494 void *bitmap;
7b3cd7d6
JK
495 struct f2fs_dir_entry *dentry;
496 __u8 (*filename)[F2FS_SLOT_LEN];
497 int max;
13f00235 498 int nr_bitmap;
7b3cd7d6
JK
499};
500
13f00235
JK
501static inline void make_dentry_ptr_block(struct inode *inode,
502 struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
7b3cd7d6 503{
d8c6822a 504 d->inode = inode;
13f00235
JK
505 d->max = NR_DENTRY_IN_BLOCK;
506 d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
507 d->bitmap = &t->dentry_bitmap;
508 d->dentry = t->dentry;
509 d->filename = t->filename;
510}
d8c6822a 511
13f00235
JK
512static inline void make_dentry_ptr_inline(struct inode *inode,
513 struct f2fs_dentry_ptr *d, void *t)
514{
515 int entry_cnt = NR_INLINE_DENTRY(inode);
516 int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
517 int reserved_size = INLINE_RESERVED_SIZE(inode);
518
519 d->inode = inode;
520 d->max = entry_cnt;
521 d->nr_bitmap = bitmap_size;
522 d->bitmap = t;
523 d->dentry = t + bitmap_size + reserved_size;
524 d->filename = t + bitmap_size + reserved_size +
525 SIZE_OF_DIR_ENTRY * entry_cnt;
7b3cd7d6
JK
526}
527
dbe6a5ff
JK
528/*
529 * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
530 * as its node offset to distinguish from index node blocks.
531 * But some bits are used to mark the node block.
532 */
533#define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
534 >> OFFSET_BIT_SHIFT)
266e97a8
JK
535enum {
536 ALLOC_NODE, /* allocate a new node page if needed */
537 LOOKUP_NODE, /* look up a node without readahead */
538 LOOKUP_NODE_RA, /*
539 * look up a node with readahead called
4f4124d0 540 * by get_data_block.
39a53e0c 541 */
266e97a8
JK
542};
543
a6db67f0 544#define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */
39a53e0c 545
817202d9
CY
546#define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
547
13054c54
CY
548/* vector size for gang look-up from extent cache that consists of radix tree */
549#define EXT_TREE_VEC_SIZE 64
550
39a53e0c 551/* for in-memory extent cache entry */
13054c54
CY
552#define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */
553
554/* number of extent info in extent cache we try to shrink */
555#define EXTENT_CACHE_SHRINK_NUMBER 128
c11abd1a 556
13f00235
JK
557struct rb_entry {
558 struct rb_node rb_node; /* rb node located in rb-tree */
559 unsigned int ofs; /* start offset of the entry */
560 unsigned int len; /* length of the entry */
561};
562
39a53e0c 563struct extent_info {
13054c54 564 unsigned int fofs; /* start offset in a file */
13054c54 565 unsigned int len; /* length of the extent */
13f00235 566 u32 blk; /* start block address of the extent */
13054c54
CY
567};
568
569struct extent_node {
13f00235
JK
570 struct rb_node rb_node;
571 union {
572 struct {
573 unsigned int fofs;
574 unsigned int len;
575 u32 blk;
576 };
577 struct extent_info ei; /* extent info */
578
579 };
13054c54 580 struct list_head list; /* node in global extent list of sbi */
c1286ff4 581 struct extent_tree *et; /* extent tree pointer */
13054c54
CY
582};
583
584struct extent_tree {
585 nid_t ino; /* inode number */
586 struct rb_root root; /* root of extent info rb-tree */
62c8af65 587 struct extent_node *cached_en; /* recently accessed extent node */
3e72f721 588 struct extent_info largest; /* largested extent info */
c1286ff4 589 struct list_head list; /* to be used by sbi->zombie_list */
13054c54 590 rwlock_t lock; /* protect extent info rb-tree */
c1286ff4 591 atomic_t node_cnt; /* # of extent node in rb-tree*/
39a53e0c
JK
592};
593
003a3e1d
JK
594/*
595 * This structure is taken from ext4_map_blocks.
596 *
597 * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
598 */
599#define F2FS_MAP_NEW (1 << BH_New)
600#define F2FS_MAP_MAPPED (1 << BH_Mapped)
7f63eb77
JK
601#define F2FS_MAP_UNWRITTEN (1 << BH_Unwritten)
602#define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
603 F2FS_MAP_UNWRITTEN)
003a3e1d
JK
604
605struct f2fs_map_blocks {
606 block_t m_pblk;
607 block_t m_lblk;
608 unsigned int m_len;
609 unsigned int m_flags;
c1286ff4 610 pgoff_t *m_next_pgofs; /* point next possible non-hole pgofs */
003a3e1d
JK
611};
612
e2b4e2bc 613/* for flag in get_data_block */
13f00235
JK
614enum {
615 F2FS_GET_BLOCK_DEFAULT,
616 F2FS_GET_BLOCK_FIEMAP,
617 F2FS_GET_BLOCK_BMAP,
618 F2FS_GET_BLOCK_PRE_DIO,
619 F2FS_GET_BLOCK_PRE_AIO,
620};
e2b4e2bc 621
39a53e0c
JK
622/*
623 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
624 */
625#define FADVISE_COLD_BIT 0x01
354a3399 626#define FADVISE_LOST_PINO_BIT 0x02
cde4de12 627#define FADVISE_ENCRYPT_BIT 0x04
e7d55452 628#define FADVISE_ENC_NAME_BIT 0x08
9f495d82 629#define FADVISE_KEEP_SIZE_BIT 0x10
39a53e0c 630
b5492af7
JK
631#define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
632#define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
633#define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
634#define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
635#define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
636#define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
cde4de12
JK
637#define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT)
638#define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
639#define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
e7d55452
JK
640#define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT)
641#define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
9f495d82
JK
642#define file_keep_isize(inode) is_file(inode, FADVISE_KEEP_SIZE_BIT)
643#define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
cde4de12 644
ab9fa662
JK
645#define DEF_DIR_LEVEL 0
646
39a53e0c
JK
647struct f2fs_inode_info {
648 struct inode vfs_inode; /* serve a vfs inode */
649 unsigned long i_flags; /* keep an inode flags for ioctl */
650 unsigned char i_advise; /* use to give file attribute hints */
38431545 651 unsigned char i_dir_level; /* use for dentry level for large dir */
39a53e0c 652 unsigned int i_current_depth; /* use only in directory structure */
6666e6aa 653 unsigned int i_pino; /* parent inode number */
39a53e0c
JK
654 umode_t i_acl_mode; /* keep file acl mode temporarily */
655
656 /* Use below internally in f2fs*/
657 unsigned long flags; /* use to pass per-file flags */
d928bfbf 658 struct rw_semaphore i_sem; /* protect fi info */
036ed1b8 659 atomic_t dirty_pages; /* # of dirty pages */
39a53e0c
JK
660 f2fs_hash_t chash; /* hash value of given file name */
661 unsigned int clevel; /* maximum level of given file name */
13f00235
JK
662 struct task_struct *task; /* lookup and create consistency */
663 struct task_struct *cp_task; /* separate cp/wb IO stats*/
39a53e0c 664 nid_t i_xattr_nid; /* node id that contains xattrs */
c1286ff4 665 loff_t last_disk_size; /* lastly written file size */
88b88a66 666
13f00235
JK
667#ifdef CONFIG_QUOTA
668 struct dquot *i_dquot[MAXQUOTAS];
669
670 /* quota space reservation, managed internally by quota code */
671 qsize_t i_reserved_quota;
672#endif
c1286ff4
JK
673 struct list_head dirty_list; /* dirty list for dirs and files */
674 struct list_head gdirty_list; /* linked in global dirty list */
353c1624 675 struct list_head inmem_ilist; /* list for inmem inodes */
88b88a66 676 struct list_head inmem_pages; /* inmemory pages managed by f2fs */
13f00235 677 struct task_struct *inmem_task; /* store inmemory task */
88b88a66 678 struct mutex inmem_lock; /* lock for inmemory pages */
3e72f721 679 struct extent_tree *extent_tree; /* cached extent_tree entry */
c1286ff4 680 struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
13f00235
JK
681 struct rw_semaphore i_mmap_sem;
682 struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
683
684 int i_extra_isize; /* size of extra space located in i_addr */
685 kprojid_t i_projid; /* id for project quota */
353c1624 686 int i_inline_xattr_size; /* inline xattr size */
39a53e0c
JK
687};
688
689static inline void get_extent_info(struct extent_info *ext,
c1286ff4 690 struct f2fs_extent *i_ext)
39a53e0c 691{
c1286ff4
JK
692 ext->fofs = le32_to_cpu(i_ext->fofs);
693 ext->blk = le32_to_cpu(i_ext->blk);
694 ext->len = le32_to_cpu(i_ext->len);
39a53e0c
JK
695}
696
697static inline void set_raw_extent(struct extent_info *ext,
698 struct f2fs_extent *i_ext)
699{
39a53e0c 700 i_ext->fofs = cpu_to_le32(ext->fofs);
4d0b0bd4 701 i_ext->blk = cpu_to_le32(ext->blk);
39a53e0c 702 i_ext->len = cpu_to_le32(ext->len);
39a53e0c
JK
703}
704
429511cd
CY
705static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
706 u32 blk, unsigned int len)
707{
708 ei->fofs = fofs;
709 ei->blk = blk;
710 ei->len = len;
711}
712
13f00235
JK
713static inline bool __is_discard_mergeable(struct discard_info *back,
714 struct discard_info *front)
0bdee482 715{
13f00235
JK
716 return back->lstart + back->len == front->lstart;
717}
718
719static inline bool __is_discard_back_mergeable(struct discard_info *cur,
720 struct discard_info *back)
721{
722 return __is_discard_mergeable(back, cur);
723}
724
725static inline bool __is_discard_front_mergeable(struct discard_info *cur,
726 struct discard_info *front)
727{
728 return __is_discard_mergeable(cur, front);
0bdee482
CY
729}
730
429511cd
CY
731static inline bool __is_extent_mergeable(struct extent_info *back,
732 struct extent_info *front)
733{
734 return (back->fofs + back->len == front->fofs &&
735 back->blk + back->len == front->blk);
736}
737
738static inline bool __is_back_mergeable(struct extent_info *cur,
739 struct extent_info *back)
740{
741 return __is_extent_mergeable(back, cur);
742}
743
744static inline bool __is_front_mergeable(struct extent_info *cur,
745 struct extent_info *front)
746{
747 return __is_extent_mergeable(cur, front);
748}
749
13f00235 750extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
c1286ff4
JK
751static inline void __try_update_largest_extent(struct inode *inode,
752 struct extent_tree *et, struct extent_node *en)
4abd3f5a 753{
c1286ff4 754 if (en->ei.len > et->largest.len) {
4abd3f5a 755 et->largest = en->ei;
0ef31c7b 756 f2fs_mark_inode_dirty_sync(inode, true);
c1286ff4 757 }
4abd3f5a
CY
758}
759
353c1624
JK
760/*
761 * For free nid management
762 */
763enum nid_state {
764 FREE_NID, /* newly added to free nid list */
765 PREALLOC_NID, /* it is preallocated */
766 MAX_NID_STATE,
e18c2624
CY
767};
768
39a53e0c
JK
769struct f2fs_nm_info {
770 block_t nat_blkaddr; /* base disk address of NAT */
771 nid_t max_nid; /* maximum possible node ids */
031017c6 772 nid_t available_nids; /* # of available node ids */
39a53e0c 773 nid_t next_scan_nid; /* the next nid to be scanned */
cdfc41c1 774 unsigned int ram_thresh; /* control the memory footprint */
ea1a29a0 775 unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */
c1286ff4 776 unsigned int dirty_nats_ratio; /* control dirty nats ratio threshold */
39a53e0c
JK
777
778 /* NAT cache management */
779 struct radix_tree_root nat_root;/* root of the nat entry cache */
309cc2b6 780 struct radix_tree_root nat_set_root;/* root of the nat set cache */
8b26ef98 781 struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */
39a53e0c 782 struct list_head nat_entries; /* cached nat entry list (clean) */
309cc2b6 783 unsigned int nat_cnt; /* the # of cached nat entries */
aec71382 784 unsigned int dirty_nat_cnt; /* total num of nat entries in set */
13f00235 785 unsigned int nat_blocks; /* # of nat blocks */
39a53e0c
JK
786
787 /* free node ids management */
8a7ed66a 788 struct radix_tree_root free_nid_root;/* root of the free_nid cache */
353c1624
JK
789 struct list_head free_nid_list; /* list for free nids excluding preallocated nids */
790 unsigned int nid_cnt[MAX_NID_STATE]; /* the number of free node id */
e18c2624 791 spinlock_t nid_list_lock; /* protect nid lists ops */
39a53e0c 792 struct mutex build_lock; /* lock for build free nids */
13f00235
JK
793 unsigned char (*free_nid_bitmap)[NAT_ENTRY_BITMAP_SIZE];
794 unsigned char *nat_block_bitmap;
795 unsigned short *free_nid_count; /* free nid count of NAT block */
39a53e0c
JK
796
797 /* for checkpoint */
798 char *nat_bitmap; /* NAT bitmap pointer */
13f00235
JK
799
800 unsigned int nat_bits_blocks; /* # of nat bits blocks */
801 unsigned char *nat_bits; /* NAT bits blocks */
802 unsigned char *full_nat_bits; /* full NAT pages */
803 unsigned char *empty_nat_bits; /* empty NAT pages */
5c53448f
CY
804#ifdef CONFIG_F2FS_CHECK_FS
805 char *nat_bitmap_mir; /* NAT bitmap mirror */
806#endif
39a53e0c
JK
807 int bitmap_size; /* bitmap size */
808};
809
810/*
811 * this structure is used as one of function parameters.
812 * all the information are dedicated to a given direct node block determined
813 * by the data offset in a file.
814 */
815struct dnode_of_data {
816 struct inode *inode; /* vfs inode pointer */
817 struct page *inode_page; /* its inode page, NULL is possible */
818 struct page *node_page; /* cached direct node page */
819 nid_t nid; /* node id of the direct node block */
820 unsigned int ofs_in_node; /* data offset in the node page */
821 bool inode_page_locked; /* inode page is locked or not */
c1286ff4
JK
822 bool node_changed; /* is node block changed */
823 char cur_level; /* level of hole node page */
824 char max_level; /* level of current page located */
39a53e0c
JK
825 block_t data_blkaddr; /* block address of the node block */
826};
827
828static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
829 struct page *ipage, struct page *npage, nid_t nid)
830{
d66d1f76 831 memset(dn, 0, sizeof(*dn));
39a53e0c
JK
832 dn->inode = inode;
833 dn->inode_page = ipage;
834 dn->node_page = npage;
835 dn->nid = nid;
39a53e0c
JK
836}
837
838/*
839 * For SIT manager
840 *
841 * By default, there are 6 active log areas across the whole main area.
842 * When considering hot and cold data separation to reduce cleaning overhead,
843 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
844 * respectively.
845 * In the current design, you should not change the numbers intentionally.
846 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
847 * logs individually according to the underlying devices. (default: 6)
848 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
849 * data and 8 for node logs.
850 */
851#define NR_CURSEG_DATA_TYPE (3)
852#define NR_CURSEG_NODE_TYPE (3)
853#define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
854
855enum {
856 CURSEG_HOT_DATA = 0, /* directory entry blocks */
857 CURSEG_WARM_DATA, /* data blocks */
858 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
859 CURSEG_HOT_NODE, /* direct node blocks of directory files */
860 CURSEG_WARM_NODE, /* direct node blocks of normal files */
861 CURSEG_COLD_NODE, /* indirect node blocks */
38aa0889 862 NO_CHECK_TYPE,
39a53e0c
JK
863};
864
6b4afdd7 865struct flush_cmd {
6b4afdd7 866 struct completion wait;
721bd4d5 867 struct llist_node llnode;
353c1624 868 nid_t ino;
6b4afdd7
JK
869 int ret;
870};
871
a688b9d9
GZ
872struct flush_cmd_control {
873 struct task_struct *f2fs_issue_flush; /* flush thread */
874 wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
13f00235
JK
875 atomic_t issued_flush; /* # of issued flushes */
876 atomic_t issing_flush; /* # of issing flushes */
721bd4d5
GZ
877 struct llist_head issue_list; /* list for command issue */
878 struct llist_node *dispatch_list; /* list for command dispatch */
a688b9d9
GZ
879};
880
39a53e0c
JK
881struct f2fs_sm_info {
882 struct sit_info *sit_info; /* whole segment information */
883 struct free_segmap_info *free_info; /* free segment information */
884 struct dirty_seglist_info *dirty_info; /* dirty segment information */
885 struct curseg_info *curseg_array; /* active segment information */
886
353c1624
JK
887 struct rw_semaphore curseg_lock; /* for preventing curseg change */
888
39a53e0c
JK
889 block_t seg0_blkaddr; /* block address of 0'th segment */
890 block_t main_blkaddr; /* start block address of main area */
891 block_t ssa_blkaddr; /* start block address of SSA area */
892
893 unsigned int segment_count; /* total # of segments */
894 unsigned int main_segments; /* # of segments in main area */
895 unsigned int reserved_segments; /* # of reserved segments */
896 unsigned int ovp_segments; /* # of overprovision segments */
81eb8d6e
JK
897
898 /* a threshold to reclaim prefree segments */
899 unsigned int rec_prefree_segments;
7fd9e544 900
bba681cb
JK
901 /* for batched trimming */
902 unsigned int trim_sections; /* # of sections to trim */
903
184a5cd2
CY
904 struct list_head sit_entry_set; /* sit entry set list */
905
216fbd64
JK
906 unsigned int ipu_policy; /* in-place-update policy */
907 unsigned int min_ipu_util; /* in-place-update threshold */
c1ce1b02 908 unsigned int min_fsync_blocks; /* threshold for fsync */
13f00235 909 unsigned int min_hot_blocks; /* threshold for hot block allocation */
353c1624 910 unsigned int min_ssr_sections; /* threshold to trigger SSR allocation */
6b4afdd7
JK
911
912 /* for flush command control */
eee3f1f5 913 struct flush_cmd_control *fcc_info;
13f00235
JK
914
915 /* for discard command control */
916 struct discard_cmd_control *dcc_info;
39a53e0c
JK
917};
918
39a53e0c
JK
919/*
920 * For superblock
921 */
922/*
923 * COUNT_TYPE for monitoring
924 *
925 * f2fs monitors the number of several block types such as on-writeback,
926 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
927 */
9e266223 928#define WB_DATA_TYPE(p) (__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
39a53e0c 929enum count_type {
39a53e0c 930 F2FS_DIRTY_DENTS,
c1286ff4 931 F2FS_DIRTY_DATA,
353c1624 932 F2FS_DIRTY_QDATA,
39a53e0c
JK
933 F2FS_DIRTY_NODES,
934 F2FS_DIRTY_META,
8dcf2ff7 935 F2FS_INMEM_PAGES,
c1286ff4 936 F2FS_DIRTY_IMETA,
9e266223
CY
937 F2FS_WB_CP_DATA,
938 F2FS_WB_DATA,
39a53e0c
JK
939 NR_COUNT_TYPE,
940};
941
39a53e0c 942/*
e1c42045 943 * The below are the page types of bios used in submit_bio().
39a53e0c
JK
944 * The available types are:
945 * DATA User data pages. It operates as async mode.
946 * NODE Node pages. It operates as async mode.
947 * META FS metadata pages such as SIT, NAT, CP.
948 * NR_PAGE_TYPE The number of page types.
949 * META_FLUSH Make sure the previous pages are written
950 * with waiting the bio's completion
951 * ... Only can be used with META.
952 */
7d5e5109 953#define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
39a53e0c
JK
954enum page_type {
955 DATA,
956 NODE,
957 META,
958 NR_PAGE_TYPE,
959 META_FLUSH,
8ce67cb0
JK
960 INMEM, /* the below types are used by tracepoints only. */
961 INMEM_DROP,
13f00235 962 INMEM_INVALIDATE,
c1286ff4 963 INMEM_REVOKE,
8ce67cb0
JK
964 IPU,
965 OPU,
39a53e0c
JK
966};
967
13f00235
JK
968enum temp_type {
969 HOT = 0, /* must be zero for meta bio */
970 WARM,
971 COLD,
972 NR_TEMP_TYPE,
973};
974
975enum need_lock_type {
976 LOCK_REQ = 0,
977 LOCK_DONE,
978 LOCK_RETRY,
979};
980
353c1624
JK
981enum cp_reason_type {
982 CP_NO_NEEDED,
983 CP_NON_REGULAR,
984 CP_HARDLINK,
985 CP_SB_NEED_CP,
986 CP_WRONG_PINO,
987 CP_NO_SPC_ROLL,
988 CP_NODE_NEED_CP,
989 CP_FASTBOOT_MODE,
990 CP_SPEC_LOG_NUM,
991};
992
13f00235
JK
993enum iostat_type {
994 APP_DIRECT_IO, /* app direct IOs */
995 APP_BUFFERED_IO, /* app buffered IOs */
996 APP_WRITE_IO, /* app write IOs */
997 APP_MAPPED_IO, /* app mapped IOs */
998 FS_DATA_IO, /* data IOs from kworker/fsync/reclaimer */
999 FS_NODE_IO, /* node IOs from kworker/fsync/reclaimer */
1000 FS_META_IO, /* meta IOs from kworker/reclaimer */
1001 FS_GC_DATA_IO, /* data IOs from forground gc */
1002 FS_GC_NODE_IO, /* node IOs from forground gc */
1003 FS_CP_DATA_IO, /* data IOs from checkpoint */
1004 FS_CP_NODE_IO, /* node IOs from checkpoint */
1005 FS_CP_META_IO, /* meta IOs from checkpoint */
1006 FS_DISCARD, /* discard */
1007 NR_IO_TYPE,
1008};
1009
458e6197 1010struct f2fs_io_info {
05ca3632 1011 struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
353c1624 1012 nid_t ino; /* inode number */
7e8f2308 1013 enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
13f00235 1014 enum temp_type temp; /* contains HOT/WARM/COLD */
dc45fd9e
JK
1015 int op; /* contains REQ_OP_ */
1016 int op_flags; /* req_flag_bits */
c1286ff4
JK
1017 block_t new_blkaddr; /* new block address to be written */
1018 block_t old_blkaddr; /* old block address before Cow */
05ca3632 1019 struct page *page; /* page to be written */
4375a336 1020 struct page *encrypted_page; /* encrypted page */
13f00235
JK
1021 struct list_head list; /* serialize IOs */
1022 bool submitted; /* indicate IO submission */
1023 int need_lock; /* indicate we need to lock cp_rwsem */
1024 bool in_list; /* indicate fio is in io_list */
1025 enum iostat_type io_type; /* io type */
458e6197
JK
1026};
1027
13f00235 1028#define is_read_io(rw) ((rw) == READ)
1ff7bd3b 1029struct f2fs_bio_info {
458e6197 1030 struct f2fs_sb_info *sbi; /* f2fs superblock */
1ff7bd3b
JK
1031 struct bio *bio; /* bios to merge */
1032 sector_t last_block_in_bio; /* last block number */
458e6197 1033 struct f2fs_io_info fio; /* store buffered io info. */
df0f8dc0 1034 struct rw_semaphore io_rwsem; /* blocking op for bio */
13f00235
JK
1035 spinlock_t io_lock; /* serialize DATA/NODE IOs */
1036 struct list_head io_list; /* track fios */
1ff7bd3b
JK
1037};
1038
07f01079
JK
1039#define FDEV(i) (sbi->devs[i])
1040#define RDEV(i) (raw_super->devs[i])
1041struct f2fs_dev_info {
1042 struct block_device *bdev;
1043 char path[MAX_PATH_LEN];
1044 unsigned int total_segments;
1045 block_t start_blk;
1046 block_t end_blk;
1047#ifdef CONFIG_BLK_DEV_ZONED
1048 unsigned int nr_blkz; /* Total number of zones */
1049 u8 *blkz_type; /* Array of zones type */
1050#endif
1051};
1052
c1286ff4
JK
1053enum inode_type {
1054 DIR_INODE, /* for dirty dir inode */
1055 FILE_INODE, /* for dirty regular/symlink inode */
1056 DIRTY_META, /* for all dirtied inode metadata */
353c1624 1057 ATOMIC_FILE, /* for all atomic files */
c1286ff4
JK
1058 NR_INODE_TYPE,
1059};
1060
67298804
CY
1061/* for inner inode cache management */
1062struct inode_management {
1063 struct radix_tree_root ino_root; /* ino entry array */
1064 spinlock_t ino_lock; /* for ino entry lock */
1065 struct list_head ino_list; /* inode list head */
1066 unsigned long ino_num; /* number of entries */
1067};
1068
caf0047e
CY
1069/* For s_flag in struct f2fs_sb_info */
1070enum {
1071 SBI_IS_DIRTY, /* dirty flag for checkpoint */
1072 SBI_IS_CLOSE, /* specify unmounting */
1073 SBI_NEED_FSCK, /* need fsck.f2fs to fix */
1074 SBI_POR_DOING, /* recovery is doing or not */
c1286ff4
JK
1075 SBI_NEED_SB_WRITE, /* need to recover superblock */
1076 SBI_NEED_CP, /* need to checkpoint */
1077};
1078
1079enum {
1080 CP_TIME,
1081 REQ_TIME,
1082 MAX_TIME,
caf0047e
CY
1083};
1084
39a53e0c
JK
1085struct f2fs_sb_info {
1086 struct super_block *sb; /* pointer to VFS super block */
5e176d54 1087 struct proc_dir_entry *s_proc; /* proc entry */
39a53e0c 1088 struct f2fs_super_block *raw_super; /* raw super block pointer */
c1286ff4
JK
1089 int valid_super_block; /* valid super block no */
1090 unsigned long s_flag; /* flags for sbi */
39a53e0c 1091
018fc18e 1092#ifdef CONFIG_BLK_DEV_ZONED
018fc18e
DLM
1093 unsigned int blocks_per_blkz; /* F2FS blocks per zone */
1094 unsigned int log_blocks_per_blkz; /* log2 F2FS blocks per zone */
018fc18e
DLM
1095#endif
1096
39a53e0c
JK
1097 /* for node-related operations */
1098 struct f2fs_nm_info *nm_info; /* node manager */
1099 struct inode *node_inode; /* cache node blocks */
1100
1101 /* for segment-related operations */
1102 struct f2fs_sm_info *sm_info; /* segment manager */
1ff7bd3b
JK
1103
1104 /* for bio operations */
13f00235
JK
1105 struct f2fs_bio_info *write_io[NR_PAGE_TYPE]; /* for write bios */
1106 struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
1107 /* bio ordering for NODE/DATA */
b3fcb700
JK
1108 int write_io_size_bits; /* Write IO size bits */
1109 mempool_t *write_io_dummy; /* Dummy pages */
39a53e0c
JK
1110
1111 /* for checkpoint */
1112 struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
6b266c3a 1113 int cur_cp_pack; /* remain current cp pack */
c1286ff4 1114 spinlock_t cp_lock; /* for flag in ckpt */
39a53e0c 1115 struct inode *meta_inode; /* cache meta blocks */
39936837 1116 struct mutex cp_mutex; /* checkpoint procedure lock */
e479556b 1117 struct rw_semaphore cp_rwsem; /* blocking FS operations */
b3582c68 1118 struct rw_semaphore node_write; /* locking node writes */
13f00235 1119 struct rw_semaphore node_change; /* locking node change */
fb51b5ef 1120 wait_queue_head_t cp_wait;
c1286ff4
JK
1121 unsigned long last_time[MAX_TIME]; /* to store time in jiffies */
1122 long interval_time[MAX_TIME]; /* to store thresholds */
39a53e0c 1123
67298804 1124 struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
6451e041
JK
1125
1126 /* for orphan inode, use 0'th array */
0d47c1ad 1127 unsigned int max_orphans; /* max orphan inodes */
39a53e0c 1128
c1286ff4
JK
1129 /* for inode management */
1130 struct list_head inode_list[NR_INODE_TYPE]; /* dirty inode list */
1131 spinlock_t inode_lock[NR_INODE_TYPE]; /* for dirty inode list lock */
39a53e0c 1132
13054c54
CY
1133 /* for extent tree cache */
1134 struct radix_tree_root extent_tree_root;/* cache extent cache entries */
13f00235 1135 struct mutex extent_tree_lock; /* locking extent radix tree */
13054c54
CY
1136 struct list_head extent_list; /* lru list for shrinker */
1137 spinlock_t extent_lock; /* locking extent lru list */
c1286ff4
JK
1138 atomic_t total_ext_tree; /* extent tree count */
1139 struct list_head zombie_list; /* extent zombie tree list */
1140 atomic_t total_zombie_tree; /* extent zombie tree count */
13054c54
CY
1141 atomic_t total_ext_node; /* extent info count */
1142
e1c42045 1143 /* basic filesystem units */
39a53e0c
JK
1144 unsigned int log_sectors_per_block; /* log2 sectors per block */
1145 unsigned int log_blocksize; /* log2 block size */
1146 unsigned int blocksize; /* block size */
1147 unsigned int root_ino_num; /* root inode number*/
1148 unsigned int node_ino_num; /* node inode number*/
1149 unsigned int meta_ino_num; /* meta inode number*/
1150 unsigned int log_blocks_per_seg; /* log2 blocks per segment */
1151 unsigned int blocks_per_seg; /* blocks per segment */
1152 unsigned int segs_per_sec; /* segments per section */
1153 unsigned int secs_per_zone; /* sections per zone */
1154 unsigned int total_sections; /* total section count */
1155 unsigned int total_node_count; /* total node block count */
1156 unsigned int total_valid_node_count; /* valid node block count */
c1286ff4 1157 loff_t max_file_blocks; /* max block index of file */
39a53e0c 1158 int active_logs; /* # of active logs */
ab9fa662 1159 int dir_level; /* directory level */
353c1624
JK
1160 int inline_xattr_size; /* inline xattr size */
1161 unsigned int trigger_ssr_threshold; /* threshold to trigger ssr */
39a53e0c
JK
1162
1163 block_t user_block_count; /* # of user blocks */
1164 block_t total_valid_block_count; /* # of valid blocks */
a66cdd98 1165 block_t discard_blks; /* discard command candidats */
39a53e0c 1166 block_t last_valid_block_count; /* for recovery */
13f00235 1167 block_t reserved_blocks; /* configurable reserved blocks */
353c1624 1168 block_t current_reserved_blocks; /* current reserved blocks */
13f00235 1169
39a53e0c 1170 u32 s_next_generation; /* for NFS support */
c1286ff4
JK
1171
1172 /* # of pages, see count_type */
3f137dda 1173 atomic_t nr_pages[NR_COUNT_TYPE];
c1286ff4
JK
1174 /* # of allocated blocks */
1175 struct percpu_counter alloc_valid_block_count;
1176
13f00235
JK
1177 /* writeback control */
1178 atomic_t wb_sync_req; /* count # of WB_SYNC threads */
1179
c1286ff4
JK
1180 /* valid inode count */
1181 struct percpu_counter total_valid_inode_count;
39a53e0c
JK
1182
1183 struct f2fs_mount_info mount_opt; /* mount options */
1184
1185 /* for cleaning operations */
1186 struct mutex gc_mutex; /* mutex for GC */
1187 struct f2fs_gc_kthread *gc_thread; /* GC thread */
5ec4e49f 1188 unsigned int cur_victim_sec; /* current victim section num */
39a53e0c 1189
13f00235
JK
1190 /* threshold for converting bg victims for fg */
1191 u64 fggc_threshold;
1192
b1c57c1c
JK
1193 /* maximum # of trials to find a victim segment for SSR and GC */
1194 unsigned int max_victim_search;
1195
39a53e0c
JK
1196 /*
1197 * for stat information.
1198 * one is for the LFS mode, and the other is for the SSR mode.
1199 */
35b09d82 1200#ifdef CONFIG_F2FS_STAT_FS
39a53e0c
JK
1201 struct f2fs_stat_info *stat_info; /* FS status information */
1202 unsigned int segment_count[2]; /* # of allocated segments */
1203 unsigned int block_count[2]; /* # of allocated blocks */
b9a2c252 1204 atomic_t inplace_count; /* # of inplace update */
5b7ee374
CY
1205 atomic64_t total_hit_ext; /* # of lookup extent cache */
1206 atomic64_t read_hit_rbtree; /* # of hit rbtree extent node */
1207 atomic64_t read_hit_largest; /* # of hit largest extent node */
1208 atomic64_t read_hit_cached; /* # of hit cached extent node */
d5e8f6c9 1209 atomic_t inline_xattr; /* # of inline_xattr inodes */
03e14d52
CY
1210 atomic_t inline_inode; /* # of inline_data inodes */
1211 atomic_t inline_dir; /* # of inline_dentry inodes */
97a43c70 1212 atomic_t aw_cnt; /* # of atomic writes */
13f00235 1213 atomic_t vw_cnt; /* # of volatile writes */
97a43c70 1214 atomic_t max_aw_cnt; /* max # of atomic writes */
13f00235 1215 atomic_t max_vw_cnt; /* max # of volatile writes */
39a53e0c 1216 int bg_gc; /* background gc calls */
c1286ff4 1217 unsigned int ndirty_inode[NR_INODE_TYPE]; /* # of dirty inodes */
35b09d82 1218#endif
39a53e0c 1219 spinlock_t stat_lock; /* lock for stat operations */
b59d0bae 1220
13f00235
JK
1221 /* For app/fs IO statistics */
1222 spinlock_t iostat_lock;
1223 unsigned long long write_iostat[NR_IO_TYPE];
1224 bool iostat_enable;
1225
b59d0bae
NJ
1226 /* For sysfs suppport */
1227 struct kobject s_kobj;
1228 struct completion s_kobj_unregister;
2658e50d
JK
1229
1230 /* For shrinker support */
1231 struct list_head s_list;
07f01079
JK
1232 int s_ndevs; /* number of devices */
1233 struct f2fs_dev_info *devs; /* for device list */
353c1624
JK
1234 unsigned int dirty_device; /* for checkpoint data flush */
1235 spinlock_t dev_lock; /* protect dirty_device */
2658e50d
JK
1236 struct mutex umount_mutex;
1237 unsigned int shrinker_run_no;
c1286ff4
JK
1238
1239 /* For write statistics */
1240 u64 sectors_written_start;
1241 u64 kbytes_written;
1242
1243 /* Reference to checksum algorithm driver via cryptoapi */
1244 struct crypto_shash *s_chksum_driver;
1245
13f00235
JK
1246 /* Precomputed FS UUID checksum for seeding other checksums */
1247 __u32 s_chksum_seed;
1248
c1286ff4
JK
1249 /* For fault injection */
1250#ifdef CONFIG_F2FS_FAULT_INJECTION
1251 struct f2fs_fault_info fault_info;
1252#endif
13f00235
JK
1253
1254#ifdef CONFIG_QUOTA
1255 /* Names of quota files with journalled quota */
1256 char *s_qf_names[MAXQUOTAS];
1257 int s_jquota_fmt; /* Format of quota to use */
1258#endif
39a53e0c
JK
1259};
1260
c1286ff4 1261#ifdef CONFIG_F2FS_FAULT_INJECTION
13f00235
JK
1262#define f2fs_show_injection_info(type) \
1263 printk("%sF2FS-fs : inject %s in %s of %pF\n", \
1264 KERN_INFO, fault_name[type], \
1265 __func__, __builtin_return_address(0))
c1286ff4
JK
1266static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
1267{
1268 struct f2fs_fault_info *ffi = &sbi->fault_info;
1269
1270 if (!ffi->inject_rate)
1271 return false;
1272
1273 if (!IS_FAULT_SET(ffi, type))
1274 return false;
1275
1276 atomic_inc(&ffi->inject_ops);
1277 if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
1278 atomic_set(&ffi->inject_ops, 0);
c1286ff4
JK
1279 return true;
1280 }
1281 return false;
1282}
1283#endif
1284
1285/* For write statistics. Suppose sector size is 512 bytes,
1286 * and the return value is in kbytes. s is of struct f2fs_sb_info.
1287 */
1288#define BD_PART_WRITTEN(s) \
13f00235
JK
1289(((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) - \
1290 (s)->sectors_written_start) >> 1)
c1286ff4
JK
1291
1292static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
1293{
1294 sbi->last_time[type] = jiffies;
1295}
1296
1297static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
1298{
353c1624 1299 unsigned long interval = sbi->interval_time[type] * HZ;
c1286ff4
JK
1300
1301 return time_after(jiffies, sbi->last_time[type] + interval);
1302}
1303
1304static inline bool is_idle(struct f2fs_sb_info *sbi)
1305{
1306 struct block_device *bdev = sbi->sb->s_bdev;
1307 struct request_queue *q = bdev_get_queue(bdev);
1308 struct request_list *rl = &q->root_rl;
1309
1310 if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1311 return 0;
1312
1313 return f2fs_time_over(sbi, REQ_TIME);
1314}
1315
39a53e0c
JK
1316/*
1317 * Inline functions
1318 */
c1286ff4
JK
1319static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1320 unsigned int length)
1321{
1322 SHASH_DESC_ON_STACK(shash, sbi->s_chksum_driver);
1323 u32 *ctx = (u32 *)shash_desc_ctx(shash);
3947a030 1324 u32 retval;
c1286ff4
JK
1325 int err;
1326
1327 shash->tfm = sbi->s_chksum_driver;
1328 shash->flags = 0;
1329 *ctx = F2FS_SUPER_MAGIC;
1330
1331 err = crypto_shash_update(shash, address, length);
1332 BUG_ON(err);
1333
3947a030
DM
1334 retval = *ctx;
1335 barrier_data(ctx);
1336 return retval;
c1286ff4
JK
1337}
1338
1339static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1340 void *buf, size_t buf_size)
1341{
1342 return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1343}
1344
13f00235
JK
1345static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1346 const void *address, unsigned int length)
1347{
1348 struct {
1349 struct shash_desc shash;
1350 char ctx[4];
1351 } desc;
1352 int err;
1353
1354 BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1355
1356 desc.shash.tfm = sbi->s_chksum_driver;
1357 desc.shash.flags = 0;
1358 *(u32 *)desc.ctx = crc;
1359
1360 err = crypto_shash_update(&desc.shash, address, length);
1361 BUG_ON(err);
1362
1363 return *(u32 *)desc.ctx;
1364}
1365
39a53e0c
JK
1366static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
1367{
1368 return container_of(inode, struct f2fs_inode_info, vfs_inode);
1369}
1370
1371static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
1372{
1373 return sb->s_fs_info;
1374}
1375
4081363f
JK
1376static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
1377{
1378 return F2FS_SB(inode->i_sb);
1379}
1380
1381static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
1382{
1383 return F2FS_I_SB(mapping->host);
1384}
1385
1386static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
1387{
1388 return F2FS_M_SB(page->mapping);
1389}
1390
39a53e0c
JK
1391static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
1392{
1393 return (struct f2fs_super_block *)(sbi->raw_super);
1394}
1395
1396static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
1397{
1398 return (struct f2fs_checkpoint *)(sbi->ckpt);
1399}
1400
45590710
GZ
1401static inline struct f2fs_node *F2FS_NODE(struct page *page)
1402{
1403 return (struct f2fs_node *)page_address(page);
1404}
1405
58bfaf44
JK
1406static inline struct f2fs_inode *F2FS_INODE(struct page *page)
1407{
1408 return &((struct f2fs_node *)page_address(page))->i;
1409}
1410
39a53e0c
JK
1411static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
1412{
1413 return (struct f2fs_nm_info *)(sbi->nm_info);
1414}
1415
1416static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
1417{
1418 return (struct f2fs_sm_info *)(sbi->sm_info);
1419}
1420
1421static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
1422{
1423 return (struct sit_info *)(SM_I(sbi)->sit_info);
1424}
1425
1426static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
1427{
1428 return (struct free_segmap_info *)(SM_I(sbi)->free_info);
1429}
1430
1431static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
1432{
1433 return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
1434}
1435
9df27d98
GZ
1436static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
1437{
1438 return sbi->meta_inode->i_mapping;
1439}
1440
4ef51a8f
JK
1441static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
1442{
1443 return sbi->node_inode->i_mapping;
1444}
1445
caf0047e
CY
1446static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
1447{
c1286ff4 1448 return test_bit(type, &sbi->s_flag);
caf0047e
CY
1449}
1450
1451static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
39a53e0c 1452{
c1286ff4 1453 set_bit(type, &sbi->s_flag);
39a53e0c
JK
1454}
1455
caf0047e 1456static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
39a53e0c 1457{
c1286ff4 1458 clear_bit(type, &sbi->s_flag);
39a53e0c
JK
1459}
1460
d71b5564
JK
1461static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1462{
1463 return le64_to_cpu(cp->checkpoint_ver);
1464}
1465
353c1624
JK
1466static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1467{
1468 if (type < F2FS_MAX_QUOTAS)
1469 return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1470 return 0;
1471}
1472
13f00235
JK
1473static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1474{
1475 size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1476 return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1477}
1478
c1286ff4 1479static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
25ca923b
JK
1480{
1481 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
c1286ff4 1482
25ca923b
JK
1483 return ckpt_flags & f;
1484}
1485
c1286ff4 1486static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
25ca923b 1487{
c1286ff4
JK
1488 return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1489}
1490
1491static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1492{
1493 unsigned int ckpt_flags;
1494
1495 ckpt_flags = le32_to_cpu(cp->ckpt_flags);
25ca923b
JK
1496 ckpt_flags |= f;
1497 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1498}
1499
c1286ff4 1500static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
25ca923b 1501{
13f00235
JK
1502 unsigned long flags;
1503
1504 spin_lock_irqsave(&sbi->cp_lock, flags);
c1286ff4 1505 __set_ckpt_flags(F2FS_CKPT(sbi), f);
13f00235 1506 spin_unlock_irqrestore(&sbi->cp_lock, flags);
c1286ff4
JK
1507}
1508
1509static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1510{
1511 unsigned int ckpt_flags;
1512
1513 ckpt_flags = le32_to_cpu(cp->ckpt_flags);
25ca923b
JK
1514 ckpt_flags &= (~f);
1515 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1516}
1517
c1286ff4
JK
1518static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1519{
13f00235
JK
1520 unsigned long flags;
1521
1522 spin_lock_irqsave(&sbi->cp_lock, flags);
c1286ff4 1523 __clear_ckpt_flags(F2FS_CKPT(sbi), f);
13f00235
JK
1524 spin_unlock_irqrestore(&sbi->cp_lock, flags);
1525}
1526
1527static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
1528{
1529 unsigned long flags;
1530
1531 set_sbi_flag(sbi, SBI_NEED_FSCK);
1532
1533 if (lock)
1534 spin_lock_irqsave(&sbi->cp_lock, flags);
1535 __clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
1536 kfree(NM_I(sbi)->nat_bits);
1537 NM_I(sbi)->nat_bits = NULL;
1538 if (lock)
1539 spin_unlock_irqrestore(&sbi->cp_lock, flags);
1540}
1541
1542static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
1543 struct cp_control *cpc)
1544{
1545 bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
1546
1547 return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
c1286ff4
JK
1548}
1549
e479556b 1550static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
39936837 1551{
e479556b 1552 down_read(&sbi->cp_rwsem);
39936837
JK
1553}
1554
13f00235
JK
1555static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1556{
1557 return down_read_trylock(&sbi->cp_rwsem);
1558}
1559
e479556b 1560static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
39a53e0c 1561{
e479556b 1562 up_read(&sbi->cp_rwsem);
39a53e0c
JK
1563}
1564
e479556b 1565static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
39a53e0c 1566{
c1286ff4 1567 down_write(&sbi->cp_rwsem);
39936837
JK
1568}
1569
e479556b 1570static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
39936837 1571{
e479556b 1572 up_write(&sbi->cp_rwsem);
39a53e0c
JK
1573}
1574
119ee914
JK
1575static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1576{
1577 int reason = CP_SYNC;
1578
1579 if (test_opt(sbi, FASTBOOT))
1580 reason = CP_FASTBOOT;
1581 if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1582 reason = CP_UMOUNT;
1583 return reason;
1584}
1585
1586static inline bool __remain_node_summaries(int reason)
1587{
13f00235 1588 return (reason & (CP_UMOUNT | CP_FASTBOOT));
119ee914
JK
1589}
1590
1591static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1592{
c1286ff4
JK
1593 return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1594 is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
119ee914
JK
1595}
1596
39a53e0c
JK
1597/*
1598 * Check whether the given nid is within node id range.
1599 */
064e0823 1600static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
39a53e0c 1601{
d6b7d4b3
CY
1602 if (unlikely(nid < F2FS_ROOT_INO(sbi)))
1603 return -EINVAL;
cfb271d4 1604 if (unlikely(nid >= NM_I(sbi)->max_nid))
064e0823
NJ
1605 return -EINVAL;
1606 return 0;
39a53e0c
JK
1607}
1608
39a53e0c
JK
1609/*
1610 * Check whether the inode has blocks or not
1611 */
1612static inline int F2FS_HAS_BLOCKS(struct inode *inode)
1613{
13f00235
JK
1614 block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
1615
1616 return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
39a53e0c
JK
1617}
1618
4bc8e9bc
CY
1619static inline bool f2fs_has_xattr_block(unsigned int ofs)
1620{
1621 return ofs == XATTR_NODE_OFFSET;
1622}
1623
13f00235
JK
1624static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
1625static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
c1286ff4 1626 struct inode *inode, blkcnt_t *count)
39a53e0c 1627{
13f00235
JK
1628 blkcnt_t diff = 0, release = 0;
1629 block_t avail_user_block_count;
1630 int ret;
1631
1632 ret = dquot_reserve_block(inode, *count);
1633 if (ret)
1634 return ret;
39a53e0c 1635
c1286ff4 1636#ifdef CONFIG_F2FS_FAULT_INJECTION
13f00235
JK
1637 if (time_to_inject(sbi, FAULT_BLOCK)) {
1638 f2fs_show_injection_info(FAULT_BLOCK);
1639 release = *count;
1640 goto enospc;
1641 }
c1286ff4
JK
1642#endif
1643 /*
1644 * let's increase this in prior to actual block count change in order
1645 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1646 */
1647 percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1648
1649 spin_lock(&sbi->stat_lock);
1650 sbi->total_valid_block_count += (block_t)(*count);
353c1624
JK
1651 avail_user_block_count = sbi->user_block_count -
1652 sbi->current_reserved_blocks;
13f00235
JK
1653 if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1654 diff = sbi->total_valid_block_count - avail_user_block_count;
c1286ff4 1655 *count -= diff;
13f00235
JK
1656 release = diff;
1657 sbi->total_valid_block_count = avail_user_block_count;
c1286ff4
JK
1658 if (!*count) {
1659 spin_unlock(&sbi->stat_lock);
1660 percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
13f00235 1661 goto enospc;
c1286ff4 1662 }
39a53e0c 1663 }
39a53e0c 1664 spin_unlock(&sbi->stat_lock);
c1286ff4 1665
13f00235
JK
1666 if (release)
1667 dquot_release_reservation_block(inode, release);
1668 f2fs_i_blocks_write(inode, *count, true, true);
1669 return 0;
1670
1671enospc:
1672 dquot_release_reservation_block(inode, release);
1673 return -ENOSPC;
39a53e0c
JK
1674}
1675
da19b0dc 1676static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
39a53e0c 1677 struct inode *inode,
13f00235 1678 block_t count)
39a53e0c 1679{
13f00235
JK
1680 blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
1681
39a53e0c 1682 spin_lock(&sbi->stat_lock);
9850cf4a 1683 f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
13f00235 1684 f2fs_bug_on(sbi, inode->i_blocks < sectors);
39a53e0c 1685 sbi->total_valid_block_count -= (block_t)count;
353c1624
JK
1686 if (sbi->reserved_blocks &&
1687 sbi->current_reserved_blocks < sbi->reserved_blocks)
1688 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
1689 sbi->current_reserved_blocks + count);
39a53e0c 1690 spin_unlock(&sbi->stat_lock);
13f00235 1691 f2fs_i_blocks_write(inode, count, false, true);
39a53e0c
JK
1692}
1693
1694static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
1695{
3f137dda 1696 atomic_inc(&sbi->nr_pages[count_type]);
c1286ff4 1697
9e266223
CY
1698 if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
1699 count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
c1286ff4
JK
1700 return;
1701
caf0047e 1702 set_sbi_flag(sbi, SBI_IS_DIRTY);
39a53e0c
JK
1703}
1704
a7ffdbe2 1705static inline void inode_inc_dirty_pages(struct inode *inode)
39a53e0c 1706{
036ed1b8 1707 atomic_inc(&F2FS_I(inode)->dirty_pages);
c1286ff4
JK
1708 inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1709 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
353c1624
JK
1710 if (IS_NOQUOTA(inode))
1711 inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
39a53e0c
JK
1712}
1713
1714static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
1715{
3f137dda 1716 atomic_dec(&sbi->nr_pages[count_type]);
39a53e0c
JK
1717}
1718
a7ffdbe2 1719static inline void inode_dec_dirty_pages(struct inode *inode)
39a53e0c 1720{
5ac9f36f
CY
1721 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1722 !S_ISLNK(inode->i_mode))
1fe54f9d
JK
1723 return;
1724
036ed1b8 1725 atomic_dec(&F2FS_I(inode)->dirty_pages);
c1286ff4
JK
1726 dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1727 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
353c1624
JK
1728 if (IS_NOQUOTA(inode))
1729 dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
39a53e0c
JK
1730}
1731
c1286ff4 1732static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
39a53e0c 1733{
3f137dda 1734 return atomic_read(&sbi->nr_pages[count_type]);
39a53e0c
JK
1735}
1736
036ed1b8 1737static inline int get_dirty_pages(struct inode *inode)
f8b2c1f9 1738{
036ed1b8 1739 return atomic_read(&F2FS_I(inode)->dirty_pages);
f8b2c1f9
JK
1740}
1741
5ac206cf
NJ
1742static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
1743{
c1286ff4
JK
1744 unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1745 unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1746 sbi->log_blocks_per_seg;
1747
1748 return segs / sbi->segs_per_sec;
5ac206cf
NJ
1749}
1750
39a53e0c
JK
1751static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
1752{
8b8343fa 1753 return sbi->total_valid_block_count;
39a53e0c
JK
1754}
1755
c1286ff4
JK
1756static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1757{
1758 return sbi->discard_blks;
1759}
1760
39a53e0c
JK
1761static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
1762{
1763 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1764
1765 /* return NAT or SIT bitmap */
1766 if (flag == NAT_BITMAP)
1767 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
1768 else if (flag == SIT_BITMAP)
1769 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
1770
1771 return 0;
1772}
1773
55141486
WL
1774static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
1775{
1776 return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
1777}
1778
39a53e0c
JK
1779static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
1780{
1781 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1dbe4152
CL
1782 int offset;
1783
55141486 1784 if (__cp_payload(sbi) > 0) {
1dbe4152
CL
1785 if (flag == NAT_BITMAP)
1786 return &ckpt->sit_nat_version_bitmap;
1787 else
65b85ccc 1788 return (unsigned char *)ckpt + F2FS_BLKSIZE;
1dbe4152
CL
1789 } else {
1790 offset = (flag == NAT_BITMAP) ?
25ca923b 1791 le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
1dbe4152
CL
1792 return &ckpt->sit_nat_version_bitmap + offset;
1793 }
39a53e0c
JK
1794}
1795
1796static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
1797{
6b266c3a 1798 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
39a53e0c 1799
6b266c3a 1800 if (sbi->cur_cp_pack == 2)
39a53e0c 1801 start_addr += sbi->blocks_per_seg;
6b266c3a
JK
1802 return start_addr;
1803}
39a53e0c 1804
6b266c3a
JK
1805static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
1806{
1807 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
1808
1809 if (sbi->cur_cp_pack == 1)
1810 start_addr += sbi->blocks_per_seg;
39a53e0c
JK
1811 return start_addr;
1812}
1813
6b266c3a
JK
1814static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
1815{
1816 sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
1817}
1818
39a53e0c
JK
1819static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
1820{
1821 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
1822}
1823
13f00235
JK
1824static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1825 struct inode *inode, bool is_inode)
39a53e0c
JK
1826{
1827 block_t valid_block_count;
1828 unsigned int valid_node_count;
13f00235
JK
1829 bool quota = inode && !is_inode;
1830
1831 if (quota) {
1832 int ret = dquot_reserve_block(inode, 1);
1833 if (ret)
1834 return ret;
1835 }
39a53e0c 1836
353c1624
JK
1837#ifdef CONFIG_F2FS_FAULT_INJECTION
1838 if (time_to_inject(sbi, FAULT_BLOCK)) {
1839 f2fs_show_injection_info(FAULT_BLOCK);
1840 goto enospc;
1841 }
1842#endif
1843
39a53e0c
JK
1844 spin_lock(&sbi->stat_lock);
1845
ef86d709 1846 valid_block_count = sbi->total_valid_block_count + 1;
353c1624 1847 if (unlikely(valid_block_count + sbi->current_reserved_blocks >
13f00235 1848 sbi->user_block_count)) {
39a53e0c 1849 spin_unlock(&sbi->stat_lock);
13f00235 1850 goto enospc;
39a53e0c
JK
1851 }
1852
ef86d709 1853 valid_node_count = sbi->total_valid_node_count + 1;
cfb271d4 1854 if (unlikely(valid_node_count > sbi->total_node_count)) {
39a53e0c 1855 spin_unlock(&sbi->stat_lock);
13f00235 1856 goto enospc;
39a53e0c
JK
1857 }
1858
ef86d709
GZ
1859 sbi->total_valid_node_count++;
1860 sbi->total_valid_block_count++;
39a53e0c
JK
1861 spin_unlock(&sbi->stat_lock);
1862
13f00235
JK
1863 if (inode) {
1864 if (is_inode)
1865 f2fs_mark_inode_dirty_sync(inode, true);
1866 else
1867 f2fs_i_blocks_write(inode, 1, true, true);
1868 }
1869
c1286ff4 1870 percpu_counter_inc(&sbi->alloc_valid_block_count);
13f00235
JK
1871 return 0;
1872
1873enospc:
1874 if (quota)
1875 dquot_release_reservation_block(inode, 1);
1876 return -ENOSPC;
39a53e0c
JK
1877}
1878
1879static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
13f00235 1880 struct inode *inode, bool is_inode)
39a53e0c
JK
1881{
1882 spin_lock(&sbi->stat_lock);
1883
9850cf4a
JK
1884 f2fs_bug_on(sbi, !sbi->total_valid_block_count);
1885 f2fs_bug_on(sbi, !sbi->total_valid_node_count);
13f00235 1886 f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
39a53e0c 1887
ef86d709
GZ
1888 sbi->total_valid_node_count--;
1889 sbi->total_valid_block_count--;
353c1624
JK
1890 if (sbi->reserved_blocks &&
1891 sbi->current_reserved_blocks < sbi->reserved_blocks)
1892 sbi->current_reserved_blocks++;
39a53e0c
JK
1893
1894 spin_unlock(&sbi->stat_lock);
13f00235
JK
1895
1896 if (!is_inode)
1897 f2fs_i_blocks_write(inode, 1, false, true);
39a53e0c
JK
1898}
1899
1900static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
1901{
8b8343fa 1902 return sbi->total_valid_node_count;
39a53e0c
JK
1903}
1904
1905static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
1906{
c1286ff4 1907 percpu_counter_inc(&sbi->total_valid_inode_count);
39a53e0c
JK
1908}
1909
0e80220a 1910static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
39a53e0c 1911{
c1286ff4 1912 percpu_counter_dec(&sbi->total_valid_inode_count);
39a53e0c
JK
1913}
1914
c1286ff4 1915static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
39a53e0c 1916{
c1286ff4 1917 return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
39a53e0c
JK
1918}
1919
a56c7c6f
JK
1920static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1921 pgoff_t index, bool for_write)
1922{
c1286ff4
JK
1923#ifdef CONFIG_F2FS_FAULT_INJECTION
1924 struct page *page = find_lock_page(mapping, index);
13f00235 1925
c1286ff4
JK
1926 if (page)
1927 return page;
1928
13f00235
JK
1929 if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
1930 f2fs_show_injection_info(FAULT_PAGE_ALLOC);
c1286ff4 1931 return NULL;
13f00235 1932 }
c1286ff4 1933#endif
a56c7c6f
JK
1934 if (!for_write)
1935 return grab_cache_page(mapping, index);
1936 return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1937}
1938
353c1624
JK
1939static inline struct page *f2fs_pagecache_get_page(
1940 struct address_space *mapping, pgoff_t index,
1941 int fgp_flags, gfp_t gfp_mask)
1942{
1943#ifdef CONFIG_F2FS_FAULT_INJECTION
1944 if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
1945 f2fs_show_injection_info(FAULT_PAGE_GET);
1946 return NULL;
1947 }
1948#endif
1949 return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
1950}
1951
6e2c64ad
JK
1952static inline void f2fs_copy_page(struct page *src, struct page *dst)
1953{
1954 char *src_kaddr = kmap(src);
1955 char *dst_kaddr = kmap(dst);
1956
1957 memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
1958 kunmap(dst);
1959 kunmap(src);
1960}
1961
39a53e0c
JK
1962static inline void f2fs_put_page(struct page *page, int unlock)
1963{
031fa8cc 1964 if (!page)
39a53e0c
JK
1965 return;
1966
1967 if (unlock) {
9850cf4a 1968 f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
39a53e0c
JK
1969 unlock_page(page);
1970 }
c1286ff4 1971 put_page(page);
39a53e0c
JK
1972}
1973
1974static inline void f2fs_put_dnode(struct dnode_of_data *dn)
1975{
1976 if (dn->node_page)
1977 f2fs_put_page(dn->node_page, 1);
1978 if (dn->inode_page && dn->node_page != dn->inode_page)
1979 f2fs_put_page(dn->inode_page, 0);
1980 dn->node_page = NULL;
1981 dn->inode_page = NULL;
1982}
1983
1984static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
e8512d2e 1985 size_t size)
39a53e0c 1986{
e8512d2e 1987 return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
39a53e0c
JK
1988}
1989
7bd59381
GZ
1990static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
1991 gfp_t flags)
1992{
1993 void *entry;
7bd59381 1994
80c54505
JK
1995 entry = kmem_cache_alloc(cachep, flags);
1996 if (!entry)
1997 entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
7bd59381
GZ
1998 return entry;
1999}
2000
353c1624
JK
2001static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
2002 int npages, bool no_fail)
740432f8
JK
2003{
2004 struct bio *bio;
2005
353c1624
JK
2006 if (no_fail) {
2007 /* No failure on bio allocation */
2008 bio = bio_alloc(GFP_NOIO, npages);
2009 if (!bio)
2010 bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
2011 return bio;
2012 }
2013#ifdef CONFIG_F2FS_FAULT_INJECTION
2014 if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
2015 f2fs_show_injection_info(FAULT_ALLOC_BIO);
2016 return NULL;
2017 }
2018#endif
2019 return bio_alloc(GFP_KERNEL, npages);
740432f8
JK
2020}
2021
9be32d72
JK
2022static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
2023 unsigned long index, void *item)
2024{
2025 while (radix_tree_insert(root, index, item))
2026 cond_resched();
2027}
2028
39a53e0c
JK
2029#define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
2030
2031static inline bool IS_INODE(struct page *page)
2032{
45590710 2033 struct f2fs_node *p = F2FS_NODE(page);
13f00235 2034
39a53e0c
JK
2035 return RAW_IS_INODE(p);
2036}
2037
13f00235
JK
2038static inline int offset_in_addr(struct f2fs_inode *i)
2039{
2040 return (i->i_inline & F2FS_EXTRA_ATTR) ?
2041 (le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
2042}
2043
39a53e0c
JK
2044static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
2045{
2046 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
2047}
2048
13f00235
JK
2049static inline int f2fs_has_extra_attr(struct inode *inode);
2050static inline block_t datablock_addr(struct inode *inode,
2051 struct page *node_page, unsigned int offset)
39a53e0c
JK
2052{
2053 struct f2fs_node *raw_node;
2054 __le32 *addr_array;
13f00235
JK
2055 int base = 0;
2056 bool is_inode = IS_INODE(node_page);
2057
45590710 2058 raw_node = F2FS_NODE(node_page);
13f00235
JK
2059
2060 /* from GC path only */
2061 if (!inode) {
2062 if (is_inode)
2063 base = offset_in_addr(&raw_node->i);
2064 } else if (f2fs_has_extra_attr(inode) && is_inode) {
2065 base = get_extra_isize(inode);
2066 }
2067
39a53e0c 2068 addr_array = blkaddr_in_node(raw_node);
13f00235 2069 return le32_to_cpu(addr_array[base + offset]);
39a53e0c
JK
2070}
2071
2072static inline int f2fs_test_bit(unsigned int nr, char *addr)
2073{
2074 int mask;
2075
2076 addr += (nr >> 3);
2077 mask = 1 << (7 - (nr & 0x07));
2078 return mask & *addr;
2079}
2080
a66cdd98
JK
2081static inline void f2fs_set_bit(unsigned int nr, char *addr)
2082{
2083 int mask;
2084
2085 addr += (nr >> 3);
2086 mask = 1 << (7 - (nr & 0x07));
2087 *addr |= mask;
2088}
2089
2090static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2091{
2092 int mask;
2093
2094 addr += (nr >> 3);
2095 mask = 1 << (7 - (nr & 0x07));
2096 *addr &= ~mask;
2097}
2098
52aca074 2099static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
39a53e0c
JK
2100{
2101 int mask;
2102 int ret;
2103
2104 addr += (nr >> 3);
2105 mask = 1 << (7 - (nr & 0x07));
2106 ret = mask & *addr;
2107 *addr |= mask;
2108 return ret;
2109}
2110
52aca074 2111static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
39a53e0c
JK
2112{
2113 int mask;
2114 int ret;
2115
2116 addr += (nr >> 3);
2117 mask = 1 << (7 - (nr & 0x07));
2118 ret = mask & *addr;
2119 *addr &= ~mask;
2120 return ret;
2121}
2122
c6ac4c0e
GZ
2123static inline void f2fs_change_bit(unsigned int nr, char *addr)
2124{
2125 int mask;
2126
2127 addr += (nr >> 3);
2128 mask = 1 << (7 - (nr & 0x07));
2129 *addr ^= mask;
2130}
2131
13f00235
JK
2132#define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
2133#define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
2134#define F2FS_FL_INHERITED (FS_PROJINHERIT_FL)
2135
2136static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
2137{
2138 if (S_ISDIR(mode))
2139 return flags;
2140 else if (S_ISREG(mode))
2141 return flags & F2FS_REG_FLMASK;
2142 else
2143 return flags & F2FS_OTHER_FLMASK;
2144}
2145
39a53e0c
JK
2146/* used for f2fs_inode_info->flags */
2147enum {
2148 FI_NEW_INODE, /* indicate newly allocated inode */
b3783873 2149 FI_DIRTY_INODE, /* indicate inode is dirty or not */
c1286ff4 2150 FI_AUTO_RECOVER, /* indicate inode is recoverable */
ed57c27f 2151 FI_DIRTY_DIR, /* indicate directory has dirty pages */
39a53e0c
JK
2152 FI_INC_LINK, /* need to increment i_nlink */
2153 FI_ACL_MODE, /* indicate acl mode */
2154 FI_NO_ALLOC, /* should not allocate any blocks */
c9b63bd0 2155 FI_FREE_NID, /* free allocated nide */
c11abd1a 2156 FI_NO_EXTENT, /* not to use the extent cache */
444c580f 2157 FI_INLINE_XATTR, /* used for inline xattr */
1001b347 2158 FI_INLINE_DATA, /* used for inline data*/
34d67deb 2159 FI_INLINE_DENTRY, /* used for inline dentry */
fff04f90
JK
2160 FI_APPEND_WRITE, /* inode has appended data */
2161 FI_UPDATE_WRITE, /* inode has in-place-update data */
88b88a66
JK
2162 FI_NEED_IPU, /* used for ipu per file */
2163 FI_ATOMIC_FILE, /* indicate atomic file */
dc8b8cea 2164 FI_ATOMIC_COMMIT, /* indicate the state of atomical committing */
02a1335f 2165 FI_VOLATILE_FILE, /* indicate volatile file */
3c6c2beb 2166 FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */
1e84371f 2167 FI_DROP_CACHE, /* drop dirty page cache */
b3d208f9 2168 FI_DATA_EXIST, /* indicate data exists */
510022a8 2169 FI_INLINE_DOTS, /* indicate inline dot dentries */
c1286ff4
JK
2170 FI_DO_DEFRAG, /* indicate defragment is running */
2171 FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */
13f00235
JK
2172 FI_NO_PREALLOC, /* indicate skipped preallocated blocks */
2173 FI_HOT_DATA, /* indicate file is hot */
2174 FI_EXTRA_ATTR, /* indicate file has extra attribute */
2175 FI_PROJ_INHERIT, /* indicate file inherits projectid */
39a53e0c
JK
2176};
2177
c1286ff4
JK
2178static inline void __mark_inode_dirty_flag(struct inode *inode,
2179 int flag, bool set)
2180{
2181 switch (flag) {
2182 case FI_INLINE_XATTR:
2183 case FI_INLINE_DATA:
2184 case FI_INLINE_DENTRY:
2185 if (set)
2186 return;
2187 case FI_DATA_EXIST:
2188 case FI_INLINE_DOTS:
0ef31c7b 2189 f2fs_mark_inode_dirty_sync(inode, true);
c1286ff4
JK
2190 }
2191}
2192
2193static inline void set_inode_flag(struct inode *inode, int flag)
2194{
2195 if (!test_bit(flag, &F2FS_I(inode)->flags))
2196 set_bit(flag, &F2FS_I(inode)->flags);
2197 __mark_inode_dirty_flag(inode, flag, true);
2198}
2199
2200static inline int is_inode_flag_set(struct inode *inode, int flag)
2201{
2202 return test_bit(flag, &F2FS_I(inode)->flags);
2203}
2204
2205static inline void clear_inode_flag(struct inode *inode, int flag)
2206{
2207 if (test_bit(flag, &F2FS_I(inode)->flags))
2208 clear_bit(flag, &F2FS_I(inode)->flags);
2209 __mark_inode_dirty_flag(inode, flag, false);
2210}
2211
2212static inline void set_acl_inode(struct inode *inode, umode_t mode)
2213{
2214 F2FS_I(inode)->i_acl_mode = mode;
2215 set_inode_flag(inode, FI_ACL_MODE);
0ef31c7b 2216 f2fs_mark_inode_dirty_sync(inode, false);
c1286ff4
JK
2217}
2218
2219static inline void f2fs_i_links_write(struct inode *inode, bool inc)
39a53e0c 2220{
c1286ff4
JK
2221 if (inc)
2222 inc_nlink(inode);
2223 else
2224 drop_nlink(inode);
0ef31c7b 2225 f2fs_mark_inode_dirty_sync(inode, true);
39a53e0c
JK
2226}
2227
c1286ff4 2228static inline void f2fs_i_blocks_write(struct inode *inode,
13f00235 2229 block_t diff, bool add, bool claim)
39a53e0c 2230{
c1286ff4
JK
2231 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
2232 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
2233
13f00235
JK
2234 /* add = 1, claim = 1 should be dquot_reserve_block in pair */
2235 if (add) {
2236 if (claim)
2237 dquot_claim_block(inode, diff);
2238 else
2239 dquot_alloc_block_nofail(inode, diff);
2240 } else {
2241 dquot_free_block(inode, diff);
2242 }
2243
0ef31c7b 2244 f2fs_mark_inode_dirty_sync(inode, true);
c1286ff4
JK
2245 if (clean || recover)
2246 set_inode_flag(inode, FI_AUTO_RECOVER);
39a53e0c
JK
2247}
2248
c1286ff4 2249static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
39a53e0c 2250{
c1286ff4
JK
2251 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
2252 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
2253
2254 if (i_size_read(inode) == i_size)
2255 return;
2256
2257 i_size_write(inode, i_size);
0ef31c7b 2258 f2fs_mark_inode_dirty_sync(inode, true);
c1286ff4
JK
2259 if (clean || recover)
2260 set_inode_flag(inode, FI_AUTO_RECOVER);
39a53e0c
JK
2261}
2262
c1286ff4
JK
2263static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2264{
2265 F2FS_I(inode)->i_current_depth = depth;
0ef31c7b 2266 f2fs_mark_inode_dirty_sync(inode, true);
39a53e0c
JK
2267}
2268
c1286ff4 2269static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
444c580f 2270{
c1286ff4 2271 F2FS_I(inode)->i_xattr_nid = xnid;
0ef31c7b 2272 f2fs_mark_inode_dirty_sync(inode, true);
c1286ff4
JK
2273}
2274
2275static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2276{
2277 F2FS_I(inode)->i_pino = pino;
0ef31c7b 2278 f2fs_mark_inode_dirty_sync(inode, true);
c1286ff4
JK
2279}
2280
2281static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2282{
2283 struct f2fs_inode_info *fi = F2FS_I(inode);
2284
444c580f 2285 if (ri->i_inline & F2FS_INLINE_XATTR)
c1286ff4 2286 set_bit(FI_INLINE_XATTR, &fi->flags);
1001b347 2287 if (ri->i_inline & F2FS_INLINE_DATA)
c1286ff4 2288 set_bit(FI_INLINE_DATA, &fi->flags);
34d67deb 2289 if (ri->i_inline & F2FS_INLINE_DENTRY)
c1286ff4 2290 set_bit(FI_INLINE_DENTRY, &fi->flags);
b3d208f9 2291 if (ri->i_inline & F2FS_DATA_EXIST)
c1286ff4 2292 set_bit(FI_DATA_EXIST, &fi->flags);
510022a8 2293 if (ri->i_inline & F2FS_INLINE_DOTS)
c1286ff4 2294 set_bit(FI_INLINE_DOTS, &fi->flags);
13f00235
JK
2295 if (ri->i_inline & F2FS_EXTRA_ATTR)
2296 set_bit(FI_EXTRA_ATTR, &fi->flags);
444c580f
JK
2297}
2298
c1286ff4 2299static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
444c580f
JK
2300{
2301 ri->i_inline = 0;
2302
c1286ff4 2303 if (is_inode_flag_set(inode, FI_INLINE_XATTR))
444c580f 2304 ri->i_inline |= F2FS_INLINE_XATTR;
c1286ff4 2305 if (is_inode_flag_set(inode, FI_INLINE_DATA))
1001b347 2306 ri->i_inline |= F2FS_INLINE_DATA;
c1286ff4 2307 if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
34d67deb 2308 ri->i_inline |= F2FS_INLINE_DENTRY;
c1286ff4 2309 if (is_inode_flag_set(inode, FI_DATA_EXIST))
b3d208f9 2310 ri->i_inline |= F2FS_DATA_EXIST;
c1286ff4 2311 if (is_inode_flag_set(inode, FI_INLINE_DOTS))
510022a8 2312 ri->i_inline |= F2FS_INLINE_DOTS;
13f00235
JK
2313 if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
2314 ri->i_inline |= F2FS_EXTRA_ATTR;
2315}
2316
2317static inline int f2fs_has_extra_attr(struct inode *inode)
2318{
2319 return is_inode_flag_set(inode, FI_EXTRA_ATTR);
444c580f
JK
2320}
2321
987c7c31
CY
2322static inline int f2fs_has_inline_xattr(struct inode *inode)
2323{
c1286ff4 2324 return is_inode_flag_set(inode, FI_INLINE_XATTR);
987c7c31
CY
2325}
2326
c1286ff4 2327static inline unsigned int addrs_per_inode(struct inode *inode)
de93653f 2328{
353c1624 2329 return CUR_ADDRS_PER_INODE(inode) - F2FS_INLINE_XATTR_ADDRS(inode);
de93653f
JK
2330}
2331
353c1624 2332static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
65985d93 2333{
695fd1ed 2334 struct f2fs_inode *ri = F2FS_INODE(page);
13f00235 2335
65985d93 2336 return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
353c1624 2337 F2FS_INLINE_XATTR_ADDRS(inode)]);
65985d93
JK
2338}
2339
2340static inline int inline_xattr_size(struct inode *inode)
2341{
353c1624 2342 return get_inline_xattr_addrs(inode) * sizeof(__le32);
65985d93
JK
2343}
2344
0dbdc2ae
JK
2345static inline int f2fs_has_inline_data(struct inode *inode)
2346{
c1286ff4 2347 return is_inode_flag_set(inode, FI_INLINE_DATA);
0dbdc2ae
JK
2348}
2349
b3d208f9
JK
2350static inline int f2fs_exist_data(struct inode *inode)
2351{
c1286ff4 2352 return is_inode_flag_set(inode, FI_DATA_EXIST);
b3d208f9
JK
2353}
2354
510022a8
JK
2355static inline int f2fs_has_inline_dots(struct inode *inode)
2356{
c1286ff4 2357 return is_inode_flag_set(inode, FI_INLINE_DOTS);
510022a8
JK
2358}
2359
88b88a66
JK
2360static inline bool f2fs_is_atomic_file(struct inode *inode)
2361{
c1286ff4 2362 return is_inode_flag_set(inode, FI_ATOMIC_FILE);
88b88a66
JK
2363}
2364
dc8b8cea
CY
2365static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
2366{
2367 return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
2368}
2369
02a1335f
JK
2370static inline bool f2fs_is_volatile_file(struct inode *inode)
2371{
c1286ff4 2372 return is_inode_flag_set(inode, FI_VOLATILE_FILE);
02a1335f
JK
2373}
2374
3c6c2beb
JK
2375static inline bool f2fs_is_first_block_written(struct inode *inode)
2376{
c1286ff4 2377 return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
3c6c2beb
JK
2378}
2379
1e84371f
JK
2380static inline bool f2fs_is_drop_cache(struct inode *inode)
2381{
c1286ff4 2382 return is_inode_flag_set(inode, FI_DROP_CACHE);
1e84371f
JK
2383}
2384
13f00235 2385static inline void *inline_data_addr(struct inode *inode, struct page *page)
1001b347 2386{
695fd1ed 2387 struct f2fs_inode *ri = F2FS_INODE(page);
13f00235
JK
2388 int extra_size = get_extra_isize(inode);
2389
2390 return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
1001b347
HL
2391}
2392
34d67deb
CY
2393static inline int f2fs_has_inline_dentry(struct inode *inode)
2394{
c1286ff4 2395 return is_inode_flag_set(inode, FI_INLINE_DENTRY);
34d67deb
CY
2396}
2397
9486ba44
JK
2398static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
2399{
2400 if (!f2fs_has_inline_dentry(dir))
2401 kunmap(page);
2402}
2403
b5492af7
JK
2404static inline int is_file(struct inode *inode, int type)
2405{
2406 return F2FS_I(inode)->i_advise & type;
2407}
2408
2409static inline void set_file(struct inode *inode, int type)
2410{
2411 F2FS_I(inode)->i_advise |= type;
0ef31c7b 2412 f2fs_mark_inode_dirty_sync(inode, true);
b5492af7
JK
2413}
2414
2415static inline void clear_file(struct inode *inode, int type)
2416{
2417 F2FS_I(inode)->i_advise &= ~type;
0ef31c7b 2418 f2fs_mark_inode_dirty_sync(inode, true);
b5492af7
JK
2419}
2420
9f495d82
JK
2421static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
2422{
353c1624
JK
2423 bool ret;
2424
9f495d82
JK
2425 if (dsync) {
2426 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
9f495d82
JK
2427
2428 spin_lock(&sbi->inode_lock[DIRTY_META]);
2429 ret = list_empty(&F2FS_I(inode)->gdirty_list);
2430 spin_unlock(&sbi->inode_lock[DIRTY_META]);
2431 return ret;
2432 }
2433 if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
2434 file_keep_isize(inode) ||
2435 i_size_read(inode) & PAGE_MASK)
2436 return false;
353c1624
JK
2437
2438 down_read(&F2FS_I(inode)->i_sem);
2439 ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2440 up_read(&F2FS_I(inode)->i_sem);
2441
2442 return ret;
9f495d82
JK
2443}
2444
353c1624 2445#define sb_rdonly f2fs_readonly
77888c1e
JK
2446static inline int f2fs_readonly(struct super_block *sb)
2447{
2448 return sb->s_flags & MS_RDONLY;
2449}
2450
1e968fdf
JK
2451static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2452{
c1286ff4 2453 return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
744602cf
JK
2454}
2455
eaa693f4
JK
2456static inline bool is_dot_dotdot(const struct qstr *str)
2457{
2458 if (str->len == 1 && str->name[0] == '.')
2459 return true;
2460
2461 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2462 return true;
2463
2464 return false;
2465}
2466
3e72f721
JK
2467static inline bool f2fs_may_extent_tree(struct inode *inode)
2468{
3e72f721 2469 if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
c1286ff4 2470 is_inode_flag_set(inode, FI_NO_EXTENT))
3e72f721
JK
2471 return false;
2472
c1286ff4
JK
2473 return S_ISREG(inode->i_mode);
2474}
2475
2476static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
2477 size_t size, gfp_t flags)
2478{
2479#ifdef CONFIG_F2FS_FAULT_INJECTION
13f00235
JK
2480 if (time_to_inject(sbi, FAULT_KMALLOC)) {
2481 f2fs_show_injection_info(FAULT_KMALLOC);
c1286ff4 2482 return NULL;
13f00235 2483 }
c1286ff4
JK
2484#endif
2485 return kmalloc(size, flags);
3e72f721
JK
2486}
2487
13f00235 2488static inline void *kvmalloc(size_t size, gfp_t flags)
39307a8e
JK
2489{
2490 void *ret;
2491
2492 ret = kmalloc(size, flags | __GFP_NOWARN);
2493 if (!ret)
2494 ret = __vmalloc(size, flags, PAGE_KERNEL);
2495 return ret;
2496}
2497
13f00235 2498static inline void *kvzalloc(size_t size, gfp_t flags)
39307a8e
JK
2499{
2500 void *ret;
2501
2502 ret = kzalloc(size, flags | __GFP_NOWARN);
2503 if (!ret)
2504 ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
2505 return ret;
2506}
2507
13f00235
JK
2508static inline int get_extra_isize(struct inode *inode)
2509{
2510 return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2511}
2512
353c1624
JK
2513static inline int f2fs_sb_has_flexible_inline_xattr(struct super_block *sb);
2514static inline int get_inline_xattr_addrs(struct inode *inode)
2515{
2516 return F2FS_I(inode)->i_inline_xattr_size;
2517}
2518
a6dda0e6 2519#define get_inode_mode(i) \
c1286ff4 2520 ((is_inode_flag_set(i, FI_ACL_MODE)) ? \
a6dda0e6
CH
2521 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2522
13f00235
JK
2523#define F2FS_TOTAL_EXTRA_ATTR_SIZE \
2524 (offsetof(struct f2fs_inode, i_extra_end) - \
2525 offsetof(struct f2fs_inode, i_extra_isize)) \
2526
2527#define F2FS_OLD_ATTRIBUTE_SIZE (offsetof(struct f2fs_inode, i_addr))
2528#define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field) \
2529 ((offsetof(typeof(*f2fs_inode), field) + \
2530 sizeof((f2fs_inode)->field)) \
2531 <= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize)) \
2532
2533static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2534{
2535 int i;
2536
2537 spin_lock(&sbi->iostat_lock);
2538 for (i = 0; i < NR_IO_TYPE; i++)
2539 sbi->write_iostat[i] = 0;
2540 spin_unlock(&sbi->iostat_lock);
2541}
2542
2543static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2544 enum iostat_type type, unsigned long long io_bytes)
2545{
2546 if (!sbi->iostat_enable)
2547 return;
2548 spin_lock(&sbi->iostat_lock);
2549 sbi->write_iostat[type] += io_bytes;
2550
2551 if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2552 sbi->write_iostat[APP_BUFFERED_IO] =
2553 sbi->write_iostat[APP_WRITE_IO] -
2554 sbi->write_iostat[APP_DIRECT_IO];
2555 spin_unlock(&sbi->iostat_lock);
2556}
267378d4 2557
39a53e0c
JK
2558/*
2559 * file.c
2560 */
13f00235
JK
2561int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2562void truncate_data_blocks(struct dnode_of_data *dn);
2563int truncate_blocks(struct inode *inode, u64 from, bool lock);
2564int f2fs_truncate(struct inode *inode);
2565int f2fs_getattr(struct vfsmount *mnt, struct dentry *dentry,
2566 struct kstat *stat);
2567int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2568int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2569int truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2570long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2571long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
39a53e0c
JK
2572
2573/*
2574 * inode.c
2575 */
13f00235
JK
2576void f2fs_set_inode_flags(struct inode *inode);
2577bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2578void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2579struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2580struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2581int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2582int update_inode(struct inode *inode, struct page *node_page);
2583int update_inode_page(struct inode *inode);
2584int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2585void f2fs_evict_inode(struct inode *inode);
2586void handle_failed_inode(struct inode *inode);
39a53e0c
JK
2587
2588/*
2589 * namei.c
2590 */
2591struct dentry *f2fs_get_parent(struct dentry *child);
2592
2593/*
2594 * dir.c
2595 */
13f00235
JK
2596void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
2597unsigned char get_de_type(struct f2fs_dir_entry *de);
2598struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
2599 f2fs_hash_t namehash, int *max_slots,
2600 struct f2fs_dentry_ptr *d);
2601int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2602 unsigned int start_pos, struct fscrypt_str *fstr);
2603void do_make_empty_dir(struct inode *inode, struct inode *parent,
2604 struct f2fs_dentry_ptr *d);
2605struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
2606 const struct qstr *new_name,
2607 const struct qstr *orig_name, struct page *dpage);
2608void update_parent_metadata(struct inode *dir, struct inode *inode,
2609 unsigned int current_depth);
2610int room_for_filename(const void *bitmap, int slots, int max_slots);
2611void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2612struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2613 struct fscrypt_name *fname, struct page **res_page);
2614struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2615 const struct qstr *child, struct page **res_page);
2616struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2617ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2618 struct page **page);
2619void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2620 struct page *page, struct inode *inode);
2621void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2622 const struct qstr *name, f2fs_hash_t name_hash,
2623 unsigned int bit_pos);
2624int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2625 const struct qstr *orig_name,
2626 struct inode *inode, nid_t ino, umode_t mode);
2627int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
2628 struct inode *inode, nid_t ino, umode_t mode);
2629int __f2fs_add_link(struct inode *dir, const struct qstr *name,
2630 struct inode *inode, nid_t ino, umode_t mode);
2631void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2632 struct inode *dir, struct inode *inode);
2633int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2634bool f2fs_empty_dir(struct inode *dir);
39a53e0c 2635
b7f7a5e0
AV
2636static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2637{
2b0143b5 2638 return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
510022a8 2639 inode, inode->i_ino, inode->i_mode);
b7f7a5e0
AV
2640}
2641
39a53e0c
JK
2642/*
2643 * super.c
2644 */
13f00235
JK
2645int f2fs_inode_dirtied(struct inode *inode, bool sync);
2646void f2fs_inode_synced(struct inode *inode);
353c1624 2647int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
13f00235
JK
2648void f2fs_quota_off_umount(struct super_block *sb);
2649int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2650int f2fs_sync_fs(struct super_block *sb, int sync);
a07ef784 2651extern __printf(3, 4)
13f00235 2652void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
c1286ff4 2653int sanity_check_ckpt(struct f2fs_sb_info *sbi);
39a53e0c
JK
2654
2655/*
2656 * hash.c
2657 */
13f00235
JK
2658f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
2659 struct fscrypt_name *fname);
39a53e0c
JK
2660
2661/*
2662 * node.c
2663 */
2664struct dnode_of_data;
2665struct node_info;
2666
13f00235
JK
2667bool available_free_memory(struct f2fs_sb_info *sbi, int type);
2668int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2669bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2670bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2671void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
2672pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2673int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2674int truncate_inode_blocks(struct inode *inode, pgoff_t from);
353c1624 2675int truncate_xattr_node(struct inode *inode);
13f00235
JK
2676int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2677int remove_inode_page(struct inode *inode);
2678struct page *new_inode_page(struct inode *inode);
2679struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs);
2680void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2681struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2682struct page *get_node_page_ra(struct page *parent, int start);
2683void move_node_page(struct page *node_page, int gc_type);
2684int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2685 struct writeback_control *wbc, bool atomic);
2686int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc,
2687 bool do_balance, enum iostat_type io_type);
2688void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2689bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2690void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2691void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2692int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2693void recover_inline_xattr(struct inode *inode, struct page *page);
2694int recover_xattr_data(struct inode *inode, struct page *page,
2695 block_t blkaddr);
2696int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2697int restore_node_summary(struct f2fs_sb_info *sbi,
2698 unsigned int segno, struct f2fs_summary_block *sum);
2699void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2700int build_node_manager(struct f2fs_sb_info *sbi);
2701void destroy_node_manager(struct f2fs_sb_info *sbi);
6e6093a8 2702int __init create_node_manager_caches(void);
39a53e0c
JK
2703void destroy_node_manager_caches(void);
2704
2705/*
2706 * segment.c
2707 */
13f00235
JK
2708bool need_SSR(struct f2fs_sb_info *sbi);
2709void register_inmem_page(struct inode *inode, struct page *page);
353c1624 2710void drop_inmem_pages_all(struct f2fs_sb_info *sbi);
13f00235
JK
2711void drop_inmem_pages(struct inode *inode);
2712void drop_inmem_page(struct inode *inode, struct page *page);
2713int commit_inmem_pages(struct inode *inode);
2714void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2715void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
353c1624 2716int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
13f00235 2717int create_flush_cmd_control(struct f2fs_sb_info *sbi);
353c1624 2718int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
13f00235
JK
2719void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2720void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2721bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
353c1624
JK
2722void init_discard_policy(struct discard_policy *dpolicy, int discard_type,
2723 unsigned int granularity);
13f00235 2724void stop_discard_thread(struct f2fs_sb_info *sbi);
353c1624 2725bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
13f00235
JK
2726void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2727void release_discard_addrs(struct f2fs_sb_info *sbi);
2728int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2729void allocate_new_segments(struct f2fs_sb_info *sbi);
2730int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2731bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2732struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2733void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2734void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2735 enum iostat_type io_type);
2736void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2737void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
2738int rewrite_data_page(struct f2fs_io_info *fio);
2739void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2740 block_t old_blkaddr, block_t new_blkaddr,
2741 bool recover_curseg, bool recover_newaddr);
2742void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2743 block_t old_addr, block_t new_addr,
2744 unsigned char version, bool recover_curseg,
2745 bool recover_newaddr);
2746void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2747 block_t old_blkaddr, block_t *new_blkaddr,
2748 struct f2fs_summary *sum, int type,
2749 struct f2fs_io_info *fio, bool add_list);
2750void f2fs_wait_on_page_writeback(struct page *page,
2751 enum page_type type, bool ordered);
2752void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr);
2753void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2754void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2755int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2756 unsigned int val, int alloc);
2757void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2758int build_segment_manager(struct f2fs_sb_info *sbi);
2759void destroy_segment_manager(struct f2fs_sb_info *sbi);
7fd9e544
JK
2760int __init create_segment_manager_caches(void);
2761void destroy_segment_manager_caches(void);
39a53e0c
JK
2762
2763/*
2764 * checkpoint.c
2765 */
13f00235
JK
2766void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
2767struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2768struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2769struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2770bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type);
2771int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2772 int type, bool sync);
2773void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2774long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2775 long nr_to_write, enum iostat_type io_type);
2776void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2777void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2778void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2779bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
353c1624
JK
2780void set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
2781 unsigned int devidx, int type);
2782bool is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
2783 unsigned int devidx, int type);
13f00235
JK
2784int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
2785int acquire_orphan_inode(struct f2fs_sb_info *sbi);
2786void release_orphan_inode(struct f2fs_sb_info *sbi);
2787void add_orphan_inode(struct inode *inode);
2788void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2789int recover_orphan_inodes(struct f2fs_sb_info *sbi);
2790int get_valid_checkpoint(struct f2fs_sb_info *sbi);
2791void update_dirty_page(struct inode *inode, struct page *page);
2792void remove_dirty_inode(struct inode *inode);
2793int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2794int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2795void init_ino_entry_info(struct f2fs_sb_info *sbi);
6e6093a8 2796int __init create_checkpoint_caches(void);
39a53e0c
JK
2797void destroy_checkpoint_caches(void);
2798
2799/*
2800 * data.c
2801 */
13f00235
JK
2802void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2803void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
2804 struct inode *inode, nid_t ino, pgoff_t idx,
2805 enum page_type type);
2806void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
2807int f2fs_submit_page_bio(struct f2fs_io_info *fio);
2808int f2fs_submit_page_write(struct f2fs_io_info *fio);
2809struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2810 block_t blk_addr, struct bio *bio);
2811int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
2812void set_data_blkaddr(struct dnode_of_data *dn);
2813void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
2814int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2815int reserve_new_block(struct dnode_of_data *dn);
2816int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2817int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2818int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
2819struct page *get_read_data_page(struct inode *inode, pgoff_t index,
2820 int op_flags, bool for_write);
2821struct page *find_data_page(struct inode *inode, pgoff_t index);
2822struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
2823 bool for_write);
2824struct page *get_new_data_page(struct inode *inode,
2825 struct page *ipage, pgoff_t index, bool new_i_size);
2826int do_write_data_page(struct f2fs_io_info *fio);
2827int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2828 int create, int flag);
2829int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2830 u64 start, u64 len);
2831void f2fs_set_page_dirty_nobuffers(struct page *page);
2832int __f2fs_write_data_pages(struct address_space *mapping,
2833 struct writeback_control *wbc,
2834 enum iostat_type io_type);
2835void f2fs_invalidate_page(struct page *page, unsigned int offset,
2836 unsigned int length);
2837int f2fs_release_page(struct page *page, gfp_t wait);
c1286ff4 2838#ifdef CONFIG_MIGRATION
13f00235
JK
2839int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2840 struct page *page, enum migrate_mode mode);
c1286ff4 2841#endif
39a53e0c
JK
2842
2843/*
2844 * gc.c
2845 */
13f00235
JK
2846int start_gc_thread(struct f2fs_sb_info *sbi);
2847void stop_gc_thread(struct f2fs_sb_info *sbi);
2848block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2849int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2850 unsigned int segno);
2851void build_gc_manager(struct f2fs_sb_info *sbi);
39a53e0c
JK
2852
2853/*
2854 * recovery.c
2855 */
13f00235
JK
2856int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
2857bool space_for_roll_forward(struct f2fs_sb_info *sbi);
39a53e0c
JK
2858
2859/*
2860 * debug.c
2861 */
2862#ifdef CONFIG_F2FS_STAT_FS
2863struct f2fs_stat_info {
2864 struct list_head stat_list;
2865 struct f2fs_sb_info *sbi;
39a53e0c
JK
2866 int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
2867 int main_area_segs, main_area_sections, main_area_zones;
5b7ee374
CY
2868 unsigned long long hit_largest, hit_cached, hit_rbtree;
2869 unsigned long long hit_total, total_ext;
c1286ff4 2870 int ext_tree, zombie_tree, ext_node;
353c1624
JK
2871 int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
2872 int ndirty_data, ndirty_qdata;
3f137dda 2873 int inmem_pages;
353c1624 2874 unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
13f00235
JK
2875 int nats, dirty_nats, sits, dirty_sits;
2876 int free_nids, avail_nids, alloc_nids;
39a53e0c 2877 int total_count, utilization;
9e266223 2878 int bg_gc, nr_wb_cp_data, nr_wb_data;
353c1624
JK
2879 int nr_flushing, nr_flushed, flush_list_empty;
2880 int nr_discarding, nr_discarded;
13f00235
JK
2881 int nr_discard_cmd;
2882 unsigned int undiscard_blks;
2883 int inline_xattr, inline_inode, inline_dir, append, update, orphans;
2884 int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
c1286ff4 2885 unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
39a53e0c
JK
2886 unsigned int bimodal, avg_vblocks;
2887 int util_free, util_valid, util_invalid;
2888 int rsvd_segs, overp_segs;
2889 int dirty_count, node_pages, meta_pages;
c1286ff4 2890 int prefree_count, call_count, cp_count, bg_cp_count;
39a53e0c 2891 int tot_segs, node_segs, data_segs, free_segs, free_secs;
e1235983 2892 int bg_node_segs, bg_data_segs;
39a53e0c 2893 int tot_blks, data_blks, node_blks;
e1235983 2894 int bg_data_blks, bg_node_blks;
39a53e0c
JK
2895 int curseg[NR_CURSEG_TYPE];
2896 int cursec[NR_CURSEG_TYPE];
2897 int curzone[NR_CURSEG_TYPE];
2898
2899 unsigned int segment_count[2];
2900 unsigned int block_count[2];
b9a2c252 2901 unsigned int inplace_count;
9edcdabf 2902 unsigned long long base_mem, cache_mem, page_mem;
39a53e0c
JK
2903};
2904
963d4f7d
GZ
2905static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
2906{
6c311ec6 2907 return (struct f2fs_stat_info *)sbi->stat_info;
963d4f7d
GZ
2908}
2909
942e0be6 2910#define stat_inc_cp_count(si) ((si)->cp_count++)
c1286ff4 2911#define stat_inc_bg_cp_count(si) ((si)->bg_cp_count++)
dcdfff65
JK
2912#define stat_inc_call_count(si) ((si)->call_count++)
2913#define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
c1286ff4
JK
2914#define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++)
2915#define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--)
5b7ee374
CY
2916#define stat_inc_total_hit(sbi) (atomic64_inc(&(sbi)->total_hit_ext))
2917#define stat_inc_rbtree_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_rbtree))
2918#define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest))
2919#define stat_inc_cached_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_cached))
d5e8f6c9
CY
2920#define stat_inc_inline_xattr(inode) \
2921 do { \
2922 if (f2fs_has_inline_xattr(inode)) \
2923 (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \
2924 } while (0)
2925#define stat_dec_inline_xattr(inode) \
2926 do { \
2927 if (f2fs_has_inline_xattr(inode)) \
2928 (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \
2929 } while (0)
0dbdc2ae
JK
2930#define stat_inc_inline_inode(inode) \
2931 do { \
2932 if (f2fs_has_inline_data(inode)) \
03e14d52 2933 (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
0dbdc2ae
JK
2934 } while (0)
2935#define stat_dec_inline_inode(inode) \
2936 do { \
2937 if (f2fs_has_inline_data(inode)) \
03e14d52 2938 (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
0dbdc2ae 2939 } while (0)
3289c061
JK
2940#define stat_inc_inline_dir(inode) \
2941 do { \
2942 if (f2fs_has_inline_dentry(inode)) \
03e14d52 2943 (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
3289c061
JK
2944 } while (0)
2945#define stat_dec_inline_dir(inode) \
2946 do { \
2947 if (f2fs_has_inline_dentry(inode)) \
03e14d52 2948 (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
3289c061 2949 } while (0)
dcdfff65
JK
2950#define stat_inc_seg_type(sbi, curseg) \
2951 ((sbi)->segment_count[(curseg)->alloc_type]++)
2952#define stat_inc_block_count(sbi, curseg) \
2953 ((sbi)->block_count[(curseg)->alloc_type]++)
b9a2c252
CL
2954#define stat_inc_inplace_blocks(sbi) \
2955 (atomic_inc(&(sbi)->inplace_count))
97a43c70 2956#define stat_inc_atomic_write(inode) \
13f00235 2957 (atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
97a43c70 2958#define stat_dec_atomic_write(inode) \
13f00235 2959 (atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
97a43c70
JK
2960#define stat_update_max_atomic_write(inode) \
2961 do { \
2962 int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt); \
2963 int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt); \
2964 if (cur > max) \
2965 atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur); \
2966 } while (0)
13f00235
JK
2967#define stat_inc_volatile_write(inode) \
2968 (atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
2969#define stat_dec_volatile_write(inode) \
2970 (atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
2971#define stat_update_max_volatile_write(inode) \
2972 do { \
2973 int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt); \
2974 int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt); \
2975 if (cur > max) \
2976 atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur); \
2977 } while (0)
e1235983 2978#define stat_inc_seg_count(sbi, type, gc_type) \
39a53e0c 2979 do { \
963d4f7d 2980 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
13f00235
JK
2981 si->tot_segs++; \
2982 if ((type) == SUM_TYPE_DATA) { \
39a53e0c 2983 si->data_segs++; \
e1235983
CL
2984 si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
2985 } else { \
39a53e0c 2986 si->node_segs++; \
e1235983
CL
2987 si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
2988 } \
39a53e0c
JK
2989 } while (0)
2990
2991#define stat_inc_tot_blk_count(si, blks) \
13f00235 2992 ((si)->tot_blks += (blks))
39a53e0c 2993
e1235983 2994#define stat_inc_data_blk_count(sbi, blks, gc_type) \
39a53e0c 2995 do { \
963d4f7d 2996 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
39a53e0c
JK
2997 stat_inc_tot_blk_count(si, blks); \
2998 si->data_blks += (blks); \
13f00235 2999 si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
39a53e0c
JK
3000 } while (0)
3001
e1235983 3002#define stat_inc_node_blk_count(sbi, blks, gc_type) \
39a53e0c 3003 do { \
963d4f7d 3004 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
39a53e0c
JK
3005 stat_inc_tot_blk_count(si, blks); \
3006 si->node_blks += (blks); \
13f00235 3007 si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
39a53e0c
JK
3008 } while (0)
3009
13f00235
JK
3010int f2fs_build_stats(struct f2fs_sb_info *sbi);
3011void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
c1286ff4 3012int __init f2fs_create_root_stats(void);
4589d25d 3013void f2fs_destroy_root_stats(void);
39a53e0c 3014#else
13f00235
JK
3015#define stat_inc_cp_count(si) do { } while (0)
3016#define stat_inc_bg_cp_count(si) do { } while (0)
3017#define stat_inc_call_count(si) do { } while (0)
3018#define stat_inc_bggc_count(si) do { } while (0)
3019#define stat_inc_dirty_inode(sbi, type) do { } while (0)
3020#define stat_dec_dirty_inode(sbi, type) do { } while (0)
3021#define stat_inc_total_hit(sb) do { } while (0)
3022#define stat_inc_rbtree_node_hit(sb) do { } while (0)
3023#define stat_inc_largest_node_hit(sbi) do { } while (0)
3024#define stat_inc_cached_node_hit(sbi) do { } while (0)
3025#define stat_inc_inline_xattr(inode) do { } while (0)
3026#define stat_dec_inline_xattr(inode) do { } while (0)
3027#define stat_inc_inline_inode(inode) do { } while (0)
3028#define stat_dec_inline_inode(inode) do { } while (0)
3029#define stat_inc_inline_dir(inode) do { } while (0)
3030#define stat_dec_inline_dir(inode) do { } while (0)
3031#define stat_inc_atomic_write(inode) do { } while (0)
3032#define stat_dec_atomic_write(inode) do { } while (0)
3033#define stat_update_max_atomic_write(inode) do { } while (0)
3034#define stat_inc_volatile_write(inode) do { } while (0)
3035#define stat_dec_volatile_write(inode) do { } while (0)
3036#define stat_update_max_volatile_write(inode) do { } while (0)
3037#define stat_inc_seg_type(sbi, curseg) do { } while (0)
3038#define stat_inc_block_count(sbi, curseg) do { } while (0)
3039#define stat_inc_inplace_blocks(sbi) do { } while (0)
3040#define stat_inc_seg_count(sbi, type, gc_type) do { } while (0)
3041#define stat_inc_tot_blk_count(si, blks) do { } while (0)
3042#define stat_inc_data_blk_count(sbi, blks, gc_type) do { } while (0)
3043#define stat_inc_node_blk_count(sbi, blks, gc_type) do { } while (0)
39a53e0c
JK
3044
3045static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
3046static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
c1286ff4 3047static inline int __init f2fs_create_root_stats(void) { return 0; }
4589d25d 3048static inline void f2fs_destroy_root_stats(void) { }
39a53e0c
JK
3049#endif
3050
3051extern const struct file_operations f2fs_dir_operations;
3052extern const struct file_operations f2fs_file_operations;
3053extern const struct inode_operations f2fs_file_inode_operations;
3054extern const struct address_space_operations f2fs_dblock_aops;
3055extern const struct address_space_operations f2fs_node_aops;
3056extern const struct address_space_operations f2fs_meta_aops;
3057extern const struct inode_operations f2fs_dir_inode_operations;
3058extern const struct inode_operations f2fs_symlink_inode_operations;
cbaf042a 3059extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
39a53e0c 3060extern const struct inode_operations f2fs_special_inode_operations;
29e7043f 3061extern struct kmem_cache *inode_entry_slab;
1001b347 3062
e18c65b2
HL
3063/*
3064 * inline.c
3065 */
13f00235
JK
3066bool f2fs_may_inline_data(struct inode *inode);
3067bool f2fs_may_inline_dentry(struct inode *inode);
3068void read_inline_data(struct page *page, struct page *ipage);
3069void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
3070int f2fs_read_inline_data(struct inode *inode, struct page *page);
3071int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3072int f2fs_convert_inline_inode(struct inode *inode);
3073int f2fs_write_inline_data(struct inode *inode, struct page *page);
3074bool recover_inline_data(struct inode *inode, struct page *npage);
3075struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
3076 struct fscrypt_name *fname, struct page **res_page);
3077int make_empty_inline_dir(struct inode *inode, struct inode *parent,
3078 struct page *ipage);
3079int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3080 const struct qstr *orig_name,
3081 struct inode *inode, nid_t ino, umode_t mode);
3082void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
3083 struct inode *dir, struct inode *inode);
3084bool f2fs_empty_inline_dir(struct inode *dir);
3085int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3086 struct fscrypt_str *fstr);
3087int f2fs_inline_data_fiemap(struct inode *inode,
3088 struct fiemap_extent_info *fieinfo,
3089 __u64 start, __u64 len);
cde4de12 3090
2658e50d
JK
3091/*
3092 * shrinker.c
3093 */
13f00235
JK
3094unsigned long f2fs_shrink_count(struct shrinker *shrink,
3095 struct shrink_control *sc);
3096unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3097 struct shrink_control *sc);
3098void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3099void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
2658e50d 3100
a28ef1f5
CY
3101/*
3102 * extent_cache.c
3103 */
13f00235
JK
3104struct rb_entry *__lookup_rb_tree(struct rb_root *root,
3105 struct rb_entry *cached_re, unsigned int ofs);
3106struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
3107 struct rb_root *root, struct rb_node **parent,
3108 unsigned int ofs);
3109struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
3110 struct rb_entry *cached_re, unsigned int ofs,
3111 struct rb_entry **prev_entry, struct rb_entry **next_entry,
3112 struct rb_node ***insert_p, struct rb_node **insert_parent,
3113 bool force);
3114bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
3115 struct rb_root *root);
3116unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3117bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3118void f2fs_drop_extent_tree(struct inode *inode);
3119unsigned int f2fs_destroy_extent_node(struct inode *inode);
3120void f2fs_destroy_extent_tree(struct inode *inode);
3121bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3122 struct extent_info *ei);
3123void f2fs_update_extent_cache(struct dnode_of_data *dn);
19b2c30d 3124void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
13f00235
JK
3125 pgoff_t fofs, block_t blkaddr, unsigned int len);
3126void init_extent_cache_info(struct f2fs_sb_info *sbi);
a28ef1f5
CY
3127int __init create_extent_cache(void);
3128void destroy_extent_cache(void);
3129
13f00235
JK
3130/*
3131 * sysfs.c
3132 */
3133int __init f2fs_init_sysfs(void);
3134void f2fs_exit_sysfs(void);
3135int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3136void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
3137
cde4de12
JK
3138/*
3139 * crypto support
3140 */
c1286ff4 3141static inline bool f2fs_encrypted_inode(struct inode *inode)
cde4de12 3142{
cde4de12 3143 return file_is_encrypt(inode);
cde4de12
JK
3144}
3145
13f00235
JK
3146static inline bool f2fs_encrypted_file(struct inode *inode)
3147{
3148 return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
3149}
3150
cde4de12
JK
3151static inline void f2fs_set_encrypted_inode(struct inode *inode)
3152{
3153#ifdef CONFIG_F2FS_FS_ENCRYPTION
3154 file_set_encrypt(inode);
3155#endif
3156}
3157
3158static inline bool f2fs_bio_encrypted(struct bio *bio)
3159{
c1286ff4 3160 return bio->bi_private != NULL;
cde4de12
JK
3161}
3162
3163static inline int f2fs_sb_has_crypto(struct super_block *sb)
3164{
cde4de12 3165 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
cde4de12 3166}
f424f664 3167
22bbc1ef 3168static inline int f2fs_sb_mounted_blkzoned(struct super_block *sb)
fcc85a4d 3169{
22bbc1ef 3170 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_BLKZONED);
fcc85a4d
JK
3171}
3172
13f00235
JK
3173static inline int f2fs_sb_has_extra_attr(struct super_block *sb)
3174{
3175 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_EXTRA_ATTR);
3176}
3177
3178static inline int f2fs_sb_has_project_quota(struct super_block *sb)
3179{
3180 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_PRJQUOTA);
3181}
3182
3183static inline int f2fs_sb_has_inode_chksum(struct super_block *sb)
3184{
3185 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_INODE_CHKSUM);
3186}
3187
353c1624
JK
3188static inline int f2fs_sb_has_flexible_inline_xattr(struct super_block *sb)
3189{
3190 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_FLEXIBLE_INLINE_XATTR);
3191}
3192
3193static inline int f2fs_sb_has_quota_ino(struct super_block *sb)
3194{
3195 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_QUOTA_INO);
3196}
3197
018fc18e
DLM
3198#ifdef CONFIG_BLK_DEV_ZONED
3199static inline int get_blkz_type(struct f2fs_sb_info *sbi,
07f01079 3200 struct block_device *bdev, block_t blkaddr)
018fc18e
DLM
3201{
3202 unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
07f01079 3203 int i;
018fc18e 3204
07f01079
JK
3205 for (i = 0; i < sbi->s_ndevs; i++)
3206 if (FDEV(i).bdev == bdev)
3207 return FDEV(i).blkz_type[zno];
3208 return -EINVAL;
018fc18e
DLM
3209}
3210#endif
3211
4b1d4ef0
DLM
3212static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
3213{
3214 struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
3215
3216 return blk_queue_discard(q) || f2fs_sb_mounted_blkzoned(sbi->sb);
3217}
3218
c1286ff4
JK
3219static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
3220{
3221 clear_opt(sbi, ADAPTIVE);
3222 clear_opt(sbi, LFS);
0adda907 3223
c1286ff4
JK
3224 switch (mt) {
3225 case F2FS_MOUNT_ADAPTIVE:
3226 set_opt(sbi, ADAPTIVE);
3227 break;
3228 case F2FS_MOUNT_LFS:
3229 set_opt(sbi, LFS);
3230 break;
3231 }
3232}
0adda907 3233
c1286ff4
JK
3234static inline bool f2fs_may_encrypt(struct inode *inode)
3235{
3236#ifdef CONFIG_F2FS_FS_ENCRYPTION
3237 umode_t mode = inode->i_mode;
6b3bd08f 3238
c1286ff4 3239 return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
57e5055b 3240#else
6b3bd08f 3241 return 0;
c1286ff4 3242#endif
6b3bd08f
JK
3243}
3244
39a53e0c 3245#endif