4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
14 #include <linux/types.h>
15 #include <linux/page-flags.h>
16 #include <linux/buffer_head.h>
17 #include <linux/slab.h>
18 #include <linux/crc32.h>
19 #include <linux/magic.h>
20 #include <linux/kobject.h>
21 #include <linux/sched.h>
22 #include <linux/vmalloc.h>
23 #include <linux/bio.h>
24 #include <linux/blkdev.h>
25 #include <linux/quotaops.h>
26 #ifdef CONFIG_F2FS_FS_ENCRYPTION
27 #include <linux/fscrypt_supp.h>
29 #include <linux/fscrypt_notsupp.h>
31 #include <crypto/hash.h>
33 #ifdef CONFIG_F2FS_CHECK_FS
34 #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
36 #define f2fs_bug_on(sbi, condition) \
38 if (unlikely(condition)) { \
40 set_sbi_flag(sbi, SBI_NEED_FSCK); \
45 #ifdef CONFIG_F2FS_FAULT_INJECTION
60 struct f2fs_fault_info {
62 unsigned int inject_rate;
63 unsigned int inject_type;
66 extern char *fault_name[FAULT_MAX];
67 #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
73 #define F2FS_MOUNT_BG_GC 0x00000001
74 #define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
75 #define F2FS_MOUNT_DISCARD 0x00000004
76 #define F2FS_MOUNT_NOHEAP 0x00000008
77 #define F2FS_MOUNT_XATTR_USER 0x00000010
78 #define F2FS_MOUNT_POSIX_ACL 0x00000020
79 #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
80 #define F2FS_MOUNT_INLINE_XATTR 0x00000080
81 #define F2FS_MOUNT_INLINE_DATA 0x00000100
82 #define F2FS_MOUNT_INLINE_DENTRY 0x00000200
83 #define F2FS_MOUNT_FLUSH_MERGE 0x00000400
84 #define F2FS_MOUNT_NOBARRIER 0x00000800
85 #define F2FS_MOUNT_FASTBOOT 0x00001000
86 #define F2FS_MOUNT_EXTENT_CACHE 0x00002000
87 #define F2FS_MOUNT_FORCE_FG_GC 0x00004000
88 #define F2FS_MOUNT_DATA_FLUSH 0x00008000
89 #define F2FS_MOUNT_FAULT_INJECTION 0x00010000
90 #define F2FS_MOUNT_ADAPTIVE 0x00020000
91 #define F2FS_MOUNT_LFS 0x00040000
92 #define F2FS_MOUNT_USRQUOTA 0x00080000
93 #define F2FS_MOUNT_GRPQUOTA 0x00100000
94 #define F2FS_MOUNT_PRJQUOTA 0x00200000
95 #define F2FS_MOUNT_QUOTA 0x00400000
97 #define clear_opt(sbi, option) ((sbi)->mount_opt.opt &= ~F2FS_MOUNT_##option)
98 #define set_opt(sbi, option) ((sbi)->mount_opt.opt |= F2FS_MOUNT_##option)
99 #define test_opt(sbi, option) ((sbi)->mount_opt.opt & F2FS_MOUNT_##option)
101 #define ver_after(a, b) (typecheck(unsigned long long, a) && \
102 typecheck(unsigned long long, b) && \
103 ((long long)((a) - (b)) > 0))
105 typedef u32 block_t; /*
106 * should not change u32, since it is the on-disk block
107 * address format, __le32.
111 struct f2fs_mount_info {
115 #define F2FS_FEATURE_ENCRYPT 0x0001
116 #define F2FS_FEATURE_BLKZONED 0x0002
117 #define F2FS_FEATURE_ATOMIC_WRITE 0x0004
118 #define F2FS_FEATURE_EXTRA_ATTR 0x0008
119 #define F2FS_FEATURE_PRJQUOTA 0x0010
120 #define F2FS_FEATURE_INODE_CHKSUM 0x0020
122 #define F2FS_HAS_FEATURE(sb, mask) \
123 ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
124 #define F2FS_SET_FEATURE(sb, mask) \
125 (F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
126 #define F2FS_CLEAR_FEATURE(sb, mask) \
127 (F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
130 * For checkpoint manager
137 #define CP_UMOUNT 0x00000001
138 #define CP_FASTBOOT 0x00000002
139 #define CP_SYNC 0x00000004
140 #define CP_RECOVERY 0x00000008
141 #define CP_DISCARD 0x00000010
142 #define CP_TRIMMED 0x00000020
144 #define DEF_BATCHED_TRIM_SECTIONS 2048
145 #define BATCHED_TRIM_SEGMENTS(sbi) \
146 (GET_SEG_FROM_SEC(sbi, SM_I(sbi)->trim_sections))
147 #define BATCHED_TRIM_BLOCKS(sbi) \
148 (BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
149 #define MAX_DISCARD_BLOCKS(sbi) BLKS_PER_SEC(sbi)
150 #define DISCARD_ISSUE_RATE 8
151 #define DEF_MIN_DISCARD_ISSUE_TIME 50 /* 50 ms, if exists */
152 #define DEF_MAX_DISCARD_ISSUE_TIME 60000 /* 60 s, if no candidates */
153 #define DEF_CP_INTERVAL 60 /* 60 secs */
154 #define DEF_IDLE_INTERVAL 5 /* 5 secs */
165 * indicate meta/data type
177 /* for the list of ino */
179 ORPHAN_INO, /* for orphan ino list */
180 APPEND_INO, /* for append ino list */
181 UPDATE_INO, /* for update ino list */
182 MAX_INO_ENTRY, /* max. list */
186 struct list_head list; /* list head */
187 nid_t ino; /* inode number */
190 /* for the list of inodes to be GCed */
192 struct list_head list; /* list head */
193 struct inode *inode; /* vfs inode pointer */
196 /* for the bitmap indicate blocks to be discarded */
197 struct discard_entry {
198 struct list_head list; /* list head */
199 block_t start_blkaddr; /* start blockaddr of current segment */
200 unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */
203 /* default discard granularity of inner discard thread, unit: block count */
204 #define DEFAULT_DISCARD_GRANULARITY 16
206 /* max discard pend list number */
207 #define MAX_PLIST_NUM 512
208 #define plist_idx(blk_num) ((blk_num) >= MAX_PLIST_NUM ? \
209 (MAX_PLIST_NUM - 1) : (blk_num - 1))
211 #define P_ACTIVE 0x01
213 #define plist_issue(tag) (((tag) & P_ACTIVE) || ((tag) & P_TRIM))
221 struct discard_info {
222 block_t lstart; /* logical start address */
223 block_t len; /* length */
224 block_t start; /* actual start address in dev */
228 struct rb_node rb_node; /* rb node located in rb-tree */
231 block_t lstart; /* logical start address */
232 block_t len; /* length */
233 block_t start; /* actual start address in dev */
235 struct discard_info di; /* discard info */
238 struct list_head list; /* command list */
239 struct completion wait; /* compleation */
240 struct block_device *bdev; /* bdev */
241 unsigned short ref; /* reference count */
242 unsigned char state; /* state */
243 int error; /* bio error */
246 struct discard_cmd_control {
247 struct task_struct *f2fs_issue_discard; /* discard thread */
248 struct list_head entry_list; /* 4KB discard entry list */
249 struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
250 unsigned char pend_list_tag[MAX_PLIST_NUM];/* tag for pending entries */
251 struct list_head wait_list; /* store on-flushing entries */
252 wait_queue_head_t discard_wait_queue; /* waiting queue for wake-up */
253 unsigned int discard_wake; /* to wake up discard thread */
254 struct mutex cmd_lock;
255 unsigned int nr_discards; /* # of discards in the list */
256 unsigned int max_discards; /* max. discards to be issued */
257 unsigned int discard_granularity; /* discard granularity */
258 unsigned int undiscard_blks; /* # of undiscard blocks */
259 atomic_t issued_discard; /* # of issued discard */
260 atomic_t issing_discard; /* # of issing discard */
261 atomic_t discard_cmd_cnt; /* # of cached cmd count */
262 struct rb_root root; /* root of discard rb-tree */
265 /* for the list of fsync inodes, used only during recovery */
266 struct fsync_inode_entry {
267 struct list_head list; /* list head */
268 struct inode *inode; /* vfs inode pointer */
269 block_t blkaddr; /* block address locating the last fsync */
270 block_t last_dentry; /* block address locating the last dentry */
273 #define nats_in_cursum(jnl) (le16_to_cpu((jnl)->n_nats))
274 #define sits_in_cursum(jnl) (le16_to_cpu((jnl)->n_sits))
276 #define nat_in_journal(jnl, i) ((jnl)->nat_j.entries[i].ne)
277 #define nid_in_journal(jnl, i) ((jnl)->nat_j.entries[i].nid)
278 #define sit_in_journal(jnl, i) ((jnl)->sit_j.entries[i].se)
279 #define segno_in_journal(jnl, i) ((jnl)->sit_j.entries[i].segno)
281 #define MAX_NAT_JENTRIES(jnl) (NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
282 #define MAX_SIT_JENTRIES(jnl) (SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
284 static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
286 int before = nats_in_cursum(journal);
288 journal->n_nats = cpu_to_le16(before + i);
292 static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
294 int before = sits_in_cursum(journal);
296 journal->n_sits = cpu_to_le16(before + i);
300 static inline bool __has_cursum_space(struct f2fs_journal *journal,
303 if (type == NAT_JOURNAL)
304 return size <= MAX_NAT_JENTRIES(journal);
305 return size <= MAX_SIT_JENTRIES(journal);
311 #define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
312 #define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
313 #define F2FS_IOC_GETVERSION FS_IOC_GETVERSION
315 #define F2FS_IOCTL_MAGIC 0xf5
316 #define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
317 #define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
318 #define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
319 #define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
320 #define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
321 #define F2FS_IOC_GARBAGE_COLLECT _IOW(F2FS_IOCTL_MAGIC, 6, __u32)
322 #define F2FS_IOC_WRITE_CHECKPOINT _IO(F2FS_IOCTL_MAGIC, 7)
323 #define F2FS_IOC_DEFRAGMENT _IOWR(F2FS_IOCTL_MAGIC, 8, \
324 struct f2fs_defragment)
325 #define F2FS_IOC_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
326 struct f2fs_move_range)
327 #define F2FS_IOC_FLUSH_DEVICE _IOW(F2FS_IOCTL_MAGIC, 10, \
328 struct f2fs_flush_device)
329 #define F2FS_IOC_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11, \
330 struct f2fs_gc_range)
331 #define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, __u32)
333 #define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
334 #define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
335 #define F2FS_IOC_GET_ENCRYPTION_PWSALT FS_IOC_GET_ENCRYPTION_PWSALT
338 * should be same as XFS_IOC_GOINGDOWN.
339 * Flags for going down operation used by FS_IOC_GOINGDOWN
341 #define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
342 #define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
343 #define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
344 #define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
345 #define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */
347 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
349 * ioctl commands in 32 bit emulation
351 #define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
352 #define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
353 #define F2FS_IOC32_GETVERSION FS_IOC32_GETVERSION
356 #define F2FS_IOC_FSGETXATTR FS_IOC_FSGETXATTR
357 #define F2FS_IOC_FSSETXATTR FS_IOC_FSSETXATTR
359 struct f2fs_gc_range {
365 struct f2fs_defragment {
370 struct f2fs_move_range {
371 u32 dst_fd; /* destination fd */
372 u64 pos_in; /* start position in src_fd */
373 u64 pos_out; /* start position in dst_fd */
374 u64 len; /* size to move */
377 struct f2fs_flush_device {
378 u32 dev_num; /* device number to flush */
379 u32 segments; /* # of segments to flush */
382 /* for inline stuff */
383 #define DEF_INLINE_RESERVED_SIZE 1
384 static inline int get_extra_isize(struct inode *inode);
385 #define MAX_INLINE_DATA(inode) (sizeof(__le32) * \
386 (CUR_ADDRS_PER_INODE(inode) - \
387 DEF_INLINE_RESERVED_SIZE - \
388 F2FS_INLINE_XATTR_ADDRS))
391 #define NR_INLINE_DENTRY(inode) (MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
392 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
394 #define INLINE_DENTRY_BITMAP_SIZE(inode) ((NR_INLINE_DENTRY(inode) + \
395 BITS_PER_BYTE - 1) / BITS_PER_BYTE)
396 #define INLINE_RESERVED_SIZE(inode) (MAX_INLINE_DATA(inode) - \
397 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
398 NR_INLINE_DENTRY(inode) + \
399 INLINE_DENTRY_BITMAP_SIZE(inode)))
402 * For INODE and NODE manager
404 /* for directory operations */
405 struct f2fs_dentry_ptr {
408 struct f2fs_dir_entry *dentry;
409 __u8 (*filename)[F2FS_SLOT_LEN];
414 static inline void make_dentry_ptr_block(struct inode *inode,
415 struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
418 d->max = NR_DENTRY_IN_BLOCK;
419 d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
420 d->bitmap = &t->dentry_bitmap;
421 d->dentry = t->dentry;
422 d->filename = t->filename;
425 static inline void make_dentry_ptr_inline(struct inode *inode,
426 struct f2fs_dentry_ptr *d, void *t)
428 int entry_cnt = NR_INLINE_DENTRY(inode);
429 int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
430 int reserved_size = INLINE_RESERVED_SIZE(inode);
434 d->nr_bitmap = bitmap_size;
436 d->dentry = t + bitmap_size + reserved_size;
437 d->filename = t + bitmap_size + reserved_size +
438 SIZE_OF_DIR_ENTRY * entry_cnt;
442 * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
443 * as its node offset to distinguish from index node blocks.
444 * But some bits are used to mark the node block.
446 #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
449 ALLOC_NODE, /* allocate a new node page if needed */
450 LOOKUP_NODE, /* look up a node without readahead */
452 * look up a node with readahead called
457 #define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */
459 #define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
461 /* vector size for gang look-up from extent cache that consists of radix tree */
462 #define EXT_TREE_VEC_SIZE 64
464 /* for in-memory extent cache entry */
465 #define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */
467 /* number of extent info in extent cache we try to shrink */
468 #define EXTENT_CACHE_SHRINK_NUMBER 128
471 struct rb_node rb_node; /* rb node located in rb-tree */
472 unsigned int ofs; /* start offset of the entry */
473 unsigned int len; /* length of the entry */
477 unsigned int fofs; /* start offset in a file */
478 unsigned int len; /* length of the extent */
479 u32 blk; /* start block address of the extent */
483 struct rb_node rb_node;
490 struct extent_info ei; /* extent info */
493 struct list_head list; /* node in global extent list of sbi */
494 struct extent_tree *et; /* extent tree pointer */
498 nid_t ino; /* inode number */
499 struct rb_root root; /* root of extent info rb-tree */
500 struct extent_node *cached_en; /* recently accessed extent node */
501 struct extent_info largest; /* largested extent info */
502 struct list_head list; /* to be used by sbi->zombie_list */
503 rwlock_t lock; /* protect extent info rb-tree */
504 atomic_t node_cnt; /* # of extent node in rb-tree*/
508 * This structure is taken from ext4_map_blocks.
510 * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
512 #define F2FS_MAP_NEW (1 << BH_New)
513 #define F2FS_MAP_MAPPED (1 << BH_Mapped)
514 #define F2FS_MAP_UNWRITTEN (1 << BH_Unwritten)
515 #define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
518 struct f2fs_map_blocks {
522 unsigned int m_flags;
523 pgoff_t *m_next_pgofs; /* point next possible non-hole pgofs */
526 /* for flag in get_data_block */
528 F2FS_GET_BLOCK_DEFAULT,
529 F2FS_GET_BLOCK_FIEMAP,
531 F2FS_GET_BLOCK_PRE_DIO,
532 F2FS_GET_BLOCK_PRE_AIO,
536 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
538 #define FADVISE_COLD_BIT 0x01
539 #define FADVISE_LOST_PINO_BIT 0x02
540 #define FADVISE_ENCRYPT_BIT 0x04
541 #define FADVISE_ENC_NAME_BIT 0x08
542 #define FADVISE_KEEP_SIZE_BIT 0x10
544 #define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
545 #define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
546 #define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
547 #define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
548 #define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
549 #define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
550 #define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT)
551 #define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
552 #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
553 #define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT)
554 #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
555 #define file_keep_isize(inode) is_file(inode, FADVISE_KEEP_SIZE_BIT)
556 #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
558 #define DEF_DIR_LEVEL 0
560 struct f2fs_inode_info {
561 struct inode vfs_inode; /* serve a vfs inode */
562 unsigned long i_flags; /* keep an inode flags for ioctl */
563 unsigned char i_advise; /* use to give file attribute hints */
564 unsigned char i_dir_level; /* use for dentry level for large dir */
565 unsigned int i_current_depth; /* use only in directory structure */
566 unsigned int i_pino; /* parent inode number */
567 umode_t i_acl_mode; /* keep file acl mode temporarily */
569 /* Use below internally in f2fs*/
570 unsigned long flags; /* use to pass per-file flags */
571 struct rw_semaphore i_sem; /* protect fi info */
572 atomic_t dirty_pages; /* # of dirty pages */
573 f2fs_hash_t chash; /* hash value of given file name */
574 unsigned int clevel; /* maximum level of given file name */
575 struct task_struct *task; /* lookup and create consistency */
576 struct task_struct *cp_task; /* separate cp/wb IO stats*/
577 nid_t i_xattr_nid; /* node id that contains xattrs */
578 loff_t last_disk_size; /* lastly written file size */
581 struct dquot *i_dquot[MAXQUOTAS];
583 /* quota space reservation, managed internally by quota code */
584 qsize_t i_reserved_quota;
586 struct list_head dirty_list; /* dirty list for dirs and files */
587 struct list_head gdirty_list; /* linked in global dirty list */
588 struct list_head inmem_pages; /* inmemory pages managed by f2fs */
589 struct task_struct *inmem_task; /* store inmemory task */
590 struct mutex inmem_lock; /* lock for inmemory pages */
591 struct extent_tree *extent_tree; /* cached extent_tree entry */
592 struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
593 struct rw_semaphore i_mmap_sem;
594 struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
596 int i_extra_isize; /* size of extra space located in i_addr */
597 kprojid_t i_projid; /* id for project quota */
600 static inline void get_extent_info(struct extent_info *ext,
601 struct f2fs_extent *i_ext)
603 ext->fofs = le32_to_cpu(i_ext->fofs);
604 ext->blk = le32_to_cpu(i_ext->blk);
605 ext->len = le32_to_cpu(i_ext->len);
608 static inline void set_raw_extent(struct extent_info *ext,
609 struct f2fs_extent *i_ext)
611 i_ext->fofs = cpu_to_le32(ext->fofs);
612 i_ext->blk = cpu_to_le32(ext->blk);
613 i_ext->len = cpu_to_le32(ext->len);
616 static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
617 u32 blk, unsigned int len)
624 static inline bool __is_discard_mergeable(struct discard_info *back,
625 struct discard_info *front)
627 return back->lstart + back->len == front->lstart;
630 static inline bool __is_discard_back_mergeable(struct discard_info *cur,
631 struct discard_info *back)
633 return __is_discard_mergeable(back, cur);
636 static inline bool __is_discard_front_mergeable(struct discard_info *cur,
637 struct discard_info *front)
639 return __is_discard_mergeable(cur, front);
642 static inline bool __is_extent_mergeable(struct extent_info *back,
643 struct extent_info *front)
645 return (back->fofs + back->len == front->fofs &&
646 back->blk + back->len == front->blk);
649 static inline bool __is_back_mergeable(struct extent_info *cur,
650 struct extent_info *back)
652 return __is_extent_mergeable(back, cur);
655 static inline bool __is_front_mergeable(struct extent_info *cur,
656 struct extent_info *front)
658 return __is_extent_mergeable(cur, front);
661 extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
662 static inline void __try_update_largest_extent(struct inode *inode,
663 struct extent_tree *et, struct extent_node *en)
665 if (en->ei.len > et->largest.len) {
666 et->largest = en->ei;
667 f2fs_mark_inode_dirty_sync(inode, true);
677 struct f2fs_nm_info {
678 block_t nat_blkaddr; /* base disk address of NAT */
679 nid_t max_nid; /* maximum possible node ids */
680 nid_t available_nids; /* # of available node ids */
681 nid_t next_scan_nid; /* the next nid to be scanned */
682 unsigned int ram_thresh; /* control the memory footprint */
683 unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */
684 unsigned int dirty_nats_ratio; /* control dirty nats ratio threshold */
686 /* NAT cache management */
687 struct radix_tree_root nat_root;/* root of the nat entry cache */
688 struct radix_tree_root nat_set_root;/* root of the nat set cache */
689 struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */
690 struct list_head nat_entries; /* cached nat entry list (clean) */
691 unsigned int nat_cnt; /* the # of cached nat entries */
692 unsigned int dirty_nat_cnt; /* total num of nat entries in set */
693 unsigned int nat_blocks; /* # of nat blocks */
695 /* free node ids management */
696 struct radix_tree_root free_nid_root;/* root of the free_nid cache */
697 struct list_head nid_list[MAX_NID_LIST];/* lists for free nids */
698 unsigned int nid_cnt[MAX_NID_LIST]; /* the number of free node id */
699 spinlock_t nid_list_lock; /* protect nid lists ops */
700 struct mutex build_lock; /* lock for build free nids */
701 unsigned char (*free_nid_bitmap)[NAT_ENTRY_BITMAP_SIZE];
702 unsigned char *nat_block_bitmap;
703 unsigned short *free_nid_count; /* free nid count of NAT block */
706 char *nat_bitmap; /* NAT bitmap pointer */
708 unsigned int nat_bits_blocks; /* # of nat bits blocks */
709 unsigned char *nat_bits; /* NAT bits blocks */
710 unsigned char *full_nat_bits; /* full NAT pages */
711 unsigned char *empty_nat_bits; /* empty NAT pages */
712 #ifdef CONFIG_F2FS_CHECK_FS
713 char *nat_bitmap_mir; /* NAT bitmap mirror */
715 int bitmap_size; /* bitmap size */
719 * this structure is used as one of function parameters.
720 * all the information are dedicated to a given direct node block determined
721 * by the data offset in a file.
723 struct dnode_of_data {
724 struct inode *inode; /* vfs inode pointer */
725 struct page *inode_page; /* its inode page, NULL is possible */
726 struct page *node_page; /* cached direct node page */
727 nid_t nid; /* node id of the direct node block */
728 unsigned int ofs_in_node; /* data offset in the node page */
729 bool inode_page_locked; /* inode page is locked or not */
730 bool node_changed; /* is node block changed */
731 char cur_level; /* level of hole node page */
732 char max_level; /* level of current page located */
733 block_t data_blkaddr; /* block address of the node block */
736 static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
737 struct page *ipage, struct page *npage, nid_t nid)
739 memset(dn, 0, sizeof(*dn));
741 dn->inode_page = ipage;
742 dn->node_page = npage;
749 * By default, there are 6 active log areas across the whole main area.
750 * When considering hot and cold data separation to reduce cleaning overhead,
751 * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
753 * In the current design, you should not change the numbers intentionally.
754 * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
755 * logs individually according to the underlying devices. (default: 6)
756 * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
757 * data and 8 for node logs.
759 #define NR_CURSEG_DATA_TYPE (3)
760 #define NR_CURSEG_NODE_TYPE (3)
761 #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
764 CURSEG_HOT_DATA = 0, /* directory entry blocks */
765 CURSEG_WARM_DATA, /* data blocks */
766 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
767 CURSEG_HOT_NODE, /* direct node blocks of directory files */
768 CURSEG_WARM_NODE, /* direct node blocks of normal files */
769 CURSEG_COLD_NODE, /* indirect node blocks */
774 struct completion wait;
775 struct llist_node llnode;
779 struct flush_cmd_control {
780 struct task_struct *f2fs_issue_flush; /* flush thread */
781 wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
782 atomic_t issued_flush; /* # of issued flushes */
783 atomic_t issing_flush; /* # of issing flushes */
784 struct llist_head issue_list; /* list for command issue */
785 struct llist_node *dispatch_list; /* list for command dispatch */
788 struct f2fs_sm_info {
789 struct sit_info *sit_info; /* whole segment information */
790 struct free_segmap_info *free_info; /* free segment information */
791 struct dirty_seglist_info *dirty_info; /* dirty segment information */
792 struct curseg_info *curseg_array; /* active segment information */
794 block_t seg0_blkaddr; /* block address of 0'th segment */
795 block_t main_blkaddr; /* start block address of main area */
796 block_t ssa_blkaddr; /* start block address of SSA area */
798 unsigned int segment_count; /* total # of segments */
799 unsigned int main_segments; /* # of segments in main area */
800 unsigned int reserved_segments; /* # of reserved segments */
801 unsigned int ovp_segments; /* # of overprovision segments */
803 /* a threshold to reclaim prefree segments */
804 unsigned int rec_prefree_segments;
806 /* for batched trimming */
807 unsigned int trim_sections; /* # of sections to trim */
809 struct list_head sit_entry_set; /* sit entry set list */
811 unsigned int ipu_policy; /* in-place-update policy */
812 unsigned int min_ipu_util; /* in-place-update threshold */
813 unsigned int min_fsync_blocks; /* threshold for fsync */
814 unsigned int min_hot_blocks; /* threshold for hot block allocation */
816 /* for flush command control */
817 struct flush_cmd_control *fcc_info;
819 /* for discard command control */
820 struct discard_cmd_control *dcc_info;
827 * COUNT_TYPE for monitoring
829 * f2fs monitors the number of several block types such as on-writeback,
830 * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
832 #define WB_DATA_TYPE(p) (__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
846 * The below are the page types of bios used in submit_bio().
847 * The available types are:
848 * DATA User data pages. It operates as async mode.
849 * NODE Node pages. It operates as async mode.
850 * META FS metadata pages such as SIT, NAT, CP.
851 * NR_PAGE_TYPE The number of page types.
852 * META_FLUSH Make sure the previous pages are written
853 * with waiting the bio's completion
854 * ... Only can be used with META.
856 #define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
863 INMEM, /* the below types are used by tracepoints only. */
872 HOT = 0, /* must be zero for meta bio */
878 enum need_lock_type {
885 APP_DIRECT_IO, /* app direct IOs */
886 APP_BUFFERED_IO, /* app buffered IOs */
887 APP_WRITE_IO, /* app write IOs */
888 APP_MAPPED_IO, /* app mapped IOs */
889 FS_DATA_IO, /* data IOs from kworker/fsync/reclaimer */
890 FS_NODE_IO, /* node IOs from kworker/fsync/reclaimer */
891 FS_META_IO, /* meta IOs from kworker/reclaimer */
892 FS_GC_DATA_IO, /* data IOs from forground gc */
893 FS_GC_NODE_IO, /* node IOs from forground gc */
894 FS_CP_DATA_IO, /* data IOs from checkpoint */
895 FS_CP_NODE_IO, /* node IOs from checkpoint */
896 FS_CP_META_IO, /* meta IOs from checkpoint */
897 FS_DISCARD, /* discard */
901 struct f2fs_io_info {
902 struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
903 enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
904 enum temp_type temp; /* contains HOT/WARM/COLD */
905 int op; /* contains REQ_OP_ */
906 int op_flags; /* req_flag_bits */
907 block_t new_blkaddr; /* new block address to be written */
908 block_t old_blkaddr; /* old block address before Cow */
909 struct page *page; /* page to be written */
910 struct page *encrypted_page; /* encrypted page */
911 struct list_head list; /* serialize IOs */
912 bool submitted; /* indicate IO submission */
913 int need_lock; /* indicate we need to lock cp_rwsem */
914 bool in_list; /* indicate fio is in io_list */
915 bool is_meta; /* indicate borrow meta inode mapping or not */
916 enum iostat_type io_type; /* io type */
919 #define is_read_io(rw) ((rw) == READ)
920 struct f2fs_bio_info {
921 struct f2fs_sb_info *sbi; /* f2fs superblock */
922 struct bio *bio; /* bios to merge */
923 sector_t last_block_in_bio; /* last block number */
924 struct f2fs_io_info fio; /* store buffered io info. */
925 struct rw_semaphore io_rwsem; /* blocking op for bio */
926 spinlock_t io_lock; /* serialize DATA/NODE IOs */
927 struct list_head io_list; /* track fios */
930 #define FDEV(i) (sbi->devs[i])
931 #define RDEV(i) (raw_super->devs[i])
932 struct f2fs_dev_info {
933 struct block_device *bdev;
934 char path[MAX_PATH_LEN];
935 unsigned int total_segments;
938 #ifdef CONFIG_BLK_DEV_ZONED
939 unsigned int nr_blkz; /* Total number of zones */
940 u8 *blkz_type; /* Array of zones type */
945 DIR_INODE, /* for dirty dir inode */
946 FILE_INODE, /* for dirty regular/symlink inode */
947 DIRTY_META, /* for all dirtied inode metadata */
951 /* for inner inode cache management */
952 struct inode_management {
953 struct radix_tree_root ino_root; /* ino entry array */
954 spinlock_t ino_lock; /* for ino entry lock */
955 struct list_head ino_list; /* inode list head */
956 unsigned long ino_num; /* number of entries */
959 /* For s_flag in struct f2fs_sb_info */
961 SBI_IS_DIRTY, /* dirty flag for checkpoint */
962 SBI_IS_CLOSE, /* specify unmounting */
963 SBI_NEED_FSCK, /* need fsck.f2fs to fix */
964 SBI_POR_DOING, /* recovery is doing or not */
965 SBI_NEED_SB_WRITE, /* need to recover superblock */
966 SBI_NEED_CP, /* need to checkpoint */
975 struct f2fs_sb_info {
976 struct super_block *sb; /* pointer to VFS super block */
977 struct proc_dir_entry *s_proc; /* proc entry */
978 struct f2fs_super_block *raw_super; /* raw super block pointer */
979 int valid_super_block; /* valid super block no */
980 unsigned long s_flag; /* flags for sbi */
982 #ifdef CONFIG_BLK_DEV_ZONED
983 unsigned int blocks_per_blkz; /* F2FS blocks per zone */
984 unsigned int log_blocks_per_blkz; /* log2 F2FS blocks per zone */
987 /* for node-related operations */
988 struct f2fs_nm_info *nm_info; /* node manager */
989 struct inode *node_inode; /* cache node blocks */
991 /* for segment-related operations */
992 struct f2fs_sm_info *sm_info; /* segment manager */
994 /* for bio operations */
995 struct f2fs_bio_info *write_io[NR_PAGE_TYPE]; /* for write bios */
996 struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
997 /* bio ordering for NODE/DATA */
998 int write_io_size_bits; /* Write IO size bits */
999 mempool_t *write_io_dummy; /* Dummy pages */
1001 /* for checkpoint */
1002 struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
1003 int cur_cp_pack; /* remain current cp pack */
1004 spinlock_t cp_lock; /* for flag in ckpt */
1005 struct inode *meta_inode; /* cache meta blocks */
1006 struct mutex cp_mutex; /* checkpoint procedure lock */
1007 struct rw_semaphore cp_rwsem; /* blocking FS operations */
1008 struct rw_semaphore node_write; /* locking node writes */
1009 struct rw_semaphore node_change; /* locking node change */
1010 wait_queue_head_t cp_wait;
1011 unsigned long last_time[MAX_TIME]; /* to store time in jiffies */
1012 long interval_time[MAX_TIME]; /* to store thresholds */
1014 struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
1016 /* for orphan inode, use 0'th array */
1017 unsigned int max_orphans; /* max orphan inodes */
1019 /* for inode management */
1020 struct list_head inode_list[NR_INODE_TYPE]; /* dirty inode list */
1021 spinlock_t inode_lock[NR_INODE_TYPE]; /* for dirty inode list lock */
1023 /* for extent tree cache */
1024 struct radix_tree_root extent_tree_root;/* cache extent cache entries */
1025 struct mutex extent_tree_lock; /* locking extent radix tree */
1026 struct list_head extent_list; /* lru list for shrinker */
1027 spinlock_t extent_lock; /* locking extent lru list */
1028 atomic_t total_ext_tree; /* extent tree count */
1029 struct list_head zombie_list; /* extent zombie tree list */
1030 atomic_t total_zombie_tree; /* extent zombie tree count */
1031 atomic_t total_ext_node; /* extent info count */
1033 /* basic filesystem units */
1034 unsigned int log_sectors_per_block; /* log2 sectors per block */
1035 unsigned int log_blocksize; /* log2 block size */
1036 unsigned int blocksize; /* block size */
1037 unsigned int root_ino_num; /* root inode number*/
1038 unsigned int node_ino_num; /* node inode number*/
1039 unsigned int meta_ino_num; /* meta inode number*/
1040 unsigned int log_blocks_per_seg; /* log2 blocks per segment */
1041 unsigned int blocks_per_seg; /* blocks per segment */
1042 unsigned int segs_per_sec; /* segments per section */
1043 unsigned int secs_per_zone; /* sections per zone */
1044 unsigned int total_sections; /* total section count */
1045 unsigned int total_node_count; /* total node block count */
1046 unsigned int total_valid_node_count; /* valid node block count */
1047 loff_t max_file_blocks; /* max block index of file */
1048 int active_logs; /* # of active logs */
1049 int dir_level; /* directory level */
1051 block_t user_block_count; /* # of user blocks */
1052 block_t total_valid_block_count; /* # of valid blocks */
1053 block_t discard_blks; /* discard command candidats */
1054 block_t last_valid_block_count; /* for recovery */
1055 block_t reserved_blocks; /* configurable reserved blocks */
1057 u32 s_next_generation; /* for NFS support */
1059 /* # of pages, see count_type */
1060 atomic_t nr_pages[NR_COUNT_TYPE];
1061 /* # of allocated blocks */
1062 struct percpu_counter alloc_valid_block_count;
1064 /* writeback control */
1065 atomic_t wb_sync_req; /* count # of WB_SYNC threads */
1067 /* valid inode count */
1068 struct percpu_counter total_valid_inode_count;
1070 struct f2fs_mount_info mount_opt; /* mount options */
1072 /* for cleaning operations */
1073 struct mutex gc_mutex; /* mutex for GC */
1074 struct f2fs_gc_kthread *gc_thread; /* GC thread */
1075 unsigned int cur_victim_sec; /* current victim section num */
1077 /* threshold for converting bg victims for fg */
1080 /* maximum # of trials to find a victim segment for SSR and GC */
1081 unsigned int max_victim_search;
1084 * for stat information.
1085 * one is for the LFS mode, and the other is for the SSR mode.
1087 #ifdef CONFIG_F2FS_STAT_FS
1088 struct f2fs_stat_info *stat_info; /* FS status information */
1089 unsigned int segment_count[2]; /* # of allocated segments */
1090 unsigned int block_count[2]; /* # of allocated blocks */
1091 atomic_t inplace_count; /* # of inplace update */
1092 atomic64_t total_hit_ext; /* # of lookup extent cache */
1093 atomic64_t read_hit_rbtree; /* # of hit rbtree extent node */
1094 atomic64_t read_hit_largest; /* # of hit largest extent node */
1095 atomic64_t read_hit_cached; /* # of hit cached extent node */
1096 atomic_t inline_xattr; /* # of inline_xattr inodes */
1097 atomic_t inline_inode; /* # of inline_data inodes */
1098 atomic_t inline_dir; /* # of inline_dentry inodes */
1099 atomic_t aw_cnt; /* # of atomic writes */
1100 atomic_t vw_cnt; /* # of volatile writes */
1101 atomic_t max_aw_cnt; /* max # of atomic writes */
1102 atomic_t max_vw_cnt; /* max # of volatile writes */
1103 int bg_gc; /* background gc calls */
1104 unsigned int ndirty_inode[NR_INODE_TYPE]; /* # of dirty inodes */
1106 spinlock_t stat_lock; /* lock for stat operations */
1108 /* For app/fs IO statistics */
1109 spinlock_t iostat_lock;
1110 unsigned long long write_iostat[NR_IO_TYPE];
1113 /* For sysfs suppport */
1114 struct kobject s_kobj;
1115 struct completion s_kobj_unregister;
1117 /* For shrinker support */
1118 struct list_head s_list;
1119 int s_ndevs; /* number of devices */
1120 struct f2fs_dev_info *devs; /* for device list */
1121 struct mutex umount_mutex;
1122 unsigned int shrinker_run_no;
1124 /* For write statistics */
1125 u64 sectors_written_start;
1128 /* Reference to checksum algorithm driver via cryptoapi */
1129 struct crypto_shash *s_chksum_driver;
1131 /* Precomputed FS UUID checksum for seeding other checksums */
1132 __u32 s_chksum_seed;
1134 /* For fault injection */
1135 #ifdef CONFIG_F2FS_FAULT_INJECTION
1136 struct f2fs_fault_info fault_info;
1140 /* Names of quota files with journalled quota */
1141 char *s_qf_names[MAXQUOTAS];
1142 int s_jquota_fmt; /* Format of quota to use */
1146 #ifdef CONFIG_F2FS_FAULT_INJECTION
1147 #define f2fs_show_injection_info(type) \
1148 printk("%sF2FS-fs : inject %s in %s of %pF\n", \
1149 KERN_INFO, fault_name[type], \
1150 __func__, __builtin_return_address(0))
1151 static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
1153 struct f2fs_fault_info *ffi = &sbi->fault_info;
1155 if (!ffi->inject_rate)
1158 if (!IS_FAULT_SET(ffi, type))
1161 atomic_inc(&ffi->inject_ops);
1162 if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
1163 atomic_set(&ffi->inject_ops, 0);
1171 * Test if the mounted volume is a multi-device volume.
1172 * - For a single regular disk volume, sbi->s_ndevs is 0.
1173 * - For a single zoned disk volume, sbi->s_ndevs is 1.
1174 * - For a multi-device volume, sbi->s_ndevs is always 2 or more.
1176 static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
1178 return sbi->s_ndevs > 1;
1181 /* For write statistics. Suppose sector size is 512 bytes,
1182 * and the return value is in kbytes. s is of struct f2fs_sb_info.
1184 #define BD_PART_WRITTEN(s) \
1185 (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) - \
1186 (s)->sectors_written_start) >> 1)
1188 static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
1190 sbi->last_time[type] = jiffies;
1193 static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
1195 struct timespec ts = {sbi->interval_time[type], 0};
1196 unsigned long interval = timespec_to_jiffies(&ts);
1198 return time_after(jiffies, sbi->last_time[type] + interval);
1201 static inline bool is_idle(struct f2fs_sb_info *sbi)
1203 struct block_device *bdev = sbi->sb->s_bdev;
1204 struct request_queue *q = bdev_get_queue(bdev);
1205 struct request_list *rl = &q->root_rl;
1207 if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1210 return f2fs_time_over(sbi, REQ_TIME);
1216 static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1217 unsigned int length)
1219 SHASH_DESC_ON_STACK(shash, sbi->s_chksum_driver);
1220 u32 *ctx = (u32 *)shash_desc_ctx(shash);
1224 shash->tfm = sbi->s_chksum_driver;
1226 *ctx = F2FS_SUPER_MAGIC;
1228 err = crypto_shash_update(shash, address, length);
1236 static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1237 void *buf, size_t buf_size)
1239 return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1242 static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1243 const void *address, unsigned int length)
1246 struct shash_desc shash;
1251 BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1253 desc.shash.tfm = sbi->s_chksum_driver;
1254 desc.shash.flags = 0;
1255 *(u32 *)desc.ctx = crc;
1257 err = crypto_shash_update(&desc.shash, address, length);
1260 return *(u32 *)desc.ctx;
1263 static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
1265 return container_of(inode, struct f2fs_inode_info, vfs_inode);
1268 static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
1270 return sb->s_fs_info;
1273 static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
1275 return F2FS_SB(inode->i_sb);
1278 static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
1280 return F2FS_I_SB(mapping->host);
1283 static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
1285 return F2FS_M_SB(page->mapping);
1288 static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
1290 return (struct f2fs_super_block *)(sbi->raw_super);
1293 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
1295 return (struct f2fs_checkpoint *)(sbi->ckpt);
1298 static inline struct f2fs_node *F2FS_NODE(struct page *page)
1300 return (struct f2fs_node *)page_address(page);
1303 static inline struct f2fs_inode *F2FS_INODE(struct page *page)
1305 return &((struct f2fs_node *)page_address(page))->i;
1308 static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
1310 return (struct f2fs_nm_info *)(sbi->nm_info);
1313 static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
1315 return (struct f2fs_sm_info *)(sbi->sm_info);
1318 static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
1320 return (struct sit_info *)(SM_I(sbi)->sit_info);
1323 static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
1325 return (struct free_segmap_info *)(SM_I(sbi)->free_info);
1328 static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
1330 return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
1333 static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
1335 return sbi->meta_inode->i_mapping;
1338 static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
1340 return sbi->node_inode->i_mapping;
1343 static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
1345 return test_bit(type, &sbi->s_flag);
1348 static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1350 set_bit(type, &sbi->s_flag);
1353 static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1355 clear_bit(type, &sbi->s_flag);
1358 static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1360 return le64_to_cpu(cp->checkpoint_ver);
1363 static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1365 size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1366 return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1369 static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1371 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1373 return ckpt_flags & f;
1376 static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1378 return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1381 static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1383 unsigned int ckpt_flags;
1385 ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1387 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1390 static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1392 unsigned long flags;
1394 spin_lock_irqsave(&sbi->cp_lock, flags);
1395 __set_ckpt_flags(F2FS_CKPT(sbi), f);
1396 spin_unlock_irqrestore(&sbi->cp_lock, flags);
1399 static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1401 unsigned int ckpt_flags;
1403 ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1405 cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1408 static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1410 unsigned long flags;
1412 spin_lock_irqsave(&sbi->cp_lock, flags);
1413 __clear_ckpt_flags(F2FS_CKPT(sbi), f);
1414 spin_unlock_irqrestore(&sbi->cp_lock, flags);
1417 static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
1419 unsigned long flags;
1421 set_sbi_flag(sbi, SBI_NEED_FSCK);
1424 spin_lock_irqsave(&sbi->cp_lock, flags);
1425 __clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
1426 kfree(NM_I(sbi)->nat_bits);
1427 NM_I(sbi)->nat_bits = NULL;
1429 spin_unlock_irqrestore(&sbi->cp_lock, flags);
1432 static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
1433 struct cp_control *cpc)
1435 bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
1437 return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
1440 static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
1442 down_read(&sbi->cp_rwsem);
1445 static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1447 return down_read_trylock(&sbi->cp_rwsem);
1450 static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
1452 up_read(&sbi->cp_rwsem);
1455 static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
1457 down_write(&sbi->cp_rwsem);
1460 static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
1462 up_write(&sbi->cp_rwsem);
1465 static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1467 int reason = CP_SYNC;
1469 if (test_opt(sbi, FASTBOOT))
1470 reason = CP_FASTBOOT;
1471 if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1476 static inline bool __remain_node_summaries(int reason)
1478 return (reason & (CP_UMOUNT | CP_FASTBOOT));
1481 static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1483 return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1484 is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1488 * Check whether the inode has blocks or not
1490 static inline int F2FS_HAS_BLOCKS(struct inode *inode)
1492 block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
1494 return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
1497 static inline bool f2fs_has_xattr_block(unsigned int ofs)
1499 return ofs == XATTR_NODE_OFFSET;
1502 static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
1503 static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
1504 struct inode *inode, blkcnt_t *count)
1506 blkcnt_t diff = 0, release = 0;
1507 block_t avail_user_block_count;
1510 ret = dquot_reserve_block(inode, *count);
1514 #ifdef CONFIG_F2FS_FAULT_INJECTION
1515 if (time_to_inject(sbi, FAULT_BLOCK)) {
1516 f2fs_show_injection_info(FAULT_BLOCK);
1522 * let's increase this in prior to actual block count change in order
1523 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1525 percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1527 spin_lock(&sbi->stat_lock);
1528 sbi->total_valid_block_count += (block_t)(*count);
1529 avail_user_block_count = sbi->user_block_count - sbi->reserved_blocks;
1530 if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1531 diff = sbi->total_valid_block_count - avail_user_block_count;
1534 sbi->total_valid_block_count = avail_user_block_count;
1536 spin_unlock(&sbi->stat_lock);
1537 percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
1541 spin_unlock(&sbi->stat_lock);
1544 dquot_release_reservation_block(inode, release);
1545 f2fs_i_blocks_write(inode, *count, true, true);
1549 dquot_release_reservation_block(inode, release);
1553 void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
1554 static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
1555 struct inode *inode,
1558 blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
1560 spin_lock(&sbi->stat_lock);
1561 f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
1562 sbi->total_valid_block_count -= (block_t)count;
1563 spin_unlock(&sbi->stat_lock);
1564 if (unlikely(inode->i_blocks < sectors)) {
1565 f2fs_msg(sbi->sb, KERN_WARNING,
1566 "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
1568 (unsigned long long)inode->i_blocks,
1569 (unsigned long long)sectors);
1570 set_sbi_flag(sbi, SBI_NEED_FSCK);
1573 f2fs_i_blocks_write(inode, count, false, true);
1576 static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
1578 atomic_inc(&sbi->nr_pages[count_type]);
1580 if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
1581 count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
1584 set_sbi_flag(sbi, SBI_IS_DIRTY);
1587 static inline void inode_inc_dirty_pages(struct inode *inode)
1589 atomic_inc(&F2FS_I(inode)->dirty_pages);
1590 inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1591 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
1594 static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
1596 atomic_dec(&sbi->nr_pages[count_type]);
1599 static inline void inode_dec_dirty_pages(struct inode *inode)
1601 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1602 !S_ISLNK(inode->i_mode))
1605 atomic_dec(&F2FS_I(inode)->dirty_pages);
1606 dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1607 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
1610 static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
1612 return atomic_read(&sbi->nr_pages[count_type]);
1615 static inline int get_dirty_pages(struct inode *inode)
1617 return atomic_read(&F2FS_I(inode)->dirty_pages);
1620 static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
1622 unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1623 unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1624 sbi->log_blocks_per_seg;
1626 return segs / sbi->segs_per_sec;
1629 static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
1631 return sbi->total_valid_block_count;
1634 static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1636 return sbi->discard_blks;
1639 static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
1641 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1643 /* return NAT or SIT bitmap */
1644 if (flag == NAT_BITMAP)
1645 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
1646 else if (flag == SIT_BITMAP)
1647 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
1652 static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
1654 return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
1657 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
1659 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1662 if (__cp_payload(sbi) > 0) {
1663 if (flag == NAT_BITMAP)
1664 return &ckpt->sit_nat_version_bitmap;
1666 return (unsigned char *)ckpt + F2FS_BLKSIZE;
1668 offset = (flag == NAT_BITMAP) ?
1669 le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
1670 return &ckpt->sit_nat_version_bitmap + offset;
1674 static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
1676 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
1678 if (sbi->cur_cp_pack == 2)
1679 start_addr += sbi->blocks_per_seg;
1683 static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
1685 block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
1687 if (sbi->cur_cp_pack == 1)
1688 start_addr += sbi->blocks_per_seg;
1692 static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
1694 sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
1697 static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
1699 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
1702 static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1703 struct inode *inode, bool is_inode)
1705 block_t valid_block_count;
1706 unsigned int valid_node_count;
1707 bool quota = inode && !is_inode;
1710 int ret = dquot_reserve_block(inode, 1);
1715 spin_lock(&sbi->stat_lock);
1717 valid_block_count = sbi->total_valid_block_count + 1;
1718 if (unlikely(valid_block_count + sbi->reserved_blocks >
1719 sbi->user_block_count)) {
1720 spin_unlock(&sbi->stat_lock);
1724 valid_node_count = sbi->total_valid_node_count + 1;
1725 if (unlikely(valid_node_count > sbi->total_node_count)) {
1726 spin_unlock(&sbi->stat_lock);
1730 sbi->total_valid_node_count++;
1731 sbi->total_valid_block_count++;
1732 spin_unlock(&sbi->stat_lock);
1736 f2fs_mark_inode_dirty_sync(inode, true);
1738 f2fs_i_blocks_write(inode, 1, true, true);
1741 percpu_counter_inc(&sbi->alloc_valid_block_count);
1746 dquot_release_reservation_block(inode, 1);
1750 static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1751 struct inode *inode, bool is_inode)
1753 spin_lock(&sbi->stat_lock);
1755 f2fs_bug_on(sbi, !sbi->total_valid_block_count);
1756 f2fs_bug_on(sbi, !sbi->total_valid_node_count);
1757 f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
1759 sbi->total_valid_node_count--;
1760 sbi->total_valid_block_count--;
1762 spin_unlock(&sbi->stat_lock);
1765 f2fs_i_blocks_write(inode, 1, false, true);
1768 static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
1770 return sbi->total_valid_node_count;
1773 static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
1775 percpu_counter_inc(&sbi->total_valid_inode_count);
1778 static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
1780 percpu_counter_dec(&sbi->total_valid_inode_count);
1783 static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
1785 return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
1788 static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1789 pgoff_t index, bool for_write)
1791 #ifdef CONFIG_F2FS_FAULT_INJECTION
1795 page = find_get_page_flags(mapping, index,
1796 FGP_LOCK | FGP_ACCESSED);
1798 page = find_lock_page(mapping, index);
1802 if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
1803 f2fs_show_injection_info(FAULT_PAGE_ALLOC);
1808 return grab_cache_page(mapping, index);
1809 return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1812 static inline void f2fs_copy_page(struct page *src, struct page *dst)
1814 char *src_kaddr = kmap(src);
1815 char *dst_kaddr = kmap(dst);
1817 memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
1822 static inline void f2fs_put_page(struct page *page, int unlock)
1828 f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
1834 static inline void f2fs_put_dnode(struct dnode_of_data *dn)
1837 f2fs_put_page(dn->node_page, 1);
1838 if (dn->inode_page && dn->node_page != dn->inode_page)
1839 f2fs_put_page(dn->inode_page, 0);
1840 dn->node_page = NULL;
1841 dn->inode_page = NULL;
1844 static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
1847 return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
1850 static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
1855 entry = kmem_cache_alloc(cachep, flags);
1857 entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
1861 static inline struct bio *f2fs_bio_alloc(int npages)
1865 /* No failure on bio allocation */
1866 bio = bio_alloc(GFP_NOIO, npages);
1868 bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
1872 static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
1873 unsigned long index, void *item)
1875 while (radix_tree_insert(root, index, item))
1879 #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
1881 static inline bool IS_INODE(struct page *page)
1883 struct f2fs_node *p = F2FS_NODE(page);
1885 return RAW_IS_INODE(p);
1888 static inline int offset_in_addr(struct f2fs_inode *i)
1890 return (i->i_inline & F2FS_EXTRA_ATTR) ?
1891 (le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
1894 static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
1896 return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
1899 static inline int f2fs_has_extra_attr(struct inode *inode);
1900 static inline block_t datablock_addr(struct inode *inode,
1901 struct page *node_page, unsigned int offset)
1903 struct f2fs_node *raw_node;
1906 bool is_inode = IS_INODE(node_page);
1908 raw_node = F2FS_NODE(node_page);
1910 /* from GC path only */
1913 base = offset_in_addr(&raw_node->i);
1914 } else if (f2fs_has_extra_attr(inode) && is_inode) {
1915 base = get_extra_isize(inode);
1918 addr_array = blkaddr_in_node(raw_node);
1919 return le32_to_cpu(addr_array[base + offset]);
1922 static inline int f2fs_test_bit(unsigned int nr, char *addr)
1927 mask = 1 << (7 - (nr & 0x07));
1928 return mask & *addr;
1931 static inline void f2fs_set_bit(unsigned int nr, char *addr)
1936 mask = 1 << (7 - (nr & 0x07));
1940 static inline void f2fs_clear_bit(unsigned int nr, char *addr)
1945 mask = 1 << (7 - (nr & 0x07));
1949 static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
1955 mask = 1 << (7 - (nr & 0x07));
1961 static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
1967 mask = 1 << (7 - (nr & 0x07));
1973 static inline void f2fs_change_bit(unsigned int nr, char *addr)
1978 mask = 1 << (7 - (nr & 0x07));
1982 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1983 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1984 #define F2FS_FL_INHERITED (FS_PROJINHERIT_FL)
1986 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
1990 else if (S_ISREG(mode))
1991 return flags & F2FS_REG_FLMASK;
1993 return flags & F2FS_OTHER_FLMASK;
1996 /* used for f2fs_inode_info->flags */
1998 FI_NEW_INODE, /* indicate newly allocated inode */
1999 FI_DIRTY_INODE, /* indicate inode is dirty or not */
2000 FI_AUTO_RECOVER, /* indicate inode is recoverable */
2001 FI_DIRTY_DIR, /* indicate directory has dirty pages */
2002 FI_INC_LINK, /* need to increment i_nlink */
2003 FI_ACL_MODE, /* indicate acl mode */
2004 FI_NO_ALLOC, /* should not allocate any blocks */
2005 FI_FREE_NID, /* free allocated nide */
2006 FI_NO_EXTENT, /* not to use the extent cache */
2007 FI_INLINE_XATTR, /* used for inline xattr */
2008 FI_INLINE_DATA, /* used for inline data*/
2009 FI_INLINE_DENTRY, /* used for inline dentry */
2010 FI_APPEND_WRITE, /* inode has appended data */
2011 FI_UPDATE_WRITE, /* inode has in-place-update data */
2012 FI_NEED_IPU, /* used for ipu per file */
2013 FI_ATOMIC_FILE, /* indicate atomic file */
2014 FI_ATOMIC_COMMIT, /* indicate the state of atomical committing */
2015 FI_VOLATILE_FILE, /* indicate volatile file */
2016 FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */
2017 FI_DROP_CACHE, /* drop dirty page cache */
2018 FI_DATA_EXIST, /* indicate data exists */
2019 FI_INLINE_DOTS, /* indicate inline dot dentries */
2020 FI_DO_DEFRAG, /* indicate defragment is running */
2021 FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */
2022 FI_NO_PREALLOC, /* indicate skipped preallocated blocks */
2023 FI_HOT_DATA, /* indicate file is hot */
2024 FI_EXTRA_ATTR, /* indicate file has extra attribute */
2025 FI_PROJ_INHERIT, /* indicate file inherits projectid */
2028 static inline void __mark_inode_dirty_flag(struct inode *inode,
2032 case FI_INLINE_XATTR:
2033 case FI_INLINE_DATA:
2034 case FI_INLINE_DENTRY:
2038 case FI_INLINE_DOTS:
2039 f2fs_mark_inode_dirty_sync(inode, true);
2043 static inline void set_inode_flag(struct inode *inode, int flag)
2045 if (!test_bit(flag, &F2FS_I(inode)->flags))
2046 set_bit(flag, &F2FS_I(inode)->flags);
2047 __mark_inode_dirty_flag(inode, flag, true);
2050 static inline int is_inode_flag_set(struct inode *inode, int flag)
2052 return test_bit(flag, &F2FS_I(inode)->flags);
2055 static inline void clear_inode_flag(struct inode *inode, int flag)
2057 if (test_bit(flag, &F2FS_I(inode)->flags))
2058 clear_bit(flag, &F2FS_I(inode)->flags);
2059 __mark_inode_dirty_flag(inode, flag, false);
2062 static inline void set_acl_inode(struct inode *inode, umode_t mode)
2064 F2FS_I(inode)->i_acl_mode = mode;
2065 set_inode_flag(inode, FI_ACL_MODE);
2066 f2fs_mark_inode_dirty_sync(inode, false);
2069 static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2075 f2fs_mark_inode_dirty_sync(inode, true);
2078 static inline void f2fs_i_blocks_write(struct inode *inode,
2079 block_t diff, bool add, bool claim)
2081 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
2082 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
2084 /* add = 1, claim = 1 should be dquot_reserve_block in pair */
2087 dquot_claim_block(inode, diff);
2089 dquot_alloc_block_nofail(inode, diff);
2091 dquot_free_block(inode, diff);
2094 f2fs_mark_inode_dirty_sync(inode, true);
2095 if (clean || recover)
2096 set_inode_flag(inode, FI_AUTO_RECOVER);
2099 static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2101 bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
2102 bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
2104 if (i_size_read(inode) == i_size)
2107 i_size_write(inode, i_size);
2108 f2fs_mark_inode_dirty_sync(inode, true);
2109 if (clean || recover)
2110 set_inode_flag(inode, FI_AUTO_RECOVER);
2113 static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2115 F2FS_I(inode)->i_current_depth = depth;
2116 f2fs_mark_inode_dirty_sync(inode, true);
2119 static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2121 F2FS_I(inode)->i_xattr_nid = xnid;
2122 f2fs_mark_inode_dirty_sync(inode, true);
2125 static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2127 F2FS_I(inode)->i_pino = pino;
2128 f2fs_mark_inode_dirty_sync(inode, true);
2131 static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2133 struct f2fs_inode_info *fi = F2FS_I(inode);
2135 if (ri->i_inline & F2FS_INLINE_XATTR)
2136 set_bit(FI_INLINE_XATTR, &fi->flags);
2137 if (ri->i_inline & F2FS_INLINE_DATA)
2138 set_bit(FI_INLINE_DATA, &fi->flags);
2139 if (ri->i_inline & F2FS_INLINE_DENTRY)
2140 set_bit(FI_INLINE_DENTRY, &fi->flags);
2141 if (ri->i_inline & F2FS_DATA_EXIST)
2142 set_bit(FI_DATA_EXIST, &fi->flags);
2143 if (ri->i_inline & F2FS_INLINE_DOTS)
2144 set_bit(FI_INLINE_DOTS, &fi->flags);
2145 if (ri->i_inline & F2FS_EXTRA_ATTR)
2146 set_bit(FI_EXTRA_ATTR, &fi->flags);
2149 static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2153 if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2154 ri->i_inline |= F2FS_INLINE_XATTR;
2155 if (is_inode_flag_set(inode, FI_INLINE_DATA))
2156 ri->i_inline |= F2FS_INLINE_DATA;
2157 if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
2158 ri->i_inline |= F2FS_INLINE_DENTRY;
2159 if (is_inode_flag_set(inode, FI_DATA_EXIST))
2160 ri->i_inline |= F2FS_DATA_EXIST;
2161 if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2162 ri->i_inline |= F2FS_INLINE_DOTS;
2163 if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
2164 ri->i_inline |= F2FS_EXTRA_ATTR;
2167 static inline int f2fs_has_extra_attr(struct inode *inode)
2169 return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2172 static inline int f2fs_has_inline_xattr(struct inode *inode)
2174 return is_inode_flag_set(inode, FI_INLINE_XATTR);
2177 static inline unsigned int addrs_per_inode(struct inode *inode)
2179 if (f2fs_has_inline_xattr(inode))
2180 return CUR_ADDRS_PER_INODE(inode) - F2FS_INLINE_XATTR_ADDRS;
2181 return CUR_ADDRS_PER_INODE(inode);
2184 static inline void *inline_xattr_addr(struct page *page)
2186 struct f2fs_inode *ri = F2FS_INODE(page);
2188 return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2189 F2FS_INLINE_XATTR_ADDRS]);
2192 static inline int inline_xattr_size(struct inode *inode)
2194 if (f2fs_has_inline_xattr(inode))
2195 return F2FS_INLINE_XATTR_ADDRS << 2;
2200 static inline int f2fs_has_inline_data(struct inode *inode)
2202 return is_inode_flag_set(inode, FI_INLINE_DATA);
2205 static inline int f2fs_exist_data(struct inode *inode)
2207 return is_inode_flag_set(inode, FI_DATA_EXIST);
2210 static inline int f2fs_has_inline_dots(struct inode *inode)
2212 return is_inode_flag_set(inode, FI_INLINE_DOTS);
2215 static inline bool f2fs_is_atomic_file(struct inode *inode)
2217 return is_inode_flag_set(inode, FI_ATOMIC_FILE);
2220 static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
2222 return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
2225 static inline bool f2fs_is_volatile_file(struct inode *inode)
2227 return is_inode_flag_set(inode, FI_VOLATILE_FILE);
2230 static inline bool f2fs_is_first_block_written(struct inode *inode)
2232 return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
2235 static inline bool f2fs_is_drop_cache(struct inode *inode)
2237 return is_inode_flag_set(inode, FI_DROP_CACHE);
2240 static inline void *inline_data_addr(struct inode *inode, struct page *page)
2242 struct f2fs_inode *ri = F2FS_INODE(page);
2243 int extra_size = get_extra_isize(inode);
2245 return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
2248 static inline int f2fs_has_inline_dentry(struct inode *inode)
2250 return is_inode_flag_set(inode, FI_INLINE_DENTRY);
2253 static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
2255 if (!f2fs_has_inline_dentry(dir))
2259 static inline int is_file(struct inode *inode, int type)
2261 return F2FS_I(inode)->i_advise & type;
2264 static inline void set_file(struct inode *inode, int type)
2266 F2FS_I(inode)->i_advise |= type;
2267 f2fs_mark_inode_dirty_sync(inode, true);
2270 static inline void clear_file(struct inode *inode, int type)
2272 F2FS_I(inode)->i_advise &= ~type;
2273 f2fs_mark_inode_dirty_sync(inode, true);
2276 static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
2279 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2282 spin_lock(&sbi->inode_lock[DIRTY_META]);
2283 ret = list_empty(&F2FS_I(inode)->gdirty_list);
2284 spin_unlock(&sbi->inode_lock[DIRTY_META]);
2287 if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
2288 file_keep_isize(inode) ||
2289 i_size_read(inode) & PAGE_MASK)
2291 return F2FS_I(inode)->last_disk_size == i_size_read(inode);
2294 static inline int f2fs_readonly(struct super_block *sb)
2296 return sb->s_flags & MS_RDONLY;
2299 static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2301 return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2304 static inline bool is_dot_dotdot(const struct qstr *str)
2306 if (str->len == 1 && str->name[0] == '.')
2309 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2315 static inline bool f2fs_may_extent_tree(struct inode *inode)
2317 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2319 if (!test_opt(sbi, EXTENT_CACHE) ||
2320 is_inode_flag_set(inode, FI_NO_EXTENT))
2324 * for recovered files during mount do not create extents
2325 * if shrinker is not registered.
2327 if (list_empty(&sbi->s_list))
2330 return S_ISREG(inode->i_mode);
2333 static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
2334 size_t size, gfp_t flags)
2336 #ifdef CONFIG_F2FS_FAULT_INJECTION
2337 if (time_to_inject(sbi, FAULT_KMALLOC)) {
2338 f2fs_show_injection_info(FAULT_KMALLOC);
2342 return kmalloc(size, flags);
2345 static inline int get_extra_isize(struct inode *inode)
2347 return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2350 #define get_inode_mode(i) \
2351 ((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2352 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2354 #define F2FS_TOTAL_EXTRA_ATTR_SIZE \
2355 (offsetof(struct f2fs_inode, i_extra_end) - \
2356 offsetof(struct f2fs_inode, i_extra_isize)) \
2358 #define F2FS_OLD_ATTRIBUTE_SIZE (offsetof(struct f2fs_inode, i_addr))
2359 #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field) \
2360 ((offsetof(typeof(*f2fs_inode), field) + \
2361 sizeof((f2fs_inode)->field)) \
2362 <= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize)) \
2364 static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2368 spin_lock(&sbi->iostat_lock);
2369 for (i = 0; i < NR_IO_TYPE; i++)
2370 sbi->write_iostat[i] = 0;
2371 spin_unlock(&sbi->iostat_lock);
2374 static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2375 enum iostat_type type, unsigned long long io_bytes)
2377 if (!sbi->iostat_enable)
2379 spin_lock(&sbi->iostat_lock);
2380 sbi->write_iostat[type] += io_bytes;
2382 if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2383 sbi->write_iostat[APP_BUFFERED_IO] =
2384 sbi->write_iostat[APP_WRITE_IO] -
2385 sbi->write_iostat[APP_DIRECT_IO];
2386 spin_unlock(&sbi->iostat_lock);
2389 #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO(fio->type) == META && \
2390 (!is_read_io(fio->op) || fio->is_meta))
2392 bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
2393 block_t blkaddr, int type);
2394 static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
2395 block_t blkaddr, int type)
2397 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
2398 f2fs_msg(sbi->sb, KERN_ERR,
2399 "invalid blkaddr: %u, type: %d, run fsck to fix.",
2401 f2fs_bug_on(sbi, 1);
2405 static inline bool __is_valid_data_blkaddr(block_t blkaddr)
2407 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
2412 static inline bool is_valid_data_blkaddr(struct f2fs_sb_info *sbi,
2415 if (!__is_valid_data_blkaddr(blkaddr))
2417 verify_blkaddr(sbi, blkaddr, DATA_GENERIC);
2424 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2425 void truncate_data_blocks(struct dnode_of_data *dn);
2426 int truncate_blocks(struct inode *inode, u64 from, bool lock);
2427 int f2fs_truncate(struct inode *inode);
2428 int f2fs_getattr(const struct path *path, struct kstat *stat,
2429 u32 request_mask, unsigned int flags);
2430 int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2431 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2432 int truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2433 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2434 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
2439 void f2fs_set_inode_flags(struct inode *inode);
2440 bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2441 void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2442 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2443 struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2444 int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2445 int update_inode(struct inode *inode, struct page *node_page);
2446 int update_inode_page(struct inode *inode);
2447 int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2448 void f2fs_evict_inode(struct inode *inode);
2449 void handle_failed_inode(struct inode *inode);
2454 struct dentry *f2fs_get_parent(struct dentry *child);
2459 void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
2460 unsigned char get_de_type(struct f2fs_dir_entry *de);
2461 struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
2462 f2fs_hash_t namehash, int *max_slots,
2463 struct f2fs_dentry_ptr *d);
2464 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2465 unsigned int start_pos, struct fscrypt_str *fstr);
2466 void do_make_empty_dir(struct inode *inode, struct inode *parent,
2467 struct f2fs_dentry_ptr *d);
2468 struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
2469 const struct qstr *new_name,
2470 const struct qstr *orig_name, struct page *dpage);
2471 void update_parent_metadata(struct inode *dir, struct inode *inode,
2472 unsigned int current_depth);
2473 int room_for_filename(const void *bitmap, int slots, int max_slots);
2474 void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2475 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2476 struct fscrypt_name *fname, struct page **res_page);
2477 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2478 const struct qstr *child, struct page **res_page);
2479 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2480 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2481 struct page **page);
2482 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2483 struct page *page, struct inode *inode);
2484 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2485 const struct qstr *name, f2fs_hash_t name_hash,
2486 unsigned int bit_pos);
2487 int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2488 const struct qstr *orig_name,
2489 struct inode *inode, nid_t ino, umode_t mode);
2490 int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
2491 struct inode *inode, nid_t ino, umode_t mode);
2492 int __f2fs_add_link(struct inode *dir, const struct qstr *name,
2493 struct inode *inode, nid_t ino, umode_t mode);
2494 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2495 struct inode *dir, struct inode *inode);
2496 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2497 bool f2fs_empty_dir(struct inode *dir);
2499 static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2501 return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2502 inode, inode->i_ino, inode->i_mode);
2508 int f2fs_inode_dirtied(struct inode *inode, bool sync);
2509 void f2fs_inode_synced(struct inode *inode);
2510 void f2fs_enable_quota_files(struct f2fs_sb_info *sbi);
2511 void f2fs_quota_off_umount(struct super_block *sb);
2512 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2513 int f2fs_sync_fs(struct super_block *sb, int sync);
2514 extern __printf(3, 4)
2515 void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
2516 int sanity_check_ckpt(struct f2fs_sb_info *sbi);
2521 f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
2522 struct fscrypt_name *fname);
2527 struct dnode_of_data;
2530 int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
2531 bool available_free_memory(struct f2fs_sb_info *sbi, int type);
2532 int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
2533 bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
2534 bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
2535 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
2536 pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
2537 int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
2538 int truncate_inode_blocks(struct inode *inode, pgoff_t from);
2539 int truncate_xattr_node(struct inode *inode, struct page *page);
2540 int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
2541 int remove_inode_page(struct inode *inode);
2542 struct page *new_inode_page(struct inode *inode);
2543 struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs);
2544 void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
2545 struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
2546 struct page *get_node_page_ra(struct page *parent, int start);
2547 void move_node_page(struct page *node_page, int gc_type);
2548 int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2549 struct writeback_control *wbc, bool atomic);
2550 int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc,
2551 bool do_balance, enum iostat_type io_type);
2552 void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
2553 bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
2554 void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
2555 void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
2556 int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
2557 void recover_inline_xattr(struct inode *inode, struct page *page);
2558 int recover_xattr_data(struct inode *inode, struct page *page,
2560 int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
2561 int restore_node_summary(struct f2fs_sb_info *sbi,
2562 unsigned int segno, struct f2fs_summary_block *sum);
2563 void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2564 int build_node_manager(struct f2fs_sb_info *sbi);
2565 void destroy_node_manager(struct f2fs_sb_info *sbi);
2566 int __init create_node_manager_caches(void);
2567 void destroy_node_manager_caches(void);
2572 bool need_SSR(struct f2fs_sb_info *sbi);
2573 void register_inmem_page(struct inode *inode, struct page *page);
2574 void drop_inmem_pages(struct inode *inode);
2575 void drop_inmem_page(struct inode *inode, struct page *page);
2576 int commit_inmem_pages(struct inode *inode);
2577 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2578 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
2579 int f2fs_issue_flush(struct f2fs_sb_info *sbi);
2580 int create_flush_cmd_control(struct f2fs_sb_info *sbi);
2581 void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
2582 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
2583 bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
2584 void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new);
2585 void stop_discard_thread(struct f2fs_sb_info *sbi);
2586 void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi, bool umount);
2587 void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2588 void release_discard_addrs(struct f2fs_sb_info *sbi);
2589 int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
2590 void allocate_new_segments(struct f2fs_sb_info *sbi);
2591 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
2592 bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2593 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
2594 void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
2595 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2596 enum iostat_type io_type);
2597 void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
2598 void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
2599 int rewrite_data_page(struct f2fs_io_info *fio);
2600 void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2601 block_t old_blkaddr, block_t new_blkaddr,
2602 bool recover_curseg, bool recover_newaddr);
2603 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2604 block_t old_addr, block_t new_addr,
2605 unsigned char version, bool recover_curseg,
2606 bool recover_newaddr);
2607 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2608 block_t old_blkaddr, block_t *new_blkaddr,
2609 struct f2fs_summary *sum, int type,
2610 struct f2fs_io_info *fio, bool add_list);
2611 void f2fs_wait_on_page_writeback(struct page *page,
2612 enum page_type type, bool ordered);
2613 void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr);
2614 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2615 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
2616 int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2617 unsigned int val, int alloc);
2618 void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2619 int build_segment_manager(struct f2fs_sb_info *sbi);
2620 void destroy_segment_manager(struct f2fs_sb_info *sbi);
2621 int __init create_segment_manager_caches(void);
2622 void destroy_segment_manager_caches(void);
2627 void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
2628 struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2629 struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
2630 struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2631 bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
2632 block_t blkaddr, int type);
2633 int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2634 int type, bool sync);
2635 void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
2636 long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2637 long nr_to_write, enum iostat_type io_type);
2638 void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2639 void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
2640 void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
2641 bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
2642 int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
2643 int acquire_orphan_inode(struct f2fs_sb_info *sbi);
2644 void release_orphan_inode(struct f2fs_sb_info *sbi);
2645 void add_orphan_inode(struct inode *inode);
2646 void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
2647 int recover_orphan_inodes(struct f2fs_sb_info *sbi);
2648 int get_valid_checkpoint(struct f2fs_sb_info *sbi);
2649 void update_dirty_page(struct inode *inode, struct page *page);
2650 void remove_dirty_inode(struct inode *inode);
2651 int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
2652 int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
2653 void init_ino_entry_info(struct f2fs_sb_info *sbi);
2654 int __init create_checkpoint_caches(void);
2655 void destroy_checkpoint_caches(void);
2660 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2661 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
2662 struct inode *inode, nid_t ino, pgoff_t idx,
2663 enum page_type type);
2664 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
2665 int f2fs_submit_page_bio(struct f2fs_io_info *fio);
2666 int f2fs_submit_page_write(struct f2fs_io_info *fio);
2667 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2668 block_t blk_addr, struct bio *bio);
2669 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
2670 void set_data_blkaddr(struct dnode_of_data *dn);
2671 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
2672 int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
2673 int reserve_new_block(struct dnode_of_data *dn);
2674 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2675 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2676 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
2677 struct page *get_read_data_page(struct inode *inode, pgoff_t index,
2678 int op_flags, bool for_write);
2679 struct page *find_data_page(struct inode *inode, pgoff_t index);
2680 struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
2682 struct page *get_new_data_page(struct inode *inode,
2683 struct page *ipage, pgoff_t index, bool new_i_size);
2684 int do_write_data_page(struct f2fs_io_info *fio);
2685 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2686 int create, int flag);
2687 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2688 u64 start, u64 len);
2689 void f2fs_set_page_dirty_nobuffers(struct page *page);
2690 int __f2fs_write_data_pages(struct address_space *mapping,
2691 struct writeback_control *wbc,
2692 enum iostat_type io_type);
2693 void f2fs_invalidate_page(struct page *page, unsigned int offset,
2694 unsigned int length);
2695 int f2fs_release_page(struct page *page, gfp_t wait);
2696 #ifdef CONFIG_MIGRATION
2697 int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2698 struct page *page, enum migrate_mode mode);
2704 int start_gc_thread(struct f2fs_sb_info *sbi);
2705 void stop_gc_thread(struct f2fs_sb_info *sbi);
2706 block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2707 int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2708 unsigned int segno);
2709 void build_gc_manager(struct f2fs_sb_info *sbi);
2714 int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
2715 bool space_for_roll_forward(struct f2fs_sb_info *sbi);
2720 #ifdef CONFIG_F2FS_STAT_FS
2721 struct f2fs_stat_info {
2722 struct list_head stat_list;
2723 struct f2fs_sb_info *sbi;
2724 int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
2725 int main_area_segs, main_area_sections, main_area_zones;
2726 unsigned long long hit_largest, hit_cached, hit_rbtree;
2727 unsigned long long hit_total, total_ext;
2728 int ext_tree, zombie_tree, ext_node;
2729 int ndirty_node, ndirty_dent, ndirty_meta, ndirty_data, ndirty_imeta;
2731 unsigned int ndirty_dirs, ndirty_files, ndirty_all;
2732 int nats, dirty_nats, sits, dirty_sits;
2733 int free_nids, avail_nids, alloc_nids;
2734 int total_count, utilization;
2735 int bg_gc, nr_wb_cp_data, nr_wb_data;
2736 int nr_flushing, nr_flushed, nr_discarding, nr_discarded;
2738 unsigned int undiscard_blks;
2739 int inline_xattr, inline_inode, inline_dir, append, update, orphans;
2740 int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
2741 unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
2742 unsigned int bimodal, avg_vblocks;
2743 int util_free, util_valid, util_invalid;
2744 int rsvd_segs, overp_segs;
2745 int dirty_count, node_pages, meta_pages;
2746 int prefree_count, call_count, cp_count, bg_cp_count;
2747 int tot_segs, node_segs, data_segs, free_segs, free_secs;
2748 int bg_node_segs, bg_data_segs;
2749 int tot_blks, data_blks, node_blks;
2750 int bg_data_blks, bg_node_blks;
2751 int curseg[NR_CURSEG_TYPE];
2752 int cursec[NR_CURSEG_TYPE];
2753 int curzone[NR_CURSEG_TYPE];
2755 unsigned int segment_count[2];
2756 unsigned int block_count[2];
2757 unsigned int inplace_count;
2758 unsigned long long base_mem, cache_mem, page_mem;
2761 static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
2763 return (struct f2fs_stat_info *)sbi->stat_info;
2766 #define stat_inc_cp_count(si) ((si)->cp_count++)
2767 #define stat_inc_bg_cp_count(si) ((si)->bg_cp_count++)
2768 #define stat_inc_call_count(si) ((si)->call_count++)
2769 #define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
2770 #define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++)
2771 #define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--)
2772 #define stat_inc_total_hit(sbi) (atomic64_inc(&(sbi)->total_hit_ext))
2773 #define stat_inc_rbtree_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_rbtree))
2774 #define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest))
2775 #define stat_inc_cached_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_cached))
2776 #define stat_inc_inline_xattr(inode) \
2778 if (f2fs_has_inline_xattr(inode)) \
2779 (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \
2781 #define stat_dec_inline_xattr(inode) \
2783 if (f2fs_has_inline_xattr(inode)) \
2784 (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \
2786 #define stat_inc_inline_inode(inode) \
2788 if (f2fs_has_inline_data(inode)) \
2789 (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
2791 #define stat_dec_inline_inode(inode) \
2793 if (f2fs_has_inline_data(inode)) \
2794 (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
2796 #define stat_inc_inline_dir(inode) \
2798 if (f2fs_has_inline_dentry(inode)) \
2799 (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
2801 #define stat_dec_inline_dir(inode) \
2803 if (f2fs_has_inline_dentry(inode)) \
2804 (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
2806 #define stat_inc_seg_type(sbi, curseg) \
2807 ((sbi)->segment_count[(curseg)->alloc_type]++)
2808 #define stat_inc_block_count(sbi, curseg) \
2809 ((sbi)->block_count[(curseg)->alloc_type]++)
2810 #define stat_inc_inplace_blocks(sbi) \
2811 (atomic_inc(&(sbi)->inplace_count))
2812 #define stat_inc_atomic_write(inode) \
2813 (atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
2814 #define stat_dec_atomic_write(inode) \
2815 (atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
2816 #define stat_update_max_atomic_write(inode) \
2818 int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt); \
2819 int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt); \
2821 atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur); \
2823 #define stat_inc_volatile_write(inode) \
2824 (atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
2825 #define stat_dec_volatile_write(inode) \
2826 (atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
2827 #define stat_update_max_volatile_write(inode) \
2829 int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt); \
2830 int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt); \
2832 atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur); \
2834 #define stat_inc_seg_count(sbi, type, gc_type) \
2836 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
2838 if ((type) == SUM_TYPE_DATA) { \
2840 si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
2843 si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
2847 #define stat_inc_tot_blk_count(si, blks) \
2848 ((si)->tot_blks += (blks))
2850 #define stat_inc_data_blk_count(sbi, blks, gc_type) \
2852 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
2853 stat_inc_tot_blk_count(si, blks); \
2854 si->data_blks += (blks); \
2855 si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
2858 #define stat_inc_node_blk_count(sbi, blks, gc_type) \
2860 struct f2fs_stat_info *si = F2FS_STAT(sbi); \
2861 stat_inc_tot_blk_count(si, blks); \
2862 si->node_blks += (blks); \
2863 si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
2866 int f2fs_build_stats(struct f2fs_sb_info *sbi);
2867 void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
2868 int __init f2fs_create_root_stats(void);
2869 void f2fs_destroy_root_stats(void);
2871 #define stat_inc_cp_count(si) do { } while (0)
2872 #define stat_inc_bg_cp_count(si) do { } while (0)
2873 #define stat_inc_call_count(si) do { } while (0)
2874 #define stat_inc_bggc_count(si) do { } while (0)
2875 #define stat_inc_dirty_inode(sbi, type) do { } while (0)
2876 #define stat_dec_dirty_inode(sbi, type) do { } while (0)
2877 #define stat_inc_total_hit(sb) do { } while (0)
2878 #define stat_inc_rbtree_node_hit(sb) do { } while (0)
2879 #define stat_inc_largest_node_hit(sbi) do { } while (0)
2880 #define stat_inc_cached_node_hit(sbi) do { } while (0)
2881 #define stat_inc_inline_xattr(inode) do { } while (0)
2882 #define stat_dec_inline_xattr(inode) do { } while (0)
2883 #define stat_inc_inline_inode(inode) do { } while (0)
2884 #define stat_dec_inline_inode(inode) do { } while (0)
2885 #define stat_inc_inline_dir(inode) do { } while (0)
2886 #define stat_dec_inline_dir(inode) do { } while (0)
2887 #define stat_inc_atomic_write(inode) do { } while (0)
2888 #define stat_dec_atomic_write(inode) do { } while (0)
2889 #define stat_update_max_atomic_write(inode) do { } while (0)
2890 #define stat_inc_volatile_write(inode) do { } while (0)
2891 #define stat_dec_volatile_write(inode) do { } while (0)
2892 #define stat_update_max_volatile_write(inode) do { } while (0)
2893 #define stat_inc_seg_type(sbi, curseg) do { } while (0)
2894 #define stat_inc_block_count(sbi, curseg) do { } while (0)
2895 #define stat_inc_inplace_blocks(sbi) do { } while (0)
2896 #define stat_inc_seg_count(sbi, type, gc_type) do { } while (0)
2897 #define stat_inc_tot_blk_count(si, blks) do { } while (0)
2898 #define stat_inc_data_blk_count(sbi, blks, gc_type) do { } while (0)
2899 #define stat_inc_node_blk_count(sbi, blks, gc_type) do { } while (0)
2901 static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
2902 static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
2903 static inline int __init f2fs_create_root_stats(void) { return 0; }
2904 static inline void f2fs_destroy_root_stats(void) { }
2907 extern const struct file_operations f2fs_dir_operations;
2908 extern const struct file_operations f2fs_file_operations;
2909 extern const struct inode_operations f2fs_file_inode_operations;
2910 extern const struct address_space_operations f2fs_dblock_aops;
2911 extern const struct address_space_operations f2fs_node_aops;
2912 extern const struct address_space_operations f2fs_meta_aops;
2913 extern const struct inode_operations f2fs_dir_inode_operations;
2914 extern const struct inode_operations f2fs_symlink_inode_operations;
2915 extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
2916 extern const struct inode_operations f2fs_special_inode_operations;
2917 extern struct kmem_cache *inode_entry_slab;
2922 bool f2fs_may_inline_data(struct inode *inode);
2923 bool f2fs_may_inline_dentry(struct inode *inode);
2924 void read_inline_data(struct page *page, struct page *ipage);
2925 void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
2926 int f2fs_read_inline_data(struct inode *inode, struct page *page);
2927 int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
2928 int f2fs_convert_inline_inode(struct inode *inode);
2929 int f2fs_write_inline_data(struct inode *inode, struct page *page);
2930 bool recover_inline_data(struct inode *inode, struct page *npage);
2931 struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
2932 struct fscrypt_name *fname, struct page **res_page);
2933 int make_empty_inline_dir(struct inode *inode, struct inode *parent,
2934 struct page *ipage);
2935 int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
2936 const struct qstr *orig_name,
2937 struct inode *inode, nid_t ino, umode_t mode);
2938 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
2939 struct inode *dir, struct inode *inode);
2940 bool f2fs_empty_inline_dir(struct inode *dir);
2941 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
2942 struct fscrypt_str *fstr);
2943 int f2fs_inline_data_fiemap(struct inode *inode,
2944 struct fiemap_extent_info *fieinfo,
2945 __u64 start, __u64 len);
2950 unsigned long f2fs_shrink_count(struct shrinker *shrink,
2951 struct shrink_control *sc);
2952 unsigned long f2fs_shrink_scan(struct shrinker *shrink,
2953 struct shrink_control *sc);
2954 void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
2955 void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
2960 struct rb_entry *__lookup_rb_tree(struct rb_root *root,
2961 struct rb_entry *cached_re, unsigned int ofs);
2962 struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
2963 struct rb_root *root, struct rb_node **parent,
2965 struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
2966 struct rb_entry *cached_re, unsigned int ofs,
2967 struct rb_entry **prev_entry, struct rb_entry **next_entry,
2968 struct rb_node ***insert_p, struct rb_node **insert_parent,
2970 bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
2971 struct rb_root *root);
2972 unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
2973 bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
2974 void f2fs_drop_extent_tree(struct inode *inode);
2975 unsigned int f2fs_destroy_extent_node(struct inode *inode);
2976 void f2fs_destroy_extent_tree(struct inode *inode);
2977 bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
2978 struct extent_info *ei);
2979 void f2fs_update_extent_cache(struct dnode_of_data *dn);
2980 void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
2981 pgoff_t fofs, block_t blkaddr, unsigned int len);
2982 void init_extent_cache_info(struct f2fs_sb_info *sbi);
2983 int __init create_extent_cache(void);
2984 void destroy_extent_cache(void);
2989 int __init f2fs_init_sysfs(void);
2990 void f2fs_exit_sysfs(void);
2991 int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
2992 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
2997 static inline bool f2fs_encrypted_inode(struct inode *inode)
2999 return file_is_encrypt(inode);
3002 static inline bool f2fs_encrypted_file(struct inode *inode)
3004 return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
3007 static inline void f2fs_set_encrypted_inode(struct inode *inode)
3009 #ifdef CONFIG_F2FS_FS_ENCRYPTION
3010 file_set_encrypt(inode);
3014 static inline bool f2fs_bio_encrypted(struct bio *bio)
3016 return bio->bi_private != NULL;
3019 static inline int f2fs_sb_has_crypto(struct super_block *sb)
3021 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
3024 static inline int f2fs_sb_mounted_blkzoned(struct super_block *sb)
3026 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_BLKZONED);
3029 static inline int f2fs_sb_has_extra_attr(struct super_block *sb)
3031 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_EXTRA_ATTR);
3034 static inline int f2fs_sb_has_project_quota(struct super_block *sb)
3036 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_PRJQUOTA);
3039 static inline int f2fs_sb_has_inode_chksum(struct super_block *sb)
3041 return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_INODE_CHKSUM);
3044 #ifdef CONFIG_BLK_DEV_ZONED
3045 static inline int get_blkz_type(struct f2fs_sb_info *sbi,
3046 struct block_device *bdev, block_t blkaddr)
3048 unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
3051 for (i = 0; i < sbi->s_ndevs; i++)
3052 if (FDEV(i).bdev == bdev)
3053 return FDEV(i).blkz_type[zno];
3058 static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
3060 struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
3062 return blk_queue_discard(q) || f2fs_sb_mounted_blkzoned(sbi->sb);
3065 static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
3067 clear_opt(sbi, ADAPTIVE);
3068 clear_opt(sbi, LFS);
3071 case F2FS_MOUNT_ADAPTIVE:
3072 set_opt(sbi, ADAPTIVE);
3074 case F2FS_MOUNT_LFS:
3080 static inline bool f2fs_may_encrypt(struct inode *inode)
3082 #ifdef CONFIG_F2FS_FS_ENCRYPTION
3083 umode_t mode = inode->i_mode;
3085 return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3093 #define EFSBADCRC EBADMSG /* Bad CRC detected */
3094 #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */