root/fs/f2fs/f2fs.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. update_nats_in_cursum
  2. update_sits_in_cursum
  3. __has_cursum_space
  4. make_dentry_ptr_block
  5. make_dentry_ptr_inline
  6. get_extent_info
  7. set_raw_extent
  8. set_extent_info
  9. __is_discard_mergeable
  10. __is_discard_back_mergeable
  11. __is_discard_front_mergeable
  12. __is_extent_mergeable
  13. __is_back_mergeable
  14. __is_front_mergeable
  15. __try_update_largest_extent
  16. set_new_dnode
  17. time_to_inject
  18. time_to_inject
  19. f2fs_is_multi_device
  20. f2fs_update_time
  21. f2fs_time_over
  22. f2fs_time_to_wait
  23. __f2fs_crc32
  24. f2fs_crc32
  25. f2fs_crc_valid
  26. f2fs_chksum
  27. F2FS_I
  28. F2FS_SB
  29. F2FS_I_SB
  30. F2FS_M_SB
  31. F2FS_P_SB
  32. F2FS_RAW_SUPER
  33. F2FS_CKPT
  34. F2FS_NODE
  35. F2FS_INODE
  36. NM_I
  37. SM_I
  38. SIT_I
  39. FREE_I
  40. DIRTY_I
  41. META_MAPPING
  42. NODE_MAPPING
  43. is_sbi_flag_set
  44. set_sbi_flag
  45. clear_sbi_flag
  46. cur_cp_version
  47. f2fs_qf_ino
  48. cur_cp_crc
  49. __is_set_ckpt_flags
  50. is_set_ckpt_flags
  51. __set_ckpt_flags
  52. set_ckpt_flags
  53. __clear_ckpt_flags
  54. clear_ckpt_flags
  55. disable_nat_bits
  56. enabled_nat_bits
  57. f2fs_lock_op
  58. f2fs_trylock_op
  59. f2fs_unlock_op
  60. f2fs_lock_all
  61. f2fs_unlock_all
  62. __get_cp_reason
  63. __remain_node_summaries
  64. __exist_node_summaries
  65. F2FS_HAS_BLOCKS
  66. f2fs_has_xattr_block
  67. __allow_reserved_blocks
  68. inc_valid_block_count
  69. dec_valid_block_count
  70. inc_page_count
  71. inode_inc_dirty_pages
  72. dec_page_count
  73. inode_dec_dirty_pages
  74. get_pages
  75. get_dirty_pages
  76. get_blocktype_secs
  77. valid_user_blocks
  78. discard_blocks
  79. __bitmap_size
  80. __cp_payload
  81. __bitmap_ptr
  82. __start_cp_addr
  83. __start_cp_next_addr
  84. __set_cp_next_pack
  85. __start_sum_addr
  86. inc_valid_node_count
  87. dec_valid_node_count
  88. valid_node_count
  89. inc_valid_inode_count
  90. dec_valid_inode_count
  91. valid_inode_count
  92. f2fs_grab_cache_page
  93. f2fs_pagecache_get_page
  94. f2fs_copy_page
  95. f2fs_put_page
  96. f2fs_put_dnode
  97. f2fs_kmem_cache_create
  98. f2fs_kmem_cache_alloc
  99. f2fs_bio_alloc
  100. is_idle
  101. f2fs_radix_tree_insert
  102. IS_INODE
  103. offset_in_addr
  104. blkaddr_in_node
  105. datablock_addr
  106. f2fs_test_bit
  107. f2fs_set_bit
  108. f2fs_clear_bit
  109. f2fs_test_and_set_bit
  110. f2fs_test_and_clear_bit
  111. f2fs_change_bit
  112. f2fs_mask_flags
  113. __mark_inode_dirty_flag
  114. set_inode_flag
  115. is_inode_flag_set
  116. clear_inode_flag
  117. f2fs_verity_in_progress
  118. set_acl_inode
  119. f2fs_i_links_write
  120. f2fs_i_blocks_write
  121. f2fs_i_size_write
  122. f2fs_i_depth_write
  123. f2fs_i_gc_failures_write
  124. f2fs_i_xnid_write
  125. f2fs_i_pino_write
  126. get_inline_info
  127. set_raw_inline
  128. f2fs_has_extra_attr
  129. f2fs_has_inline_xattr
  130. addrs_per_inode
  131. addrs_per_block
  132. inline_xattr_addr
  133. inline_xattr_size
  134. f2fs_has_inline_data
  135. f2fs_exist_data
  136. f2fs_has_inline_dots
  137. f2fs_is_pinned_file
  138. f2fs_is_atomic_file
  139. f2fs_is_commit_atomic_write
  140. f2fs_is_volatile_file
  141. f2fs_is_first_block_written
  142. f2fs_is_drop_cache
  143. inline_data_addr
  144. f2fs_has_inline_dentry
  145. is_file
  146. set_file
  147. clear_file
  148. f2fs_is_time_consistent
  149. f2fs_skip_inode_update
  150. f2fs_readonly
  151. f2fs_cp_error
  152. is_dot_dotdot
  153. f2fs_may_extent_tree
  154. f2fs_kmalloc
  155. f2fs_kzalloc
  156. f2fs_kvmalloc
  157. f2fs_kvzalloc
  158. get_extra_isize
  159. get_inline_xattr_addrs
  160. f2fs_reset_iostat
  161. f2fs_update_iostat
  162. verify_blkaddr
  163. __is_valid_data_blkaddr
  164. f2fs_set_page_private
  165. f2fs_clear_page_private
  166. f2fs_add_link
  167. F2FS_STAT
  168. f2fs_build_stats
  169. f2fs_destroy_stats
  170. f2fs_create_root_stats
  171. f2fs_destroy_root_stats
  172. f2fs_encrypted_file
  173. f2fs_set_encrypted_inode
  174. f2fs_post_read_required
  175. f2fs_blkz_is_seq
  176. f2fs_hw_should_discard
  177. f2fs_bdev_support_discard
  178. f2fs_hw_support_discard
  179. f2fs_realtime_discard_enable
  180. f2fs_hw_is_readonly
  181. set_opt_mode
  182. f2fs_may_encrypt
  183. block_unaligned_IO
  184. allow_outplace_dio
  185. f2fs_force_buffered_io
  186. is_journalled_quota

   1 // SPDX-License-Identifier: GPL-2.0
   2 /*
   3  * fs/f2fs/f2fs.h
   4  *
   5  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
   6  *             http://www.samsung.com/
   7  */
   8 #ifndef _LINUX_F2FS_H
   9 #define _LINUX_F2FS_H
  10 
  11 #include <linux/uio.h>
  12 #include <linux/types.h>
  13 #include <linux/page-flags.h>
  14 #include <linux/buffer_head.h>
  15 #include <linux/slab.h>
  16 #include <linux/crc32.h>
  17 #include <linux/magic.h>
  18 #include <linux/kobject.h>
  19 #include <linux/sched.h>
  20 #include <linux/cred.h>
  21 #include <linux/vmalloc.h>
  22 #include <linux/bio.h>
  23 #include <linux/blkdev.h>
  24 #include <linux/quotaops.h>
  25 #include <crypto/hash.h>
  26 
  27 #include <linux/fscrypt.h>
  28 #include <linux/fsverity.h>
  29 
  30 #ifdef CONFIG_F2FS_CHECK_FS
  31 #define f2fs_bug_on(sbi, condition)     BUG_ON(condition)
  32 #else
  33 #define f2fs_bug_on(sbi, condition)                                     \
  34         do {                                                            \
  35                 if (unlikely(condition)) {                              \
  36                         WARN_ON(1);                                     \
  37                         set_sbi_flag(sbi, SBI_NEED_FSCK);               \
  38                 }                                                       \
  39         } while (0)
  40 #endif
  41 
  42 enum {
  43         FAULT_KMALLOC,
  44         FAULT_KVMALLOC,
  45         FAULT_PAGE_ALLOC,
  46         FAULT_PAGE_GET,
  47         FAULT_ALLOC_BIO,
  48         FAULT_ALLOC_NID,
  49         FAULT_ORPHAN,
  50         FAULT_BLOCK,
  51         FAULT_DIR_DEPTH,
  52         FAULT_EVICT_INODE,
  53         FAULT_TRUNCATE,
  54         FAULT_READ_IO,
  55         FAULT_CHECKPOINT,
  56         FAULT_DISCARD,
  57         FAULT_WRITE_IO,
  58         FAULT_MAX,
  59 };
  60 
  61 #ifdef CONFIG_F2FS_FAULT_INJECTION
  62 #define F2FS_ALL_FAULT_TYPE             ((1 << FAULT_MAX) - 1)
  63 
  64 struct f2fs_fault_info {
  65         atomic_t inject_ops;
  66         unsigned int inject_rate;
  67         unsigned int inject_type;
  68 };
  69 
  70 extern const char *f2fs_fault_name[FAULT_MAX];
  71 #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
  72 #endif
  73 
  74 /*
  75  * For mount options
  76  */
  77 #define F2FS_MOUNT_BG_GC                0x00000001
  78 #define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
  79 #define F2FS_MOUNT_DISCARD              0x00000004
  80 #define F2FS_MOUNT_NOHEAP               0x00000008
  81 #define F2FS_MOUNT_XATTR_USER           0x00000010
  82 #define F2FS_MOUNT_POSIX_ACL            0x00000020
  83 #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
  84 #define F2FS_MOUNT_INLINE_XATTR         0x00000080
  85 #define F2FS_MOUNT_INLINE_DATA          0x00000100
  86 #define F2FS_MOUNT_INLINE_DENTRY        0x00000200
  87 #define F2FS_MOUNT_FLUSH_MERGE          0x00000400
  88 #define F2FS_MOUNT_NOBARRIER            0x00000800
  89 #define F2FS_MOUNT_FASTBOOT             0x00001000
  90 #define F2FS_MOUNT_EXTENT_CACHE         0x00002000
  91 #define F2FS_MOUNT_FORCE_FG_GC          0x00004000
  92 #define F2FS_MOUNT_DATA_FLUSH           0x00008000
  93 #define F2FS_MOUNT_FAULT_INJECTION      0x00010000
  94 #define F2FS_MOUNT_ADAPTIVE             0x00020000
  95 #define F2FS_MOUNT_LFS                  0x00040000
  96 #define F2FS_MOUNT_USRQUOTA             0x00080000
  97 #define F2FS_MOUNT_GRPQUOTA             0x00100000
  98 #define F2FS_MOUNT_PRJQUOTA             0x00200000
  99 #define F2FS_MOUNT_QUOTA                0x00400000
 100 #define F2FS_MOUNT_INLINE_XATTR_SIZE    0x00800000
 101 #define F2FS_MOUNT_RESERVE_ROOT         0x01000000
 102 #define F2FS_MOUNT_DISABLE_CHECKPOINT   0x02000000
 103 #define F2FS_MOUNT_NORECOVERY           0x04000000
 104 
 105 #define F2FS_OPTION(sbi)        ((sbi)->mount_opt)
 106 #define clear_opt(sbi, option)  (F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
 107 #define set_opt(sbi, option)    (F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
 108 #define test_opt(sbi, option)   (F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
 109 
 110 #define ver_after(a, b) (typecheck(unsigned long long, a) &&            \
 111                 typecheck(unsigned long long, b) &&                     \
 112                 ((long long)((a) - (b)) > 0))
 113 
 114 typedef u32 block_t;    /*
 115                          * should not change u32, since it is the on-disk block
 116                          * address format, __le32.
 117                          */
 118 typedef u32 nid_t;
 119 
 120 struct f2fs_mount_info {
 121         unsigned int opt;
 122         int write_io_size_bits;         /* Write IO size bits */
 123         block_t root_reserved_blocks;   /* root reserved blocks */
 124         kuid_t s_resuid;                /* reserved blocks for uid */
 125         kgid_t s_resgid;                /* reserved blocks for gid */
 126         int active_logs;                /* # of active logs */
 127         int inline_xattr_size;          /* inline xattr size */
 128 #ifdef CONFIG_F2FS_FAULT_INJECTION
 129         struct f2fs_fault_info fault_info;      /* For fault injection */
 130 #endif
 131 #ifdef CONFIG_QUOTA
 132         /* Names of quota files with journalled quota */
 133         char *s_qf_names[MAXQUOTAS];
 134         int s_jquota_fmt;                       /* Format of quota to use */
 135 #endif
 136         /* For which write hints are passed down to block layer */
 137         int whint_mode;
 138         int alloc_mode;                 /* segment allocation policy */
 139         int fsync_mode;                 /* fsync policy */
 140         bool test_dummy_encryption;     /* test dummy encryption */
 141         block_t unusable_cap;           /* Amount of space allowed to be
 142                                          * unusable when disabling checkpoint
 143                                          */
 144 };
 145 
 146 #define F2FS_FEATURE_ENCRYPT            0x0001
 147 #define F2FS_FEATURE_BLKZONED           0x0002
 148 #define F2FS_FEATURE_ATOMIC_WRITE       0x0004
 149 #define F2FS_FEATURE_EXTRA_ATTR         0x0008
 150 #define F2FS_FEATURE_PRJQUOTA           0x0010
 151 #define F2FS_FEATURE_INODE_CHKSUM       0x0020
 152 #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR      0x0040
 153 #define F2FS_FEATURE_QUOTA_INO          0x0080
 154 #define F2FS_FEATURE_INODE_CRTIME       0x0100
 155 #define F2FS_FEATURE_LOST_FOUND         0x0200
 156 #define F2FS_FEATURE_VERITY             0x0400
 157 #define F2FS_FEATURE_SB_CHKSUM          0x0800
 158 #define F2FS_FEATURE_CASEFOLD           0x1000
 159 
 160 #define __F2FS_HAS_FEATURE(raw_super, mask)                             \
 161         ((raw_super->feature & cpu_to_le32(mask)) != 0)
 162 #define F2FS_HAS_FEATURE(sbi, mask)     __F2FS_HAS_FEATURE(sbi->raw_super, mask)
 163 #define F2FS_SET_FEATURE(sbi, mask)                                     \
 164         (sbi->raw_super->feature |= cpu_to_le32(mask))
 165 #define F2FS_CLEAR_FEATURE(sbi, mask)                                   \
 166         (sbi->raw_super->feature &= ~cpu_to_le32(mask))
 167 
 168 /*
 169  * Default values for user and/or group using reserved blocks
 170  */
 171 #define F2FS_DEF_RESUID         0
 172 #define F2FS_DEF_RESGID         0
 173 
 174 /*
 175  * For checkpoint manager
 176  */
 177 enum {
 178         NAT_BITMAP,
 179         SIT_BITMAP
 180 };
 181 
 182 #define CP_UMOUNT       0x00000001
 183 #define CP_FASTBOOT     0x00000002
 184 #define CP_SYNC         0x00000004
 185 #define CP_RECOVERY     0x00000008
 186 #define CP_DISCARD      0x00000010
 187 #define CP_TRIMMED      0x00000020
 188 #define CP_PAUSE        0x00000040
 189 
 190 #define MAX_DISCARD_BLOCKS(sbi)         BLKS_PER_SEC(sbi)
 191 #define DEF_MAX_DISCARD_REQUEST         8       /* issue 8 discards per round */
 192 #define DEF_MIN_DISCARD_ISSUE_TIME      50      /* 50 ms, if exists */
 193 #define DEF_MID_DISCARD_ISSUE_TIME      500     /* 500 ms, if device busy */
 194 #define DEF_MAX_DISCARD_ISSUE_TIME      60000   /* 60 s, if no candidates */
 195 #define DEF_DISCARD_URGENT_UTIL         80      /* do more discard over 80% */
 196 #define DEF_CP_INTERVAL                 60      /* 60 secs */
 197 #define DEF_IDLE_INTERVAL               5       /* 5 secs */
 198 #define DEF_DISABLE_INTERVAL            5       /* 5 secs */
 199 #define DEF_DISABLE_QUICK_INTERVAL      1       /* 1 secs */
 200 #define DEF_UMOUNT_DISCARD_TIMEOUT      5       /* 5 secs */
 201 
 202 struct cp_control {
 203         int reason;
 204         __u64 trim_start;
 205         __u64 trim_end;
 206         __u64 trim_minlen;
 207 };
 208 
 209 /*
 210  * indicate meta/data type
 211  */
 212 enum {
 213         META_CP,
 214         META_NAT,
 215         META_SIT,
 216         META_SSA,
 217         META_MAX,
 218         META_POR,
 219         DATA_GENERIC,           /* check range only */
 220         DATA_GENERIC_ENHANCE,   /* strong check on range and segment bitmap */
 221         DATA_GENERIC_ENHANCE_READ,      /*
 222                                          * strong check on range and segment
 223                                          * bitmap but no warning due to race
 224                                          * condition of read on truncated area
 225                                          * by extent_cache
 226                                          */
 227         META_GENERIC,
 228 };
 229 
 230 /* for the list of ino */
 231 enum {
 232         ORPHAN_INO,             /* for orphan ino list */
 233         APPEND_INO,             /* for append ino list */
 234         UPDATE_INO,             /* for update ino list */
 235         TRANS_DIR_INO,          /* for trasactions dir ino list */
 236         FLUSH_INO,              /* for multiple device flushing */
 237         MAX_INO_ENTRY,          /* max. list */
 238 };
 239 
 240 struct ino_entry {
 241         struct list_head list;          /* list head */
 242         nid_t ino;                      /* inode number */
 243         unsigned int dirty_device;      /* dirty device bitmap */
 244 };
 245 
 246 /* for the list of inodes to be GCed */
 247 struct inode_entry {
 248         struct list_head list;  /* list head */
 249         struct inode *inode;    /* vfs inode pointer */
 250 };
 251 
 252 struct fsync_node_entry {
 253         struct list_head list;  /* list head */
 254         struct page *page;      /* warm node page pointer */
 255         unsigned int seq_id;    /* sequence id */
 256 };
 257 
 258 /* for the bitmap indicate blocks to be discarded */
 259 struct discard_entry {
 260         struct list_head list;  /* list head */
 261         block_t start_blkaddr;  /* start blockaddr of current segment */
 262         unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */
 263 };
 264 
 265 /* default discard granularity of inner discard thread, unit: block count */
 266 #define DEFAULT_DISCARD_GRANULARITY             16
 267 
 268 /* max discard pend list number */
 269 #define MAX_PLIST_NUM           512
 270 #define plist_idx(blk_num)      ((blk_num) >= MAX_PLIST_NUM ?           \
 271                                         (MAX_PLIST_NUM - 1) : ((blk_num) - 1))
 272 
 273 enum {
 274         D_PREP,                 /* initial */
 275         D_PARTIAL,              /* partially submitted */
 276         D_SUBMIT,               /* all submitted */
 277         D_DONE,                 /* finished */
 278 };
 279 
 280 struct discard_info {
 281         block_t lstart;                 /* logical start address */
 282         block_t len;                    /* length */
 283         block_t start;                  /* actual start address in dev */
 284 };
 285 
 286 struct discard_cmd {
 287         struct rb_node rb_node;         /* rb node located in rb-tree */
 288         union {
 289                 struct {
 290                         block_t lstart; /* logical start address */
 291                         block_t len;    /* length */
 292                         block_t start;  /* actual start address in dev */
 293                 };
 294                 struct discard_info di; /* discard info */
 295 
 296         };
 297         struct list_head list;          /* command list */
 298         struct completion wait;         /* compleation */
 299         struct block_device *bdev;      /* bdev */
 300         unsigned short ref;             /* reference count */
 301         unsigned char state;            /* state */
 302         unsigned char queued;           /* queued discard */
 303         int error;                      /* bio error */
 304         spinlock_t lock;                /* for state/bio_ref updating */
 305         unsigned short bio_ref;         /* bio reference count */
 306 };
 307 
 308 enum {
 309         DPOLICY_BG,
 310         DPOLICY_FORCE,
 311         DPOLICY_FSTRIM,
 312         DPOLICY_UMOUNT,
 313         MAX_DPOLICY,
 314 };
 315 
 316 struct discard_policy {
 317         int type;                       /* type of discard */
 318         unsigned int min_interval;      /* used for candidates exist */
 319         unsigned int mid_interval;      /* used for device busy */
 320         unsigned int max_interval;      /* used for candidates not exist */
 321         unsigned int max_requests;      /* # of discards issued per round */
 322         unsigned int io_aware_gran;     /* minimum granularity discard not be aware of I/O */
 323         bool io_aware;                  /* issue discard in idle time */
 324         bool sync;                      /* submit discard with REQ_SYNC flag */
 325         bool ordered;                   /* issue discard by lba order */
 326         unsigned int granularity;       /* discard granularity */
 327         int timeout;                    /* discard timeout for put_super */
 328 };
 329 
 330 struct discard_cmd_control {
 331         struct task_struct *f2fs_issue_discard; /* discard thread */
 332         struct list_head entry_list;            /* 4KB discard entry list */
 333         struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
 334         struct list_head wait_list;             /* store on-flushing entries */
 335         struct list_head fstrim_list;           /* in-flight discard from fstrim */
 336         wait_queue_head_t discard_wait_queue;   /* waiting queue for wake-up */
 337         unsigned int discard_wake;              /* to wake up discard thread */
 338         struct mutex cmd_lock;
 339         unsigned int nr_discards;               /* # of discards in the list */
 340         unsigned int max_discards;              /* max. discards to be issued */
 341         unsigned int discard_granularity;       /* discard granularity */
 342         unsigned int undiscard_blks;            /* # of undiscard blocks */
 343         unsigned int next_pos;                  /* next discard position */
 344         atomic_t issued_discard;                /* # of issued discard */
 345         atomic_t queued_discard;                /* # of queued discard */
 346         atomic_t discard_cmd_cnt;               /* # of cached cmd count */
 347         struct rb_root_cached root;             /* root of discard rb-tree */
 348         bool rbtree_check;                      /* config for consistence check */
 349 };
 350 
 351 /* for the list of fsync inodes, used only during recovery */
 352 struct fsync_inode_entry {
 353         struct list_head list;  /* list head */
 354         struct inode *inode;    /* vfs inode pointer */
 355         block_t blkaddr;        /* block address locating the last fsync */
 356         block_t last_dentry;    /* block address locating the last dentry */
 357 };
 358 
 359 #define nats_in_cursum(jnl)             (le16_to_cpu((jnl)->n_nats))
 360 #define sits_in_cursum(jnl)             (le16_to_cpu((jnl)->n_sits))
 361 
 362 #define nat_in_journal(jnl, i)          ((jnl)->nat_j.entries[i].ne)
 363 #define nid_in_journal(jnl, i)          ((jnl)->nat_j.entries[i].nid)
 364 #define sit_in_journal(jnl, i)          ((jnl)->sit_j.entries[i].se)
 365 #define segno_in_journal(jnl, i)        ((jnl)->sit_j.entries[i].segno)
 366 
 367 #define MAX_NAT_JENTRIES(jnl)   (NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
 368 #define MAX_SIT_JENTRIES(jnl)   (SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
 369 
 370 static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
 371 {
 372         int before = nats_in_cursum(journal);
 373 
 374         journal->n_nats = cpu_to_le16(before + i);
 375         return before;
 376 }
 377 
 378 static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
 379 {
 380         int before = sits_in_cursum(journal);
 381 
 382         journal->n_sits = cpu_to_le16(before + i);
 383         return before;
 384 }
 385 
 386 static inline bool __has_cursum_space(struct f2fs_journal *journal,
 387                                                         int size, int type)
 388 {
 389         if (type == NAT_JOURNAL)
 390                 return size <= MAX_NAT_JENTRIES(journal);
 391         return size <= MAX_SIT_JENTRIES(journal);
 392 }
 393 
 394 /*
 395  * ioctl commands
 396  */
 397 #define F2FS_IOC_GETFLAGS               FS_IOC_GETFLAGS
 398 #define F2FS_IOC_SETFLAGS               FS_IOC_SETFLAGS
 399 #define F2FS_IOC_GETVERSION             FS_IOC_GETVERSION
 400 
 401 #define F2FS_IOCTL_MAGIC                0xf5
 402 #define F2FS_IOC_START_ATOMIC_WRITE     _IO(F2FS_IOCTL_MAGIC, 1)
 403 #define F2FS_IOC_COMMIT_ATOMIC_WRITE    _IO(F2FS_IOCTL_MAGIC, 2)
 404 #define F2FS_IOC_START_VOLATILE_WRITE   _IO(F2FS_IOCTL_MAGIC, 3)
 405 #define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
 406 #define F2FS_IOC_ABORT_VOLATILE_WRITE   _IO(F2FS_IOCTL_MAGIC, 5)
 407 #define F2FS_IOC_GARBAGE_COLLECT        _IOW(F2FS_IOCTL_MAGIC, 6, __u32)
 408 #define F2FS_IOC_WRITE_CHECKPOINT       _IO(F2FS_IOCTL_MAGIC, 7)
 409 #define F2FS_IOC_DEFRAGMENT             _IOWR(F2FS_IOCTL_MAGIC, 8,      \
 410                                                 struct f2fs_defragment)
 411 #define F2FS_IOC_MOVE_RANGE             _IOWR(F2FS_IOCTL_MAGIC, 9,      \
 412                                                 struct f2fs_move_range)
 413 #define F2FS_IOC_FLUSH_DEVICE           _IOW(F2FS_IOCTL_MAGIC, 10,      \
 414                                                 struct f2fs_flush_device)
 415 #define F2FS_IOC_GARBAGE_COLLECT_RANGE  _IOW(F2FS_IOCTL_MAGIC, 11,      \
 416                                                 struct f2fs_gc_range)
 417 #define F2FS_IOC_GET_FEATURES           _IOR(F2FS_IOCTL_MAGIC, 12, __u32)
 418 #define F2FS_IOC_SET_PIN_FILE           _IOW(F2FS_IOCTL_MAGIC, 13, __u32)
 419 #define F2FS_IOC_GET_PIN_FILE           _IOR(F2FS_IOCTL_MAGIC, 14, __u32)
 420 #define F2FS_IOC_PRECACHE_EXTENTS       _IO(F2FS_IOCTL_MAGIC, 15)
 421 #define F2FS_IOC_RESIZE_FS              _IOW(F2FS_IOCTL_MAGIC, 16, __u64)
 422 
 423 #define F2FS_IOC_GET_VOLUME_NAME        FS_IOC_GETFSLABEL
 424 #define F2FS_IOC_SET_VOLUME_NAME        FS_IOC_SETFSLABEL
 425 
 426 #define F2FS_IOC_SET_ENCRYPTION_POLICY  FS_IOC_SET_ENCRYPTION_POLICY
 427 #define F2FS_IOC_GET_ENCRYPTION_POLICY  FS_IOC_GET_ENCRYPTION_POLICY
 428 #define F2FS_IOC_GET_ENCRYPTION_PWSALT  FS_IOC_GET_ENCRYPTION_PWSALT
 429 
 430 /*
 431  * should be same as XFS_IOC_GOINGDOWN.
 432  * Flags for going down operation used by FS_IOC_GOINGDOWN
 433  */
 434 #define F2FS_IOC_SHUTDOWN       _IOR('X', 125, __u32)   /* Shutdown */
 435 #define F2FS_GOING_DOWN_FULLSYNC        0x0     /* going down with full sync */
 436 #define F2FS_GOING_DOWN_METASYNC        0x1     /* going down with metadata */
 437 #define F2FS_GOING_DOWN_NOSYNC          0x2     /* going down */
 438 #define F2FS_GOING_DOWN_METAFLUSH       0x3     /* going down with meta flush */
 439 #define F2FS_GOING_DOWN_NEED_FSCK       0x4     /* going down to trigger fsck */
 440 
 441 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
 442 /*
 443  * ioctl commands in 32 bit emulation
 444  */
 445 #define F2FS_IOC32_GETFLAGS             FS_IOC32_GETFLAGS
 446 #define F2FS_IOC32_SETFLAGS             FS_IOC32_SETFLAGS
 447 #define F2FS_IOC32_GETVERSION           FS_IOC32_GETVERSION
 448 #endif
 449 
 450 #define F2FS_IOC_FSGETXATTR             FS_IOC_FSGETXATTR
 451 #define F2FS_IOC_FSSETXATTR             FS_IOC_FSSETXATTR
 452 
 453 struct f2fs_gc_range {
 454         u32 sync;
 455         u64 start;
 456         u64 len;
 457 };
 458 
 459 struct f2fs_defragment {
 460         u64 start;
 461         u64 len;
 462 };
 463 
 464 struct f2fs_move_range {
 465         u32 dst_fd;             /* destination fd */
 466         u64 pos_in;             /* start position in src_fd */
 467         u64 pos_out;            /* start position in dst_fd */
 468         u64 len;                /* size to move */
 469 };
 470 
 471 struct f2fs_flush_device {
 472         u32 dev_num;            /* device number to flush */
 473         u32 segments;           /* # of segments to flush */
 474 };
 475 
 476 /* for inline stuff */
 477 #define DEF_INLINE_RESERVED_SIZE        1
 478 static inline int get_extra_isize(struct inode *inode);
 479 static inline int get_inline_xattr_addrs(struct inode *inode);
 480 #define MAX_INLINE_DATA(inode)  (sizeof(__le32) *                       \
 481                                 (CUR_ADDRS_PER_INODE(inode) -           \
 482                                 get_inline_xattr_addrs(inode) - \
 483                                 DEF_INLINE_RESERVED_SIZE))
 484 
 485 /* for inline dir */
 486 #define NR_INLINE_DENTRY(inode) (MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
 487                                 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
 488                                 BITS_PER_BYTE + 1))
 489 #define INLINE_DENTRY_BITMAP_SIZE(inode) \
 490         DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
 491 #define INLINE_RESERVED_SIZE(inode)     (MAX_INLINE_DATA(inode) - \
 492                                 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
 493                                 NR_INLINE_DENTRY(inode) + \
 494                                 INLINE_DENTRY_BITMAP_SIZE(inode)))
 495 
 496 /*
 497  * For INODE and NODE manager
 498  */
 499 /* for directory operations */
 500 struct f2fs_dentry_ptr {
 501         struct inode *inode;
 502         void *bitmap;
 503         struct f2fs_dir_entry *dentry;
 504         __u8 (*filename)[F2FS_SLOT_LEN];
 505         int max;
 506         int nr_bitmap;
 507 };
 508 
 509 static inline void make_dentry_ptr_block(struct inode *inode,
 510                 struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
 511 {
 512         d->inode = inode;
 513         d->max = NR_DENTRY_IN_BLOCK;
 514         d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
 515         d->bitmap = t->dentry_bitmap;
 516         d->dentry = t->dentry;
 517         d->filename = t->filename;
 518 }
 519 
 520 static inline void make_dentry_ptr_inline(struct inode *inode,
 521                                         struct f2fs_dentry_ptr *d, void *t)
 522 {
 523         int entry_cnt = NR_INLINE_DENTRY(inode);
 524         int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
 525         int reserved_size = INLINE_RESERVED_SIZE(inode);
 526 
 527         d->inode = inode;
 528         d->max = entry_cnt;
 529         d->nr_bitmap = bitmap_size;
 530         d->bitmap = t;
 531         d->dentry = t + bitmap_size + reserved_size;
 532         d->filename = t + bitmap_size + reserved_size +
 533                                         SIZE_OF_DIR_ENTRY * entry_cnt;
 534 }
 535 
 536 /*
 537  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
 538  * as its node offset to distinguish from index node blocks.
 539  * But some bits are used to mark the node block.
 540  */
 541 #define XATTR_NODE_OFFSET       ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
 542                                 >> OFFSET_BIT_SHIFT)
 543 enum {
 544         ALLOC_NODE,                     /* allocate a new node page if needed */
 545         LOOKUP_NODE,                    /* look up a node without readahead */
 546         LOOKUP_NODE_RA,                 /*
 547                                          * look up a node with readahead called
 548                                          * by get_data_block.
 549                                          */
 550 };
 551 
 552 #define DEFAULT_RETRY_IO_COUNT  8       /* maximum retry read IO count */
 553 
 554 /* maximum retry quota flush count */
 555 #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT         8
 556 
 557 #define F2FS_LINK_MAX   0xffffffff      /* maximum link count per file */
 558 
 559 #define MAX_DIR_RA_PAGES        4       /* maximum ra pages of dir */
 560 
 561 /* for in-memory extent cache entry */
 562 #define F2FS_MIN_EXTENT_LEN     64      /* minimum extent length */
 563 
 564 /* number of extent info in extent cache we try to shrink */
 565 #define EXTENT_CACHE_SHRINK_NUMBER      128
 566 
 567 struct rb_entry {
 568         struct rb_node rb_node;         /* rb node located in rb-tree */
 569         unsigned int ofs;               /* start offset of the entry */
 570         unsigned int len;               /* length of the entry */
 571 };
 572 
 573 struct extent_info {
 574         unsigned int fofs;              /* start offset in a file */
 575         unsigned int len;               /* length of the extent */
 576         u32 blk;                        /* start block address of the extent */
 577 };
 578 
 579 struct extent_node {
 580         struct rb_node rb_node;         /* rb node located in rb-tree */
 581         struct extent_info ei;          /* extent info */
 582         struct list_head list;          /* node in global extent list of sbi */
 583         struct extent_tree *et;         /* extent tree pointer */
 584 };
 585 
 586 struct extent_tree {
 587         nid_t ino;                      /* inode number */
 588         struct rb_root_cached root;     /* root of extent info rb-tree */
 589         struct extent_node *cached_en;  /* recently accessed extent node */
 590         struct extent_info largest;     /* largested extent info */
 591         struct list_head list;          /* to be used by sbi->zombie_list */
 592         rwlock_t lock;                  /* protect extent info rb-tree */
 593         atomic_t node_cnt;              /* # of extent node in rb-tree*/
 594         bool largest_updated;           /* largest extent updated */
 595 };
 596 
 597 /*
 598  * This structure is taken from ext4_map_blocks.
 599  *
 600  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
 601  */
 602 #define F2FS_MAP_NEW            (1 << BH_New)
 603 #define F2FS_MAP_MAPPED         (1 << BH_Mapped)
 604 #define F2FS_MAP_UNWRITTEN      (1 << BH_Unwritten)
 605 #define F2FS_MAP_FLAGS          (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
 606                                 F2FS_MAP_UNWRITTEN)
 607 
 608 struct f2fs_map_blocks {
 609         block_t m_pblk;
 610         block_t m_lblk;
 611         unsigned int m_len;
 612         unsigned int m_flags;
 613         pgoff_t *m_next_pgofs;          /* point next possible non-hole pgofs */
 614         pgoff_t *m_next_extent;         /* point to next possible extent */
 615         int m_seg_type;
 616         bool m_may_create;              /* indicate it is from write path */
 617 };
 618 
 619 /* for flag in get_data_block */
 620 enum {
 621         F2FS_GET_BLOCK_DEFAULT,
 622         F2FS_GET_BLOCK_FIEMAP,
 623         F2FS_GET_BLOCK_BMAP,
 624         F2FS_GET_BLOCK_DIO,
 625         F2FS_GET_BLOCK_PRE_DIO,
 626         F2FS_GET_BLOCK_PRE_AIO,
 627         F2FS_GET_BLOCK_PRECACHE,
 628 };
 629 
 630 /*
 631  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
 632  */
 633 #define FADVISE_COLD_BIT        0x01
 634 #define FADVISE_LOST_PINO_BIT   0x02
 635 #define FADVISE_ENCRYPT_BIT     0x04
 636 #define FADVISE_ENC_NAME_BIT    0x08
 637 #define FADVISE_KEEP_SIZE_BIT   0x10
 638 #define FADVISE_HOT_BIT         0x20
 639 #define FADVISE_VERITY_BIT      0x40
 640 
 641 #define FADVISE_MODIFIABLE_BITS (FADVISE_COLD_BIT | FADVISE_HOT_BIT)
 642 
 643 #define file_is_cold(inode)     is_file(inode, FADVISE_COLD_BIT)
 644 #define file_wrong_pino(inode)  is_file(inode, FADVISE_LOST_PINO_BIT)
 645 #define file_set_cold(inode)    set_file(inode, FADVISE_COLD_BIT)
 646 #define file_lost_pino(inode)   set_file(inode, FADVISE_LOST_PINO_BIT)
 647 #define file_clear_cold(inode)  clear_file(inode, FADVISE_COLD_BIT)
 648 #define file_got_pino(inode)    clear_file(inode, FADVISE_LOST_PINO_BIT)
 649 #define file_is_encrypt(inode)  is_file(inode, FADVISE_ENCRYPT_BIT)
 650 #define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
 651 #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
 652 #define file_enc_name(inode)    is_file(inode, FADVISE_ENC_NAME_BIT)
 653 #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
 654 #define file_keep_isize(inode)  is_file(inode, FADVISE_KEEP_SIZE_BIT)
 655 #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
 656 #define file_is_hot(inode)      is_file(inode, FADVISE_HOT_BIT)
 657 #define file_set_hot(inode)     set_file(inode, FADVISE_HOT_BIT)
 658 #define file_clear_hot(inode)   clear_file(inode, FADVISE_HOT_BIT)
 659 #define file_is_verity(inode)   is_file(inode, FADVISE_VERITY_BIT)
 660 #define file_set_verity(inode)  set_file(inode, FADVISE_VERITY_BIT)
 661 
 662 #define DEF_DIR_LEVEL           0
 663 
 664 enum {
 665         GC_FAILURE_PIN,
 666         GC_FAILURE_ATOMIC,
 667         MAX_GC_FAILURE
 668 };
 669 
 670 struct f2fs_inode_info {
 671         struct inode vfs_inode;         /* serve a vfs inode */
 672         unsigned long i_flags;          /* keep an inode flags for ioctl */
 673         unsigned char i_advise;         /* use to give file attribute hints */
 674         unsigned char i_dir_level;      /* use for dentry level for large dir */
 675         unsigned int i_current_depth;   /* only for directory depth */
 676         /* for gc failure statistic */
 677         unsigned int i_gc_failures[MAX_GC_FAILURE];
 678         unsigned int i_pino;            /* parent inode number */
 679         umode_t i_acl_mode;             /* keep file acl mode temporarily */
 680 
 681         /* Use below internally in f2fs*/
 682         unsigned long flags;            /* use to pass per-file flags */
 683         struct rw_semaphore i_sem;      /* protect fi info */
 684         atomic_t dirty_pages;           /* # of dirty pages */
 685         f2fs_hash_t chash;              /* hash value of given file name */
 686         unsigned int clevel;            /* maximum level of given file name */
 687         struct task_struct *task;       /* lookup and create consistency */
 688         struct task_struct *cp_task;    /* separate cp/wb IO stats*/
 689         nid_t i_xattr_nid;              /* node id that contains xattrs */
 690         loff_t  last_disk_size;         /* lastly written file size */
 691 
 692 #ifdef CONFIG_QUOTA
 693         struct dquot *i_dquot[MAXQUOTAS];
 694 
 695         /* quota space reservation, managed internally by quota code */
 696         qsize_t i_reserved_quota;
 697 #endif
 698         struct list_head dirty_list;    /* dirty list for dirs and files */
 699         struct list_head gdirty_list;   /* linked in global dirty list */
 700         struct list_head inmem_ilist;   /* list for inmem inodes */
 701         struct list_head inmem_pages;   /* inmemory pages managed by f2fs */
 702         struct task_struct *inmem_task; /* store inmemory task */
 703         struct mutex inmem_lock;        /* lock for inmemory pages */
 704         struct extent_tree *extent_tree;        /* cached extent_tree entry */
 705 
 706         /* avoid racing between foreground op and gc */
 707         struct rw_semaphore i_gc_rwsem[2];
 708         struct rw_semaphore i_mmap_sem;
 709         struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
 710 
 711         int i_extra_isize;              /* size of extra space located in i_addr */
 712         kprojid_t i_projid;             /* id for project quota */
 713         int i_inline_xattr_size;        /* inline xattr size */
 714         struct timespec64 i_crtime;     /* inode creation time */
 715         struct timespec64 i_disk_time[4];/* inode disk times */
 716 };
 717 
 718 static inline void get_extent_info(struct extent_info *ext,
 719                                         struct f2fs_extent *i_ext)
 720 {
 721         ext->fofs = le32_to_cpu(i_ext->fofs);
 722         ext->blk = le32_to_cpu(i_ext->blk);
 723         ext->len = le32_to_cpu(i_ext->len);
 724 }
 725 
 726 static inline void set_raw_extent(struct extent_info *ext,
 727                                         struct f2fs_extent *i_ext)
 728 {
 729         i_ext->fofs = cpu_to_le32(ext->fofs);
 730         i_ext->blk = cpu_to_le32(ext->blk);
 731         i_ext->len = cpu_to_le32(ext->len);
 732 }
 733 
 734 static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
 735                                                 u32 blk, unsigned int len)
 736 {
 737         ei->fofs = fofs;
 738         ei->blk = blk;
 739         ei->len = len;
 740 }
 741 
 742 static inline bool __is_discard_mergeable(struct discard_info *back,
 743                         struct discard_info *front, unsigned int max_len)
 744 {
 745         return (back->lstart + back->len == front->lstart) &&
 746                 (back->len + front->len <= max_len);
 747 }
 748 
 749 static inline bool __is_discard_back_mergeable(struct discard_info *cur,
 750                         struct discard_info *back, unsigned int max_len)
 751 {
 752         return __is_discard_mergeable(back, cur, max_len);
 753 }
 754 
 755 static inline bool __is_discard_front_mergeable(struct discard_info *cur,
 756                         struct discard_info *front, unsigned int max_len)
 757 {
 758         return __is_discard_mergeable(cur, front, max_len);
 759 }
 760 
 761 static inline bool __is_extent_mergeable(struct extent_info *back,
 762                                                 struct extent_info *front)
 763 {
 764         return (back->fofs + back->len == front->fofs &&
 765                         back->blk + back->len == front->blk);
 766 }
 767 
 768 static inline bool __is_back_mergeable(struct extent_info *cur,
 769                                                 struct extent_info *back)
 770 {
 771         return __is_extent_mergeable(back, cur);
 772 }
 773 
 774 static inline bool __is_front_mergeable(struct extent_info *cur,
 775                                                 struct extent_info *front)
 776 {
 777         return __is_extent_mergeable(cur, front);
 778 }
 779 
 780 extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
 781 static inline void __try_update_largest_extent(struct extent_tree *et,
 782                                                 struct extent_node *en)
 783 {
 784         if (en->ei.len > et->largest.len) {
 785                 et->largest = en->ei;
 786                 et->largest_updated = true;
 787         }
 788 }
 789 
 790 /*
 791  * For free nid management
 792  */
 793 enum nid_state {
 794         FREE_NID,               /* newly added to free nid list */
 795         PREALLOC_NID,           /* it is preallocated */
 796         MAX_NID_STATE,
 797 };
 798 
 799 struct f2fs_nm_info {
 800         block_t nat_blkaddr;            /* base disk address of NAT */
 801         nid_t max_nid;                  /* maximum possible node ids */
 802         nid_t available_nids;           /* # of available node ids */
 803         nid_t next_scan_nid;            /* the next nid to be scanned */
 804         unsigned int ram_thresh;        /* control the memory footprint */
 805         unsigned int ra_nid_pages;      /* # of nid pages to be readaheaded */
 806         unsigned int dirty_nats_ratio;  /* control dirty nats ratio threshold */
 807 
 808         /* NAT cache management */
 809         struct radix_tree_root nat_root;/* root of the nat entry cache */
 810         struct radix_tree_root nat_set_root;/* root of the nat set cache */
 811         struct rw_semaphore nat_tree_lock;      /* protect nat_tree_lock */
 812         struct list_head nat_entries;   /* cached nat entry list (clean) */
 813         spinlock_t nat_list_lock;       /* protect clean nat entry list */
 814         unsigned int nat_cnt;           /* the # of cached nat entries */
 815         unsigned int dirty_nat_cnt;     /* total num of nat entries in set */
 816         unsigned int nat_blocks;        /* # of nat blocks */
 817 
 818         /* free node ids management */
 819         struct radix_tree_root free_nid_root;/* root of the free_nid cache */
 820         struct list_head free_nid_list;         /* list for free nids excluding preallocated nids */
 821         unsigned int nid_cnt[MAX_NID_STATE];    /* the number of free node id */
 822         spinlock_t nid_list_lock;       /* protect nid lists ops */
 823         struct mutex build_lock;        /* lock for build free nids */
 824         unsigned char **free_nid_bitmap;
 825         unsigned char *nat_block_bitmap;
 826         unsigned short *free_nid_count; /* free nid count of NAT block */
 827 
 828         /* for checkpoint */
 829         char *nat_bitmap;               /* NAT bitmap pointer */
 830 
 831         unsigned int nat_bits_blocks;   /* # of nat bits blocks */
 832         unsigned char *nat_bits;        /* NAT bits blocks */
 833         unsigned char *full_nat_bits;   /* full NAT pages */
 834         unsigned char *empty_nat_bits;  /* empty NAT pages */
 835 #ifdef CONFIG_F2FS_CHECK_FS
 836         char *nat_bitmap_mir;           /* NAT bitmap mirror */
 837 #endif
 838         int bitmap_size;                /* bitmap size */
 839 };
 840 
 841 /*
 842  * this structure is used as one of function parameters.
 843  * all the information are dedicated to a given direct node block determined
 844  * by the data offset in a file.
 845  */
 846 struct dnode_of_data {
 847         struct inode *inode;            /* vfs inode pointer */
 848         struct page *inode_page;        /* its inode page, NULL is possible */
 849         struct page *node_page;         /* cached direct node page */
 850         nid_t nid;                      /* node id of the direct node block */
 851         unsigned int ofs_in_node;       /* data offset in the node page */
 852         bool inode_page_locked;         /* inode page is locked or not */
 853         bool node_changed;              /* is node block changed */
 854         char cur_level;                 /* level of hole node page */
 855         char max_level;                 /* level of current page located */
 856         block_t data_blkaddr;           /* block address of the node block */
 857 };
 858 
 859 static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
 860                 struct page *ipage, struct page *npage, nid_t nid)
 861 {
 862         memset(dn, 0, sizeof(*dn));
 863         dn->inode = inode;
 864         dn->inode_page = ipage;
 865         dn->node_page = npage;
 866         dn->nid = nid;
 867 }
 868 
 869 /*
 870  * For SIT manager
 871  *
 872  * By default, there are 6 active log areas across the whole main area.
 873  * When considering hot and cold data separation to reduce cleaning overhead,
 874  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
 875  * respectively.
 876  * In the current design, you should not change the numbers intentionally.
 877  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
 878  * logs individually according to the underlying devices. (default: 6)
 879  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
 880  * data and 8 for node logs.
 881  */
 882 #define NR_CURSEG_DATA_TYPE     (3)
 883 #define NR_CURSEG_NODE_TYPE     (3)
 884 #define NR_CURSEG_TYPE  (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
 885 
 886 enum {
 887         CURSEG_HOT_DATA = 0,    /* directory entry blocks */
 888         CURSEG_WARM_DATA,       /* data blocks */
 889         CURSEG_COLD_DATA,       /* multimedia or GCed data blocks */
 890         CURSEG_HOT_NODE,        /* direct node blocks of directory files */
 891         CURSEG_WARM_NODE,       /* direct node blocks of normal files */
 892         CURSEG_COLD_NODE,       /* indirect node blocks */
 893         NO_CHECK_TYPE,
 894 };
 895 
 896 struct flush_cmd {
 897         struct completion wait;
 898         struct llist_node llnode;
 899         nid_t ino;
 900         int ret;
 901 };
 902 
 903 struct flush_cmd_control {
 904         struct task_struct *f2fs_issue_flush;   /* flush thread */
 905         wait_queue_head_t flush_wait_queue;     /* waiting queue for wake-up */
 906         atomic_t issued_flush;                  /* # of issued flushes */
 907         atomic_t queued_flush;                  /* # of queued flushes */
 908         struct llist_head issue_list;           /* list for command issue */
 909         struct llist_node *dispatch_list;       /* list for command dispatch */
 910 };
 911 
 912 struct f2fs_sm_info {
 913         struct sit_info *sit_info;              /* whole segment information */
 914         struct free_segmap_info *free_info;     /* free segment information */
 915         struct dirty_seglist_info *dirty_info;  /* dirty segment information */
 916         struct curseg_info *curseg_array;       /* active segment information */
 917 
 918         struct rw_semaphore curseg_lock;        /* for preventing curseg change */
 919 
 920         block_t seg0_blkaddr;           /* block address of 0'th segment */
 921         block_t main_blkaddr;           /* start block address of main area */
 922         block_t ssa_blkaddr;            /* start block address of SSA area */
 923 
 924         unsigned int segment_count;     /* total # of segments */
 925         unsigned int main_segments;     /* # of segments in main area */
 926         unsigned int reserved_segments; /* # of reserved segments */
 927         unsigned int ovp_segments;      /* # of overprovision segments */
 928 
 929         /* a threshold to reclaim prefree segments */
 930         unsigned int rec_prefree_segments;
 931 
 932         /* for batched trimming */
 933         unsigned int trim_sections;             /* # of sections to trim */
 934 
 935         struct list_head sit_entry_set; /* sit entry set list */
 936 
 937         unsigned int ipu_policy;        /* in-place-update policy */
 938         unsigned int min_ipu_util;      /* in-place-update threshold */
 939         unsigned int min_fsync_blocks;  /* threshold for fsync */
 940         unsigned int min_seq_blocks;    /* threshold for sequential blocks */
 941         unsigned int min_hot_blocks;    /* threshold for hot block allocation */
 942         unsigned int min_ssr_sections;  /* threshold to trigger SSR allocation */
 943 
 944         /* for flush command control */
 945         struct flush_cmd_control *fcc_info;
 946 
 947         /* for discard command control */
 948         struct discard_cmd_control *dcc_info;
 949 };
 950 
 951 /*
 952  * For superblock
 953  */
 954 /*
 955  * COUNT_TYPE for monitoring
 956  *
 957  * f2fs monitors the number of several block types such as on-writeback,
 958  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
 959  */
 960 #define WB_DATA_TYPE(p) (__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
 961 enum count_type {
 962         F2FS_DIRTY_DENTS,
 963         F2FS_DIRTY_DATA,
 964         F2FS_DIRTY_QDATA,
 965         F2FS_DIRTY_NODES,
 966         F2FS_DIRTY_META,
 967         F2FS_INMEM_PAGES,
 968         F2FS_DIRTY_IMETA,
 969         F2FS_WB_CP_DATA,
 970         F2FS_WB_DATA,
 971         F2FS_RD_DATA,
 972         F2FS_RD_NODE,
 973         F2FS_RD_META,
 974         F2FS_DIO_WRITE,
 975         F2FS_DIO_READ,
 976         NR_COUNT_TYPE,
 977 };
 978 
 979 /*
 980  * The below are the page types of bios used in submit_bio().
 981  * The available types are:
 982  * DATA                 User data pages. It operates as async mode.
 983  * NODE                 Node pages. It operates as async mode.
 984  * META                 FS metadata pages such as SIT, NAT, CP.
 985  * NR_PAGE_TYPE         The number of page types.
 986  * META_FLUSH           Make sure the previous pages are written
 987  *                      with waiting the bio's completion
 988  * ...                  Only can be used with META.
 989  */
 990 #define PAGE_TYPE_OF_BIO(type)  ((type) > META ? META : (type))
 991 enum page_type {
 992         DATA,
 993         NODE,
 994         META,
 995         NR_PAGE_TYPE,
 996         META_FLUSH,
 997         INMEM,          /* the below types are used by tracepoints only. */
 998         INMEM_DROP,
 999         INMEM_INVALIDATE,
1000         INMEM_REVOKE,
1001         IPU,
1002         OPU,
1003 };
1004 
1005 enum temp_type {
1006         HOT = 0,        /* must be zero for meta bio */
1007         WARM,
1008         COLD,
1009         NR_TEMP_TYPE,
1010 };
1011 
1012 enum need_lock_type {
1013         LOCK_REQ = 0,
1014         LOCK_DONE,
1015         LOCK_RETRY,
1016 };
1017 
1018 enum cp_reason_type {
1019         CP_NO_NEEDED,
1020         CP_NON_REGULAR,
1021         CP_HARDLINK,
1022         CP_SB_NEED_CP,
1023         CP_WRONG_PINO,
1024         CP_NO_SPC_ROLL,
1025         CP_NODE_NEED_CP,
1026         CP_FASTBOOT_MODE,
1027         CP_SPEC_LOG_NUM,
1028         CP_RECOVER_DIR,
1029 };
1030 
1031 enum iostat_type {
1032         APP_DIRECT_IO,                  /* app direct IOs */
1033         APP_BUFFERED_IO,                /* app buffered IOs */
1034         APP_WRITE_IO,                   /* app write IOs */
1035         APP_MAPPED_IO,                  /* app mapped IOs */
1036         FS_DATA_IO,                     /* data IOs from kworker/fsync/reclaimer */
1037         FS_NODE_IO,                     /* node IOs from kworker/fsync/reclaimer */
1038         FS_META_IO,                     /* meta IOs from kworker/reclaimer */
1039         FS_GC_DATA_IO,                  /* data IOs from forground gc */
1040         FS_GC_NODE_IO,                  /* node IOs from forground gc */
1041         FS_CP_DATA_IO,                  /* data IOs from checkpoint */
1042         FS_CP_NODE_IO,                  /* node IOs from checkpoint */
1043         FS_CP_META_IO,                  /* meta IOs from checkpoint */
1044         FS_DISCARD,                     /* discard */
1045         NR_IO_TYPE,
1046 };
1047 
1048 struct f2fs_io_info {
1049         struct f2fs_sb_info *sbi;       /* f2fs_sb_info pointer */
1050         nid_t ino;              /* inode number */
1051         enum page_type type;    /* contains DATA/NODE/META/META_FLUSH */
1052         enum temp_type temp;    /* contains HOT/WARM/COLD */
1053         int op;                 /* contains REQ_OP_ */
1054         int op_flags;           /* req_flag_bits */
1055         block_t new_blkaddr;    /* new block address to be written */
1056         block_t old_blkaddr;    /* old block address before Cow */
1057         struct page *page;      /* page to be written */
1058         struct page *encrypted_page;    /* encrypted page */
1059         struct list_head list;          /* serialize IOs */
1060         bool submitted;         /* indicate IO submission */
1061         int need_lock;          /* indicate we need to lock cp_rwsem */
1062         bool in_list;           /* indicate fio is in io_list */
1063         bool is_por;            /* indicate IO is from recovery or not */
1064         bool retry;             /* need to reallocate block address */
1065         enum iostat_type io_type;       /* io type */
1066         struct writeback_control *io_wbc; /* writeback control */
1067         struct bio **bio;               /* bio for ipu */
1068         sector_t *last_block;           /* last block number in bio */
1069         unsigned char version;          /* version of the node */
1070 };
1071 
1072 #define is_read_io(rw) ((rw) == READ)
1073 struct f2fs_bio_info {
1074         struct f2fs_sb_info *sbi;       /* f2fs superblock */
1075         struct bio *bio;                /* bios to merge */
1076         sector_t last_block_in_bio;     /* last block number */
1077         struct f2fs_io_info fio;        /* store buffered io info. */
1078         struct rw_semaphore io_rwsem;   /* blocking op for bio */
1079         spinlock_t io_lock;             /* serialize DATA/NODE IOs */
1080         struct list_head io_list;       /* track fios */
1081 };
1082 
1083 #define FDEV(i)                         (sbi->devs[i])
1084 #define RDEV(i)                         (raw_super->devs[i])
1085 struct f2fs_dev_info {
1086         struct block_device *bdev;
1087         char path[MAX_PATH_LEN];
1088         unsigned int total_segments;
1089         block_t start_blk;
1090         block_t end_blk;
1091 #ifdef CONFIG_BLK_DEV_ZONED
1092         unsigned int nr_blkz;           /* Total number of zones */
1093         unsigned long *blkz_seq;        /* Bitmap indicating sequential zones */
1094 #endif
1095 };
1096 
1097 enum inode_type {
1098         DIR_INODE,                      /* for dirty dir inode */
1099         FILE_INODE,                     /* for dirty regular/symlink inode */
1100         DIRTY_META,                     /* for all dirtied inode metadata */
1101         ATOMIC_FILE,                    /* for all atomic files */
1102         NR_INODE_TYPE,
1103 };
1104 
1105 /* for inner inode cache management */
1106 struct inode_management {
1107         struct radix_tree_root ino_root;        /* ino entry array */
1108         spinlock_t ino_lock;                    /* for ino entry lock */
1109         struct list_head ino_list;              /* inode list head */
1110         unsigned long ino_num;                  /* number of entries */
1111 };
1112 
1113 /* For s_flag in struct f2fs_sb_info */
1114 enum {
1115         SBI_IS_DIRTY,                           /* dirty flag for checkpoint */
1116         SBI_IS_CLOSE,                           /* specify unmounting */
1117         SBI_NEED_FSCK,                          /* need fsck.f2fs to fix */
1118         SBI_POR_DOING,                          /* recovery is doing or not */
1119         SBI_NEED_SB_WRITE,                      /* need to recover superblock */
1120         SBI_NEED_CP,                            /* need to checkpoint */
1121         SBI_IS_SHUTDOWN,                        /* shutdown by ioctl */
1122         SBI_IS_RECOVERED,                       /* recovered orphan/data */
1123         SBI_CP_DISABLED,                        /* CP was disabled last mount */
1124         SBI_CP_DISABLED_QUICK,                  /* CP was disabled quickly */
1125         SBI_QUOTA_NEED_FLUSH,                   /* need to flush quota info in CP */
1126         SBI_QUOTA_SKIP_FLUSH,                   /* skip flushing quota in current CP */
1127         SBI_QUOTA_NEED_REPAIR,                  /* quota file may be corrupted */
1128         SBI_IS_RESIZEFS,                        /* resizefs is in process */
1129 };
1130 
1131 enum {
1132         CP_TIME,
1133         REQ_TIME,
1134         DISCARD_TIME,
1135         GC_TIME,
1136         DISABLE_TIME,
1137         UMOUNT_DISCARD_TIMEOUT,
1138         MAX_TIME,
1139 };
1140 
1141 enum {
1142         GC_NORMAL,
1143         GC_IDLE_CB,
1144         GC_IDLE_GREEDY,
1145         GC_URGENT,
1146 };
1147 
1148 enum {
1149         WHINT_MODE_OFF,         /* not pass down write hints */
1150         WHINT_MODE_USER,        /* try to pass down hints given by users */
1151         WHINT_MODE_FS,          /* pass down hints with F2FS policy */
1152 };
1153 
1154 enum {
1155         ALLOC_MODE_DEFAULT,     /* stay default */
1156         ALLOC_MODE_REUSE,       /* reuse segments as much as possible */
1157 };
1158 
1159 enum fsync_mode {
1160         FSYNC_MODE_POSIX,       /* fsync follows posix semantics */
1161         FSYNC_MODE_STRICT,      /* fsync behaves in line with ext4 */
1162         FSYNC_MODE_NOBARRIER,   /* fsync behaves nobarrier based on posix */
1163 };
1164 
1165 #ifdef CONFIG_FS_ENCRYPTION
1166 #define DUMMY_ENCRYPTION_ENABLED(sbi) \
1167                         (unlikely(F2FS_OPTION(sbi).test_dummy_encryption))
1168 #else
1169 #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
1170 #endif
1171 
1172 struct f2fs_sb_info {
1173         struct super_block *sb;                 /* pointer to VFS super block */
1174         struct proc_dir_entry *s_proc;          /* proc entry */
1175         struct f2fs_super_block *raw_super;     /* raw super block pointer */
1176         struct rw_semaphore sb_lock;            /* lock for raw super block */
1177         int valid_super_block;                  /* valid super block no */
1178         unsigned long s_flag;                           /* flags for sbi */
1179         struct mutex writepages;                /* mutex for writepages() */
1180 #ifdef CONFIG_UNICODE
1181         struct unicode_map *s_encoding;
1182         __u16 s_encoding_flags;
1183 #endif
1184 
1185 #ifdef CONFIG_BLK_DEV_ZONED
1186         unsigned int blocks_per_blkz;           /* F2FS blocks per zone */
1187         unsigned int log_blocks_per_blkz;       /* log2 F2FS blocks per zone */
1188 #endif
1189 
1190         /* for node-related operations */
1191         struct f2fs_nm_info *nm_info;           /* node manager */
1192         struct inode *node_inode;               /* cache node blocks */
1193 
1194         /* for segment-related operations */
1195         struct f2fs_sm_info *sm_info;           /* segment manager */
1196 
1197         /* for bio operations */
1198         struct f2fs_bio_info *write_io[NR_PAGE_TYPE];   /* for write bios */
1199         /* keep migration IO order for LFS mode */
1200         struct rw_semaphore io_order_lock;
1201         mempool_t *write_io_dummy;              /* Dummy pages */
1202 
1203         /* for checkpoint */
1204         struct f2fs_checkpoint *ckpt;           /* raw checkpoint pointer */
1205         int cur_cp_pack;                        /* remain current cp pack */
1206         spinlock_t cp_lock;                     /* for flag in ckpt */
1207         struct inode *meta_inode;               /* cache meta blocks */
1208         struct mutex cp_mutex;                  /* checkpoint procedure lock */
1209         struct rw_semaphore cp_rwsem;           /* blocking FS operations */
1210         struct rw_semaphore node_write;         /* locking node writes */
1211         struct rw_semaphore node_change;        /* locking node change */
1212         wait_queue_head_t cp_wait;
1213         unsigned long last_time[MAX_TIME];      /* to store time in jiffies */
1214         long interval_time[MAX_TIME];           /* to store thresholds */
1215 
1216         struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
1217 
1218         spinlock_t fsync_node_lock;             /* for node entry lock */
1219         struct list_head fsync_node_list;       /* node list head */
1220         unsigned int fsync_seg_id;              /* sequence id */
1221         unsigned int fsync_node_num;            /* number of node entries */
1222 
1223         /* for orphan inode, use 0'th array */
1224         unsigned int max_orphans;               /* max orphan inodes */
1225 
1226         /* for inode management */
1227         struct list_head inode_list[NR_INODE_TYPE];     /* dirty inode list */
1228         spinlock_t inode_lock[NR_INODE_TYPE];   /* for dirty inode list lock */
1229         struct mutex flush_lock;                /* for flush exclusion */
1230 
1231         /* for extent tree cache */
1232         struct radix_tree_root extent_tree_root;/* cache extent cache entries */
1233         struct mutex extent_tree_lock;  /* locking extent radix tree */
1234         struct list_head extent_list;           /* lru list for shrinker */
1235         spinlock_t extent_lock;                 /* locking extent lru list */
1236         atomic_t total_ext_tree;                /* extent tree count */
1237         struct list_head zombie_list;           /* extent zombie tree list */
1238         atomic_t total_zombie_tree;             /* extent zombie tree count */
1239         atomic_t total_ext_node;                /* extent info count */
1240 
1241         /* basic filesystem units */
1242         unsigned int log_sectors_per_block;     /* log2 sectors per block */
1243         unsigned int log_blocksize;             /* log2 block size */
1244         unsigned int blocksize;                 /* block size */
1245         unsigned int root_ino_num;              /* root inode number*/
1246         unsigned int node_ino_num;              /* node inode number*/
1247         unsigned int meta_ino_num;              /* meta inode number*/
1248         unsigned int log_blocks_per_seg;        /* log2 blocks per segment */
1249         unsigned int blocks_per_seg;            /* blocks per segment */
1250         unsigned int segs_per_sec;              /* segments per section */
1251         unsigned int secs_per_zone;             /* sections per zone */
1252         unsigned int total_sections;            /* total section count */
1253         struct mutex resize_mutex;              /* for resize exclusion */
1254         unsigned int total_node_count;          /* total node block count */
1255         unsigned int total_valid_node_count;    /* valid node block count */
1256         loff_t max_file_blocks;                 /* max block index of file */
1257         int dir_level;                          /* directory level */
1258         int readdir_ra;                         /* readahead inode in readdir */
1259 
1260         block_t user_block_count;               /* # of user blocks */
1261         block_t total_valid_block_count;        /* # of valid blocks */
1262         block_t discard_blks;                   /* discard command candidats */
1263         block_t last_valid_block_count;         /* for recovery */
1264         block_t reserved_blocks;                /* configurable reserved blocks */
1265         block_t current_reserved_blocks;        /* current reserved blocks */
1266 
1267         /* Additional tracking for no checkpoint mode */
1268         block_t unusable_block_count;           /* # of blocks saved by last cp */
1269 
1270         unsigned int nquota_files;              /* # of quota sysfile */
1271         struct rw_semaphore quota_sem;          /* blocking cp for flags */
1272 
1273         /* # of pages, see count_type */
1274         atomic_t nr_pages[NR_COUNT_TYPE];
1275         /* # of allocated blocks */
1276         struct percpu_counter alloc_valid_block_count;
1277 
1278         /* writeback control */
1279         atomic_t wb_sync_req[META];     /* count # of WB_SYNC threads */
1280 
1281         /* valid inode count */
1282         struct percpu_counter total_valid_inode_count;
1283 
1284         struct f2fs_mount_info mount_opt;       /* mount options */
1285 
1286         /* for cleaning operations */
1287         struct mutex gc_mutex;                  /* mutex for GC */
1288         struct f2fs_gc_kthread  *gc_thread;     /* GC thread */
1289         unsigned int cur_victim_sec;            /* current victim section num */
1290         unsigned int gc_mode;                   /* current GC state */
1291         unsigned int next_victim_seg[2];        /* next segment in victim section */
1292         /* for skip statistic */
1293         unsigned int atomic_files;              /* # of opened atomic file */
1294         unsigned long long skipped_atomic_files[2];     /* FG_GC and BG_GC */
1295         unsigned long long skipped_gc_rwsem;            /* FG_GC only */
1296 
1297         /* threshold for gc trials on pinned files */
1298         u64 gc_pin_file_threshold;
1299 
1300         /* maximum # of trials to find a victim segment for SSR and GC */
1301         unsigned int max_victim_search;
1302         /* migration granularity of garbage collection, unit: segment */
1303         unsigned int migration_granularity;
1304 
1305         /*
1306          * for stat information.
1307          * one is for the LFS mode, and the other is for the SSR mode.
1308          */
1309 #ifdef CONFIG_F2FS_STAT_FS
1310         struct f2fs_stat_info *stat_info;       /* FS status information */
1311         atomic_t meta_count[META_MAX];          /* # of meta blocks */
1312         unsigned int segment_count[2];          /* # of allocated segments */
1313         unsigned int block_count[2];            /* # of allocated blocks */
1314         atomic_t inplace_count;         /* # of inplace update */
1315         atomic64_t total_hit_ext;               /* # of lookup extent cache */
1316         atomic64_t read_hit_rbtree;             /* # of hit rbtree extent node */
1317         atomic64_t read_hit_largest;            /* # of hit largest extent node */
1318         atomic64_t read_hit_cached;             /* # of hit cached extent node */
1319         atomic_t inline_xattr;                  /* # of inline_xattr inodes */
1320         atomic_t inline_inode;                  /* # of inline_data inodes */
1321         atomic_t inline_dir;                    /* # of inline_dentry inodes */
1322         atomic_t aw_cnt;                        /* # of atomic writes */
1323         atomic_t vw_cnt;                        /* # of volatile writes */
1324         atomic_t max_aw_cnt;                    /* max # of atomic writes */
1325         atomic_t max_vw_cnt;                    /* max # of volatile writes */
1326         int bg_gc;                              /* background gc calls */
1327         unsigned int io_skip_bggc;              /* skip background gc for in-flight IO */
1328         unsigned int other_skip_bggc;           /* skip background gc for other reasons */
1329         unsigned int ndirty_inode[NR_INODE_TYPE];       /* # of dirty inodes */
1330 #endif
1331         spinlock_t stat_lock;                   /* lock for stat operations */
1332 
1333         /* For app/fs IO statistics */
1334         spinlock_t iostat_lock;
1335         unsigned long long write_iostat[NR_IO_TYPE];
1336         bool iostat_enable;
1337 
1338         /* For sysfs suppport */
1339         struct kobject s_kobj;
1340         struct completion s_kobj_unregister;
1341 
1342         /* For shrinker support */
1343         struct list_head s_list;
1344         int s_ndevs;                            /* number of devices */
1345         struct f2fs_dev_info *devs;             /* for device list */
1346         unsigned int dirty_device;              /* for checkpoint data flush */
1347         spinlock_t dev_lock;                    /* protect dirty_device */
1348         struct mutex umount_mutex;
1349         unsigned int shrinker_run_no;
1350 
1351         /* For write statistics */
1352         u64 sectors_written_start;
1353         u64 kbytes_written;
1354 
1355         /* Reference to checksum algorithm driver via cryptoapi */
1356         struct crypto_shash *s_chksum_driver;
1357 
1358         /* Precomputed FS UUID checksum for seeding other checksums */
1359         __u32 s_chksum_seed;
1360 };
1361 
1362 struct f2fs_private_dio {
1363         struct inode *inode;
1364         void *orig_private;
1365         bio_end_io_t *orig_end_io;
1366         bool write;
1367 };
1368 
1369 #ifdef CONFIG_F2FS_FAULT_INJECTION
1370 #define f2fs_show_injection_info(type)                                  \
1371         printk_ratelimited("%sF2FS-fs : inject %s in %s of %pS\n",      \
1372                 KERN_INFO, f2fs_fault_name[type],                       \
1373                 __func__, __builtin_return_address(0))
1374 static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
1375 {
1376         struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
1377 
1378         if (!ffi->inject_rate)
1379                 return false;
1380 
1381         if (!IS_FAULT_SET(ffi, type))
1382                 return false;
1383 
1384         atomic_inc(&ffi->inject_ops);
1385         if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
1386                 atomic_set(&ffi->inject_ops, 0);
1387                 return true;
1388         }
1389         return false;
1390 }
1391 #else
1392 #define f2fs_show_injection_info(type) do { } while (0)
1393 static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
1394 {
1395         return false;
1396 }
1397 #endif
1398 
1399 /*
1400  * Test if the mounted volume is a multi-device volume.
1401  *   - For a single regular disk volume, sbi->s_ndevs is 0.
1402  *   - For a single zoned disk volume, sbi->s_ndevs is 1.
1403  *   - For a multi-device volume, sbi->s_ndevs is always 2 or more.
1404  */
1405 static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
1406 {
1407         return sbi->s_ndevs > 1;
1408 }
1409 
1410 /* For write statistics. Suppose sector size is 512 bytes,
1411  * and the return value is in kbytes. s is of struct f2fs_sb_info.
1412  */
1413 #define BD_PART_WRITTEN(s)                                               \
1414 (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[STAT_WRITE]) -   \
1415                 (s)->sectors_written_start) >> 1)
1416 
1417 static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
1418 {
1419         unsigned long now = jiffies;
1420 
1421         sbi->last_time[type] = now;
1422 
1423         /* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1424         if (type == REQ_TIME) {
1425                 sbi->last_time[DISCARD_TIME] = now;
1426                 sbi->last_time[GC_TIME] = now;
1427         }
1428 }
1429 
1430 static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
1431 {
1432         unsigned long interval = sbi->interval_time[type] * HZ;
1433 
1434         return time_after(jiffies, sbi->last_time[type] + interval);
1435 }
1436 
1437 static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1438                                                 int type)
1439 {
1440         unsigned long interval = sbi->interval_time[type] * HZ;
1441         unsigned int wait_ms = 0;
1442         long delta;
1443 
1444         delta = (sbi->last_time[type] + interval) - jiffies;
1445         if (delta > 0)
1446                 wait_ms = jiffies_to_msecs(delta);
1447 
1448         return wait_ms;
1449 }
1450 
1451 /*
1452  * Inline functions
1453  */
1454 static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1455                               const void *address, unsigned int length)
1456 {
1457         struct {
1458                 struct shash_desc shash;
1459                 char ctx[4];
1460         } desc;
1461         int err;
1462 
1463         BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1464 
1465         desc.shash.tfm = sbi->s_chksum_driver;
1466         *(u32 *)desc.ctx = crc;
1467 
1468         err = crypto_shash_update(&desc.shash, address, length);
1469         BUG_ON(err);
1470 
1471         return *(u32 *)desc.ctx;
1472 }
1473 
1474 static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1475                            unsigned int length)
1476 {
1477         return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1478 }
1479 
1480 static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1481                                   void *buf, size_t buf_size)
1482 {
1483         return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1484 }
1485 
1486 static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1487                               const void *address, unsigned int length)
1488 {
1489         return __f2fs_crc32(sbi, crc, address, length);
1490 }
1491 
1492 static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
1493 {
1494         return container_of(inode, struct f2fs_inode_info, vfs_inode);
1495 }
1496 
1497 static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
1498 {
1499         return sb->s_fs_info;
1500 }
1501 
1502 static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
1503 {
1504         return F2FS_SB(inode->i_sb);
1505 }
1506 
1507 static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
1508 {
1509         return F2FS_I_SB(mapping->host);
1510 }
1511 
1512 static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
1513 {
1514         return F2FS_M_SB(page_file_mapping(page));
1515 }
1516 
1517 static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
1518 {
1519         return (struct f2fs_super_block *)(sbi->raw_super);
1520 }
1521 
1522 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
1523 {
1524         return (struct f2fs_checkpoint *)(sbi->ckpt);
1525 }
1526 
1527 static inline struct f2fs_node *F2FS_NODE(struct page *page)
1528 {
1529         return (struct f2fs_node *)page_address(page);
1530 }
1531 
1532 static inline struct f2fs_inode *F2FS_INODE(struct page *page)
1533 {
1534         return &((struct f2fs_node *)page_address(page))->i;
1535 }
1536 
1537 static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
1538 {
1539         return (struct f2fs_nm_info *)(sbi->nm_info);
1540 }
1541 
1542 static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
1543 {
1544         return (struct f2fs_sm_info *)(sbi->sm_info);
1545 }
1546 
1547 static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
1548 {
1549         return (struct sit_info *)(SM_I(sbi)->sit_info);
1550 }
1551 
1552 static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
1553 {
1554         return (struct free_segmap_info *)(SM_I(sbi)->free_info);
1555 }
1556 
1557 static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
1558 {
1559         return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
1560 }
1561 
1562 static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
1563 {
1564         return sbi->meta_inode->i_mapping;
1565 }
1566 
1567 static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
1568 {
1569         return sbi->node_inode->i_mapping;
1570 }
1571 
1572 static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
1573 {
1574         return test_bit(type, &sbi->s_flag);
1575 }
1576 
1577 static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1578 {
1579         set_bit(type, &sbi->s_flag);
1580 }
1581 
1582 static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1583 {
1584         clear_bit(type, &sbi->s_flag);
1585 }
1586 
1587 static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1588 {
1589         return le64_to_cpu(cp->checkpoint_ver);
1590 }
1591 
1592 static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1593 {
1594         if (type < F2FS_MAX_QUOTAS)
1595                 return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1596         return 0;
1597 }
1598 
1599 static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1600 {
1601         size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1602         return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1603 }
1604 
1605 static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1606 {
1607         unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1608 
1609         return ckpt_flags & f;
1610 }
1611 
1612 static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1613 {
1614         return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1615 }
1616 
1617 static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1618 {
1619         unsigned int ckpt_flags;
1620 
1621         ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1622         ckpt_flags |= f;
1623         cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1624 }
1625 
1626 static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1627 {
1628         unsigned long flags;
1629 
1630         spin_lock_irqsave(&sbi->cp_lock, flags);
1631         __set_ckpt_flags(F2FS_CKPT(sbi), f);
1632         spin_unlock_irqrestore(&sbi->cp_lock, flags);
1633 }
1634 
1635 static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1636 {
1637         unsigned int ckpt_flags;
1638 
1639         ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1640         ckpt_flags &= (~f);
1641         cp->ckpt_flags = cpu_to_le32(ckpt_flags);
1642 }
1643 
1644 static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1645 {
1646         unsigned long flags;
1647 
1648         spin_lock_irqsave(&sbi->cp_lock, flags);
1649         __clear_ckpt_flags(F2FS_CKPT(sbi), f);
1650         spin_unlock_irqrestore(&sbi->cp_lock, flags);
1651 }
1652 
1653 static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
1654 {
1655         unsigned long flags;
1656         unsigned char *nat_bits;
1657 
1658         /*
1659          * In order to re-enable nat_bits we need to call fsck.f2fs by
1660          * set_sbi_flag(sbi, SBI_NEED_FSCK). But it may give huge cost,
1661          * so let's rely on regular fsck or unclean shutdown.
1662          */
1663 
1664         if (lock)
1665                 spin_lock_irqsave(&sbi->cp_lock, flags);
1666         __clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
1667         nat_bits = NM_I(sbi)->nat_bits;
1668         NM_I(sbi)->nat_bits = NULL;
1669         if (lock)
1670                 spin_unlock_irqrestore(&sbi->cp_lock, flags);
1671 
1672         kvfree(nat_bits);
1673 }
1674 
1675 static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
1676                                         struct cp_control *cpc)
1677 {
1678         bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
1679 
1680         return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
1681 }
1682 
1683 static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
1684 {
1685         down_read(&sbi->cp_rwsem);
1686 }
1687 
1688 static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1689 {
1690         return down_read_trylock(&sbi->cp_rwsem);
1691 }
1692 
1693 static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
1694 {
1695         up_read(&sbi->cp_rwsem);
1696 }
1697 
1698 static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
1699 {
1700         down_write(&sbi->cp_rwsem);
1701 }
1702 
1703 static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
1704 {
1705         up_write(&sbi->cp_rwsem);
1706 }
1707 
1708 static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1709 {
1710         int reason = CP_SYNC;
1711 
1712         if (test_opt(sbi, FASTBOOT))
1713                 reason = CP_FASTBOOT;
1714         if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1715                 reason = CP_UMOUNT;
1716         return reason;
1717 }
1718 
1719 static inline bool __remain_node_summaries(int reason)
1720 {
1721         return (reason & (CP_UMOUNT | CP_FASTBOOT));
1722 }
1723 
1724 static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1725 {
1726         return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1727                         is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1728 }
1729 
1730 /*
1731  * Check whether the inode has blocks or not
1732  */
1733 static inline int F2FS_HAS_BLOCKS(struct inode *inode)
1734 {
1735         block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
1736 
1737         return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
1738 }
1739 
1740 static inline bool f2fs_has_xattr_block(unsigned int ofs)
1741 {
1742         return ofs == XATTR_NODE_OFFSET;
1743 }
1744 
1745 static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1746                                         struct inode *inode, bool cap)
1747 {
1748         if (!inode)
1749                 return true;
1750         if (!test_opt(sbi, RESERVE_ROOT))
1751                 return false;
1752         if (IS_NOQUOTA(inode))
1753                 return true;
1754         if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
1755                 return true;
1756         if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
1757                                         in_group_p(F2FS_OPTION(sbi).s_resgid))
1758                 return true;
1759         if (cap && capable(CAP_SYS_RESOURCE))
1760                 return true;
1761         return false;
1762 }
1763 
1764 static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
1765 static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
1766                                  struct inode *inode, blkcnt_t *count)
1767 {
1768         blkcnt_t diff = 0, release = 0;
1769         block_t avail_user_block_count;
1770         int ret;
1771 
1772         ret = dquot_reserve_block(inode, *count);
1773         if (ret)
1774                 return ret;
1775 
1776         if (time_to_inject(sbi, FAULT_BLOCK)) {
1777                 f2fs_show_injection_info(FAULT_BLOCK);
1778                 release = *count;
1779                 goto release_quota;
1780         }
1781 
1782         /*
1783          * let's increase this in prior to actual block count change in order
1784          * for f2fs_sync_file to avoid data races when deciding checkpoint.
1785          */
1786         percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1787 
1788         spin_lock(&sbi->stat_lock);
1789         sbi->total_valid_block_count += (block_t)(*count);
1790         avail_user_block_count = sbi->user_block_count -
1791                                         sbi->current_reserved_blocks;
1792 
1793         if (!__allow_reserved_blocks(sbi, inode, true))
1794                 avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
1795         if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1796                 if (avail_user_block_count > sbi->unusable_block_count)
1797                         avail_user_block_count -= sbi->unusable_block_count;
1798                 else
1799                         avail_user_block_count = 0;
1800         }
1801         if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1802                 diff = sbi->total_valid_block_count - avail_user_block_count;
1803                 if (diff > *count)
1804                         diff = *count;
1805                 *count -= diff;
1806                 release = diff;
1807                 sbi->total_valid_block_count -= diff;
1808                 if (!*count) {
1809                         spin_unlock(&sbi->stat_lock);
1810                         goto enospc;
1811                 }
1812         }
1813         spin_unlock(&sbi->stat_lock);
1814 
1815         if (unlikely(release)) {
1816                 percpu_counter_sub(&sbi->alloc_valid_block_count, release);
1817                 dquot_release_reservation_block(inode, release);
1818         }
1819         f2fs_i_blocks_write(inode, *count, true, true);
1820         return 0;
1821 
1822 enospc:
1823         percpu_counter_sub(&sbi->alloc_valid_block_count, release);
1824 release_quota:
1825         dquot_release_reservation_block(inode, release);
1826         return -ENOSPC;
1827 }
1828 
1829 __printf(2, 3)
1830 void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...);
1831 
1832 #define f2fs_err(sbi, fmt, ...)                                         \
1833         f2fs_printk(sbi, KERN_ERR fmt, ##__VA_ARGS__)
1834 #define f2fs_warn(sbi, fmt, ...)                                        \
1835         f2fs_printk(sbi, KERN_WARNING fmt, ##__VA_ARGS__)
1836 #define f2fs_notice(sbi, fmt, ...)                                      \
1837         f2fs_printk(sbi, KERN_NOTICE fmt, ##__VA_ARGS__)
1838 #define f2fs_info(sbi, fmt, ...)                                        \
1839         f2fs_printk(sbi, KERN_INFO fmt, ##__VA_ARGS__)
1840 #define f2fs_debug(sbi, fmt, ...)                                       \
1841         f2fs_printk(sbi, KERN_DEBUG fmt, ##__VA_ARGS__)
1842 
1843 static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
1844                                                 struct inode *inode,
1845                                                 block_t count)
1846 {
1847         blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
1848 
1849         spin_lock(&sbi->stat_lock);
1850         f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
1851         sbi->total_valid_block_count -= (block_t)count;
1852         if (sbi->reserved_blocks &&
1853                 sbi->current_reserved_blocks < sbi->reserved_blocks)
1854                 sbi->current_reserved_blocks = min(sbi->reserved_blocks,
1855                                         sbi->current_reserved_blocks + count);
1856         spin_unlock(&sbi->stat_lock);
1857         if (unlikely(inode->i_blocks < sectors)) {
1858                 f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
1859                           inode->i_ino,
1860                           (unsigned long long)inode->i_blocks,
1861                           (unsigned long long)sectors);
1862                 set_sbi_flag(sbi, SBI_NEED_FSCK);
1863                 return;
1864         }
1865         f2fs_i_blocks_write(inode, count, false, true);
1866 }
1867 
1868 static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
1869 {
1870         atomic_inc(&sbi->nr_pages[count_type]);
1871 
1872         if (count_type == F2FS_DIRTY_DENTS ||
1873                         count_type == F2FS_DIRTY_NODES ||
1874                         count_type == F2FS_DIRTY_META ||
1875                         count_type == F2FS_DIRTY_QDATA ||
1876                         count_type == F2FS_DIRTY_IMETA)
1877                 set_sbi_flag(sbi, SBI_IS_DIRTY);
1878 }
1879 
1880 static inline void inode_inc_dirty_pages(struct inode *inode)
1881 {
1882         atomic_inc(&F2FS_I(inode)->dirty_pages);
1883         inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1884                                 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
1885         if (IS_NOQUOTA(inode))
1886                 inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
1887 }
1888 
1889 static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
1890 {
1891         atomic_dec(&sbi->nr_pages[count_type]);
1892 }
1893 
1894 static inline void inode_dec_dirty_pages(struct inode *inode)
1895 {
1896         if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1897                         !S_ISLNK(inode->i_mode))
1898                 return;
1899 
1900         atomic_dec(&F2FS_I(inode)->dirty_pages);
1901         dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1902                                 F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
1903         if (IS_NOQUOTA(inode))
1904                 dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
1905 }
1906 
1907 static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
1908 {
1909         return atomic_read(&sbi->nr_pages[count_type]);
1910 }
1911 
1912 static inline int get_dirty_pages(struct inode *inode)
1913 {
1914         return atomic_read(&F2FS_I(inode)->dirty_pages);
1915 }
1916 
1917 static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
1918 {
1919         unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1920         unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1921                                                 sbi->log_blocks_per_seg;
1922 
1923         return segs / sbi->segs_per_sec;
1924 }
1925 
1926 static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
1927 {
1928         return sbi->total_valid_block_count;
1929 }
1930 
1931 static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1932 {
1933         return sbi->discard_blks;
1934 }
1935 
1936 static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
1937 {
1938         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1939 
1940         /* return NAT or SIT bitmap */
1941         if (flag == NAT_BITMAP)
1942                 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
1943         else if (flag == SIT_BITMAP)
1944                 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
1945 
1946         return 0;
1947 }
1948 
1949 static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
1950 {
1951         return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
1952 }
1953 
1954 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
1955 {
1956         struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
1957         int offset;
1958 
1959         if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
1960                 offset = (flag == SIT_BITMAP) ?
1961                         le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
1962                 /*
1963                  * if large_nat_bitmap feature is enabled, leave checksum
1964                  * protection for all nat/sit bitmaps.
1965                  */
1966                 return &ckpt->sit_nat_version_bitmap + offset + sizeof(__le32);
1967         }
1968 
1969         if (__cp_payload(sbi) > 0) {
1970                 if (flag == NAT_BITMAP)
1971                         return &ckpt->sit_nat_version_bitmap;
1972                 else
1973                         return (unsigned char *)ckpt + F2FS_BLKSIZE;
1974         } else {
1975                 offset = (flag == NAT_BITMAP) ?
1976                         le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
1977                 return &ckpt->sit_nat_version_bitmap + offset;
1978         }
1979 }
1980 
1981 static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
1982 {
1983         block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
1984 
1985         if (sbi->cur_cp_pack == 2)
1986                 start_addr += sbi->blocks_per_seg;
1987         return start_addr;
1988 }
1989 
1990 static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
1991 {
1992         block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
1993 
1994         if (sbi->cur_cp_pack == 1)
1995                 start_addr += sbi->blocks_per_seg;
1996         return start_addr;
1997 }
1998 
1999 static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
2000 {
2001         sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
2002 }
2003 
2004 static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
2005 {
2006         return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
2007 }
2008 
2009 static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
2010                                         struct inode *inode, bool is_inode)
2011 {
2012         block_t valid_block_count;
2013         unsigned int valid_node_count, user_block_count;
2014         int err;
2015 
2016         if (is_inode) {
2017                 if (inode) {
2018                         err = dquot_alloc_inode(inode);
2019                         if (err)
2020                                 return err;
2021                 }
2022         } else {
2023                 err = dquot_reserve_block(inode, 1);
2024                 if (err)
2025                         return err;
2026         }
2027 
2028         if (time_to_inject(sbi, FAULT_BLOCK)) {
2029                 f2fs_show_injection_info(FAULT_BLOCK);
2030                 goto enospc;
2031         }
2032 
2033         spin_lock(&sbi->stat_lock);
2034 
2035         valid_block_count = sbi->total_valid_block_count +
2036                                         sbi->current_reserved_blocks + 1;
2037 
2038         if (!__allow_reserved_blocks(sbi, inode, false))
2039                 valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
2040         user_block_count = sbi->user_block_count;
2041         if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2042                 user_block_count -= sbi->unusable_block_count;
2043 
2044         if (unlikely(valid_block_count > user_block_count)) {
2045                 spin_unlock(&sbi->stat_lock);
2046                 goto enospc;
2047         }
2048 
2049         valid_node_count = sbi->total_valid_node_count + 1;
2050         if (unlikely(valid_node_count > sbi->total_node_count)) {
2051                 spin_unlock(&sbi->stat_lock);
2052                 goto enospc;
2053         }
2054 
2055         sbi->total_valid_node_count++;
2056         sbi->total_valid_block_count++;
2057         spin_unlock(&sbi->stat_lock);
2058 
2059         if (inode) {
2060                 if (is_inode)
2061                         f2fs_mark_inode_dirty_sync(inode, true);
2062                 else
2063                         f2fs_i_blocks_write(inode, 1, true, true);
2064         }
2065 
2066         percpu_counter_inc(&sbi->alloc_valid_block_count);
2067         return 0;
2068 
2069 enospc:
2070         if (is_inode) {
2071                 if (inode)
2072                         dquot_free_inode(inode);
2073         } else {
2074                 dquot_release_reservation_block(inode, 1);
2075         }
2076         return -ENOSPC;
2077 }
2078 
2079 static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2080                                         struct inode *inode, bool is_inode)
2081 {
2082         spin_lock(&sbi->stat_lock);
2083 
2084         f2fs_bug_on(sbi, !sbi->total_valid_block_count);
2085         f2fs_bug_on(sbi, !sbi->total_valid_node_count);
2086 
2087         sbi->total_valid_node_count--;
2088         sbi->total_valid_block_count--;
2089         if (sbi->reserved_blocks &&
2090                 sbi->current_reserved_blocks < sbi->reserved_blocks)
2091                 sbi->current_reserved_blocks++;
2092 
2093         spin_unlock(&sbi->stat_lock);
2094 
2095         if (is_inode) {
2096                 dquot_free_inode(inode);
2097         } else {
2098                 if (unlikely(inode->i_blocks == 0)) {
2099                         f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu",
2100                                   inode->i_ino,
2101                                   (unsigned long long)inode->i_blocks);
2102                         set_sbi_flag(sbi, SBI_NEED_FSCK);
2103                         return;
2104                 }
2105                 f2fs_i_blocks_write(inode, 1, false, true);
2106         }
2107 }
2108 
2109 static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
2110 {
2111         return sbi->total_valid_node_count;
2112 }
2113 
2114 static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
2115 {
2116         percpu_counter_inc(&sbi->total_valid_inode_count);
2117 }
2118 
2119 static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
2120 {
2121         percpu_counter_dec(&sbi->total_valid_inode_count);
2122 }
2123 
2124 static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
2125 {
2126         return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
2127 }
2128 
2129 static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2130                                                 pgoff_t index, bool for_write)
2131 {
2132         struct page *page;
2133 
2134         if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
2135                 if (!for_write)
2136                         page = find_get_page_flags(mapping, index,
2137                                                         FGP_LOCK | FGP_ACCESSED);
2138                 else
2139                         page = find_lock_page(mapping, index);
2140                 if (page)
2141                         return page;
2142 
2143                 if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
2144                         f2fs_show_injection_info(FAULT_PAGE_ALLOC);
2145                         return NULL;
2146                 }
2147         }
2148 
2149         if (!for_write)
2150                 return grab_cache_page(mapping, index);
2151         return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
2152 }
2153 
2154 static inline struct page *f2fs_pagecache_get_page(
2155                                 struct address_space *mapping, pgoff_t index,
2156                                 int fgp_flags, gfp_t gfp_mask)
2157 {
2158         if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
2159                 f2fs_show_injection_info(FAULT_PAGE_GET);
2160                 return NULL;
2161         }
2162 
2163         return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
2164 }
2165 
2166 static inline void f2fs_copy_page(struct page *src, struct page *dst)
2167 {
2168         char *src_kaddr = kmap(src);
2169         char *dst_kaddr = kmap(dst);
2170 
2171         memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
2172         kunmap(dst);
2173         kunmap(src);
2174 }
2175 
2176 static inline void f2fs_put_page(struct page *page, int unlock)
2177 {
2178         if (!page)
2179                 return;
2180 
2181         if (unlock) {
2182                 f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
2183                 unlock_page(page);
2184         }
2185         put_page(page);
2186 }
2187 
2188 static inline void f2fs_put_dnode(struct dnode_of_data *dn)
2189 {
2190         if (dn->node_page)
2191                 f2fs_put_page(dn->node_page, 1);
2192         if (dn->inode_page && dn->node_page != dn->inode_page)
2193                 f2fs_put_page(dn->inode_page, 0);
2194         dn->node_page = NULL;
2195         dn->inode_page = NULL;
2196 }
2197 
2198 static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2199                                         size_t size)
2200 {
2201         return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
2202 }
2203 
2204 static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
2205                                                 gfp_t flags)
2206 {
2207         void *entry;
2208 
2209         entry = kmem_cache_alloc(cachep, flags);
2210         if (!entry)
2211                 entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
2212         return entry;
2213 }
2214 
2215 static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
2216                                                 int npages, bool no_fail)
2217 {
2218         struct bio *bio;
2219 
2220         if (no_fail) {
2221                 /* No failure on bio allocation */
2222                 bio = bio_alloc(GFP_NOIO, npages);
2223                 if (!bio)
2224                         bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
2225                 return bio;
2226         }
2227         if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
2228                 f2fs_show_injection_info(FAULT_ALLOC_BIO);
2229                 return NULL;
2230         }
2231 
2232         return bio_alloc(GFP_KERNEL, npages);
2233 }
2234 
2235 static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
2236 {
2237         if (sbi->gc_mode == GC_URGENT)
2238                 return true;
2239 
2240         if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
2241                 get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2242                 get_pages(sbi, F2FS_WB_CP_DATA) ||
2243                 get_pages(sbi, F2FS_DIO_READ) ||
2244                 get_pages(sbi, F2FS_DIO_WRITE))
2245                 return false;
2246 
2247         if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
2248                         atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
2249                 return false;
2250 
2251         if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
2252                         atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
2253                 return false;
2254 
2255         return f2fs_time_over(sbi, type);
2256 }
2257 
2258 static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
2259                                 unsigned long index, void *item)
2260 {
2261         while (radix_tree_insert(root, index, item))
2262                 cond_resched();
2263 }
2264 
2265 #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
2266 
2267 static inline bool IS_INODE(struct page *page)
2268 {
2269         struct f2fs_node *p = F2FS_NODE(page);
2270 
2271         return RAW_IS_INODE(p);
2272 }
2273 
2274 static inline int offset_in_addr(struct f2fs_inode *i)
2275 {
2276         return (i->i_inline & F2FS_EXTRA_ATTR) ?
2277                         (le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
2278 }
2279 
2280 static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
2281 {
2282         return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
2283 }
2284 
2285 static inline int f2fs_has_extra_attr(struct inode *inode);
2286 static inline block_t datablock_addr(struct inode *inode,
2287                         struct page *node_page, unsigned int offset)
2288 {
2289         struct f2fs_node *raw_node;
2290         __le32 *addr_array;
2291         int base = 0;
2292         bool is_inode = IS_INODE(node_page);
2293 
2294         raw_node = F2FS_NODE(node_page);
2295 
2296         /* from GC path only */
2297         if (is_inode) {
2298                 if (!inode)
2299                         base = offset_in_addr(&raw_node->i);
2300                 else if (f2fs_has_extra_attr(inode))
2301                         base = get_extra_isize(inode);
2302         }
2303 
2304         addr_array = blkaddr_in_node(raw_node);
2305         return le32_to_cpu(addr_array[base + offset]);
2306 }
2307 
2308 static inline int f2fs_test_bit(unsigned int nr, char *addr)
2309 {
2310         int mask;
2311 
2312         addr += (nr >> 3);
2313         mask = 1 << (7 - (nr & 0x07));
2314         return mask & *addr;
2315 }
2316 
2317 static inline void f2fs_set_bit(unsigned int nr, char *addr)
2318 {
2319         int mask;
2320 
2321         addr += (nr >> 3);
2322         mask = 1 << (7 - (nr & 0x07));
2323         *addr |= mask;
2324 }
2325 
2326 static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2327 {
2328         int mask;
2329 
2330         addr += (nr >> 3);
2331         mask = 1 << (7 - (nr & 0x07));
2332         *addr &= ~mask;
2333 }
2334 
2335 static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
2336 {
2337         int mask;
2338         int ret;
2339 
2340         addr += (nr >> 3);
2341         mask = 1 << (7 - (nr & 0x07));
2342         ret = mask & *addr;
2343         *addr |= mask;
2344         return ret;
2345 }
2346 
2347 static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
2348 {
2349         int mask;
2350         int ret;
2351 
2352         addr += (nr >> 3);
2353         mask = 1 << (7 - (nr & 0x07));
2354         ret = mask & *addr;
2355         *addr &= ~mask;
2356         return ret;
2357 }
2358 
2359 static inline void f2fs_change_bit(unsigned int nr, char *addr)
2360 {
2361         int mask;
2362 
2363         addr += (nr >> 3);
2364         mask = 1 << (7 - (nr & 0x07));
2365         *addr ^= mask;
2366 }
2367 
2368 /*
2369  * On-disk inode flags (f2fs_inode::i_flags)
2370  */
2371 #define F2FS_SYNC_FL                    0x00000008 /* Synchronous updates */
2372 #define F2FS_IMMUTABLE_FL               0x00000010 /* Immutable file */
2373 #define F2FS_APPEND_FL                  0x00000020 /* writes to file may only append */
2374 #define F2FS_NODUMP_FL                  0x00000040 /* do not dump file */
2375 #define F2FS_NOATIME_FL                 0x00000080 /* do not update atime */
2376 #define F2FS_INDEX_FL                   0x00001000 /* hash-indexed directory */
2377 #define F2FS_DIRSYNC_FL                 0x00010000 /* dirsync behaviour (directories only) */
2378 #define F2FS_PROJINHERIT_FL             0x20000000 /* Create with parents projid */
2379 #define F2FS_CASEFOLD_FL                0x40000000 /* Casefolded file */
2380 
2381 /* Flags that should be inherited by new inodes from their parent. */
2382 #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
2383                            F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
2384                            F2FS_CASEFOLD_FL)
2385 
2386 /* Flags that are appropriate for regular files (all but dir-specific ones). */
2387 #define F2FS_REG_FLMASK         (~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
2388                                 F2FS_CASEFOLD_FL))
2389 
2390 /* Flags that are appropriate for non-directories/regular files. */
2391 #define F2FS_OTHER_FLMASK       (F2FS_NODUMP_FL | F2FS_NOATIME_FL)
2392 
2393 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
2394 {
2395         if (S_ISDIR(mode))
2396                 return flags;
2397         else if (S_ISREG(mode))
2398                 return flags & F2FS_REG_FLMASK;
2399         else
2400                 return flags & F2FS_OTHER_FLMASK;
2401 }
2402 
2403 /* used for f2fs_inode_info->flags */
2404 enum {
2405         FI_NEW_INODE,           /* indicate newly allocated inode */
2406         FI_DIRTY_INODE,         /* indicate inode is dirty or not */
2407         FI_AUTO_RECOVER,        /* indicate inode is recoverable */
2408         FI_DIRTY_DIR,           /* indicate directory has dirty pages */
2409         FI_INC_LINK,            /* need to increment i_nlink */
2410         FI_ACL_MODE,            /* indicate acl mode */
2411         FI_NO_ALLOC,            /* should not allocate any blocks */
2412         FI_FREE_NID,            /* free allocated nide */
2413         FI_NO_EXTENT,           /* not to use the extent cache */
2414         FI_INLINE_XATTR,        /* used for inline xattr */
2415         FI_INLINE_DATA,         /* used for inline data*/
2416         FI_INLINE_DENTRY,       /* used for inline dentry */
2417         FI_APPEND_WRITE,        /* inode has appended data */
2418         FI_UPDATE_WRITE,        /* inode has in-place-update data */
2419         FI_NEED_IPU,            /* used for ipu per file */
2420         FI_ATOMIC_FILE,         /* indicate atomic file */
2421         FI_ATOMIC_COMMIT,       /* indicate the state of atomical committing */
2422         FI_VOLATILE_FILE,       /* indicate volatile file */
2423         FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */
2424         FI_DROP_CACHE,          /* drop dirty page cache */
2425         FI_DATA_EXIST,          /* indicate data exists */
2426         FI_INLINE_DOTS,         /* indicate inline dot dentries */
2427         FI_DO_DEFRAG,           /* indicate defragment is running */
2428         FI_DIRTY_FILE,          /* indicate regular/symlink has dirty pages */
2429         FI_NO_PREALLOC,         /* indicate skipped preallocated blocks */
2430         FI_HOT_DATA,            /* indicate file is hot */
2431         FI_EXTRA_ATTR,          /* indicate file has extra attribute */
2432         FI_PROJ_INHERIT,        /* indicate file inherits projectid */
2433         FI_PIN_FILE,            /* indicate file should not be gced */
2434         FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
2435         FI_VERITY_IN_PROGRESS,  /* building fs-verity Merkle tree */
2436 };
2437 
2438 static inline void __mark_inode_dirty_flag(struct inode *inode,
2439                                                 int flag, bool set)
2440 {
2441         switch (flag) {
2442         case FI_INLINE_XATTR:
2443         case FI_INLINE_DATA:
2444         case FI_INLINE_DENTRY:
2445         case FI_NEW_INODE:
2446                 if (set)
2447                         return;
2448                 /* fall through */
2449         case FI_DATA_EXIST:
2450         case FI_INLINE_DOTS:
2451         case FI_PIN_FILE:
2452                 f2fs_mark_inode_dirty_sync(inode, true);
2453         }
2454 }
2455 
2456 static inline void set_inode_flag(struct inode *inode, int flag)
2457 {
2458         if (!test_bit(flag, &F2FS_I(inode)->flags))
2459                 set_bit(flag, &F2FS_I(inode)->flags);
2460         __mark_inode_dirty_flag(inode, flag, true);
2461 }
2462 
2463 static inline int is_inode_flag_set(struct inode *inode, int flag)
2464 {
2465         return test_bit(flag, &F2FS_I(inode)->flags);
2466 }
2467 
2468 static inline void clear_inode_flag(struct inode *inode, int flag)
2469 {
2470         if (test_bit(flag, &F2FS_I(inode)->flags))
2471                 clear_bit(flag, &F2FS_I(inode)->flags);
2472         __mark_inode_dirty_flag(inode, flag, false);
2473 }
2474 
2475 static inline bool f2fs_verity_in_progress(struct inode *inode)
2476 {
2477         return IS_ENABLED(CONFIG_FS_VERITY) &&
2478                is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
2479 }
2480 
2481 static inline void set_acl_inode(struct inode *inode, umode_t mode)
2482 {
2483         F2FS_I(inode)->i_acl_mode = mode;
2484         set_inode_flag(inode, FI_ACL_MODE);
2485         f2fs_mark_inode_dirty_sync(inode, false);
2486 }
2487 
2488 static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2489 {
2490         if (inc)
2491                 inc_nlink(inode);
2492         else
2493                 drop_nlink(inode);
2494         f2fs_mark_inode_dirty_sync(inode, true);
2495 }
2496 
2497 static inline void f2fs_i_blocks_write(struct inode *inode,
2498                                         block_t diff, bool add, bool claim)
2499 {
2500         bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
2501         bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
2502 
2503         /* add = 1, claim = 1 should be dquot_reserve_block in pair */
2504         if (add) {
2505                 if (claim)
2506                         dquot_claim_block(inode, diff);
2507                 else
2508                         dquot_alloc_block_nofail(inode, diff);
2509         } else {
2510                 dquot_free_block(inode, diff);
2511         }
2512 
2513         f2fs_mark_inode_dirty_sync(inode, true);
2514         if (clean || recover)
2515                 set_inode_flag(inode, FI_AUTO_RECOVER);
2516 }
2517 
2518 static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2519 {
2520         bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
2521         bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
2522 
2523         if (i_size_read(inode) == i_size)
2524                 return;
2525 
2526         i_size_write(inode, i_size);
2527         f2fs_mark_inode_dirty_sync(inode, true);
2528         if (clean || recover)
2529                 set_inode_flag(inode, FI_AUTO_RECOVER);
2530 }
2531 
2532 static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2533 {
2534         F2FS_I(inode)->i_current_depth = depth;
2535         f2fs_mark_inode_dirty_sync(inode, true);
2536 }
2537 
2538 static inline void f2fs_i_gc_failures_write(struct inode *inode,
2539                                         unsigned int count)
2540 {
2541         F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
2542         f2fs_mark_inode_dirty_sync(inode, true);
2543 }
2544 
2545 static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2546 {
2547         F2FS_I(inode)->i_xattr_nid = xnid;
2548         f2fs_mark_inode_dirty_sync(inode, true);
2549 }
2550 
2551 static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2552 {
2553         F2FS_I(inode)->i_pino = pino;
2554         f2fs_mark_inode_dirty_sync(inode, true);
2555 }
2556 
2557 static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2558 {
2559         struct f2fs_inode_info *fi = F2FS_I(inode);
2560 
2561         if (ri->i_inline & F2FS_INLINE_XATTR)
2562                 set_bit(FI_INLINE_XATTR, &fi->flags);
2563         if (ri->i_inline & F2FS_INLINE_DATA)
2564                 set_bit(FI_INLINE_DATA, &fi->flags);
2565         if (ri->i_inline & F2FS_INLINE_DENTRY)
2566                 set_bit(FI_INLINE_DENTRY, &fi->flags);
2567         if (ri->i_inline & F2FS_DATA_EXIST)
2568                 set_bit(FI_DATA_EXIST, &fi->flags);
2569         if (ri->i_inline & F2FS_INLINE_DOTS)
2570                 set_bit(FI_INLINE_DOTS, &fi->flags);
2571         if (ri->i_inline & F2FS_EXTRA_ATTR)
2572                 set_bit(FI_EXTRA_ATTR, &fi->flags);
2573         if (ri->i_inline & F2FS_PIN_FILE)
2574                 set_bit(FI_PIN_FILE, &fi->flags);
2575 }
2576 
2577 static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2578 {
2579         ri->i_inline = 0;
2580 
2581         if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2582                 ri->i_inline |= F2FS_INLINE_XATTR;
2583         if (is_inode_flag_set(inode, FI_INLINE_DATA))
2584                 ri->i_inline |= F2FS_INLINE_DATA;
2585         if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
2586                 ri->i_inline |= F2FS_INLINE_DENTRY;
2587         if (is_inode_flag_set(inode, FI_DATA_EXIST))
2588                 ri->i_inline |= F2FS_DATA_EXIST;
2589         if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2590                 ri->i_inline |= F2FS_INLINE_DOTS;
2591         if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
2592                 ri->i_inline |= F2FS_EXTRA_ATTR;
2593         if (is_inode_flag_set(inode, FI_PIN_FILE))
2594                 ri->i_inline |= F2FS_PIN_FILE;
2595 }
2596 
2597 static inline int f2fs_has_extra_attr(struct inode *inode)
2598 {
2599         return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2600 }
2601 
2602 static inline int f2fs_has_inline_xattr(struct inode *inode)
2603 {
2604         return is_inode_flag_set(inode, FI_INLINE_XATTR);
2605 }
2606 
2607 static inline unsigned int addrs_per_inode(struct inode *inode)
2608 {
2609         unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
2610                                 get_inline_xattr_addrs(inode);
2611         return ALIGN_DOWN(addrs, 1);
2612 }
2613 
2614 static inline unsigned int addrs_per_block(struct inode *inode)
2615 {
2616         return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, 1);
2617 }
2618 
2619 static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
2620 {
2621         struct f2fs_inode *ri = F2FS_INODE(page);
2622 
2623         return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2624                                         get_inline_xattr_addrs(inode)]);
2625 }
2626 
2627 static inline int inline_xattr_size(struct inode *inode)
2628 {
2629         if (f2fs_has_inline_xattr(inode))
2630                 return get_inline_xattr_addrs(inode) * sizeof(__le32);
2631         return 0;
2632 }
2633 
2634 static inline int f2fs_has_inline_data(struct inode *inode)
2635 {
2636         return is_inode_flag_set(inode, FI_INLINE_DATA);
2637 }
2638 
2639 static inline int f2fs_exist_data(struct inode *inode)
2640 {
2641         return is_inode_flag_set(inode, FI_DATA_EXIST);
2642 }
2643 
2644 static inline int f2fs_has_inline_dots(struct inode *inode)
2645 {
2646         return is_inode_flag_set(inode, FI_INLINE_DOTS);
2647 }
2648 
2649 static inline bool f2fs_is_pinned_file(struct inode *inode)
2650 {
2651         return is_inode_flag_set(inode, FI_PIN_FILE);
2652 }
2653 
2654 static inline bool f2fs_is_atomic_file(struct inode *inode)
2655 {
2656         return is_inode_flag_set(inode, FI_ATOMIC_FILE);
2657 }
2658 
2659 static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
2660 {
2661         return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
2662 }
2663 
2664 static inline bool f2fs_is_volatile_file(struct inode *inode)
2665 {
2666         return is_inode_flag_set(inode, FI_VOLATILE_FILE);
2667 }
2668 
2669 static inline bool f2fs_is_first_block_written(struct inode *inode)
2670 {
2671         return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
2672 }
2673 
2674 static inline bool f2fs_is_drop_cache(struct inode *inode)
2675 {
2676         return is_inode_flag_set(inode, FI_DROP_CACHE);
2677 }
2678 
2679 static inline void *inline_data_addr(struct inode *inode, struct page *page)
2680 {
2681         struct f2fs_inode *ri = F2FS_INODE(page);
2682         int extra_size = get_extra_isize(inode);
2683 
2684         return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
2685 }
2686 
2687 static inline int f2fs_has_inline_dentry(struct inode *inode)
2688 {
2689         return is_inode_flag_set(inode, FI_INLINE_DENTRY);
2690 }
2691 
2692 static inline int is_file(struct inode *inode, int type)
2693 {
2694         return F2FS_I(inode)->i_advise & type;
2695 }
2696 
2697 static inline void set_file(struct inode *inode, int type)
2698 {
2699         F2FS_I(inode)->i_advise |= type;
2700         f2fs_mark_inode_dirty_sync(inode, true);
2701 }
2702 
2703 static inline void clear_file(struct inode *inode, int type)
2704 {
2705         F2FS_I(inode)->i_advise &= ~type;
2706         f2fs_mark_inode_dirty_sync(inode, true);
2707 }
2708 
2709 static inline bool f2fs_is_time_consistent(struct inode *inode)
2710 {
2711         if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
2712                 return false;
2713         if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
2714                 return false;
2715         if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
2716                 return false;
2717         if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
2718                                                 &F2FS_I(inode)->i_crtime))
2719                 return false;
2720         return true;
2721 }
2722 
2723 static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
2724 {
2725         bool ret;
2726 
2727         if (dsync) {
2728                 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2729 
2730                 spin_lock(&sbi->inode_lock[DIRTY_META]);
2731                 ret = list_empty(&F2FS_I(inode)->gdirty_list);
2732                 spin_unlock(&sbi->inode_lock[DIRTY_META]);
2733                 return ret;
2734         }
2735         if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
2736                         file_keep_isize(inode) ||
2737                         i_size_read(inode) & ~PAGE_MASK)
2738                 return false;
2739 
2740         if (!f2fs_is_time_consistent(inode))
2741                 return false;
2742 
2743         down_read(&F2FS_I(inode)->i_sem);
2744         ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2745         up_read(&F2FS_I(inode)->i_sem);
2746 
2747         return ret;
2748 }
2749 
2750 static inline bool f2fs_readonly(struct super_block *sb)
2751 {
2752         return sb_rdonly(sb);
2753 }
2754 
2755 static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2756 {
2757         return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2758 }
2759 
2760 static inline bool is_dot_dotdot(const struct qstr *str)
2761 {
2762         if (str->len == 1 && str->name[0] == '.')
2763                 return true;
2764 
2765         if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2766                 return true;
2767 
2768         return false;
2769 }
2770 
2771 static inline bool f2fs_may_extent_tree(struct inode *inode)
2772 {
2773         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2774 
2775         if (!test_opt(sbi, EXTENT_CACHE) ||
2776                         is_inode_flag_set(inode, FI_NO_EXTENT))
2777                 return false;
2778 
2779         /*
2780          * for recovered files during mount do not create extents
2781          * if shrinker is not registered.
2782          */
2783         if (list_empty(&sbi->s_list))
2784                 return false;
2785 
2786         return S_ISREG(inode->i_mode);
2787 }
2788 
2789 static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
2790                                         size_t size, gfp_t flags)
2791 {
2792         void *ret;
2793 
2794         if (time_to_inject(sbi, FAULT_KMALLOC)) {
2795                 f2fs_show_injection_info(FAULT_KMALLOC);
2796                 return NULL;
2797         }
2798 
2799         ret = kmalloc(size, flags);
2800         if (ret)
2801                 return ret;
2802 
2803         return kvmalloc(size, flags);
2804 }
2805 
2806 static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2807                                         size_t size, gfp_t flags)
2808 {
2809         return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2810 }
2811 
2812 static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2813                                         size_t size, gfp_t flags)
2814 {
2815         if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2816                 f2fs_show_injection_info(FAULT_KVMALLOC);
2817                 return NULL;
2818         }
2819 
2820         return kvmalloc(size, flags);
2821 }
2822 
2823 static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2824                                         size_t size, gfp_t flags)
2825 {
2826         return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2827 }
2828 
2829 static inline int get_extra_isize(struct inode *inode)
2830 {
2831         return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2832 }
2833 
2834 static inline int get_inline_xattr_addrs(struct inode *inode)
2835 {
2836         return F2FS_I(inode)->i_inline_xattr_size;
2837 }
2838 
2839 #define f2fs_get_inode_mode(i) \
2840         ((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2841          (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2842 
2843 #define F2FS_TOTAL_EXTRA_ATTR_SIZE                      \
2844         (offsetof(struct f2fs_inode, i_extra_end) -     \
2845         offsetof(struct f2fs_inode, i_extra_isize))     \
2846 
2847 #define F2FS_OLD_ATTRIBUTE_SIZE (offsetof(struct f2fs_inode, i_addr))
2848 #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)              \
2849                 ((offsetof(typeof(*(f2fs_inode)), field) +      \
2850                 sizeof((f2fs_inode)->field))                    \
2851                 <= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))   \
2852 
2853 static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2854 {
2855         int i;
2856 
2857         spin_lock(&sbi->iostat_lock);
2858         for (i = 0; i < NR_IO_TYPE; i++)
2859                 sbi->write_iostat[i] = 0;
2860         spin_unlock(&sbi->iostat_lock);
2861 }
2862 
2863 static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2864                         enum iostat_type type, unsigned long long io_bytes)
2865 {
2866         if (!sbi->iostat_enable)
2867                 return;
2868         spin_lock(&sbi->iostat_lock);
2869         sbi->write_iostat[type] += io_bytes;
2870 
2871         if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2872                 sbi->write_iostat[APP_BUFFERED_IO] =
2873                         sbi->write_iostat[APP_WRITE_IO] -
2874                         sbi->write_iostat[APP_DIRECT_IO];
2875         spin_unlock(&sbi->iostat_lock);
2876 }
2877 
2878 #define __is_large_section(sbi)         ((sbi)->segs_per_sec > 1)
2879 
2880 #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
2881 
2882 bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
2883                                         block_t blkaddr, int type);
2884 static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
2885                                         block_t blkaddr, int type)
2886 {
2887         if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
2888                 f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
2889                          blkaddr, type);
2890                 f2fs_bug_on(sbi, 1);
2891         }
2892 }
2893 
2894 static inline bool __is_valid_data_blkaddr(block_t blkaddr)
2895 {
2896         if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
2897                 return false;
2898         return true;
2899 }
2900 
2901 static inline void f2fs_set_page_private(struct page *page,
2902                                                 unsigned long data)
2903 {
2904         if (PagePrivate(page))
2905                 return;
2906 
2907         get_page(page);
2908         SetPagePrivate(page);
2909         set_page_private(page, data);
2910 }
2911 
2912 static inline void f2fs_clear_page_private(struct page *page)
2913 {
2914         if (!PagePrivate(page))
2915                 return;
2916 
2917         set_page_private(page, 0);
2918         ClearPagePrivate(page);
2919         f2fs_put_page(page, 0);
2920 }
2921 
2922 /*
2923  * file.c
2924  */
2925 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
2926 void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
2927 int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
2928 int f2fs_truncate(struct inode *inode);
2929 int f2fs_getattr(const struct path *path, struct kstat *stat,
2930                         u32 request_mask, unsigned int flags);
2931 int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
2932 int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
2933 void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2934 int f2fs_precache_extents(struct inode *inode);
2935 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2936 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
2937 int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
2938 int f2fs_pin_file_control(struct inode *inode, bool inc);
2939 
2940 /*
2941  * inode.c
2942  */
2943 void f2fs_set_inode_flags(struct inode *inode);
2944 bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2945 void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2946 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2947 struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
2948 int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
2949 void f2fs_update_inode(struct inode *inode, struct page *node_page);
2950 void f2fs_update_inode_page(struct inode *inode);
2951 int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2952 void f2fs_evict_inode(struct inode *inode);
2953 void f2fs_handle_failed_inode(struct inode *inode);
2954 
2955 /*
2956  * namei.c
2957  */
2958 int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
2959                                                         bool hot, bool set);
2960 struct dentry *f2fs_get_parent(struct dentry *child);
2961 
2962 extern int f2fs_ci_compare(const struct inode *parent,
2963                            const struct qstr *name,
2964                            const struct qstr *entry,
2965                            bool quick);
2966 
2967 /*
2968  * dir.c
2969  */
2970 unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
2971 struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
2972                         f2fs_hash_t namehash, int *max_slots,
2973                         struct f2fs_dentry_ptr *d);
2974 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2975                         unsigned int start_pos, struct fscrypt_str *fstr);
2976 void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
2977                         struct f2fs_dentry_ptr *d);
2978 struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
2979                         const struct qstr *new_name,
2980                         const struct qstr *orig_name, struct page *dpage);
2981 void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
2982                         unsigned int current_depth);
2983 int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
2984 void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2985 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2986                         struct fscrypt_name *fname, struct page **res_page);
2987 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2988                         const struct qstr *child, struct page **res_page);
2989 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2990 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2991                         struct page **page);
2992 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2993                         struct page *page, struct inode *inode);
2994 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2995                         const struct qstr *name, f2fs_hash_t name_hash,
2996                         unsigned int bit_pos);
2997 int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2998                         const struct qstr *orig_name,
2999                         struct inode *inode, nid_t ino, umode_t mode);
3000 int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
3001                         struct inode *inode, nid_t ino, umode_t mode);
3002 int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
3003                         struct inode *inode, nid_t ino, umode_t mode);
3004 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
3005                         struct inode *dir, struct inode *inode);
3006 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
3007 bool f2fs_empty_dir(struct inode *dir);
3008 
3009 static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
3010 {
3011         return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
3012                                 inode, inode->i_ino, inode->i_mode);
3013 }
3014 
3015 /*
3016  * super.c
3017  */
3018 int f2fs_inode_dirtied(struct inode *inode, bool sync);
3019 void f2fs_inode_synced(struct inode *inode);
3020 int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
3021 int f2fs_quota_sync(struct super_block *sb, int type);
3022 void f2fs_quota_off_umount(struct super_block *sb);
3023 int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
3024 int f2fs_sync_fs(struct super_block *sb, int sync);
3025 int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
3026 
3027 /*
3028  * hash.c
3029  */
3030 f2fs_hash_t f2fs_dentry_hash(const struct inode *dir,
3031                 const struct qstr *name_info, struct fscrypt_name *fname);
3032 
3033 /*
3034  * node.c
3035  */
3036 struct dnode_of_data;
3037 struct node_info;
3038 
3039 int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
3040 bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
3041 bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
3042 void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
3043 void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
3044 void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
3045 int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
3046 bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
3047 bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
3048 int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
3049                                                 struct node_info *ni);
3050 pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
3051 int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
3052 int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
3053 int f2fs_truncate_xattr_node(struct inode *inode);
3054 int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
3055                                         unsigned int seq_id);
3056 int f2fs_remove_inode_page(struct inode *inode);
3057 struct page *f2fs_new_inode_page(struct inode *inode);
3058 struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
3059 void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
3060 struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
3061 struct page *f2fs_get_node_page_ra(struct page *parent, int start);
3062 int f2fs_move_node_page(struct page *node_page, int gc_type);
3063 int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
3064                         struct writeback_control *wbc, bool atomic,
3065                         unsigned int *seq_id);
3066 int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
3067                         struct writeback_control *wbc,
3068                         bool do_balance, enum iostat_type io_type);
3069 int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
3070 bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
3071 void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
3072 void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
3073 int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
3074 void f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
3075 int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
3076 int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
3077 int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3078                         unsigned int segno, struct f2fs_summary_block *sum);
3079 int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3080 int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
3081 void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
3082 int __init f2fs_create_node_manager_caches(void);
3083 void f2fs_destroy_node_manager_caches(void);
3084 
3085 /*
3086  * segment.c
3087  */
3088 bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
3089 void f2fs_register_inmem_page(struct inode *inode, struct page *page);
3090 void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
3091 void f2fs_drop_inmem_pages(struct inode *inode);
3092 void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
3093 int f2fs_commit_inmem_pages(struct inode *inode);
3094 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
3095 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
3096 int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
3097 int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
3098 int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
3099 void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
3100 void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
3101 bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
3102 void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
3103 void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
3104 bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
3105 void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
3106                                         struct cp_control *cpc);
3107 void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
3108 block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
3109 int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
3110 void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
3111 int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
3112 void allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
3113                                         unsigned int start, unsigned int end);
3114 void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3115 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
3116 bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
3117                                         struct cp_control *cpc);
3118 struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
3119 void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
3120                                         block_t blk_addr);
3121 void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3122                                                 enum iostat_type io_type);
3123 void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
3124 void f2fs_outplace_write_data(struct dnode_of_data *dn,
3125                         struct f2fs_io_info *fio);
3126 int f2fs_inplace_write_data(struct f2fs_io_info *fio);
3127 void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3128                         block_t old_blkaddr, block_t new_blkaddr,
3129                         bool recover_curseg, bool recover_newaddr);
3130 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3131                         block_t old_addr, block_t new_addr,
3132                         unsigned char version, bool recover_curseg,
3133                         bool recover_newaddr);
3134 void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3135                         block_t old_blkaddr, block_t *new_blkaddr,
3136                         struct f2fs_summary *sum, int type,
3137                         struct f2fs_io_info *fio, bool add_list);
3138 void f2fs_wait_on_page_writeback(struct page *page,
3139                         enum page_type type, bool ordered, bool locked);
3140 void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
3141 void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
3142                                                                 block_t len);
3143 void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
3144 void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
3145 int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3146                         unsigned int val, int alloc);
3147 void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3148 int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
3149 void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
3150 int __init f2fs_create_segment_manager_caches(void);
3151 void f2fs_destroy_segment_manager_caches(void);
3152 int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
3153 enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
3154                         enum page_type type, enum temp_type temp);
3155 
3156 /*
3157  * checkpoint.c
3158  */
3159 void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
3160 struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
3161 struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
3162 struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index);
3163 struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3164 bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3165                                         block_t blkaddr, int type);
3166 int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3167                         int type, bool sync);
3168 void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
3169 long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3170                         long nr_to_write, enum iostat_type io_type);
3171 void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
3172 void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
3173 void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
3174 bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
3175 void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
3176                                         unsigned int devidx, int type);
3177 bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
3178                                         unsigned int devidx, int type);
3179 int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
3180 int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
3181 void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
3182 void f2fs_add_orphan_inode(struct inode *inode);
3183 void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
3184 int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
3185 int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
3186 void f2fs_update_dirty_page(struct inode *inode, struct page *page);
3187 void f2fs_remove_dirty_inode(struct inode *inode);
3188 int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
3189 void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
3190 int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3191 void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
3192 int __init f2fs_create_checkpoint_caches(void);
3193 void f2fs_destroy_checkpoint_caches(void);
3194 
3195 /*
3196  * data.c
3197  */
3198 int f2fs_init_post_read_processing(void);
3199 void f2fs_destroy_post_read_processing(void);
3200 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3201 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3202                                 struct inode *inode, struct page *page,
3203                                 nid_t ino, enum page_type type);
3204 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3205 int f2fs_submit_page_bio(struct f2fs_io_info *fio);
3206 int f2fs_merge_page_bio(struct f2fs_io_info *fio);
3207 void f2fs_submit_page_write(struct f2fs_io_info *fio);
3208 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
3209                         block_t blk_addr, struct bio *bio);
3210 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
3211 void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3212 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
3213 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
3214 int f2fs_reserve_new_block(struct dnode_of_data *dn);
3215 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3216 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
3217 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
3218 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
3219                         int op_flags, bool for_write);
3220 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
3221 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3222                         bool for_write);
3223 struct page *f2fs_get_new_data_page(struct inode *inode,
3224                         struct page *ipage, pgoff_t index, bool new_i_size);
3225 int f2fs_do_write_data_page(struct f2fs_io_info *fio);
3226 void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock);
3227 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3228                         int create, int flag);
3229 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3230                         u64 start, u64 len);
3231 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
3232 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
3233 void f2fs_invalidate_page(struct page *page, unsigned int offset,
3234                         unsigned int length);
3235 int f2fs_release_page(struct page *page, gfp_t wait);
3236 #ifdef CONFIG_MIGRATION
3237 int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
3238                         struct page *page, enum migrate_mode mode);
3239 #endif
3240 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
3241 void f2fs_clear_page_cache_dirty_tag(struct page *page);
3242 
3243 /*
3244  * gc.c
3245  */
3246 int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
3247 void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
3248 block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
3249 int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
3250                         unsigned int segno);
3251 void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
3252 int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count);
3253 
3254 /*
3255  * recovery.c
3256  */
3257 int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
3258 bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
3259 
3260 /*
3261  * debug.c
3262  */
3263 #ifdef CONFIG_F2FS_STAT_FS
3264 struct f2fs_stat_info {
3265         struct list_head stat_list;
3266         struct f2fs_sb_info *sbi;
3267         int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
3268         int main_area_segs, main_area_sections, main_area_zones;
3269         unsigned long long hit_largest, hit_cached, hit_rbtree;
3270         unsigned long long hit_total, total_ext;
3271         int ext_tree, zombie_tree, ext_node;
3272         int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
3273         int ndirty_data, ndirty_qdata;
3274         int inmem_pages;
3275         unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
3276         int nats, dirty_nats, sits, dirty_sits;
3277         int free_nids, avail_nids, alloc_nids;
3278         int total_count, utilization;
3279         int bg_gc, nr_wb_cp_data, nr_wb_data;
3280         int nr_rd_data, nr_rd_node, nr_rd_meta;
3281         int nr_dio_read, nr_dio_write;
3282         unsigned int io_skip_bggc, other_skip_bggc;
3283         int nr_flushing, nr_flushed, flush_list_empty;
3284         int nr_discarding, nr_discarded;
3285         int nr_discard_cmd;
3286         unsigned int undiscard_blks;
3287         int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3288         int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3289         unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
3290         unsigned int bimodal, avg_vblocks;
3291         int util_free, util_valid, util_invalid;
3292         int rsvd_segs, overp_segs;
3293         int dirty_count, node_pages, meta_pages;
3294         int prefree_count, call_count, cp_count, bg_cp_count;
3295         int tot_segs, node_segs, data_segs, free_segs, free_secs;
3296         int bg_node_segs, bg_data_segs;
3297         int tot_blks, data_blks, node_blks;
3298         int bg_data_blks, bg_node_blks;
3299         unsigned long long skipped_atomic_files[2];
3300         int curseg[NR_CURSEG_TYPE];
3301         int cursec[NR_CURSEG_TYPE];
3302         int curzone[NR_CURSEG_TYPE];
3303 
3304         unsigned int meta_count[META_MAX];
3305         unsigned int segment_count[2];
3306         unsigned int block_count[2];
3307         unsigned int inplace_count;
3308         unsigned long long base_mem, cache_mem, page_mem;
3309 };
3310 
3311 static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3312 {
3313         return (struct f2fs_stat_info *)sbi->stat_info;
3314 }
3315 
3316 #define stat_inc_cp_count(si)           ((si)->cp_count++)
3317 #define stat_inc_bg_cp_count(si)        ((si)->bg_cp_count++)
3318 #define stat_inc_call_count(si)         ((si)->call_count++)
3319 #define stat_inc_bggc_count(sbi)        ((sbi)->bg_gc++)
3320 #define stat_io_skip_bggc_count(sbi)    ((sbi)->io_skip_bggc++)
3321 #define stat_other_skip_bggc_count(sbi) ((sbi)->other_skip_bggc++)
3322 #define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++)
3323 #define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--)
3324 #define stat_inc_total_hit(sbi)         (atomic64_inc(&(sbi)->total_hit_ext))
3325 #define stat_inc_rbtree_node_hit(sbi)   (atomic64_inc(&(sbi)->read_hit_rbtree))
3326 #define stat_inc_largest_node_hit(sbi)  (atomic64_inc(&(sbi)->read_hit_largest))
3327 #define stat_inc_cached_node_hit(sbi)   (atomic64_inc(&(sbi)->read_hit_cached))
3328 #define stat_inc_inline_xattr(inode)                                    \
3329         do {                                                            \
3330                 if (f2fs_has_inline_xattr(inode))                       \
3331                         (atomic_inc(&F2FS_I_SB(inode)->inline_xattr));  \
3332         } while (0)
3333 #define stat_dec_inline_xattr(inode)                                    \
3334         do {                                                            \
3335                 if (f2fs_has_inline_xattr(inode))                       \
3336                         (atomic_dec(&F2FS_I_SB(inode)->inline_xattr));  \
3337         } while (0)
3338 #define stat_inc_inline_inode(inode)                                    \
3339         do {                                                            \
3340                 if (f2fs_has_inline_data(inode))                        \
3341                         (atomic_inc(&F2FS_I_SB(inode)->inline_inode));  \
3342         } while (0)
3343 #define stat_dec_inline_inode(inode)                                    \
3344         do {                                                            \
3345                 if (f2fs_has_inline_data(inode))                        \
3346                         (atomic_dec(&F2FS_I_SB(inode)->inline_inode));  \
3347         } while (0)
3348 #define stat_inc_inline_dir(inode)                                      \
3349         do {                                                            \
3350                 if (f2fs_has_inline_dentry(inode))                      \
3351                         (atomic_inc(&F2FS_I_SB(inode)->inline_dir));    \
3352         } while (0)
3353 #define stat_dec_inline_dir(inode)                                      \
3354         do {                                                            \
3355                 if (f2fs_has_inline_dentry(inode))                      \
3356                         (atomic_dec(&F2FS_I_SB(inode)->inline_dir));    \
3357         } while (0)
3358 #define stat_inc_meta_count(sbi, blkaddr)                               \
3359         do {                                                            \
3360                 if (blkaddr < SIT_I(sbi)->sit_base_addr)                \
3361                         atomic_inc(&(sbi)->meta_count[META_CP]);        \
3362                 else if (blkaddr < NM_I(sbi)->nat_blkaddr)              \
3363                         atomic_inc(&(sbi)->meta_count[META_SIT]);       \
3364                 else if (blkaddr < SM_I(sbi)->ssa_blkaddr)              \
3365                         atomic_inc(&(sbi)->meta_count[META_NAT]);       \
3366                 else if (blkaddr < SM_I(sbi)->main_blkaddr)             \
3367                         atomic_inc(&(sbi)->meta_count[META_SSA]);       \
3368         } while (0)
3369 #define stat_inc_seg_type(sbi, curseg)                                  \
3370                 ((sbi)->segment_count[(curseg)->alloc_type]++)
3371 #define stat_inc_block_count(sbi, curseg)                               \
3372                 ((sbi)->block_count[(curseg)->alloc_type]++)
3373 #define stat_inc_inplace_blocks(sbi)                                    \
3374                 (atomic_inc(&(sbi)->inplace_count))
3375 #define stat_inc_atomic_write(inode)                                    \
3376                 (atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
3377 #define stat_dec_atomic_write(inode)                                    \
3378                 (atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
3379 #define stat_update_max_atomic_write(inode)                             \
3380         do {                                                            \
3381                 int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);       \
3382                 int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);   \
3383                 if (cur > max)                                          \
3384                         atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur); \
3385         } while (0)
3386 #define stat_inc_volatile_write(inode)                                  \
3387                 (atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3388 #define stat_dec_volatile_write(inode)                                  \
3389                 (atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3390 #define stat_update_max_volatile_write(inode)                           \
3391         do {                                                            \
3392                 int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);       \
3393                 int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);   \
3394                 if (cur > max)                                          \
3395                         atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur); \
3396         } while (0)
3397 #define stat_inc_seg_count(sbi, type, gc_type)                          \
3398         do {                                                            \
3399                 struct f2fs_stat_info *si = F2FS_STAT(sbi);             \
3400                 si->tot_segs++;                                         \
3401                 if ((type) == SUM_TYPE_DATA) {                          \
3402                         si->data_segs++;                                \
3403                         si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
3404                 } else {                                                \
3405                         si->node_segs++;                                \
3406                         si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
3407                 }                                                       \
3408         } while (0)
3409 
3410 #define stat_inc_tot_blk_count(si, blks)                                \
3411         ((si)->tot_blks += (blks))
3412 
3413 #define stat_inc_data_blk_count(sbi, blks, gc_type)                     \
3414         do {                                                            \
3415                 struct f2fs_stat_info *si = F2FS_STAT(sbi);             \
3416                 stat_inc_tot_blk_count(si, blks);                       \
3417                 si->data_blks += (blks);                                \
3418                 si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;  \
3419         } while (0)
3420 
3421 #define stat_inc_node_blk_count(sbi, blks, gc_type)                     \
3422         do {                                                            \
3423                 struct f2fs_stat_info *si = F2FS_STAT(sbi);             \
3424                 stat_inc_tot_blk_count(si, blks);                       \
3425                 si->node_blks += (blks);                                \
3426                 si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;  \
3427         } while (0)
3428 
3429 int f2fs_build_stats(struct f2fs_sb_info *sbi);
3430 void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
3431 void __init f2fs_create_root_stats(void);
3432 void f2fs_destroy_root_stats(void);
3433 #else
3434 #define stat_inc_cp_count(si)                           do { } while (0)
3435 #define stat_inc_bg_cp_count(si)                        do { } while (0)
3436 #define stat_inc_call_count(si)                         do { } while (0)
3437 #define stat_inc_bggc_count(si)                         do { } while (0)
3438 #define stat_io_skip_bggc_count(sbi)                    do { } while (0)
3439 #define stat_other_skip_bggc_count(sbi)                 do { } while (0)
3440 #define stat_inc_dirty_inode(sbi, type)                 do { } while (0)
3441 #define stat_dec_dirty_inode(sbi, type)                 do { } while (0)
3442 #define stat_inc_total_hit(sb)                          do { } while (0)
3443 #define stat_inc_rbtree_node_hit(sb)                    do { } while (0)
3444 #define stat_inc_largest_node_hit(sbi)                  do { } while (0)
3445 #define stat_inc_cached_node_hit(sbi)                   do { } while (0)
3446 #define stat_inc_inline_xattr(inode)                    do { } while (0)
3447 #define stat_dec_inline_xattr(inode)                    do { } while (0)
3448 #define stat_inc_inline_inode(inode)                    do { } while (0)
3449 #define stat_dec_inline_inode(inode)                    do { } while (0)
3450 #define stat_inc_inline_dir(inode)                      do { } while (0)
3451 #define stat_dec_inline_dir(inode)                      do { } while (0)
3452 #define stat_inc_atomic_write(inode)                    do { } while (0)
3453 #define stat_dec_atomic_write(inode)                    do { } while (0)
3454 #define stat_update_max_atomic_write(inode)             do { } while (0)
3455 #define stat_inc_volatile_write(inode)                  do { } while (0)
3456 #define stat_dec_volatile_write(inode)                  do { } while (0)
3457 #define stat_update_max_volatile_write(inode)           do { } while (0)
3458 #define stat_inc_meta_count(sbi, blkaddr)               do { } while (0)
3459 #define stat_inc_seg_type(sbi, curseg)                  do { } while (0)
3460 #define stat_inc_block_count(sbi, curseg)               do { } while (0)
3461 #define stat_inc_inplace_blocks(sbi)                    do { } while (0)
3462 #define stat_inc_seg_count(sbi, type, gc_type)          do { } while (0)
3463 #define stat_inc_tot_blk_count(si, blks)                do { } while (0)
3464 #define stat_inc_data_blk_count(sbi, blks, gc_type)     do { } while (0)
3465 #define stat_inc_node_blk_count(sbi, blks, gc_type)     do { } while (0)
3466 
3467 static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
3468 static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
3469 static inline void __init f2fs_create_root_stats(void) { }
3470 static inline void f2fs_destroy_root_stats(void) { }
3471 #endif
3472 
3473 extern const struct file_operations f2fs_dir_operations;
3474 #ifdef CONFIG_UNICODE
3475 extern const struct dentry_operations f2fs_dentry_ops;
3476 #endif
3477 extern const struct file_operations f2fs_file_operations;
3478 extern const struct inode_operations f2fs_file_inode_operations;
3479 extern const struct address_space_operations f2fs_dblock_aops;
3480 extern const struct address_space_operations f2fs_node_aops;
3481 extern const struct address_space_operations f2fs_meta_aops;
3482 extern const struct inode_operations f2fs_dir_inode_operations;
3483 extern const struct inode_operations f2fs_symlink_inode_operations;
3484 extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
3485 extern const struct inode_operations f2fs_special_inode_operations;
3486 extern struct kmem_cache *f2fs_inode_entry_slab;
3487 
3488 /*
3489  * inline.c
3490  */
3491 bool f2fs_may_inline_data(struct inode *inode);
3492 bool f2fs_may_inline_dentry(struct inode *inode);
3493 void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
3494 void f2fs_truncate_inline_inode(struct inode *inode,
3495                                                 struct page *ipage, u64 from);
3496 int f2fs_read_inline_data(struct inode *inode, struct page *page);
3497 int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3498 int f2fs_convert_inline_inode(struct inode *inode);
3499 int f2fs_write_inline_data(struct inode *inode, struct page *page);
3500 bool f2fs_recover_inline_data(struct inode *inode, struct page *npage);
3501 struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
3502                         struct fscrypt_name *fname, struct page **res_page);
3503 int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
3504                         struct page *ipage);
3505 int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3506                         const struct qstr *orig_name,
3507                         struct inode *inode, nid_t ino, umode_t mode);
3508 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
3509                                 struct page *page, struct inode *dir,
3510                                 struct inode *inode);
3511 bool f2fs_empty_inline_dir(struct inode *dir);
3512 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3513                         struct fscrypt_str *fstr);
3514 int f2fs_inline_data_fiemap(struct inode *inode,
3515                         struct fiemap_extent_info *fieinfo,
3516                         __u64 start, __u64 len);
3517 
3518 /*
3519  * shrinker.c
3520  */
3521 unsigned long f2fs_shrink_count(struct shrinker *shrink,
3522                         struct shrink_control *sc);
3523 unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3524                         struct shrink_control *sc);
3525 void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3526 void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
3527 
3528 /*
3529  * extent_cache.c
3530  */
3531 struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
3532                                 struct rb_entry *cached_re, unsigned int ofs);
3533 struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
3534                                 struct rb_root_cached *root,
3535                                 struct rb_node **parent,
3536                                 unsigned int ofs, bool *leftmost);
3537 struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
3538                 struct rb_entry *cached_re, unsigned int ofs,
3539                 struct rb_entry **prev_entry, struct rb_entry **next_entry,
3540                 struct rb_node ***insert_p, struct rb_node **insert_parent,
3541                 bool force, bool *leftmost);
3542 bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
3543                                                 struct rb_root_cached *root);
3544 unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3545 bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3546 void f2fs_drop_extent_tree(struct inode *inode);
3547 unsigned int f2fs_destroy_extent_node(struct inode *inode);
3548 void f2fs_destroy_extent_tree(struct inode *inode);
3549 bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3550                         struct extent_info *ei);
3551 void f2fs_update_extent_cache(struct dnode_of_data *dn);
3552 void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3553                         pgoff_t fofs, block_t blkaddr, unsigned int len);
3554 void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
3555 int __init f2fs_create_extent_cache(void);
3556 void f2fs_destroy_extent_cache(void);
3557 
3558 /*
3559  * sysfs.c
3560  */
3561 int __init f2fs_init_sysfs(void);
3562 void f2fs_exit_sysfs(void);
3563 int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3564 void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
3565 
3566 /* verity.c */
3567 extern const struct fsverity_operations f2fs_verityops;
3568 
3569 /*
3570  * crypto support
3571  */
3572 static inline bool f2fs_encrypted_file(struct inode *inode)
3573 {
3574         return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
3575 }
3576 
3577 static inline void f2fs_set_encrypted_inode(struct inode *inode)
3578 {
3579 #ifdef CONFIG_FS_ENCRYPTION
3580         file_set_encrypt(inode);
3581         f2fs_set_inode_flags(inode);
3582 #endif
3583 }
3584 
3585 /*
3586  * Returns true if the reads of the inode's data need to undergo some
3587  * postprocessing step, like decryption or authenticity verification.
3588  */
3589 static inline bool f2fs_post_read_required(struct inode *inode)
3590 {
3591         return f2fs_encrypted_file(inode) || fsverity_active(inode);
3592 }
3593 
3594 #define F2FS_FEATURE_FUNCS(name, flagname) \
3595 static inline int f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
3596 { \
3597         return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
3598 }
3599 
3600 F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3601 F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3602 F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3603 F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3604 F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3605 F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3606 F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3607 F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
3608 F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
3609 F2FS_FEATURE_FUNCS(verity, VERITY);
3610 F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
3611 F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
3612 
3613 #ifdef CONFIG_BLK_DEV_ZONED
3614 static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
3615                                     block_t blkaddr)
3616 {
3617         unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
3618 
3619         return test_bit(zno, FDEV(devi).blkz_seq);
3620 }
3621 #endif
3622 
3623 static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
3624 {
3625         return f2fs_sb_has_blkzoned(sbi);
3626 }
3627 
3628 static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
3629 {
3630         return blk_queue_discard(bdev_get_queue(bdev)) ||
3631                bdev_is_zoned(bdev);
3632 }
3633 
3634 static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
3635 {
3636         int i;
3637 
3638         if (!f2fs_is_multi_device(sbi))
3639                 return f2fs_bdev_support_discard(sbi->sb->s_bdev);
3640 
3641         for (i = 0; i < sbi->s_ndevs; i++)
3642                 if (f2fs_bdev_support_discard(FDEV(i).bdev))
3643                         return true;
3644         return false;
3645 }
3646 
3647 static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
3648 {
3649         return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
3650                                         f2fs_hw_should_discard(sbi);
3651 }
3652 
3653 static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
3654 {
3655         int i;
3656 
3657         if (!f2fs_is_multi_device(sbi))
3658                 return bdev_read_only(sbi->sb->s_bdev);
3659 
3660         for (i = 0; i < sbi->s_ndevs; i++)
3661                 if (bdev_read_only(FDEV(i).bdev))
3662                         return true;
3663         return false;
3664 }
3665 
3666 
3667 static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
3668 {
3669         clear_opt(sbi, ADAPTIVE);
3670         clear_opt(sbi, LFS);
3671 
3672         switch (mt) {
3673         case F2FS_MOUNT_ADAPTIVE:
3674                 set_opt(sbi, ADAPTIVE);
3675                 break;
3676         case F2FS_MOUNT_LFS:
3677                 set_opt(sbi, LFS);
3678                 break;
3679         }
3680 }
3681 
3682 static inline bool f2fs_may_encrypt(struct inode *inode)
3683 {
3684 #ifdef CONFIG_FS_ENCRYPTION
3685         umode_t mode = inode->i_mode;
3686 
3687         return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3688 #else
3689         return false;
3690 #endif
3691 }
3692 
3693 static inline int block_unaligned_IO(struct inode *inode,
3694                                 struct kiocb *iocb, struct iov_iter *iter)
3695 {
3696         unsigned int i_blkbits = READ_ONCE(inode->i_blkbits);
3697         unsigned int blocksize_mask = (1 << i_blkbits) - 1;
3698         loff_t offset = iocb->ki_pos;
3699         unsigned long align = offset | iov_iter_alignment(iter);
3700 
3701         return align & blocksize_mask;
3702 }
3703 
3704 static inline int allow_outplace_dio(struct inode *inode,
3705                                 struct kiocb *iocb, struct iov_iter *iter)
3706 {
3707         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3708         int rw = iov_iter_rw(iter);
3709 
3710         return (test_opt(sbi, LFS) && (rw == WRITE) &&
3711                                 !block_unaligned_IO(inode, iocb, iter));
3712 }
3713 
3714 static inline bool f2fs_force_buffered_io(struct inode *inode,
3715                                 struct kiocb *iocb, struct iov_iter *iter)
3716 {
3717         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3718         int rw = iov_iter_rw(iter);
3719 
3720         if (f2fs_post_read_required(inode))
3721                 return true;
3722         if (f2fs_is_multi_device(sbi))
3723                 return true;
3724         /*
3725          * for blkzoned device, fallback direct IO to buffered IO, so
3726          * all IOs can be serialized by log-structured write.
3727          */
3728         if (f2fs_sb_has_blkzoned(sbi))
3729                 return true;
3730         if (test_opt(sbi, LFS) && (rw == WRITE)) {
3731                 if (block_unaligned_IO(inode, iocb, iter))
3732                         return true;
3733                 if (F2FS_IO_ALIGNED(sbi))
3734                         return true;
3735         }
3736         if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED) &&
3737                                         !IS_SWAPFILE(inode))
3738                 return true;
3739 
3740         return false;
3741 }
3742 
3743 #ifdef CONFIG_F2FS_FAULT_INJECTION
3744 extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
3745                                                         unsigned int type);
3746 #else
3747 #define f2fs_build_fault_attr(sbi, rate, type)          do { } while (0)
3748 #endif
3749 
3750 static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
3751 {
3752 #ifdef CONFIG_QUOTA
3753         if (f2fs_sb_has_quota_ino(sbi))
3754                 return true;
3755         if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
3756                 F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
3757                 F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
3758                 return true;
3759 #endif
3760         return false;
3761 }
3762 
3763 #define EFSBADCRC       EBADMSG         /* Bad CRC detected */
3764 #define EFSCORRUPTED    EUCLEAN         /* Filesystem is corrupted */
3765 
3766 #endif /* _LINUX_F2FS_H */

/* [<][>][^][v][top][bottom][index][help] */